[FFmpeg-devel] [PATCH 05/18] fftools/ffmpeg_dec: factor opening the decoder out of dec_open()

Anton Khirnov anton at khirnov.net
Wed Mar 6 13:03:06 EET 2024


Rename dec_open to dec_init(), as it is more descriptive of its new
purpose.

Will be useful in following commits, which will add a new path for
opening decoders.
---
 fftools/ffmpeg.h       |  2 +-
 fftools/ffmpeg_dec.c   | 61 ++++++++++++++++++++++--------------------
 fftools/ffmpeg_demux.c |  2 +-
 3 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 1c5ebcb43f..6b049e329b 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -740,7 +740,7 @@ AVBufferRef *hw_device_for_filter(void);
  * @retval ">=0" non-negative scheduler index on success
  * @retval "<0"  an error code on failure
  */
-int dec_open(Decoder **pdec, Scheduler *sch,
+int dec_init(Decoder **pdec, Scheduler *sch,
              AVDictionary **dec_opts, const DecoderOpts *o);
 void dec_free(Decoder **pdec);
 
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index 98c4a0c83c..af57b2bb9d 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -1066,19 +1066,11 @@ static int hw_device_setup_for_decode(DecoderPriv *dp,
     return 0;
 }
 
-int dec_open(Decoder **pdec, Scheduler *sch,
-             AVDictionary **dec_opts, const DecoderOpts *o)
+static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, const DecoderOpts *o)
 {
-    DecoderPriv *dp;
     const AVCodec *codec = o->codec;
     int ret;
 
-    *pdec = NULL;
-
-    ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
-    if (ret < 0)
-        return ret;
-
     dp->flags      = o->flags;
     dp->log_parent = o->log_parent;
 
@@ -1091,39 +1083,31 @@ int dec_open(Decoder **pdec, Scheduler *sch,
     snprintf(dp->log_name, sizeof(dp->log_name), "dec:%s", codec->name);
 
     dp->parent_name = av_strdup(o->name ? o->name : "");
-    if (!dp->parent_name) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!dp->parent_name)
+        return AVERROR(ENOMEM);
 
     if (codec->type == AVMEDIA_TYPE_SUBTITLE &&
         (dp->flags & DECODER_FLAG_FIX_SUB_DURATION)) {
         for (int i = 0; i < FF_ARRAY_ELEMS(dp->sub_prev); i++) {
             dp->sub_prev[i] = av_frame_alloc();
-            if (!dp->sub_prev[i]) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
+            if (!dp->sub_prev[i])
+                return AVERROR(ENOMEM);
         }
         dp->sub_heartbeat = av_frame_alloc();
-        if (!dp->sub_heartbeat) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
+        if (!dp->sub_heartbeat)
+            return AVERROR(ENOMEM);
     }
 
     dp->sar_override = o->par->sample_aspect_ratio;
 
     dp->dec_ctx = avcodec_alloc_context3(codec);
-    if (!dp->dec_ctx) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!dp->dec_ctx)
+        return AVERROR(ENOMEM);
 
     ret = avcodec_parameters_to_context(dp->dec_ctx, o->par);
     if (ret < 0) {
         av_log(dp, AV_LOG_ERROR, "Error initializing the decoder context.\n");
-        goto fail;
+        return ret;
     }
 
     dp->dec_ctx->opaque                = dp;
@@ -1140,22 +1124,41 @@ int dec_open(Decoder **pdec, Scheduler *sch,
         av_log(dp, AV_LOG_ERROR,
                "Hardware device setup failed for decoder: %s\n",
                av_err2str(ret));
-        goto fail;
+        return ret;
     }
 
     if ((ret = avcodec_open2(dp->dec_ctx, codec, dec_opts)) < 0) {
         av_log(dp, AV_LOG_ERROR, "Error while opening decoder: %s\n",
                av_err2str(ret));
-        goto fail;
+        return ret;
     }
 
     ret = check_avoptions(*dec_opts);
     if (ret < 0)
-        goto fail;
+        return ret;
 
     dp->dec.subtitle_header      = dp->dec_ctx->subtitle_header;
     dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
 
+    return 0;
+}
+
+int dec_init(Decoder **pdec, Scheduler *sch,
+             AVDictionary **dec_opts, const DecoderOpts *o)
+{
+    DecoderPriv *dp;
+    int ret;
+
+    *pdec = NULL;
+
+    ret = dec_alloc(&dp, sch, !!(o->flags & DECODER_FLAG_SEND_END_TS));
+    if (ret < 0)
+        return ret;
+
+    ret = dec_open(dp, dec_opts, o);
+    if (ret < 0)
+        goto fail;
+
     *pdec = &dp->dec;
 
     return dp->sch_idx;
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 29f4a26224..ae0f635d8c 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -935,7 +935,7 @@ static int ist_use(InputStream *ist, int decoding_needed)
 
         ds->dec_opts.log_parent = ist;
 
-        ret = dec_open(&ist->decoder, d->sch,
+        ret = dec_init(&ist->decoder, d->sch,
                        &ds->decoder_opts, &ds->dec_opts);
         if (ret < 0)
             return ret;
-- 
2.43.0



More information about the ffmpeg-devel mailing list