From patchwork Fri Nov 20 07:32:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 23867 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 8C6194400A0 for ; Fri, 20 Nov 2020 09:47:13 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4C80068BCE8; Fri, 20 Nov 2020 09:34:30 +0200 (EET) 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 6C50B68BCC2 for ; Fri, 20 Nov 2020 09:34:22 +0200 (EET) Received: by mail-ej1-f67.google.com with SMTP id f20so11518743ejz.4 for ; Thu, 19 Nov 2020 23:34:22 -0800 (PST) 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:reply-to :mime-version:content-transfer-encoding; bh=nEPgpZVMK5OkSL2QqbXAytYnjna17tWlaq5joIIeJDY=; b=SYRNgHMlTYAHg/scDjJYFR814YsGidFlffDc3ry9k5+w1RYDKIRyZornY6OvtIiuEg m6f8MHOPzDLr//TtMut1RZafhIsm/y5hcNtzUoRvDJBSQoc9t5ccvlmwdNaPx1F3KNAy 0AqqtDHYM/pkRZa32PfiaeslNinYWtNQPJPvjqQPtVXZhNBBwGMJNiLwc2dBO9UPzbnA Kk7AzT8QkE3P7ofcsOyHYIQkrxwjQhz/Tn09dRUDFwjRi8H+4Zj1MSm4kPKSSubpwxV7 eh9mg22OiPP4B/ezD6jDkL5ylG0nqpG37XzFw0LgOowE2ApLn8hFehoV7sp2EncH4NBt d4xQ== 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:reply-to:mime-version:content-transfer-encoding; bh=nEPgpZVMK5OkSL2QqbXAytYnjna17tWlaq5joIIeJDY=; b=f0YTbuxlWXkbcoMqL3Ar7tb2HnpK/eG7RyNibEoSnoPPcpGLb+MY6T8D9UomCX1hwg EmBRfR0SKBruh8zaVq+/31IF+ejTrza6We9eFQW2Lt8ALwMwY8W+/IgxwLhzLZxXls03 xzFh8RXxeGaHETjuzgIzTcalINM+et5Np08+Ep0G3KrKgbEktUqSLh9gWIjUScYYeCzR xCr2iz/mr7S03n4IxWbtsb8OAhrrPs4u0iWIBrUuYWzYO9Vtp5hEo4vmgOivjhztF30c HgQqRbcCBx81WNK15W+3nV0wdIw+Nkm3YhykyTBQ5k8zJfFR9RFiG2HMz4gl/LfuI2SY dEsQ== X-Gm-Message-State: AOAM533xGvXc0VDVAVHxWKrzAyo9MI45hdsD/liK7b9Vp8013nDuPttk zE1fW6/TP2+m9KwX79hXrBibD4z6Nl6wjA== X-Google-Smtp-Source: ABdhPJy+G2KnBnBqUnPy1Xd4+fFZY3hdDb8JcWnlbVZnIXnGFfnrW4cPQEf2s3FsSKKn4bhqgizasg== X-Received: by 2002:a17:906:3547:: with SMTP id s7mr30757551eja.70.1605857661378; Thu, 19 Nov 2020 23:34:21 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc1aa4b.dynamic.kabel-deutschland.de. [188.193.170.75]) by smtp.gmail.com with ESMTPSA id i13sm769110ejv.84.2020.11.19.23.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 23:34:20 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Nov 2020 08:32:44 +0100 Message-Id: <20201120073327.820745-20-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> References: <20201120072116.818090-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 119/162] avcodec/atrac9dec: Make VLCs static 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" Also remove code duplication and use a named constant for the number of VLC bits while just at it. Signed-off-by: Andreas Rheinhardt --- libavcodec/atrac9dec.c | 129 +++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 62 deletions(-) diff --git a/libavcodec/atrac9dec.c b/libavcodec/atrac9dec.c index b5f819985a..6849296ac0 100644 --- a/libavcodec/atrac9dec.c +++ b/libavcodec/atrac9dec.c @@ -19,6 +19,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/thread.h" + #include "internal.h" #include "get_bits.h" #include "fft.h" @@ -27,6 +29,7 @@ #include "libavutil/float_dsp.h" #define ATRAC9_SF_VLC_BITS 8 +#define ATRAC9_COEFF_VLC_BITS 9 typedef struct ATRAC9ChannelData { int band_ext; @@ -92,14 +95,15 @@ typedef struct ATRAC9Context { const ATRAC9BlockConfig *block_config; /* Generated on init */ - VLC sf_vlc[2][8]; /* Signed/unsigned, length */ - VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */ uint8_t alloc_curve[48][48]; DECLARE_ALIGNED(32, float, imdct_win)[256]; DECLARE_ALIGNED(32, float, temp)[256]; } ATRAC9Context; +static VLC sf_vlc[2][8]; /* Signed/unsigned, length */ +static VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */ + static inline int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b, GetBitContext *gb) { @@ -269,7 +273,7 @@ static inline int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)]; const int base = get_bits(gb, 5); const int len = get_bits(gb, 2) + 3; - const VLC *tab = &s->sf_vlc[0][len]; + const VLC *tab = &sf_vlc[0][len]; c->scalefactors[0] = get_bits(gb, len); @@ -302,7 +306,7 @@ static inline int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int len = get_bits(gb, 2) + 2; const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len); - const VLC *tab = &s->sf_vlc[1][len]; + const VLC *tab = &sf_vlc[1][len]; for (int i = 0; i < unit_cnt; i++) { int dist = get_vlc2(gb, tab->table, ATRAC9_SF_VLC_BITS, 1); @@ -323,7 +327,7 @@ static inline int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b, const int base = get_bits(gb, 5) - (1 << (5 - 1)); const int len = get_bits(gb, 2) + 1; const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len); - const VLC *tab = &s->sf_vlc[0][len]; + const VLC *tab = &sf_vlc[0][len]; c->scalefactors[0] = get_bits(gb, len); @@ -410,12 +414,12 @@ static inline void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b, if (prec <= max_prec) { const int cb = c->codebookset[i]; const int cbi = at9_q_unit_to_codebookidx[i]; - const VLC *tab = &s->coeff_vlc[cb][prec][cbi]; + const VLC *tab = &coeff_vlc[cb][prec][cbi]; const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi]; const int groups = bands >> huff->value_cnt_pow; for (int j = 0; j < groups; j++) { - uint16_t val = get_vlc2(gb, tab->table, 9, 2); + uint16_t val = get_vlc2(gb, tab->table, ATRAC9_COEFF_VLC_BITS, 2); for (int k = 0; k < huff->value_cnt; k++) { coeffs[k] = sign_extend(val, huff->value_bits); @@ -828,28 +832,72 @@ static av_cold int atrac9_decode_close(AVCodecContext *avctx) { ATRAC9Context *s = avctx->priv_data; - for (int i = 1; i < 7; i++) - ff_free_vlc(&s->sf_vlc[0][i]); - for (int i = 2; i < 6; i++) - ff_free_vlc(&s->sf_vlc[1][i]); - for (int i = 0; i < 2; i++) - for (int j = 0; j < 8; j++) - for (int k = 0; k < 4; k++) - ff_free_vlc(&s->coeff_vlc[i][j][k]); - ff_mdct_end(&s->imdct); av_free(s->fdsp); return 0; } +static av_cold void atrac9_init_vlc(VLC *vlc, int nb_bits, int nb_codes, + const uint8_t (**tab)[2], + unsigned *buf_offset, int offset) +{ + static VLC_TYPE vlc_buf[24812][2]; + + vlc->table = &vlc_buf[*buf_offset]; + vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset; + ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes, + &(*tab)[0][1], 2, &(*tab)[0][0], 2, 1, + offset, INIT_VLC_STATIC_OVERLONG, NULL); + *buf_offset += vlc->table_size; + *tab += nb_codes; +} + +static av_cold void atrac9_init_static(void) +{ + const uint8_t (*tab)[2]; + unsigned offset = 0; + + /* Unsigned scalefactor VLCs */ + tab = at9_sfb_a_tab; + for (int i = 1; i < 7; i++) { + const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i]; + + atrac9_init_vlc(&sf_vlc[0][i], ATRAC9_SF_VLC_BITS, + hf->size, &tab, &offset, 0); + } + + /* Signed scalefactor VLCs */ + tab = at9_sfb_b_tab; + for (int i = 2; i < 6; i++) { + const HuffmanCodebook *hf = &at9_huffman_sf_signed[i]; + + /* The symbols are signed integers in the range -16..15; + * the values in the source table are offset by 16 to make + * them fit into an uint8_t; the -16 reverses this shift. */ + atrac9_init_vlc(&sf_vlc[1][i], ATRAC9_SF_VLC_BITS, + hf->size, &tab, &offset, -16); + } + + /* Coefficient VLCs */ + tab = at9_coeffs_tab; + for (int i = 0; i < 2; i++) { + for (int j = 2; j < 8; j++) { + for (int k = i; k < 4; k++) { + const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k]; + atrac9_init_vlc(&coeff_vlc[i][j][k], ATRAC9_COEFF_VLC_BITS, + hf->size, &tab, &offset, 0); + } + } + } +} + static av_cold int atrac9_decode_init(AVCodecContext *avctx) { + static AVOnce static_table_init = AV_ONCE_INIT; GetBitContext gb; ATRAC9Context *s = avctx->priv_data; int version, block_config_idx, superframe_idx, alloc_c_len; - const uint8_t (*tab)[2]; - int ret; s->avctx = avctx; @@ -932,50 +980,7 @@ static av_cold int atrac9_decode_init(AVCodecContext *avctx) for (int j = 0; j < i; j++) s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i]; - /* Unsigned scalefactor VLCs */ - tab = at9_sfb_a_tab; - for (int i = 1; i < 7; i++) { - const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i]; - - ret = ff_init_vlc_from_lengths(&s->sf_vlc[0][i], ATRAC9_SF_VLC_BITS, - hf->size, &tab[0][1], 2, - &tab[0][0], 2, 1, 0, 0, avctx); - if (ret < 0) - return ret; - tab += hf->size; - } - - /* Signed scalefactor VLCs */ - tab = at9_sfb_b_tab; - for (int i = 2; i < 6; i++) { - const HuffmanCodebook *hf = &at9_huffman_sf_signed[i]; - - /* The symbols are signed integers in the range -16..15; - * the values in the source table are offset by 16 to make - * them fit into an uint8_t; the -16 reverses this shift. */ - ret = ff_init_vlc_from_lengths(&s->sf_vlc[1][i], ATRAC9_SF_VLC_BITS, - hf->size, &tab[0][1], 2, - &tab[0][0], 2, 1, -16, 0, avctx); - if (ret < 0) - return ret; - tab += hf->size; - } - - /* Coefficient VLCs */ - tab = at9_coeffs_tab; - for (int i = 0; i < 2; i++) { - for (int j = 2; j < 8; j++) { - for (int k = i; k < 4; k++) { - const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k]; - ret = ff_init_vlc_from_lengths(&s->coeff_vlc[i][j][k], 9, - hf->size, &tab[0][1], 2, - &tab[0][0], 2, 1, 0, 0, avctx); - if (ret < 0) - return ret; - tab += hf->size; - } - } - } + ff_thread_once(&static_table_init, atrac9_init_static); return 0; }