From patchwork Tue Mar 31 12:37:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18533 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 883D544B0FC for ; Tue, 31 Mar 2020 15:38:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7142E68B1A2; Tue, 31 Mar 2020 15:38:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2948568B12E for ; Tue, 31 Mar 2020 15:38:34 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id w10so25793572wrm.4 for ; Tue, 31 Mar 2020 05:38: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=6R8+Kr9hrOWT+vnM3paFM9n5w1lmbEh26KAsRMViRH0=; b=TZeBqaRqT6ZryE+XxFxfphx4zdq06pTa7iF6y2NM/wvtjDoYRqH0lezX6UeAlh9vKJ ClFONr7Ae6uRBNAgbxDFqcyL8si5lIQvykbJkReg4nlRtj6AXqDRtx/vgGOlYKjpapgm SZpRx1JFZurFYcRrc8Bm0TG0uvNjvHVpnEQxn1qqv8JqWgbuzH+Tp7ck9ggOBHtNWSsk bazDuUMOYN+6/GQoin7f2hERVQSAmLWMWupHsWsHzmq3EwZvH7aBAvbXyS35CRqsL8jj b0UwES8b/hX7FKn+vqJzdMC/v/Irhjc5gFOMUccPfZElXHgvw5+Th+1g67SKVjbRS9Hp zZGg== 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=6R8+Kr9hrOWT+vnM3paFM9n5w1lmbEh26KAsRMViRH0=; b=by5hztjA8Crf7SZCwDqbJmm5D4sLSP9nD5NDmL+1KpoisDftsa8FZ8i1abDhOjs62G 9HvZ9Z4leFEpIz4K7d1uUPoOCJDRpKlGEsquD4DUk/pFUhGry6dnzzWKIAC9zFB0VPcA RUGxvfkaYpYbgmrZS1W2D+bfob+cxxFtf8mQwuxgNzF96TUOLHK6VopMxkF3I1mwFZTe mMUCTqeCEXsvPi/FroqTX6oOKM9MMMzS01naRkEM0N0kRcA37sY765CrRWTFc+uK4mNZ 4E2NHtEDW73l0uGv5Dg+/pSLnM0e9iMS2yIS9/U/we1QdHxd1pnbqL6etRyHaC/ZFztS YQZw== X-Gm-Message-State: ANhLgQ2xLImkjatPjuAlG9M8ynzYguEv9a+vK2wOwEvrwKZ6ukTd5KTq gXY9tAMTLunrTDStqdZkOb97UHBL X-Google-Smtp-Source: ADFU+vswniNXXu4+3gTksSVA93YKHvW6nMlidBl/df7GBVfEdaWYDpk/B5/FWT7FFq11ZLLgcUc6vA== X-Received: by 2002:a5d:4bc1:: with SMTP id l1mr21753064wrt.103.1585658313167; Tue, 31 Mar 2020 05:38:33 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id p13sm26301532wru.3.2020.03.31.05.38.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2020 05:38:32 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Mar 2020 14:37:41 +0200 Message-Id: <20200331123745.6461-7-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200331123745.6461-1-andreas.rheinhardt@gmail.com> References: <20200331123745.6461-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/10] 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 av_bsf_send_packet. 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, an empty 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. Signed-off-by: Andreas Rheinhardt --- Whether a commit like this is necessary depends upon the meaning of "ownership" (whether not having ownership disallows one from modifying a packet) and of the user's expectations (namely if the av_write_frame() should attempt to not modify the user-provided packet even if it were allowed to). Furthermore, this commit has the downside of adding an AVPacket on the stack. libavformat/mux.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 79731d3008..be7652c97b 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -880,11 +880,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); @@ -895,22 +896,43 @@ int av_write_frame(AVFormatContext *s, AVPacket *pkt) return 1; } + /* 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->data = in->data; + pkt->size = in->size; + if (in->buf) { + pkt->buf = av_buffer_ref(in->buf); + if (!pkt->buf) + return AVERROR(ENOMEM); + } else { + pkt->buf = NULL; + } + ret = av_packet_copy_props(pkt, in); + if (ret < 0) + 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: + av_packet_unref(pkt); + return ret; } #define CHUNK_START 0x1000