[FFmpeg-devel] [PATCH v2 02/15] avfilter: always call ff_default_query_formats

Niklas Haas ffmpeg at haasn.xyz
Wed Dec 13 15:11:59 EET 2023


From: Niklas Haas <git at haasn.dev>

Even if a query func is set. This is safe to do, because
ff_default_query_formats is documented not to touch any filter lists
that were already set by the query func.

The reason to do this is because it allows us to extend
AVFilterFormatsConfig without having to touch every filter in existence.
An alternative implementation of this commit would be to explicitly add
a `ff_default_query_formats` call at the end of every query_formats
function, but that would end up functionally equivalent to this change
while touching a whole lot more code paths for no reason.

As a bonus, eliminates some code/logic duplication from this function.
---
 libavfilter/avfiltergraph.c | 39 ++++++++++++-------------------------
 1 file changed, 12 insertions(+), 27 deletions(-)

diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 68daa93e61..625cbc022e 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -341,33 +341,21 @@ static int filter_check_formats(AVFilterContext *ctx)
 static int filter_query_formats(AVFilterContext *ctx)
 {
     int ret;
-    AVFilterFormats *formats;
-    AVFilterChannelLayouts *chlayouts;
-    enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
-                            ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
-                            AVMEDIA_TYPE_VIDEO;
-
-    if ((ret = ctx->filter->formats.query_func(ctx)) < 0) {
-        if (ret != AVERROR(EAGAIN))
-            av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
-                   ctx->name, av_err2str(ret));
-        return ret;
-    }
-    ret = filter_check_formats(ctx);
-    if (ret < 0)
-        return ret;
 
-    formats = ff_all_formats(type);
-    if ((ret = ff_set_common_formats(ctx, formats)) < 0)
-        return ret;
-    if (type == AVMEDIA_TYPE_AUDIO) {
-        if ((ret = ff_set_common_all_samplerates(ctx)) < 0)
+    if (ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
+        if ((ret = ctx->filter->formats.query_func(ctx)) < 0) {
+            if (ret != AVERROR(EAGAIN))
+                av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
+                       ctx->name, av_err2str(ret));
             return ret;
-        chlayouts = ff_all_channel_layouts();
-        if ((ret = ff_set_common_channel_layouts(ctx, chlayouts)) < 0)
+        }
+
+        ret = filter_check_formats(ctx);
+        if (ret < 0)
             return ret;
     }
-    return 0;
+
+    return ff_default_query_formats(ctx);
 }
 
 static int formats_declared(AVFilterContext *f)
@@ -416,10 +404,7 @@ static int query_formats(AVFilterGraph *graph, void *log_ctx)
         AVFilterContext *f = graph->filters[i];
         if (formats_declared(f))
             continue;
-        if (f->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC)
-            ret = filter_query_formats(f);
-        else
-            ret = ff_default_query_formats(f);
+        ret = filter_query_formats(f);
         if (ret < 0 && ret != AVERROR(EAGAIN))
             return ret;
         /* note: EAGAIN could indicate a partial success, not counted yet */
-- 
2.43.0



More information about the ffmpeg-devel mailing list