From patchwork Thu Sep 26 00:05:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 15303 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 25821449439 for ; Thu, 26 Sep 2019 03:06:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 019FB689E00; Thu, 26 Sep 2019 03:06:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C8F45688309 for ; Thu, 26 Sep 2019 03:06:13 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id h7so302605wrw.8 for ; Wed, 25 Sep 2019 17:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2uuJki6jvvKfV7PWKnC9C5qZuwxXZEpOcu443GgmJAw=; b=TJ34lcJ0tjMmlGkMXCB01OAfj4a3PoW/lq+9KZflPgvkm9MLN/qGmaves8y6sikP8u eJ4FbUguodC67isC7Z7BMw4Ey0Dv1o/aTvGsnpzmTsPz8awdvoaW4SU8Vk9CWqzyGcWk P0B+l0DHBXd6pKXEKSSqPsy4Z/O+a4gVM7xKbGfvS3tf9JD5KMeYxffFtAe06WpkqRJ+ qJpDQsn4ivAivWVaarE1EAweU4eMDFFJWjGV5LAMXUqadQwd0lvqkljAlp+PTVtO8MSQ 3r2a6qr/YwuleQHhSL3wqiHStVcc1Bf7V7Y1QyJq6sDEKspXshC5UtEQNPh8RGvpGGAV 49Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2uuJki6jvvKfV7PWKnC9C5qZuwxXZEpOcu443GgmJAw=; b=jpLOUPcBLGtRy56TRHwnPky5zaDVLz0cGpMQeOPUwz1kGzAGN0hMeAje8mGoXPNFUa /Kh84+EduJecBcXNSV/MLYOHqwMPLIf8QmukbkzqakLNYXe3CZKDf3V/U3d21WfC3ptU 7huKF2yAZPUW38MA+QTF14hLPRlPJr7Bos3fPkoPdkfuKjbmgjigAfE4JNjAAcuUTqwm qZNyIv9/CqcP+DZrosGIoHmDPN3PltSR4n40yGf1TNyzmor2oVJFJmReYM3PNU8176bK RUALcBMyPHaiDeKIBYFOsvxV4LKuop3Y3/Ybz1mZGAOpSZn58FoqVDRsjMZKJoQG2vAt aGuA== X-Gm-Message-State: APjAAAVpb8NwNW6JMaQ03wBCZA1Pi++5vFqfXbDOEUioXcIKodDw905R aePDEioX0Ih+xX5tz63tXcGvuiu8 X-Google-Smtp-Source: APXvYqyZr3o0h3fqV2dARn99kaz4HkWT2r3Ne+M8U59S/Yb4NJuJ0lRGMkuUoReKi7+zGWoCLnkQgg== X-Received: by 2002:a5d:4c48:: with SMTP id n8mr591665wrt.192.1569456373116; Wed, 25 Sep 2019 17:06:13 -0700 (PDT) Received: from localhost.localdomain (ipbcc0f857.dynamic.kabel-deutschland.de. [188.192.248.87]) by smtp.gmail.com with ESMTPSA id f13sm446387wmj.17.2019.09.25.17.06.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2019 17:06:12 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 26 Sep 2019 02:05:58 +0200 Message-Id: <20190926000558.8410-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <5e998549-8df1-d619-cd32-a834adcb79f2@gmail.com> References: <5e998549-8df1-d619-cd32-a834adcb79f2@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/utils: Move the reference to the packet list X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Up until now, ff_packet_list_put had a flaw: When it moved a packet to the list (meaning, when it ought to move the reference to the packet list instead of creating a new one via av_packet_ref), it did not reset the original packet, confusing the ownership of the data in the packet. This has been done because some callers of this function were not compatible with resetting the packet. This commit changes these callers and fixes this flaw. In order to indicate that the ownership of the packet has moved to the packet list, pointers to constant AVPackets are used whenever the target of the pointer might already be owned by the packet list. Furthermore, the packet is always made refcounted so that its data is really owned by the packet. This was already true for the current callers. Signed-off-by: Andreas Rheinhardt --- libavformat/internal.h | 3 ++- libavformat/utils.c | 37 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/libavformat/internal.h b/libavformat/internal.h index 163587f416..a37404d823 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -763,7 +763,8 @@ void ff_format_set_url(AVFormatContext *s, char *url); * * @param head List head element * @param tail List tail element - * @param pkt The packet being appended + * @param pkt The packet being appended. It will be made refcounted + * if it isn't already. * @param flags Any combination of FF_PACKETLIST_FLAG_* flags * @return 0 on success, negative AVERROR value on failure. On failure, the list is unchanged diff --git a/libavformat/utils.c b/libavformat/utils.c index 4657ba2642..9f8a5bfb63 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -460,10 +460,12 @@ int ff_packet_list_put(AVPacketList **packet_buffer, return ret; } } else { - // TODO: Adapt callers in this file so the line below can use - // av_packet_move_ref() to effectively move the reference - // to the list. - pktl->pkt = *pkt; + ret = av_packet_make_refcounted(pkt); + if (ret < 0) { + av_free(pktl); + return ret; + } + av_packet_move_ref(&pktl->pkt, pkt); } if (*packet_buffer) @@ -835,6 +837,7 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) for (;;) { AVPacketList *pktl = s->internal->raw_packet_buffer; + const AVPacket *pkt1; if (pktl) { st = s->streams[pktl->pkt.stream_index]; @@ -922,9 +925,10 @@ int ff_read_packet(AVFormatContext *s, AVPacket *pkt) av_packet_unref(pkt); return err; } - s->internal->raw_packet_buffer_remaining_size -= pkt->size; + pkt1 = &s->internal->raw_packet_buffer_end->pkt; + s->internal->raw_packet_buffer_remaining_size -= pkt1->size; - if ((err = probe_codec(s, st, pkt)) < 0) + if ((err = probe_codec(s, st, pkt1)) < 0) return err; } } @@ -3032,8 +3036,8 @@ static int has_codec_parameters(AVStream *st, const char **errmsg_ptr) } /* returns 1 or 0 if or if not decoded data was returned, or a negative error */ -static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt, - AVDictionary **options) +static int try_decode_frame(AVFormatContext *s, AVStream *st, + const AVPacket *avpkt, AVDictionary **options) { AVCodecContext *avctx = st->internal->avctx; const AVCodec *codec; @@ -3525,7 +3529,7 @@ fail: return ret; } -static int extract_extradata(AVStream *st, AVPacket *pkt) +static int extract_extradata(AVStream *st, const AVPacket *pkt) { AVStreamInternal *sti = st->internal; AVPacket *pkt_ref; @@ -3588,7 +3592,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) int64_t read_size; AVStream *st; AVCodecContext *avctx; - AVPacket pkt1, *pkt; + AVPacket pkt1; int64_t old_offset = avio_tell(ic->pb); // new streams might appear, no options for those int orig_nb_streams = ic->nb_streams; @@ -3707,6 +3711,7 @@ FF_ENABLE_DEPRECATION_WARNINGS read_size = 0; for (;;) { + const AVPacket *pkt; int analyzed_all_streams; if (ff_check_interrupt(&ic->interrupt_callback)) { ret = AVERROR_EXIT; @@ -3800,14 +3805,16 @@ FF_ENABLE_DEPRECATION_WARNINGS break; } - pkt = &pkt1; - if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) { ret = ff_packet_list_put(&ic->internal->packet_buffer, &ic->internal->packet_buffer_end, - pkt, 0); + &pkt1, 0); if (ret < 0) goto find_stream_info_err; + + pkt = &ic->internal->packet_buffer_end->pkt; + } else { + pkt = &pkt1; } st = ic->streams[pkt->stream_index]; @@ -3885,7 +3892,7 @@ FF_ENABLE_DEPRECATION_WARNINGS limit, t, pkt->stream_index); if (ic->flags & AVFMT_FLAG_NOBUFFER) - av_packet_unref(pkt); + av_packet_unref(&pkt1); break; } if (pkt->duration) { @@ -3922,7 +3929,7 @@ FF_ENABLE_DEPRECATION_WARNINGS (options && i < orig_nb_streams) ? &options[i] : NULL); if (ic->flags & AVFMT_FLAG_NOBUFFER) - av_packet_unref(pkt); + av_packet_unref(&pkt1); st->codec_info_nb_frames++; count++;