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); } /**