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 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00458 max_coded_values = 1024 - pComponent[component_count].pos;
00459 coded_values = coded_values_per_component + 1;
00460 coded_values = FFMIN(max_coded_values,coded_values);
00461
00462 scalefactor = SFTable[sfIndx] * iMaxQuant[quant_step_index];
00463
00464 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00465
00466 pComponent[component_count].numCoefs = coded_values;
00467
00468
00469 pCoef = pComponent[component_count].coef;
00470 for (cnt = 0; cnt < coded_values; cnt++)
00471 pCoef[cnt] = mantissa[cnt] * scalefactor;
00472
00473 component_count++;
00474 }
00475 }
00476 }
00477
00478 return component_count;
00479 }
00480
00489 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00490 {
00491 int i, cf, numData;
00492 int *pLevel, *pLoc;
00493
00494 gain_info *pGain = pGb->gBlock;
00495
00496 for (i=0 ; i<=numBands; i++)
00497 {
00498 numData = get_bits(gb,3);
00499 pGain[i].num_gain_data = numData;
00500 pLevel = pGain[i].levcode;
00501 pLoc = pGain[i].loccode;
00502
00503 for (cf = 0; cf < numData; cf++){
00504 pLevel[cf]= get_bits(gb,4);
00505 pLoc [cf]= get_bits(gb,5);
00506 if(cf && pLoc[cf] <= pLoc[cf-1])
00507 return -1;
00508 }
00509 }
00510
00511
00512 for (; i<4 ; i++)
00513 pGain[i].num_gain_data = 0;
00514
00515 return 0;
00516 }
00517
00528 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00529 {
00530
00531 float gain1, gain2, gain_inc;
00532 int cnt, numdata, nsample, startLoc, endLoc;
00533
00534
00535 if (pGain2->num_gain_data == 0)
00536 gain1 = 1.0;
00537 else
00538 gain1 = gain_tab1[pGain2->levcode[0]];
00539
00540 if (pGain1->num_gain_data == 0) {
00541 for (cnt = 0; cnt < 256; cnt++)
00542 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00543 } else {
00544 numdata = pGain1->num_gain_data;
00545 pGain1->loccode[numdata] = 32;
00546 pGain1->levcode[numdata] = 4;
00547
00548 nsample = 0;
00549
00550 for (cnt = 0; cnt < numdata; cnt++) {
00551 startLoc = pGain1->loccode[cnt] * 8;
00552 endLoc = startLoc + 8;
00553
00554 gain2 = gain_tab1[pGain1->levcode[cnt]];
00555 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00556
00557
00558 for (; nsample < startLoc; nsample++)
00559 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00560
00561
00562 for (; nsample < endLoc; nsample++) {
00563 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00564 gain2 *= gain_inc;
00565 }
00566 }
00567
00568 for (; nsample < 256; nsample++)
00569 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00570 }
00571
00572
00573 memcpy(pPrev, &pIn[256], 256*sizeof(float));
00574 }
00575
00585 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00586 {
00587 int cnt, i, lastPos = -1;
00588 float *pIn, *pOut;
00589
00590 for (cnt = 0; cnt < numComponents; cnt++){
00591 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
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 return lastPos;
00600 }
00601
00602
00603 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00604
00605 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00606 {
00607 int i, band, nsample, s1, s2;
00608 float c1, c2;
00609 float mc1_l, mc1_r, mc2_l, mc2_r;
00610
00611 for (i=0,band = 0; band < 4*256; band+=256,i++) {
00612 s1 = pPrevCode[i];
00613 s2 = pCurrCode[i];
00614 nsample = 0;
00615
00616 if (s1 != s2) {
00617
00618 mc1_l = matrixCoeffs[s1*2];
00619 mc1_r = matrixCoeffs[s1*2+1];
00620 mc2_l = matrixCoeffs[s2*2];
00621 mc2_r = matrixCoeffs[s2*2+1];
00622
00623
00624 for(; nsample < 8; nsample++) {
00625 c1 = su1[band+nsample];
00626 c2 = su2[band+nsample];
00627 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00628 su1[band+nsample] = c2;
00629 su2[band+nsample] = c1 * 2.0 - c2;
00630 }
00631 }
00632
00633
00634 switch (s2) {
00635 case 0:
00636 for (; nsample < 256; nsample++) {
00637 c1 = su1[band+nsample];
00638 c2 = su2[band+nsample];
00639 su1[band+nsample] = c2 * 2.0;
00640 su2[band+nsample] = (c1 - c2) * 2.0;
00641 }
00642 break;
00643
00644 case 1:
00645 for (; nsample < 256; nsample++) {
00646 c1 = su1[band+nsample];
00647 c2 = su2[band+nsample];
00648 su1[band+nsample] = (c1 + c2) * 2.0;
00649 su2[band+nsample] = c2 * -2.0;
00650 }
00651 break;
00652 case 2:
00653 case 3:
00654 for (; nsample < 256; nsample++) {
00655 c1 = su1[band+nsample];
00656 c2 = su2[band+nsample];
00657 su1[band+nsample] = c1 + c2;
00658 su2[band+nsample] = c1 - c2;
00659 }
00660 break;
00661 default:
00662 assert(0);
00663 }
00664 }
00665 }
00666
00667 static void getChannelWeights (int indx, int flag, float ch[2]){
00668
00669 if (indx == 7) {
00670 ch[0] = 1.0;
00671 ch[1] = 1.0;
00672 } else {
00673 ch[0] = (float)(indx & 7) / 7.0;
00674 ch[1] = sqrt(2 - ch[0]*ch[0]);
00675 if(flag)
00676 FFSWAP(float, ch[0], ch[1]);
00677 }
00678 }
00679
00680 static void channelWeighting (float *su1, float *su2, int *p3)
00681 {
00682 int band, nsample;
00683
00684 float w[2][2];
00685
00686 if (p3[1] != 7 || p3[3] != 7){
00687 getChannelWeights(p3[1], p3[0], w[0]);
00688 getChannelWeights(p3[3], p3[2], w[1]);
00689
00690 for(band = 1; band < 4; band++) {
00691
00692 for(nsample = 0; nsample < 8; nsample++) {
00693 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00694 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00695 }
00696
00697 for(; nsample < 256; nsample++) {
00698 su1[band*256+nsample] *= w[1][0];
00699 su2[band*256+nsample] *= w[1][1];
00700 }
00701 }
00702 }
00703 }
00704
00705
00717 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00718 {
00719 int band, result=0, numSubbands, lastTonal, numBands;
00720
00721 if (codingMode == JOINT_STEREO && channelNum == 1) {
00722 if (get_bits(gb,2) != 3) {
00723 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00724 return -1;
00725 }
00726 } else {
00727 if (get_bits(gb,6) != 0x28) {
00728 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00729 return -1;
00730 }
00731 }
00732
00733
00734 pSnd->bandsCoded = get_bits(gb,2);
00735
00736 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00737 if (result) return result;
00738
00739 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00740 if (pSnd->numComponents == -1) return -1;
00741
00742 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00743
00744
00745 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00746
00747
00748
00749 numBands = (subbandTab[numSubbands] - 1) >> 8;
00750 if (lastTonal >= 0)
00751 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00752
00753
00754
00755 for (band=0; band<4; band++) {
00756
00757 if (band <= numBands) {
00758 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00759 } else
00760 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00761
00762
00763 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
00764 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
00765 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
00766 }
00767
00768
00769 pSnd->gcBlkSwitch ^= 1;
00770
00771 return 0;
00772 }
00773
00781 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
00782 {
00783 int result, i;
00784 float *p1, *p2, *p3, *p4;
00785 uint8_t *ptr1;
00786
00787 if (q->codingMode == JOINT_STEREO) {
00788
00789
00790
00791 init_get_bits(&q->gb,databuf,q->bits_per_frame);
00792
00793 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
00794 if (result != 0)
00795 return (result);
00796
00797
00798
00799 if (databuf == q->decoded_bytes_buffer) {
00800 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00801 ptr1 = q->decoded_bytes_buffer;
00802 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00803 FFSWAP(uint8_t,*ptr1,*ptr2);
00804 }
00805 } else {
00806 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00807 for (i = 0; i < q->bytes_per_frame; i++)
00808 q->decoded_bytes_buffer[i] = *ptr2--;
00809 }
00810
00811
00812 ptr1 = q->decoded_bytes_buffer;
00813 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00814 if (i >= q->bytes_per_frame)
00815 return -1;
00816 }
00817
00818
00819
00820 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00821
00822
00823 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00824 q->weighting_delay[4] = get_bits1(&q->gb);
00825 q->weighting_delay[5] = get_bits(&q->gb,3);
00826
00827 for (i = 0; i < 4; i++) {
00828 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00829 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00830 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00831 }
00832
00833
00834 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
00835 if (result != 0)
00836 return (result);
00837
00838
00839 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00840
00841 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
00842
00843 } else {
00844
00845
00846 for (i=0 ; i<q->channels ; i++) {
00847
00848
00849 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
00850
00851 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
00852 if (result != 0)
00853 return (result);
00854 }
00855 }
00856
00857
00858 p1= q->outSamples;
00859 for (i=0 ; i<q->channels ; i++) {
00860 p2= p1+256;
00861 p3= p2+256;
00862 p4= p3+256;
00863 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00864 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00865 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00866 p1 +=1024;
00867 }
00868
00869 return 0;
00870 }
00871
00872
00879 static int atrac3_decode_frame(AVCodecContext *avctx,
00880 void *data, int *data_size,
00881 const uint8_t *buf, int buf_size) {
00882 ATRAC3Context *q = avctx->priv_data;
00883 int result = 0, i;
00884 const uint8_t* databuf;
00885 int16_t* samples = data;
00886
00887 if (buf_size < avctx->block_align)
00888 return buf_size;
00889
00890
00891 if (q->scrambled_stream) {
00892 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00893 databuf = q->decoded_bytes_buffer;
00894 } else {
00895 databuf = buf;
00896 }
00897
00898 result = decodeFrame(q, databuf);
00899
00900 if (result != 0) {
00901 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00902 return -1;
00903 }
00904
00905 if (q->channels == 1) {
00906
00907 for (i = 0; i<1024; i++)
00908 samples[i] = av_clip_int16(round(q->outSamples[i]));
00909 *data_size = 1024 * sizeof(int16_t);
00910 } else {
00911
00912 for (i = 0; i < 1024; i++) {
00913 samples[i*2] = av_clip_int16(round(q->outSamples[i]));
00914 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
00915 }
00916 *data_size = 2048 * sizeof(int16_t);
00917 }
00918
00919 return avctx->block_align;
00920 }
00921
00922
00929 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00930 {
00931 int i;
00932 const uint8_t *edata_ptr = avctx->extradata;
00933 ATRAC3Context *q = avctx->priv_data;
00934
00935
00936 q->sample_rate = avctx->sample_rate;
00937 q->channels = avctx->channels;
00938 q->bit_rate = avctx->bit_rate;
00939 q->bits_per_frame = avctx->block_align * 8;
00940 q->bytes_per_frame = avctx->block_align;
00941
00942
00943 if (avctx->extradata_size == 14) {
00944
00945 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));
00946 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00947 q->codingMode = bytestream_get_le16(&edata_ptr);
00948 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));
00949 q->frame_factor = bytestream_get_le16(&edata_ptr);
00950 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));
00951
00952
00953 q->samples_per_frame = 1024 * q->channels;
00954 q->atrac3version = 4;
00955 q->delay = 0x88E;
00956 if (q->codingMode)
00957 q->codingMode = JOINT_STEREO;
00958 else
00959 q->codingMode = STEREO;
00960
00961 q->scrambled_stream = 0;
00962
00963 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)) {
00964 } else {
00965 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);
00966 return -1;
00967 }
00968
00969 } else if (avctx->extradata_size == 10) {
00970
00971 q->atrac3version = bytestream_get_be32(&edata_ptr);
00972 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00973 q->delay = bytestream_get_be16(&edata_ptr);
00974 q->codingMode = bytestream_get_be16(&edata_ptr);
00975
00976 q->samples_per_channel = q->samples_per_frame / q->channels;
00977 q->scrambled_stream = 1;
00978
00979 } else {
00980 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00981 }
00982
00983
00984 if (q->atrac3version != 4) {
00985 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00986 return -1;
00987 }
00988
00989 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
00990 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00991 return -1;
00992 }
00993
00994 if (q->delay != 0x88E) {
00995 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00996 return -1;
00997 }
00998
00999 if (q->codingMode == STEREO) {
01000 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
01001 } else if (q->codingMode == JOINT_STEREO) {
01002 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
01003 } else {
01004 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
01005 return -1;
01006 }
01007
01008 if (avctx->channels <= 0 || avctx->channels > 2 ) {
01009 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
01010 return -1;
01011 }
01012
01013
01014 if(avctx->block_align >= UINT_MAX/2)
01015 return -1;
01016
01017
01018
01019 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
01020 return AVERROR(ENOMEM);
01021
01022
01023
01024 for (i=0 ; i<7 ; i++) {
01025 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
01026 huff_bits[i], 1, 1,
01027 huff_codes[i], 1, 1, INIT_VLC_USE_STATIC);
01028 }
01029
01030 init_atrac3_transforms(q);
01031
01032
01033 for (i=0 ; i<64 ; i++)
01034 SFTable[i] = pow(2.0, (i - 15) / 3.0);
01035
01036
01037 for (i=0 ; i<16 ; i++)
01038 gain_tab1[i] = powf (2.0, (4 - i));
01039
01040 for (i=-15 ; i<16 ; i++)
01041 gain_tab2[i+15] = powf (2.0, i * -0.125);
01042
01043
01044 q->weighting_delay[0] = 0;
01045 q->weighting_delay[1] = 7;
01046 q->weighting_delay[2] = 0;
01047 q->weighting_delay[3] = 7;
01048 q->weighting_delay[4] = 0;
01049 q->weighting_delay[5] = 7;
01050
01051 for (i=0; i<4; i++) {
01052 q->matrix_coeff_index_prev[i] = 3;
01053 q->matrix_coeff_index_now[i] = 3;
01054 q->matrix_coeff_index_next[i] = 3;
01055 }
01056
01057 dsputil_init(&dsp, avctx);
01058
01059 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01060 if (!q->pUnits) {
01061 av_free(q->decoded_bytes_buffer);
01062 return AVERROR(ENOMEM);
01063 }
01064
01065 avctx->sample_fmt = SAMPLE_FMT_S16;
01066 return 0;
01067 }
01068
01069
01070 AVCodec atrac3_decoder =
01071 {
01072 .name = "atrac3",
01073 .type = CODEC_TYPE_AUDIO,
01074 .id = CODEC_ID_ATRAC3,
01075 .priv_data_size = sizeof(ATRAC3Context),
01076 .init = atrac3_decode_init,
01077 .close = atrac3_decode_close,
01078 .decode = atrac3_decode_frame,
01079 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01080 };