From patchwork Mon Dec 13 15:20:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32422 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5572330iog; Mon, 13 Dec 2021 07:22:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJx471e/PV5EYHFPXEjB8TjDWE7ukhfLmsBDEy+rs3CB0JyxZa6ypS7X/Y4cGRU1GSG9WTBr X-Received: by 2002:a17:906:e115:: with SMTP id gj21mr45378389ejb.348.1639408954635; Mon, 13 Dec 2021 07:22:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639408954; cv=none; d=google.com; s=arc-20160816; b=JokVPZqgzNu776rvxcAwSZYjTC+kJmueTZJ2J1havNk3lzzStFBMm52IvJCCO1NABn I+qo4pelQ78a7GnIe7T3wjyFGSu2Bhxvh4yFhd5qdNU+fPudD9YVwnIohHxtamh8YXrX /rTMceZRNd2eJMPH7jjU/bj7UY+ArIcGuJ4Gt8LrJgpz9EKkAsQNWCCK/oGTvgp3iL80 misxjTkqouTaXAmyKJMoRezU8YCOGSF0KblxiX/BmvIGaUdx/PVHyj8WMAc9l4oqKs7l xAPiRQh2EZr0Ug0GVOb1p7atcfLldBouhCiLFj9Fh86WxtYLoVnMUmgTQ1rXpKARYd3c vWKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=WfyYjVxM8IEVMj4PFPv469RtSzg6+mWHQ6f6JMDhS5I=; b=0it+CBv1QLRA1uEilp76OLVRpzSjShacE5CxyssC9EZo1ijq4bfHnebbmpSlY3bygj eYgT5+BnMAEa5HsSQcmoVjKJwjeX2nj0jGPIUU9eyue70cAzoHxddd/CIO6ugnUIaX2c scH6OSyvHQglDhaITxbjU64jQgsUbe+H7fXBPlvZtgVuZ9GLDj4sJIhK2JhvidzkxZCO by9DFFpDItG/K0Z28fGibHXS23IOc8d++pKP1tBeFUmUmdYgpyz/amRQ9zKCTeyXWJVR IFVpoFy8r8On055qbZltV98homPs1LOeygzG77/gsqGecghjQPJRVCrD8xk2ejD4adQg BXFg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i11si22047568ejw.240.2021.12.13.07.22.34; Mon, 13 Dec 2021 07:22:34 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3191D68AEEA; Mon, 13 Dec 2021 17:22:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 082EA68A7A9 for ; Mon, 13 Dec 2021 17:22:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8E4B724017C for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id yOSeEoFmrAaw for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 2B8FC240179 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0BE603A0631; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:19 +0100 Message-Id: <20211213152042.5900-1-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 01/24] ffmpeg: pass the muxer context explicitly to some functions X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: iLe3uyuTG8J2 Stop accessing OutputFile.ctx. This will be useful in the following commits, where it will become hidden. --- fftools/ffmpeg_opt.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 9c820ab73f..f638edace9 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2094,10 +2094,10 @@ static int opt_streamid(void *optctx, const char *opt, const char *arg) return 0; } -static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) +static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os, + int copy_metadata) { AVFormatContext *is = ifile->ctx; - AVFormatContext *os = ofile->ctx; AVChapter **tmp; int i; @@ -2137,14 +2137,14 @@ static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata) return 0; } -static int set_dispositions(OutputFile *of) +static int set_dispositions(OutputFile *of, AVFormatContext *ctx) { int nb_streams[AVMEDIA_TYPE_NB] = { 0 }; int have_default[AVMEDIA_TYPE_NB] = { 0 }; int have_manual = 0; // first, copy the input dispositions - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; nb_streams[ost->st->codecpar->codec_type]++; @@ -2161,7 +2161,7 @@ static int set_dispositions(OutputFile *of) if (have_manual) { // process manually set dispositions - they override the above copy - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; int ret; @@ -2187,7 +2187,7 @@ static int set_dispositions(OutputFile *of) // For each media type with more than one stream, find a suitable stream to // mark as default, unless one is already marked default. // "Suitable" means the first of that type, skipping attached pictures. - for (int i = 0; i< of->ctx->nb_streams; i++) { + for (int i = 0; i < ctx->nb_streams; i++) { OutputStream *ost = output_streams[of->ost_index + i]; enum AVMediaType type = ost->st->codecpar->codec_type; @@ -2725,7 +2725,7 @@ loop_end: } } if (o->chapters_input_file >= 0) - copy_chapters(input_files[o->chapters_input_file], of, + copy_chapters(input_files[o->chapters_input_file], of, oc, !o->metadata_chapters_manual); /* copy global metadata by default */ @@ -2878,7 +2878,7 @@ loop_end: } } - err = set_dispositions(of); + err = set_dispositions(of, oc); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error setting output stream dispositions\n"); exit_program(1); From patchwork Mon Dec 13 15:20:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32423 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5572542iog; Mon, 13 Dec 2021 07:22:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJypCVMMA3UbWgM12vwN1d1VscUXikfuDt3lo3z1Kabmd3Yvz1wSIKea05QaDfAvWUrzOoof X-Received: by 2002:aa7:cc82:: with SMTP id p2mr63379633edt.201.1639408966090; Mon, 13 Dec 2021 07:22:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639408966; cv=none; d=google.com; s=arc-20160816; b=cgsuy3plrWueObfUKuHfhCoPWec42g3PvKkJWDhKV2g+AqvSavyUYxKyUQovHLwbUz +WAm3bJvqwPxopVIlWFScaTeL86MIQyHoTsG/+wNm6r+RLUMEPqViyw4+KIJg5IgyKFV 7/zit5zWwC1qYskzCHKVcu/YpE1ML6XIrZcbEECHbHJbUN5LYJnOcJ1dIrplDjlMGPPk kZDQamUqKPB1TngXLIPV6+pQ69JA/F/bpPliyzJTnAGw17+AfVJUg3fQ/K0CZvezcoUD uYPDvjDhQ/igmAb2sypb9dBmXcwnFk5F3QDXaqfDJ3PZbdGq4pzSd6ZVnb4sVq/SIRgS H5Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=xRpL+4WM+nBKjWJcK4CXLMTmf2EJChjVjcs/BfaXSmI=; b=lVwLqG+4cz5VvBx/ksRam0f70TmwuY665rkRYObiRaafNEDp+RZ6hreNmCrnYJDjVu PrxFUZoUx0GgnuGfm5NZxha4InHPess6MM6QxU2uwt39QTH9sty7EF9hB1bdgubsvV8Z uIJWbb7vrgPnd6HBqtvyRWMDfgW6AeGMRxJoF9fOf5M10dYeU2bc5rn1OtrT7ami69Xx hbnD1+WyMhbnppXKy+85pRNWkAryZE8gGRy9MkQTwQbXMlikx7YG6dl62502fj6pB8tD 6mIzZqPZCbolfSSY7y0doYZSTM7+YtAT5wrMEcE0CCKyrBmUn75A22+2jA0s/TbwM1u4 L7OA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r11si643603ejr.881.2021.12.13.07.22.45; Mon, 13 Dec 2021 07:22:46 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 20E1968AF94; Mon, 13 Dec 2021 17:22:18 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 34BCB68807F for ; Mon, 13 Dec 2021 17:22:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D2CE5240179 for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id gXWCGSa109vm for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 353F824017E for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0D8EB3A00D9; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:20 +0100 Message-Id: <20211213152042.5900-2-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/24] ffmpeg: simplify getting output file size X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 9G4zH2wLiNdc Stop calling avio_size()/avio_tell(), which are potentially heavy operations and may interfere with muxer operation. Use the recently introduced bytes_written field instead. --- fftools/ffmpeg.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index bdeff9a12e..b77531cb7f 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1677,8 +1677,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti { AVBPrint buf, buf_script; OutputStream *ost; - AVFormatContext *oc; - int64_t total_size; + int64_t total_size = -1; AVCodecContext *enc; int frame_number, vid, i; double bitrate; @@ -1708,11 +1707,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti t = (cur_time-timer_start) / 1000000.0; - oc = output_files[0]->ctx; - - total_size = avio_size(oc->pb); - if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too - total_size = avio_tell(oc->pb); + if (output_files[0]->ctx->pb) + total_size = output_files[0]->ctx->pb->bytes_written; vid = 0; av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC); From patchwork Mon Dec 13 15:20:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32424 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5572839iog; Mon, 13 Dec 2021 07:22:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZa8NZgwdFJ8ShUqCUbBq59iHFZe5ozR4wRLBY64tjWmycr03a8XuyelURHsm4+8BhscAb X-Received: by 2002:a17:907:7213:: with SMTP id dr19mr44390536ejc.157.1639408977754; Mon, 13 Dec 2021 07:22:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639408977; cv=none; d=google.com; s=arc-20160816; b=w8yPwlhQeaOIC+H/z8jkXE5DQu/jLds6/1FRhSl9uwpqCefkTSZjTFsh5Gm6SOL6d7 NjRmrq70jF6PpzT0eAX864qIhqkVMRsY4ybgoGQXyudGfSH2pa18XTfhDlxvQdMJBydO QrZKXLoqNpxuONRUf6hEU7XQymUrMxjQwCOtp8FokFbbLAQWRtAmCnNavjnntcz2irUd K6fGh4u9v7tYZIhLhaobwz+8+L3Kbe43xBEvxSGwEN6ZEgYwnkwZo18Er3VCncHoee7l tmhPe5X6hSboSuoUaN5NXEbbblkGvWZIXEWEihxF/7hXeH3AwDxJxzKIznqam/syHjV0 x/dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/zXeGamRCBBqV6iyFI0iPSu0qNVHqnnVspJeixfxo34=; b=o9ppU7C0jOAEZEUHY0EmIUVZsLnLN9BY9zcdTr9ZAYQoxETyB3chh16mxyNez1hzuS sItK8vINXkuPIE/QJ4t9h8qLJxwFBK/x64etZ20iJjuZWaMmchbkfF6lJ1M3E1a0FzQr E0t3aJgPMZV+2mn9l4jr91OAl4ORg0Ol4P6dfPBORT11QEXKw+C6HFl/9EGYTtcWCbAq klHATT4Tyx/B5QSUSmAt6UtQmwzhCkrk8op/WSFdRW5sjuKmLRyj0Bbb2akp9hYI7+HD M5hR2lugsecQDc/ScDBQsMtKNycTQmbkoLW+Mx3QWaFHoRA3g4yKleesOPR4ie3pj/Xo O2tA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id de8si14662268ejc.412.2021.12.13.07.22.57; Mon, 13 Dec 2021 07:22:57 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1FD3168AF9C; Mon, 13 Dec 2021 17:22:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5BFFE68807F for ; Mon, 13 Dec 2021 17:22:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 04CD424017E for ; Mon, 13 Dec 2021 16:22:09 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 20e8F4J_aeMK for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 37D2D2404FE for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 122753A073E; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:21 +0100 Message-Id: <20211213152042.5900-3-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 03/24] ffmpeg: remove a redundant assignment of interrupt_callback X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PaPJ/GZLWsvz It is already set in open_output_file(). --- fftools/ffmpeg.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b77531cb7f..fb017a1e37 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2947,8 +2947,6 @@ static int check_init_output_file(OutputFile *of, int file_index) return 0; } - of->ctx->interrupt_callback = int_cb; - ret = avformat_write_header(of->ctx, &of->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, From patchwork Mon Dec 13 15:20:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32421 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5572033iog; Mon, 13 Dec 2021 07:22:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJzXB5CbfdFfWwuy0VX6I913S0tphunu0OcwhCcdK3Cpb44LnfKlnHK/wbpIvnGmM1qv9ckD X-Received: by 2002:a17:906:2b47:: with SMTP id b7mr1197261ejg.618.1639408942335; Mon, 13 Dec 2021 07:22:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639408942; cv=none; d=google.com; s=arc-20160816; b=feIc2TQxD6sTwL54k9pMFn22kCiuA1JxcEqWlQXbuMKQRlSZckQxdLUvwND6cE52ji RCBNFc91LumQzpEiIIyqqBS60DHwfd8PqUpqY4owRY845ipGfSzFGIkcUwjfjbtCSttx QDZhuz+r+vb0szfC+HEJexMMHrv2ThNfaUQ+HLjzvivLxdUu/JLI3tXwJzHe0KU4wJVd rFzyYHwo3SKkCfbYbdvomew0yK1UZfQ9z5i/UvZbYN8OcP47w7oQjKi1ykq+dvPMkc4N S1VwxIqwNsaFM+g5I9c/LsZhmNJxeP0ng8OgQp7wm+o9U2n1/Qsi5CPTpDi1WivsKxSZ 3N5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=DVS54A5kJdocCGaXugf8vxYe7POG82IINRlRrULnP3U=; b=0wuL1GoCvHnXbtrRkeU/L4z8r3Spphqyr8LK8NaowOPx+sWr+3rHUsXlSjKfJpixbW YYnc6FT/kbdBu5cf4x/eMHN5qQu9s4GiHBdlOzEJMulm9u0B4tBuOJC2Z2tvMRCW/0GT XaARiPBFsCspKEZDeG7Ps+xEXQ6TqCL8zRfn59aCLGW8AJe9uRRrdfLt4C7RabXIqra1 HsFKWucv6OAefo/Wgt4fg7wxuPReDXXhnxy9F16gkPgFXFnf/5o8x91MPO57bbuEDEx3 MROJkXMvdlvBN6A6JIcmAeUTW7syTRrSxjf8jMoEwx127l05sqySnOEaz0k52tkrf/xE cZlg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m15si21519595edc.235.2021.12.13.07.22.20; Mon, 13 Dec 2021 07:22:22 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E306168AF81; Mon, 13 Dec 2021 17:22:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E218F68807F for ; Mon, 13 Dec 2021 17:22:08 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 7195B24068F for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id YXNKqvRmeemU for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 3459D24017C for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 167A63A0743; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:22 +0100 Message-Id: <20211213152042.5900-4-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/24] ffmpeg: store the output file index in OutputFile X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: YCnpi7SBKfDG Use it to simplify check_init_output_file(). Will allow further simplifications in the following commits. --- fftools/ffmpeg.c | 10 +++++----- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index fb017a1e37..564ab01335 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2937,7 +2937,7 @@ static int compare_int64(const void *a, const void *b) } /* open the muxer when all the streams are initialized */ -static int check_init_output_file(OutputFile *of, int file_index) +static int check_init_output_file(OutputFile *of) { int ret, i; @@ -2952,13 +2952,13 @@ static int check_init_output_file(OutputFile *of, int file_index) av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " "(incorrect codec parameters ?): %s\n", - file_index, av_err2str(ret)); + of->index, av_err2str(ret)); return ret; } //assert_avoptions(of->opts); of->header_written = 1; - av_dump_format(of->ctx, file_index, of->ctx->url, 1); + av_dump_format(of->ctx, of->index, of->ctx->url, 1); nb_output_dumped++; if (sdp_filename || want_sdp) { @@ -3561,7 +3561,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->initialized = 1; - ret = check_init_output_file(output_files[ost->file_index], ost->file_index); + ret = check_init_output_file(output_files[ost->file_index]); if (ret < 0) return ret; @@ -3664,7 +3664,7 @@ static int transcode_init(void) for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = check_init_output_file(output_files[i], i); + ret = check_init_output_file(output_files[i]); if (ret < 0) goto dump_format; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 9b200b806a..5fd5d2606b 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -574,6 +574,8 @@ typedef struct OutputStream { } OutputStream; typedef struct OutputFile { + int index; + AVFormatContext *ctx; AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index f638edace9..e8db515e0c 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2283,6 +2283,7 @@ static int open_output_file(OptionsContext *o, const char *filename) of = ALLOC_ARRAY_ELEM(output_files, nb_output_files); + of->index = nb_output_files - 1; of->ost_index = nb_output_streams; of->recording_time = o->recording_time; of->start_time = o->start_time; From patchwork Mon Dec 13 15:20:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32427 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5573583iog; Mon, 13 Dec 2021 07:23:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwk1PCN+sGXUgDiiYsKCvAaCStvzA2u234L1trPPR8zWgQoXLa8aWG/Msu8U6CdPexhZ8qF X-Received: by 2002:a17:907:7b9f:: with SMTP id ne31mr1188641ejc.592.1639409011300; Mon, 13 Dec 2021 07:23:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409011; cv=none; d=google.com; s=arc-20160816; b=UiKpl6jquZW0OuGIq8QDZbyWXt4WX+t6pdsZugt96t3NGSsxgHI4oGYecrT2p0FHY8 eiVhRbG3cGQFzC8kTBFTpoEbCjz8vx/aKpqLPDnTntACm+Yz9so619Ji3makcfgbU13d rE0Q7Tr0k+setrv7Inm6h2YLtv0kma0mMWBJDR5MILNd9jO+80Gg3MkG2suq8VHkuy+O cQniic32S4l14br5q12j0hMdtdAgpnkR12geQFB75CMkGLvhAakI2Pba4oTtHx1BI4jo VtNBJC6sVdOIKoINRjCRNF3O3HPGGTn0aqLRyiyyCi/1nVpKb7ZV97DLnG5b4gV6Qcrl V4rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=5EhKaT3/ZSei25A9GK7qz20GlhAckfmxgVe8eC1kubo=; b=gWtuoQ8rb4ZuNSA7NnRcaD3hANiS5lhGvTabjIxeblZTQz8aZg8BsbcxIH+3ZcuTI8 ZTzFBf4svGemx3RGvsJTpIJv8sNmipBao4Ph0bb8Ctt2boqUsX5YfgGLr2QVwfrIhGwn +P409GgVqimsXeqfDMNDv9OsWKPvfjK/I/mMCEZKWUws/D4OnZ4oHPfVr7pSwmbHt8Fq txoireP1NVpCT4wx2XLKiQS7t3cM45ACANjyD6V+6HNvlGePwSjrQw33JdWZ3bdwxcJ/ 3p2ipqP+JxqaiQhFOZQzg31Cjmb99ixFgim/L8kziUSI0LDrtckMCo9kYmGcIE1RjRiv 7X0Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id sc16si23952872ejc.599.2021.12.13.07.23.30; Mon, 13 Dec 2021 07:23:31 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F24DA68AFEF; Mon, 13 Dec 2021 17:22:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FA2268AFA4 for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 24D0A24050B for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id CkgsdMfHrk8E for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 4AC43240506 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1B7763A0746; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:23 +0100 Message-Id: <20211213152042.5900-5-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/24] ffmpeg: move some muxing-related code into a separate file X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: hGG9MBs5O5QW This is a first step towards making muxers more independent from the rest of the code. --- fftools/Makefile | 11 +- fftools/ffmpeg.c | 273 ++-------------------------------------- fftools/ffmpeg.h | 10 ++ fftools/ffmpeg_mux.c | 293 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+), 267 deletions(-) create mode 100644 fftools/ffmpeg_mux.c diff --git a/fftools/Makefile b/fftools/Makefile index da420786eb..bd90ef8f32 100644 --- a/fftools/Makefile +++ b/fftools/Makefile @@ -9,7 +9,16 @@ AVBASENAMES = ffmpeg ffplay ffprobe ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF)) ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF)) -OBJS-ffmpeg += fftools/ffmpeg_opt.o fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o +OBJS-ffmpeg += \ + fftools/ffmpeg_filter.o \ + fftools/ffmpeg_hw.o \ + fftools/ffmpeg_mux.o \ + fftools/ffmpeg_opt.o \ + +ifndef CONFIG_VIDEOTOOLBOX +OBJS-ffmpeg-$(CONFIG_VDA) += fftools/ffmpeg_videotoolbox.o +endif +OBJS-ffmpeg-$(CONFIG_VIDEOTOOLBOX) += fftools/ffmpeg_videotoolbox.o define DOFFTOOL OBJS-$(1) += fftools/cmdutils.o fftools/$(1).o $(OBJS-$(1)-yes) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 564ab01335..a00fe58063 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -136,9 +136,9 @@ static int nb_frames_dup = 0; static unsigned dup_warning = 1000; static int nb_frames_drop = 0; static int64_t decode_error_stat[2]; -static unsigned nb_output_dumped = 0; +unsigned nb_output_dumped = 0; -static int want_sdp = 1; +int want_sdp = 1; static BenchmarkTimeStamps current_time; AVIOContext *progress_avio = NULL; @@ -344,7 +344,7 @@ static volatile int received_sigterm = 0; static volatile int received_nb_signals = 0; static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0); static volatile int ffmpeg_exited = 0; -static int main_return_code = 0; +int main_return_code = 0; static int64_t copy_ts_first_pts = AV_NOPTS_VALUE; static void @@ -715,159 +715,6 @@ static void update_benchmark(const char *fmt, ...) } } -static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) -{ - int i; - for (i = 0; i < nb_output_streams; i++) { - OutputStream *ost2 = output_streams[i]; - ost2->finished |= ost == ost2 ? this_stream : others; - } -} - -static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue) -{ - AVFormatContext *s = of->ctx; - AVStream *st = ost->st; - int ret; - - /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - * Do not count the packet when unqueued because it has been counted when queued. - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; - } - - if (!of->header_written) { - AVPacket *tmp_pkt; - /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_space(ost->muxing_queue)) { - size_t cur_size = av_fifo_size(ost->muxing_queue); - unsigned int are_we_over_size = - (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; - size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX; - size_t new_size = FFMIN(2 * cur_size, limit); - - if (new_size <= cur_size) { - av_log(NULL, AV_LOG_ERROR, - "Too many packets buffered for output stream %d:%d.\n", - ost->file_index, ost->st->index); - exit_program(1); - } - ret = av_fifo_realloc2(ost->muxing_queue, new_size); - if (ret < 0) - exit_program(1); - } - ret = av_packet_make_refcounted(pkt); - if (ret < 0) - exit_program(1); - tmp_pkt = av_packet_alloc(); - if (!tmp_pkt) - exit_program(1); - av_packet_move_ref(tmp_pkt, pkt); - ost->muxing_queue_data_size += tmp_pkt->size; - av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); - return; - } - - if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || - (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) - pkt->pts = pkt->dts = AV_NOPTS_VALUE; - - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { - int i; - uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, - NULL); - ost->quality = sd ? AV_RL32(sd) : -1; - ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; - - for (i = 0; ierror); i++) { - if (sd && i < sd[5]) - ost->error[i] = AV_RL64(sd + 8 + 8*i); - else - ost->error[i] = -1; - } - - if (ost->frame_rate.num && ost->is_cfr) { - if (pkt->duration > 0) - av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); - pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), - ost->mux_timebase); - } - } - - av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); - - if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { - if (pkt->dts != AV_NOPTS_VALUE && - pkt->pts != AV_NOPTS_VALUE && - pkt->dts > pkt->pts) { - av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n", - pkt->dts, pkt->pts, - ost->file_index, ost->st->index); - pkt->pts = - pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 - - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) - - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); - } - if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) && - pkt->dts != AV_NOPTS_VALUE && - !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->stream_copy) && - ost->last_mux_dts != AV_NOPTS_VALUE) { - int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); - if (pkt->dts < max) { - int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; - if (exit_on_error) - loglevel = AV_LOG_ERROR; - av_log(s, loglevel, "Non-monotonous DTS in output stream " - "%d:%d; previous: %"PRId64", current: %"PRId64"; ", - ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); - if (exit_on_error) { - av_log(NULL, AV_LOG_FATAL, "aborting.\n"); - exit_program(1); - } - av_log(s, loglevel, "changing to %"PRId64". This may result " - "in incorrect timestamps in the output file.\n", - max); - if (pkt->pts >= pkt->dts) - pkt->pts = FFMAX(pkt->pts, max); - pkt->dts = max; - } - } - } - ost->last_mux_dts = pkt->dts; - - ost->data_size += pkt->size; - ost->packets_written++; - - pkt->stream_index = ost->index; - - if (debug_ts) { - av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " - "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n", - av_get_media_type_string(ost->enc_ctx->codec_type), - av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base), - av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base), - pkt->size - ); - } - - ret = av_interleaved_write_frame(s, pkt); - if (ret < 0) { - print_error("av_interleaved_write_frame()", ret); - main_return_code = 1; - close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED); - } -} - static void close_output_stream(OutputStream *ost) { OutputFile *of = output_files[ost->file_index]; @@ -902,11 +749,11 @@ static void output_packet(OutputFile *of, AVPacket *pkt, if (ret < 0) goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - write_packet(of, pkt, ost, 0); + of_write_packet(of, pkt, ost, 0); if (ret == AVERROR(EAGAIN)) ret = 0; } else if (!eof) - write_packet(of, pkt, ost, 0); + of_write_packet(of, pkt, ost, 0); finish: if (ret < 0 && ret != AVERROR_EOF) { @@ -2757,59 +2604,6 @@ static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eo return !eof_reached; } -static int print_sdp(void) -{ - char sdp[16384]; - int i; - int j, ret; - AVIOContext *sdp_pb; - AVFormatContext **avc; - - for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->header_written) - return 0; - } - - avc = av_malloc_array(nb_output_files, sizeof(*avc)); - if (!avc) - exit_program(1); - for (i = 0, j = 0; i < nb_output_files; i++) { - if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { - avc[j] = output_files[i]->ctx; - j++; - } - } - - if (!j) { - av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); - ret = AVERROR(EINVAL); - goto fail; - } - - ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); - if (ret < 0) - goto fail; - - if (!sdp_filename) { - printf("SDP:\n%s\n", sdp); - fflush(stdout); - } else { - ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); - goto fail; - } - - avio_print(sdp_pb, sdp); - avio_closep(&sdp_pb); - av_freep(&sdp_filename); - } - -fail: - av_freep(&avc); - return ret; -} - static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts) { InputStream *ist = s->opaque; @@ -2936,59 +2730,6 @@ static int compare_int64(const void *a, const void *b) return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b); } -/* open the muxer when all the streams are initialized */ -static int check_init_output_file(OutputFile *of) -{ - int ret, i; - - for (i = 0; i < of->ctx->nb_streams; i++) { - OutputStream *ost = output_streams[of->ost_index + i]; - if (!ost->initialized) - return 0; - } - - ret = avformat_write_header(of->ctx, &of->opts); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, - "Could not write header for output file #%d " - "(incorrect codec parameters ?): %s\n", - of->index, av_err2str(ret)); - return ret; - } - //assert_avoptions(of->opts); - of->header_written = 1; - - av_dump_format(of->ctx, of->index, of->ctx->url, 1); - nb_output_dumped++; - - if (sdp_filename || want_sdp) { - ret = print_sdp(); - if (ret < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n"); - return ret; - } - } - - /* flush the muxing queues */ - for (i = 0; i < of->ctx->nb_streams; i++) { - OutputStream *ost = output_streams[of->ost_index + i]; - - /* try to improve muxing time_base (only possible if nothing has been written yet) */ - if (!av_fifo_size(ost->muxing_queue)) - ost->mux_timebase = ost->st->time_base; - - while (av_fifo_size(ost->muxing_queue)) { - AVPacket *pkt; - av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); - ost->muxing_queue_data_size -= pkt->size; - write_packet(of, pkt, ost, 1); - av_packet_free(&pkt); - } - } - - return 0; -} - static int init_output_bsfs(OutputStream *ost) { AVBSFContext *ctx = ost->bsf_ctx; @@ -3561,7 +3302,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->initialized = 1; - ret = check_init_output_file(output_files[ost->file_index]); + ret = of_check_init(output_files[ost->file_index]); if (ret < 0) return ret; @@ -3664,7 +3405,7 @@ static int transcode_init(void) for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = check_init_output_file(output_files[i]); + ret = of_check_init(output_files[i]); if (ret < 0) goto dump_format; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 5fd5d2606b..fed34b06f8 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -644,6 +644,10 @@ extern char *qsv_device; #endif extern HWDevice *filter_hw_device; +extern int want_sdp; +extern unsigned nb_output_dumped; +extern int main_return_code; + void term_init(void); void term_exit(void); @@ -680,4 +684,10 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); +/* open the muxer when all the streams are initialized */ +int of_check_init(OutputFile *of); + +void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, + int unqueue); + #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c new file mode 100644 index 0000000000..e7c6ddd8f8 --- /dev/null +++ b/fftools/ffmpeg_mux.c @@ -0,0 +1,293 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavformat/avformat.h" +#include "libavformat/avio.h" + +#include "libavcodec/packet.h" + +#include "libavutil/fifo.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/log.h" +#include "libavutil/mem.h" +#include "libavutil/timestamp.h" + +#include "ffmpeg.h" + +static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) +{ + int i; + for (i = 0; i < nb_output_streams; i++) { + OutputStream *ost2 = output_streams[i]; + ost2->finished |= ost == ost2 ? this_stream : others; + } +} + +void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, + int unqueue) +{ + AVFormatContext *s = of->ctx; + AVStream *st = ost->st; + int ret; + + /* + * Audio encoders may split the packets -- #frames in != #packets out. + * But there is no reordering, so we can limit the number of output packets + * by simply dropping them here. + * Counting encoded video frames needs to be done separately because of + * reordering, see do_video_out(). + * Do not count the packet when unqueued because it has been counted when queued. + */ + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; + } + ost->frame_number++; + } + + if (!of->header_written) { + AVPacket *tmp_pkt; + /* the muxer is not initialized yet, buffer the packet */ + if (!av_fifo_space(ost->muxing_queue)) { + size_t cur_size = av_fifo_size(ost->muxing_queue); + unsigned int are_we_over_size = + (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX; + size_t new_size = FFMIN(2 * cur_size, limit); + + if (new_size <= cur_size) { + av_log(NULL, AV_LOG_ERROR, + "Too many packets buffered for output stream %d:%d.\n", + ost->file_index, ost->st->index); + exit_program(1); + } + ret = av_fifo_realloc2(ost->muxing_queue, new_size); + if (ret < 0) + exit_program(1); + } + ret = av_packet_make_refcounted(pkt); + if (ret < 0) + exit_program(1); + tmp_pkt = av_packet_alloc(); + if (!tmp_pkt) + exit_program(1); + av_packet_move_ref(tmp_pkt, pkt); + ost->muxing_queue_data_size += tmp_pkt->size; + av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); + return; + } + + if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || + (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) + pkt->pts = pkt->dts = AV_NOPTS_VALUE; + + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + int i; + uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, + NULL); + ost->quality = sd ? AV_RL32(sd) : -1; + ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE; + + for (i = 0; ierror); i++) { + if (sd && i < sd[5]) + ost->error[i] = AV_RL64(sd + 8 + 8*i); + else + ost->error[i] = -1; + } + + if (ost->frame_rate.num && ost->is_cfr) { + if (pkt->duration > 0) + av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n"); + pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate), + ost->mux_timebase); + } + } + + av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base); + + if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) { + if (pkt->dts != AV_NOPTS_VALUE && + pkt->pts != AV_NOPTS_VALUE && + pkt->dts > pkt->pts) { + av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n", + pkt->dts, pkt->pts, + ost->file_index, ost->st->index); + pkt->pts = + pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1 + - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1) + - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1); + } + if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) && + pkt->dts != AV_NOPTS_VALUE && + !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->stream_copy) && + ost->last_mux_dts != AV_NOPTS_VALUE) { + int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT); + if (pkt->dts < max) { + int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG; + if (exit_on_error) + loglevel = AV_LOG_ERROR; + av_log(s, loglevel, "Non-monotonous DTS in output stream " + "%d:%d; previous: %"PRId64", current: %"PRId64"; ", + ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts); + if (exit_on_error) { + av_log(NULL, AV_LOG_FATAL, "aborting.\n"); + exit_program(1); + } + av_log(s, loglevel, "changing to %"PRId64". This may result " + "in incorrect timestamps in the output file.\n", + max); + if (pkt->pts >= pkt->dts) + pkt->pts = FFMAX(pkt->pts, max); + pkt->dts = max; + } + } + } + ost->last_mux_dts = pkt->dts; + + ost->data_size += pkt->size; + ost->packets_written++; + + pkt->stream_index = ost->index; + + if (debug_ts) { + av_log(NULL, AV_LOG_INFO, "muxer <- type:%s " + "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n", + av_get_media_type_string(ost->enc_ctx->codec_type), + av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base), + av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base), + pkt->size + ); + } + + ret = av_interleaved_write_frame(s, pkt); + if (ret < 0) { + print_error("av_interleaved_write_frame()", ret); + main_return_code = 1; + close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED); + } +} + +static int print_sdp(void) +{ + char sdp[16384]; + int i; + int j, ret; + AVIOContext *sdp_pb; + AVFormatContext **avc; + + for (i = 0; i < nb_output_files; i++) { + if (!output_files[i]->header_written) + return 0; + } + + avc = av_malloc_array(nb_output_files, sizeof(*avc)); + if (!avc) + exit_program(1); + for (i = 0, j = 0; i < nb_output_files; i++) { + if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) { + avc[j] = output_files[i]->ctx; + j++; + } + } + + if (!j) { + av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + ret = av_sdp_create(avc, j, sdp, sizeof(sdp)); + if (ret < 0) + goto fail; + + if (!sdp_filename) { + printf("SDP:\n%s\n", sdp); + fflush(stdout); + } else { + ret = avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename); + goto fail; + } + + avio_print(sdp_pb, sdp); + avio_closep(&sdp_pb); + av_freep(&sdp_filename); + } + +fail: + av_freep(&avc); + return ret; +} + +/* open the muxer when all the streams are initialized */ +int of_check_init(OutputFile *of) +{ + int ret, i; + + for (i = 0; i < of->ctx->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + if (!ost->initialized) + return 0; + } + + ret = avformat_write_header(of->ctx, &of->opts); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, + "Could not write header for output file #%d " + "(incorrect codec parameters ?): %s\n", + of->index, av_err2str(ret)); + return ret; + } + //assert_avoptions(of->opts); + of->header_written = 1; + + av_dump_format(of->ctx, of->index, of->ctx->url, 1); + nb_output_dumped++; + + if (sdp_filename || want_sdp) { + ret = print_sdp(); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error writing the SDP.\n"); + return ret; + } + } + + /* flush the muxing queues */ + for (i = 0; i < of->ctx->nb_streams; i++) { + OutputStream *ost = output_streams[of->ost_index + i]; + + /* try to improve muxing time_base (only possible if nothing has been written yet) */ + if (!av_fifo_size(ost->muxing_queue)) + ost->mux_timebase = ost->st->time_base; + + while (av_fifo_size(ost->muxing_queue)) { + AVPacket *pkt; + av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); + ost->muxing_queue_data_size -= pkt->size; + of_write_packet(of, pkt, ost, 1); + av_packet_free(&pkt); + } + } + + return 0; +} From patchwork Mon Dec 13 15:20:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32425 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5573125iog; Mon, 13 Dec 2021 07:23:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcdT6szUch30s/rfS9FEeKw5FUuRLWLPfxB+ZxcaX0zZPa/CjwenkEGvR/K6KBNqsm+sJp X-Received: by 2002:a50:ce46:: with SMTP id k6mr64095540edj.45.1639408989255; Mon, 13 Dec 2021 07:23:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639408989; cv=none; d=google.com; s=arc-20160816; b=HTzVDdSTs9FvPp/2vhEP5kyP2iPvqdbZ7zUu014AmG+V1Ao6GFbSb2BKG71pi20ewT Jqzewhmd2tvZkj+jYqOuXIDgPbs0NhsdWwHOpyW9VszaCMRfk/goEI8YJIBoulgLnJH8 ARE5hrfpHVcBPUNTyqlp+TBRPE1Uk+v0UiT47ebxdXgb10NyxjL78rQV5VsA6cZtzFtR D8xlwWVY3zvPmGvIim9warhee27ncIyS9TQMWaxavKQLCFB6PYEwKDch1B9Xh5cWVVbv bekvqbLMchRXkB2adtCeqPhrPVeOMfhA/ofK526/H32VY6H3Ed+ubX+U0gyxirWUr+st 3yxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=yODfgVJi+TFd/3w8DTVFaxGhhqbrAPhkLFskU7MhU0M=; b=yu8WVWEq1WlS0TC1u+GP6X1GsOhmK9QwNYQsQWm3a+vHJLgjy0rb0VHJEfXAmIJDkp UWkJS/CSA7/V+sSkmQhAFM2Lgrh8Xaa1DxJhuCuJCEYzd7M2UWj5pPDotyzyuaN/uHjz UI8/YQ/jXKY4Qxl/diSd31nFnZ2OfbOenHUIjBFEuVh2cThMAp4T3Q1+VqXjICZ7E7Bc C9W2ohlchRhz2OFv2YOQBuE/GHecT9dqDL0bhGOnz05wXQG/KB71aSxeYb1PhNdhPTA5 gufaotf/C/PpNnQvWIHfTjd59kI8OpuF9b7kROojGBrjPj6/pJ/FRgHd2mZCznH+j3oJ JDLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id lf8si15000915ejb.635.2021.12.13.07.23.08; Mon, 13 Dec 2021 07:23:09 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 20A1368AFBC; Mon, 13 Dec 2021 17:22:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EB24568807F for ; Mon, 13 Dec 2021 17:22:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 861B32404FE for ; Mon, 13 Dec 2021 16:22:09 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id EiiUcL44btjH for ; Mon, 13 Dec 2021 16:22:09 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 8E667240507 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 1FB313A0769; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:24 +0100 Message-Id: <20211213152042.5900-6-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/24] ffmpeg: move writing the trailer to ffmpeg_mux.c X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: MIDhkTALt8bx --- fftools/ffmpeg.c | 16 +++------------- fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a00fe58063..1fb10869b4 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -4436,19 +4436,9 @@ static int transcode(void) /* write the trailer if needed */ for (i = 0; i < nb_output_files; i++) { - os = output_files[i]->ctx; - if (!output_files[i]->header_written) { - av_log(NULL, AV_LOG_ERROR, - "Nothing was written into output file %d (%s), because " - "at least one of its streams received no packets.\n", - i, os->url); - continue; - } - if ((ret = av_write_trailer(os)) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", os->url, av_err2str(ret)); - if (exit_on_error) - exit_program(1); - } + ret = of_write_trailer(output_files[i]); + if (exit_on_error) + exit_program(1); } /* dump report by using the first video and audio streams */ diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fed34b06f8..91c313d8ef 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -686,6 +686,7 @@ int hwaccel_decode_init(AVCodecContext *avctx); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); +int of_write_trailer(OutputFile *of); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e7c6ddd8f8..fec8537506 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -291,3 +291,24 @@ int of_check_init(OutputFile *of) return 0; } + +int of_write_trailer(OutputFile *of) +{ + int ret; + + if (!of->header_written) { + av_log(NULL, AV_LOG_ERROR, + "Nothing was written into output file %d (%s), because " + "at least one of its streams received no packets.\n", + of->index, of->ctx->url); + return AVERROR(EINVAL); + } + + ret = av_write_trailer(of->ctx); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", of->ctx->url, av_err2str(ret)); + return ret; + } + + return 0; +} From patchwork Mon Dec 13 15:20:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32428 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5573877iog; Mon, 13 Dec 2021 07:23:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxI9lqpdlCyJLpn1jbh7c1rPdgprWKwOXdkck9RwJ2MdxH8whLARfVx36fnee151VgAEgZL X-Received: by 2002:a05:6402:26d4:: with SMTP id x20mr66367580edd.119.1639409023713; Mon, 13 Dec 2021 07:23:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409023; cv=none; d=google.com; s=arc-20160816; b=y+enJ21E+BDSL5DB4YkO6r8BgEuCdd/h89kejZI2lT3tIZJUobgesspjob0hpNSWgJ oaOvSgwQ9VJwX6alG/lnxFWyiX48DfRnuq9iikAYhZP09Yysmh7ghkNuTM+rr3BRGaSr sZ7sQ8htTmB356O7QpBMcBTcpJlhK3+EYJ5bdYb9q8RJyilVWiAQgMjPBHpHnVpuXsiY 4luJx03dfZY5B6NWBCKw69MUjHSyaR2Y8LT8AwhhZ4FD4xNwsBJDMsORh/SyWa7GSpMR BRmMmXZ3Gg5XjBa+fnwu1g80jrMBOLnec93Jm02adpRBqFMJa14PdOyYlyyR6B3ICvRG sD4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=deJMCb0aAphpLf7IC4MWzFIwyhzledwc8yG7hQUoWDE=; b=ggVzQi09XCfxbxR5N2dbWUMZPE9NRn1qa0VmxBfA3Wjg4LtdGBOciy2/kYhcrDOwnW tylhuZu+t2JG4UMOrkgapAx6lT7rsD5L9ZOMBqUy3Nfe6flMqSCuHAe26Zr9VhS9CjOh WFXFogukb2ONGV/cb2zwC0N4+VkExt7lwe5Ye83KSfxnE/YBnZox5N+gGMaYW/lCYJLY 3T0EIJ6EhpUHkjM/e7SX1fWHPRoCbUnqzxk0qmqDZuiEkF0IpJA7O3+SrOll9bodUDTB EoU1c1sux7YtCfRsIx0gxJRKYNNr7Kmyt8Io3kkA756daXfdoWI2NkQkOZ6l1B6p1PHq sLLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s2si16526443edx.402.2021.12.13.07.23.43; Mon, 13 Dec 2021 07:23:43 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C7EEB68AFF3; Mon, 13 Dec 2021 17:22:22 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 57D3668AFA8 for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 95B3C240506 for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id M_9RVyRqI7gk for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 958A0240512 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 23E483A078F; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:25 +0100 Message-Id: <20211213152042.5900-7-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/24] ffmpeg: move freeing the output file to ffmpeg_mux.c X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: bKnP40sjhVxi --- fftools/ffmpeg.c | 14 ++------------ fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 1fb10869b4..86f7b11654 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -567,19 +567,9 @@ static void ffmpeg_cleanup(int ret) av_freep(&subtitle_out); /* close files */ - for (i = 0; i < nb_output_files; i++) { - OutputFile *of = output_files[i]; - AVFormatContext *s; - if (!of) - continue; - s = of->ctx; - if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) - avio_closep(&s->pb); - avformat_free_context(s); - av_dict_free(&of->opts); + for (i = 0; i < nb_output_files; i++) + of_close(&output_files[i]); - av_freep(&output_files[i]); - } for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 91c313d8ef..288e0f2981 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -687,6 +687,7 @@ int hwaccel_decode_init(AVCodecContext *avctx); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); +void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index fec8537506..9787fe7f78 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -312,3 +312,20 @@ int of_write_trailer(OutputFile *of) return 0; } + +void of_close(OutputFile **pof) +{ + OutputFile *of = *pof; + AVFormatContext *s; + + if (!of) + return; + + s = of->ctx; + if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) + avio_closep(&s->pb); + avformat_free_context(s); + av_dict_free(&of->opts); + + av_freep(pof); +} From patchwork Mon Dec 13 15:20:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32429 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5574191iog; Mon, 13 Dec 2021 07:23:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxtazkJmJ5u6dt5LHKKlJUhv7Zm0oZuWwTAJ7v0T0UQv4maME1XOiggAkExOfZ3iWBO/oaa X-Received: by 2002:a05:6402:50c7:: with SMTP id h7mr66305047edb.277.1639409036550; Mon, 13 Dec 2021 07:23:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409036; cv=none; d=google.com; s=arc-20160816; b=KRh8lhfuonPpJDO2TdE9USTKkeAbkuCOHQrUtd5ys9LBeURU7TMu/RZZFQSIi6dlGx YtJh/ffaDPiUV4tPfbAsdv7ACzqsZ/ZRRxNXWdkjUArGUSO30JnvP2Scg32nTDwcRjsd BznVp+r9ss2GaS5ECH/R6BnD5jw+LA88DQD0aMoJXc3MsWLYi4uORd6XaOrKH+1iokV9 hd9kOGLKdR3VEQdNToCNBuTjeGsOyZJUL62S1e/rn7e2xoZWkwY/+54oNcCVufFuDC3l AsSktXotqcyBU7qS4OVJpL0F2mOtgH3rJe49lE0n7cn6NuWXxhRCii7Ervt9dkG006HT l9Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=6rJ8gOael32n2UybwjXDLerwOebjWt/h7KcK3DxFRZw=; b=EYtXez5oi25ELh2GJzRcHQRgzs9yg1j1QFqy/VAHk+ABFn1Q6sNQj2A3M57vPrYqiH 5RI/+VErbB+WAUyPL8l30qcU7WO+uMpQ1ajYEWaGCPckTLYifj7Im6uzXrhZS2Wnqe+U QS7hYOGOXkLa8bf1BTGYX4P/qB5vUur+mQtmP67DBn3ryZJVpVkcHeSVgksiAZNFJAzk 2SXqN0h34Bkox62eN3trjaJ3LBTWh4ZGSxB20C4F2Q+3U/g6l55Go4zm7EXV+vsxWZZL WXgGol2TztJdt9ZLk9Q8wwRQhlm/rjTVqvkwTVr6/6U1ckb87lPNnX5X0oi8YL/Sv6Ip U+jw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ji10si22022574ejc.539.2021.12.13.07.23.54; Mon, 13 Dec 2021 07:23:56 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D040668AF9F; Mon, 13 Dec 2021 17:22:23 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5DC7368AFAA for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id BCC05240512 for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Q39kKl36e-05 for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 94730240511 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 281A93A0804; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:26 +0100 Message-Id: <20211213152042.5900-8-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/24] ffmpeg: store output format separately from the muxer context X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8rJtz6bIbd9Y Allows accessing it without going through the muxer context. This will be useful in the following commits, where the muxer context will be hidden. --- fftools/ffmpeg.c | 18 ++++++++++-------- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 1 + 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 86f7b11654..8a7cab3fab 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2779,9 +2779,9 @@ static int init_output_stream_streamcopy(OutputStream *ost) if (!codec_tag) { unsigned int codec_tag_tmp; - if (!of->ctx->oformat->codec_tag || - av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id || - !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp)) + if (!of->format->codec_tag || + av_codec_get_id (of->format->codec_tag, par_src->codec_tag) == par_src->codec_id || + !av_codec_get_tag2(of->format->codec_tag, par_src->codec_id, &codec_tag_tmp)) codec_tag = par_src->codec_tag; } @@ -2799,7 +2799,7 @@ static int init_output_stream_streamcopy(OutputStream *ost) else ost->st->avg_frame_rate = ist->st->avg_frame_rate; - ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb); + ret = avformat_transfer_internal_stream_timing_info(of->format, ost->st, ist->st, copy_tb); if (ret < 0) return ret; @@ -3001,7 +3001,8 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) InputStream *ist = get_input_stream(ost); AVCodecContext *enc_ctx = ost->enc_ctx; AVCodecContext *dec_ctx = NULL; - AVFormatContext *oc = output_files[ost->file_index]->ctx; + OutputFile *of = output_files[ost->file_index]; + AVFormatContext *oc = of->ctx; int ret; set_encoder_id(output_files[ost->file_index], ost); @@ -3061,7 +3062,8 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) if (!(enc_ctx->time_base.num && enc_ctx->time_base.den)) enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter); if ( av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH - && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & AVFMT_VARIABLE_FPS)))){ + && (video_sync_method == VSYNC_CFR || video_sync_method == VSYNC_VSCFR || + (video_sync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){ av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n" "Please consider specifying a lower framerate, a different muxer or -vsync 2\n"); } @@ -3394,7 +3396,7 @@ static int transcode_init(void) /* write headers for files with no streams */ for (i = 0; i < nb_output_files; i++) { oc = output_files[i]->ctx; - if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { + if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { ret = of_check_init(output_files[i]); if (ret < 0) goto dump_format; @@ -4601,7 +4603,7 @@ int main(int argc, char **argv) } for (i = 0; i < nb_output_files; i++) { - if (strcmp(output_files[i]->ctx->oformat->name, "rtp")) + if (strcmp(output_files[i]->format->name, "rtp")) want_sdp = 0; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 288e0f2981..c28acad4f1 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -576,6 +576,8 @@ typedef struct OutputStream { typedef struct OutputFile { int index; + const AVOutputFormat *format; + AVFormatContext *ctx; AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index e8db515e0c..433102bb49 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2301,6 +2301,7 @@ static int open_output_file(OptionsContext *o, const char *filename) } of->ctx = oc; + of->format = oc->oformat; if (o->recording_time != INT64_MAX) oc->duration = o->recording_time; From patchwork Mon Dec 13 15:20:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32430 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5574426iog; Mon, 13 Dec 2021 07:24:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyWI+RREPVhm3oPUk7Fax4tJ2Yd6B8Y3Snr7YeCxrWqibBGtlVyTYH7bOB0F/pVDKy42g/p X-Received: by 2002:a17:906:b51:: with SMTP id v17mr45857685ejg.262.1639409045602; Mon, 13 Dec 2021 07:24:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409045; cv=none; d=google.com; s=arc-20160816; b=syTLhwzqRhkyPxW9CPNxHw3KHkpFUs++SVBHjJ6qHvpe1/+S6SbMAEao24dqHKsX/s RBvKEKsJ/clkCmPddqjj4fxxYh1qA7cRnirnsG1H8JQDC3u4rVNYYW0zDFofaRXsd6kA URpPAVhLA+J+eQqm66yergNWG9MEBtCFn02BPsSRxLFI1U0ujo2zgoSjMyWceJeYFHvw FAKcT4+pX1Pa2LL/20m2LdXwc6PEoeUE/Ov+XzulTqiBEvT8uQbKzsbxis+47SuXhtXf 4FaOfnrKQO3Q1tlBzHbXVMVoXRiAnqPK6OnSz9ZZte9amZjVjcFVVDoHYt0FiLsah8yw 5YFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=PoarOX/+2TH4b4Y6RAhiUzcHpc9MEEbAIIdAcVuYqHo=; b=aVVvIRD55nfYSr68WQqqrlMIVJMSTEA+mT//TglEWZ6G7zYaBFGQRVBxVpiadV4TTb cGtdV7CbfLKajT+sXF0q/iGTbL3E6vr8oA5ad5t4O6BHe83pAbUH9a4ER1KDjm7Jgber 8OeDLi2S2/GhNSZBJFlrZzar4G/Y7T47Ga2IKHM0bEHfPIaJ/su7BP4daG6AwW8yXghi tkvTQunn1ERvJA7pguxnoKeq/lVq2Tm60nCJbNTBzzG0IZr3oke0PqPaeTnmM8kTO7hN 8nu3ykIWGEPpsw1+hYq1ZCNzHknC1iTTgglHteRuaR0LUF8D97U6maXdwMceG4FU19hN Umog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id e15si22656449edz.579.2021.12.13.07.24.05; Mon, 13 Dec 2021 07:24:05 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 183FF68AF8F; Mon, 13 Dec 2021 17:22:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6D19368AFAD for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 36047240511 for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id cAOn4QRjPgjw for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A63AA24055A for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 2CC763A09CE; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:27 +0100 Message-Id: <20211213152042.5900-9-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/24] ffmpeg_mux: add private muxer context X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 10GRdq2qufWP Move header_written into it, which is not (and should not be) used by any code outside of ffmpeg_mux. In the future this context will contain more muxer-private state that should not be visible to other code. --- fftools/ffmpeg.h | 6 ++++-- fftools/ffmpeg_mux.c | 26 ++++++++++++++++++++++---- fftools/ffmpeg_opt.c | 6 ++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index c28acad4f1..279a99cc48 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -573,9 +573,12 @@ typedef struct OutputStream { int64_t error[4]; } OutputStream; +typedef struct Muxer Muxer; + typedef struct OutputFile { int index; + Muxer *mux; const AVOutputFormat *format; AVFormatContext *ctx; @@ -586,8 +589,6 @@ typedef struct OutputFile { uint64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; - - int header_written; } OutputFile; extern InputStream **input_streams; @@ -686,6 +687,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); +int of_muxer_init(OutputFile *of); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 9787fe7f78..cf85db674e 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -32,6 +32,10 @@ #include "ffmpeg.h" +struct Muxer { + int header_written; +}; + static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) { int i; @@ -64,7 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->frame_number++; } - if (!of->header_written) { + if (!of->mux->header_written) { AVPacket *tmp_pkt; /* the muxer is not initialized yet, buffer the packet */ if (!av_fifo_space(ost->muxing_queue)) { @@ -195,7 +199,7 @@ static int print_sdp(void) AVFormatContext **avc; for (i = 0; i < nb_output_files; i++) { - if (!output_files[i]->header_written) + if (!output_files[i]->mux->header_written) return 0; } @@ -259,7 +263,7 @@ int of_check_init(OutputFile *of) return ret; } //assert_avoptions(of->opts); - of->header_written = 1; + of->mux->header_written = 1; av_dump_format(of->ctx, of->index, of->ctx->url, 1); nb_output_dumped++; @@ -296,7 +300,7 @@ int of_write_trailer(OutputFile *of) { int ret; - if (!of->header_written) { + if (!of->mux->header_written) { av_log(NULL, AV_LOG_ERROR, "Nothing was written into output file %d (%s), because " "at least one of its streams received no packets.\n", @@ -327,5 +331,19 @@ void of_close(OutputFile **pof) avformat_free_context(s); av_dict_free(&of->opts); + av_freep(&of->mux); + av_freep(pof); } + +int of_muxer_init(OutputFile *of) +{ + Muxer *mux = av_mallocz(sizeof(*mux)); + + if (!mux) + return AVERROR(ENOMEM); + + of->mux = mux; + + return 0; +} diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 433102bb49..5b42b450f3 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2886,6 +2886,12 @@ loop_end: exit_program(1); } + err = of_muxer_init(of); + if (err < 0) { + av_log(NULL, AV_LOG_FATAL, "Error initializing the muxer\n"); + exit_program(1); + } + return 0; } From patchwork Mon Dec 13 15:20:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32431 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5574671iog; Mon, 13 Dec 2021 07:24:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJzetLuoHRfnuKiW0vHm5Ci15IYdZ2UpA3V7lUU2V18sA2Tw9Mj9pQB7K/ep63TRbl2UHGsm X-Received: by 2002:a17:906:974a:: with SMTP id o10mr45045524ejy.226.1639409057625; Mon, 13 Dec 2021 07:24:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409057; cv=none; d=google.com; s=arc-20160816; b=cd4Z8WESbPScOl+kIg/aZ/X43Mv1paNlNgMs+eNsw0PpjoQ+n4V57uC9ToIAvR3/NS TTU6Q5Ouwua4dLp6RDuffpWQbStfcQeaOwQ44kcEKerk1EEAJhM37aulidnU16AQQEaU GK8//cARPLgzcL3HAdmnJeqc+sYbjNEitCSeFgVmEmkceqX8Il/ze2I0+DUspL202Z4d +e8IK/LO91z/Yyz2Vn3uH5VtvLQ1cBnseteECcJ2AMjJ9SCqpbgwbJZGC0iLduYj3owa umbATdpd+iiVJ7jzjxkI3UmoGK+jN8beusnDLW4D74nTT73PcS+dOUVA5bX7b2ZYk775 AWAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=89zDMh5h9cCmjfjwVrN1dh2V17qVl7x4GmZe6JWXcAM=; b=meZrPQY5+ZWBXkcOkXr/khOUcxm4SLZnX/1yvABMubrw0fA+ofPr03B0pSF4Q9bto6 C1KcGphwrf3gBuQv20l7AhL+oYAaIb4BtjzDd12HbQDaV7OzrRLMQAv8etkpC4hcJjBw ZC/ljIwJAgh9tRCftNXuGtr3O+Tck7/rAZIREUeYzxAmPns0Ik/7Ce4gKQWaPl7G8S6A 4PTRtEDCqH7eeppPYWc0trEHiiZfK69pZDlhpimh6hkj45PhIotZLEktM0KQkooQ/KVY 2MeD4OqUgzpvZZQzMAaNS4rRsx/F2xb6HXwmlmjaBUkkRlPKAtR/V5+EMMQcMvETn0nP KQkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a5si16973737edy.585.2021.12.13.07.24.17; Mon, 13 Dec 2021 07:24:17 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3533D68AFC9; Mon, 13 Dec 2021 17:22:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 759A368AFA1 for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 40EF524055A for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 2pXlerp2nlUJ for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A55A3240555 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 3102C3A09DE; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:28 +0100 Message-Id: <20211213152042.5900-10-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/24] ffmpeg: refactor limiting output file size with -fs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: YTQtKxDzbhJ5 Move the file size checking code to ffmpeg_mux. Stop calling avio_tell(), which is a potentially heavy operation - use the recently introduced bytes_written instead. --- fftools/ffmpeg.c | 4 +--- fftools/ffmpeg.h | 4 ++-- fftools/ffmpeg_mux.c | 14 +++++++++++++- fftools/ffmpeg_opt.c | 3 +-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 8a7cab3fab..3ed1201fda 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3505,10 +3505,8 @@ static int need_output(void) for (i = 0; i < nb_output_streams; i++) { OutputStream *ost = output_streams[i]; OutputFile *of = output_files[ost->file_index]; - AVFormatContext *os = output_files[ost->file_index]->ctx; - if (ost->finished || - (os->pb && avio_tell(os->pb) >= of->limit_filesize)) + if (ost->finished || of_finished(of)) continue; if (ost->frame_number >= ost->max_frames) { int j; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 279a99cc48..67ff391334 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -586,7 +586,6 @@ typedef struct OutputFile { int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units - uint64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; } OutputFile; @@ -687,7 +686,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of); +int of_muxer_init(OutputFile *of, uint64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); @@ -695,5 +694,6 @@ void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); +int of_finished(OutputFile *of); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index cf85db674e..aba4b563a4 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -33,6 +33,8 @@ #include "ffmpeg.h" struct Muxer { + /* filesize limit expressed in bytes */ + uint64_t limit_filesize; int header_written; }; @@ -336,7 +338,7 @@ void of_close(OutputFile **pof) av_freep(pof); } -int of_muxer_init(OutputFile *of) +int of_muxer_init(OutputFile *of, uint64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); @@ -345,5 +347,15 @@ int of_muxer_init(OutputFile *of) of->mux = mux; + mux->limit_filesize = limit_filesize; + + return 0; +} + +int of_finished(OutputFile *of) +{ + AVIOContext *pb = of->ctx->pb; + if (pb && pb->bytes_written >= of->mux->limit_filesize) + return 1; return 0; } diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 5b42b450f3..09301acb03 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2287,7 +2287,6 @@ static int open_output_file(OptionsContext *o, const char *filename) of->ost_index = nb_output_streams; of->recording_time = o->recording_time; of->start_time = o->start_time; - of->limit_filesize = o->limit_filesize; of->shortest = o->shortest; av_dict_copy(&of->opts, o->g->format_opts, 0); @@ -2886,7 +2885,7 @@ loop_end: exit_program(1); } - err = of_muxer_init(of); + err = of_muxer_init(of, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing the muxer\n"); exit_program(1); From patchwork Mon Dec 13 15:20:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32426 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5573333iog; Mon, 13 Dec 2021 07:23:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJyQY1KrDj2gM94RD/ZG064RHPM7IL0pGk8jtV57yoY+OZK/Ldh1RB9HQ2Z5o0UXL7trbnxO X-Received: by 2002:a17:907:20e8:: with SMTP id rh8mr44241695ejb.437.1639409000237; Mon, 13 Dec 2021 07:23:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409000; cv=none; d=google.com; s=arc-20160816; b=o2RGhZ9vdn3u1+vDdwD+d+hz9nhU0y85jw7JHDM80ceHTDS/XLNLU90jO9cMW+mvfA lCcl8fNZKMJeN6lVHsSUCRCzGKznqPA+n8q+PJ1w6812+R+NqQQsirRRn1nAlsun+DGX HlbeILyeuSl1fYPvUNao90EI63XN+CJ07EkFin1njFozmcRJcHsoMknn1ZJBZYE71a1a ZhFLBEnQ7aXTcxYb31VR0cstCOY8j07PwIPaA4M5APj6UC6DcdbRwFYCMMzHyM0B5D2e vOhyIS89X1EFAr2QmCP3/cQ7RRJHIJ2WZPFKQnjC8u9v37BRQXXwC8ZRV4KD4hQVb+jQ el+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=s7+iZ3j/JJrTucdg54sev1F90/Sku0tfLemDStd9XwA=; b=aiMDeyC9HY8xfId1sAQdTtcKOzH3Yc/L9qpHJeAe5ED7L0qyRv3Xg7GvxBU/IiXd0N Mt8z3xoO0ek2tGbd5wY2CV+cPmPp8DnxyFGHcQWeyvfLBxRLRV6kVqyRSiH2KpVykGBs uz5mb3MNfooZ4iYQ6XBFK167KoWEZg43rifcu6+7LXUM1PljP1OvE+92qeJYmkw8rjoN VoUYH00d0D8BOMW+yrcyfT8QzV6PdfPd7kbAGcZh66LdsqMHmxmdk61aZEUxBlMBzLtH qvlqj/XRLj2z9OX6bwaJRxthu+PMZngeXrcskyOXCpUw8Z7EZddiRoLKL+fY1NvuuXKq hX0A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id mp32si19820025ejc.237.2021.12.13.07.23.19; Mon, 13 Dec 2021 07:23:20 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 02FAA68AF96; Mon, 13 Dec 2021 17:22:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4E27868AFA1 for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 1B5D8240507 for ; Mon, 13 Dec 2021 16:22:10 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 0INwrVHVfE5A for ; Mon, 13 Dec 2021 16:22:09 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 9353824050B for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 355B23A09E6; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:29 +0100 Message-Id: <20211213152042.5900-11-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 11/24] ffmpeg: set want_sdp when initializing the muxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: uSwa2UIMsmtI Allows making the variable local to ffmpeg_mux. --- fftools/ffmpeg.c | 9 +-------- fftools/ffmpeg.h | 1 - fftools/ffmpeg_mux.c | 5 +++++ 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 3ed1201fda..f76e5df8d2 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -138,8 +138,6 @@ static int nb_frames_drop = 0; static int64_t decode_error_stat[2]; unsigned nb_output_dumped = 0; -int want_sdp = 1; - static BenchmarkTimeStamps current_time; AVIOContext *progress_avio = NULL; @@ -4557,7 +4555,7 @@ static void log_callback_null(void *ptr, int level, const char *fmt, va_list vl) int main(int argc, char **argv) { - int i, ret; + int ret; BenchmarkTimeStamps ti; init_dynload(); @@ -4600,11 +4598,6 @@ int main(int argc, char **argv) exit_program(1); } - for (i = 0; i < nb_output_files; i++) { - if (strcmp(output_files[i]->format->name, "rtp")) - want_sdp = 0; - } - current_time = ti = get_benchmark_time_stamps(); if (transcode() < 0) exit_program(1); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 67ff391334..4aed24c2a7 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -646,7 +646,6 @@ extern char *qsv_device; #endif extern HWDevice *filter_hw_device; -extern int want_sdp; extern unsigned nb_output_dumped; extern int main_return_code; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index aba4b563a4..e6417cdb8c 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -38,6 +38,8 @@ struct Muxer { int header_written; }; +static int want_sdp = 1; + static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others) { int i; @@ -349,6 +351,9 @@ int of_muxer_init(OutputFile *of, uint64_t limit_filesize) mux->limit_filesize = limit_filesize; + if (strcmp(of->format->name, "rtp")) + want_sdp = 0; + return 0; } From patchwork Mon Dec 13 15:20:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32437 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5576133iog; Mon, 13 Dec 2021 07:25:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJw2bU3I/KKXPTbXB/Srz9VQhOVvuCHb44bq5wJChAv3JMGJHlR09x0rEZ7l+/4yCZQ98oOz X-Received: by 2002:a05:6402:42d5:: with SMTP id i21mr65691548edc.373.1639409128316; Mon, 13 Dec 2021 07:25:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409128; cv=none; d=google.com; s=arc-20160816; b=TEHylOfsCvpyDKWk7936frbnxUtrNN1HtS+hE6rfynb8oaIaRjdSDlds/CR38gRkny jJ3+Y37Im7bb15YdtwIRFdxn8AUUX1qAP2U3Sof3hodaVwXHG5WeUtmnQplLdQla/KYV 386lKp+GMUJi2WPQcuO82pvKacZ4Ex4kBe2aJLuM68qu44h46H44YxPddPhE3fpEGXEv Y6zEhL7G5gF3PRnTr6LtelXXSaXizADC35CHxBFrvdSLHZu9ZATLR4uGLhdkjKs80U3B WxGWK56wqTLuQ75tZ41Z/1XovtehOCQTWOGYU17zb9BTx835DS0ii6KwyEtiYcdn/DHh prfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=RgVjTnRCQ8ge/HYVFIY6rEfALepWyiVS5o11BjITru4=; b=KOjnSVXyncLtSEoFWr0AeJum3fd5rJH/6dSwF6TPmRKPvhMAdw4M6as2xYIlj33ijG p4kBwBVsHQb0SxWoaKX4pKepDi+qcYxsYYPQXJMVMAL16AV1vKIDd1l1AKEccT7y9N9H Pd74f7QsO+W1xZnLFKMM/Xj3ippxgCNPVZicr3hN0hswPsjvEM++RlX0BjoJTDn2yc4S q7fUZrRwXJTXrMd2XLDbUh1uDAtQG4caLi4x0Ooe1MtdrrW/FVot3fgOvhLxRc3k9bvQ S0SdNUadOy5EVjO6/0pfbl6/N14w0acJEMyNtllDVNzLUmtNZLYRoHuGkcG+in7nM1M3 mGug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ce16si14409874ejb.5.2021.12.13.07.25.27; Mon, 13 Dec 2021 07:25:28 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5ED2A68B01B; Mon, 13 Dec 2021 17:22:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9CB9668AFBC for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id BB238240555 for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id iQhYCCA30vrN for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A322524052D for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 393A03A0A2B; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:30 +0100 Message-Id: <20211213152042.5900-12-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 12/24] ffmpeg: write the header for stream-less outputs when initializing the muxer X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: cKx+6QJAIPxP There is no reason to delay this. --- fftools/ffmpeg.c | 11 ----------- fftools/ffmpeg_mux.c | 7 +++++++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index f76e5df8d2..c01ff2fa92 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3317,7 +3317,6 @@ static void report_new_stream(int input_index, AVPacket *pkt) static int transcode_init(void) { int ret = 0, i, j, k; - AVFormatContext *oc; OutputStream *ost; InputStream *ist; char error[1024] = {0}; @@ -3391,16 +3390,6 @@ static int transcode_init(void) } } - /* write headers for files with no streams */ - for (i = 0; i < nb_output_files; i++) { - oc = output_files[i]->ctx; - if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) { - ret = of_check_init(output_files[i]); - if (ret < 0) - goto dump_format; - } - } - dump_format: /* dump the stream mapping */ av_log(NULL, AV_LOG_INFO, "Stream mapping:\n"); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e6417cdb8c..dcba02b8a3 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -354,6 +354,13 @@ int of_muxer_init(OutputFile *of, uint64_t limit_filesize) if (strcmp(of->format->name, "rtp")) want_sdp = 0; + /* write the header for files with no streams */ + if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { + int ret = of_check_init(of); + if (ret < 0) + return ret; + } + return 0; } From patchwork Mon Dec 13 15:20:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32443 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5577685iog; Mon, 13 Dec 2021 07:26:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJw80NLU0onsQnKbyLsoQADTpaSQ0DkXtK4ATgFJKxroEVxAYvHJrFeUjK7hgC6Zo+8v6F04 X-Received: by 2002:a05:6402:124e:: with SMTP id l14mr63660889edw.74.1639409199439; Mon, 13 Dec 2021 07:26:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409199; cv=none; d=google.com; s=arc-20160816; b=AnUbEZW2DhYBanSkJhaVlk8XopH4gO+0ZyQEZMYht7ogXNdMTCCI74cKIJ/+KC3/jd aTkMGeD/duiSVEoYkxvNCwX/71MDCSnML1YJAg1HAG3QQBagA4BEUghn0oJ40YqRUdbb ffp8LoV07mHEi2wL133MHmHDcCS3hF4DOysISgE29yYrLzKON2A4mdOnaGB6pjLI60fU MQlY5BHtn65HYJNyyNlnZCLZL5l8m24P2SETmdbFn8J8YC35dsSWtrPZUkRA8gafHVYi W8Q11vs8PNSjAc8Fk/ajk6MBfDVzBG2yqzHaYvcwRh49gHtdiJAiXvMuP8VzrTCvbVAY Gaww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=k9XsEtJozWHy+RWVlcIBBPFd460WNGhUmcnYf/Mu1r0=; b=ny1htwhkUWTrRu1RxGbcXHTSQh7nWqVVcFMgcJ0kce1ILHACbheE97WIuogN7qpnT1 VIqVu89KuHj7MLoeJKgg6mYOdt9Mq3HtYp9MB5+RlcfmMjwNEfOGTQVBD/cumVvo3pkH ZCt5LTJANtWN1LIz0NdinVRCQhFthH04LkDwHd30CHvjMrfGBHh2eJB6U4cd/l/cbIG0 ZVRnhi3tnOmwAkscv4i/Z5aOR6m9HsKXGp0YDC2P8CpdNXgzB6+hmYUcWkOpxOzthJHH J60TzTvJJbAzXdjkKqxAtPeOJM0J0CM8SnyVNXGOKPv/tjj0tIxaN2otzHajK0+OtFec SYfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gs30si22662311ejc.247.2021.12.13.07.26.39; Mon, 13 Dec 2021 07:26:39 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 81F9668B050; Mon, 13 Dec 2021 17:22:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3303D68AFC0 for ; Mon, 13 Dec 2021 17:22:15 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 001E6240590 for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id tpqV7g5w9r1y for ; Mon, 13 Dec 2021 16:22:12 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id D3080240592 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 3D7623A0A44; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:31 +0100 Message-Id: <20211213152042.5900-13-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 13/24] ffmpeg: add a helper function to access output file size X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: v0gWzr+0hbDl Stop accessing muxer internals from outside of ffmpeg_mux. --- fftools/ffmpeg.c | 6 +----- fftools/ffmpeg.h | 1 + fftools/ffmpeg_mux.c | 6 ++++++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index c01ff2fa92..b7f26fe288 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1512,7 +1512,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti { AVBPrint buf, buf_script; OutputStream *ost; - int64_t total_size = -1; + int64_t total_size = of_bytes_written(output_files[0]); AVCodecContext *enc; int frame_number, vid, i; double bitrate; @@ -1541,10 +1541,6 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti t = (cur_time-timer_start) / 1000000.0; - - if (output_files[0]->ctx->pb) - total_size = output_files[0]->ctx->pb->bytes_written; - vid = 0; av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC); av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 4aed24c2a7..c1990561af 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -694,5 +694,6 @@ void of_close(OutputFile **pof); void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); int of_finished(OutputFile *of); +int64_t of_bytes_written(OutputFile *of); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index dcba02b8a3..3ae20fdeeb 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -371,3 +371,9 @@ int of_finished(OutputFile *of) return 1; return 0; } + +int64_t of_bytes_written(OutputFile *of) +{ + AVIOContext *pb = of->ctx->pb; + return pb ? pb->bytes_written : -1; +} From patchwork Mon Dec 13 15:20:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32444 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5577962iog; Mon, 13 Dec 2021 07:26:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJzg+LNYisJTJJbBqIsYF3RYoKuaD+NLQoBuCIR28aP7ukBVCoDLlWRU6uLXye7CHK8qQoYt X-Received: by 2002:a50:fb09:: with SMTP id d9mr63040616edq.283.1639409211457; Mon, 13 Dec 2021 07:26:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409211; cv=none; d=google.com; s=arc-20160816; b=sgAa6zQNoo2GKMCTtUk5DNpDwQOTa7g1uKijDx+7/XBwSiZFrl0OtdCyHQqs7iqQFz MtUNWP4iTjsk533KQmD89nPhUN3yQgCXqUBMamJ+/9wRKR1t8qsZuXCHSM6JsGxfNc99 vpXl803Z2uYgd3D3WhZ02jVTO3ovTe/BonPltFOMv/dAn+jCrVFHQvuG18aaIJjl/Brj z0jFPfPHIiJQtw4jc6iJEgWs0vSzeIiAHcH2Fl0NmiOBBzsAkV+ENgq7kprBhXIHK5VP W96nc8XuQYy01qAqvnVFdPmJ0x0Vhi0pPN0E4o6HrrLurqEz+NYn0kwvkr29HvQX4eqK CahA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=ThxEN5I0GAGev/RxFP/0O5p9WChZSRxjVVkZ+BkMJqQ=; b=cmzwWsCO3vzqutJxHgeAJJvrAtqOEay9FyUtaQuBVdATcYGFmhE58mOExVYDCq3r6G 2wrf5cIxoeiUMncKod3UzN2PTvfJOVRHjpwtFYzYGFruw3klOxmBJ6lk54BB3FGxZnIR 9Fs4poPxEyk+8DyUFlI12hHBqRElen9pcjyTS0jdZkCXOKsAdScWwuZ67B2kq9y6s9vL 1YH2+gnbYbKOu/qm10f1mkZ+EAkK99cBw9I5ugbKjY8pbt9cJJ3cAz6LCwD9ZZqvmv4u MObO+jb3yRTrBmq+qr+S5xzh/Khaxv+A7E9YoUhcQ872a7LCELWOmlqlLEefivGnZ8yL aglQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id hb16si19050840ejc.365.2021.12.13.07.26.48; Mon, 13 Dec 2021 07:26:51 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5705368B056; Mon, 13 Dec 2021 17:22:39 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3DCBB68AFCC for ; Mon, 13 Dec 2021 17:22:15 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9998C240592 for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id HOTryu1PxL-5 for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id D3280240593 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 419323A0A7F; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:32 +0100 Message-Id: <20211213152042.5900-14-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/24] ffmpeg: move closing the file into of_write_trailer() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: Mr03CW/ikJmd The current code postpones closing the files until after printing the final report, which accesses the output file size. Deal with this by storing the final file size before closing the file. --- fftools/ffmpeg.c | 13 ------------- fftools/ffmpeg_mux.c | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index b7f26fe288..81a41fc8d1 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -4348,7 +4348,6 @@ static int transcode_step(void) static int transcode(void) { int ret, i; - AVFormatContext *os; OutputStream *ost; InputStream *ist; int64_t timer_start; @@ -4417,18 +4416,6 @@ static int transcode(void) /* dump report by using the first video and audio streams */ print_report(1, timer_start, av_gettime_relative()); - /* close the output files */ - for (i = 0; i < nb_output_files; i++) { - os = output_files[i]->ctx; - if (os && os->oformat && !(os->oformat->flags & AVFMT_NOFILE)) { - if ((ret = avio_closep(&os->pb)) < 0) { - av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", os->url, av_err2str(ret)); - if (exit_on_error) - exit_program(1); - } - } - } - /* close each encoder */ for (i = 0; i < nb_output_streams; i++) { ost = output_streams[i]; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 3ae20fdeeb..3ee0fc0667 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -35,6 +35,7 @@ struct Muxer { /* filesize limit expressed in bytes */ uint64_t limit_filesize; + uint64_t final_filesize; int header_written; }; @@ -318,6 +319,17 @@ int of_write_trailer(OutputFile *of) return ret; } + if (!(of->format->flags & AVFMT_NOFILE)) { + of->mux->final_filesize = of->ctx->pb->bytes_written; + + ret = avio_closep(&of->ctx->pb); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", + of->ctx->url, av_err2str(ret)); + return ret; + } + } + return 0; } @@ -375,5 +387,6 @@ int of_finished(OutputFile *of) int64_t of_bytes_written(OutputFile *of) { AVIOContext *pb = of->ctx->pb; - return pb ? pb->bytes_written : -1; + return of->mux->final_filesize ? of->mux->final_filesize : + pb ? pb->bytes_written : -1; } From patchwork Mon Dec 13 15:20:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32435 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5575652iog; Mon, 13 Dec 2021 07:25:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdhMW1Ukn0RU2+jnZu19k1OQ6ard/P1USuqYVqyMJ8RJPs93MiEG+olM/BVzW9wThbXpm2 X-Received: by 2002:a17:906:58ce:: with SMTP id e14mr43236227ejs.525.1639409107122; Mon, 13 Dec 2021 07:25:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409107; cv=none; d=google.com; s=arc-20160816; b=Orvwe8UEtDbWTGfrsmvCREAXUXEXHkG8jPgFL6kvNEYbKMIInPR4pkDHsI6eP3ywiT BliYY6chVsMglk/8OYgAIaOVmhj9dPHmC/PkKus9Go+qNxNINe4Gt1W8NMirBLwnwh7a I2DvTIAHvk5BEO54WciqB5PEnjrB5uorUJEr3JIbju98Co/CbwGNlqb3QDBtbTlAhAwq UBQcctktJtnZS39sXSaUZ8pEuiFWulUFaHyw3c1nPi1iVZ9JZ0TXHkT2kNQ6s+DSyhlS Y9yTDPldZhAylmN0trzrvqtQkBtsPNV8lXxKBw7QdMOKjQ72a5UAzUvAV57xJGYY6gyb ndAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/nrDflbzUmrKcrqVsb5w7qphUeuWbilW4KbA1d3c6RE=; b=ACZr4xU9LwbUjLF1n9L22PImjM9rfvYeLs/NIDCDK3RLP/7hLaf7kQesKiB2bMJCl5 6oGFsXuXdVd8Tio4MgzZxrPaGYO713yndzxa89KzTFs+6QF6IGhtC6bczGde3dna3nqk oWKtGApvrpQxd75+SflRE343QGt2N32YDIDlaE8yiQb5v7lqJap/s67xjbFwT0YT9wq4 o8npGbrSdZVq9EO+OjwoNbxWdD5MUDgQ+tL6C9ORXb2s3d2oacuqUtMeznFsM4DY0Doa IKZVXgeVPgQxm1k3vDpxZivpvL+9agSmdsiVF1atvGczwcHEyFTQyLiUCGcx3jM2xGMm jciA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x16si3039399ejo.360.2021.12.13.07.25.05; Mon, 13 Dec 2021 07:25:07 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 85F5368B00F; Mon, 13 Dec 2021 17:22:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9554968AFAA for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C65C0240593 for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id l0Vu6f340hiA for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id A726424056A for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 45A763A0AB5; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:33 +0100 Message-Id: <20211213152042.5900-15-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/24] ffmpeg: refactor the code checking for bitexact output X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: l8o/ZRMdTVbS Figure out earlier whether the output stream/file should be bitexact and store this information in a flag in OutputFile/OutputStream. Stop accessing the muxer in set_encoder_id(), which will become forbidden in future commits. --- fftools/ffmpeg.c | 21 +-------------------- fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_opt.c | 27 ++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 81a41fc8d1..902f190191 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2864,37 +2864,18 @@ static int init_output_stream_streamcopy(OutputStream *ost) static void set_encoder_id(OutputFile *of, OutputStream *ost) { - const AVDictionaryEntry *e; - uint8_t *encoder_string; int encoder_string_len; - int format_flags = 0; - int codec_flags = ost->enc_ctx->flags; if (av_dict_get(ost->st->metadata, "encoder", NULL, 0)) return; - e = av_dict_get(of->opts, "fflags", NULL, 0); - if (e) { - const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0); - if (!o) - return; - av_opt_eval_flags(of->ctx, o, e->value, &format_flags); - } - e = av_dict_get(ost->encoder_opts, "flags", NULL, 0); - if (e) { - const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0); - if (!o) - return; - av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags); - } - encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2; encoder_string = av_mallocz(encoder_string_len); if (!encoder_string) exit_program(1); - if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT)) + if (!of->bitexact && !ost->bitexact) av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len); else av_strlcpy(encoder_string, "Lavc ", encoder_string_len); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index c1990561af..78c2295c8e 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -491,6 +491,7 @@ typedef struct OutputStream { int top_field_first; int rotate_overridden; int autoscale; + int bitexact; int bits_per_raw_sample; double rotate_override_value; @@ -588,6 +589,7 @@ typedef struct OutputFile { int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units int shortest; + int bitexact; } OutputFile; extern InputStream **input_streams; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 09301acb03..ec95202d95 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1427,6 +1427,22 @@ static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *o return 0; } +static int check_opt_bitexact(void *ctx, const AVDictionary *opts, + const char *opt_name, int flag) +{ + const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0); + + if (e) { + const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0); + int val = 0; + if (!o) + return 0; + av_opt_eval_flags(ctx, o, e->value, &val); + return !!(val & flag); + } + return 0; +} + static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type, int source_index) { OutputStream *ost; @@ -1522,8 +1538,13 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e } - if (o->bitexact) + if (o->bitexact) { ost->enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT; + ost->bitexact = 1; + } else { + ost->bitexact = check_opt_bitexact(ost->enc_ctx, ost->encoder_opts, "flags", + AV_CODEC_FLAG_BITEXACT); + } MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st); if (time_base) { @@ -2308,6 +2329,10 @@ static int open_output_file(OptionsContext *o, const char *filename) if (o->bitexact) { oc->flags |= AVFMT_FLAG_BITEXACT; + of->bitexact = 1; + } else { + of->bitexact = check_opt_bitexact(oc, of->opts, "fflags", + AVFMT_FLAG_BITEXACT); } /* create streams for all unlabeled output pads */ From patchwork Mon Dec 13 15:20:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32439 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5576644iog; Mon, 13 Dec 2021 07:25:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMg/FdT6696d0tgT4a7NxNvgTlkEOnJVk4lQzWN4GaJnvOa5fmH+czUzZX612K4NSoJGZz X-Received: by 2002:a17:907:2723:: with SMTP id d3mr1218157ejl.654.1639409153190; Mon, 13 Dec 2021 07:25:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409153; cv=none; d=google.com; s=arc-20160816; b=dqFY33jVFqC1QY+NMSyQ/YH4C4VUOr6mSOEDa5Zc0U1gy9YH+eXKckbYkyz2fGxjmD 35lEFb4j1Dlj/XusrrJsN9rF3ItI3uPg0hAw7Kykg5BxjaBc2M7dlhHMu0JmoahYUAtP rPJyR6Wn6+xt8xmnQyUywxczWCGHhxG6eypIJw8WqssSKhJi3GdN69Ei+2UHMrHiuPrQ DMbLygyZaYJIJbzgy7cd2waZQnCSvANXBtwMudoih+opApwpfyjslYPzuMv7mHTFSgGm ASeMkD/9B7Vsi4Iccvhbaty0sjQnC/QRx5y3c1X7F+EKFAKzkB64t5ajAQ17mhOpUAte Td7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=8sk75POO2uUesRsfYp7G/UshT5QHRxlO5721rO7gxBE=; b=iz8igt1g/aHNlwO8WH65jhTYTtNWnk4aaTuBdx1qaGPIS7BmnvqyLMpsAlcl5gGmt0 8Zed6UTY4rrZrQmSwEFfi+Fq+nYKequ16mSRMx7zZaRed6j7kfJ7+Ga8EtSWO9E6TI73 yQbI5NagxCbCof0+n2hBcek4LfLIs1tvj4IrKTXOGeTQXFt0Ec1AhK0RkOen/c2Si5sy t9Dxv4xehsV/NKIiMh9DdH1z8e5K+QYlljENte6D/WomjJv09NnNwK65mHIHWqhOyUBH 8VqZnrfM2mH9G3Mb5Z2X0v6rCZUpb8d9Bo4OXs4+zucu79qSewhue1R+7RK+cd2/QqJY 8qZg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bl4si15314569ejb.152.2021.12.13.07.25.52; Mon, 13 Dec 2021 07:25:53 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3278E68AF75; Mon, 13 Dec 2021 17:22:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9DBC168AFBE for ; Mon, 13 Dec 2021 17:22:14 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6380A24052D for ; Mon, 13 Dec 2021 16:22:12 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id OMn4TenVAiZb for ; Mon, 13 Dec 2021 16:22:11 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id D2643240590 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 49FB33A0B4A; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:34 +0100 Message-Id: <20211213152042.5900-16-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 16/24] ffmpeg: access output file chapters through a wrapper X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: aGdxkmM5hFc/ Avoid accessing the muxer context directly, as this will become forbidden in future commits. --- fftools/ffmpeg.c | 15 +++++++++------ fftools/ffmpeg.h | 2 ++ fftools/ffmpeg_mux.c | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 902f190191..d69e4119ef 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2909,12 +2909,15 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, *next++ = 0; if (!memcmp(p, "chapters", 8)) { - - AVFormatContext *avf = output_files[ost->file_index]->ctx; + OutputFile *of = output_files[ost->file_index]; + AVChapter * const *ch; + unsigned int nb_ch; int j; - if (avf->nb_chapters > INT_MAX - size || - !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1, + ch = of_get_chapters(of, &nb_ch); + + if (nb_ch > INT_MAX - size || + !(pts = av_realloc_f(pts, size += nb_ch - 1, sizeof(*pts)))) { av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n"); @@ -2923,8 +2926,8 @@ static void parse_forced_key_frames(char *kf, OutputStream *ost, t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0; t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base); - for (j = 0; j < avf->nb_chapters; j++) { - AVChapter *c = avf->chapters[j]; + for (j = 0; j < nb_ch; j++) { + const AVChapter *c = ch[j]; av_assert1(index < size); pts[index++] = av_rescale_q(c->start, c->time_base, avctx->time_base) + t; diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 78c2295c8e..8119282aed 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -697,5 +697,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue); int of_finished(OutputFile *of); int64_t of_bytes_written(OutputFile *of); +AVChapter * const * +of_get_chapters(OutputFile *of, unsigned int *nb_chapters); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 3ee0fc0667..6c9f10db9c 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -390,3 +390,10 @@ int64_t of_bytes_written(OutputFile *of) return of->mux->final_filesize ? of->mux->final_filesize : pb ? pb->bytes_written : -1; } + +AVChapter * const * +of_get_chapters(OutputFile *of, unsigned int *nb_chapters) +{ + *nb_chapters = of->ctx->nb_chapters; + return of->ctx->chapters; +} From patchwork Mon Dec 13 15:20:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32432 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5574897iog; Mon, 13 Dec 2021 07:24:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJwqtPhsNIdiILw1hm2cVeRRCKR8Pmwm0O8pnFQDjnEAW1eSoT8TBD4nfH5omDJn7s1wlHao X-Received: by 2002:a17:906:9482:: with SMTP id t2mr44247613ejx.288.1639409070819; Mon, 13 Dec 2021 07:24:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409070; cv=none; d=google.com; s=arc-20160816; b=Dye3YBMVl70Jh29hNUogBmJQrTykP8tPESELxBMvNULIDw1Ifrt3SBZvKwQhvIttXj 16gdsQYoWKZOwxHZ53ubQFIEg6R4cQLUzknegbOOJ0/4IyICZSEGv9/L+o4L7UZtp6cs yFJCF6BwEkgD5Smq7Org6KJOorlxjhnxQGDlGoQWdZ5EEXHmkh38pAkLSVjnebVPoOV0 LUlVpTGpBgLouLu4ZlRThAAxMSXch4D6mgRxIKIZdieAKqzh1nbxBCHmpJexlUOTMFTJ M2UPM4PnZnxv4c5JF7tVYAvPA2sl9eYiFHOzkng5vBeH7hMEPGN8ISrOr8pxWu3keu+q USwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=HnmNI+pJVI4yqMzmKgbrISX4cfKWqdIhCc1yYAWhpsY=; b=xMmhX1LgNWiY8c1NrgLn0vzFCeffKepwJ1iipdTwWUXDdqzf7616BgA4x7FTFu1vtQ 33+WKtWEi18ERZVeOdsagXXQ7OxcyHE5AVzsl29PIoWLDJPX6Pm+QeBNaK24UwAYbJa6 lEthC0HERVxEcH44NI/GhWNDYoiU8Uf3fkDMIydW56DNSmx7QlkI2+2GK50pkNw8CBPI 2WV8Q0e3UUOjIyROd9ME/G/7nDKnLgHwbC+Snol3hY47JiJhllfPM030yBza4hjF73hx 4KctqgfOba9kPvR/pf30Rnoz9IwI1+oQPA7L723hTZ4cG8nbzWADvvDluHm2uwuOF740 I2mw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id cw16si21486669ejc.503.2021.12.13.07.24.30; Mon, 13 Dec 2021 07:24:30 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 55F1768AFBA; Mon, 13 Dec 2021 17:22:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 95C9E68AFB4 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 1D34724017C for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id js3iSpp4H5X0 for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id EE7492405F0 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 4E4403A0BC7; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:35 +0100 Message-Id: <20211213152042.5900-17-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/24] ffmpeg: do not log to the muxer context X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8WzdRH1ie27M All other logging goes to NULL context. --- fftools/ffmpeg.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index d69e4119ef..afd442ff4e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2954,7 +2954,6 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba { InputStream *ist = get_input_stream(ost); AVCodecContext *enc_ctx = ost->enc_ctx; - AVFormatContext *oc; if (ost->enc_timebase.num > 0) { enc_ctx->time_base = ost->enc_timebase; @@ -2967,8 +2966,7 @@ static void init_encoder_time_base(OutputStream *ost, AVRational default_time_ba return; } - oc = output_files[ost->file_index]->ctx; - av_log(oc, AV_LOG_WARNING, "Input stream data not available, using default time base\n"); + av_log(NULL, AV_LOG_WARNING, "Input stream data not available, using default time base\n"); } enc_ctx->time_base = default_time_base; From patchwork Mon Dec 13 15:20:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32441 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5577242iog; Mon, 13 Dec 2021 07:26:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJxoK4j0hOz7hsc8kaglam+Ovpi7rIbcePlUtUJXENo+RF7hV12dE2CNTtlUPLF2yPs4rvvR X-Received: by 2002:a17:907:7e8e:: with SMTP id qb14mr43219043ejc.562.1639409180099; Mon, 13 Dec 2021 07:26:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409180; cv=none; d=google.com; s=arc-20160816; b=xB8ZUUDflXw6rumRm/6Dm5wr5M/iH9osGxx4vGKzlLb06jjy2w/ub/RgNXPGKtlFPe cHlnZuj7OHLIQSVGWC2QMh17xndr4kXjFylKaxjq5ierob2XRtWUeMdS0IELFALLXM2k lx1mwP7SpfcrYeSK4YmN7kPJlOmG2I9E8zZ9JcWetr/zdVQpmJHTfLSy8B/hznpwdO42 17Bk9EDZzigQvWhN+c1CI7qfeKaYo6jAEhXr4OP8dmJqiPYTC/OtLXB0n7ed+KGrrAfI c3ss3D02JorFM5Iqtk89obg47aA9g9v2oc1MlF1scWeEdBSNkoPLe1oFr80+dNkoldyJ cVEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=rsHltAVVVarWMhRiTahaRP9EzzNDhA9xKhYgFVaLRpM=; b=Z9MKJ4SPjlNJXukVmYR647Ya9843ioBolVvoYzryYoLDQKEzpynaQRVOSxK1p39+FO U7Yq1cOW3eaxmZcYqpA8aeeRrbQ/VPW9E046JD1UD1cj/T/QaJj+co8gZEFL1wRVlHOj f86Q/C/nHnGfQCuzaBR/AYKMo74rgKw/Z59l8HcP4bfXIaBHI6MsVi7w3KGIeSzOq/61 xp6i2/FGnJ8fFIWt7hObkXziDpE9VUDQWYMXnI7mqiplfqM6ZwSFtxYrSHtnKMmG9Vks MGVhnNSp0Ff7Piv7DGLp++/bFOHvpnTbTFR8EihgMtYB6PFuv6EId4L7xxF0+v88LYM+ nDyA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id eb10si24481616edb.599.2021.12.13.07.26.19; Mon, 13 Dec 2021 07:26:20 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 10C5A68B037; Mon, 13 Dec 2021 17:22:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A7FC168AFC0 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B15F2240687 for ; Mon, 13 Dec 2021 16:22:18 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id z4zkxQAhJWcO for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id F2609240685 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 52CAF3A0BE6; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:36 +0100 Message-Id: <20211213152042.5900-18-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 18/24] ffmpeg: move the mux queue into muxer private data X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: XmbtfrxlcZ18 The muxing queue currently lives in OutputStream, which is a very large struct storing the state for both encoding and muxing. The muxing queue is only used by the code in ffmpeg_mux, so it makes sense to restrict it to that file. This makes the first step towards reducing the scope of OutputStream. --- fftools/ffmpeg.c | 9 ----- fftools/ffmpeg.h | 9 ----- fftools/ffmpeg_mux.c | 91 ++++++++++++++++++++++++++++++++++++-------- fftools/ffmpeg_opt.c | 6 --- 4 files changed, 76 insertions(+), 39 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index afd442ff4e..e9a5c0f523 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -595,15 +595,6 @@ static void ffmpeg_cleanup(int ret) avcodec_free_context(&ost->enc_ctx); avcodec_parameters_free(&ost->ref_par); - if (ost->muxing_queue) { - while (av_fifo_size(ost->muxing_queue)) { - AVPacket *pkt; - av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); - av_packet_free(&pkt); - } - av_fifo_freep(&ost->muxing_queue); - } - av_freep(&output_streams[i]); } #if HAVE_THREADS diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 8119282aed..e6e472f994 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -555,15 +555,6 @@ typedef struct OutputStream { int max_muxing_queue_size; - /* the packets are buffered here until the muxer is ready to be initialized */ - AVFifoBuffer *muxing_queue; - - /* - * The size of the AVPackets' buffers in queue. - * Updated when a packet is either pushed or pulled from the queue. - */ - size_t muxing_queue_data_size; - /* Threshold after which max_muxing_queue_size will be in effect */ size_t muxing_queue_data_threshold; diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 6c9f10db9c..5a12a1c899 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -32,7 +32,20 @@ #include "ffmpeg.h" +typedef struct MuxStream { + /* the packets are buffered here until the muxer is ready to be initialized */ + AVFifoBuffer *muxing_queue; + + /* + * The size of the AVPackets' buffers in queue. + * Updated when a packet is either pushed or pulled from the queue. + */ + size_t muxing_queue_data_size; +} MuxStream; + struct Muxer { + MuxStream *streams; + /* filesize limit expressed in bytes */ uint64_t limit_filesize; uint64_t final_filesize; @@ -55,6 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, { AVFormatContext *s = of->ctx; AVStream *st = ost->st; + MuxStream *ms = &of->mux->streams[st->index]; int ret; /* @@ -76,10 +90,10 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, if (!of->mux->header_written) { AVPacket *tmp_pkt; /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_space(ost->muxing_queue)) { - size_t cur_size = av_fifo_size(ost->muxing_queue); + if (!av_fifo_space(ms->muxing_queue)) { + size_t cur_size = av_fifo_size(ms->muxing_queue); unsigned int are_we_over_size = - (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX; size_t new_size = FFMIN(2 * cur_size, limit); @@ -89,7 +103,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->file_index, ost->st->index); exit_program(1); } - ret = av_fifo_realloc2(ost->muxing_queue, new_size); + ret = av_fifo_realloc2(ms->muxing_queue, new_size); if (ret < 0) exit_program(1); } @@ -100,8 +114,8 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, if (!tmp_pkt) exit_program(1); av_packet_move_ref(tmp_pkt, pkt); - ost->muxing_queue_data_size += tmp_pkt->size; - av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); + ms->muxing_queue_data_size += tmp_pkt->size; + av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); return; } @@ -283,16 +297,17 @@ int of_check_init(OutputFile *of) /* flush the muxing queues */ for (i = 0; i < of->ctx->nb_streams; i++) { + MuxStream *ms = &of->mux->streams[i]; OutputStream *ost = output_streams[of->ost_index + i]; /* try to improve muxing time_base (only possible if nothing has been written yet) */ - if (!av_fifo_size(ost->muxing_queue)) + if (!av_fifo_size(ms->muxing_queue)) ost->mux_timebase = ost->st->time_base; - while (av_fifo_size(ost->muxing_queue)) { + while (av_fifo_size(ms->muxing_queue)) { AVPacket *pkt; - av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL); - ost->muxing_queue_data_size -= pkt->size; + av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL); + ms->muxing_queue_data_size -= pkt->size; of_write_packet(of, pkt, ost, 1); av_packet_free(&pkt); } @@ -333,6 +348,31 @@ int of_write_trailer(OutputFile *of) return 0; } +static void mux_free(Muxer **pmux, int nb_streams) +{ + Muxer *mux = *pmux; + + if (!mux) + return; + + for (int i = 0; i < nb_streams; i++) { + MuxStream *ms = &mux->streams[i]; + + if (!ms->muxing_queue) + continue; + + while (av_fifo_size(ms->muxing_queue)) { + AVPacket *pkt; + av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL); + av_packet_free(&pkt); + } + av_fifo_freep(&ms->muxing_queue); + } + av_freep(&mux->streams); + + av_freep(pmux); +} + void of_close(OutputFile **pof) { OutputFile *of = *pof; @@ -342,25 +382,42 @@ void of_close(OutputFile **pof) return; s = of->ctx; + + mux_free(&of->mux, s ? s->nb_streams : 0); + if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) avio_closep(&s->pb); avformat_free_context(s); av_dict_free(&of->opts); - av_freep(&of->mux); - av_freep(pof); } int of_muxer_init(OutputFile *of, uint64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); + int ret = 0; if (!mux) return AVERROR(ENOMEM); + mux->streams = av_calloc(of->ctx->nb_streams, sizeof(*mux->streams)); + if (!mux->streams) { + av_freep(&mux); + return AVERROR(ENOMEM); + } + of->mux = mux; + for (int i = 0; i < of->ctx->nb_streams; i++) { + MuxStream *ms = &mux->streams[i]; + ms->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket)); + if (!ms->muxing_queue) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + mux->limit_filesize = limit_filesize; if (strcmp(of->format->name, "rtp")) @@ -368,12 +425,16 @@ int of_muxer_init(OutputFile *of, uint64_t limit_filesize) /* write the header for files with no streams */ if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) { - int ret = of_check_init(of); + ret = of_check_init(of); if (ret < 0) - return ret; + goto fail; } - return 0; +fail: + if (ret < 0) + mux_free(&of->mux, of->ctx->nb_streams); + + return ret; } int of_finished(OutputFile *of) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index ec95202d95..2d16068c4f 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1613,8 +1613,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e ost->max_muxing_queue_size = FFMIN(ost->max_muxing_queue_size, INT_MAX / sizeof(ost->pkt)); ost->max_muxing_queue_size *= sizeof(ost->pkt); - ost->muxing_queue_data_size = 0; - ost->muxing_queue_data_threshold = 50*1024*1024; MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ost->muxing_queue_data_threshold, oc, st); @@ -1638,10 +1636,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e } ost->last_mux_dts = AV_NOPTS_VALUE; - ost->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket)); - if (!ost->muxing_queue) - exit_program(1); - return ost; } From patchwork Mon Dec 13 15:20:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32433 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5575188iog; Mon, 13 Dec 2021 07:24:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJy1EFeT/xwZb7yFF3ANPuACUVJOC95TlKfb7u5pjKFCNsuC4kfdP4iIMlpCFzMCM0DmgIuA X-Received: by 2002:a17:906:e17:: with SMTP id l23mr1182727eji.625.1639409084600; Mon, 13 Dec 2021 07:24:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409084; cv=none; d=google.com; s=arc-20160816; b=w9WqMFo4rm6lCpEnbO/77DB1afYHN3KyYlQYFgOzmJORlMwdvkHAM4kFR1H7kXdDUV TPDgH4uzN/BDuQAqfb89AOngA213OcW4MrV7p89mr2QKwK+MhwKbzsDJ3vzlmDXq8qx+ NGkACY/z9j5KK6UrnftPSdmsJQScKtXSpQu1bX0SzRNcjBoAZecK6LMPrTEHfVrmVbMW yuq3cbVIKWO4khlUmwhp0zh4pLq7UyyxVnuNtDPQgjIVpJiGv5N9tT0dlxZ0y5rJRhT7 mbZKS4lSBMdwXjoEX99cSkQkIJtZRqXfk0eWDLfXywGY2m/6+mja/okvZAYmTyGl68VT BQbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=VD9Qyxp4ZAgdwaki3pU9um3WZcHMFZScjOdvh85DKdM=; b=j6fulObhURUt5GEt93klEhFWeCN9QqVmbGIDUHo055sTuA6oSg00Vf6XfxVn85uxhT PZ+OGGHJ7ibO9BmuarOvQTYRm46e4JmyGUHPmjJAkallgIP5UHJEMWgpZ0OlRGM7tSXE eVj05IZGYtcVeqvQaIlWcEn5wWKxgkNli/hefOOP6GSgE6LVJPQWIKmbbMdRjANO4H5K NmYXIwTagYMlhZtUwmJbLNHaYul5cluyJQzipdbf28ank/XiZTRMPw1ON0iqGVmjOzid sIEmo/mXHP6utM4p++bgWZpop+xhOsguEBqBRi1nfLO8xLHtWLyPAIioMcUfNPEY3PeA XrFQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t18si20617873edd.410.2021.12.13.07.24.44; Mon, 13 Dec 2021 07:24:44 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5923B68AFB7; Mon, 13 Dec 2021 17:22:28 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 951E468AFA8 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id D2B0F24017E for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 58ey-oPemwCb for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id F1FFE2405F5 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 56DBD3A067C; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:37 +0100 Message-Id: <20211213152042.5900-19-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 19/24] ffmpeg: fix initial muxing queue size X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: DbmehEMYtxZP It stores pointers to AVPacket, not AVPackets themselves. --- fftools/ffmpeg_mux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 5a12a1c899..9281e6c94c 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -411,7 +411,7 @@ int of_muxer_init(OutputFile *of, uint64_t limit_filesize) for (int i = 0; i < of->ctx->nb_streams; i++) { MuxStream *ms = &mux->streams[i]; - ms->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket)); + ms->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket*)); if (!ms->muxing_queue) { ret = AVERROR(ENOMEM); goto fail; From patchwork Mon Dec 13 15:20:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32434 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5575385iog; Mon, 13 Dec 2021 07:24:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJwv2yW2NJBVbgT7vL3DJMUSBHjJ6024gaaSzYmsejgJxDBHf+Xxu38aiVwg4wponpvbM/NK X-Received: by 2002:a05:6402:147:: with SMTP id s7mr64490803edu.8.1639409095322; Mon, 13 Dec 2021 07:24:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409095; cv=none; d=google.com; s=arc-20160816; b=rImejk6yBVMFCUn2GAYB2okhjEOkvj9AY/Jiu9j77OiydEQMPYV5BcWJbvzfjjaSxX NcoSdvhznwE8iGbAtP5AU3a0LIdEF3RLsdf0rje0Cqs2Pag28rNnIBKiR3EtB7ldGvg1 nZik4+4xb8Bj/WMAFcbgcWxQpsRMmD4WJVmPr+SodCPfxN60iDpXYB/tGOo0HeL1HIkX IsbTqxh68CokON4nrC5KnqVp/UqS3NDrrhZQdKN4naKIcwDq4TRuuWGK1Wvii0Z2GP6g rUgbWtrNCgO+NL7klLjbmSvDFGb9VYvxYNWrH3GbQ3xwDIgQhCUAaI/VKJxnnlatB0Di Sq+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=b5wGnDnOgqLNFT8SfcZYhbuFdvqQdzog4YZahxIax0s=; b=guwcWBGmxM7oMY06XQkHn/E5QimKtRQb6aWTsPKiSnWp4HbTm5/Ug31s1cRPw4rvXV hxZSvytuaVOEdFMAEEvKQR0jAzffN+YA6O1l7EFtriIr/3Mx/DFsYI8SOPXrO3Nl+ym/ GUIxiftfWvGzJd7eOo+0VrkQW4LJqNWXj+1zht3Ojrp2KyJRIe+fK69DKtT/sg/E0JIw nw0/VxHWR+Y+cjCxkiG/e+NEsbbRhtimBYTxeBD9PR84yO9kHvVxZzMCoDOCEj/PzWOn 2xacPyslbMpseKemc4xm8AbeRhs9jdw7gBLfL2uxCf6AwfcQwggBzGxf+UVkjkppFYso jDrg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id u24si16080346edr.523.2021.12.13.07.24.54; Mon, 13 Dec 2021 07:24:55 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 792D568B009; Mon, 13 Dec 2021 17:22:29 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9605C68AFB7 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 71CD4240179 for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 9Lp4dyeCC4Tt for ; Mon, 13 Dec 2021 16:22:13 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id F234924062A for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 5B36D3A0BF3; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:38 +0100 Message-Id: <20211213152042.5900-20-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 20/24] ffmpeg_mux: split queuing packets into a separate function X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: w8k1uuXse8Ds --- fftools/ffmpeg_mux.c | 72 +++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 9281e6c94c..d4b674c9e2 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -63,12 +63,50 @@ static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, } } +static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) +{ + MuxStream *ms = &of->mux->streams[ost->index]; + AVPacket *tmp_pkt; + int ret; + + if (!av_fifo_space(ms->muxing_queue)) { + size_t cur_size = av_fifo_size(ms->muxing_queue); + unsigned int are_we_over_size = + (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; + size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX; + size_t new_size = FFMIN(2 * cur_size, limit); + + if (new_size <= cur_size) { + av_log(NULL, AV_LOG_ERROR, + "Too many packets buffered for output stream %d:%d.\n", + ost->file_index, ost->st->index); + return AVERROR(ENOSPC); + } + ret = av_fifo_realloc2(ms->muxing_queue, new_size); + if (ret < 0) + return ret; + } + + ret = av_packet_make_refcounted(pkt); + if (ret < 0) + return ret; + + tmp_pkt = av_packet_alloc(); + if (!tmp_pkt) + return AVERROR(ENOMEM); + + av_packet_move_ref(tmp_pkt, pkt); + ms->muxing_queue_data_size += tmp_pkt->size; + av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); + + return 0; +} + void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue) { AVFormatContext *s = of->ctx; AVStream *st = ost->st; - MuxStream *ms = &of->mux->streams[st->index]; int ret; /* @@ -87,35 +125,13 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, ost->frame_number++; } + /* the muxer is not initialized yet, buffer the packet */ if (!of->mux->header_written) { - AVPacket *tmp_pkt; - /* the muxer is not initialized yet, buffer the packet */ - if (!av_fifo_space(ms->muxing_queue)) { - size_t cur_size = av_fifo_size(ms->muxing_queue); - unsigned int are_we_over_size = - (ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold; - size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX; - size_t new_size = FFMIN(2 * cur_size, limit); - - if (new_size <= cur_size) { - av_log(NULL, AV_LOG_ERROR, - "Too many packets buffered for output stream %d:%d.\n", - ost->file_index, ost->st->index); - exit_program(1); - } - ret = av_fifo_realloc2(ms->muxing_queue, new_size); - if (ret < 0) - exit_program(1); - } - ret = av_packet_make_refcounted(pkt); - if (ret < 0) - exit_program(1); - tmp_pkt = av_packet_alloc(); - if (!tmp_pkt) + ret = queue_packet(of, ost, pkt); + if (ret < 0) { + av_packet_unref(pkt); exit_program(1); - av_packet_move_ref(tmp_pkt, pkt); - ms->muxing_queue_data_size += tmp_pkt->size; - av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL); + } return; } From patchwork Mon Dec 13 15:20:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32440 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5576951iog; Mon, 13 Dec 2021 07:26:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzK/cWTtjltXXuNQqOV0iByXKyGRVSNnp4rQtmnRAtQ0A2gUE+5BaXOTGXPDmfgSQxmJi4z X-Received: by 2002:a17:906:8256:: with SMTP id f22mr44491685ejx.207.1639409166403; Mon, 13 Dec 2021 07:26:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409166; cv=none; d=google.com; s=arc-20160816; b=G1xHlzUAE+hjWlzsodxOLw2BIcJ8Sde7ffp5II2wVULUSgRXb8nJCfDmr+n7weXNTv OPAQcu5lWAho8hrYO9Vy+R1jbRDLVPVkp6nBNpktL9311bMu5/iRJemDPQfJHTzThn2u LbYBfSj+lvbaaw0albBd+pbkJmWOOuTkWEv9q03cLERd9xZ4dWCTxfTrKvE/ejBIQaUE lyaEAShWhFu3E9LRDyegiSfJFiUdRBh0gylQkCj0BRPhuQ8DnT90RroFwIeacoxhhFPe NFo9Zlb2U6WPQMBawtWYoQnfZ8ulCjYHCuS2QWSmkgDIHj7cHi3SHHK/QMkcArWY0h31 HCtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=6SIKYEW5Jddeg9El8fQdwFGmv+f9hzUQ2EpA8oKCIhM=; b=lvP7WiiRtskGbbs002sh9M51zPpVsISikKDTN1/23M7KtagvGcBcoAYACVZ/lzRiXd gFSRlk4+xCNAyJuH2WTIHbvuckFtF+R+aBIaW4oY20ZzJQ6bMQS3sRbLs44vlKgxSyKJ VcQaRRdogBKRIKzTpsQls22AFgWRevgZfhU3hcdw7C4X8StNpwbVhYKUwuzFjL/6XRLk 2KID8X//CWX4mLPe1AOQQaZ7FrqlH8Pwfx5tCp/aW75pXiT0YO4QyuaelnhGV6plkhyX D836xabT6jRL9hbou7+1r0jSfpN3TCrponHfN86Jh5nlsQcOCIqnLPszNezw+W4w2lL7 YUWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ds7si29592829ejc.661.2021.12.13.07.26.06; Mon, 13 Dec 2021 07:26:06 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 11AAD68AFCC; Mon, 13 Dec 2021 17:22:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9D28D68AFBD for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 452F22405F0 for ; Mon, 13 Dec 2021 16:22:16 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id J-7CzHM68gVy for ; Mon, 13 Dec 2021 16:22:15 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 02A9A240688 for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 5FFC03A0743; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:39 +0100 Message-Id: <20211213152042.5900-21-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 21/24] ffmpeg_mux: split of_write_packet() X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: kWbvznC+xPTe It is currently called from two places: - output_packet() in ffmpeg.c, which submits the newly available output packet to the muxer - from of_check_init() in ffmpeg_mux.c after the header has been written, to flush the muxing queue Some packets will thus be processed by this function twice, so it requires an extra parameter to indicate the place it is called from and avoid modifying some state twice. This is fragile and hard to follow, so split this function into two. Also rename of_write_packet() to of_submit_packet() to better reflect its new purpose. --- fftools/ffmpeg.c | 4 +-- fftools/ffmpeg.h | 3 +-- fftools/ffmpeg_mux.c | 63 ++++++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index e9a5c0f523..bbedf867b4 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -728,11 +728,11 @@ static void output_packet(OutputFile *of, AVPacket *pkt, if (ret < 0) goto finish; while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0) - of_write_packet(of, pkt, ost, 0); + of_submit_packet(of, pkt, ost); if (ret == AVERROR(EAGAIN)) ret = 0; } else if (!eof) - of_write_packet(of, pkt, ost, 0); + of_submit_packet(of, pkt, ost); finish: if (ret < 0 && ret != AVERROR_EOF) { diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index e6e472f994..76c8dfa4c8 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -684,8 +684,7 @@ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); void of_close(OutputFile **pof); -void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, - int unqueue); +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost); int of_finished(OutputFile *of); int64_t of_bytes_written(OutputFile *of); AVChapter * const * diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index d4b674c9e2..e97ec8ab93 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -102,39 +102,12 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) return 0; } -void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, - int unqueue) +static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) { AVFormatContext *s = of->ctx; AVStream *st = ost->st; int ret; - /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). - * Do not count the packet when unqueued because it has been counted when queued. - */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; - } - - /* the muxer is not initialized yet, buffer the packet */ - if (!of->mux->header_written) { - ret = queue_packet(of, ost, pkt); - if (ret < 0) { - av_packet_unref(pkt); - exit_program(1); - } - return; - } - if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) || (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0)) pkt->pts = pkt->dts = AV_NOPTS_VALUE; @@ -225,6 +198,38 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, } } +void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) +{ + AVStream *st = ost->st; + int ret; + + /* + * Audio encoders may split the packets -- #frames in != #packets out. + * But there is no reordering, so we can limit the number of output packets + * by simply dropping them here. + * Counting encoded video frames needs to be done separately because of + * reordering, see do_video_out(). + */ + if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; + } + ost->frame_number++; + } + + if (of->mux->header_written) { + write_packet(of, ost, pkt); + } else { + /* the muxer is not initialized yet, buffer the packet */ + ret = queue_packet(of, ost, pkt); + if (ret < 0) { + av_packet_unref(pkt); + exit_program(1); + } + } +} + static int print_sdp(void) { char sdp[16384]; @@ -324,7 +329,7 @@ int of_check_init(OutputFile *of) AVPacket *pkt; av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL); ms->muxing_queue_data_size -= pkt->size; - of_write_packet(of, pkt, ost, 1); + write_packet(of, ost, pkt); av_packet_free(&pkt); } } From patchwork Mon Dec 13 15:20:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32438 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5576377iog; Mon, 13 Dec 2021 07:25:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxVaBVCsLfZEeCVgaGxJ4oLmQJGyoxB8jIJR2JQi/sdDWBWniFB6G3mDC/OuwODEb1PZ1lO X-Received: by 2002:aa7:c946:: with SMTP id h6mr66809768edt.190.1639409140072; Mon, 13 Dec 2021 07:25:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409140; cv=none; d=google.com; s=arc-20160816; b=rFf8wu6iBdsb4qWQFPk2lEtwVZxUEyozvrIVT90ZJ7Cz7mdS5GhsRgkHC0CJjDpeWB x8AA6cgy9rK/zA6WeeYgWYPOfxOaaSTOrBEpFonokCG0fm1P+n4OMUIMUQG2cx1wnKAP QoGz4hEHjJ0GA3PL4gPdEOQkAClFdcRTEG1psF4I9JtSrG7BBf1IqdGFG4oOYTpNqN8a hr4QFLebgL5Oe7hMFCxUnR7SNBXYv2zI0E0pnl7eH9tT2DumVJGH7EjGVY7I99A9snyP +nkEWkL/oJgoOzCj2x4UVVJL3fQZoeobYQt0M4PhEjMPCrkim58PwWELcXesQ/Ny9JjQ 2hgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=fLCT9eUBnT84w43wNs8vY+yfOpDkZS2JxISrySThyAM=; b=xF2PpeeWWvTmuRiCdAvH2Hbi3oAxCEQEOkjnR4svfGHKAbNgRDigDjR2qTtno4jamf rTR1dQKUb1Je8zY82LjpvuxEr6hNLAWWbHBBJ0fWoSNUFszCBADy8sHIo8EkU6NiOxdk zCwePxpRk9CJc2CvoYzRPSw+METcf+b2r0Z1xyDQUYUPr+tzJ7uNi6CSMe+THTPSFLPw 9lXwVPDMO+X7NGqJM4XEKrh2tADt5HdNJa7WAojGVcFEHztTfL9z0lDKDyPRdfhTPpix PW3UjMqph2ka1kA3cnTyY2NzHOBh0M7pttPsefYdavcFKA4s7XdkTDf6eTs4IZNMcMs+ we1w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id kb24si15272302ejb.453.2021.12.13.07.25.39; Mon, 13 Dec 2021 07:25:40 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5CB7C68B024; Mon, 13 Dec 2021 17:22:33 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A11D868AFAD for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C0B462405F5 for ; Mon, 13 Dec 2021 16:22:16 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id sAGY2OF23YSS for ; Mon, 13 Dec 2021 16:22:16 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 0320124068C for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 646F03A0631; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:40 +0100 Message-Id: <20211213152042.5900-22-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 22/24] ffmpeg: move a comment to a more appropriate place X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 8V4FPD6hf2ce --- fftools/ffmpeg.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index bbedf867b4..538601e440 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1064,6 +1064,11 @@ static void do_video_out(OutputFile *of, } } + /* + * For video, number of frames in == number of packets out. + * But there may be reordering, so we can't throw away frames on encoder + * flush, we need to limit them here, before they go into encoder. + */ nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); nb0_frames = FFMIN(nb0_frames, nb_frames); @@ -1217,11 +1222,6 @@ static void do_video_out(OutputFile *of, } } ost->sync_opts++; - /* - * For video, number of frames in == number of packets out. - * But there may be reordering, so we can't throw away frames on encoder - * flush, we need to limit them here, before they go into encoder. - */ ost->frame_number++; if (vstats_filename && frame_size) From patchwork Mon Dec 13 15:20:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32442 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5577459iog; Mon, 13 Dec 2021 07:26:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxgpQT71BO3o5lwXkv6rh3b8mIWikpZtMnnCVWwERLcpNUoWMA9zX7OPR1XUbLCP273a2CG X-Received: by 2002:a17:906:dfe4:: with SMTP id lc4mr46225988ejc.125.1639409190087; Mon, 13 Dec 2021 07:26:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409190; cv=none; d=google.com; s=arc-20160816; b=M7+yk7G5XGj9wnpLTbyCWSBmjdAUvBJpux6e69hzK7SkAabM7RApZOjV/vJXxvhx8a YiHpsMRwScC+0S8DNM+QSG6rcpTrlm9P3WuoXqwoxKrjZiAQ9nm56+KAKBTij0aoz2Dz nyMMZ2jnI6SYMTPv/8/HI/iDeLN7d6YJsOyQCy9koUKs+8ZPvO7kjL8wpvfDKwD0uw3v MDB6Mo3br8phoEO7sJCjTsoRMuLUAgtywn0DzeoEZiivGMLL6eN8ffipiq5VaTCHa5jL raojhK5lo/2MRS0JAYSwYjTOLqn56oSYxhi8Itwog4/JvnBEEkU98dRTZ7ThmHuYCmSu ozGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=e13l7emmgtxEOfdv1O3mepues7Q7EZS5UF5gtgwGCTU=; b=xHCpox8e8YF8eobu0rMzpAPhbRZpM/Q3BhWZGkohdyLTDCS+suuZF+zo1Prdp4em8l ER9CTN7r/hbgHhckyxZUwUEtvllJ0rwBVKfIlcYqJqqKtLB2qSqUNmEUphQW4JORXaX+ Wqr/NRnGxGZzDn3i9m+KlLTy+GkFbj6It9Yne7vDZqhPLeU12mThwKVb0/QIyOS2YSWy MorEiYS5GovOIGDDKsZcN2Qv+cqvO2GC+Ppa4w+/y6Yfs3mPHKoMSnRi05lsx93gXEPy MwlBwA1wGAHEFswPi4H8MwIgRgzJM5WqKZuIfPwosFskH5V3vIbmxY8lp1w280hBf+EX qX/A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z1si23716834edc.600.2021.12.13.07.26.29; Mon, 13 Dec 2021 07:26:30 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9A7DB68B04A; Mon, 13 Dec 2021 17:22:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AAE4768AFC2 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6C62524062A for ; Mon, 13 Dec 2021 16:22:17 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 72DuX7DqOJw9 for ; Mon, 13 Dec 2021 16:22:16 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 44F0E24068D for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 68FCB3A0C0A; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:41 +0100 Message-Id: <20211213152042.5900-23-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 23/24] ffmpeg: simplify the use of OutputStream.frame_number X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: SBDTYCHlqF7w It features in limiting the number of output frames (-frames option) and currently can be incremented from two places: - for video encoding (not streamcopy), in do_video_out() after each successful avcodec_send_frame() call - for all other cases, in of_submit_packet() Not only is this inconsistent and confusing, but also the special treatment for video encoding is redundant, since AVCodecContext.frame_count stores the exact same value (number of successful avcodec_send_frame()) calls. Replace the use of OutputStream.frame_count in do_video_out() with AVCodecContext.frame_count. Modify OutputStream.frame_count in the same way for all streams. --- fftools/ffmpeg.c | 9 ++++----- fftools/ffmpeg_mux.c | 19 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 538601e440..aff3cc350e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1031,7 +1031,7 @@ static void do_video_out(OutputFile *of, switch (ost->vsync_method) { case VSYNC_VSCFR: - if (ost->frame_number == 0 && delta0 >= 0.5) { + if (enc->frame_number == 0 && delta0 >= 0.5) { av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); delta = duration; delta0 = 0; @@ -1039,7 +1039,7 @@ static void do_video_out(OutputFile *of, } case VSYNC_CFR: // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c - if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) { + if (frame_drop_threshold && delta < frame_drop_threshold && enc->frame_number) { nb_frames = 0; } else if (delta < -1.1) nb_frames = 0; @@ -1069,7 +1069,7 @@ static void do_video_out(OutputFile *of, * But there may be reordering, so we can't throw away frames on encoder * flush, we need to limit them here, before they go into encoder. */ - nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); + nb_frames = FFMIN(nb_frames, ost->max_frames - enc->frame_number); nb0_frames = FFMIN(nb0_frames, nb_frames); memmove(ost->last_nb0_frames + 1, @@ -1081,7 +1081,7 @@ static void do_video_out(OutputFile *of, nb_frames_drop++; av_log(NULL, AV_LOG_VERBOSE, "*** dropping frame %d from stream %d at ts %"PRId64"\n", - ost->frame_number, ost->st->index, ost->last_frame->pts); + enc->frame_number, ost->st->index, ost->last_frame->pts); } if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) { if (nb_frames > dts_error_threshold * 30) { @@ -1222,7 +1222,6 @@ static void do_video_out(OutputFile *of, } } ost->sync_opts++; - ost->frame_number++; if (vstats_filename && frame_size) do_video_stats(ost, frame_size); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e97ec8ab93..c3666fbba1 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -200,23 +200,18 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { - AVStream *st = ost->st; int ret; /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). + * Limit the number of output frames by dropping any excess packets. + * This should not trigger for video encoding, which due to possible + * reordering needs to be treated specially - see do_video_out(). */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; } + ost->frame_number++; if (of->mux->header_written) { write_packet(of, ost, pkt); From patchwork Mon Dec 13 15:20:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32436 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5575874iog; Mon, 13 Dec 2021 07:25:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyjHTSg1RW//czKF9SJQ366egUrBJPVrx3YR/oQ4NHVEtHuQ+Pu6jO9hkGvPG1LGxdYmVjJ X-Received: by 2002:a17:907:8a20:: with SMTP id sc32mr43227732ejc.65.1639409116732; Mon, 13 Dec 2021 07:25:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409116; cv=none; d=google.com; s=arc-20160816; b=a+rQWJ4Co89Hit1ocgo9dNdV0m7u4C7fC8EHp1JaektCZ4CGaa39CxyFe7W/RPDQdy BqMbEcWjO/05xsHRl32MiAl3a5RV69oXKdUrQvZK7O7BdgzmQUyc6r5jOwrzMe3hhwDF IU2Z0luM/rFXUJZULixCJjR77OieGSOvOoJ5FIGiH+30vXOUFwsBh+Lvj2MBgGgmv27k eLc7oBR9RdCld5qiG8lnaHmsnjHm1ETi1DdEzapetKZEbjg94VPX0lXDaH/6a8tAMAsu Q2gxhj0153hYaJaVxJjtefV1TWYMK6mH2F6k+C7utu5OrmzN5Mb/KYQ9/L7eEedFWBzX hibQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=U/Fc0kZJi0DSbXY0kQ+Jmiav4dCKbIiP3iNGllqBX4s=; b=QcMQrWAl86lMw+r7yIx8RjMSb+dAFMJKmPOfElw4PtKf8fW/DaRCEsSWrsgHtKeIrw sDsc+95DaDl8zXuCkJmxCSGYCaCWYiSlPpT285QPmlrvlvqg6ryuB1634I3Mmue7iC32 e4Ykz+nrbcObi+6kSM9UjLe33WYyCaSGaXJogR29pTFIIcyhofwntPOJ0ReTlJQNrbyo Ibi4ixiCPphs0KfhgEKc3nUnqRmJSjnF9j0ddNYTTCwl0MyYW3x9vqfMABuEgJ3GmEyo 9IpuyxO4QGE0QcapyuorHU8xPgUQmpkPeGiTPhMr54BGmQdAViOKjvbbXO1dAtz/wJJ1 xnYQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id pg17si12761778ejb.318.2021.12.13.07.25.16; Mon, 13 Dec 2021 07:25:16 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7FA5D68B014; Mon, 13 Dec 2021 17:22:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9A66B68AFBA for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8B0CC2404FE for ; Mon, 13 Dec 2021 16:22:15 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id TwRnjk3SQqmn for ; Mon, 13 Dec 2021 16:22:14 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id F3B3C240687 for ; Mon, 13 Dec 2021 16:22:07 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 6D46D3A073E; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:42 +0100 Message-Id: <20211213152042.5900-24-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 24/24] ffmpeg: move output file opts into private context X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ZlWLVm7yuD2O It is private to the muxer, no reason to access it from outside. --- fftools/ffmpeg.h | 3 +-- fftools/ffmpeg_mux.c | 9 ++++++--- fftools/ffmpeg_opt.c | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 76c8dfa4c8..6930671f5d 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -574,7 +574,6 @@ typedef struct OutputFile { const AVOutputFormat *format; AVFormatContext *ctx; - AVDictionary *opts; int ost_index; /* index of the first stream in output_streams */ int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units @@ -678,7 +677,7 @@ int hw_device_setup_for_filter(FilterGraph *fg); int hwaccel_decode_init(AVCodecContext *avctx); -int of_muxer_init(OutputFile *of, uint64_t limit_filesize); +int of_muxer_init(OutputFile *of, AVDictionary *opts, uint64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index c3666fbba1..f457ac5239 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -46,6 +46,8 @@ typedef struct MuxStream { struct Muxer { MuxStream *streams; + AVDictionary *opts; + /* filesize limit expressed in bytes */ uint64_t limit_filesize; uint64_t final_filesize; @@ -289,7 +291,7 @@ int of_check_init(OutputFile *of) return 0; } - ret = avformat_write_header(of->ctx, &of->opts); + ret = avformat_write_header(of->ctx, &of->mux->opts); if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "Could not write header for output file #%d " @@ -385,6 +387,7 @@ static void mux_free(Muxer **pmux, int nb_streams) av_fifo_freep(&ms->muxing_queue); } av_freep(&mux->streams); + av_dict_free(&mux->opts); av_freep(pmux); } @@ -404,12 +407,11 @@ void of_close(OutputFile **pof) if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE)) avio_closep(&s->pb); avformat_free_context(s); - av_dict_free(&of->opts); av_freep(pof); } -int of_muxer_init(OutputFile *of, uint64_t limit_filesize) +int of_muxer_init(OutputFile *of, AVDictionary *opts, uint64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); int ret = 0; @@ -435,6 +437,7 @@ int of_muxer_init(OutputFile *of, uint64_t limit_filesize) } mux->limit_filesize = limit_filesize; + mux->opts = opts; if (strcmp(of->format->name, "rtp")) want_sdp = 0; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 2d16068c4f..e2e1bfd40c 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2278,7 +2278,7 @@ static int open_output_file(OptionsContext *o, const char *filename) OutputFile *of; OutputStream *ost; InputStream *ist; - AVDictionary *unused_opts = NULL; + AVDictionary *unused_opts = NULL, *format_opts = NULL; const AVDictionaryEntry *e = NULL; if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) { @@ -2303,7 +2303,7 @@ static int open_output_file(OptionsContext *o, const char *filename) of->recording_time = o->recording_time; of->start_time = o->start_time; of->shortest = o->shortest; - av_dict_copy(&of->opts, o->g->format_opts, 0); + av_dict_copy(&format_opts, o->g->format_opts, 0); if (!strcmp(filename, "-")) filename = "pipe:"; @@ -2325,7 +2325,7 @@ static int open_output_file(OptionsContext *o, const char *filename) oc->flags |= AVFMT_FLAG_BITEXACT; of->bitexact = 1; } else { - of->bitexact = check_opt_bitexact(oc, of->opts, "fflags", + of->bitexact = check_opt_bitexact(oc, format_opts, "fflags", AVFMT_FLAG_BITEXACT); } @@ -2702,7 +2702,7 @@ loop_end: /* open the file */ if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE, &oc->interrupt_callback, - &of->opts)) < 0) { + &format_opts)) < 0) { print_error(filename, err); exit_program(1); } @@ -2710,7 +2710,7 @@ loop_end: assert_file_overwrite(filename); if (o->mux_preload) { - av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0); + av_dict_set_int(&format_opts, "preload", o->mux_preload*AV_TIME_BASE, 0); } oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE); @@ -2904,7 +2904,7 @@ loop_end: exit_program(1); } - err = of_muxer_init(of, o->limit_filesize); + err = of_muxer_init(of, format_opts, o->limit_filesize); if (err < 0) { av_log(NULL, AV_LOG_FATAL, "Error initializing the muxer\n"); exit_program(1);