Commit 0eaca98202 for ffmpeg
commit 0eaca982028156202e222366896260009039b680
Author: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Date: Wed Nov 18 13:34:59 2026 -0800
avfilter/aarch64: add NEON filter_line for idet
Optimize samples up to 15 bits, keeping 16-bit samples on C. Row tails
reuse the last eight samples with a lane mask, and rows shorter than
eight samples tail-call C.
Use have_neon() for CPU and build-time dispatch, following the other
AArch64 DSP initializers. Inputs must fit their declared pixel depth.
Apple M3 Max, checkasm --bench:
idet8_neon: 2.34x
idet10_neon: 1.31x
idet15_neon: 1.30x
Signed-off-by: Baptiste Coudurier <baptiste.coudurier@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/libavfilter/aarch64/Makefile b/libavfilter/aarch64/Makefile
index d09f7e4080..b2d72fe4b4 100644
--- a/libavfilter/aarch64/Makefile
+++ b/libavfilter/aarch64/Makefile
@@ -1,9 +1,11 @@
OBJS-$(CONFIG_BWDIF_FILTER) += aarch64/vf_bwdif_init_aarch64.o
OBJS-$(CONFIG_COLORDETECT_FILTER) += aarch64/vf_colordetect_init.o
+OBJS-$(CONFIG_IDET_FILTER) += aarch64/vf_idetdsp_init.o
OBJS-$(CONFIG_NLMEANS_FILTER) += aarch64/vf_nlmeans_init.o
OBJS-$(CONFIG_THRESHOLD_FILTER) += aarch64/vf_threshold_init.o
NEON-OBJS-$(CONFIG_BWDIF_FILTER) += aarch64/vf_bwdif_neon.o
NEON-OBJS-$(CONFIG_COLORDETECT_FILTER) += aarch64/vf_colordetect_neon.o
+NEON-OBJS-$(CONFIG_IDET_FILTER) += aarch64/vf_idetdsp_neon.o
NEON-OBJS-$(CONFIG_NLMEANS_FILTER) += aarch64/vf_nlmeans_neon.o
NEON-OBJS-$(CONFIG_THRESHOLD_FILTER) += aarch64/vf_threshold_neon.o
diff --git a/libavfilter/aarch64/vf_idetdsp_init.c b/libavfilter/aarch64/vf_idetdsp_init.c
new file mode 100644
index 0000000000..96642edaa8
--- /dev/null
+++ b/libavfilter/aarch64/vf_idetdsp_init.c
@@ -0,0 +1,43 @@
+/*
+ * 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 "config.h"
+
+#include "libavutil/attributes.h"
+#include "libavutil/aarch64/cpu.h"
+#include "libavfilter/vf_idetdsp.h"
+
+int ff_idet_filter_line_neon(const uint8_t *a, const uint8_t *b,
+ const uint8_t *c, int w);
+int ff_idet_filter_line_15bit_neon(const uint8_t *a, const uint8_t *b,
+ const uint8_t *c, int w);
+
+av_cold void ff_idet_dsp_init_aarch64(IDETDSPContext *dsp, int depth)
+{
+ int cpu_flags = av_get_cpu_flags();
+
+ if (!have_neon(cpu_flags))
+ return;
+
+ /* 16-bit samples need 32-bit lanes for a + c and 2 * b, which ran no
+ * faster than the C loop, so they stay on it. */
+ if (depth <= 8)
+ dsp->filter_line = ff_idet_filter_line_neon;
+ else if (depth <= 15)
+ dsp->filter_line = ff_idet_filter_line_15bit_neon;
+}
diff --git a/libavfilter/aarch64/vf_idetdsp_neon.S b/libavfilter/aarch64/vf_idetdsp_neon.S
new file mode 100644
index 0000000000..7020affbbb
--- /dev/null
+++ b/libavfilter/aarch64/vf_idetdsp_neon.S
@@ -0,0 +1,167 @@
+/*
+ * 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/aarch64/asm.S"
+
+/* Loaded at idet_tail_mask + 2 * n, keeps the last n of 8 halfword lanes */
+const idet_tail_mask, align=4
+ .short 0, 0, 0, 0, 0, 0, 0, 0
+ .short -1, -1, -1, -1, -1, -1, -1, -1
+endconst
+
+/* Sum of |a + c - 2 * b| over w pixels.
+ * x0: const uint8_t *a
+ * x1: const uint8_t *b
+ * x2: const uint8_t *c
+ * w3: int w
+ */
+function ff_idet_filter_line_neon, export=1
+ cmp w3, #8
+ b.lt 8f
+ movi v16.4s, #0
+ movi v17.4s, #0
+ movi v18.4s, #0
+ movi v19.4s, #0
+ subs w3, w3, #32
+ b.lt 2f
+1:
+ ldp q0, q1, [x0], #32
+ ldp q2, q3, [x1], #32
+ ldp q4, q5, [x2], #32
+ subs w3, w3, #32
+ uaddl v20.8h, v0.8b, v4.8b // a + c
+ uaddl2 v21.8h, v0.16b, v4.16b
+ uaddl v22.8h, v1.8b, v5.8b
+ uaddl2 v23.8h, v1.16b, v5.16b
+ uaddl v24.8h, v2.8b, v2.8b // 2 * b
+ uaddl2 v25.8h, v2.16b, v2.16b
+ uaddl v26.8h, v3.8b, v3.8b
+ uaddl2 v27.8h, v3.16b, v3.16b
+ uabd v20.8h, v20.8h, v24.8h
+ uabd v21.8h, v21.8h, v25.8h
+ uabd v22.8h, v22.8h, v26.8h
+ uabd v23.8h, v23.8h, v27.8h
+ uadalp v16.4s, v20.8h
+ uadalp v17.4s, v21.8h
+ uadalp v18.4s, v22.8h
+ uadalp v19.4s, v23.8h
+ b.ge 1b
+2:
+ adds w3, w3, #24
+ b.lt 4f
+3:
+ ldr d0, [x0], #8
+ ldr d2, [x1], #8
+ ldr d4, [x2], #8
+ subs w3, w3, #8
+ uaddl v20.8h, v0.8b, v4.8b
+ uaddl v24.8h, v2.8b, v2.8b
+ uabd v20.8h, v20.8h, v24.8h
+ uadalp v16.4s, v20.8h
+ b.ge 3b
+4:
+ // Last 1-7 pixels: reload the final 8 and mask the overlap
+ ands w3, w3, #7
+ b.eq 5f
+ movrel x5, idet_tail_mask
+ sub x4, x3, #8
+ add x5, x5, x3, lsl #1
+ ldr d0, [x0, x4]
+ ldr d2, [x1, x4]
+ ldr d4, [x2, x4]
+ ldr q6, [x5]
+ uaddl v20.8h, v0.8b, v4.8b
+ uaddl v24.8h, v2.8b, v2.8b
+ uabd v20.8h, v20.8h, v24.8h
+ and v20.16b, v20.16b, v6.16b
+ uadalp v17.4s, v20.8h
+5:
+ add v16.4s, v16.4s, v17.4s
+ add v18.4s, v18.4s, v19.4s
+ add v16.4s, v16.4s, v18.4s
+ addv s0, v16.4s
+ fmov w0, s0
+ ret
+8:
+ b X(ff_idet_filter_line_c)
+endfunc
+
+/* Same as above for samples of up to 15 bits, where a + c and 2 * b
+ * still fit in 16-bit lanes. Samples must be within that depth, so
+ * nonzero padding bits above it can give a different sum than C.
+ * x0: const uint8_t *a
+ * x1: const uint8_t *b
+ * x2: const uint8_t *c
+ * w3: int w, in samples
+ */
+function ff_idet_filter_line_15bit_neon, export=1
+ cmp w3, #8
+ b.lt 8f
+ movi v16.4s, #0
+ movi v17.4s, #0
+ subs w3, w3, #16
+ b.lt 2f
+1:
+ ldp q0, q1, [x0], #32
+ ldp q2, q3, [x1], #32
+ ldp q4, q5, [x2], #32
+ subs w3, w3, #16
+ add v0.8h, v0.8h, v4.8h // a + c
+ add v1.8h, v1.8h, v5.8h
+ add v2.8h, v2.8h, v2.8h // 2 * b
+ add v3.8h, v3.8h, v3.8h
+ uabd v0.8h, v0.8h, v2.8h
+ uabd v1.8h, v1.8h, v3.8h
+ uadalp v16.4s, v0.8h
+ uadalp v17.4s, v1.8h
+ b.ge 1b
+2:
+ // The low 4 bits of w3 are the samples left
+ ubfiz x4, x3, #1, #4
+ tbz w3, #3, 3f
+ ldr q0, [x0]
+ ldr q2, [x1]
+ ldr q4, [x2]
+ add v0.8h, v0.8h, v4.8h
+ add v2.8h, v2.8h, v2.8h
+ uabd v0.8h, v0.8h, v2.8h
+ uadalp v16.4s, v0.8h
+3:
+ // Last 1-7 samples: reload the final 8 and mask the overlap
+ ands w3, w3, #7
+ b.eq 5f
+ movrel x5, idet_tail_mask
+ sub x4, x4, #16
+ lsl x3, x3, #1
+ ldr q0, [x0, x4]
+ ldr q2, [x1, x4]
+ ldr q4, [x2, x4]
+ ldr q6, [x5, x3]
+ add v0.8h, v0.8h, v4.8h
+ add v2.8h, v2.8h, v2.8h
+ uabd v0.8h, v0.8h, v2.8h
+ and v0.16b, v0.16b, v6.16b
+ uadalp v17.4s, v0.8h
+5:
+ add v16.4s, v16.4s, v17.4s
+ addv s0, v16.4s
+ fmov w0, s0
+ ret
+8:
+ b X(ff_idet_filter_line_c_16bit)
+endfunc
diff --git a/libavfilter/vf_idetdsp.c b/libavfilter/vf_idetdsp.c
index d7c271df20..5676c056b1 100644
--- a/libavfilter/vf_idetdsp.c
+++ b/libavfilter/vf_idetdsp.c
@@ -56,7 +56,9 @@ int ff_idet_filter_line_c_16bit(const uint8_t *a, const uint8_t *b, const uint8_
void av_cold ff_idet_dsp_init(IDETDSPContext *dsp, int depth)
{
dsp->filter_line = depth > 8 ? ff_idet_filter_line_c_16bit : ff_idet_filter_line_c;
-#if ARCH_X86 && HAVE_X86ASM
+#if ARCH_AARCH64
+ ff_idet_dsp_init_aarch64(dsp, depth);
+#elif ARCH_X86 && HAVE_X86ASM
ff_idet_dsp_init_x86(dsp, depth);
#endif
}
diff --git a/libavfilter/vf_idetdsp.h b/libavfilter/vf_idetdsp.h
index 174d77c171..a630098676 100644
--- a/libavfilter/vf_idetdsp.h
+++ b/libavfilter/vf_idetdsp.h
@@ -29,6 +29,7 @@ typedef struct IDETDSPContext {
void ff_idet_dsp_init(IDETDSPContext *idet, int depth);
+void ff_idet_dsp_init_aarch64(IDETDSPContext *idet, int depth);
void ff_idet_dsp_init_x86(IDETDSPContext *idet, int depth);
/* main fall-back for left-over */