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