PR #25769 opened by Rodrigo Manuel Pita Camacho (R-Camacho)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25769
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25769.patch

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: 3403380650
RV_zve32x:
 - vf_threshold.threshold8  [OK]
 - vf_threshold.threshold16 [OK]
checkasm: all 2 tests passed
Benchmark results:
  name                    nsecs (vs ref)
  threshold8_c:          1390.1
  threshold8_rvv_i32:      64.1 (21.67x)
  threshold16_c:          741.2
  threshold16_rvv_i32:     64.8 (11.43x)

Signed-off-by: Rodrigo Camacho <[email protected]>


From f7879659f406b8a77d901189ae2536d3008531da Mon Sep 17 00:00:00 2001
From: Rodrigo Camacho <[email protected]>
Date: Wed, 4 Nov 2026 18:52:13 +0000
Subject: [PATCH] av_filter/vf_threshold: R-V V vf_threshold
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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: 3403380650
RV_zve32x:
 - vf_threshold.threshold8  [OK]
 - vf_threshold.threshold16 [OK]
checkasm: all 2 tests passed
Benchmark results:
  name                    nsecs (vs ref)
  threshold8_c:          1390.1
  threshold8_rvv_i32:      64.1 (21.67x)
  threshold16_c:          741.2
  threshold16_rvv_i32:     64.8 (11.43x)

Signed-off-by: Rodrigo Camacho <[email protected]>
---
 libavfilter/riscv/Makefile            |   2 +
 libavfilter/riscv/vf_threshold_init.c |  49 +++++++++
 libavfilter/riscv/vf_threshold_rvv.S  | 145 ++++++++++++++++++++++++++
 libavfilter/threshold.h               |   1 +
 libavfilter/vf_threshold_init.h       |   3 +
 5 files changed, 200 insertions(+)
 create mode 100644 libavfilter/riscv/vf_threshold_init.c
 create mode 100644 libavfilter/riscv/vf_threshold_rvv.S

diff --git a/libavfilter/riscv/Makefile b/libavfilter/riscv/Makefile
index 3934b3da8be..ed1ee185d09 100644
--- a/libavfilter/riscv/Makefile
+++ b/libavfilter/riscv/Makefile
@@ -4,5 +4,7 @@ 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
+OBJS-$(CONFIG_THRESHOLD_FILTER)              += riscv/vf_threshold_init.o
+RVV-OBJS-$(CONFIG_THRESHOLD_FILTER)          += riscv/vf_threshold_rvv.o
 
 SHLIBOBJS += riscv/cpu_common.o
diff --git a/libavfilter/riscv/vf_threshold_init.c 
b/libavfilter/riscv/vf_threshold_init.c
new file mode 100644
index 00000000000..e00c26e2197
--- /dev/null
+++ b/libavfilter/riscv/vf_threshold_init.c
@@ -0,0 +1,49 @@
+/*
+ * 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/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavfilter/threshold.h"
+
+#define THRESHOLD_FUNC(depth) \
+void ff_threshold##depth##_rvv(const uint8_t *in, const uint8_t *threshold,\
+                                const uint8_t *min, const uint8_t *max,     \
+                                uint8_t *out,                               \
+                                ptrdiff_t ilinesize, ptrdiff_t tlinesize,   \
+                                ptrdiff_t flinesize, ptrdiff_t slinesize,   \
+                                ptrdiff_t olinesize,                        \
+                                int w, int h);
+
+THRESHOLD_FUNC(8)
+THRESHOLD_FUNC(16)
+
+av_cold void ff_threshold_init_riscv(ThresholdContext *s)
+{
+#if HAVE_RVV
+    int flags = av_get_cpu_flags();
+
+    if (flags & AV_CPU_FLAG_RVV_I32) {
+        if (s->depth == 8)
+            s->threshold = ff_threshold8_rvv;
+        else if (s->depth == 16)
+            s->threshold = ff_threshold16_rvv;
+    }
+#endif
+}
diff --git a/libavfilter/riscv/vf_threshold_rvv.S 
b/libavfilter/riscv/vf_threshold_rvv.S
new file mode 100644
index 00000000000..6b5e3c7b8b3
--- /dev/null
+++ b/libavfilter/riscv/vf_threshold_rvv.S
@@ -0,0 +1,145 @@
+/*
+ * 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"
+
+#define XSZ            (__riscv_xlen / 8)
+
+/*
+ * void ff_threshold8_rvv(const uint8_t *in, const uint8_t *threshold,
+ *                        const uint8_t *min, const uint8_t *max,
+ *                        uint8_t *out,
+ *                        ptrdiff_t ilinesize, ptrdiff_t tlinesize,
+ *                        ptrdiff_t flinesize, ptrdiff_t slinesize,
+ *                        ptrdiff_t olinesize,
+ *                        int w, int h)
+*/
+func ff_threshold8_rvv, zve32x
+        lpad 0
+
+        lx   t0, 0(sp)         # slinesize
+        lx   t1, XSZ(sp)       # olinesize
+        lw   t2, (2 * XSZ)(sp) # w
+        lw   t3, (3 * XSZ)(sp) # h
+
+        sub  a5, a5, t2
+        sub  a6, a6, t2
+        sub  a7, a7, t2
+        sub  t0, t0, t2
+        sub  t1, t1, t2
+1:
+        mv   t4, t2
+        addi t3, t3, -1
+2:
+        vsetvli t5, t4, e8, m8, ta, ma
+
+        vle8.v  v8, (a0)
+        add a0, a0, t5
+        vle8.v  v16, (a1)
+        add a1, a1, t5
+
+        vmsleu.vv v0, v8, v16
+
+        vle8.v v8, (a2)
+        add    a2, a2, t5
+        vle8.v v24, (a3)
+        add    a3, a3, t5
+
+        vmerge.vvm v16, v24, v8, v0
+        sub t4, t4, t5
+
+        vse8.v v16, (a4)
+
+        add  a4, a4, t5
+
+        bnez t4, 2b
+
+        add  a0, a0, a5
+        add  a1, a1, a6
+        add  a2, a2, a7
+        add  a3, a3, t0
+        add  a4, a4, t1
+
+        bnez t3, 1b
+
+        ret
+endfunc
+
+/*
+ * void ff_threshold16_rvv(const uint8_t *in, const uint8_t *threshold,
+ *                         const uint8_t *min, const uint8_t *max,
+ *                         uint8_t *out,
+ *                         ptrdiff_t ilinesize, ptrdiff_t tlinesize,
+ *                         ptrdiff_t flinesize, ptrdiff_t slinesize,
+ *                         ptrdiff_t olinesize,
+ *                         int w, int h)
+*/
+func ff_threshold16_rvv, zve32x
+        lpad 0
+
+        lx   t0, 0(sp)         # slinesize
+        lx   t1, XSZ(sp)       # olinesize
+        lw   t2, (2 * XSZ)(sp) # w
+        lw   t3, (3 * XSZ)(sp) # h
+
+        slli t4, t2, 1
+
+        sub  a5, a5, t4
+        sub  a6, a6, t4
+        sub  a7, a7, t4
+        sub  t0, t0, t4
+        sub  t1, t1, t4
+1:
+        mv   t4, t2
+        addi t3, t3, -1
+2:
+        vsetvli t5, t4, e16, m8, ta, ma
+
+        vle16.v v8, (a0)
+        sub     t4, t4, t5
+        vle16.v v16, (a1)
+        slli    t6, t5, 1
+
+        vmsleu.vv v0, v8, v16
+        add     a0, a0, t6
+
+        vle16.v v8, (a2)
+        add     a1, a1, t6
+        vle16.v v24, (a3)
+        add     a2, a2, t6
+
+        vmerge.vvm v16, v24, v8, v0
+        add     a3, a3, t6
+
+        vse16.v v16, (a4)
+        add     a4, a4, t6
+
+        bnez    t4, 2b
+
+        add     a0, a0, a5
+        add     a1, a1, a6
+        add     a2, a2, a7
+        add     a3, a3, t0
+        add     a4, a4, t1
+
+        bnez    t3, 1b
+
+        ret
+endfunc
diff --git a/libavfilter/threshold.h b/libavfilter/threshold.h
index 8b55ad6ba1a..7ca71b851c7 100644
--- a/libavfilter/threshold.h
+++ b/libavfilter/threshold.h
@@ -47,5 +47,6 @@ typedef struct ThresholdContext {
 } ThresholdContext;
 
 void ff_threshold_init_x86(ThresholdContext *s);
+void ff_threshold_init_riscv(ThresholdContext *s);
 
 #endif /* AVFILTER_THRESHOLD_H */
diff --git a/libavfilter/vf_threshold_init.h b/libavfilter/vf_threshold_init.h
index fb319c6cf82..d5d746f53b6 100644
--- a/libavfilter/vf_threshold_init.h
+++ b/libavfilter/vf_threshold_init.h
@@ -87,6 +87,9 @@ av_unused static void ff_threshold_init(ThresholdContext *s)
 #if ARCH_X86 && HAVE_X86ASM
     ff_threshold_init_x86(s);
 #endif
+#if ARCH_RISCV
+    ff_threshold_init_riscv(s);
+#endif
 }
 
 #endif /* AVFILTER_THRESHOLD_INIT_H */
-- 
2.54.0

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

Reply via email to