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/atrac3.cpp
Views: 1401
1
/*
2
* ATRAC3 compatible decoder
3
* Copyright (c) 2006-2008 Maxim Poliakovski
4
* Copyright (c) 2006-2008 Benjamin Larsson
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
* ATRAC3 compatible decoder.
26
* This decoder handles Sony's ATRAC3 data.
27
*
28
* Container formats used to store ATRAC3 data:
29
* RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
30
*
31
* To use this decoder, a calling application must supply the extradata
32
* bytes provided in the containers above.
33
*/
34
#define _USE_MATH_DEFINES
35
36
#include <math.h>
37
#include <stddef.h>
38
#include <stdio.h>
39
#include <string.h>
40
41
#include "float_dsp.h"
42
#include "fft.h"
43
#include "mem.h"
44
#include "compat.h"
45
#include "get_bits.h"
46
47
#include "atrac.h"
48
#include "atrac3data.h"
49
50
#define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
51
52
#define JOINT_STEREO 0x12
53
#define STEREO 0x2
54
55
#define SAMPLES_PER_FRAME 1024
56
#define MDCT_SIZE 512
57
58
typedef struct GainBlock {
59
AtracGainInfo g_block[4];
60
} GainBlock;
61
62
typedef struct TonalComponent {
63
int pos;
64
int num_coefs;
65
float coef[8];
66
} TonalComponent;
67
68
typedef struct ChannelUnit {
69
int bands_coded;
70
int num_components;
71
float prev_frame[SAMPLES_PER_FRAME];
72
int gc_blk_switch;
73
TonalComponent components[64];
74
GainBlock gain_block[2];
75
76
DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
77
DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME];
78
79
float delay_buf1[46]; ///<qmf delay buffers
80
float delay_buf2[46];
81
float delay_buf3[46];
82
} ChannelUnit;
83
84
typedef struct ATRAC3Context {
85
GetBitContext gb;
86
//@{
87
/** stream data */
88
int coding_mode;
89
90
ChannelUnit *units;
91
//@}
92
//@{
93
/** joint-stereo related variables */
94
int matrix_coeff_index_prev[4];
95
int matrix_coeff_index_now[4];
96
int matrix_coeff_index_next[4];
97
int weighting_delay[6];
98
//@}
99
//@{
100
/** data buffers */
101
uint8_t *decoded_bytes_buffer;
102
float temp_buf[1070];
103
//@}
104
//@{
105
/** extradata */
106
int scrambled_stream;
107
//@}
108
109
AtracGCContext gainc_ctx;
110
FFTContext mdct_ctx;
111
112
int block_align;
113
int channels;
114
} ATRAC3Context;
115
116
static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
117
static VLC_TYPE atrac3_vlc_table[4096][2];
118
static VLC spectral_coeff_tab[7];
119
120
/**
121
* Regular 512 points IMDCT without overlapping, with the exception of the
122
* swapping of odd bands caused by the reverse spectra of the QMF.
123
*
124
* @param odd_band 1 if the band is an odd band
125
*/
126
static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band)
127
{
128
int i;
129
130
if (odd_band) {
131
/**
132
* Reverse the odd bands before IMDCT, this is an effect of the QMF
133
* transform or it gives better compression to do it this way.
134
* FIXME: It should be possible to handle this in imdct_calc
135
* for that to happen a modification of the prerotation step of
136
* all SIMD code and C code is needed.
137
* Or fix the functions before so they generate a pre reversed spectrum.
138
*/
139
for (i = 0; i < 128; i++)
140
FFSWAP(float, input[i], input[255 - i]);
141
}
142
143
imdct_calc(&q->mdct_ctx, output, input);
144
145
/* Perform windowing on the output. */
146
vector_fmul(output, mdct_window, MDCT_SIZE);
147
}
148
149
/*
150
* indata descrambling, only used for data coming from the rm container
151
*/
152
static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes)
153
{
154
int i, off;
155
uint32_t c;
156
const uint32_t *buf;
157
uint32_t *output = (uint32_t *)out;
158
159
off = (intptr_t)input & 3;
160
buf = (const uint32_t *)(input - off);
161
if (off)
162
c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
163
else
164
c = av_be2ne32(0x537F6103U);
165
bytes += 3 + off;
166
for (i = 0; i < bytes / 4; i++)
167
output[i] = c ^ buf[i];
168
169
//if (off)
170
// avpriv_request_sample(NULL, "Offset of %d", off);
171
172
return off;
173
}
174
175
static void init_imdct_window(void)
176
{
177
int i, j;
178
179
/* generate the mdct window, for details see
180
* http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
181
for (i = 0, j = 255; i < 128; i++, j--) {
182
float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
183
float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0;
184
float w = 0.5 * (wi * wi + wj * wj);
185
mdct_window[i] = mdct_window[511 - i] = wi / w;
186
mdct_window[j] = mdct_window[511 - j] = wj / w;
187
}
188
}
189
190
void atrac3_free(ATRAC3Context *ctx)
191
{
192
av_freep(&ctx->units);
193
av_freep(&ctx->decoded_bytes_buffer);
194
195
ff_mdct_end(&ctx->mdct_ctx);
196
av_freep(&ctx);
197
}
198
199
/**
200
* Mantissa decoding
201
*
202
* @param selector which table the output values are coded with
203
* @param coding_flag constant length coding or variable length coding
204
* @param mantissas mantissa output table
205
* @param num_codes number of values to get
206
*/
207
static void read_quant_spectral_coeffs(GetBitContext *gb, int selector,
208
int coding_flag, int *mantissas,
209
int num_codes)
210
{
211
int i, code, huff_symb;
212
213
if (selector == 1)
214
num_codes /= 2;
215
216
if (coding_flag != 0) {
217
/* constant length coding (CLC) */
218
int num_bits = clc_length_tab[selector];
219
220
if (selector > 1) {
221
for (i = 0; i < num_codes; i++) {
222
if (num_bits)
223
code = get_sbits(gb, num_bits);
224
else
225
code = 0;
226
mantissas[i] = code;
227
}
228
} else {
229
for (i = 0; i < num_codes; i++) {
230
if (num_bits)
231
code = get_bits(gb, num_bits); // num_bits is always 4 in this case
232
else
233
code = 0;
234
mantissas[i * 2 ] = mantissa_clc_tab[code >> 2];
235
mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3];
236
}
237
}
238
} else {
239
/* variable length coding (VLC) */
240
if (selector != 1) {
241
for (i = 0; i < num_codes; i++) {
242
huff_symb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
243
spectral_coeff_tab[selector-1].bits, 3);
244
huff_symb += 1;
245
code = huff_symb >> 1;
246
if (huff_symb & 1)
247
code = -code;
248
mantissas[i] = code;
249
}
250
} else {
251
for (i = 0; i < num_codes; i++) {
252
huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table,
253
spectral_coeff_tab[selector - 1].bits, 3);
254
mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ];
255
mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1];
256
}
257
}
258
}
259
}
260
261
/**
262
* Restore the quantized band spectrum coefficients
263
*
264
* @return subband count, fix for broken specification/files
265
*/
266
static int decode_spectrum(GetBitContext *gb, float *output)
267
{
268
int num_subbands, coding_mode, i, j, first, last, subband_size;
269
int subband_vlc_index[32], sf_index[32];
270
int mantissas[128];
271
float scale_factor;
272
273
num_subbands = get_bits(gb, 5); // number of coded subbands
274
coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
275
276
/* get the VLC selector table for the subbands, 0 means not coded */
277
for (i = 0; i <= num_subbands; i++)
278
subband_vlc_index[i] = get_bits(gb, 3);
279
280
/* read the scale factor indexes from the stream */
281
for (i = 0; i <= num_subbands; i++) {
282
if (subband_vlc_index[i] != 0)
283
sf_index[i] = get_bits(gb, 6);
284
}
285
286
for (i = 0; i <= num_subbands; i++) {
287
first = subband_tab[i ];
288
last = subband_tab[i + 1];
289
290
subband_size = last - first;
291
292
if (subband_vlc_index[i] != 0) {
293
/* decode spectral coefficients for this subband */
294
/* TODO: This can be done faster is several blocks share the
295
* same VLC selector (subband_vlc_index) */
296
read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode,
297
mantissas, subband_size);
298
299
/* decode the scale factor for this subband */
300
scale_factor = av_atrac_sf_table[sf_index[i]] *
301
inv_max_quant[subband_vlc_index[i]];
302
303
/* inverse quantize the coefficients */
304
for (j = 0; first < last; first++, j++)
305
output[first] = mantissas[j] * scale_factor;
306
} else {
307
/* this subband was not coded, so zero the entire subband */
308
memset(output + first, 0, subband_size * sizeof(*output));
309
}
310
}
311
312
/* clear the subbands that were not coded */
313
first = subband_tab[i];
314
memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output));
315
return num_subbands;
316
}
317
318
/**
319
* Restore the quantized tonal components
320
*
321
* @param components tonal components
322
* @param num_bands number of coded bands
323
*/
324
static int decode_tonal_components(GetBitContext *gb,
325
TonalComponent *components, int num_bands)
326
{
327
int i, b, c, m;
328
int nb_components, coding_mode_selector, coding_mode;
329
int band_flags[4], mantissa[8];
330
int component_count = 0;
331
332
nb_components = get_bits(gb, 5);
333
334
/* no tonal components */
335
if (nb_components == 0)
336
return 0;
337
338
coding_mode_selector = get_bits(gb, 2);
339
if (coding_mode_selector == 2)
340
return AVERROR_INVALIDDATA;
341
342
coding_mode = coding_mode_selector & 1;
343
344
for (i = 0; i < nb_components; i++) {
345
int coded_values_per_component, quant_step_index;
346
347
for (b = 0; b <= num_bands; b++)
348
band_flags[b] = get_bits1(gb);
349
350
coded_values_per_component = get_bits(gb, 3);
351
352
quant_step_index = get_bits(gb, 3);
353
if (quant_step_index <= 1)
354
return AVERROR_INVALIDDATA;
355
356
if (coding_mode_selector == 3)
357
coding_mode = get_bits1(gb);
358
359
for (b = 0; b < (num_bands + 1) * 4; b++) {
360
int coded_components;
361
362
if (band_flags[b >> 2] == 0)
363
continue;
364
365
coded_components = get_bits(gb, 3);
366
367
for (c = 0; c < coded_components; c++) {
368
TonalComponent *cmp = &components[component_count];
369
int sf_index, coded_values, max_coded_values;
370
float scale_factor;
371
372
sf_index = get_bits(gb, 6);
373
if (component_count >= 64)
374
return AVERROR_INVALIDDATA;
375
376
cmp->pos = b * 64 + get_bits(gb, 6);
377
378
max_coded_values = SAMPLES_PER_FRAME - cmp->pos;
379
coded_values = coded_values_per_component + 1;
380
coded_values = FFMIN(max_coded_values, coded_values);
381
382
scale_factor = av_atrac_sf_table[sf_index] *
383
inv_max_quant[quant_step_index];
384
385
read_quant_spectral_coeffs(gb, quant_step_index, coding_mode,
386
mantissa, coded_values);
387
388
cmp->num_coefs = coded_values;
389
390
/* inverse quant */
391
for (m = 0; m < coded_values; m++)
392
cmp->coef[m] = mantissa[m] * scale_factor;
393
394
component_count++;
395
}
396
}
397
}
398
399
return component_count;
400
}
401
402
/**
403
* Decode gain parameters for the coded bands
404
*
405
* @param block the gainblock for the current band
406
* @param num_bands amount of coded bands
407
*/
408
static int decode_gain_control(GetBitContext *gb, GainBlock *block,
409
int num_bands)
410
{
411
int b, j;
412
int *level, *loc;
413
414
AtracGainInfo *gain = block->g_block;
415
416
for (b = 0; b <= num_bands; b++) {
417
gain[b].num_points = get_bits(gb, 3);
418
level = gain[b].lev_code;
419
loc = gain[b].loc_code;
420
421
for (j = 0; j < gain[b].num_points; j++) {
422
level[j] = get_bits(gb, 4);
423
loc[j] = get_bits(gb, 5);
424
if (j && loc[j] <= loc[j - 1])
425
return AVERROR_INVALIDDATA;
426
}
427
}
428
429
/* Clear the unused blocks. */
430
for (; b < 4 ; b++)
431
gain[b].num_points = 0;
432
433
return 0;
434
}
435
436
/**
437
* Combine the tonal band spectrum and regular band spectrum
438
*
439
* @param spectrum output spectrum buffer
440
* @param num_components number of tonal components
441
* @param components tonal components for this band
442
* @return position of the last tonal coefficient
443
*/
444
static int add_tonal_components(float *spectrum, int num_components,
445
TonalComponent *components)
446
{
447
int i, j, last_pos = -1;
448
float *input, *output;
449
450
for (i = 0; i < num_components; i++) {
451
last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos);
452
input = components[i].coef;
453
output = &spectrum[components[i].pos];
454
455
for (j = 0; j < components[i].num_coefs; j++)
456
output[j] += input[j];
457
}
458
459
return last_pos;
460
}
461
462
#define INTERPOLATE(old, new, nsample) \
463
((old) + (nsample) * 0.125 * ((new) - (old)))
464
465
static void reverse_matrixing(float *su1, float *su2, int *prev_code,
466
int *curr_code)
467
{
468
int i, nsample, band;
469
float mc1_l, mc1_r, mc2_l, mc2_r;
470
471
for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
472
int s1 = prev_code[i];
473
int s2 = curr_code[i];
474
nsample = band;
475
476
if (s1 != s2) {
477
/* Selector value changed, interpolation needed. */
478
mc1_l = matrix_coeffs[s1 * 2 ];
479
mc1_r = matrix_coeffs[s1 * 2 + 1];
480
mc2_l = matrix_coeffs[s2 * 2 ];
481
mc2_r = matrix_coeffs[s2 * 2 + 1];
482
483
/* Interpolation is done over the first eight samples. */
484
for (; nsample < band + 8; nsample++) {
485
float c1 = su1[nsample];
486
float c2 = su2[nsample];
487
c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) +
488
c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band);
489
su1[nsample] = c2;
490
su2[nsample] = c1 * 2.0 - c2;
491
}
492
}
493
494
/* Apply the matrix without interpolation. */
495
switch (s2) {
496
case 0: /* M/S decoding */
497
for (; nsample < band + 256; nsample++) {
498
float c1 = su1[nsample];
499
float c2 = su2[nsample];
500
su1[nsample] = c2 * 2.0;
501
su2[nsample] = (c1 - c2) * 2.0;
502
}
503
break;
504
case 1:
505
for (; nsample < band + 256; nsample++) {
506
float c1 = su1[nsample];
507
float c2 = su2[nsample];
508
su1[nsample] = (c1 + c2) * 2.0;
509
su2[nsample] = c2 * -2.0;
510
}
511
break;
512
case 2:
513
case 3:
514
for (; nsample < band + 256; nsample++) {
515
float c1 = su1[nsample];
516
float c2 = su2[nsample];
517
su1[nsample] = c1 + c2;
518
su2[nsample] = c1 - c2;
519
}
520
break;
521
default:
522
av_assert1(0);
523
}
524
}
525
}
526
527
static void get_channel_weights(int index, int flag, float ch[2])
528
{
529
if (index == 7) {
530
ch[0] = 1.0;
531
ch[1] = 1.0;
532
} else {
533
ch[0] = (index & 7) / 7.0;
534
ch[1] = sqrt(2 - ch[0] * ch[0]);
535
if (flag)
536
FFSWAP(float, ch[0], ch[1]);
537
}
538
}
539
540
static void channel_weighting(float *su1, float *su2, int *p3)
541
{
542
int band, nsample;
543
/* w[x][y] y=0 is left y=1 is right */
544
float w[2][2];
545
546
if (p3[1] != 7 || p3[3] != 7) {
547
get_channel_weights(p3[1], p3[0], w[0]);
548
get_channel_weights(p3[3], p3[2], w[1]);
549
550
for (band = 256; band < 4 * 256; band += 256) {
551
for (nsample = band; nsample < band + 8; nsample++) {
552
su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band);
553
su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band);
554
}
555
for(; nsample < band + 256; nsample++) {
556
su1[nsample] *= w[1][0];
557
su2[nsample] *= w[1][1];
558
}
559
}
560
}
561
}
562
563
/**
564
* Decode a Sound Unit
565
*
566
* @param snd the channel unit to be used
567
* @param output the decoded samples before IQMF in float representation
568
* @param channel_num channel number
569
* @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono)
570
*/
571
static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb,
572
ChannelUnit *snd, float *output,
573
int channel_num, int coding_mode)
574
{
575
int band, ret, num_subbands, last_tonal, num_bands;
576
GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch];
577
GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch];
578
579
if (coding_mode == JOINT_STEREO && channel_num == 1) {
580
if (get_bits(gb, 2) != 3) {
581
av_log(AV_LOG_ERROR,"JS mono Sound Unit id != 3.");
582
return AVERROR_INVALIDDATA;
583
}
584
} else {
585
if (get_bits(gb, 6) != 0x28) {
586
av_log(AV_LOG_ERROR,"Sound Unit id != 0x28.");
587
return AVERROR_INVALIDDATA;
588
}
589
}
590
591
/* number of coded QMF bands */
592
snd->bands_coded = get_bits(gb, 2);
593
594
ret = decode_gain_control(gb, gain2, snd->bands_coded);
595
if (ret)
596
return ret;
597
598
snd->num_components = decode_tonal_components(gb, snd->components,
599
snd->bands_coded);
600
if (snd->num_components < 0)
601
return snd->num_components;
602
603
num_subbands = decode_spectrum(gb, snd->spectrum);
604
605
/* Merge the decoded spectrum and tonal components. */
606
last_tonal = add_tonal_components(snd->spectrum, snd->num_components,
607
snd->components);
608
609
610
/* calculate number of used MLT/QMF bands according to the amount of coded
611
spectral lines */
612
num_bands = (subband_tab[num_subbands] - 1) >> 8;
613
if (last_tonal >= 0)
614
num_bands = FFMAX((last_tonal + 256) >> 8, num_bands);
615
616
617
/* Reconstruct time domain samples. */
618
for (band = 0; band < 4; band++) {
619
/* Perform the IMDCT step without overlapping. */
620
if (band <= num_bands)
621
imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1);
622
else
623
memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf));
624
625
/* gain compensation and overlapping */
626
ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf,
627
&snd->prev_frame[band * 256],
628
&gain1->g_block[band], &gain2->g_block[band],
629
256, &output[band * 256]);
630
}
631
632
/* Swap the gain control buffers for the next frame. */
633
snd->gc_blk_switch ^= 1;
634
635
return 0;
636
}
637
638
static int decode_frame(ATRAC3Context *q, int block_align, int channels, const uint8_t *databuf,
639
float **out_samples)
640
{
641
int ret, i;
642
uint8_t *ptr1;
643
644
if (q->coding_mode == JOINT_STEREO) {
645
/* channel coupling mode */
646
/* decode Sound Unit 1 */
647
init_get_bits(&q->gb, databuf, block_align * 8);
648
649
ret = decode_channel_sound_unit(q, &q->gb, q->units, out_samples[0], 0,
650
JOINT_STEREO);
651
if (ret != 0)
652
return ret;
653
654
/* Framedata of the su2 in the joint-stereo mode is encoded in
655
* reverse byte order so we need to swap it first. */
656
if (databuf == q->decoded_bytes_buffer) {
657
uint8_t *ptr2 = q->decoded_bytes_buffer + block_align - 1;
658
ptr1 = q->decoded_bytes_buffer;
659
for (i = 0; i < block_align / 2; i++, ptr1++, ptr2--)
660
FFSWAP(uint8_t, *ptr1, *ptr2);
661
} else {
662
const uint8_t *ptr2 = databuf + block_align - 1;
663
for (i = 0; i < block_align; i++)
664
q->decoded_bytes_buffer[i] = *ptr2--;
665
}
666
667
/* Skip the sync codes (0xF8). */
668
ptr1 = q->decoded_bytes_buffer;
669
for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
670
if (i >= block_align)
671
return AVERROR_INVALIDDATA;
672
}
673
674
675
/* set the bitstream reader at the start of the second Sound Unit*/
676
init_get_bits8(&q->gb, ptr1, q->decoded_bytes_buffer + block_align - ptr1);
677
678
/* Fill the Weighting coeffs delay buffer */
679
memmove(q->weighting_delay, &q->weighting_delay[2],
680
4 * sizeof(*q->weighting_delay));
681
q->weighting_delay[4] = get_bits1(&q->gb);
682
q->weighting_delay[5] = get_bits(&q->gb, 3);
683
684
for (i = 0; i < 4; i++) {
685
q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
686
q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
687
q->matrix_coeff_index_next[i] = get_bits(&q->gb, 2);
688
}
689
690
/* Decode Sound Unit 2. */
691
ret = decode_channel_sound_unit(q, &q->gb, &q->units[1],
692
out_samples[1], 1, JOINT_STEREO);
693
if (ret != 0)
694
return ret;
695
696
/* Reconstruct the channel coefficients. */
697
reverse_matrixing(out_samples[0], out_samples[1],
698
q->matrix_coeff_index_prev,
699
q->matrix_coeff_index_now);
700
701
channel_weighting(out_samples[0], out_samples[1], q->weighting_delay);
702
} else {
703
/* normal stereo mode or mono */
704
/* Decode the channel sound units. */
705
for (i = 0; i < channels; i++) {
706
/* Set the bitstream reader at the start of a channel sound unit. */
707
init_get_bits(&q->gb,
708
databuf + i * block_align / channels,
709
block_align * 8 / channels);
710
711
ret = decode_channel_sound_unit(q, &q->gb, &q->units[i],
712
out_samples[i], i, q->coding_mode);
713
if (ret != 0)
714
return ret;
715
}
716
}
717
718
/* Apply the iQMF synthesis filter. */
719
for (i = 0; i < channels; i++) {
720
float *p1 = out_samples[i];
721
float *p2 = p1 + 256;
722
float *p3 = p2 + 256;
723
float *p4 = p3 + 256;
724
ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf);
725
ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf);
726
ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf);
727
}
728
729
return 0;
730
}
731
732
int atrac3_decode_frame(ATRAC3Context *ctx, float *out_data[2], int *nb_samples, const uint8_t *buf, int buf_size)
733
{
734
int ret;
735
const uint8_t *databuf;
736
737
const int block_align = ctx->block_align;
738
const int channels = ctx->channels;
739
740
*nb_samples = 0;
741
742
if (buf_size < block_align) {
743
av_log(AV_LOG_ERROR,
744
"Frame too small (%d bytes). Truncated file?", buf_size);
745
return AVERROR_INVALIDDATA;
746
}
747
748
/* Check if we need to descramble and what buffer to pass on. */
749
if (ctx->scrambled_stream) {
750
decode_bytes(buf, ctx->decoded_bytes_buffer, block_align);
751
databuf = ctx->decoded_bytes_buffer;
752
} else {
753
databuf = buf;
754
}
755
756
ret = decode_frame(ctx, block_align, channels, databuf, out_data);
757
if (ret) {
758
av_log(AV_LOG_ERROR, "Frame decoding error!");
759
return ret;
760
}
761
762
*nb_samples = SAMPLES_PER_FRAME;
763
return block_align;
764
}
765
766
void atrac3_flush_buffers(ATRAC3Context *c) {
767
// There's no known correct way to do this, so let's just reset some stuff.
768
memset(c->temp_buf, 0, sizeof(c->temp_buf));
769
}
770
771
static void atrac3_init_static_data(void)
772
{
773
int i;
774
775
init_imdct_window();
776
ff_atrac_generate_tables();
777
778
/* Initialize the VLC tables. */
779
for (i = 0; i < 7; i++) {
780
spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
781
spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] -
782
atrac3_vlc_offs[i ];
783
init_vlc(&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
784
huff_bits[i], 1, 1,
785
huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
786
}
787
}
788
789
static int static_init_done;
790
791
ATRAC3Context *atrac3_alloc(int channels, int *block_align, const uint8_t *extra_data, int extra_data_size) {
792
int i, ret;
793
int version, delay, samples_per_frame, frame_factor;
794
795
const uint8_t *edata_ptr = extra_data;
796
797
if (channels <= 0 || channels > 2) {
798
av_log(AV_LOG_ERROR, "Channel configuration error!");
799
return nullptr;
800
}
801
802
ATRAC3Context *q = (ATRAC3Context *)av_mallocz(sizeof(ATRAC3Context));
803
q->channels = channels;
804
if (*block_align) {
805
q->block_align = *block_align;
806
} else {
807
// Atrac3 (unlike Atrac3+) requires a specified block align.
808
atrac3_free(q);
809
return nullptr;
810
}
811
812
if (!static_init_done)
813
atrac3_init_static_data();
814
static_init_done = 1;
815
816
/* Take care of the codec-specific extradata. */
817
if (extra_data_size == 14) {
818
/* Parse the extradata, WAV format */
819
av_log(AV_LOG_DEBUG, "[0-1] %d",
820
bytestream_get_le16(&edata_ptr)); // Unknown value always 1
821
edata_ptr += 4; // samples per channel
822
q->coding_mode = bytestream_get_le16(&edata_ptr);
823
av_log(AV_LOG_DEBUG,"[8-9] %d",
824
bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
825
frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
826
av_log(AV_LOG_DEBUG,"[12-13] %d",
827
bytestream_get_le16(&edata_ptr)); // Unknown always 0
828
829
/* setup */
830
samples_per_frame = SAMPLES_PER_FRAME * channels;
831
version = 4;
832
delay = 0x88E;
833
q->coding_mode = q->coding_mode ? JOINT_STEREO : STEREO;
834
q->scrambled_stream = 0;
835
836
if (q->block_align != 96 * channels * frame_factor &&
837
q->block_align != 152 * channels * frame_factor &&
838
q->block_align != 192 * channels * frame_factor) {
839
av_log(AV_LOG_ERROR, "Unknown frame/channel/frame_factor "
840
"configuration %d/%d/%d", block_align,
841
channels, frame_factor);
842
atrac3_free(q);
843
return nullptr;
844
}
845
} else if (extra_data_size == 12 || extra_data_size == 10) {
846
/* Parse the extradata, RM format. */
847
version = bytestream_get_be32(&edata_ptr);
848
samples_per_frame = bytestream_get_be16(&edata_ptr);
849
delay = bytestream_get_be16(&edata_ptr);
850
q->coding_mode = bytestream_get_be16(&edata_ptr);
851
q->scrambled_stream = 1;
852
853
} else {
854
av_log(AV_LOG_ERROR, "Unknown extradata size %d.",
855
extra_data_size);
856
atrac3_free(q);
857
return nullptr;
858
}
859
860
/* Check the extradata */
861
862
if (version != 4) {
863
av_log(AV_LOG_ERROR, "Version %d != 4.", version);
864
atrac3_free(q);
865
return nullptr;
866
}
867
868
if (samples_per_frame != SAMPLES_PER_FRAME &&
869
samples_per_frame != SAMPLES_PER_FRAME * 2) {
870
av_log(AV_LOG_ERROR, "Unknown amount of samples per frame %d.",
871
samples_per_frame);
872
atrac3_free(q);
873
return nullptr;
874
}
875
876
if (delay != 0x88E) {
877
av_log(AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.",
878
delay);
879
atrac3_free(q);
880
return nullptr;
881
}
882
883
if (q->coding_mode == STEREO)
884
av_log(AV_LOG_DEBUG, "Normal stereo detected.");
885
else if (q->coding_mode == JOINT_STEREO) {
886
if (channels != 2) {
887
av_log(AV_LOG_ERROR, "Invalid coding mode");
888
atrac3_free(q);
889
return nullptr;
890
}
891
av_log(AV_LOG_DEBUG, "Joint stereo detected.");
892
} else {
893
av_log(AV_LOG_ERROR, "Unknown channel coding mode %x!",
894
q->coding_mode);
895
atrac3_free(q);
896
return nullptr;
897
}
898
899
q->decoded_bytes_buffer = (uint8_t *)av_mallocz(FFALIGN(q->block_align, 4) + AV_INPUT_BUFFER_PADDING_SIZE);
900
901
/* initialize the MDCT transform */
902
if ((ret = ff_mdct_init(&q->mdct_ctx, 9, 1, 1.0 / 32768)) < 0) {
903
av_log(AV_LOG_ERROR, "Error initializing MDCT");
904
av_freep(&q->decoded_bytes_buffer);
905
906
return nullptr;
907
}
908
909
/* init the joint-stereo decoding data */
910
q->weighting_delay[0] = 0;
911
q->weighting_delay[1] = 7;
912
q->weighting_delay[2] = 0;
913
q->weighting_delay[3] = 7;
914
q->weighting_delay[4] = 0;
915
q->weighting_delay[5] = 7;
916
917
for (i = 0; i < 4; i++) {
918
q->matrix_coeff_index_prev[i] = 3;
919
q->matrix_coeff_index_now[i] = 3;
920
q->matrix_coeff_index_next[i] = 3;
921
}
922
923
ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3);
924
925
q->units = (ChannelUnit *)av_mallocz_array(channels, sizeof(*q->units));
926
return q;
927
}
928
929