[FFmpeg-devel] [PATCH 4/8] fftools/ffmpeg: propagate decoded_side_data from decoded streams to the filterchain
James Almer
jamrial at gmail.com
Fri Jan 10 20:34:55 EET 2025
Global side data as exported by a decoder may no longer apply if a filter in
the chain altered the frames in some form, like changing color, dimensions,
or channel layout information.
After this change, any such changes in side data will be taken into account by
the encoder futher in the process.
Signed-off-by: James Almer <jamrial at gmail.com>
---
fftools/ffmpeg_dec.c | 10 ++++++++
fftools/ffmpeg_filter.c | 56 ++++++++++++++++++++++++++++++++++++++++-
fftools/ffmpeg_utils.h | 16 ++++++++++++
3 files changed, 81 insertions(+), 1 deletion(-)
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index 2723a0312e..84ba5c6d5d 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -788,6 +788,11 @@ static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame)
frame->time_base = dec->pkt_timebase;
+ ret = clone_side_data(&frame->side_data, &frame->nb_side_data,
+ dec->decoded_side_data, dec->nb_decoded_side_data, 0);
+ if (ret < 0)
+ return ret;
+
if (dec->codec_type == AVMEDIA_TYPE_AUDIO) {
dp->dec.samples_decoded += frame->nb_samples;
@@ -1638,6 +1643,11 @@ static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts,
param_out->color_range = dp->dec_ctx->color_range;
}
+ av_frame_side_data_free(¶m_out->side_data, ¶m_out->nb_side_data);
+ ret = clone_side_data(¶m_out->side_data, ¶m_out->nb_side_data,
+ dp->dec_ctx->decoded_side_data, dp->dec_ctx->nb_decoded_side_data, 0);
+ if (ret < 0)
+ return ret;
param_out->time_base = dp->dec_ctx->pkt_timebase;
}
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 29797a0b71..e5a1bd7b6b 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -139,6 +139,9 @@ typedef struct InputFilterPriv {
AVRational time_base;
+ AVFrameSideData **side_data;
+ int nb_side_data;
+
AVFifo *frame_queue;
AVBufferRef *hw_frames_ctx;
@@ -205,6 +208,9 @@ typedef struct OutputFilterPriv {
enum AVColorSpace color_space;
enum AVColorRange color_range;
+ AVFrameSideData **side_data;
+ int nb_side_data;
+
// time base in which the output is sent to our downstream
// does not need to match the filtersink's timebase
AVRational tb_out;
@@ -1009,6 +1015,7 @@ void fg_free(FilterGraph **pfg)
av_buffer_unref(&ifp->hw_frames_ctx);
av_freep(&ifp->linklabel);
av_freep(&ifp->opts.name);
+ av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
av_freep(&ifilter->name);
av_freep(&fg->inputs[j]);
}
@@ -1026,6 +1033,7 @@ void fg_free(FilterGraph **pfg)
av_freep(&ofilter->apad);
av_freep(&ofp->name);
av_channel_layout_uninit(&ofp->ch_layout);
+ av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data);
av_freep(&fg->outputs[j]);
}
av_freep(&fg->outputs);
@@ -1725,6 +1733,9 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
par->color_space = ifp->color_space;
par->color_range = ifp->color_range;
par->hw_frames_ctx = ifp->hw_frames_ctx;
+ par->side_data = ifp->side_data;
+ par->nb_side_data = ifp->nb_side_data;
+
ret = av_buffersrc_parameters_set(ifp->filter, par);
if (ret < 0)
goto fail;
@@ -1813,6 +1824,9 @@ static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
AVBPrint args;
char name[255];
int ret, pad_idx = 0;
+ AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
+ if (!par)
+ return AVERROR(ENOMEM);
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
@@ -1830,7 +1844,13 @@ static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
if ((ret = avfilter_graph_create_filter(&ifp->filter, abuffer_filt,
name, args.str, NULL,
graph)) < 0)
- return ret;
+ goto fail;
+ par->side_data = ifp->side_data;
+ par->nb_side_data = ifp->nb_side_data;
+ ret = av_buffersrc_parameters_set(ifp->filter, par);
+ if (ret < 0)
+ goto fail;
+ av_freep(&par);
last_filter = ifp->filter;
snprintf(name, sizeof(name), "trim for input stream %s", ifp->opts.name);
@@ -1843,6 +1863,10 @@ static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
return ret;
return 0;
+fail:
+ av_freep(&par);
+
+ return ret;
}
static int configure_input_filter(FilterGraph *fg, AVFilterGraph *graph,
@@ -1998,6 +2022,10 @@ static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout);
if (ret < 0)
goto fail;
+ av_frame_side_data_free(&ofp->side_data, &ofp->nb_side_data);
+ ret = av_buffersink_get_side_data(sink, &ofp->side_data, &ofp->nb_side_data);
+ if (ret < 0)
+ goto fail;
}
for (int i = 0; i < fg->nb_inputs; i++) {
@@ -2066,6 +2094,20 @@ static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *fr
if (ret < 0)
return ret;
+ av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
+ for (int i = 0; i < frame->nb_side_data; i++) {
+ const AVSideDataDescriptor *desc = av_frame_side_data_desc(frame->side_data[i]->type);
+
+ if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL))
+ continue;
+
+ ret = av_frame_side_data_clone(&ifp->side_data,
+ &ifp->nb_side_data,
+ frame->side_data[i], 0);
+ if (ret < 0)
+ return ret;
+ }
+
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
if (sd)
memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
@@ -2396,6 +2438,11 @@ static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt)
if (ret < 0)
return ret;
}
+ av_frame_side_data_free(&frame->side_data, &frame->nb_side_data);
+ ret = clone_side_data(&frame->side_data, &frame->nb_side_data,
+ ofp->side_data, ofp->nb_side_data, 0);
+ if (ret < 0)
+ return ret;
fd = frame_data(frame);
if (!fd)
@@ -2748,6 +2795,13 @@ static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
if (ret < 0)
return ret;
+ av_frame_side_data_free(&ifp->side_data, &ifp->nb_side_data);
+ ret = clone_side_data(&ifp->side_data, &ifp->nb_side_data,
+ ifp->opts.fallback->side_data,
+ ifp->opts.fallback->nb_side_data, 0);
+ if (ret < 0)
+ return ret;
+
if (ifilter_has_all_input_formats(ifilter->graph)) {
ret = configure_filtergraph(ifilter->graph, fgt);
if (ret < 0) {
diff --git a/fftools/ffmpeg_utils.h b/fftools/ffmpeg_utils.h
index 8ed6f81b28..9ca3afffa0 100644
--- a/fftools/ffmpeg_utils.h
+++ b/fftools/ffmpeg_utils.h
@@ -44,4 +44,20 @@ static inline int err_merge(int err0, int err1)
return (err0 < 0) ? err0 : FFMIN(err1, 0);
}
+/**
+ * Wrapper calling av_frame_side_data_clone() in a loop for all source entries.
+ * It does not clear dst beforehand. */
+static inline int clone_side_data(AVFrameSideData ***dst, int *nb_dst,
+ AVFrameSideData * const *src, int nb_src,
+ unsigned int flags)
+{
+ for (int i = 0; i < nb_src; i++) {
+ int ret = av_frame_side_data_clone(dst, nb_dst, src[i], flags);
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+
#endif // FFTOOLS_FFMPEG_UTILS_H
--
2.47.1
More information about the ffmpeg-devel
mailing list