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