00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00035 #include <math.h>
00036 #include <stddef.h>
00037 #include <stdio.h>
00038
00039 #include "avcodec.h"
00040 #include "bitstream.h"
00041 #include "dsputil.h"
00042 #include "bytestream.h"
00043
00044 #include "atrac3data.h"
00045
00046 #define JOINT_STEREO 0x12
00047 #define STEREO 0x2
00048
00049
00050
00051 typedef struct {
00052 int num_gain_data;
00053 int levcode[8];
00054 int loccode[8];
00055 } gain_info;
00056
00057 typedef struct {
00058 gain_info gBlock[4];
00059 } gain_block;
00060
00061 typedef struct {
00062 int pos;
00063 int numCoefs;
00064 float coef[8];
00065 } tonal_component;
00066
00067 typedef struct {
00068 int bandsCoded;
00069 int numComponents;
00070 tonal_component components[64];
00071 float prevFrame[1024];
00072 int gcBlkSwitch;
00073 gain_block gainBlock[2];
00074
00075 DECLARE_ALIGNED_16(float, spectrum[1024]);
00076 DECLARE_ALIGNED_16(float, IMDCT_buf[1024]);
00077
00078 float delayBuf1[46];
00079 float delayBuf2[46];
00080 float delayBuf3[46];
00081 } channel_unit;
00082
00083 typedef struct {
00084 GetBitContext gb;
00086
00087 int channels;
00088 int codingMode;
00089 int bit_rate;
00090 int sample_rate;
00091 int samples_per_channel;
00092 int samples_per_frame;
00093
00094 int bits_per_frame;
00095 int bytes_per_frame;
00096 int pBs;
00097 channel_unit* pUnits;
00099
00100
00101 int matrix_coeff_index_prev[4];
00102 int matrix_coeff_index_now[4];
00103 int matrix_coeff_index_next[4];
00104 int weighting_delay[6];
00106
00107
00108 float outSamples[2048];
00109 uint8_t* decoded_bytes_buffer;
00110 float tempBuf[1070];
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
00193 static void IMLT(float *pInput, float *pOutput, int odd_band)
00194 {
00195 int i;
00196
00197 if (odd_band) {
00207 for (i=0; i<128; i++)
00208 FFSWAP(float, pInput[i], pInput[255-i]);
00209 }
00210
00211 ff_imdct_calc(&mdct_ctx,pOutput,pInput);
00212
00213
00214 dsp.vector_fmul(pOutput,mdct_window,512);
00215
00216 }
00217
00218
00227 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00228 int i, off;
00229 uint32_t c;
00230 const uint32_t* buf;
00231 uint32_t* obuf = (uint32_t*) out;
00232
00233 off = (int)((long)inbuffer & 3);
00234 buf = (const uint32_t*) (inbuffer - off);
00235 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
00236 bytes += 3 + off;
00237 for (i = 0; i < bytes/4; i++)
00238 obuf[i] = c ^ buf[i];
00239
00240 if (off)
00241 av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
00242
00243 return off;
00244 }
00245
00246
00247 static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
00248 float enc_window[256];
00249 float s;
00250 int i;
00251
00252
00253
00254 for (i=0 ; i<256; i++)
00255 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
00256
00257 if (!mdct_window[0])
00258 for (i=0 ; i<256; i++) {
00259 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
00260 mdct_window[511-i] = mdct_window[i];
00261 }
00262
00263
00264 for (i=0 ; i<24; i++) {
00265 s = qmf_48tap_half[i] * 2.0;
00266 qmf_window[i] = s;
00267 qmf_window[47 - i] = s;
00268 }
00269
00270
00271 ff_mdct_init(&mdct_ctx, 9, 1);
00272 }
00273
00278 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
00279 {
00280 ATRAC3Context *q = avctx->priv_data;
00281
00282 av_free(q->pUnits);
00283 av_free(q->decoded_bytes_buffer);
00284
00285 return 0;
00286 }
00287
00298 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
00299 {
00300 int numBits, cnt, code, huffSymb;
00301
00302 if (selector == 1)
00303 numCodes /= 2;
00304
00305 if (codingFlag != 0) {
00306
00307 numBits = CLCLengthTab[selector];
00308
00309 if (selector > 1) {
00310 for (cnt = 0; cnt < numCodes; cnt++) {
00311 if (numBits)
00312 code = get_sbits(gb, numBits);
00313 else
00314 code = 0;
00315 mantissas[cnt] = code;
00316 }
00317 } else {
00318 for (cnt = 0; cnt < numCodes; cnt++) {
00319 if (numBits)
00320 code = get_bits(gb, numBits);
00321 else
00322 code = 0;
00323 mantissas[cnt*2] = seTab_0[code >> 2];
00324 mantissas[cnt*2+1] = seTab_0[code & 3];
00325 }
00326 }
00327 } else {
00328
00329 if (selector != 1) {
00330 for (cnt = 0; cnt < numCodes; cnt++) {
00331 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00332 huffSymb += 1;
00333 code = huffSymb >> 1;
00334 if (huffSymb & 1)
00335 code = -code;
00336 mantissas[cnt] = code;
00337 }
00338 } else {
00339 for (cnt = 0; cnt < numCodes; cnt++) {
00340 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00341 mantissas[cnt*2] = decTable1[huffSymb*2];
00342 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
00343 }
00344 }
00345 }
00346 }
00347
00356 static int decodeSpectrum (GetBitContext *gb, float *pOut)
00357 {
00358 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
00359 int subband_vlc_index[32], SF_idxs[32];
00360 int mantissas[128];
00361 float SF;
00362
00363 numSubbands = get_bits(gb, 5);
00364 codingMode = get_bits1(gb);
00365
00366
00367 for (cnt = 0; cnt <= numSubbands; cnt++)
00368 subband_vlc_index[cnt] = get_bits(gb, 3);
00369
00370
00371 for (cnt = 0; cnt <= numSubbands; cnt++) {
00372 if (subband_vlc_index[cnt] != 0)
00373 SF_idxs[cnt] = get_bits(gb, 6);
00374 }
00375
00376 for (cnt = 0; cnt <= numSubbands; cnt++) {
00377 first = subbandTab[cnt];
00378 last = subbandTab[cnt+1];
00379
00380 subbWidth = last - first;
00381
00382 if (subband_vlc_index[cnt] != 0) {
00383
00384
00385
00386 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
00387
00388
00389 SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
00390
00391
00392 for (pIn=mantissas ; first<last; first++, pIn++)
00393 pOut[first] = *pIn * SF;
00394 } else {
00395
00396 memset(pOut+first, 0, subbWidth*sizeof(float));
00397 }
00398 }
00399
00400
00401 first = subbandTab[cnt];
00402 memset(pOut+first, 0, (1024 - first) * sizeof(float));
00403 return numSubbands;
00404 }
00405
00414 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
00415 {
00416 int i,j,k,cnt;
00417 int components, coding_mode_selector, coding_mode, coded_values_per_component;
00418 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
00419 int band_flags[4], mantissa[8];
00420 float *pCoef;
00421 float scalefactor;
00422 int component_count = 0;
00423
00424 components = get_bits(gb,5);
00425
00426
00427 if (components == 0)
00428 return 0;
00429
00430 coding_mode_selector = get_bits(gb,2);
00431 if (coding_mode_selector == 2)
00432 return -1;
00433
00434 coding_mode = coding_mode_selector & 1;
00435
00436 for (i = 0; i < components; i++) {
00437 for (cnt = 0; cnt <= numBands; cnt++)
00438 band_flags[cnt] = get_bits1(gb);
00439
00440 coded_values_per_component = get_bits(gb,3);
00441
00442 quant_step_index = get_bits(gb,3);
00443 if (quant_step_index <= 1)
00444 return -1;
00445
00446 if (coding_mode_selector == 3)
00447 coding_mode = get_bits1(gb);
00448
00449 for (j = 0; j < (numBands + 1) * 4; j++) {
00450 if (band_flags[j >> 2] == 0)
00451 continue;
00452
00453 coded_components = get_bits(gb,3);
00454
00455 for (k=0; k<coded_components; k++) {
00456 sfIndx = get_bits(gb,6);
00457 if(component_count>=64)
00458 return AVERROR_INVALIDDATA;
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[component_count].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
00587 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00588 {
00589 int cnt, i, lastPos = -1;
00590 float *pIn, *pOut;
00591
00592 for (cnt = 0; cnt < numComponents; cnt++){
00593 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
00594 pIn = pComponent[cnt].coef;
00595 pOut = &(pSpectrum[pComponent[cnt].pos]);
00596
00597 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00598 pOut[i] += pIn[i];
00599 }
00600
00601 return lastPos;
00602 }
00603
00604
00605 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00606
00607 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00608 {
00609 int i, band, nsample, s1, s2;
00610 float c1, c2;
00611 float mc1_l, mc1_r, mc2_l, mc2_r;
00612
00613 for (i=0,band = 0; band < 4*256; band+=256,i++) {
00614 s1 = pPrevCode[i];
00615 s2 = pCurrCode[i];
00616 nsample = 0;
00617
00618 if (s1 != s2) {
00619
00620 mc1_l = matrixCoeffs[s1*2];
00621 mc1_r = matrixCoeffs[s1*2+1];
00622 mc2_l = matrixCoeffs[s2*2];
00623 mc2_r = matrixCoeffs[s2*2+1];
00624
00625
00626 for(; nsample < 8; nsample++) {
00627 c1 = su1[band+nsample];
00628 c2 = su2[band+nsample];
00629 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00630 su1[band+nsample] = c2;
00631 su2[band+nsample] = c1 * 2.0 - c2;
00632 }
00633 }
00634
00635
00636 switch (s2) {
00637 case 0:
00638 for (; nsample < 256; nsample++) {
00639 c1 = su1[band+nsample];
00640 c2 = su2[band+nsample];
00641 su1[band+nsample] = c2 * 2.0;
00642 su2[band+nsample] = (c1 - c2) * 2.0;
00643 }
00644 break;
00645
00646 case 1:
00647 for (; nsample < 256; nsample++) {
00648 c1 = su1[band+nsample];
00649 c2 = su2[band+nsample];
00650 su1[band+nsample] = (c1 + c2) * 2.0;
00651 su2[band+nsample] = c2 * -2.0;
00652 }
00653 break;
00654 case 2:
00655 case 3:
00656 for (; nsample < 256; nsample++) {
00657 c1 = su1[band+nsample];
00658 c2 = su2[band+nsample];
00659 su1[band+nsample] = c1 + c2;
00660 su2[band+nsample] = c1 - c2;
00661 }
00662 break;
00663 default:
00664 assert(0);
00665 }
00666 }
00667 }
00668
00669 static void getChannelWeights (int indx, int flag, float ch[2]){
00670
00671 if (indx == 7) {
00672 ch[0] = 1.0;
00673 ch[1] = 1.0;
00674 } else {
00675 ch[0] = (float)(indx & 7) / 7.0;
00676 ch[1] = sqrt(2 - ch[0]*ch[0]);
00677 if(flag)
00678 FFSWAP(float, ch[0], ch[1]);
00679 }
00680 }
00681
00682 static void channelWeighting (float *su1, float *su2, int *p3)
00683 {
00684 int band, nsample;
00685
00686 float w[2][2];
00687
00688 if (p3[1] != 7 || p3[3] != 7){
00689 getChannelWeights(p3[1], p3[0], w[0]);
00690 getChannelWeights(p3[3], p3[2], w[1]);
00691
00692 for(band = 1; band < 4; band++) {
00693
00694 for(nsample = 0; nsample < 8; nsample++) {
00695 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00696 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00697 }
00698
00699 for(; nsample < 256; nsample++) {
00700 su1[band*256+nsample] *= w[1][0];
00701 su2[band*256+nsample] *= w[1][1];
00702 }
00703 }
00704 }
00705 }
00706
00707
00719 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00720 {
00721 int band, result=0, numSubbands, lastTonal, numBands;
00722
00723 if (codingMode == JOINT_STEREO && channelNum == 1) {
00724 if (get_bits(gb,2) != 3) {
00725 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00726 return -1;
00727 }
00728 } else {
00729 if (get_bits(gb,6) != 0x28) {
00730 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00731 return -1;
00732 }
00733 }
00734
00735
00736 pSnd->bandsCoded = get_bits(gb,2);
00737
00738 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00739 if (result) return result;
00740
00741 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00742 if (pSnd->numComponents == -1) return -1;
00743
00744 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00745
00746
00747 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00748
00749
00750
00751 numBands = (subbandTab[numSubbands] - 1) >> 8;
00752 if (lastTonal >= 0)
00753 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00754
00755
00756
00757 for (band=0; band<4; band++) {
00758
00759 if (band <= numBands) {
00760 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00761 } else
00762 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00763
00764
00765 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
00766 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
00767 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
00768 }
00769
00770
00771 pSnd->gcBlkSwitch ^= 1;
00772
00773 return 0;
00774 }
00775
00783 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
00784 {
00785 int result, i;
00786 float *p1, *p2, *p3, *p4;
00787 uint8_t *ptr1;
00788
00789 if (q->codingMode == JOINT_STEREO) {
00790
00791
00792
00793 init_get_bits(&q->gb,databuf,q->bits_per_frame);
00794
00795 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
00796 if (result != 0)
00797 return (result);
00798
00799
00800
00801 if (databuf == q->decoded_bytes_buffer) {
00802 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00803 ptr1 = q->decoded_bytes_buffer;
00804 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00805 FFSWAP(uint8_t,*ptr1,*ptr2);
00806 }
00807 } else {
00808 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00809 for (i = 0; i < q->bytes_per_frame; i++)
00810 q->decoded_bytes_buffer[i] = *ptr2--;
00811 }
00812
00813
00814 ptr1 = q->decoded_bytes_buffer;
00815 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00816 if (i >= q->bytes_per_frame)
00817 return -1;
00818 }
00819
00820
00821
00822 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00823
00824
00825 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00826 q->weighting_delay[4] = get_bits1(&q->gb);
00827 q->weighting_delay[5] = get_bits(&q->gb,3);
00828
00829 for (i = 0; i < 4; i++) {
00830 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00831 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00832 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00833 }
00834
00835
00836 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
00837 if (result != 0)
00838 return (result);
00839
00840
00841 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00842
00843 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
00844
00845 } else {
00846
00847
00848 for (i=0 ; i<q->channels ; i++) {
00849
00850
00851 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
00852
00853 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
00854 if (result != 0)
00855 return (result);
00856 }
00857 }
00858
00859
00860 p1= q->outSamples;
00861 for (i=0 ; i<q->channels ; i++) {
00862 p2= p1+256;
00863 p3= p2+256;
00864 p4= p3+256;
00865 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00866 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00867 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00868 p1 +=1024;
00869 }
00870
00871 return 0;
00872 }
00873
00874
00881 static int atrac3_decode_frame(AVCodecContext *avctx,
00882 void *data, int *data_size,
00883 const uint8_t *buf, int buf_size) {
00884 ATRAC3Context *q = avctx->priv_data;
00885 int result = 0, i;
00886 const uint8_t* databuf;
00887 int16_t* samples = data;
00888
00889 if (buf_size < avctx->block_align)
00890 return buf_size;
00891
00892
00893 if (q->scrambled_stream) {
00894 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00895 databuf = q->decoded_bytes_buffer;
00896 } else {
00897 databuf = buf;
00898 }
00899
00900 result = decodeFrame(q, databuf);
00901
00902 if (result != 0) {
00903 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00904 return -1;
00905 }
00906
00907 if (q->channels == 1) {
00908
00909 for (i = 0; i<1024; i++)
00910 samples[i] = av_clip_int16(round(q->outSamples[i]));
00911 *data_size = 1024 * sizeof(int16_t);
00912 } else {
00913
00914 for (i = 0; i < 1024; i++) {
00915 samples[i*2] = av_clip_int16(round(q->outSamples[i]));
00916 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
00917 }
00918 *data_size = 2048 * sizeof(int16_t);
00919 }
00920
00921 return avctx->block_align;
00922 }
00923
00924
00931 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00932 {
00933 int i;
00934 const uint8_t *edata_ptr = avctx->extradata;
00935 ATRAC3Context *q = avctx->priv_data;
00936
00937
00938 q->sample_rate = avctx->sample_rate;
00939 q->channels = avctx->channels;
00940 q->bit_rate = avctx->bit_rate;
00941 q->bits_per_frame = avctx->block_align * 8;
00942 q->bytes_per_frame = avctx->block_align;
00943
00944
00945 if (avctx->extradata_size == 14) {
00946
00947 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));
00948 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00949 q->codingMode = bytestream_get_le16(&edata_ptr);
00950 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));
00951 q->frame_factor = bytestream_get_le16(&edata_ptr);
00952 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));
00953
00954
00955 q->samples_per_frame = 1024 * q->channels;
00956 q->atrac3version = 4;
00957 q->delay = 0x88E;
00958 if (q->codingMode)
00959 q->codingMode = JOINT_STEREO;
00960 else
00961 q->codingMode = STEREO;
00962
00963 q->scrambled_stream = 0;
00964
00965 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)) {
00966 } else {
00967 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);
00968 return -1;
00969 }
00970
00971 } else if (avctx->extradata_size == 10) {
00972
00973 q->atrac3version = bytestream_get_be32(&edata_ptr);
00974 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00975 q->delay = bytestream_get_be16(&edata_ptr);
00976 q->codingMode = bytestream_get_be16(&edata_ptr);
00977
00978 q->samples_per_channel = q->samples_per_frame / q->channels;
00979 q->scrambled_stream = 1;
00980
00981 } else {
00982 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00983 }
00984
00985
00986 if (q->atrac3version != 4) {
00987 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00988 return -1;
00989 }
00990
00991 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
00992 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00993 return -1;
00994 }
00995
00996 if (q->delay != 0x88E) {
00997 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00998 return -1;
00999 }
01000
01001 if (q->codingMode == STEREO) {
01002 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
01003 } else if (q->codingMode == JOINT_STEREO) {
01004 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
01005 } else {
01006 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
01007 return -1;
01008 }
01009
01010 if (avctx->channels <= 0 || avctx->channels > 2 ) {
01011 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
01012 return -1;
01013 }
01014
01015
01016 if(avctx->block_align >= UINT_MAX/2)
01017 return -1;
01018
01019
01020
01021 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
01022 return AVERROR(ENOMEM);
01023
01024
01025
01026 for (i=0 ; i<7 ; i++) {
01027 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
01028 huff_bits[i], 1, 1,
01029 huff_codes[i], 1, 1, INIT_VLC_USE_STATIC);
01030 }
01031
01032 init_atrac3_transforms(q);
01033
01034
01035 for (i=0 ; i<64 ; i++)
01036 SFTable[i] = pow(2.0, (i - 15) / 3.0);
01037
01038
01039 for (i=0 ; i<16 ; i++)
01040 gain_tab1[i] = powf (2.0, (4 - i));
01041
01042 for (i=-15 ; i<16 ; i++)
01043 gain_tab2[i+15] = powf (2.0, i * -0.125);
01044
01045
01046 q->weighting_delay[0] = 0;
01047 q->weighting_delay[1] = 7;
01048 q->weighting_delay[2] = 0;
01049 q->weighting_delay[3] = 7;
01050 q->weighting_delay[4] = 0;
01051 q->weighting_delay[5] = 7;
01052
01053 for (i=0; i<4; i++) {
01054 q->matrix_coeff_index_prev[i] = 3;
01055 q->matrix_coeff_index_now[i] = 3;
01056 q->matrix_coeff_index_next[i] = 3;
01057 }
01058
01059 dsputil_init(&dsp, avctx);
01060
01061 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01062 if (!q->pUnits) {
01063 av_free(q->decoded_bytes_buffer);
01064 return AVERROR(ENOMEM);
01065 }
01066
01067 avctx->sample_fmt = SAMPLE_FMT_S16;
01068 return 0;
01069 }
01070
01071
01072 AVCodec atrac3_decoder =
01073 {
01074 .name = "atrac3",
01075 .type = CODEC_TYPE_AUDIO,
01076 .id = CODEC_ID_ATRAC3,
01077 .priv_data_size = sizeof(ATRAC3Context),
01078 .init = atrac3_decode_init,
01079 .close = atrac3_decode_close,
01080 .decode = atrac3_decode_frame,
01081 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01082 };