[FFmpeg-devel] [PATCH 10/36] fftools/ffmpeg: return error codes from ist_*_add()

Anton Khirnov anton at khirnov.net
Wed May 17 13:20:03 EEST 2023


Will be useful in future commits.
---
 fftools/ffmpeg.h          |  4 ++--
 fftools/ffmpeg_demux.c    | 26 +++++++++++++++++++-------
 fftools/ffmpeg_filter.c   | 15 ++++++++++++---
 fftools/ffmpeg_mux_init.c | 10 ++++++++--
 4 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 9cb7198dfd..189454d629 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -879,8 +879,8 @@ void ifile_close(InputFile **f);
  */
 int ifile_get_packet(InputFile *f, AVPacket **pkt);
 
-void ist_output_add(InputStream *ist, OutputStream *ost);
-void ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
+int ist_output_add(InputStream *ist, OutputStream *ost);
+int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple);
 
 /**
  * Find an unused input stream of given type.
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index b93e171037..ae2133bdbf 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -844,7 +844,7 @@ void ifile_close(InputFile **pf)
     av_freep(pf);
 }
 
-static void ist_use(InputStream *ist, int decoding_needed)
+static int ist_use(InputStream *ist, int decoding_needed)
 {
     DemuxStream *ds = ds_from_ist(ist);
 
@@ -856,23 +856,33 @@ static void ist_use(InputStream *ist, int decoding_needed)
     if (decoding_needed && !avcodec_is_open(ist->dec_ctx)) {
         int ret = dec_open(ist);
         if (ret < 0)
-            report_and_exit(ret);
+            return ret;
     }
+
+    return 0;
 }
 
-void ist_output_add(InputStream *ist, OutputStream *ost)
+int ist_output_add(InputStream *ist, OutputStream *ost)
 {
-    ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
+    int ret;
+
+    ret = ist_use(ist, ost->enc ? DECODING_FOR_OST : 0);
+    if (ret < 0)
+        return ret;
 
     GROW_ARRAY(ist->outputs, ist->nb_outputs);
     ist->outputs[ist->nb_outputs - 1] = ost;
+
+    return 0;
 }
 
-void ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
+int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
 {
     int ret;
 
-    ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
+    ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER);
+    if (ret < 0)
+        return ret;
 
     GROW_ARRAY(ist->filters, ist->nb_filters);
     ist->filters[ist->nb_filters - 1] = ifilter;
@@ -880,7 +890,9 @@ void ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
     // initialize fallback parameters for filtering
     ret = ifilter_parameters_from_dec(ifilter, ist->dec_ctx);
     if (ret < 0)
-        report_and_exit(ret);
+        return ret;
+
+    return 0;
 }
 
 static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 8cc76209d0..ca43b4803a 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -334,6 +334,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     FilterGraph *fg;
     OutputFilter *ofilter;
     InputFilter  *ifilter;
+    int ret;
 
     fg = fg_create(NULL);
     if (!fg)
@@ -347,7 +348,9 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
     ifilter         = ifilter_alloc(fg);
     ifilter->ist    = ist;
 
-    ist_filter_add(ist, ifilter, 1);
+    ret = ist_filter_add(ist, ifilter, 1);
+    if (ret < 0)
+        return ret;
 
     return 0;
 }
@@ -375,7 +378,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     InputStream *ist = NULL;
     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
     InputFilter *ifilter;
-    int i;
+    int i, ret;
 
     // TODO: support other filter types
     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
@@ -435,7 +438,13 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
     ifilter->type   = ist->st->codecpar->codec_type;
     ifilter->name   = describe_filter_link(fg, in, 1);
 
-    ist_filter_add(ist, ifilter, 0);
+    ret = ist_filter_add(ist, ifilter, 0);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR,
+               "Error binding an input stream to complex filtergraph input %s.\n",
+               in->name ? in->name : "");
+        exit_program(1);
+    }
 }
 
 static int read_binary(const char *path, uint8_t **data, int *len)
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index 2c0e2faf4a..b73791acee 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1225,8 +1225,14 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o,
                        "Error initializing a simple filtergraph\n");
                 exit_program(1);
             }
-        } else
-            ist_output_add(ost->ist, ost);
+        } else {
+            ret = ist_output_add(ost->ist, ost);
+            if (ret < 0) {
+                av_log(ost, AV_LOG_ERROR,
+                       "Error binding an input stream\n");
+                exit_program(1);
+            }
+        }
     }
 
     if (ost->ist && !ost->enc) {
-- 
2.39.2



More information about the ffmpeg-devel mailing list