PR #25018 opened by darpat039
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25018
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25018.patch

# Summary of changes

AudioDSP has assembly for x86, 32-bit ARM, PPC and RISC-V, but none for 
AArch64. This PR adds NEON versions of vector_clip_int32 and vector_clipf.

vector_clip_int32 is a port of the 32-bit ARM NEON version. I also tried 16 and 
32 elements per iteration, on an Apple M4 neither was measurably faster than 8, 
so I kept the shortest variant.

vector_clipf uses fmaxnm/fminnm with max first, so a NaN input yields min, 
matching the C version.

The C vector_clip_int32 is unrolled by hand, which keeps clang from vectorizing 
it. A plain loop does get auto-vectorized, but in my measurements the NEON 
version was still faster than that, and it does not depend on the compiler.

scalarproduct_int16 is left alone: clang already vectorizes the C version well.

checkasm --bench on Apple M4:

    vector_clip_int32_c:      1029.6
    vector_clip_int32_neon:    180.7 ( 5.38x)
    vector_clipf_c:            682.9
    vector_clipf_neon:         190.8 ( 3.34x)

Tested with checkasm and fate-ac3-fixed-encode, fate-ac3-fixed-encode-2, 
fate-ac3-fixed-encode-3, fate-ac3-encode, fate-eac3-encode and fate-ra-cook. I 
only have Apple Silicon to test on.



>From 2a5c8965f177cf13d93d3512d1b5c435b5fa5d4e Mon Sep 17 00:00:00 2001
From: Patryk Darmofalski <[email protected]>
Date: Sun, 11 Oct 2026 23:20:48 +0200
Subject: [PATCH] avcodec/aarch64/audiodsp: add NEON vector_clip_int32 and
 vector_clipf

vector_clip_int32 is based on the 32-bit ARM NEON version. Unrolling to
16 or 32 elements per iteration gave no measurable improvement over 8.

vector_clipf uses fmaxnm/fminnm, so a NaN input yields min as in C.

checkasm --bench on Apple M4:
vector_clip_int32_c:      1029.6
vector_clip_int32_neon:    180.7 ( 5.38x)
vector_clipf_c:            682.9
vector_clipf_neon:         190.8 ( 3.34x)

Signed-off-by: Patryk Darmofalski <[email protected]>
---
 libavcodec/aarch64/Makefile        |  2 ++
 libavcodec/aarch64/audiodsp_init.c | 41 ++++++++++++++++++++++++
 libavcodec/aarch64/audiodsp_neon.S | 51 ++++++++++++++++++++++++++++++
 libavcodec/audiodsp.c              |  4 ++-
 libavcodec/audiodsp.h              |  1 +
 5 files changed, 98 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/aarch64/audiodsp_init.c
 create mode 100644 libavcodec/aarch64/audiodsp_neon.S

diff --git a/libavcodec/aarch64/Makefile b/libavcodec/aarch64/Makefile
index e0762e3fa1..df15002068 100644
--- a/libavcodec/aarch64/Makefile
+++ b/libavcodec/aarch64/Makefile
@@ -1,5 +1,6 @@
 # subsystems
 OBJS-$(CONFIG_AC3DSP)                   += aarch64/ac3dsp_init_aarch64.o
+OBJS-$(CONFIG_AUDIODSP)                 += aarch64/audiodsp_init.o
 OBJS-$(CONFIG_FDCTDSP)                  += aarch64/fdctdsp_init_aarch64.o
 OBJS-$(CONFIG_H264CHROMA)               += aarch64/h264chroma_init_aarch64.o
 OBJS-$(CONFIG_H264DSP)                  += aarch64/h264dsp_init_aarch64.o
@@ -43,6 +44,7 @@ ARMV8-OBJS-$(CONFIG_VIDEODSP)           += aarch64/videodsp.o
 NEON-OBJS-$(CONFIG_AAC_DECODER)         += aarch64/sbrdsp_neon.o
 NEON-OBJS-$(CONFIG_AAC_ENCODER)         += aarch64/aacencdsp_neon.o
 NEON-OBJS-$(CONFIG_AC3DSP)              += aarch64/ac3dsp_neon.o
+NEON-OBJS-$(CONFIG_AUDIODSP)            += aarch64/audiodsp_neon.o
 NEON-OBJS-$(CONFIG_FDCTDSP)             += aarch64/fdctdsp_neon.o
 NEON-OBJS-$(CONFIG_H264CHROMA)          += aarch64/h264cmc_neon.o
 NEON-OBJS-$(CONFIG_H264DSP)             += aarch64/h264dsp_neon.o              
\
diff --git a/libavcodec/aarch64/audiodsp_init.c 
b/libavcodec/aarch64/audiodsp_init.c
new file mode 100644
index 0000000000..07b48ae70a
--- /dev/null
+++ b/libavcodec/aarch64/audiodsp_init.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2026 Patryk Darmofalski <[email protected]>
+ *
+ * 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 <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavutil/aarch64/cpu.h"
+#include "libavcodec/audiodsp.h"
+
+void ff_vector_clip_int32_neon(int32_t *dst, const int32_t *src, int32_t min,
+                               int32_t max, unsigned int len);
+void ff_vector_clipf_neon(float *dst, const float *src, int len, float min,
+                          float max);
+
+av_cold void ff_audiodsp_init_aarch64(AudioDSPContext *c)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if (have_neon(cpu_flags)) {
+        c->vector_clip_int32 = ff_vector_clip_int32_neon;
+        c->vector_clipf      = ff_vector_clipf_neon;
+    }
+}
diff --git a/libavcodec/aarch64/audiodsp_neon.S 
b/libavcodec/aarch64/audiodsp_neon.S
new file mode 100644
index 0000000000..36e0f146cf
--- /dev/null
+++ b/libavcodec/aarch64/audiodsp_neon.S
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2026 Patryk Darmofalski <[email protected]>
+ *
+ * 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"
+
+function ff_vector_clip_int32_neon, export=1
+        dup             v0.4s, w2
+        dup             v1.4s, w3
+1:
+        ld1             {v2.4s, v3.4s}, [x1], #32
+        smin            v2.4s, v2.4s, v1.4s
+        smin            v3.4s, v3.4s, v1.4s
+        smax            v2.4s, v2.4s, v0.4s
+        smax            v3.4s, v3.4s, v0.4s
+        subs            w4, w4, #8
+        st1             {v2.4s, v3.4s}, [x0], #32
+        b.gt            1b
+        ret
+endfunc
+
+function ff_vector_clipf_neon, export=1
+        dup             v0.4s, v0.s[0]
+        dup             v1.4s, v1.s[0]
+1:
+        ld1             {v2.4s, v3.4s}, [x1], #32
+        fmaxnm          v2.4s, v2.4s, v0.4s
+        fmaxnm          v3.4s, v3.4s, v0.4s
+        fminnm          v2.4s, v2.4s, v1.4s
+        fminnm          v3.4s, v3.4s, v1.4s
+        subs            w2, w2, #8
+        st1             {v2.4s, v3.4s}, [x0], #32
+        b.gt            1b
+        ret
+endfunc
diff --git a/libavcodec/audiodsp.c b/libavcodec/audiodsp.c
index a4758bb4c3..6bd9e719fa 100644
--- a/libavcodec/audiodsp.c
+++ b/libavcodec/audiodsp.c
@@ -68,7 +68,9 @@ av_cold void ff_audiodsp_init(AudioDSPContext *c)
     c->vector_clip_int32   = vector_clip_int32_c;
     c->vector_clipf        = vector_clipf_c;
 
-#if ARCH_ARM
+#if ARCH_AARCH64
+    ff_audiodsp_init_aarch64(c);
+#elif ARCH_ARM
     ff_audiodsp_init_arm(c);
 #elif ARCH_PPC
     ff_audiodsp_init_ppc(c);
diff --git a/libavcodec/audiodsp.h b/libavcodec/audiodsp.h
index 485b512839..764f23cd57 100644
--- a/libavcodec/audiodsp.h
+++ b/libavcodec/audiodsp.h
@@ -53,6 +53,7 @@ typedef struct AudioDSPContext {
 } AudioDSPContext;
 
 void ff_audiodsp_init(AudioDSPContext *c);
+void ff_audiodsp_init_aarch64(AudioDSPContext *c);
 void ff_audiodsp_init_arm(AudioDSPContext *c);
 void ff_audiodsp_init_ppc(AudioDSPContext *c);
 void ff_audiodsp_init_riscv(AudioDSPContext *c);
-- 
2.52.0

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

Reply via email to