PR #22738 opened by Deng-zewen
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22738
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22738.patch

This patch adds hand-optimized RISC-V Vector (Zve32x) code for
hevc_add_res.

All checkasm tests passed. Benchmarks were performed on Sophon SG2044 with 
native
FFmpeg timers:
hevc_add_res_4x4_8_c:        26.4 ( 1.00x)
hevc_add_res_4x4_8_rvv_i32:  22.9 ( 1.15x)
hevc_add_res_8x8_8_c:        88.1 ( 1.00x)
hevc_add_res_8x8_8_rvv_i32:  29.6 ( 2.98x)
hevc_add_res_16x16_8_c:      318.9 ( 1.00x)
hevc_add_res_16x16_8_rvv_i32: 50.2 ( 6.35x)
hevc_add_res_32x32_8_c:     1815.4 ( 1.00x)
hevc_add_res_32x32_8_rvv_i32:158.4 (11.46x)

Signed-off-by: Deng Zewen <[email protected]>


>From c9c46aebfe2010013bbc943b245de9db001c62b3 Mon Sep 17 00:00:00 2001
From: "deng.zewen" <[email protected]>
Date: Tue, 7 Apr 2026 17:23:23 +0800
Subject: [PATCH] libavcodec/riscv: add RVV optimized hevc_add_res

---
 libavcodec/riscv/Makefile           |   1 +
 libavcodec/riscv/hevcdsp_idct_rvv.S | 136 ++++++++++++++++++++++++++++
 libavcodec/riscv/hevcdsp_init.c     |  20 ++++
 3 files changed, 157 insertions(+)
 create mode 100644 libavcodec/riscv/hevcdsp_idct_rvv.S

diff --git a/libavcodec/riscv/Makefile b/libavcodec/riscv/Makefile
index 2c53334923..43aa0c907b 100644
--- a/libavcodec/riscv/Makefile
+++ b/libavcodec/riscv/Makefile
@@ -36,6 +36,7 @@ RVV-OBJS-$(CONFIG_H264DSP) += riscv/h264addpx_rvv.o 
riscv/h264dsp_rvv.o \
 OBJS-$(CONFIG_H264QPEL) += riscv/h264qpel_init.o
 RVV-OBJS-$(CONFIG_H264QPEL) += riscv/h264qpel_rvv.o
 OBJS-$(CONFIG_HEVC_DECODER) += riscv/hevcdsp_init.o
+OBJS-$(CONFIG_HEVC_DECODER) += riscv/hevcdsp_idct_rvv.o
 RVV-OBJS-$(CONFIG_HEVC_DECODER)  += riscv/h26x/h2656_inter_rvv.o
 OBJS-$(CONFIG_HUFFYUV_DECODER) += riscv/huffyuvdsp_init.o
 RVV-OBJS-$(CONFIG_HUFFYUV_DECODER) += riscv/huffyuvdsp_rvv.o
diff --git a/libavcodec/riscv/hevcdsp_idct_rvv.S 
b/libavcodec/riscv/hevcdsp_idct_rvv.S
new file mode 100644
index 0000000000..d709f7430c
--- /dev/null
+++ b/libavcodec/riscv/hevcdsp_idct_rvv.S
@@ -0,0 +1,136 @@
+/*
+ * Copyright (c) 2026 ZTE Corporation.
+ *
+ * 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_hevc_add_residual_4x4_8_rvv, zve32x
+        mv              t1, a0
+        add             t3, a0, a2
+        slli            a2, a2, 1
+        vsetivli        zero, 4, e8, mf4, ta, ma
+.rept 2
+        vle8.v          v2, (a0)
+        vle8.v          v3, (t3)
+
+        vsetivli        zero, 4, e16, mf2, ta, ma
+        vle16.v         v0, (a1)
+        addi            a1, a1, 8
+        vle16.v         v1, (a1)
+        addi            a1, a1, 8
+
+        vzext.vf2       v4, v2
+        vzext.vf2       v5, v3
+
+        vsadd.vv        v0, v0, v4
+        vsadd.vv        v1, v1, v5
+
+        vmax.vx         v0, v0, zero
+        vmax.vx         v1, v1, zero
+
+        vsetivli        zero, 4, e8, mf2, ta, ma
+        vnclipu.wi      v0, v0, 0
+        vnclipu.wi      v1, v1, 0
+
+        vse8.v          v0, (a0)
+        add             a0, a0, a2
+        vse8.v          v1, (t3)
+        add             t3, t3, a2
+.endr
+        ret
+endfunc
+
+func ff_hevc_add_residual_8x8_8_rvv, zve32x
+        add             t3, a0, a2
+        slli            a2, a2, 1
+        vsetivli        zero, 8, e8, mf2, ta, ma
+.rept 4
+        vle8.v          v2, (a0)
+        vle8.v          v3, (t3)
+
+        vsetivli        zero, 8, e16, m1, ta, ma
+        vle16.v         v0, (a1)
+        addi            a1, a1, 16
+        vle16.v         v1, (a1)
+        addi            a1, a1, 16
+
+        vzext.vf2       v4, v2
+        vzext.vf2       v5, v3
+
+        vsadd.vv        v0, v0, v4
+        vsadd.vv        v1, v1, v5
+
+        vmax.vx         v0, v0, zero
+        vmax.vx         v1, v1, zero
+
+        vsetivli        zero, 8, e8, mf2, ta, ma
+        vnclipu.wi      v0, v0, 0
+        vnclipu.wi      v1, v1, 0
+
+        vse8.v          v0, (a0)
+        add             a0, a0, a2
+        vse8.v          v1, (t3)
+        add             t3, t3, a2
+.endr
+        ret
+endfunc
+
+func ff_hevc_add_residual_16x16_8_rvv, zve32x
+        vsetivli        zero, 16, e8, m1, ta, ma
+.rept 16
+        vle8.v          v2, (a0)
+
+        vsetivli        zero, 16, e16, m2, ta, ma
+        vle16.v         v0, (a1)
+        addi            a1, a1, 32
+
+        vzext.vf2       v4, v2
+        vsadd.vv        v0, v0, v4
+        vmax.vx         v0, v0, zero
+        vsetivli        zero, 16, e8, m1, ta, ma
+        vnclipu.wi      v0, v0, 0
+
+        vse8.v          v0, (a0)
+        add             a0, a0, a2
+.endr
+        ret
+endfunc
+
+func ff_hevc_add_residual_32x32_8_rvv, zve32x
+        li              t0, 32
+        vsetvli         zero, t0, e8, m2, ta, ma
+.rept 32
+        vle8.v          v4, (a0)
+
+        vsetvli         zero, t0, e16, m4, ta, ma
+        vle16.v         v0, (a1)
+        addi            a1, a1, 64
+
+        vzext.vf2       v8, v4
+        vsadd.vv        v0, v0, v8
+        vmax.vx         v0, v0, zero
+        vsetvli         zero, t0, e8, m2, ta, ma
+        vnclipu.wi      v0, v0, 0
+
+        vse8.v          v0, (a0)
+        add             a0, a0, a2
+.endr
+        ret
+endfunc
+
diff --git a/libavcodec/riscv/hevcdsp_init.c b/libavcodec/riscv/hevcdsp_init.c
index 70bc8ebea7..bf1a1eb4c8 100644
--- a/libavcodec/riscv/hevcdsp_init.c
+++ b/libavcodec/riscv/hevcdsp_init.c
@@ -27,6 +27,15 @@
 #include "libavcodec/hevc/dsp.h"
 #include "libavcodec/riscv/h26x/h2656dsp.h"
 
+void ff_hevc_add_residual_4x4_8_rvv(uint8_t *_dst, const int16_t *coeffs,
+                                     ptrdiff_t stride);
+void ff_hevc_add_residual_8x8_8_rvv(uint8_t *_dst, const int16_t *coeffs,
+                                     ptrdiff_t stride);
+void ff_hevc_add_residual_16x16_8_rvv(uint8_t *_dst, const int16_t *coeffs,
+                                       ptrdiff_t stride);
+void ff_hevc_add_residual_32x32_8_rvv(uint8_t *_dst, const int16_t *coeffs,
+                                       ptrdiff_t stride);
+
 #define RVV_FNASSIGN(member, v, h, fn, ext) \
         member[1][v][h] = ff_h2656_put_pixels_##8_##ext;  \
         member[3][v][h] = ff_h2656_put_pixels_##8_##ext;  \
@@ -63,5 +72,16 @@ void ff_hevc_dsp_init_riscv(HEVCDSPContext *c, const int 
bit_depth)
                 break;
         }
     }
+
+    switch (bit_depth){
+        case 8:
+            c->add_residual[0]             = ff_hevc_add_residual_4x4_8_rvv;
+            c->add_residual[1]             = ff_hevc_add_residual_8x8_8_rvv;
+            c->add_residual[2]             = ff_hevc_add_residual_16x16_8_rvv;
+            c->add_residual[3]             = ff_hevc_add_residual_32x32_8_rvv;
+            break;
+        default:
+            break;
+    }
 #endif
 }
-- 
2.52.0

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

Reply via email to