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