History

avfilter: Replace query_formats callback with union of list and callback

If one looks at the many query_formats callbacks in existence, one will immediately recognize that there is one type of default callback for video and a slightly different default callback for audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);" for video with a filter-specific pix_fmts list. For audio, it is the same with a filter-specific sample_fmts list together with ff_set_common_all_samplerates() and ff_set_common_all_channel_counts(). This commit allows to remove the boilerplate query_formats callbacks by replacing said callback with a union consisting the old callback and pointers for pixel and sample format arrays. For the not uncommon case in which these lists only contain a single entry (besides the sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also added to the union to store them directly in the AVFilter, thereby avoiding a relocation. The state of said union will be contained in a new, dedicated AVFilter field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t in order to create a hole for this new field; this is no problem, as the maximum of all the nb_inputs is four; for nb_outputs it is only two). The state's default value coincides with the earlier default of query_formats being unset, namely that the filter accepts all formats (and also sample rates and channel counts/layouts for audio) provided that these properties agree coincide for all inputs and outputs. By using different union members for audio and video filters the type-unsafety of using the same functions for audio and video lists will furthermore be more confined to formats.c than before. When the new fields are used, they will also avoid allocations: Currently something nearly equivalent to ff_default_query_formats() is called after every successful call to a query_formats callback; yet in the common case that the newly allocated AVFilterFormats are not used at all (namely if there are no free links) these newly allocated AVFilterFormats are freed again without ever being used. Filters no longer using the callback will not exhibit this any more.

Andreas Rheinhardt committed 5 years ago

Commit b4f52011 parent aa63609360 files · +551 −441Browse files

Showing the changes to libavfilter/avfiltergraph.c (1 of 360 files). Show all

Unified · Split

libavfilter/avfiltergraph.c+2−2
@@ -349,7 +349,7 @@ static int filter_query_formats(AVFilterContext *ctx) · 348 unmodified lines
349 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :349 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
350 AVMEDIA_TYPE_VIDEO;350 AVMEDIA_TYPE_VIDEO;
351 351
352 if ((ret = ctx->filter->query_formats(ctx)) < 0) {352 if ((ret = ctx->filter->formats.query_func(ctx)) < 0) {
353 if (ret != AVERROR(EAGAIN))353 if (ret != AVERROR(EAGAIN))
354 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",354 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
355 ctx->name, av_err2str(ret));355 ctx->name, av_err2str(ret));
@@ -418,7 +418,7 @@ static int query_formats(AVFilterGraph *graph, void *log_ctx) · 62 unmodified lines
418 AVFilterContext *f = graph->filters[i];418 AVFilterContext *f = graph->filters[i];
419 if (formats_declared(f))419 if (formats_declared(f))
420 continue;420 continue;
421 if (f->filter->query_formats)421 if (f->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC)
422 ret = filter_query_formats(f);422 ret = filter_query_formats(f);
423 else423 else
424 ret = ff_default_query_formats(f);424 ret = ff_default_query_formats(f);

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