History

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().

Anton Khirnov committed 2 years ago

Commit 53c71771 parent 4ffd5861 file · +29 −24Browse files

Unified · Split

fftools/ffmpeg_filter.c+29−24
@@ -1586,14 +1586,18 @@ static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph, · 1585 unmodified lines
15861586 int ret;
15871587
15881588 snprintf(name, sizeof(name), "out_%s", ofp->name);
1589− ret = avfilter_graph_create_filter(&ofp->filter,
1590− avfilter_get_by_name("abuffersink"),
1591− name, NULL, NULL, graph);
1592− if (ret < 0)
1593− return ret;
1589+ ofp->filter = avfilter_graph_alloc_filter(graph,
1590+ avfilter_get_by_name("abuffersink"),
1591+ name);
1592+ if (!ofp->filter)
1593+ return AVERROR(ENOMEM);
15941594 if ((ret = av_opt_set_int(ofp->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
15951595 return ret;
15961596
1597+ ret = avfilter_init_dict(ofp->filter, NULL);
1598+ if (ret < 0)
1599+ return ret;
1600+
15971601 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
15981602 AVFilterContext *filt_ctx; \
15991603 \
@@ -1686,9 +1690,6 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph, · 86 unmodified lines
16861690 AVFilterContext *last_filter;
16871691 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
16881692 const AVPixFmtDescriptor *desc;
1689− AVRational fr = ifp->opts.framerate;
1690− AVRational sar;
1691− AVBPrint args;
16921693 char name[255];
16931694 int ret, pad_idx = 0;
16941695 AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
@@ -1698,30 +1699,34 @@ static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph, · 3 unmodified lines
16981699 if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
16991700 sub2video_prepare(ifp);
17001701
1701− sar = ifp->sample_aspect_ratio;
1702− if(!sar.den)
1703− sar = (AVRational){0,1};
1704− av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
1705− av_bprintf(&args,
1706− "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
1707− "pixel_aspect=%d/%d:colorspace=%d:range=%d",
1708− ifp->width, ifp->height, ifp->format,
1709− ifp->time_base.num, ifp->time_base.den, sar.num, sar.den,
1710− ifp->color_space, ifp->color_range);
1711− if (fr.num && fr.den)
1712− av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
17131702 snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index,
17141703 ifp->opts.name);
17151704
1716−
1717− if ((ret = avfilter_graph_create_filter(&ifp->filter, buffer_filt, name,
1718− args.str, NULL, graph)) < 0)
1705+ ifp->filter = avfilter_graph_alloc_filter(graph, buffer_filt, name);
1706+ if (!ifp->filter) {
1707+ ret = AVERROR(ENOMEM);
17191708 goto fail;
1720− par->hw_frames_ctx = ifp->hw_frames_ctx;
1709+ }
1710+
1711+ par->format = ifp->format;
1712+ par->time_base = ifp->time_base;
1713+ par->frame_rate = ifp->opts.framerate;
1714+ par->width = ifp->width;
1715+ par->height = ifp->height;
1716+ par->sample_aspect_ratio = ifp->sample_aspect_ratio.den > 0 ?
1717+ ifp->sample_aspect_ratio : (AVRational){ 0, 1 };
1718+ par->color_space = ifp->color_space;
1719+ par->color_range = ifp->color_range;
1720+ par->hw_frames_ctx = ifp->hw_frames_ctx;
17211721 ret = av_buffersrc_parameters_set(ifp->filter, par);
17221722 if (ret < 0)
17231723 goto fail;
17241724 av_freep(&par);
1725+
1726+ ret = avfilter_init_dict(ifp->filter, NULL);
1727+ if (ret < 0)
1728+ goto fail;
1729+
17251730 last_filter = ifp->filter;
17261731
17271732 desc = av_pix_fmt_desc_get(ifp->format);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t