[FFmpeg-cvslog] fftools/ffmpeg: simplify output stream initialization call graph

Anton Khirnov git at videolan.org
Sun Apr 9 16:50:33 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Sat Mar 25 05:00:11 2023 +0100| [a996478e8c492215e021a22d7b197c255d05f725] | committer: Anton Khirnov

fftools/ffmpeg: simplify output stream initialization call graph

Several places in the code currently call init_output_stream_wrapper(),
which in turn calls init_output_stream(), which then calls either
enc_open() 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).

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=a996478e8c492215e021a22d7b197c255d05f725
---

 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 c439f94de1..5b42a914f8 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;
 }



More information about the ffmpeg-cvslog mailing list