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