e3c2504178ac29312bec0f6dda75c764786ee1fe
[libav.git] / libavcodec / atrac3plus.c
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"
29
30 #include "avcodec.h"
31 #include "bitstream.h"
32 #include "atrac3plus.h"
33 #include "atrac3plus_data.h"
34
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];
42
43 /**
44 * Generate canonical VLC table from given descriptor.
45 *
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
50 */
51 static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
52 int *tab_offset, VLC *out_vlc)
53 {
54 int i, b;
55 uint16_t codes[256];
56 uint8_t bits[256];
57 unsigned code = 0;
58 int index = 0;
59 int min_len = *cb++; // get shortest codeword length
60 int max_len = *cb++; // get longest codeword length
61
62 for (b = min_len; b <= max_len; b++) {
63 for (i = *cb++; i > 0; i--) {
64 av_assert0(index < 256);
65 bits[index] = b;
66 codes[index] = code++;
67 index++;
68 }
69 code <<= 1;
70 }
71
72 out_vlc->table = &tables_data[*tab_offset];
73 out_vlc->table_allocated = 1 << max_len;
74
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);
77
78 *tab_offset += 1 << max_len;
79 }
80
81 av_cold void ff_atrac3p_init_vlcs(AVCodec *codec)
82 {
83 int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
84
85 static const int wl_nb_bits[4] = { 2, 3, 5, 5 };
86 static const int wl_nb_codes[4] = { 3, 5, 8, 8 };
87 static const uint8_t * const wl_bits[4] = {
88 atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
89 atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
90 };
91 static const uint8_t * const wl_codes[4] = {
92 atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
93 atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
94 };
95 static const uint8_t * const wl_xlats[4] = {
96 atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
97 };
98
99 static const int ct_nb_bits[4] = { 3, 4, 4, 4 };
100 static const int ct_nb_codes[4] = { 4, 8, 8, 8 };
101 static const uint8_t * const ct_bits[4] = {
102 atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
103 atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
104 };
105 static const uint8_t * const ct_codes[4] = {
106 atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
107 atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
108 };
109 static const uint8_t * const ct_xlats[4] = {
110 NULL, NULL, atrac3p_ct_huff_xlat1, NULL
111 };
112
113 static const int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
114 static const int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
115 static const uint8_t * const 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
119 };
120 static const uint16_t * const 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
124 };
125 static const uint8_t * const 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
128 };
129
130 static const uint8_t * const 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
137 };
138 static const uint8_t * const 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
144 };
145
146 static const uint8_t * const 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,
150 atrac3p_huff_freq_cb
151 };
152 static const uint8_t * const 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
156 };
157
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];
163
164 ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
165 wl_bits[i], 1, 1,
166 wl_codes[i], 1, 1,
167 wl_xlats[i], 1, 1,
168 INIT_VLC_USE_NEW_STATIC);
169
170 ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
171 ct_bits[i], 1, 1,
172 ct_codes[i], 1, 1,
173 ct_xlats[i], 1, 1,
174 INIT_VLC_USE_NEW_STATIC);
175
176 wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
177 ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
178 }
179
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];
183
184 ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
185 sf_bits[i], 1, 1,
186 sf_codes[i], 2, 2,
187 sf_xlats[i], 1, 1,
188 INIT_VLC_USE_NEW_STATIC);
189 sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
190 }
191
192 tab_offset = 2564;
193
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]);
200 else
201 spec_vlc_tabs[i].table = 0;
202 }
203
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]);
207
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]);
211 }
212
213 /**
214 * Decode number of coded quantization units.
215 *
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
221 */
222 static int num_coded_units(BitstreamContext *bc, Atrac3pChanParams *chan,
223 Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
224 {
225 chan->fill_mode = bitstream_read(bc, 2);
226 if (!chan->fill_mode) {
227 chan->num_coded_vals = ctx->num_quant_units;
228 } else {
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;
234 }
235
236 if (chan->fill_mode == 3)
237 chan->split_point = bitstream_read(bc, 2) + (chan->ch_num << 1) + 1;
238 }
239
240 return 0;
241 }
242
243 /**
244 * Add weighting coefficients to the decoded word-length information.
245 *
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
251 */
252 static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
253 Atrac3pChanParams *chan, int wtab_idx,
254 AVCodecContext *avctx)
255 {
256 int i;
257 const int8_t *weights_tab =
258 &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
259
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;
267 }
268 }
269
270 return 0;
271 }
272
273 /**
274 * Subtract weighting coefficients from decoded scalefactors.
275 *
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
281 */
282 static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
283 Atrac3pChanParams *chan, int wtab_idx,
284 AVCodecContext *avctx)
285 {
286 int i;
287 const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
288
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;
296 }
297 }
298
299 return 0;
300 }
301
302 /**
303 * Unpack vector quantization tables.
304 *
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
309 */
310 static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
311 int *dst, int num_values)
312 {
313 int i;
314
315 if (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];
319 }
320 }
321
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], \
325 (dst), (num_vals))
326
327 /**
328 * Decode word length for each quantization unit of a channel.
329 *
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
335 */
336 static int decode_channel_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
337 int ch_num, AVCodecContext *avctx)
338 {
339 int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
340 ret, start_val;
341 VLC *vlc_tab;
342 Atrac3pChanParams *chan = &ctx->channels[ch_num];
343 Atrac3pChanParams *ref_chan = &ctx->channels[0];
344
345 chan->fill_mode = 0;
346
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);
351 break;
352 case 1:
353 if (ch_num) {
354 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
355 return ret;
356
357 if (chan->num_coded_vals) {
358 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
359
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;
363 }
364 }
365 } else {
366 weight_idx = bitstream_read(bc, 2);
367 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
368 return ret;
369
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;
376 }
377
378 delta_bits = bitstream_read(bc, 2);
379 min_val = bitstream_read(bc, 3);
380
381 for (i = 0; i < pos; i++)
382 chan->qu_wordlen[i] = bitstream_read(bc, 3);
383
384 for (i = pos; i < chan->num_coded_vals; i++)
385 chan->qu_wordlen[i] = (min_val + bitstream_read(bc, delta_bits)) & 7;
386 }
387 }
388 break;
389 case 2:
390 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
391 return ret;
392
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;
397
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;
402 }
403 } else if (chan->num_coded_vals) {
404 flag = bitstream_read(bc, 1);
405 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 1)];
406
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);
411
412 if (!flag) {
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;
416 }
417 } else {
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;
426 }
427
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;
432 }
433 }
434 break;
435 case 3:
436 weight_idx = bitstream_read(bc, 2);
437 if ((ret = num_coded_units(bc, chan, ctx, avctx)) < 0)
438 return ret;
439
440 if (chan->num_coded_vals) {
441 vlc_tab = &wl_vlc_tabs[bitstream_read(bc, 2)];
442
443 /* first coefficient is coded directly */
444 chan->qu_wordlen[0] = bitstream_read(bc, 3);
445
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;
449 }
450 }
451 break;
452 }
453
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;
462 }
463
464 if (weight_idx)
465 return add_wordlen_weights(ctx, chan, weight_idx, avctx);
466
467 return 0;
468 }
469
470 /**
471 * Decode scale factor indexes for each quant unit of a channel.
472 *
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
478 */
479 static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
480 int ch_num, AVCodecContext *avctx)
481 {
482 int i, weight_idx = 0, delta, diff, num_long_vals,
483 delta_bits, min_val, vlc_sel, start_val;
484 VLC *vlc_tab;
485 Atrac3pChanParams *chan = &ctx->channels[ch_num];
486 Atrac3pChanParams *ref_chan = &ctx->channels[0];
487
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);
492 break;
493 case 1:
494 if (ch_num) {
495 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
496
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;
500 }
501 } else {
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);
505
506 num_long_vals = bitstream_read(bc, 5);
507 delta_bits = bitstream_read(bc, 2);
508 min_val = bitstream_read(bc, 4) - 7;
509
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;
513
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;
518 } else {
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;
526 }
527
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);
531
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;
536 }
537 }
538 break;
539 case 2:
540 if (ch_num) {
541 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];
542
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;
545
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;
550 }
551 } else {
552 vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];
553
554 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
555
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;
560 }
561 }
562 break;
563 case 3:
564 if (ch_num) {
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];
568 } else {
569 weight_idx = bitstream_read(bc, 2);
570 vlc_sel = bitstream_read(bc, 2);
571 vlc_tab = &sf_vlc_tabs[vlc_sel];
572
573 if (weight_idx == 3) {
574 vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
575
576 UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);
577
578 diff = (bitstream_read(bc, 4) + 56) & 0x3F;
579 chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
580
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;
585 }
586 } else {
587 /* 1st coefficient is coded directly */
588 chan->qu_sf_idx[0] = bitstream_read(bc, 6);
589
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;
593 }
594 }
595 }
596 break;
597 }
598
599 if (weight_idx && weight_idx < 3)
600 return subtract_sf_weights(ctx, chan, weight_idx, avctx);
601
602 return 0;
603 }
604
605 /**
606 * Decode word length information for each channel.
607 *
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
613 */
614 static int decode_quant_wordlen(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
615 int num_channels, AVCodecContext *avctx)
616 {
617 int ch_num, i, ret;
618
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));
622
623 if ((ret = decode_channel_wordlen(bc, ctx, ch_num, avctx)) < 0)
624 return ret;
625 }
626
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]))
632 break;
633 ctx->used_quant_units = i + 1;
634
635 return 0;
636 }
637
638 /**
639 * Decode scale factor indexes for each channel.
640 *
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
646 */
647 static int decode_scale_factors(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
648 int num_channels, AVCodecContext *avctx)
649 {
650 int ch_num, ret;
651
652 if (!ctx->used_quant_units)
653 return 0;
654
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));
658
659 if ((ret = decode_channel_sf_idx(bc, ctx, ch_num, avctx)) < 0)
660 return ret;
661 }
662
663 return 0;
664 }
665
666 /**
667 * Decode number of code table values.
668 *
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
673 */
674 static int get_num_ct_values(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
675 AVCodecContext *avctx)
676 {
677 int num_coded_vals;
678
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;
685 }
686 return num_coded_vals;
687 } else
688 return ctx->used_quant_units;
689 }
690
691 #define DEC_CT_IDX_COMMON(OP) \
692 num_vals = get_num_ct_values(bc, ctx, avctx); \
693 if (num_vals < 0) \
694 return num_vals; \
695 \
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); \
702 }
703
704 #define CODING_DIRECT bitstream_read(bc, num_bits)
705
706 #define CODING_VLC bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1)
707
708 #define CODING_VLC_DELTA \
709 (!i) ? CODING_VLC \
710 : (pred + bitstream_read_vlc(bc, delta_vlc->table, \
711 delta_vlc->bits, 1)) & mask; \
712 pred = chan->qu_tab_idx[i]
713
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
717
718 /**
719 * Decode code table indexes for each quant unit of a channel.
720 *
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
726 */
727 static int decode_channel_code_tab(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
728 int ch_num, AVCodecContext *avctx)
729 {
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];
735
736 chan->table_type = bitstream_read_bit(bc);
737
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);
742 break;
743 case 1: /* entropy-coded */
744 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
745 : ct_vlc_tabs;
746 DEC_CT_IDX_COMMON(CODING_VLC);
747 break;
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];
752 } else {
753 vlc_tab = ct_vlc_tabs;
754 delta_vlc = ct_vlc_tabs;
755 }
756 pred = 0;
757 DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
758 break;
759 case 3: /* entropy-coded difference to master */
760 if (ch_num) {
761 vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
762 : ct_vlc_tabs;
763 DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
764 }
765 break;
766 }
767
768 return 0;
769 }
770
771 /**
772 * Decode code table indexes for each channel.
773 *
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
779 */
780 static int decode_code_table_indexes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
781 int num_channels, AVCodecContext *avctx)
782 {
783 int ch_num, ret;
784
785 if (!ctx->used_quant_units)
786 return 0;
787
788 ctx->use_full_table = bitstream_read_bit(bc);
789
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));
793
794 if ((ret = decode_channel_code_tab(bc, ctx, ch_num, avctx)) < 0)
795 return ret;
796 }
797
798 return 0;
799 }
800
801 /**
802 * Decode huffman-coded spectral lines for a given quant unit.
803 *
804 * This is a generalized version for all known coding modes.
805 * Its speed can be improved by creating separate functions for each mode.
806 *
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
812 */
813 static void decode_qu_spectra(BitstreamContext *bc, const Atrac3pSpecCodeTab *tab,
814 VLC *vlc_tab, int16_t *out, const int num_specs)
815 {
816 int i, j, pos, cf;
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;
822
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);
827
828 for (i = 0; i < num_coeffs; i++) {
829 cf = val & mask;
830 if (is_signed)
831 cf = sign_extend(cf, bits);
832 else if (cf && bitstream_read_bit(bc))
833 cf = -cf;
834
835 out[pos++] = cf;
836 val >>= bits;
837 }
838 }
839 } else /* group skipped */
840 pos += group_size * num_coeffs;
841 }
842 }
843
844 /**
845 * Decode huffman-coded IMDCT spectrum for all channels.
846 *
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
851 */
852 static void decode_spectrum(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
853 int num_channels, AVCodecContext *avctx)
854 {
855 int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
856 const Atrac3pSpecCodeTab *tab;
857 Atrac3pChanParams *chan;
858
859 for (ch_num = 0; ch_num < num_channels; ch_num++) {
860 chan = &ctx->channels[ch_num];
861
862 memset(chan->spectrum, 0, sizeof(chan->spectrum));
863
864 /* set power compensation level to disabled */
865 memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
866
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];
870
871 wordlen = chan->qu_wordlen[qu];
872 codetab = chan->qu_tab_idx[qu];
873 if (wordlen) {
874 if (!ctx->use_full_table)
875 codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
876
877 tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
878 tab = &atrac3p_spectra_tabs[tab_index];
879
880 /* this allows reusing VLC tables */
881 if (tab->redirect >= 0)
882 tab_index = tab->redirect;
883
884 decode_qu_spectra(bc, tab, &spec_vlc_tabs[tab_index],
885 &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
886 num_specs);
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]],
891 num_specs *
892 sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
893 chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
894 }
895 }
896
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);
905 }
906 }
907 }
908
909 /**
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.
916 *
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
921 */
922 static int get_subband_flags(BitstreamContext *bc, uint8_t *out, int num_flags)
923 {
924 int i, result;
925
926 memset(out, 0, num_flags);
927
928 result = bitstream_read_bit(bc);
929 if (result) {
930 if (bitstream_read_bit(bc))
931 for (i = 0; i < num_flags; i++)
932 out[i] = bitstream_read_bit(bc);
933 else
934 memset(out, 1, num_flags);
935 }
936
937 return result;
938 }
939
940 /**
941 * Decode mdct window shape flags for all channels.
942 *
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
946 */
947 static void decode_window_shape(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
948 int num_channels)
949 {
950 int ch_num;
951
952 for (ch_num = 0; ch_num < num_channels; ch_num++)
953 get_subband_flags(bc, ctx->channels[ch_num].wnd_shape,
954 ctx->num_subbands);
955 }
956
957 /**
958 * Decode number of gain control points.
959 *
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
965 */
966 static int decode_gainc_npoints(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
967 int ch_num, int coded_subbands)
968 {
969 int i, delta, delta_bits, min_val;
970 Atrac3pChanParams *chan = &ctx->channels[ch_num];
971 Atrac3pChanParams *ref_chan = &ctx->channels[0];
972
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);
977 break;
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);
983 break;
984 case 2:
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;
991 }
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);
996
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;
1002 }
1003 }
1004 break;
1005 case 3:
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);
1013
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;
1018 }
1019 }
1020 }
1021
1022 return 0;
1023 }
1024
1025 /**
1026 * Implements coding mode 3 (slave) for gain compensation levels.
1027 *
1028 * @param[out] dst ptr to the output array
1029 * @param[in] ref ptr to the reference channel
1030 */
1031 static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1032 {
1033 int i;
1034
1035 for (i = 0; i < dst->num_points; i++)
1036 dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1037 }
1038
1039 /**
1040 * Implements coding mode 1 (master) for gain compensation levels.
1041 *
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
1045 */
1046 static inline void gainc_level_mode1m(BitstreamContext *bc,
1047 Atrac3pChanUnitCtx *ctx,
1048 AtracGainInfo *dst)
1049 {
1050 int i, delta;
1051
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);
1055
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;
1060 }
1061 }
1062
1063 /**
1064 * Decode level code for each gain control point.
1065 *
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
1071 */
1072 static int decode_gainc_levels(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1073 int ch_num, int coded_subbands)
1074 {
1075 int sb, i, delta, delta_bits, min_val, pred;
1076 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1077 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1078
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);
1084 break;
1085 case 1:
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;
1094 }
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]);
1098 }
1099 break;
1100 case 2:
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]);
1106 else
1107 gainc_level_mode3s(&chan->gain_data[sb],
1108 &ref_chan->gain_data[sb]);
1109 }
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]);
1113
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;
1121 }
1122 }
1123 break;
1124 case 3:
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);
1132
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;
1138 }
1139 }
1140 break;
1141 }
1142
1143 return 0;
1144 }
1145
1146 /**
1147 * Implements coding mode 0 for gain compensation locations.
1148 *
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
1153 */
1154 static inline void gainc_loc_mode0(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1155 AtracGainInfo *dst, int pos)
1156 {
1157 int delta_bits;
1158
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;
1163 else {
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;
1167 }
1168 }
1169
1170 /**
1171 * Implements coding mode 1 for gain compensation locations.
1172 *
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
1176 */
1177 static inline void gainc_loc_mode1(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1178 AtracGainInfo *dst)
1179 {
1180 int i;
1181 VLC *tab;
1182
1183 if (dst->num_points > 0) {
1184 /* 1st coefficient is stored directly */
1185 dst->loc_code[0] = bitstream_read(bc, 5);
1186
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])
1191 ? &gain_vlc_tabs[7]
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);
1195 }
1196 }
1197 }
1198
1199 /**
1200 * Decode location code for each gain control point.
1201 *
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
1208 */
1209 static int decode_gainc_loc_codes(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1210 int ch_num, int coded_subbands,
1211 AVCodecContext *avctx)
1212 {
1213 int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1214 AtracGainInfo *dst, *ref;
1215 VLC *tab;
1216 Atrac3pChanParams *chan = &ctx->channels[ch_num];
1217 Atrac3pChanParams *ref_chan = &ctx->channels[0];
1218
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);
1224 break;
1225 case 1:
1226 if (ch_num) {
1227 for (sb = 0; sb < coded_subbands; sb++) {
1228 if (chan->gain_data[sb].num_points <= 0)
1229 continue;
1230 dst = &chan->gain_data[sb];
1231 ref = &ref_chan->gain_data[sb];
1232
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;
1238
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) {
1244 delta =
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;
1248 } else {
1249 if (bitstream_read_bit(bc))
1250 gainc_loc_mode0(bc, ctx, dst, i); // direct coding
1251 else
1252 dst->loc_code[i] = ref->loc_code[i]; // clone master
1253 }
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);
1258 if (more_than_ref)
1259 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1260 else
1261 dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1262 }
1263 }
1264 }
1265 } else /* VLC delta to previous */
1266 for (sb = 0; sb < coded_subbands; sb++)
1267 gainc_loc_mode1(bc, ctx, &chan->gain_data[sb]);
1268 break;
1269 case 2:
1270 if (ch_num) {
1271 for (sb = 0; sb < coded_subbands; sb++) {
1272 if (chan->gain_data[sb].num_points <= 0)
1273 continue;
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];
1281 }
1282 } else {
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);
1286
1287 for (sb = 1; sb < coded_subbands; sb++) {
1288 if (chan->gain_data[sb].num_points <= 0)
1289 continue;
1290 dst = &chan->gain_data[sb];
1291
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;
1299
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);
1307 if (more_than_ref)
1308 dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1309 else
1310 dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1311 }
1312 }
1313 }
1314 break;
1315 case 3:
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);
1321 else
1322 chan->gain_data[sb].loc_code[i] =
1323 ref_chan->gain_data[sb].loc_code[i];
1324 }
1325 } else { /* shorter delta to min */
1326 delta_bits = bitstream_read(bc, 2) + 1;
1327 min_val = bitstream_read(bc, 5);
1328
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);
1333 }
1334 break;
1335 }
1336
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;
1347 }
1348 }
1349 }
1350
1351 return 0;
1352 }
1353
1354 /**
1355 * Decode gain control data for all channels.
1356 *
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
1362 */
1363 static int decode_gainc_data(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1364 int num_channels, AVCodecContext *avctx)
1365 {
1366 int ch_num, coded_subbands, sb, ret;
1367
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);
1371
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;
1376 else
1377 ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1378
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)
1382 return ret;
1383
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];
1388 }
1389 } else {
1390 ctx->channels[ch_num].num_gain_subbands = 0;
1391 }
1392 }
1393
1394 return 0;
1395 }
1396
1397 /**
1398 * Decode envelope for all tones of a channel.
1399 *
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
1405 */
1406 static void decode_tones_envelope(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1407 int ch_num, int band_has_tones[])
1408 {
1409 int sb;
1410 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1411 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1412
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])
1416 continue;
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;
1423 }
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])
1427 continue;
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;
1432 }
1433 }
1434 }
1435
1436 /**
1437 * Decode number of tones for each subband of a channel.
1438 *
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
1446 */
1447 static int decode_band_numwavs(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1448 int ch_num, int band_has_tones[],
1449 AVCodecContext *avctx)
1450 {
1451 int mode, sb, delta;
1452 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1453 Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1454
1455 mode = bitstream_read(bc, ch_num + 1);
1456 switch (mode) {
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);
1461 break;
1462 case 1: /** variable-length coding */
1463 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1464 if (band_has_tones[sb])
1465 dst[sb].num_wavs =
1466 bitstream_read_vlc(bc, tone_vlc_tabs[1].table,
1467 tone_vlc_tabs[1].bits, 1);
1468 break;
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;
1476 }
1477 break;
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;
1482 break;
1483 }
1484
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;
1494 }
1495 dst[sb].start_index = ctx->waves_info->tones_index;
1496 ctx->waves_info->tones_index += dst[sb].num_wavs;
1497 }
1498
1499 return 0;
1500 }
1501
1502 /**
1503 * Decode frequency information for each subband of a channel.
1504 *
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
1510 */
1511 static void decode_tones_frequency(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1512 int ch_num, int band_has_tones[])
1513 {
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;
1518
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)
1522 continue;
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);
1531 }
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);
1536 else {
1537 nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1538 iwav[i].freq_index = bitstream_read(bc, nbits) +
1539 1024 - (1 << nbits);
1540 }
1541 }
1542 }
1543 }
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)
1547 continue;
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;
1557 }
1558 }
1559 }
1560 }
1561
1562 /**
1563 * Decode amplitude information for each subband of a channel.
1564 *
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
1570 */
1571 static void decode_tones_amplitude(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1572 int ch_num, int band_has_tones[])
1573 {
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;
1579
1580 if (ch_num) {
1581 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1582 if (!band_has_tones[sb] || !dst[sb].num_wavs)
1583 continue;
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) {
1590 maxdiff = diff;
1591 fi = i;
1592 }
1593 }
1594
1595 if (maxdiff < 8)
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;
1599 else
1600 refwaves[dst[sb].start_index + j] = -1;
1601 }
1602 }
1603 }
1604
1605 mode = bitstream_read(bc, ch_num + 1);
1606
1607 switch (mode) {
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)
1611 continue;
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);
1615 else
1616 ctx->waves_info->waves[dst[sb].start_index].amp_sf = bitstream_read(bc, 6);
1617 }
1618 break;
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)
1622 continue;
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;
1628 else
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;
1632 }
1633 break;
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)
1637 continue;
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;
1645 }
1646 }
1647 break;
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])
1651 continue;
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
1656 : 32;
1657 }
1658 break;
1659 }
1660 }
1661
1662 /**
1663 * Decode phase information for each subband of a channel.
1664 *
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
1670 */
1671 static void decode_tones_phase(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1672 int ch_num, int band_has_tones[])
1673 {
1674 int sb, i;
1675 Atrac3pWaveParam *wparam;
1676 Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1677
1678 for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1679 if (!band_has_tones[sb])
1680 continue;
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);
1684 }
1685 }
1686
1687 /**
1688 * Decode tones info for all channels.
1689 *
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
1695 */
1696 static int decode_tones_info(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1697 int num_channels, AVCodecContext *avctx)
1698 {
1699 int ch_num, i, ret;
1700 int band_has_tones[16];
1701
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);
1705
1706 ctx->waves_info->tones_present = bitstream_read_bit(bc);
1707 if (!ctx->waves_info->tones_present)
1708 return 0;
1709
1710 memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1711
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;
1716 }
1717
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;
1721
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;
1729 }
1730 }
1731
1732 ctx->waves_info->tones_index = 0;
1733
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];
1737
1738 decode_tones_envelope(bc, ctx, ch_num, band_has_tones);
1739 if ((ret = decode_band_numwavs(bc, ctx, ch_num, band_has_tones,
1740 avctx)) < 0)
1741 return ret;
1742
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);
1746 }
1747
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];
1752
1753 if (ctx->waves_info->tone_master[i])
1754 FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1755 ctx->channels[1].tones_info[i]);
1756 }
1757 }
1758
1759 return 0;
1760 }
1761
1762 int ff_atrac3p_decode_channel_unit(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,
1763 int num_channels, AVCodecContext *avctx)
1764 {
1765 int ret;
1766
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;
1774 }
1775
1776 ctx->mute_flag = bitstream_read_bit(bc);
1777
1778 /* decode various sound parameters */
1779 if ((ret = decode_quant_wordlen(bc, ctx, num_channels, avctx)) < 0)
1780 return ret;
1781
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
1785 : 0;
1786
1787 if ((ret = decode_scale_factors(bc, ctx, num_channels, avctx)) < 0)
1788 return ret;
1789
1790 if ((ret = decode_code_table_indexes(bc, ctx, num_channels, avctx)) < 0)
1791 return ret;
1792
1793 decode_spectrum(bc, ctx, num_channels, avctx);
1794
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);
1798 }
1799
1800 decode_window_shape(bc, ctx, num_channels);
1801
1802 if ((ret = decode_gainc_data(bc, ctx, num_channels, avctx)) < 0)
1803 return ret;
1804
1805 if ((ret = decode_tones_info(bc, ctx, num_channels, avctx)) < 0)
1806 return ret;
1807
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);
1813 }
1814
1815 return 0;
1816 }