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
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdio.h>
38
47
50
51 #define JOINT_STEREO 0x12
53
54 #define SAMPLES_PER_FRAME 1024
56
60
66
74
77
82
85 //@{
86 /** stream data */
88
90 //@}
91 //@{
92 /** joint-stereo related variables */
97 //@}
98 //@{
99 /** data buffers */
102 //@}
103 //@{
104 /** extradata */
106 //@}
107
112
116
117 /**
118 * Regular 512 points IMDCT without overlapping, with the exception of the
119 * swapping of odd bands caused by the reverse spectra of the QMF.
120 *
121 * @param odd_band 1 if the band is an odd band
122 */
124 {
125 int i;
126
127 if (odd_band) {
128 /**
129 * Reverse the odd bands before IMDCT, this is an effect of the QMF
130 * transform or it gives better compression to do it this way.
131 * FIXME: It should be possible to handle this in imdct_calc
132 * for that to happen a modification of the prerotation step of
133 * all SIMD code and C code is needed.
134 * Or fix the functions before so they generate a pre reversed spectrum.
135 */
136 for (i = 0; i < 128; i++)
137 FFSWAP(
float, input[i], input[255 - i]);
138 }
139
141
142 /* Perform windowing on the output. */
144 }
145
146 /*
147 * indata descrambling, only used for data coming from the rm container
148 */
150 {
151 int i, off;
154 uint32_t *output = (uint32_t *)out;
155
156 off = (intptr_t)input & 3;
157 buf = (const uint32_t *)(input - off);
158 if (off)
159 c =
av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
160 else
162 bytes += 3 + off;
163 for (i = 0; i < bytes / 4; i++)
164 output[i] = c ^ buf[i];
165
166 if (off)
168
169 return off;
170 }
171
173 {
174 int i, j;
175
176 /* generate the mdct window, for details see
177 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
178 for (i = 0, j = 255; i < 128; i++, j--) {
179 float wi = sin(((i + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
180 float wj = sin(((j + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
181 float w = 0.5 * (wi * wi + wj * wj);
184 }
185 }
186
188 {
190
194
196
197 return 0;
198 }
199
200 /**
201 * Mantissa decoding
202 *
203 * @param selector which table the output values are coded with
204 * @param coding_flag constant length coding or variable length coding
205 * @param mantissas mantissa output table
206 * @param num_codes number of values to get
207 */
209 int coding_flag, int *mantissas,
210 int num_codes)
211 {
212 int i, code, huff_symb;
213
214 if (selector == 1)
215 num_codes /= 2;
216
217 if (coding_flag != 0) {
218 /* constant length coding (CLC) */
220
221 if (selector > 1) {
222 for (i = 0; i < num_codes; i++) {
223 if (num_bits)
225 else
226 code = 0;
227 mantissas[i] = code;
228 }
229 } else {
230 for (i = 0; i < num_codes; i++) {
231 if (num_bits)
232 code =
get_bits(gb, num_bits);
// num_bits is always 4 in this case
233 else
234 code = 0;
237 }
238 }
239 } else {
240 /* variable length coding (VLC) */
241 if (selector != 1) {
242 for (i = 0; i < num_codes; i++) {
243 huff_symb =
get_vlc2(gb, spectral_coeff_tab[selector-1].
table,
244 spectral_coeff_tab[selector-1].
bits, 3);
245 huff_symb += 1;
246 code = huff_symb >> 1;
247 if (huff_symb & 1)
248 code = -code;
249 mantissas[i] = code;
250 }
251 } else {
252 for (i = 0; i < num_codes; i++) {
253 huff_symb =
get_vlc2(gb, spectral_coeff_tab[selector - 1].
table,
254 spectral_coeff_tab[selector - 1].
bits, 3);
257 }
258 }
259 }
260 }
261
262 /**
263 * Restore the quantized band spectrum coefficients
264 *
265 * @return subband count, fix for broken specification/files
266 */
268 {
269 int num_subbands, coding_mode, i, j, first, last, subband_size;
270 int subband_vlc_index[32], sf_index[32];
271 int mantissas[128];
272 float scale_factor;
273
274 num_subbands =
get_bits(gb, 5);
// number of coded subbands
275 coding_mode =
get_bits1(gb);
// coding Mode: 0 - VLC/ 1-CLC
276
277 /* get the VLC selector table for the subbands, 0 means not coded */
278 for (i = 0; i <= num_subbands; i++)
279 subband_vlc_index[i] =
get_bits(gb, 3);
280
281 /* read the scale factor indexes from the stream */
282 for (i = 0; i <= num_subbands; i++) {
283 if (subband_vlc_index[i] != 0)
285 }
286
287 for (i = 0; i <= num_subbands; i++) {
290
291 subband_size = last - first;
292
293 if (subband_vlc_index[i] != 0) {
294 /* decode spectral coefficients for this subband */
295 /* TODO: This can be done faster is several blocks share the
296 * same VLC selector (subband_vlc_index) */
298 mantissas, subband_size);
299
300 /* decode the scale factor for this subband */
303
304 /* inverse quantize the coefficients */
305 for (j = 0; first < last; first++, j++)
306 output[first] = mantissas[j] * scale_factor;
307 } else {
308 /* this subband was not coded, so zero the entire subband */
309 memset(output + first, 0, subband_size * sizeof(*output));
310 }
311 }
312
313 /* clear the subbands that were not coded */
316 return num_subbands;
317 }
318
319 /**
320 * Restore the quantized tonal components
321 *
322 * @param components tonal components
323 * @param num_bands number of coded bands
324 */
327 {
329 int nb_components, coding_mode_selector, coding_mode;
330 int band_flags[4], mantissa[8];
331 int component_count = 0;
332
334
335 /* no tonal components */
336 if (nb_components == 0)
337 return 0;
338
339 coding_mode_selector =
get_bits(gb, 2);
340 if (coding_mode_selector == 2)
342
343 coding_mode = coding_mode_selector & 1;
344
345 for (i = 0; i < nb_components; i++) {
346 int coded_values_per_component, quant_step_index;
347
348 for (b = 0; b <= num_bands; b++)
350
351 coded_values_per_component =
get_bits(gb, 3);
352
354 if (quant_step_index <= 1)
356
357 if (coding_mode_selector == 3)
359
360 for (b = 0; b < (num_bands + 1) * 4; b++) {
361 int coded_components;
362
363 if (band_flags[b >> 2] == 0)
364 continue;
365
367
368 for (c = 0; c < coded_components; c++) {
370 int sf_index, coded_values, max_coded_values;
371 float scale_factor;
372
374 if (component_count >= 64)
376
378
380 coded_values = coded_values_per_component + 1;
381 coded_values =
FFMIN(max_coded_values, coded_values);
382
385
387 mantissa, coded_values);
388
390
391 /* inverse quant */
392 for (m = 0; m < coded_values; m++)
393 cmp->
coef[m] = mantissa[m] * scale_factor;
394
395 component_count++;
396 }
397 }
398 }
399
400 return component_count;
401 }
402
403 /**
404 * Decode gain parameters for the coded bands
405 *
406 * @param block the gainblock for the current band
407 * @param num_bands amount of coded bands
408 */
410 int num_bands)
411 {
414
416
417 for (b = 0; b <= num_bands; b++) {
421
425 if (j && loc[j] <= loc[j - 1])
427 }
428 }
429
430 /* Clear the unused blocks. */
431 for (; b < 4 ; b++)
432 gain[b].num_points = 0;
433
434 return 0;
435 }
436
437 /**
438 * Combine the tonal band spectrum and regular band spectrum
439 *
440 * @param spectrum output spectrum buffer
441 * @param num_components number of tonal components
442 * @param components tonal components for this band
443 * @return position of the last tonal coefficient
444 */
447 {
448 int i, j, last_pos = -1;
449 float *input, *output;
450
451 for (i = 0; i < num_components; i++) {
452 last_pos =
FFMAX(components[i].pos + components[i].num_coefs, last_pos);
453 input = components[i].
coef;
454 output = &spectrum[components[i].
pos];
455
456 for (j = 0; j < components[i].num_coefs; j++)
457 output[j] += input[j];
458 }
459
460 return last_pos;
461 }
462
463 #define INTERPOLATE(old, new, nsample) \
464 ((old) + (nsample) * 0.125 * ((new) - (old)))
465
467 int *curr_code)
468 {
469 int i, nsample,
band;
470 float mc1_l, mc1_r, mc2_l, mc2_r;
471
472 for (i = 0, band = 0; band < 4 * 256; band += 256, i++) {
473 int s1 = prev_code[i];
474 int s2 = curr_code[i];
476
477 if (s1 != s2) {
478 /* Selector value changed, interpolation needed. */
483
484 /* Interpolation is done over the first eight samples. */
485 for (; nsample < band + 8; nsample++) {
486 float c1 = su1[nsample];
487 float c2 = su2[nsample];
488 c2 = c1 *
INTERPOLATE(mc1_l, mc2_l, nsample - band) +
491 su2[nsample] = c1 * 2.0 -
c2;
492 }
493 }
494
495 /* Apply the matrix without interpolation. */
496 switch (s2) {
497 case 0: /* M/S decoding */
498 for (; nsample < band + 256; nsample++) {
499 float c1 = su1[nsample];
500 float c2 = su2[nsample];
501 su1[nsample] = c2 * 2.0;
502 su2[nsample] = (c1 -
c2) * 2.0;
503 }
504 break;
505 case 1:
506 for (; nsample < band + 256; nsample++) {
507 float c1 = su1[nsample];
508 float c2 = su2[nsample];
509 su1[nsample] = (c1 +
c2) * 2.0;
510 su2[nsample] = c2 * -2.0;
511 }
512 break;
513 case 2:
514 case 3:
515 for (; nsample < band + 256; nsample++) {
516 float c1 = su1[nsample];
517 float c2 = su2[nsample];
518 su1[nsample] = c1 +
c2;
519 su2[nsample] = c1 -
c2;
520 }
521 break;
522 default:
524 }
525 }
526 }
527
529 {
530 if (index == 7) {
531 ch[0] = 1.0;
532 ch[1] = 1.0;
533 } else {
534 ch[0] = (index & 7) / 7.0;
535 ch[1] = sqrt(2 - ch[0] * ch[0]);
536 if (flag)
537 FFSWAP(
float, ch[0], ch[1]);
538 }
539 }
540
542 {
544 /* w[x][y] y=0 is left y=1 is right */
545 float w[2][2];
546
547 if (p3[1] != 7 || p3[3] != 7) {
550
551 for (band = 256; band < 4 * 256; band += 256) {
552 for (nsample = band; nsample < band + 8; nsample++) {
553 su1[nsample] *=
INTERPOLATE(w[0][0], w[0][1], nsample - band);
554 su2[nsample] *=
INTERPOLATE(w[1][0], w[1][1], nsample - band);
555 }
556 for(; nsample < band + 256; nsample++) {
557 su1[nsample] *= w[1][0];
558 su2[nsample] *= w[1][1];
559 }
560 }
561 }
562 }
563
564 /**
565 * Decode a Sound Unit
566 *
567 * @param snd the channel unit to be used
568 * @param output the decoded samples before IQMF in float representation
569 * @param channel_num channel number
570 * @param coding_mode the coding mode (JOINT_STEREO or regular stereo/mono)
571 */
574 int channel_num, int coding_mode)
575 {
576 int band,
ret, num_subbands, last_tonal, num_bands;
579
584 }
585 } else {
589 }
590 }
591
592 /* number of coded QMF bands */
594
596 if (ret)
598
603
605
606 /* Merge the decoded spectrum and tonal components. */
609
610
611 /* calculate number of used MLT/QMF bands according to the amount of coded
612 spectral lines */
614 if (last_tonal >= 0)
615 num_bands =
FFMAX((last_tonal + 256) >> 8, num_bands);
616
617
618 /* Reconstruct time domain samples. */
619 for (band = 0; band < 4; band++) {
620 /* Perform the IMDCT step without overlapping. */
621 if (band <= num_bands)
623 else
625
626 /* gain compensation and overlapping */
630 256, &output[band * 256]);
631 }
632
633 /* Swap the gain control buffers for the next frame. */
635
636 return 0;
637 }
638
640 float **out_samples)
641 {
645
647 /* channel coupling mode */
648 /* decode Sound Unit 1 */
650
653 if (ret != 0)
655
656 /* Framedata of the su2 in the joint-stereo mode is encoded in
657 * reverse byte order so we need to swap it first. */
661 for (i = 0; i < avctx->
block_align / 2; i++, ptr1++, ptr2--)
663 } else {
667 }
668
669 /* Skip the sync codes (0xF8). */
671 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
674 }
675
676
677 /* set the bitstream reader at the start of the second Sound Unit*/
679
680 /* Fill the Weighting coeffs delay buffer */
685
686 for (i = 0; i < 4; i++) {
690 }
691
692 /* Decode Sound Unit 2. */
695 if (ret != 0)
697
698 /* Reconstruct the channel coefficients. */
702
704 } else {
705 /* normal stereo mode or mono */
706 /* Decode the channel sound units. */
707 for (i = 0; i < avctx->
channels; i++) {
708 /* Set the bitstream reader at the start of a channel sound unit. */
712
715 if (ret != 0)
717 }
718 }
719
720 /* Apply the iQMF synthesis filter. */
721 for (i = 0; i < avctx->
channels; i++) {
722 float *p1 = out_samples[i];
723 float *p2 = p1 + 256;
724 float *p3 = p2 + 256;
725 float *p4 = p3 + 256;
729 }
730
731 return 0;
732 }
733
735 int *got_frame_ptr,
AVPacket *avpkt)
736 {
739 int buf_size = avpkt->
size;
743
744 if (buf_size < avctx->block_align) {
746 "Frame too small (%d bytes). Truncated file?\n", buf_size);
748 }
749
750 /* get output buffer */
754
755 /* Check if we need to descramble and what buffer to pass on. */
759 } else {
761 }
762
764 if (ret) {
767 }
768
769 *got_frame_ptr = 1;
770
772 }
773
775 {
776 int i;
777
780
781 /* Initialize the VLC tables. */
782 for (i = 0; i < 7; i++) {
785 atrac3_vlc_offs[i ];
789 }
790 }
791
793 {
794 static int static_init_done;
796 int version, delay, samples_per_frame, frame_factor;
799
803 }
804
805 if (!static_init_done)
807 static_init_done = 1;
808
809 /* Take care of the codec-specific extradata. */
811 /* Parse the extradata, WAV format */
813 bytestream_get_le16(&edata_ptr)); // Unknown value always 1
814 edata_ptr += 4; // samples per channel
817 bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
818 frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
820 bytestream_get_le16(&edata_ptr)); // Unknown always 0
821
822 /* setup */
824 version = 4;
825 delay = 0x88E;
828
836 }
838 /* Parse the extradata, RM format. */
839 version = bytestream_get_be32(&edata_ptr);
840 samples_per_frame = bytestream_get_be16(&edata_ptr);
841 delay = bytestream_get_be16(&edata_ptr);
844
845 } else {
849 }
850
851 /* Check the extradata */
852
853 if (version != 4) {
856 }
857
861 samples_per_frame);
863 }
864
865 if (delay != 0x88E) {
867 delay);
869 }
870
877 }
879 } else {
883 }
884
887
892
894
895 /* initialize the MDCT transform */
900 }
901
902 /* init the joint-stereo decoding data */
909
910 for (i = 0; i < 4; i++) {
914 }
915
918
923 }
924
925 return 0;
926 }
927
940 };