2 * ATRAC3+ compatible decoder
4 * Copyright (c) 2010-2013 Maxim Poliakovski
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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 "bitstream.h"
32 #include "atrac3plus.h"
33 #include "atrac3plus_data.h"
35 static VLC_TYPE tables_data[154276][2];
36 static VLC wl_vlc_tabs[4];
37 static VLC sf_vlc_tabs[8];
38 static VLC ct_vlc_tabs[4];
39 static VLC spec_vlc_tabs[112];
40 static VLC gain_vlc_tabs[11];
41 static VLC tone_vlc_tabs[7];
44 * Generate canonical VLC table from given descriptor.
46 * @param[in] cb ptr to codebook descriptor
47 * @param[in] xlat ptr to translation table or NULL
48 * @param[in,out] tab_offset starting offset to the generated vlc table
49 * @param[out] out_vlc ptr to vlc table to be generated
51 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
52 int *tab_offset, VLC *out_vlc)
59 int min_len = *cb++; // get shortest codeword length
60 int max_len = *cb++; // get longest codeword length
62 for (b = min_len; b <= max_len; b++) {
63 for (i = *cb++; i > 0; i--) {
64 av_assert0(index < 256);
66 codes[index] = code++;
72 out_vlc->table = &tables_data[*tab_offset];
73 out_vlc->table_allocated = 1 << max_len;
75 ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
76 xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
78 *tab_offset += 1 << max_len;
81 av_cold void ff_atrac3p_init_vlcs(AVCodec *codec)
83 int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
85 static int wl_nb_bits[4] = { 2, 3, 5, 5 };
86 static int wl_nb_codes[4] = { 3, 5, 8, 8 };
87 static const uint8_t *wl_bits[4] = {
88 atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
89 atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
91 static const uint8_t *wl_codes[4] = {
92 atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
93 atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
95 static const uint8_t *wl_xlats[4] = {
96 atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
99 static int ct_nb_bits[4] = { 3, 4, 4, 4 };
100 static int ct_nb_codes[4] = { 4, 8, 8, 8 };
101 static const uint8_t *ct_bits[4] = {
102 atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
103 atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
105 static const uint8_t *ct_codes[4] = {
106 atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
107 atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
109 static const uint8_t *ct_xlats[4] = {
110 NULL, NULL, atrac3p_ct_huff_xlat1, NULL
113 static int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
114 static int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
115 static const uint8_t *sf_bits[8] = {
116 atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
117 atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
118 atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
120 static const uint16_t *sf_codes[8] = {
121 atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
122 atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
123 atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
125 static const uint8_t *sf_xlats[8] = {
126 atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
127 atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
130 static const uint8_t *gain_cbs[11] = {
131 atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
132 atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
133 atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
134 atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
135 atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
136 atrac3p_huff_gain_loc5_cb
138 static const uint8_t *gain_xlats[11] = {
139 NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
140 atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
141 atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
142 atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
143 atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
146 static const uint8_t *tone_cbs[7] = {
147 atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,
148 atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,
149 atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
152 static const uint8_t *tone_xlats[7] = {
153 NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
154 atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
155 atrac3p_huff_freq_xlat
158 for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
159 wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
160 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
161 ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
162 ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
164 ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
168 INIT_VLC_USE_NEW_STATIC);
170 ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
174 INIT_VLC_USE_NEW_STATIC);
176 wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
177 ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
180 for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
181 sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
182 sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
184 ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
188 INIT_VLC_USE_NEW_STATIC);
189 sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
194 /* build huffman tables for spectrum decoding */
195 for (i = 0; i < 112; i++) {
196 if (atrac3p_spectra_tabs[i].cb)
197 build_canonical_huff(atrac3p_spectra_tabs[i].cb,
198 atrac3p_spectra_tabs[i].xlat,
199 &tab_offset, &spec_vlc_tabs[i]);
201 spec_vlc_tabs[i].table = 0;
204 /* build huffman tables for gain data decoding */
205 for (i = 0; i < 11; i++)
206 build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
208 /* build huffman tables for tone decoding */
209 for (i = 0; i < 7; i++)
210 build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
214 * Decode number of coded quantization units.
216 * @param[in] bc the Bitstream context
217 * @param[in,out] chan ptr to the channel parameters
218 * @param[in,out] ctx ptr to the channel unit context
219 * @param[in] avctx ptr to the AVCodecContext
220 * @return result code: 0 = OK, otherwise - error code
222 static int num_coded_units(BitstreamContext *bc, Atrac3pChanParams *chan,
223 Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
225 chan->fill_mode = bitstream_read(bc, 2);
226 if (!chan->fill_mode) {
227 chan->num_coded_vals = ctx->num_quant_units;
229 chan->num_coded_vals = bitstream_read(bc, 5);
230 if (chan->num_coded_vals > ctx->num_quant_units) {
231 av_log(avctx, AV_LOG_ERROR,
232 "Invalid number of transmitted units!\n");
233 return AVERROR_INVALIDDATA;
236 if (chan->fill_mode == 3)
237 chan->split_point = bitstream_read(bc, 2) + (chan->ch_num << 1) + 1;
244 * Add weighting coefficients to the decoded word-length information.
246 * @param[in,out] ctx ptr to the channel unit context
247 * @param[in,out] chan ptr to the channel parameters
248 * @param[in] wtab_idx index of the table of weights
249 * @param[in] avctx ptr to the AVCodecContext
250 * @return result code: 0 = OK, otherwise - error code
252 static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
253 Atrac3pChanParams *chan, int wtab_idx,
254 AVCodecContext *avctx)
257 const int8_t *weights_tab =
258 &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
260 for (i = 0; i < ctx->num_quant_units; i++) {
261 chan->qu_wordlen[i] += weights_tab[i];
262 if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
263 av_log(avctx, AV_LOG_ERROR,
264 "WL index out of range: pos=%d, val=%d!\n",
265 i, chan->qu_wordlen[i]);
266 return AVERROR_INVALIDDATA;
274 * Subtract weighting coefficients from decoded scalefactors.
276 * @param[in,out] ctx ptr to the channel unit context
277 * @param[in,out] chan ptr to the channel parameters
278 * @param[in] wtab_idx index of table of weights
279 * @param[in] avctx ptr to the AVCodecContext
280 * @return result code: 0 = OK, otherwise - error code
282 static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
283 Atrac3pChanParams *chan, int wtab_idx,
284 AVCodecContext *avctx)
287 const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
289 for (i = 0; i < ctx->used_quant_units; i++) {
290 chan->qu_sf_idx[i] -= weights_tab[i];
291 if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
292 av_log(avctx, AV_LOG_ERROR,
293 "SF index out of range: pos=%d, val=%d!\n",
294 i, chan->qu_sf_idx[i]);
295 return AVERROR_INVALIDDATA;
303 * Unpack vector quantization tables.
305 * @param[in] start_val start value for the unpacked table
306 * @param[in] shape_vec ptr to table to unpack
307 * @param[out] dst ptr to output array
308 * @param[in] num_values number of values to unpack
310 static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
311 int *dst, int num_values)
316 dst[0] = dst[1] = dst[2] = start_val;
317 for (i = 3; i < num_values; i++)
318 dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
322 #define UNPACK_SF_VQ_SHAPE(bc, dst, num_vals) \
323 start_val = bitstream_read((bc), 6); \
324 unpack_vq_shape(start_val, &atrac3p_sf_shapes[bitstream_read((bc), 6)][0], \
328 * Decode word length for each quantization unit of a channel.
330 * @param[in] bc the Bitstream context
331 * @param[in,out] ctx ptr to the channel unit context
332 * @param[in] ch_num channel to process
333 * @param[in] avctx ptr to the AVCodecContext
334 * @return result code: 0 = OK, otherwise - error code
336 static int decode_channel_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
337 int ch_num, AVCodecContext *avctx)
339 int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
342 Atrac3pChanParams *chan = &ctx->channels[ch_num];
343 Atrac3pChanParams *ref_chan = &ctx->channels[0];
347 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
348 case 0: /* coded using constant number of bits */
349 for (i = 0; i < ctx->num_quant_units; i++)
350 chan->qu_wordlen[i] = bitstream_read(bc, 3);
354 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
357 if (chan->num_coded_vals) {
358 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
360 for (i = 0; i < chan->num_coded_vals; i++) {
361 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
362 chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
366 weight_idx = bitstream_read(bc, 2);
367 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
370 if (chan->num_coded_vals) {
371 pos = bitstream_read(bc, 5);
372 if (pos > chan->num_coded_vals) {
373 av_log(avctx, AV_LOG_ERROR,
374 "WL mode 1: invalid position!\n");
375 return AVERROR_INVALIDDATA;
378 delta_bits = bitstream_read(bc, 2);
379 min_val = bitstream_read(bc, 3);
381 for (i = 0; i < pos; i++)
382 chan->qu_wordlen[i] = bitstream_read(bc, 3);
384 for (i = pos; i < chan->num_coded_vals; i++)
385 chan->qu_wordlen[i] = (min_val + bitstream_read(bc, delta_bits)) & 7;
390 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
393 if (ch_num && chan->num_coded_vals) {
394 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
395 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
396 chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
398 for (i = 1; i < chan->num_coded_vals; i++) {
399 diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
400 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
401 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
403 } else if (chan->num_coded_vals) {
404 flag = bitstream_read(bc, 1);
405 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 1)];
407 start_val = bitstream_read(bc, 3);
408 unpack_vq_shape(start_val,
409 &atrac3p_wl_shapes[start_val][bitstream_read(bc, 4)][0],
410 chan->qu_wordlen, chan->num_coded_vals);
413 for (i = 0; i < chan->num_coded_vals; i++) {
414 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
415 chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
418 for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
419 if (!bitstream_read_bit(bc)) {
420 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
421 bitstream_read_vlc(bc, vlc_tab->table,
422 vlc_tab->bits, 1)) & 7;
423 chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
424 bitstream_read_vlc(bc, vlc_tab->table,
425 vlc_tab->bits, 1)) & 7;
428 if (chan->num_coded_vals & 1)
429 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
430 bitstream_read_vlc(bc, vlc_tab->table,
431 vlc_tab->bits, 1)) & 7;
436 weight_idx = bitstream_read(bc, 2);
437 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
440 if (chan->num_coded_vals) {
441 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
443 /* first coefficient is coded directly */
444 chan->qu_wordlen[0] = bitstream_read(bc, 3);
446 for (i = 1; i < chan->num_coded_vals; i++) {
447 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
448 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
454 if (chan->fill_mode == 2) {
455 for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
456 chan->qu_wordlen[i] = ch_num ? bitstream_read_bit(bc) : 1;
457 } else if (chan->fill_mode == 3) {
458 pos = ch_num ? chan->num_coded_vals + chan->split_point
459 : ctx->num_quant_units - chan->split_point;
460 for (i = chan->num_coded_vals; i < pos; i++)
461 chan->qu_wordlen[i] = 1;
465 return add_wordlen_weights(ctx, chan, weight_idx, avctx);
471 * Decode scale factor indexes for each quant unit of a channel.
473 * @param[in] bc the Bitstream context
474 * @param[in,out] ctx ptr to the channel unit context
475 * @param[in] ch_num channel to process
476 * @param[in] avctx ptr to the AVCodecContext
477 * @return result code: 0 = OK, otherwise - error code
479 static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
480 int ch_num, AVCodecContext *avctx)
482 int i, weight_idx = 0, delta, diff, num_long_vals,
483 delta_bits, min_val, vlc_sel, start_val;
485 Atrac3pChanParams *chan = &ctx->channels[ch_num];
486 Atrac3pChanParams *ref_chan = &ctx->channels[0];
488 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
489 case 0: /* coded using constant number of bits */
490 for (i = 0; i < ctx->used_quant_units; i++)
491 chan->qu_sf_idx[i] = bitstream_read(bc, 6);
495 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
497 for (i = 0; i < ctx->used_quant_units; i++) {
498 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
499 chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
502 weight_idx = bitstream_read(bc, 2);
503 if (weight_idx == 3) {
504 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
506 num_long_vals = bitstream_read(bc, 5);
507 delta_bits = bitstream_read(bc, 2);
508 min_val = bitstream_read(bc, 4) - 7;
510 for (i = 0; i < num_long_vals; i++)
511 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
512 bitstream_read(bc, 4) - 7) & 0x3F;
514 /* all others are: min_val + delta */
515 for (i = num_long_vals; i < ctx->used_quant_units; i++)
516 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
517 bitstream_read(bc, delta_bits)) & 0x3F;
519 num_long_vals = bitstream_read(bc, 5);
520 delta_bits = bitstream_read(bc, 3);
521 min_val = bitstream_read(bc, 6);
522 if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
523 av_log(avctx, AV_LOG_ERROR,
524 "SF mode 1: invalid parameters!\n");
525 return AVERROR_INVALIDDATA;
528 /* read full-precision SF indexes */
529 for (i = 0; i < num_long_vals; i++)
530 chan->qu_sf_idx[i] = bitstream_read(bc, 6);
532 /* all others are: min_val + delta */
533 for (i = num_long_vals; i < ctx->used_quant_units; i++)
534 chan->qu_sf_idx[i] = (min_val +
535 bitstream_read(bc, delta_bits)) & 0x3F;
541 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
543 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
544 chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
546 for (i = 1; i < ctx->used_quant_units; i++) {
547 diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
548 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
549 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
552 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];
554 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
556 for (i = 0; i < ctx->used_quant_units; i++) {
557 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
558 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
559 sign_extend(delta, 4)) & 0x3F;
565 /* copy coefficients from reference channel */
566 for (i = 0; i < ctx->used_quant_units; i++)
567 chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
569 weight_idx = bitstream_read(bc, 2);
570 vlc_sel = bitstream_read(bc, 2);
571 vlc_tab = &sf_vlc_tabs[vlc_sel];
573 if (weight_idx == 3) {
574 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
576 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
578 diff = (bitstream_read(bc, 4) + 56) & 0x3F;
579 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
581 for (i = 1; i < ctx->used_quant_units; i++) {
582 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
583 diff = (diff + sign_extend(delta, 4)) & 0x3F;
584 chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
587 /* 1st coefficient is coded directly */
588 chan->qu_sf_idx[0] = bitstream_read(bc, 6);
590 for (i = 1; i < ctx->used_quant_units; i++) {
591 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
592 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
599 if (weight_idx && weight_idx < 3)
600 return subtract_sf_weights(ctx, chan, weight_idx, avctx);
606 * Decode word length information for each channel.
608 * @param[in] bc the Bitstream context
609 * @param[in,out] ctx ptr to the channel unit context
610 * @param[in] num_channels number of channels to process
611 * @param[in] avctx ptr to the AVCodecContext
612 * @return result code: 0 = OK, otherwise - error code
614 static int decode_quant_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
615 int num_channels, AVCodecContext *avctx)
619 for (ch_num = 0; ch_num < num_channels; ch_num++) {
620 memset(ctx->channels[ch_num].qu_wordlen, 0,
621 sizeof(ctx->channels[ch_num].qu_wordlen));
623 if ((ret = decode_channel_wordlen(bc, ctx, ch_num, avctx)) < 0)
627 /* scan for last non-zero coeff in both channels and
628 * set number of quant units having coded spectrum */
629 for (i = ctx->num_quant_units - 1; i >= 0; i--)
630 if (ctx->channels[0].qu_wordlen[i] ||
631 (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
633 ctx->used_quant_units = i + 1;
639 * Decode scale factor indexes for each channel.
641 * @param[in] bc the Bitstream context
642 * @param[in,out] ctx ptr to the channel unit context
643 * @param[in] num_channels number of channels to process
644 * @param[in] avctx ptr to the AVCodecContext
645 * @return result code: 0 = OK, otherwise - error code
647 static int decode_scale_factors(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
648 int num_channels, AVCodecContext *avctx)
652 if (!ctx->used_quant_units)
655 for (ch_num = 0; ch_num < num_channels; ch_num++) {
656 memset(ctx->channels[ch_num].qu_sf_idx, 0,
657 sizeof(ctx->channels[ch_num].qu_sf_idx));
659 if ((ret = decode_channel_sf_idx(bc, ctx, ch_num, avctx)) < 0)
667 * Decode number of code table values.
669 * @param[in] bc the Bitstream context
670 * @param[in,out] ctx ptr to the channel unit context
671 * @param[in] avctx ptr to the AVCodecContext
672 * @return result code: 0 = OK, otherwise - error code
674 static int get_num_ct_values(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
675 AVCodecContext *avctx)
679 if (bitstream_read_bit(bc)) {
680 num_coded_vals = bitstream_read(bc, 5);
681 if (num_coded_vals > ctx->used_quant_units) {
682 av_log(avctx, AV_LOG_ERROR,
683 "Invalid number of code table indexes: %d!\n", num_coded_vals);
684 return AVERROR_INVALIDDATA;
686 return num_coded_vals;
688 return ctx->used_quant_units;
691 #define DEC_CT_IDX_COMMON(OP) \
692 num_vals = get_num_ct_values(bc, ctx, avctx); \
696 for (i = 0; i < num_vals; i++) { \
697 if (chan->qu_wordlen[i]) { \
698 chan->qu_tab_idx[i] = OP; \
699 } else if (ch_num && ref_chan->qu_wordlen[i]) \
700 /* get clone master flag */ \
701 chan->qu_tab_idx[i] = bitstream_read_bit(bc); \
704 #define CODING_DIRECT bitstream_read(bc, num_bits)
706 #define CODING_VLC bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)
708 #define CODING_VLC_DELTA \
710 : (pred + bitstream_read_vlc(bc, delta_vlc->table, \
711 delta_vlc->bits, 1)) & mask; \
712 pred = chan->qu_tab_idx[i]
714 #define CODING_VLC_DIFF \
715 (ref_chan->qu_tab_idx[i] + \
716 bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)) & mask
719 * Decode code table indexes for each quant unit of a channel.
721 * @param[in] bc the Bitstream context
722 * @param[in,out] ctx ptr to the channel unit context
723 * @param[in] ch_num channel to process
724 * @param[in] avctx ptr to the AVCodecContext
725 * @return result code: 0 = OK, otherwise - error code
727 static int decode_channel_code_tab(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
728 int ch_num, AVCodecContext *avctx)
730 int i, num_vals, num_bits, pred;
731 int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
732 VLC *vlc_tab, *delta_vlc;
733 Atrac3pChanParams *chan = &ctx->channels[ch_num];
734 Atrac3pChanParams *ref_chan = &ctx->channels[0];
736 chan->table_type = bitstream_read_bit(bc);
738 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
739 case 0: /* directly coded */
740 num_bits = ctx->use_full_table + 2;
741 DEC_CT_IDX_COMMON(CODING_DIRECT);
743 case 1: /* entropy-coded */
744 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
746 DEC_CT_IDX_COMMON(CODING_VLC);
748 case 2: /* entropy-coded delta */
749 if (ctx->use_full_table) {
750 vlc_tab = &ct_vlc_tabs[1];
751 delta_vlc = &ct_vlc_tabs[2];
753 vlc_tab = ct_vlc_tabs;
754 delta_vlc = ct_vlc_tabs;
757 DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
759 case 3: /* entropy-coded difference to master */
761 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
763 DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
772 * Decode code table indexes for each channel.
774 * @param[in] bc the Bitstream context
775 * @param[in,out] ctx ptr to the channel unit context
776 * @param[in] num_channels number of channels to process
777 * @param[in] avctx ptr to the AVCodecContext
778 * @return result code: 0 = OK, otherwise - error code
780 static int decode_code_table_indexes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
781 int num_channels, AVCodecContext *avctx)
785 if (!ctx->used_quant_units)
788 ctx->use_full_table = bitstream_read_bit(bc);
790 for (ch_num = 0; ch_num < num_channels; ch_num++) {
791 memset(ctx->channels[ch_num].qu_tab_idx, 0,
792 sizeof(ctx->channels[ch_num].qu_tab_idx));
794 if ((ret = decode_channel_code_tab(bc, ctx, ch_num, avctx)) < 0)
802 * Decode huffman-coded spectral lines for a given quant unit.
804 * This is a generalized version for all known coding modes.
805 * Its speed can be improved by creating separate functions for each mode.
807 * @param[in] bc the Bitstream context
808 * @param[in] tab code table telling how to decode spectral lines
809 * @param[in] vlc_tab ptr to the huffman table associated with the code table
810 * @param[out] out pointer to buffer where decoded data should be stored
811 * @param[in] num_specs number of spectral lines to decode
813 static void decode_qu_spectra(BitstreamContext *bc, const Atrac3pSpecCodeTab *tab,
814 VLC *vlc_tab, int16_t *out, const int num_specs)
817 int group_size = tab->group_size;
818 int num_coeffs = tab->num_coeffs;
819 int bits = tab->bits;
820 int is_signed = tab->is_signed;
821 unsigned val, mask = (1 << bits) - 1;
823 for (pos = 0; pos < num_specs;) {
824 if (group_size == 1 || bitstream_read_bit(bc)) {
825 for (j = 0; j < group_size; j++) {
826 val = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
828 for (i = 0; i < num_coeffs; i++) {
831 cf = sign_extend(cf, bits);
832 else if (cf && bitstream_read_bit(bc))
839 } else /* group skipped */
840 pos += group_size * num_coeffs;
845 * Decode huffman-coded IMDCT spectrum for all channels.
847 * @param[in] bc the Bitstream context
848 * @param[in,out] ctx ptr to the channel unit context
849 * @param[in] num_channels number of channels to process
850 * @param[in] avctx ptr to the AVCodecContext
852 static void decode_spectrum(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
853 int num_channels, AVCodecContext *avctx)
855 int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
856 const Atrac3pSpecCodeTab *tab;
857 Atrac3pChanParams *chan;
859 for (ch_num = 0; ch_num < num_channels; ch_num++) {
860 chan = &ctx->channels[ch_num];
862 memset(chan->spectrum, 0, sizeof(chan->spectrum));
864 /* set power compensation level to disabled */
865 memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
867 for (qu = 0; qu < ctx->used_quant_units; qu++) {
868 num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
869 ff_atrac3p_qu_to_spec_pos[qu];
871 wordlen = chan->qu_wordlen[qu];
872 codetab = chan->qu_tab_idx[qu];
874 if (!ctx->use_full_table)
875 codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
877 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
878 tab = &atrac3p_spectra_tabs[tab_index];
880 /* this allows reusing VLC tables */
881 if (tab->redirect >= 0)
882 tab_index = tab->redirect;
884 decode_qu_spectra(bc, tab, &spec_vlc_tabs[tab_index],
885 &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
887 } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
888 /* copy coefficients from master */
889 memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
890 &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
892 sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
893 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
897 /* Power compensation levels only present in the bitstream
898 * if there are more than 2 quant units. The lowest two units
899 * correspond to the frequencies 0...351 Hz, whose shouldn't
900 * be affected by the power compensation. */
901 if (ctx->used_quant_units > 2) {
902 num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
903 for (i = 0; i < num_specs; i++)
904 chan->power_levs[i] = bitstream_read(bc, 4);
910 * Retrieve specified amount of flag bits from the input bitstream.
911 * The data can be shortened in the case of the following two common conditions:
912 * if all bits are zero then only one signal bit = 0 will be stored,
913 * if all bits are ones then two signal bits = 1,0 will be stored.
914 * Otherwise, all necessary bits will be directly stored
915 * prefixed by two signal bits = 1,1.
917 * @param[in] bc ptr to the BitstreamContext
918 * @param[out] out where to place decoded flags
919 * @param[in] num_flags number of flags to process
920 * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
922 static int get_subband_flags(BitstreamContext *bc, uint8_t *out, int num_flags)
926 memset(out, 0, num_flags);
928 result = bitstream_read_bit(bc);
930 if (bitstream_read_bit(bc))
931 for (i = 0; i < num_flags; i++)
932 out[i] = bitstream_read_bit(bc);
934 memset(out, 1, num_flags);
941 * Decode mdct window shape flags for all channels.
943 * @param[in] bc the Bitstream context
944 * @param[in,out] ctx ptr to the channel unit context
945 * @param[in] num_channels number of channels to process
947 static void decode_window_shape(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
952 for (ch_num = 0; ch_num < num_channels; ch_num++)
953 get_subband_flags(bc, ctx->channels[ch_num].wnd_shape,
958 * Decode number of gain control points.
960 * @param[in] bc the Bitstream context
961 * @param[in,out] ctx ptr to the channel unit context
962 * @param[in] ch_num channel to process
963 * @param[in] coded_subbands number of subbands to process
964 * @return result code: 0 = OK, otherwise - error code
966 static int decode_gainc_npoints(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
967 int ch_num, int coded_subbands)
969 int i, delta, delta_bits, min_val;
970 Atrac3pChanParams *chan = &ctx->channels[ch_num];
971 Atrac3pChanParams *ref_chan = &ctx->channels[0];
973 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
974 case 0: /* fixed-length coding */
975 for (i = 0; i < coded_subbands; i++)
976 chan->gain_data[i].num_points = bitstream_read(bc, 3);
978 case 1: /* variable-length coding */
979 for (i = 0; i < coded_subbands; i++)
980 chan->gain_data[i].num_points =
981 bitstream_read_vlc(bc, gain_vlc_tabs[0].table,
982 gain_vlc_tabs[0].bits, 1);
985 if (ch_num) { /* VLC modulo delta to master channel */
986 for (i = 0; i < coded_subbands; i++) {
987 delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table,
988 gain_vlc_tabs[1].bits, 1);
989 chan->gain_data[i].num_points =
990 (ref_chan->gain_data[i].num_points + delta) & 7;
992 } else { /* VLC modulo delta to previous */
993 chan->gain_data[0].num_points =
994 bitstream_read_vlc(bc, gain_vlc_tabs[0].table,
995 gain_vlc_tabs[0].bits, 1);
997 for (i = 1; i < coded_subbands; i++) {
998 delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table,
999 gain_vlc_tabs[1].bits, 1);
1000 chan->gain_data[i].num_points =
1001 (chan->gain_data[i - 1].num_points + delta) & 7;
1006 if (ch_num) { /* copy data from master channel */
1007 for (i = 0; i < coded_subbands; i++)
1008 chan->gain_data[i].num_points =
1009 ref_chan->gain_data[i].num_points;
1010 } else { /* shorter delta to min */
1011 delta_bits = bitstream_read(bc, 2);
1012 min_val = bitstream_read(bc, 3);
1014 for (i = 0; i < coded_subbands; i++) {
1015 chan->gain_data[i].num_points = min_val + bitstream_read(bc, delta_bits);
1016 if (chan->gain_data[i].num_points > 7)
1017 return AVERROR_INVALIDDATA;
1026 * Implements coding mode 3 (slave) for gain compensation levels.
1028 * @param[out] dst ptr to the output array
1029 * @param[in] ref ptr to the reference channel
1031 static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1035 for (i = 0; i < dst->num_points; i++)
1036 dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1040 * Implements coding mode 1 (master) for gain compensation levels.
1042 * @param[in] bc the Bitstream context
1043 * @param[in] ctx ptr to the channel unit context
1044 * @param[out] dst ptr to the output array
1046 static inline void gainc_level_mode1m(BitstreamContext *bc,
1047 Atrac3pChanUnitCtx *ctx,
1052 if (dst->num_points > 0)
1053 dst->lev_code[0] = bitstream_read_vlc(bc, gain_vlc_tabs[2].table,
1054 gain_vlc_tabs[2].bits, 1);
1056 for (i = 1; i < dst->num_points; i++) {
1057 delta = bitstream_read_vlc(bc, gain_vlc_tabs[3].table,
1058 gain_vlc_tabs[3].bits, 1);
1059 dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
1064 * Decode level code for each gain control point.
1066 * @param[in] bc the Bitstream context
1067 * @param[in,out] ctx ptr to the channel unit context
1068 * @param[in] ch_num channel to process
1069 * @param[in] coded_subbands number of subbands to process
1070 * @return result code: 0 = OK, otherwise - error code
1072 static int decode_gainc_levels(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1073 int ch_num, int coded_subbands)
1075 int sb, i, delta, delta_bits, min_val, pred;
1076 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1077 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1079 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
1080 case 0: /* fixed-length coding */
1081 for (sb = 0; sb < coded_subbands; sb++)
1082 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1083 chan->gain_data[sb].lev_code[i] = bitstream_read(bc, 4);
1086 if (ch_num) { /* VLC modulo delta to master channel */
1087 for (sb = 0; sb < coded_subbands; sb++)
1088 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1089 delta = bitstream_read_vlc(bc, gain_vlc_tabs[5].table,
1090 gain_vlc_tabs[5].bits, 1);
1091 pred = (i >= ref_chan->gain_data[sb].num_points)
1092 ? 7 : ref_chan->gain_data[sb].lev_code[i];
1093 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1095 } else { /* VLC modulo delta to previous */
1096 for (sb = 0; sb < coded_subbands; sb++)
1097 gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]);
1101 if (ch_num) { /* VLC modulo delta to previous or clone master */
1102 for (sb = 0; sb < coded_subbands; sb++)
1103 if (chan->gain_data[sb].num_points > 0) {
1104 if (bitstream_read_bit(bc))
1105 gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]);
1107 gainc_level_mode3s(&chan->gain_data[sb],
1108 &ref_chan->gain_data[sb]);
1110 } else { /* VLC modulo delta to lev_codes of previous subband */
1111 if (chan->gain_data[0].num_points > 0)
1112 gainc_level_mode1m(bc, ctx, &chan->gain_data[0]);
1114 for (sb = 1; sb < coded_subbands; sb++)
1115 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1116 delta = bitstream_read_vlc(bc, gain_vlc_tabs[4].table,
1117 gain_vlc_tabs[4].bits, 1);
1118 pred = (i >= chan->gain_data[sb - 1].num_points)
1119 ? 7 : chan->gain_data[sb - 1].lev_code[i];
1120 chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1125 if (ch_num) { /* clone master */
1126 for (sb = 0; sb < coded_subbands; sb++)
1127 gainc_level_mode3s(&chan->gain_data[sb],
1128 &ref_chan->gain_data[sb]);
1129 } else { /* shorter delta to min */
1130 delta_bits = bitstream_read(bc, 2);
1131 min_val = bitstream_read(bc, 4);
1133 for (sb = 0; sb < coded_subbands; sb++)
1134 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1135 chan->gain_data[sb].lev_code[i] = min_val + bitstream_read(bc, delta_bits);
1136 if (chan->gain_data[sb].lev_code[i] > 15)
1137 return AVERROR_INVALIDDATA;
1147 * Implements coding mode 0 for gain compensation locations.
1149 * @param[in] bc the Bitstream context
1150 * @param[in] ctx ptr to the channel unit context
1151 * @param[out] dst ptr to the output array
1152 * @param[in] pos position of the value to be processed
1154 static inline void gainc_loc_mode0(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1155 AtracGainInfo *dst, int pos)
1159 if (!pos || dst->loc_code[pos - 1] < 15)
1160 dst->loc_code[pos] = bitstream_read(bc, 5);
1161 else if (dst->loc_code[pos - 1] >= 30)
1162 dst->loc_code[pos] = 31;
1164 delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1165 dst->loc_code[pos] = dst->loc_code[pos - 1] +
1166 bitstream_read(bc, delta_bits) + 1;
1171 * Implements coding mode 1 for gain compensation locations.
1173 * @param[in] bc the Bitstream context
1174 * @param[in] ctx ptr to the channel unit context
1175 * @param[out] dst ptr to the output array
1177 static inline void gainc_loc_mode1(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1183 if (dst->num_points > 0) {
1184 /* 1st coefficient is stored directly */
1185 dst->loc_code[0] = bitstream_read(bc, 5);
1187 for (i = 1; i < dst->num_points; i++) {
1188 /* switch VLC according to the curve direction
1189 * (ascending/descending) */
1190 tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
1192 : &gain_vlc_tabs[9];
1193 dst->loc_code[i] = dst->loc_code[i - 1] +
1194 bitstream_read_vlc(bc, tab->table, tab->bits, 1);
1200 * Decode location code for each gain control point.
1202 * @param[in] bc the Bitstream context
1203 * @param[in,out] ctx ptr to the channel unit context
1204 * @param[in] ch_num channel to process
1205 * @param[in] coded_subbands number of subbands to process
1206 * @param[in] avctx ptr to the AVCodecContext
1207 * @return result code: 0 = OK, otherwise - error code
1209 static int decode_gainc_loc_codes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1210 int ch_num, int coded_subbands,
1211 AVCodecContext *avctx)
1213 int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1214 AtracGainInfo *dst, *ref;
1216 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1217 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1219 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
1220 case 0: /* sequence of numbers in ascending order */
1221 for (sb = 0; sb < coded_subbands; sb++)
1222 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1223 gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i);
1227 for (sb = 0; sb < coded_subbands; sb++) {
1228 if (chan->gain_data[sb].num_points <= 0)
1230 dst = &chan->gain_data[sb];
1231 ref = &ref_chan->gain_data[sb];
1233 /* 1st value is vlc-coded modulo delta to master */
1234 delta = bitstream_read_vlc(bc, gain_vlc_tabs[10].table,
1235 gain_vlc_tabs[10].bits, 1);
1236 pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1237 dst->loc_code[0] = (pred + delta) & 0x1F;
1239 for (i = 1; i < dst->num_points; i++) {
1240 more_than_ref = i >= ref->num_points;
1241 if (dst->lev_code[i] > dst->lev_code[i - 1]) {
1242 /* ascending curve */
1243 if (more_than_ref) {
1245 bitstream_read_vlc(bc, gain_vlc_tabs[9].table,
1246 gain_vlc_tabs[9].bits, 1);
1247 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1249 if (bitstream_read_bit(bc))
1250 gainc_loc_mode0(bc, ctx, dst, i); // direct coding
1252 dst->loc_code[i] = ref->loc_code[i]; // clone master
1254 } else { /* descending curve */
1255 tab = more_than_ref ? &gain_vlc_tabs[7]
1256 : &gain_vlc_tabs[10];
1257 delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1);
1259 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1261 dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1265 } else /* VLC delta to previous */
1266 for (sb = 0; sb < coded_subbands; sb++)
1267 gainc_loc_mode1(bc, ctx, &chan->gain_data[sb]);
1271 for (sb = 0; sb < coded_subbands; sb++) {
1272 if (chan->gain_data[sb].num_points <= 0)
1274 dst = &chan->gain_data[sb];
1275 ref = &ref_chan->gain_data[sb];
1276 if (dst->num_points > ref->num_points || bitstream_read_bit(bc))
1277 gainc_loc_mode1(bc, ctx, dst);
1278 else /* clone master for the whole subband */
1279 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1280 dst->loc_code[i] = ref->loc_code[i];
1283 /* data for the first subband is coded directly */
1284 for (i = 0; i < chan->gain_data[0].num_points; i++)
1285 gainc_loc_mode0(bc, ctx, &chan->gain_data[0], i);
1287 for (sb = 1; sb < coded_subbands; sb++) {
1288 if (chan->gain_data[sb].num_points <= 0)
1290 dst = &chan->gain_data[sb];
1292 /* 1st value is vlc-coded modulo delta to the corresponding
1293 * value of the previous subband if any or zero */
1294 delta = bitstream_read_vlc(bc, gain_vlc_tabs[6].table,
1295 gain_vlc_tabs[6].bits, 1);
1296 pred = dst[-1].num_points > 0
1297 ? dst[-1].loc_code[0] : 0;
1298 dst->loc_code[0] = (pred + delta) & 0x1F;
1300 for (i = 1; i < dst->num_points; i++) {
1301 more_than_ref = i >= dst[-1].num_points;
1302 /* Select VLC table according to curve direction and
1303 * presence of prediction. */
1304 tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
1305 2 + more_than_ref + 6];
1306 delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1);
1308 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1310 dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1316 if (ch_num) { /* clone master or direct or direct coding */
1317 for (sb = 0; sb < coded_subbands; sb++)
1318 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1319 if (i >= ref_chan->gain_data[sb].num_points)
1320 gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i);
1322 chan->gain_data[sb].loc_code[i] =
1323 ref_chan->gain_data[sb].loc_code[i];
1325 } else { /* shorter delta to min */
1326 delta_bits = bitstream_read(bc, 2) + 1;
1327 min_val = bitstream_read(bc, 5);
1329 for (sb = 0; sb < coded_subbands; sb++)
1330 for (i = 0; i < chan->gain_data[sb].num_points; i++)
1331 chan->gain_data[sb].loc_code[i] = min_val + i +
1332 bitstream_read(bc, delta_bits);
1337 /* Validate decoded information */
1338 for (sb = 0; sb < coded_subbands; sb++) {
1339 dst = &chan->gain_data[sb];
1340 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1341 if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
1342 (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
1343 av_log(avctx, AV_LOG_ERROR,
1344 "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
1345 ch_num, sb, i, dst->loc_code[i]);
1346 return AVERROR_INVALIDDATA;
1355 * Decode gain control data for all channels.
1357 * @param[in] bc the Bitstream context
1358 * @param[in,out] ctx ptr to the channel unit context
1359 * @param[in] num_channels number of channels to process
1360 * @param[in] avctx ptr to the AVCodecContext
1361 * @return result code: 0 = OK, otherwise - error code
1363 static int decode_gainc_data(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1364 int num_channels, AVCodecContext *avctx)
1366 int ch_num, coded_subbands, sb, ret;
1368 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1369 memset(ctx->channels[ch_num].gain_data, 0,
1370 sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
1372 if (bitstream_read_bit(bc)) { /* gain control data present? */
1373 coded_subbands = bitstream_read(bc, 4) + 1;
1374 if (bitstream_read_bit(bc)) /* is high band gain data replication on? */
1375 ctx->channels[ch_num].num_gain_subbands = bitstream_read(bc, 4) + 1;
1377 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1379 if ((ret = decode_gainc_npoints(bc, ctx, ch_num, coded_subbands)) < 0 ||
1380 (ret = decode_gainc_levels(bc, ctx, ch_num, coded_subbands)) < 0 ||
1381 (ret = decode_gainc_loc_codes(bc, ctx, ch_num, coded_subbands, avctx)) < 0)
1384 if (coded_subbands > 0) { /* propagate gain data if requested */
1385 for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
1386 ctx->channels[ch_num].gain_data[sb] =
1387 ctx->channels[ch_num].gain_data[sb - 1];
1390 ctx->channels[ch_num].num_gain_subbands = 0;
1398 * Decode envelope for all tones of a channel.
1400 * @param[in] bc the Bitstream context
1401 * @param[in,out] ctx ptr to the channel unit context
1402 * @param[in] ch_num channel to process
1403 * @param[in] band_has_tones ptr to an array of per-band-flags:
1404 * 1 - tone data present
1406 static void decode_tones_envelope(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1407 int ch_num, int band_has_tones[])
1410 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1411 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1413 if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */
1414 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1415 if (!band_has_tones[sb])
1417 dst[sb].pend_env.has_start_point = bitstream_read_bit(bc);
1418 dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
1419 ? bitstream_read(bc, 5) : -1;
1420 dst[sb].pend_env.has_stop_point = bitstream_read_bit(bc);
1421 dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
1422 ? bitstream_read(bc, 5) : 32;
1424 } else { /* mode 1(slave only): copy master */
1425 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1426 if (!band_has_tones[sb])
1428 dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
1429 dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
1430 dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
1431 dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
1437 * Decode number of tones for each subband of a channel.
1439 * @param[in] bc the Bitstream context
1440 * @param[in,out] ctx ptr to the channel unit context
1441 * @param[in] ch_num channel to process
1442 * @param[in] band_has_tones ptr to an array of per-band-flags:
1443 * 1 - tone data present
1444 * @param[in] avctx ptr to the AVCodecContext
1445 * @return result code: 0 = OK, otherwise - error code
1447 static int decode_band_numwavs(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1448 int ch_num, int band_has_tones[],
1449 AVCodecContext *avctx)
1451 int mode, sb, delta;
1452 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1453 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1455 mode = bitstream_read(bc, ch_num + 1);
1457 case 0: /** fixed-length coding */
1458 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1459 if (band_has_tones[sb])
1460 dst[sb].num_wavs = bitstream_read(bc, 4);
1462 case 1: /** variable-length coding */
1463 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1464 if (band_has_tones[sb])
1466 bitstream_read_vlc(bc, tone_vlc_tabs[1].table,
1467 tone_vlc_tabs[1].bits, 1);
1469 case 2: /** VLC modulo delta to master (slave only) */
1470 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1471 if (band_has_tones[sb]) {
1472 delta = bitstream_read_vlc(bc, tone_vlc_tabs[2].table,
1473 tone_vlc_tabs[2].bits, 1);
1474 delta = sign_extend(delta, 3);
1475 dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1478 case 3: /** copy master (slave only) */
1479 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1480 if (band_has_tones[sb])
1481 dst[sb].num_wavs = ref[sb].num_wavs;
1485 /** initialize start tone index for each subband */
1486 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1487 if (band_has_tones[sb]) {
1488 if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
1489 av_log(avctx, AV_LOG_ERROR,
1490 "Too many tones: %d (max. 48), frame: %d!\n",
1491 ctx->waves_info->tones_index + dst[sb].num_wavs,
1492 avctx->frame_number);
1493 return AVERROR_INVALIDDATA;
1495 dst[sb].start_index = ctx->waves_info->tones_index;
1496 ctx->waves_info->tones_index += dst[sb].num_wavs;
1503 * Decode frequency information for each subband of a channel.
1505 * @param[in] bc the Bitstream context
1506 * @param[in,out] ctx ptr to the channel unit context
1507 * @param[in] ch_num channel to process
1508 * @param[in] band_has_tones ptr to an array of per-band-flags:
1509 * 1 - tone data present
1511 static void decode_tones_frequency(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1512 int ch_num, int band_has_tones[])
1514 int sb, i, direction, nbits, pred, delta;
1515 Atrac3pWaveParam *iwav, *owav;
1516 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1517 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1519 if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */
1520 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1521 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1523 iwav = &ctx->waves_info->waves[dst[sb].start_index];
1524 direction = (dst[sb].num_wavs > 1) ? bitstream_read_bit(bc) : 0;
1525 if (direction) { /** packed numbers in descending order */
1526 if (dst[sb].num_wavs)
1527 iwav[dst[sb].num_wavs - 1].freq_index = bitstream_read(bc, 10);
1528 for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1529 nbits = av_log2(iwav[i+1].freq_index) + 1;
1530 iwav[i].freq_index = bitstream_read(bc, nbits);
1532 } else { /** packed numbers in ascending order */
1533 for (i = 0; i < dst[sb].num_wavs; i++) {
1534 if (!i || iwav[i - 1].freq_index < 512)
1535 iwav[i].freq_index = bitstream_read(bc, 10);
1537 nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1538 iwav[i].freq_index = bitstream_read(bc, nbits) +
1539 1024 - (1 << nbits);
1544 } else { /* mode 1: VLC modulo delta to master (slave only) */
1545 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1546 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1548 iwav = &ctx->waves_info->waves[ref[sb].start_index];
1549 owav = &ctx->waves_info->waves[dst[sb].start_index];
1550 for (i = 0; i < dst[sb].num_wavs; i++) {
1551 delta = bitstream_read_vlc(bc, tone_vlc_tabs[6].table,
1552 tone_vlc_tabs[6].bits, 1);
1553 delta = sign_extend(delta, 8);
1554 pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
1555 (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
1556 owav[i].freq_index = (pred + delta) & 0x3FF;
1563 * Decode amplitude information for each subband of a channel.
1565 * @param[in] bc the Bitstream context
1566 * @param[in,out] ctx ptr to the channel unit context
1567 * @param[in] ch_num channel to process
1568 * @param[in] band_has_tones ptr to an array of per-band-flags:
1569 * 1 - tone data present
1571 static void decode_tones_amplitude(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1572 int ch_num, int band_has_tones[])
1574 int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1575 Atrac3pWaveParam *wsrc, *wref;
1576 int refwaves[48] = { 0 };
1577 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1578 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1581 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1582 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1584 wsrc = &ctx->waves_info->waves[dst[sb].start_index];
1585 wref = &ctx->waves_info->waves[ref[sb].start_index];
1586 for (j = 0; j < dst[sb].num_wavs; j++) {
1587 for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
1588 diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
1589 if (diff < maxdiff) {
1596 refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
1597 else if (j < ref[sb].num_wavs)
1598 refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
1600 refwaves[dst[sb].start_index + j] = -1;
1605 mode = bitstream_read(bc, ch_num + 1);
1608 case 0: /** fixed-length coding */
1609 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1610 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1612 if (ctx->waves_info->amplitude_mode)
1613 for (i = 0; i < dst[sb].num_wavs; i++)
1614 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = bitstream_read(bc, 6);
1616 ctx->waves_info->waves[dst[sb].start_index].amp_sf = bitstream_read(bc, 6);
1619 case 1: /** min + VLC delta */
1620 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1621 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1623 if (ctx->waves_info->amplitude_mode)
1624 for (i = 0; i < dst[sb].num_wavs; i++)
1625 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1626 bitstream_read_vlc(bc, tone_vlc_tabs[3].table,
1627 tone_vlc_tabs[3].bits, 1) + 20;
1629 ctx->waves_info->waves[dst[sb].start_index].amp_sf =
1630 bitstream_read_vlc(bc, tone_vlc_tabs[4].table,
1631 tone_vlc_tabs[4].bits, 1) + 24;
1634 case 2: /** VLC modulo delta to master (slave only) */
1635 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1636 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1638 for (i = 0; i < dst[sb].num_wavs; i++) {
1639 delta = bitstream_read_vlc(bc, tone_vlc_tabs[5].table,
1640 tone_vlc_tabs[5].bits, 1);
1641 delta = sign_extend(delta, 5);
1642 pred = refwaves[dst[sb].start_index + i] >= 0 ?
1643 ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
1644 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
1648 case 3: /** clone master (slave only) */
1649 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1650 if (!band_has_tones[sb])
1652 for (i = 0; i < dst[sb].num_wavs; i++)
1653 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1654 refwaves[dst[sb].start_index + i] >= 0
1655 ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
1663 * Decode phase information for each subband of a channel.
1665 * @param[in] bc the Bitstream context
1666 * @param[in,out] ctx ptr to the channel unit context
1667 * @param[in] ch_num channel to process
1668 * @param[in] band_has_tones ptr to an array of per-band-flags:
1669 * 1 - tone data present
1671 static void decode_tones_phase(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1672 int ch_num, int band_has_tones[])
1675 Atrac3pWaveParam *wparam;
1676 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1678 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1679 if (!band_has_tones[sb])
1681 wparam = &ctx->waves_info->waves[dst[sb].start_index];
1682 for (i = 0; i < dst[sb].num_wavs; i++)
1683 wparam[i].phase_index = bitstream_read(bc, 5);
1688 * Decode tones info for all channels.
1690 * @param[in] bc the Bitstream context
1691 * @param[in,out] ctx ptr to the channel unit context
1692 * @param[in] num_channels number of channels to process
1693 * @param[in] avctx ptr to the AVCodecContext
1694 * @return result code: 0 = OK, otherwise - error code
1696 static int decode_tones_info(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1697 int num_channels, AVCodecContext *avctx)
1700 int band_has_tones[16];
1702 for (ch_num = 0; ch_num < num_channels; ch_num++)
1703 memset(ctx->channels[ch_num].tones_info, 0,
1704 sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
1706 ctx->waves_info->tones_present = bitstream_read_bit(bc);
1707 if (!ctx->waves_info->tones_present)
1710 memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1712 ctx->waves_info->amplitude_mode = bitstream_read_bit(bc);
1713 if (!ctx->waves_info->amplitude_mode) {
1714 avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1715 return AVERROR_PATCHWELCOME;
1718 ctx->waves_info->num_tone_bands =
1719 bitstream_read_vlc(bc, tone_vlc_tabs[0].table,
1720 tone_vlc_tabs[0].bits, 1) + 1;
1722 if (num_channels == 2) {
1723 get_subband_flags(bc, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1724 get_subband_flags(bc, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1725 if (get_subband_flags(bc, ctx->waves_info->phase_shift,
1726 ctx->waves_info->num_tone_bands)) {
1727 avpriv_report_missing_feature(avctx, "GHA Phase shifting");
1728 return AVERROR_PATCHWELCOME;
1732 ctx->waves_info->tones_index = 0;
1734 for (ch_num = 0; ch_num < num_channels; ch_num++) {
1735 for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
1736 band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
1738 decode_tones_envelope(bc, ctx, ch_num, band_has_tones);
1739 if ((ret = decode_band_numwavs(bc, ctx, ch_num, band_has_tones,
1743 decode_tones_frequency(bc, ctx, ch_num, band_has_tones);
1744 decode_tones_amplitude(bc, ctx, ch_num, band_has_tones);
1745 decode_tones_phase(bc, ctx, ch_num, band_has_tones);
1748 if (num_channels == 2) {
1749 for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
1750 if (ctx->waves_info->tone_sharing[i])
1751 ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
1753 if (ctx->waves_info->tone_master[i])
1754 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1755 ctx->channels[1].tones_info[i]);
1762 int ff_atrac3p_decode_channel_unit(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1763 int num_channels, AVCodecContext *avctx)
1767 /* parse sound header */
1768 ctx->num_quant_units = bitstream_read(bc, 5) + 1;
1769 if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
1770 av_log(avctx, AV_LOG_ERROR,
1771 "Invalid number of quantization units: %d!\n",
1772 ctx->num_quant_units);
1773 return AVERROR_INVALIDDATA;
1776 ctx->mute_flag = bitstream_read_bit(bc);
1778 /* decode various sound parameters */
1779 if ((ret = decode_quant_wordlen(bc, ctx, num_channels, avctx)) < 0)
1782 ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
1783 ctx->num_coded_subbands = ctx->used_quant_units
1784 ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
1787 if ((ret = decode_scale_factors(bc, ctx, num_channels, avctx)) < 0)
1790 if ((ret = decode_code_table_indexes(bc, ctx, num_channels, avctx)) < 0)
1793 decode_spectrum(bc, ctx, num_channels, avctx);
1795 if (num_channels == 2) {
1796 get_subband_flags(bc, ctx->swap_channels, ctx->num_coded_subbands);
1797 get_subband_flags(bc, ctx->negate_coeffs, ctx->num_coded_subbands);
1800 decode_window_shape(bc, ctx, num_channels);
1802 if ((ret = decode_gainc_data(bc, ctx, num_channels, avctx)) < 0)
1805 if ((ret = decode_tones_info(bc, ctx, num_channels, avctx)) < 0)
1808 /* decode global noise info */
1809 ctx->noise_present = bitstream_read_bit(bc);
1810 if (ctx->noise_present) {
1811 ctx->noise_level_index = bitstream_read(bc, 4);
1812 ctx->noise_table_index = bitstream_read(bc, 4);