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

Cherry-pick of minor fixes from the subsampling branch; only removes code 
that's dead today


>From 7d9c5d1fbce238b39a7c63725a15885ac94853fd Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 15:41:29 +0100
Subject: [PATCH 1/6] swscale/ops_dispatch: set SwsOpExec dispatch size from
 actual planes

Once we support writing subsampled outputs, the overall image size may
no longer match the size of the plane we're currently writing.

>From the perspective of a compiled implementation, it makes much more sense
to use the actual plane size here, since that's what the loop bounds will
actually have to compute against.

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

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index bfc776c43ac..85cae7c791f 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -208,10 +208,9 @@ static int op_pass_setup(const SwsFrame *out, const 
SwsFrame *in,
 {
     const AVPixFmtDescriptor *indesc  = av_pix_fmt_desc_get(in->format);
     const bool float_in = indesc->flags & AV_PIX_FMT_FLAG_FLOAT;
-    const int width = out->width;
-
     SwsOpPass *p = pass->priv;
     SwsOpExec *exec = &p->exec_base;
+    const int width = exec->width;
     const SwsCompiledOp *comp = &p->comp;
 
     /* Set up main loop parameters */
@@ -584,15 +583,6 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
     p->planes_out     = rw_data_planes(write);
     p->pixel_bits_out = rw_pixel_bits(write);
     p->palette_idx    = -1;
-    p->num_blocks     = (dst->width + comp->block_size - 1) / comp->block_size;
-    if (p->num_blocks * comp->block_size < (unsigned) dst->width)
-        return AVERROR(ERANGE);
-
-    SwsOpExec *exec = &p->exec_base;
-    *exec = (SwsOpExec) {
-        .width  = dst->width,
-        .height = dst->height,
-    };
 
     const SwsOp *read = ff_sws_op_list_input(ops);
     if (read) {
@@ -613,6 +603,7 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
     for (int i = 0; i < 4; i++)
         p->idx_in[i] = p->idx_out[i] = -1;
 
+    SwsOpExec *exec = &p->exec_base;
     for (int i = 0; i < p->planes_in; i++) {
         const int idx = ops->plane_src[i];
         const int chroma = idx == 1 || idx == 2;
@@ -633,8 +624,24 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
         exec->out_sub_y[i] = sub_y;
         exec->block_size_out[i] = block_bits_out >> 3;
         p->idx_out[i] = idx;
+
+        const int plane_w = AV_CEIL_RSHIFT(dst->width,  sub_x);
+        const int plane_h = AV_CEIL_RSHIFT(dst->height, sub_y);
+        if (!exec->width || !exec->height) {
+            /* Set output size from first plane */
+            exec->width  = plane_w;
+            exec->height = plane_h;
+        } else if (plane_w != exec->width || plane_h != exec->height) {
+            /* Subsampled output planes should have been split into separate
+             * subpasses by the dispatch layer */
+            return AVERROR_BUG;
+        }
     }
 
+    p->num_blocks = (exec->width + comp->block_size - 1) / comp->block_size;
+    if (p->num_blocks * comp->block_size < (unsigned) exec->width)
+        return AVERROR(ERANGE);
+
     const SwsFilterWeights *filter = read ? read->rw.filter.kernel : NULL;
     if (read && read->rw.filter.op == SWS_OP_FILTER_V) {
         p->offsets_y = av_refstruct_ref(filter->offsets);
diff --git a/libswscale/ops_dispatch.h b/libswscale/ops_dispatch.h
index f88ba638256..efed8e60f33 100644
--- a/libswscale/ops_dispatch.h
+++ b/libswscale/ops_dispatch.h
@@ -53,7 +53,7 @@ typedef struct SwsOpExec {
     ptrdiff_t out_bump[4];
 
     /* Extra metadata, may or may not be useful */
-    int32_t width, height;      /* Overall output image dimensions */
+    int32_t width, height;      /* Current pass output dimensions */
     int32_t slice_y, slice_h;   /* Start and height of current slice */
     int32_t block_size_in[4];   /* Size of a block of pixels in bytes */
     int32_t block_size_out[4];
-- 
2.54.0


>From 37f2501204b79a9c95bac5ec83053223581f2f52 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 15:45:26 +0100
Subject: [PATCH 2/6] swscale/ops_dispatch: forward execution height to
 SwsPass.lines

This may be fewer lines than the image height, when writing to subsampled
outputs.

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops_dispatch.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index 85cae7c791f..0b5a5e0370c 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -690,8 +690,9 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
         p->filter_size_h = filter->filter_size;
     }
 
+    av_assert0(exec->height);
     ret = ff_sws_graph_add_pass(graph, dst->format, dst->width, dst->height,
-                                input, 0, comp->slice_align, op_pass_run,
+                                input, exec->height, comp->slice_align, 
op_pass_run,
                                 op_pass_setup, p, op_pass_free, output);
     if (ret < 0)
         return ret;
-- 
2.54.0


>From 968208dc340dd76e58be5c387fdd7a0caafdedad Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Sun, 8 Nov 2026 15:46:15 +0100
Subject: [PATCH 3/6] swscale/ops_dispatch: log resolved dispatch dimensions

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

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index 0b5a5e0370c..6c7f86ba7cc 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -703,6 +703,9 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
     align_pass(*output, comp->block_size, comp->over_write, p->pixel_bits_out);
     if (read)
         align_pass(input, comp->block_size, comp->over_read,  
p->pixel_bits_in);
+
+    av_log(ctx, AV_LOG_DEBUG, "Sub-pass dispatch dimensions = %dx%d\n",
+           exec->width, exec->height);
     return 0;
 
 fail:
-- 
2.54.0


>From c861b98860bc2e977e7c158a80f6f8c1dff90f52 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Mon, 22 Jun 2026 11:59:30 +0200
Subject: [PATCH 4/6] swscale/ops_dispatch: precompute input plane pixel size

The main motivating factor here is a desire to remove in_sub_x altogether.

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops_dispatch.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index 6c7f86ba7cc..404b6eca0a9 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -57,6 +57,7 @@ typedef struct SwsOpPass {
     int filter_size_v;
     bool memcpy_in;
     bool memcpy_out;
+    size_t input_bytes[4];
     size_t tail_blocks;
     uint8_t *tail_buf; /* extra memory for fixing unpadded tails */
     unsigned int tail_buf_size;
@@ -226,8 +227,7 @@ static int op_pass_setup(const SwsFrame *out, const 
SwsFrame *in,
         size_t input_bytes = in->linesize[idx];
         if (p->filter_size_h && float_in) {
             /* Floating point inputs may contain NaN / Infinity in the padding 
*/
-            const int plane_w = AV_CEIL_RSHIFT(in->width, exec->in_sub_x[i]);
-            input_bytes = pixel_bytes(plane_w, p->pixel_bits_in, AV_ROUND_UP);
+            input_bytes = p->input_bytes[i];
         }
 
         size_t safe_bytes = safe_bytes_pad(input_bytes, comp->over_read[i]);
@@ -613,6 +613,9 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
         exec->in_sub_y[i] = sub_y;
         exec->block_size_in[i] = block_bits_in >> 3;
         p->idx_in[i] = idx;
+
+        const int plane_w = AV_CEIL_RSHIFT(src->width, sub_x);
+        p->input_bytes[i] = pixel_bytes(plane_w, p->pixel_bits_in, 
AV_ROUND_UP);
     }
 
     for (int i = 0; i < p->planes_out; i++) {
-- 
2.54.0


>From 737a43487a9246750c2b36ae0a47096d21036961 Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Mon, 22 Jun 2026 11:32:05 +0200
Subject: [PATCH 5/6] swscale/ops_dispatch: remove SwsOpExec.out_sub_x/y

It turns out this is not really necessary, as we can always split op
lists involving subsampled planes into separate passes, one pass to compute
each output plane. This makes the loop dispatch more natural to implement,
and in fact is basically necessary for the SIMD loop model.

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops_dispatch.c      |  7 ++-----
 libswscale/ops_dispatch.h      | 12 ++++++++----
 libswscale/x86/ops_include.asm |  2 --
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index 404b6eca0a9..6b0909d52c7 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -156,7 +156,7 @@ static inline void get_row_data(const SwsOpPass *p, const 
int y_dst,
     for (int i = 0; i < p->planes_in; i++)
         in[i] = base->in[i] + (y_src >> base->in_sub_y[i]) * 
base->in_stride[i];
     for (int i = 0; i < p->planes_out; i++)
-        out[i] = base->out[i] + (y_dst >> base->out_sub_y[i]) * 
base->out_stride[i];
+        out[i] = base->out[i] + y_dst * base->out_stride[i];
 }
 
 static inline size_t pixel_bytes(size_t pixels, int pixel_bits,
@@ -461,9 +461,8 @@ static void op_pass_run(const SwsFrame *out, const SwsFrame 
*in, const int y,
     comp->func(&tail, comp->priv, num_blocks - tail_blocks, y, num_blocks, y + 
h);
 
     for (int i = 0; memcpy_out && i < p->planes_out; i++) {
-        const int lines = h >> tail.out_sub_y[i];
         copy_lines(exec.out[i], exec.out_stride[i],
-                   tail.out[i], tail.out_stride[i], lines, p->tail_size_out);
+                   tail.out[i], tail.out_stride[i], h, p->tail_size_out);
     }
 }
 
@@ -623,8 +622,6 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
         const int chroma = idx == 1 || idx == 2;
         const int sub_x = chroma ? outdesc->log2_chroma_w : 0;
         const int sub_y = chroma ? outdesc->log2_chroma_h : 0;
-        exec->out_sub_x[i] = sub_x;
-        exec->out_sub_y[i] = sub_y;
         exec->block_size_out[i] = block_bits_out >> 3;
         p->idx_out[i] = idx;
 
diff --git a/libswscale/ops_dispatch.h b/libswscale/ops_dispatch.h
index efed8e60f33..eaffb10ce3c 100644
--- a/libswscale/ops_dispatch.h
+++ b/libswscale/ops_dispatch.h
@@ -58,9 +58,13 @@ typedef struct SwsOpExec {
     int32_t block_size_in[4];   /* Size of a block of pixels in bytes */
     int32_t block_size_out[4];
 
-    /* Subsampling factors for each plane */
-    uint8_t in_sub_y[4], out_sub_y[4];
-    uint8_t in_sub_x[4], out_sub_x[4];
+    /**
+     * Subsampling factors for each input plane. Note that output planes
+     * are never subsampled within a single pass, since the pass dispatch
+     * dimensions must match the plane size being operated on.
+     */
+    uint8_t in_sub_y[4];
+    uint8_t in_sub_x[4];
 
     /**
      * Line bump; determines how many additional lines to advance (after
@@ -83,7 +87,7 @@ typedef struct SwsOpExec {
 
 static_assert(sizeof(SwsOpExec) == 24 * sizeof(void *) +
                                    12 * sizeof(int32_t) +
-                                   16 * sizeof(uint8_t) +
+                                   8  * sizeof(uint8_t) +
                                    2  * sizeof(void *),
               "SwsOpExec layout mismatch");
 
diff --git a/libswscale/x86/ops_include.asm b/libswscale/x86/ops_include.asm
index c0655581f17..cf1ed47b526 100644
--- a/libswscale/x86/ops_include.asm
+++ b/libswscale/x86/ops_include.asm
@@ -126,9 +126,7 @@ struc SwsOpExec
     .block_size_in resd 4
     .block_size_out resd 4
     .in_sub_y4 resb 4
-    .out_sub_y4 resb 4
     .in_sub_x4 resb 4
-    .out_sub_x4 resb 4
     .in_bump_y resq 1
     .in_offset_x resq 1
 endstruc
-- 
2.54.0


>From 13d7f5a47b94973dc5f08aeacfe7e92a2b8d165c Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Mon, 22 Jun 2026 12:00:30 +0200
Subject: [PATCH 6/6] swscale/ops_dispatch: remove SwsOpExec.in_sub_x/y

This needs to be handled with metadata on the uops compilation stage, so
there's no reason to have this extra information here in the first place.

(In fact, applying in_sub_y again would lead to double application,
corrupting data)

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops_dispatch.c      | 5 +----
 libswscale/ops_dispatch.h      | 9 ---------
 libswscale/x86/ops_include.asm | 2 --
 3 files changed, 1 insertion(+), 15 deletions(-)

diff --git a/libswscale/ops_dispatch.c b/libswscale/ops_dispatch.c
index 6b0909d52c7..037eaf79061 100644
--- a/libswscale/ops_dispatch.c
+++ b/libswscale/ops_dispatch.c
@@ -154,7 +154,7 @@ static inline void get_row_data(const SwsOpPass *p, const 
int y_dst,
     const SwsOpExec *base = &p->exec_base;
     const int y_src = p->offsets_y ? p->offsets_y[y_dst] : y_dst;
     for (int i = 0; i < p->planes_in; i++)
-        in[i] = base->in[i] + (y_src >> base->in_sub_y[i]) * 
base->in_stride[i];
+        in[i] = base->in[i] + y_src * base->in_stride[i];
     for (int i = 0; i < p->planes_out; i++)
         out[i] = base->out[i] + y_dst * base->out_stride[i];
 }
@@ -607,9 +607,6 @@ static int compile_single(const CompileArgs *args, const 
SwsOpList *ops,
         const int idx = ops->plane_src[i];
         const int chroma = idx == 1 || idx == 2;
         const int sub_x = chroma ? indesc->log2_chroma_w : 0;
-        const int sub_y = chroma ? indesc->log2_chroma_h : 0;
-        exec->in_sub_x[i] = sub_x;
-        exec->in_sub_y[i] = sub_y;
         exec->block_size_in[i] = block_bits_in >> 3;
         p->idx_in[i] = idx;
 
diff --git a/libswscale/ops_dispatch.h b/libswscale/ops_dispatch.h
index eaffb10ce3c..b0636b22f26 100644
--- a/libswscale/ops_dispatch.h
+++ b/libswscale/ops_dispatch.h
@@ -58,14 +58,6 @@ typedef struct SwsOpExec {
     int32_t block_size_in[4];   /* Size of a block of pixels in bytes */
     int32_t block_size_out[4];
 
-    /**
-     * Subsampling factors for each input plane. Note that output planes
-     * are never subsampled within a single pass, since the pass dispatch
-     * dimensions must match the plane size being operated on.
-     */
-    uint8_t in_sub_y[4];
-    uint8_t in_sub_x[4];
-
     /**
      * Line bump; determines how many additional lines to advance (after
      * incrementing normally to the next line), for each filtered output line.
@@ -87,7 +79,6 @@ typedef struct SwsOpExec {
 
 static_assert(sizeof(SwsOpExec) == 24 * sizeof(void *) +
                                    12 * sizeof(int32_t) +
-                                   8  * sizeof(uint8_t) +
                                    2  * sizeof(void *),
               "SwsOpExec layout mismatch");
 
diff --git a/libswscale/x86/ops_include.asm b/libswscale/x86/ops_include.asm
index cf1ed47b526..86da79d3ca0 100644
--- a/libswscale/x86/ops_include.asm
+++ b/libswscale/x86/ops_include.asm
@@ -125,8 +125,6 @@ struc SwsOpExec
     .slice_h resd 1
     .block_size_in resd 4
     .block_size_out resd 4
-    .in_sub_y4 resb 4
-    .in_sub_x4 resb 4
     .in_bump_y resq 1
     .in_offset_x resq 1
 endstruc
-- 
2.54.0

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

Reply via email to