CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/at3_standalone/atrac3plus.cpp
Views: 1401
/*1* ATRAC3+ compatible decoder2*3* Copyright (c) 2010-2013 Maxim Poliakovski4*5* This file is part of FFmpeg.6*7* FFmpeg is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public9* License as published by the Free Software Foundation; either10* version 2.1 of the License, or (at your option) any later version.11*12* FFmpeg is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* Lesser General Public License for more details.16*17* You should have received a copy of the GNU Lesser General Public18* License along with FFmpeg; if not, write to the Free Software19* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA20*/2122/**23* @file24* Bitstream parser for ATRAC3+ decoder.25*/2627#include <string.h>28#include <stdlib.h>2930#include "get_bits.h"31#include "atrac3plus.h"32#include "atrac3plus_data.h"3334static VLC_TYPE tables_data[154276][2];35static VLC wl_vlc_tabs[4];36static VLC sf_vlc_tabs[8];37static VLC ct_vlc_tabs[4];38static VLC spec_vlc_tabs[112];39static VLC gain_vlc_tabs[11];40static VLC tone_vlc_tabs[7];4142static const uint8_t ff_logg2_tab[256] = {430,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,445,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,456,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,466,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,477,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,487,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,497,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,507,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,751};5253// todo: Replace with clz type instructions.54int av_log2(unsigned int v)55{56int n = 0;57if (v & 0xffff0000) {58v >>= 16;59n += 16;60}61if (v & 0xff00) {62v >>= 8;63n += 8;64}65n += ff_logg2_tab[v];6667return n;68}6970/**71* Generate canonical VLC table from given descriptor.72*73* @param[in] cb ptr to codebook descriptor74* @param[in] xlat ptr to translation table or NULL75* @param[in,out] tab_offset starting offset to the generated vlc table76* @param[out] out_vlc ptr to vlc table to be generated77*/78static void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,79int *tab_offset, VLC *out_vlc)80{81int i, b;82uint16_t codes[256];83uint8_t bits[256];84unsigned code = 0;85int index = 0;86int min_len = *cb++; // get shortest codeword length87int max_len = *cb++; // get longest codeword length8889for (b = min_len; b <= max_len; b++) {90for (i = *cb++; i > 0; i--) {91av_assert0(index < 256);92bits[index] = b;93codes[index] = code++;94index++;95}96code <<= 1;97}9899out_vlc->table = &tables_data[*tab_offset];100out_vlc->table_allocated = 1 << max_len;101102ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,103xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);104105*tab_offset += 1 << max_len;106}107108void ff_atrac3p_init_vlcs(void)109{110int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;111112static const int wl_nb_bits[4] = { 2, 3, 5, 5 };113static const int wl_nb_codes[4] = { 3, 5, 8, 8 };114static const uint8_t * const wl_bits[4] = {115atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,116atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4117};118static const uint8_t * const wl_codes[4] = {119atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,120atrac3p_wl_huff_code3, atrac3p_wl_huff_code4121};122static const uint8_t * const wl_xlats[4] = {123atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL124};125126static const int ct_nb_bits[4] = { 3, 4, 4, 4 };127static const int ct_nb_codes[4] = { 4, 8, 8, 8 };128static const uint8_t * const ct_bits[4] = {129atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,130atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3131};132static const uint8_t * const ct_codes[4] = {133atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,134atrac3p_ct_huff_code2, atrac3p_ct_huff_code3135};136static const uint8_t * const ct_xlats[4] = {137NULL, NULL, atrac3p_ct_huff_xlat1, NULL138};139140static const int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };141static const int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };142static const uint8_t * const sf_bits[8] = {143atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,144atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,145atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6146};147static const uint16_t * const sf_codes[8] = {148atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,149atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,150atrac3p_sf_huff_code5, atrac3p_sf_huff_code6151};152static const uint8_t * const sf_xlats[8] = {153atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,154atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL155};156157static const uint8_t * const gain_cbs[11] = {158atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,159atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,160atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,161atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,162atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,163atrac3p_huff_gain_loc5_cb164};165static const uint8_t * const gain_xlats[11] = {166NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,167atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,168atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,169atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,170atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat171};172173static const uint8_t * const tone_cbs[7] = {174atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,175atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,176atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,177atrac3p_huff_freq_cb178};179static const uint8_t * const tone_xlats[7] = {180NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,181atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,182atrac3p_huff_freq_xlat183};184185for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {186wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];187wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];188ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];189ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];190191ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],192wl_bits[i], 1, 1,193wl_codes[i], 1, 1,194wl_xlats[i], 1, 1,195INIT_VLC_USE_NEW_STATIC);196197ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],198ct_bits[i], 1, 1,199ct_codes[i], 1, 1,200ct_xlats[i], 1, 1,201INIT_VLC_USE_NEW_STATIC);202203wl_vlc_offs += wl_vlc_tabs[i].table_allocated;204ct_vlc_offs += ct_vlc_tabs[i].table_allocated;205}206207for (i = 0, sf_vlc_offs = 76; i < 8; i++) {208sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];209sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];210211ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],212sf_bits[i], 1, 1,213sf_codes[i], 2, 2,214sf_xlats[i], 1, 1,215INIT_VLC_USE_NEW_STATIC);216sf_vlc_offs += sf_vlc_tabs[i].table_allocated;217}218219tab_offset = 2564;220221/* build huffman tables for spectrum decoding */222for (i = 0; i < 112; i++) {223if (atrac3p_spectra_tabs[i].cb)224build_canonical_huff(atrac3p_spectra_tabs[i].cb,225atrac3p_spectra_tabs[i].xlat,226&tab_offset, &spec_vlc_tabs[i]);227else228spec_vlc_tabs[i].table = 0;229}230231/* build huffman tables for gain data decoding */232for (i = 0; i < 11; i++)233build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);234235/* build huffman tables for tone decoding */236for (i = 0; i < 7; i++)237build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);238}239240/**241* Decode number of coded quantization units.242*243* @param[in] gb the GetBit context244* @param[in,out] chan ptr to the channel parameters245* @param[in,out] ctx ptr to the channel unit context246* @return result code: 0 = OK, otherwise - error code247*/248static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,249Atrac3pChanUnitCtx *ctx)250{251chan->fill_mode = get_bits(gb, 2);252if (!chan->fill_mode) {253chan->num_coded_vals = ctx->num_quant_units;254} else {255chan->num_coded_vals = get_bits(gb, 5);256if (chan->num_coded_vals > ctx->num_quant_units) {257av_log(AV_LOG_ERROR,258"Invalid number of transmitted units!\n");259return AVERROR_INVALIDDATA;260}261262if (chan->fill_mode == 3)263chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;264}265266return 0;267}268269/**270* Add weighting coefficients to the decoded word-length information.271*272* @param[in,out] ctx ptr to the channel unit context273* @param[in,out] chan ptr to the channel parameters274* @param[in] wtab_idx index of the table of weights275* @return result code: 0 = OK, otherwise - error code276*/277static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,278Atrac3pChanParams *chan, int wtab_idx)279{280int i;281const int8_t *weights_tab =282&atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];283284for (i = 0; i < ctx->num_quant_units; i++) {285chan->qu_wordlen[i] += weights_tab[i];286if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {287av_log(AV_LOG_ERROR,288"WL index out of range: pos=%d, val=%d!\n",289i, chan->qu_wordlen[i]);290return AVERROR_INVALIDDATA;291}292}293294return 0;295}296297/**298* Subtract weighting coefficients from decoded scalefactors.299*300* @param[in,out] ctx ptr to the channel unit context301* @param[in,out] chan ptr to the channel parameters302* @param[in] wtab_idx index of table of weights303* @return result code: 0 = OK, otherwise - error code304*/305static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,306Atrac3pChanParams *chan, int wtab_idx)307{308int i;309const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];310311for (i = 0; i < ctx->used_quant_units; i++) {312chan->qu_sf_idx[i] -= weights_tab[i];313if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {314av_log(AV_LOG_ERROR,315"SF index out of range: pos=%d, val=%d!\n",316i, chan->qu_sf_idx[i]);317return AVERROR_INVALIDDATA;318}319}320321return 0;322}323324/**325* Unpack vector quantization tables.326*327* @param[in] start_val start value for the unpacked table328* @param[in] shape_vec ptr to table to unpack329* @param[out] dst ptr to output array330* @param[in] num_values number of values to unpack331*/332static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,333int *dst, int num_values)334{335int i;336337if (num_values) {338dst[0] = dst[1] = dst[2] = start_val;339for (i = 3; i < num_values; i++)340dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];341}342}343344#define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \345start_val = get_bits((gb), 6); \346unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \347(dst), (num_vals))348349/**350* Decode word length for each quantization unit of a channel.351*352* @param[in] gb the GetBit context353* @param[in,out] ctx ptr to the channel unit context354* @param[in] ch_num channel to process355* @return result code: 0 = OK, otherwise - error code356*/357static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num)358{359int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,360ret, start_val;361VLC *vlc_tab;362Atrac3pChanParams *chan = &ctx->channels[ch_num];363Atrac3pChanParams *ref_chan = &ctx->channels[0];364365chan->fill_mode = 0;366367switch (get_bits(gb, 2)) { /* switch according to coding mode */368case 0: /* coded using constant number of bits */369for (i = 0; i < ctx->num_quant_units; i++)370chan->qu_wordlen[i] = get_bits(gb, 3);371break;372case 1:373if (ch_num) {374if ((ret = num_coded_units(gb, chan, ctx)) < 0)375return ret;376377if (chan->num_coded_vals) {378vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];379380for (i = 0; i < chan->num_coded_vals; i++) {381delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);382chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;383}384}385} else {386weight_idx = get_bits(gb, 2);387if ((ret = num_coded_units(gb, chan, ctx)) < 0)388return ret;389390if (chan->num_coded_vals) {391pos = get_bits(gb, 5);392if (pos > chan->num_coded_vals) {393av_log(AV_LOG_ERROR,394"WL mode 1: invalid position!\n");395return AVERROR_INVALIDDATA;396}397398delta_bits = get_bits(gb, 2);399min_val = get_bits(gb, 3);400401for (i = 0; i < pos; i++)402chan->qu_wordlen[i] = get_bits(gb, 3);403404for (i = pos; i < chan->num_coded_vals; i++)405chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;406}407}408break;409case 2:410if ((ret = num_coded_units(gb, chan, ctx)) < 0)411return ret;412413if (ch_num && chan->num_coded_vals) {414vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];415delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);416chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;417418for (i = 1; i < chan->num_coded_vals; i++) {419diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];420delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);421chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;422}423} else if (chan->num_coded_vals) {424flag = get_bits(gb, 1);425vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];426427start_val = get_bits(gb, 3);428unpack_vq_shape(start_val,429&atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],430chan->qu_wordlen, chan->num_coded_vals);431432if (!flag) {433for (i = 0; i < chan->num_coded_vals; i++) {434delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);435chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;436}437} else {438for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)439if (!get_bits1(gb)) {440chan->qu_wordlen[i] = (chan->qu_wordlen[i] +441get_vlc2(gb, vlc_tab->table,442vlc_tab->bits, 1)) & 7;443chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +444get_vlc2(gb, vlc_tab->table,445vlc_tab->bits, 1)) & 7;446}447448if (chan->num_coded_vals & 1)449chan->qu_wordlen[i] = (chan->qu_wordlen[i] +450get_vlc2(gb, vlc_tab->table,451vlc_tab->bits, 1)) & 7;452}453}454break;455case 3:456weight_idx = get_bits(gb, 2);457if ((ret = num_coded_units(gb, chan, ctx)) < 0)458return ret;459460if (chan->num_coded_vals) {461vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];462463/* first coefficient is coded directly */464chan->qu_wordlen[0] = get_bits(gb, 3);465466for (i = 1; i < chan->num_coded_vals; i++) {467delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);468chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;469}470}471break;472}473474if (chan->fill_mode == 2) {475for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)476chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;477} else if (chan->fill_mode == 3) {478pos = ch_num ? chan->num_coded_vals + chan->split_point479: ctx->num_quant_units - chan->split_point;480if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {481av_log(AV_LOG_ERROR, "Split point beyond array");482pos = FF_ARRAY_ELEMS(chan->qu_wordlen);483}484for (i = chan->num_coded_vals; i < pos; i++)485chan->qu_wordlen[i] = 1;486}487488if (weight_idx)489return add_wordlen_weights(ctx, chan, weight_idx);490491return 0;492}493494/**495* Decode scale factor indexes for each quant unit of a channel.496*497* @param[in] gb the GetBit context498* @param[in,out] ctx ptr to the channel unit context499* @param[in] ch_num channel to process500* @return result code: 0 = OK, otherwise - error code501*/502static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num)503{504int i, weight_idx = 0, delta, diff, num_long_vals,505delta_bits, min_val, vlc_sel, start_val;506VLC *vlc_tab;507Atrac3pChanParams *chan = &ctx->channels[ch_num];508Atrac3pChanParams *ref_chan = &ctx->channels[0];509510switch (get_bits(gb, 2)) { /* switch according to coding mode */511case 0: /* coded using constant number of bits */512for (i = 0; i < ctx->used_quant_units; i++)513chan->qu_sf_idx[i] = get_bits(gb, 6);514break;515case 1:516if (ch_num) {517vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];518519for (i = 0; i < ctx->used_quant_units; i++) {520delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);521chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;522}523} else {524weight_idx = get_bits(gb, 2);525if (weight_idx == 3) {526UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);527528num_long_vals = get_bits(gb, 5);529delta_bits = get_bits(gb, 2);530min_val = get_bits(gb, 4) - 7;531532for (i = 0; i < num_long_vals; i++)533chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +534get_bits(gb, 4) - 7) & 0x3F;535536/* all others are: min_val + delta */537for (i = num_long_vals; i < ctx->used_quant_units; i++)538chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +539get_bitsz(gb, delta_bits)) & 0x3F;540} else {541num_long_vals = get_bits(gb, 5);542delta_bits = get_bits(gb, 3);543min_val = get_bits(gb, 6);544if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {545av_log(AV_LOG_ERROR,546"SF mode 1: invalid parameters!\n");547return AVERROR_INVALIDDATA;548}549550/* read full-precision SF indexes */551for (i = 0; i < num_long_vals; i++)552chan->qu_sf_idx[i] = get_bits(gb, 6);553554/* all others are: min_val + delta */555for (i = num_long_vals; i < ctx->used_quant_units; i++)556chan->qu_sf_idx[i] = (min_val +557get_bitsz(gb, delta_bits)) & 0x3F;558}559}560break;561case 2:562if (ch_num) {563vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];564565delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);566chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;567568for (i = 1; i < ctx->used_quant_units; i++) {569diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];570delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);571chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;572}573} else {574vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];575576UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);577578for (i = 0; i < ctx->used_quant_units; i++) {579delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);580chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +581sign_extend(delta, 4)) & 0x3F;582}583}584break;585case 3:586if (ch_num) {587/* copy coefficients from reference channel */588for (i = 0; i < ctx->used_quant_units; i++)589chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];590} else {591weight_idx = get_bits(gb, 2);592vlc_sel = get_bits(gb, 2);593vlc_tab = &sf_vlc_tabs[vlc_sel];594595if (weight_idx == 3) {596vlc_tab = &sf_vlc_tabs[vlc_sel + 4];597598UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);599600diff = (get_bits(gb, 4) + 56) & 0x3F;601chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;602603for (i = 1; i < ctx->used_quant_units; i++) {604delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);605diff = (diff + sign_extend(delta, 4)) & 0x3F;606chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;607}608} else {609/* 1st coefficient is coded directly */610chan->qu_sf_idx[0] = get_bits(gb, 6);611612for (i = 1; i < ctx->used_quant_units; i++) {613delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);614chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;615}616}617}618break;619}620621if (weight_idx && weight_idx < 3)622return subtract_sf_weights(ctx, chan, weight_idx);623624return 0;625}626627/**628* Decode word length information for each channel.629*630* @param[in] gb the GetBit context631* @param[in,out] ctx ptr to the channel unit context632* @param[in] num_channels number of channels to process633* @return result code: 0 = OK, otherwise - error code634*/635static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,636int num_channels)637{638int ch_num, i, ret;639640for (ch_num = 0; ch_num < num_channels; ch_num++) {641memset(ctx->channels[ch_num].qu_wordlen, 0,642sizeof(ctx->channels[ch_num].qu_wordlen));643644if ((ret = decode_channel_wordlen(gb, ctx, ch_num)) < 0)645return ret;646}647648/* scan for last non-zero coeff in both channels and649* set number of quant units having coded spectrum */650for (i = ctx->num_quant_units - 1; i >= 0; i--)651if (ctx->channels[0].qu_wordlen[i] ||652(num_channels == 2 && ctx->channels[1].qu_wordlen[i]))653break;654ctx->used_quant_units = i + 1;655656return 0;657}658659/**660* Decode scale factor indexes for each channel.661*662* @param[in] gb the GetBit context663* @param[in,out] ctx ptr to the channel unit context664* @param[in] num_channels number of channels to process665* @return result code: 0 = OK, otherwise - error code666*/667static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,668int num_channels)669{670int ch_num, ret;671672if (!ctx->used_quant_units)673return 0;674675for (ch_num = 0; ch_num < num_channels; ch_num++) {676memset(ctx->channels[ch_num].qu_sf_idx, 0,677sizeof(ctx->channels[ch_num].qu_sf_idx));678679if ((ret = decode_channel_sf_idx(gb, ctx, ch_num)) < 0)680return ret;681}682683return 0;684}685686/**687* Decode number of code table values.688*689* @param[in] gb the GetBit context690* @param[in,out] ctx ptr to the channel unit context691* @return result code: 0 = OK, otherwise - error code692*/693static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx)694{695int num_coded_vals;696697if (get_bits1(gb)) {698num_coded_vals = get_bits(gb, 5);699if (num_coded_vals > ctx->used_quant_units) {700av_log(AV_LOG_ERROR,701"Invalid number of code table indexes: %d!\n", num_coded_vals);702return AVERROR_INVALIDDATA;703}704return num_coded_vals;705} else706return ctx->used_quant_units;707}708709#define DEC_CT_IDX_COMMON(OP) \710num_vals = get_num_ct_values(gb, ctx); \711if (num_vals < 0) \712return num_vals; \713\714for (i = 0; i < num_vals; i++) { \715if (chan->qu_wordlen[i]) { \716chan->qu_tab_idx[i] = OP; \717} else if (ch_num && ref_chan->qu_wordlen[i]) \718/* get clone master flag */ \719chan->qu_tab_idx[i] = get_bits1(gb); \720}721722#define CODING_DIRECT get_bits(gb, num_bits)723724#define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)725726#define CODING_VLC_DELTA \727(!i) ? CODING_VLC \728: (pred + get_vlc2(gb, delta_vlc->table, \729delta_vlc->bits, 1)) & mask; \730pred = chan->qu_tab_idx[i]731732#define CODING_VLC_DIFF \733(ref_chan->qu_tab_idx[i] + \734get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask735736/**737* Decode code table indexes for each quant unit of a channel.738*739* @param[in] gb the GetBit context740* @param[in,out] ctx ptr to the channel unit context741* @param[in] ch_num channel to process742* @return result code: 0 = OK, otherwise - error code743*/744static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,745int ch_num)746{747int i, num_vals, num_bits, pred;748int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */749VLC *vlc_tab, *delta_vlc;750Atrac3pChanParams *chan = &ctx->channels[ch_num];751Atrac3pChanParams *ref_chan = &ctx->channels[0];752753chan->table_type = get_bits1(gb);754755switch (get_bits(gb, 2)) { /* switch according to coding mode */756case 0: /* directly coded */757num_bits = ctx->use_full_table + 2;758DEC_CT_IDX_COMMON(CODING_DIRECT);759break;760case 1: /* entropy-coded */761vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]762: ct_vlc_tabs;763DEC_CT_IDX_COMMON(CODING_VLC);764break;765case 2: /* entropy-coded delta */766if (ctx->use_full_table) {767vlc_tab = &ct_vlc_tabs[1];768delta_vlc = &ct_vlc_tabs[2];769} else {770vlc_tab = ct_vlc_tabs;771delta_vlc = ct_vlc_tabs;772}773pred = 0;774DEC_CT_IDX_COMMON(CODING_VLC_DELTA);775break;776case 3: /* entropy-coded difference to master */777if (ch_num) {778vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]779: ct_vlc_tabs;780DEC_CT_IDX_COMMON(CODING_VLC_DIFF);781}782break;783}784785return 0;786}787788/**789* Decode code table indexes for each channel.790*791* @param[in] gb the GetBit context792* @param[in,out] ctx ptr to the channel unit context793* @param[in] num_channels number of channels to process794* @return result code: 0 = OK, otherwise - error code795*/796static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,797int num_channels)798{799int ch_num, ret;800801if (!ctx->used_quant_units)802return 0;803804ctx->use_full_table = get_bits1(gb);805806for (ch_num = 0; ch_num < num_channels; ch_num++) {807memset(ctx->channels[ch_num].qu_tab_idx, 0,808sizeof(ctx->channels[ch_num].qu_tab_idx));809810if ((ret = decode_channel_code_tab(gb, ctx, ch_num)) < 0)811return ret;812}813814return 0;815}816817/**818* Decode huffman-coded spectral lines for a given quant unit.819*820* This is a generalized version for all known coding modes.821* Its speed can be improved by creating separate functions for each mode.822*823* @param[in] gb the GetBit context824* @param[in] tab code table telling how to decode spectral lines825* @param[in] vlc_tab ptr to the huffman table associated with the code table826* @param[out] out pointer to buffer where decoded data should be stored827* @param[in] num_specs number of spectral lines to decode828*/829static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,830VLC *vlc_tab, int16_t *out, const int num_specs)831{832int i, j, pos, cf;833int group_size = tab->group_size;834int num_coeffs = tab->num_coeffs;835int bits = tab->bits;836int is_signed = tab->is_signed;837unsigned val;838const unsigned bitmask = ((1 << bits) - 1); // mask to clear higher bits.839840for (pos = 0; pos < num_specs;) {841if (group_size == 1 || get_bits1(gb)) {842for (j = 0; j < group_size; j++) {843val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);844for (i = 0; i < num_coeffs; i++) {845cf = val & bitmask;846if (is_signed)847cf = sign_extend(cf, bits);848else if (cf && get_bits1(gb))849cf = -cf;850851out[pos++] = cf;852val >>= bits;853}854}855} else /* group skipped */856pos += group_size * num_coeffs;857}858}859860/**861* Decode huffman-coded IMDCT spectrum for all channels.862*863* @param[in] gb the GetBit context864* @param[in,out] ctx ptr to the channel unit context865* @param[in] num_channels number of channels to process866*/867static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,868int num_channels)869{870int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;871const Atrac3pSpecCodeTab *tab;872Atrac3pChanParams *chan;873874for (ch_num = 0; ch_num < num_channels; ch_num++) {875chan = &ctx->channels[ch_num];876877memset(chan->spectrum, 0, sizeof(chan->spectrum));878879/* set power compensation level to disabled */880memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));881882for (qu = 0; qu < ctx->used_quant_units; qu++) {883num_specs = av_atrac3p_qu_to_spec_pos[qu + 1] -884av_atrac3p_qu_to_spec_pos[qu];885886wordlen = chan->qu_wordlen[qu];887codetab = chan->qu_tab_idx[qu];888if (wordlen) {889if (!ctx->use_full_table)890codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];891892tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;893tab = &atrac3p_spectra_tabs[tab_index];894895/* this allows reusing VLC tables */896if (tab->redirect >= 0)897tab_index = tab->redirect;898899decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],900&chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]],901num_specs);902} else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {903/* copy coefficients from master */904memcpy(&chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]],905&ctx->channels[0].spectrum[av_atrac3p_qu_to_spec_pos[qu]],906num_specs *907sizeof(chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]]));908chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];909}910}911912/* Power compensation levels only present in the bitstream913* if there are more than 2 quant units. The lowest two units914* correspond to the frequencies 0...351 Hz, whose shouldn't915* be affected by the power compensation. */916if (ctx->used_quant_units > 2) {917num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];918for (i = 0; i < num_specs; i++)919chan->power_levs[i] = get_bits(gb, 4);920}921}922}923924/**925* Retrieve specified amount of flag bits from the input bitstream.926* The data can be shortened in the case of the following two common conditions:927* if all bits are zero then only one signal bit = 0 will be stored,928* if all bits are ones then two signal bits = 1,0 will be stored.929* Otherwise, all necessary bits will be directly stored930* prefixed by two signal bits = 1,1.931*932* @param[in] gb ptr to the GetBitContext933* @param[out] out where to place decoded flags934* @param[in] num_flags number of flags to process935* @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit936*/937static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)938{939int i, result;940941memset(out, 0, num_flags);942943result = get_bits1(gb);944if (result) {945if (get_bits1(gb))946for (i = 0; i < num_flags; i++)947out[i] = get_bits1(gb);948else949memset(out, 1, num_flags);950}951952return result;953}954955/**956* Decode mdct window shape flags for all channels.957*958* @param[in] gb the GetBit context959* @param[in,out] ctx ptr to the channel unit context960* @param[in] num_channels number of channels to process961*/962static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,963int num_channels)964{965int ch_num;966967for (ch_num = 0; ch_num < num_channels; ch_num++)968get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,969ctx->num_subbands);970}971972/**973* Decode number of gain control points.974*975* @param[in] gb the GetBit context976* @param[in,out] ctx ptr to the channel unit context977* @param[in] ch_num channel to process978* @param[in] coded_subbands number of subbands to process979* @return result code: 0 = OK, otherwise - error code980*/981static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,982int ch_num, int coded_subbands)983{984int i, delta, delta_bits, min_val;985Atrac3pChanParams *chan = &ctx->channels[ch_num];986Atrac3pChanParams *ref_chan = &ctx->channels[0];987988switch (get_bits(gb, 2)) { /* switch according to coding mode */989case 0: /* fixed-length coding */990for (i = 0; i < coded_subbands; i++)991chan->gain_data[i].num_points = get_bits(gb, 3);992break;993case 1: /* variable-length coding */994for (i = 0; i < coded_subbands; i++)995chan->gain_data[i].num_points =996get_vlc2(gb, gain_vlc_tabs[0].table,997gain_vlc_tabs[0].bits, 1);998break;999case 2:1000if (ch_num) { /* VLC modulo delta to master channel */1001for (i = 0; i < coded_subbands; i++) {1002delta = get_vlc2(gb, gain_vlc_tabs[1].table,1003gain_vlc_tabs[1].bits, 1);1004chan->gain_data[i].num_points =1005(ref_chan->gain_data[i].num_points + delta) & 7;1006}1007} else { /* VLC modulo delta to previous */1008chan->gain_data[0].num_points =1009get_vlc2(gb, gain_vlc_tabs[0].table,1010gain_vlc_tabs[0].bits, 1);10111012for (i = 1; i < coded_subbands; i++) {1013delta = get_vlc2(gb, gain_vlc_tabs[1].table,1014gain_vlc_tabs[1].bits, 1);1015chan->gain_data[i].num_points =1016(chan->gain_data[i - 1].num_points + delta) & 7;1017}1018}1019break;1020case 3:1021if (ch_num) { /* copy data from master channel */1022for (i = 0; i < coded_subbands; i++)1023chan->gain_data[i].num_points =1024ref_chan->gain_data[i].num_points;1025} else { /* shorter delta to min */1026delta_bits = get_bits(gb, 2);1027min_val = get_bits(gb, 3);10281029for (i = 0; i < coded_subbands; i++) {1030chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);1031if (chan->gain_data[i].num_points > 7)1032return AVERROR_INVALIDDATA;1033}1034}1035}10361037return 0;1038}10391040/**1041* Implements coding mode 3 (slave) for gain compensation levels.1042*1043* @param[out] dst ptr to the output array1044* @param[in] ref ptr to the reference channel1045*/1046static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)1047{1048int i;10491050for (i = 0; i < dst->num_points; i++)1051dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];1052}10531054/**1055* Implements coding mode 1 (master) for gain compensation levels.1056*1057* @param[in] gb the GetBit context1058* @param[in] ctx ptr to the channel unit context1059* @param[out] dst ptr to the output array1060*/1061static inline void gainc_level_mode1m(GetBitContext *gb,1062Atrac3pChanUnitCtx *ctx,1063AtracGainInfo *dst)1064{1065int i, delta;10661067if (dst->num_points > 0)1068dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,1069gain_vlc_tabs[2].bits, 1);10701071for (i = 1; i < dst->num_points; i++) {1072delta = get_vlc2(gb, gain_vlc_tabs[3].table,1073gain_vlc_tabs[3].bits, 1);1074dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;1075}1076}10771078/**1079* Decode level code for each gain control point.1080*1081* @param[in] gb the GetBit context1082* @param[in,out] ctx ptr to the channel unit context1083* @param[in] ch_num channel to process1084* @param[in] coded_subbands number of subbands to process1085* @return result code: 0 = OK, otherwise - error code1086*/1087static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1088int ch_num, int coded_subbands)1089{1090int sb, i, delta, delta_bits, min_val, pred;1091Atrac3pChanParams *chan = &ctx->channels[ch_num];1092Atrac3pChanParams *ref_chan = &ctx->channels[0];10931094switch (get_bits(gb, 2)) { /* switch according to coding mode */1095case 0: /* fixed-length coding */1096for (sb = 0; sb < coded_subbands; sb++)1097for (i = 0; i < chan->gain_data[sb].num_points; i++)1098chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);1099break;1100case 1:1101if (ch_num) { /* VLC modulo delta to master channel */1102for (sb = 0; sb < coded_subbands; sb++)1103for (i = 0; i < chan->gain_data[sb].num_points; i++) {1104delta = get_vlc2(gb, gain_vlc_tabs[5].table,1105gain_vlc_tabs[5].bits, 1);1106pred = (i >= ref_chan->gain_data[sb].num_points)1107? 7 : ref_chan->gain_data[sb].lev_code[i];1108chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;1109}1110} else { /* VLC modulo delta to previous */1111for (sb = 0; sb < coded_subbands; sb++)1112gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);1113}1114break;1115case 2:1116if (ch_num) { /* VLC modulo delta to previous or clone master */1117for (sb = 0; sb < coded_subbands; sb++)1118if (chan->gain_data[sb].num_points > 0) {1119if (get_bits1(gb))1120gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);1121else1122gainc_level_mode3s(&chan->gain_data[sb],1123&ref_chan->gain_data[sb]);1124}1125} else { /* VLC modulo delta to lev_codes of previous subband */1126if (chan->gain_data[0].num_points > 0)1127gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);11281129for (sb = 1; sb < coded_subbands; sb++)1130for (i = 0; i < chan->gain_data[sb].num_points; i++) {1131delta = get_vlc2(gb, gain_vlc_tabs[4].table,1132gain_vlc_tabs[4].bits, 1);1133pred = (i >= chan->gain_data[sb - 1].num_points)1134? 7 : chan->gain_data[sb - 1].lev_code[i];1135chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;1136}1137}1138break;1139case 3:1140if (ch_num) { /* clone master */1141for (sb = 0; sb < coded_subbands; sb++)1142gainc_level_mode3s(&chan->gain_data[sb],1143&ref_chan->gain_data[sb]);1144} else { /* shorter delta to min */1145delta_bits = get_bits(gb, 2);1146min_val = get_bits(gb, 4);11471148for (sb = 0; sb < coded_subbands; sb++)1149for (i = 0; i < chan->gain_data[sb].num_points; i++) {1150chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);1151if (chan->gain_data[sb].lev_code[i] > 15)1152return AVERROR_INVALIDDATA;1153}1154}1155break;1156}11571158return 0;1159}11601161/**1162* Implements coding mode 0 for gain compensation locations.1163*1164* @param[in] gb the GetBit context1165* @param[in] ctx ptr to the channel unit context1166* @param[out] dst ptr to the output array1167* @param[in] pos position of the value to be processed1168*/1169static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1170AtracGainInfo *dst, int pos)1171{1172int delta_bits;11731174if (!pos || dst->loc_code[pos - 1] < 15)1175dst->loc_code[pos] = get_bits(gb, 5);1176else if (dst->loc_code[pos - 1] >= 30)1177dst->loc_code[pos] = 31;1178else {1179delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;1180dst->loc_code[pos] = dst->loc_code[pos - 1] +1181get_bits(gb, delta_bits) + 1;1182}1183}11841185/**1186* Implements coding mode 1 for gain compensation locations.1187*1188* @param[in] gb the GetBit context1189* @param[in] ctx ptr to the channel unit context1190* @param[out] dst ptr to the output array1191*/1192static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1193AtracGainInfo *dst)1194{1195int i;1196VLC *tab;11971198if (dst->num_points > 0) {1199/* 1st coefficient is stored directly */1200dst->loc_code[0] = get_bits(gb, 5);12011202for (i = 1; i < dst->num_points; i++) {1203/* switch VLC according to the curve direction1204* (ascending/descending) */1205tab = (dst->lev_code[i] <= dst->lev_code[i - 1])1206? &gain_vlc_tabs[7]1207: &gain_vlc_tabs[9];1208dst->loc_code[i] = dst->loc_code[i - 1] +1209get_vlc2(gb, tab->table, tab->bits, 1);1210}1211}1212}12131214/**1215* Decode location code for each gain control point.1216*1217* @param[in] gb the GetBit context1218* @param[in,out] ctx ptr to the channel unit context1219* @param[in] ch_num channel to process1220* @param[in] coded_subbands number of subbands to process1221* @return result code: 0 = OK, otherwise - error code1222*/1223static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1224int ch_num, int coded_subbands)1225{1226int sb, i, delta, delta_bits, min_val, pred, more_than_ref;1227AtracGainInfo *dst, *ref;1228VLC *tab;1229Atrac3pChanParams *chan = &ctx->channels[ch_num];1230Atrac3pChanParams *ref_chan = &ctx->channels[0];12311232switch (get_bits(gb, 2)) { /* switch according to coding mode */1233case 0: /* sequence of numbers in ascending order */1234for (sb = 0; sb < coded_subbands; sb++)1235for (i = 0; i < chan->gain_data[sb].num_points; i++)1236gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);1237break;1238case 1:1239if (ch_num) {1240for (sb = 0; sb < coded_subbands; sb++) {1241if (chan->gain_data[sb].num_points <= 0)1242continue;1243dst = &chan->gain_data[sb];1244ref = &ref_chan->gain_data[sb];12451246/* 1st value is vlc-coded modulo delta to master */1247delta = get_vlc2(gb, gain_vlc_tabs[10].table,1248gain_vlc_tabs[10].bits, 1);1249pred = ref->num_points > 0 ? ref->loc_code[0] : 0;1250dst->loc_code[0] = (pred + delta) & 0x1F;12511252for (i = 1; i < dst->num_points; i++) {1253more_than_ref = i >= ref->num_points;1254if (dst->lev_code[i] > dst->lev_code[i - 1]) {1255/* ascending curve */1256if (more_than_ref) {1257delta =1258get_vlc2(gb, gain_vlc_tabs[9].table,1259gain_vlc_tabs[9].bits, 1);1260dst->loc_code[i] = dst->loc_code[i - 1] + delta;1261} else {1262if (get_bits1(gb))1263gainc_loc_mode0(gb, ctx, dst, i); // direct coding1264else1265dst->loc_code[i] = ref->loc_code[i]; // clone master1266}1267} else { /* descending curve */1268tab = more_than_ref ? &gain_vlc_tabs[7]1269: &gain_vlc_tabs[10];1270delta = get_vlc2(gb, tab->table, tab->bits, 1);1271if (more_than_ref)1272dst->loc_code[i] = dst->loc_code[i - 1] + delta;1273else1274dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;1275}1276}1277}1278} else /* VLC delta to previous */1279for (sb = 0; sb < coded_subbands; sb++)1280gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);1281break;1282case 2:1283if (ch_num) {1284for (sb = 0; sb < coded_subbands; sb++) {1285if (chan->gain_data[sb].num_points <= 0)1286continue;1287dst = &chan->gain_data[sb];1288ref = &ref_chan->gain_data[sb];1289if (dst->num_points > ref->num_points || get_bits1(gb))1290gainc_loc_mode1(gb, ctx, dst);1291else /* clone master for the whole subband */1292for (i = 0; i < chan->gain_data[sb].num_points; i++)1293dst->loc_code[i] = ref->loc_code[i];1294}1295} else {1296/* data for the first subband is coded directly */1297for (i = 0; i < chan->gain_data[0].num_points; i++)1298gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);12991300for (sb = 1; sb < coded_subbands; sb++) {1301if (chan->gain_data[sb].num_points <= 0)1302continue;1303dst = &chan->gain_data[sb];13041305/* 1st value is vlc-coded modulo delta to the corresponding1306* value of the previous subband if any or zero */1307delta = get_vlc2(gb, gain_vlc_tabs[6].table,1308gain_vlc_tabs[6].bits, 1);1309pred = dst[-1].num_points > 01310? dst[-1].loc_code[0] : 0;1311dst->loc_code[0] = (pred + delta) & 0x1F;13121313for (i = 1; i < dst->num_points; i++) {1314more_than_ref = i >= dst[-1].num_points;1315/* Select VLC table according to curve direction and1316* presence of prediction. */1317tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *13182 + more_than_ref + 6];1319delta = get_vlc2(gb, tab->table, tab->bits, 1);1320if (more_than_ref)1321dst->loc_code[i] = dst->loc_code[i - 1] + delta;1322else1323dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;1324}1325}1326}1327break;1328case 3:1329if (ch_num) { /* clone master or direct or direct coding */1330for (sb = 0; sb < coded_subbands; sb++)1331for (i = 0; i < chan->gain_data[sb].num_points; i++) {1332if (i >= ref_chan->gain_data[sb].num_points)1333gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);1334else1335chan->gain_data[sb].loc_code[i] =1336ref_chan->gain_data[sb].loc_code[i];1337}1338} else { /* shorter delta to min */1339delta_bits = get_bits(gb, 2) + 1;1340min_val = get_bits(gb, 5);13411342for (sb = 0; sb < coded_subbands; sb++)1343for (i = 0; i < chan->gain_data[sb].num_points; i++)1344chan->gain_data[sb].loc_code[i] = min_val + i +1345get_bits(gb, delta_bits);1346}1347break;1348}13491350/* Validate decoded information */1351for (sb = 0; sb < coded_subbands; sb++) {1352dst = &chan->gain_data[sb];1353for (i = 0; i < chan->gain_data[sb].num_points; i++) {1354if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||1355(i && dst->loc_code[i] <= dst->loc_code[i - 1])) {1356av_log(AV_LOG_ERROR,1357"Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",1358ch_num, sb, i, dst->loc_code[i]);1359return AVERROR_INVALIDDATA;1360}1361}1362}13631364return 0;1365}13661367/**1368* Decode gain control data for all channels.1369*1370* @param[in] gb the GetBit context1371* @param[in,out] ctx ptr to the channel unit context1372* @param[in] num_channels number of channels to process1373* @return result code: 0 = OK, otherwise - error code1374*/1375static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1376int num_channels)1377{1378int ch_num, coded_subbands, sb, ret;13791380for (ch_num = 0; ch_num < num_channels; ch_num++) {1381memset(ctx->channels[ch_num].gain_data, 0,1382sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);13831384if (get_bits1(gb)) { /* gain control data present? */1385coded_subbands = get_bits(gb, 4) + 1;1386if (get_bits1(gb)) /* is high band gain data replication on? */1387ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;1388else1389ctx->channels[ch_num].num_gain_subbands = coded_subbands;13901391if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||1392(ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||1393(ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands)) < 0)1394return ret;13951396if (coded_subbands > 0) { /* propagate gain data if requested */1397for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)1398ctx->channels[ch_num].gain_data[sb] =1399ctx->channels[ch_num].gain_data[sb - 1];1400}1401} else {1402ctx->channels[ch_num].num_gain_subbands = 0;1403}1404}14051406return 0;1407}14081409/**1410* Decode envelope for all tones of a channel.1411*1412* @param[in] gb the GetBit context1413* @param[in,out] ctx ptr to the channel unit context1414* @param[in] ch_num channel to process1415* @param[in] band_has_tones ptr to an array of per-band-flags:1416* 1 - tone data present1417*/1418static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1419int ch_num, int band_has_tones[])1420{1421int sb;1422Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;1423Atrac3pWavesData *ref = ctx->channels[0].tones_info;14241425if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */1426for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1427if (!band_has_tones[sb])1428continue;1429dst[sb].pend_env.has_start_point = get_bits1(gb);1430dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point1431? get_bits(gb, 5) : -1;1432dst[sb].pend_env.has_stop_point = get_bits1(gb);1433dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point1434? get_bits(gb, 5) : 32;1435}1436} else { /* mode 1(slave only): copy master */1437for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1438if (!band_has_tones[sb])1439continue;1440dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;1441dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;1442dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;1443dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;1444}1445}1446}14471448/**1449* Decode number of tones for each subband of a channel.1450*1451* @param[in] gb the GetBit context1452* @param[in,out] ctx ptr to the channel unit context1453* @param[in] ch_num channel to process1454* @param[in] band_has_tones ptr to an array of per-band-flags:1455* 1 - tone data present1456* @return result code: 0 = OK, otherwise - error code1457*/1458static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1459int ch_num, int band_has_tones[])1460{1461int mode, sb, delta;1462Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;1463Atrac3pWavesData *ref = ctx->channels[0].tones_info;14641465mode = get_bits(gb, ch_num + 1);1466switch (mode) {1467case 0: /** fixed-length coding */1468for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)1469if (band_has_tones[sb])1470dst[sb].num_wavs = get_bits(gb, 4);1471break;1472case 1: /** variable-length coding */1473for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)1474if (band_has_tones[sb])1475dst[sb].num_wavs =1476get_vlc2(gb, tone_vlc_tabs[1].table,1477tone_vlc_tabs[1].bits, 1);1478break;1479case 2: /** VLC modulo delta to master (slave only) */1480for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)1481if (band_has_tones[sb]) {1482delta = get_vlc2(gb, tone_vlc_tabs[2].table,1483tone_vlc_tabs[2].bits, 1);1484delta = sign_extend(delta, 3);1485dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;1486}1487break;1488case 3: /** copy master (slave only) */1489for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)1490if (band_has_tones[sb])1491dst[sb].num_wavs = ref[sb].num_wavs;1492break;1493}14941495/** initialize start tone index for each subband */1496for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)1497if (band_has_tones[sb]) {1498if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {1499av_log(AV_LOG_ERROR,1500"Too many tones: %d (max. 48)!\n",1501ctx->waves_info->tones_index + dst[sb].num_wavs);1502return AVERROR_INVALIDDATA;1503}1504dst[sb].start_index = ctx->waves_info->tones_index;1505ctx->waves_info->tones_index += dst[sb].num_wavs;1506}15071508return 0;1509}15101511/**1512* Decode frequency information for each subband of a channel.1513*1514* @param[in] gb the GetBit context1515* @param[in,out] ctx ptr to the channel unit context1516* @param[in] ch_num channel to process1517* @param[in] band_has_tones ptr to an array of per-band-flags:1518* 1 - tone data present1519*/1520static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1521int ch_num, int band_has_tones[])1522{1523int sb, i, direction, nbits, pred, delta;1524Atrac3pWaveParam *iwav, *owav;1525Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;1526Atrac3pWavesData *ref = ctx->channels[0].tones_info;15271528if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */1529for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1530if (!band_has_tones[sb] || !dst[sb].num_wavs)1531continue;1532iwav = &ctx->waves_info->waves[dst[sb].start_index];1533direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;1534if (direction) { /** packed numbers in descending order */1535if (dst[sb].num_wavs)1536iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);1537for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {1538nbits = av_log2(iwav[i+1].freq_index) + 1;1539iwav[i].freq_index = get_bits(gb, nbits);1540}1541} else { /** packed numbers in ascending order */1542for (i = 0; i < dst[sb].num_wavs; i++) {1543if (!i || iwav[i - 1].freq_index < 512)1544iwav[i].freq_index = get_bits(gb, 10);1545else {1546nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;1547iwav[i].freq_index = get_bits(gb, nbits) +15481024 - (1 << nbits);1549}1550}1551}1552}1553} else { /* mode 1: VLC modulo delta to master (slave only) */1554for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1555if (!band_has_tones[sb] || !dst[sb].num_wavs)1556continue;1557iwav = &ctx->waves_info->waves[ref[sb].start_index];1558owav = &ctx->waves_info->waves[dst[sb].start_index];1559for (i = 0; i < dst[sb].num_wavs; i++) {1560delta = get_vlc2(gb, tone_vlc_tabs[6].table,1561tone_vlc_tabs[6].bits, 1);1562delta = sign_extend(delta, 8);1563pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :1564(ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);1565owav[i].freq_index = (pred + delta) & 0x3FF;1566}1567}1568}1569}15701571/**1572* Decode amplitude information for each subband of a channel.1573*1574* @param[in] gb the GetBit context1575* @param[in,out] ctx ptr to the channel unit context1576* @param[in] ch_num channel to process1577* @param[in] band_has_tones ptr to an array of per-band-flags:1578* 1 - tone data present1579*/1580static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1581int ch_num, int band_has_tones[])1582{1583int mode, sb, j, i, diff, maxdiff, fi, delta, pred;1584Atrac3pWaveParam *wsrc, *wref;1585int refwaves[48] = { 0 };1586Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;1587Atrac3pWavesData *ref = ctx->channels[0].tones_info;15881589if (ch_num) {1590for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1591if (!band_has_tones[sb] || !dst[sb].num_wavs)1592continue;1593wsrc = &ctx->waves_info->waves[dst[sb].start_index];1594wref = &ctx->waves_info->waves[ref[sb].start_index];1595for (j = 0; j < dst[sb].num_wavs; j++) {1596for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {1597diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);1598if (diff < maxdiff) {1599maxdiff = diff;1600fi = i;1601}1602}16031604if (maxdiff < 8)1605refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;1606else if (j < ref[sb].num_wavs)1607refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;1608else1609refwaves[dst[sb].start_index + j] = -1;1610}1611}1612}16131614mode = get_bits(gb, ch_num + 1);16151616switch (mode) {1617case 0: /** fixed-length coding */1618for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1619if (!band_has_tones[sb] || !dst[sb].num_wavs)1620continue;1621if (ctx->waves_info->amplitude_mode)1622for (i = 0; i < dst[sb].num_wavs; i++)1623ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);1624else1625ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);1626}1627break;1628case 1: /** min + VLC delta */1629for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1630if (!band_has_tones[sb] || !dst[sb].num_wavs)1631continue;1632if (ctx->waves_info->amplitude_mode)1633for (i = 0; i < dst[sb].num_wavs; i++)1634ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =1635get_vlc2(gb, tone_vlc_tabs[3].table,1636tone_vlc_tabs[3].bits, 1) + 20;1637else1638ctx->waves_info->waves[dst[sb].start_index].amp_sf =1639get_vlc2(gb, tone_vlc_tabs[4].table,1640tone_vlc_tabs[4].bits, 1) + 24;1641}1642break;1643case 2: /** VLC modulo delta to master (slave only) */1644for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1645if (!band_has_tones[sb] || !dst[sb].num_wavs)1646continue;1647for (i = 0; i < dst[sb].num_wavs; i++) {1648delta = get_vlc2(gb, tone_vlc_tabs[5].table,1649tone_vlc_tabs[5].bits, 1);1650delta = sign_extend(delta, 5);1651pred = refwaves[dst[sb].start_index + i] >= 0 ?1652ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;1653ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;1654}1655}1656break;1657case 3: /** clone master (slave only) */1658for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1659if (!band_has_tones[sb])1660continue;1661for (i = 0; i < dst[sb].num_wavs; i++)1662ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =1663refwaves[dst[sb].start_index + i] >= 01664? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf1665: 32;1666}1667break;1668}1669}16701671/**1672* Decode phase information for each subband of a channel.1673*1674* @param[in] gb the GetBit context1675* @param[in,out] ctx ptr to the channel unit context1676* @param[in] ch_num channel to process1677* @param[in] band_has_tones ptr to an array of per-band-flags:1678* 1 - tone data present1679*/1680static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1681int ch_num, int band_has_tones[])1682{1683int sb, i;1684Atrac3pWaveParam *wparam;1685Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;16861687for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {1688if (!band_has_tones[sb])1689continue;1690wparam = &ctx->waves_info->waves[dst[sb].start_index];1691for (i = 0; i < dst[sb].num_wavs; i++)1692wparam[i].phase_index = get_bits(gb, 5);1693}1694}16951696/**1697* Decode tones info for all channels.1698*1699* @param[in] gb the GetBit context1700* @param[in,out] ctx ptr to the channel unit context1701* @param[in] num_channels number of channels to process1702* @return result code: 0 = OK, otherwise - error code1703*/1704static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1705int num_channels)1706{1707int ch_num, i, ret;1708int band_has_tones[16];17091710for (ch_num = 0; ch_num < num_channels; ch_num++)1711memset(ctx->channels[ch_num].tones_info, 0,1712sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);17131714ctx->waves_info->tones_present = get_bits1(gb);1715if (!ctx->waves_info->tones_present)1716return 0;17171718memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));17191720ctx->waves_info->amplitude_mode = get_bits1(gb);1721if (!ctx->waves_info->amplitude_mode) {1722avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");1723return AVERROR_PATCHWELCOME;1724}17251726ctx->waves_info->num_tone_bands =1727get_vlc2(gb, tone_vlc_tabs[0].table,1728tone_vlc_tabs[0].bits, 1) + 1;17291730if (num_channels == 2) {1731get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);1732get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);1733get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);1734}17351736ctx->waves_info->tones_index = 0;17371738for (ch_num = 0; ch_num < num_channels; ch_num++) {1739for (i = 0; i < ctx->waves_info->num_tone_bands; i++)1740band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];17411742decode_tones_envelope(gb, ctx, ch_num, band_has_tones);1743if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones)) < 0)1744return ret;17451746decode_tones_frequency(gb, ctx, ch_num, band_has_tones);1747decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);1748decode_tones_phase(gb, ctx, ch_num, band_has_tones);1749}17501751if (num_channels == 2) {1752for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {1753if (ctx->waves_info->tone_sharing[i])1754ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];17551756if (ctx->waves_info->tone_master[i])1757FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],1758ctx->channels[1].tones_info[i]);1759}1760}17611762return 0;1763}17641765int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,1766int num_channels)1767{1768int ret;17691770/* parse sound header */1771ctx->num_quant_units = get_bits(gb, 5) + 1;1772if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {1773av_log(AV_LOG_ERROR,1774"Invalid number of quantization units: %d!\n",1775ctx->num_quant_units);1776return AVERROR_INVALIDDATA;1777}17781779ctx->mute_flag = get_bits1(gb);17801781/* decode various sound parameters */1782if ((ret = decode_quant_wordlen(gb, ctx, num_channels)) < 0)1783return ret;17841785ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;1786ctx->num_coded_subbands = ctx->used_quant_units1787? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 11788: 0;17891790if ((ret = decode_scale_factors(gb, ctx, num_channels)) < 0)1791return ret;17921793if ((ret = decode_code_table_indexes(gb, ctx, num_channels)) < 0)1794return ret;17951796decode_spectrum(gb, ctx, num_channels);17971798if (num_channels == 2) {1799get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);1800get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);1801}18021803decode_window_shape(gb, ctx, num_channels);18041805if ((ret = decode_gainc_data(gb, ctx, num_channels)) < 0)1806return ret;18071808if ((ret = decode_tones_info(gb, ctx, num_channels)) < 0)1809return ret;18101811/* decode global noise info */1812ctx->noise_present = get_bits1(gb);1813if (ctx->noise_present) {1814ctx->noise_level_index = get_bits(gb, 4);1815ctx->noise_table_index = get_bits(gb, 4);1816}18171818return 0;1819}182018211822