Commit 7c792b425d for ffmpeg
commit 7c792b425d76bec1258db9fbae77b6282d6da928
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Fri Nov 13 12:44:52 2026 -0800
avfilter/x86/scene_sad: add AVX2 and AVX-512 byte-swapped 16-bit SAD
Reuses the existing SAD macros with a bswap argument; safe for the full
16-bit range.
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavfilter/scene_sad.c b/libavfilter/scene_sad.c
index 0bf32b62f4..82259a3a61 100644
--- a/libavfilter/scene_sad.c
+++ b/libavfilter/scene_sad.c
@@ -96,5 +96,9 @@ ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void)
{
- return ff_scene_sad16_bswap_c;
+ ff_scene_sad_fn sad = NULL;
+#if ARCH_X86 && HAVE_X86ASM
+ sad = ff_scene_sad_bswap_get_fn_x86();
+#endif
+ return sad ? sad : ff_scene_sad16_bswap_c;
}
diff --git a/libavfilter/scene_sad.h b/libavfilter/scene_sad.h
index 06281ffbc5..2495a08099 100644
--- a/libavfilter/scene_sad.h
+++ b/libavfilter/scene_sad.h
@@ -48,6 +48,8 @@ ff_scene_sad_fn ff_scene_sad_get_fn_riscv(int depth);
ff_scene_sad_fn ff_scene_sad_get_fn(int depth);
+ff_scene_sad_fn ff_scene_sad_bswap_get_fn_x86(void);
+
ff_scene_sad_fn ff_scene_sad_bswap_get_fn(void);
#endif /* AVFILTER_SCENE_SAD_H */
diff --git a/libavfilter/x86/scene_sad.asm b/libavfilter/x86/scene_sad.asm
index 3ed201843e..861f5d869a 100644
--- a/libavfilter/x86/scene_sad.asm
+++ b/libavfilter/x86/scene_sad.asm
@@ -27,27 +27,47 @@
SECTION_RODATA
pw_1: times 32 dw 1
+pb_bswap16: times 4 db 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14
SECTION .text
-%macro SAD_INIT 1 ; depth
+%macro SAD_INIT 1-2 0 ; depth, bswap
+%if %2
+cglobal scene_sad%1_bswap, 6, 7, 6, src1, stride1, src2, stride2, width, end, x
+ movu m3, [pb_bswap16]
+ pxor m5, m5
+%else
cglobal scene_sad%1, 6, 7, 3, src1, stride1, src2, stride2, width, end, x
+%endif
add src1q, widthq
add src2q, widthq
neg widthq
pxor m1, m1
%endmacro
-%macro PSADQ 4 ; depth, dst, [src2], tmp
+%macro PSADQ 4-5 0 ; depth, dst, [src2], tmp, bswap
%if %1 == 8
; PSADBW requires an aligned memory operand.
movu %4, %3
psadbw %2, %4
%else
+ %if %5
+ ; byte-swapped samples, safe for the full 16-bit range
+ movu %4, %3
+ pshufb %2, m3
+ pshufb %4, m3
+ pminuw m4, %2, %4
+ pmaxuw %2, %4
+ psubw %2, m4
+ punpckhwd %4, %2, m5
+ punpcklwd %2, m5
+ paddd %2, %4
+ %else
psubw %2, %3
pabsw %2, %2
pmaddwd %2, [pw_1]
+ %endif
%if mmsize == 64
vextracti32x8 ymm%4, %2, 1
paddd ymm%2, ymm%4
@@ -60,13 +80,13 @@ cglobal scene_sad%1, 6, 7, 3, src1, stride1, src2, stride2, width, end, x
%endif
%endmacro
-%macro SAD_LOOP 1 ; depth
+%macro SAD_LOOP 1-2 0 ; depth, bswap
.nextrow:
mov xq, widthq
.loop:
movu m0, [src1q + xq]
- PSADQ %1, m0, [src2q + xq], m2
+ PSADQ %1, m0, [src2q + xq], m2, %2
paddq m1, m0
add xq, mmsize
jl .loop
@@ -81,9 +101,9 @@ RET
%endmacro
-%macro SAD_FRAMES 1 ; depth
- SAD_INIT %1
- SAD_LOOP %1
+%macro SAD_FRAMES 1-2 0 ; depth, bswap
+ SAD_INIT %1, %2
+ SAD_LOOP %1, %2
%endmacro
@@ -95,6 +115,7 @@ SAD_FRAMES 8
INIT_YMM avx2
SAD_FRAMES 8
SAD_FRAMES 16
+SAD_FRAMES 16, 1
%endif
@@ -103,5 +124,6 @@ SAD_FRAMES 16
INIT_ZMM avx512
SAD_FRAMES 8
SAD_FRAMES 16
+SAD_FRAMES 16, 1
%endif
diff --git a/libavfilter/x86/scene_sad_init.c b/libavfilter/x86/scene_sad_init.c
index 169a93d8b3..b9daf30d18 100644
--- a/libavfilter/x86/scene_sad_init.c
+++ b/libavfilter/x86/scene_sad_init.c
@@ -37,7 +37,7 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) { \
*sum += sad[0]; \
}
-#define SCENE_SAD16_FUNC(FUNC_NAME, ASM_FUNC_NAME, MMSIZE) \
+#define SCENE_SAD16_FUNC(FUNC_NAME, ASM_FUNC_NAME, C_FUNC_NAME, MMSIZE) \
void ASM_FUNC_NAME(SCENE_SAD_PARAMS); \
\
static void FUNC_NAME(SCENE_SAD_PARAMS) { \
@@ -48,20 +48,24 @@ static void FUNC_NAME(SCENE_SAD_PARAMS) { \
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); \
+ C_FUNC_NAME(src1 + bytes, stride1, \
+ src2 + bytes, stride2, \
+ width - (bytes >> 1), height, sad); \
*sum += sad[0]; \
}
SCENE_SAD_FUNC(scene_sad_sse2, ff_scene_sad8_sse2, 16)
#if HAVE_AVX2_EXTERNAL
SCENE_SAD_FUNC(scene_sad_avx2, ff_scene_sad8_avx2, 32)
-SCENE_SAD16_FUNC(scene_sad16_avx2, ff_scene_sad16_avx2, 32)
+SCENE_SAD16_FUNC(scene_sad16_avx2, ff_scene_sad16_avx2, ff_scene_sad16_c, 32)
+SCENE_SAD16_FUNC(scene_sad16_bswap_avx2, ff_scene_sad16_bswap_avx2,
+ ff_scene_sad16_bswap_c, 32)
#endif
#if HAVE_AVX512_EXTERNAL
SCENE_SAD_FUNC(scene_sad_avx512, ff_scene_sad8_avx512, 64)
-SCENE_SAD16_FUNC(scene_sad16_avx512, ff_scene_sad16_avx512, 64)
+SCENE_SAD16_FUNC(scene_sad16_avx512, ff_scene_sad16_avx512, ff_scene_sad16_c, 64)
+SCENE_SAD16_FUNC(scene_sad16_bswap_avx512, ff_scene_sad16_bswap_avx512,
+ ff_scene_sad16_bswap_c, 64)
#endif
ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth)
@@ -90,3 +94,17 @@ ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth)
}
return NULL;
}
+
+ff_scene_sad_fn ff_scene_sad_bswap_get_fn_x86(void)
+{
+ int cpu_flags = av_get_cpu_flags();
+#if HAVE_AVX512_EXTERNAL
+ if (EXTERNAL_AVX512(cpu_flags))
+ return scene_sad16_bswap_avx512;
+#endif
+#if HAVE_AVX2_EXTERNAL
+ if (EXTERNAL_AVX2_FAST(cpu_flags))
+ return scene_sad16_bswap_avx2;
+#endif
+ return NULL;
+}
diff --git a/tests/checkasm/scene_sad.c b/tests/checkasm/scene_sad.c
index 1f534d480c..f2892aa360 100644
--- a/tests/checkasm/scene_sad.c
+++ b/tests/checkasm/scene_sad.c
@@ -32,7 +32,7 @@
tmp_buf[j] = rnd() & mask; \
} while (0)
-static void check_scene_sad(int depth)
+static void check_scene_sad(int depth, int bswap)
{
LOCAL_ALIGNED_32(uint8_t, src1, [WIDTH * HEIGHT * 2]);
LOCAL_ALIGNED_32(uint8_t, src2, [WIDTH * HEIGHT * 2]);
@@ -50,7 +50,8 @@ static void check_scene_sad(int depth)
randomize_buffers(uint16_t, src2, width * HEIGHT, mask);
}
- if (check_func(ff_scene_sad_get_fn(depth), "scene_sad%d", depth)) {
+ if (check_func(bswap ? ff_scene_sad_bswap_get_fn() : ff_scene_sad_get_fn(depth),
+ "scene_sad%d%s", depth, bswap ? "_bswap" : "")) {
/* include rows narrower than and around each vector width */
const int widths[] = { 1, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 65, width };
const int heights[] = { 0, 1, HEIGHT };
@@ -63,8 +64,8 @@ static void check_scene_sad(int depth)
call_ref(src1, STRIDE, src2, STRIDE, widths[w], heights[h], &sum1);
call_new(src1, STRIDE, src2, STRIDE, widths[w], heights[h], &sum2);
if (sum1 != sum2) {
- fprintf(stderr, "scene_sad%d: sum mismatch for %dx%d: %llu != %llu\n",
- depth, widths[w], heights[h],
+ fprintf(stderr, "scene_sad%d%s: sum mismatch for %dx%d: %llu != %llu\n",
+ depth, bswap ? "_bswap" : "", widths[w], heights[h],
(unsigned long long) sum1, (unsigned long long) sum2);
fail();
}
@@ -98,7 +99,9 @@ void checkasm_check_scene_sad(void)
{
const int depths[] = { 8, 10, 12, 14, 15, 16 };
for (int i = 0; i < FF_ARRAY_ELEMS(depths); i++) {
- check_scene_sad(depths[i]);
+ check_scene_sad(depths[i], 0);
report("scene_sad%d", depths[i]);
}
+ check_scene_sad(16, 1);
+ report("scene_sad16_bswap");
}