From patchwork Sun Jun 30 09:04:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 50215 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a59:cc64:0:b0:482:c625:d099 with SMTP id k4csp1176033vqv; Sun, 30 Jun 2024 02:04:41 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUxEBpXIcVOOF099JdGVqmpxaMU2sL3ZV6/GroC871qj+xK7aZ5vEKpMOOtxJQc5GYmg5rN03KHz01Y6sL7bU18l5Zwohih45MfIA== X-Google-Smtp-Source: AGHT+IFu2a1hF903V8KVwl8SQhYe1Z2Jb37lZLdye71jd/W0C4oMja3bfjE+GMA/ArP0HT9HK8JB X-Received: by 2002:a17:907:7d8d:b0:a75:1923:eb2e with SMTP id a640c23a62f3a-a751923f7eamr192814166b.14.1719738281103; Sun, 30 Jun 2024 02:04:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719738281; cv=none; d=google.com; s=arc-20160816; b=RfdCC863HDHy+PG3Y08WiN5Bgc94rjhrkikQCcF6M0q+nzA+XTHkbGwfvdDfMAtWNy xR4PY6N+buRSyDRfXQRlq9VbxEiwzBc3glZduoz0XUrNxclEP1FiHg0euqC5+WOMQ7do m02qIgR1E0TcSfmRHONUJP2kvh6cm+5xkFaVUpzs0UlRRWY+iobpU3GoEMZLuUT+j/Mt l1Ox0iujSYZZt4Rl09+kzbRzObJgNTx1l79oEB2LARw2emG4PNGbRs+vwz0Bf6vst+jp QqPHoGw/6WmBmpmwaPmjxZo9j4nODP8AmSRLePc+kc1zjtuSJ7s728vW2RZcRkxjgEYQ 1pvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/9oxc4Zf91oAEl0Q6vsHnv8Hz+4kgCGjfFvLR2rUCPE=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=gFXbNaIROef5vdRogZQnPWNk7tS/9m8tTL3/rGEH4EG2DakHbUks/yPRHVRXdkIdE4 wqNBhpi6zGvL7aBlQYV4TMXALKHjnHL+ZV2iLf/xAp0NF1Lpx3Ki7YYkQp4048XCIZEc 1cCK6LhKQsReoY9XxP5I5qA8uzIT/OqBI0LDQCBWXVxhtcJz1C7nttZPgUkGPAkYX0go rkxpcww40j5XgPiOVISIX1wZAHvAwA3bZCZvv3NBwXXXNR4pv+Yic91kFzhdHt3TpjJo w8uiQP86iwPAUTBdr4u3FH3qYyFVqy2yN7Pe7ZRkC27+QOaoV6E3rSabwl+U93Yp2u0D ptxw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a640c23a62f3a-a72b27b286bsi214331266b.783.2024.06.30.02.04.40; Sun, 30 Jun 2024 02:04:41 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2E12168C0E3; Sun, 30 Jun 2024 12:04:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3CC2168C0E3 for ; Sun, 30 Jun 2024 12:04:31 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id A6C21C0145 for ; Sun, 30 Jun 2024 12:04:30 +0300 (EEST) From: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= To: ffmpeg-devel@ffmpeg.org Date: Sun, 30 Jun 2024 12:04:29 +0300 Message-ID: <20240630090430.65753-2-remi@remlab.net> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240630090430.65753-1-remi@remlab.net> References: <20240630090430.65753-1-remi@remlab.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavc/h264dsp: R-V V 8-bit luma loop filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: isDNV3VTGd0i T-Head C908 (cycles): h264_h_loop_filter_luma_8bpp_c: 297.5 h264_h_loop_filter_luma_8bpp_rvv_i32: 374.7 h264_v_loop_filter_luma_8bpp_c: 862.7 h264_v_loop_filter_luma_8bpp_rvv_i32: 200.7 Performance in the horizontal scenario seems worse than scalar. x86 SSE2 and AVX optimisations are similarly affected. This is presumably caused by unlucky inputs from checkasm, such that the C code short-circuits almost all filter calculations. --- libavcodec/riscv/Makefile | 1 + libavcodec/riscv/h264dsp_init.c | 13 ++- libavcodec/riscv/h264dsp_rvv.S | 136 ++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 libavcodec/riscv/h264dsp_rvv.S diff --git a/libavcodec/riscv/Makefile b/libavcodec/riscv/Makefile index c180223141..a1510e8c6e 100644 --- a/libavcodec/riscv/Makefile +++ b/libavcodec/riscv/Makefile @@ -31,6 +31,7 @@ RVV-OBJS-$(CONFIG_H263DSP) += riscv/h263dsp_rvv.o OBJS-$(CONFIG_H264CHROMA) += riscv/h264_chroma_init_riscv.o RVV-OBJS-$(CONFIG_H264CHROMA) += riscv/h264_mc_chroma.o OBJS-$(CONFIG_H264DSP) += riscv/h264dsp_init.o +RVV-OBJS-$(CONFIG_H264DSP) += riscv/h264dsp_rvv.o OBJS-$(CONFIG_HUFFYUV_DECODER) += riscv/huffyuvdsp_init.o RVV-OBJS-$(CONFIG_HUFFYUV_DECODER) += riscv/huffyuvdsp_rvv.o OBJS-$(CONFIG_IDCTDSP) += riscv/idctdsp_init.o diff --git a/libavcodec/riscv/h264dsp_init.c b/libavcodec/riscv/h264dsp_init.c index dbbf3db400..0d4d541992 100644 --- a/libavcodec/riscv/h264dsp_init.c +++ b/libavcodec/riscv/h264dsp_init.c @@ -24,8 +24,14 @@ #include "libavutil/attributes.h" #include "libavutil/cpu.h" +#include "libavutil/riscv/cpu.h" #include "libavcodec/h264dsp.h" +void ff_h264_v_loop_filter_luma_8_rvv(uint8_t *pix, ptrdiff_t stride, + int alpha, int beta, int8_t *tc0); +void ff_h264_h_loop_filter_luma_8_rvv(uint8_t *pix, ptrdiff_t stride, + int alpha, int beta, int8_t *tc0); + extern int ff_startcode_find_candidate_rvb(const uint8_t *, int); extern int ff_startcode_find_candidate_rvv(const uint8_t *, int); @@ -38,8 +44,13 @@ av_cold void ff_h264dsp_init_riscv(H264DSPContext *dsp, const int bit_depth, if (flags & AV_CPU_FLAG_RVB_BASIC) dsp->startcode_find_candidate = ff_startcode_find_candidate_rvb; # if HAVE_RVV - if (flags & AV_CPU_FLAG_RVV_I32) + if (flags & AV_CPU_FLAG_RVV_I32) { + if (bit_depth == 8 && ff_rv_vlen_least(128)) { + dsp->h264_v_loop_filter_luma = ff_h264_v_loop_filter_luma_8_rvv; + dsp->h264_h_loop_filter_luma = ff_h264_h_loop_filter_luma_8_rvv; + } dsp->startcode_find_candidate = ff_startcode_find_candidate_rvv; + } # endif #endif } diff --git a/libavcodec/riscv/h264dsp_rvv.S b/libavcodec/riscv/h264dsp_rvv.S new file mode 100644 index 0000000000..ea9dfb1a7e --- /dev/null +++ b/libavcodec/riscv/h264dsp_rvv.S @@ -0,0 +1,136 @@ +/* + * Copyright © 2024 Rémi Denis-Courmont. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "libavutil/riscv/asm.S" + + .variant_cc ff_h264_loop_filter_luma_8_rvv +func ff_h264_loop_filter_luma_8_rvv, zve32x + # p2: v8, p1: v9, p0: v10, q0: v11, q1: v12, q2: v13 + # alpha: a2, beta: a3 + csrwi vxrm, 0 + vid.v v0 + vaaddu.vv v14, v10, v11 # (p0 + q0 + 1) / 2 + vsrl.vi v0, v0, 2 # v0[i] = i / inner_iters + vwsubu.vv v16, v9, v12 + vrgather.vv v6, v4, v0 # tc_orig + vwaddu.vv v18, v8, v14 + vwaddu.vv v20, v13, v14 + vnsra.wi v24, v16, 2 # (p1 - q1) / 4 + vnsrl.wi v14, v18, 1 + vnsrl.wi v15, v20, 1 + vneg.v v5, v6 # -tc_orig + vwsubu.vv v22, v11, v10 # q0 - p0 + vwsubu.vv v18, v14, v9 + vwsubu.vv v20, v15, v12 + vwadd.wv v16, v22, v24 + vmsge.vi v7, v6, 0 # tc_orig >= 0 + vnclip.wi v14, v18, 0 + vnclip.wi v15, v20, 0 + vnclip.wi v16, v16, 1 # clip8((q0 - p0 + (p1 - q1) / 4 + 1) >> 1) + vmin.vv v14, v14, v6 + vmin.vv v15, v15, v6 + vmax.vv v14, v14, v5 # clip(p2 + ... - p1, +/-tc_orig) + vmax.vv v15, v15, v5 # clip(q2 + ... - q1, +/-tc_orig) + vwsubu.vv v20, v10, v11 + vwsubu.vv v24, v9, v10 + vwsubu.vv v26, v10, v9 + vwsubu.vv v28, v12, v11 + vwsubu.vv v30, v11, v12 + vwsubu.vv v0, v8, v10 + vwsubu.vv v2, v10, v8 + vwsubu.vv v4, v13, v11 + vwsubu.vv v18, v11, v13 + vsetvli zero, zero, e16, m2, ta, ma + vmax.vv v20, v20, v22 # abs(p0 - q0) + vmax.vv v24, v24, v26 # abs(p1 - p0) + vmax.vv v28, v28, v30 # abs(q1 - q0) + vmax.vv v22, v0, v2 # abs(p2 - p0) + vmax.vv v26, v4, v18 # abs(q2 - q0) + vmslt.vx v1, v20, a2 + vmslt.vx v2, v24, a3 + vmand.mm v7, v7, v1 + vmslt.vx v3, v28, a3 + vmand.mm v7, v7, v2 + vmslt.vx v0, v22, a3 + vmand.mm v7, v7, v3 # whether to update p0 and q0 + vmslt.vx v1, v26, a3 + vmand.mm v0, v0, v7 + vsetvli zero, zero, e8, m1, ta, mu + vadd.vi v6, v6, 1, v0.t # tc++ + vadd.vv v9, v9, v14, v0.t # p1' + vmand.mm v0, v1, v7 + vadd.vi v6, v6, 1, v0.t # tc++ + vadd.vv v12, v12, v15, v0.t # q1' + vneg.v v5, v6 # -tc + vmin.vv v16, v16, v6 + vwcvtu.x.x.v v18, v10 + vmax.vv v16, v16, v5 + vwcvtu.x.x.v v20, v11 + vwadd.wv v18, v18, v16 + vwsub.wv v20, v20, v16 + vmmv.m v0, v7 + vsetvli zero, zero, e16, m2, ta, ma + vmax.vx v18, v18, zero + vmax.vx v20, v20, zero + vsetvli zero, zero, e8, m1, ta, mu + vnclipu.wi v10, v18, 0, v0.t # p0' + vnclipu.wi v11, v20, 0, v0.t # q0' + jr t0 +endfunc + +func ff_h264_v_loop_filter_luma_8_rvv, zve32x + vsetivli zero, 4, e8, mf4, ta, ma + vle8.v v4, (a4) + sub t3, a0, a1 + vsetivli zero, 16, e8, m1, ta, ma + vle8.v v11, (a0) + sub t2, t3, a1 + vle8.v v10, (t3) + sub t1, t2, a1 + vle8.v v9, (t2) + add t5, a0, a1 + vle8.v v8, (t1) + add t6, t5, a1 + vle8.v v12, (t5) + vle8.v v13, (t6) + jal t0, ff_h264_loop_filter_luma_8_rvv + vse8.v v9, (t2) + vse8.v v10, (t3) + vse8.v v11, (a0) + vse8.v v12, (t5) + ret +endfunc + +func ff_h264_h_loop_filter_luma_8_rvv, zve32x + vsetivli zero, 4, e8, mf4, ta, ma + vle8.v v4, (a4) + addi a0, a0, -3 + vsetivli zero, 16, e8, m1, ta, ma + vlsseg6e8.v v8, (a0), a1 + jal t0, ff_h264_loop_filter_luma_8_rvv + vssseg6e8.v v8, (a0), a1 + ret +endfunc