00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00034 #include <math.h>
00035 #include <stddef.h>
00036 #include <stdio.h>
00037
00038 #include "avcodec.h"
00039 #include "bitstream.h"
00040 #include "dsputil.h"
00041 #include "bytestream.h"
00042
00043 #include "atrac3data.h"
00044
00045 #define JOINT_STEREO 0x12
00046 #define STEREO 0x2
00047
00048
00049
00050 typedef struct {
00051 int num_gain_data;
00052 int levcode[8];
00053 int loccode[8];
00054 } gain_info;
00055
00056 typedef struct {
00057 gain_info gBlock[4];
00058 } gain_block;
00059
00060 typedef struct {
00061 int pos;
00062 int numCoefs;
00063 float coef[8];
00064 } tonal_component;
00065
00066 typedef struct {
00067 int bandsCoded;
00068 int numComponents;
00069 tonal_component components[64];
00070 float prevFrame[1024];
00071 int gcBlkSwitch;
00072 gain_block gainBlock[2];
00073
00074 DECLARE_ALIGNED_16(float, spectrum[1024]);
00075 DECLARE_ALIGNED_16(float, IMDCT_buf[1024]);
00076
00077 float delayBuf1[46];
00078 float delayBuf2[46];
00079 float delayBuf3[46];
00080 } channel_unit;
00081
00082 typedef struct {
00083 GetBitContext gb;
00085
00086 int channels;
00087 int codingMode;
00088 int bit_rate;
00089 int sample_rate;
00090 int samples_per_channel;
00091 int samples_per_frame;
00092
00093 int bits_per_frame;
00094 int bytes_per_frame;
00095 int pBs;
00096 channel_unit* pUnits;
00098
00099
00100 int matrix_coeff_index_prev[4];
00101 int matrix_coeff_index_now[4];
00102 int matrix_coeff_index_next[4];
00103 int weighting_delay[6];
00105
00106
00107 float outSamples[2048];
00108 uint8_t* decoded_bytes_buffer;
00109 float tempBuf[1070];
00110 DECLARE_ALIGNED_16(float,mdct_tmp[512]);
00112
00113
00114 int atrac3version;
00115 int delay;
00116 int scrambled_stream;
00117 int frame_factor;
00119 } ATRAC3Context;
00120
00121 static DECLARE_ALIGNED_16(float,mdct_window[512]);
00122 static float qmf_window[48];
00123 static VLC spectral_coeff_tab[7];
00124 static float SFTable[64];
00125 static float gain_tab1[16];
00126 static float gain_tab2[31];
00127 static MDCTContext mdct_ctx;
00128 static DSPContext dsp;
00129
00130
00131
00132
00145 static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
00146 {
00147 int i, j;
00148 float *p1, *p3;
00149
00150 memcpy(temp, delayBuf, 46*sizeof(float));
00151
00152 p3 = temp + 46;
00153
00154
00155 for(i=0; i<nIn; i+=2){
00156 p3[2*i+0] = inlo[i ] + inhi[i ];
00157 p3[2*i+1] = inlo[i ] - inhi[i ];
00158 p3[2*i+2] = inlo[i+1] + inhi[i+1];
00159 p3[2*i+3] = inlo[i+1] - inhi[i+1];
00160 }
00161
00162
00163 p1 = temp;
00164 for (j = nIn; j != 0; j--) {
00165 float s1 = 0.0;
00166 float s2 = 0.0;
00167
00168 for (i = 0; i < 48; i += 2) {
00169 s1 += p1[i] * qmf_window[i];
00170 s2 += p1[i+1] * qmf_window[i+1];
00171 }
00172
00173 pOut[0] = s2;
00174 pOut[1] = s1;
00175
00176 p1 += 2;
00177 pOut += 2;
00178 }
00179
00180
00181 memcpy(delayBuf, temp + nIn*2, 46*sizeof(float));
00182 }
00183
00194 static void IMLT(float *pInput, float *pOutput, int odd_band, float* mdct_tmp)
00195 {
00196 int i;
00197
00198 if (odd_band) {
00208 for (i=0; i<128; i++)
00209 FFSWAP(float, pInput[i], pInput[255-i]);
00210 }
00211
00212 mdct_ctx.fft.imdct_calc(&mdct_ctx,pOutput,pInput,mdct_tmp);
00213
00214
00215 dsp.vector_fmul(pOutput,mdct_window,512);
00216
00217 }
00218
00219
00228 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00229 int i, off;
00230 uint32_t c;
00231 const uint32_t* buf;
00232 uint32_t* obuf = (uint32_t*) out;
00233
00234 off = (int)((long)inbuffer & 3);
00235 buf = (const uint32_t*) (inbuffer - off);
00236 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
00237 bytes += 3 + off;
00238 for (i = 0; i < bytes/4; i++)
00239 obuf[i] = c ^ buf[i];
00240
00241 if (off)
00242 av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
00243
00244 return off;
00245 }
00246
00247
00248 static void init_atrac3_transforms(ATRAC3Context *q) {
00249 float enc_window[256];
00250 float s;
00251 int i;
00252
00253
00254
00255 for (i=0 ; i<256; i++)
00256 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
00257
00258 if (!mdct_window[0])
00259 for (i=0 ; i<256; i++) {
00260 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
00261 mdct_window[511-i] = mdct_window[i];
00262 }
00263
00264
00265 for (i=0 ; i<24; i++) {
00266 s = qmf_48tap_half[i] * 2.0;
00267 qmf_window[i] = s;
00268 qmf_window[47 - i] = s;
00269 }
00270
00271
00272 ff_mdct_init(&mdct_ctx, 9, 1);
00273 }
00274
00279 static int atrac3_decode_close(AVCodecContext *avctx)
00280 {
00281 ATRAC3Context *q = avctx->priv_data;
00282
00283 av_free(q->pUnits);
00284 av_free(q->decoded_bytes_buffer);
00285
00286 return 0;
00287 }
00288
00299 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
00300 {
00301 int numBits, cnt, code, huffSymb;
00302
00303 if (selector == 1)
00304 numCodes /= 2;
00305
00306 if (codingFlag != 0) {
00307
00308
00309 numBits = CLCLengthTab[selector];
00310
00311 if (selector > 1) {
00312 for (cnt = 0; cnt < numCodes; cnt++) {
00313 if (numBits)
00314 code = get_sbits(gb, numBits);
00315 else
00316 code = 0;
00317 mantissas[cnt] = code;
00318 }
00319 } else {
00320 for (cnt = 0; cnt < numCodes; cnt++) {
00321 if (numBits)
00322 code = get_bits(gb, numBits);
00323 else
00324 code = 0;
00325 mantissas[cnt*2] = seTab_0[code >> 2];
00326 mantissas[cnt*2+1] = seTab_0[code & 3];
00327 }
00328 }
00329 } else {
00330
00331 if (selector != 1) {
00332 for (cnt = 0; cnt < numCodes; cnt++) {
00333 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00334 huffSymb += 1;
00335 code = huffSymb >> 1;
00336 if (huffSymb & 1)
00337 code = -code;
00338 mantissas[cnt] = code;
00339 }
00340 } else {
00341 for (cnt = 0; cnt < numCodes; cnt++) {
00342 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00343 mantissas[cnt*2] = decTable1[huffSymb*2];
00344 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
00345 }
00346 }
00347 }
00348 }
00349
00358 static int decodeSpectrum (GetBitContext *gb, float *pOut)
00359 {
00360 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
00361 int subband_vlc_index[32], SF_idxs[32];
00362 int mantissas[128];
00363 float SF;
00364
00365 numSubbands = get_bits(gb, 5);
00366 codingMode = get_bits1(gb);
00367
00368
00369 for (cnt = 0; cnt <= numSubbands; cnt++)
00370 subband_vlc_index[cnt] = get_bits(gb, 3);
00371
00372
00373 for (cnt = 0; cnt <= numSubbands; cnt++) {
00374 if (subband_vlc_index[cnt] != 0)
00375 SF_idxs[cnt] = get_bits(gb, 6);
00376 }
00377
00378 for (cnt = 0; cnt <= numSubbands; cnt++) {
00379 first = subbandTab[cnt];
00380 last = subbandTab[cnt+1];
00381
00382 subbWidth = last - first;
00383
00384 if (subband_vlc_index[cnt] != 0) {
00385
00386
00387
00388 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
00389
00390
00391 SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
00392
00393
00394 for (pIn=mantissas ; first<last; first++, pIn++)
00395 pOut[first] = *pIn * SF;
00396 } else {
00397
00398 memset(pOut+first, 0, subbWidth*sizeof(float));
00399 }
00400 }
00401
00402
00403 first = subbandTab[cnt];
00404 memset(pOut+first, 0, (1024 - first) * sizeof(float));
00405 return numSubbands;
00406 }
00407
00416 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
00417 {
00418 int i,j,k,cnt;
00419 int components, coding_mode_selector, coding_mode, coded_values_per_component;
00420 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
00421 int band_flags[4], mantissa[8];
00422 float *pCoef;
00423 float scalefactor;
00424 int component_count = 0;
00425
00426 components = get_bits(gb,5);
00427
00428
00429 if (components == 0)
00430 return 0;
00431
00432 coding_mode_selector = get_bits(gb,2);
00433 if (coding_mode_selector == 2)
00434 return -1;
00435
00436 coding_mode = coding_mode_selector & 1;
00437
00438 for (i = 0; i < components; i++) {
00439 for (cnt = 0; cnt <= numBands; cnt++)
00440 band_flags[cnt] = get_bits1(gb);
00441
00442 coded_values_per_component = get_bits(gb,3);
00443
00444 quant_step_index = get_bits(gb,3);
00445 if (quant_step_index <= 1)
00446 return -1;
00447
00448 if (coding_mode_selector == 3)
00449 coding_mode = get_bits1(gb);
00450
00451 for (j = 0; j < (numBands + 1) * 4; j++) {
00452 if (band_flags[j >> 2] == 0)
00453 continue;
00454
00455 coded_components = get_bits(gb,3);
00456
00457 for (k=0; k<coded_components; k++) {
00458 sfIndx = get_bits(gb,6);
00459 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00460 max_coded_values = 1024 - pComponent[component_count].pos;
00461 coded_values = coded_values_per_component + 1;
00462 coded_values = FFMIN(max_coded_values,coded_values);
00463
00464 scalefactor = SFTable[sfIndx] * iMaxQuant[quant_step_index];
00465
00466 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00467
00468 pComponent[component_count].numCoefs = coded_values;
00469
00470
00471 pCoef = pComponent[k].coef;
00472 for (cnt = 0; cnt < coded_values; cnt++)
00473 pCoef[cnt] = mantissa[cnt] * scalefactor;
00474
00475 component_count++;
00476 }
00477 }
00478 }
00479
00480 return component_count;
00481 }
00482
00491 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00492 {
00493 int i, cf, numData;
00494 int *pLevel, *pLoc;
00495
00496 gain_info *pGain = pGb->gBlock;
00497
00498 for (i=0 ; i<=numBands; i++)
00499 {
00500 numData = get_bits(gb,3);
00501 pGain[i].num_gain_data = numData;
00502 pLevel = pGain[i].levcode;
00503 pLoc = pGain[i].loccode;
00504
00505 for (cf = 0; cf < numData; cf++){
00506 pLevel[cf]= get_bits(gb,4);
00507 pLoc [cf]= get_bits(gb,5);
00508 if(cf && pLoc[cf] <= pLoc[cf-1])
00509 return -1;
00510 }
00511 }
00512
00513
00514 for (; i<4 ; i++)
00515 pGain[i].num_gain_data = 0;
00516
00517 return 0;
00518 }
00519
00530 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00531 {
00532
00533 float gain1, gain2, gain_inc;
00534 int cnt, numdata, nsample, startLoc, endLoc;
00535
00536
00537 if (pGain2->num_gain_data == 0)
00538 gain1 = 1.0;
00539 else
00540 gain1 = gain_tab1[pGain2->levcode[0]];
00541
00542 if (pGain1->num_gain_data == 0) {
00543 for (cnt = 0; cnt < 256; cnt++)
00544 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00545 } else {
00546 numdata = pGain1->num_gain_data;
00547 pGain1->loccode[numdata] = 32;
00548 pGain1->levcode[numdata] = 4;
00549
00550 nsample = 0;
00551
00552 for (cnt = 0; cnt < numdata; cnt++) {
00553 startLoc = pGain1->loccode[cnt] * 8;
00554 endLoc = startLoc + 8;
00555
00556 gain2 = gain_tab1[pGain1->levcode[cnt]];
00557 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00558
00559
00560 for (; nsample < startLoc; nsample++)
00561 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00562
00563
00564 for (; nsample < endLoc; nsample++) {
00565 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00566 gain2 *= gain_inc;
00567 }
00568 }
00569
00570 for (; nsample < 256; nsample++)
00571 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00572 }
00573
00574
00575 memcpy(pPrev, &pIn[256], 256*sizeof(float));
00576 }
00577
00586 static void addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00587 {
00588 int cnt, i;
00589 float *pIn, *pOut;
00590
00591 for (cnt = 0; cnt < numComponents; cnt++){
00592 pIn = pComponent[cnt].coef;
00593 pOut = &(pSpectrum[pComponent[cnt].pos]);
00594
00595 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00596 pOut[i] += pIn[i];
00597 }
00598 }
00599
00600
00601 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00602
00603 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00604 {
00605 int i, band, nsample, s1, s2;
00606 float c1, c2;
00607 float mc1_l, mc1_r, mc2_l, mc2_r;
00608
00609 for (i=0,band = 0; band < 4*256; band+=256,i++) {
00610 s1 = pPrevCode[i];
00611 s2 = pCurrCode[i];
00612 nsample = 0;
00613
00614 if (s1 != s2) {
00615
00616 mc1_l = matrixCoeffs[s1*2];
00617 mc1_r = matrixCoeffs[s1*2+1];
00618 mc2_l = matrixCoeffs[s2*2];
00619 mc2_r = matrixCoeffs[s2*2+1];
00620
00621
00622 for(; nsample < 8; nsample++) {
00623 c1 = su1[band+nsample];
00624 c2 = su2[band+nsample];
00625 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00626 su1[band+nsample] = c2;
00627 su2[band+nsample] = c1 * 2.0 - c2;
00628 }
00629 }
00630
00631
00632 switch (s2) {
00633 case 0:
00634 for (; nsample < 256; nsample++) {
00635 c1 = su1[band+nsample];
00636 c2 = su2[band+nsample];
00637 su1[band+nsample] = c2 * 2.0;
00638 su2[band+nsample] = (c1 - c2) * 2.0;
00639 }
00640 break;
00641
00642 case 1:
00643 for (; nsample < 256; nsample++) {
00644 c1 = su1[band+nsample];
00645 c2 = su2[band+nsample];
00646 su1[band+nsample] = (c1 + c2) * 2.0;
00647 su2[band+nsample] = c2 * -2.0;
00648 }
00649 break;
00650 case 2:
00651 case 3:
00652 for (; nsample < 256; nsample++) {
00653 c1 = su1[band+nsample];
00654 c2 = su2[band+nsample];
00655 su1[band+nsample] = c1 + c2;
00656 su2[band+nsample] = c1 - c2;
00657 }
00658 break;
00659 default:
00660 assert(0);
00661 }
00662 }
00663 }
00664
00665 static void getChannelWeights (int indx, int flag, float ch[2]){
00666
00667 if (indx == 7) {
00668 ch[0] = 1.0;
00669 ch[1] = 1.0;
00670 } else {
00671 ch[0] = (float)(indx & 7) / 7.0;
00672 ch[1] = sqrt(2 - ch[0]*ch[0]);
00673 if(flag)
00674 FFSWAP(float, ch[0], ch[1]);
00675 }
00676 }
00677
00678 static void channelWeighting (float *su1, float *su2, int *p3)
00679 {
00680 int band, nsample;
00681
00682 float w[2][2];
00683
00684 if (p3[1] != 7 || p3[3] != 7){
00685 getChannelWeights(p3[1], p3[0], w[0]);
00686 getChannelWeights(p3[3], p3[2], w[1]);
00687
00688 for(band = 1; band < 4; band++) {
00689
00690 for(nsample = 0; nsample < 8; nsample++) {
00691 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00692 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00693 }
00694
00695 for(; nsample < 256; nsample++) {
00696 su1[band*256+nsample] *= w[1][0];
00697 su2[band*256+nsample] *= w[1][1];
00698 }
00699 }
00700 }
00701 }
00702
00703
00715 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00716 {
00717 int band, result=0, numSubbands, numBands;
00718
00719 if (codingMode == JOINT_STEREO && channelNum == 1) {
00720 if (get_bits(gb,2) != 3) {
00721 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00722 return -1;
00723 }
00724 } else {
00725 if (get_bits(gb,6) != 0x28) {
00726 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00727 return -1;
00728 }
00729 }
00730
00731
00732 pSnd->bandsCoded = get_bits(gb,2);
00733
00734 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00735 if (result) return result;
00736
00737 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00738 if (pSnd->numComponents == -1) return -1;
00739
00740 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00741
00742
00743 addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00744
00745
00746
00747 numBands = (subbandTab[numSubbands] - 1) >> 8;
00748
00749
00750
00751 for (band=0; band<4; band++) {
00752
00753 if (band <= numBands) {
00754 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1,q->mdct_tmp);
00755 } else
00756 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00757
00758
00759 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
00760 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
00761 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
00762 }
00763
00764
00765 pSnd->gcBlkSwitch ^= 1;
00766
00767 return 0;
00768 }
00769
00777 static int decodeFrame(ATRAC3Context *q, uint8_t* databuf)
00778 {
00779 int result, i;
00780 float *p1, *p2, *p3, *p4;
00781 uint8_t *ptr1, *ptr2;
00782
00783 if (q->codingMode == JOINT_STEREO) {
00784
00785
00786
00787 init_get_bits(&q->gb,databuf,q->bits_per_frame);
00788
00789 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
00790 if (result != 0)
00791 return (result);
00792
00793
00794
00795 ptr1 = databuf;
00796 ptr2 = databuf+q->bytes_per_frame-1;
00797 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00798 FFSWAP(uint8_t,*ptr1,*ptr2);
00799 }
00800
00801
00802 ptr1 = databuf;
00803 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00804 if (i >= q->bytes_per_frame)
00805 return -1;
00806 }
00807
00808
00809
00810 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00811
00812
00813 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00814 q->weighting_delay[4] = get_bits1(&q->gb);
00815 q->weighting_delay[5] = get_bits(&q->gb,3);
00816
00817 for (i = 0; i < 4; i++) {
00818 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00819 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00820 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00821 }
00822
00823
00824 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
00825 if (result != 0)
00826 return (result);
00827
00828
00829 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00830
00831 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
00832
00833 } else {
00834
00835
00836 for (i=0 ; i<q->channels ; i++) {
00837
00838
00839 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
00840
00841 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
00842 if (result != 0)
00843 return (result);
00844 }
00845 }
00846
00847
00848 p1= q->outSamples;
00849 for (i=0 ; i<q->channels ; i++) {
00850 p2= p1+256;
00851 p3= p2+256;
00852 p4= p3+256;
00853 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00854 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00855 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00856 p1 +=1024;
00857 }
00858
00859 return 0;
00860 }
00861
00862
00869 static int atrac3_decode_frame(AVCodecContext *avctx,
00870 void *data, int *data_size,
00871 const uint8_t *buf, int buf_size) {
00872 ATRAC3Context *q = avctx->priv_data;
00873 int result = 0, i;
00874 uint8_t* databuf;
00875 int16_t* samples = data;
00876
00877 if (buf_size < avctx->block_align)
00878 return buf_size;
00879
00880
00881 if (q->scrambled_stream) {
00882 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00883 databuf = q->decoded_bytes_buffer;
00884 } else {
00885 databuf = buf;
00886 }
00887
00888 result = decodeFrame(q, databuf);
00889
00890 if (result != 0) {
00891 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00892 return -1;
00893 }
00894
00895 if (q->channels == 1) {
00896
00897 for (i = 0; i<1024; i++)
00898 samples[i] = av_clip_int16(round(q->outSamples[i]));
00899 *data_size = 1024 * sizeof(int16_t);
00900 } else {
00901
00902 for (i = 0; i < 1024; i++) {
00903 samples[i*2] = av_clip_int16(round(q->outSamples[i]));
00904 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
00905 }
00906 *data_size = 2048 * sizeof(int16_t);
00907 }
00908
00909 return avctx->block_align;
00910 }
00911
00912
00919 static int atrac3_decode_init(AVCodecContext *avctx)
00920 {
00921 int i;
00922 const uint8_t *edata_ptr = avctx->extradata;
00923 ATRAC3Context *q = avctx->priv_data;
00924
00925
00926 q->sample_rate = avctx->sample_rate;
00927 q->channels = avctx->channels;
00928 q->bit_rate = avctx->bit_rate;
00929 q->bits_per_frame = avctx->block_align * 8;
00930 q->bytes_per_frame = avctx->block_align;
00931
00932
00933 if (avctx->extradata_size == 14) {
00934
00935 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));
00936 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00937 q->codingMode = bytestream_get_le16(&edata_ptr);
00938 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));
00939 q->frame_factor = bytestream_get_le16(&edata_ptr);
00940 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));
00941
00942
00943 q->samples_per_frame = 1024 * q->channels;
00944 q->atrac3version = 4;
00945 q->delay = 0x88E;
00946 if (q->codingMode)
00947 q->codingMode = JOINT_STEREO;
00948 else
00949 q->codingMode = STEREO;
00950
00951 q->scrambled_stream = 0;
00952
00953 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)) {
00954 } else {
00955 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);
00956 return -1;
00957 }
00958
00959 } else if (avctx->extradata_size == 10) {
00960
00961 q->atrac3version = bytestream_get_be32(&edata_ptr);
00962 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00963 q->delay = bytestream_get_be16(&edata_ptr);
00964 q->codingMode = bytestream_get_be16(&edata_ptr);
00965
00966 q->samples_per_channel = q->samples_per_frame / q->channels;
00967 q->scrambled_stream = 1;
00968
00969 } else {
00970 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00971 }
00972
00973
00974 if (q->atrac3version != 4) {
00975 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00976 return -1;
00977 }
00978
00979 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
00980 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00981 return -1;
00982 }
00983
00984 if (q->delay != 0x88E) {
00985 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00986 return -1;
00987 }
00988
00989 if (q->codingMode == STEREO) {
00990 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
00991 } else if (q->codingMode == JOINT_STEREO) {
00992 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
00993 } else {
00994 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
00995 return -1;
00996 }
00997
00998 if (avctx->channels <= 0 || avctx->channels > 2 ) {
00999 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
01000 return -1;
01001 }
01002
01003
01004 if(avctx->block_align >= UINT_MAX/2)
01005 return -1;
01006
01007
01008
01009 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
01010 return AVERROR(ENOMEM);
01011
01012
01013
01014 for (i=0 ; i<7 ; i++) {
01015 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
01016 huff_bits[i], 1, 1,
01017 huff_codes[i], 1, 1, INIT_VLC_USE_STATIC);
01018 }
01019
01020 init_atrac3_transforms(q);
01021
01022
01023 for (i=0 ; i<64 ; i++)
01024 SFTable[i] = pow(2.0, (i - 15) / 3.0);
01025
01026
01027 for (i=0 ; i<16 ; i++)
01028 gain_tab1[i] = powf (2.0, (4 - i));
01029
01030 for (i=-15 ; i<16 ; i++)
01031 gain_tab2[i+15] = powf (2.0, i * -0.125);
01032
01033
01034 q->weighting_delay[0] = 0;
01035 q->weighting_delay[1] = 7;
01036 q->weighting_delay[2] = 0;
01037 q->weighting_delay[3] = 7;
01038 q->weighting_delay[4] = 0;
01039 q->weighting_delay[5] = 7;
01040
01041 for (i=0; i<4; i++) {
01042 q->matrix_coeff_index_prev[i] = 3;
01043 q->matrix_coeff_index_now[i] = 3;
01044 q->matrix_coeff_index_next[i] = 3;
01045 }
01046
01047 dsputil_init(&dsp, avctx);
01048
01049 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01050 if (!q->pUnits) {
01051 av_free(q->decoded_bytes_buffer);
01052 return AVERROR(ENOMEM);
01053 }
01054
01055 return 0;
01056 }
01057
01058
01059 AVCodec atrac3_decoder =
01060 {
01061 .name = "atrac 3",
01062 .type = CODEC_TYPE_AUDIO,
01063 .id = CODEC_ID_ATRAC3,
01064 .priv_data_size = sizeof(ATRAC3Context),
01065 .init = atrac3_decode_init,
01066 .close = atrac3_decode_close,
01067 .decode = atrac3_decode_frame,
01068 };