CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/at3_standalone/atrac3plus.cpp
Views: 1401
1
/*
2
* ATRAC3+ compatible decoder
3
*
4
* Copyright (c) 2010-2013 Maxim Poliakovski
5
*
6
* This file is part of FFmpeg.
7
*
8
* FFmpeg is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
12
*
13
* FFmpeg is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
17
*
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with FFmpeg; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
*/
22
23
/**
24
* @file
25
* Bitstream parser for ATRAC3+ decoder.
26
*/
27
28
#include <string.h>
29
#include <stdlib.h>
30
31
#include "get_bits.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
static const uint8_t ff_logg2_tab[256] = {
44
0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
45
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
46
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
47
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
48
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
50
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
51
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
52
};
53
54
// todo: Replace with clz type instructions.
55
int av_log2(unsigned int v)
56
{
57
int n = 0;
58
if (v & 0xffff0000) {
59
v >>= 16;
60
n += 16;
61
}
62
if (v & 0xff00) {
63
v >>= 8;
64
n += 8;
65
}
66
n += ff_logg2_tab[v];
67
68
return n;
69
}
70
71
/**
72
* Generate canonical VLC table from given descriptor.
73
*
74
* @param[in] cb ptr to codebook descriptor
75
* @param[in] xlat ptr to translation table or NULL
76
* @param[in,out] tab_offset starting offset to the generated vlc table
77
* @param[out] out_vlc ptr to vlc table to be generated
78
*/
79
static void build_canonical_huff(const uint8_t *cb, const uint8_t *xlat,
80
int *tab_offset, VLC *out_vlc)
81
{
82
int i, b;
83
uint16_t codes[256];
84
uint8_t bits[256];
85
unsigned code = 0;
86
int index = 0;
87
int min_len = *cb++; // get shortest codeword length
88
int max_len = *cb++; // get longest codeword length
89
90
for (b = min_len; b <= max_len; b++) {
91
for (i = *cb++; i > 0; i--) {
92
av_assert0(index < 256);
93
bits[index] = b;
94
codes[index] = code++;
95
index++;
96
}
97
code <<= 1;
98
}
99
100
out_vlc->table = &tables_data[*tab_offset];
101
out_vlc->table_allocated = 1 << max_len;
102
103
ff_init_vlc_sparse(out_vlc, max_len, index, bits, 1, 1, codes, 2, 2,
104
xlat, 1, 1, INIT_VLC_USE_NEW_STATIC);
105
106
*tab_offset += 1 << max_len;
107
}
108
109
void ff_atrac3p_init_vlcs(void)
110
{
111
int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
112
113
static const int wl_nb_bits[4] = { 2, 3, 5, 5 };
114
static const int wl_nb_codes[4] = { 3, 5, 8, 8 };
115
static const uint8_t * const wl_bits[4] = {
116
atrac3p_wl_huff_bits1, atrac3p_wl_huff_bits2,
117
atrac3p_wl_huff_bits3, atrac3p_wl_huff_bits4
118
};
119
static const uint8_t * const wl_codes[4] = {
120
atrac3p_wl_huff_code1, atrac3p_wl_huff_code2,
121
atrac3p_wl_huff_code3, atrac3p_wl_huff_code4
122
};
123
static const uint8_t * const wl_xlats[4] = {
124
atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
125
};
126
127
static const int ct_nb_bits[4] = { 3, 4, 4, 4 };
128
static const int ct_nb_codes[4] = { 4, 8, 8, 8 };
129
static const uint8_t * const ct_bits[4] = {
130
atrac3p_ct_huff_bits1, atrac3p_ct_huff_bits2,
131
atrac3p_ct_huff_bits2, atrac3p_ct_huff_bits3
132
};
133
static const uint8_t * const ct_codes[4] = {
134
atrac3p_ct_huff_code1, atrac3p_ct_huff_code2,
135
atrac3p_ct_huff_code2, atrac3p_ct_huff_code3
136
};
137
static const uint8_t * const ct_xlats[4] = {
138
NULL, NULL, atrac3p_ct_huff_xlat1, NULL
139
};
140
141
static const int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
142
static const int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
143
static const uint8_t * const sf_bits[8] = {
144
atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits1, atrac3p_sf_huff_bits2,
145
atrac3p_sf_huff_bits3, atrac3p_sf_huff_bits4, atrac3p_sf_huff_bits4,
146
atrac3p_sf_huff_bits5, atrac3p_sf_huff_bits6
147
};
148
static const uint16_t * const sf_codes[8] = {
149
atrac3p_sf_huff_code1, atrac3p_sf_huff_code1, atrac3p_sf_huff_code2,
150
atrac3p_sf_huff_code3, atrac3p_sf_huff_code4, atrac3p_sf_huff_code4,
151
atrac3p_sf_huff_code5, atrac3p_sf_huff_code6
152
};
153
static const uint8_t * const sf_xlats[8] = {
154
atrac3p_sf_huff_xlat1, atrac3p_sf_huff_xlat2, NULL, NULL,
155
atrac3p_sf_huff_xlat4, atrac3p_sf_huff_xlat5, NULL, NULL
156
};
157
158
static const uint8_t * const gain_cbs[11] = {
159
atrac3p_huff_gain_npoints1_cb, atrac3p_huff_gain_npoints1_cb,
160
atrac3p_huff_gain_lev1_cb, atrac3p_huff_gain_lev2_cb,
161
atrac3p_huff_gain_lev3_cb, atrac3p_huff_gain_lev4_cb,
162
atrac3p_huff_gain_loc3_cb, atrac3p_huff_gain_loc1_cb,
163
atrac3p_huff_gain_loc4_cb, atrac3p_huff_gain_loc2_cb,
164
atrac3p_huff_gain_loc5_cb
165
};
166
static const uint8_t * const gain_xlats[11] = {
167
NULL, atrac3p_huff_gain_npoints2_xlat, atrac3p_huff_gain_lev1_xlat,
168
atrac3p_huff_gain_lev2_xlat, atrac3p_huff_gain_lev3_xlat,
169
atrac3p_huff_gain_lev4_xlat, atrac3p_huff_gain_loc3_xlat,
170
atrac3p_huff_gain_loc1_xlat, atrac3p_huff_gain_loc4_xlat,
171
atrac3p_huff_gain_loc2_xlat, atrac3p_huff_gain_loc5_xlat
172
};
173
174
static const uint8_t * const tone_cbs[7] = {
175
atrac3p_huff_tonebands_cb, atrac3p_huff_numwavs1_cb,
176
atrac3p_huff_numwavs2_cb, atrac3p_huff_wav_ampsf1_cb,
177
atrac3p_huff_wav_ampsf2_cb, atrac3p_huff_wav_ampsf3_cb,
178
atrac3p_huff_freq_cb
179
};
180
static const uint8_t * const tone_xlats[7] = {
181
NULL, NULL, atrac3p_huff_numwavs2_xlat, atrac3p_huff_wav_ampsf1_xlat,
182
atrac3p_huff_wav_ampsf2_xlat, atrac3p_huff_wav_ampsf3_xlat,
183
atrac3p_huff_freq_xlat
184
};
185
186
for (i = 0, wl_vlc_offs = 0, ct_vlc_offs = 2508; i < 4; i++) {
187
wl_vlc_tabs[i].table = &tables_data[wl_vlc_offs];
188
wl_vlc_tabs[i].table_allocated = 1 << wl_nb_bits[i];
189
ct_vlc_tabs[i].table = &tables_data[ct_vlc_offs];
190
ct_vlc_tabs[i].table_allocated = 1 << ct_nb_bits[i];
191
192
ff_init_vlc_sparse(&wl_vlc_tabs[i], wl_nb_bits[i], wl_nb_codes[i],
193
wl_bits[i], 1, 1,
194
wl_codes[i], 1, 1,
195
wl_xlats[i], 1, 1,
196
INIT_VLC_USE_NEW_STATIC);
197
198
ff_init_vlc_sparse(&ct_vlc_tabs[i], ct_nb_bits[i], ct_nb_codes[i],
199
ct_bits[i], 1, 1,
200
ct_codes[i], 1, 1,
201
ct_xlats[i], 1, 1,
202
INIT_VLC_USE_NEW_STATIC);
203
204
wl_vlc_offs += wl_vlc_tabs[i].table_allocated;
205
ct_vlc_offs += ct_vlc_tabs[i].table_allocated;
206
}
207
208
for (i = 0, sf_vlc_offs = 76; i < 8; i++) {
209
sf_vlc_tabs[i].table = &tables_data[sf_vlc_offs];
210
sf_vlc_tabs[i].table_allocated = 1 << sf_nb_bits[i];
211
212
ff_init_vlc_sparse(&sf_vlc_tabs[i], sf_nb_bits[i], sf_nb_codes[i],
213
sf_bits[i], 1, 1,
214
sf_codes[i], 2, 2,
215
sf_xlats[i], 1, 1,
216
INIT_VLC_USE_NEW_STATIC);
217
sf_vlc_offs += sf_vlc_tabs[i].table_allocated;
218
}
219
220
tab_offset = 2564;
221
222
/* build huffman tables for spectrum decoding */
223
for (i = 0; i < 112; i++) {
224
if (atrac3p_spectra_tabs[i].cb)
225
build_canonical_huff(atrac3p_spectra_tabs[i].cb,
226
atrac3p_spectra_tabs[i].xlat,
227
&tab_offset, &spec_vlc_tabs[i]);
228
else
229
spec_vlc_tabs[i].table = 0;
230
}
231
232
/* build huffman tables for gain data decoding */
233
for (i = 0; i < 11; i++)
234
build_canonical_huff(gain_cbs[i], gain_xlats[i], &tab_offset, &gain_vlc_tabs[i]);
235
236
/* build huffman tables for tone decoding */
237
for (i = 0; i < 7; i++)
238
build_canonical_huff(tone_cbs[i], tone_xlats[i], &tab_offset, &tone_vlc_tabs[i]);
239
}
240
241
/**
242
* Decode number of coded quantization units.
243
*
244
* @param[in] gb the GetBit context
245
* @param[in,out] chan ptr to the channel parameters
246
* @param[in,out] ctx ptr to the channel unit context
247
* @return result code: 0 = OK, otherwise - error code
248
*/
249
static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,
250
Atrac3pChanUnitCtx *ctx)
251
{
252
chan->fill_mode = get_bits(gb, 2);
253
if (!chan->fill_mode) {
254
chan->num_coded_vals = ctx->num_quant_units;
255
} else {
256
chan->num_coded_vals = get_bits(gb, 5);
257
if (chan->num_coded_vals > ctx->num_quant_units) {
258
av_log(AV_LOG_ERROR,
259
"Invalid number of transmitted units!\n");
260
return AVERROR_INVALIDDATA;
261
}
262
263
if (chan->fill_mode == 3)
264
chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
265
}
266
267
return 0;
268
}
269
270
/**
271
* Add weighting coefficients to the decoded word-length information.
272
*
273
* @param[in,out] ctx ptr to the channel unit context
274
* @param[in,out] chan ptr to the channel parameters
275
* @param[in] wtab_idx index of the table of weights
276
* @return result code: 0 = OK, otherwise - error code
277
*/
278
static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
279
Atrac3pChanParams *chan, int wtab_idx)
280
{
281
int i;
282
const int8_t *weights_tab =
283
&atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
284
285
for (i = 0; i < ctx->num_quant_units; i++) {
286
chan->qu_wordlen[i] += weights_tab[i];
287
if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
288
av_log(AV_LOG_ERROR,
289
"WL index out of range: pos=%d, val=%d!\n",
290
i, chan->qu_wordlen[i]);
291
return AVERROR_INVALIDDATA;
292
}
293
}
294
295
return 0;
296
}
297
298
/**
299
* Subtract weighting coefficients from decoded scalefactors.
300
*
301
* @param[in,out] ctx ptr to the channel unit context
302
* @param[in,out] chan ptr to the channel parameters
303
* @param[in] wtab_idx index of table of weights
304
* @return result code: 0 = OK, otherwise - error code
305
*/
306
static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
307
Atrac3pChanParams *chan, int wtab_idx)
308
{
309
int i;
310
const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
311
312
for (i = 0; i < ctx->used_quant_units; i++) {
313
chan->qu_sf_idx[i] -= weights_tab[i];
314
if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
315
av_log(AV_LOG_ERROR,
316
"SF index out of range: pos=%d, val=%d!\n",
317
i, chan->qu_sf_idx[i]);
318
return AVERROR_INVALIDDATA;
319
}
320
}
321
322
return 0;
323
}
324
325
/**
326
* Unpack vector quantization tables.
327
*
328
* @param[in] start_val start value for the unpacked table
329
* @param[in] shape_vec ptr to table to unpack
330
* @param[out] dst ptr to output array
331
* @param[in] num_values number of values to unpack
332
*/
333
static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
334
int *dst, int num_values)
335
{
336
int i;
337
338
if (num_values) {
339
dst[0] = dst[1] = dst[2] = start_val;
340
for (i = 3; i < num_values; i++)
341
dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
342
}
343
}
344
345
#define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \
346
start_val = get_bits((gb), 6); \
347
unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
348
(dst), (num_vals))
349
350
/**
351
* Decode word length for each quantization unit of a channel.
352
*
353
* @param[in] gb the GetBit context
354
* @param[in,out] ctx ptr to the channel unit context
355
* @param[in] ch_num channel to process
356
* @return result code: 0 = OK, otherwise - error code
357
*/
358
static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num)
359
{
360
int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
361
ret, start_val;
362
VLC *vlc_tab;
363
Atrac3pChanParams *chan = &ctx->channels[ch_num];
364
Atrac3pChanParams *ref_chan = &ctx->channels[0];
365
366
chan->fill_mode = 0;
367
368
switch (get_bits(gb, 2)) { /* switch according to coding mode */
369
case 0: /* coded using constant number of bits */
370
for (i = 0; i < ctx->num_quant_units; i++)
371
chan->qu_wordlen[i] = get_bits(gb, 3);
372
break;
373
case 1:
374
if (ch_num) {
375
if ((ret = num_coded_units(gb, chan, ctx)) < 0)
376
return ret;
377
378
if (chan->num_coded_vals) {
379
vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
380
381
for (i = 0; i < chan->num_coded_vals; i++) {
382
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
383
chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
384
}
385
}
386
} else {
387
weight_idx = get_bits(gb, 2);
388
if ((ret = num_coded_units(gb, chan, ctx)) < 0)
389
return ret;
390
391
if (chan->num_coded_vals) {
392
pos = get_bits(gb, 5);
393
if (pos > chan->num_coded_vals) {
394
av_log(AV_LOG_ERROR,
395
"WL mode 1: invalid position!\n");
396
return AVERROR_INVALIDDATA;
397
}
398
399
delta_bits = get_bits(gb, 2);
400
min_val = get_bits(gb, 3);
401
402
for (i = 0; i < pos; i++)
403
chan->qu_wordlen[i] = get_bits(gb, 3);
404
405
for (i = pos; i < chan->num_coded_vals; i++)
406
chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;
407
}
408
}
409
break;
410
case 2:
411
if ((ret = num_coded_units(gb, chan, ctx)) < 0)
412
return ret;
413
414
if (ch_num && chan->num_coded_vals) {
415
vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
416
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
417
chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
418
419
for (i = 1; i < chan->num_coded_vals; i++) {
420
diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
421
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
422
chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
423
}
424
} else if (chan->num_coded_vals) {
425
flag = get_bits(gb, 1);
426
vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
427
428
start_val = get_bits(gb, 3);
429
unpack_vq_shape(start_val,
430
&atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
431
chan->qu_wordlen, chan->num_coded_vals);
432
433
if (!flag) {
434
for (i = 0; i < chan->num_coded_vals; i++) {
435
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
436
chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
437
}
438
} else {
439
for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
440
if (!get_bits1(gb)) {
441
chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
442
get_vlc2(gb, vlc_tab->table,
443
vlc_tab->bits, 1)) & 7;
444
chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
445
get_vlc2(gb, vlc_tab->table,
446
vlc_tab->bits, 1)) & 7;
447
}
448
449
if (chan->num_coded_vals & 1)
450
chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
451
get_vlc2(gb, vlc_tab->table,
452
vlc_tab->bits, 1)) & 7;
453
}
454
}
455
break;
456
case 3:
457
weight_idx = get_bits(gb, 2);
458
if ((ret = num_coded_units(gb, chan, ctx)) < 0)
459
return ret;
460
461
if (chan->num_coded_vals) {
462
vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
463
464
/* first coefficient is coded directly */
465
chan->qu_wordlen[0] = get_bits(gb, 3);
466
467
for (i = 1; i < chan->num_coded_vals; i++) {
468
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
469
chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
470
}
471
}
472
break;
473
}
474
475
if (chan->fill_mode == 2) {
476
for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
477
chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
478
} else if (chan->fill_mode == 3) {
479
pos = ch_num ? chan->num_coded_vals + chan->split_point
480
: ctx->num_quant_units - chan->split_point;
481
if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {
482
av_log(AV_LOG_ERROR, "Split point beyond array");
483
pos = FF_ARRAY_ELEMS(chan->qu_wordlen);
484
}
485
for (i = chan->num_coded_vals; i < pos; i++)
486
chan->qu_wordlen[i] = 1;
487
}
488
489
if (weight_idx)
490
return add_wordlen_weights(ctx, chan, weight_idx);
491
492
return 0;
493
}
494
495
/**
496
* Decode scale factor indexes for each quant unit of a channel.
497
*
498
* @param[in] gb the GetBit context
499
* @param[in,out] ctx ptr to the channel unit context
500
* @param[in] ch_num channel to process
501
* @return result code: 0 = OK, otherwise - error code
502
*/
503
static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx, int ch_num)
504
{
505
int i, weight_idx = 0, delta, diff, num_long_vals,
506
delta_bits, min_val, vlc_sel, start_val;
507
VLC *vlc_tab;
508
Atrac3pChanParams *chan = &ctx->channels[ch_num];
509
Atrac3pChanParams *ref_chan = &ctx->channels[0];
510
511
switch (get_bits(gb, 2)) { /* switch according to coding mode */
512
case 0: /* coded using constant number of bits */
513
for (i = 0; i < ctx->used_quant_units; i++)
514
chan->qu_sf_idx[i] = get_bits(gb, 6);
515
break;
516
case 1:
517
if (ch_num) {
518
vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
519
520
for (i = 0; i < ctx->used_quant_units; i++) {
521
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
522
chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
523
}
524
} else {
525
weight_idx = get_bits(gb, 2);
526
if (weight_idx == 3) {
527
UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
528
529
num_long_vals = get_bits(gb, 5);
530
delta_bits = get_bits(gb, 2);
531
min_val = get_bits(gb, 4) - 7;
532
533
for (i = 0; i < num_long_vals; i++)
534
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
535
get_bits(gb, 4) - 7) & 0x3F;
536
537
/* all others are: min_val + delta */
538
for (i = num_long_vals; i < ctx->used_quant_units; i++)
539
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
540
get_bitsz(gb, delta_bits)) & 0x3F;
541
} else {
542
num_long_vals = get_bits(gb, 5);
543
delta_bits = get_bits(gb, 3);
544
min_val = get_bits(gb, 6);
545
if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
546
av_log(AV_LOG_ERROR,
547
"SF mode 1: invalid parameters!\n");
548
return AVERROR_INVALIDDATA;
549
}
550
551
/* read full-precision SF indexes */
552
for (i = 0; i < num_long_vals; i++)
553
chan->qu_sf_idx[i] = get_bits(gb, 6);
554
555
/* all others are: min_val + delta */
556
for (i = num_long_vals; i < ctx->used_quant_units; i++)
557
chan->qu_sf_idx[i] = (min_val +
558
get_bitsz(gb, delta_bits)) & 0x3F;
559
}
560
}
561
break;
562
case 2:
563
if (ch_num) {
564
vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
565
566
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
567
chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
568
569
for (i = 1; i < ctx->used_quant_units; i++) {
570
diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
571
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
572
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
573
}
574
} else {
575
vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
576
577
UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
578
579
for (i = 0; i < ctx->used_quant_units; i++) {
580
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
581
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
582
sign_extend(delta, 4)) & 0x3F;
583
}
584
}
585
break;
586
case 3:
587
if (ch_num) {
588
/* copy coefficients from reference channel */
589
for (i = 0; i < ctx->used_quant_units; i++)
590
chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
591
} else {
592
weight_idx = get_bits(gb, 2);
593
vlc_sel = get_bits(gb, 2);
594
vlc_tab = &sf_vlc_tabs[vlc_sel];
595
596
if (weight_idx == 3) {
597
vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
598
599
UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
600
601
diff = (get_bits(gb, 4) + 56) & 0x3F;
602
chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
603
604
for (i = 1; i < ctx->used_quant_units; i++) {
605
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
606
diff = (diff + sign_extend(delta, 4)) & 0x3F;
607
chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
608
}
609
} else {
610
/* 1st coefficient is coded directly */
611
chan->qu_sf_idx[0] = get_bits(gb, 6);
612
613
for (i = 1; i < ctx->used_quant_units; i++) {
614
delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
615
chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
616
}
617
}
618
}
619
break;
620
}
621
622
if (weight_idx && weight_idx < 3)
623
return subtract_sf_weights(ctx, chan, weight_idx);
624
625
return 0;
626
}
627
628
/**
629
* Decode word length information for each channel.
630
*
631
* @param[in] gb the GetBit context
632
* @param[in,out] ctx ptr to the channel unit context
633
* @param[in] num_channels number of channels to process
634
* @return result code: 0 = OK, otherwise - error code
635
*/
636
static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
637
int num_channels)
638
{
639
int ch_num, i, ret;
640
641
for (ch_num = 0; ch_num < num_channels; ch_num++) {
642
memset(ctx->channels[ch_num].qu_wordlen, 0,
643
sizeof(ctx->channels[ch_num].qu_wordlen));
644
645
if ((ret = decode_channel_wordlen(gb, ctx, ch_num)) < 0)
646
return ret;
647
}
648
649
/* scan for last non-zero coeff in both channels and
650
* set number of quant units having coded spectrum */
651
for (i = ctx->num_quant_units - 1; i >= 0; i--)
652
if (ctx->channels[0].qu_wordlen[i] ||
653
(num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
654
break;
655
ctx->used_quant_units = i + 1;
656
657
return 0;
658
}
659
660
/**
661
* Decode scale factor indexes for each channel.
662
*
663
* @param[in] gb the GetBit context
664
* @param[in,out] ctx ptr to the channel unit context
665
* @param[in] num_channels number of channels to process
666
* @return result code: 0 = OK, otherwise - error code
667
*/
668
static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
669
int num_channels)
670
{
671
int ch_num, ret;
672
673
if (!ctx->used_quant_units)
674
return 0;
675
676
for (ch_num = 0; ch_num < num_channels; ch_num++) {
677
memset(ctx->channels[ch_num].qu_sf_idx, 0,
678
sizeof(ctx->channels[ch_num].qu_sf_idx));
679
680
if ((ret = decode_channel_sf_idx(gb, ctx, ch_num)) < 0)
681
return ret;
682
}
683
684
return 0;
685
}
686
687
/**
688
* Decode number of code table values.
689
*
690
* @param[in] gb the GetBit context
691
* @param[in,out] ctx ptr to the channel unit context
692
* @return result code: 0 = OK, otherwise - error code
693
*/
694
static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx)
695
{
696
int num_coded_vals;
697
698
if (get_bits1(gb)) {
699
num_coded_vals = get_bits(gb, 5);
700
if (num_coded_vals > ctx->used_quant_units) {
701
av_log(AV_LOG_ERROR,
702
"Invalid number of code table indexes: %d!\n", num_coded_vals);
703
return AVERROR_INVALIDDATA;
704
}
705
return num_coded_vals;
706
} else
707
return ctx->used_quant_units;
708
}
709
710
#define DEC_CT_IDX_COMMON(OP) \
711
num_vals = get_num_ct_values(gb, ctx); \
712
if (num_vals < 0) \
713
return num_vals; \
714
\
715
for (i = 0; i < num_vals; i++) { \
716
if (chan->qu_wordlen[i]) { \
717
chan->qu_tab_idx[i] = OP; \
718
} else if (ch_num && ref_chan->qu_wordlen[i]) \
719
/* get clone master flag */ \
720
chan->qu_tab_idx[i] = get_bits1(gb); \
721
}
722
723
#define CODING_DIRECT get_bits(gb, num_bits)
724
725
#define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
726
727
#define CODING_VLC_DELTA \
728
(!i) ? CODING_VLC \
729
: (pred + get_vlc2(gb, delta_vlc->table, \
730
delta_vlc->bits, 1)) & mask; \
731
pred = chan->qu_tab_idx[i]
732
733
#define CODING_VLC_DIFF \
734
(ref_chan->qu_tab_idx[i] + \
735
get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
736
737
/**
738
* Decode code table indexes for each quant unit of a channel.
739
*
740
* @param[in] gb the GetBit context
741
* @param[in,out] ctx ptr to the channel unit context
742
* @param[in] ch_num channel to process
743
* @return result code: 0 = OK, otherwise - error code
744
*/
745
static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
746
int ch_num)
747
{
748
int i, num_vals, num_bits, pred;
749
int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
750
VLC *vlc_tab, *delta_vlc;
751
Atrac3pChanParams *chan = &ctx->channels[ch_num];
752
Atrac3pChanParams *ref_chan = &ctx->channels[0];
753
754
chan->table_type = get_bits1(gb);
755
756
switch (get_bits(gb, 2)) { /* switch according to coding mode */
757
case 0: /* directly coded */
758
num_bits = ctx->use_full_table + 2;
759
DEC_CT_IDX_COMMON(CODING_DIRECT);
760
break;
761
case 1: /* entropy-coded */
762
vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
763
: ct_vlc_tabs;
764
DEC_CT_IDX_COMMON(CODING_VLC);
765
break;
766
case 2: /* entropy-coded delta */
767
if (ctx->use_full_table) {
768
vlc_tab = &ct_vlc_tabs[1];
769
delta_vlc = &ct_vlc_tabs[2];
770
} else {
771
vlc_tab = ct_vlc_tabs;
772
delta_vlc = ct_vlc_tabs;
773
}
774
pred = 0;
775
DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
776
break;
777
case 3: /* entropy-coded difference to master */
778
if (ch_num) {
779
vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
780
: ct_vlc_tabs;
781
DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
782
}
783
break;
784
}
785
786
return 0;
787
}
788
789
/**
790
* Decode code table indexes for each channel.
791
*
792
* @param[in] gb the GetBit context
793
* @param[in,out] ctx ptr to the channel unit context
794
* @param[in] num_channels number of channels to process
795
* @return result code: 0 = OK, otherwise - error code
796
*/
797
static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
798
int num_channels)
799
{
800
int ch_num, ret;
801
802
if (!ctx->used_quant_units)
803
return 0;
804
805
ctx->use_full_table = get_bits1(gb);
806
807
for (ch_num = 0; ch_num < num_channels; ch_num++) {
808
memset(ctx->channels[ch_num].qu_tab_idx, 0,
809
sizeof(ctx->channels[ch_num].qu_tab_idx));
810
811
if ((ret = decode_channel_code_tab(gb, ctx, ch_num)) < 0)
812
return ret;
813
}
814
815
return 0;
816
}
817
818
/**
819
* Decode huffman-coded spectral lines for a given quant unit.
820
*
821
* This is a generalized version for all known coding modes.
822
* Its speed can be improved by creating separate functions for each mode.
823
*
824
* @param[in] gb the GetBit context
825
* @param[in] tab code table telling how to decode spectral lines
826
* @param[in] vlc_tab ptr to the huffman table associated with the code table
827
* @param[out] out pointer to buffer where decoded data should be stored
828
* @param[in] num_specs number of spectral lines to decode
829
*/
830
static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
831
VLC *vlc_tab, int16_t *out, const int num_specs)
832
{
833
int i, j, pos, cf;
834
int group_size = tab->group_size;
835
int num_coeffs = tab->num_coeffs;
836
int bits = tab->bits;
837
int is_signed = tab->is_signed;
838
unsigned val;
839
const unsigned bitmask = ((1 << bits) - 1); // mask to clear higher bits.
840
841
for (pos = 0; pos < num_specs;) {
842
if (group_size == 1 || get_bits1(gb)) {
843
for (j = 0; j < group_size; j++) {
844
val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
845
for (i = 0; i < num_coeffs; i++) {
846
cf = val & bitmask;
847
if (is_signed)
848
cf = sign_extend(cf, bits);
849
else if (cf && get_bits1(gb))
850
cf = -cf;
851
852
out[pos++] = cf;
853
val >>= bits;
854
}
855
}
856
} else /* group skipped */
857
pos += group_size * num_coeffs;
858
}
859
}
860
861
/**
862
* Decode huffman-coded IMDCT spectrum for all channels.
863
*
864
* @param[in] gb the GetBit context
865
* @param[in,out] ctx ptr to the channel unit context
866
* @param[in] num_channels number of channels to process
867
*/
868
static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
869
int num_channels)
870
{
871
int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
872
const Atrac3pSpecCodeTab *tab;
873
Atrac3pChanParams *chan;
874
875
for (ch_num = 0; ch_num < num_channels; ch_num++) {
876
chan = &ctx->channels[ch_num];
877
878
memset(chan->spectrum, 0, sizeof(chan->spectrum));
879
880
/* set power compensation level to disabled */
881
memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
882
883
for (qu = 0; qu < ctx->used_quant_units; qu++) {
884
num_specs = av_atrac3p_qu_to_spec_pos[qu + 1] -
885
av_atrac3p_qu_to_spec_pos[qu];
886
887
wordlen = chan->qu_wordlen[qu];
888
codetab = chan->qu_tab_idx[qu];
889
if (wordlen) {
890
if (!ctx->use_full_table)
891
codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
892
893
tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
894
tab = &atrac3p_spectra_tabs[tab_index];
895
896
/* this allows reusing VLC tables */
897
if (tab->redirect >= 0)
898
tab_index = tab->redirect;
899
900
decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
901
&chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]],
902
num_specs);
903
} else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
904
/* copy coefficients from master */
905
memcpy(&chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]],
906
&ctx->channels[0].spectrum[av_atrac3p_qu_to_spec_pos[qu]],
907
num_specs *
908
sizeof(chan->spectrum[av_atrac3p_qu_to_spec_pos[qu]]));
909
chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
910
}
911
}
912
913
/* Power compensation levels only present in the bitstream
914
* if there are more than 2 quant units. The lowest two units
915
* correspond to the frequencies 0...351 Hz, whose shouldn't
916
* be affected by the power compensation. */
917
if (ctx->used_quant_units > 2) {
918
num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
919
for (i = 0; i < num_specs; i++)
920
chan->power_levs[i] = get_bits(gb, 4);
921
}
922
}
923
}
924
925
/**
926
* Retrieve specified amount of flag bits from the input bitstream.
927
* The data can be shortened in the case of the following two common conditions:
928
* if all bits are zero then only one signal bit = 0 will be stored,
929
* if all bits are ones then two signal bits = 1,0 will be stored.
930
* Otherwise, all necessary bits will be directly stored
931
* prefixed by two signal bits = 1,1.
932
*
933
* @param[in] gb ptr to the GetBitContext
934
* @param[out] out where to place decoded flags
935
* @param[in] num_flags number of flags to process
936
* @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
937
*/
938
static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
939
{
940
int i, result;
941
942
memset(out, 0, num_flags);
943
944
result = get_bits1(gb);
945
if (result) {
946
if (get_bits1(gb))
947
for (i = 0; i < num_flags; i++)
948
out[i] = get_bits1(gb);
949
else
950
memset(out, 1, num_flags);
951
}
952
953
return result;
954
}
955
956
/**
957
* Decode mdct window shape flags for all channels.
958
*
959
* @param[in] gb the GetBit context
960
* @param[in,out] ctx ptr to the channel unit context
961
* @param[in] num_channels number of channels to process
962
*/
963
static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
964
int num_channels)
965
{
966
int ch_num;
967
968
for (ch_num = 0; ch_num < num_channels; ch_num++)
969
get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
970
ctx->num_subbands);
971
}
972
973
/**
974
* Decode number of gain control points.
975
*
976
* @param[in] gb the GetBit context
977
* @param[in,out] ctx ptr to the channel unit context
978
* @param[in] ch_num channel to process
979
* @param[in] coded_subbands number of subbands to process
980
* @return result code: 0 = OK, otherwise - error code
981
*/
982
static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
983
int ch_num, int coded_subbands)
984
{
985
int i, delta, delta_bits, min_val;
986
Atrac3pChanParams *chan = &ctx->channels[ch_num];
987
Atrac3pChanParams *ref_chan = &ctx->channels[0];
988
989
switch (get_bits(gb, 2)) { /* switch according to coding mode */
990
case 0: /* fixed-length coding */
991
for (i = 0; i < coded_subbands; i++)
992
chan->gain_data[i].num_points = get_bits(gb, 3);
993
break;
994
case 1: /* variable-length coding */
995
for (i = 0; i < coded_subbands; i++)
996
chan->gain_data[i].num_points =
997
get_vlc2(gb, gain_vlc_tabs[0].table,
998
gain_vlc_tabs[0].bits, 1);
999
break;
1000
case 2:
1001
if (ch_num) { /* VLC modulo delta to master channel */
1002
for (i = 0; i < coded_subbands; i++) {
1003
delta = get_vlc2(gb, gain_vlc_tabs[1].table,
1004
gain_vlc_tabs[1].bits, 1);
1005
chan->gain_data[i].num_points =
1006
(ref_chan->gain_data[i].num_points + delta) & 7;
1007
}
1008
} else { /* VLC modulo delta to previous */
1009
chan->gain_data[0].num_points =
1010
get_vlc2(gb, gain_vlc_tabs[0].table,
1011
gain_vlc_tabs[0].bits, 1);
1012
1013
for (i = 1; i < coded_subbands; i++) {
1014
delta = get_vlc2(gb, gain_vlc_tabs[1].table,
1015
gain_vlc_tabs[1].bits, 1);
1016
chan->gain_data[i].num_points =
1017
(chan->gain_data[i - 1].num_points + delta) & 7;
1018
}
1019
}
1020
break;
1021
case 3:
1022
if (ch_num) { /* copy data from master channel */
1023
for (i = 0; i < coded_subbands; i++)
1024
chan->gain_data[i].num_points =
1025
ref_chan->gain_data[i].num_points;
1026
} else { /* shorter delta to min */
1027
delta_bits = get_bits(gb, 2);
1028
min_val = get_bits(gb, 3);
1029
1030
for (i = 0; i < coded_subbands; i++) {
1031
chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
1032
if (chan->gain_data[i].num_points > 7)
1033
return AVERROR_INVALIDDATA;
1034
}
1035
}
1036
}
1037
1038
return 0;
1039
}
1040
1041
/**
1042
* Implements coding mode 3 (slave) for gain compensation levels.
1043
*
1044
* @param[out] dst ptr to the output array
1045
* @param[in] ref ptr to the reference channel
1046
*/
1047
static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
1048
{
1049
int i;
1050
1051
for (i = 0; i < dst->num_points; i++)
1052
dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
1053
}
1054
1055
/**
1056
* Implements coding mode 1 (master) for gain compensation levels.
1057
*
1058
* @param[in] gb the GetBit context
1059
* @param[in] ctx ptr to the channel unit context
1060
* @param[out] dst ptr to the output array
1061
*/
1062
static inline void gainc_level_mode1m(GetBitContext *gb,
1063
Atrac3pChanUnitCtx *ctx,
1064
AtracGainInfo *dst)
1065
{
1066
int i, delta;
1067
1068
if (dst->num_points > 0)
1069
dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
1070
gain_vlc_tabs[2].bits, 1);
1071
1072
for (i = 1; i < dst->num_points; i++) {
1073
delta = get_vlc2(gb, gain_vlc_tabs[3].table,
1074
gain_vlc_tabs[3].bits, 1);
1075
dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
1076
}
1077
}
1078
1079
/**
1080
* Decode level code for each gain control point.
1081
*
1082
* @param[in] gb the GetBit context
1083
* @param[in,out] ctx ptr to the channel unit context
1084
* @param[in] ch_num channel to process
1085
* @param[in] coded_subbands number of subbands to process
1086
* @return result code: 0 = OK, otherwise - error code
1087
*/
1088
static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1089
int ch_num, int coded_subbands)
1090
{
1091
int sb, i, delta, delta_bits, min_val, pred;
1092
Atrac3pChanParams *chan = &ctx->channels[ch_num];
1093
Atrac3pChanParams *ref_chan = &ctx->channels[0];
1094
1095
switch (get_bits(gb, 2)) { /* switch according to coding mode */
1096
case 0: /* fixed-length coding */
1097
for (sb = 0; sb < coded_subbands; sb++)
1098
for (i = 0; i < chan->gain_data[sb].num_points; i++)
1099
chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
1100
break;
1101
case 1:
1102
if (ch_num) { /* VLC modulo delta to master channel */
1103
for (sb = 0; sb < coded_subbands; sb++)
1104
for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1105
delta = get_vlc2(gb, gain_vlc_tabs[5].table,
1106
gain_vlc_tabs[5].bits, 1);
1107
pred = (i >= ref_chan->gain_data[sb].num_points)
1108
? 7 : ref_chan->gain_data[sb].lev_code[i];
1109
chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1110
}
1111
} else { /* VLC modulo delta to previous */
1112
for (sb = 0; sb < coded_subbands; sb++)
1113
gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1114
}
1115
break;
1116
case 2:
1117
if (ch_num) { /* VLC modulo delta to previous or clone master */
1118
for (sb = 0; sb < coded_subbands; sb++)
1119
if (chan->gain_data[sb].num_points > 0) {
1120
if (get_bits1(gb))
1121
gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
1122
else
1123
gainc_level_mode3s(&chan->gain_data[sb],
1124
&ref_chan->gain_data[sb]);
1125
}
1126
} else { /* VLC modulo delta to lev_codes of previous subband */
1127
if (chan->gain_data[0].num_points > 0)
1128
gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
1129
1130
for (sb = 1; sb < coded_subbands; sb++)
1131
for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1132
delta = get_vlc2(gb, gain_vlc_tabs[4].table,
1133
gain_vlc_tabs[4].bits, 1);
1134
pred = (i >= chan->gain_data[sb - 1].num_points)
1135
? 7 : chan->gain_data[sb - 1].lev_code[i];
1136
chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
1137
}
1138
}
1139
break;
1140
case 3:
1141
if (ch_num) { /* clone master */
1142
for (sb = 0; sb < coded_subbands; sb++)
1143
gainc_level_mode3s(&chan->gain_data[sb],
1144
&ref_chan->gain_data[sb]);
1145
} else { /* shorter delta to min */
1146
delta_bits = get_bits(gb, 2);
1147
min_val = get_bits(gb, 4);
1148
1149
for (sb = 0; sb < coded_subbands; sb++)
1150
for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1151
chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
1152
if (chan->gain_data[sb].lev_code[i] > 15)
1153
return AVERROR_INVALIDDATA;
1154
}
1155
}
1156
break;
1157
}
1158
1159
return 0;
1160
}
1161
1162
/**
1163
* Implements coding mode 0 for gain compensation locations.
1164
*
1165
* @param[in] gb the GetBit context
1166
* @param[in] ctx ptr to the channel unit context
1167
* @param[out] dst ptr to the output array
1168
* @param[in] pos position of the value to be processed
1169
*/
1170
static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1171
AtracGainInfo *dst, int pos)
1172
{
1173
int delta_bits;
1174
1175
if (!pos || dst->loc_code[pos - 1] < 15)
1176
dst->loc_code[pos] = get_bits(gb, 5);
1177
else if (dst->loc_code[pos - 1] >= 30)
1178
dst->loc_code[pos] = 31;
1179
else {
1180
delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
1181
dst->loc_code[pos] = dst->loc_code[pos - 1] +
1182
get_bits(gb, delta_bits) + 1;
1183
}
1184
}
1185
1186
/**
1187
* Implements coding mode 1 for gain compensation locations.
1188
*
1189
* @param[in] gb the GetBit context
1190
* @param[in] ctx ptr to the channel unit context
1191
* @param[out] dst ptr to the output array
1192
*/
1193
static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1194
AtracGainInfo *dst)
1195
{
1196
int i;
1197
VLC *tab;
1198
1199
if (dst->num_points > 0) {
1200
/* 1st coefficient is stored directly */
1201
dst->loc_code[0] = get_bits(gb, 5);
1202
1203
for (i = 1; i < dst->num_points; i++) {
1204
/* switch VLC according to the curve direction
1205
* (ascending/descending) */
1206
tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
1207
? &gain_vlc_tabs[7]
1208
: &gain_vlc_tabs[9];
1209
dst->loc_code[i] = dst->loc_code[i - 1] +
1210
get_vlc2(gb, tab->table, tab->bits, 1);
1211
}
1212
}
1213
}
1214
1215
/**
1216
* Decode location code for each gain control point.
1217
*
1218
* @param[in] gb the GetBit context
1219
* @param[in,out] ctx ptr to the channel unit context
1220
* @param[in] ch_num channel to process
1221
* @param[in] coded_subbands number of subbands to process
1222
* @return result code: 0 = OK, otherwise - error code
1223
*/
1224
static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1225
int ch_num, int coded_subbands)
1226
{
1227
int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
1228
AtracGainInfo *dst, *ref;
1229
VLC *tab;
1230
Atrac3pChanParams *chan = &ctx->channels[ch_num];
1231
Atrac3pChanParams *ref_chan = &ctx->channels[0];
1232
1233
switch (get_bits(gb, 2)) { /* switch according to coding mode */
1234
case 0: /* sequence of numbers in ascending order */
1235
for (sb = 0; sb < coded_subbands; sb++)
1236
for (i = 0; i < chan->gain_data[sb].num_points; i++)
1237
gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1238
break;
1239
case 1:
1240
if (ch_num) {
1241
for (sb = 0; sb < coded_subbands; sb++) {
1242
if (chan->gain_data[sb].num_points <= 0)
1243
continue;
1244
dst = &chan->gain_data[sb];
1245
ref = &ref_chan->gain_data[sb];
1246
1247
/* 1st value is vlc-coded modulo delta to master */
1248
delta = get_vlc2(gb, gain_vlc_tabs[10].table,
1249
gain_vlc_tabs[10].bits, 1);
1250
pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
1251
dst->loc_code[0] = (pred + delta) & 0x1F;
1252
1253
for (i = 1; i < dst->num_points; i++) {
1254
more_than_ref = i >= ref->num_points;
1255
if (dst->lev_code[i] > dst->lev_code[i - 1]) {
1256
/* ascending curve */
1257
if (more_than_ref) {
1258
delta =
1259
get_vlc2(gb, gain_vlc_tabs[9].table,
1260
gain_vlc_tabs[9].bits, 1);
1261
dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1262
} else {
1263
if (get_bits1(gb))
1264
gainc_loc_mode0(gb, ctx, dst, i); // direct coding
1265
else
1266
dst->loc_code[i] = ref->loc_code[i]; // clone master
1267
}
1268
} else { /* descending curve */
1269
tab = more_than_ref ? &gain_vlc_tabs[7]
1270
: &gain_vlc_tabs[10];
1271
delta = get_vlc2(gb, tab->table, tab->bits, 1);
1272
if (more_than_ref)
1273
dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1274
else
1275
dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
1276
}
1277
}
1278
}
1279
} else /* VLC delta to previous */
1280
for (sb = 0; sb < coded_subbands; sb++)
1281
gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
1282
break;
1283
case 2:
1284
if (ch_num) {
1285
for (sb = 0; sb < coded_subbands; sb++) {
1286
if (chan->gain_data[sb].num_points <= 0)
1287
continue;
1288
dst = &chan->gain_data[sb];
1289
ref = &ref_chan->gain_data[sb];
1290
if (dst->num_points > ref->num_points || get_bits1(gb))
1291
gainc_loc_mode1(gb, ctx, dst);
1292
else /* clone master for the whole subband */
1293
for (i = 0; i < chan->gain_data[sb].num_points; i++)
1294
dst->loc_code[i] = ref->loc_code[i];
1295
}
1296
} else {
1297
/* data for the first subband is coded directly */
1298
for (i = 0; i < chan->gain_data[0].num_points; i++)
1299
gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
1300
1301
for (sb = 1; sb < coded_subbands; sb++) {
1302
if (chan->gain_data[sb].num_points <= 0)
1303
continue;
1304
dst = &chan->gain_data[sb];
1305
1306
/* 1st value is vlc-coded modulo delta to the corresponding
1307
* value of the previous subband if any or zero */
1308
delta = get_vlc2(gb, gain_vlc_tabs[6].table,
1309
gain_vlc_tabs[6].bits, 1);
1310
pred = dst[-1].num_points > 0
1311
? dst[-1].loc_code[0] : 0;
1312
dst->loc_code[0] = (pred + delta) & 0x1F;
1313
1314
for (i = 1; i < dst->num_points; i++) {
1315
more_than_ref = i >= dst[-1].num_points;
1316
/* Select VLC table according to curve direction and
1317
* presence of prediction. */
1318
tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
1319
2 + more_than_ref + 6];
1320
delta = get_vlc2(gb, tab->table, tab->bits, 1);
1321
if (more_than_ref)
1322
dst->loc_code[i] = dst->loc_code[i - 1] + delta;
1323
else
1324
dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
1325
}
1326
}
1327
}
1328
break;
1329
case 3:
1330
if (ch_num) { /* clone master or direct or direct coding */
1331
for (sb = 0; sb < coded_subbands; sb++)
1332
for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1333
if (i >= ref_chan->gain_data[sb].num_points)
1334
gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
1335
else
1336
chan->gain_data[sb].loc_code[i] =
1337
ref_chan->gain_data[sb].loc_code[i];
1338
}
1339
} else { /* shorter delta to min */
1340
delta_bits = get_bits(gb, 2) + 1;
1341
min_val = get_bits(gb, 5);
1342
1343
for (sb = 0; sb < coded_subbands; sb++)
1344
for (i = 0; i < chan->gain_data[sb].num_points; i++)
1345
chan->gain_data[sb].loc_code[i] = min_val + i +
1346
get_bits(gb, delta_bits);
1347
}
1348
break;
1349
}
1350
1351
/* Validate decoded information */
1352
for (sb = 0; sb < coded_subbands; sb++) {
1353
dst = &chan->gain_data[sb];
1354
for (i = 0; i < chan->gain_data[sb].num_points; i++) {
1355
if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
1356
(i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
1357
av_log(AV_LOG_ERROR,
1358
"Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
1359
ch_num, sb, i, dst->loc_code[i]);
1360
return AVERROR_INVALIDDATA;
1361
}
1362
}
1363
}
1364
1365
return 0;
1366
}
1367
1368
/**
1369
* Decode gain control data for all channels.
1370
*
1371
* @param[in] gb the GetBit context
1372
* @param[in,out] ctx ptr to the channel unit context
1373
* @param[in] num_channels number of channels to process
1374
* @return result code: 0 = OK, otherwise - error code
1375
*/
1376
static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1377
int num_channels)
1378
{
1379
int ch_num, coded_subbands, sb, ret;
1380
1381
for (ch_num = 0; ch_num < num_channels; ch_num++) {
1382
memset(ctx->channels[ch_num].gain_data, 0,
1383
sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
1384
1385
if (get_bits1(gb)) { /* gain control data present? */
1386
coded_subbands = get_bits(gb, 4) + 1;
1387
if (get_bits1(gb)) /* is high band gain data replication on? */
1388
ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
1389
else
1390
ctx->channels[ch_num].num_gain_subbands = coded_subbands;
1391
1392
if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
1393
(ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
1394
(ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands)) < 0)
1395
return ret;
1396
1397
if (coded_subbands > 0) { /* propagate gain data if requested */
1398
for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
1399
ctx->channels[ch_num].gain_data[sb] =
1400
ctx->channels[ch_num].gain_data[sb - 1];
1401
}
1402
} else {
1403
ctx->channels[ch_num].num_gain_subbands = 0;
1404
}
1405
}
1406
1407
return 0;
1408
}
1409
1410
/**
1411
* Decode envelope for all tones of a channel.
1412
*
1413
* @param[in] gb the GetBit context
1414
* @param[in,out] ctx ptr to the channel unit context
1415
* @param[in] ch_num channel to process
1416
* @param[in] band_has_tones ptr to an array of per-band-flags:
1417
* 1 - tone data present
1418
*/
1419
static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1420
int ch_num, int band_has_tones[])
1421
{
1422
int sb;
1423
Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1424
Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1425
1426
if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1427
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1428
if (!band_has_tones[sb])
1429
continue;
1430
dst[sb].pend_env.has_start_point = get_bits1(gb);
1431
dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
1432
? get_bits(gb, 5) : -1;
1433
dst[sb].pend_env.has_stop_point = get_bits1(gb);
1434
dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
1435
? get_bits(gb, 5) : 32;
1436
}
1437
} else { /* mode 1(slave only): copy master */
1438
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1439
if (!band_has_tones[sb])
1440
continue;
1441
dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
1442
dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
1443
dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
1444
dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
1445
}
1446
}
1447
}
1448
1449
/**
1450
* Decode number of tones for each subband of a channel.
1451
*
1452
* @param[in] gb the GetBit context
1453
* @param[in,out] ctx ptr to the channel unit context
1454
* @param[in] ch_num channel to process
1455
* @param[in] band_has_tones ptr to an array of per-band-flags:
1456
* 1 - tone data present
1457
* @return result code: 0 = OK, otherwise - error code
1458
*/
1459
static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1460
int ch_num, int band_has_tones[])
1461
{
1462
int mode, sb, delta;
1463
Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1464
Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1465
1466
mode = get_bits(gb, ch_num + 1);
1467
switch (mode) {
1468
case 0: /** fixed-length coding */
1469
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1470
if (band_has_tones[sb])
1471
dst[sb].num_wavs = get_bits(gb, 4);
1472
break;
1473
case 1: /** variable-length coding */
1474
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1475
if (band_has_tones[sb])
1476
dst[sb].num_wavs =
1477
get_vlc2(gb, tone_vlc_tabs[1].table,
1478
tone_vlc_tabs[1].bits, 1);
1479
break;
1480
case 2: /** VLC modulo delta to master (slave only) */
1481
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1482
if (band_has_tones[sb]) {
1483
delta = get_vlc2(gb, tone_vlc_tabs[2].table,
1484
tone_vlc_tabs[2].bits, 1);
1485
delta = sign_extend(delta, 3);
1486
dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
1487
}
1488
break;
1489
case 3: /** copy master (slave only) */
1490
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1491
if (band_has_tones[sb])
1492
dst[sb].num_wavs = ref[sb].num_wavs;
1493
break;
1494
}
1495
1496
/** initialize start tone index for each subband */
1497
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
1498
if (band_has_tones[sb]) {
1499
if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
1500
av_log(AV_LOG_ERROR,
1501
"Too many tones: %d (max. 48)!\n",
1502
ctx->waves_info->tones_index + dst[sb].num_wavs);
1503
return AVERROR_INVALIDDATA;
1504
}
1505
dst[sb].start_index = ctx->waves_info->tones_index;
1506
ctx->waves_info->tones_index += dst[sb].num_wavs;
1507
}
1508
1509
return 0;
1510
}
1511
1512
/**
1513
* Decode frequency information for each subband of a channel.
1514
*
1515
* @param[in] gb the GetBit context
1516
* @param[in,out] ctx ptr to the channel unit context
1517
* @param[in] ch_num channel to process
1518
* @param[in] band_has_tones ptr to an array of per-band-flags:
1519
* 1 - tone data present
1520
*/
1521
static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1522
int ch_num, int band_has_tones[])
1523
{
1524
int sb, i, direction, nbits, pred, delta;
1525
Atrac3pWaveParam *iwav, *owav;
1526
Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1527
Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1528
1529
if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
1530
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1531
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1532
continue;
1533
iwav = &ctx->waves_info->waves[dst[sb].start_index];
1534
direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
1535
if (direction) { /** packed numbers in descending order */
1536
if (dst[sb].num_wavs)
1537
iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
1538
for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
1539
nbits = av_log2(iwav[i+1].freq_index) + 1;
1540
iwav[i].freq_index = get_bits(gb, nbits);
1541
}
1542
} else { /** packed numbers in ascending order */
1543
for (i = 0; i < dst[sb].num_wavs; i++) {
1544
if (!i || iwav[i - 1].freq_index < 512)
1545
iwav[i].freq_index = get_bits(gb, 10);
1546
else {
1547
nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
1548
iwav[i].freq_index = get_bits(gb, nbits) +
1549
1024 - (1 << nbits);
1550
}
1551
}
1552
}
1553
}
1554
} else { /* mode 1: VLC modulo delta to master (slave only) */
1555
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1556
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1557
continue;
1558
iwav = &ctx->waves_info->waves[ref[sb].start_index];
1559
owav = &ctx->waves_info->waves[dst[sb].start_index];
1560
for (i = 0; i < dst[sb].num_wavs; i++) {
1561
delta = get_vlc2(gb, tone_vlc_tabs[6].table,
1562
tone_vlc_tabs[6].bits, 1);
1563
delta = sign_extend(delta, 8);
1564
pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
1565
(ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
1566
owav[i].freq_index = (pred + delta) & 0x3FF;
1567
}
1568
}
1569
}
1570
}
1571
1572
/**
1573
* Decode amplitude information for each subband of a channel.
1574
*
1575
* @param[in] gb the GetBit context
1576
* @param[in,out] ctx ptr to the channel unit context
1577
* @param[in] ch_num channel to process
1578
* @param[in] band_has_tones ptr to an array of per-band-flags:
1579
* 1 - tone data present
1580
*/
1581
static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1582
int ch_num, int band_has_tones[])
1583
{
1584
int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
1585
Atrac3pWaveParam *wsrc, *wref;
1586
int refwaves[48] = { 0 };
1587
Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1588
Atrac3pWavesData *ref = ctx->channels[0].tones_info;
1589
1590
if (ch_num) {
1591
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1592
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1593
continue;
1594
wsrc = &ctx->waves_info->waves[dst[sb].start_index];
1595
wref = &ctx->waves_info->waves[ref[sb].start_index];
1596
for (j = 0; j < dst[sb].num_wavs; j++) {
1597
for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
1598
diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
1599
if (diff < maxdiff) {
1600
maxdiff = diff;
1601
fi = i;
1602
}
1603
}
1604
1605
if (maxdiff < 8)
1606
refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
1607
else if (j < ref[sb].num_wavs)
1608
refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
1609
else
1610
refwaves[dst[sb].start_index + j] = -1;
1611
}
1612
}
1613
}
1614
1615
mode = get_bits(gb, ch_num + 1);
1616
1617
switch (mode) {
1618
case 0: /** fixed-length coding */
1619
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1620
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1621
continue;
1622
if (ctx->waves_info->amplitude_mode)
1623
for (i = 0; i < dst[sb].num_wavs; i++)
1624
ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
1625
else
1626
ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
1627
}
1628
break;
1629
case 1: /** min + VLC delta */
1630
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1631
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1632
continue;
1633
if (ctx->waves_info->amplitude_mode)
1634
for (i = 0; i < dst[sb].num_wavs; i++)
1635
ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1636
get_vlc2(gb, tone_vlc_tabs[3].table,
1637
tone_vlc_tabs[3].bits, 1) + 20;
1638
else
1639
ctx->waves_info->waves[dst[sb].start_index].amp_sf =
1640
get_vlc2(gb, tone_vlc_tabs[4].table,
1641
tone_vlc_tabs[4].bits, 1) + 24;
1642
}
1643
break;
1644
case 2: /** VLC modulo delta to master (slave only) */
1645
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1646
if (!band_has_tones[sb] || !dst[sb].num_wavs)
1647
continue;
1648
for (i = 0; i < dst[sb].num_wavs; i++) {
1649
delta = get_vlc2(gb, tone_vlc_tabs[5].table,
1650
tone_vlc_tabs[5].bits, 1);
1651
delta = sign_extend(delta, 5);
1652
pred = refwaves[dst[sb].start_index + i] >= 0 ?
1653
ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
1654
ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
1655
}
1656
}
1657
break;
1658
case 3: /** clone master (slave only) */
1659
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1660
if (!band_has_tones[sb])
1661
continue;
1662
for (i = 0; i < dst[sb].num_wavs; i++)
1663
ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
1664
refwaves[dst[sb].start_index + i] >= 0
1665
? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
1666
: 32;
1667
}
1668
break;
1669
}
1670
}
1671
1672
/**
1673
* Decode phase information for each subband of a channel.
1674
*
1675
* @param[in] gb the GetBit context
1676
* @param[in,out] ctx ptr to the channel unit context
1677
* @param[in] ch_num channel to process
1678
* @param[in] band_has_tones ptr to an array of per-band-flags:
1679
* 1 - tone data present
1680
*/
1681
static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1682
int ch_num, int band_has_tones[])
1683
{
1684
int sb, i;
1685
Atrac3pWaveParam *wparam;
1686
Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
1687
1688
for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
1689
if (!band_has_tones[sb])
1690
continue;
1691
wparam = &ctx->waves_info->waves[dst[sb].start_index];
1692
for (i = 0; i < dst[sb].num_wavs; i++)
1693
wparam[i].phase_index = get_bits(gb, 5);
1694
}
1695
}
1696
1697
/**
1698
* Decode tones info for all channels.
1699
*
1700
* @param[in] gb the GetBit context
1701
* @param[in,out] ctx ptr to the channel unit context
1702
* @param[in] num_channels number of channels to process
1703
* @return result code: 0 = OK, otherwise - error code
1704
*/
1705
static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1706
int num_channels)
1707
{
1708
int ch_num, i, ret;
1709
int band_has_tones[16];
1710
1711
for (ch_num = 0; ch_num < num_channels; ch_num++)
1712
memset(ctx->channels[ch_num].tones_info, 0,
1713
sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
1714
1715
ctx->waves_info->tones_present = get_bits1(gb);
1716
if (!ctx->waves_info->tones_present)
1717
return 0;
1718
1719
memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
1720
1721
ctx->waves_info->amplitude_mode = get_bits1(gb);
1722
if (!ctx->waves_info->amplitude_mode) {
1723
avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
1724
return AVERROR_PATCHWELCOME;
1725
}
1726
1727
ctx->waves_info->num_tone_bands =
1728
get_vlc2(gb, tone_vlc_tabs[0].table,
1729
tone_vlc_tabs[0].bits, 1) + 1;
1730
1731
if (num_channels == 2) {
1732
get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
1733
get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
1734
get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
1735
}
1736
1737
ctx->waves_info->tones_index = 0;
1738
1739
for (ch_num = 0; ch_num < num_channels; ch_num++) {
1740
for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
1741
band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
1742
1743
decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
1744
if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones)) < 0)
1745
return ret;
1746
1747
decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
1748
decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
1749
decode_tones_phase(gb, ctx, ch_num, band_has_tones);
1750
}
1751
1752
if (num_channels == 2) {
1753
for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
1754
if (ctx->waves_info->tone_sharing[i])
1755
ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
1756
1757
if (ctx->waves_info->tone_master[i])
1758
FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
1759
ctx->channels[1].tones_info[i]);
1760
}
1761
}
1762
1763
return 0;
1764
}
1765
1766
int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
1767
int num_channels)
1768
{
1769
int ret;
1770
1771
/* parse sound header */
1772
ctx->num_quant_units = get_bits(gb, 5) + 1;
1773
if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
1774
av_log(AV_LOG_ERROR,
1775
"Invalid number of quantization units: %d!\n",
1776
ctx->num_quant_units);
1777
return AVERROR_INVALIDDATA;
1778
}
1779
1780
ctx->mute_flag = get_bits1(gb);
1781
1782
/* decode various sound parameters */
1783
if ((ret = decode_quant_wordlen(gb, ctx, num_channels)) < 0)
1784
return ret;
1785
1786
ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
1787
ctx->num_coded_subbands = ctx->used_quant_units
1788
? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
1789
: 0;
1790
1791
if ((ret = decode_scale_factors(gb, ctx, num_channels)) < 0)
1792
return ret;
1793
1794
if ((ret = decode_code_table_indexes(gb, ctx, num_channels)) < 0)
1795
return ret;
1796
1797
decode_spectrum(gb, ctx, num_channels);
1798
1799
if (num_channels == 2) {
1800
get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
1801
get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
1802
}
1803
1804
decode_window_shape(gb, ctx, num_channels);
1805
1806
if ((ret = decode_gainc_data(gb, ctx, num_channels)) < 0)
1807
return ret;
1808
1809
if ((ret = decode_tones_info(gb, ctx, num_channels)) < 0)
1810
return ret;
1811
1812
/* decode global noise info */
1813
ctx->noise_present = get_bits1(gb);
1814
if (ctx->noise_present) {
1815
ctx->noise_level_index = get_bits(gb, 4);
1816
ctx->noise_table_index = get_bits(gb, 4);
1817
}
1818
1819
return 0;
1820
}
1821
1822