PR #25986 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25986
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25986.patch

The x86 scene SAD wrappers round the width down to a multiple of the vector 
size and let the C code sum the remainder, but the asm loops are bottom-tested: 
with an aligned width of 0 they still summed one full vector per row (reading 
past the end of the row), and the remainder was added on top. A non-positive 
height likewise summed one row.

This hits real callers: freezedetect passes per-plane widths, and any narrow 
scdet input is affected. For example:

```
ffmpeg -cpuflags sse2 -f lavfi -i testsrc2=s=64x64:d=0.2 \
  -vf "format=gray,crop=8:8:16:16,scdet=t=0,metadata=print:file=-" -f null -
```

printed `lavfi.scd.mafd=0.684` for the second frame, against `0.293` with 
`-cpuflags 0`.

The wrappers now only call the asm when there is at least one full vector and 
one row, and checkasm compares widths below and not a multiple of the vector 
sizes against the C reference.

Found while reviewing #25530; independent of it.

Testing: x86_64 build, `checkasm --test=scene_sad` and 
`fate-checkasm-scene_sad` pass with SSE2, and the command above now matches C. 
The AVX2/AVX-512 versions were built but not executed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 798b44c2b2dcbdf7631715ef72e4cd4a7955b101 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 11 Nov 2026 12:06:52 -0800
Subject: [PATCH] avfilter/x86/scene_sad: skip the asm when the width is below
 one vector

The wrappers round the width down to a multiple of the vector size and
let the C code sum the remainder. The asm loops test their counters at
the bottom, so with an aligned width of 0 they still summed one full
vector per row, reading past the end of the row, and the remainder was
then added on top of that. Likewise one row was summed for a
non-positive height.

freezedetect passes the width of each plane, so small chroma planes
were affected, as is any narrow input of scdet:

ffmpeg -cpuflags sse2 -f lavfi -i testsrc2=s=64x64:d=0.2 -vf \
"format=gray,crop=8:8:16:16,scdet=t=0,metadata=print:file=-" -f null -

printed lavfi.scd.mafd=0.684 for the second frame, against 0.293 with
-cpuflags 0.

Only call the asm when there is at least one full vector and one row,
and make checkasm compare widths below and not a multiple of the
vector sizes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavfilter/x86/scene_sad_init.c | 18 ++++++++++++------
 tests/checkasm/scene_sad.c       | 20 ++++++++++++++------
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/libavfilter/x86/scene_sad_init.c b/libavfilter/x86/scene_sad_init.c
index a35f02c5600..daeeaf5ab4c 100644
--- a/libavfilter/x86/scene_sad_init.c
+++ b/libavfilter/x86/scene_sad_init.c
@@ -27,9 +27,12 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) {                    
                 \
     uint64_t sad[MMSIZE / 8] = {0};                                           \
     ptrdiff_t awidth = width & ~(MMSIZE - 1);                                 \
     *sum = 0;                                                                 \
-    ASM_FUNC_NAME(src1, stride1, src2, stride2, awidth, height, sad);         \
-    for (int i = 0; i < MMSIZE / 8; i++)                                      \
-        *sum += sad[i];                                                       \
+    /* the asm loops run at least once per row and per frame */               \
+    if (awidth > 0 && height > 0) {                                           \
+        ASM_FUNC_NAME(src1, stride1, src2, stride2, awidth, height, sad);     \
+        for (int i = 0; i < MMSIZE / 8; i++)                                  \
+            *sum += sad[i];                                                   \
+    }                                                                         \
     ff_scene_sad_c(src1 + awidth, stride1,                                    \
                    src2 + awidth, stride2,                                    \
                    width - awidth, height, sad);                              \
@@ -43,9 +46,12 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) {                    
                 \
     uint64_t sad[MMSIZE / 8] = {0};                                           \
     ptrdiff_t bytes = (width << 1) & ~(MMSIZE - 1);                           \
     *sum = 0;                                                                 \
-    ASM_FUNC_NAME(src1, stride1, src2, stride2, bytes, height, sad);          \
-    for (int i = 0; i < MMSIZE / 8; i++)                                      \
-        *sum += sad[i];                                                       \
+    /* the asm loops run at least once per row and per frame */               \
+    if (bytes > 0 && height > 0) {                                            \
+        ASM_FUNC_NAME(src1, stride1, src2, stride2, bytes, height, sad);      \
+        for (int i = 0; i < MMSIZE / 8; i++)                                  \
+            *sum += sad[i];                                                   \
+    }                                                                         \
     ff_scene_sad16_c(src1 + bytes, stride1,                                   \
                      src2 + bytes, stride2,                                   \
                      width - (bytes >> 1), height, sad);                      \
diff --git a/tests/checkasm/scene_sad.c b/tests/checkasm/scene_sad.c
index bc33c428c63..4bb8cbb6fe5 100644
--- a/tests/checkasm/scene_sad.c
+++ b/tests/checkasm/scene_sad.c
@@ -51,13 +51,21 @@ static void check_scene_sad(int depth)
     }
 
     if (check_func(ff_scene_sad_get_fn(depth), "scene_sad%d", depth)) {
+        /* widths below and not a multiple of the vector sizes,
+         * then the full width */
+        const int widths[] = { 1, 3, 7, 8, 9, 15, 17, 31, 33,
+                               63, 65, 127, width };
         uint64_t sum1, sum2;
-        call_ref(src1, STRIDE, src2, STRIDE, width, HEIGHT, &sum1);
-        call_new(src1, STRIDE, src2, STRIDE, width, HEIGHT, &sum2);
-        if (sum1 != sum2) {
-            fprintf(stderr, "scene_sad%d: sum mismatch: %llu != %llu\n",
-                    depth, (unsigned long long) sum1, (unsigned long long) 
sum2);
-            fail();
+        for (int i = 0; i < FF_ARRAY_ELEMS(widths); i++) {
+            const int w = widths[i];
+            call_ref(src1, STRIDE, src2, STRIDE, w, HEIGHT, &sum1);
+            call_new(src1, STRIDE, src2, STRIDE, w, HEIGHT, &sum2);
+            if (sum1 != sum2) {
+                fprintf(stderr, "scene_sad%d: sum mismatch at width %d: "
+                        "%llu != %llu\n", depth, w,
+                        (unsigned long long) sum1, (unsigned long long) sum2);
+                fail();
+            }
         }
         bench_new(src1, STRIDE, src2, STRIDE, WIDTH, HEIGHT, &sum2);
     }
-- 
2.54.0

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

Reply via email to