[FFmpeg-cvslog] fftools/cmdutils: add error handling to filter_codec_opts()

Anton Khirnov git at videolan.org
Thu Jul 20 21:57:32 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Fri Jul 14 17:19:28 2023 +0200| [87f0333af1810047fcd592f680c8d0a596bca7f6] | committer: Anton Khirnov

fftools/cmdutils: add error handling to filter_codec_opts()

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

 fftools/cmdutils.c        | 41 ++++++++++++++++++++++++++++-------------
 fftools/cmdutils.h        |  8 +++++---
 fftools/ffmpeg_demux.c    |  5 ++++-
 fftools/ffmpeg_mux_init.c | 11 ++++++++---
 fftools/ffplay.c          |  6 +++++-
 fftools/ffprobe.c         |  8 ++++++--
 6 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index fa2797ebba..c2a783a983 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -943,8 +943,9 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
     return ret;
 }
 
-AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
-                                AVFormatContext *s, AVStream *st, const AVCodec *codec)
+int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
+                      AVFormatContext *s, AVStream *st, const AVCodec *codec,
+                      AVDictionary **dst)
 {
     AVDictionary    *ret = NULL;
     const AVDictionaryEntry *t = NULL;
@@ -977,12 +978,16 @@ AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_i
         char *p = strchr(t->key, ':');
 
         /* check stream specification in opt name */
-        if (p)
-            switch (check_stream_specifier(s, st, p + 1)) {
-            case  1: *p = 0; break;
-            case  0:         continue;
-            default:         exit_program(1);
-            }
+        if (p) {
+            int err = check_stream_specifier(s, st, p + 1);
+            if (err < 0) {
+                av_dict_free(&ret);
+                return err;
+            } else if (!err)
+                continue;
+
+            *p = 0;
+        }
 
         if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) ||
             !codec ||
@@ -998,14 +1003,16 @@ AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_i
         if (p)
             *p = ':';
     }
-    return ret;
+
+    *dst = ret;
+    return 0;
 }
 
 int setup_find_stream_info_opts(AVFormatContext *s,
                                 AVDictionary *codec_opts,
                                 AVDictionary ***dst)
 {
-    int i;
+    int ret;
     AVDictionary **opts;
 
     *dst = NULL;
@@ -1017,11 +1024,19 @@ int setup_find_stream_info_opts(AVFormatContext *s,
     if (!opts)
         return AVERROR(ENOMEM);
 
-    for (i = 0; i < s->nb_streams; i++)
-        opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
-                                    s, s->streams[i], NULL);
+    for (int i = 0; i < s->nb_streams; i++) {
+        ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id,
+                                s, s->streams[i], NULL, &opts[i]);
+        if (ret < 0)
+            goto fail;
+    }
     *dst = opts;
     return 0;
+fail:
+    for (int i = 0; i < s->nb_streams; i++)
+        av_dict_free(&opts[i]);
+    av_freep(&opts);
+    return ret;
 }
 
 int grow_array(void **array, int elem_size, int *size, int new_size)
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 8c1bde2a2f..56b7f130af 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -333,10 +333,12 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
  * @param st A stream from s for which the options should be filtered.
  * @param codec The particular codec for which the options should be filtered.
  *              If null, the default one is looked up according to the codec id.
- * @return a pointer to the created dictionary
+ * @param dst a pointer to the created dictionary
+ * @return a non-negative number on success, a negative error code on failure
  */
-AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
-                                AVFormatContext *s, AVStream *st, const AVCodec *codec);
+int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
+                      AVFormatContext *s, AVStream *st, const AVCodec *codec,
+                      AVDictionary **dst);
 
 /**
  * Setup AVCodecContext options for avformat_find_stream_info().
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index a41b59ceb8..d612c5f434 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -1176,7 +1176,10 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
     if (ret < 0)
         return ret;
 
-    ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
+    ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id,
+                            ic, st, ist->dec, &ist->decoder_opts);
+    if (ret < 0)
+        return ret;
 
     ist->reinit_filters = -1;
     MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st);
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index da304ee727..86521417ec 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -1206,8 +1206,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
         const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
         const char *enc_time_base = NULL;
 
-        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, enc->codec_id,
-                                              oc, st, enc->codec);
+        ret = filter_codec_opts(o->g->codec_opts, enc->codec_id,
+                                oc, st, enc->codec, &ost->encoder_opts);
+        if (ret < 0)
+            return ret;
 
         MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
         ost->autoscale = 1;
@@ -1305,7 +1307,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type,
             ost->enc_timebase = q;
         }
     } else {
-        ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL);
+        ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st,
+                                NULL, &ost->encoder_opts);
+        if (ret < 0)
+            return ret;
     }
 
 
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index 4e26b3309d..df20c6a29d 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2584,7 +2584,11 @@ static int stream_component_open(VideoState *is, int stream_index)
     if (fast)
         avctx->flags2 |= AV_CODEC_FLAG2_FAST;
 
-    opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
+    ret = filter_codec_opts(codec_opts, avctx->codec_id, ic,
+                            ic->streams[stream_index], codec, &opts);
+    if (ret < 0)
+        goto fail;
+
     if (!av_dict_get(opts, "threads", NULL, 0))
         av_dict_set(&opts, "threads", "auto", 0);
     if (stream_lowres)
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index ba55437760..da8fc89830 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -3421,8 +3421,12 @@ static int open_input_file(InputFile *ifile, const char *filename,
             continue;
         }
         {
-            AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
-                                                   fmt_ctx, stream, codec);
+            AVDictionary *opts;
+
+            err = filter_codec_opts(codec_opts, stream->codecpar->codec_id,
+                                    fmt_ctx, stream, codec, &opts);
+            if (err < 0)
+                exit(1);
 
             ist->dec_ctx = avcodec_alloc_context3(codec);
             if (!ist->dec_ctx)



More information about the ffmpeg-cvslog mailing list