Commit 21c81303cb for ffmpeg
commit 21c81303cb7c6c407dac1a8c03991ab273f1fe13
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Tue Nov 10 22:08:57 2026 -0800
avfilter/x86: add SSE2 16-bit SAD for mpdecimate
About 1.7x faster than the C version auto-vectorized by GCC 13.3, making
the filter itself 1.56x faster on 1080p 10 and 16-bit input on Zen 3.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
diff --git a/libavfilter/mpdecimate.h b/libavfilter/mpdecimate.h
new file mode 100644
index 0000000000..d8728792ab
--- /dev/null
+++ b/libavfilter/mpdecimate.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFILTER_MPDECIMATE_H
+#define AVFILTER_MPDECIMATE_H
+
+#include "libavutil/pixelutils.h"
+
+/**
+ * Get the SAD function for 8x8 blocks of 16-bit samples.
+ * Strides are in bytes.
+ */
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn(void);
+
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn_x86(void);
+
+#endif /* AVFILTER_MPDECIMATE_H */
diff --git a/libavfilter/vf_mpdecimate.c b/libavfilter/vf_mpdecimate.c
index 37cd8cb017..6bed7186cd 100644
--- a/libavfilter/vf_mpdecimate.c
+++ b/libavfilter/vf_mpdecimate.c
@@ -25,6 +25,8 @@
* Rich Felker.
*/
+#include "config.h"
+
#include "libavutil/bprint.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
@@ -32,6 +34,7 @@
#include "libavutil/timestamp.h"
#include "avfilter.h"
#include "filters.h"
+#include "mpdecimate.h"
#include "video.h"
typedef enum {
@@ -101,6 +104,17 @@ static int sad_8x8_16(const uint8_t *src1, ptrdiff_t stride1,
return sum;
}
+av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn(void)
+{
+ av_pixelutils_sad_fn sad = NULL;
+
+#if ARCH_X86 && HAVE_X86ASM
+ sad = ff_mpdecimate_get_sad16_fn_x86();
+#endif
+
+ return sad ? sad : sad_8x8_16;
+}
+
/**
* Return 1 if the two planes are different, 0 otherwise.
* The reason is appended to bp, which the caller prints with the frame's
@@ -271,7 +285,7 @@ static int config_input(AVFilterLink *inlink)
decimate->lo_thr = av_clip64(decimate->lo * scale, INT_MIN, INT_MAX);
if (depth > 8)
- decimate->sad = sad_8x8_16;
+ decimate->sad = ff_mpdecimate_get_sad16_fn();
else
decimate->sad = av_pixelutils_get_sad_fn(3, 3, 0, ctx); // 8x8, not aligned on blocksize
if (!decimate->sad)
diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
index cfbed4317c..895b33f483 100644
--- a/libavfilter/x86/Makefile
+++ b/libavfilter/x86/Makefile
@@ -43,6 +43,8 @@ X86ASM-OBJS-$(CONFIG_MASKEDCLAMP_FILTER) += x86/vf_maskedclamp.o \
x86/vf_maskedclamp_init.o
X86ASM-OBJS-$(CONFIG_MASKEDMERGE_FILTER) += x86/vf_maskedmerge.o \
x86/vf_maskedmerge_init.o
+X86ASM-OBJS-$(CONFIG_MPDECIMATE_FILTER) += x86/vf_mpdecimate.o \
+ x86/vf_mpdecimate_init.o
X86ASM-OBJS-$(CONFIG_NLMEANS_FILTER) += x86/vf_nlmeans.o \
x86/vf_nlmeans_init.o
X86ASM-OBJS-$(CONFIG_OVERLAY_FILTER) += x86/vf_overlay.o \
diff --git a/libavfilter/x86/vf_mpdecimate.asm b/libavfilter/x86/vf_mpdecimate.asm
new file mode 100644
index 0000000000..1780585aa9
--- /dev/null
+++ b/libavfilter/x86/vf_mpdecimate.asm
@@ -0,0 +1,54 @@
+;*****************************************************************************
+;* x86-optimized functions for mpdecimate filter
+;*
+;* This file is part of FFmpeg.
+;*
+;* FFmpeg is free software; you can redistribute it and/or
+;* modify it under the terms of the GNU Lesser General Public
+;* License as published by the Free Software Foundation; either
+;* version 2.1 of the License, or (at your option) any later version.
+;*
+;* FFmpeg is distributed in the hope that it will be useful,
+;* but WITHOUT ANY WARRANTY; without even the implied warranty of
+;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;* Lesser General Public License for more details.
+;*
+;* You should have received a copy of the GNU Lesser General Public
+;* License along with FFmpeg; if not, write to the Free Software
+;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+;******************************************************************************
+
+%include "libavutil/x86/x86util.asm"
+
+SECTION .text
+
+;------------------------------------------------------------------------------
+; int ff_mpdecimate_sad_8x8_16(const uint8_t *src1, ptrdiff_t stride1,
+; const uint8_t *src2, ptrdiff_t stride2);
+;
+; SAD of an 8x8 block of unaligned 16-bit samples, strides in bytes.
+;------------------------------------------------------------------------------
+INIT_XMM sse2
+cglobal mpdecimate_sad_8x8_16, 4, 5, 5, src1, stride1, src2, stride2, cnt
+ pxor m0, m0
+ pxor m4, m4
+ mov cntd, 8
+.loop:
+ movu m1, [src1q]
+ movu m2, [src2q]
+ mova m3, m1
+ psubusw m1, m2
+ psubusw m2, m3
+ por m1, m2 ; |src1 - src2|, full 16-bit range
+ mova m2, m1
+ punpcklwd m1, m4
+ punpckhwd m2, m4
+ paddd m0, m1
+ paddd m0, m2
+ add src1q, stride1q
+ add src2q, stride2q
+ dec cntd
+ jg .loop
+ HADDD m0, m1
+ movd eax, m0
+ RET
diff --git a/libavfilter/x86/vf_mpdecimate_init.c b/libavfilter/x86/vf_mpdecimate_init.c
new file mode 100644
index 0000000000..583277e1b2
--- /dev/null
+++ b/libavfilter/x86/vf_mpdecimate_init.c
@@ -0,0 +1,35 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavutil/x86/cpu.h"
+#include "libavfilter/mpdecimate.h"
+
+int ff_mpdecimate_sad_8x8_16_sse2(const uint8_t *src1, ptrdiff_t stride1,
+ const uint8_t *src2, ptrdiff_t stride2);
+
+av_cold av_pixelutils_sad_fn ff_mpdecimate_get_sad16_fn_x86(void)
+{
+ int cpu_flags = av_get_cpu_flags();
+
+ if (EXTERNAL_SSE2(cpu_flags))
+ return ff_mpdecimate_sad_8x8_16_sse2;
+
+ return NULL;
+}
diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index ab1cc17b56..28d7d8400c 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -82,6 +82,7 @@ AVFILTEROBJS-$(CONFIG_GBLUR_FILTER) += vf_gblur.o
AVFILTEROBJS-$(CONFIG_HFLIP_FILTER) += vf_hflip.o
AVFILTEROBJS-$(CONFIG_IDET_FILTER) += vf_idet.o
AVFILTEROBJS-$(CONFIG_LIMITER_FILTER) += vf_limiter.o
+AVFILTEROBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o
AVFILTEROBJS-$(CONFIG_PP7_FILTER) += vf_pp7.o
AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER) += vf_threshold.o
AVFILTEROBJS-$(CONFIG_NLMEANS_FILTER) += vf_nlmeans.o
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index 4d15a7b7d1..e0d8593d4f 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -285,6 +285,9 @@ static const CheckasmTest tests[] = {
#if CONFIG_IDET_FILTER
{ "vf_idet", checkasm_check_idet },
#endif
+ #if CONFIG_MPDECIMATE_FILTER
+ { "vf_mpdecimate", checkasm_check_mpdecimate },
+ #endif
#if CONFIG_NLMEANS_FILTER
{ "vf_nlmeans", checkasm_check_nlmeans },
#endif
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 0fc6823c4f..e00e5c63ea 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -82,6 +82,7 @@ void checkasm_check_llvidencdsp(void);
void checkasm_check_lpc(void);
void checkasm_check_motion(void);
void checkasm_check_mpeg4videodsp(void);
+void checkasm_check_mpdecimate(void);
void checkasm_check_mpegvideo_unquantize(void);
void checkasm_check_mpegvideoencdsp(void);
void checkasm_check_nlmeans(void);
diff --git a/tests/checkasm/vf_mpdecimate.c b/tests/checkasm/vf_mpdecimate.c
new file mode 100644
index 0000000000..1a74cc5ca5
--- /dev/null
+++ b/tests/checkasm/vf_mpdecimate.c
@@ -0,0 +1,71 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "checkasm.h"
+
+#include "libavfilter/mpdecimate.h"
+#include "libavutil/mem_internal.h"
+
+#define STRIDE 32
+
+static void check_sad16(int depth)
+{
+ LOCAL_ALIGNED_32(uint16_t, src1, [STRIDE * 9]);
+ LOCAL_ALIGNED_32(uint16_t, src2, [STRIDE * 9]);
+ const int mask = (1 << depth) - 1;
+
+ declare_func(int, const uint8_t *src1, ptrdiff_t stride1,
+ const uint8_t *src2, ptrdiff_t stride2);
+
+ for (int i = 0; i < STRIDE * 9; i++) {
+ src1[i] = rnd() & mask;
+ src2[i] = rnd() & mask;
+ }
+ if (depth == 16) {
+ /* largest possible SAD */
+ for (int i = 0; i < STRIDE * 9; i++) {
+ src1[i] = 0;
+ src2[i] = 0xffff;
+ }
+ }
+
+ if (check_func(ff_mpdecimate_get_sad16_fn(), "sad_8x8_16_%d", depth)) {
+ /* blocks are not aligned in the filter */
+ for (int off = 0; off < 2; off++) {
+ const uint8_t *p1 = (const uint8_t *)(src1 + off * 3);
+ const uint8_t *p2 = (const uint8_t *)(src2 + off * 5);
+ int ref = call_ref(p1, STRIDE * 2, p2, STRIDE * 2);
+ int new = call_new(p1, STRIDE * 2, p2, STRIDE * 2);
+ if (ref != new) {
+ fprintf(stderr, "sad_8x8_16_%d: %d != %d\n", depth, ref, new);
+ fail();
+ }
+ }
+ bench_new((const uint8_t *)src1, STRIDE * 2,
+ (const uint8_t *)src2, STRIDE * 2);
+ }
+}
+
+void checkasm_check_mpdecimate(void)
+{
+ static const int depths[] = { 10, 12, 15, 16 };
+
+ for (int i = 0; i < FF_ARRAY_ELEMS(depths); i++)
+ check_sad16(depths[i]);
+ report("sad_8x8_16");
+}
diff --git a/tests/fate/checkasm.mak b/tests/fate/checkasm.mak
index 19bbe7e571..df996cccec 100644
--- a/tests/fate/checkasm.mak
+++ b/tests/fate/checkasm.mak
@@ -88,6 +88,7 @@ FATE_CHECKASM = fate-checkasm-aacencdsp \
fate-checkasm-vf_hflip \
fate-checkasm-vf_idet \
fate-checkasm-vf_limiter \
+ fate-checkasm-vf_mpdecimate \
fate-checkasm-vf_nlmeans \
fate-checkasm-vf_pp7 \
fate-checkasm-vf_threshold \