From patchwork Sat Apr 11 21:19:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18875 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 766B444A8DB for ; Sun, 12 Apr 2020 00:20:16 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5A1E368B816; Sun, 12 Apr 2020 00:20:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 60C9168B0BE for ; Sun, 12 Apr 2020 00:20:10 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id z6so6169135wml.2 for ; Sat, 11 Apr 2020 14:20:10 -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:mime-version :content-transfer-encoding; bh=oTSwzZoTK2ViCcqkq1Qbq96M4z5KzqMJKMR0O2WdV18=; b=h3FXW3bv+SjRH4BVMKE+uZVJMUbetUzoqMc6PpXnS/P72KMyRI2YupUr4ZHjUdXy3K ppMCzPKkpCO6fvc/1cruHkAaoRJROkPd25v+wkrT55aIQyD0OptiAJOxJJCv6YOJixsO 6aRun16G4GMb9DIbcTors2RsqXJCUjT+r3Lt843tows3ywkKNCaX5jBLQDO4kGu6xt83 BPy7LDdRgf9h0vH/hGZcXeAzEn9BRoN3dykU9HVbcEzRiMFzYNWkmKo1lWAlpeqgRNBn 1VtL5hNhI8tQXS+MKc+7ju+c4pGvs/a9zHLo22Lh3c+27PGMSkCkQ7jwDzpfEsEcBEmw 2bQA== 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:mime-version :content-transfer-encoding; bh=oTSwzZoTK2ViCcqkq1Qbq96M4z5KzqMJKMR0O2WdV18=; b=Fkz2j9sbM3Cn4MFx9jaexpeYLlXl4ZoXu596UjKI0XGlXrVL51rso6lqfq09BQDSe5 lYDbGJimjs9EE1klr0ULI2nrrtIFS2gUtoFz9NpuXlUZQyEDnXvWI7qBvSruonmYGe+P F96LElEeyRxLBkDD87CmJKdiYodYC68C3ioR8YxXshdLc9qPKPa4UP3jSAnmOK0T+ctE R1c/eTtgSQRuQEi6w2DQGTEGjJEuuH96PpIEe8pjFMn0ORglnst7DMCq/XYGdS4dvIu6 XSoUlmUF2b+iAQuEz9KUJ0HBSMqVkUR+YR1lM1qRzXj1UKR7/KmXQeZqOYAbiA5ZfCOa 4SRQ== X-Gm-Message-State: AGi0PuZFxme39VyQA/TvnmpD+S0U4tU5YfgJuU0POOzIhSfVhe+xUjGB igel+lfsRvOO8JCBcaQdpz1Ab3iw X-Google-Smtp-Source: APiQypKDei7mIrKXTII26f0HnjXRLHd9+QDnT2OV2rn5wv0R8QGcap7c6gpq5L/OvSP60tdbH/kzOg== X-Received: by 2002:a1c:23d6:: with SMTP id j205mr11229128wmj.22.1586640009400; Sat, 11 Apr 2020 14:20:09 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:08 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:50 +0200 Message-Id: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/6] avformat/mux: Make uncoded frames av_packet_unref() compatible 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" Currently uncoded frames (i.e. packets whose data actually points to an AVFrame) are not refcounted. As a consequence, calling av_packet_unref() on them will not free them, but may simply make sure that they leak by losing the pointer to the frame. This commit changes this by mimicking what is being done for wrapped AVFrames: Ownership of the AVFrame is passed to a special AVBuffer with a custom free function that properly frees the AVFrame. The packet is equipped with an AVBufferRef referencing this AVBuffer. Thereby the packet becomes compatible with av_packet_unref(). This already has three advantages, all in interleaved mode: 1. If an error happens at the preparatory steps (before the packet is put into the interleavement queue), the frame is properly freed. 2. If the trailer is never written, the frames still in the interleavement queue will now be properly freed by ff_packet_list_free(). 3. The custom code for moving the packet to the packet list in ff_interleave_add_packet() can be removed. It will also simplify fixing further memleaks in future commits. Given that the AVFrame is now owned by an AVBuffer, the muxer may no longer take ownership of the AVFrame, because the function used to call the muxer when writing uncoded frames does not allow to transfer ownership of the reference contained in the packet. (Changing the function signature is not possible (except at a major version bump), because most of these muxers reside in libavdevice.) But this is no loss as none of the muxers ever made use of this. This change has been documented. Signed-off-by: Andreas Rheinhardt --- The new semantic of AVOutputFormat.write_uncoded_frame() basically boils down to treat frame as AVFrame * const *. I wonder whether I should simply set it that way and remove the then redundant comment. libavformat/avformat.h | 4 ++-- libavformat/mux.c | 43 ++++++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 39b99b4481..89207b9823 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -578,8 +578,8 @@ typedef struct AVOutputFormat { * * See av_write_uncoded_frame() for details. * - * The library will free *frame afterwards, but the muxer can prevent it - * by setting the pointer to NULL. + * The muxer must not change *frame, but it can keep the content of **frame + * by av_frame_move_ref(). */ int (*write_uncoded_frame)(struct AVFormatContext *, int stream_index, AVFrame **frame, unsigned flags); diff --git a/libavformat/mux.c b/libavformat/mux.c index cc2d1e275a..712ba0c319 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -550,12 +550,6 @@ fail: #define AV_PKT_FLAG_UNCODED_FRAME 0x2000 -/* Note: using sizeof(AVFrame) from outside lavu is unsafe in general, but - it is only being used internally to this file as a consistency check. - The value is chosen to be very unlikely to appear on its own and to cause - immediate failure if used anywhere as a real size. */ -#define UNCODED_FRAME_PACKET_SIZE (INT_MIN / 3 * 2 + (int)sizeof(AVFrame)) - #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX FF_DISABLE_DEPRECATION_WARNINGS @@ -747,9 +741,10 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) { AVFrame *frame = (AVFrame *)pkt->data; - av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE); + av_assert0(pkt->size == sizeof(*frame)); ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0); - av_frame_free(&frame); + av_assert0((void*)frame == pkt->data); + av_packet_unref(pkt); } else { ret = s->oformat->write_packet(s, pkt); } @@ -926,14 +921,9 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, this_pktl = av_malloc(sizeof(AVPacketList)); if (!this_pktl) return AVERROR(ENOMEM); - if (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) { - av_assert0(pkt->size == UNCODED_FRAME_PACKET_SIZE); - av_assert0(((AVFrame *)pkt->data)->buf); - } else { - if ((ret = av_packet_make_refcounted(pkt)) < 0) { - av_free(this_pktl); - return ret; - } + if ((ret = av_packet_make_refcounted(pkt)) < 0) { + av_free(this_pktl); + return ret; } av_packet_move_ref(&this_pktl->pkt, pkt); @@ -1319,22 +1309,39 @@ int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, return ret; } +static void uncoded_frame_free(void *unused, uint8_t *data) +{ + AVFrame *frame = (AVFrame *)data; + + av_frame_free(&frame); +} + static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, AVFrame *frame, int interleaved) { AVPacket pkt, *pktp; av_assert0(s->oformat); - if (!s->oformat->write_uncoded_frame) + if (!s->oformat->write_uncoded_frame) { + av_frame_free(&frame); return AVERROR(ENOSYS); + } if (!frame) { pktp = NULL; } else { pktp = &pkt; av_init_packet(&pkt); + pkt.buf = av_buffer_create((uint8_t *)frame, sizeof(*frame), + uncoded_frame_free, NULL, + AV_BUFFER_FLAG_READONLY); + if (!pkt.buf) { + av_frame_free(&frame); + return AVERROR(ENOMEM); + } + pkt.data = (void *)frame; - pkt.size = UNCODED_FRAME_PACKET_SIZE; + pkt.size = sizeof(*frame); pkt.pts = pkt.dts = frame->pts; pkt.duration = frame->pkt_duration; From patchwork Sat Apr 11 21:19:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18876 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 75F3F44A8DB for ; Sun, 12 Apr 2020 00:20:37 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5FFCF68B87D; Sun, 12 Apr 2020 00:20:37 +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 357B668B4AC for ; Sun, 12 Apr 2020 00:20:31 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id i10so6084756wrv.10 for ; Sat, 11 Apr 2020 14:20:31 -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=hm4URzCqlSnXQS4bMQ296KY/4W2NJEY2cyiMbPEvnz0=; b=acD0E1XGsiDcfkSsBEPpcxNik+SzgPH8LBdY11n5Qw9yqxWIcYzuBM4tXAB0OrVPDT SFvYeeVKaBzgsmz7RJV3LqFTo0bh1k80n2x4ZT6E5xGr6qOLO9JfEb1MmN8I4R5yhmeZ CgWPsgnwUoaQyol6hDGPbpI7zSl9zDazWKfm48DzKK/z0dc3axVKJLwHei++PMK1Dbmf Wnqv2DW8BPWZSBsbw54tCjwhNLMBLskZASbDA6Bf8nFdl2Hsj6LTrNKmWZEWLBMZBhOZ eGrUQqyoIcHT79qKjau+EJLzbo1+fqYEDnt0f41vAC8INVu3kasrfyWAlrPRTODIgjoK 28NQ== 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=hm4URzCqlSnXQS4bMQ296KY/4W2NJEY2cyiMbPEvnz0=; b=IEY3zjzyKaP6gP2Yil4aTUd2oaR4NzzLO3dBSNByazGQXTlQ+X/7qVkvUdYOZN5HFt jAx8KJwD33UpnXiroARHWJdMFkL+Cji4ajL15baZa9PhwDmdyyYec42QkZQF8x0jRhKd Ct5EjJEI0Z0LHlRQSk1waBqq76f+VbDi3L8I8WKVyvTxQwXpaVYN9KmIY+QgG5AQr+nN JL5iP24A4wOpm0cEtnK+adeqK18Np9tlzkG1RH9kkcG27aCDXo/oWCJCdpmUaHYA+qsn 0j0yVPzI+oHnUu4QI1kBB/jBLn8dqMN5Izm4lYWRVpc5N1jRoeoRPehmL3iRcpszcfMY wTPQ== X-Gm-Message-State: AGi0PuYy8c4rf5iYJcm5bEo3aTZ8OAUrftzI5ef5SBZM0KXRaH/p4BhG lMX7+TLDpgWfLniJC5Krstk0WGDE X-Google-Smtp-Source: APiQypI96/a+btSNjc72iBoHeIc6fPTwdT98WFXI7Axy1WsT2hxYHgtOmSncv4v7Q9Wdjher7rVuFw== X-Received: by 2002:adf:b344:: with SMTP id k4mr1711463wrd.76.1586640030340; Sat, 11 Apr 2020 14:20:30 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:29 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:51 +0200 Message-Id: <20200411211955.20843-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> References: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6] avformat/mux: Fix leaks on error when writing noninterleaved uncoded frames 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" If writing uncoded frames in noninterleaved mode fails at the preparatory steps (i.e. before it reaches write_packet()), the packet would not be unreferenced and the frame would leak. This is fixed by unreferencing the packet in write_uncoded_frame_internal() instead. This also makes it possible to remove the unreferencing in write_packet() itself: In noninterleaved mode frames are now freed in write_uncoded_frame_internal(), while they are freed in interleaved mode when their containing packet gets unreferenced (like normal packets). Signed-off-by: Andreas Rheinhardt --- After this commit the best way to send uncoded frames to muxers supporting them would be via the normal AVOutputFormat.write_packet(), because that way ownership of the AVFrame can be passed. Then write_packet() in mux.c could be further simplified. (This of course only applies if the uncoded frame API is kept.) libavformat/mux.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 712ba0c319..e86214d585 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -744,7 +744,6 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) av_assert0(pkt->size == sizeof(*frame)); ret = s->oformat->write_uncoded_frame(s, pkt->stream_index, &frame, 0); av_assert0((void*)frame == pkt->data); - av_packet_unref(pkt); } else { ret = s->oformat->write_packet(s, pkt); } @@ -1320,6 +1319,7 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, AVFrame *frame, int interleaved) { AVPacket pkt, *pktp; + int ret; av_assert0(s->oformat); if (!s->oformat->write_uncoded_frame) { @@ -1349,8 +1349,11 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME; } - return interleaved ? av_interleaved_write_frame(s, pktp) : - av_write_frame(s, pktp); + ret = interleaved ? av_interleaved_write_frame(s, pktp) : + av_write_frame(s, pktp); + if (pktp) + av_packet_unref(pktp); + return ret; } int av_write_uncoded_frame(AVFormatContext *s, int stream_index, From patchwork Sat Apr 11 21:19:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18877 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 3EA8C44A8DB for ; Sun, 12 Apr 2020 00:20:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 24C7268B895; Sun, 12 Apr 2020 00:20:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3228468B821 for ; Sun, 12 Apr 2020 00:20:32 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id h2so5811082wmb.4 for ; Sat, 11 Apr 2020 14:20:32 -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=doIcomMt6UqvFQY1t4MsbKpfpCkgbeZvdqOM+meyZBs=; b=Su+4h5s0NAwRJ8J9eYhZYR2sfPuaN27yVoK5j63E2q3WUcd55thUlPAz5dRWRz2cm7 uoZWnM2o0ds79GySGI3hVnjlXLKfGz4E4p7xPuR37yMHftyT3yEw5TU6/pzrerlXP4py V9DOD+3h+D+NW7Lk4by08XESX6BAKxwCbOjn+NP2uzleyZsAFPQQD9O08aCfsVYsVaFs vv5DmTM6of8//ky+H6k0TzFYnaZxOhX1XLnA2m35/vB4AQjxczljUqGZrtxQGE8zLyX2 +vBd3eeWXQKWzIa06GD9kY/fpeSZk0uuaBy99aWz9E20zQIO+yzmAxjFnNb/3lvYpxAX 0fzA== 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=doIcomMt6UqvFQY1t4MsbKpfpCkgbeZvdqOM+meyZBs=; b=GOjJ7IL79jKLjHJitin3gyz17rqFTAcBkNkqX0EYFkiSsIMiGsyMRPHi3dgBM0G5Wb LvhCENvKWWQ1S7TC5x8L6bqjYTllNIdJs0wbwPbsYyU7Fk8zT84BGvCtRYx9ftQI429b +QBRPlX73EIrf2yYCyIQs4AwWcaL5StKxEEwZB2EleZ9G9mwLuNafXn3P+hoLbEtVd92 wyETsQDPj1mHn51Af+5TcOHKx2vcqJv2lhuutU4oH9byiCZJWeecNScqkW+YEM89XnZD Cb1/EG+IaFGruw6iRGQpEMbxDq/kcw+S+FDi5gTLX4C8E2gYpMihZNw+yJ/MLR86h0rK WJbg== X-Gm-Message-State: AGi0Pub+0g40lmKkqmIAisi57mdsyOV7sInCKFP2WFOAhPr4ZB2SDC2K 5k5o9RIkCoRgnplCGUZftN3w0tSJ X-Google-Smtp-Source: APiQypLlXJ0PvCc6BnfxxXNcVevVgXgMVrw6BsWBRPYN1A5PISm2GvrmA6X7uXCpjwFEHOr1yYZtcA== X-Received: by 2002:a1c:cc16:: with SMTP id h22mr10859835wmb.47.1586640031272; Sat, 11 Apr 2020 14:20:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:30 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:52 +0200 Message-Id: <20200411211955.20843-3-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> References: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/6] avformat/mux: Fix leak when adding packet to interleavement queue fails 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" When an error happened in ff_interleave_add_packet() when adding a packet to the packet queue, said packet would not be unreferenced in ff_interleave_add_packet(), but would be zeroed in av_interleaved_write_frame(), which results in a memleak. This has been fixed: ff_interleave_add_packet() now always unreferences the input packet on error; as a result, it always returns blank packets which has been documented. Relying on this a call to av_packet_unref() in ff_audio_rechunk_interleave() can be removed. Signed-off-by: Andreas Rheinhardt --- This approach is way more robust than my old "let the caller clean up" approach. And given that uncoded frames are no longer as horrible as they are, one doesn't need to add further checks for whether one has an uncoded frame to free on error in ff_interleave_add_packet(). libavformat/audiointerleave.c | 4 +--- libavformat/internal.h | 4 ++-- libavformat/mux.c | 7 +++++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/libavformat/audiointerleave.c b/libavformat/audiointerleave.c index f10c83d7c9..36a3288242 100644 --- a/libavformat/audiointerleave.c +++ b/libavformat/audiointerleave.c @@ -136,10 +136,8 @@ int ff_audio_rechunk_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { AVPacket new_pkt; while ((ret = interleave_new_audio_packet(s, &new_pkt, i, flush)) > 0) { - if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0) { - av_packet_unref(&new_pkt); + if ((ret = ff_interleave_add_packet(s, &new_pkt, compare_ts)) < 0) return ret; - } } if (ret < 0) return ret; diff --git a/libavformat/internal.h b/libavformat/internal.h index 332477a532..e9d7d6754a 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -230,9 +230,9 @@ char *ff_data_to_hex(char *buf, const uint8_t *src, int size, int lowercase); int ff_hex_to_data(uint8_t *data, const char *p); /** - * Add packet to AVFormatContext->packet_buffer list, determining its + * Add packet to an AVFormatContext's packet_buffer list, determining its * interleaved position using compare() function argument. - * @return 0, or < 0 on error + * @return 0 on success, < 0 on error. pkt will always be blank on return. */ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int (*compare)(AVFormatContext *, const AVPacket *, const AVPacket *)); diff --git a/libavformat/mux.c b/libavformat/mux.c index e86214d585..f61dbd3c89 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -918,10 +918,13 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt, int chunked = s->max_chunk_size || s->max_chunk_duration; this_pktl = av_malloc(sizeof(AVPacketList)); - if (!this_pktl) + if (!this_pktl) { + av_packet_unref(pkt); return AVERROR(ENOMEM); + } if ((ret = av_packet_make_refcounted(pkt)) < 0) { av_free(this_pktl); + av_packet_unref(pkt); return ret; } @@ -1216,7 +1219,7 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) av_init_packet(pkt); pkt = NULL; } - if (ret <= 0) //FIXME cleanup needed for ret<0 ? + if (ret <= 0) return ret; ret = write_packet(s, &opkt); From patchwork Sat Apr 11 21:19:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18878 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 5956644A8DB for ; Sun, 12 Apr 2020 00:20:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 419D068B87E; Sun, 12 Apr 2020 00:20:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4C42268B868 for ; Sun, 12 Apr 2020 00:20:33 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id x4so5827023wmj.1 for ; Sat, 11 Apr 2020 14:20:33 -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=BVYIQQjzpqopRI/rek5FoyKvrcDSVADJsfgPsNO9jXo=; b=TaXUyInLo6C/XW2gGHncRVSg6dIR9wnBimxBeNV49gl8D26Zro5S68HTy+TuSIGdzp Z2rVK2gEs+x0pmoVRb8qHUlScErviYdr63rBvhOLxrwU4G+TpDCXGJYuNyQ44/OfbfwP m8GJ/oVk8zlBqeh6QhiZpyO0Zlfb0a7IR66U1Co9xdomw0XY/HBDmkiHjytuX7+lnjST lhh7NYpGFIdNCE+DraCzkmYwds3M7iq3eJ7WSDIad5XWXwQyqdAXEvfP7mWZuj+3uNM6 Hi78EozjV+CmZL+M3n+ARcMPJgpeFkX+5p0fj/HnumygmQ9zuadObxi1vRlU09dqzhzw rn8g== 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=BVYIQQjzpqopRI/rek5FoyKvrcDSVADJsfgPsNO9jXo=; b=eurwSGUS8/mbttQlYNXgjM1radUhda1HFsQGQqFU6gF8a//zcmwFUnPiymctuJzE4U uWxFUS7+fO1NK7r9tw5I0cTz84B8/SxmeqLgiI0v2LZBTPZCVg0XU3Em9rl1KVx2z/cF 3DS1pn7KQ+ivlrPbJQCfKu0XKfOuZWmO/bpGtX8iGsj28DObE99IVeMf2OgagLOO+uQb 4LdibI4873CQMGv+I+hv3BEL9rwUTDVQRwsh37c68tWFmVFLXPMiOtIJbmjitCZAW1iO UUpiIOym4vqHhYmoXood2XZBJTWK43aDkFIMc4ntJLchTLPPhu0/zuI+FkoIGovavTSb SXNw== X-Gm-Message-State: AGi0PuaOQgxBPZk3n5tc4hgvy5RFcMKYk3cwKDlfLLohGWKQVjw+ZKIM X48JhMNCfTOOZ+EG2BKDVTPb7Qq8 X-Google-Smtp-Source: APiQypLT0yR3Cw804E+dwFh+ZVOE5WulBp4okLQdNGVVIg799Eta9HlIU8W87tGT9R+PStTeTU3zYA== X-Received: by 2002:a7b:ce88:: with SMTP id q8mr12168617wmj.161.1586640032242; Sat, 11 Apr 2020 14:20:32 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:31 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:53 +0200 Message-Id: <20200411211955.20843-4-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> References: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/6] avformat/mux: Remove redundant resetting 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" Now that ff_interleave_add_packet() always returns blank packets, the input packet to ff_interleave_packet_per_dts() will always be blank on return as well (if supplied) and the same goes for interleave_packet() in mux.c. Document these facts and remove the redundant resetting that happened in av_interleaved_write_frame(). The last reference to the (long removed) destruct field that AVPackets once had has been removed as well when updating the documentation of ff_interleave_packet_per_dts(). Signed-off-by: Andreas Rheinhardt --- libavformat/internal.h | 11 ++++------- libavformat/mux.c | 9 +++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/libavformat/internal.h b/libavformat/internal.h index e9d7d6754a..2cb5964720 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -495,15 +495,12 @@ int ff_framehash_write_header(AVFormatContext *s); int ff_read_packet(AVFormatContext *s, AVPacket *pkt); /** - * Interleave a packet per dts in an output media file. + * Interleave an AVPacket per dts so it can be muxed. * - * Packets with pkt->destruct == av_destruct_packet will be freed inside this - * function, so they cannot be used after it. Note that calling av_packet_unref() - * on them is still safe. - * - * @param s media file handle + * @param s an AVFormatContext for output. in resp. out will be added to + * resp. taken from its packet buffer. * @param out the interleaved packet will be output here - * @param pkt the input packet + * @param pkt the input packet; will be blank on return if not NULL * @param flush 1 if no further packets are available as input and all * remaining packets should be output * @return 1 if a packet was output, 0 if no packet could be output, diff --git a/libavformat/mux.c b/libavformat/mux.c index f61dbd3c89..cae9f42d11 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1159,7 +1159,7 @@ int ff_interleaved_peek(AVFormatContext *s, int stream, /** * Interleave an AVPacket correctly so it can be muxed. * @param out the interleaved packet will be output here - * @param in the input packet + * @param in the input packet; will always be blank on return if not NULL * @param flush 1 if no further packets are available as input and all * remaining packets should be output * @return 1 if a packet was output, 0 if no packet could be output, @@ -1214,14 +1214,11 @@ int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt) for (;; ) { AVPacket opkt; int ret = interleave_packet(s, &opkt, pkt, flush); - if (pkt) { - memset(pkt, 0, sizeof(*pkt)); - av_init_packet(pkt); - pkt = NULL; - } if (ret <= 0) return ret; + pkt = NULL; + ret = write_packet(s, &opkt); av_packet_unref(&opkt); From patchwork Sat Apr 11 21:19:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18879 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 1F6C044A8DB for ; Sun, 12 Apr 2020 00:20:43 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 08A2E68B89A; Sun, 12 Apr 2020 00:20:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2F30568B4AC for ; Sun, 12 Apr 2020 00:20:34 +0300 (EEST) Received: by mail-wm1-f68.google.com with SMTP id f20so6148529wmh.3 for ; Sat, 11 Apr 2020 14:20:34 -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=OkE/Bf7XusloCvT8nBJTvGOYanhB5LOvauoXiUCnoUo=; b=SKDl2dZsHqsA2a2A6kVvg3QuG1srpDTQY8dVJI4gkdymiWsiJ8T8G6x0gDP6auW0IG slF089JlaNi4pAYWsABNtUcjp7P7s0GfPCumbOGs3kNwDNmGYrTxsLQIENicLlGgRxKi D4uIfb/oYjQI2kUw/9Oo/4mCcWJTW/S0810UYWnO07v73cKgIvzqAJ+R0Cazs+49IF8l 1NFaeVi1ZqQQpE/n4O/vjMZV5r6/GPm0kHwga1Xn95mvGOrLTCSoyuUE4YlKssPt4bI4 CKQTpsOEcHfedZrmlA2Ps5Mwa5lqC214nwm+bMchKPWAlKa2f5gmyRkhVtUsRUeqlfEk zTcg== 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=OkE/Bf7XusloCvT8nBJTvGOYanhB5LOvauoXiUCnoUo=; b=bgC/DAUQnvqftYblG3fTflikzOaxmMqmT64Ud+UdZP2xjEvKPgC5mNboQPh3D/+iu3 ev3g5/ivHFj37+fycv97ZxanRjV9195y9p5wWMGzfRppnGB7XwJF2IIsFRwxWo2yG/Do GX3ipDA3VxyTwSVANCDnR/ee52KOLoljvSS3RxlYJ2iTVmZyjY3AVtSO1ok427XtYp58 IizKXnccMwtZUA+ijPGk4GCeE9RPExgza138Fukitpu8ieO0DgXh4Hq2NdvZSdXqERoB qT2qQ3tw3KdefJOwyQu7RF8vwrDa6W89A+/sImYMwMR2T5M6xLQ94GXuE3BZIwtP0vF0 Fr6Q== X-Gm-Message-State: AGi0PuZL3pNMslEvg7Au5SL1vkbZdFgTKW1XaeiElGQ66wWbP85a55kL UC2cAo+cPyTiGILj/D455HI+PWun X-Google-Smtp-Source: APiQypKPpkkBMguIJ1SnpcMKSOYJJgwWVN/V42W4QtG+6G786FeoPA5D7Kifer5Ef6PE4Fa+WU3Lzw== X-Received: by 2002:a7b:cb03:: with SMTP id u3mr10692323wmj.12.1586640033268; Sat, 11 Apr 2020 14:20:33 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:54 +0200 Message-Id: <20200411211955.20843-5-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> References: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/6] avformat/mux: Don't modify packets we don't own 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" The documentation of av_write_frame() explicitly states that the function doesn't take ownership of the packets sent to it; while av_write_frame() does not directly unreference the packets after having written them, it nevertheless modifies the packet in various ways: 1. The timestamps might be modified either by prepare_input_packet() or compute_muxer_pkt_fields(). 2. If a bitstream filter gets applied, it takes ownership of the reference and the side-data in the packet sent to it. In case of do_packet_auto_bsf(), the end result is that the returned packet contains the output of the last bsf in the chain. If an error happens, a blank packet will be returned; a packet may also simply not lead to any output (vp9_superframe). This also implies that side data needs to be really copied and can't be shared with the input packet. The method choosen here minimizes copying of data: When the input isn't refcounted and no bitstream filter is applied, the packet's data will not be copied. Notice that packets that contain uncoded frames are exempt from this because these packets are not owned by and returned to the user. This also moves unreferencing the packets containing uncoded frames to av_write_frame() in the noninterleaved codepath; in the interleaved codepath, these packets are already freed in av_interleaved_write_frame(), so that unreferencing the packets in write_uncoded_frame_internal() is no longer needed. It has been removed. Signed-off-by: Andreas Rheinhardt --- I was actually surprised when I realized how freeing uncoded frames in the noninterleaved codepath could be left to av_write_frame(). libavformat/mux.c | 48 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index cae9f42d11..48c0d4cd5f 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -874,11 +874,12 @@ static int do_packet_auto_bsf(AVFormatContext *s, AVPacket *pkt) { return 1; } -int av_write_frame(AVFormatContext *s, AVPacket *pkt) +int av_write_frame(AVFormatContext *s, AVPacket *in) { + AVPacket local_pkt, *pkt = &local_pkt; int ret; - if (!pkt) { + if (!in) { if (s->oformat->flags & AVFMT_ALLOW_FLUSH) { ret = s->oformat->write_packet(s, NULL); flush_if_needed(s); @@ -889,22 +890,49 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt) return 1; } + if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) { + pkt = in; + } else { + /* We don't own in, so we have to make sure not to modify it. + * The following avoids copying in's data unnecessarily. + * Copying side data is unavoidable as a bitstream filter + * may change it, e.g. free it on errors. */ + pkt->buf = NULL; + pkt->data = in->data; + pkt->size = in->size; + ret = av_packet_copy_props(pkt, in); + if (ret < 0) + return ret; + if (in->buf) { + pkt->buf = av_buffer_ref(in->buf); + if (!pkt->buf) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + } + ret = prepare_input_packet(s, pkt); if (ret < 0) - return ret; + goto fail; ret = do_packet_auto_bsf(s, pkt); if (ret <= 0) - return ret; + goto fail; #if FF_API_COMPUTE_PKT_FIELDS2 && FF_API_LAVF_AVCTX ret = compute_muxer_pkt_fields(s, s->streams[pkt->stream_index], pkt); if (ret < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS)) - return ret; + goto fail; #endif - return write_packet(s, pkt); + ret = write_packet(s, pkt); + +fail: + // Uncoded frames using the noninterleaved codepath are freed here + av_packet_unref(pkt); + return ret; } #define CHUNK_START 0x1000 @@ -1319,7 +1347,6 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, AVFrame *frame, int interleaved) { AVPacket pkt, *pktp; - int ret; av_assert0(s->oformat); if (!s->oformat->write_uncoded_frame) { @@ -1349,11 +1376,8 @@ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index, pkt.flags |= AV_PKT_FLAG_UNCODED_FRAME; } - ret = interleaved ? av_interleaved_write_frame(s, pktp) : - av_write_frame(s, pktp); - if (pktp) - av_packet_unref(pktp); - return ret; + return interleaved ? av_interleaved_write_frame(s, pktp) : + av_write_frame(s, pktp); } int av_write_uncoded_frame(AVFormatContext *s, int stream_index, From patchwork Sat Apr 11 21:19:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18880 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 C72EF44A8DB for ; Sun, 12 Apr 2020 00:20:43 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BAC0768B8AD; Sun, 12 Apr 2020 00:20:43 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0D5E568B4AC for ; Sun, 12 Apr 2020 00:20:35 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id j2so6102859wrs.9 for ; Sat, 11 Apr 2020 14:20:35 -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=ni4vTJHrM4Odu2ZlUR1YqGROgtVW3wP/th9pCwQVG2Y=; b=kKIh/VsydM9U+5om5AT/k4BywtC7N83g9lyyRJNAgOXEkJGhAyNjZNHiuq2jn4w51R A7CoHuuVadF7cHh+joQ3cnHoAjvUi6NZJ6wB5n/I118k1EGZoMV3w4mMpEiJrhoY7U40 MukcPMlN4ye9sGIDnTe4sjlvlA51L8YrRzUL64J8Js05d1BYUZB31HJAzPWlbkgQpsVu nmnYUCSjWfrWHE9yyK49uesBI4jVtrUvU3tWQei0b2XIYnUbYVATSZag+FzbdCuLwW1q InQS5oX6OhPtcPpZl12/xy0DElsm49HsI/bqbq9ODNNPZ0KIwrwoQ2rSqCXJkBNEhzCt nvGw== 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=ni4vTJHrM4Odu2ZlUR1YqGROgtVW3wP/th9pCwQVG2Y=; b=t3uLvTYfGTK5UZ0jeMAKtwJkKKiG/ScuMBwC4dZ6lMUYry3kM1mmCQeC9ACOw4wlkv OTbeHLLrQmbQevV1FfWwSb0udAY5/xynaALyplwHCCT4GMV6yHFxCxQdykD5b8jqzbUv 1egwOI6jp60cweHqWOdykPTxrFTjgvYxS9oSuCW2MJwBqobwdcHa3ynEikdhhdx7HyG5 nP6Kg4GZHX1s/y2vtK6i4UgcS+ptfuBcQFG4WX4tLFqNQDFEhTSW2IK3eeMR0DAkEnaq lasXleabHKACD/U57VliiSFefcikJssD0RAVQU6sgn/rXB4nZDqI9y8roJb3086NYZH7 +Dag== X-Gm-Message-State: AGi0PuYfpk4/pLwW96XPWANzGGCqZnAQIxyZ6dzYjMKFigvXK/o3BNDT b6BFA70MQXqdOQzV6UUuBbCM5eq+ X-Google-Smtp-Source: APiQypJJbUTklnHrPvPlYzvGMOSjXPXVscMtQUXtw2X0Xgv1XqL0VbHnT8yhTNwxMAjdlTbP9yszlA== X-Received: by 2002:a5d:6a10:: with SMTP id m16mr12195099wru.371.1586640034311; Sat, 11 Apr 2020 14:20:34 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id h2sm7820650wmb.16.2020.04.11.14.20.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2020 14:20:33 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 11 Apr 2020 23:19:55 +0200 Message-Id: <20200411211955.20843-6-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> References: <20200411211955.20843-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 6/6] avformat/mux: Remove pointless timestamp backups 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" write_packet() currently saves the original timestamps of the packet it got and restores them in case writing fails. This is unnecessary as we are no longer working directly with the user-supplied AVPacket here; and it is also pointless because the timestamps may already have been altered before write_packet(). So remove this and add a general comment to the function that timestamps may be modified; also remove a long outdated comment about side data. Signed-off-by: Andreas Rheinhardt --- libavformat/mux.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 48c0d4cd5f..1135e52f71 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -664,8 +664,7 @@ FF_ENABLE_DEPRECATION_WARNINGS #endif /** - * Make timestamps non negative, move side data from payload to internal struct, call muxer, and restore - * sidedata. + * Shift timestamps and call muxer; the original pts/dts are not kept. * * FIXME: this function should NEVER get undefined pts/dts beside when the * AVFMT_NOTIMESTAMPS is set. @@ -675,10 +674,6 @@ FF_ENABLE_DEPRECATION_WARNINGS static int write_packet(AVFormatContext *s, AVPacket *pkt) { int ret; - int64_t pts_backup, dts_backup; - - pts_backup = pkt->pts; - dts_backup = pkt->dts; // If the timestamp offsetting below is adjusted, adjust // ff_interleaved_peek similarly. @@ -754,10 +749,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) ret = s->pb->error; } - if (ret < 0) { - pkt->pts = pts_backup; - pkt->dts = dts_backup; - } else + if (ret >= 0) s->streams[pkt->stream_index]->nb_frames++; return ret;