2 * ATRAC3+ compatible decoder
4 * Copyright (c) 2010-2013 Maxim Poliakovski
6 * This file is part of FFmpeg.
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.
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.
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
25 * Bitstream parser for ATRAC3+ decoder.
28 #include "libavutil/avassert.h"
31 #include "atrac3plus.h"
32 #include "atrac3plus_data.h"
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];
43 * Generate canonical VLC table from given descriptor.
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
50 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
51 int *tab_offset, VLC *out_vlc)
56 int min_len = *cb++; // get shortest codeword length
57 int max_len = *cb++; // get longest codeword length
59 for (b = min_len; b <= max_len; b++) {
60 for (i = *cb++; i > 0; i--) {
61 av_assert0(index < 256);
67 out_vlc->table = &tables_data[*tab_offset];
68 out_vlc->table_allocated = 1 << max_len;
70 ff_init_vlc_from_lengths(out_vlc, max_len, index, bits, 1,
71 xlat, 1, 1, 0, INIT_VLC_USE_NEW_STATIC, NULL);
73 *tab_offset += 1 << max_len;
76 av_cold void ff_atrac3p_init_vlcs(void)
78 int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
80 static const uint8_t wl_nb_bits[4] = { 2, 3, 5, 5 };
81 static const uint8_t wl_nb_codes[4] = { 3, 5, 8, 8 };
82 static const uint8_t * const wl_bits[4] = {
83 atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
84 atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
86 static const uint8_t * const wl_codes[4] = {
87 atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
88 atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
90 static const uint8_t * const wl_xlats[4] = {
91 atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
94 static const uint8_t ct_nb_bits[4] = { 3, 4, 4, 4 };
95 static const uint8_t ct_nb_codes[4] = { 4, 8, 8, 8 };
96 static const uint8_t * const ct_bits[4] = {
97 atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
98 atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
100 static const uint8_t * const ct_codes[4] = {
101 atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
102 atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
104 static const uint8_t * const ct_xlats[4] = {
105 NULL, NULL, atrac3p_ct_huff_xlat1, NULL
108 static const uint8_t sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
109 static const uint8_t sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
110 static const uint8_t * const sf_bits[8] = {
111 atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
112 atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
113 atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
115 static const uint16_t * const sf_codes[8] = {
116 atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
117 atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
118 atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
120 static const uint8_t * const sf_xlats[8] = {
121 atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
122 atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
125 static const uint8_t * const gain_cbs[11] = {
126 atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
127 atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
128 atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
129 atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
130 atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
131 atrac3p_huff_gain_loc5_cb
133 static const uint8_t * const gain_xlats[11] = {
134 NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
135 atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
136 atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
137 atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
138 atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
141 static const uint8_t * const tone_cbs[7] = {
142 atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,
143 atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,
144 atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
147 static const uint8_t * const tone_xlats[7] = {
148 NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
149 atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
150 atrac3p_huff_freq_xlat
153 for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
154 wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
155 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
156 ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
157 ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
159 ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
163 INIT_VLC_USE_NEW_STATIC);
165 ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
169 INIT_VLC_USE_NEW_STATIC);
171 wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
172 ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
175 for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
176 sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
177 sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
179 ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
183 INIT_VLC_USE_NEW_STATIC);
184 sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
189 /* build huffman tables for spectrum decoding */
190 for (i = 0; i < 112; i++) {
191 if (atrac3p_spectra_tabs[i].redirect < 0)
192 build_canonical_huff(atrac3p_spectra_tabs[i].cb,
193 atrac3p_spectra_tabs[i].xlat,
194 &tab_offset, &spec_vlc_tabs[i]);
195 else /* Reuse already initialized VLC table */
196 spec_vlc_tabs[i] = spec_vlc_tabs[atrac3p_spectra_tabs[i].redirect];
199 /* build huffman tables for gain data decoding */
200 for (i = 0; i < 11; i++)
201 build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
203 /* build huffman tables for tone decoding */
204 for (i = 0; i < 7; i++)
205 build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
209 * Decode number of coded quantization units.
211 * @param[in] gb the GetBit context
212 * @param[in,out] chan ptr to the channel parameters
213 * @param[in,out] ctx ptr to the channel unit context
214 * @param[in] avctx ptr to the AVCodecContext
215 * @return result code: 0 = OK, otherwise - error code
217 static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,
218 Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
220 chan->fill_mode = get_bits(gb, 2);
221 if (!chan->fill_mode) {
222 chan->num_coded_vals = ctx->num_quant_units;
224 chan->num_coded_vals = get_bits(gb, 5);
225 if (chan->num_coded_vals > ctx->num_quant_units) {
226 av_log(avctx, AV_LOG_ERROR,
227 "Invalid number of transmitted units!\n");
228 return AVERROR_INVALIDDATA;
231 if (chan->fill_mode == 3)
232 chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
239 * Add weighting coefficients to the decoded word-length information.
241 * @param[in,out] ctx ptr to the channel unit context
242 * @param[in,out] chan ptr to the channel parameters
243 * @param[in] wtab_idx index of the table of weights
244 * @param[in] avctx ptr to the AVCodecContext
245 * @return result code: 0 = OK, otherwise - error code
247 static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
248 Atrac3pChanParams *chan, int wtab_idx,
249 AVCodecContext *avctx)
252 const int8_t *weights_tab =
253 &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
255 for (i = 0; i < ctx->num_quant_units; i++) {
256 chan->qu_wordlen[i] += weights_tab[i];
257 if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
258 av_log(avctx, AV_LOG_ERROR,
259 "WL index out of range: pos=%d, val=%d!\n",
260 i, chan->qu_wordlen[i]);
261 return AVERROR_INVALIDDATA;
269 * Subtract weighting coefficients from decoded scalefactors.
271 * @param[in,out] ctx ptr to the channel unit context
272 * @param[in,out] chan ptr to the channel parameters
273 * @param[in] wtab_idx index of table of weights
274 * @param[in] avctx ptr to the AVCodecContext
275 * @return result code: 0 = OK, otherwise - error code
277 static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
278 Atrac3pChanParams *chan, int wtab_idx,
279 AVCodecContext *avctx)
282 const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
284 for (i = 0; i < ctx->used_quant_units; i++) {
285 chan->qu_sf_idx[i] -= weights_tab[i];
286 if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
287 av_log(avctx, AV_LOG_ERROR,
288 "SF index out of range: pos=%d, val=%d!\n",
289 i, chan->qu_sf_idx[i]);
290 return AVERROR_INVALIDDATA;
298 * Unpack vector quantization tables.
300 * @param[in] start_val start value for the unpacked table
301 * @param[in] shape_vec ptr to table to unpack
302 * @param[out] dst ptr to output array
303 * @param[in] num_values number of values to unpack
305 static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
306 int *dst, int num_values)
311 dst[0] = dst[1] = dst[2] = start_val;
312 for (i = 3; i < num_values; i++)
313 dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
317 #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \
318 start_val = get_bits((gb), 6); \
319 unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
323 * Decode word length for each quantization unit of a channel.
325 * @param[in] gb the GetBit context
326 * @param[in,out] ctx ptr to the channel unit context
327 * @param[in] ch_num channel to process
328 * @param[in] avctx ptr to the AVCodecContext
329 * @return result code: 0 = OK, otherwise - error code
331 static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
332 int ch_num, AVCodecContext *avctx)
334 int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
337 Atrac3pChanParams *chan = &ctx->channels[ch_num];
338 Atrac3pChanParams *ref_chan = &ctx->channels[0];
342 switch (get_bits(gb, 2)) { /* switch according to coding mode */
343 case 0: /* coded using constant number of bits */
344 for (i = 0; i < ctx->num_quant_units; i++)
345 chan->qu_wordlen[i] = get_bits(gb, 3);
349 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
352 if (chan->num_coded_vals) {
353 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
355 for (i = 0; i < chan->num_coded_vals; i++) {
356 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
357 chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
361 weight_idx = get_bits(gb, 2);
362 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
365 if (chan->num_coded_vals) {
366 pos = get_bits(gb, 5);
367 if (pos > chan->num_coded_vals) {
368 av_log(avctx, AV_LOG_ERROR,
369 "WL mode 1: invalid position!\n");
370 return AVERROR_INVALIDDATA;
373 delta_bits = get_bits(gb, 2);
374 min_val = get_bits(gb, 3);
376 for (i = 0; i < pos; i++)
377 chan->qu_wordlen[i] = get_bits(gb, 3);
379 for (i = pos; i < chan->num_coded_vals; i++)
380 chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;
385 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
388 if (ch_num && chan->num_coded_vals) {
389 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
390 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
391 chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
393 for (i = 1; i < chan->num_coded_vals; i++) {
394 diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
395 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
396 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
398 } else if (chan->num_coded_vals) {
399 flag = get_bits(gb, 1);
400 vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
402 start_val = get_bits(gb, 3);
403 unpack_vq_shape(start_val,
404 &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
405 chan->qu_wordlen, chan->num_coded_vals);
408 for (i = 0; i < chan->num_coded_vals; i++) {
409 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
410 chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
413 for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
414 if (!get_bits1(gb)) {
415 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
416 get_vlc2(gb, vlc_tab->table,
417 vlc_tab->bits, 1)) & 7;
418 chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
419 get_vlc2(gb, vlc_tab->table,
420 vlc_tab->bits, 1)) & 7;
423 if (chan->num_coded_vals & 1)
424 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
425 get_vlc2(gb, vlc_tab->table,
426 vlc_tab->bits, 1)) & 7;
431 weight_idx = get_bits(gb, 2);
432 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
435 if (chan->num_coded_vals) {
436 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
438 /* first coefficient is coded directly */
439 chan->qu_wordlen[0] = get_bits(gb, 3);
441 for (i = 1; i < chan->num_coded_vals; i++) {
442 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
443 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
449 if (chan->fill_mode == 2) {
450 for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
451 chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
452 } else if (chan->fill_mode == 3) {
453 pos = ch_num ? chan->num_coded_vals + chan->split_point
454 : ctx->num_quant_units - chan->split_point;
455 if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {
456 av_log(avctx, AV_LOG_ERROR, "Split point beyond array\n");
457 pos = FF_ARRAY_ELEMS(chan->qu_wordlen);
459 for (i = chan->num_coded_vals; i < pos; i++)
460 chan->qu_wordlen[i] = 1;
464 return add_wordlen_weights(ctx, chan, weight_idx, avctx);
470 * Decode scale factor indexes for each quant unit of a channel.
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
478 static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
479 int ch_num, AVCodecContext *avctx)
481 int i, weight_idx = 0, delta, diff, num_long_vals,
482 delta_bits, min_val, vlc_sel, start_val;
484 Atrac3pChanParams *chan = &ctx->channels[ch_num];
485 Atrac3pChanParams *ref_chan = &ctx->channels[0];
487 switch (get_bits(gb, 2)) { /* switch according to coding mode */
488 case 0: /* coded using constant number of bits */
489 for (i = 0; i < ctx->used_quant_units; i++)
490 chan->qu_sf_idx[i] = get_bits(gb, 6);
494 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
496 for (i = 0; i < ctx->used_quant_units; i++) {
497 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
498 chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
501 weight_idx = get_bits(gb, 2);
502 if (weight_idx == 3) {
503 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
505 num_long_vals = get_bits(gb, 5);
506 delta_bits = get_bits(gb, 2);
507 min_val = get_bits(gb, 4) - 7;
509 for (i = 0; i < num_long_vals; i++)
510 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
511 get_bits(gb, 4) - 7) & 0x3F;
513 /* all others are: min_val + delta */
514 for (i = num_long_vals; i < ctx->used_quant_units; i++)
515 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
516 get_bitsz(gb, delta_bits)) & 0x3F;
518 num_long_vals = get_bits(gb, 5);
519 delta_bits = get_bits(gb, 3);
520 min_val = get_bits(gb, 6);
521 if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
522 av_log(avctx, AV_LOG_ERROR,
523 "SF mode 1: invalid parameters!\n");
524 return AVERROR_INVALIDDATA;
527 /* read full-precision SF indexes */
528 for (i = 0; i < num_long_vals; i++)
529 chan->qu_sf_idx[i] = get_bits(gb, 6);
531 /* all others are: min_val + delta */
532 for (i = num_long_vals; i < ctx->used_quant_units; i++)
533 chan->qu_sf_idx[i] = (min_val +
534 get_bitsz(gb, delta_bits)) & 0x3F;
540 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
542 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
543 chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
545 for (i = 1; i < ctx->used_quant_units; i++) {
546 diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
547 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
548 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
551 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
553 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
555 for (i = 0; i < ctx->used_quant_units; i++) {
556 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
557 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
558 sign_extend(delta, 4)) & 0x3F;
564 /* copy coefficients from reference channel */
565 for (i = 0; i < ctx->used_quant_units; i++)
566 chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
568 weight_idx = get_bits(gb, 2);
569 vlc_sel = get_bits(gb, 2);
570 vlc_tab = &sf_vlc_tabs[vlc_sel];
572 if (weight_idx == 3) {
573 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
575 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
577 diff = (get_bits(gb, 4) + 56) & 0x3F;
578 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
580 for (i = 1; i < ctx->used_quant_units; i++) {
581 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
582 diff = (diff + sign_extend(delta, 4)) & 0x3F;
583 chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
586 /* 1st coefficient is coded directly */
587 chan->qu_sf_idx[0] = get_bits(gb, 6);
589 for (i = 1; i < ctx->used_quant_units; i++) {
590 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
591 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
598 if (weight_idx && weight_idx < 3)
599 return subtract_sf_weights(ctx, chan, weight_idx, avctx);
605 * Decode word length information for each channel.
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
613 static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
614 int num_channels, AVCodecContext *avctx)
618 for (ch_num = 0; ch_num < num_channels; ch_num++) {
619 memset(ctx->channels[ch_num].qu_wordlen, 0,
620 sizeof(ctx->channels[ch_num].qu_wordlen));
622 if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
626 /* scan for last non-zero coeff in both channels and
627 * set number of quant units having coded spectrum */
628 for (i = ctx->num_quant_units - 1; i >= 0; i--)
629 if (ctx->channels[0].qu_wordlen[i] ||
630 (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
632 ctx->used_quant_units = i + 1;
638 * Decode scale factor indexes for each channel.
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
646 static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
647 int num_channels, AVCodecContext *avctx)
651 if (!ctx->used_quant_units)
654 for (ch_num = 0; ch_num < num_channels; ch_num++) {
655 memset(ctx->channels[ch_num].qu_sf_idx, 0,
656 sizeof(ctx->channels[ch_num].qu_sf_idx));
658 if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
666 * Decode number of code table values.
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
673 static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
674 AVCodecContext *avctx)
679 num_coded_vals = get_bits(gb, 5);
680 if (num_coded_vals > ctx->used_quant_units) {
681 av_log(avctx, AV_LOG_ERROR,
682 "Invalid number of code table indexes: %d!\n", num_coded_vals);
683 return AVERROR_INVALIDDATA;
685 return num_coded_vals;
687 return ctx->used_quant_units;
690 #define DEC_CT_IDX_COMMON(OP) \
691 num_vals = get_num_ct_values(gb, ctx, avctx); \
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); \
703 #define CODING_DIRECT get_bits(gb, num_bits)
705 #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
707 #define CODING_VLC_DELTA \
709 : (pred + get_vlc2(gb, delta_vlc->table, \
710 delta_vlc->bits, 1)) & mask; \
711 pred = chan->qu_tab_idx[i]
713 #define CODING_VLC_DIFF \
714 (ref_chan->qu_tab_idx[i] + \
715 get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
718 * Decode code table indexes for each quant unit of a channel.
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
726 static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
727 int ch_num, AVCodecContext *avctx)
729 int i, num_vals, num_bits, pred;
730 int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
731 VLC *vlc_tab, *delta_vlc;
732 Atrac3pChanParams *chan = &ctx->channels[ch_num];
733 Atrac3pChanParams *ref_chan = &ctx->channels[0];
735 chan->table_type = get_bits1(gb);
737 switch (get_bits(gb, 2)) { /* switch according to coding mode */
738 case 0: /* directly coded */
739 num_bits = ctx->use_full_table + 2;
740 DEC_CT_IDX_COMMON(CODING_DIRECT);
742 case 1: /* entropy-coded */
743 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
745 DEC_CT_IDX_COMMON(CODING_VLC);
747 case 2: /* entropy-coded delta */
748 if (ctx->use_full_table) {
749 vlc_tab = &ct_vlc_tabs[1];
750 delta_vlc = &ct_vlc_tabs[2];
752 vlc_tab = ct_vlc_tabs;
753 delta_vlc = ct_vlc_tabs;
756 DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
758 case 3: /* entropy-coded difference to master */
760 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
762 DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
771 * Decode code table indexes for each channel.
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
779 static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
780 int num_channels, AVCodecContext *avctx)
784 if (!ctx->used_quant_units)
787 ctx->use_full_table = get_bits1(gb);
789 for (ch_num = 0; ch_num < num_channels; ch_num++) {
790 memset(ctx->channels[ch_num].qu_tab_idx, 0,
791 sizeof(ctx->channels[ch_num].qu_tab_idx));
793 if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
801 * Decode huffman-coded spectral lines for a given quant unit.
803 * This is a generalized version for all known coding modes.
804 * Its speed can be improved by creating separate functions for each mode.
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
812 static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
813 VLC *vlc_tab, int16_t *out, const int num_specs)
816 int group_size = tab->group_size;
817 int num_coeffs = tab->num_coeffs;
818 int bits = tab->bits;
819 int is_signed = tab->is_signed;
822 for (pos = 0; pos < num_specs;) {
823 if (group_size == 1 || get_bits1(gb)) {
824 for (j = 0; j < group_size; j++) {
825 val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
827 for (i = 0; i < num_coeffs; i++) {
828 cf = av_mod_uintp2(val, bits);
830 cf = sign_extend(cf, bits);
831 else if (cf && get_bits1(gb))
838 } else /* group skipped */
839 pos += group_size * num_coeffs;
844 * Decode huffman-coded IMDCT spectrum for all channels.
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
851 static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
852 int num_channels, AVCodecContext *avctx)
854 int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
855 const Atrac3pSpecCodeTab *tab;
856 Atrac3pChanParams *chan;
858 for (ch_num = 0; ch_num < num_channels; ch_num++) {
859 chan = &ctx->channels[ch_num];
861 memset(chan->spectrum, 0, sizeof(chan->spectrum));
863 /* set power compensation level to disabled */
864 memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
866 for (qu = 0; qu < ctx->used_quant_units; qu++) {
867 num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
868 ff_atrac3p_qu_to_spec_pos[qu];
870 wordlen = chan->qu_wordlen[qu];
871 codetab = chan->qu_tab_idx[qu];
873 if (!ctx->use_full_table)
874 codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
876 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
877 tab = &atrac3p_spectra_tabs[tab_index];
879 decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
880 &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
882 } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
883 /* copy coefficients from master */
884 memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
885 &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
887 sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
888 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
892 /* Power compensation levels only present in the bitstream
893 * if there are more than 2 quant units. The lowest two units
894 * correspond to the frequencies 0...351 Hz, whose shouldn't
895 * be affected by the power compensation. */
896 if (ctx->used_quant_units > 2) {
897 num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
898 for (i = 0; i < num_specs; i++)
899 chan->power_levs[i] = get_bits(gb, 4);
905 * Retrieve specified amount of flag bits from the input bitstream.
906 * The data can be shortened in the case of the following two common conditions:
907 * if all bits are zero then only one signal bit = 0 will be stored,
908 * if all bits are ones then two signal bits = 1,0 will be stored.
909 * Otherwise, all necessary bits will be directly stored
910 * prefixed by two signal bits = 1,1.
912 * @param[in] gb ptr to the GetBitContext
913 * @param[out] out where to place decoded flags
914 * @param[in] num_flags number of flags to process
915 * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
917 static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
921 memset(out, 0, num_flags);
923 result = get_bits1(gb);
926 for (i = 0; i < num_flags; i++)
927 out[i] = get_bits1(gb);
929 memset(out, 1, num_flags);
936 * Decode mdct window shape flags for all channels.
938 * @param[in] gb the GetBit context
939 * @param[in,out] ctx ptr to the channel unit context
940 * @param[in] num_channels number of channels to process
942 static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
947 for (ch_num = 0; ch_num < num_channels; ch_num++)
948 get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
953 * Decode number of gain control points.
955 * @param[in] gb the GetBit context
956 * @param[in,out] ctx ptr to the channel unit context
957 * @param[in] ch_num channel to process
958 * @param[in] coded_subbands number of subbands to process
959 * @return result code: 0 = OK, otherwise - error code
961 static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
962 int ch_num, int coded_subbands)
964 int i, delta, delta_bits, min_val;
965 Atrac3pChanParams *chan = &ctx->channels[ch_num];
966 Atrac3pChanParams *ref_chan = &ctx->channels[0];
968 switch (get_bits(gb, 2)) { /* switch according to coding mode */
969 case 0: /* fixed-length coding */
970 for (i = 0; i < coded_subbands; i++)
971 chan->gain_data[i].num_points = get_bits(gb, 3);
973 case 1: /* variable-length coding */
974 for (i = 0; i < coded_subbands; i++)
975 chan->gain_data[i].num_points =
976 get_vlc2(gb, gain_vlc_tabs[0].table,
977 gain_vlc_tabs[0].bits, 1);
980 if (ch_num) { /* VLC modulo delta to master channel */
981 for (i = 0; i < coded_subbands; i++) {
982 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
983 gain_vlc_tabs[1].bits, 1);
984 chan->gain_data[i].num_points =
985 (ref_chan->gain_data[i].num_points + delta) & 7;
987 } else { /* VLC modulo delta to previous */
988 chan->gain_data[0].num_points =
989 get_vlc2(gb, gain_vlc_tabs[0].table,
990 gain_vlc_tabs[0].bits, 1);
992 for (i = 1; i < coded_subbands; i++) {
993 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
994 gain_vlc_tabs[1].bits, 1);
995 chan->gain_data[i].num_points =
996 (chan->gain_data[i - 1].num_points + delta) & 7;
1001 if (ch_num) { /* copy data from master channel */
1002 for (i = 0; i < coded_subbands; i++)
1003 chan->gain_data[i].num_points =
1004 ref_chan->gain_data[i].num_points;
1005 } else { /* shorter delta to min */
1006 delta_bits = get_bits(gb, 2);
1007 min_val = get_bits(gb, 3);
1009 for (i = 0; i < coded_subbands; i++) {
1010 chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
1011 if (chan->gain_data[i].num_points > 7)
1012 return AVERROR_INVALIDDATA;
1021 * Implements coding mode 3 (slave) for gain compensation levels.
1023 * @param[out] dst ptr to the output array
1024 * @param[in] ref ptr to the reference channel
1026 static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1030 for (i = 0; i < dst->num_points; i++)
1031 dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1035 * Implements coding mode 1 (master) for gain compensation levels.
1037 * @param[in] gb the GetBit context
1038 * @param[in] ctx ptr to the channel unit context
1039 * @param[out] dst ptr to the output array
1041 static inline void gainc_level_mode1m(GetBitContext *gb,
1042 Atrac3pChanUnitCtx *ctx,
1047 if (dst->num_points > 0)
1048 dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
1049 gain_vlc_tabs[2].bits, 1);
1051 for (i = 1; i < dst->num_points; i++) {
1052 delta = get_vlc2(gb, gain_vlc_tabs[3].table,
1053 gain_vlc_tabs[3].bits, 1);
1054 dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
1059 * Decode level code for each gain control point.
1061 * @param[in] gb the GetBit context
1062 * @param[in,out] ctx ptr to the channel unit context
1063 * @param[in] ch_num channel to process
1064 * @param[in] coded_subbands number of subbands to process
1065 * @return result code: 0 = OK, otherwise - error code
1067 static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1068 int ch_num, int coded_subbands)
1070 int sb, i, delta, delta_bits, min_val, pred;
1071 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1072 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1074 switch (get_bits(gb, 2)) { /* switch according to coding mode */
1075 case 0: /* fixed-length coding */
1076 for (sb = 0; sb < coded_subbands; sb++)
1077 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1078 chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
1081 if (ch_num) { /* VLC modulo delta to master channel */
1082 for (sb = 0; sb < coded_subbands; sb++)
1083 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1084 delta = get_vlc2(gb, gain_vlc_tabs[5].table,
1085 gain_vlc_tabs[5].bits, 1);
1086 pred = (i >= ref_chan->gain_data[sb].num_points)
1087 ? 7 : ref_chan->gain_data[sb].lev_code[i];
1088 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1090 } else { /* VLC modulo delta to previous */
1091 for (sb = 0; sb < coded_subbands; sb++)
1092 gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1096 if (ch_num) { /* VLC modulo delta to previous or clone master */
1097 for (sb = 0; sb < coded_subbands; sb++)
1098 if (chan->gain_data[sb].num_points > 0) {
1100 gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1102 gainc_level_mode3s(&chan->gain_data[sb],
1103 &ref_chan->gain_data[sb]);
1105 } else { /* VLC modulo delta to lev_codes of previous subband */
1106 if (chan->gain_data[0].num_points > 0)
1107 gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
1109 for (sb = 1; sb < coded_subbands; sb++)
1110 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1111 delta = get_vlc2(gb, gain_vlc_tabs[4].table,
1112 gain_vlc_tabs[4].bits, 1);
1113 pred = (i >= chan->gain_data[sb - 1].num_points)
1114 ? 7 : chan->gain_data[sb - 1].lev_code[i];
1115 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1120 if (ch_num) { /* clone master */
1121 for (sb = 0; sb < coded_subbands; sb++)
1122 gainc_level_mode3s(&chan->gain_data[sb],
1123 &ref_chan->gain_data[sb]);
1124 } else { /* shorter delta to min */
1125 delta_bits = get_bits(gb, 2);
1126 min_val = get_bits(gb, 4);
1128 for (sb = 0; sb < coded_subbands; sb++)
1129 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1130 chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
1131 if (chan->gain_data[sb].lev_code[i] > 15)
1132 return AVERROR_INVALIDDATA;
1142 * Implements coding mode 0 for gain compensation locations.
1144 * @param[in] gb the GetBit context
1145 * @param[in] ctx ptr to the channel unit context
1146 * @param[out] dst ptr to the output array
1147 * @param[in] pos position of the value to be processed
1149 static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1150 AtracGainInfo *dst, int pos)
1154 if (!pos || dst->loc_code[pos - 1] < 15)
1155 dst->loc_code[pos] = get_bits(gb, 5);
1156 else if (dst->loc_code[pos - 1] >= 30)
1157 dst->loc_code[pos] = 31;
1159 delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1160 dst->loc_code[pos] = dst->loc_code[pos - 1] +
1161 get_bits(gb, delta_bits) + 1;
1166 * Implements coding mode 1 for gain compensation locations.
1168 * @param[in] gb the GetBit context
1169 * @param[in] ctx ptr to the channel unit context
1170 * @param[out] dst ptr to the output array
1172 static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1178 if (dst->num_points > 0) {
1179 /* 1st coefficient is stored directly */
1180 dst->loc_code[0] = get_bits(gb, 5);
1182 for (i = 1; i < dst->num_points; i++) {
1183 /* switch VLC according to the curve direction
1184 * (ascending/descending) */
1185 tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
1187 : &gain_vlc_tabs[9];
1188 dst->loc_code[i] = dst->loc_code[i - 1] +
1189 get_vlc2(gb, tab->table, tab->bits, 1);
1195 * Decode location code for each gain control point.
1197 * @param[in] gb the GetBit context
1198 * @param[in,out] ctx ptr to the channel unit context
1199 * @param[in] ch_num channel to process
1200 * @param[in] coded_subbands number of subbands to process
1201 * @param[in] avctx ptr to the AVCodecContext
1202 * @return result code: 0 = OK, otherwise - error code
1204 static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1205 int ch_num, int coded_subbands,
1206 AVCodecContext *avctx)
1208 int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1209 AtracGainInfo *dst, *ref;
1211 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1212 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1214 switch (get_bits(gb, 2)) { /* switch according to coding mode */
1215 case 0: /* sequence of numbers in ascending order */
1216 for (sb = 0; sb < coded_subbands; sb++)
1217 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1218 gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1222 for (sb = 0; sb < coded_subbands; sb++) {
1223 if (chan->gain_data[sb].num_points <= 0)
1225 dst = &chan->gain_data[sb];
1226 ref = &ref_chan->gain_data[sb];
1228 /* 1st value is vlc-coded modulo delta to master */
1229 delta = get_vlc2(gb, gain_vlc_tabs[10].table,
1230 gain_vlc_tabs[10].bits, 1);
1231 pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1232 dst->loc_code[0] = (pred + delta) & 0x1F;
1234 for (i = 1; i < dst->num_points; i++) {
1235 more_than_ref = i >= ref->num_points;
1236 if (dst->lev_code[i] > dst->lev_code[i - 1]) {
1237 /* ascending curve */
1238 if (more_than_ref) {
1240 get_vlc2(gb, gain_vlc_tabs[9].table,
1241 gain_vlc_tabs[9].bits, 1);
1242 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1245 gainc_loc_mode0(gb, ctx, dst, i); // direct coding
1247 dst->loc_code[i] = ref->loc_code[i]; // clone master
1249 } else { /* descending curve */
1250 tab = more_than_ref ? &gain_vlc_tabs[7]
1251 : &gain_vlc_tabs[10];
1252 delta = get_vlc2(gb, tab->table, tab->bits, 1);
1254 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1256 dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1260 } else /* VLC delta to previous */
1261 for (sb = 0; sb < coded_subbands; sb++)
1262 gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
1266 for (sb = 0; sb < coded_subbands; sb++) {
1267 if (chan->gain_data[sb].num_points <= 0)
1269 dst = &chan->gain_data[sb];
1270 ref = &ref_chan->gain_data[sb];
1271 if (dst->num_points > ref->num_points || get_bits1(gb))
1272 gainc_loc_mode1(gb, ctx, dst);
1273 else /* clone master for the whole subband */
1274 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1275 dst->loc_code[i] = ref->loc_code[i];
1278 /* data for the first subband is coded directly */
1279 for (i = 0; i < chan->gain_data[0].num_points; i++)
1280 gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
1282 for (sb = 1; sb < coded_subbands; sb++) {
1283 if (chan->gain_data[sb].num_points <= 0)
1285 dst = &chan->gain_data[sb];
1287 /* 1st value is vlc-coded modulo delta to the corresponding
1288 * value of the previous subband if any or zero */
1289 delta = get_vlc2(gb, gain_vlc_tabs[6].table,
1290 gain_vlc_tabs[6].bits, 1);
1291 pred = dst[-1].num_points > 0
1292 ? dst[-1].loc_code[0] : 0;
1293 dst->loc_code[0] = (pred + delta) & 0x1F;
1295 for (i = 1; i < dst->num_points; i++) {
1296 more_than_ref = i >= dst[-1].num_points;
1297 /* Select VLC table according to curve direction and
1298 * presence of prediction. */
1299 tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
1300 2 + more_than_ref + 6];
1301 delta = get_vlc2(gb, tab->table, tab->bits, 1);
1303 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1305 dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1311 if (ch_num) { /* clone master or direct or direct coding */
1312 for (sb = 0; sb < coded_subbands; sb++)
1313 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1314 if (i >= ref_chan->gain_data[sb].num_points)
1315 gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1317 chan->gain_data[sb].loc_code[i] =
1318 ref_chan->gain_data[sb].loc_code[i];
1320 } else { /* shorter delta to min */
1321 delta_bits = get_bits(gb, 2) + 1;
1322 min_val = get_bits(gb, 5);
1324 for (sb = 0; sb < coded_subbands; sb++)
1325 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1326 chan->gain_data[sb].loc_code[i] = min_val + i +
1327 get_bits(gb, delta_bits);
1332 /* Validate decoded information */
1333 for (sb = 0; sb < coded_subbands; sb++) {
1334 dst = &chan->gain_data[sb];
1335 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1336 if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
1337 (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
1338 av_log(avctx, AV_LOG_ERROR,
1339 "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
1340 ch_num, sb, i, dst->loc_code[i]);
1341 return AVERROR_INVALIDDATA;
1350 * Decode gain control data for all channels.
1352 * @param[in] gb the GetBit context
1353 * @param[in,out] ctx ptr to the channel unit context
1354 * @param[in] num_channels number of channels to process
1355 * @param[in] avctx ptr to the AVCodecContext
1356 * @return result code: 0 = OK, otherwise - error code
1358 static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1359 int num_channels, AVCodecContext *avctx)
1361 int ch_num, coded_subbands, sb, ret;
1363 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1364 memset(ctx->channels[ch_num].gain_data, 0,
1365 sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
1367 if (get_bits1(gb)) { /* gain control data present? */
1368 coded_subbands = get_bits(gb, 4) + 1;
1369 if (get_bits1(gb)) /* is high band gain data replication on? */
1370 ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
1372 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1374 if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
1375 (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
1376 (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
1379 if (coded_subbands > 0) { /* propagate gain data if requested */
1380 for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
1381 ctx->channels[ch_num].gain_data[sb] =
1382 ctx->channels[ch_num].gain_data[sb - 1];
1385 ctx->channels[ch_num].num_gain_subbands = 0;
1393 * Decode envelope for all tones of a channel.
1395 * @param[in] gb the GetBit context
1396 * @param[in,out] ctx ptr to the channel unit context
1397 * @param[in] ch_num channel to process
1398 * @param[in] band_has_tones ptr to an array of per-band-flags:
1399 * 1 - tone data present
1401 static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1402 int ch_num, int band_has_tones[])
1405 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1406 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1408 if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1409 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1410 if (!band_has_tones[sb])
1412 dst[sb].pend_env.has_start_point = get_bits1(gb);
1413 dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
1414 ? get_bits(gb, 5) : -1;
1415 dst[sb].pend_env.has_stop_point = get_bits1(gb);
1416 dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
1417 ? get_bits(gb, 5) : 32;
1419 } else { /* mode 1(slave only): copy master */
1420 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1421 if (!band_has_tones[sb])
1423 dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
1424 dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
1425 dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
1426 dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
1432 * Decode number of tones for each subband of a channel.
1434 * @param[in] gb the GetBit context
1435 * @param[in,out] ctx ptr to the channel unit context
1436 * @param[in] ch_num channel to process
1437 * @param[in] band_has_tones ptr to an array of per-band-flags:
1438 * 1 - tone data present
1439 * @param[in] avctx ptr to the AVCodecContext
1440 * @return result code: 0 = OK, otherwise - error code
1442 static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1443 int ch_num, int band_has_tones[],
1444 AVCodecContext *avctx)
1446 int mode, sb, delta;
1447 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1448 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1450 mode = get_bits(gb, ch_num + 1);
1452 case 0: /** fixed-length coding */
1453 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1454 if (band_has_tones[sb])
1455 dst[sb].num_wavs = get_bits(gb, 4);
1457 case 1: /** variable-length coding */
1458 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1459 if (band_has_tones[sb])
1461 get_vlc2(gb, tone_vlc_tabs[1].table,
1462 tone_vlc_tabs[1].bits, 1);
1464 case 2: /** VLC modulo delta to master (slave only) */
1465 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1466 if (band_has_tones[sb]) {
1467 delta = get_vlc2(gb, tone_vlc_tabs[2].table,
1468 tone_vlc_tabs[2].bits, 1);
1469 delta = sign_extend(delta, 3);
1470 dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1473 case 3: /** copy master (slave only) */
1474 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1475 if (band_has_tones[sb])
1476 dst[sb].num_wavs = ref[sb].num_wavs;
1480 /** initialize start tone index for each subband */
1481 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1482 if (band_has_tones[sb]) {
1483 if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
1484 av_log(avctx, AV_LOG_ERROR,
1485 "Too many tones: %d (max. 48), frame: %d!\n",
1486 ctx->waves_info->tones_index + dst[sb].num_wavs,
1487 avctx->frame_number);
1488 return AVERROR_INVALIDDATA;
1490 dst[sb].start_index = ctx->waves_info->tones_index;
1491 ctx->waves_info->tones_index += dst[sb].num_wavs;
1498 * Decode frequency information for each subband of a channel.
1500 * @param[in] gb the GetBit context
1501 * @param[in,out] ctx ptr to the channel unit context
1502 * @param[in] ch_num channel to process
1503 * @param[in] band_has_tones ptr to an array of per-band-flags:
1504 * 1 - tone data present
1506 static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1507 int ch_num, int band_has_tones[])
1509 int sb, i, direction, nbits, pred, delta;
1510 Atrac3pWaveParam *iwav, *owav;
1511 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1512 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1514 if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1515 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1516 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1518 iwav = &ctx->waves_info->waves[dst[sb].start_index];
1519 direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
1520 if (direction) { /** packed numbers in descending order */
1521 if (dst[sb].num_wavs)
1522 iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
1523 for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1524 nbits = av_log2(iwav[i+1].freq_index) + 1;
1525 iwav[i].freq_index = get_bits(gb, nbits);
1527 } else { /** packed numbers in ascending order */
1528 for (i = 0; i < dst[sb].num_wavs; i++) {
1529 if (!i || iwav[i - 1].freq_index < 512)
1530 iwav[i].freq_index = get_bits(gb, 10);
1532 nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1533 iwav[i].freq_index = get_bits(gb, nbits) +
1534 1024 - (1 << nbits);
1539 } else { /* mode 1: VLC modulo delta to master (slave only) */
1540 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1541 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1543 iwav = &ctx->waves_info->waves[ref[sb].start_index];
1544 owav = &ctx->waves_info->waves[dst[sb].start_index];
1545 for (i = 0; i < dst[sb].num_wavs; i++) {
1546 delta = get_vlc2(gb, tone_vlc_tabs[6].table,
1547 tone_vlc_tabs[6].bits, 1);
1548 delta = sign_extend(delta, 8);
1549 pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
1550 (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
1551 owav[i].freq_index = (pred + delta) & 0x3FF;
1558 * Decode amplitude information for each subband of a channel.
1560 * @param[in] gb the GetBit context
1561 * @param[in,out] ctx ptr to the channel unit context
1562 * @param[in] ch_num channel to process
1563 * @param[in] band_has_tones ptr to an array of per-band-flags:
1564 * 1 - tone data present
1566 static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1567 int ch_num, int band_has_tones[])
1569 int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1570 Atrac3pWaveParam *wsrc, *wref;
1571 int refwaves[48] = { 0 };
1572 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1573 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1576 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1577 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1579 wsrc = &ctx->waves_info->waves[dst[sb].start_index];
1580 wref = &ctx->waves_info->waves[ref[sb].start_index];
1581 for (j = 0; j < dst[sb].num_wavs; j++) {
1582 for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
1583 diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
1584 if (diff < maxdiff) {
1591 refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
1592 else if (j < ref[sb].num_wavs)
1593 refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
1595 refwaves[dst[sb].start_index + j] = -1;
1600 mode = get_bits(gb, ch_num + 1);
1603 case 0: /** fixed-length coding */
1604 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1605 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1607 if (ctx->waves_info->amplitude_mode)
1608 for (i = 0; i < dst[sb].num_wavs; i++)
1609 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
1611 ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
1614 case 1: /** min + VLC delta */
1615 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1616 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1618 if (ctx->waves_info->amplitude_mode)
1619 for (i = 0; i < dst[sb].num_wavs; i++)
1620 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1621 get_vlc2(gb, tone_vlc_tabs[3].table,
1622 tone_vlc_tabs[3].bits, 1) + 20;
1624 ctx->waves_info->waves[dst[sb].start_index].amp_sf =
1625 get_vlc2(gb, tone_vlc_tabs[4].table,
1626 tone_vlc_tabs[4].bits, 1) + 24;
1629 case 2: /** VLC modulo delta to master (slave only) */
1630 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1631 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1633 for (i = 0; i < dst[sb].num_wavs; i++) {
1634 delta = get_vlc2(gb, tone_vlc_tabs[5].table,
1635 tone_vlc_tabs[5].bits, 1);
1636 delta = sign_extend(delta, 5);
1637 pred = refwaves[dst[sb].start_index + i] >= 0 ?
1638 ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
1639 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
1643 case 3: /** clone master (slave only) */
1644 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1645 if (!band_has_tones[sb])
1647 for (i = 0; i < dst[sb].num_wavs; i++)
1648 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1649 refwaves[dst[sb].start_index + i] >= 0
1650 ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
1658 * Decode phase information for each subband of a channel.
1660 * @param[in] gb the GetBit context
1661 * @param[in,out] ctx ptr to the channel unit context
1662 * @param[in] ch_num channel to process
1663 * @param[in] band_has_tones ptr to an array of per-band-flags:
1664 * 1 - tone data present
1666 static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1667 int ch_num, int band_has_tones[])
1670 Atrac3pWaveParam *wparam;
1671 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1673 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1674 if (!band_has_tones[sb])
1676 wparam = &ctx->waves_info->waves[dst[sb].start_index];
1677 for (i = 0; i < dst[sb].num_wavs; i++)
1678 wparam[i].phase_index = get_bits(gb, 5);
1683 * Decode tones info for all channels.
1685 * @param[in] gb the GetBit context
1686 * @param[in,out] ctx ptr to the channel unit context
1687 * @param[in] num_channels number of channels to process
1688 * @param[in] avctx ptr to the AVCodecContext
1689 * @return result code: 0 = OK, otherwise - error code
1691 static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1692 int num_channels, AVCodecContext *avctx)
1695 int band_has_tones[16];
1697 for (ch_num = 0; ch_num < num_channels; ch_num++)
1698 memset(ctx->channels[ch_num].tones_info, 0,
1699 sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
1701 ctx->waves_info->tones_present = get_bits1(gb);
1702 if (!ctx->waves_info->tones_present)
1705 memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1707 ctx->waves_info->amplitude_mode = get_bits1(gb);
1708 if (!ctx->waves_info->amplitude_mode) {
1709 avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1710 return AVERROR_PATCHWELCOME;
1713 ctx->waves_info->num_tone_bands =
1714 get_vlc2(gb, tone_vlc_tabs[0].table,
1715 tone_vlc_tabs[0].bits, 1) + 1;
1717 if (num_channels == 2) {
1718 get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1719 get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1720 get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
1723 ctx->waves_info->tones_index = 0;
1725 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1726 for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
1727 band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
1729 decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
1730 if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones,
1734 decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
1735 decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
1736 decode_tones_phase(gb, ctx, ch_num, band_has_tones);
1739 if (num_channels == 2) {
1740 for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
1741 if (ctx->waves_info->tone_sharing[i])
1742 ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
1744 if (ctx->waves_info->tone_master[i])
1745 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1746 ctx->channels[1].tones_info[i]);
1753 int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1754 int num_channels, AVCodecContext *avctx)
1758 /* parse sound header */
1759 ctx->num_quant_units = get_bits(gb, 5) + 1;
1760 if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
1761 av_log(avctx, AV_LOG_ERROR,
1762 "Invalid number of quantization units: %d!\n",
1763 ctx->num_quant_units);
1764 return AVERROR_INVALIDDATA;
1767 ctx->mute_flag = get_bits1(gb);
1769 /* decode various sound parameters */
1770 if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0)
1773 ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
1774 ctx->num_coded_subbands = ctx->used_quant_units
1775 ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
1778 if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0)
1781 if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0)
1784 decode_spectrum(gb, ctx, num_channels, avctx);
1786 if (num_channels == 2) {
1787 get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
1788 get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
1791 decode_window_shape(gb, ctx, num_channels);
1793 if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0)
1796 if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0)
1799 /* decode global noise info */
1800 ctx->noise_present = get_bits1(gb);
1801 if (ctx->noise_present) {
1802 ctx->noise_level_index = get_bits(gb, 4);
1803 ctx->noise_table_index = get_bits(gb, 4);