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
45
51
54
55 #define MIN_CHANNELS 1
56 #define MAX_CHANNELS 8
57 #define MAX_JS_PAIRS 8 / 2
58
59 #define JOINT_STEREO 0x12
61
62 #define SAMPLES_PER_FRAME 1024
64
65 #define ATRAC3_VLC_BITS 8
66
70
76
84
87
92
95 //@{
96 /** stream data */
98
100 //@}
101 //@{
102 /** joint-stereo related variables */
107 //@}
108 //@{
109 /** data buffers */
112 //@}
113 //@{
114 /** extradata */
116 //@}
117
124
128
129 /**
130 * Regular 512 points IMDCT without overlapping, with the exception of the
131 * swapping of odd bands caused by the reverse spectra of the QMF.
132 *
133 * @param odd_band 1 if the band is an odd band
134 */
136 {
138
139 if (odd_band) {
140 /**
141 * Reverse the odd bands before IMDCT, this is an effect of the QMF
142 * transform or it gives better compression to do it this way.
143 * FIXME: It should be possible to handle this in imdct_calc
144 * for that to happen a modification of the prerotation step of
145 * all SIMD code and C code is needed.
146 * Or fix the functions before so they generate a pre reversed spectrum.
147 */
148 for (
i = 0;
i < 128;
i++)
150 }
151
153
154 /* Perform windowing on the output. */
156 }
157
158 /*
159 * indata descrambling, only used for data coming from the rm container
160 */
162 {
165 const uint32_t *buf;
167
168 off = (intptr_t)
input & 3;
169 buf = (
const uint32_t *)(
input - off);
170 if (off)
171 c =
av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
172 else
174 bytes += 3 + off;
175 for (
i = 0;
i < bytes / 4;
i++)
177
178 if (off)
180
181 return off;
182 }
183
185 {
187
188 /* generate the mdct window, for details see
189 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
190 for (
i = 0, j = 255;
i < 128;
i++, j--) {
191 float wi = sin(((
i + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
192 float wj = sin(((j + 0.5) / 256.0 - 0.5) *
M_PI) + 1.0;
193 float w = 0.5 * (wi * wi + wj * wj);
196 }
197 }
198
200 {
202
205
207
208 return 0;
209 }
210
211 /**
212 * Mantissa decoding
213 *
214 * @param selector which table the output values are coded with
215 * @param coding_flag constant length coding or variable length coding
216 * @param mantissas mantissa output table
217 * @param num_codes number of values to get
218 */
220 int coding_flag, int *mantissas,
221 int num_codes)
222 {
223 int i,
code, huff_symb;
224
225 if (selector == 1)
226 num_codes /= 2;
227
228 if (coding_flag != 0) {
229 /* constant length coding (CLC) */
231
232 if (selector > 1) {
233 for (
i = 0;
i < num_codes;
i++) {
234 if (num_bits)
236 else
239 }
240 } else {
241 for (
i = 0;
i < num_codes;
i++) {
242 if (num_bits)
243 code =
get_bits(gb, num_bits);
// num_bits is always 4 in this case
244 else
248 }
249 }
250 } else {
251 /* variable length coding (VLC) */
252 if (selector != 1) {
253 for (
i = 0;
i < num_codes;
i++) {
256 }
257 } else {
258 for (
i = 0;
i < num_codes;
i++) {
263 }
264 }
265 }
266 }
267
268 /**
269 * Restore the quantized band spectrum coefficients
270 *
271 * @return subband count, fix for broken specification/files
272 */
274 {
275 int num_subbands, coding_mode,
i, j,
first, last, subband_size;
276 int subband_vlc_index[32], sf_index[32];
277 int mantissas[128];
278 float scale_factor;
279
280 num_subbands =
get_bits(gb, 5);
// number of coded subbands
281 coding_mode =
get_bits1(gb);
// coding Mode: 0 - VLC/ 1-CLC
282
283 /* get the VLC selector table for the subbands, 0 means not coded */
284 for (
i = 0;
i <= num_subbands;
i++)
286
287 /* read the scale factor indexes from the stream */
288 for (
i = 0;
i <= num_subbands;
i++) {
289 if (subband_vlc_index[
i] != 0)
291 }
292
293 for (
i = 0;
i <= num_subbands;
i++) {
296
297 subband_size = last -
first;
298
299 if (subband_vlc_index[
i] != 0) {
300 /* decode spectral coefficients for this subband */
301 /* TODO: This can be done faster is several blocks share the
302 * same VLC selector (subband_vlc_index) */
304 mantissas, subband_size);
305
306 /* decode the scale factor for this subband */
309
310 /* inverse quantize the coefficients */
313 } else {
314 /* this subband was not coded, so zero the entire subband */
316 }
317 }
318
319 /* clear the subbands that were not coded */
322 return num_subbands;
323 }
324
325 /**
326 * Restore the quantized tonal components
327 *
328 * @param components tonal components
329 * @param num_bands number of coded bands
330 */
333 {
335 int nb_components, coding_mode_selector, coding_mode;
336 int band_flags[4], mantissa[8];
337 int component_count = 0;
338
340
341 /* no tonal components */
342 if (nb_components == 0)
343 return 0;
344
345 coding_mode_selector =
get_bits(gb, 2);
346 if (coding_mode_selector == 2)
348
349 coding_mode = coding_mode_selector & 1;
350
351 for (
i = 0;
i < nb_components;
i++) {
352 int coded_values_per_component, quant_step_index;
353
354 for (
b = 0;
b <= num_bands;
b++)
356
357 coded_values_per_component =
get_bits(gb, 3);
358
360 if (quant_step_index <= 1)
362
363 if (coding_mode_selector == 3)
365
366 for (
b = 0;
b < (num_bands + 1) * 4;
b++) {
367 int coded_components;
368
369 if (band_flags[
b >> 2] == 0)
370 continue;
371
373
374 for (
c = 0;
c < coded_components;
c++) {
376 int sf_index, coded_values, max_coded_values;
377 float scale_factor;
378
380 if (component_count >= 64)
382
384
386 coded_values = coded_values_per_component + 1;
387 coded_values =
FFMIN(max_coded_values, coded_values);
388
391
393 mantissa, coded_values);
394
395 cmp->num_coefs = coded_values;
396
397 /* inverse quant */
398 for (m = 0; m < coded_values; m++)
399 cmp->coef[m] = mantissa[m] * scale_factor;
400
401 component_count++;
402 }
403 }
404 }
405
406 return component_count;
407 }
408
409 /**
410 * Decode gain parameters for the coded bands
411 *
412 * @param block the gainblock for the current band
413 * @param num_bands amount of coded bands
414 */
416 int num_bands)
417 {
420
422
423 for (
b = 0;
b <= num_bands;
b++) {
427
431 if (j && loc[j] <= loc[j - 1])
433 }
434 }
435
436 /* Clear the unused blocks. */
438 gain[
b].num_points = 0;
439
440 return 0;
441 }
442
443 /**
444 * Combine the tonal band spectrum and regular band spectrum
445 *
446 * @param spectrum output spectrum buffer
447 * @param num_components number of tonal components
448 * @param components tonal components for this band
449 * @return position of the last tonal coefficient
450 */
453 {
454 int i, j, last_pos = -1;
456
457 for (
i = 0;
i < num_components;
i++) {
458 last_pos =
FFMAX(components[
i].
pos + components[
i].num_coefs, last_pos);
461
462 for (j = 0; j < components[
i].num_coefs; j++)
464 }
465
466 return last_pos;
467 }
468
469 #define INTERPOLATE(old, new, nsample) \
470 ((old) + (nsample) * 0.125 * ((new) - (old)))
471
473 int *curr_code)
474 {
475 int i, nsample, band;
476 float mc1_l, mc1_r, mc2_l, mc2_r;
477
478 for (
i = 0, band = 0; band < 4 * 256; band += 256,
i++) {
479 int s1 = prev_code[
i];
480 int s2 = curr_code[
i];
481 nsample = band;
482
483 if (s1 != s2) {
484 /* Selector value changed, interpolation needed. */
489
490 /* Interpolation is done over the first eight samples. */
491 for (; nsample < band + 8; nsample++) {
492 float c1 = su1[nsample];
493 float c2 = su2[nsample];
497 su2[nsample] =
c1 * 2.0 -
c2;
498 }
499 }
500
501 /* Apply the matrix without interpolation. */
502 switch (s2) {
503 case 0: /* M/S decoding */
504 for (; nsample < band + 256; nsample++) {
505 float c1 = su1[nsample];
506 float c2 = su2[nsample];
507 su1[nsample] =
c2 * 2.0;
508 su2[nsample] = (
c1 -
c2) * 2.0;
509 }
510 break;
511 case 1:
512 for (; nsample < band + 256; nsample++) {
513 float c1 = su1[nsample];
514 float c2 = su2[nsample];
515 su1[nsample] = (
c1 +
c2) * 2.0;
516 su2[nsample] =
c2 * -2.0;
517 }
518 break;
519 case 2:
520 case 3:
521 for (; nsample < band + 256; nsample++) {
522 float c1 = su1[nsample];
523 float c2 = su2[nsample];
524 su1[nsample] =
c1 +
c2;
525 su2[nsample] =
c1 -
c2;
526 }
527 break;
528 default:
529 av_unreachable(
"curr_code/matrix_coeff_index_* values are stored in two bits");
530 }
531 }
532 }
533
535 {
537 ch[0] = 1.0;
538 ch[1] = 1.0;
539 } else {
540 ch[0] = (
index & 7) / 7.0;
541 ch[1] = sqrt(2 - ch[0] * ch[0]);
543 FFSWAP(
float, ch[0], ch[1]);
544 }
545 }
546
548 {
549 int band, nsample;
550 /* w[x][y] y=0 is left y=1 is right */
552
553 if (p3[1] != 7 || p3[3] != 7) {
556
557 for (band = 256; band < 4 * 256; band += 256) {
558 for (nsample = band; nsample < band + 8; nsample++) {
559 su1[nsample] *=
INTERPOLATE(
w[0][0],
w[0][1], nsample - band);
560 su2[nsample] *=
INTERPOLATE(
w[1][0],
w[1][1], nsample - band);
561 }
562 for(; nsample < band + 256; nsample++) {
563 su1[nsample] *=
w[1][0];
564 su2[nsample] *=
w[1][1];
565 }
566 }
567 }
568 }
569
570 /**
571 * Decode a Sound Unit
572 *
573 * @param snd the channel unit to be used
574 * @param output the decoded samples before IQMF in float representation
575 * @param channel_num channel number
576 * @param coding_mode the coding mode (JOINT_STEREO or single channels)
577 */
580 int channel_num, int coding_mode)
581 {
582 int band,
ret, num_subbands, last_tonal, num_bands;
585
586 if (coding_mode ==
JOINT_STEREO && (channel_num % 2) == 1) {
590 }
591 } else {
595 }
596 }
597
598 /* number of coded QMF bands */
600
604
609
611
612 /* Merge the decoded spectrum and tonal components. */
615
616
617 /* calculate number of used MLT/QMF bands according to the amount of coded
618 spectral lines */
619 num_bands = (
subband_tab[num_subbands + 1] - 1) >> 8;
620 if (last_tonal >= 0)
621 num_bands =
FFMAX((last_tonal + 256) >> 8, num_bands);
622
623
624 /* Reconstruct time domain samples. */
625 for (band = 0; band < 4; band++) {
626 /* Perform the IMDCT step without overlapping. */
627 if (band <= num_bands)
629 else
631
632 /* gain compensation and overlapping */
636 256, &
output[band * 256]);
637 }
638
639 /* Swap the gain control buffers for the next frame. */
641
642 return 0;
643 }
644
646 float **out_samples)
647 {
650 uint8_t *ptr1;
652
654 /* channel coupling mode */
655
656 /* Decode sound unit pairs (channels are expected to be even).
657 * Multichannel joint stereo interleaves pairs (6ch: 2ch + 2ch + 2ch) */
658 const uint8_t *js_databuf;
659 int js_pair, js_block_align;
660
662
663 for (ch = 0; ch <
channels; ch = ch + 2) {
664 js_pair = ch/2;
665 js_databuf = databuf + js_pair * js_block_align; /* align to current pair */
666
667 /* Set the bitstream reader at the start of first channel sound unit. */
669 js_databuf, js_block_align * 8);
670
671 /* decode Sound Unit 1 */
676
677 /* Framedata of the su2 in the joint-stereo mode is encoded in
678 * reverse byte order so we need to swap it first. */
682 for (
i = 0;
i < js_block_align / 2;
i++, ptr1++, ptr2--)
683 FFSWAP(uint8_t, *ptr1, *ptr2);
684 } else {
685 const uint8_t *ptr2 = js_databuf + js_block_align - 1;
686 for (
i = 0;
i < js_block_align;
i++)
688 }
689
690 /* Skip the sync codes (0xF8). */
692 for (
i = 4; *ptr1 == 0xF8;
i++, ptr1++) {
693 if (
i >= js_block_align)
695 }
696
697
698 /* set the bitstream reader at the start of the second Sound Unit */
703
704 /* Fill the Weighting coeffs delay buffer */
709
710 for (
i = 0;
i < 4;
i++) {
714 }
715
716 /* Decode Sound Unit 2. */
721
722 /* Reconstruct the channel coefficients. */
726
728 }
729 } else {
730 /* single channels */
731 /* Decode the channel sound units. */
733 /* Set the bitstream reader at the start of a channel sound unit. */
737
742 }
743 }
744
745 /* Apply the iQMF synthesis filter. */
747 float *p1 = out_samples[
i];
748 float *p2 = p1 + 256;
749 float *p3 = p2 + 256;
750 float *p4 = p3 + 256;
754 }
755
756 return 0;
757 }
758
760 int size,
float **out_samples)
761 {
765
766 /* Set the bitstream reader at the start of a channel sound unit. */
768 /* single channels */
769 /* Decode the channel sound units. */
777 }
778 }
779
780 /* Apply the iQMF synthesis filter. */
782 float *p1 = out_samples[
i];
783 float *p2 = p1 + 256;
784 float *p3 = p2 + 256;
785 float *p4 = p3 + 256;
789 }
790
791 return 0;
792 }
793
795 int *got_frame_ptr,
AVPacket *avpkt)
796 {
797 const uint8_t *buf = avpkt->
data;
798 int buf_size = avpkt->
size;
801 const uint8_t *databuf;
802
803 if (buf_size < avctx->block_align) {
805 "Frame too small (%d bytes). Truncated file?\n", buf_size);
807 }
808
809 /* get output buffer */
813
814 /* Check if we need to descramble and what buffer to pass on. */
818 } else {
819 databuf = buf;
820 }
821
826 }
827
828 *got_frame_ptr = 1;
829
831 }
832
834 int *got_frame_ptr,
AVPacket *avpkt)
835 {
837
841
843 (
float **)
frame->extended_data);
847 }
848
849 *got_frame_ptr = 1;
850
852 }
853
855 {
859
862
863 /* Initialize the VLC tables. */
864 for (
i = 0;
i < 7;
i++) {
868 &hufftabs[0][1], 2,
869 &hufftabs[0][0], 2, 1,
873 }
874 }
875
877 {
880 int version, delay, samples_per_frame, frame_factor;
881 const uint8_t *edata_ptr = avctx->
extradata;
884 float scale = 1.0 / 32768;
886
890 }
891
892 /* Take care of the codec-specific extradata. */
896 delay = 0x88E;
899 /* Parse the extradata, WAV format */
901 bytestream_get_le16(&edata_ptr)); // Unknown value always 1
902 edata_ptr += 4; // samples per channel
905 bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
906 frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1
908 bytestream_get_le16(&edata_ptr)); // Unknown always 0
909
910 /* setup */
913 delay = 0x88E;
916
924 }
926 /* Parse the extradata, RM format. */
927 version = bytestream_get_be32(&edata_ptr);
928 samples_per_frame = bytestream_get_be16(&edata_ptr);
929 delay = bytestream_get_be16(&edata_ptr);
932
933 } else {
937 }
938
939 /* Check the extradata */
940
944 }
945
948 samples_per_frame);
950 }
951
952 if (delay != 0x88E) {
954 delay);
956 }
957
961 if (
channels % 2 == 1) {
/* Joint stereo channels must be even */
964 }
966 } else {
970 }
971
974
979
981
982 /* initialize the MDCT transform */
987 }
988
989 /* init the joint-stereo decoding data */
997
998 for (
i = 0;
i < 4;
i++) {
1002 }
1003 }
1004
1007 if (!fdsp)
1011
1015
1017
1018 return 0;
1019 }
1020
1033 };
1034
1036 .
p.
name =
"atrac3al",
1037 CODEC_LONG_NAME(
"ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"),
1047 };