From patchwork Sat Mar 25 19:15:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40830 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:7a30:b0:df:834d:2c1a with SMTP id t48csp225997pzh; Sat, 25 Mar 2023 12:17:52 -0700 (PDT) X-Google-Smtp-Source: AKy350ZMB1h1pmM+8Y5E+4vrWZEGnwqk6mRreA819iUEx2PuhIqbpiQKGJVxjJSkfpcj/957XO4B X-Received: by 2002:a17:907:9725:b0:93f:9594:d97d with SMTP id jg37-20020a170907972500b0093f9594d97dmr2244295ejc.14.1679771871765; Sat, 25 Mar 2023 12:17:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679771871; cv=none; d=google.com; s=arc-20160816; b=bn/9Lfpl53vQ7+rxY3D62IfZ5kADApXqI/u7uKafbbXvVGvwsvjGXszv716q7l7/gf JLpo4M+zWbeReGAmFkeGLjUTcD3E0IjryM9WaPEjeU1qD5CSwiSsZ7e0s4KT9glmRUDm od+zKacKg8lLOJMRAAwhhMt0ehI7xHrwLKpWFUDMexSfvFA57CAJojIAtbHxBTafqxmk 3wWeLuWeduDSPGaZ39VxBB3ovpKTvnKlkj8jDE2PiUawJIj/ulXoSAiPq/B9U6LxcWge TwN1nEvWElVqVkagRQAA2hrru4GxTAUPaIC5P5Vco1qw52gYcSVwTqLTtQC+39ztjRfG PluQ== 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=FYyjtuwH4FzMAgIMEpl8E78IzIC+VPArsG3JVHq9Zpk=; b=nQRthSVsXQwv0bCRWDy0Y8N4Dq30eTTJOAgBpqUQcLIC82u0Bk4trvHJwwDVvvN9mk doa+VQecKgrdrmlPF7J/aJ7eysgrcafUJgzWTlZUU75gA6AbACy4tREhx62DrJLvvgeF H3JziVMwMD8PLrL8WF++6JqCjPxtOKmK11t3doblmLGnZ3DxWKrflkjL4l7C7Xv7wFLB R4NcZmWBfcDQsrgeRsT54P8fp9w2+lvBClzz9TsTVkAia12lXtibabTXDMpaf9BWe9qd MtJsvVH0vOf8ISTtf3OXKBTj6g2X7nFbm/aZUP7C0jjnSsk53dLKoQJLkoY7cgCdX1TH NW2A== 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 t8-20020a1709067c0800b0093129677645si10703538ejo.671.2023.03.25.12.17.51; Sat, 25 Mar 2023 12:17:51 -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 4C12C68C9D8; Sat, 25 Mar 2023 21:16: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 A385268C97F for ; Sat, 25 Mar 2023 21:16:11 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 5C00A2404EE for ; Sat, 25 Mar 2023 20:16:11 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id BkTuPfZfEToo for ; Sat, 25 Mar 2023 20:16: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 4364D240D03 for ; Sat, 25 Mar 2023 20:16:01 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 0630D3A06E4 for ; Sat, 25 Mar 2023 20:15:55 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Mar 2023 20:15:21 +0100 Message-Id: <20230325191529.10578-15-anton@khirnov.net> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230325191529.10578-1-anton@khirnov.net> References: <20230325191529.10578-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/23] fftools/ffmpeg: simplify output stream initialization call graph 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: dpN7V/5fhFPn Several places in the code currently call init_output_stream_wrapper(), which in turn calls init_output_stream(), which then calls either enc_init() or init_output_stream_streamcopy(), followed by of_stream_init(), which tells the muxer the stream is ready for muxing. All except one of these callers are in the encoding code, which will be moved to ffmpeg_enc.c. Keeping this structure would then necessitate ffmpeg_enc.c calling back into the common code in ffmpeg.c, which would then just call ffmpeg_mux, thus making the already convoluted call chain even more so. Simplify the situation by using separate paths for filter-fed output streams (audio and video encoders) and others (subtitles, streamcopy, data, attachments). --- fftools/ffmpeg.c | 51 +++++++++++++++++--------------------------- fftools/ffmpeg_enc.c | 7 ++++++ 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 03e5391970..1969ce9295 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -724,25 +724,6 @@ early_exit: return float_pts; } -static int init_output_stream(OutputStream *ost, AVFrame *frame); - -static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame, - unsigned int fatal) -{ - int ret = AVERROR_BUG; - - if (ost->initialized) - return 0; - - ret = init_output_stream(ost, frame); - if (ret < 0) { - if (fatal) - exit_program(1); - } - - return ret; -} - static double psnr(double d) { return -10.0 * log10(d); @@ -1023,7 +1004,9 @@ static void do_audio_out(OutputFile *of, OutputStream *ost, AVCodecContext *enc = ost->enc_ctx; int ret; - init_output_stream_wrapper(ost, frame, 1); + ret = enc_open(ost, frame); + if (ret < 0) + exit_program(1); if (frame->pts == AV_NOPTS_VALUE) frame->pts = ost->next_pts; @@ -1264,7 +1247,9 @@ static void do_video_out(OutputFile *of, InputStream *ist = ost->ist; AVFilterContext *filter = ost->filter->filter; - init_output_stream_wrapper(ost, next_picture, 1); + ret = enc_open(ost, next_picture); + if (ret < 0) + exit_program(1); frame_rate = av_buffersink_get_frame_rate(filter); if (frame_rate.num > 0 && frame_rate.den > 0) @@ -1820,7 +1805,9 @@ static void flush_encoders(void) of_output_packet(of, ost->pkt, ost, 1); } - init_output_stream_wrapper(ost, NULL, 1); + ret = enc_open(ost, NULL); + if (ret < 0) + exit_program(1); } if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO) @@ -2967,24 +2954,26 @@ static int init_output_stream_streamcopy(OutputStream *ost) return 0; } -static int init_output_stream(OutputStream *ost, AVFrame *frame) +static int init_output_stream_nofilter(OutputStream *ost) { int ret = 0; if (ost->enc_ctx) { - ret = enc_open(ost, frame); + ret = enc_open(ost, NULL); if (ret < 0) return ret; - } else if (ost->ist) { - ret = init_output_stream_streamcopy(ost); + } else { + if (ost->ist) { + ret = init_output_stream_streamcopy(ost); + if (ret < 0) + return ret; + } + + ret = of_stream_init(output_files[ost->file_index], ost); if (ret < 0) return ret; } - ret = of_stream_init(output_files[ost->file_index], ost); - if (ret < 0) - return ret; - return ret; } @@ -3017,7 +3006,7 @@ static int transcode_init(void) ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)) continue; - ret = init_output_stream_wrapper(ost, NULL, 0); + ret = init_output_stream_nofilter(ost); if (ret < 0) goto dump_format; } diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 9db2ff5ef3..a4660051a2 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -93,6 +93,9 @@ int enc_open(OutputStream *ost, AVFrame *frame) OutputFile *of = output_files[ost->file_index]; int ret; + if (ost->initialized) + return 0; + set_encoder_id(output_files[ost->file_index], ost); if (ist) { @@ -338,5 +341,9 @@ int enc_open(OutputStream *ost, AVFrame *frame) ost->mux_timebase = enc_ctx->time_base; + ret = of_stream_init(of, ost); + if (ret < 0) + return ret; + return 0; }