PR #26231 opened by rnissl
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26231
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26231.patch

Software links still insert `scale` with the swscale options.

A CUDA link inserts `scale_cuda` for a color-space or range mismatch, without 
those options. A chroma-only mismatch or a pixel-format change fails. 
`scale_cuda` in master still shares one color list, so the inserted filter 
cannot yet finish a YUV matrix conversion.

A Vulkan link inserts `libplacebo` with `inherit_device=1` for color space, 
range and chroma, using the device on the incoming frames. A pixel-format 
change fails. VAAPI, QSV, D3D12 and mixed hardware/software lists fail, with no 
automatic download.

On a Vulkan build, bt709 frames are converted to bt470bg full range by the 
inserted libplacebo before `mjpeg_vulkan`. A Vulkan to yuv420p link is rejected.



From 922e266e29719b911317646ab780044f7c41b76d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Reinhard=20Ni=C3=9Fl?= <[email protected]>
Date: Fri, 20 Nov 2026 09:26:57 +0100
Subject: [PATCH] avfilter: select a hardware scaler while negotiating formats
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Software links keep scale. A CUDA link uses scale_cuda for color space and 
range, and a Vulkan link uses libplacebo on the input device. Other hardware, 
and a pixel-format change on those links, fails instead of inserting a software 
scale.

Signed-off-by: Reinhard Nißl <[email protected]>
Co-authored-by: Cursor <[email protected]>
---
 libavfilter/avfiltergraph.c | 134 +++++++++++++++++++++++++++++++-----
 1 file changed, 118 insertions(+), 16 deletions(-)

diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 7b58883c651..88f99863030 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -516,6 +516,98 @@ static void print_filter_formats(void *log_ctx, int level, 
const AVFilterContext
     av_bprint_finalize(&bp, NULL);
 }
 
+/*
+ * Software links keep scale and its sws options. A CUDA link uses scale_cuda
+ * for color space and range; with no format option that filter shares chroma
+ * and cannot change the pixel format. A Vulkan link uses libplacebo and
+ * inherit_device=1, so the device is the one on the incoming frames.
+ * VAAPI, QSV, D3D12 and mixed lists have no converter here.
+ * *replace_opts tells the caller not to pass scale_sws_opts.
+ * *extra is that filter's option string, or NULL.
+ */
+static const char *pick_conversion_filter(AVFilterLink *link,
+                                         const AVFilterFormatsMerger *m,
+                                         const char **extra, int *replace_opts)
+{
+    const AVFilterFormats *lists[2];
+    enum AVPixelFormat hw = AV_PIX_FMT_NONE;
+    int software = 0;
+    int n, i;
+
+    *extra = NULL;
+    *replace_opts = 0;
+    if (!m->conversion_filter || strcmp(m->conversion_filter, "scale"))
+        return m->conversion_filter;
+
+    lists[0] = link->incfg.formats;
+    lists[1] = link->outcfg.formats;
+    for (n = 0; n < 2; n++) {
+        const AVFilterFormats *fmts = lists[n];
+        if (!fmts)
+            continue;
+        for (i = 0; i < fmts->nb_formats; i++) {
+            const AVPixFmtDescriptor *desc = 
av_pix_fmt_desc_get(fmts->formats[i]);
+            if (!desc)
+                continue;
+            if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
+                if (hw == AV_PIX_FMT_NONE)
+                    hw = fmts->formats[i];
+                else if (hw != fmts->formats[i])
+                    return NULL;
+            } else {
+                software = 1;
+            }
+        }
+    }
+
+    if (hw == AV_PIX_FMT_NONE)
+        return "scale";
+    if (software)
+        return NULL;
+
+    if (hw == AV_PIX_FMT_CUDA) {
+        if (!strcmp(m->name, "Pixel formats") ||
+            !strcmp(m->name, "Chroma locations"))
+            return NULL;
+        *replace_opts = 1;
+        return avfilter_get_by_name("scale_cuda") ? "scale_cuda" : NULL;
+    }
+    if (hw == AV_PIX_FMT_VULKAN) {
+        if (!strcmp(m->name, "Pixel formats"))
+            return NULL;
+        *replace_opts = 1;
+        *extra = "inherit_device=1";
+        return avfilter_get_by_name("libplacebo") ? "libplacebo" : NULL;
+    }
+    return NULL;
+}
+
+static int queue_conversion(AVFilterLink *link, const AVFilterFormatsMerger *m,
+                            AVFilterGraph *graph,
+                            const char **conv_filters, const char **conv_opts,
+                            unsigned *num_conv)
+{
+    int replace_opts = 0;
+    const char *extra = NULL;
+    const char *name = pick_conversion_filter(link, m, &extra, &replace_opts);
+    unsigned k;
+
+    if (!name)
+        return AVERROR(ENOSYS);
+    for (k = 0; k < *num_conv; k++) {
+        if (!strcmp(conv_filters[k], name))
+            return 0;
+    }
+    av_assert1(*num_conv < 4);
+    conv_filters[*num_conv] = name;
+    if (replace_opts)
+        conv_opts[*num_conv] = extra;
+    else if (m->conversion_opts_offset)
+        conv_opts[*num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, 
*graph);
+    (*num_conv)++;
+    return 0;
+}
+
 /**
  * Perform one round of query_formats() and merging formats lists on the
  * filter graph.
@@ -572,19 +664,18 @@ retry:
                 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
                 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
                 if (a && b && a != b && !m->can_merge(a, b)) {
-                    for (k = 0; k < num_conv; k++) {
-                        if (!strcmp(conv_filters[k], m->conversion_filter))
-                            break;
-                    }
-                    if (k == num_conv) {
-                        av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
-                        conv_filters[num_conv] = m->conversion_filter;
-                        if (m->conversion_opts_offset)
-                            conv_opts[num_conv] = FF_FIELD_AT(char *, 
m->conversion_opts_offset, *graph);
-                        num_conv++;
-                    }
                     av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
                     mergers[num_mergers++] = m;
+                    ret = queue_conversion(link, m, graph, conv_filters, 
conv_opts,
+                                           &num_conv);
+                    if (ret < 0) {
+                        av_log(log_ctx, AV_LOG_ERROR,
+                               "Impossible to convert between the formats 
supported by the filter "
+                               "'%s' and the filter '%s'\n",
+                               link->src->name, link->dst->name);
+                        print_link_formats(log_ctx, AV_LOG_ERROR, link, 
mergers, num_mergers);
+                        return ret;
+                    }
                 }
             }
             for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
@@ -601,11 +692,18 @@ retry:
                     if (ret < 0)
                         return ret;
                     if (!ret) {
+                        av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
                         mergers[num_mergers++] = m;
-                        conv_filters[num_conv] = m->conversion_filter;
-                        if (m->conversion_opts_offset)
-                            conv_opts[num_conv] = FF_FIELD_AT(char *, 
m->conversion_opts_offset, *graph);
-                        num_conv++;
+                        ret = queue_conversion(link, m, graph, conv_filters, 
conv_opts,
+                                               &num_conv);
+                        if (ret < 0) {
+                            av_log(log_ctx, AV_LOG_ERROR,
+                                   "Impossible to convert between the formats 
supported by the filter "
+                                   "'%s' and the filter '%s'\n",
+                                   link->src->name, link->dst->name);
+                            print_link_formats(log_ctx, AV_LOG_ERROR, link, 
mergers, num_mergers);
+                            return ret;
+                        }
                     }
                 }
             }
@@ -690,7 +788,11 @@ retry:
 
                 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
                     const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
-                    if (strcmp(m->conversion_filter, conv_filters[k]))
+                    int replace_opts = 0;
+                    const char *extra = NULL;
+                    const char *name = pick_conversion_filter(link, m, &extra,
+                                                              &replace_opts);
+                    if (!name || strcmp(name, conv_filters[k]))
                         continue;
                     if ((ret = MERGE(m,  inlink)) <= 0 ||
                         (ret = MERGE(m, outlink)) <= 0) {
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to