PR #25949 opened by Baptiste Coudurier (bcoudurier) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25949 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25949.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. 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. Likewise one row was summed for a non-positive height. freezedetect passes the width of each plane, so small chroma planes are 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 - ``` prints `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, which covers all the instantiations (SSE2, AVX2, AVX-512, 8 and 16 bit). checkasm now compares widths below and not a multiple of the vector sizes; without the fix it fails for widths 1 to 15 with SSE2. Tested with an x86_64 build, SSE2 only: the command above matches the C output, `checkasm --test=scene_sad` and `fate-checkasm-scene_sad`, `fate-filter-metadata-freezedetect`, `fate-filter-mpdecimate`, `fate-filter-select` pass. The AVX2 and AVX-512 versions build but I could not run them. 🤖 Generated with [Claude Code](https://claude.com/claude-code) From 3314bf397be69f6d3edd114cafca4ece79dc3673 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]> --- libavfilter/x86/scene_sad_init.c | 18 ++++++++++++------ tests/checkasm/scene_sad.c | 17 +++++++++++------ 2 files changed, 23 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..06922ee6da4 100644 --- a/tests/checkasm/scene_sad.c +++ b/tests/checkasm/scene_sad.c @@ -51,13 +51,18 @@ 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]
