diff mbox series

[FFmpeg-devel,09/18] fftools/ffmpeg: simplify propagating fallback parameters from decoders to filters

Message ID 20240306110319.17339-9-anton@khirnov.net
State Accepted
Commit f5d03b972c49323a0cb03195d596aaae0ab397b1
Headers show
Series [FFmpeg-devel,01/18] fftools/cmdutils: fix printing group name in split_commandline() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov March 6, 2024, 11:03 a.m. UTC
Current callstack looks like this:
* ifilter_bind_ist() (filter) calls ist_filter_add() (demuxer);
* ist_filter_add() opens the decoder, and then calls
  dec_add_filter() (decoder);
* dec_add_filter() calls ifilter_parameters_from_dec() (i.e. back into
  the filtering code) in order to give post-avcodec_open2() parameters
  to the filter.

This is unnecessarily complicated. Pass the parameters as follows
instead:
* dec_init() (which opens the decoder) returns post-avcodec_open2()
  parameters to its caller (i.e. the demuxer) in a parameter-only
  AVFrame
* the demuxer passes these parameters to the filter in
  InputFilterOptions, together with other filter options
---
 fftools/ffmpeg.h        |  9 ++++---
 fftools/ffmpeg_dec.c    | 36 ++++++++++++++++++--------
 fftools/ffmpeg_demux.c  | 25 ++++++++++++++----
 fftools/ffmpeg_filter.c | 57 ++++++++++++-----------------------------
 4 files changed, 67 insertions(+), 60 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d3e03543ac..1a60f035b0 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -259,6 +259,8 @@  typedef struct InputFilterOptions {
 
     // a combination of IFILTER_FLAG_*
     unsigned            flags;
+
+    AVFrame            *fallback;
 } InputFilterOptions;
 
 typedef struct InputFilter {
@@ -735,16 +737,17 @@  AVBufferRef *hw_device_for_filter(void);
 /**
  * @param dec_opts Dictionary filled with decoder options. Its ownership
  *                 is transferred to the decoder.
+ * @param param_out If non-NULL, media properties after opening the decoder
+ *                  are written here.
  *
  * @retval ">=0" non-negative scheduler index on success
  * @retval "<0"  an error code on failure
  */
 int dec_init(Decoder **pdec, Scheduler *sch,
-             AVDictionary **dec_opts, const DecoderOpts *o);
+             AVDictionary **dec_opts, const DecoderOpts *o,
+             AVFrame *param_out);
 void dec_free(Decoder **pdec);
 
-int dec_add_filter(Decoder *dec, InputFilter *ifilter);
-
 int enc_alloc(Encoder **penc, const AVCodec *codec,
               Scheduler *sch, unsigned sch_idx);
 void enc_free(Encoder **penc);
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index af57b2bb9d..7005ada527 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -1066,7 +1066,8 @@  static int hw_device_setup_for_decode(DecoderPriv *dp,
     return 0;
 }
 
-static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, const DecoderOpts *o)
+static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
+                    const DecoderOpts *o, AVFrame *param_out)
 {
     const AVCodec *codec = o->codec;
     int ret;
@@ -1140,11 +1141,32 @@  static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, const DecoderOpts
     dp->dec.subtitle_header      = dp->dec_ctx->subtitle_header;
     dp->dec.subtitle_header_size = dp->dec_ctx->subtitle_header_size;
 
+    if (param_out) {
+        if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
+            param_out->format               = dp->dec_ctx->sample_fmt;
+            param_out->sample_rate          = dp->dec_ctx->sample_rate;
+
+            ret = av_channel_layout_copy(&param_out->ch_layout, &dp->dec_ctx->ch_layout);
+            if (ret < 0)
+                return ret;
+        } else if (dp->dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+            param_out->format               = dp->dec_ctx->pix_fmt;
+            param_out->width                = dp->dec_ctx->width;
+            param_out->height               = dp->dec_ctx->height;
+            param_out->sample_aspect_ratio  = dp->dec_ctx->sample_aspect_ratio;
+            param_out->colorspace           = dp->dec_ctx->colorspace;
+            param_out->color_range          = dp->dec_ctx->color_range;
+        }
+
+        param_out->time_base = dp->dec_ctx->pkt_timebase;
+    }
+
     return 0;
 }
 
 int dec_init(Decoder **pdec, Scheduler *sch,
-             AVDictionary **dec_opts, const DecoderOpts *o)
+             AVDictionary **dec_opts, const DecoderOpts *o,
+             AVFrame *param_out)
 {
     DecoderPriv *dp;
     int ret;
@@ -1155,7 +1177,7 @@  int dec_init(Decoder **pdec, Scheduler *sch,
     if (ret < 0)
         return ret;
 
-    ret = dec_open(dp, dec_opts, o);
+    ret = dec_open(dp, dec_opts, o, param_out);
     if (ret < 0)
         goto fail;
 
@@ -1166,11 +1188,3 @@  fail:
     dec_free((Decoder**)&dp);
     return ret;
 }
-
-int dec_add_filter(Decoder *dec, InputFilter *ifilter)
-{
-    DecoderPriv *dp = dp_from_dec(dec);
-
-    // initialize fallback parameters for filtering
-    return ifilter_parameters_from_dec(ifilter, dp->dec_ctx);
-}
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index ae0f635d8c..032a96567f 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -81,6 +81,8 @@  typedef struct DemuxStream {
     AVDictionary            *decoder_opts;
     DecoderOpts              dec_opts;
     char                     dec_name[16];
+    // decoded media properties, as estimated by opening the decoder
+    AVFrame                 *decoded_params;
 
     AVBSFContext            *bsf;
 
@@ -839,6 +841,8 @@  static void ist_free(InputStream **pist)
 
     avcodec_parameters_free(&ist->par);
 
+    av_frame_free(&ds->decoded_params);
+
     av_bsf_free(&ds->bsf);
 
     av_freep(pist);
@@ -935,8 +939,12 @@  static int ist_use(InputStream *ist, int decoding_needed)
 
         ds->dec_opts.log_parent = ist;
 
+        ds->decoded_params = av_frame_alloc();
+        if (!ds->decoded_params)
+            return AVERROR(ENOMEM);
+
         ret = dec_init(&ist->decoder, d->sch,
-                       &ds->decoder_opts, &ds->dec_opts);
+                       &ds->decoder_opts, &ds->dec_opts, ds->decoded_params);
         if (ret < 0)
             return ret;
         ds->sch_idx_dec = ret;
@@ -988,10 +996,6 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
 
     ist->filters[ist->nb_filters - 1] = ifilter;
 
-    ret = dec_add_filter(ist->decoder, ifilter);
-    if (ret < 0)
-        return ret;
-
     if (ist->par->codec_type == AVMEDIA_TYPE_VIDEO) {
         if (ist->framerate.num > 0 && ist->framerate.den > 0) {
             opts->framerate = ist->framerate;
@@ -1027,6 +1031,17 @@  int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
         ds->have_sub2video = 1;
     }
 
+    ret = av_frame_copy_props(opts->fallback, ds->decoded_params);
+    if (ret < 0)
+        return ret;
+    opts->fallback->format = ds->decoded_params->format;
+    opts->fallback->width  = ds->decoded_params->width;
+    opts->fallback->height = ds->decoded_params->height;
+
+    ret = av_channel_layout_copy(&opts->fallback->ch_layout, &ds->decoded_params->ch_layout);
+    if (ret < 0)
+        return ret;
+
     if (copy_ts) {
         tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time;
         if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE)
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 7aa9305c88..6438cbd73b 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -675,6 +675,10 @@  static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist)
 
     ifp->type_src        = ist->st->codecpar->codec_type;
 
+    ifp->opts.fallback = av_frame_alloc();
+    if (!ifp->opts.fallback)
+        return AVERROR(ENOMEM);
+
     dec_idx = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph),
                              &ifp->opts);
     if (dec_idx < 0)
@@ -863,9 +867,8 @@  static InputFilter *ifilter_alloc(FilterGraph *fg)
 
     ifp->index           = fg->nb_inputs - 1;
     ifp->format          = -1;
-    ifp->fallback.format = -1;
-    ifp->color_space = ifp->fallback.color_space = AVCOL_SPC_UNSPECIFIED;
-    ifp->color_range = ifp->fallback.color_range = AVCOL_RANGE_UNSPECIFIED;
+    ifp->color_space     = AVCOL_SPC_UNSPECIFIED;
+    ifp->color_range     = AVCOL_RANGE_UNSPECIFIED;
 
     ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
     if (!ifp->frame_queue)
@@ -895,9 +898,8 @@  void fg_free(FilterGraph **pfg)
         }
         av_frame_free(&ifp->sub2video.frame);
 
-        av_channel_layout_uninit(&ifp->fallback.ch_layout);
-
         av_frame_free(&ifp->frame);
+        av_frame_free(&ifp->opts.fallback);
 
         av_buffer_unref(&ifp->hw_frames_ctx);
         av_freep(&ifp->linklabel);
@@ -1804,33 +1806,6 @@  fail:
     return ret;
 }
 
-int ifilter_parameters_from_dec(InputFilter *ifilter, const AVCodecContext *dec)
-{
-    InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
-
-    ifp->fallback.time_base = dec->pkt_timebase;
-
-    if (dec->codec_type == AVMEDIA_TYPE_VIDEO) {
-        ifp->fallback.format                 = dec->pix_fmt;
-        ifp->fallback.width                  = dec->width;
-        ifp->fallback.height                 = dec->height;
-        ifp->fallback.sample_aspect_ratio    = dec->sample_aspect_ratio;
-        ifp->fallback.color_space            = dec->colorspace;
-        ifp->fallback.color_range            = dec->color_range;
-    } else if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
-        int ret;
-
-        ifp->fallback.format                 = dec->sample_fmt;
-        ifp->fallback.sample_rate            = dec->sample_rate;
-
-        ret = av_channel_layout_copy(&ifp->fallback.ch_layout, &dec->ch_layout);
-        if (ret < 0)
-            return ret;
-    }
-
-    return 0;
-}
-
 static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
 {
     InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
@@ -2527,17 +2502,17 @@  static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
     } else {
         if (ifp->format < 0) {
             // the filtergraph was never configured, use the fallback parameters
-            ifp->format                 = ifp->fallback.format;
-            ifp->sample_rate            = ifp->fallback.sample_rate;
-            ifp->width                  = ifp->fallback.width;
-            ifp->height                 = ifp->fallback.height;
-            ifp->sample_aspect_ratio    = ifp->fallback.sample_aspect_ratio;
-            ifp->color_space            = ifp->fallback.color_space;
-            ifp->color_range            = ifp->fallback.color_range;
-            ifp->time_base              = ifp->fallback.time_base;
+            ifp->format                 = ifp->opts.fallback->format;
+            ifp->sample_rate            = ifp->opts.fallback->sample_rate;
+            ifp->width                  = ifp->opts.fallback->width;
+            ifp->height                 = ifp->opts.fallback->height;
+            ifp->sample_aspect_ratio    = ifp->opts.fallback->sample_aspect_ratio;
+            ifp->color_space            = ifp->opts.fallback->colorspace;
+            ifp->color_range            = ifp->opts.fallback->color_range;
+            ifp->time_base              = ifp->opts.fallback->time_base;
 
             ret = av_channel_layout_copy(&ifp->ch_layout,
-                                         &ifp->fallback.ch_layout);
+                                         &ifp->opts.fallback->ch_layout);
             if (ret < 0)
                 return ret;