LCOV - code coverage report
Current view: top level - src/libavcodec - atrac3plus.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 592 839 70.6 %
Date: 2017-08-17 10:06:07 Functions: 27 32 84.4 %

          Line data    Source code
       1             : /*
       2             :  * ATRAC3+ compatible decoder
       3             :  *
       4             :  * Copyright (c) 2010-2013 Maxim Poliakovski
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * Bitstream parser for ATRAC3+ decoder.
      26             :  */
      27             : 
      28             : #include "libavutil/avassert.h"
      29             : #include "avcodec.h"
      30             : #include "get_bits.h"
      31             : #include "atrac3plus.h"
      32             : #include "atrac3plus_data.h"
      33             : 
      34             : static VLC_TYPE tables_data[154276][2];
      35             : static VLC wl_vlc_tabs[4];
      36             : static VLC sf_vlc_tabs[8];
      37             : static VLC ct_vlc_tabs[4];
      38             : static VLC spec_vlc_tabs[112];
      39             : static VLC gain_vlc_tabs[11];
      40             : static VLC tone_vlc_tabs[7];
      41             : 
      42             : /**
      43             :  * Generate canonical VLC table from given descriptor.
      44             :  *
      45             :  * @param[in]     cb          ptr to codebook descriptor
      46             :  * @param[in]     xlat        ptr to translation table or NULL
      47             :  * @param[in,out] tab_offset  starting offset to the generated vlc table
      48             :  * @param[out]    out_vlc     ptr to vlc table to be generated
      49             :  */
      50         420 : static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
      51             :                                          int *tab_offset, VLC *out_vlc)
      52             : {
      53             :     int i, b;
      54             :     uint16_t codes[256];
      55             :     uint8_t bits[256];
      56         420 :     unsigned code = 0;
      57         420 :     int index = 0;
      58         420 :     int min_len = *cb++; // get shortest codeword length
      59         420 :     int max_len = *cb++; // get longest  codeword length
      60             : 
      61        3792 :     for (b = min_len; b <= max_len; b++) {
      62       39320 :         for (i = *cb++; i > 0; i--) {
      63       35948 :             av_assert0(index < 256);
      64       35948 :             bits[index]  = b;
      65       35948 :             codes[index] = code++;
      66       35948 :             index++;
      67             :         }
      68        3372 :         code <<= 1;
      69             :     }
      70             : 
      71         420 :     out_vlc->table = &tables_data[*tab_offset];
      72         420 :     out_vlc->table_allocated = 1 << max_len;
      73             : 
      74         420 :     ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
      75             :                        xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
      76             : 
      77         420 :     *tab_offset += 1 << max_len;
      78         420 : }
      79             : 
      80           4 : av_cold void ff_atrac3p_init_vlcs(void)
      81             : {
      82             :     int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
      83             : 
      84             :     static const int wl_nb_bits[4]  = { 2, 3, 5, 5 };
      85             :     static const int wl_nb_codes[4] = { 3, 5, 8, 8 };
      86             :     static const uint8_t * const wl_bits[4] = {
      87             :         atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
      88             :         atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
      89             :     };
      90             :     static const uint8_t * const wl_codes[4] = {
      91             :         atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
      92             :         atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
      93             :     };
      94             :     static const uint8_t * const wl_xlats[4] = {
      95             :         atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
      96             :     };
      97             : 
      98             :     static const int ct_nb_bits[4]  = { 3, 4, 4, 4 };
      99             :     static const int ct_nb_codes[4] = { 4, 8, 8, 8 };
     100             :     static const uint8_t * const ct_bits[4]  = {
     101             :         atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
     102             :         atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
     103             :     };
     104             :     static const uint8_t * const ct_codes[4] = {
     105             :         atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
     106             :         atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
     107             :     };
     108             :     static const uint8_t * const ct_xlats[4] = {
     109             :         NULL, NULL, atrac3p_ct_huff_xlat1, NULL
     110             :     };
     111             : 
     112             :     static const  int sf_nb_bits[8]  = {  9,  9,  9,  9,  6,  6,  7,  7 };
     113             :     static const  int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
     114             :     static const uint8_t  * const sf_bits[8]  = {
     115             :         atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
     116             :         atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
     117             :         atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
     118             :     };
     119             :     static const uint16_t * const sf_codes[8] = {
     120             :         atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
     121             :         atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
     122             :         atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
     123             :     };
     124             :     static const uint8_t  * const sf_xlats[8] = {
     125             :         atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
     126             :         atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
     127             :     };
     128             : 
     129             :     static const uint8_t * const gain_cbs[11] = {
     130             :         atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
     131             :         atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
     132             :         atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
     133             :         atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
     134             :         atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
     135             :         atrac3p_huff_gain_loc5_cb
     136             :     };
     137             :     static const uint8_t * const gain_xlats[11] = {
     138             :         NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
     139             :         atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
     140             :         atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
     141             :         atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
     142             :         atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
     143             :     };
     144             : 
     145             :     static const uint8_t * const tone_cbs[7] = {
     146             :         atrac3p_huff_tonebands_cb,  atrac3p_huff_numwavs1_cb,
     147             :         atrac3p_huff_numwavs2_cb,   atrac3p_huff_wav_ampsf1_cb,
     148             :         atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
     149             :         atrac3p_huff_freq_cb
     150             :     };
     151             :     static const uint8_t * const tone_xlats[7] = {
     152             :         NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
     153             :         atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
     154             :         atrac3p_huff_freq_xlat
     155             :     };
     156             : 
     157          20 :     for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
     158          16 :         wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
     159          16 :         wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
     160          16 :         ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
     161          16 :         ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
     162             : 
     163          16 :         ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
     164          16 :                            wl_bits[i],  1, 1,
     165          16 :                            wl_codes[i], 1, 1,
     166          16 :                            wl_xlats[i], 1, 1,
     167             :                            INIT_VLC_USE_NEW_STATIC);
     168             : 
     169          16 :         ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
     170          16 :                            ct_bits[i],  1, 1,
     171          16 :                            ct_codes[i], 1, 1,
     172          16 :                            ct_xlats[i], 1, 1,
     173             :                            INIT_VLC_USE_NEW_STATIC);
     174             : 
     175          16 :         wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
     176          16 :         ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
     177             :     }
     178             : 
     179          36 :     for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
     180          32 :         sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
     181          32 :         sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
     182             : 
     183          32 :         ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
     184          32 :                            sf_bits[i],  1, 1,
     185          32 :                            sf_codes[i], 2, 2,
     186          32 :                            sf_xlats[i], 1, 1,
     187             :                            INIT_VLC_USE_NEW_STATIC);
     188          32 :         sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
     189             :     }
     190             : 
     191           4 :     tab_offset = 2564;
     192             : 
     193             :     /* build huffman tables for spectrum decoding */
     194         452 :     for (i = 0; i < 112; i++) {
     195         448 :         if (atrac3p_spectra_tabs[i].cb)
     196         348 :             build_canonical_huff(atrac3p_spectra_tabs[i].cb,
     197             :                                  atrac3p_spectra_tabs[i].xlat,
     198             :                                  &tab_offset, &spec_vlc_tabs[i]);
     199             :         else
     200         100 :             spec_vlc_tabs[i].table = 0;
     201             :     }
     202             : 
     203             :     /* build huffman tables for gain data decoding */
     204          48 :     for (i = 0; i < 11; i++)
     205          44 :         build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
     206             : 
     207             :     /* build huffman tables for tone decoding */
     208          32 :     for (i = 0; i < 7; i++)
     209          28 :         build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
     210           4 : }
     211             : 
     212             : /**
     213             :  * Decode number of coded quantization units.
     214             :  *
     215             :  * @param[in]     gb            the GetBit context
     216             :  * @param[in,out] chan          ptr to the channel parameters
     217             :  * @param[in,out] ctx           ptr to the channel unit context
     218             :  * @param[in]     avctx         ptr to the AVCodecContext
     219             :  * @return result code: 0 = OK, otherwise - error code
     220             :  */
     221         724 : static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,
     222             :                            Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
     223             : {
     224         724 :     chan->fill_mode = get_bits(gb, 2);
     225         724 :     if (!chan->fill_mode) {
     226         155 :         chan->num_coded_vals = ctx->num_quant_units;
     227             :     } else {
     228         569 :         chan->num_coded_vals = get_bits(gb, 5);
     229         569 :         if (chan->num_coded_vals > ctx->num_quant_units) {
     230           0 :             av_log(avctx, AV_LOG_ERROR,
     231             :                    "Invalid number of transmitted units!\n");
     232           0 :             return AVERROR_INVALIDDATA;
     233             :         }
     234             : 
     235         569 :         if (chan->fill_mode == 3)
     236         134 :             chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
     237             :     }
     238             : 
     239         724 :     return 0;
     240             : }
     241             : 
     242             : /**
     243             :  * Add weighting coefficients to the decoded word-length information.
     244             :  *
     245             :  * @param[in,out] ctx           ptr to the channel unit context
     246             :  * @param[in,out] chan          ptr to the channel parameters
     247             :  * @param[in]     wtab_idx      index of the table of weights
     248             :  * @param[in]     avctx         ptr to the AVCodecContext
     249             :  * @return result code: 0 = OK, otherwise - error code
     250             :  */
     251         110 : static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
     252             :                                Atrac3pChanParams *chan, int wtab_idx,
     253             :                                AVCodecContext *avctx)
     254             : {
     255             :     int i;
     256         110 :     const int8_t *weights_tab =
     257         110 :         &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
     258             : 
     259        2970 :     for (i = 0; i < ctx->num_quant_units; i++) {
     260        2860 :         chan->qu_wordlen[i] += weights_tab[i];
     261        2860 :         if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
     262           0 :             av_log(avctx, AV_LOG_ERROR,
     263             :                    "WL index out of range: pos=%d, val=%d!\n",
     264             :                    i, chan->qu_wordlen[i]);
     265           0 :             return AVERROR_INVALIDDATA;
     266             :         }
     267             :     }
     268             : 
     269         110 :     return 0;
     270             : }
     271             : 
     272             : /**
     273             :  * Subtract weighting coefficients from decoded scalefactors.
     274             :  *
     275             :  * @param[in,out] ctx           ptr to the channel unit context
     276             :  * @param[in,out] chan          ptr to the channel parameters
     277             :  * @param[in]     wtab_idx      index of table of weights
     278             :  * @param[in]     avctx         ptr to the AVCodecContext
     279             :  * @return result code: 0 = OK, otherwise - error code
     280             :  */
     281         144 : static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
     282             :                                Atrac3pChanParams *chan, int wtab_idx,
     283             :                                AVCodecContext *avctx)
     284             : {
     285             :     int i;
     286         144 :     const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
     287             : 
     288        3888 :     for (i = 0; i < ctx->used_quant_units; i++) {
     289        3744 :         chan->qu_sf_idx[i] -= weights_tab[i];
     290        3744 :         if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
     291           0 :             av_log(avctx, AV_LOG_ERROR,
     292             :                    "SF index out of range: pos=%d, val=%d!\n",
     293             :                    i, chan->qu_sf_idx[i]);
     294           0 :             return AVERROR_INVALIDDATA;
     295             :         }
     296             :     }
     297             : 
     298         144 :     return 0;
     299             : }
     300             : 
     301             : /**
     302             :  * Unpack vector quantization tables.
     303             :  *
     304             :  * @param[in]    start_val    start value for the unpacked table
     305             :  * @param[in]    shape_vec    ptr to table to unpack
     306             :  * @param[out]   dst          ptr to output array
     307             :  * @param[in]    num_values   number of values to unpack
     308             :  */
     309         243 : static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
     310             :                                    int *dst, int num_values)
     311             : {
     312             :     int i;
     313             : 
     314         243 :     if (num_values) {
     315         243 :         dst[0] = dst[1] = dst[2] = start_val;
     316        4965 :         for (i = 3; i < num_values; i++)
     317        4722 :             dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
     318             :     }
     319         243 : }
     320             : 
     321             : #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals)                            \
     322             :     start_val = get_bits((gb), 6);                                       \
     323             :     unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
     324             :                     (dst), (num_vals))
     325             : 
     326             : /**
     327             :  * Decode word length for each quantization unit of a channel.
     328             :  *
     329             :  * @param[in]     gb            the GetBit context
     330             :  * @param[in,out] ctx           ptr to the channel unit context
     331             :  * @param[in]     ch_num        channel to process
     332             :  * @param[in]     avctx         ptr to the AVCodecContext
     333             :  * @return result code: 0 = OK, otherwise - error code
     334             :  */
     335        1626 : static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     336             :                                   int ch_num, AVCodecContext *avctx)
     337             : {
     338        1626 :     int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
     339             :         ret, start_val;
     340             :     VLC *vlc_tab;
     341        1626 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
     342        1626 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
     343             : 
     344        1626 :     chan->fill_mode = 0;
     345             : 
     346        1626 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
     347         902 :     case 0: /* coded using constant number of bits */
     348       29766 :         for (i = 0; i < ctx->num_quant_units; i++)
     349       28864 :             chan->qu_wordlen[i] = get_bits(gb, 3);
     350         902 :         break;
     351         368 :     case 1:
     352         368 :         if (ch_num) {
     353         351 :             if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
     354           0 :                 return ret;
     355             : 
     356         351 :             if (chan->num_coded_vals) {
     357         314 :                 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
     358             : 
     359        4833 :                 for (i = 0; i < chan->num_coded_vals; i++) {
     360        4519 :                     delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     361        4519 :                     chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
     362             :                 }
     363             :             }
     364             :         } else {
     365          17 :             weight_idx = get_bits(gb, 2);
     366          17 :             if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
     367           0 :                 return ret;
     368             : 
     369          17 :             if (chan->num_coded_vals) {
     370          17 :                 pos = get_bits(gb, 5);
     371          17 :                 if (pos > chan->num_coded_vals) {
     372           0 :                     av_log(avctx, AV_LOG_ERROR,
     373             :                            "WL mode 1: invalid position!\n");
     374           0 :                     return AVERROR_INVALIDDATA;
     375             :                 }
     376             : 
     377          17 :                 delta_bits = get_bits(gb, 2);
     378          17 :                 min_val    = get_bits(gb, 3);
     379             : 
     380          75 :                 for (i = 0; i < pos; i++)
     381          58 :                     chan->qu_wordlen[i] = get_bits(gb, 3);
     382             : 
     383         363 :                 for (i = pos; i < chan->num_coded_vals; i++)
     384         346 :                     chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;
     385             :             }
     386             :         }
     387         368 :         break;
     388         162 :     case 2:
     389         162 :         if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
     390           0 :             return ret;
     391             : 
     392         173 :         if (ch_num && chan->num_coded_vals) {
     393          11 :             vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
     394          11 :             delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     395          11 :             chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
     396             : 
     397         151 :             for (i = 1; i < chan->num_coded_vals; i++) {
     398         140 :                 diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
     399         140 :                 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     400         140 :                 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
     401             :             }
     402         151 :         } else if (chan->num_coded_vals) {
     403         151 :             flag    = get_bits(gb, 1);
     404         151 :             vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
     405             : 
     406         151 :             start_val = get_bits(gb, 3);
     407         302 :             unpack_vq_shape(start_val,
     408         151 :                             &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
     409         151 :                             chan->qu_wordlen, chan->num_coded_vals);
     410             : 
     411         151 :             if (!flag) {
     412        2964 :                 for (i = 0; i < chan->num_coded_vals; i++) {
     413        2826 :                     delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     414        2826 :                     chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
     415             :                 }
     416             :             } else {
     417         128 :                 for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
     418         115 :                     if (!get_bits1(gb)) {
     419         153 :                         chan->qu_wordlen[i]     = (chan->qu_wordlen[i] +
     420          51 :                                                    get_vlc2(gb, vlc_tab->table,
     421          51 :                                                             vlc_tab->bits, 1)) & 7;
     422         153 :                         chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
     423          51 :                                                    get_vlc2(gb, vlc_tab->table,
     424          51 :                                                             vlc_tab->bits, 1)) & 7;
     425             :                     }
     426             : 
     427          13 :                 if (chan->num_coded_vals & 1)
     428           9 :                     chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
     429           3 :                                            get_vlc2(gb, vlc_tab->table,
     430           3 :                                                     vlc_tab->bits, 1)) & 7;
     431             :             }
     432             :         }
     433         162 :         break;
     434         194 :     case 3:
     435         194 :         weight_idx = get_bits(gb, 2);
     436         194 :         if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
     437           0 :             return ret;
     438             : 
     439         194 :         if (chan->num_coded_vals) {
     440         157 :             vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
     441             : 
     442             :             /* first coefficient is coded directly */
     443         157 :             chan->qu_wordlen[0] = get_bits(gb, 3);
     444             : 
     445        3005 :             for (i = 1; i < chan->num_coded_vals; i++) {
     446        2848 :                 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     447        2848 :                 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
     448             :             }
     449             :         }
     450         194 :         break;
     451             :     }
     452             : 
     453        1626 :     if (chan->fill_mode == 2) {
     454        2147 :         for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
     455        1978 :             chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
     456        1457 :     } else if (chan->fill_mode == 3) {
     457         267 :         pos = ch_num ? chan->num_coded_vals + chan->split_point
     458         267 :                      : ctx->num_quant_units - chan->split_point;
     459         663 :         for (i = chan->num_coded_vals; i < pos; i++)
     460         529 :             chan->qu_wordlen[i] = 1;
     461             :     }
     462             : 
     463        1626 :     if (weight_idx)
     464         110 :         return add_wordlen_weights(ctx, chan, weight_idx, avctx);
     465             : 
     466        1516 :     return 0;
     467             : }
     468             : 
     469             : /**
     470             :  * Decode scale factor indexes for each quant unit of a channel.
     471             :  *
     472             :  * @param[in]     gb            the GetBit context
     473             :  * @param[in,out] ctx           ptr to the channel unit context
     474             :  * @param[in]     ch_num        channel to process
     475             :  * @param[in]     avctx         ptr to the AVCodecContext
     476             :  * @return result code: 0 = OK, otherwise - error code
     477             :  */
     478        1552 : static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     479             :                                  int ch_num, AVCodecContext *avctx)
     480             : {
     481        1552 :     int i, weight_idx = 0, delta, diff, num_long_vals,
     482             :         delta_bits, min_val, vlc_sel, start_val;
     483             :     VLC *vlc_tab;
     484        1552 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
     485        1552 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
     486             : 
     487        1552 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
     488         902 :     case 0: /* coded using constant number of bits */
     489       27962 :         for (i = 0; i < ctx->used_quant_units; i++)
     490       27060 :             chan->qu_sf_idx[i] = get_bits(gb, 6);
     491         902 :         break;
     492         322 :     case 1:
     493         322 :         if (ch_num) {
     494         211 :             vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
     495             : 
     496        5697 :             for (i = 0; i < ctx->used_quant_units; i++) {
     497        5486 :                 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     498        5486 :                 chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
     499             :             }
     500             :         } else {
     501         111 :             weight_idx = get_bits(gb, 2);
     502         111 :             if (weight_idx == 3) {
     503          42 :                 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
     504             : 
     505          42 :                 num_long_vals = get_bits(gb, 5);
     506          42 :                 delta_bits    = get_bits(gb, 2);
     507          42 :                 min_val       = get_bits(gb, 4) - 7;
     508             : 
     509         874 :                 for (i = 0; i < num_long_vals; i++)
     510        2496 :                     chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
     511        1664 :                                           get_bits(gb, 4) - 7) & 0x3F;
     512             : 
     513             :                 /* all others are: min_val + delta */
     514         302 :                 for (i = num_long_vals; i < ctx->used_quant_units; i++)
     515         780 :                     chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
     516         520 :                                           get_bitsz(gb, delta_bits)) & 0x3F;
     517             :             } else {
     518          69 :                 num_long_vals = get_bits(gb, 5);
     519          69 :                 delta_bits    = get_bits(gb, 3);
     520          69 :                 min_val       = get_bits(gb, 6);
     521          69 :                 if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
     522           0 :                     av_log(avctx, AV_LOG_ERROR,
     523             :                            "SF mode 1: invalid parameters!\n");
     524           0 :                     return AVERROR_INVALIDDATA;
     525             :                 }
     526             : 
     527             :                 /* read full-precision SF indexes */
     528         719 :                 for (i = 0; i < num_long_vals; i++)
     529         650 :                     chan->qu_sf_idx[i] = get_bits(gb, 6);
     530             : 
     531             :                 /* all others are: min_val + delta */
     532        1213 :                 for (i = num_long_vals; i < ctx->used_quant_units; i++)
     533        2288 :                     chan->qu_sf_idx[i] = (min_val +
     534        2288 :                                           get_bitsz(gb, delta_bits)) & 0x3F;
     535             :             }
     536             :         }
     537         322 :         break;
     538         163 :     case 2:
     539         163 :         if (ch_num) {
     540         114 :             vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
     541             : 
     542         114 :             delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     543         114 :             chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
     544             : 
     545        2964 :             for (i = 1; i < ctx->used_quant_units; i++) {
     546        2850 :                 diff  = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
     547        2850 :                 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     548        2850 :                 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
     549             :             }
     550             :         } else {
     551          49 :             vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
     552             : 
     553          49 :             UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
     554             : 
     555        1323 :             for (i = 0; i < ctx->used_quant_units; i++) {
     556        1274 :                 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     557        3822 :                 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
     558        2548 :                                       sign_extend(delta, 4)) & 0x3F;
     559             :             }
     560             :         }
     561         163 :         break;
     562         165 :     case 3:
     563         165 :         if (ch_num) {
     564             :             /* copy coefficients from reference channel */
     565           0 :             for (i = 0; i < ctx->used_quant_units; i++)
     566           0 :                 chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
     567             :         } else {
     568         165 :             weight_idx = get_bits(gb, 2);
     569         165 :             vlc_sel    = get_bits(gb, 2);
     570         165 :             vlc_tab    = &sf_vlc_tabs[vlc_sel];
     571             : 
     572         165 :             if (weight_idx == 3) {
     573           1 :                 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
     574             : 
     575           1 :                 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
     576             : 
     577           1 :                 diff               = (get_bits(gb, 4)    + 56)   & 0x3F;
     578           1 :                 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
     579             : 
     580          26 :                 for (i = 1; i < ctx->used_quant_units; i++) {
     581          25 :                     delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     582          25 :                     diff               = (diff + sign_extend(delta, 4)) & 0x3F;
     583          25 :                     chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i])    & 0x3F;
     584             :                 }
     585             :             } else {
     586             :                 /* 1st coefficient is coded directly */
     587         164 :                 chan->qu_sf_idx[0] = get_bits(gb, 6);
     588             : 
     589        4264 :                 for (i = 1; i < ctx->used_quant_units; i++) {
     590        4100 :                     delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     591        4100 :                     chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
     592             :                 }
     593             :             }
     594             :         }
     595         165 :         break;
     596             :     }
     597             : 
     598        1552 :     if (weight_idx && weight_idx < 3)
     599         144 :         return subtract_sf_weights(ctx, chan, weight_idx, avctx);
     600             : 
     601        1408 :     return 0;
     602             : }
     603             : 
     604             : /**
     605             :  * Decode word length information for each channel.
     606             :  *
     607             :  * @param[in]     gb            the GetBit context
     608             :  * @param[in,out] ctx           ptr to the channel unit context
     609             :  * @param[in]     num_channels  number of channels to process
     610             :  * @param[in]     avctx         ptr to the AVCodecContext
     611             :  * @return result code: 0 = OK, otherwise - error code
     612             :  */
     613         813 : static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     614             :                                 int num_channels, AVCodecContext *avctx)
     615             : {
     616             :     int ch_num, i, ret;
     617             : 
     618        2439 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
     619        1626 :         memset(ctx->channels[ch_num].qu_wordlen, 0,
     620             :                sizeof(ctx->channels[ch_num].qu_wordlen));
     621             : 
     622        1626 :         if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
     623           0 :             return ret;
     624             :     }
     625             : 
     626             :     /* scan for last non-zero coeff in both channels and
     627             :      * set number of quant units having coded spectrum */
     628        2677 :     for (i = ctx->num_quant_units - 1; i >= 0; i--)
     629        2640 :         if (ctx->channels[0].qu_wordlen[i] ||
     630        1864 :             (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
     631             :             break;
     632         813 :     ctx->used_quant_units = i + 1;
     633             : 
     634         813 :     return 0;
     635             : }
     636             : 
     637             : /**
     638             :  * Decode scale factor indexes for each channel.
     639             :  *
     640             :  * @param[in]     gb            the GetBit context
     641             :  * @param[in,out] ctx           ptr to the channel unit context
     642             :  * @param[in]     num_channels  number of channels to process
     643             :  * @param[in]     avctx         ptr to the AVCodecContext
     644             :  * @return result code: 0 = OK, otherwise - error code
     645             :  */
     646         813 : static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     647             :                                 int num_channels, AVCodecContext *avctx)
     648             : {
     649             :     int ch_num, ret;
     650             : 
     651         813 :     if (!ctx->used_quant_units)
     652          37 :         return 0;
     653             : 
     654        2328 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
     655        1552 :         memset(ctx->channels[ch_num].qu_sf_idx, 0,
     656             :                sizeof(ctx->channels[ch_num].qu_sf_idx));
     657             : 
     658        1552 :         if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
     659           0 :             return ret;
     660             :     }
     661             : 
     662         776 :     return 0;
     663             : }
     664             : 
     665             : /**
     666             :  * Decode number of code table values.
     667             :  *
     668             :  * @param[in]     gb            the GetBit context
     669             :  * @param[in,out] ctx           ptr to the channel unit context
     670             :  * @param[in]     avctx         ptr to the AVCodecContext
     671             :  * @return result code: 0 = OK, otherwise - error code
     672             :  */
     673        1552 : static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     674             :                              AVCodecContext *avctx)
     675             : {
     676             :     int num_coded_vals;
     677             : 
     678        1552 :     if (get_bits1(gb)) {
     679         356 :         num_coded_vals = get_bits(gb, 5);
     680         356 :         if (num_coded_vals > ctx->used_quant_units) {
     681           0 :             av_log(avctx, AV_LOG_ERROR,
     682             :                    "Invalid number of code table indexes: %d!\n", num_coded_vals);
     683           0 :             return AVERROR_INVALIDDATA;
     684             :         }
     685         356 :         return num_coded_vals;
     686             :     } else
     687        1196 :         return ctx->used_quant_units;
     688             : }
     689             : 
     690             : #define DEC_CT_IDX_COMMON(OP)                                           \
     691             :     num_vals = get_num_ct_values(gb, ctx, avctx);                       \
     692             :     if (num_vals < 0)                                                   \
     693             :         return num_vals;                                                \
     694             :                                                                         \
     695             :     for (i = 0; i < num_vals; i++) {                                    \
     696             :         if (chan->qu_wordlen[i]) {                                      \
     697             :             chan->qu_tab_idx[i] = OP;                                   \
     698             :         } else if (ch_num && ref_chan->qu_wordlen[i])                   \
     699             :             /* get clone master flag */                                 \
     700             :             chan->qu_tab_idx[i] = get_bits1(gb);                        \
     701             :     }
     702             : 
     703             : #define CODING_DIRECT get_bits(gb, num_bits)
     704             : 
     705             : #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
     706             : 
     707             : #define CODING_VLC_DELTA                                                \
     708             :     (!i) ? CODING_VLC                                                   \
     709             :          : (pred + get_vlc2(gb, delta_vlc->table,                       \
     710             :                             delta_vlc->bits, 1)) & mask;                \
     711             :     pred = chan->qu_tab_idx[i]
     712             : 
     713             : #define CODING_VLC_DIFF                                                 \
     714             :     (ref_chan->qu_tab_idx[i] +                                          \
     715             :      get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
     716             : 
     717             : /**
     718             :  * Decode code table indexes for each quant unit of a channel.
     719             :  *
     720             :  * @param[in]     gb            the GetBit context
     721             :  * @param[in,out] ctx           ptr to the channel unit context
     722             :  * @param[in]     ch_num        channel to process
     723             :  * @param[in]     avctx         ptr to the AVCodecContext
     724             :  * @return result code: 0 = OK, otherwise - error code
     725             :  */
     726        1552 : static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     727             :                                    int ch_num, AVCodecContext *avctx)
     728             : {
     729             :     int i, num_vals, num_bits, pred;
     730        1552 :     int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
     731             :     VLC *vlc_tab, *delta_vlc;
     732        1552 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
     733        1552 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
     734             : 
     735        1552 :     chan->table_type = get_bits1(gb);
     736             : 
     737        1552 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
     738         980 :     case 0: /* directly coded */
     739         980 :         num_bits = ctx->use_full_table + 2;
     740         980 :         DEC_CT_IDX_COMMON(CODING_DIRECT);
     741         980 :         break;
     742         275 :     case 1: /* entropy-coded */
     743         550 :         vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
     744         275 :                                       : ct_vlc_tabs;
     745         275 :         DEC_CT_IDX_COMMON(CODING_VLC);
     746         275 :         break;
     747         191 :     case 2: /* entropy-coded delta */
     748         191 :         if (ctx->use_full_table) {
     749         191 :             vlc_tab   = &ct_vlc_tabs[1];
     750         191 :             delta_vlc = &ct_vlc_tabs[2];
     751             :         } else {
     752           0 :             vlc_tab   = ct_vlc_tabs;
     753           0 :             delta_vlc = ct_vlc_tabs;
     754             :         }
     755         191 :         pred = 0;
     756         191 :         DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
     757         191 :         break;
     758         106 :     case 3: /* entropy-coded difference to master */
     759         106 :         if (ch_num) {
     760         212 :             vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
     761         106 :                                           : ct_vlc_tabs;
     762         106 :             DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
     763             :         }
     764         106 :         break;
     765             :     }
     766             : 
     767        1552 :     return 0;
     768             : }
     769             : 
     770             : /**
     771             :  * Decode code table indexes for each channel.
     772             :  *
     773             :  * @param[in]     gb            the GetBit context
     774             :  * @param[in,out] ctx           ptr to the channel unit context
     775             :  * @param[in]     num_channels  number of channels to process
     776             :  * @param[in]     avctx         ptr to the AVCodecContext
     777             :  * @return result code: 0 = OK, otherwise - error code
     778             :  */
     779         813 : static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     780             :                                      int num_channels, AVCodecContext *avctx)
     781             : {
     782             :     int ch_num, ret;
     783             : 
     784         813 :     if (!ctx->used_quant_units)
     785          37 :         return 0;
     786             : 
     787         776 :     ctx->use_full_table = get_bits1(gb);
     788             : 
     789        2328 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
     790        1552 :         memset(ctx->channels[ch_num].qu_tab_idx, 0,
     791             :                sizeof(ctx->channels[ch_num].qu_tab_idx));
     792             : 
     793        1552 :         if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
     794           0 :             return ret;
     795             :     }
     796             : 
     797         776 :     return 0;
     798             : }
     799             : 
     800             : /**
     801             :  * Decode huffman-coded spectral lines for a given quant unit.
     802             :  *
     803             :  * This is a generalized version for all known coding modes.
     804             :  * Its speed can be improved by creating separate functions for each mode.
     805             :  *
     806             :  * @param[in]   gb          the GetBit context
     807             :  * @param[in]   tab         code table telling how to decode spectral lines
     808             :  * @param[in]   vlc_tab     ptr to the huffman table associated with the code table
     809             :  * @param[out]  out         pointer to buffer where decoded data should be stored
     810             :  * @param[in]   num_specs   number of spectral lines to decode
     811             :  */
     812       40703 : static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
     813             :                               VLC *vlc_tab, int16_t *out, const int num_specs)
     814             : {
     815             :     int i, j, pos, cf;
     816       40703 :     int group_size = tab->group_size;
     817       40703 :     int num_coeffs = tab->num_coeffs;
     818       40703 :     int bits       = tab->bits;
     819       40703 :     int is_signed  = tab->is_signed;
     820             :     unsigned val;
     821             : 
     822     1250324 :     for (pos = 0; pos < num_specs;) {
     823     2321014 :         if (group_size == 1 || get_bits1(gb)) {
     824     2332486 :             for (j = 0; j < group_size; j++) {
     825     1180390 :                 val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
     826             : 
     827     3119394 :                 for (i = 0; i < num_coeffs; i++) {
     828     1939004 :                     cf = av_mod_uintp2(val, bits);
     829     1939004 :                     if (is_signed)
     830     1268212 :                         cf = sign_extend(cf, bits);
     831      670792 :                     else if (cf && get_bits1(gb))
     832      233973 :                         cf = -cf;
     833             : 
     834     1939004 :                     out[pos++] = cf;
     835     1939004 :                     val      >>= bits;
     836             :                 }
     837             :             }
     838             :         } else /* group skipped */
     839       16822 :             pos += group_size * num_coeffs;
     840             :     }
     841       40703 : }
     842             : 
     843             : /**
     844             :  * Decode huffman-coded IMDCT spectrum for all channels.
     845             :  *
     846             :  * @param[in]     gb            the GetBit context
     847             :  * @param[in,out] ctx           ptr to the channel unit context
     848             :  * @param[in]     num_channels  number of channels to process
     849             :  * @param[in]     avctx         ptr to the AVCodecContext
     850             :  */
     851         813 : static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     852             :                             int num_channels, AVCodecContext *avctx)
     853             : {
     854             :     int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
     855             :     const Atrac3pSpecCodeTab *tab;
     856             :     Atrac3pChanParams *chan;
     857             : 
     858        2439 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
     859        1626 :         chan = &ctx->channels[ch_num];
     860             : 
     861        1626 :         memset(chan->spectrum, 0, sizeof(chan->spectrum));
     862             : 
     863             :         /* set power compensation level to disabled */
     864        1626 :         memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
     865             : 
     866       45586 :         for (qu = 0; qu < ctx->used_quant_units; qu++) {
     867       87920 :             num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
     868       43960 :                         ff_atrac3p_qu_to_spec_pos[qu];
     869             : 
     870       43960 :             wordlen = chan->qu_wordlen[qu];
     871       43960 :             codetab = chan->qu_tab_idx[qu];
     872       43960 :             if (wordlen) {
     873       40703 :                 if (!ctx->use_full_table)
     874       27060 :                     codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
     875             : 
     876       40703 :                 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
     877       40703 :                 tab       = &atrac3p_spectra_tabs[tab_index];
     878             : 
     879             :                 /* this allows reusing VLC tables */
     880       40703 :                 if (tab->redirect >= 0)
     881        6326 :                     tab_index = tab->redirect;
     882             : 
     883       40703 :                 decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
     884       40703 :                                   &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
     885             :                                   num_specs);
     886        3257 :             } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
     887             :                 /* copy coefficients from master */
     888        6514 :                 memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
     889        3257 :                        &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
     890             :                        num_specs *
     891             :                        sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
     892        3257 :                 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
     893             :             }
     894             :         }
     895             : 
     896             :         /* Power compensation levels only present in the bitstream
     897             :          * if there are more than 2 quant units. The lowest two units
     898             :          * correspond to the frequencies 0...351 Hz, whose shouldn't
     899             :          * be affected by the power compensation. */
     900        1626 :         if (ctx->used_quant_units > 2) {
     901        1552 :             num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
     902        8662 :             for (i = 0; i < num_specs; i++)
     903        7110 :                 chan->power_levs[i] = get_bits(gb, 4);
     904             :         }
     905             :     }
     906         813 : }
     907             : 
     908             : /**
     909             :  * Retrieve specified amount of flag bits from the input bitstream.
     910             :  * The data can be shortened in the case of the following two common conditions:
     911             :  * if all bits are zero then only one signal bit = 0 will be stored,
     912             :  * if all bits are ones then two signal bits = 1,0 will be stored.
     913             :  * Otherwise, all necessary bits will be directly stored
     914             :  * prefixed by two signal bits = 1,1.
     915             :  *
     916             :  * @param[in]   gb              ptr to the GetBitContext
     917             :  * @param[out]  out             where to place decoded flags
     918             :  * @param[in]   num_flags       number of flags to process
     919             :  * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
     920             :  */
     921        3252 : static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
     922             : {
     923             :     int i, result;
     924             : 
     925        3252 :     memset(out, 0, num_flags);
     926             : 
     927        3252 :     result = get_bits1(gb);
     928        3252 :     if (result) {
     929         493 :         if (get_bits1(gb))
     930        4609 :             for (i = 0; i < num_flags; i++)
     931        4190 :                 out[i] = get_bits1(gb);
     932             :         else
     933          74 :             memset(out, 1, num_flags);
     934             :     }
     935             : 
     936        3252 :     return result;
     937             : }
     938             : 
     939             : /**
     940             :  * Decode mdct window shape flags for all channels.
     941             :  *
     942             :  * @param[in]     gb            the GetBit context
     943             :  * @param[in,out] ctx           ptr to the channel unit context
     944             :  * @param[in]     num_channels  number of channels to process
     945             :  */
     946         813 : static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     947             :                                 int num_channels)
     948             : {
     949             :     int ch_num;
     950             : 
     951        2439 :     for (ch_num = 0; ch_num < num_channels; ch_num++)
     952        1626 :         get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
     953             :                           ctx->num_subbands);
     954         813 : }
     955             : 
     956             : /**
     957             :  * Decode number of gain control points.
     958             :  *
     959             :  * @param[in]     gb              the GetBit context
     960             :  * @param[in,out] ctx             ptr to the channel unit context
     961             :  * @param[in]     ch_num          channel to process
     962             :  * @param[in]     coded_subbands  number of subbands to process
     963             :  * @return result code: 0 = OK, otherwise - error code
     964             :  */
     965         582 : static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
     966             :                                 int ch_num, int coded_subbands)
     967             : {
     968             :     int i, delta, delta_bits, min_val;
     969         582 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
     970         582 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
     971             : 
     972         582 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
     973          15 :     case 0: /* fixed-length coding */
     974          40 :         for (i = 0; i < coded_subbands; i++)
     975          25 :             chan->gain_data[i].num_points = get_bits(gb, 3);
     976          15 :         break;
     977         509 :     case 1: /* variable-length coding */
     978        4086 :         for (i = 0; i < coded_subbands; i++)
     979        7154 :             chan->gain_data[i].num_points =
     980        7154 :                 get_vlc2(gb, gain_vlc_tabs[0].table,
     981             :                          gain_vlc_tabs[0].bits, 1);
     982         509 :         break;
     983          38 :     case 2:
     984          38 :         if (ch_num) { /* VLC modulo delta to master channel */
     985         162 :             for (i = 0; i < coded_subbands; i++) {
     986         142 :                 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
     987             :                                  gain_vlc_tabs[1].bits, 1);
     988         284 :                 chan->gain_data[i].num_points =
     989         284 :                     (ref_chan->gain_data[i].num_points + delta) & 7;
     990             :             }
     991             :         } else { /* VLC modulo delta to previous */
     992          36 :             chan->gain_data[0].num_points =
     993          36 :                 get_vlc2(gb, gain_vlc_tabs[0].table,
     994             :                          gain_vlc_tabs[0].bits, 1);
     995             : 
     996         105 :             for (i = 1; i < coded_subbands; i++) {
     997          87 :                 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
     998             :                                  gain_vlc_tabs[1].bits, 1);
     999         174 :                 chan->gain_data[i].num_points =
    1000         174 :                     (chan->gain_data[i - 1].num_points + delta) & 7;
    1001             :             }
    1002             :         }
    1003          38 :         break;
    1004          20 :     case 3:
    1005          20 :         if (ch_num) { /* copy data from master channel */
    1006          66 :             for (i = 0; i < coded_subbands; i++)
    1007          96 :                 chan->gain_data[i].num_points =
    1008          96 :                     ref_chan->gain_data[i].num_points;
    1009             :         } else { /* shorter delta to min */
    1010           2 :             delta_bits = get_bits(gb, 2);
    1011           2 :             min_val    = get_bits(gb, 3);
    1012             : 
    1013          25 :             for (i = 0; i < coded_subbands; i++) {
    1014          23 :                 chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
    1015          23 :                 if (chan->gain_data[i].num_points > 7)
    1016           0 :                     return AVERROR_INVALIDDATA;
    1017             :             }
    1018             :         }
    1019             :     }
    1020             : 
    1021         582 :     return 0;
    1022             : }
    1023             : 
    1024             : /**
    1025             :  * Implements coding mode 3 (slave) for gain compensation levels.
    1026             :  *
    1027             :  * @param[out]   dst   ptr to the output array
    1028             :  * @param[in]    ref   ptr to the reference channel
    1029             :  */
    1030        1605 : static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
    1031             : {
    1032             :     int i;
    1033             : 
    1034        2000 :     for (i = 0; i < dst->num_points; i++)
    1035         395 :         dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
    1036        1605 : }
    1037             : 
    1038             : /**
    1039             :  * Implements coding mode 1 (master) for gain compensation levels.
    1040             :  *
    1041             :  * @param[in]     gb     the GetBit context
    1042             :  * @param[in]     ctx    ptr to the channel unit context
    1043             :  * @param[out]    dst    ptr to the output array
    1044             :  */
    1045        2009 : static inline void gainc_level_mode1m(GetBitContext *gb,
    1046             :                                       Atrac3pChanUnitCtx *ctx,
    1047             :                                       AtracGainInfo *dst)
    1048             : {
    1049             :     int i, delta;
    1050             : 
    1051        2009 :     if (dst->num_points > 0)
    1052         501 :         dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
    1053             :                                     gain_vlc_tabs[2].bits, 1);
    1054             : 
    1055        2110 :     for (i = 1; i < dst->num_points; i++) {
    1056         101 :         delta = get_vlc2(gb, gain_vlc_tabs[3].table,
    1057             :                          gain_vlc_tabs[3].bits, 1);
    1058         101 :         dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
    1059             :     }
    1060        2009 : }
    1061             : 
    1062             : /**
    1063             :  * Decode level code for each gain control point.
    1064             :  *
    1065             :  * @param[in]     gb              the GetBit context
    1066             :  * @param[in,out] ctx             ptr to the channel unit context
    1067             :  * @param[in]     ch_num          channel to process
    1068             :  * @param[in]     coded_subbands  number of subbands to process
    1069             :  * @return result code: 0 = OK, otherwise - error code
    1070             :  */
    1071         582 : static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1072             :                                int ch_num, int coded_subbands)
    1073             : {
    1074             :     int sb, i, delta, delta_bits, min_val, pred;
    1075         582 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
    1076         582 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
    1077             : 
    1078         582 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
    1079           9 :     case 0: /* fixed-length coding */
    1080          62 :         for (sb = 0; sb < coded_subbands; sb++)
    1081          71 :             for (i = 0; i < chan->gain_data[sb].num_points; i++)
    1082          18 :                 chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
    1083           9 :         break;
    1084         302 :     case 1:
    1085         302 :         if (ch_num) { /* VLC modulo delta to master channel */
    1086         112 :             for (sb = 0; sb < coded_subbands; sb++)
    1087         162 :                 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
    1088          68 :                     delta = get_vlc2(gb, gain_vlc_tabs[5].table,
    1089             :                                      gain_vlc_tabs[5].bits, 1);
    1090         136 :                     pred = (i >= ref_chan->gain_data[sb].num_points)
    1091          68 :                            ? 7 : ref_chan->gain_data[sb].lev_code[i];
    1092          68 :                     chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
    1093             :                 }
    1094             :         } else { /* VLC modulo delta to previous */
    1095        2242 :             for (sb = 0; sb < coded_subbands; sb++)
    1096        1958 :                 gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
    1097             :         }
    1098         302 :         break;
    1099          36 :     case 2:
    1100          36 :         if (ch_num) { /* VLC modulo delta to previous or clone master */
    1101         257 :             for (sb = 0; sb < coded_subbands; sb++)
    1102         225 :                 if (chan->gain_data[sb].num_points > 0) {
    1103         102 :                     if (get_bits1(gb))
    1104          49 :                         gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
    1105             :                     else
    1106          53 :                         gainc_level_mode3s(&chan->gain_data[sb],
    1107          53 :                                            &ref_chan->gain_data[sb]);
    1108             :                 }
    1109             :         } else { /* VLC modulo delta to lev_codes of previous subband */
    1110           4 :             if (chan->gain_data[0].num_points > 0)
    1111           2 :                 gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
    1112             : 
    1113          38 :             for (sb = 1; sb < coded_subbands; sb++)
    1114          77 :                 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
    1115          43 :                     delta = get_vlc2(gb, gain_vlc_tabs[4].table,
    1116             :                                      gain_vlc_tabs[4].bits, 1);
    1117          86 :                     pred = (i >= chan->gain_data[sb - 1].num_points)
    1118          43 :                            ? 7 : chan->gain_data[sb - 1].lev_code[i];
    1119          43 :                     chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
    1120             :                 }
    1121             :         }
    1122          36 :         break;
    1123         235 :     case 3:
    1124         235 :         if (ch_num) { /* clone master */
    1125        1787 :             for (sb = 0; sb < coded_subbands; sb++)
    1126        1552 :                 gainc_level_mode3s(&chan->gain_data[sb],
    1127        1552 :                                    &ref_chan->gain_data[sb]);
    1128             :         } else { /* shorter delta to min */
    1129           0 :             delta_bits = get_bits(gb, 2);
    1130           0 :             min_val    = get_bits(gb, 4);
    1131             : 
    1132           0 :             for (sb = 0; sb < coded_subbands; sb++)
    1133           0 :                 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
    1134           0 :                     chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
    1135           0 :                     if (chan->gain_data[sb].lev_code[i] > 15)
    1136           0 :                         return AVERROR_INVALIDDATA;
    1137             :                 }
    1138             :         }
    1139         235 :         break;
    1140             :     }
    1141             : 
    1142         582 :     return 0;
    1143             : }
    1144             : 
    1145             : /**
    1146             :  * Implements coding mode 0 for gain compensation locations.
    1147             :  *
    1148             :  * @param[in]     gb     the GetBit context
    1149             :  * @param[in]     ctx    ptr to the channel unit context
    1150             :  * @param[out]    dst    ptr to the output array
    1151             :  * @param[in]     pos    position of the value to be processed
    1152             :  */
    1153         664 : static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1154             :                                    AtracGainInfo *dst, int pos)
    1155             : {
    1156             :     int delta_bits;
    1157             : 
    1158         664 :     if (!pos || dst->loc_code[pos - 1] < 15)
    1159         646 :         dst->loc_code[pos] = get_bits(gb, 5);
    1160          18 :     else if (dst->loc_code[pos - 1] >= 30)
    1161           0 :         dst->loc_code[pos] = 31;
    1162             :     else {
    1163          18 :         delta_bits         = av_log2(30 - dst->loc_code[pos - 1]) + 1;
    1164          54 :         dst->loc_code[pos] = dst->loc_code[pos - 1] +
    1165          36 :                              get_bits(gb, delta_bits) + 1;
    1166             :     }
    1167         664 : }
    1168             : 
    1169             : /**
    1170             :  * Implements coding mode 1 for gain compensation locations.
    1171             :  *
    1172             :  * @param[in]     gb     the GetBit context
    1173             :  * @param[in]     ctx    ptr to the channel unit context
    1174             :  * @param[out]    dst    ptr to the output array
    1175             :  */
    1176         111 : static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1177             :                                    AtracGainInfo *dst)
    1178             : {
    1179             :     int i;
    1180             :     VLC *tab;
    1181             : 
    1182         111 :     if (dst->num_points > 0) {
    1183             :         /* 1st coefficient is stored directly */
    1184          70 :         dst->loc_code[0] = get_bits(gb, 5);
    1185             : 
    1186         114 :         for (i = 1; i < dst->num_points; i++) {
    1187             :             /* switch VLC according to the curve direction
    1188             :              * (ascending/descending) */
    1189          88 :             tab              = (dst->lev_code[i] <= dst->lev_code[i - 1])
    1190             :                                ? &gain_vlc_tabs[7]
    1191          44 :                                : &gain_vlc_tabs[9];
    1192          88 :             dst->loc_code[i] = dst->loc_code[i - 1] +
    1193          44 :                                get_vlc2(gb, tab->table, tab->bits, 1);
    1194             :         }
    1195             :     }
    1196         111 : }
    1197             : 
    1198             : /**
    1199             :  * Decode location code for each gain control point.
    1200             :  *
    1201             :  * @param[in]     gb              the GetBit context
    1202             :  * @param[in,out] ctx             ptr to the channel unit context
    1203             :  * @param[in]     ch_num          channel to process
    1204             :  * @param[in]     coded_subbands  number of subbands to process
    1205             :  * @param[in]     avctx           ptr to the AVCodecContext
    1206             :  * @return result code: 0 = OK, otherwise - error code
    1207             :  */
    1208         582 : static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1209             :                                   int ch_num, int coded_subbands,
    1210             :                                   AVCodecContext *avctx)
    1211             : {
    1212             :     int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
    1213             :     AtracGainInfo *dst, *ref;
    1214             :     VLC *tab;
    1215         582 :     Atrac3pChanParams *chan     = &ctx->channels[ch_num];
    1216         582 :     Atrac3pChanParams *ref_chan = &ctx->channels[0];
    1217             : 
    1218         582 :     switch (get_bits(gb, 2)) { /* switch according to coding mode */
    1219         433 :     case 0: /* sequence of numbers in ascending order */
    1220        3350 :         for (sb = 0; sb < coded_subbands; sb++)
    1221        3563 :             for (i = 0; i < chan->gain_data[sb].num_points; i++)
    1222         646 :                 gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
    1223         433 :         break;
    1224          64 :     case 1:
    1225          64 :         if (ch_num) {
    1226         365 :             for (sb = 0; sb < coded_subbands; sb++) {
    1227         312 :                 if (chan->gain_data[sb].num_points <= 0)
    1228         219 :                     continue;
    1229          93 :                 dst = &chan->gain_data[sb];
    1230          93 :                 ref = &ref_chan->gain_data[sb];
    1231             : 
    1232             :                 /* 1st value is vlc-coded modulo delta to master */
    1233          93 :                 delta = get_vlc2(gb, gain_vlc_tabs[10].table,
    1234             :                                  gain_vlc_tabs[10].bits, 1);
    1235          93 :                 pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
    1236          93 :                 dst->loc_code[0] = (pred + delta) & 0x1F;
    1237             : 
    1238         113 :                 for (i = 1; i < dst->num_points; i++) {
    1239          20 :                     more_than_ref = i >= ref->num_points;
    1240          20 :                     if (dst->lev_code[i] > dst->lev_code[i - 1]) {
    1241             :                         /* ascending curve */
    1242           0 :                         if (more_than_ref) {
    1243           0 :                             delta =
    1244           0 :                                 get_vlc2(gb, gain_vlc_tabs[9].table,
    1245             :                                          gain_vlc_tabs[9].bits, 1);
    1246           0 :                             dst->loc_code[i] = dst->loc_code[i - 1] + delta;
    1247             :                         } else {
    1248           0 :                             if (get_bits1(gb))
    1249           0 :                                 gainc_loc_mode0(gb, ctx, dst, i);  // direct coding
    1250             :                             else
    1251           0 :                                 dst->loc_code[i] = ref->loc_code[i];  // clone master
    1252             :                         }
    1253             :                     } else { /* descending curve */
    1254          20 :                         tab   = more_than_ref ? &gain_vlc_tabs[7]
    1255          20 :                                               : &gain_vlc_tabs[10];
    1256          20 :                         delta = get_vlc2(gb, tab->table, tab->bits, 1);
    1257          20 :                         if (more_than_ref)
    1258          13 :                             dst->loc_code[i] = dst->loc_code[i - 1] + delta;
    1259             :                         else
    1260           7 :                             dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
    1261             :                     }
    1262             :                 }
    1263             :             }
    1264             :         } else /* VLC delta to previous */
    1265          78 :             for (sb = 0; sb < coded_subbands; sb++)
    1266          67 :                 gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
    1267          64 :         break;
    1268          57 :     case 2:
    1269          57 :         if (ch_num) {
    1270         150 :             for (sb = 0; sb < coded_subbands; sb++) {
    1271         134 :                 if (chan->gain_data[sb].num_points <= 0)
    1272          74 :                     continue;
    1273          60 :                 dst = &chan->gain_data[sb];
    1274          60 :                 ref = &ref_chan->gain_data[sb];
    1275          60 :                 if (dst->num_points > ref->num_points || get_bits1(gb))
    1276          44 :                     gainc_loc_mode1(gb, ctx, dst);
    1277             :                 else /* clone master for the whole subband */
    1278          38 :                     for (i = 0; i < chan->gain_data[sb].num_points; i++)
    1279          22 :                         dst->loc_code[i] = ref->loc_code[i];
    1280             :             }
    1281             :         } else {
    1282             :             /* data for the first subband is coded directly */
    1283          49 :             for (i = 0; i < chan->gain_data[0].num_points; i++)
    1284           8 :                 gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
    1285             : 
    1286         315 :             for (sb = 1; sb < coded_subbands; sb++) {
    1287         274 :                 if (chan->gain_data[sb].num_points <= 0)
    1288         177 :                     continue;
    1289          97 :                 dst = &chan->gain_data[sb];
    1290             : 
    1291             :                 /* 1st value is vlc-coded modulo delta to the corresponding
    1292             :                  * value of the previous subband if any or zero */
    1293          97 :                 delta = get_vlc2(gb, gain_vlc_tabs[6].table,
    1294             :                                  gain_vlc_tabs[6].bits, 1);
    1295         194 :                 pred             = dst[-1].num_points > 0
    1296          97 :                                    ? dst[-1].loc_code[0] : 0;
    1297          97 :                 dst->loc_code[0] = (pred + delta) & 0x1F;
    1298             : 
    1299         123 :                 for (i = 1; i < dst->num_points; i++) {
    1300          26 :                     more_than_ref = i >= dst[-1].num_points;
    1301             :                     /* Select VLC table according to curve direction and
    1302             :                      * presence of prediction. */
    1303          52 :                     tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
    1304          26 :                                                    2 + more_than_ref + 6];
    1305          26 :                     delta = get_vlc2(gb, tab->table, tab->bits, 1);
    1306          26 :                     if (more_than_ref)
    1307          23 :                         dst->loc_code[i] = dst->loc_code[i - 1] + delta;
    1308             :                     else
    1309           3 :                         dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
    1310             :                 }
    1311             :             }
    1312             :         }
    1313          57 :         break;
    1314          28 :     case 3:
    1315          28 :         if (ch_num) { /* clone master or direct or direct coding */
    1316          84 :             for (sb = 0; sb < coded_subbands; sb++)
    1317         102 :                 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
    1318          33 :                     if (i >= ref_chan->gain_data[sb].num_points)
    1319          10 :                         gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
    1320             :                     else
    1321          46 :                         chan->gain_data[sb].loc_code[i] =
    1322          46 :                             ref_chan->gain_data[sb].loc_code[i];
    1323             :                 }
    1324             :         } else { /* shorter delta to min */
    1325          13 :             delta_bits = get_bits(gb, 2) + 1;
    1326          13 :             min_val    = get_bits(gb, 5);
    1327             : 
    1328         119 :             for (sb = 0; sb < coded_subbands; sb++)
    1329         173 :                 for (i = 0; i < chan->gain_data[sb].num_points; i++)
    1330         134 :                     chan->gain_data[sb].loc_code[i] = min_val + i +
    1331          67 :                                                       get_bits(gb, delta_bits);
    1332             :         }
    1333          28 :         break;
    1334             :     }
    1335             : 
    1336             :     /* Validate decoded information */
    1337        4502 :     for (sb = 0; sb < coded_subbands; sb++) {
    1338        3920 :         dst = &chan->gain_data[sb];
    1339        5046 :         for (i = 0; i < chan->gain_data[sb].num_points; i++) {
    1340        1126 :             if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
    1341         151 :                 (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
    1342           0 :                 av_log(avctx, AV_LOG_ERROR,
    1343             :                        "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
    1344             :                        ch_num, sb, i, dst->loc_code[i]);
    1345           0 :                 return AVERROR_INVALIDDATA;
    1346             :             }
    1347             :         }
    1348             :     }
    1349             : 
    1350         582 :     return 0;
    1351             : }
    1352             : 
    1353             : /**
    1354             :  * Decode gain control data for all channels.
    1355             :  *
    1356             :  * @param[in]     gb            the GetBit context
    1357             :  * @param[in,out] ctx           ptr to the channel unit context
    1358             :  * @param[in]     num_channels  number of channels to process
    1359             :  * @param[in]     avctx         ptr to the AVCodecContext
    1360             :  * @return result code: 0 = OK, otherwise - error code
    1361             :  */
    1362         813 : static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1363             :                              int num_channels, AVCodecContext *avctx)
    1364             : {
    1365             :     int ch_num, coded_subbands, sb, ret;
    1366             : 
    1367        2439 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
    1368        1626 :         memset(ctx->channels[ch_num].gain_data, 0,
    1369             :                sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
    1370             : 
    1371        1626 :         if (get_bits1(gb)) { /* gain control data present? */
    1372         582 :             coded_subbands = get_bits(gb, 4) + 1;
    1373         582 :             if (get_bits1(gb)) /* is high band gain data replication on? */
    1374           6 :                 ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
    1375             :             else
    1376         576 :                 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
    1377             : 
    1378         582 :             if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
    1379         582 :                 (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands))  < 0 ||
    1380             :                 (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
    1381           0 :                 return ret;
    1382             : 
    1383         582 :             if (coded_subbands > 0) { /* propagate gain data if requested */
    1384         588 :                 for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
    1385           6 :                     ctx->channels[ch_num].gain_data[sb] =
    1386           6 :                         ctx->channels[ch_num].gain_data[sb - 1];
    1387             :             }
    1388             :         } else {
    1389        1044 :             ctx->channels[ch_num].num_gain_subbands = 0;
    1390             :         }
    1391             :     }
    1392             : 
    1393         813 :     return 0;
    1394             : }
    1395             : 
    1396             : /**
    1397             :  * Decode envelope for all tones of a channel.
    1398             :  *
    1399             :  * @param[in]     gb                the GetBit context
    1400             :  * @param[in,out] ctx               ptr to the channel unit context
    1401             :  * @param[in]     ch_num            channel to process
    1402             :  * @param[in]     band_has_tones    ptr to an array of per-band-flags:
    1403             :  *                                  1 - tone data present
    1404             :  */
    1405           0 : static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1406             :                                   int ch_num, int band_has_tones[])
    1407             : {
    1408             :     int sb;
    1409           0 :     Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
    1410           0 :     Atrac3pWavesData *ref = ctx->channels[0].tones_info;
    1411             : 
    1412           0 :     if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
    1413           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1414           0 :             if (!band_has_tones[sb])
    1415           0 :                 continue;
    1416           0 :             dst[sb].pend_env.has_start_point = get_bits1(gb);
    1417           0 :             dst[sb].pend_env.start_pos       = dst[sb].pend_env.has_start_point
    1418           0 :                                                ? get_bits(gb, 5) : -1;
    1419           0 :             dst[sb].pend_env.has_stop_point  = get_bits1(gb);
    1420           0 :             dst[sb].pend_env.stop_pos        = dst[sb].pend_env.has_stop_point
    1421           0 :                                                ? get_bits(gb, 5) : 32;
    1422             :         }
    1423             :     } else { /* mode 1(slave only): copy master */
    1424           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1425           0 :             if (!band_has_tones[sb])
    1426           0 :                 continue;
    1427           0 :             dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
    1428           0 :             dst[sb].pend_env.has_stop_point  = ref[sb].pend_env.has_stop_point;
    1429           0 :             dst[sb].pend_env.start_pos       = ref[sb].pend_env.start_pos;
    1430           0 :             dst[sb].pend_env.stop_pos        = ref[sb].pend_env.stop_pos;
    1431             :         }
    1432             :     }
    1433           0 : }
    1434             : 
    1435             : /**
    1436             :  * Decode number of tones for each subband of a channel.
    1437             :  *
    1438             :  * @param[in]     gb                the GetBit context
    1439             :  * @param[in,out] ctx               ptr to the channel unit context
    1440             :  * @param[in]     ch_num            channel to process
    1441             :  * @param[in]     band_has_tones    ptr to an array of per-band-flags:
    1442             :  *                                  1 - tone data present
    1443             :  * @param[in]     avctx             ptr to the AVCodecContext
    1444             :  * @return result code: 0 = OK, otherwise - error code
    1445             :  */
    1446           0 : static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1447             :                                int ch_num, int band_has_tones[],
    1448             :                                AVCodecContext *avctx)
    1449             : {
    1450             :     int mode, sb, delta;
    1451           0 :     Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
    1452           0 :     Atrac3pWavesData *ref = ctx->channels[0].tones_info;
    1453             : 
    1454           0 :     mode = get_bits(gb, ch_num + 1);
    1455           0 :     switch (mode) {
    1456           0 :     case 0: /** fixed-length coding */
    1457           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
    1458           0 :             if (band_has_tones[sb])
    1459           0 :                 dst[sb].num_wavs = get_bits(gb, 4);
    1460           0 :         break;
    1461           0 :     case 1: /** variable-length coding */
    1462           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
    1463           0 :             if (band_has_tones[sb])
    1464           0 :                 dst[sb].num_wavs =
    1465           0 :                     get_vlc2(gb, tone_vlc_tabs[1].table,
    1466             :                              tone_vlc_tabs[1].bits, 1);
    1467           0 :         break;
    1468           0 :     case 2: /** VLC modulo delta to master (slave only) */
    1469           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
    1470           0 :             if (band_has_tones[sb]) {
    1471           0 :                 delta = get_vlc2(gb, tone_vlc_tabs[2].table,
    1472             :                                  tone_vlc_tabs[2].bits, 1);
    1473           0 :                 delta = sign_extend(delta, 3);
    1474           0 :                 dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
    1475             :             }
    1476           0 :         break;
    1477           0 :     case 3: /** copy master (slave only) */
    1478           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
    1479           0 :             if (band_has_tones[sb])
    1480           0 :                 dst[sb].num_wavs = ref[sb].num_wavs;
    1481           0 :         break;
    1482             :     }
    1483             : 
    1484             :     /** initialize start tone index for each subband */
    1485           0 :     for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
    1486           0 :         if (band_has_tones[sb]) {
    1487           0 :             if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
    1488           0 :                 av_log(avctx, AV_LOG_ERROR,
    1489             :                        "Too many tones: %d (max. 48), frame: %d!\n",
    1490           0 :                        ctx->waves_info->tones_index + dst[sb].num_wavs,
    1491             :                        avctx->frame_number);
    1492           0 :                 return AVERROR_INVALIDDATA;
    1493             :             }
    1494           0 :             dst[sb].start_index           = ctx->waves_info->tones_index;
    1495           0 :             ctx->waves_info->tones_index += dst[sb].num_wavs;
    1496             :         }
    1497             : 
    1498           0 :     return 0;
    1499             : }
    1500             : 
    1501             : /**
    1502             :  * Decode frequency information for each subband of a channel.
    1503             :  *
    1504             :  * @param[in]     gb                the GetBit context
    1505             :  * @param[in,out] ctx               ptr to the channel unit context
    1506             :  * @param[in]     ch_num            channel to process
    1507             :  * @param[in]     band_has_tones    ptr to an array of per-band-flags:
    1508             :  *                                  1 - tone data present
    1509             :  */
    1510           0 : static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1511             :                                    int ch_num, int band_has_tones[])
    1512             : {
    1513             :     int sb, i, direction, nbits, pred, delta;
    1514             :     Atrac3pWaveParam *iwav, *owav;
    1515           0 :     Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
    1516           0 :     Atrac3pWavesData *ref = ctx->channels[0].tones_info;
    1517             : 
    1518           0 :     if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
    1519           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1520           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1521           0 :                 continue;
    1522           0 :             iwav      = &ctx->waves_info->waves[dst[sb].start_index];
    1523           0 :             direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
    1524           0 :             if (direction) { /** packed numbers in descending order */
    1525           0 :                 if (dst[sb].num_wavs)
    1526           0 :                     iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
    1527           0 :                 for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
    1528           0 :                     nbits = av_log2(iwav[i+1].freq_index) + 1;
    1529           0 :                     iwav[i].freq_index = get_bits(gb, nbits);
    1530             :                 }
    1531             :             } else { /** packed numbers in ascending order */
    1532           0 :                 for (i = 0; i < dst[sb].num_wavs; i++) {
    1533           0 :                     if (!i || iwav[i - 1].freq_index < 512)
    1534           0 :                         iwav[i].freq_index = get_bits(gb, 10);
    1535             :                     else {
    1536           0 :                         nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
    1537           0 :                         iwav[i].freq_index = get_bits(gb, nbits) +
    1538           0 :                                              1024 - (1 << nbits);
    1539             :                     }
    1540             :                 }
    1541             :             }
    1542             :         }
    1543             :     } else { /* mode 1: VLC modulo delta to master (slave only) */
    1544           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1545           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1546           0 :                 continue;
    1547           0 :             iwav = &ctx->waves_info->waves[ref[sb].start_index];
    1548           0 :             owav = &ctx->waves_info->waves[dst[sb].start_index];
    1549           0 :             for (i = 0; i < dst[sb].num_wavs; i++) {
    1550           0 :                 delta = get_vlc2(gb, tone_vlc_tabs[6].table,
    1551             :                                  tone_vlc_tabs[6].bits, 1);
    1552           0 :                 delta = sign_extend(delta, 8);
    1553           0 :                 pred  = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
    1554           0 :                         (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
    1555           0 :                 owav[i].freq_index = (pred + delta) & 0x3FF;
    1556             :             }
    1557             :         }
    1558             :     }
    1559           0 : }
    1560             : 
    1561             : /**
    1562             :  * Decode amplitude information for each subband of a channel.
    1563             :  *
    1564             :  * @param[in]     gb                the GetBit context
    1565             :  * @param[in,out] ctx               ptr to the channel unit context
    1566             :  * @param[in]     ch_num            channel to process
    1567             :  * @param[in]     band_has_tones    ptr to an array of per-band-flags:
    1568             :  *                                  1 - tone data present
    1569             :  */
    1570           0 : static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1571             :                                    int ch_num, int band_has_tones[])
    1572             : {
    1573             :     int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
    1574             :     Atrac3pWaveParam *wsrc, *wref;
    1575           0 :     int refwaves[48] = { 0 };
    1576           0 :     Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
    1577           0 :     Atrac3pWavesData *ref = ctx->channels[0].tones_info;
    1578             : 
    1579           0 :     if (ch_num) {
    1580           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1581           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1582           0 :                 continue;
    1583           0 :             wsrc = &ctx->waves_info->waves[dst[sb].start_index];
    1584           0 :             wref = &ctx->waves_info->waves[ref[sb].start_index];
    1585           0 :             for (j = 0; j < dst[sb].num_wavs; j++) {
    1586           0 :                 for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
    1587           0 :                     diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
    1588           0 :                     if (diff < maxdiff) {
    1589           0 :                         maxdiff = diff;
    1590           0 :                         fi      = i;
    1591             :                     }
    1592             :                 }
    1593             : 
    1594           0 :                 if (maxdiff < 8)
    1595           0 :                     refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
    1596           0 :                 else if (j < ref[sb].num_wavs)
    1597           0 :                     refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
    1598             :                 else
    1599           0 :                     refwaves[dst[sb].start_index + j] = -1;
    1600             :             }
    1601             :         }
    1602             :     }
    1603             : 
    1604           0 :     mode = get_bits(gb, ch_num + 1);
    1605             : 
    1606           0 :     switch (mode) {
    1607           0 :     case 0: /** fixed-length coding */
    1608           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1609           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1610           0 :                 continue;
    1611           0 :             if (ctx->waves_info->amplitude_mode)
    1612           0 :                 for (i = 0; i < dst[sb].num_wavs; i++)
    1613           0 :                     ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
    1614             :             else
    1615           0 :                 ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
    1616             :         }
    1617           0 :         break;
    1618           0 :     case 1: /** min + VLC delta */
    1619           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1620           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1621           0 :                 continue;
    1622           0 :             if (ctx->waves_info->amplitude_mode)
    1623           0 :                 for (i = 0; i < dst[sb].num_wavs; i++)
    1624           0 :                     ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
    1625           0 :                         get_vlc2(gb, tone_vlc_tabs[3].table,
    1626           0 :                                  tone_vlc_tabs[3].bits, 1) + 20;
    1627             :             else
    1628           0 :                 ctx->waves_info->waves[dst[sb].start_index].amp_sf =
    1629           0 :                     get_vlc2(gb, tone_vlc_tabs[4].table,
    1630           0 :                              tone_vlc_tabs[4].bits, 1) + 24;
    1631             :         }
    1632           0 :         break;
    1633           0 :     case 2: /** VLC modulo delta to master (slave only) */
    1634           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1635           0 :             if (!band_has_tones[sb] || !dst[sb].num_wavs)
    1636           0 :                 continue;
    1637           0 :             for (i = 0; i < dst[sb].num_wavs; i++) {
    1638           0 :                 delta = get_vlc2(gb, tone_vlc_tabs[5].table,
    1639             :                                  tone_vlc_tabs[5].bits, 1);
    1640           0 :                 delta = sign_extend(delta, 5);
    1641           0 :                 pred  = refwaves[dst[sb].start_index + i] >= 0 ?
    1642           0 :                         ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
    1643           0 :                 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
    1644             :             }
    1645             :         }
    1646           0 :         break;
    1647           0 :     case 3: /** clone master (slave only) */
    1648           0 :         for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1649           0 :             if (!band_has_tones[sb])
    1650           0 :                 continue;
    1651           0 :             for (i = 0; i < dst[sb].num_wavs; i++)
    1652           0 :                 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
    1653           0 :                     refwaves[dst[sb].start_index + i] >= 0
    1654           0 :                     ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
    1655           0 :                     : 32;
    1656             :         }
    1657           0 :         break;
    1658             :     }
    1659           0 : }
    1660             : 
    1661             : /**
    1662             :  * Decode phase information for each subband of a channel.
    1663             :  *
    1664             :  * @param[in]     gb                the GetBit context
    1665             :  * @param[in,out] ctx               ptr to the channel unit context
    1666             :  * @param[in]     ch_num            channel to process
    1667             :  * @param[in]     band_has_tones    ptr to an array of per-band-flags:
    1668             :  *                                  1 - tone data present
    1669             :  */
    1670           0 : static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1671             :                                int ch_num, int band_has_tones[])
    1672             : {
    1673             :     int sb, i;
    1674             :     Atrac3pWaveParam *wparam;
    1675           0 :     Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
    1676             : 
    1677           0 :     for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
    1678           0 :         if (!band_has_tones[sb])
    1679           0 :             continue;
    1680           0 :         wparam = &ctx->waves_info->waves[dst[sb].start_index];
    1681           0 :         for (i = 0; i < dst[sb].num_wavs; i++)
    1682           0 :             wparam[i].phase_index = get_bits(gb, 5);
    1683             :     }
    1684           0 : }
    1685             : 
    1686             : /**
    1687             :  * Decode tones info for all channels.
    1688             :  *
    1689             :  * @param[in]     gb            the GetBit context
    1690             :  * @param[in,out] ctx           ptr to the channel unit context
    1691             :  * @param[in]     num_channels  number of channels to process
    1692             :  * @param[in]     avctx         ptr to the AVCodecContext
    1693             :  * @return result code: 0 = OK, otherwise - error code
    1694             :  */
    1695         813 : static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1696             :                              int num_channels, AVCodecContext *avctx)
    1697             : {
    1698             :     int ch_num, i, ret;
    1699             :     int band_has_tones[16];
    1700             : 
    1701        2439 :     for (ch_num = 0; ch_num < num_channels; ch_num++)
    1702        1626 :         memset(ctx->channels[ch_num].tones_info, 0,
    1703             :                sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
    1704             : 
    1705         813 :     ctx->waves_info->tones_present = get_bits1(gb);
    1706         813 :     if (!ctx->waves_info->tones_present)
    1707         813 :         return 0;
    1708             : 
    1709           0 :     memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
    1710             : 
    1711           0 :     ctx->waves_info->amplitude_mode = get_bits1(gb);
    1712           0 :     if (!ctx->waves_info->amplitude_mode) {
    1713           0 :         avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
    1714           0 :         return AVERROR_PATCHWELCOME;
    1715             :     }
    1716             : 
    1717           0 :     ctx->waves_info->num_tone_bands =
    1718           0 :         get_vlc2(gb, tone_vlc_tabs[0].table,
    1719           0 :                  tone_vlc_tabs[0].bits, 1) + 1;
    1720             : 
    1721           0 :     if (num_channels == 2) {
    1722           0 :         get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
    1723           0 :         get_subband_flags(gb, ctx->waves_info->tone_master,  ctx->waves_info->num_tone_bands);
    1724           0 :         get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
    1725             :     }
    1726             : 
    1727           0 :     ctx->waves_info->tones_index = 0;
    1728             : 
    1729           0 :     for (ch_num = 0; ch_num < num_channels; ch_num++) {
    1730           0 :         for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
    1731           0 :             band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
    1732             : 
    1733           0 :         decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
    1734           0 :         if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones,
    1735             :                                        avctx)) < 0)
    1736           0 :             return ret;
    1737             : 
    1738           0 :         decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
    1739           0 :         decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
    1740           0 :         decode_tones_phase(gb, ctx, ch_num, band_has_tones);
    1741             :     }
    1742             : 
    1743           0 :     if (num_channels == 2) {
    1744           0 :         for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
    1745           0 :             if (ctx->waves_info->tone_sharing[i])
    1746           0 :                 ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
    1747             : 
    1748           0 :             if (ctx->waves_info->tone_master[i])
    1749           0 :                 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
    1750             :                        ctx->channels[1].tones_info[i]);
    1751             :         }
    1752             :     }
    1753             : 
    1754           0 :     return 0;
    1755             : }
    1756             : 
    1757         813 : int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
    1758             :                                    int num_channels, AVCodecContext *avctx)
    1759             : {
    1760             :     int ret;
    1761             : 
    1762             :     /* parse sound header */
    1763         813 :     ctx->num_quant_units = get_bits(gb, 5) + 1;
    1764         813 :     if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
    1765           0 :         av_log(avctx, AV_LOG_ERROR,
    1766             :                "Invalid number of quantization units: %d!\n",
    1767             :                ctx->num_quant_units);
    1768           0 :         return AVERROR_INVALIDDATA;
    1769             :     }
    1770             : 
    1771         813 :     ctx->mute_flag = get_bits1(gb);
    1772             : 
    1773             :     /* decode various sound parameters */
    1774         813 :     if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0)
    1775           0 :         return ret;
    1776             : 
    1777         813 :     ctx->num_subbands       = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
    1778        1626 :     ctx->num_coded_subbands = ctx->used_quant_units
    1779         776 :                               ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
    1780        1589 :                               : 0;
    1781             : 
    1782         813 :     if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0)
    1783           0 :         return ret;
    1784             : 
    1785         813 :     if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0)
    1786           0 :         return ret;
    1787             : 
    1788         813 :     decode_spectrum(gb, ctx, num_channels, avctx);
    1789             : 
    1790         813 :     if (num_channels == 2) {
    1791         813 :         get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
    1792         813 :         get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
    1793             :     }
    1794             : 
    1795         813 :     decode_window_shape(gb, ctx, num_channels);
    1796             : 
    1797         813 :     if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0)
    1798           0 :         return ret;
    1799             : 
    1800         813 :     if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0)
    1801           0 :         return ret;
    1802             : 
    1803             :     /* decode global noise info */
    1804         813 :     ctx->noise_present = get_bits1(gb);
    1805         813 :     if (ctx->noise_present) {
    1806           0 :         ctx->noise_level_index = get_bits(gb, 4);
    1807           0 :         ctx->noise_table_index = get_bits(gb, 4);
    1808             :     }
    1809             : 
    1810         813 :     return 0;
    1811             : }

Generated by: LCOV version 1.13