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(16, float, spectrum)[1024];
00078 DECLARE_ALIGNED(16, 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(16, 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 ff_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(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\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 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
00399 max_coded_values = 1024 - pComponent[component_count].pos;
00400 coded_values = coded_values_per_component + 1;
00401 coded_values = FFMIN(max_coded_values,coded_values);
00402
00403 scalefactor = ff_atrac_sf_table[sfIndx] * iMaxQuant[quant_step_index];
00404
00405 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
00406
00407 pComponent[component_count].numCoefs = coded_values;
00408
00409
00410 pCoef = pComponent[component_count].coef;
00411 for (cnt = 0; cnt < coded_values; cnt++)
00412 pCoef[cnt] = mantissa[cnt] * scalefactor;
00413
00414 component_count++;
00415 }
00416 }
00417 }
00418
00419 return component_count;
00420 }
00421
00430 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
00431 {
00432 int i, cf, numData;
00433 int *pLevel, *pLoc;
00434
00435 gain_info *pGain = pGb->gBlock;
00436
00437 for (i=0 ; i<=numBands; i++)
00438 {
00439 numData = get_bits(gb,3);
00440 pGain[i].num_gain_data = numData;
00441 pLevel = pGain[i].levcode;
00442 pLoc = pGain[i].loccode;
00443
00444 for (cf = 0; cf < numData; cf++){
00445 pLevel[cf]= get_bits(gb,4);
00446 pLoc [cf]= get_bits(gb,5);
00447 if(cf && pLoc[cf] <= pLoc[cf-1])
00448 return -1;
00449 }
00450 }
00451
00452
00453 for (; i<4 ; i++)
00454 pGain[i].num_gain_data = 0;
00455
00456 return 0;
00457 }
00458
00469 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
00470 {
00471
00472 float gain1, gain2, gain_inc;
00473 int cnt, numdata, nsample, startLoc, endLoc;
00474
00475
00476 if (pGain2->num_gain_data == 0)
00477 gain1 = 1.0;
00478 else
00479 gain1 = gain_tab1[pGain2->levcode[0]];
00480
00481 if (pGain1->num_gain_data == 0) {
00482 for (cnt = 0; cnt < 256; cnt++)
00483 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
00484 } else {
00485 numdata = pGain1->num_gain_data;
00486 pGain1->loccode[numdata] = 32;
00487 pGain1->levcode[numdata] = 4;
00488
00489 nsample = 0;
00490
00491 for (cnt = 0; cnt < numdata; cnt++) {
00492 startLoc = pGain1->loccode[cnt] * 8;
00493 endLoc = startLoc + 8;
00494
00495 gain2 = gain_tab1[pGain1->levcode[cnt]];
00496 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
00497
00498
00499 for (; nsample < startLoc; nsample++)
00500 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00501
00502
00503 for (; nsample < endLoc; nsample++) {
00504 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
00505 gain2 *= gain_inc;
00506 }
00507 }
00508
00509 for (; nsample < 256; nsample++)
00510 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
00511 }
00512
00513
00514 memcpy(pPrev, &pIn[256], 256*sizeof(float));
00515 }
00516
00526 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
00527 {
00528 int cnt, i, lastPos = -1;
00529 float *pIn, *pOut;
00530
00531 for (cnt = 0; cnt < numComponents; cnt++){
00532 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
00533 pIn = pComponent[cnt].coef;
00534 pOut = &(pSpectrum[pComponent[cnt].pos]);
00535
00536 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
00537 pOut[i] += pIn[i];
00538 }
00539
00540 return lastPos;
00541 }
00542
00543
00544 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
00545
00546 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
00547 {
00548 int i, band, nsample, s1, s2;
00549 float c1, c2;
00550 float mc1_l, mc1_r, mc2_l, mc2_r;
00551
00552 for (i=0,band = 0; band < 4*256; band+=256,i++) {
00553 s1 = pPrevCode[i];
00554 s2 = pCurrCode[i];
00555 nsample = 0;
00556
00557 if (s1 != s2) {
00558
00559 mc1_l = matrixCoeffs[s1*2];
00560 mc1_r = matrixCoeffs[s1*2+1];
00561 mc2_l = matrixCoeffs[s2*2];
00562 mc2_r = matrixCoeffs[s2*2+1];
00563
00564
00565 for(; nsample < 8; nsample++) {
00566 c1 = su1[band+nsample];
00567 c2 = su2[band+nsample];
00568 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
00569 su1[band+nsample] = c2;
00570 su2[band+nsample] = c1 * 2.0 - c2;
00571 }
00572 }
00573
00574
00575 switch (s2) {
00576 case 0:
00577 for (; nsample < 256; nsample++) {
00578 c1 = su1[band+nsample];
00579 c2 = su2[band+nsample];
00580 su1[band+nsample] = c2 * 2.0;
00581 su2[band+nsample] = (c1 - c2) * 2.0;
00582 }
00583 break;
00584
00585 case 1:
00586 for (; nsample < 256; nsample++) {
00587 c1 = su1[band+nsample];
00588 c2 = su2[band+nsample];
00589 su1[band+nsample] = (c1 + c2) * 2.0;
00590 su2[band+nsample] = c2 * -2.0;
00591 }
00592 break;
00593 case 2:
00594 case 3:
00595 for (; nsample < 256; nsample++) {
00596 c1 = su1[band+nsample];
00597 c2 = su2[band+nsample];
00598 su1[band+nsample] = c1 + c2;
00599 su2[band+nsample] = c1 - c2;
00600 }
00601 break;
00602 default:
00603 assert(0);
00604 }
00605 }
00606 }
00607
00608 static void getChannelWeights (int indx, int flag, float ch[2]){
00609
00610 if (indx == 7) {
00611 ch[0] = 1.0;
00612 ch[1] = 1.0;
00613 } else {
00614 ch[0] = (float)(indx & 7) / 7.0;
00615 ch[1] = sqrt(2 - ch[0]*ch[0]);
00616 if(flag)
00617 FFSWAP(float, ch[0], ch[1]);
00618 }
00619 }
00620
00621 static void channelWeighting (float *su1, float *su2, int *p3)
00622 {
00623 int band, nsample;
00624
00625 float w[2][2];
00626
00627 if (p3[1] != 7 || p3[3] != 7){
00628 getChannelWeights(p3[1], p3[0], w[0]);
00629 getChannelWeights(p3[3], p3[2], w[1]);
00630
00631 for(band = 1; band < 4; band++) {
00632
00633 for(nsample = 0; nsample < 8; nsample++) {
00634 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
00635 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
00636 }
00637
00638 for(; nsample < 256; nsample++) {
00639 su1[band*256+nsample] *= w[1][0];
00640 su2[band*256+nsample] *= w[1][1];
00641 }
00642 }
00643 }
00644 }
00645
00646
00658 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
00659 {
00660 int band, result=0, numSubbands, lastTonal, numBands;
00661
00662 if (codingMode == JOINT_STEREO && channelNum == 1) {
00663 if (get_bits(gb,2) != 3) {
00664 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
00665 return -1;
00666 }
00667 } else {
00668 if (get_bits(gb,6) != 0x28) {
00669 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
00670 return -1;
00671 }
00672 }
00673
00674
00675 pSnd->bandsCoded = get_bits(gb,2);
00676
00677 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
00678 if (result) return result;
00679
00680 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
00681 if (pSnd->numComponents == -1) return -1;
00682
00683 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
00684
00685
00686 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
00687
00688
00689
00690 numBands = (subbandTab[numSubbands] - 1) >> 8;
00691 if (lastTonal >= 0)
00692 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
00693
00694
00695
00696 for (band=0; band<4; band++) {
00697
00698 if (band <= numBands) {
00699 IMLT(q, &(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
00700 } else
00701 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
00702
00703
00704 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
00705 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
00706 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
00707 }
00708
00709
00710 pSnd->gcBlkSwitch ^= 1;
00711
00712 return 0;
00713 }
00714
00722 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
00723 {
00724 int result, i;
00725 float *p1, *p2, *p3, *p4;
00726 uint8_t *ptr1;
00727
00728 if (q->codingMode == JOINT_STEREO) {
00729
00730
00731
00732 init_get_bits(&q->gb,databuf,q->bits_per_frame);
00733
00734 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
00735 if (result != 0)
00736 return (result);
00737
00738
00739
00740 if (databuf == q->decoded_bytes_buffer) {
00741 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
00742 ptr1 = q->decoded_bytes_buffer;
00743 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
00744 FFSWAP(uint8_t,*ptr1,*ptr2);
00745 }
00746 } else {
00747 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
00748 for (i = 0; i < q->bytes_per_frame; i++)
00749 q->decoded_bytes_buffer[i] = *ptr2--;
00750 }
00751
00752
00753 ptr1 = q->decoded_bytes_buffer;
00754 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
00755 if (i >= q->bytes_per_frame)
00756 return -1;
00757 }
00758
00759
00760
00761 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
00762
00763
00764 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
00765 q->weighting_delay[4] = get_bits1(&q->gb);
00766 q->weighting_delay[5] = get_bits(&q->gb,3);
00767
00768 for (i = 0; i < 4; i++) {
00769 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
00770 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
00771 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
00772 }
00773
00774
00775 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
00776 if (result != 0)
00777 return (result);
00778
00779
00780 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
00781
00782 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
00783
00784 } else {
00785
00786
00787 for (i=0 ; i<q->channels ; i++) {
00788
00789
00790 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
00791
00792 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
00793 if (result != 0)
00794 return (result);
00795 }
00796 }
00797
00798
00799 p1= q->outSamples;
00800 for (i=0 ; i<q->channels ; i++) {
00801 p2= p1+256;
00802 p3= p2+256;
00803 p4= p3+256;
00804 atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
00805 atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
00806 atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
00807 p1 +=1024;
00808 }
00809
00810 return 0;
00811 }
00812
00813
00820 static int atrac3_decode_frame(AVCodecContext *avctx,
00821 void *data, int *data_size,
00822 AVPacket *avpkt) {
00823 const uint8_t *buf = avpkt->data;
00824 int buf_size = avpkt->size;
00825 ATRAC3Context *q = avctx->priv_data;
00826 int result = 0, i;
00827 const uint8_t* databuf;
00828 int16_t* samples = data;
00829
00830 if (buf_size < avctx->block_align) {
00831 av_log(avctx, AV_LOG_ERROR,
00832 "Frame too small (%d bytes). Truncated file?\n", buf_size);
00833 *data_size = 0;
00834 return buf_size;
00835 }
00836
00837
00838 if (q->scrambled_stream) {
00839 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
00840 databuf = q->decoded_bytes_buffer;
00841 } else {
00842 databuf = buf;
00843 }
00844
00845 result = decodeFrame(q, databuf);
00846
00847 if (result != 0) {
00848 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
00849 return -1;
00850 }
00851
00852 if (q->channels == 1) {
00853
00854 for (i = 0; i<1024; i++)
00855 samples[i] = av_clip_int16(round(q->outSamples[i]));
00856 *data_size = 1024 * sizeof(int16_t);
00857 } else {
00858
00859 for (i = 0; i < 1024; i++) {
00860 samples[i*2] = av_clip_int16(round(q->outSamples[i]));
00861 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
00862 }
00863 *data_size = 2048 * sizeof(int16_t);
00864 }
00865
00866 return avctx->block_align;
00867 }
00868
00869
00876 static av_cold int atrac3_decode_init(AVCodecContext *avctx)
00877 {
00878 int i;
00879 const uint8_t *edata_ptr = avctx->extradata;
00880 ATRAC3Context *q = avctx->priv_data;
00881 static VLC_TYPE atrac3_vlc_table[4096][2];
00882 static int vlcs_initialized = 0;
00883
00884
00885 q->sample_rate = avctx->sample_rate;
00886 q->channels = avctx->channels;
00887 q->bit_rate = avctx->bit_rate;
00888 q->bits_per_frame = avctx->block_align * 8;
00889 q->bytes_per_frame = avctx->block_align;
00890
00891
00892 if (avctx->extradata_size == 14) {
00893
00894 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr));
00895 q->samples_per_channel = bytestream_get_le32(&edata_ptr);
00896 q->codingMode = bytestream_get_le16(&edata_ptr);
00897 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr));
00898 q->frame_factor = bytestream_get_le16(&edata_ptr);
00899 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr));
00900
00901
00902 q->samples_per_frame = 1024 * q->channels;
00903 q->atrac3version = 4;
00904 q->delay = 0x88E;
00905 if (q->codingMode)
00906 q->codingMode = JOINT_STEREO;
00907 else
00908 q->codingMode = STEREO;
00909
00910 q->scrambled_stream = 0;
00911
00912 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)) {
00913 } else {
00914 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);
00915 return -1;
00916 }
00917
00918 } else if (avctx->extradata_size == 10) {
00919
00920 q->atrac3version = bytestream_get_be32(&edata_ptr);
00921 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
00922 q->delay = bytestream_get_be16(&edata_ptr);
00923 q->codingMode = bytestream_get_be16(&edata_ptr);
00924
00925 q->samples_per_channel = q->samples_per_frame / q->channels;
00926 q->scrambled_stream = 1;
00927
00928 } else {
00929 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
00930 }
00931
00932
00933 if (q->atrac3version != 4) {
00934 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
00935 return -1;
00936 }
00937
00938 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
00939 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
00940 return -1;
00941 }
00942
00943 if (q->delay != 0x88E) {
00944 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
00945 return -1;
00946 }
00947
00948 if (q->codingMode == STEREO) {
00949 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
00950 } else if (q->codingMode == JOINT_STEREO) {
00951 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
00952 } else {
00953 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
00954 return -1;
00955 }
00956
00957 if (avctx->channels <= 0 || avctx->channels > 2 ) {
00958 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
00959 return -1;
00960 }
00961
00962
00963 if(avctx->block_align >= UINT_MAX/2)
00964 return -1;
00965
00966
00967
00968 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
00969 return AVERROR(ENOMEM);
00970
00971
00972
00973 if (!vlcs_initialized) {
00974 for (i=0 ; i<7 ; i++) {
00975 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
00976 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
00977 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
00978 huff_bits[i], 1, 1,
00979 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
00980 }
00981 vlcs_initialized = 1;
00982 }
00983
00984 init_atrac3_transforms(q);
00985
00986 atrac_generate_tables();
00987
00988
00989 for (i=0 ; i<16 ; i++)
00990 gain_tab1[i] = powf (2.0, (4 - i));
00991
00992 for (i=-15 ; i<16 ; i++)
00993 gain_tab2[i+15] = powf (2.0, i * -0.125);
00994
00995
00996 q->weighting_delay[0] = 0;
00997 q->weighting_delay[1] = 7;
00998 q->weighting_delay[2] = 0;
00999 q->weighting_delay[3] = 7;
01000 q->weighting_delay[4] = 0;
01001 q->weighting_delay[5] = 7;
01002
01003 for (i=0; i<4; i++) {
01004 q->matrix_coeff_index_prev[i] = 3;
01005 q->matrix_coeff_index_now[i] = 3;
01006 q->matrix_coeff_index_next[i] = 3;
01007 }
01008
01009 dsputil_init(&dsp, avctx);
01010
01011 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
01012 if (!q->pUnits) {
01013 av_free(q->decoded_bytes_buffer);
01014 return AVERROR(ENOMEM);
01015 }
01016
01017 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01018 return 0;
01019 }
01020
01021
01022 AVCodec ff_atrac3_decoder =
01023 {
01024 .name = "atrac3",
01025 .type = AVMEDIA_TYPE_AUDIO,
01026 .id = CODEC_ID_ATRAC3,
01027 .priv_data_size = sizeof(ATRAC3Context),
01028 .init = atrac3_decode_init,
01029 .close = atrac3_decode_close,
01030 .decode = atrac3_decode_frame,
01031 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
01032 };