vlc: Add header #include when the types are used
[libav.git] / libavcodec / atrac3plus.c
CommitLineData
e6f0bb65
MP
1/*
2 * ATRAC3+ compatible decoder
3 *
4 * Copyright (c) 2010-2013 Maxim Poliakovski
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
23/**
24 * @file
25 * Bitstream parser for ATRAC3+ decoder.
26 */
27
28#include "libavutil/avassert.h"
edd4c19a 29
e6f0bb65 30#include "avcodec.h"
edd4c19a 31#include "bitstream.h"
73fc82f3 32#include "vlc.h"
e6f0bb65
MP
33#include "atrac3plus.h"
34#include "atrac3plus_data.h"
35
36static VLC_TYPE tables_data[154276][2];
37static VLC wl_vlc_tabs[4];
38static VLC sf_vlc_tabs[8];
39static VLC ct_vlc_tabs[4];
40static VLC spec_vlc_tabs[112];
41static VLC gain_vlc_tabs[11];
42static VLC tone_vlc_tabs[7];
43
e6f0bb65
MP
44/**
45 * Generate canonical VLC table from given descriptor.
46 *
47 * @param[in] cb ptr to codebook descriptor
48 * @param[in] xlat ptr to translation table or NULL
49 * @param[in,out] tab_offset starting offset to the generated vlc table
50 * @param[out] out_vlc ptr to vlc table to be generated
51 */
52static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
53 int *tab_offset, VLC *out_vlc)
54{
55 int i, b;
56 uint16_t codes[256];
57 uint8_t bits[256];
58 unsigned code = 0;
59 int index = 0;
60 int min_len = *cb++; // get shortest codeword length
61 int max_len = *cb++; // get longest codeword length
62
63 for (b = min_len; b <= max_len; b++) {
64 for (i = *cb++; i > 0; i--) {
65 av_assert0(index < 256);
66 bits[index] = b;
67 codes[index] = code++;
68 index++;
69 }
70 code <<= 1;
71 }
72
73 out_vlc->table = &tables_data[*tab_offset];
74 out_vlc->table_allocated = 1 << max_len;
75
76 ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
77 xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
78
79 *tab_offset += 1 << max_len;
80}
81
82av_cold void ff_atrac3p_init_vlcs(AVCodec *codec)
83{
84 int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
85
fd9212f2
AK
86 static const int wl_nb_bits[4] = { 2, 3, 5, 5 };
87 static const int wl_nb_codes[4] = { 3, 5, 8, 8 };
88 static const uint8_t * const wl_bits[4] = {
e6f0bb65
MP
89 atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
90 atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
91 };
fd9212f2 92 static const uint8_t * const wl_codes[4] = {
e6f0bb65
MP
93 atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
94 atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
95 };
fd9212f2 96 static const uint8_t * const wl_xlats[4] = {
e6f0bb65
MP
97 atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
98 };
99
fd9212f2
AK
100 static const int ct_nb_bits[4] = { 3, 4, 4, 4 };
101 static const int ct_nb_codes[4] = { 4, 8, 8, 8 };
102 static const uint8_t * const ct_bits[4] = {
e6f0bb65
MP
103 atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
104 atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
105 };
fd9212f2 106 static const uint8_t * const ct_codes[4] = {
e6f0bb65
MP
107 atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
108 atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
109 };
fd9212f2 110 static const uint8_t * const ct_xlats[4] = {
e6f0bb65
MP
111 NULL, NULL, atrac3p_ct_huff_xlat1, NULL
112 };
113
fd9212f2
AK
114 static const int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
115 static const int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
116 static const uint8_t * const sf_bits[8] = {
e6f0bb65
MP
117 atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
118 atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
119 atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
120 };
fd9212f2 121 static const uint16_t * const sf_codes[8] = {
e6f0bb65
MP
122 atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
123 atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
124 atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
125 };
fd9212f2 126 static const uint8_t * const sf_xlats[8] = {
e6f0bb65
MP
127 atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
128 atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
129 };
130
fd9212f2 131 static const uint8_t * const gain_cbs[11] = {
e6f0bb65
MP
132 atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
133 atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
134 atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
135 atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
136 atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
137 atrac3p_huff_gain_loc5_cb
138 };
fd9212f2 139 static const uint8_t * const gain_xlats[11] = {
e6f0bb65
MP
140 NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
141 atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
142 atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
143 atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
144 atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
145 };
146
fd9212f2 147 static const uint8_t * const tone_cbs[7] = {
e6f0bb65
MP
148 atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,
149 atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,
150 atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
151 atrac3p_huff_freq_cb
152 };
fd9212f2 153 static const uint8_t * const tone_xlats[7] = {
e6f0bb65
MP
154 NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
155 atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
156 atrac3p_huff_freq_xlat
157 };
158
159 for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
160 wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
161 wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
162 ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
163 ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
164
165 ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
166 wl_bits[i], 1, 1,
167 wl_codes[i], 1, 1,
168 wl_xlats[i], 1, 1,
169 INIT_VLC_USE_NEW_STATIC);
170
171 ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
172 ct_bits[i], 1, 1,
173 ct_codes[i], 1, 1,
174 ct_xlats[i], 1, 1,
175 INIT_VLC_USE_NEW_STATIC);
176
177 wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
178 ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
179 }
180
181 for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
182 sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
183 sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
184
185 ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
186 sf_bits[i], 1, 1,
187 sf_codes[i], 2, 2,
188 sf_xlats[i], 1, 1,
189 INIT_VLC_USE_NEW_STATIC);
190 sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
191 }
192
193 tab_offset = 2564;
194
195 /* build huffman tables for spectrum decoding */
196 for (i = 0; i < 112; i++) {
197 if (atrac3p_spectra_tabs[i].cb)
198 build_canonical_huff(atrac3p_spectra_tabs[i].cb,
199 atrac3p_spectra_tabs[i].xlat,
200 &tab_offset, &spec_vlc_tabs[i]);
201 else
202 spec_vlc_tabs[i].table = 0;
203 }
204
205 /* build huffman tables for gain data decoding */
206 for (i = 0; i < 11; i++)
207 build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
208
209 /* build huffman tables for tone decoding */
210 for (i = 0; i < 7; i++)
211 build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
212}
213
214/**
215 * Decode number of coded quantization units.
216 *
edd4c19a 217 * @param[in] bc the Bitstream context
e6f0bb65
MP
218 * @param[in,out] chan ptr to the channel parameters
219 * @param[in,out] ctx ptr to the channel unit context
220 * @param[in] avctx ptr to the AVCodecContext
221 * @return result code: 0 = OK, otherwise - error code
222 */
edd4c19a 223static int num_coded_units(BitstreamContext *bc, Atrac3pChanParams *chan,
e6f0bb65
MP
224 Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
225{
edd4c19a 226 chan->fill_mode = bitstream_read(bc, 2);
e6f0bb65
MP
227 if (!chan->fill_mode) {
228 chan->num_coded_vals = ctx->num_quant_units;
229 } else {
edd4c19a 230 chan->num_coded_vals = bitstream_read(bc, 5);
e6f0bb65
MP
231 if (chan->num_coded_vals > ctx->num_quant_units) {
232 av_log(avctx, AV_LOG_ERROR,
233 "Invalid number of transmitted units!\n");
234 return AVERROR_INVALIDDATA;
235 }
236
237 if (chan->fill_mode == 3)
edd4c19a 238 chan->split_point = bitstream_read(bc, 2) + (chan->ch_num << 1) + 1;
e6f0bb65
MP
239 }
240
241 return 0;
242}
243
244/**
245 * Add weighting coefficients to the decoded word-length information.
246 *
247 * @param[in,out] ctx ptr to the channel unit context
248 * @param[in,out] chan ptr to the channel parameters
249 * @param[in] wtab_idx index of the table of weights
250 * @param[in] avctx ptr to the AVCodecContext
251 * @return result code: 0 = OK, otherwise - error code
252 */
253static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
254 Atrac3pChanParams *chan, int wtab_idx,
255 AVCodecContext *avctx)
256{
257 int i;
258 const int8_t *weights_tab =
259 &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
260
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;
268 }
269 }
270
271 return 0;
272}
273
274/**
275 * Subtract weighting coefficients from decoded scalefactors.
276 *
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
282 */
283static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
284 Atrac3pChanParams *chan, int wtab_idx,
285 AVCodecContext *avctx)
286{
287 int i;
288 const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
289
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;
297 }
298 }
299
300 return 0;
301}
302
303/**
304 * Unpack vector quantization tables.
305 *
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
310 */
311static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
312 int *dst, int num_values)
313{
314 int i;
315
316 if (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];
320 }
321}
322
edd4c19a
AH
323#define UNPACK_SF_VQ_SHAPE(bc, dst, num_vals) \
324 start_val = bitstream_read((bc), 6); \
325 unpack_vq_shape(start_val, &atrac3p_sf_shapes[bitstream_read((bc), 6)][0], \
e6f0bb65
MP
326 (dst), (num_vals))
327
328/**
329 * Decode word length for each quantization unit of a channel.
330 *
edd4c19a 331 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
336 */
edd4c19a 337static int decode_channel_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
338 int ch_num, AVCodecContext *avctx)
339{
340 int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
341 ret, start_val;
342 VLC *vlc_tab;
343 Atrac3pChanParams *chan = &ctx->channels[ch_num];
344 Atrac3pChanParams *ref_chan = &ctx->channels[0];
345
346 chan->fill_mode = 0;
347
edd4c19a 348 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
349 case 0: /* coded using constant number of bits */
350 for (i = 0; i < ctx->num_quant_units; i++)
edd4c19a 351 chan->qu_wordlen[i] = bitstream_read(bc, 3);
e6f0bb65
MP
352 break;
353 case 1:
354 if (ch_num) {
edd4c19a 355 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
e6f0bb65
MP
356 return ret;
357
358 if (chan->num_coded_vals) {
edd4c19a 359 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
e6f0bb65
MP
360
361 for (i = 0; i < chan->num_coded_vals; i++) {
edd4c19a 362 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
363 chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
364 }
365 }
366 } else {
edd4c19a
AH
367 weight_idx = bitstream_read(bc, 2);
368 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
e6f0bb65
MP
369 return ret;
370
371 if (chan->num_coded_vals) {
edd4c19a 372 pos = bitstream_read(bc, 5);
e6f0bb65
MP
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;
377 }
378
edd4c19a
AH
379 delta_bits = bitstream_read(bc, 2);
380 min_val = bitstream_read(bc, 3);
e6f0bb65
MP
381
382 for (i = 0; i < pos; i++)
edd4c19a 383 chan->qu_wordlen[i] = bitstream_read(bc, 3);
e6f0bb65
MP
384
385 for (i = pos; i < chan->num_coded_vals; i++)
edd4c19a 386 chan->qu_wordlen[i] = (min_val + bitstream_read(bc, delta_bits)) & 7;
e6f0bb65
MP
387 }
388 }
389 break;
390 case 2:
edd4c19a 391 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
e6f0bb65
MP
392 return ret;
393
394 if (ch_num && chan->num_coded_vals) {
edd4c19a
AH
395 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
396 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
397 chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
398
399 for (i = 1; i < chan->num_coded_vals; i++) {
400 diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
edd4c19a 401 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
402 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
403 }
404 } else if (chan->num_coded_vals) {
edd4c19a
AH
405 flag = bitstream_read(bc, 1);
406 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 1)];
e6f0bb65 407
edd4c19a 408 start_val = bitstream_read(bc, 3);
e6f0bb65 409 unpack_vq_shape(start_val,
edd4c19a 410 &atrac3p_wl_shapes[start_val][bitstream_read(bc, 4)][0],
e6f0bb65
MP
411 chan->qu_wordlen, chan->num_coded_vals);
412
413 if (!flag) {
414 for (i = 0; i < chan->num_coded_vals; i++) {
edd4c19a 415 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
416 chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
417 }
418 } else {
419 for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
edd4c19a 420 if (!bitstream_read_bit(bc)) {
e6f0bb65 421 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
edd4c19a
AH
422 bitstream_read_vlc(bc, vlc_tab->table,
423 vlc_tab->bits, 1)) & 7;
e6f0bb65 424 chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
edd4c19a
AH
425 bitstream_read_vlc(bc, vlc_tab->table,
426 vlc_tab->bits, 1)) & 7;
e6f0bb65
MP
427 }
428
429 if (chan->num_coded_vals & 1)
430 chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
edd4c19a
AH
431 bitstream_read_vlc(bc, vlc_tab->table,
432 vlc_tab->bits, 1)) & 7;
e6f0bb65
MP
433 }
434 }
435 break;
436 case 3:
edd4c19a
AH
437 weight_idx = bitstream_read(bc, 2);
438 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
e6f0bb65
MP
439 return ret;
440
441 if (chan->num_coded_vals) {
edd4c19a 442 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
e6f0bb65
MP
443
444 /* first coefficient is coded directly */
edd4c19a 445 chan->qu_wordlen[0] = bitstream_read(bc, 3);
e6f0bb65
MP
446
447 for (i = 1; i < chan->num_coded_vals; i++) {
edd4c19a 448 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
449 chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
450 }
451 }
452 break;
453 }
454
455 if (chan->fill_mode == 2) {
456 for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
edd4c19a 457 chan->qu_wordlen[i] = ch_num ? bitstream_read_bit(bc) : 1;
e6f0bb65
MP
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;
463 }
464
465 if (weight_idx)
466 return add_wordlen_weights(ctx, chan, weight_idx, avctx);
467
468 return 0;
469}
470
471/**
472 * Decode scale factor indexes for each quant unit of a channel.
473 *
edd4c19a 474 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
479 */
edd4c19a 480static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
481 int ch_num, AVCodecContext *avctx)
482{
483 int i, weight_idx = 0, delta, diff, num_long_vals,
484 delta_bits, min_val, vlc_sel, start_val;
485 VLC *vlc_tab;
486 Atrac3pChanParams *chan = &ctx->channels[ch_num];
487 Atrac3pChanParams *ref_chan = &ctx->channels[0];
488
edd4c19a 489 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
490 case 0: /* coded using constant number of bits */
491 for (i = 0; i < ctx->used_quant_units; i++)
edd4c19a 492 chan->qu_sf_idx[i] = bitstream_read(bc, 6);
e6f0bb65
MP
493 break;
494 case 1:
495 if (ch_num) {
edd4c19a 496 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
e6f0bb65
MP
497
498 for (i = 0; i < ctx->used_quant_units; i++) {
edd4c19a 499 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
500 chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
501 }
502 } else {
edd4c19a 503 weight_idx = bitstream_read(bc, 2);
e6f0bb65 504 if (weight_idx == 3) {
edd4c19a 505 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
e6f0bb65 506
edd4c19a
AH
507 num_long_vals = bitstream_read(bc, 5);
508 delta_bits = bitstream_read(bc, 2);
509 min_val = bitstream_read(bc, 4) - 7;
e6f0bb65
MP
510
511 for (i = 0; i < num_long_vals; i++)
512 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
edd4c19a 513 bitstream_read(bc, 4) - 7) & 0x3F;
e6f0bb65
MP
514
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 +
edd4c19a 518 bitstream_read(bc, delta_bits)) & 0x3F;
e6f0bb65 519 } else {
edd4c19a
AH
520 num_long_vals = bitstream_read(bc, 5);
521 delta_bits = bitstream_read(bc, 3);
522 min_val = bitstream_read(bc, 6);
e6f0bb65
MP
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;
527 }
528
529 /* read full-precision SF indexes */
530 for (i = 0; i < num_long_vals; i++)
edd4c19a 531 chan->qu_sf_idx[i] = bitstream_read(bc, 6);
e6f0bb65
MP
532
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 +
edd4c19a 536 bitstream_read(bc, delta_bits)) & 0x3F;
e6f0bb65
MP
537 }
538 }
539 break;
540 case 2:
541 if (ch_num) {
edd4c19a 542 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
e6f0bb65 543
edd4c19a 544 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
545 chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
546
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];
edd4c19a 549 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
550 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
551 }
552 } else {
edd4c19a 553 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];
e6f0bb65 554
edd4c19a 555 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
e6f0bb65
MP
556
557 for (i = 0; i < ctx->used_quant_units; i++) {
edd4c19a 558 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
559 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
560 sign_extend(delta, 4)) & 0x3F;
561 }
562 }
563 break;
564 case 3:
565 if (ch_num) {
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];
569 } else {
edd4c19a
AH
570 weight_idx = bitstream_read(bc, 2);
571 vlc_sel = bitstream_read(bc, 2);
e6f0bb65
MP
572 vlc_tab = &sf_vlc_tabs[vlc_sel];
573
574 if (weight_idx == 3) {
575 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
576
edd4c19a 577 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
e6f0bb65 578
edd4c19a
AH
579 diff = (bitstream_read(bc, 4) + 56) & 0x3F;
580 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
e6f0bb65
MP
581
582 for (i = 1; i < ctx->used_quant_units; i++) {
edd4c19a 583 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
584 diff = (diff + sign_extend(delta, 4)) & 0x3F;
585 chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
586 }
587 } else {
588 /* 1st coefficient is coded directly */
edd4c19a 589 chan->qu_sf_idx[0] = bitstream_read(bc, 6);
e6f0bb65
MP
590
591 for (i = 1; i < ctx->used_quant_units; i++) {
edd4c19a 592 delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
593 chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
594 }
595 }
596 }
597 break;
598 }
599
600 if (weight_idx && weight_idx < 3)
601 return subtract_sf_weights(ctx, chan, weight_idx, avctx);
602
603 return 0;
604}
605
606/**
607 * Decode word length information for each channel.
608 *
edd4c19a 609 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
614 */
edd4c19a 615static int decode_quant_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
616 int num_channels, AVCodecContext *avctx)
617{
618 int ch_num, i, ret;
619
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));
623
edd4c19a 624 if ((ret = decode_channel_wordlen(bc, ctx, ch_num, avctx)) < 0)
e6f0bb65
MP
625 return ret;
626 }
627
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]))
633 break;
634 ctx->used_quant_units = i + 1;
635
636 return 0;
637}
638
639/**
640 * Decode scale factor indexes for each channel.
641 *
edd4c19a 642 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
647 */
edd4c19a 648static int decode_scale_factors(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
649 int num_channels, AVCodecContext *avctx)
650{
651 int ch_num, ret;
652
653 if (!ctx->used_quant_units)
654 return 0;
655
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));
659
edd4c19a 660 if ((ret = decode_channel_sf_idx(bc, ctx, ch_num, avctx)) < 0)
e6f0bb65
MP
661 return ret;
662 }
663
664 return 0;
665}
666
667/**
668 * Decode number of code table values.
669 *
edd4c19a 670 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
674 */
edd4c19a 675static int get_num_ct_values(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
676 AVCodecContext *avctx)
677{
678 int num_coded_vals;
679
edd4c19a
AH
680 if (bitstream_read_bit(bc)) {
681 num_coded_vals = bitstream_read(bc, 5);
e6f0bb65
MP
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;
686 }
687 return num_coded_vals;
688 } else
689 return ctx->used_quant_units;
690}
691
692#define DEC_CT_IDX_COMMON(OP) \
edd4c19a 693 num_vals = get_num_ct_values(bc, ctx, avctx); \
e6f0bb65
MP
694 if (num_vals < 0) \
695 return num_vals; \
696 \
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 */ \
edd4c19a 702 chan->qu_tab_idx[i] = bitstream_read_bit(bc); \
e6f0bb65
MP
703 }
704
edd4c19a 705#define CODING_DIRECT bitstream_read(bc, num_bits)
e6f0bb65 706
edd4c19a 707#define CODING_VLC bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)
e6f0bb65
MP
708
709#define CODING_VLC_DELTA \
710 (!i) ? CODING_VLC \
edd4c19a
AH
711 : (pred + bitstream_read_vlc(bc, delta_vlc->table, \
712 delta_vlc->bits, 1)) & mask; \
e6f0bb65
MP
713 pred = chan->qu_tab_idx[i]
714
715#define CODING_VLC_DIFF \
716 (ref_chan->qu_tab_idx[i] + \
edd4c19a 717 bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)) & mask
e6f0bb65
MP
718
719/**
720 * Decode code table indexes for each quant unit of a channel.
721 *
edd4c19a 722 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
727 */
edd4c19a 728static int decode_channel_code_tab(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
729 int ch_num, AVCodecContext *avctx)
730{
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];
736
edd4c19a 737 chan->table_type = bitstream_read_bit(bc);
e6f0bb65 738
edd4c19a 739 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
740 case 0: /* directly coded */
741 num_bits = ctx->use_full_table + 2;
742 DEC_CT_IDX_COMMON(CODING_DIRECT);
743 break;
744 case 1: /* entropy-coded */
745 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
746 : ct_vlc_tabs;
747 DEC_CT_IDX_COMMON(CODING_VLC);
748 break;
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];
753 } else {
754 vlc_tab = ct_vlc_tabs;
755 delta_vlc = ct_vlc_tabs;
756 }
757 pred = 0;
758 DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
759 break;
760 case 3: /* entropy-coded difference to master */
761 if (ch_num) {
762 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
763 : ct_vlc_tabs;
764 DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
765 }
766 break;
767 }
768
769 return 0;
770}
771
772/**
773 * Decode code table indexes for each channel.
774 *
edd4c19a 775 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
780 */
edd4c19a 781static int decode_code_table_indexes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
782 int num_channels, AVCodecContext *avctx)
783{
784 int ch_num, ret;
785
786 if (!ctx->used_quant_units)
787 return 0;
788
edd4c19a 789 ctx->use_full_table = bitstream_read_bit(bc);
e6f0bb65
MP
790
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));
794
edd4c19a 795 if ((ret = decode_channel_code_tab(bc, ctx, ch_num, avctx)) < 0)
e6f0bb65
MP
796 return ret;
797 }
798
799 return 0;
800}
801
802/**
803 * Decode huffman-coded spectral lines for a given quant unit.
804 *
805 * This is a generalized version for all known coding modes.
806 * Its speed can be improved by creating separate functions for each mode.
807 *
edd4c19a 808 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
813 */
edd4c19a 814static void decode_qu_spectra(BitstreamContext *bc, const Atrac3pSpecCodeTab *tab,
e6f0bb65
MP
815 VLC *vlc_tab, int16_t *out, const int num_specs)
816{
817 int i, j, pos, cf;
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;
823
824 for (pos = 0; pos < num_specs;) {
edd4c19a 825 if (group_size == 1 || bitstream_read_bit(bc)) {
e6f0bb65 826 for (j = 0; j < group_size; j++) {
edd4c19a 827 val = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);
e6f0bb65
MP
828
829 for (i = 0; i < num_coeffs; i++) {
830 cf = val & mask;
831 if (is_signed)
832 cf = sign_extend(cf, bits);
edd4c19a 833 else if (cf && bitstream_read_bit(bc))
e6f0bb65
MP
834 cf = -cf;
835
836 out[pos++] = cf;
837 val >>= bits;
838 }
839 }
840 } else /* group skipped */
841 pos += group_size * num_coeffs;
842 }
843}
844
845/**
846 * Decode huffman-coded IMDCT spectrum for all channels.
847 *
edd4c19a 848 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
852 */
edd4c19a 853static void decode_spectrum(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
854 int num_channels, AVCodecContext *avctx)
855{
856 int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
857 const Atrac3pSpecCodeTab *tab;
858 Atrac3pChanParams *chan;
859
860 for (ch_num = 0; ch_num < num_channels; ch_num++) {
861 chan = &ctx->channels[ch_num];
862
863 memset(chan->spectrum, 0, sizeof(chan->spectrum));
864
865 /* set power compensation level to disabled */
866 memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
867
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];
871
872 wordlen = chan->qu_wordlen[qu];
873 codetab = chan->qu_tab_idx[qu];
874 if (wordlen) {
875 if (!ctx->use_full_table)
876 codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
877
878 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
879 tab = &atrac3p_spectra_tabs[tab_index];
880
881 /* this allows reusing VLC tables */
882 if (tab->redirect >= 0)
883 tab_index = tab->redirect;
884
edd4c19a 885 decode_qu_spectra(bc, tab, &spec_vlc_tabs[tab_index],
e6f0bb65
MP
886 &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
887 num_specs);
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]],
892 num_specs *
893 sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
894 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
895 }
896 }
897
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++)
edd4c19a 905 chan->power_levs[i] = bitstream_read(bc, 4);
e6f0bb65
MP
906 }
907 }
908}
909
910/**
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.
917 *
edd4c19a 918 * @param[in] bc ptr to the BitstreamContext
e6f0bb65
MP
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
922 */
edd4c19a 923static int get_subband_flags(BitstreamContext *bc, uint8_t *out, int num_flags)
e6f0bb65
MP
924{
925 int i, result;
926
927 memset(out, 0, num_flags);
928
edd4c19a 929 result = bitstream_read_bit(bc);
e6f0bb65 930 if (result) {
edd4c19a 931 if (bitstream_read_bit(bc))
e6f0bb65 932 for (i = 0; i < num_flags; i++)
edd4c19a 933 out[i] = bitstream_read_bit(bc);
e6f0bb65
MP
934 else
935 memset(out, 1, num_flags);
936 }
937
938 return result;
939}
940
941/**
942 * Decode mdct window shape flags for all channels.
943 *
edd4c19a 944 * @param[in] bc the Bitstream context
e6f0bb65
MP
945 * @param[in,out] ctx ptr to the channel unit context
946 * @param[in] num_channels number of channels to process
947 */
edd4c19a 948static void decode_window_shape(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
949 int num_channels)
950{
951 int ch_num;
952
953 for (ch_num = 0; ch_num < num_channels; ch_num++)
edd4c19a 954 get_subband_flags(bc, ctx->channels[ch_num].wnd_shape,
e6f0bb65
MP
955 ctx->num_subbands);
956}
957
958/**
959 * Decode number of gain control points.
960 *
edd4c19a 961 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
966 */
edd4c19a 967static int decode_gainc_npoints(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
968 int ch_num, int coded_subbands)
969{
970 int i, delta, delta_bits, min_val;
971 Atrac3pChanParams *chan = &ctx->channels[ch_num];
972 Atrac3pChanParams *ref_chan = &ctx->channels[0];
973
edd4c19a 974 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
975 case 0: /* fixed-length coding */
976 for (i = 0; i < coded_subbands; i++)
edd4c19a 977 chan->gain_data[i].num_points = bitstream_read(bc, 3);
e6f0bb65
MP
978 break;
979 case 1: /* variable-length coding */
980 for (i = 0; i < coded_subbands; i++)
981 chan->gain_data[i].num_points =
edd4c19a
AH
982 bitstream_read_vlc(bc, gain_vlc_tabs[0].table,
983 gain_vlc_tabs[0].bits, 1);
e6f0bb65
MP
984 break;
985 case 2:
986 if (ch_num) { /* VLC modulo delta to master channel */
987 for (i = 0; i < coded_subbands; i++) {
edd4c19a
AH
988 delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table,
989 gain_vlc_tabs[1].bits, 1);
e6f0bb65
MP
990 chan->gain_data[i].num_points =
991 (ref_chan->gain_data[i].num_points + delta) & 7;
992 }
993 } else { /* VLC modulo delta to previous */
994 chan->gain_data[0].num_points =
edd4c19a
AH
995 bitstream_read_vlc(bc, gain_vlc_tabs[0].table,
996 gain_vlc_tabs[0].bits, 1);
e6f0bb65
MP
997
998 for (i = 1; i < coded_subbands; i++) {
edd4c19a
AH
999 delta = bitstream_read_vlc(bc, gain_vlc_tabs[1].table,
1000 gain_vlc_tabs[1].bits, 1);
e6f0bb65
MP
1001 chan->gain_data[i].num_points =
1002 (chan->gain_data[i - 1].num_points + delta) & 7;
1003 }
1004 }
1005 break;
1006 case 3:
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 */
edd4c19a
AH
1012 delta_bits = bitstream_read(bc, 2);
1013 min_val = bitstream_read(bc, 3);
e6f0bb65
MP
1014
1015 for (i = 0; i < coded_subbands; i++) {
edd4c19a 1016 chan->gain_data[i].num_points = min_val + bitstream_read(bc, delta_bits);
e6f0bb65
MP
1017 if (chan->gain_data[i].num_points > 7)
1018 return AVERROR_INVALIDDATA;
1019 }
1020 }
1021 }
1022
1023 return 0;
1024}
1025
1026/**
1027 * Implements coding mode 3 (slave) for gain compensation levels.
1028 *
1029 * @param[out] dst ptr to the output array
1030 * @param[in] ref ptr to the reference channel
1031 */
1032static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1033{
1034 int i;
1035
1036 for (i = 0; i < dst->num_points; i++)
1037 dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1038}
1039
1040/**
1041 * Implements coding mode 1 (master) for gain compensation levels.
1042 *
edd4c19a 1043 * @param[in] bc the Bitstream context
e6f0bb65
MP
1044 * @param[in] ctx ptr to the channel unit context
1045 * @param[out] dst ptr to the output array
1046 */
edd4c19a 1047static inline void gainc_level_mode1m(BitstreamContext *bc,
e6f0bb65
MP
1048 Atrac3pChanUnitCtx *ctx,
1049 AtracGainInfo *dst)
1050{
1051 int i, delta;
1052
1053 if (dst->num_points > 0)
edd4c19a
AH
1054 dst->lev_code[0] = bitstream_read_vlc(bc, gain_vlc_tabs[2].table,
1055 gain_vlc_tabs[2].bits, 1);
e6f0bb65
MP
1056
1057 for (i = 1; i < dst->num_points; i++) {
edd4c19a
AH
1058 delta = bitstream_read_vlc(bc, gain_vlc_tabs[3].table,
1059 gain_vlc_tabs[3].bits, 1);
e6f0bb65
MP
1060 dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
1061 }
1062}
1063
1064/**
1065 * Decode level code for each gain control point.
1066 *
edd4c19a 1067 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1072 */
edd4c19a 1073static int decode_gainc_levels(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1074 int ch_num, int coded_subbands)
1075{
1076 int sb, i, delta, delta_bits, min_val, pred;
1077 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1078 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1079
edd4c19a 1080 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
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++)
edd4c19a 1084 chan->gain_data[sb].lev_code[i] = bitstream_read(bc, 4);
e6f0bb65
MP
1085 break;
1086 case 1:
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++) {
edd4c19a
AH
1090 delta = bitstream_read_vlc(bc, gain_vlc_tabs[5].table,
1091 gain_vlc_tabs[5].bits, 1);
e6f0bb65
MP
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;
1095 }
1096 } else { /* VLC modulo delta to previous */
1097 for (sb = 0; sb < coded_subbands; sb++)
edd4c19a 1098 gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]);
e6f0bb65
MP
1099 }
1100 break;
1101 case 2:
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) {
edd4c19a
AH
1105 if (bitstream_read_bit(bc))
1106 gainc_level_mode1m(bc, ctx, &chan->gain_data[sb]);
e6f0bb65
MP
1107 else
1108 gainc_level_mode3s(&chan->gain_data[sb],
1109 &ref_chan->gain_data[sb]);
1110 }
1111 } else { /* VLC modulo delta to lev_codes of previous subband */
1112 if (chan->gain_data[0].num_points > 0)
edd4c19a 1113 gainc_level_mode1m(bc, ctx, &chan->gain_data[0]);
e6f0bb65
MP
1114
1115 for (sb = 1; sb < coded_subbands; sb++)
1116 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
edd4c19a
AH
1117 delta = bitstream_read_vlc(bc, gain_vlc_tabs[4].table,
1118 gain_vlc_tabs[4].bits, 1);
e6f0bb65
MP
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;
1122 }
1123 }
1124 break;
1125 case 3:
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 */
edd4c19a
AH
1131 delta_bits = bitstream_read(bc, 2);
1132 min_val = bitstream_read(bc, 4);
e6f0bb65
MP
1133
1134 for (sb = 0; sb < coded_subbands; sb++)
1135 for (i = 0; i < chan->gain_data[sb].num_points; i++) {
edd4c19a 1136 chan->gain_data[sb].lev_code[i] = min_val + bitstream_read(bc, delta_bits);
e6f0bb65
MP
1137 if (chan->gain_data[sb].lev_code[i] > 15)
1138 return AVERROR_INVALIDDATA;
1139 }
1140 }
1141 break;
1142 }
1143
1144 return 0;
1145}
1146
1147/**
1148 * Implements coding mode 0 for gain compensation locations.
1149 *
edd4c19a 1150 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1154 */
edd4c19a 1155static inline void gainc_loc_mode0(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1156 AtracGainInfo *dst, int pos)
1157{
1158 int delta_bits;
1159
1160 if (!pos || dst->loc_code[pos - 1] < 15)
edd4c19a 1161 dst->loc_code[pos] = bitstream_read(bc, 5);
e6f0bb65
MP
1162 else if (dst->loc_code[pos - 1] >= 30)
1163 dst->loc_code[pos] = 31;
1164 else {
1165 delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1166 dst->loc_code[pos] = dst->loc_code[pos - 1] +
edd4c19a 1167 bitstream_read(bc, delta_bits) + 1;
e6f0bb65
MP
1168 }
1169}
1170
1171/**
1172 * Implements coding mode 1 for gain compensation locations.
1173 *
edd4c19a 1174 * @param[in] bc the Bitstream context
e6f0bb65
MP
1175 * @param[in] ctx ptr to the channel unit context
1176 * @param[out] dst ptr to the output array
1177 */
edd4c19a 1178static inline void gainc_loc_mode1(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1179 AtracGainInfo *dst)
1180{
1181 int i;
1182 VLC *tab;
1183
1184 if (dst->num_points > 0) {
1185 /* 1st coefficient is stored directly */
edd4c19a 1186 dst->loc_code[0] = bitstream_read(bc, 5);
e6f0bb65
MP
1187
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])
1192 ? &gain_vlc_tabs[7]
1193 : &gain_vlc_tabs[9];
1194 dst->loc_code[i] = dst->loc_code[i - 1] +
edd4c19a 1195 bitstream_read_vlc(bc, tab->table, tab->bits, 1);
e6f0bb65
MP
1196 }
1197 }
1198}
1199
1200/**
1201 * Decode location code for each gain control point.
1202 *
edd4c19a 1203 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1209 */
edd4c19a 1210static int decode_gainc_loc_codes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1211 int ch_num, int coded_subbands,
1212 AVCodecContext *avctx)
1213{
1214 int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1215 AtracGainInfo *dst, *ref;
1216 VLC *tab;
1217 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1218 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1219
edd4c19a 1220 switch (bitstream_read(bc, 2)) { /* switch according to coding mode */
e6f0bb65
MP
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++)
edd4c19a 1224 gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i);
e6f0bb65
MP
1225 break;
1226 case 1:
1227 if (ch_num) {
1228 for (sb = 0; sb < coded_subbands; sb++) {
1229 if (chan->gain_data[sb].num_points <= 0)
1230 continue;
1231 dst = &chan->gain_data[sb];
1232 ref = &ref_chan->gain_data[sb];
1233
1234 /* 1st value is vlc-coded modulo delta to master */
edd4c19a
AH
1235 delta = bitstream_read_vlc(bc, gain_vlc_tabs[10].table,
1236 gain_vlc_tabs[10].bits, 1);
e6f0bb65
MP
1237 pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1238 dst->loc_code[0] = (pred + delta) & 0x1F;
1239
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) {
1245 delta =
edd4c19a
AH
1246 bitstream_read_vlc(bc, gain_vlc_tabs[9].table,
1247 gain_vlc_tabs[9].bits, 1);
e6f0bb65
MP
1248 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1249 } else {
edd4c19a
AH
1250 if (bitstream_read_bit(bc))
1251 gainc_loc_mode0(bc, ctx, dst, i); // direct coding
e6f0bb65
MP
1252 else
1253 dst->loc_code[i] = ref->loc_code[i]; // clone master
1254 }
1255 } else { /* descending curve */
1256 tab = more_than_ref ? &gain_vlc_tabs[7]
1257 : &gain_vlc_tabs[10];
edd4c19a 1258 delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1);
e6f0bb65
MP
1259 if (more_than_ref)
1260 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1261 else
1262 dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1263 }
1264 }
1265 }
1266 } else /* VLC delta to previous */
1267 for (sb = 0; sb < coded_subbands; sb++)
edd4c19a 1268 gainc_loc_mode1(bc, ctx, &chan->gain_data[sb]);
e6f0bb65
MP
1269 break;
1270 case 2:
1271 if (ch_num) {
1272 for (sb = 0; sb < coded_subbands; sb++) {
1273 if (chan->gain_data[sb].num_points <= 0)
1274 continue;
1275 dst = &chan->gain_data[sb];
1276 ref = &ref_chan->gain_data[sb];
edd4c19a
AH
1277 if (dst->num_points > ref->num_points || bitstream_read_bit(bc))
1278 gainc_loc_mode1(bc, ctx, dst);
e6f0bb65
MP
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];
1282 }
1283 } else {
1284 /* data for the first subband is coded directly */
1285 for (i = 0; i < chan->gain_data[0].num_points; i++)
edd4c19a 1286 gainc_loc_mode0(bc, ctx, &chan->gain_data[0], i);
e6f0bb65
MP
1287
1288 for (sb = 1; sb < coded_subbands; sb++) {
1289 if (chan->gain_data[sb].num_points <= 0)
1290 continue;
1291 dst = &chan->gain_data[sb];
1292
1293 /* 1st value is vlc-coded modulo delta to the corresponding
1294 * value of the previous subband if any or zero */
edd4c19a
AH
1295 delta = bitstream_read_vlc(bc, gain_vlc_tabs[6].table,
1296 gain_vlc_tabs[6].bits, 1);
e6f0bb65
MP
1297 pred = dst[-1].num_points > 0
1298 ? dst[-1].loc_code[0] : 0;
1299 dst->loc_code[0] = (pred + delta) & 0x1F;
1300
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];
edd4c19a 1307 delta = bitstream_read_vlc(bc, tab->table, tab->bits, 1);
e6f0bb65
MP
1308 if (more_than_ref)
1309 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1310 else
1311 dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1312 }
1313 }
1314 }
1315 break;
1316 case 3:
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)
edd4c19a 1321 gainc_loc_mode0(bc, ctx, &chan->gain_data[sb], i);
e6f0bb65
MP
1322 else
1323 chan->gain_data[sb].loc_code[i] =
1324 ref_chan->gain_data[sb].loc_code[i];
1325 }
1326 } else { /* shorter delta to min */
edd4c19a
AH
1327 delta_bits = bitstream_read(bc, 2) + 1;
1328 min_val = bitstream_read(bc, 5);
e6f0bb65
MP
1329
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 +
edd4c19a 1333 bitstream_read(bc, delta_bits);
e6f0bb65
MP
1334 }
1335 break;
1336 }
1337
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;
1348 }
1349 }
1350 }
1351
1352 return 0;
1353}
1354
1355/**
1356 * Decode gain control data for all channels.
1357 *
edd4c19a 1358 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1363 */
edd4c19a 1364static int decode_gainc_data(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1365 int num_channels, AVCodecContext *avctx)
1366{
1367 int ch_num, coded_subbands, sb, ret;
1368
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);
1372
edd4c19a
AH
1373 if (bitstream_read_bit(bc)) { /* gain control data present? */
1374 coded_subbands = bitstream_read(bc, 4) + 1;
1375 if (bitstream_read_bit(bc)) /* is high band gain data replication on? */
1376 ctx->channels[ch_num].num_gain_subbands = bitstream_read(bc, 4) + 1;
e6f0bb65
MP
1377 else
1378 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1379
edd4c19a
AH
1380 if ((ret = decode_gainc_npoints(bc, ctx, ch_num, coded_subbands)) < 0 ||
1381 (ret = decode_gainc_levels(bc, ctx, ch_num, coded_subbands)) < 0 ||
1382 (ret = decode_gainc_loc_codes(bc, ctx, ch_num, coded_subbands, avctx)) < 0)
e6f0bb65
MP
1383 return ret;
1384
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];
1389 }
1390 } else {
1391 ctx->channels[ch_num].num_gain_subbands = 0;
1392 }
1393 }
1394
1395 return 0;
1396}
1397
1398/**
1399 * Decode envelope for all tones of a channel.
1400 *
edd4c19a 1401 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1406 */
edd4c19a 1407static void decode_tones_envelope(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1408 int ch_num, int band_has_tones[])
1409{
1410 int sb;
1411 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1412 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1413
edd4c19a 1414 if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */
e6f0bb65
MP
1415 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1416 if (!band_has_tones[sb])
1417 continue;
edd4c19a 1418 dst[sb].pend_env.has_start_point = bitstream_read_bit(bc);
e6f0bb65 1419 dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
edd4c19a
AH
1420 ? bitstream_read(bc, 5) : -1;
1421 dst[sb].pend_env.has_stop_point = bitstream_read_bit(bc);
e6f0bb65 1422 dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
edd4c19a 1423 ? bitstream_read(bc, 5) : 32;
e6f0bb65
MP
1424 }
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])
1428 continue;
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;
1433 }
1434 }
1435}
1436
1437/**
1438 * Decode number of tones for each subband of a channel.
1439 *
edd4c19a 1440 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1447 */
edd4c19a 1448static int decode_band_numwavs(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1449 int ch_num, int band_has_tones[],
1450 AVCodecContext *avctx)
1451{
1452 int mode, sb, delta;
1453 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1454 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1455
edd4c19a 1456 mode = bitstream_read(bc, ch_num + 1);
e6f0bb65
MP
1457 switch (mode) {
1458 case 0: /** fixed-length coding */
1459 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1460 if (band_has_tones[sb])
edd4c19a 1461 dst[sb].num_wavs = bitstream_read(bc, 4);
e6f0bb65
MP
1462 break;
1463 case 1: /** variable-length coding */
1464 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1465 if (band_has_tones[sb])
1466 dst[sb].num_wavs =
edd4c19a
AH
1467 bitstream_read_vlc(bc, tone_vlc_tabs[1].table,
1468 tone_vlc_tabs[1].bits, 1);
e6f0bb65
MP
1469 break;
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]) {
edd4c19a
AH
1473 delta = bitstream_read_vlc(bc, tone_vlc_tabs[2].table,
1474 tone_vlc_tabs[2].bits, 1);
e6f0bb65
MP
1475 delta = sign_extend(delta, 3);
1476 dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1477 }
1478 break;
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;
1483 break;
1484 }
1485
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;
1495 }
1496 dst[sb].start_index = ctx->waves_info->tones_index;
1497 ctx->waves_info->tones_index += dst[sb].num_wavs;
1498 }
1499
1500 return 0;
1501}
1502
1503/**
1504 * Decode frequency information for each subband of a channel.
1505 *
edd4c19a 1506 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1511 */
edd4c19a 1512static void decode_tones_frequency(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1513 int ch_num, int band_has_tones[])
1514{
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;
1519
edd4c19a 1520 if (!ch_num || !bitstream_read_bit(bc)) { /* mode 0: fixed-length coding */
e6f0bb65
MP
1521 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1522 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1523 continue;
1524 iwav = &ctx->waves_info->waves[dst[sb].start_index];
edd4c19a 1525 direction = (dst[sb].num_wavs > 1) ? bitstream_read_bit(bc) : 0;
e6f0bb65
MP
1526 if (direction) { /** packed numbers in descending order */
1527 if (dst[sb].num_wavs)
edd4c19a 1528 iwav[dst[sb].num_wavs - 1].freq_index = bitstream_read(bc, 10);
e6f0bb65
MP
1529 for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1530 nbits = av_log2(iwav[i+1].freq_index) + 1;
edd4c19a 1531 iwav[i].freq_index = bitstream_read(bc, nbits);
e6f0bb65
MP
1532 }
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)
edd4c19a 1536 iwav[i].freq_index = bitstream_read(bc, 10);
e6f0bb65
MP
1537 else {
1538 nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
edd4c19a 1539 iwav[i].freq_index = bitstream_read(bc, nbits) +
e6f0bb65
MP
1540 1024 - (1 << nbits);
1541 }
1542 }
1543 }
1544 }
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)
1548 continue;
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++) {
edd4c19a
AH
1552 delta = bitstream_read_vlc(bc, tone_vlc_tabs[6].table,
1553 tone_vlc_tabs[6].bits, 1);
e6f0bb65
MP
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;
1558 }
1559 }
1560 }
1561}
1562
1563/**
1564 * Decode amplitude information for each subband of a channel.
1565 *
edd4c19a 1566 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1571 */
edd4c19a 1572static void decode_tones_amplitude(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1573 int ch_num, int band_has_tones[])
1574{
1575 int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1576 Atrac3pWaveParam *wsrc, *wref;
d16ec1b6 1577 int refwaves[48] = { 0 };
e6f0bb65
MP
1578 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1579 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1580
1581 if (ch_num) {
1582 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1583 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1584 continue;
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) {
1591 maxdiff = diff;
1592 fi = i;
1593 }
1594 }
1595
1596 if (maxdiff < 8)
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;
1600 else
1601 refwaves[dst[sb].start_index + j] = -1;
1602 }
1603 }
1604 }
1605
edd4c19a 1606 mode = bitstream_read(bc, ch_num + 1);
e6f0bb65
MP
1607
1608 switch (mode) {
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)
1612 continue;
1613 if (ctx->waves_info->amplitude_mode)
1614 for (i = 0; i < dst[sb].num_wavs; i++)
edd4c19a 1615 ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = bitstream_read(bc, 6);
e6f0bb65 1616 else
edd4c19a 1617 ctx->waves_info->waves[dst[sb].start_index].amp_sf = bitstream_read(bc, 6);
e6f0bb65
MP
1618 }
1619 break;
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)
1623 continue;
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 =
edd4c19a
AH
1627 bitstream_read_vlc(bc, tone_vlc_tabs[3].table,
1628 tone_vlc_tabs[3].bits, 1) + 20;
e6f0bb65
MP
1629 else
1630 ctx->waves_info->waves[dst[sb].start_index].amp_sf =
edd4c19a
AH
1631 bitstream_read_vlc(bc, tone_vlc_tabs[4].table,
1632 tone_vlc_tabs[4].bits, 1) + 24;
e6f0bb65
MP
1633 }
1634 break;
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)
1638 continue;
1639 for (i = 0; i < dst[sb].num_wavs; i++) {
edd4c19a
AH
1640 delta = bitstream_read_vlc(bc, tone_vlc_tabs[5].table,
1641 tone_vlc_tabs[5].bits, 1);
e6f0bb65
MP
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;
1646 }
1647 }
1648 break;
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])
1652 continue;
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
1657 : 32;
1658 }
1659 break;
1660 }
1661}
1662
1663/**
1664 * Decode phase information for each subband of a channel.
1665 *
edd4c19a 1666 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1671 */
edd4c19a 1672static void decode_tones_phase(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1673 int ch_num, int band_has_tones[])
1674{
1675 int sb, i;
1676 Atrac3pWaveParam *wparam;
1677 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1678
1679 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1680 if (!band_has_tones[sb])
1681 continue;
1682 wparam = &ctx->waves_info->waves[dst[sb].start_index];
1683 for (i = 0; i < dst[sb].num_wavs; i++)
edd4c19a 1684 wparam[i].phase_index = bitstream_read(bc, 5);
e6f0bb65
MP
1685 }
1686}
1687
1688/**
1689 * Decode tones info for all channels.
1690 *
edd4c19a 1691 * @param[in] bc the Bitstream context
e6f0bb65
MP
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
1696 */
edd4c19a 1697static int decode_tones_info(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1698 int num_channels, AVCodecContext *avctx)
1699{
1700 int ch_num, i, ret;
1701 int band_has_tones[16];
1702
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);
1706
edd4c19a 1707 ctx->waves_info->tones_present = bitstream_read_bit(bc);
e6f0bb65
MP
1708 if (!ctx->waves_info->tones_present)
1709 return 0;
1710
1711 memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1712
edd4c19a 1713 ctx->waves_info->amplitude_mode = bitstream_read_bit(bc);
e6f0bb65
MP
1714 if (!ctx->waves_info->amplitude_mode) {
1715 avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1716 return AVERROR_PATCHWELCOME;
1717 }
1718
1719 ctx->waves_info->num_tone_bands =
edd4c19a
AH
1720 bitstream_read_vlc(bc, tone_vlc_tabs[0].table,
1721 tone_vlc_tabs[0].bits, 1) + 1;
e6f0bb65
MP
1722
1723 if (num_channels == 2) {
edd4c19a
AH
1724 get_subband_flags(bc, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1725 get_subband_flags(bc, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1726 if (get_subband_flags(bc, ctx->waves_info->phase_shift,
e6f0bb65
MP
1727 ctx->waves_info->num_tone_bands)) {
1728 avpriv_report_missing_feature(avctx, "GHA Phase shifting");
1729 return AVERROR_PATCHWELCOME;
1730 }
1731 }
1732
1733 ctx->waves_info->tones_index = 0;
1734
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];
1738
edd4c19a
AH
1739 decode_tones_envelope(bc, ctx, ch_num, band_has_tones);
1740 if ((ret = decode_band_numwavs(bc, ctx, ch_num, band_has_tones,
e6f0bb65
MP
1741 avctx)) < 0)
1742 return ret;
1743
edd4c19a
AH
1744 decode_tones_frequency(bc, ctx, ch_num, band_has_tones);
1745 decode_tones_amplitude(bc, ctx, ch_num, band_has_tones);
1746 decode_tones_phase(bc, ctx, ch_num, band_has_tones);
e6f0bb65
MP
1747 }
1748
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];
1753
1754 if (ctx->waves_info->tone_master[i])
1755 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1756 ctx->channels[1].tones_info[i]);
1757 }
1758 }
1759
1760 return 0;
1761}
1762
edd4c19a 1763int ff_atrac3p_decode_channel_unit(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
e6f0bb65
MP
1764 int num_channels, AVCodecContext *avctx)
1765{
1766 int ret;
1767
1768 /* parse sound header */
edd4c19a 1769 ctx->num_quant_units = bitstream_read(bc, 5) + 1;
e6f0bb65
MP
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;
1775 }
1776
edd4c19a 1777 ctx->mute_flag = bitstream_read_bit(bc);
e6f0bb65
MP
1778
1779 /* decode various sound parameters */
edd4c19a 1780 if ((ret = decode_quant_wordlen(bc, ctx, num_channels, avctx)) < 0)
e6f0bb65
MP
1781 return ret;
1782
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
1786 : 0;
1787
edd4c19a 1788 if ((ret = decode_scale_factors(bc, ctx, num_channels, avctx)) < 0)
e6f0bb65
MP
1789 return ret;
1790
edd4c19a 1791 if ((ret = decode_code_table_indexes(bc, ctx, num_channels, avctx)) < 0)
e6f0bb65
MP
1792 return ret;
1793
edd4c19a 1794 decode_spectrum(bc, ctx, num_channels, avctx);
e6f0bb65
MP
1795
1796 if (num_channels == 2) {
edd4c19a
AH
1797 get_subband_flags(bc, ctx->swap_channels, ctx->num_coded_subbands);
1798 get_subband_flags(bc, ctx->negate_coeffs, ctx->num_coded_subbands);
e6f0bb65
MP
1799 }
1800
edd4c19a 1801 decode_window_shape(bc, ctx, num_channels);
e6f0bb65 1802
edd4c19a 1803 if ((ret = decode_gainc_data(bc, ctx, num_channels, avctx)) < 0)
e6f0bb65
MP
1804 return ret;
1805
edd4c19a 1806 if ((ret = decode_tones_info(bc, ctx, num_channels, avctx)) < 0)
e6f0bb65
MP
1807 return ret;
1808
1809 /* decode global noise info */
edd4c19a 1810 ctx->noise_present = bitstream_read_bit(bc);
e6f0bb65 1811 if (ctx->noise_present) {
edd4c19a
AH
1812 ctx->noise_level_index = bitstream_read(bc, 4);
1813 ctx->noise_table_index = bitstream_read(bc, 4);
e6f0bb65
MP
1814 }
1815
1816 return 0;
1817}