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];
42 #define GET_DELTA(gb, delta_bits) \
43 ((delta_bits) ? get_bits((gb), (delta_bits)) : 0)
46 * Generate canonical VLC table from given descriptor.
48 * @param[in] cb ptr to codebook descriptor
49 * @param[in] xlat ptr to translation table or NULL
50 * @param[in,out] tab_offset starting offset to the generated vlc table
51 * @param[out] out_vlc ptr to vlc table to be generated
53 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
54 int *tab_offset, VLC *out_vlc)
61 int min_len = *cb++; // get shortest codeword length
62 int max_len = *cb++; // get longest codeword length
64 for (b = min_len; b <= max_len; b++) {
65 for (i = *cb++; i > 0; i--) {
66 av_assert0(index < 256);
68 codes[index] = code++;
74 out_vlc->table = &tables_data[*tab_offset];
75 out_vlc->table_allocated = 1 << max_len;
77 ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
78 xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
80 *tab_offset += 1 << max_len;
83 av_cold void ff_atrac3p_init_vlcs(void)
85 int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
87 static int wl_nb_bits[4] = { 2, 3, 5, 5 };
88 static int wl_nb_codes[4] = { 3, 5, 8, 8 };
89 static const uint8_t *wl_bits[4] = {
90 atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
91 atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
93 static const uint8_t *wl_codes[4] = {
94 atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
95 atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
97 static const uint8_t *wl_xlats[4] = {
98 atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
101 static int ct_nb_bits[4] = { 3, 4, 4, 4 };
102 static int ct_nb_codes[4] = { 4, 8, 8, 8 };
103 static const uint8_t *ct_bits[4] = {
104 atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
105 atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
107 static const uint8_t *ct_codes[4] = {
108 atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
109 atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
111 static const uint8_t *ct_xlats[4] = {
112 NULL, NULL, atrac3p_ct_huff_xlat1, NULL
115 static int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
116 static int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
117 static const uint8_t *sf_bits[8] = {
118 atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
119 atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
120 atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
122 static const uint16_t *sf_codes[8] = {
123 atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
124 atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
125 atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
127 static const uint8_t *sf_xlats[8] = {
128 atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
129 atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
132 static const uint8_t *gain_cbs[11] = {
133 atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
134 atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
135 atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
136 atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
137 atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
138 atrac3p_huff_gain_loc5_cb
140 static const uint8_t *gain_xlats[11] = {
141 NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
142 atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
143 atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
144 atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
145 atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
148 static const uint8_t *tone_cbs[7] = {
149 atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,
150 atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,
151 atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
154 static const uint8_t *tone_xlats[7] = {
155 NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
156 atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
157 atrac3p_huff_freq_xlat
160 for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
161 wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
162 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
163 ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
164 ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
166 ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
170 INIT_VLC_USE_NEW_STATIC);
172 ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
176 INIT_VLC_USE_NEW_STATIC);
178 wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
179 ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
182 for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
183 sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
184 sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
186 ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
190 INIT_VLC_USE_NEW_STATIC);
191 sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
196 /* build huffman tables for spectrum decoding */
197 for (i = 0; i < 112; i++) {
198 if (atrac3p_spectra_tabs[i].cb)
199 build_canonical_huff(atrac3p_spectra_tabs[i].cb,
200 atrac3p_spectra_tabs[i].xlat,
201 &tab_offset, &spec_vlc_tabs[i]);
203 spec_vlc_tabs[i].table = 0;
206 /* build huffman tables for gain data decoding */
207 for (i = 0; i < 11; i++)
208 build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
210 /* build huffman tables for tone decoding */
211 for (i = 0; i < 7; i++)
212 build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
216 * Decode number of coded quantization units.
218 * @param[in] gb the GetBit context
219 * @param[in,out] chan ptr to the channel parameters
220 * @param[in,out] ctx ptr to the channel unit context
221 * @param[in] avctx ptr to the AVCodecContext
222 * @return result code: 0 = OK, otherwise - error code
224 static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,
225 Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
227 chan->fill_mode = get_bits(gb, 2);
228 if (!chan->fill_mode) {
229 chan->num_coded_vals = ctx->num_quant_units;
231 chan->num_coded_vals = get_bits(gb, 5);
232 if (chan->num_coded_vals > ctx->num_quant_units) {
233 av_log(avctx, AV_LOG_ERROR,
234 "Invalid number of transmitted units!\n");
235 return AVERROR_INVALIDDATA;
238 if (chan->fill_mode == 3)
239 chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
246 * Add weighting coefficients to the decoded word-length information.
248 * @param[in,out] ctx ptr to the channel unit context
249 * @param[in,out] chan ptr to the channel parameters
250 * @param[in] avctx ptr to the AVCodecContext
251 * @return result code: 0 = OK, otherwise - error code
253 static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
254 Atrac3pChanParams *chan, int wtab_idx,
255 AVCodecContext *avctx)
258 const int8_t *weights_tab =
259 &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
261 for (i = 0; i < ctx->num_quant_units; i++) {
262 chan->qu_wordlen[i] += weights_tab[i];
263 if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
264 av_log(avctx, AV_LOG_ERROR,
265 "WL index out of range: pos=%d, val=%d!\n",
266 i, chan->qu_wordlen[i]);
267 return AVERROR_INVALIDDATA;
275 * Subtract weighting coefficients from decoded scalefactors.
277 * @param[in,out] ctx ptr to the channel unit context
278 * @param[in,out] chan ptr to the channel parameters
279 * @param[in] wtab_idx index of table of weights
280 * @param[in] avctx ptr to the AVCodecContext
281 * @return result code: 0 = OK, otherwise - error code
283 static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
284 Atrac3pChanParams *chan, int wtab_idx,
285 AVCodecContext *avctx)
288 const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
290 for (i = 0; i < ctx->used_quant_units; i++) {
291 chan->qu_sf_idx[i] -= weights_tab[i];
292 if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
293 av_log(avctx, AV_LOG_ERROR,
294 "SF index out of range: pos=%d, val=%d!\n",
295 i, chan->qu_sf_idx[i]);
296 return AVERROR_INVALIDDATA;
304 * Unpack vector quantization tables.
306 * @param[in] start_val start value for the unpacked table
307 * @param[in] shape_vec ptr to table to unpack
308 * @param[out] dst ptr to output array
309 * @param[in] num_values number of values to unpack
311 static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
312 int *dst, int num_values)
317 dst[0] = dst[1] = dst[2] = start_val;
318 for (i = 3; i < num_values; i++)
319 dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
323 #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \
324 start_val = get_bits((gb), 6); \
325 unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
329 * Decode word length for each quantization unit of a channel.
331 * @param[in] gb the GetBit context
332 * @param[in,out] ctx ptr to the channel unit context
333 * @param[in] ch_num channel to process
334 * @param[in] avctx ptr to the AVCodecContext
335 * @return result code: 0 = OK, otherwise - error code
337 static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
338 int ch_num, AVCodecContext *avctx)
340 int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
343 Atrac3pChanParams *chan = &ctx->channels[ch_num];
344 Atrac3pChanParams *ref_chan = &ctx->channels[0];
348 switch (get_bits(gb, 2)) { /* switch according to coding mode */
349 case 0: /* coded using constant number of bits */
350 for (i = 0; i < ctx->num_quant_units; i++)
351 chan->qu_wordlen[i] = get_bits(gb, 3);
355 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
358 if (chan->num_coded_vals) {
359 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
361 for (i = 0; i < chan->num_coded_vals; i++) {
362 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
363 chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
367 weight_idx = get_bits(gb, 2);
368 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
371 if (chan->num_coded_vals) {
372 pos = get_bits(gb, 5);
373 if (pos > chan->num_coded_vals) {
374 av_log(avctx, AV_LOG_ERROR,
375 "WL mode 1: invalid position!\n");
376 return AVERROR_INVALIDDATA;
379 delta_bits = get_bits(gb, 2);
380 min_val = get_bits(gb, 3);
382 for (i = 0; i < pos; i++)
383 chan->qu_wordlen[i] = get_bits(gb, 3);
385 for (i = pos; i < chan->num_coded_vals; i++)
386 chan->qu_wordlen[i] = (min_val + GET_DELTA(gb, delta_bits)) & 7;
391 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
394 if (ch_num && chan->num_coded_vals) {
395 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
396 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
397 chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
399 for (i = 1; i < chan->num_coded_vals; i++) {
400 diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
401 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
402 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
404 } else if (chan->num_coded_vals) {
405 flag = get_bits(gb, 1);
406 vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
408 start_val = get_bits(gb, 3);
409 unpack_vq_shape(start_val,
410 &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
411 chan->qu_wordlen, chan->num_coded_vals);
414 for (i = 0; i < chan->num_coded_vals; i++) {
415 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
416 chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
419 for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
420 if (!get_bits1(gb)) {
421 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
422 get_vlc2(gb, vlc_tab->table,
423 vlc_tab->bits, 1)) & 7;
424 chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
425 get_vlc2(gb, vlc_tab->table,
426 vlc_tab->bits, 1)) & 7;
429 if (chan->num_coded_vals & 1)
430 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
431 get_vlc2(gb, vlc_tab->table,
432 vlc_tab->bits, 1)) & 7;
437 weight_idx = get_bits(gb, 2);
438 if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
441 if (chan->num_coded_vals) {
442 vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
444 /* first coefficient is coded directly */
445 chan->qu_wordlen[0] = get_bits(gb, 3);
447 for (i = 1; i < chan->num_coded_vals; i++) {
448 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
449 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
455 if (chan->fill_mode == 2) {
456 for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
457 chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
458 } else if (chan->fill_mode == 3) {
459 pos = ch_num ? chan->num_coded_vals + chan->split_point
460 : ctx->num_quant_units - chan->split_point;
461 for (i = chan->num_coded_vals; i < pos; i++)
462 chan->qu_wordlen[i] = 1;
466 return add_wordlen_weights(ctx, chan, weight_idx, avctx);
472 * Decode scale factor indexes for each quant unit of a channel.
474 * @param[in] gb the GetBit context
475 * @param[in,out] ctx ptr to the channel unit context
476 * @param[in] ch_num channel to process
477 * @param[in] avctx ptr to the AVCodecContext
478 * @return result code: 0 = OK, otherwise - error code
480 static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
481 int ch_num, AVCodecContext *avctx)
483 int i, weight_idx = 0, delta, diff, num_long_vals,
484 delta_bits, min_val, vlc_sel, start_val;
486 Atrac3pChanParams *chan = &ctx->channels[ch_num];
487 Atrac3pChanParams *ref_chan = &ctx->channels[0];
489 switch (get_bits(gb, 2)) { /* switch according to coding mode */
490 case 0: /* coded using constant number of bits */
491 for (i = 0; i < ctx->used_quant_units; i++)
492 chan->qu_sf_idx[i] = get_bits(gb, 6);
496 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
498 for (i = 0; i < ctx->used_quant_units; i++) {
499 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
500 chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
503 weight_idx = get_bits(gb, 2);
504 if (weight_idx == 3) {
505 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
507 num_long_vals = get_bits(gb, 5);
508 delta_bits = get_bits(gb, 2);
509 min_val = get_bits(gb, 4) - 7;
511 for (i = 0; i < num_long_vals; i++)
512 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
513 get_bits(gb, 4) - 7) & 0x3F;
515 /* all others are: min_val + delta */
516 for (i = num_long_vals; i < ctx->used_quant_units; i++)
517 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
518 GET_DELTA(gb, delta_bits)) & 0x3F;
520 num_long_vals = get_bits(gb, 5);
521 delta_bits = get_bits(gb, 3);
522 min_val = get_bits(gb, 6);
523 if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
524 av_log(avctx, AV_LOG_ERROR,
525 "SF mode 1: invalid parameters!\n");
526 return AVERROR_INVALIDDATA;
529 /* read full-precision SF indexes */
530 for (i = 0; i < num_long_vals; i++)
531 chan->qu_sf_idx[i] = get_bits(gb, 6);
533 /* all others are: min_val + delta */
534 for (i = num_long_vals; i < ctx->used_quant_units; i++)
535 chan->qu_sf_idx[i] = (min_val +
536 GET_DELTA(gb, delta_bits)) & 0x3F;
542 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
544 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
545 chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
547 for (i = 1; i < ctx->used_quant_units; i++) {
548 diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
549 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
550 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
553 vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
555 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
557 for (i = 0; i < ctx->used_quant_units; i++) {
558 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
559 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
560 sign_extend(delta, 4)) & 0x3F;
566 /* copy coefficients from reference channel */
567 for (i = 0; i < ctx->used_quant_units; i++)
568 chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
570 weight_idx = get_bits(gb, 2);
571 vlc_sel = get_bits(gb, 2);
572 vlc_tab = &sf_vlc_tabs[vlc_sel];
574 if (weight_idx == 3) {
575 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
577 UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
579 diff = (get_bits(gb, 4) + 56) & 0x3F;
580 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
582 for (i = 1; i < ctx->used_quant_units; i++) {
583 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
584 diff = (diff + sign_extend(delta, 4)) & 0x3F;
585 chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
588 /* 1st coefficient is coded directly */
589 chan->qu_sf_idx[0] = get_bits(gb, 6);
591 for (i = 1; i < ctx->used_quant_units; i++) {
592 delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
593 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
600 if (weight_idx && weight_idx < 3)
601 return subtract_sf_weights(ctx, chan, weight_idx, avctx);
607 * Decode word length information for each channel.
609 * @param[in] gb the GetBit context
610 * @param[in,out] ctx ptr to the channel unit context
611 * @param[in] num_channels number of channels to process
612 * @param[in] avctx ptr to the AVCodecContext
613 * @return result code: 0 = OK, otherwise - error code
615 static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
616 int num_channels, AVCodecContext *avctx)
620 for (ch_num = 0; ch_num < num_channels; ch_num++) {
621 memset(ctx->channels[ch_num].qu_wordlen, 0,
622 sizeof(ctx->channels[ch_num].qu_wordlen));
624 if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
628 /* scan for last non-zero coeff in both channels and
629 * set number of quant units having coded spectrum */
630 for (i = ctx->num_quant_units - 1; i >= 0; i--)
631 if (ctx->channels[0].qu_wordlen[i] ||
632 (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
634 ctx->used_quant_units = i + 1;
640 * Decode scale factor indexes for each channel.
642 * @param[in] gb the GetBit context
643 * @param[in,out] ctx ptr to the channel unit context
644 * @param[in] num_channels number of channels to process
645 * @param[in] avctx ptr to the AVCodecContext
646 * @return result code: 0 = OK, otherwise - error code
648 static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
649 int num_channels, AVCodecContext *avctx)
653 if (!ctx->used_quant_units)
656 for (ch_num = 0; ch_num < num_channels; ch_num++) {
657 memset(ctx->channels[ch_num].qu_sf_idx, 0,
658 sizeof(ctx->channels[ch_num].qu_sf_idx));
660 if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
668 * Decode number of code table values.
670 * @param[in] gb the GetBit context
671 * @param[in,out] ctx ptr to the channel unit context
672 * @param[in] avctx ptr to the AVCodecContext
673 * @return result code: 0 = OK, otherwise - error code
675 static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
676 AVCodecContext *avctx)
681 num_coded_vals = get_bits(gb, 5);
682 if (num_coded_vals > ctx->used_quant_units) {
683 av_log(avctx, AV_LOG_ERROR,
684 "Invalid number of code table indexes: %d!\n", num_coded_vals);
685 return AVERROR_INVALIDDATA;
687 return num_coded_vals;
689 return ctx->used_quant_units;
692 #define DEC_CT_IDX_COMMON(OP) \
693 num_vals = get_num_ct_values(gb, ctx, avctx); \
697 for (i = 0; i < num_vals; i++) { \
698 if (chan->qu_wordlen[i]) { \
699 chan->qu_tab_idx[i] = OP; \
700 } else if (ch_num && ref_chan->qu_wordlen[i]) \
701 /* get clone master flag */ \
702 chan->qu_tab_idx[i] = get_bits1(gb); \
705 #define CODING_DIRECT get_bits(gb, num_bits)
707 #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
709 #define CODING_VLC_DELTA \
711 : (pred + get_vlc2(gb, delta_vlc->table, \
712 delta_vlc->bits, 1)) & mask; \
713 pred = chan->qu_tab_idx[i]
715 #define CODING_VLC_DIFF \
716 (ref_chan->qu_tab_idx[i] + \
717 get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
720 * Decode code table indexes for each quant unit of a channel.
722 * @param[in] gb the GetBit context
723 * @param[in,out] ctx ptr to the channel unit context
724 * @param[in] ch_num channel to process
725 * @param[in] avctx ptr to the AVCodecContext
726 * @return result code: 0 = OK, otherwise - error code
728 static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
729 int ch_num, AVCodecContext *avctx)
731 int i, num_vals, num_bits, pred;
732 int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
733 VLC *vlc_tab, *delta_vlc;
734 Atrac3pChanParams *chan = &ctx->channels[ch_num];
735 Atrac3pChanParams *ref_chan = &ctx->channels[0];
737 chan->table_type = get_bits1(gb);
739 switch (get_bits(gb, 2)) { /* switch according to coding mode */
740 case 0: /* directly coded */
741 num_bits = ctx->use_full_table + 2;
742 DEC_CT_IDX_COMMON(CODING_DIRECT);
744 case 1: /* entropy-coded */
745 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
747 DEC_CT_IDX_COMMON(CODING_VLC);
749 case 2: /* entropy-coded delta */
750 if (ctx->use_full_table) {
751 vlc_tab = &ct_vlc_tabs[1];
752 delta_vlc = &ct_vlc_tabs[2];
754 vlc_tab = ct_vlc_tabs;
755 delta_vlc = ct_vlc_tabs;
758 DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
760 case 3: /* entropy-coded difference to master */
762 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
764 DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
773 * Decode code table indexes for each channel.
775 * @param[in] gb the GetBit context
776 * @param[in,out] ctx ptr to the channel unit context
777 * @param[in] num_channels number of channels to process
778 * @param[in] avctx ptr to the AVCodecContext
779 * @return result code: 0 = OK, otherwise - error code
781 static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
782 int num_channels, AVCodecContext *avctx)
786 if (!ctx->used_quant_units)
789 ctx->use_full_table = get_bits1(gb);
791 for (ch_num = 0; ch_num < num_channels; ch_num++) {
792 memset(ctx->channels[ch_num].qu_tab_idx, 0,
793 sizeof(ctx->channels[ch_num].qu_tab_idx));
795 if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
803 * Decode huffman-coded spectral lines for a given quant unit.
805 * This is a generalized version for all known coding modes.
806 * Its speed can be improved by creating separate functions for each mode.
808 * @param[in] gb the GetBit context
809 * @param[in] tab code table telling how to decode spectral lines
810 * @param[in] vlc_tab ptr to the huffman table associated with the code table
811 * @param[out] out pointer to buffer where decoded data should be stored
812 * @param[in] num_specs number of spectral lines to decode
814 static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
815 VLC *vlc_tab, int16_t *out, const int num_specs)
818 int group_size = tab->group_size;
819 int num_coeffs = tab->num_coeffs;
820 int bits = tab->bits;
821 int is_signed = tab->is_signed;
822 unsigned val, mask = (1 << bits) - 1;
824 for (pos = 0; pos < num_specs;) {
825 if (group_size == 1 || get_bits1(gb)) {
826 for (j = 0; j < group_size; j++) {
827 val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
829 for (i = 0; i < num_coeffs; i++) {
832 cf = sign_extend(cf, bits);
833 else if (cf && get_bits1(gb))
840 } else /* group skipped */
841 pos += group_size * num_coeffs;
846 * Decode huffman-coded IMDCT spectrum for all channels.
848 * @param[in] gb the GetBit context
849 * @param[in,out] ctx ptr to the channel unit context
850 * @param[in] num_channels number of channels to process
851 * @param[in] avctx ptr to the AVCodecContext
853 static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
854 int num_channels, AVCodecContext *avctx)
856 int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
857 const Atrac3pSpecCodeTab *tab;
858 Atrac3pChanParams *chan;
860 for (ch_num = 0; ch_num < num_channels; ch_num++) {
861 chan = &ctx->channels[ch_num];
863 memset(chan->spectrum, 0, sizeof(chan->spectrum));
865 /* set power compensation level to disabled */
866 memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
868 for (qu = 0; qu < ctx->used_quant_units; qu++) {
869 num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
870 ff_atrac3p_qu_to_spec_pos[qu];
872 wordlen = chan->qu_wordlen[qu];
873 codetab = chan->qu_tab_idx[qu];
875 if (!ctx->use_full_table)
876 codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
878 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
879 tab = &atrac3p_spectra_tabs[tab_index];
881 /* this allows reusing VLC tables */
882 if (tab->redirect >= 0)
883 tab_index = tab->redirect;
885 decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
886 &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
888 } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
889 /* copy coefficients from master */
890 memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
891 &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
893 sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
894 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
898 /* Power compensation levels only present in the bitstream
899 * if there are more than 2 quant units. The lowest two units
900 * correspond to the frequencies 0...351 Hz, whose shouldn't
901 * be affected by the power compensation. */
902 if (ctx->used_quant_units > 2) {
903 num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
904 for (i = 0; i < num_specs; i++)
905 chan->power_levs[i] = get_bits(gb, 4);
911 * Retrieve specified amount of flag bits from the input bitstream.
912 * The data can be shortened in the case of the following two common conditions:
913 * if all bits are zero then only one signal bit = 0 will be stored,
914 * if all bits are ones then two signal bits = 1,0 will be stored.
915 * Otherwise, all necessary bits will be directly stored
916 * prefixed by two signal bits = 1,1.
918 * @param[in] gb ptr to the GetBitContext
919 * @param[out] out where to place decoded flags
920 * @param[in] num_flags number of flags to process
921 * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
923 static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
927 memset(out, 0, num_flags);
929 result = get_bits1(gb);
932 for (i = 0; i < num_flags; i++)
933 out[i] = get_bits1(gb);
935 memset(out, 1, num_flags);
942 * Decode mdct window shape flags for all channels.
944 * @param[in] gb the GetBit context
945 * @param[in,out] ctx ptr to the channel unit context
946 * @param[in] num_channels number of channels to process
948 static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
953 for (ch_num = 0; ch_num < num_channels; ch_num++)
954 get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
959 * Decode number of gain control points.
961 * @param[in] gb the GetBit context
962 * @param[in,out] ctx ptr to the channel unit context
963 * @param[in] ch_num channel to process
964 * @param[in] coded_subbands number of subbands to process
965 * @return result code: 0 = OK, otherwise - error code
967 static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
968 int ch_num, int coded_subbands)
970 int i, delta, delta_bits, min_val;
971 Atrac3pChanParams *chan = &ctx->channels[ch_num];
972 Atrac3pChanParams *ref_chan = &ctx->channels[0];
974 switch (get_bits(gb, 2)) { /* switch according to coding mode */
975 case 0: /* fixed-length coding */
976 for (i = 0; i < coded_subbands; i++)
977 chan->gain_data[i].num_points = get_bits(gb, 3);
979 case 1: /* variable-length coding */
980 for (i = 0; i < coded_subbands; i++)
981 chan->gain_data[i].num_points =
982 get_vlc2(gb, gain_vlc_tabs[0].table,
983 gain_vlc_tabs[0].bits, 1);
986 if (ch_num) { /* VLC modulo delta to master channel */
987 for (i = 0; i < coded_subbands; i++) {
988 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
989 gain_vlc_tabs[1].bits, 1);
990 chan->gain_data[i].num_points =
991 (ref_chan->gain_data[i].num_points + delta) & 7;
993 } else { /* VLC modulo delta to previous */
994 chan->gain_data[0].num_points =
995 get_vlc2(gb, gain_vlc_tabs[0].table,
996 gain_vlc_tabs[0].bits, 1);
998 for (i = 1; i < coded_subbands; i++) {
999 delta = get_vlc2(gb, gain_vlc_tabs[1].table,
1000 gain_vlc_tabs[1].bits, 1);
1001 chan->gain_data[i].num_points =
1002 (chan->gain_data[i - 1].num_points + delta) & 7;
1007 if (ch_num) { /* copy data from master channel */
1008 for (i = 0; i < coded_subbands; i++)
1009 chan->gain_data[i].num_points =
1010 ref_chan->gain_data[i].num_points;
1011 } else { /* shorter delta to min */
1012 delta_bits = get_bits(gb, 2);
1013 min_val = get_bits(gb, 3);
1015 for (i = 0; i < coded_subbands; i++) {
1016 chan->gain_data[i].num_points = min_val + GET_DELTA(gb, delta_bits);
1017 if (chan->gain_data[i].num_points > 7)
1018 return AVERROR_INVALIDDATA;
1027 * Implements coding mode 3 (slave) for gain compensation levels.
1029 * @param[out] dst ptr to the output array
1030 * @param[in] ref ptr to the reference channel
1032 static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1036 for (i = 0; i < dst->num_points; i++)
1037 dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1041 * Implements coding mode 1 (master) for gain compensation levels.
1043 * @param[in] gb the GetBit context
1044 * @param[in] ctx ptr to the channel unit context
1045 * @param[out] dst ptr to the output array
1047 static inline void gainc_level_mode1m(GetBitContext *gb,
1048 Atrac3pChanUnitCtx *ctx,
1053 if (dst->num_points > 0)
1054 dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
1055 gain_vlc_tabs[2].bits, 1);
1057 for (i = 1; i < dst->num_points; i++) {
1058 delta = get_vlc2(gb, gain_vlc_tabs[3].table,
1059 gain_vlc_tabs[3].bits, 1);
1060 dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
1065 * Decode level code for each gain control point.
1067 * @param[in] gb the GetBit context
1068 * @param[in,out] ctx ptr to the channel unit context
1069 * @param[in] ch_num channel to process
1070 * @param[in] coded_subbands number of subbands to process
1071 * @return result code: 0 = OK, otherwise - error code
1073 static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1074 int ch_num, int coded_subbands)
1076 int sb, i, delta, delta_bits, min_val, pred;
1077 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1078 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1080 switch (get_bits(gb, 2)) { /* switch according to coding mode */
1081 case 0: /* fixed-length coding */
1082 for (sb = 0; sb < coded_subbands; sb++)
1083 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1084 chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
1087 if (ch_num) { /* VLC modulo delta to master channel */
1088 for (sb = 0; sb < coded_subbands; sb++)
1089 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1090 delta = get_vlc2(gb, gain_vlc_tabs[5].table,
1091 gain_vlc_tabs[5].bits, 1);
1092 pred = (i >= ref_chan->gain_data[sb].num_points)
1093 ? 7 : ref_chan->gain_data[sb].lev_code[i];
1094 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1096 } else { /* VLC modulo delta to previous */
1097 for (sb = 0; sb < coded_subbands; sb++)
1098 gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1102 if (ch_num) { /* VLC modulo delta to previous or clone master */
1103 for (sb = 0; sb < coded_subbands; sb++)
1104 if (chan->gain_data[sb].num_points > 0) {
1106 gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1108 gainc_level_mode3s(&chan->gain_data[sb],
1109 &ref_chan->gain_data[sb]);
1111 } else { /* VLC modulo delta to lev_codes of previous subband */
1112 if (chan->gain_data[0].num_points > 0)
1113 gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
1115 for (sb = 1; sb < coded_subbands; sb++)
1116 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1117 delta = get_vlc2(gb, gain_vlc_tabs[4].table,
1118 gain_vlc_tabs[4].bits, 1);
1119 pred = (i >= chan->gain_data[sb - 1].num_points)
1120 ? 7 : chan->gain_data[sb - 1].lev_code[i];
1121 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1126 if (ch_num) { /* clone master */
1127 for (sb = 0; sb < coded_subbands; sb++)
1128 gainc_level_mode3s(&chan->gain_data[sb],
1129 &ref_chan->gain_data[sb]);
1130 } else { /* shorter delta to min */
1131 delta_bits = get_bits(gb, 2);
1132 min_val = get_bits(gb, 4);
1134 for (sb = 0; sb < coded_subbands; sb++)
1135 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1136 chan->gain_data[sb].lev_code[i] = min_val + GET_DELTA(gb, delta_bits);
1137 if (chan->gain_data[sb].lev_code[i] > 15)
1138 return AVERROR_INVALIDDATA;
1148 * Implements coding mode 0 for gain compensation locations.
1150 * @param[in] gb the GetBit context
1151 * @param[in] ctx ptr to the channel unit context
1152 * @param[out] dst ptr to the output array
1153 * @param[in] pos position of the value to be processed
1155 static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1156 AtracGainInfo *dst, int pos)
1160 if (!pos || dst->loc_code[pos - 1] < 15)
1161 dst->loc_code[pos] = get_bits(gb, 5);
1162 else if (dst->loc_code[pos - 1] >= 30)
1163 dst->loc_code[pos] = 31;
1165 delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1166 dst->loc_code[pos] = dst->loc_code[pos - 1] +
1167 get_bits(gb, delta_bits) + 1;
1172 * Implements coding mode 1 for gain compensation locations.
1174 * @param[in] gb the GetBit context
1175 * @param[in] ctx ptr to the channel unit context
1176 * @param[out] dst ptr to the output array
1178 static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1184 if (dst->num_points > 0) {
1185 /* 1st coefficient is stored directly */
1186 dst->loc_code[0] = get_bits(gb, 5);
1188 for (i = 1; i < dst->num_points; i++) {
1189 /* switch VLC according to the curve direction
1190 * (ascending/descending) */
1191 tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
1193 : &gain_vlc_tabs[9];
1194 dst->loc_code[i] = dst->loc_code[i - 1] +
1195 get_vlc2(gb, tab->table, tab->bits, 1);
1201 * Decode location code for each gain control point.
1203 * @param[in] gb the GetBit context
1204 * @param[in,out] ctx ptr to the channel unit context
1205 * @param[in] ch_num channel to process
1206 * @param[in] coded_subbands number of subbands to process
1207 * @param[in] avctx ptr to the AVCodecContext
1208 * @return result code: 0 = OK, otherwise - error code
1210 static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1211 int ch_num, int coded_subbands,
1212 AVCodecContext *avctx)
1214 int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1215 AtracGainInfo *dst, *ref;
1217 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1218 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1220 switch (get_bits(gb, 2)) { /* switch according to coding mode */
1221 case 0: /* sequence of numbers in ascending order */
1222 for (sb = 0; sb < coded_subbands; sb++)
1223 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1224 gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1228 for (sb = 0; sb < coded_subbands; sb++) {
1229 if (chan->gain_data[sb].num_points <= 0)
1231 dst = &chan->gain_data[sb];
1232 ref = &ref_chan->gain_data[sb];
1234 /* 1st value is vlc-coded modulo delta to master */
1235 delta = get_vlc2(gb, gain_vlc_tabs[10].table,
1236 gain_vlc_tabs[10].bits, 1);
1237 pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1238 dst->loc_code[0] = (pred + delta) & 0x1F;
1240 for (i = 1; i < dst->num_points; i++) {
1241 more_than_ref = i >= ref->num_points;
1242 if (dst->lev_code[i] > dst->lev_code[i - 1]) {
1243 /* ascending curve */
1244 if (more_than_ref) {
1246 get_vlc2(gb, gain_vlc_tabs[9].table,
1247 gain_vlc_tabs[9].bits, 1);
1248 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1251 gainc_loc_mode0(gb, ctx, dst, i); // direct coding
1253 dst->loc_code[i] = ref->loc_code[i]; // clone master
1255 } else { /* descending curve */
1256 tab = more_than_ref ? &gain_vlc_tabs[7]
1257 : &gain_vlc_tabs[10];
1258 delta = get_vlc2(gb, tab->table, tab->bits, 1);
1260 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1262 dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1266 } else /* VLC delta to previous */
1267 for (sb = 0; sb < coded_subbands; sb++)
1268 gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
1272 for (sb = 0; sb < coded_subbands; sb++) {
1273 if (chan->gain_data[sb].num_points <= 0)
1275 dst = &chan->gain_data[sb];
1276 ref = &ref_chan->gain_data[sb];
1277 if (dst->num_points > ref->num_points || get_bits1(gb))
1278 gainc_loc_mode1(gb, ctx, dst);
1279 else /* clone master for the whole subband */
1280 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1281 dst->loc_code[i] = ref->loc_code[i];
1284 /* data for the first subband is coded directly */
1285 for (i = 0; i < chan->gain_data[0].num_points; i++)
1286 gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
1288 for (sb = 1; sb < coded_subbands; sb++) {
1289 if (chan->gain_data[sb].num_points <= 0)
1291 dst = &chan->gain_data[sb];
1293 /* 1st value is vlc-coded modulo delta to the corresponding
1294 * value of the previous subband if any or zero */
1295 delta = get_vlc2(gb, gain_vlc_tabs[6].table,
1296 gain_vlc_tabs[6].bits, 1);
1297 pred = dst[-1].num_points > 0
1298 ? dst[-1].loc_code[0] : 0;
1299 dst->loc_code[0] = (pred + delta) & 0x1F;
1301 for (i = 1; i < dst->num_points; i++) {
1302 more_than_ref = i >= dst[-1].num_points;
1303 /* Select VLC table according to curve direction and
1304 * presence of prediction. */
1305 tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
1306 2 + more_than_ref + 6];
1307 delta = get_vlc2(gb, tab->table, tab->bits, 1);
1309 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1311 dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1317 if (ch_num) { /* clone master or direct or direct coding */
1318 for (sb = 0; sb < coded_subbands; sb++)
1319 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1320 if (i >= ref_chan->gain_data[sb].num_points)
1321 gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1323 chan->gain_data[sb].loc_code[i] =
1324 ref_chan->gain_data[sb].loc_code[i];
1326 } else { /* shorter delta to min */
1327 delta_bits = get_bits(gb, 2) + 1;
1328 min_val = get_bits(gb, 5);
1330 for (sb = 0; sb < coded_subbands; sb++)
1331 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1332 chan->gain_data[sb].loc_code[i] = min_val + i +
1333 get_bits(gb, delta_bits);
1338 /* Validate decoded information */
1339 for (sb = 0; sb < coded_subbands; sb++) {
1340 dst = &chan->gain_data[sb];
1341 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1342 if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
1343 (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
1344 av_log(avctx, AV_LOG_ERROR,
1345 "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
1346 ch_num, sb, i, dst->loc_code[i]);
1347 return AVERROR_INVALIDDATA;
1356 * Decode gain control data for all channels.
1358 * @param[in] gb the GetBit context
1359 * @param[in,out] ctx ptr to the channel unit context
1360 * @param[in] num_channels number of channels to process
1361 * @param[in] avctx ptr to the AVCodecContext
1362 * @return result code: 0 = OK, otherwise - error code
1364 static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1365 int num_channels, AVCodecContext *avctx)
1367 int ch_num, coded_subbands, sb, ret;
1369 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1370 memset(ctx->channels[ch_num].gain_data, 0,
1371 sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
1373 if (get_bits1(gb)) { /* gain control data present? */
1374 coded_subbands = get_bits(gb, 4) + 1;
1375 if (get_bits1(gb)) /* is high band gain data replication on? */
1376 ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
1378 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1380 if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
1381 (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
1382 (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
1385 if (coded_subbands > 0) { /* propagate gain data if requested */
1386 for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
1387 ctx->channels[ch_num].gain_data[sb] =
1388 ctx->channels[ch_num].gain_data[sb - 1];
1391 ctx->channels[ch_num].num_gain_subbands = 0;
1399 * Decode envelope for all tones of a channel.
1401 * @param[in] gb the GetBit context
1402 * @param[in,out] ctx ptr to the channel unit context
1403 * @param[in] ch_num channel to process
1404 * @param[in] band_has_tones ptr to an array of per-band-flags:
1405 * 1 - tone data present
1407 static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1408 int ch_num, int band_has_tones[])
1411 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1412 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1414 if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1415 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1416 if (!band_has_tones[sb])
1418 dst[sb].pend_env.has_start_point = get_bits1(gb);
1419 dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
1420 ? get_bits(gb, 5) : -1;
1421 dst[sb].pend_env.has_stop_point = get_bits1(gb);
1422 dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
1423 ? get_bits(gb, 5) : 32;
1425 } else { /* mode 1(slave only): copy master */
1426 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1427 if (!band_has_tones[sb])
1429 dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
1430 dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
1431 dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
1432 dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
1438 * Decode number of tones for each subband of a channel.
1440 * @param[in] gb the GetBit context
1441 * @param[in,out] ctx ptr to the channel unit context
1442 * @param[in] ch_num channel to process
1443 * @param[in] band_has_tones ptr to an array of per-band-flags:
1444 * 1 - tone data present
1445 * @param[in] avctx ptr to the AVCodecContext
1446 * @return result code: 0 = OK, otherwise - error code
1448 static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1449 int ch_num, int band_has_tones[],
1450 AVCodecContext *avctx)
1452 int mode, sb, delta;
1453 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1454 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1456 mode = get_bits(gb, ch_num + 1);
1458 case 0: /** fixed-length coding */
1459 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1460 if (band_has_tones[sb])
1461 dst[sb].num_wavs = get_bits(gb, 4);
1463 case 1: /** variable-length coding */
1464 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1465 if (band_has_tones[sb])
1467 get_vlc2(gb, tone_vlc_tabs[1].table,
1468 tone_vlc_tabs[1].bits, 1);
1470 case 2: /** VLC modulo delta to master (slave only) */
1471 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1472 if (band_has_tones[sb]) {
1473 delta = get_vlc2(gb, tone_vlc_tabs[2].table,
1474 tone_vlc_tabs[2].bits, 1);
1475 delta = sign_extend(delta, 3);
1476 dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1479 case 3: /** copy master (slave only) */
1480 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1481 if (band_has_tones[sb])
1482 dst[sb].num_wavs = ref[sb].num_wavs;
1486 /** initialize start tone index for each subband */
1487 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1488 if (band_has_tones[sb]) {
1489 if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
1490 av_log(avctx, AV_LOG_ERROR,
1491 "Too many tones: %d (max. 48), frame: %d!\n",
1492 ctx->waves_info->tones_index + dst[sb].num_wavs,
1493 avctx->frame_number);
1494 return AVERROR_INVALIDDATA;
1496 dst[sb].start_index = ctx->waves_info->tones_index;
1497 ctx->waves_info->tones_index += dst[sb].num_wavs;
1504 * Decode frequency information for each subband of a channel.
1506 * @param[in] gb the GetBit context
1507 * @param[in,out] ctx ptr to the channel unit context
1508 * @param[in] ch_num channel to process
1509 * @param[in] band_has_tones ptr to an array of per-band-flags:
1510 * 1 - tone data present
1512 static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1513 int ch_num, int band_has_tones[])
1515 int sb, i, direction, nbits, pred, delta;
1516 Atrac3pWaveParam *iwav, *owav;
1517 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1518 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1520 if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1521 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1522 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1524 iwav = &ctx->waves_info->waves[dst[sb].start_index];
1525 direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
1526 if (direction) { /** packed numbers in descending order */
1527 if (dst[sb].num_wavs)
1528 iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
1529 for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1530 nbits = av_log2(iwav[i+1].freq_index) + 1;
1531 iwav[i].freq_index = get_bits(gb, nbits);
1533 } else { /** packed numbers in ascending order */
1534 for (i = 0; i < dst[sb].num_wavs; i++) {
1535 if (!i || iwav[i - 1].freq_index < 512)
1536 iwav[i].freq_index = get_bits(gb, 10);
1538 nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1539 iwav[i].freq_index = get_bits(gb, nbits) +
1540 1024 - (1 << nbits);
1545 } else { /* mode 1: VLC modulo delta to master (slave only) */
1546 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1547 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1549 iwav = &ctx->waves_info->waves[ref[sb].start_index];
1550 owav = &ctx->waves_info->waves[dst[sb].start_index];
1551 for (i = 0; i < dst[sb].num_wavs; i++) {
1552 delta = get_vlc2(gb, tone_vlc_tabs[6].table,
1553 tone_vlc_tabs[6].bits, 1);
1554 delta = sign_extend(delta, 8);
1555 pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
1556 (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
1557 owav[i].freq_index = (pred + delta) & 0x3FF;
1564 * Decode amplitude information for each subband of a channel.
1566 * @param[in] gb the GetBit context
1567 * @param[in,out] ctx ptr to the channel unit context
1568 * @param[in] ch_num channel to process
1569 * @param[in] band_has_tones ptr to an array of per-band-flags:
1570 * 1 - tone data present
1572 static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1573 int ch_num, int band_has_tones[])
1575 int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1576 Atrac3pWaveParam *wsrc, *wref;
1578 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1579 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1582 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1583 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1585 wsrc = &ctx->waves_info->waves[dst[sb].start_index];
1586 wref = &ctx->waves_info->waves[ref[sb].start_index];
1587 for (j = 0; j < dst[sb].num_wavs; j++) {
1588 for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
1589 diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
1590 if (diff < maxdiff) {
1597 refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
1598 else if (j < ref[sb].num_wavs)
1599 refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
1601 refwaves[dst[sb].start_index + j] = -1;
1606 mode = get_bits(gb, ch_num + 1);
1609 case 0: /** fixed-length coding */
1610 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1611 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1613 if (ctx->waves_info->amplitude_mode)
1614 for (i = 0; i < dst[sb].num_wavs; i++)
1615 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
1617 ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
1620 case 1: /** min + VLC delta */
1621 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1622 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1624 if (ctx->waves_info->amplitude_mode)
1625 for (i = 0; i < dst[sb].num_wavs; i++)
1626 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1627 get_vlc2(gb, tone_vlc_tabs[3].table,
1628 tone_vlc_tabs[3].bits, 1) + 20;
1630 ctx->waves_info->waves[dst[sb].start_index].amp_sf =
1631 get_vlc2(gb, tone_vlc_tabs[4].table,
1632 tone_vlc_tabs[4].bits, 1) + 24;
1635 case 2: /** VLC modulo delta to master (slave only) */
1636 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1637 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1639 for (i = 0; i < dst[sb].num_wavs; i++) {
1640 delta = get_vlc2(gb, tone_vlc_tabs[5].table,
1641 tone_vlc_tabs[5].bits, 1);
1642 delta = sign_extend(delta, 5);
1643 pred = refwaves[dst[sb].start_index + i] >= 0 ?
1644 ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
1645 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
1649 case 3: /** clone master (slave only) */
1650 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1651 if (!band_has_tones[sb])
1653 for (i = 0; i < dst[sb].num_wavs; i++)
1654 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1655 refwaves[dst[sb].start_index + i] >= 0
1656 ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
1664 * Decode phase information for each subband of a channel.
1666 * @param[in] gb the GetBit context
1667 * @param[in,out] ctx ptr to the channel unit context
1668 * @param[in] ch_num channel to process
1669 * @param[in] band_has_tones ptr to an array of per-band-flags:
1670 * 1 - tone data present
1672 static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1673 int ch_num, int band_has_tones[])
1676 Atrac3pWaveParam *wparam;
1677 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1679 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1680 if (!band_has_tones[sb])
1682 wparam = &ctx->waves_info->waves[dst[sb].start_index];
1683 for (i = 0; i < dst[sb].num_wavs; i++)
1684 wparam[i].phase_index = get_bits(gb, 5);
1689 * Decode tones info for all channels.
1691 * @param[in] gb the GetBit context
1692 * @param[in,out] ctx ptr to the channel unit context
1693 * @param[in] num_channels number of channels to process
1694 * @param[in] avctx ptr to the AVCodecContext
1695 * @return result code: 0 = OK, otherwise - error code
1697 static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1698 int num_channels, AVCodecContext *avctx)
1701 int band_has_tones[16];
1703 for (ch_num = 0; ch_num < num_channels; ch_num++)
1704 memset(ctx->channels[ch_num].tones_info, 0,
1705 sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
1707 ctx->waves_info->tones_present = get_bits1(gb);
1708 if (!ctx->waves_info->tones_present)
1711 memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1713 ctx->waves_info->amplitude_mode = get_bits1(gb);
1714 if (!ctx->waves_info->amplitude_mode) {
1715 avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1716 return AVERROR_PATCHWELCOME;
1719 ctx->waves_info->num_tone_bands =
1720 get_vlc2(gb, tone_vlc_tabs[0].table,
1721 tone_vlc_tabs[0].bits, 1) + 1;
1723 if (num_channels == 2) {
1724 get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1725 get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1726 if (get_subband_flags(gb, ctx->waves_info->phase_shift,
1727 ctx->waves_info->num_tone_bands)) {
1728 avpriv_report_missing_feature(avctx, "GHA Phase shifting");
1729 return AVERROR_PATCHWELCOME;
1733 ctx->waves_info->tones_index = 0;
1735 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1736 for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
1737 band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
1739 decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
1740 if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones,
1744 decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
1745 decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
1746 decode_tones_phase(gb, ctx, ch_num, band_has_tones);
1749 if (num_channels == 2) {
1750 for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
1751 if (ctx->waves_info->tone_sharing[i])
1752 ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
1754 if (ctx->waves_info->tone_master[i])
1755 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1756 ctx->channels[1].tones_info[i]);
1763 int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1764 int num_channels, AVCodecContext *avctx)
1768 /* parse sound header */
1769 ctx->num_quant_units = get_bits(gb, 5) + 1;
1770 if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
1771 av_log(avctx, AV_LOG_ERROR,
1772 "Invalid number of quantization units: %d!\n",
1773 ctx->num_quant_units);
1774 return AVERROR_INVALIDDATA;
1777 ctx->mute_flag = get_bits1(gb);
1779 /* decode various sound parameters */
1780 if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0)
1783 ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
1784 ctx->num_coded_subbands = ctx->used_quant_units
1785 ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
1788 if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0)
1791 if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0)
1794 decode_spectrum(gb, ctx, num_channels, avctx);
1796 if (num_channels == 2) {
1797 get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
1798 get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
1801 decode_window_shape(gb, ctx, num_channels);
1803 if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0)
1806 if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0)
1809 /* decode global noise info */
1810 ctx->noise_present = get_bits1(gb);
1811 if (ctx->noise_present) {
1812 ctx->noise_level_index = get_bits(gb, 4);
1813 ctx->noise_table_index = get_bits(gb, 4);