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