Libav

libavcodec/atrac3.c

Go to the documentation of this file.
00001 /*
00002  * Atrac 3 compatible decoder
00003  * Copyright (c) 2006-2008 Maxim Poliakovski
00004  * Copyright (c) 2006-2008 Benjamin Larsson
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00035 #include <math.h>
00036 #include <stddef.h>
00037 #include <stdio.h>
00038 
00039 #include "avcodec.h"
00040 #include "get_bits.h"
00041 #include "dsputil.h"
00042 #include "bytestream.h"
00043 #include "fft.h"
00044 
00045 #include "atrac.h"
00046 #include "atrac3data.h"
00047 
00048 #define JOINT_STEREO    0x12
00049 #define STEREO          0x2
00050 
00051 
00052 /* These structures are needed to store the parsed gain control data. */
00053 typedef struct {
00054     int   num_gain_data;
00055     int   levcode[8];
00056     int   loccode[8];
00057 } gain_info;
00058 
00059 typedef struct {
00060     gain_info   gBlock[4];
00061 } gain_block;
00062 
00063 typedef struct {
00064     int     pos;
00065     int     numCoefs;
00066     float   coef[8];
00067 } tonal_component;
00068 
00069 typedef struct {
00070     int               bandsCoded;
00071     int               numComponents;
00072     tonal_component   components[64];
00073     float             prevFrame[1024];
00074     int               gcBlkSwitch;
00075     gain_block        gainBlock[2];
00076 
00077     DECLARE_ALIGNED(16, float, spectrum)[1024];
00078     DECLARE_ALIGNED(16, float, IMDCT_buf)[1024];
00079 
00080     float             delayBuf1[46]; 
00081     float             delayBuf2[46];
00082     float             delayBuf3[46];
00083 } channel_unit;
00084 
00085 typedef struct {
00086     GetBitContext       gb;
00088 
00089     int                 channels;
00090     int                 codingMode;
00091     int                 bit_rate;
00092     int                 sample_rate;
00093     int                 samples_per_channel;
00094     int                 samples_per_frame;
00095 
00096     int                 bits_per_frame;
00097     int                 bytes_per_frame;
00098     int                 pBs;
00099     channel_unit*       pUnits;
00101 
00102 
00103     int                 matrix_coeff_index_prev[4];
00104     int                 matrix_coeff_index_now[4];
00105     int                 matrix_coeff_index_next[4];
00106     int                 weighting_delay[6];
00108 
00109 
00110     float               outSamples[2048];
00111     uint8_t*            decoded_bytes_buffer;
00112     float               tempBuf[1070];
00114 
00115 
00116     int                 atrac3version;
00117     int                 delay;
00118     int                 scrambled_stream;
00119     int                 frame_factor;
00121 } ATRAC3Context;
00122 
00123 static DECLARE_ALIGNED(16, float,mdct_window)[512];
00124 static VLC              spectral_coeff_tab[7];
00125 static float            gain_tab1[16];
00126 static float            gain_tab2[31];
00127 static FFTContext       mdct_ctx;
00128 static DSPContext       dsp;
00129 
00130 
00140 static void IMLT(float *pInput, float *pOutput, int odd_band)
00141 {
00142     int     i;
00143 
00144     if (odd_band) {
00154         for (i=0; i<128; i++)
00155             FFSWAP(float, pInput[i], pInput[255-i]);
00156     }
00157 
00158     ff_imdct_calc(&mdct_ctx,pOutput,pInput);
00159 
00160     /* Perform windowing on the output. */
00161     dsp.vector_fmul(pOutput,mdct_window,512);
00162 
00163 }
00164 
00165 
00174 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00175     int i, off;
00176     uint32_t c;
00177     const uint32_t* buf;
00178     uint32_t* obuf = (uint32_t*) out;
00179 
00180     off = (intptr_t)inbuffer & 3;
00181     buf = (const uint32_t*) (inbuffer - off);
00182     c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
00183     bytes += 3 + off;
00184     for (i = 0; i < bytes/4; i++)
00185         obuf[i] = c ^ buf[i];
00186 
00187     if (off)
00188         av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
00189 
00190     return off;
00191 }
00192 
00193 
00194 static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
00195     float enc_window[256];
00196     int i;
00197 
00198     /* Generate the mdct window, for details see
00199      * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
00200     for (i=0 ; i<256; i++)
00201         enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
00202 
00203     if (!mdct_window[0])
00204         for (i=0 ; i<256; i++) {
00205             mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
00206             mdct_window[511-i] = mdct_window[i];
00207         }
00208 
00209     /* Initialize the MDCT transform. */
00210     ff_mdct_init(&mdct_ctx, 9, 1, 1.0);
00211 }
00212 
00217 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
00218 {
00219     ATRAC3Context *q = avctx->priv_data;
00220 
00221     av_free(q->pUnits);
00222     av_free(q->decoded_bytes_buffer);
00223 
00224     return 0;
00225 }
00226 
00237 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
00238 {
00239     int   numBits, cnt, code, huffSymb;
00240 
00241     if (selector == 1)
00242         numCodes /= 2;
00243 
00244     if (codingFlag != 0) {
00245         /* constant length coding (CLC) */
00246         numBits = CLCLengthTab[selector];
00247 
00248         if (selector > 1) {
00249             for (cnt = 0; cnt < numCodes; cnt++) {
00250                 if (numBits)
00251                     code = get_sbits(gb, numBits);
00252                 else
00253                     code = 0;
00254                 mantissas[cnt] = code;
00255             }
00256         } else {
00257             for (cnt = 0; cnt < numCodes; cnt++) {
00258                 if (numBits)
00259                     code = get_bits(gb, numBits); //numBits is always 4 in this case
00260                 else
00261                     code = 0;
00262                 mantissas[cnt*2] = seTab_0[code >> 2];
00263                 mantissas[cnt*2+1] = seTab_0[code & 3];
00264             }
00265         }
00266     } else {
00267         /* variable length coding (VLC) */
00268         if (selector != 1) {
00269             for (cnt = 0; cnt < numCodes; cnt++) {
00270                 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00271                 huffSymb += 1;
00272                 code = huffSymb >> 1;
00273                 if (huffSymb & 1)
00274                     code = -code;
00275                 mantissas[cnt] = code;
00276             }
00277         } else {
00278             for (cnt = 0; cnt < numCodes; cnt++) {
00279                 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00280                 mantissas[cnt*2] = decTable1[huffSymb*2];
00281                 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
00282             }
00283         }
00284     }
00285 }
00286 
00295 static int decodeSpectrum (GetBitContext *gb, float *pOut)
00296 {
00297     int   numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
00298     int   subband_vlc_index[32], SF_idxs[32];
00299     int   mantissas[128];
00300     float SF;
00301 
00302     numSubbands = get_bits(gb, 5); // number of coded subbands
00303     codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
00304 
00305     /* Get the VLC selector table for the subbands, 0 means not coded. */
00306     for (cnt = 0; cnt <= numSubbands; cnt++)
00307         subband_vlc_index[cnt] = get_bits(gb, 3);
00308 
00309     /* Read the scale factor indexes from the stream. */
00310     for (cnt = 0; cnt <= numSubbands; cnt++) {
00311         if (subband_vlc_index[cnt] != 0)
00312             SF_idxs[cnt] = get_bits(gb, 6);
00313     }
00314 
00315     for (cnt = 0; cnt <= numSubbands; cnt++) {
00316         first = subbandTab[cnt];
00317         last = subbandTab[cnt+1];
00318 
00319         subbWidth = last - first;
00320 
00321         if (subband_vlc_index[cnt] != 0) {
00322             /* Decode spectral coefficients for this subband. */
00323             /* TODO: This can be done faster is several blocks share the
00324              * same VLC selector (subband_vlc_index) */
00325             readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
00326 
00327             /* Decode the scale factor for this subband. */
00328             SF = sf_table[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
00329 
00330             /* Inverse quantize the coefficients. */
00331             for (pIn=mantissas ; first<last; first++, pIn++)
00332                 pOut[first] = *pIn * SF;
00333         } else {
00334             /* This subband was not coded, so zero the entire subband. */
00335             memset(pOut+first, 0, subbWidth*sizeof(float));
00336         }
00337     }
00338 
00339     /* Clear the subbands that were not coded. */
00340     first = subbandTab[cnt];
00341     memset(pOut+first, 0, (1024 - first) * sizeof(float));
00342     return numSubbands;
00343 }
00344 
00353 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
00354 {
00355     int i,j,k,cnt;
00356     int   components, coding_mode_selector, coding_mode, coded_values_per_component;
00357     int   sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
00358     int   band_flags[4], mantissa[8];
00359     float  *pCoef;
00360     float  scalefactor;
00361     int   component_count = 0;
00362 
00363     components = get_bits(gb,5);
00364 
00365     /* no tonal components */
00366     if (components == 0)
00367         return 0;
00368 
00369     coding_mode_selector = get_bits(gb,2);
00370     if (coding_mode_selector == 2)
00371         return -1;
00372 
00373     coding_mode = coding_mode_selector & 1;
00374 
00375     for (i = 0; i < components; i++) {
00376         for (cnt = 0; cnt <= numBands; cnt++)
00377             band_flags[cnt] = get_bits1(gb);
00378 
00379         coded_values_per_component = get_bits(gb,3);
00380 
00381         quant_step_index = get_bits(gb,3);
00382         if (quant_step_index <= 1)
00383             return -1;
00384 
00385         if (coding_mode_selector == 3)
00386             coding_mode = get_bits1(gb);
00387 
00388         for (j = 0; j < (numBands + 1) * 4; j++) {
00389             if (band_flags[j >> 2] == 0)
00390                 continue;
00391 
00392             coded_components = get_bits(gb,3);
00393 
00394             for (k=0; k<coded_components; k++) {
00395                 sfIndx = get_bits(gb,6);
00396                 if (component_count >= 64)
00397                     return AVERROR_INVALIDDATA;
00398                 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00399                 max_coded_values = 1024 - pComponent[component_count].pos;
00400                 coded_values = coded_values_per_component + 1;
00401                 coded_values = FFMIN(max_coded_values,coded_values);
00402 
00403                 scalefactor = sf_table[sfIndx] * iMaxQuant[quant_step_index];
00404 
00405                 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00406 
00407                 pComponent[component_count].numCoefs = coded_values;
00408 
00409                 /* inverse quant */
00410                 pCoef = pComponent[component_count].coef;
00411                 for (cnt = 0; cnt < coded_values; cnt++)
00412                     pCoef[cnt] = mantissa[cnt] * scalefactor;
00413 
00414                 component_count++;
00415             }
00416         }
00417     }
00418 
00419     return component_count;
00420 }
00421 
00430 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00431 {
00432     int   i, cf, numData;
00433     int   *pLevel, *pLoc;
00434 
00435     gain_info   *pGain = pGb->gBlock;
00436 
00437     for (i=0 ; i<=numBands; i++)
00438     {
00439         numData = get_bits(gb,3);
00440         pGain[i].num_gain_data = numData;
00441         pLevel = pGain[i].levcode;
00442         pLoc = pGain[i].loccode;
00443 
00444         for (cf = 0; cf < numData; cf++){
00445             pLevel[cf]= get_bits(gb,4);
00446             pLoc  [cf]= get_bits(gb,5);
00447             if(cf && pLoc[cf] <= pLoc[cf-1])
00448                 return -1;
00449         }
00450     }
00451 
00452     /* Clear the unused blocks. */
00453     for (; i<4 ; i++)
00454         pGain[i].num_gain_data = 0;
00455 
00456     return 0;
00457 }
00458 
00469 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00470 {
00471     /* gain compensation function */
00472     float  gain1, gain2, gain_inc;
00473     int   cnt, numdata, nsample, startLoc, endLoc;
00474 
00475 
00476     if (pGain2->num_gain_data == 0)
00477         gain1 = 1.0;
00478     else
00479         gain1 = gain_tab1[pGain2->levcode[0]];
00480 
00481     if (pGain1->num_gain_data == 0) {
00482         for (cnt = 0; cnt < 256; cnt++)
00483             pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00484     } else {
00485         numdata = pGain1->num_gain_data;
00486         pGain1->loccode[numdata] = 32;
00487         pGain1->levcode[numdata] = 4;
00488 
00489         nsample = 0; // current sample = 0
00490 
00491         for (cnt = 0; cnt < numdata; cnt++) {
00492             startLoc = pGain1->loccode[cnt] * 8;
00493             endLoc = startLoc + 8;
00494 
00495             gain2 = gain_tab1[pGain1->levcode[cnt]];
00496             gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00497 
00498             /* interpolate */
00499             for (; nsample < startLoc; nsample++)
00500                 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00501 
00502             /* interpolation is done over eight samples */
00503             for (; nsample < endLoc; nsample++) {
00504                 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00505                 gain2 *= gain_inc;
00506             }
00507         }
00508 
00509         for (; nsample < 256; nsample++)
00510             pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00511     }
00512 
00513     /* Delay for the overlapping part. */
00514     memcpy(pPrev, &pIn[256], 256*sizeof(float));
00515 }
00516 
00526 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00527 {
00528     int   cnt, i, lastPos = -1;
00529     float   *pIn, *pOut;
00530 
00531     for (cnt = 0; cnt < numComponents; cnt++){
00532         lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
00533         pIn = pComponent[cnt].coef;
00534         pOut = &(pSpectrum[pComponent[cnt].pos]);
00535 
00536         for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00537             pOut[i] += pIn[i];
00538     }
00539 
00540     return lastPos;
00541 }
00542 
00543 
00544 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00545 
00546 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00547 {
00548     int    i, band, nsample, s1, s2;
00549     float    c1, c2;
00550     float    mc1_l, mc1_r, mc2_l, mc2_r;
00551 
00552     for (i=0,band = 0; band < 4*256; band+=256,i++) {
00553         s1 = pPrevCode[i];
00554         s2 = pCurrCode[i];
00555         nsample = 0;
00556 
00557         if (s1 != s2) {
00558             /* Selector value changed, interpolation needed. */
00559             mc1_l = matrixCoeffs[s1*2];
00560             mc1_r = matrixCoeffs[s1*2+1];
00561             mc2_l = matrixCoeffs[s2*2];
00562             mc2_r = matrixCoeffs[s2*2+1];
00563 
00564             /* Interpolation is done over the first eight samples. */
00565             for(; nsample < 8; nsample++) {
00566                 c1 = su1[band+nsample];
00567                 c2 = su2[band+nsample];
00568                 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00569                 su1[band+nsample] = c2;
00570                 su2[band+nsample] = c1 * 2.0 - c2;
00571             }
00572         }
00573 
00574         /* Apply the matrix without interpolation. */
00575         switch (s2) {
00576             case 0:     /* M/S decoding */
00577                 for (; nsample < 256; nsample++) {
00578                     c1 = su1[band+nsample];
00579                     c2 = su2[band+nsample];
00580                     su1[band+nsample] = c2 * 2.0;
00581                     su2[band+nsample] = (c1 - c2) * 2.0;
00582                 }
00583                 break;
00584 
00585             case 1:
00586                 for (; nsample < 256; nsample++) {
00587                     c1 = su1[band+nsample];
00588                     c2 = su2[band+nsample];
00589                     su1[band+nsample] = (c1 + c2) * 2.0;
00590                     su2[band+nsample] = c2 * -2.0;
00591                 }
00592                 break;
00593             case 2:
00594             case 3:
00595                 for (; nsample < 256; nsample++) {
00596                     c1 = su1[band+nsample];
00597                     c2 = su2[band+nsample];
00598                     su1[band+nsample] = c1 + c2;
00599                     su2[band+nsample] = c1 - c2;
00600                 }
00601                 break;
00602             default:
00603                 assert(0);
00604         }
00605     }
00606 }
00607 
00608 static void getChannelWeights (int indx, int flag, float ch[2]){
00609 
00610     if (indx == 7) {
00611         ch[0] = 1.0;
00612         ch[1] = 1.0;
00613     } else {
00614         ch[0] = (float)(indx & 7) / 7.0;
00615         ch[1] = sqrt(2 - ch[0]*ch[0]);
00616         if(flag)
00617             FFSWAP(float, ch[0], ch[1]);
00618     }
00619 }
00620 
00621 static void channelWeighting (float *su1, float *su2, int *p3)
00622 {
00623     int   band, nsample;
00624     /* w[x][y] y=0 is left y=1 is right */
00625     float w[2][2];
00626 
00627     if (p3[1] != 7 || p3[3] != 7){
00628         getChannelWeights(p3[1], p3[0], w[0]);
00629         getChannelWeights(p3[3], p3[2], w[1]);
00630 
00631         for(band = 1; band < 4; band++) {
00632             /* scale the channels by the weights */
00633             for(nsample = 0; nsample < 8; nsample++) {
00634                 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00635                 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00636             }
00637 
00638             for(; nsample < 256; nsample++) {
00639                 su1[band*256+nsample] *= w[1][0];
00640                 su2[band*256+nsample] *= w[1][1];
00641             }
00642         }
00643     }
00644 }
00645 
00646 
00658 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00659 {
00660     int   band, result=0, numSubbands, lastTonal, numBands;
00661 
00662     if (codingMode == JOINT_STEREO && channelNum == 1) {
00663         if (get_bits(gb,2) != 3) {
00664             av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00665             return -1;
00666         }
00667     } else {
00668         if (get_bits(gb,6) != 0x28) {
00669             av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00670             return -1;
00671         }
00672     }
00673 
00674     /* number of coded QMF bands */
00675     pSnd->bandsCoded = get_bits(gb,2);
00676 
00677     result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00678     if (result) return result;
00679 
00680     pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00681     if (pSnd->numComponents == -1) return -1;
00682 
00683     numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00684 
00685     /* Merge the decoded spectrum and tonal components. */
00686     lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00687 
00688 
00689     /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
00690     numBands = (subbandTab[numSubbands] - 1) >> 8;
00691     if (lastTonal >= 0)
00692         numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00693 
00694 
00695     /* Reconstruct time domain samples. */
00696     for (band=0; band<4; band++) {
00697         /* Perform the IMDCT step without overlapping. */
00698         if (band <= numBands) {
00699             IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00700         } else
00701             memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00702 
00703         /* gain compensation and overlapping */
00704         gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
00705                                     &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
00706                                     &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
00707     }
00708 
00709     /* Swap the gain control buffers for the next frame. */
00710     pSnd->gcBlkSwitch ^= 1;
00711 
00712     return 0;
00713 }
00714 
00722 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
00723 {
00724     int   result, i;
00725     float   *p1, *p2, *p3, *p4;
00726     uint8_t *ptr1;
00727 
00728     if (q->codingMode == JOINT_STEREO) {
00729 
00730         /* channel coupling mode */
00731         /* decode Sound Unit 1 */
00732         init_get_bits(&q->gb,databuf,q->bits_per_frame);
00733 
00734         result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
00735         if (result != 0)
00736             return (result);
00737 
00738         /* Framedata of the su2 in the joint-stereo mode is encoded in
00739          * reverse byte order so we need to swap it first. */
00740         if (databuf == q->decoded_bytes_buffer) {
00741             uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00742             ptr1 = q->decoded_bytes_buffer;
00743             for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00744                 FFSWAP(uint8_t,*ptr1,*ptr2);
00745             }
00746         } else {
00747             const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00748             for (i = 0; i < q->bytes_per_frame; i++)
00749                 q->decoded_bytes_buffer[i] = *ptr2--;
00750         }
00751 
00752         /* Skip the sync codes (0xF8). */
00753         ptr1 = q->decoded_bytes_buffer;
00754         for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00755             if (i >= q->bytes_per_frame)
00756                 return -1;
00757         }
00758 
00759 
00760         /* set the bitstream reader at the start of the second Sound Unit*/
00761         init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00762 
00763         /* Fill the Weighting coeffs delay buffer */
00764         memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00765         q->weighting_delay[4] = get_bits1(&q->gb);
00766         q->weighting_delay[5] = get_bits(&q->gb,3);
00767 
00768         for (i = 0; i < 4; i++) {
00769             q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00770             q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00771             q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00772         }
00773 
00774         /* Decode Sound Unit 2. */
00775         result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
00776         if (result != 0)
00777             return (result);
00778 
00779         /* Reconstruct the channel coefficients. */
00780         reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00781 
00782         channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
00783 
00784     } else {
00785         /* normal stereo mode or mono */
00786         /* Decode the channel sound units. */
00787         for (i=0 ; i<q->channels ; i++) {
00788 
00789             /* Set the bitstream reader at the start of a channel sound unit. */
00790             init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
00791 
00792             result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
00793             if (result != 0)
00794                 return (result);
00795         }
00796     }
00797 
00798     /* Apply the iQMF synthesis filter. */
00799     p1= q->outSamples;
00800     for (i=0 ; i<q->channels ; i++) {
00801         p2= p1+256;
00802         p3= p2+256;
00803         p4= p3+256;
00804         atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00805         atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00806         atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00807         p1 +=1024;
00808     }
00809 
00810     return 0;
00811 }
00812 
00813 
00820 static int atrac3_decode_frame(AVCodecContext *avctx,
00821             void *data, int *data_size,
00822             AVPacket *avpkt) {
00823     const uint8_t *buf = avpkt->data;
00824     int buf_size = avpkt->size;
00825     ATRAC3Context *q = avctx->priv_data;
00826     int result = 0, i;
00827     const uint8_t* databuf;
00828     int16_t* samples = data;
00829 
00830     if (buf_size < avctx->block_align)
00831         return buf_size;
00832 
00833     /* Check if we need to descramble and what buffer to pass on. */
00834     if (q->scrambled_stream) {
00835         decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00836         databuf = q->decoded_bytes_buffer;
00837     } else {
00838         databuf = buf;
00839     }
00840 
00841     result = decodeFrame(q, databuf);
00842 
00843     if (result != 0) {
00844         av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00845         return -1;
00846     }
00847 
00848     if (q->channels == 1) {
00849         /* mono */
00850         for (i = 0; i<1024; i++)
00851             samples[i] = av_clip_int16(round(q->outSamples[i]));
00852         *data_size = 1024 * sizeof(int16_t);
00853     } else {
00854         /* stereo */
00855         for (i = 0; i < 1024; i++) {
00856             samples[i*2] = av_clip_int16(round(q->outSamples[i]));
00857             samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
00858         }
00859         *data_size = 2048 * sizeof(int16_t);
00860     }
00861 
00862     return avctx->block_align;
00863 }
00864 
00865 
00872 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00873 {
00874     int i;
00875     const uint8_t *edata_ptr = avctx->extradata;
00876     ATRAC3Context *q = avctx->priv_data;
00877     static VLC_TYPE atrac3_vlc_table[4096][2];
00878     static int vlcs_initialized = 0;
00879 
00880     /* Take data from the AVCodecContext (RM container). */
00881     q->sample_rate = avctx->sample_rate;
00882     q->channels = avctx->channels;
00883     q->bit_rate = avctx->bit_rate;
00884     q->bits_per_frame = avctx->block_align * 8;
00885     q->bytes_per_frame = avctx->block_align;
00886 
00887     /* Take care of the codec-specific extradata. */
00888     if (avctx->extradata_size == 14) {
00889         /* Parse the extradata, WAV format */
00890         av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown value always 1
00891         q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00892         q->codingMode = bytestream_get_le16(&edata_ptr);
00893         av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));  //Dupe of coding mode
00894         q->frame_factor = bytestream_get_le16(&edata_ptr);  //Unknown always 1
00895         av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown always 0
00896 
00897         /* setup */
00898         q->samples_per_frame = 1024 * q->channels;
00899         q->atrac3version = 4;
00900         q->delay = 0x88E;
00901         if (q->codingMode)
00902             q->codingMode = JOINT_STEREO;
00903         else
00904             q->codingMode = STEREO;
00905 
00906         q->scrambled_stream = 0;
00907 
00908         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)) {
00909         } else {
00910             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);
00911             return -1;
00912         }
00913 
00914     } else if (avctx->extradata_size == 10) {
00915         /* Parse the extradata, RM format. */
00916         q->atrac3version = bytestream_get_be32(&edata_ptr);
00917         q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00918         q->delay = bytestream_get_be16(&edata_ptr);
00919         q->codingMode = bytestream_get_be16(&edata_ptr);
00920 
00921         q->samples_per_channel = q->samples_per_frame / q->channels;
00922         q->scrambled_stream = 1;
00923 
00924     } else {
00925         av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00926     }
00927     /* Check the extradata. */
00928 
00929     if (q->atrac3version != 4) {
00930         av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00931         return -1;
00932     }
00933 
00934     if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
00935         av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00936         return -1;
00937     }
00938 
00939     if (q->delay != 0x88E) {
00940         av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00941         return -1;
00942     }
00943 
00944     if (q->codingMode == STEREO) {
00945         av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
00946     } else if (q->codingMode == JOINT_STEREO) {
00947         av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
00948     } else {
00949         av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
00950         return -1;
00951     }
00952 
00953     if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
00954         av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
00955         return -1;
00956     }
00957 
00958 
00959     if(avctx->block_align >= UINT_MAX/2)
00960         return -1;
00961 
00962     /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
00963      * this is for the bitstream reader. */
00964     if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE)))  == NULL)
00965         return AVERROR(ENOMEM);
00966 
00967 
00968     /* Initialize the VLC tables. */
00969     if (!vlcs_initialized) {
00970         for (i=0 ; i<7 ; i++) {
00971             spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
00972             spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
00973             init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
00974                 huff_bits[i], 1, 1,
00975                 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00976         }
00977         vlcs_initialized = 1;
00978     }
00979 
00980     init_atrac3_transforms(q);
00981 
00982     atrac_generate_tables();
00983 
00984     /* Generate gain tables. */
00985     for (i=0 ; i<16 ; i++)
00986         gain_tab1[i] = powf (2.0, (4 - i));
00987 
00988     for (i=-15 ; i<16 ; i++)
00989         gain_tab2[i+15] = powf (2.0, i * -0.125);
00990 
00991     /* init the joint-stereo decoding data */
00992     q->weighting_delay[0] = 0;
00993     q->weighting_delay[1] = 7;
00994     q->weighting_delay[2] = 0;
00995     q->weighting_delay[3] = 7;
00996     q->weighting_delay[4] = 0;
00997     q->weighting_delay[5] = 7;
00998 
00999     for (i=0; i<4; i++) {
01000         q->matrix_coeff_index_prev[i] = 3;
01001         q->matrix_coeff_index_now[i] = 3;
01002         q->matrix_coeff_index_next[i] = 3;
01003     }
01004 
01005     dsputil_init(&dsp, avctx);
01006 
01007     q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01008     if (!q->pUnits) {
01009         av_free(q->decoded_bytes_buffer);
01010         return AVERROR(ENOMEM);
01011     }
01012 
01013     avctx->sample_fmt = SAMPLE_FMT_S16;
01014     return 0;
01015 }
01016 
01017 
01018 AVCodec atrac3_decoder =
01019 {
01020     .name = "atrac3",
01021     .type = AVMEDIA_TYPE_AUDIO,
01022     .id = CODEC_ID_ATRAC3,
01023     .priv_data_size = sizeof(ATRAC3Context),
01024     .init = atrac3_decode_init,
01025     .close = atrac3_decode_close,
01026     .decode = atrac3_decode_frame,
01027     .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01028 };