From patchwork Tue Jan 11 09:58:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 33211 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp3545507iog; Tue, 11 Jan 2022 02:00:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJx1CL+X90jesMPYuvcU/PIAlfJz1agswTx3MBivNx/c3CQ96QfFqr0MJBuB0dBVlLPhtCoZ X-Received: by 2002:a17:907:3fa0:: with SMTP id hr32mr3145229ejc.196.1641895229168; Tue, 11 Jan 2022 02:00:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641895229; cv=none; d=google.com; s=arc-20160816; b=Ed7B77rG5HQFnQDVVOaeKQpym6W6xYro90fk5cVXdrsx7AvNxuCyEFB9JaWx0HUsuF 4Q921xJeP8rDN1lamGbBa2fpdoN57THus8GsBn/Ym6DT6txgVmAGbB6RKV5AT7t8AEax uoqS19SDpPIFyZl02DNvwHRuOcbWWZWgYj8DZc+9x65ZHOSQbsfbr60LNmPIDfRpLPO5 pOAAsGW2XgV/t5BblA8q7bMTntmwPfjIqY+Zfz4quS2sFYMZ23rJXplVt3LV438CYkUY Re6KSp5Gr94ugzkpsYVj3VpeCKFmY7auwOBdyqlI5mRD9JkxkCCwe8MYuUrZl0WNzFne /tzA== 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=u0QGZ+MpkSAc/rGLhxXI/KJGTInvm3s0ZJbgpFVljGs=; b=YP5qwWEAWEOuODzF9anuCsfr50g23T2y2UmROWjxffa+szUB9vFPjRw42g/jHSFzas EYYZO/fvnLUGqr8XlTVBVnWaCx0R9phlEtPRDOX8RwxUUgnPcLakh5hYBh8RoFCMifiG cvyM7+bv0pmfSnOAiOLXQsfletDIB48UsiK0OCpkkbnByOLu8tB/XNQ0serYOVF7gF41 /sCzrWwmy2E0+svNWwdWpyc0IBfE/U4gEZdQzeMNuuVRWf/YC+nEG0eWRAyoP/u/zHa/ 86Tew28fDJF9pn53nWkIBaRGLUkuLNoWd7JkisfTXwoirBJRrbimx3+//k0L2hCXXJsU xxZQ== 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 x12si33848edd.118.2022.01.11.02.00.28; Tue, 11 Jan 2022 02:00:29 -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 0FD2968AF96; Tue, 11 Jan 2022 11:59: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 640B068AF3E for ; Tue, 11 Jan 2022 11:59:09 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 51A15240507 for ; Tue, 11 Jan 2022 10:59:06 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id rO1yYBrt-Ilu for ; Tue, 11 Jan 2022 10:59:05 +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 9282624052D for ; Tue, 11 Jan 2022 10:59:02 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id F3F883A09DE; Tue, 11 Jan 2022 10:59:01 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jan 2022 10:58:12 +0100 Message-Id: <20220111095830.31542-10-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220111095830.31542-1-anton@khirnov.net> References: <20220111095830.31542-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/28] 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: lPWk6X8f2b15 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 1b5cf4d9af..37c4a0d285 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3501,10 +3501,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 80b90e372d..cae9800da3 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 - int64_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, 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); @@ -695,6 +694,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 7c8414ac93..aff40776f6 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; }; @@ -336,7 +338,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)); @@ -345,9 +347,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 4d2b80683e..0b4f437912 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 internal muxing state\n"); exit_program(1);