From patchwork Thu Jun 16 19:55:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 36259 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1127499pzb; Thu, 16 Jun 2022 13:04:07 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sT2JYXLz8ilJOUCjZRZ7aTmdLRZjllpm+av7+mW2ZZpoVZZyWOV3r8Tjo4dPUDuoS7D/QT X-Received: by 2002:a17:906:dff9:b0:6ff:2f22:7d0 with SMTP id lc25-20020a170906dff900b006ff2f2207d0mr5912019ejc.198.1655409847003; Thu, 16 Jun 2022 13:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655409846; cv=none; d=google.com; s=arc-20160816; b=nl+ISEh9jE8bq0PYknMTp8xeB37qb8e6SnHb4n14Ue8iUD2pIE3WR84OYEX56UDdj2 TRULYr1eEpf6Oiqj8wckRiHOg06qlhV0uP7NaN1ebaEJcVVp5MxwL1+bGp5s9Im3YeiG +2WDPAoWH9Oy7SQ213KV6XCMy6rM8wo4d0RJ+F47u2I3S+dX7iDphI1WaAHu18llKXtj lbUWT9C22HdHlNRISbeGHVyIy01K60YBityNHVu3erCzEFNXi4xjsInk2W+MvXQKnFRz 4jCLBhlwc07+rRQRd86Zu5XmSgJogABRR9AhpbO4UzO7VvLv6xVf6DSanMKOjS3i5ydT E75A== 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=6xJvu9N9I3jNuYLMPzOsk212UKLxd+8FPyUZPT7wJEg=; b=mYDyeKqW6CT24mf36RQXPc9bNvowKXK/7DNt+dm9vHky7vXuvomYusOpSJw5RD6873 iob3jggjzztquLpdky/hSFD+DFkUXz0TiB3/cnrDWywhaaMGrNhBeLyf6he9hGrSqNEa h15K+667cc75jYYkwO+WMtrZAZYVaqGX4Mk+LOTpCpeOGfjguCqHhlZdbayVoIXSLMWu 02B8GWGMUJiY8hYWa2C6ZtWcogDKbCwyTZwNlBfi24tVzTb3482OZTzr9GgW91tKkL0j X0uPrfMznwxbcAcx2+JfTbzLE1pUf3N5opmGD3PzbEo5lGbmK40CTxi/JgBx3di0lGl/ m2XQ== 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 oz42-20020a1709077daa00b006fe4c66b711si3126283ejc.46.2022.06.16.13.04.06; Thu, 16 Jun 2022 13:04:06 -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 1457968B80E; Thu, 16 Jun 2022 23:03:44 +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 987C468B7B3 for ; Thu, 16 Jun 2022 23:03:35 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 4CA1D24017E for ; Thu, 16 Jun 2022 22:03:35 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id uL3EDKtJdAH4 for ; Thu, 16 Jun 2022 22:03:34 +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 9E93924017C for ; Thu, 16 Jun 2022 22:03:33 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 85C953A1209; Thu, 16 Jun 2022 22:03:30 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 16 Jun 2022 21:55:03 +0200 Message-Id: <20220616195534.5278-4-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220616195534.5278-1-anton@khirnov.net> References: <20220616195534.5278-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 04/35] 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: Jc1bV6Mdqgls 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 5763f231c6..8d5f071fd1 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3454,10 +3454,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 d126f5d8e6..a97cd2a8e4 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -594,7 +594,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; @@ -695,7 +694,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); @@ -703,6 +702,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 83558f7f7d..207231f33b 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; }; @@ -322,7 +324,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)); @@ -331,9 +333,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 8cd07eddb0..28809d0c0b 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -2344,7 +2344,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); @@ -2947,7 +2946,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);