Commit 3719f257cf for ffmpeg
commit 3719f257cf76e4463adc8b5b77029c3594def120
Author: Rodrigo Camacho <rodrigompcamacho@gmail.com>
Date: Thu Feb 12 17:45:04 2026 +0000
avfilter/scene_sad: R-V V scene_sad
Benchmarking results:
checkasm:
- CPU: SpacemiT, vendor arch 0x58000001, imp 0x1000000049772200
- CPU: VLEN = 256 bits
- Timing source: gettime
- Bench duration: 1000 µs per function (1000000 nsecs)
- Random seed: 4056509538
RV_zve64x:
- scene_sad.scene_sad8 [OK]
- scene_sad.scene_sad10 [OK]
- scene_sad.scene_sad12 [OK]
- scene_sad.scene_sad14 [OK]
- scene_sad.scene_sad15 [OK]
- scene_sad.scene_sad16 [OK]
checkasm: all 6 tests passed
Benchmark results:
name nsecs (vs ref)
scene_sad8_c: 331870.0
scene_sad8_rvv_i64: 27121.5 (12.23x)
scene_sad10_c: 335723.6
scene_sad10_rvv_i64: 52943.8 ( 6.33x)
scene_sad12_c: 335534.9
scene_sad12_rvv_i64: 52930.4 ( 6.33x)
scene_sad14_c: 335625.7
scene_sad14_rvv_i64: 52728.6 ( 6.36x)
scene_sad15_c: 335521.3
scene_sad15_rvv_i64: 52937.0 ( 6.33x)
scene_sad16_c: 335549.9
scene_sad16_rvv_i64: 52768.3 ( 6.35x)
Signed-off-by: Rodrigo Camacho <rodrigompcamacho@gmail.com>
diff --git a/libavfilter/riscv/Makefile b/libavfilter/riscv/Makefile
index 32b07eec1a..3934b3da8b 100644
--- a/libavfilter/riscv/Makefile
+++ b/libavfilter/riscv/Makefile
@@ -2,5 +2,7 @@ OBJS-$(CONFIG_AFIR_FILTER) += riscv/af_afir_init.o
RVV-OBJS-$(CONFIG_AFIR_FILTER) += riscv/af_afir_rvv.o
OBJS-$(CONFIG_BLACKDETECT_FILTER) += riscv/vf_blackdetect_init.o
RVV-OBJS-$(CONFIG_BLACKDETECT_FILTER) += riscv/vf_blackdetect_rvv.o
+OBJS-$(CONFIG_SCENE_SAD) += riscv/scene_sad_init.o
+RVV-OBJS-$(CONFIG_SCENE_SAD) += riscv/scene_sad_rvv.o
SHLIBOBJS += riscv/cpu_common.o
diff --git a/libavfilter/riscv/scene_sad_init.c b/libavfilter/riscv/scene_sad_init.c
new file mode 100644
index 0000000000..15d2df3cd0
--- /dev/null
+++ b/libavfilter/riscv/scene_sad_init.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright © 2026 Rodrigo Camacho.
+ *
+ * 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/cpu.h"
+#include "libavfilter/scene_sad.h"
+
+void ff_scene_sad8_rvv(SCENE_SAD_PARAMS);
+
+void ff_scene_sad16_rvv(SCENE_SAD_PARAMS);
+
+ff_scene_sad_fn ff_scene_sad_get_fn_riscv(int depth) {
+#if HAVE_RVV && (__riscv_xlen >= 64)
+ int flags = av_get_cpu_flags();
+
+ if (flags & AV_CPU_FLAG_RVV_I64) {
+ if (depth <= 8) {
+ return ff_scene_sad8_rvv;
+ } else if (depth <= 16) {
+ return ff_scene_sad16_rvv;
+ }
+ }
+#endif
+ return NULL;
+}
diff --git a/libavfilter/riscv/scene_sad_rvv.S b/libavfilter/riscv/scene_sad_rvv.S
new file mode 100644
index 0000000000..c4a7ce564a
--- /dev/null
+++ b/libavfilter/riscv/scene_sad_rvv.S
@@ -0,0 +1,142 @@
+/*
+ * Copyright © 2026 Rodrigo Camacho.
+ *
+ * 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/riscv/asm.S"
+
+/*
+ * void ff_scene_sad8_rvv(const uint8_t *src1, ptrdiff_t stride1,
+ * const uint8_t *src2, ptrdiff_t stride2,
+ * ptrdiff_t width, ptrdiff_t height,
+ * uint64_t *sum)
+*/
+#if __riscv_xlen >= 64
+func ff_scene_sad8_rvv, zve64x
+ lpad 0
+
+ bnez a5, 3f
+ sd zero, 0(a6)
+ ret
+3:
+ vsetvli t0, zero, e64, m8, ta, ma
+ vmv.v.x v24, zero
+
+ sub a1, a1, a4
+ sub a3, a3, a4
+1:
+ mv t0, a4
+ addi a5, a5, -1
+
+ vsetvli zero, zero, e32, m4, ta, ma
+ vmv.v.x v16, zero
+2:
+ vsetvli t1, t0, e8, m1, ta, ma
+ vle8.v v0, (a0)
+ vle8.v v1, (a2)
+
+ add a0, a0, t1
+ vmaxu.vv v2, v0, v1
+ vminu.vv v3, v0, v1
+ sub t0, t0, t1
+ vwsubu.vv v4, v2, v3
+ add a2, a2, t1
+
+ vsetvli zero, t1, e16, m2, tu, ma
+ vwaddu.wv v16, v16, v4
+
+ bnez t0, 2b
+
+ add a0, a0, a1
+ vsetvli t0, zero, e32, m4, ta, ma
+ add a2, a2, a3
+ vwaddu.wv v24, v24, v16
+
+ bnez a5, 1b
+
+ vsetvli zero, zero, e64, m8, ta, ma
+ vmv.s.x v0, zero
+ vredsum.vs v0, v24, v0
+ vmv.x.s a7, v0
+
+ sd a7, 0(a6)
+ ret
+endfunc
+
+/*
+ * void ff_scene_sad16_rvv(const uint8_t *src1, ptrdiff_t stride1,
+ * const uint8_t *src2, ptrdiff_t stride2,
+ * ptrdiff_t width, ptrdiff_t height,
+ * uint64_t *sum)
+*/
+func ff_scene_sad16_rvv, zve64x
+ lpad 0
+
+ bnez a5, 3f
+ sd zero, 0(a6)
+ ret
+3:
+ slli t3, a4, 1
+
+ vsetvli t0, zero, e64, m8, ta, ma
+ vmv.v.x v24, zero
+
+ sub a1, a1, t3
+ sub a3, a3, t3
+1:
+ mv t0, a4
+ addi a5, a5, -1
+
+ vsetvli t2, zero, e64, m8, ta, ma
+ vmv.v.x v16, zero
+2:
+ vsetvli t1, t0, e16, m2, ta, ma
+ vle16.v v0, (a0)
+ vle16.v v2, (a2)
+
+ slli t2, t1, 1
+ sub t0, t0, t1
+
+ vmaxu.vv v4, v0, v2
+ vminu.vv v6, v0, v2
+ add a2, a2, t2
+ vwsubu.vv v8, v4, v6
+ add a0, a0, t2
+
+ vsetvli zero, t1, e32, m4, tu, ma
+ vwaddu.wv v16, v16, v8
+
+ bnez t0, 2b
+
+ add a0, a0, a1
+ add a2, a2, a3
+
+ vsetvli t0, zero, e64, m8, ta, ma
+ vadd.vv v24, v24, v16
+
+ bnez a5, 1b
+
+ vsetvli t0, zero, e64, m8, ta, ma
+ vmv.s.x v0, zero
+ vredsum.vs v0, v24, v0
+ vmv.x.s a7, v0
+
+ sd a7, 0(a6)
+ ret
+endfunc
+#endif
diff --git a/libavfilter/scene_sad.c b/libavfilter/scene_sad.c
index 56177ced76..a6087611b8 100644
--- a/libavfilter/scene_sad.c
+++ b/libavfilter/scene_sad.c
@@ -61,6 +61,9 @@ ff_scene_sad_fn ff_scene_sad_get_fn(int depth)
ff_scene_sad_fn sad = NULL;
#if ARCH_X86 && HAVE_X86ASM
sad = ff_scene_sad_get_fn_x86(depth);
+#endif
+#if ARCH_RISCV
+ sad = ff_scene_sad_get_fn_riscv(depth);
#endif
if (!sad) {
if (depth <= 8)
diff --git a/libavfilter/scene_sad.h b/libavfilter/scene_sad.h
index 173a051f2b..cfa3509afc 100644
--- a/libavfilter/scene_sad.h
+++ b/libavfilter/scene_sad.h
@@ -39,6 +39,8 @@ void ff_scene_sad16_c(SCENE_SAD_PARAMS);
ff_scene_sad_fn ff_scene_sad_get_fn_x86(int depth);
+ff_scene_sad_fn ff_scene_sad_get_fn_riscv(int depth);
+
ff_scene_sad_fn ff_scene_sad_get_fn(int depth);
#endif /* AVFILTER_SCENE_SAD_H */