PR #25861 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25861
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25861.patch

Note: This depends on !25846, those commits have been included here.

This pulls in some more commits from the stalled !25424


>From d4a6ca45527011c7a55c541eb6f5659fdd7ff2ea Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 03:12:45 +0100
Subject: [PATCH 01/19] swscale/filters: exclude integer boundaries from
 start/stop

ceil(pos) gives pos when pos is exactly aligned with the filter edge,
where the weight is 0 (or SWS_MAX_REDUCE_CUTOFF). Using floor(pos) + 1
excludes this case instead. Ditto for the upper bound.

Mainly useful for the following commit.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index ddc4db87703..3f8d8d1f862 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -84,8 +84,8 @@ static void compute_row(SwsFilterWeights *f, const 
SwsFilterFunction *fun,
     }
 
     /* First and last pixels that are actually within the filter envelope */
-    const int start   = ceil (src_pos - radius);
-    const int stop    = floor(src_pos + radius);
+    const int start   = floor(src_pos - radius) + 1;
+    const int stop    = ceil (src_pos + radius) - 1;
     const int clamped = av_clip(start, 0, f->src_size - f->filter_size);
     *pos = clamped;
 
-- 
2.54.0


>From 46f85d4281924f56f2551b1492234da22403eae1 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 03:42:53 +0100
Subject: [PATCH 02/19] swscale/filters: reject invalid sizes explicitly

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 3f8d8d1f862..e5c415fa5a5 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -200,7 +200,7 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
                            SwsFilterWeights **out)
 {
     SwsScaler scaler = params->scaler;
-    if (scaler >= SWS_SCALE_NB)
+    if (scaler >= SWS_SCALE_NB || params->src_size <= 0 || params->dst_size <= 
0)
         return AVERROR(EINVAL);
 
     if (scaler == SWS_SCALE_AUTO)
-- 
2.54.0


>From 8d4ab47657def3c9699c9f663fad5bfe88a53d47 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 02:19:25 +0100
Subject: [PATCH 03/19] swscale/filters: move src_pos calculation to helper

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index e5c415fa5a5..8a5cf53cad7 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -60,6 +60,20 @@ static double scaler_sample(const SwsFilterFunction *f, 
double x)
     return w;
 }
 
+/**
+ * Explanation of the 0.5 offsets: Normally, pixel samples are assumed
+ * to be representative of the center of their containing area; e.g. for
+ * a 2x2 image, the samples are located at {0.5, 1.5}^2. However, with
+ * integer indexing, we round sample positions down (0-based indexing).
+ * So the (0, 0) sample is actually located at (0.5, 0.5) and represents
+ * the entire square from (0,0) to (1,1). When normalizing between different
+ * image sizes, we therefore need to add/subtract off these 0.5 offsets.
+ */
+static double get_src_pos(int dst_pos, double ratio_inv, double offset)
+{
+    return (dst_pos + 0.5) * ratio_inv - 0.5 + offset;
+}
+
 static void compute_row(SwsFilterWeights *f, const SwsFilterFunction *fun,
                         double radius, double ratio_inv, double stretch_inv,
                         int dst_pos, double *tmp)
@@ -67,16 +81,7 @@ static void compute_row(SwsFilterWeights *f, const 
SwsFilterFunction *fun,
     int *out = &f->weights[dst_pos * f->filter_size];
     int *pos = &f->offsets[dst_pos];
 
-    /**
-     * Explanation of the 0.5 offsets: Normally, pixel samples are assumed
-     * to be representative of the center of their containing area; e.g. for
-     * a 2x2 image, the samples are located at {0.5, 1.5}^2. However, with
-     * integer indexing, we round sample positions down (0-based indexing).
-     * So the (0, 0) sample is actually located at (0.5, 0.5) and represents
-     * the entire square from (0,0) to (1,1). When normalizing between 
different
-     * image sizes, we therefore need to add/subtract off these 0.5 offsets.
-     */
-    const double src_pos = (dst_pos + 0.5) * ratio_inv - 0.5 + f->offset;
+    const double src_pos = get_src_pos(dst_pos, ratio_inv, f->offset);
     if (f->filter_size == 1) {
         *pos = fmin(fmax(round(src_pos), 0.0), f->src_size - 1);
         *out = SWS_FILTER_SCALE;
-- 
2.54.0


>From 152ade9f18f060d4d989148ecd3750644bfa8ccf Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 03:34:44 +0100
Subject: [PATCH 04/19] swscale/filters: move up caculation of
 ratio/stretch_inv

And avoid a double reciprocal for stretch_inv.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 8a5cf53cad7..006cad5f831 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -216,10 +216,12 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
         virtual_size = params->dst_size;
 
     const double ratio = virtual_size / params->src_size;
-    double stretch = 1.0;
+    const double ratio_inv = 1.0 / ratio;
+    double stretch = 1.0, stretch_inv = 1.0;
     if (ratio < 1.0 && scaler != SWS_SCALE_POINT) {
         /* Widen filter for downscaling (anti-aliasing) */
         stretch = 1.0 / ratio;
+        stretch_inv = ratio;
     }
 
     if (scaler == SWS_SCALE_AREA) {
@@ -308,7 +310,6 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
         return AVERROR(ENOMEM);
     }
 
-    const double ratio_inv = 1.0 / ratio, stretch_inv = 1.0 / stretch;
     for (int i = 0; i < params->dst_size; i++)
         compute_row(filter, &fun, radius, ratio_inv, stretch_inv, i, tmp);
     av_free(tmp);
-- 
2.54.0


>From ba8e76d920b2de3dc1c1c1c46dd29317fb61be47 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 02:54:37 +0100
Subject: [PATCH 05/19] swscale/filters: calculate true filter size

Instead of estimating based on a worst-case scenario, calculate the
number of taps each row needs (which depends on the phase), and take
the maximum of these values as the overall filter size.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 006cad5f831..55441169613 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -267,11 +267,22 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
     }
     radius *= stretch;
 
-    int filter_size = ceil(radius * 2.0);
-    filter_size = FFMIN(filter_size, params->src_size);
-    av_assert0(filter_size >= 1);
-    if (filter_size > SWS_FILTER_SIZE_MAX)
-        return AVERROR(ENOTSUP);
+    /* Compute filter size by checking the number of taps in each row */
+    int filter_size = 0;
+    for (int i = 0; i < params->dst_size; i++) {
+        /* Matches the indexing in compute_row() */
+        const double src_pos = get_src_pos(i, ratio_inv, params->offset);
+        const int start = floor(src_pos - radius) + 1;
+        const int stop  = ceil (src_pos + radius) - 1;
+        const int taps  = av_clip(stop - start + 1, 1, params->src_size);
+        if (taps > SWS_FILTER_SIZE_MAX) {
+            return AVERROR(ENOTSUP);
+        } else if (taps > filter_size) {
+            filter_size = taps;
+            if (taps == params->src_size)
+                break; /* can't get any bigger */
+        }
+    }
 
     SwsFilterWeights *filter;
     filter = av_refstruct_alloc_ext(sizeof(*filter), 0, NULL, sws_filter_free);
-- 
2.54.0


>From b6d79f54dd0978bbf7212646ac9220bbda7cbad9 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:24:53 +0100
Subject: [PATCH 06/19] swscale/filters: fix SWS_SCALE_AREA kernel

This behaved like an anti-aliased point filter when downscaling, which
is not the same as a true area filter. That should behave like a trapezoid,
not a box, approaching a bilinear kernel (triangle filter) as the ratio
approaches 1.0.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 55441169613..e1a3cfb91e0 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -224,16 +224,6 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
         stretch_inv = ratio;
     }
 
-    if (scaler == SWS_SCALE_AREA) {
-        /**
-         * SWS_SCALE_AREA is a pseudo-filter that is equivalent to bilinear
-         * filtering for upscaling (since bilinear just evenly mixes samples
-         * according to the relative distance), and equivalent to 
(anti-aliased)
-         * point sampling for downscaling.
-         */
-        scaler = ratio >= 1.0 ? SWS_SCALE_BILINEAR : SWS_SCALE_POINT;
-    }
-
     SwsFilterFunction fun = filter_functions[scaler];
     if (!fun.kernel)
         return AVERROR(EINVAL);
@@ -260,6 +250,12 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
     case SWS_SCALE_BILINEAR:
         radius = 1.0 - SWS_MAX_REDUCE_CUTOFF;
         break;
+    case SWS_SCALE_AREA:
+        /* Above ratio 1.0, this behaves like bilinear */
+        fun.params[0] = fmin(ratio, 1.0);
+        fun.radius = 0.5 + 0.5 * fun.params[0];
+        radius = fun.radius - SWS_MAX_REDUCE_CUTOFF * fun.params[0];
+        break;
     default:
         /* Numerically estimate radius of nontrivial or parametric kernels */
         radius = est_filter_radius(&fun);
@@ -353,6 +349,19 @@ av_unused static double box(double x, const double *params)
     return 1.0;
 }
 
+/**
+ * Overlap between a destination pixel (unit box) and a source pixel of width
+ * params[0] <= 1, i.e. the convolution of two box filters (a trapezoid).
+ */
+static double area(double x, const double *params)
+{
+    const double w = params[0];
+    const double edge = 0.5 - 0.5 * w;
+    if (x <= edge)
+        return 1.0;
+    return fmax(1.0 - (x - edge) / w, 0.0);
+}
+
 av_unused static double triangle(double x, const double *params)
 {
     return 1.0 - x;
@@ -496,5 +505,5 @@ static const SwsFilterFunction 
filter_functions[SWS_SCALE_NB] = {
     [SWS_SCALE_SINC]        = { "sinc",         RADIUS_MAX, sinc },
     [SWS_SCALE_LANCZOS]     = { "lanczos",      -1.0, sinc, sinc, .params = { 
3.0 } },
     [SWS_SCALE_SPLINE]      = { "spline",       RADIUS_MAX, spline },
-    /* SWS_SCALE_AREA is a pseudo-filter, see code above */
+    [SWS_SCALE_AREA]        = { "area",         1.0, area },
 };
-- 
2.54.0


>From c8e4a99fdcf5f36d8f0e72b544444f51399926eb Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:31:06 +0100
Subject: [PATCH 07/19] swscale/filters: add SwsFilterWeights.scaler

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 1 +
 libswscale/filters.h | 1 +
 2 files changed, 2 insertions(+)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index e1a3cfb91e0..396543d3057 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -284,6 +284,7 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     filter = av_refstruct_alloc_ext(sizeof(*filter), 0, NULL, sws_filter_free);
     if (!filter)
         return AVERROR(ENOMEM);
+    filter->scaler = scaler;
     memcpy(filter->name, fun.name, sizeof(filter->name));
     filter->src_size = params->src_size;
     filter->dst_size = params->dst_size;
diff --git a/libswscale/filters.h b/libswscale/filters.h
index b38cd34777b..16ad2b5eea9 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -108,6 +108,7 @@ typedef struct SwsFilterWeights {
     /**
      * Copy of the parameters used to generate this filter, for reference.
      */
+    SwsScaler scaler; /* SWS_SCALE_AUTO is resolved */
     int src_size;
     int dst_size;
     double virtual_size;
-- 
2.54.0


>From a297f3a1fa246254faa0bd3fdfe460dc188973e9 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:11:01 +0100
Subject: [PATCH 08/19] swscale/filters: add ff_sws_scaler_name()

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 18 ++++++++++++++++++
 libswscale/filters.h |  5 +++++
 2 files changed, 23 insertions(+)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 396543d3057..21343bd4a6c 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -508,3 +508,21 @@ static const SwsFilterFunction 
filter_functions[SWS_SCALE_NB] = {
     [SWS_SCALE_SPLINE]      = { "spline",       RADIUS_MAX, spline },
     [SWS_SCALE_AREA]        = { "area",         1.0, area },
 };
+
+const char *ff_sws_scaler_name(SwsScaler scaler)
+{
+    switch (scaler) {
+    case SWS_SCALE_AUTO:     return "auto";
+    case SWS_SCALE_BILINEAR: return "bilinear";
+    case SWS_SCALE_BICUBIC:  return "bicubic";
+    case SWS_SCALE_POINT:    return "point";
+    case SWS_SCALE_AREA:     return "area";
+    case SWS_SCALE_GAUSSIAN: return "gaussian";
+    case SWS_SCALE_SINC:     return "sinc";
+    case SWS_SCALE_LANCZOS:  return "lanczos";
+    case SWS_SCALE_SPLINE:   return "spline";
+    case SWS_SCALE_NB:       break;
+    }
+
+    return NULL;
+}
diff --git a/libswscale/filters.h b/libswscale/filters.h
index 16ad2b5eea9..95135567ec0 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -42,6 +42,11 @@ enum {
     SWS_FILTER_PAD_ROWS = 32, /* extra zero rows past `weights` */
 };
 
+/**
+ * Return the friendly name of a given scaler enum, or NULL if invalid.
+ */
+const char *ff_sws_scaler_name(SwsScaler scaler);
+
 /* Parameters for filter generation. */
 typedef struct SwsFilterParams {
     /**
-- 
2.54.0


>From e41ae6deab2532c8b3acf015bf1d17a693493acb Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:33:45 +0100
Subject: [PATCH 09/19] swscale/ops: use ff_sws_scaler_name() instead of
 SwsFilterWeights.name

The latter field is about to be removed.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libswscale/ops.c b/libswscale/ops.c
index 07dd1aba482..26c9b22305a 100644
--- a/libswscale/ops.c
+++ b/libswscale/ops.c
@@ -948,7 +948,7 @@ void ff_sws_op_desc(AVBPrint *bp, const SwsOp *op)
             break;
         const SwsFilterWeights *kernel = op->rw.filter.kernel;
         av_bprintf(bp, " + %d tap %s filter (%c)",
-                   kernel->filter_size, kernel->name,
+                   kernel->filter_size, ff_sws_scaler_name(kernel->scaler),
                    op->rw.filter.op == SWS_OP_FILTER_H ? 'H' : 'V');
         break;
     case SWS_OP_LSHIFT:
@@ -1013,7 +1013,7 @@ void ff_sws_op_desc(AVBPrint *bp, const SwsOp *op)
         const SwsFilterWeights *kernel = op->filter.kernel;
         av_bprintf(bp, "%-20s: %d -> %d %s (%d taps)", name,
                    kernel->src_size, kernel->dst_size,
-                   kernel->name, kernel->filter_size);
+                   ff_sws_scaler_name(kernel->scaler), kernel->filter_size);
         break;
     }
     case SWS_OP_LUT_3D:
-- 
2.54.0


>From d2b6b4ef9c0c4e4f309d6950bf0f2de8acb17779 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 02:05:54 +0100
Subject: [PATCH 10/19] tests/checkasm/sw_ops: use ff_sws_scaler_name()
 directly

Signed-off-by: Niklas Haas <[email protected]>
---
 tests/checkasm/sw_ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/checkasm/sw_ops.c b/tests/checkasm/sw_ops.c
index 298618029f5..90ce160f555 100644
--- a/tests/checkasm/sw_ops.c
+++ b/tests/checkasm/sw_ops.c
@@ -532,7 +532,7 @@ static void check_filter(const char *name, SwsUOp *uop)
 
             char desc[256];
             snprintf(desc, sizeof(desc), "%s_%s_%d", name,
-                     uop->data.kernel->name, par.src_size);
+                     ff_sws_scaler_name(par.scaler), par.src_size);
 
             check_read(desc, uop);
             av_refstruct_unref(&uop->data.kernel);
-- 
2.54.0


>From a80b6fd92508e45f4621f7bf469040352f749f98 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:35:36 +0100
Subject: [PATCH 11/19] avfilter/vf_scale_cuda: use ff_sws_scaler_name()

See previous commit.

Signed-off-by: Niklas Haas <[email protected]>
---
 libavfilter/vf_scale_cuda.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavfilter/vf_scale_cuda.c b/libavfilter/vf_scale_cuda.c
index 55839acfc49..efa6c3f059e 100644
--- a/libavfilter/vf_scale_cuda.c
+++ b/libavfilter/vf_scale_cuda.c
@@ -1059,7 +1059,7 @@ static int cudascale_filter_init(AVFilterContext *ctx,
         goto fail;
 
     av_log(ctx, AV_LOG_VERBOSE, "  using %d tap '%s' filter: %d -> %d\n",
-           f->filter_size, weights->name, src_size, dst_size);
+           f->filter_size, ff_sws_scaler_name(weights->scaler), src_size, 
dst_size);
 
     ret = 0;
 
-- 
2.54.0


>From 086c9b0377404a347d14e029095b246e3012161d Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sat, 7 Nov 2026 21:36:50 +0100
Subject: [PATCH 12/19] swscale/filters: drop SwsFilterWeights.name field

Redundant with the more flexible ff_sws_scaler_name().
Also drops the now-unused internal name[] field.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 22 ++++++++++------------
 libswscale/filters.h |  1 -
 2 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 21343bd4a6c..54c0e45f8af 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -39,7 +39,6 @@
 typedef double (*SwsFilterKernel)(double x, const double *params);
 
 typedef struct SwsFilterFunction {
-    char name[16];
     double radius; /* negative means resizable */
     SwsFilterKernel kernel;
     SwsFilterKernel window; /* optional */
@@ -235,7 +234,7 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
 
     if (!validate_params(&fun, scaler)) {
         av_log(log, AV_LOG_ERROR, "Invalid parameters for scaler %s: {%f, 
%f}\n",
-               fun.name, fun.params[0], fun.params[1]);
+               ff_sws_scaler_name(scaler), fun.params[0], fun.params[1]);
         return AVERROR(EINVAL);
     }
 
@@ -285,7 +284,6 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     if (!filter)
         return AVERROR(ENOMEM);
     filter->scaler = scaler;
-    memcpy(filter->name, fun.name, sizeof(filter->name));
     filter->src_size = params->src_size;
     filter->dst_size = params->dst_size;
     filter->virtual_size = virtual_size;
@@ -295,7 +293,7 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
         filter->sum_positive = SWS_FILTER_SCALE;
 
     av_log(log, AV_LOG_DEBUG, "Generating %s filter with %d taps (radius = 
%f)\n",
-           filter->name, filter->filter_size, radius);
+           ff_sws_scaler_name(scaler), filter->filter_size, radius);
 
     filter->num_weights = (size_t) params->dst_size * filter->filter_size;
     const size_t padded = filter->num_weights +
@@ -499,14 +497,14 @@ av_unused static double spline(double x, const double 
*params)
 }
 
 static const SwsFilterFunction filter_functions[SWS_SCALE_NB] = {
-    [SWS_SCALE_BILINEAR]    = { "bilinear",     1.0, triangle },
-    [SWS_SCALE_BICUBIC]     = { "bicubic",      2.0, cubic, .params = { 0.0, 
0.6 } },
-    [SWS_SCALE_POINT]       = { "point",        0.5, box },
-    [SWS_SCALE_GAUSSIAN]    = { "gaussian",     4.0, gaussian, .params = { 3.0 
} },
-    [SWS_SCALE_SINC]        = { "sinc",         RADIUS_MAX, sinc },
-    [SWS_SCALE_LANCZOS]     = { "lanczos",      -1.0, sinc, sinc, .params = { 
3.0 } },
-    [SWS_SCALE_SPLINE]      = { "spline",       RADIUS_MAX, spline },
-    [SWS_SCALE_AREA]        = { "area",         1.0, area },
+    [SWS_SCALE_BILINEAR]    = { 1.0, triangle },
+    [SWS_SCALE_BICUBIC]     = { 2.0, cubic, .params = { 0.0, 0.6 } },
+    [SWS_SCALE_POINT]       = { 0.5, box },
+    [SWS_SCALE_GAUSSIAN]    = { 4.0, gaussian, .params = { 3.0 } },
+    [SWS_SCALE_SINC]        = { RADIUS_MAX, sinc },
+    [SWS_SCALE_LANCZOS]     = { -1.0, sinc, sinc, .params = { 3.0 } },
+    [SWS_SCALE_SPLINE]      = { RADIUS_MAX, spline },
+    [SWS_SCALE_AREA]        = { 1.0, area },
 };
 
 const char *ff_sws_scaler_name(SwsScaler scaler)
diff --git a/libswscale/filters.h b/libswscale/filters.h
index 95135567ec0..dcadf4b35fb 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -123,7 +123,6 @@ typedef struct SwsFilterWeights {
      * Extra metadata about the filter, used to inform the optimizer / range
      * tracker about the filter's behavior.
      */
-    char name[16];    /* name of the configured filter kernel */
     int sum_positive; /* (maximum) sum of all positive weights */
     int sum_negative; /* (minimum) sum of all negative weights */
 } SwsFilterWeights;
-- 
2.54.0


>From 58bf10c5c1943a91d741486be94d6c529590ba87 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 13:08:00 +0100
Subject: [PATCH 13/19] swscale/filters: add more metadata to SwsFilterWeights

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 3 +++
 libswscale/filters.h | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 54c0e45f8af..7d29b98f06e 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -289,6 +289,9 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     filter->virtual_size = virtual_size;
     filter->offset = params->offset;
     filter->filter_size = filter_size;
+    filter->ratio = ratio;
+    filter->radius = radius;
+    filter->stretch = stretch;
     if (filter->filter_size == 1)
         filter->sum_positive = SWS_FILTER_SCALE;
 
diff --git a/libswscale/filters.h b/libswscale/filters.h
index dcadf4b35fb..5195a4139bb 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -123,6 +123,9 @@ typedef struct SwsFilterWeights {
      * Extra metadata about the filter, used to inform the optimizer / range
      * tracker about the filter's behavior.
      */
+    double ratio;     /* effective scaling ratio */
+    double radius;    /* effective (resolved) filter radius */
+    double stretch;   /* filter stretch factor (for anti-aliasing) */
     int sum_positive; /* (maximum) sum of all positive weights */
     int sum_negative; /* (minimum) sum of all negative weights */
 } SwsFilterWeights;
-- 
2.54.0


>From 30f27aaf8cf1d98afeac679996340d706f11e36a Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 13:30:04 +0100
Subject: [PATCH 14/19] swscale/filters: add resolved filter params to
 SwsFilterWeights

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 2 ++
 libswscale/filters.h | 6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 7d29b98f06e..658f540884f 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -284,6 +284,8 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     if (!filter)
         return AVERROR(ENOMEM);
     filter->scaler = scaler;
+    for (int i = 0; i < SWS_NUM_SCALER_PARAMS; i++)
+        filter->scaler_params[i] = fun.params[i];
     filter->src_size = params->src_size;
     filter->dst_size = params->dst_size;
     filter->virtual_size = virtual_size;
diff --git a/libswscale/filters.h b/libswscale/filters.h
index 5195a4139bb..e792b7969b1 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -111,9 +111,11 @@ typedef struct SwsFilterWeights {
     int *offsets; /* refstruct */
 
     /**
-     * Copy of the parameters used to generate this filter, for reference.
+     * The resolved parameters used to generate this filter, for reference.
+     * SWS_SCALE_AUTO, SWS_PARAM_DEFAULT and virtual_size are fully resolved.
      */
-    SwsScaler scaler; /* SWS_SCALE_AUTO is resolved */
+    SwsScaler scaler;
+    double scaler_params[SWS_NUM_SCALER_PARAMS];
     int src_size;
     int dst_size;
     double virtual_size;
-- 
2.54.0


>From 745f3b98bc8fb23f379e5f1475361e8a60de8322 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Tue, 1 Sep 2026 18:24:01 +0200
Subject: [PATCH 15/19] swscale/filters: report overshoot instead of sums

These are strictly redundant, because the row sum is already enforced as
SWS_FILTER_SCALE = sum_positive + sum_negative. So the row with the highest
overshoot in one direction has the highest overshoot in the other direction
as well.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 9 +++------
 libswscale/filters.h | 3 +--
 libswscale/ops.c     | 5 +++--
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 658f540884f..7cba6b03a18 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -142,10 +142,9 @@ static void compute_row(SwsFilterWeights *f, const 
SwsFilterFunction *fun,
             sum_neg += out[i];
     }
 
-    if (sum_pos > f->sum_positive)
-        f->sum_positive = sum_pos;
-    if (sum_neg < f->sum_negative)
-        f->sum_negative = sum_neg;
+    const int overshoot = sum_pos - SWS_FILTER_SCALE;
+    if (overshoot > f->overshoot)
+        f->overshoot = overshoot;
 }
 
 static void sws_filter_free(AVRefStructOpaque opaque, void *obj)
@@ -294,8 +293,6 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     filter->ratio = ratio;
     filter->radius = radius;
     filter->stretch = stretch;
-    if (filter->filter_size == 1)
-        filter->sum_positive = SWS_FILTER_SCALE;
 
     av_log(log, AV_LOG_DEBUG, "Generating %s filter with %d taps (radius = 
%f)\n",
            ff_sws_scaler_name(scaler), filter->filter_size, radius);
diff --git a/libswscale/filters.h b/libswscale/filters.h
index e792b7969b1..7da4270ec82 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -128,8 +128,7 @@ typedef struct SwsFilterWeights {
     double ratio;     /* effective scaling ratio */
     double radius;    /* effective (resolved) filter radius */
     double stretch;   /* filter stretch factor (for anti-aliasing) */
-    int sum_positive; /* (maximum) sum of all positive weights */
-    int sum_negative; /* (minimum) sum of all negative weights */
+    int overshoot;    /* maximum filter overshoot (relative to 
SWS_FILTER_SCALE) */
 } SwsFilterWeights;
 
 /**
diff --git a/libswscale/ops.c b/libswscale/ops.c
index 26c9b22305a..4a5f94337a8 100644
--- a/libswscale/ops.c
+++ b/libswscale/ops.c
@@ -329,8 +329,9 @@ static SwsCompFlags merge_comp_flags(SwsCompFlags a, 
SwsCompFlags b)
 
 static void apply_filter_weights(SwsOpPad *pad, const SwsFilterWeights 
*weights)
 {
-    const AVRational64 posw = { weights->sum_positive, SWS_FILTER_SCALE };
-    const AVRational64 negw = { weights->sum_negative, SWS_FILTER_SCALE };
+    const int one = SWS_FILTER_SCALE, overshoot = weights->overshoot;
+    const AVRational64 posw = { one + overshoot, one };
+    const AVRational64 negw = { -overshoot, one };
 
     /* Only point sampling preserves exactness */
     if (weights->filter_size != 1)
-- 
2.54.0


>From f3ca0237fef8f798d2ec2d3c0dfa304c382d5bc7 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 13:54:04 +0100
Subject: [PATCH 16/19] swscale/filters: estimate filter overshoot with upper
 bound

This allows us to determine the filter overshoot without actually
generating the filter. This is a weak bound, as the true overshoot
can be somewhere between 0 and 1, but the extra range doesn't actually
affect anything in the optimizer (as this commit proves).

Needed for the following commit.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/filters.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/libswscale/filters.c b/libswscale/filters.c
index 7cba6b03a18..48c48929529 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -141,10 +141,6 @@ static void compute_row(SwsFilterWeights *f, const 
SwsFilterFunction *fun,
         else
             sum_neg += out[i];
     }
-
-    const int overshoot = sum_pos - SWS_FILTER_SCALE;
-    if (overshoot > f->overshoot)
-        f->overshoot = overshoot;
 }
 
 static void sws_filter_free(AVRefStructOpaque opaque, void *obj)
@@ -278,6 +274,19 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
         }
     }
 
+    /* Estimate (upper bound on) the filter overshoot */
+    double overshoot = 0.0;
+    switch (scaler) {
+    case SWS_SCALE_BICUBIC:
+        overshoot = fun.params[1] ? 1.0 : 0; /* no negative lobes at C=0 */
+        break;
+    case SWS_SCALE_LANCZOS:
+    case SWS_SCALE_SPLINE:
+    case SWS_SCALE_SINC:
+        overshoot = 1.0; /* ca. 0.903 at RADIUS_MAX (sinc) */
+        break;
+    }
+
     SwsFilterWeights *filter;
     filter = av_refstruct_alloc_ext(sizeof(*filter), 0, NULL, sws_filter_free);
     if (!filter)
@@ -293,6 +302,7 @@ int ff_sws_filter_generate(void *log, const SwsFilterParams 
*params,
     filter->ratio = ratio;
     filter->radius = radius;
     filter->stretch = stretch;
+    filter->overshoot = overshoot * SWS_FILTER_SCALE;
 
     av_log(log, AV_LOG_DEBUG, "Generating %s filter with %d taps (radius = 
%f)\n",
            ff_sws_scaler_name(scaler), filter->filter_size, radius);
-- 
2.54.0


>From 9f63900b7ae2d7932bc0c80611b5ed113ed89153 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 14:08:51 +0100
Subject: [PATCH 17/19] swscale/filters: split filter allocation and generation

This will allow us to defer generating the filter weights until needed
by the actual backend.

Signed-off-by: Niklas Haas <[email protected]>
---
 libavfilter/vf_scale_cuda.c |  4 +-
 libswscale/filters.c        | 80 ++++++++++++++++++++++---------------
 libswscale/filters.h        | 20 ++++++++--
 libswscale/format.c         |  4 +-
 tests/checkasm/sw_ops.c     |  4 +-
 5 files changed, 72 insertions(+), 40 deletions(-)

diff --git a/libavfilter/vf_scale_cuda.c b/libavfilter/vf_scale_cuda.c
index efa6c3f059e..fd17f16bd2e 100644
--- a/libavfilter/vf_scale_cuda.c
+++ b/libavfilter/vf_scale_cuda.c
@@ -1021,7 +1021,9 @@ static int cudascale_filter_init(AVFilterContext *ctx,
     }
 
     SwsFilterWeights *weights = NULL;
-    int ret = ff_sws_filter_generate(ctx, &params, &weights);
+    int ret = ff_sws_filter_create(ctx, &params, &weights);
+    if (ret >= 0)
+        ret = ff_sws_filter_generate(ctx, weights);
     if (ret < 0) {
         if (ret == AVERROR(ENOTSUP)) {
             av_log(ctx, AV_LOG_ERROR, "Filter size exceeds the maximum "
diff --git a/libswscale/filters.c b/libswscale/filters.c
index 48c48929529..a27827d9797 100644
--- a/libswscale/filters.c
+++ b/libswscale/filters.c
@@ -195,8 +195,8 @@ static double est_filter_radius(const SwsFilterFunction 
*fun)
     return radius;
 }
 
-int ff_sws_filter_generate(void *log, const SwsFilterParams *params,
-                           SwsFilterWeights **out)
+int ff_sws_filter_create(void *log, const SwsFilterParams *params,
+                         SwsFilterWeights **out)
 {
     SwsScaler scaler = params->scaler;
     if (scaler >= SWS_SCALE_NB || params->src_size <= 0 || params->dst_size <= 
0)
@@ -211,11 +211,10 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
 
     const double ratio = virtual_size / params->src_size;
     const double ratio_inv = 1.0 / ratio;
-    double stretch = 1.0, stretch_inv = 1.0;
+    double stretch = 1.0;
     if (ratio < 1.0 && scaler != SWS_SCALE_POINT) {
         /* Widen filter for downscaling (anti-aliasing) */
         stretch = 1.0 / ratio;
-        stretch_inv = ratio;
     }
 
     SwsFilterFunction fun = filter_functions[scaler];
@@ -299,43 +298,58 @@ int ff_sws_filter_generate(void *log, const 
SwsFilterParams *params,
     filter->virtual_size = virtual_size;
     filter->offset = params->offset;
     filter->filter_size = filter_size;
+    filter->num_weights = (size_t) params->dst_size * filter->filter_size;
     filter->ratio = ratio;
     filter->radius = radius;
     filter->stretch = stretch;
     filter->overshoot = overshoot * SWS_FILTER_SCALE;
-
-    av_log(log, AV_LOG_DEBUG, "Generating %s filter with %d taps (radius = 
%f)\n",
-           ff_sws_scaler_name(scaler), filter->filter_size, radius);
-
-    filter->num_weights = (size_t) params->dst_size * filter->filter_size;
-    const size_t padded = filter->num_weights +
-                          (size_t) filter->filter_size * SWS_FILTER_PAD_ROWS;
-    filter->weights = ff_refstruct_calloc(padded, sizeof(*filter->weights));
-    if (!filter->weights) {
-        av_refstruct_unref(&filter);
-        return AVERROR(ENOMEM);
-    }
-
-    filter->offsets = ff_refstruct_calloc(params->dst_size, 
sizeof(*filter->offsets));
-    if (!filter->offsets) {
-        av_refstruct_unref(&filter);
-        return AVERROR(ENOMEM);
-    }
-
-    double *tmp = av_malloc(filter->filter_size * sizeof(*tmp));
-    if (!tmp) {
-        av_refstruct_unref(&filter);
-        return AVERROR(ENOMEM);
-    }
-
-    for (int i = 0; i < params->dst_size; i++)
-        compute_row(filter, &fun, radius, ratio_inv, stretch_inv, i, tmp);
-    av_free(tmp);
-
     *out = filter;
     return 0;
 }
 
+int ff_sws_filter_generate(void *log, SwsFilterWeights *filter)
+{
+    if (filter->weights)
+        return 0;
+
+    av_log(log, AV_LOG_DEBUG, "Generating %s filter with %d taps (radius = 
%f)\n",
+           ff_sws_scaler_name(filter->scaler), filter->filter_size, 
filter->radius);
+
+    const size_t padded = filter->num_weights +
+                          (size_t) filter->filter_size * SWS_FILTER_PAD_ROWS;
+
+    filter->weights = ff_refstruct_calloc(padded, sizeof(*filter->weights));
+    if (!filter->weights)
+        goto fail;
+
+    filter->offsets = ff_refstruct_calloc(filter->dst_size, 
sizeof(*filter->offsets));
+    if (!filter->offsets)
+        goto fail;
+
+    double *tmp = av_malloc(filter->filter_size * sizeof(*tmp));
+    if (!tmp)
+        goto fail;
+
+    av_assert0(filter->scaler <= SWS_SCALE_NB);
+    SwsFilterFunction fun = filter_functions[filter->scaler];
+    memcpy(fun.params, filter->scaler_params, sizeof(fun.params));
+    fun.radius = filter->radius;
+
+    const double ratio_inv = 1.0 / filter->ratio;
+    const double stretch_inv = 1.0 / filter->stretch;
+    for (int i = 0; i < filter->dst_size; i++)
+        compute_row(filter, &fun, filter->radius, ratio_inv, stretch_inv, i, 
tmp);
+
+    av_free(tmp);
+    return 0;
+
+fail:
+    /* Don't leave partially computed state */
+    av_refstruct_unref(&filter->weights);
+    av_refstruct_unref(&filter->offsets);
+    return AVERROR(ENOMEM);
+}
+
 /*
  * Some of the filter code originally derives (via libplacebo/mpv) from Glumpy:
  * # Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
diff --git a/libswscale/filters.h b/libswscale/filters.h
index 7da4270ec82..df552eb0eb4 100644
--- a/libswscale/filters.h
+++ b/libswscale/filters.h
@@ -100,7 +100,7 @@ typedef struct SwsFilterWeights {
      * samples to convolve with the corresponding input pixels. The outer
      * coordinate is indexed by the position of the sample to reconstruct.
      */
-    int *weights; /* refstruct */
+    int *weights; /* refstruct, or NULL if not generated yet */
     size_t num_weights;
 
     /**
@@ -108,7 +108,7 @@ typedef struct SwsFilterWeights {
      * indexes into the source image, and gives the position of the first
      * source pixel to convolve with for each entry.
      */
-    int *offsets; /* refstruct */
+    int *offsets; /* refstruct, or NULL if not generated yet */
 
     /**
      * The resolved parameters used to generate this filter, for reference.
@@ -135,12 +135,24 @@ typedef struct SwsFilterWeights {
  * Generate a filter kernel for the given parameters. The generated filter is
  * allocated as a refstruct and must be unref'd by the caller.
  *
+ * The actual filter weights are not computed initially; instead this is
+ * deferred to ff_sws_filter_generate().
+ *
  * Returns 0 or a negative error code. In particular, this may return:
  * - AVERROR(ENOMEM) if memory allocation fails.
  * - AVERROR(EINVAL) if the provided parameters are invalid (e.g. out of 
range).
  * - AVERROR(ENOTSUP) if the generated filter would exceed SWS_FILTER_SIZE_MAX.
  **/
-int ff_sws_filter_generate(void *log_ctx, const SwsFilterParams *params,
-                           SwsFilterWeights **out);
+int ff_sws_filter_create(void *log_ctx, const SwsFilterParams *params,
+                         SwsFilterWeights **out);
+
+/**
+ * Fill in `weights` and `offsets` for the filter kernel. No effect if the
+ * weights were already generated.
+ *
+ * Returns 0 or a negative error code. In particular, this may return:
+ * - AVERROR(ENOMEM) if memory allocation fails.
+ **/
+int ff_sws_filter_generate(void *log_ctx, SwsFilterWeights *filter);
 
 #endif /* SWSCALE_FILTERS_H */
diff --git a/libswscale/format.c b/libswscale/format.c
index f14737f649b..950a88ac9fd 100644
--- a/libswscale/format.c
+++ b/libswscale/format.c
@@ -1720,7 +1720,9 @@ static int add_filter(SwsContext *ctx, SwsPixelType type, 
SwsOpList *ops,
         params.scaler_params[i] = ctx->scaler_params[i];
 
     SwsFilterWeights *kernel;
-    int ret = ff_sws_filter_generate(ctx, &params, &kernel);
+    int ret = ff_sws_filter_create(ctx, &params, &kernel);
+    if (ret >= 0)
+        ret = ff_sws_filter_generate(ctx, kernel);
     if (ret == AVERROR(ENOTSUP)) {
         /* Filter size exceeds limit; cascade with geometric mean size */
         int mean = sqrt((int64_t) src_size * dst_size);
diff --git a/tests/checkasm/sw_ops.c b/tests/checkasm/sw_ops.c
index 90ce160f555..1273fcfc1b2 100644
--- a/tests/checkasm/sw_ops.c
+++ b/tests/checkasm/sw_ops.c
@@ -525,7 +525,9 @@ static void check_filter(const char *name, SwsUOp *uop)
         par.scaler = scalers[s];
 
         for (par.src_size = 1; par.src_size <= par.dst_size; par.src_size <<= 
1) {
-            if (ff_sws_filter_generate(NULL, &par, &uop->data.kernel) < 0) {
+            if (ff_sws_filter_create(NULL, &par, &uop->data.kernel) < 0 ||
+                ff_sws_filter_generate(NULL, uop->data.kernel) < 0)
+            {
                 fail();
                 return;
             }
-- 
2.54.0


>From 2caa7c3e7c49afc913cecf1bb1af453a353b30bc Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 14:12:10 +0100
Subject: [PATCH 18/19] swscale/format: defer filter generation to uops stage

Does not (yet) affect the actual runtime of fate-sws-uops-macros etc,
because those tests still perform the UOP translation, but the next patch
will address that.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/format.c | 2 --
 libswscale/uops.c   | 4 ++++
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/libswscale/format.c b/libswscale/format.c
index 950a88ac9fd..d0692cf4af0 100644
--- a/libswscale/format.c
+++ b/libswscale/format.c
@@ -1721,8 +1721,6 @@ static int add_filter(SwsContext *ctx, SwsPixelType type, 
SwsOpList *ops,
 
     SwsFilterWeights *kernel;
     int ret = ff_sws_filter_create(ctx, &params, &kernel);
-    if (ret >= 0)
-        ret = ff_sws_filter_generate(ctx, kernel);
     if (ret == AVERROR(ENOTSUP)) {
         /* Filter size exceeds limit; cascade with geometric mean size */
         int mean = sqrt((int64_t) src_size * dst_size);
diff --git a/libswscale/uops.c b/libswscale/uops.c
index a03c08ae434..96eee3c056f 100644
--- a/libswscale/uops.c
+++ b/libswscale/uops.c
@@ -316,6 +316,10 @@ static int translate_rw_op(SwsContext *ctx, SwsUOpList 
*ops, SwsUOpFlags flags,
         } else {
             uop.uop = SWS_UOP_READ_PLANAR_FV;
         }
+
+        int ret = ff_sws_filter_generate(ctx, op->rw.filter.kernel);
+        if (ret < 0)
+            return ret;
     } else if (op->rw.mode == SWS_RW_PACKED && op->rw.elems > 1) {
         if (op->rw.frac)
             return AVERROR(ENOTSUP);
-- 
2.54.0


>From fb3196b6624869bb3ac0cce355700adcd3315b38 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Tue, 27 Oct 2026 12:56:21 +0100
Subject: [PATCH 19/19] swscale/uops: add SWS_UOP_FLAG_NO_DATA to skip uop data
 generation

10%-20% speedup for the FATE tests.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/tests/sws_ops.c   |  3 ++-
 libswscale/uops.c            | 18 +++++++++++++-----
 libswscale/uops.h            |  1 +
 libswscale/uops_macros_gen.c |  2 +-
 4 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/libswscale/tests/sws_ops.c b/libswscale/tests/sws_ops.c
index fa1f292ce44..2654cddd4f8 100644
--- a/libswscale/tests/sws_ops.c
+++ b/libswscale/tests/sws_ops.c
@@ -42,7 +42,8 @@ static int print_ops(SwsContext *ctx, const SwsOpList *ops, 
SwsCompiledOp *out)
     const SwsUOpFlags flags = SWS_UOP_FLAG_EXPAND_BIT
                             | SWS_UOP_FLAG_READ_PALETTE
                             | SWS_UOP_FLAG_ADD
-                            | SWS_UOP_FLAG_PUNNING;
+                            | SWS_UOP_FLAG_PUNNING
+                            | SWS_UOP_FLAG_NO_DATA;
     int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
     if (ret == AVERROR(ENOTSUP))
         goto fail;
diff --git a/libswscale/uops.c b/libswscale/uops.c
index 96eee3c056f..29e2f5faaeb 100644
--- a/libswscale/uops.c
+++ b/libswscale/uops.c
@@ -308,7 +308,6 @@ static int translate_rw_op(SwsContext *ctx, SwsUOpList 
*ops, SwsUOpFlags flags,
         if (op->op == SWS_OP_WRITE || op->rw.frac || op->rw.mode != 
SWS_RW_PLANAR)
             return AVERROR(ENOTSUP);
         uop.par.filter.type = op->rw.filter.type;
-        uop.data.kernel = av_refstruct_ref(op->rw.filter.kernel);
         if (op->rw.filter.op == SWS_OP_FILTER_H) {
             uop.uop = SWS_UOP_READ_PLANAR_FH;
         } else if (check_filter_fma(ctx, flags, op)) {
@@ -317,9 +316,14 @@ static int translate_rw_op(SwsContext *ctx, SwsUOpList 
*ops, SwsUOpFlags flags,
             uop.uop = SWS_UOP_READ_PLANAR_FV;
         }
 
-        int ret = ff_sws_filter_generate(ctx, op->rw.filter.kernel);
-        if (ret < 0)
-            return ret;
+        /* Skip generation of filter data if the caller doesn't need it,
+         * since this can be quite expensive */
+        if (!(flags & SWS_UOP_FLAG_NO_DATA)) {
+            uop.data.kernel = av_refstruct_ref(op->rw.filter.kernel);
+            int ret = ff_sws_filter_generate(ctx, op->rw.filter.kernel);
+            if (ret < 0)
+                return ret;
+        }
     } else if (op->rw.mode == SWS_RW_PACKED && op->rw.elems > 1) {
         if (op->rw.frac)
             return AVERROR(ENOTSUP);
@@ -483,6 +487,9 @@ static int translate_dither_op(SwsUOpList *ops, SwsUOpFlags 
flags, const SwsOp *
         uop.par.dither.y_offset[i] = off;
     }
 
+    if (flags & SWS_UOP_FLAG_NO_DATA)
+        return ff_sws_uop_list_append(ops, &uop);
+
     /* Allocate extra rows to allow over-reading for row offsets. Note that
      * y_offset is currently never larger than 5, so the extra space needed
      * for this over-allocation is bounded by 5 * size * sizeof(float),
@@ -635,7 +642,8 @@ static int translate_op(SwsContext *ctx, SwsUOpList *uops, 
SwsUOpFlags flags,
     case SWS_OP_LUT_3D:
         uop.uop = SWS_UOP_LUT_3D;
         uop.par.lut3d.dynamic = op->lut3d.dynamic;
-        uop.data.lut3d = av_refstruct_ref_c(op->lut3d.lut);
+        if (!(flags & SWS_UOP_FLAG_NO_DATA))
+            uop.data.lut3d = av_refstruct_ref_c(op->lut3d.lut);
         break;
     default:
         return AVERROR(ENOTSUP);
diff --git a/libswscale/uops.h b/libswscale/uops.h
index f89ee5ef52c..867f736235d 100644
--- a/libswscale/uops.h
+++ b/libswscale/uops.h
@@ -164,6 +164,7 @@ typedef enum SwsUOpFlagBits {
     SWS_UOP_FLAG_READ_PALETTE = (1 << 3), /* backend implements 
SWS_UOP_READ_PALETTE */
     SWS_UOP_FLAG_ADD          = (1 << 4), /* backend implements SWS_UOP_ADD */
     SWS_UOP_FLAG_PUNNING      = (1 << 5), /* allow type punning float data as 
int */
+    SWS_UOP_FLAG_NO_DATA      = (1 << 6), /* skip generation of op data (where 
beneficial) */
 } SwsUOpFlagBits;
 
 typedef enum SwsUOpType {
diff --git a/libswscale/uops_macros_gen.c b/libswscale/uops_macros_gen.c
index 2af7e6fd984..e9e8db7c532 100644
--- a/libswscale/uops_macros_gen.c
+++ b/libswscale/uops_macros_gen.c
@@ -200,7 +200,7 @@ static int register_flags(SwsContext *ctx, const SwsOpList 
*ops, SwsUOpFlags fla
     if (!uops)
         return AVERROR(ENOMEM);
 
-    int ret = ff_sws_ops_translate(ctx, ops, flags, uops);
+    int ret = ff_sws_ops_translate(ctx, ops, flags | SWS_UOP_FLAG_NO_DATA, 
uops);
     if (ret < 0)
         goto fail;
 
-- 
2.54.0

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

Reply via email to