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 "libavutil/float_dsp.h"
00040 #include "libavutil/libm.h"
00041 #include "avcodec.h"
00042 #include "get_bits.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
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 AVFloatDSPContext fdsp;
00130 } ATRAC3Context;
00131
00132 static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE];
00133 static VLC spectral_coeff_tab[7];
00134 static float gain_tab1[16];
00135 static float gain_tab2[31];
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
00168 q->fdsp.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 c = av_be2ne32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
00190 bytes += 3 + off;
00191 for (i = 0; i < bytes/4; i++)
00192 obuf[i] = c ^ buf[i];
00193
00194 if (off)
00195 av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
00196
00197 return off;
00198 }
00199
00200
00201 static av_cold int init_atrac3_transforms(ATRAC3Context *q, int is_float) {
00202 float enc_window[256];
00203 int i;
00204
00205
00206
00207 for (i=0 ; i<256; i++)
00208 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
00209
00210 if (!mdct_window[0])
00211 for (i=0 ; i<256; i++) {
00212 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
00213 mdct_window[511-i] = mdct_window[i];
00214 }
00215
00216
00217 return ff_mdct_init(&q->mdct_ctx, 9, 1, is_float ? 1.0 / 32768 : 1.0);
00218 }
00219
00224 static av_cold int atrac3_decode_close(AVCodecContext *avctx)
00225 {
00226 ATRAC3Context *q = avctx->priv_data;
00227
00228 av_free(q->pUnits);
00229 av_free(q->decoded_bytes_buffer);
00230 av_freep(&q->outSamples[0]);
00231
00232 ff_mdct_end(&q->mdct_ctx);
00233
00234 return 0;
00235 }
00236
00247 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
00248 {
00249 int numBits, cnt, code, huffSymb;
00250
00251 if (selector == 1)
00252 numCodes /= 2;
00253
00254 if (codingFlag != 0) {
00255
00256 numBits = CLCLengthTab[selector];
00257
00258 if (selector > 1) {
00259 for (cnt = 0; cnt < numCodes; cnt++) {
00260 if (numBits)
00261 code = get_sbits(gb, numBits);
00262 else
00263 code = 0;
00264 mantissas[cnt] = code;
00265 }
00266 } else {
00267 for (cnt = 0; cnt < numCodes; cnt++) {
00268 if (numBits)
00269 code = get_bits(gb, numBits);
00270 else
00271 code = 0;
00272 mantissas[cnt*2] = seTab_0[code >> 2];
00273 mantissas[cnt*2+1] = seTab_0[code & 3];
00274 }
00275 }
00276 } else {
00277
00278 if (selector != 1) {
00279 for (cnt = 0; cnt < numCodes; cnt++) {
00280 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00281 huffSymb += 1;
00282 code = huffSymb >> 1;
00283 if (huffSymb & 1)
00284 code = -code;
00285 mantissas[cnt] = code;
00286 }
00287 } else {
00288 for (cnt = 0; cnt < numCodes; cnt++) {
00289 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
00290 mantissas[cnt*2] = decTable1[huffSymb*2];
00291 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
00292 }
00293 }
00294 }
00295 }
00296
00305 static int decodeSpectrum (GetBitContext *gb, float *pOut)
00306 {
00307 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
00308 int subband_vlc_index[32], SF_idxs[32];
00309 int mantissas[128];
00310 float SF;
00311
00312 numSubbands = get_bits(gb, 5);
00313 codingMode = get_bits1(gb);
00314
00315
00316 for (cnt = 0; cnt <= numSubbands; cnt++)
00317 subband_vlc_index[cnt] = get_bits(gb, 3);
00318
00319
00320 for (cnt = 0; cnt <= numSubbands; cnt++) {
00321 if (subband_vlc_index[cnt] != 0)
00322 SF_idxs[cnt] = get_bits(gb, 6);
00323 }
00324
00325 for (cnt = 0; cnt <= numSubbands; cnt++) {
00326 first = subbandTab[cnt];
00327 last = subbandTab[cnt+1];
00328
00329 subbWidth = last - first;
00330
00331 if (subband_vlc_index[cnt] != 0) {
00332
00333
00334
00335 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
00336
00337
00338 SF = ff_atrac_sf_table[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
00339
00340
00341 for (pIn=mantissas ; first<last; first++, pIn++)
00342 pOut[first] = *pIn * SF;
00343 } else {
00344
00345 memset(pOut+first, 0, subbWidth*sizeof(float));
00346 }
00347 }
00348
00349
00350 first = subbandTab[cnt];
00351 memset(pOut+first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
00352 return numSubbands;
00353 }
00354
00363 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
00364 {
00365 int i,j,k,cnt;
00366 int components, coding_mode_selector, coding_mode, coded_values_per_component;
00367 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
00368 int band_flags[4], mantissa[8];
00369 float *pCoef;
00370 float scalefactor;
00371 int component_count = 0;
00372
00373 components = get_bits(gb,5);
00374
00375
00376 if (components == 0)
00377 return 0;
00378
00379 coding_mode_selector = get_bits(gb,2);
00380 if (coding_mode_selector == 2)
00381 return AVERROR_INVALIDDATA;
00382
00383 coding_mode = coding_mode_selector & 1;
00384
00385 for (i = 0; i < components; i++) {
00386 for (cnt = 0; cnt <= numBands; cnt++)
00387 band_flags[cnt] = get_bits1(gb);
00388
00389 coded_values_per_component = get_bits(gb,3);
00390
00391 quant_step_index = get_bits(gb,3);
00392 if (quant_step_index <= 1)
00393 return AVERROR_INVALIDDATA;
00394
00395 if (coding_mode_selector == 3)
00396 coding_mode = get_bits1(gb);
00397
00398 for (j = 0; j < (numBands + 1) * 4; j++) {
00399 if (band_flags[j >> 2] == 0)
00400 continue;
00401
00402 coded_components = get_bits(gb,3);
00403
00404 for (k=0; k<coded_components; k++) {
00405 sfIndx = get_bits(gb,6);
00406 if (component_count >= 64)
00407 return AVERROR_INVALIDDATA;
00408 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00409 max_coded_values = SAMPLES_PER_FRAME - pComponent[component_count].pos;
00410 coded_values = coded_values_per_component + 1;
00411 coded_values = FFMIN(max_coded_values,coded_values);
00412
00413 scalefactor = ff_atrac_sf_table[sfIndx] * iMaxQuant[quant_step_index];
00414
00415 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00416
00417 pComponent[component_count].numCoefs = coded_values;
00418
00419
00420 pCoef = pComponent[component_count].coef;
00421 for (cnt = 0; cnt < coded_values; cnt++)
00422 pCoef[cnt] = mantissa[cnt] * scalefactor;
00423
00424 component_count++;
00425 }
00426 }
00427 }
00428
00429 return component_count;
00430 }
00431
00440 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00441 {
00442 int i, cf, numData;
00443 int *pLevel, *pLoc;
00444
00445 gain_info *pGain = pGb->gBlock;
00446
00447 for (i=0 ; i<=numBands; i++)
00448 {
00449 numData = get_bits(gb,3);
00450 pGain[i].num_gain_data = numData;
00451 pLevel = pGain[i].levcode;
00452 pLoc = pGain[i].loccode;
00453
00454 for (cf = 0; cf < numData; cf++){
00455 pLevel[cf]= get_bits(gb,4);
00456 pLoc [cf]= get_bits(gb,5);
00457 if(cf && pLoc[cf] <= pLoc[cf-1])
00458 return AVERROR_INVALIDDATA;
00459 }
00460 }
00461
00462
00463 for (; i<4 ; i++)
00464 pGain[i].num_gain_data = 0;
00465
00466 return 0;
00467 }
00468
00479 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00480 {
00481
00482 float gain1, gain2, gain_inc;
00483 int cnt, numdata, nsample, startLoc, endLoc;
00484
00485
00486 if (pGain2->num_gain_data == 0)
00487 gain1 = 1.0;
00488 else
00489 gain1 = gain_tab1[pGain2->levcode[0]];
00490
00491 if (pGain1->num_gain_data == 0) {
00492 for (cnt = 0; cnt < 256; cnt++)
00493 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00494 } else {
00495 numdata = pGain1->num_gain_data;
00496 pGain1->loccode[numdata] = 32;
00497 pGain1->levcode[numdata] = 4;
00498
00499 nsample = 0;
00500
00501 for (cnt = 0; cnt < numdata; cnt++) {
00502 startLoc = pGain1->loccode[cnt] * 8;
00503 endLoc = startLoc + 8;
00504
00505 gain2 = gain_tab1[pGain1->levcode[cnt]];
00506 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00507
00508
00509 for (; nsample < startLoc; nsample++)
00510 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00511
00512
00513 for (; nsample < endLoc; nsample++) {
00514 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00515 gain2 *= gain_inc;
00516 }
00517 }
00518
00519 for (; nsample < 256; nsample++)
00520 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00521 }
00522
00523
00524 memcpy(pPrev, &pIn[256], 256*sizeof(float));
00525 }
00526
00536 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00537 {
00538 int cnt, i, lastPos = -1;
00539 float *pIn, *pOut;
00540
00541 for (cnt = 0; cnt < numComponents; cnt++){
00542 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
00543 pIn = pComponent[cnt].coef;
00544 pOut = &(pSpectrum[pComponent[cnt].pos]);
00545
00546 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00547 pOut[i] += pIn[i];
00548 }
00549
00550 return lastPos;
00551 }
00552
00553
00554 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00555
00556 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00557 {
00558 int i, band, nsample, s1, s2;
00559 float c1, c2;
00560 float mc1_l, mc1_r, mc2_l, mc2_r;
00561
00562 for (i=0,band = 0; band < 4*256; band+=256,i++) {
00563 s1 = pPrevCode[i];
00564 s2 = pCurrCode[i];
00565 nsample = 0;
00566
00567 if (s1 != s2) {
00568
00569 mc1_l = matrixCoeffs[s1*2];
00570 mc1_r = matrixCoeffs[s1*2+1];
00571 mc2_l = matrixCoeffs[s2*2];
00572 mc2_r = matrixCoeffs[s2*2+1];
00573
00574
00575 for(; nsample < 8; nsample++) {
00576 c1 = su1[band+nsample];
00577 c2 = su2[band+nsample];
00578 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00579 su1[band+nsample] = c2;
00580 su2[band+nsample] = c1 * 2.0 - c2;
00581 }
00582 }
00583
00584
00585 switch (s2) {
00586 case 0:
00587 for (; nsample < 256; nsample++) {
00588 c1 = su1[band+nsample];
00589 c2 = su2[band+nsample];
00590 su1[band+nsample] = c2 * 2.0;
00591 su2[band+nsample] = (c1 - c2) * 2.0;
00592 }
00593 break;
00594
00595 case 1:
00596 for (; nsample < 256; nsample++) {
00597 c1 = su1[band+nsample];
00598 c2 = su2[band+nsample];
00599 su1[band+nsample] = (c1 + c2) * 2.0;
00600 su2[band+nsample] = c2 * -2.0;
00601 }
00602 break;
00603 case 2:
00604 case 3:
00605 for (; nsample < 256; nsample++) {
00606 c1 = su1[band+nsample];
00607 c2 = su2[band+nsample];
00608 su1[band+nsample] = c1 + c2;
00609 su2[band+nsample] = c1 - c2;
00610 }
00611 break;
00612 default:
00613 av_assert1(0);
00614 }
00615 }
00616 }
00617
00618 static void getChannelWeights (int indx, int flag, float ch[2]){
00619
00620 if (indx == 7) {
00621 ch[0] = 1.0;
00622 ch[1] = 1.0;
00623 } else {
00624 ch[0] = (float)(indx & 7) / 7.0;
00625 ch[1] = sqrt(2 - ch[0]*ch[0]);
00626 if(flag)
00627 FFSWAP(float, ch[0], ch[1]);
00628 }
00629 }
00630
00631 static void channelWeighting (float *su1, float *su2, int *p3)
00632 {
00633 int band, nsample;
00634
00635 float w[2][2];
00636
00637 if (p3[1] != 7 || p3[3] != 7){
00638 getChannelWeights(p3[1], p3[0], w[0]);
00639 getChannelWeights(p3[3], p3[2], w[1]);
00640
00641 for(band = 1; band < 4; band++) {
00642
00643 for(nsample = 0; nsample < 8; nsample++) {
00644 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00645 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00646 }
00647
00648 for(; nsample < 256; nsample++) {
00649 su1[band*256+nsample] *= w[1][0];
00650 su2[band*256+nsample] *= w[1][1];
00651 }
00652 }
00653 }
00654 }
00655
00656
00668 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00669 {
00670 int band, result=0, numSubbands, lastTonal, numBands;
00671
00672 if (codingMode == JOINT_STEREO && channelNum == 1) {
00673 if (get_bits(gb,2) != 3) {
00674 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00675 return AVERROR_INVALIDDATA;
00676 }
00677 } else {
00678 if (get_bits(gb,6) != 0x28) {
00679 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00680 return AVERROR_INVALIDDATA;
00681 }
00682 }
00683
00684
00685 pSnd->bandsCoded = get_bits(gb,2);
00686
00687 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00688 if (result) return result;
00689
00690 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00691 if (pSnd->numComponents == -1) return -1;
00692
00693 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00694
00695
00696 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00697
00698
00699
00700 numBands = (subbandTab[numSubbands] - 1) >> 8;
00701 if (lastTonal >= 0)
00702 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00703
00704
00705
00706 for (band=0; band<4; band++) {
00707
00708 if (band <= numBands) {
00709 IMLT(q, &(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00710 } else
00711 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00712
00713
00714 gainCompensateAndOverlap(pSnd->IMDCT_buf, &pSnd->prevFrame[band * 256],
00715 &pOut[band * 256],
00716 &pSnd->gainBlock[1 - pSnd->gcBlkSwitch].gBlock[band],
00717 &pSnd->gainBlock[ pSnd->gcBlkSwitch].gBlock[band]);
00718 }
00719
00720
00721 pSnd->gcBlkSwitch ^= 1;
00722
00723 return 0;
00724 }
00725
00733 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf,
00734 float **out_samples)
00735 {
00736 int result, i;
00737 float *p1, *p2, *p3, *p4;
00738 uint8_t *ptr1;
00739
00740 if (q->codingMode == JOINT_STEREO) {
00741
00742
00743
00744 init_get_bits(&q->gb,databuf,q->bits_per_frame);
00745
00746 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, out_samples[0], 0, JOINT_STEREO);
00747 if (result != 0)
00748 return result;
00749
00750
00751
00752 if (databuf == q->decoded_bytes_buffer) {
00753 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00754 ptr1 = q->decoded_bytes_buffer;
00755 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00756 FFSWAP(uint8_t,*ptr1,*ptr2);
00757 }
00758 } else {
00759 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00760 for (i = 0; i < q->bytes_per_frame; i++)
00761 q->decoded_bytes_buffer[i] = *ptr2--;
00762 }
00763
00764
00765 ptr1 = q->decoded_bytes_buffer;
00766 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00767 if (i >= q->bytes_per_frame)
00768 return AVERROR_INVALIDDATA;
00769 }
00770
00771
00772
00773 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00774
00775
00776 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00777 q->weighting_delay[4] = get_bits1(&q->gb);
00778 q->weighting_delay[5] = get_bits(&q->gb,3);
00779
00780 for (i = 0; i < 4; i++) {
00781 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00782 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00783 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00784 }
00785
00786
00787 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], out_samples[1], 1, JOINT_STEREO);
00788 if (result != 0)
00789 return result;
00790
00791
00792 reverseMatrixing(out_samples[0], out_samples[1], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00793
00794 channelWeighting(out_samples[0], out_samples[1], q->weighting_delay);
00795
00796 } else {
00797
00798
00799 for (i=0 ; i<q->channels ; i++) {
00800
00801
00802 init_get_bits(&q->gb,
00803 databuf + i * q->bytes_per_frame / q->channels,
00804 q->bits_per_frame / q->channels);
00805
00806 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], out_samples[i], i, q->codingMode);
00807 if (result != 0)
00808 return result;
00809 }
00810 }
00811
00812
00813 for (i=0 ; i<q->channels ; i++) {
00814 p1 = out_samples[i];
00815 p2= p1+256;
00816 p3= p2+256;
00817 p4= p3+256;
00818 ff_atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00819 ff_atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00820 ff_atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00821 }
00822
00823 return 0;
00824 }
00825
00826
00833 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
00834 int *got_frame_ptr, AVPacket *avpkt)
00835 {
00836 const uint8_t *buf = avpkt->data;
00837 int buf_size = avpkt->size;
00838 ATRAC3Context *q = avctx->priv_data;
00839 int result;
00840 const uint8_t* databuf;
00841 float *samples_flt;
00842 int16_t *samples_s16;
00843
00844 if (buf_size < avctx->block_align) {
00845 av_log(avctx, AV_LOG_ERROR,
00846 "Frame too small (%d bytes). Truncated file?\n", buf_size);
00847 return AVERROR_INVALIDDATA;
00848 }
00849
00850
00851 q->frame.nb_samples = SAMPLES_PER_FRAME;
00852 if ((result = avctx->get_buffer(avctx, &q->frame)) < 0) {
00853 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00854 return result;
00855 }
00856 samples_flt = (float *)q->frame.data[0];
00857 samples_s16 = (int16_t *)q->frame.data[0];
00858
00859
00860 if (q->scrambled_stream) {
00861 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00862 databuf = q->decoded_bytes_buffer;
00863 } else {
00864 databuf = buf;
00865 }
00866
00867 if (q->channels == 1 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
00868 result = decodeFrame(q, databuf, &samples_flt);
00869 else
00870 result = decodeFrame(q, databuf, q->outSamples);
00871
00872 if (result != 0) {
00873 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00874 return result;
00875 }
00876
00877
00878 if (q->channels == 2 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
00879 q->fmt_conv.float_interleave(samples_flt,
00880 (const float **)q->outSamples,
00881 SAMPLES_PER_FRAME, 2);
00882 } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
00883 q->fmt_conv.float_to_int16_interleave(samples_s16,
00884 (const float **)q->outSamples,
00885 SAMPLES_PER_FRAME, q->channels);
00886 }
00887
00888 *got_frame_ptr = 1;
00889 *(AVFrame *)data = q->frame;
00890
00891 return avctx->block_align;
00892 }
00893
00894
00901 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00902 {
00903 int i, ret;
00904 const uint8_t *edata_ptr = avctx->extradata;
00905 ATRAC3Context *q = avctx->priv_data;
00906 static VLC_TYPE atrac3_vlc_table[4096][2];
00907 static int vlcs_initialized = 0;
00908
00909
00910 q->sample_rate = avctx->sample_rate;
00911 q->channels = avctx->channels;
00912 q->bit_rate = avctx->bit_rate;
00913 q->bits_per_frame = avctx->block_align * 8;
00914 q->bytes_per_frame = avctx->block_align;
00915
00916
00917 if (avctx->extradata_size == 14) {
00918
00919 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));
00920 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00921 q->codingMode = bytestream_get_le16(&edata_ptr);
00922 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));
00923 q->frame_factor = bytestream_get_le16(&edata_ptr);
00924 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));
00925
00926
00927 q->samples_per_frame = SAMPLES_PER_FRAME * q->channels;
00928 q->atrac3version = 4;
00929 q->delay = 0x88E;
00930 if (q->codingMode)
00931 q->codingMode = JOINT_STEREO;
00932 else
00933 q->codingMode = STEREO;
00934
00935 q->scrambled_stream = 0;
00936
00937 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)) {
00938 } else {
00939 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);
00940 return AVERROR_INVALIDDATA;
00941 }
00942
00943 } else if (avctx->extradata_size == 10) {
00944
00945 q->atrac3version = bytestream_get_be32(&edata_ptr);
00946 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00947 q->delay = bytestream_get_be16(&edata_ptr);
00948 q->codingMode = bytestream_get_be16(&edata_ptr);
00949
00950 q->samples_per_channel = q->samples_per_frame / q->channels;
00951 q->scrambled_stream = 1;
00952
00953 } else {
00954 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00955 }
00956
00957
00958 if (q->atrac3version != 4) {
00959 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00960 return AVERROR_INVALIDDATA;
00961 }
00962
00963 if (q->samples_per_frame != SAMPLES_PER_FRAME && q->samples_per_frame != SAMPLES_PER_FRAME*2) {
00964 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00965 return AVERROR_INVALIDDATA;
00966 }
00967
00968 if (q->delay != 0x88E) {
00969 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00970 return AVERROR_INVALIDDATA;
00971 }
00972
00973 if (q->codingMode == STEREO) {
00974 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
00975 } else if (q->codingMode == JOINT_STEREO) {
00976 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
00977 } else {
00978 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
00979 return AVERROR_INVALIDDATA;
00980 }
00981
00982 if (avctx->channels <= 0 || avctx->channels > 2 ) {
00983 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
00984 return AVERROR(EINVAL);
00985 }
00986
00987
00988 if(avctx->block_align >= UINT_MAX/2)
00989 return AVERROR(EINVAL);
00990
00991
00992
00993 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
00994 return AVERROR(ENOMEM);
00995
00996
00997
00998 if (!vlcs_initialized) {
00999 for (i=0 ; i<7 ; i++) {
01000 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
01001 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
01002 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
01003 huff_bits[i], 1, 1,
01004 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
01005 }
01006 vlcs_initialized = 1;
01007 }
01008
01009 if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT)
01010 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01011 else
01012 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01013
01014 if ((ret = init_atrac3_transforms(q, avctx->sample_fmt == AV_SAMPLE_FMT_FLT))) {
01015 av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
01016 av_freep(&q->decoded_bytes_buffer);
01017 return ret;
01018 }
01019
01020 ff_atrac_generate_tables();
01021
01022
01023 for (i=0 ; i<16 ; i++)
01024 gain_tab1[i] = exp2f (4 - i);
01025
01026 for (i=-15 ; i<16 ; i++)
01027 gain_tab2[i+15] = exp2f (i * -0.125);
01028
01029
01030 q->weighting_delay[0] = 0;
01031 q->weighting_delay[1] = 7;
01032 q->weighting_delay[2] = 0;
01033 q->weighting_delay[3] = 7;
01034 q->weighting_delay[4] = 0;
01035 q->weighting_delay[5] = 7;
01036
01037 for (i=0; i<4; i++) {
01038 q->matrix_coeff_index_prev[i] = 3;
01039 q->matrix_coeff_index_now[i] = 3;
01040 q->matrix_coeff_index_next[i] = 3;
01041 }
01042
01043 avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
01044 ff_fmt_convert_init(&q->fmt_conv, avctx);
01045
01046 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01047 if (!q->pUnits) {
01048 atrac3_decode_close(avctx);
01049 return AVERROR(ENOMEM);
01050 }
01051
01052 if (avctx->channels > 1 || avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
01053 q->outSamples[0] = av_mallocz(SAMPLES_PER_FRAME * avctx->channels * sizeof(*q->outSamples[0]));
01054 q->outSamples[1] = q->outSamples[0] + SAMPLES_PER_FRAME;
01055 if (!q->outSamples[0]) {
01056 atrac3_decode_close(avctx);
01057 return AVERROR(ENOMEM);
01058 }
01059 }
01060
01061 avcodec_get_frame_defaults(&q->frame);
01062 avctx->coded_frame = &q->frame;
01063
01064 return 0;
01065 }
01066
01067
01068 AVCodec ff_atrac3_decoder =
01069 {
01070 .name = "atrac3",
01071 .type = AVMEDIA_TYPE_AUDIO,
01072 .id = AV_CODEC_ID_ATRAC3,
01073 .priv_data_size = sizeof(ATRAC3Context),
01074 .init = atrac3_decode_init,
01075 .close = atrac3_decode_close,
01076 .decode = atrac3_decode_frame,
01077 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01078 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01079 };