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 Libav.
00007  *
00008  * Libav 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  * Libav 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 Libav; 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 "internal.h"
00041 #include "get_bits.h"
00042 #include "dsputil.h"
00043 #include "bytestream.h"
00044 #include "fft.h"
00045 #include "fmtconvert.h"
00046 
00047 #include "atrac.h"
00048 #include "atrac3data.h"
00049 
00050 #define JOINT_STEREO    0x12
00051 #define STEREO          0x2
00052 
00053 #define SAMPLES_PER_FRAME 1024
00054 #define MDCT_SIZE          512
00055 
00056 /* These structures are needed to store the parsed gain control data. */
00057 typedef struct {
00058     int   num_gain_data;
00059     int   levcode[8];
00060     int   loccode[8];
00061 } gain_info;
00062 
00063 typedef struct {
00064     gain_info   gBlock[4];
00065 } gain_block;
00066 
00067 typedef struct {
00068     int     pos;
00069     int     numCoefs;
00070     float   coef[8];
00071 } tonal_component;
00072 
00073 typedef struct {
00074     int               bandsCoded;
00075     int               numComponents;
00076     tonal_component   components[64];
00077     float             prevFrame[SAMPLES_PER_FRAME];
00078     int               gcBlkSwitch;
00079     gain_block        gainBlock[2];
00080 
00081     DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
00082     DECLARE_ALIGNED(32, float, IMDCT_buf)[SAMPLES_PER_FRAME];
00083 
00084     float             delayBuf1[46]; 
00085     float             delayBuf2[46];
00086     float             delayBuf3[46];
00087 } channel_unit;
00088 
00089 typedef struct {
00090     AVFrame             frame;
00091     GetBitContext       gb;
00093 
00094     int                 channels;
00095     int                 codingMode;
00096     int                 bit_rate;
00097     int                 sample_rate;
00098     int                 samples_per_channel;
00099     int                 samples_per_frame;
00100 
00101     int                 bits_per_frame;
00102     int                 bytes_per_frame;
00103     int                 pBs;
00104     channel_unit*       pUnits;
00106 
00107 
00108     int                 matrix_coeff_index_prev[4];
00109     int                 matrix_coeff_index_now[4];
00110     int                 matrix_coeff_index_next[4];
00111     int                 weighting_delay[6];
00113 
00114 
00115     float              *outSamples[2];
00116     uint8_t*            decoded_bytes_buffer;
00117     float               tempBuf[1070];
00119 
00120 
00121     int                 atrac3version;
00122     int                 delay;
00123     int                 scrambled_stream;
00124     int                 frame_factor;
00126 
00127     FFTContext          mdct_ctx;
00128     FmtConvertContext   fmt_conv;
00129 } ATRAC3Context;
00130 
00131 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
00132 static VLC              spectral_coeff_tab[7];
00133 static float            gain_tab1[16];
00134 static float            gain_tab2[31];
00135 static DSPContext       dsp;
00136 
00137 
00147 static void IMLT(ATRAC3Context *q, float *pInput, float *pOutput, int odd_band)
00148 {
00149     int     i;
00150 
00151     if (odd_band) {
00161         for (i=0; i<128; i++)
00162             FFSWAP(float, pInput[i], pInput[255-i]);
00163     }
00164 
00165     q->mdct_ctx.imdct_calc(&q->mdct_ctx,pOutput,pInput);
00166 
00167     /* Perform windowing on the output. */
00168     dsp.vector_fmul(pOutput, pOutput, mdct_window, MDCT_SIZE);
00169 
00170 }
00171 
00172 
00181 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00182     int i, off;
00183     uint32_t c;
00184     const uint32_t* buf;
00185     uint32_t* obuf = (uint32_t*) out;
00186 
00187     off = (intptr_t)inbuffer & 3;
00188     buf = (const uint32_t *)(inbuffer - off);
00189     if (off)
00190         c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
00191     else
00192         c = av_be2ne32(0x537F6103U);
00193     bytes += 3 + off;
00194     for (i = 0; i < bytes/4; i++)
00195         obuf[i] = c ^ buf[i];
00196 
00197     if (off)
00198         av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
00199 
00200     return off;
00201 }
00202 
00203 
00204 static av_cold int init_atrac3_transforms(ATRAC3Context *q, int is_float) {
00205     float enc_window[256];
00206     int i;
00207 
00208     /* Generate the mdct window, for details see
00209      * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
00210     for (i=0 ; i<256; i++)
00211         enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
00212 
00213     if (!mdct_window[0])
00214         for (i=0 ; i<256; i++) {
00215             mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
00216             mdct_window[511-i] = mdct_window[i];
00217         }
00218 
00219     /* Initialize the MDCT transform. */
00220     return ff_mdct_init(&q->mdct_ctx, 9, 1, is_float ? 1.0 / 32768 : 1.0);
00221 }
00222 
00227 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
00228 {
00229     ATRAC3Context *q = avctx->priv_data;
00230 
00231     av_free(q->pUnits);
00232     av_free(q->decoded_bytes_buffer);
00233     av_freep(&q->outSamples[0]);
00234 
00235     ff_mdct_end(&q->mdct_ctx);
00236 
00237     return 0;
00238 }
00239 
00250 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
00251 {
00252     int   numBits, cnt, code, huffSymb;
00253 
00254     if (selector == 1)
00255         numCodes /= 2;
00256 
00257     if (codingFlag != 0) {
00258         /* constant length coding (CLC) */
00259         numBits = CLCLengthTab[selector];
00260 
00261         if (selector > 1) {
00262             for (cnt = 0; cnt < numCodes; cnt++) {
00263                 if (numBits)
00264                     code = get_sbits(gb, numBits);
00265                 else
00266                     code = 0;
00267                 mantissas[cnt] = code;
00268             }
00269         } else {
00270             for (cnt = 0; cnt < numCodes; cnt++) {
00271                 if (numBits)
00272                     code = get_bits(gb, numBits); //numBits is always 4 in this case
00273                 else
00274                     code = 0;
00275                 mantissas[cnt*2] = seTab_0[code >> 2];
00276                 mantissas[cnt*2+1] = seTab_0[code & 3];
00277             }
00278         }
00279     } else {
00280         /* variable length coding (VLC) */
00281         if (selector != 1) {
00282             for (cnt = 0; cnt < numCodes; cnt++) {
00283                 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00284                 huffSymb += 1;
00285                 code = huffSymb >> 1;
00286                 if (huffSymb & 1)
00287                     code = -code;
00288                 mantissas[cnt] = code;
00289             }
00290         } else {
00291             for (cnt = 0; cnt < numCodes; cnt++) {
00292                 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00293                 mantissas[cnt*2] = decTable1[huffSymb*2];
00294                 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
00295             }
00296         }
00297     }
00298 }
00299 
00308 static int decodeSpectrum (GetBitContext *gb, float *pOut)
00309 {
00310     int   numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
00311     int   subband_vlc_index[32], SF_idxs[32];
00312     int   mantissas[128];
00313     float SF;
00314 
00315     numSubbands = get_bits(gb, 5); // number of coded subbands
00316     codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
00317 
00318     /* Get the VLC selector table for the subbands, 0 means not coded. */
00319     for (cnt = 0; cnt <= numSubbands; cnt++)
00320         subband_vlc_index[cnt] = get_bits(gb, 3);
00321 
00322     /* Read the scale factor indexes from the stream. */
00323     for (cnt = 0; cnt <= numSubbands; cnt++) {
00324         if (subband_vlc_index[cnt] != 0)
00325             SF_idxs[cnt] = get_bits(gb, 6);
00326     }
00327 
00328     for (cnt = 0; cnt <= numSubbands; cnt++) {
00329         first = subbandTab[cnt];
00330         last = subbandTab[cnt+1];
00331 
00332         subbWidth = last - first;
00333 
00334         if (subband_vlc_index[cnt] != 0) {
00335             /* Decode spectral coefficients for this subband. */
00336             /* TODO: This can be done faster is several blocks share the
00337              * same VLC selector (subband_vlc_index) */
00338             readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
00339 
00340             /* Decode the scale factor for this subband. */
00341             SF = ff_atrac_sf_table[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
00342 
00343             /* Inverse quantize the coefficients. */
00344             for (pIn=mantissas ; first<last; first++, pIn++)
00345                 pOut[first] = *pIn * SF;
00346         } else {
00347             /* This subband was not coded, so zero the entire subband. */
00348             memset(pOut+first, 0, subbWidth*sizeof(float));
00349         }
00350     }
00351 
00352     /* Clear the subbands that were not coded. */
00353     first = subbandTab[cnt];
00354     memset(pOut+first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
00355     return numSubbands;
00356 }
00357 
00366 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
00367 {
00368     int i,j,k,cnt;
00369     int   components, coding_mode_selector, coding_mode, coded_values_per_component;
00370     int   sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
00371     int   band_flags[4], mantissa[8];
00372     float  *pCoef;
00373     float  scalefactor;
00374     int   component_count = 0;
00375 
00376     components = get_bits(gb,5);
00377 
00378     /* no tonal components */
00379     if (components == 0)
00380         return 0;
00381 
00382     coding_mode_selector = get_bits(gb,2);
00383     if (coding_mode_selector == 2)
00384         return AVERROR_INVALIDDATA;
00385 
00386     coding_mode = coding_mode_selector & 1;
00387 
00388     for (i = 0; i < components; i++) {
00389         for (cnt = 0; cnt <= numBands; cnt++)
00390             band_flags[cnt] = get_bits1(gb);
00391 
00392         coded_values_per_component = get_bits(gb,3);
00393 
00394         quant_step_index = get_bits(gb,3);
00395         if (quant_step_index <= 1)
00396             return AVERROR_INVALIDDATA;
00397 
00398         if (coding_mode_selector == 3)
00399             coding_mode = get_bits1(gb);
00400 
00401         for (j = 0; j < (numBands + 1) * 4; j++) {
00402             if (band_flags[j >> 2] == 0)
00403                 continue;
00404 
00405             coded_components = get_bits(gb,3);
00406 
00407             for (k=0; k<coded_components; k++) {
00408                 sfIndx = get_bits(gb,6);
00409                 if (component_count >= 64)
00410                     return AVERROR_INVALIDDATA;
00411                 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00412                 max_coded_values = SAMPLES_PER_FRAME - pComponent[component_count].pos;
00413                 coded_values = coded_values_per_component + 1;
00414                 coded_values = FFMIN(max_coded_values,coded_values);
00415 
00416                 scalefactor = ff_atrac_sf_table[sfIndx] * iMaxQuant[quant_step_index];
00417 
00418                 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00419 
00420                 pComponent[component_count].numCoefs = coded_values;
00421 
00422                 /* inverse quant */
00423                 pCoef = pComponent[component_count].coef;
00424                 for (cnt = 0; cnt < coded_values; cnt++)
00425                     pCoef[cnt] = mantissa[cnt] * scalefactor;
00426 
00427                 component_count++;
00428             }
00429         }
00430     }
00431 
00432     return component_count;
00433 }
00434 
00443 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00444 {
00445     int   i, cf, numData;
00446     int   *pLevel, *pLoc;
00447 
00448     gain_info   *pGain = pGb->gBlock;
00449 
00450     for (i=0 ; i<=numBands; i++)
00451     {
00452         numData = get_bits(gb,3);
00453         pGain[i].num_gain_data = numData;
00454         pLevel = pGain[i].levcode;
00455         pLoc = pGain[i].loccode;
00456 
00457         for (cf = 0; cf < numData; cf++){
00458             pLevel[cf]= get_bits(gb,4);
00459             pLoc  [cf]= get_bits(gb,5);
00460             if(cf && pLoc[cf] <= pLoc[cf-1])
00461                 return AVERROR_INVALIDDATA;
00462         }
00463     }
00464 
00465     /* Clear the unused blocks. */
00466     for (; i<4 ; i++)
00467         pGain[i].num_gain_data = 0;
00468 
00469     return 0;
00470 }
00471 
00482 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00483 {
00484     /* gain compensation function */
00485     float  gain1, gain2, gain_inc;
00486     int   cnt, numdata, nsample, startLoc, endLoc;
00487 
00488 
00489     if (pGain2->num_gain_data == 0)
00490         gain1 = 1.0;
00491     else
00492         gain1 = gain_tab1[pGain2->levcode[0]];
00493 
00494     if (pGain1->num_gain_data == 0) {
00495         for (cnt = 0; cnt < 256; cnt++)
00496             pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00497     } else {
00498         numdata = pGain1->num_gain_data;
00499         pGain1->loccode[numdata] = 32;
00500         pGain1->levcode[numdata] = 4;
00501 
00502         nsample = 0; // current sample = 0
00503 
00504         for (cnt = 0; cnt < numdata; cnt++) {
00505             startLoc = pGain1->loccode[cnt] * 8;
00506             endLoc = startLoc + 8;
00507 
00508             gain2 = gain_tab1[pGain1->levcode[cnt]];
00509             gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00510 
00511             /* interpolate */
00512             for (; nsample < startLoc; nsample++)
00513                 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00514 
00515             /* interpolation is done over eight samples */
00516             for (; nsample < endLoc; nsample++) {
00517                 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00518                 gain2 *= gain_inc;
00519             }
00520         }
00521 
00522         for (; nsample < 256; nsample++)
00523             pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00524     }
00525 
00526     /* Delay for the overlapping part. */
00527     memcpy(pPrev, &pIn[256], 256*sizeof(float));
00528 }
00529 
00539 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00540 {
00541     int   cnt, i, lastPos = -1;
00542     float   *pIn, *pOut;
00543 
00544     for (cnt = 0; cnt < numComponents; cnt++){
00545         lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
00546         pIn = pComponent[cnt].coef;
00547         pOut = &(pSpectrum[pComponent[cnt].pos]);
00548 
00549         for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00550             pOut[i] += pIn[i];
00551     }
00552 
00553     return lastPos;
00554 }
00555 
00556 
00557 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00558 
00559 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00560 {
00561     int    i, band, nsample, s1, s2;
00562     float    c1, c2;
00563     float    mc1_l, mc1_r, mc2_l, mc2_r;
00564 
00565     for (i=0,band = 0; band < 4*256; band+=256,i++) {
00566         s1 = pPrevCode[i];
00567         s2 = pCurrCode[i];
00568         nsample = 0;
00569 
00570         if (s1 != s2) {
00571             /* Selector value changed, interpolation needed. */
00572             mc1_l = matrixCoeffs[s1*2];
00573             mc1_r = matrixCoeffs[s1*2+1];
00574             mc2_l = matrixCoeffs[s2*2];
00575             mc2_r = matrixCoeffs[s2*2+1];
00576 
00577             /* Interpolation is done over the first eight samples. */
00578             for(; nsample < 8; nsample++) {
00579                 c1 = su1[band+nsample];
00580                 c2 = su2[band+nsample];
00581                 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00582                 su1[band+nsample] = c2;
00583                 su2[band+nsample] = c1 * 2.0 - c2;
00584             }
00585         }
00586 
00587         /* Apply the matrix without interpolation. */
00588         switch (s2) {
00589             case 0:     /* M/S decoding */
00590                 for (; nsample < 256; nsample++) {
00591                     c1 = su1[band+nsample];
00592                     c2 = su2[band+nsample];
00593                     su1[band+nsample] = c2 * 2.0;
00594                     su2[band+nsample] = (c1 - c2) * 2.0;
00595                 }
00596                 break;
00597 
00598             case 1:
00599                 for (; nsample < 256; nsample++) {
00600                     c1 = su1[band+nsample];
00601                     c2 = su2[band+nsample];
00602                     su1[band+nsample] = (c1 + c2) * 2.0;
00603                     su2[band+nsample] = c2 * -2.0;
00604                 }
00605                 break;
00606             case 2:
00607             case 3:
00608                 for (; nsample < 256; nsample++) {
00609                     c1 = su1[band+nsample];
00610                     c2 = su2[band+nsample];
00611                     su1[band+nsample] = c1 + c2;
00612                     su2[band+nsample] = c1 - c2;
00613                 }
00614                 break;
00615             default:
00616                 assert(0);
00617         }
00618     }
00619 }
00620 
00621 static void getChannelWeights (int indx, int flag, float ch[2]){
00622 
00623     if (indx == 7) {
00624         ch[0] = 1.0;
00625         ch[1] = 1.0;
00626     } else {
00627         ch[0] = (float)(indx & 7) / 7.0;
00628         ch[1] = sqrt(2 - ch[0]*ch[0]);
00629         if(flag)
00630             FFSWAP(float, ch[0], ch[1]);
00631     }
00632 }
00633 
00634 static void channelWeighting (float *su1, float *su2, int *p3)
00635 {
00636     int   band, nsample;
00637     /* w[x][y] y=0 is left y=1 is right */
00638     float w[2][2];
00639 
00640     if (p3[1] != 7 || p3[3] != 7){
00641         getChannelWeights(p3[1], p3[0], w[0]);
00642         getChannelWeights(p3[3], p3[2], w[1]);
00643 
00644         for(band = 1; band < 4; band++) {
00645             /* scale the channels by the weights */
00646             for(nsample = 0; nsample < 8; nsample++) {
00647                 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00648                 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00649             }
00650 
00651             for(; nsample < 256; nsample++) {
00652                 su1[band*256+nsample] *= w[1][0];
00653                 su2[band*256+nsample] *= w[1][1];
00654             }
00655         }
00656     }
00657 }
00658 
00659 
00671 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00672 {
00673     int   band, result=0, numSubbands, lastTonal, numBands;
00674 
00675     if (codingMode == JOINT_STEREO && channelNum == 1) {
00676         if (get_bits(gb,2) != 3) {
00677             av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00678             return AVERROR_INVALIDDATA;
00679         }
00680     } else {
00681         if (get_bits(gb,6) != 0x28) {
00682             av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00683             return AVERROR_INVALIDDATA;
00684         }
00685     }
00686 
00687     /* number of coded QMF bands */
00688     pSnd->bandsCoded = get_bits(gb,2);
00689 
00690     result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00691     if (result) return result;
00692 
00693     pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00694     if (pSnd->numComponents < 0)
00695         return pSnd->numComponents;
00696 
00697     numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00698 
00699     /* Merge the decoded spectrum and tonal components. */
00700     lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00701 
00702 
00703     /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
00704     numBands = (subbandTab[numSubbands] - 1) >> 8;
00705     if (lastTonal >= 0)
00706         numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00707 
00708 
00709     /* Reconstruct time domain samples. */
00710     for (band=0; band<4; band++) {
00711         /* Perform the IMDCT step without overlapping. */
00712         if (band <= numBands) {
00713             IMLT(q, &(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00714         } else
00715             memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00716 
00717         /* gain compensation and overlapping */
00718         gainCompensateAndOverlap(pSnd->IMDCT_buf, &pSnd->prevFrame[band * 256],
00719                                  &pOut[band * 256],
00720                                  &pSnd->gainBlock[1 - pSnd->gcBlkSwitch].gBlock[band],
00721                                  &pSnd->gainBlock[    pSnd->gcBlkSwitch].gBlock[band]);
00722     }
00723 
00724     /* Swap the gain control buffers for the next frame. */
00725     pSnd->gcBlkSwitch ^= 1;
00726 
00727     return 0;
00728 }
00729 
00737 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf,
00738                        float **out_samples)
00739 {
00740     int   result, i;
00741     float   *p1, *p2, *p3, *p4;
00742     uint8_t *ptr1;
00743 
00744     if (q->codingMode == JOINT_STEREO) {
00745 
00746         /* channel coupling mode */
00747         /* decode Sound Unit 1 */
00748         init_get_bits(&q->gb,databuf,q->bits_per_frame);
00749 
00750         result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, out_samples[0], 0, JOINT_STEREO);
00751         if (result != 0)
00752             return result;
00753 
00754         /* Framedata of the su2 in the joint-stereo mode is encoded in
00755          * reverse byte order so we need to swap it first. */
00756         if (databuf == q->decoded_bytes_buffer) {
00757             uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00758             ptr1 = q->decoded_bytes_buffer;
00759             for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00760                 FFSWAP(uint8_t,*ptr1,*ptr2);
00761             }
00762         } else {
00763             const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00764             for (i = 0; i < q->bytes_per_frame; i++)
00765                 q->decoded_bytes_buffer[i] = *ptr2--;
00766         }
00767 
00768         /* Skip the sync codes (0xF8). */
00769         ptr1 = q->decoded_bytes_buffer;
00770         for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00771             if (i >= q->bytes_per_frame)
00772                 return AVERROR_INVALIDDATA;
00773         }
00774 
00775 
00776         /* set the bitstream reader at the start of the second Sound Unit*/
00777         init_get_bits(&q->gb, ptr1, (q->bytes_per_frame - i) * 8);
00778 
00779         /* Fill the Weighting coeffs delay buffer */
00780         memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00781         q->weighting_delay[4] = get_bits1(&q->gb);
00782         q->weighting_delay[5] = get_bits(&q->gb,3);
00783 
00784         for (i = 0; i < 4; i++) {
00785             q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00786             q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00787             q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00788         }
00789 
00790         /* Decode Sound Unit 2. */
00791         result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], out_samples[1], 1, JOINT_STEREO);
00792         if (result != 0)
00793             return result;
00794 
00795         /* Reconstruct the channel coefficients. */
00796         reverseMatrixing(out_samples[0], out_samples[1], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00797 
00798         channelWeighting(out_samples[0], out_samples[1], q->weighting_delay);
00799 
00800     } else {
00801         /* normal stereo mode or mono */
00802         /* Decode the channel sound units. */
00803         for (i=0 ; i<q->channels ; i++) {
00804 
00805             /* Set the bitstream reader at the start of a channel sound unit. */
00806             init_get_bits(&q->gb,
00807                           databuf + i * q->bytes_per_frame / q->channels,
00808                           q->bits_per_frame / q->channels);
00809 
00810             result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], out_samples[i], i, q->codingMode);
00811             if (result != 0)
00812                 return result;
00813         }
00814     }
00815 
00816     /* Apply the iQMF synthesis filter. */
00817     for (i=0 ; i<q->channels ; i++) {
00818         p1 = out_samples[i];
00819         p2= p1+256;
00820         p3= p2+256;
00821         p4= p3+256;
00822         atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00823         atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00824         atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00825     }
00826 
00827     return 0;
00828 }
00829 
00830 
00837 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
00838                                int *got_frame_ptr, AVPacket *avpkt)
00839 {
00840     const uint8_t *buf = avpkt->data;
00841     int buf_size = avpkt->size;
00842     ATRAC3Context *q = avctx->priv_data;
00843     int result;
00844     const uint8_t* databuf;
00845     float   *samples_flt;
00846     int16_t *samples_s16;
00847 
00848     if (buf_size < avctx->block_align) {
00849         av_log(avctx, AV_LOG_ERROR,
00850                "Frame too small (%d bytes). Truncated file?\n", buf_size);
00851         return AVERROR_INVALIDDATA;
00852     }
00853 
00854     /* get output buffer */
00855     q->frame.nb_samples = SAMPLES_PER_FRAME;
00856     if ((result = ff_get_buffer(avctx, &q->frame)) < 0) {
00857         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00858         return result;
00859     }
00860     samples_flt = (float   *)q->frame.data[0];
00861     samples_s16 = (int16_t *)q->frame.data[0];
00862 
00863     /* Check if we need to descramble and what buffer to pass on. */
00864     if (q->scrambled_stream) {
00865         decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00866         databuf = q->decoded_bytes_buffer;
00867     } else {
00868         databuf = buf;
00869     }
00870 
00871     if (q->channels == 1 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
00872         result = decodeFrame(q, databuf, &samples_flt);
00873     else
00874         result = decodeFrame(q, databuf, q->outSamples);
00875 
00876     if (result != 0) {
00877         av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00878         return result;
00879     }
00880 
00881     /* interleave */
00882     if (q->channels == 2 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
00883         q->fmt_conv.float_interleave(samples_flt,
00884                                      (const float **)q->outSamples,
00885                                      SAMPLES_PER_FRAME, 2);
00886     } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
00887         q->fmt_conv.float_to_int16_interleave(samples_s16,
00888                                               (const float **)q->outSamples,
00889                                               SAMPLES_PER_FRAME, q->channels);
00890     }
00891 
00892     *got_frame_ptr   = 1;
00893     *(AVFrame *)data = q->frame;
00894 
00895     return avctx->block_align;
00896 }
00897 
00898 
00905 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00906 {
00907     int i, ret;
00908     const uint8_t *edata_ptr = avctx->extradata;
00909     ATRAC3Context *q = avctx->priv_data;
00910     static VLC_TYPE atrac3_vlc_table[4096][2];
00911     static int vlcs_initialized = 0;
00912 
00913     /* Take data from the AVCodecContext (RM container). */
00914     q->sample_rate = avctx->sample_rate;
00915     q->channels = avctx->channels;
00916     q->bit_rate = avctx->bit_rate;
00917     q->bits_per_frame = avctx->block_align * 8;
00918     q->bytes_per_frame = avctx->block_align;
00919 
00920     /* Take care of the codec-specific extradata. */
00921     if (avctx->extradata_size == 14) {
00922         /* Parse the extradata, WAV format */
00923         av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown value always 1
00924         q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00925         q->codingMode = bytestream_get_le16(&edata_ptr);
00926         av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));  //Dupe of coding mode
00927         q->frame_factor = bytestream_get_le16(&edata_ptr);  //Unknown always 1
00928         av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));  //Unknown always 0
00929 
00930         /* setup */
00931         q->samples_per_frame = SAMPLES_PER_FRAME * q->channels;
00932         q->atrac3version = 4;
00933         q->delay = 0x88E;
00934         if (q->codingMode)
00935             q->codingMode = JOINT_STEREO;
00936         else
00937             q->codingMode = STEREO;
00938 
00939         q->scrambled_stream = 0;
00940 
00941         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)) {
00942         } else {
00943             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);
00944             return AVERROR_INVALIDDATA;
00945         }
00946 
00947     } else if (avctx->extradata_size == 10) {
00948         /* Parse the extradata, RM format. */
00949         q->atrac3version = bytestream_get_be32(&edata_ptr);
00950         q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00951         q->delay = bytestream_get_be16(&edata_ptr);
00952         q->codingMode = bytestream_get_be16(&edata_ptr);
00953 
00954         q->samples_per_channel = q->samples_per_frame / q->channels;
00955         q->scrambled_stream = 1;
00956 
00957     } else {
00958         av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00959     }
00960     /* Check the extradata. */
00961 
00962     if (q->atrac3version != 4) {
00963         av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00964         return AVERROR_INVALIDDATA;
00965     }
00966 
00967     if (q->samples_per_frame != SAMPLES_PER_FRAME && q->samples_per_frame != SAMPLES_PER_FRAME*2) {
00968         av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00969         return AVERROR_INVALIDDATA;
00970     }
00971 
00972     if (q->delay != 0x88E) {
00973         av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00974         return AVERROR_INVALIDDATA;
00975     }
00976 
00977     if (q->codingMode == STEREO) {
00978         av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
00979     } else if (q->codingMode == JOINT_STEREO) {
00980         if (avctx->channels != 2)
00981             return AVERROR_INVALIDDATA;
00982         av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
00983     } else {
00984         av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
00985         return AVERROR_INVALIDDATA;
00986     }
00987 
00988     if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
00989         av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
00990         return AVERROR(EINVAL);
00991     }
00992 
00993 
00994     if(avctx->block_align >= UINT_MAX/2)
00995         return AVERROR(EINVAL);
00996 
00997     /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
00998      * this is for the bitstream reader. */
00999     if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE)))  == NULL)
01000         return AVERROR(ENOMEM);
01001 
01002 
01003     /* Initialize the VLC tables. */
01004     if (!vlcs_initialized) {
01005         for (i=0 ; i<7 ; i++) {
01006             spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
01007             spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
01008             init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
01009                 huff_bits[i], 1, 1,
01010                 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
01011         }
01012         vlcs_initialized = 1;
01013     }
01014 
01015     if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT)
01016         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01017     else
01018         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01019 
01020     if ((ret = init_atrac3_transforms(q, avctx->sample_fmt == AV_SAMPLE_FMT_FLT))) {
01021         av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
01022         av_freep(&q->decoded_bytes_buffer);
01023         return ret;
01024     }
01025 
01026     atrac_generate_tables();
01027 
01028     /* Generate gain tables. */
01029     for (i=0 ; i<16 ; i++)
01030         gain_tab1[i] = powf (2.0, (4 - i));
01031 
01032     for (i=-15 ; i<16 ; i++)
01033         gain_tab2[i+15] = powf (2.0, i * -0.125);
01034 
01035     /* init the joint-stereo decoding data */
01036     q->weighting_delay[0] = 0;
01037     q->weighting_delay[1] = 7;
01038     q->weighting_delay[2] = 0;
01039     q->weighting_delay[3] = 7;
01040     q->weighting_delay[4] = 0;
01041     q->weighting_delay[5] = 7;
01042 
01043     for (i=0; i<4; i++) {
01044         q->matrix_coeff_index_prev[i] = 3;
01045         q->matrix_coeff_index_now[i] = 3;
01046         q->matrix_coeff_index_next[i] = 3;
01047     }
01048 
01049     dsputil_init(&dsp, avctx);
01050     ff_fmt_convert_init(&q->fmt_conv, avctx);
01051 
01052     q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01053     if (!q->pUnits) {
01054         atrac3_decode_close(avctx);
01055         return AVERROR(ENOMEM);
01056     }
01057 
01058     if (avctx->channels > 1 || avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
01059         q->outSamples[0] = av_mallocz(SAMPLES_PER_FRAME * avctx->channels * sizeof(*q->outSamples[0]));
01060         q->outSamples[1] = q->outSamples[0] + SAMPLES_PER_FRAME;
01061         if (!q->outSamples[0]) {
01062             atrac3_decode_close(avctx);
01063             return AVERROR(ENOMEM);
01064         }
01065     }
01066 
01067     avcodec_get_frame_defaults(&q->frame);
01068     avctx->coded_frame = &q->frame;
01069 
01070     return 0;
01071 }
01072 
01073 
01074 AVCodec ff_atrac3_decoder =
01075 {
01076     .name = "atrac3",
01077     .type = AVMEDIA_TYPE_AUDIO,
01078     .id = CODEC_ID_ATRAC3,
01079     .priv_data_size = sizeof(ATRAC3Context),
01080     .init = atrac3_decode_init,
01081     .close = atrac3_decode_close,
01082     .decode = atrac3_decode_frame,
01083     .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01084     .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01085 };