From patchwork Mon Apr 4 11:30:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 35162 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:c05:b0:7a:e998:b410 with SMTP id bw5csp1911876pzb; Mon, 4 Apr 2022 04:33:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiyXo/UpOyUL/GnXawbNonTDvzJtrv73lEwtLkimzKw7NAaA77IxBvzfvFMejk5RJUYZYz X-Received: by 2002:a17:907:6d94:b0:6e7:fe12:6e9a with SMTP id sb20-20020a1709076d9400b006e7fe126e9amr1015192ejc.584.1649072001332; Mon, 04 Apr 2022 04:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649072001; cv=none; d=google.com; s=arc-20160816; b=dDGiqH5CArOrk64ZvFfeyyiWEaG8/QcdYHNXhtZY9yf5jx4vQsmbkb/ns1BrwjvbRs nqucb+vbxPT5KFhDfG2MPBGiw8WXxXti3/aInG5OqIS+Qyuun5kQi31hMgoBsndOWItM ccOi3wTZ6Z2lGOxTZBVT0yQbHzY2uWswi/jZUZfkW2N9g5eZcNDrKx/pSB5xZSLJQ2a4 dA64djvlkyI2r1VS8upirJVXELqh3CiM41Nx3y7T4UsEq+E0GuEDg3iAMTLzEH6rtusg nsviUN+ZciPFeIBehLCLjptvTZKvhVnxY91KadKCmKVYHwN699ErqcQ7DOkAZ4+rNSIn NFCw== 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=mJF4RjokMhQmJtiBLdy1Pqjq5j86v/FBL2UHxbxw0kQ=; b=eWEMqgwaMm1rTCNmYA5OzX6ZEA4ecFXRQDiM2BT1eF5scPLnrKSYDUiJylWT4e58R7 ursjMSURmMPrwdEZGokmLCVoqysHcQyNHOZnf3f6NEhE/1QjZuwe1kLGiuLgDpUO1v22 fF4IEiLucDdHYPKz45toXfjj0EBiA24QeiMQnG4+vzhESifDyYb+4sJ4SOytBuqN0kva M0edeOmJ9TTkHK+VzSNgeemPcRWKJTjTHeWPUs11iT0wWyycLsWROkozT3s42N39LPKs OIrSkwAUs6v7HGoPRKUdUob5YRKfhJyFK6VMhKbmp+PvF15Xv9R34pM28WlwtnlXo+i+ /emg== 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 s16-20020a170906501000b006df76385bdasi6504972ejj.122.2022.04.04.04.33.21; Mon, 04 Apr 2022 04:33:21 -0700 (PDT) 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 9A6EB68B120; Mon, 4 Apr 2022 14:32:28 +0300 (EEST) 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 2FD65680602 for ; Mon, 4 Apr 2022 14:32:23 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B48E2240555 for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id XuUNjfVYXL9n for ; Mon, 4 Apr 2022 13:32:19 +0200 (CEST) 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 4E7AB240590 for ; Mon, 4 Apr 2022 13:32:15 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id C89753A0A3C; Mon, 4 Apr 2022 13:32:11 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Apr 2022 13:30:01 +0200 Message-Id: <20220404113037.13070-14-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404113037.13070-1-anton@khirnov.net> References: <20220404113037.13070-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 13/49] fftools/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: 5IC0v+UPrt7S Move the file size checking code to ffmpeg_mux. Use the recently introduced of_filesize(), making this code consistent with the size shown by print_report(). --- fftools/ffmpeg.c | 4 +--- fftools/ffmpeg.h | 4 ++-- fftools/ffmpeg_mux.c | 11 ++++++++++- fftools/ffmpeg_opt.c | 3 +-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 52dffa57bd..05e14c42c5 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3508,10 +3508,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 d76f4266d6..c3b90ac9a2 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -589,7 +589,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 - int64_t limit_filesize; /* filesize limit expressed in bytes */ int shortest; } OutputFile; @@ -690,7 +689,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, int64_t limit_filesize); /* open the muxer when all the streams are initialized */ int of_check_init(OutputFile *of); int of_write_trailer(OutputFile *of); @@ -698,6 +697,7 @@ 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_filesize(OutputFile *of); #endif /* FFTOOLS_FFMPEG_H */ diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 930a7bdfe7..78bb338bce 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -33,6 +33,8 @@ #include "libavformat/avio.h" struct Muxer { + /* filesize limit expressed in bytes */ + int64_t limit_filesize; int header_written; }; @@ -335,7 +337,7 @@ void of_close(OutputFile **pof) av_freep(pof); } -int of_muxer_init(OutputFile *of) +int of_muxer_init(OutputFile *of, int64_t limit_filesize) { Muxer *mux = av_mallocz(sizeof(*mux)); @@ -344,9 +346,16 @@ int of_muxer_init(OutputFile *of) of->mux = mux; + mux->limit_filesize = limit_filesize; + return 0; } +int of_finished(OutputFile *of) +{ + return of_filesize(of) >= of->mux->limit_filesize; +} + int64_t of_filesize(OutputFile *of) { AVIOContext *pb = of->ctx->pb; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 4e601f1cf8..4a41ebc736 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2318,7 +2318,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); @@ -2921,7 +2920,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 internal muxing state\n"); exit_program(1);