From patchwork Thu Oct 22 10:20:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23147 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 1A0F144B6E5 for ; Thu, 22 Oct 2020 13:20:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D633A68B32D; Thu, 22 Oct 2020 13:20:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DBC5668A466 for ; Thu, 22 Oct 2020 13:20:14 +0300 (EEST) Received: by mail-ej1-f67.google.com with SMTP id k3so1457734ejj.10 for ; Thu, 22 Oct 2020 03:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7L/gIumZvCeiMPXMDRlpZ8BhGroDnNrKujflHK3n76Q=; b=mOteXX/qelMPCh7+RmyhLMfG5zbTaN+UcjROKHNzDatyPKFb5B0KANkdFcz50J6iBT Ri2e0yvSCfkSX/yzKjhpQZFd4mqrp+lfvGQpP8BllrohlN6XWbPgYOz/UTLU/6QXpMMC pmc6hX9Qa9Q2evj88E8xr87YqGm6F+0cZWeE9NdXVY6amkxfp59xiygvCdzB8rvuqpLm v3AUp8legqxnAU+h1ijWOqvbRpIuUsUUqyeP6NLmiNnS85w12Wh3HAtg12hA2xXXa9EH hUz5ACk4N53TpWyx/fX9adWMq07A/cP6isHcJDTrNICmdrb5Esdnf3SoeqAw4F7wr7yr vzOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=7L/gIumZvCeiMPXMDRlpZ8BhGroDnNrKujflHK3n76Q=; b=GiP2gE8OHPaAskMpRv3PkWPIgLbpxJopEjvpyy2zNI4rZDjGYl5IW+VqQZ34I1cnA5 5Vhi6whQ/BKBYz9568ax6Fzm4aVihJbzrJwU3xvkCZ4L8k+o2f1sCpz+zNC1OXau/twI aliPn5jkFt8bYd8lAwqm/vWIg30N98WQiunr6ZVwrv6QabCb0FYSNt5TpMhrjRuQFfKB itZBVt2nODY9vdbhFRG3LA8MOsDT6AVnICU4NrmNfqjh2A9+r6h1TvOPuuATMQDgQT8w bb+YR09V1sn1lhh5l5UA9czuXKMGHjnJwFW6jLWWo3JerMLacIW1c/Z+Litlh/49OaDJ f0mw== X-Gm-Message-State: AOAM532HDs1TkLsbVwVawHRa6P6gG7hDRADVQXsV/TXyf3oHUuKvTcdd DjXIR6S4J0Qj34JScBGK1K8FfHGh5w0= X-Google-Smtp-Source: ABdhPJyDDs87Ntn1uJRcgd7RM8sJAQIYQ3vXpI2XXOPOwkw5bVOb6VHaF3GtF767xpJyCiDQRJOiRw== X-Received: by 2002:a17:907:2662:: with SMTP id ci2mr1486893ejc.554.1603362013949; Thu, 22 Oct 2020 03:20:13 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id p16sm582746ejz.103.2020.10.22.03.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 03:20:13 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Oct 2020 12:20:05 +0200 Message-Id: <20201022102006.571413-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/rv34: Don't needlessly copy VLC length and symbol arrays X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Most of the VLCs used by RealVideo 3 and 4 obey three simple rules: Shorter codes are on the left of the tree, for each length, the symbols are ascending from left to right and the symbols either form a permutation of 1..size or 0..(size - 1). For the latter case, one just needs to store the length of each symbol and create the codes according to the other rules; no explicit code or symbol array must be stored. The former case is also treated in much the same way by artificially assigning a length of zero to the symbol 0; when a length of zero was encountered, the element was ignored except that the symbol counter was still incremented. If the length was nonzero, the symbol would be assigned via the symbol counter and the length copied over into a new array. Yet this is unnecessary, as ff_init_vlc_sparse() follows exactly the same pattern: If a length of zero is encountered, the element is ignored and only the symbol counter incremented. So one can directly forward the length array and also need not create a symbol table oneself, because ff_init_vlc_sparse() will infer the same symbol table in this case. Signed-off-by: Andreas Rheinhardt --- libavcodec/rv34.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index ec0cd27916..d251c6817c 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -106,37 +106,33 @@ static VLC_TYPE table_data[117592][2]; * @param insyms symbols for input codes (NULL for default ones) * @param num VLC table number (for static initialization) */ -static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms, +static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *syms, const int num) { - int i; int counts[17] = {0}, codes[17]; - uint16_t cw[MAX_VLC_SIZE], syms[MAX_VLC_SIZE]; - uint8_t bits2[MAX_VLC_SIZE]; - int maxbits = 0, realsize = 0; - - for(i = 0; i < size; i++){ - if(bits[i]){ - bits2[realsize] = bits[i]; - syms[realsize] = insyms ? insyms[i] : i; - realsize++; - maxbits = FFMAX(maxbits, bits[i]); - counts[bits[i]]++; - } - } + uint16_t cw[MAX_VLC_SIZE]; + int maxbits; - codes[0] = 0; - for(i = 0; i < 16; i++) + for (int i = 0; i < size; i++) + counts[bits[i]]++; + + /* bits[0] is zero for some tables, i.e. syms actually starts at 1. + * So we reset it here. The code assigned to this element is 0x00. */ + codes[0] = counts[0] = 0; + for (int i = 0; i < 16; i++) { codes[i+1] = (codes[i] + counts[i]) << 1; - for(i = 0; i < realsize; i++) - cw[i] = codes[bits2[i]]++; + if (counts[i]) + maxbits = i; + } + for (int i = 0; i < size; i++) + cw[i] = codes[bits[i]]++; vlc->table = &table_data[table_offs[num]]; vlc->table_allocated = table_offs[num + 1] - table_offs[num]; - ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize, - bits2, 1, 1, + ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), size, + bits, 1, 1, cw, 2, 2, - syms, 2, 2, INIT_VLC_USE_NEW_STATIC); + syms, !!syms, !!syms, INIT_VLC_USE_NEW_STATIC); } /** From patchwork Thu Oct 22 10:20:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23148 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 43BB54498B0 for ; Thu, 22 Oct 2020 13:20:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2B31368A98A; Thu, 22 Oct 2020 13:20:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 867606880F2 for ; Thu, 22 Oct 2020 13:20:32 +0300 (EEST) Received: by mail-ed1-f53.google.com with SMTP id bc23so1200382edb.5 for ; Thu, 22 Oct 2020 03:20:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YZ3qy0oOFyBSSZQApHD6+wvdee6nemVasFSpkJeyCXk=; b=PNrwnQzBURwCUtiVPSe0PMINh5vbr/s9eKxsUweH3SbbV3IXG7zykRGU/plQPy2ij/ He2KGhb1fPpDL1gBhx2mPMGIvpUcSPLnXJuRuwtbdZWPTirKxF2Gk7aF/gIUL+iYKGJ2 KJ8VE0bNopRkeC85lJPVUTOJ8+9VpKbr19QHES+atW7aljU5mIrCMSf9+RHkB5rB4r4J OLwCbOFvGveV/iu6XBjq1mkvx48+HTqgi5DsHRimTRpbtX6TZPYLvG/MNehp3v7F1mBw KEfd6NK5Imite1XpVnyUmP1pmWGt6fvlHsyYIWyApRQboL/sdPZCko4pFQJqtk/NAjrA WPvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YZ3qy0oOFyBSSZQApHD6+wvdee6nemVasFSpkJeyCXk=; b=YBaDosPjOEEpFMTRhV6YYoM0DZ0eUmborFimApEiIiLpsn6ufZThIdTLqWOzxwEs3Y PE0ub9+W7BeGRLXTcOx4Xyn7kAy5EEvHrY+LpN4YcrYGRpK3KQBgfB5+MPG8Bkzn6pSX EZXcIov2dqy7PvWJnRWAkq9cZgM5xrmDvE84rbcCqUL+5tH24pPoP91MF5CKIvvsxko7 Hy+nNAVmarLb134GU924QEhyCHCrpIILg51k1sjXJeXZJmz1WydbmbxDhoMF1lxFHQz6 lf+zVJ06+zDf5/bM6LfL1NMQi7wNe4XtIaNpa89nRROcuxvtOGBAJ4xXnuQoihOcf6fW BL4Q== X-Gm-Message-State: AOAM5327dJ6zSIoDC83nU7V6l613HSZ96z4kwVvmI+FH2y3SEEgkPlg3 JAAVcO5XnZW6+D5JY5/DHvzx4/LtZDI= X-Google-Smtp-Source: ABdhPJwuy4Psg576ljqc52WdDq88brOoSfMTGXbM/dvMUTThfll5LOQtwDiFxp09DQyiOHj2mKzWJw== X-Received: by 2002:a50:f307:: with SMTP id p7mr1193125edm.235.1603362031586; Thu, 22 Oct 2020 03:20:31 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id p16sm582746ejz.103.2020.10.22.03.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Oct 2020 03:20:31 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Oct 2020 12:20:06 +0200 Message-Id: <20201022102006.571413-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201022102006.571413-1-andreas.rheinhardt@gmail.com> References: <20201022102006.571413-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/rv34: Simplify getting right VLC X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" For both RealVideo 3.0 as well as RealVideo 4.0 the VLC table to use depends upon the slice's quantization parameter; these are coded on five bits in the bitstream and are therefore in the range of 0..31; yet the last element here is not valid and therefore the quantizer is clipped to the range 0..30 to get the index. But this is unnecessary: One can just add one element more to the relevant array to avoid the clipping. Signed-off-by: Andreas Rheinhardt --- libavcodec/rv34.c | 6 ++++-- libavcodec/rv34data.h | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c index d251c6817c..09fa962b2f 100644 --- a/libavcodec/rv34.c +++ b/libavcodec/rv34.c @@ -24,6 +24,7 @@ * RV30/40 decoder common data */ +#include "libavutil/avassert.h" #include "libavutil/imgutils.h" #include "libavutil/internal.h" @@ -339,8 +340,9 @@ static inline RV34VLC* choose_vlc_set(int quant, int mod, int type) { if(mod == 2 && quant < 19) quant += 10; else if(mod && quant < 26) quant += 5; - return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]] - : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]]; + av_assert2(quant >= 0 && quant < 32); + return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][quant]] + : &intra_vlcs[rv34_quant_to_vlc_set[0][quant]]; } /** diff --git a/libavcodec/rv34data.h b/libavcodec/rv34data.h index 4b2701fee6..32ecc395a8 100644 --- a/libavcodec/rv34data.h +++ b/libavcodec/rv34data.h @@ -90,13 +90,13 @@ static const uint16_t rv34_qscale_tab[32] = { /** * tables used to translate a quantizer value into a VLC set for decoding - * The first table is used for intraframes. + * The first table is used for intraframes. The two last entries are invalid. */ -static const uint8_t rv34_quant_to_vlc_set[2][31] = { +static const uint8_t rv34_quant_to_vlc_set[2][32] = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, - 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0 }, + 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, - 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 }, + 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6 }, }; /**