From patchwork Fri Oct 11 04:06: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: 15692 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 B590544A685 for ; Fri, 11 Oct 2019 07:14:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 80E336881F7; Fri, 11 Oct 2019 07:14:38 +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 BC8C36881F4 for ; Fri, 11 Oct 2019 07:14:32 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id h4so10227168wrv.7 for ; Thu, 10 Oct 2019 21:14: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:mime-version :content-transfer-encoding; bh=f767gdS445jNgf335cijAKERrTWIqSAu/FQsSEGzB4A=; b=nJdfss2qcTBEhZKChxEqqeOLOGsrgH74kculpOudEA8CYcKKCS79oHBx/rotLpxRnX s6osBKfJUCYuS4yblY75Pa7fGL3MVDytfCIHpH+NBmCk2EOjIT9+Z1fzHMU/PqcCFss9 seON+5vN91WgAl5fgIMzMKiL/8uskFudKw24FduwJls3nORAFkKgrKFBE8XcpZgdFds3 n8khWNXNeXgoQ2EKdtEqUQ2VBAnBl+QwVjSlbbB2E4WvAfOe0gVd1GTQsD/lOz6dKoAy /4ifvdrX4V4jRjb3k5CgojpRZ5bHaxVHwQb+xTSaHy7l3xBgxjKryW/FuMPsh8jsN5vT SV8Q== 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=f767gdS445jNgf335cijAKERrTWIqSAu/FQsSEGzB4A=; b=miOkU52Wv8GddMuksLnnjzYLB7iSMYEAWNfZmzCtZw9fb1CKg5GPK0e1ZG2tz5IRc3 R9HR96fKKYLogRYr29rfVcaB5U5z7zN4NuF0xWNemPE3imYD7sa6kqklHR+HHFJ+TLYc 9QmZ7uzp7xh0EH9SS8vwp/H65hfXLr6BfQRABfG1nhOj6O/36lDC4Ks+Y/Jy0fnCd95p We/B+yaMwwl8PaSlAAmd8qRytJtn8svHjRFttW7ymp/fhaZWC/foF3pqWzjXW9PNMDXe sINezqAHdinEcBQ8olXEVzMXgd0fOA5K/Mll1s13MjVlQrIXfz0Mc+MNPjN7uvybyZAX nQuQ== X-Gm-Message-State: APjAAAU+/WtF6ykze/Susi5KcczCi1kjNJnh/KAsJXNqoOacBMVaPUzp w8HFRLutFLoZR6P9Ky5T8loVsBaX X-Google-Smtp-Source: APXvYqynVvNOdvuiqYyAtslLkj1qzof826XFc1Dx2WwfrVGPV/Vqn8ZplAVLCm4F/vXoGXc09xO40g== X-Received: by 2002:adf:f402:: with SMTP id g2mr11561348wro.64.1570766827432; Thu, 10 Oct 2019 21:07:07 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id o18sm17256052wrw.90.2019.10.10.21.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 21:07:06 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Oct 2019 06:06:58 +0200 Message-Id: <20191011040658.31714-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] fftools/ffmpeg: Improve streamcopy 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" do_streamcopy() has a packet that gets zero-initialized first, then gets initialized via av_init_packet() after which some of its fields are oerwritten again with the actually desired values (unless it's EOF): The side data is copied into the packet with av_copy_packet_side_data() and if the source packet is refcounted, the packet will get a new reference to the source packet's data. Furthermore, the flags are copied and the timestamp related fields are overwritten with new values. This commit replaces this by using av_packet_ref() to both initialize the packet as well as populate its fields with the right values (unless it's EOF again in which case the packet will still be initialized). The differences to the current approach are as follows: a) There is no call to a deprecated function (av_copy_packet_side_data()) any more. b) Several fields that weren't copied before are now copied from the source packet to the new packet (e.g. pos). Some of them (the timestamp related fields) may be immediately overwritten again and some don't seem to be used at all (e.g. pos), but in return using av_packet_ref() allows to forgo the initializations. c) There was no check for whether copying side data fails or not. This has been changed: Now the program is exited in this case. Using av_packet_ref() does not lead to unnecessary copying of data, because the source packets are already always refcounted (they originate from av_read_frame()). Signed-off-by: Andreas Rheinhardt --- After this patch there will be only call to av_copy_packet_side_data() left: In libavformat/movenc.c. Btw: Initializing the packet for flushing is actually unnecessary. fftools/ffmpeg.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b6ecb89893..8e408c808a 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1995,12 +1995,13 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p InputFile *f = input_files [ist->file_index]; int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time; int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase); - AVPacket opkt = { 0 }; - - av_init_packet(&opkt); + AVPacket opkt; // EOF: flush output bitstream filters. if (!pkt) { + av_init_packet(&opkt); + opkt.data = NULL; + opkt.size = 0; output_packet(of, &opkt, ost, 1); return; } @@ -2039,10 +2040,11 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) ost->sync_opts++; + if (av_packet_ref(&opkt, pkt) < 0) + exit_program(1); + if (pkt->pts != AV_NOPTS_VALUE) opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time; - else - opkt.pts = AV_NOPTS_VALUE; if (pkt->dts == AV_NOPTS_VALUE) opkt.dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase); @@ -2061,18 +2063,6 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase); - opkt.flags = pkt->flags; - - if (pkt->buf) { - opkt.buf = av_buffer_ref(pkt->buf); - if (!opkt.buf) - exit_program(1); - } - opkt.data = pkt->data; - opkt.size = pkt->size; - - av_copy_packet_side_data(&opkt, pkt); - output_packet(of, &opkt, ost, 0); }