PR #21170 opened by Rémi Denis-Courmont (Courmisch)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21170
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21170.patch


From 1e16298e659794b93eb81dec6ccae9a94d7b6bbe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <[email protected]>
Date: Thu, 11 Dec 2025 18:58:37 +0200
Subject: [PATCH 1/2] lavc/llvidencdsp: R-V B sub_median_pred

SiFive U74:
sub_median_pred_c:                                  238947.3 ( 1.00x)
sub_median_pred_rvb_b:                              106686.9 ( 2.24x)

SpacemiT X60:
sub_median_pred_c:                                  297862.8 ( 1.00x)
sub_median_pred_rvb_b:                              101992.2 ( 2.92x)
---
 libavcodec/riscv/Makefile           |  1 +
 libavcodec/riscv/llvidencdsp_init.c | 13 +++++++-
 libavcodec/riscv/llvidencdsp_rvb.S  | 49 +++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/riscv/llvidencdsp_rvb.S

diff --git a/libavcodec/riscv/Makefile b/libavcodec/riscv/Makefile
index 38021f873f..20f6bc73d6 100644
--- a/libavcodec/riscv/Makefile
+++ b/libavcodec/riscv/Makefile
@@ -46,6 +46,7 @@ RVV-OBJS-$(CONFIG_LLAUDDSP) += riscv/llauddsp_rvv.o
 OBJS-$(CONFIG_LLVIDDSP) += riscv/llviddsp_init.o
 RVV-OBJS-$(CONFIG_LLVIDDSP) += riscv/llviddsp_rvv.o
 OBJS-$(CONFIG_LLVIDENCDSP) += riscv/llvidencdsp_init.o
+RV-OBJS-$(CONFIG_LLVIDDSP) += riscv/llvidencdsp_rvb.o
 RVV-OBJS-$(CONFIG_LLVIDENCDSP) += riscv/llvidencdsp_rvv.o
 OBJS-$(CONFIG_LPC) += riscv/lpc_init.o
 RVV-OBJS-$(CONFIG_LPC) += riscv/lpc_rvv.o
diff --git a/libavcodec/riscv/llvidencdsp_init.c 
b/libavcodec/riscv/llvidencdsp_init.c
index bd2ffef42f..75f8438ab4 100644
--- a/libavcodec/riscv/llvidencdsp_init.c
+++ b/libavcodec/riscv/llvidencdsp_init.c
@@ -24,6 +24,12 @@
 #include "libavutil/cpu.h"
 #include "libavcodec/lossless_videoencdsp.h"
 
+#include "libavcodec/mathops.h"
+#include <sys/param.h>
+
+void ff_llvidenc_sub_median_pred_rvb(uint8_t *dst, const uint8_t *src1,
+                                     const uint8_t *src2, intptr_t w,
+                                     int *left, int *left_top);
 void ff_llvidenc_diff_bytes_rvv(uint8_t *dst, const uint8_t *src1,
                                 const uint8_t *src2, intptr_t w);
 void ff_llvidenc_sub_left_predict_rvv(uint8_t *dst, const uint8_t *src,
@@ -32,12 +38,17 @@ void ff_llvidenc_sub_left_predict_rvv(uint8_t *dst, const 
uint8_t *src,
 
 av_cold void ff_llvidencdsp_init_riscv(LLVidEncDSPContext *c)
 {
-#if HAVE_RVV
+#if HAVE_RV
     int flags = av_get_cpu_flags();
 
+    if (flags & AV_CPU_FLAG_RVB_BASIC)
+        c->sub_median_pred = ff_llvidenc_sub_median_pred_rvb;
+
+#if HAVE_RVV
     if (flags & AV_CPU_FLAG_RVV_I32) {
         c->diff_bytes = ff_llvidenc_diff_bytes_rvv;
         c->sub_left_predict = ff_llvidenc_sub_left_predict_rvv;
     }
 #endif
+#endif
 }
diff --git a/libavcodec/riscv/llvidencdsp_rvb.S 
b/libavcodec/riscv/llvidencdsp_rvb.S
new file mode 100644
index 0000000000..d34aef394e
--- /dev/null
+++ b/libavcodec/riscv/llvidencdsp_rvb.S
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2025 Rémi Denis-Courmont.
+ *
+ * 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"
+
+func ff_llvidenc_sub_median_pred_rvb, zbb
+        lpad    0
+        lbu     t4, (a4)    # l
+        lbu     t5, (a5)    # lt
+        add     a3, a0, a3
+1:
+        lbu     t1, (a1)    # src1
+        addi    a1, a1, 1
+        add     t6, t4, t1
+        max     t3, t4, t1
+        sub     t6, t6, t5  # l + src1 - lt
+        min     t0, t4, t1
+        andi    t6, t6, 0xff
+        mv      t5, t1
+        min     t3, t3, t6
+        lbu     t4, (a2)    # src2
+        addi    a0, a0, 1
+        max     t6, t0, t3  # mid_pred(...)
+        sub     t0, t4, t6
+        addi    a2, a2, 1
+        sb      t0, -1(a0)
+        bne     a0, a3, 1b
+
+        sw      t4, (a4)
+        sw      t5, (a5)
+        ret
+endfunc
-- 
2.49.1


From 7b90a718d8206e56711c3287fcf83c845c6a22a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <[email protected]>
Date: Thu, 11 Dec 2025 17:24:28 +0200
Subject: [PATCH 2/2] lavc/llvidencdsp: R-V V sub_median_pred

SpacemiT X60:
sub_median_pred_c:                                  297862.8 ( 1.00x)
sub_median_pred_rvb_b:                              101992.2 ( 2.92x)
sub_median_pred_rvv_i32:                              4820.0 (61.80x)
---
 libavcodec/riscv/llvidencdsp_init.c |  4 ++++
 libavcodec/riscv/llvidencdsp_rvv.S  | 31 +++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/libavcodec/riscv/llvidencdsp_init.c 
b/libavcodec/riscv/llvidencdsp_init.c
index 75f8438ab4..017aa778d9 100644
--- a/libavcodec/riscv/llvidencdsp_init.c
+++ b/libavcodec/riscv/llvidencdsp_init.c
@@ -32,6 +32,9 @@ void ff_llvidenc_sub_median_pred_rvb(uint8_t *dst, const 
uint8_t *src1,
                                      int *left, int *left_top);
 void ff_llvidenc_diff_bytes_rvv(uint8_t *dst, const uint8_t *src1,
                                 const uint8_t *src2, intptr_t w);
+void ff_llvidenc_sub_median_pred_rvv(uint8_t *dst, const uint8_t *src1,
+                                     const uint8_t *src2, intptr_t width,
+                                     int *left, int *left_top);
 void ff_llvidenc_sub_left_predict_rvv(uint8_t *dst, const uint8_t *src,
                                       ptrdiff_t stride, ptrdiff_t width,
                                       int height);
@@ -47,6 +50,7 @@ av_cold void ff_llvidencdsp_init_riscv(LLVidEncDSPContext *c)
 #if HAVE_RVV
     if (flags & AV_CPU_FLAG_RVV_I32) {
         c->diff_bytes = ff_llvidenc_diff_bytes_rvv;
+        c->sub_median_pred = ff_llvidenc_sub_median_pred_rvv;
         c->sub_left_predict = ff_llvidenc_sub_left_predict_rvv;
     }
 #endif
diff --git a/libavcodec/riscv/llvidencdsp_rvv.S 
b/libavcodec/riscv/llvidencdsp_rvv.S
index a862b776e0..068f36aea3 100644
--- a/libavcodec/riscv/llvidencdsp_rvv.S
+++ b/libavcodec/riscv/llvidencdsp_rvv.S
@@ -37,6 +37,37 @@ func ff_llvidenc_diff_bytes_rvv, zve32x
         ret
 endfunc
 
+func ff_llvidenc_sub_median_pred_rvv, zve32x
+        lpad    0
+        lw      t4, (a4)
+        lw      t5, (a5)
+1:
+        vsetvli t3, a3, e8, m4, ta, ma
+        vle8.v  v16, (a1)            # src1
+        sub     a3, a3, t3
+        vle8.v  v24, (a2)            # src2
+        add     a1, t3, a1
+        vslide1up.vx    v20, v16, t5 # lt
+        add     a2, t3, a2
+        vslide1up.vx    v28, v24, t4 # l
+        lbu     t5, -1(a1)
+        vsub.vv     v20, v16, v20
+        lbu     t4, -1(a2)
+        vmaxu.vv    v8, v28, v16
+        vadd.vv     v20, v28, v20    # l + src1 - lt
+        vminu.vv    v12, v28, v16
+        vminu.vv    v8, v8, v20
+        vmaxu.vv    v8, v12, v8      # mid_pred(...)
+        vsub.vv     v8, v24, v8
+        vse8.v  v8, (a0)
+        add     a0, t3, a0
+        bnez    a3, 1b
+
+        sw      t4, (a4)
+        sw      t5, (a5)
+        ret
+endfunc
+
 func ff_llvidenc_sub_left_predict_rvv, zve32x
         lpad    0
         li      a5, -0x80
-- 
2.49.1

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

Reply via email to