From patchwork Tue Nov 9 07:09:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shiyou Yin X-Patchwork-Id: 31331 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp761224iob; Mon, 8 Nov 2021 23:09:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJxETmprhP8Ll7mWX+XAyC6pWSx7vPS6mxl1pv7DU0RPfNa9Zg00Nx8ChSS8LV4QPa5QCHYt X-Received: by 2002:a17:906:9744:: with SMTP id o4mr1899009ejy.322.1636441795469; Mon, 08 Nov 2021 23:09:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636441795; cv=none; d=google.com; s=arc-20160816; b=g9YgtZqdaqcR2kUm78EXdbnutYSqyCp1R+3wP+9oNMfjJmygOK5gWvd1x/J5LTyb4W H83tQAByvpz3q8n3WDqVku9rRvod2bp/LcwGERobFg03k9VUg788AiXd3sJR2rkgOJa0 LxxvrKcvqpI8FJREojTV0T1Hf3uhKtMbo5ywEs9Yhq+48KCrf+dj//c3TcX7pjHkcrCj cpUWL7xOPsc0DS5bpaMhTUah3Iyw11PpjKEkBbfFV4+/oNP/2hesbvnCvP/8mDDcaSo8 sHS0kTyhecO4me5A4BpiJWri4m3VCkLZ9A0U0jRAbb51xloSuGmSXzjd1OKwC9ZU+ViW 99Mg== 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=UqQMbr4FpHuyJpncWGB0ifuHptIimvyG1PnCzOH0GJI=; b=VAteRN//WcJX6j2pocidO1brs1OCFZQAZejWM7k3MT3QkPRfg9k0jPoCt3ayQuoYw0 0H5DwyGwStPa9FzQbeP6KYadUb61L3S2wlTZNM3VS6nxbuxVpaKPHE1vQb8H854PoKR4 paaKilCisoYvmRey4qnH3cDMoBx7Vz2x2KKRC6luUXInX0Hv4vlgrZnbtrejL40khFgJ 2j1qnroUVo5VkZSurUgFYV8mv7C4SRFf7Xq/CK8c9r28C8wauwXra6zSOX7oz0Hjln5F zzCtk9Xug/5hKR8Qpuk5Tu4rzVUKmDNeoSq03nX1qJZ5C5pBdKNRE1ZNn/RurtaiMCqJ sPMw== 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 sd30si39469851ejc.764.2021.11.08.23.09.53; Mon, 08 Nov 2021 23:09:55 -0800 (PST) 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 D3387687FB5; Tue, 9 Nov 2021 09:09:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E67DF689D14 for ; Tue, 9 Nov 2021 09:09:17 +0200 (EET) Received: from localhost (unknown [36.33.26.144]) by mail.loongson.cn (Coremail) with SMTP id AQAAf9CxKdGaHophZXwBAA--.3453S3; Tue, 09 Nov 2021 15:09:14 +0800 (CST) From: Shiyou Yin To: ffmpeg-devel@ffmpeg.org Date: Tue, 9 Nov 2021 15:09:09 +0800 Message-Id: <20211109070910.4447-3-yinshiyou@loongson.cn> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20211109070910.4447-1-yinshiyou@loongson.cn> References: <20211109070910.4447-1-yinshiyou@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf9CxKdGaHophZXwBAA--.3453S3 X-Coremail-Antispam: 1UD129KBjvJXoW3ZFy5WFy3KF4fJw1DuF17KFg_yoWDWr4xpF y8KrsYyr4SvF47ursxGF48Kr92kF40qr18GFy5tF1jvrs8tr48urWkGryxWF15GFykCFya vF1UZFy3Cr17XaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUkIb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rw A2F7IY1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xII jxv20xvEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4 vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xv F2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jrv_JF1lYx0Ex4A2jsIE14v26F4j6r 4UJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc2xSY4AK67AK6r4xMxAI w28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr 4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUJVWUXwCIc40Y0x0EwIxG rwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8Jw CI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2 z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU5D3vUUUUUU== X-CM-SenderInfo: p1lq2x5l1r3gtki6z05rqj20fqof0/ Subject: [FFmpeg-devel] [PATCH 2/3] avcodec: [loongarch] optimize get_cabac. 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: M4TvBsUCh8Tx Decoding 1080P H264 on 2.5Ghz 3A5000: 165fps==>168fps. Testing command: ffmpeg -i ***.mp4 -f rawvideo -y /dev/null -an --- libavcodec/cabac_functions.h | 3 + libavcodec/loongarch/cabac.h | 254 +++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 libavcodec/loongarch/cabac.h diff --git a/libavcodec/cabac_functions.h b/libavcodec/cabac_functions.h index 46af921822..2f2d48a8f8 100644 --- a/libavcodec/cabac_functions.h +++ b/libavcodec/cabac_functions.h @@ -49,6 +49,9 @@ #if ARCH_MIPS # include "mips/cabac.h" #endif +#if ARCH_LOONGARCH64 +# include "loongarch/cabac.h" +#endif static const uint8_t * const ff_h264_norm_shift = ff_h264_cabac_tables + H264_NORM_SHIFT_OFFSET; static const uint8_t * const ff_h264_lps_range = ff_h264_cabac_tables + H264_LPS_RANGE_OFFSET; diff --git a/libavcodec/loongarch/cabac.h b/libavcodec/loongarch/cabac.h new file mode 100644 index 0000000000..71e8ba3be7 --- /dev/null +++ b/libavcodec/loongarch/cabac.h @@ -0,0 +1,254 @@ +/* + * Loongson optimized cabac + * + * Copyright (c) 2020 Loongson Technology Corporation Limited + * Contributed by Shiyou Yin + * Gu Xiwei(guxiwei-hf@loongson.cn) + * + * 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 + */ + +#ifndef AVCODEC_LOONGARCH_CABAC_H +#define AVCODEC_LOONGARCH_CABAC_H + +#include "libavcodec/cabac.h" +#include "config.h" + +#define GET_CABAC_LOONGARCH_UNCBSR \ + "ld.bu %[bit], %[state], 0x0 \n\t" \ + "andi %[tmp0], %[c_range], 0xC0 \n\t" \ + "slli.d %[tmp0], %[tmp0], 0x01 \n\t" \ + "add.d %[tmp0], %[tmp0], %[tables] \n\t" \ + "add.d %[tmp0], %[tmp0], %[bit] \n\t" \ + /* tmp1: RangeLPS */ \ + "ld.bu %[tmp1], %[tmp0], %[lps_off] \n\t" \ + \ + "sub.d %[c_range], %[c_range], %[tmp1] \n\t" \ + "slli.d %[tmp0], %[c_range], 0x11 \n\t" \ + "bge %[tmp0], %[c_low], 1f \n\t" \ + "move %[c_range], %[tmp1] \n\t" \ + "nor %[bit], %[bit], %[bit] \n\t" \ + "sub.d %[c_low], %[c_low], %[tmp0] \n\t" \ + \ + "1: \n\t" \ + /* tmp1: *state */ \ + "add.d %[tmp0], %[tables], %[bit] \n\t" \ + "ld.bu %[tmp1], %[tmp0], %[mlps_off] \n\t" \ + /* tmp2: lps_mask */ \ + "add.d %[tmp0], %[tables], %[c_range] \n\t" \ + "ld.bu %[tmp2], %[tmp0], %[norm_off] \n\t" \ + \ + "andi %[bit], %[bit], 0x01 \n\t" \ + "st.b %[tmp1], %[state], 0x0 \n\t" \ + "sll.d %[c_range], %[c_range], %[tmp2] \n\t" \ + "sll.d %[c_low], %[c_low], %[tmp2] \n\t" \ + \ + "and %[tmp1], %[c_low], %[cabac_mask] \n\t" \ + "bnez %[tmp1], 1f \n\t" \ + "ld.hu %[tmp1], %[c_bytestream], 0x0 \n\t" \ + "addi.d %[tmp0], %[c_low], -0X01 \n\t" \ + "xor %[tmp0], %[c_low], %[tmp0] \n\t" \ + "srai.d %[tmp0], %[tmp0], 0x0f \n\t" \ + "add.d %[tmp0], %[tmp0], %[tables] \n\t" \ + /* tmp2: ff_h264_norm_shift[x >> (CABAC_BITS - 1)] */ \ + "ld.bu %[tmp2], %[tmp0], %[norm_off] \n\t" \ + \ + "revb.2h %[tmp0], %[tmp1] \n\t" \ + "slli.d %[tmp0], %[tmp0], 0x01 \n\t" \ + "sub.d %[tmp0], %[tmp0], %[cabac_mask] \n\t" \ + \ + "li.d %[tmp1], 0x07 \n\t" \ + "sub.d %[tmp1], %[tmp1], %[tmp2] \n\t" \ + "sll.d %[tmp0], %[tmp0], %[tmp1] \n\t" \ + "add.d %[c_low], %[c_low], %[tmp0] \n\t" \ + \ + "addi.d %[c_bytestream], %[c_bytestream], 0x02 \n\t" \ + "1: \n\t" \ + +#define GET_CABAC_LOONGARCH \ + "ld.bu %[bit], %[state], 0x0 \n\t" \ + "andi %[tmp0], %[c_range], 0xC0 \n\t" \ + "slli.d %[tmp0], %[tmp0], 0x01 \n\t" \ + "add.d %[tmp0], %[tmp0], %[tables] \n\t" \ + "add.d %[tmp0], %[tmp0], %[bit] \n\t" \ + /* tmp1: RangeLPS */ \ + "ld.bu %[tmp1], %[tmp0], %[lps_off] \n\t" \ + \ + "sub.d %[c_range], %[c_range], %[tmp1] \n\t" \ + "slli.d %[tmp0], %[c_range], 0x11 \n\t" \ + "bge %[tmp0], %[c_low], 1f \n\t" \ + "move %[c_range], %[tmp1] \n\t" \ + "nor %[bit], %[bit], %[bit] \n\t" \ + "sub.d %[c_low], %[c_low], %[tmp0] \n\t" \ + \ + "1: \n\t" \ + /* tmp1: *state */ \ + "add.d %[tmp0], %[tables], %[bit] \n\t" \ + "ld.bu %[tmp1], %[tmp0], %[mlps_off] \n\t" \ + /* tmp2: lps_mask */ \ + "add.d %[tmp0], %[tables], %[c_range] \n\t" \ + "ld.bu %[tmp2], %[tmp0], %[norm_off] \n\t" \ + \ + "andi %[bit], %[bit], 0x01 \n\t" \ + "st.b %[tmp1], %[state], 0x0 \n\t" \ + "sll.d %[c_range], %[c_range], %[tmp2] \n\t" \ + "sll.d %[c_low], %[c_low], %[tmp2] \n\t" \ + \ + "and %[tmp1], %[c_low], %[cabac_mask] \n\t" \ + "bnez %[tmp1], 1f \n\t" \ + "ld.hu %[tmp1], %[c_bytestream], 0x0 \n\t" \ + "addi.d %[tmp0], %[c_low], -0X01 \n\t" \ + "xor %[tmp0], %[c_low], %[tmp0] \n\t" \ + "srai.d %[tmp0], %[tmp0], 0x0f \n\t" \ + "add.d %[tmp0], %[tmp0], %[tables] \n\t" \ + /* tmp2: ff_h264_norm_shift[x >> (CABAC_BITS - 1)] */ \ + "ld.bu %[tmp2], %[tmp0], %[norm_off] \n\t" \ + \ + "revb.2h %[tmp0], %[tmp1] \n\t" \ + "slli.d %[tmp0], %[tmp0], 0x01 \n\t" \ + "sub.d %[tmp0], %[tmp0], %[cabac_mask] \n\t" \ + \ + "li.d %[tmp1], 0x07 \n\t" \ + "sub.d %[tmp1], %[tmp1], %[tmp2] \n\t" \ + "sll.d %[tmp0], %[tmp0], %[tmp1] \n\t" \ + "add.d %[c_low], %[c_low], %[tmp0] \n\t" \ + \ + "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t" \ + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" \ + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" \ + "1: \n\t" \ + +#define get_cabac_inline get_cabac_inline_loongarch +static av_always_inline +int get_cabac_inline_loongarch(CABACContext *c, uint8_t * const state) +{ + int64_t tmp0, tmp1, tmp2, bit; + + __asm__ volatile ( +#if UNCHECKED_BITSTREAM_READER + GET_CABAC_LOONGARCH_UNCBSR +#else + GET_CABAC_LOONGARCH +#endif + : [bit]"=&r"(bit), [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2), + [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), + [c_bytestream]"+&r"(c->bytestream) + : [state]"r"(state), [tables]"r"(ff_h264_cabac_tables), +#if !UNCHECKED_BITSTREAM_READER + [c_bytestream_end]"r"(c->bytestream_end), +#endif + [lps_off]"i"(H264_LPS_RANGE_OFFSET), + [mlps_off]"i"(H264_MLPS_STATE_OFFSET + 128), + [norm_off]"i"(H264_NORM_SHIFT_OFFSET), + [cabac_mask]"r"(CABAC_MASK) + : "memory" + ); + + return bit; +} + +#define get_cabac_bypass get_cabac_bypass_loongarch +static av_always_inline int get_cabac_bypass_loongarch(CABACContext *c) +{ + int64_t tmp0, tmp1, tmp2; + int res = 0; + __asm__ volatile( + "slli.d %[c_low], %[c_low], 0x01 \n\t" + "and %[tmp0], %[c_low], %[cabac_mask] \n\t" + "bnez %[tmp0], 1f \n\t" + "ld.hu %[tmp1], %[c_bytestream], 0x0 \n\t" +#if UNCHECKED_BITSTREAM_READER + "addi.d %[c_bytestream], %[c_bytestream], 0x02 \n\t" +#else + "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t" + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" +#endif + "revb.2h %[tmp1], %[tmp1] \n\t" + "slli.d %[tmp1], %[tmp1], 0x01 \n\t" + "sub.d %[tmp1], %[tmp1], %[cabac_mask] \n\t" + "add.d %[c_low], %[c_low], %[tmp1] \n\t" + "1: \n\t" + "slli.d %[tmp1], %[c_range], 0x11 \n\t" + "slt %[tmp0], %[c_low], %[tmp1] \n\t" + "sub.d %[tmp1], %[c_low], %[tmp1] \n\t" + "masknez %[tmp2], %[one], %[tmp0] \n\t" + "maskeqz %[res], %[res], %[tmp0] \n\t" + "or %[res], %[res], %[tmp2] \n\t" + "masknez %[tmp2], %[tmp1], %[tmp0] \n\t" + "maskeqz %[c_low], %[c_low], %[tmp0] \n\t" + "or %[c_low], %[c_low], %[tmp2] \n\t" + : [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [tmp2]"=&r"(tmp2), + [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), + [c_bytestream]"+&r"(c->bytestream), [res]"+&r"(res) + : [cabac_mask]"r"(CABAC_MASK), +#if !UNCHECKED_BITSTREAM_READER + [c_bytestream_end]"r"(c->bytestream_end), +#endif + [one]"r"(0x01) + : "memory" + ); + return res; +} + +#define get_cabac_bypass_sign get_cabac_bypass_sign_loongarch +static av_always_inline +int get_cabac_bypass_sign_loongarch(CABACContext *c, int val) +{ + int64_t tmp0, tmp1; + int res = val; + __asm__ volatile( + "slli.d %[c_low], %[c_low], 0x01 \n\t" + "and %[tmp0], %[c_low], %[cabac_mask] \n\t" + "bnez %[tmp0], 1f \n\t" + "ld.hu %[tmp1], %[c_bytestream], 0x0 \n\t" +#if UNCHECKED_BITSTREAM_READER + "addi.d %[c_bytestream], %[c_bytestream], 0x02 \n\t" +#else + "slt %[tmp0], %[c_bytestream], %[c_bytestream_end] \n\t" + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" + "add.d %[c_bytestream], %[c_bytestream], %[tmp0] \n\t" +#endif + "revb.2h %[tmp1], %[tmp1] \n\t" + "slli.d %[tmp1], %[tmp1], 0x01 \n\t" + "sub.d %[tmp1], %[tmp1], %[cabac_mask] \n\t" + "add.d %[c_low], %[c_low], %[tmp1] \n\t" + "1: \n\t" + "slli.d %[tmp1], %[c_range], 0x11 \n\t" + "slt %[tmp0], %[c_low], %[tmp1] \n\t" + "sub.d %[tmp1], %[c_low], %[tmp1] \n\t" + "masknez %[tmp1], %[tmp1], %[tmp0] \n\t" + "maskeqz %[c_low], %[c_low], %[tmp0] \n\t" + "or %[c_low], %[c_low], %[tmp1] \n\t" + "sub.d %[tmp1], %[zero], %[res] \n\t" + "maskeqz %[tmp1], %[tmp1], %[tmp0] \n\t" + "masknez %[res], %[res], %[tmp0] \n\t" + "or %[res], %[res], %[tmp1] \n\t" + : [tmp0]"=&r"(tmp0), [tmp1]"=&r"(tmp1), [res]"+&r"(res), + [c_range]"+&r"(c->range), [c_low]"+&r"(c->low), + [c_bytestream]"+&r"(c->bytestream) + : [cabac_mask]"r"(CABAC_MASK), +#if !UNCHECKED_BITSTREAM_READER + [c_bytestream_end]"r"(c->bytestream_end), +#endif + [zero]"r"(0x0) + : "memory" + ); + + return res; +} +#endif /* AVCODEC_LOONGARCH_CABAC_H */