fftools/ffmpeg_filter: stop using avfilter_graph_create_filter() incorrectly

This function creates AND initializes a filter, so setting any filter
options after it is wrong. It happens to work when the filter's init
function does not touch the options in question, but is forbidden by the
API and is not guaranteed to remain functional.

Instead, use avfilter_graph_alloc_filter(), followed by setting the
options, and avfilter_init_dict().
This commit is contained in:
Anton Khirnov 2024-09-25 11:58:57 +02:00
parent 4ffd586e34
commit 53c71777e1
1 changed files with 29 additions and 24 deletions

View File

@ -1586,14 +1586,18 @@ static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
int ret;
snprintf(name, sizeof(name), "out_%s", ofp->name);
ret = avfilter_graph_create_filter(&ofp->filter,
avfilter_get_by_name("abuffersink"),
name, NULL, NULL, graph);
if (ret < 0)
return ret;
ofp->filter = avfilter_graph_alloc_filter(graph,
avfilter_get_by_name("abuffersink"),
name);
if (!ofp->filter)
return AVERROR(ENOMEM);
if ((ret = av_opt_set_int(ofp->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
return ret;
ret = avfilter_init_dict(ofp->filter, NULL);
if (ret < 0)
return ret;
#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
AVFilterContext *filt_ctx; \
\
@ -1686,9 +1690,6 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
AVFilterContext *last_filter;
const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
const AVPixFmtDescriptor *desc;
AVRational fr = ifp->opts.framerate;
AVRational sar;
AVBPrint args;
char name[255];
int ret, pad_idx = 0;
AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
@ -1698,30 +1699,34 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
sub2video_prepare(ifp);
sar = ifp->sample_aspect_ratio;
if(!sar.den)
sar = (AVRational){0,1};
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
av_bprintf(&args,
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
"pixel_aspect=%d/%d:colorspace=%d:range=%d",
ifp->width, ifp->height, ifp->format,
ifp->time_base.num, ifp->time_base.den, sar.num, sar.den,
ifp->color_space, ifp->color_range);
if (fr.num && fr.den)
av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index,
ifp->opts.name);
if ((ret = avfilter_graph_create_filter(&ifp->filter, buffer_filt, name,
args.str, NULL, graph)) < 0)
ifp->filter = avfilter_graph_alloc_filter(graph, buffer_filt, name);
if (!ifp->filter) {
ret = AVERROR(ENOMEM);
goto fail;
par->hw_frames_ctx = ifp->hw_frames_ctx;
}
par->format = ifp->format;
par->time_base = ifp->time_base;
par->frame_rate = ifp->opts.framerate;
par->width = ifp->width;
par->height = ifp->height;
par->sample_aspect_ratio = ifp->sample_aspect_ratio.den > 0 ?
ifp->sample_aspect_ratio : (AVRational){ 0, 1 };
par->color_space = ifp->color_space;
par->color_range = ifp->color_range;
par->hw_frames_ctx = ifp->hw_frames_ctx;
ret = av_buffersrc_parameters_set(ifp->filter, par);
if (ret < 0)
goto fail;
av_freep(&par);
ret = avfilter_init_dict(ifp->filter, NULL);
if (ret < 0)
goto fail;
last_filter = ifp->filter;
desc = av_pix_fmt_desc_get(ifp->format);