Libav
|
00001 /* 00002 * Atrac 3 compatible decoder 00003 * Copyright (c) 2006-2008 Maxim Poliakovski 00004 * Copyright (c) 2006-2008 Benjamin Larsson 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 /* These structures are needed to store the parsed gain control data. */ 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 } ATRAC3Context; 00122 00123 static DECLARE_ALIGNED(16, float,mdct_window)[512]; 00124 static VLC spectral_coeff_tab[7]; 00125 static float gain_tab1[16]; 00126 static float gain_tab2[31]; 00127 static FFTContext mdct_ctx; 00128 static DSPContext dsp; 00129 00130 00140 static void IMLT(float *pInput, float *pOutput, int odd_band) 00141 { 00142 int i; 00143 00144 if (odd_band) { 00154 for (i=0; i<128; i++) 00155 FFSWAP(float, pInput[i], pInput[255-i]); 00156 } 00157 00158 ff_imdct_calc(&mdct_ctx,pOutput,pInput); 00159 00160 /* Perform windowing on the output. */ 00161 dsp.vector_fmul(pOutput,mdct_window,512); 00162 00163 } 00164 00165 00174 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ 00175 int i, off; 00176 uint32_t c; 00177 const uint32_t* buf; 00178 uint32_t* obuf = (uint32_t*) out; 00179 00180 off = (intptr_t)inbuffer & 3; 00181 buf = (const uint32_t*) (inbuffer - off); 00182 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8)))); 00183 bytes += 3 + off; 00184 for (i = 0; i < bytes/4; i++) 00185 obuf[i] = c ^ buf[i]; 00186 00187 if (off) 00188 av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off); 00189 00190 return off; 00191 } 00192 00193 00194 static av_cold void init_atrac3_transforms(ATRAC3Context *q) { 00195 float enc_window[256]; 00196 int i; 00197 00198 /* Generate the mdct window, for details see 00199 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */ 00200 for (i=0 ; i<256; i++) 00201 enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5; 00202 00203 if (!mdct_window[0]) 00204 for (i=0 ; i<256; i++) { 00205 mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]); 00206 mdct_window[511-i] = mdct_window[i]; 00207 } 00208 00209 /* Initialize the MDCT transform. */ 00210 ff_mdct_init(&mdct_ctx, 9, 1, 1.0); 00211 } 00212 00217 static av_cold int atrac3_decode_close(AVCodecContext *avctx) 00218 { 00219 ATRAC3Context *q = avctx->priv_data; 00220 00221 av_free(q->pUnits); 00222 av_free(q->decoded_bytes_buffer); 00223 00224 return 0; 00225 } 00226 00237 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes) 00238 { 00239 int numBits, cnt, code, huffSymb; 00240 00241 if (selector == 1) 00242 numCodes /= 2; 00243 00244 if (codingFlag != 0) { 00245 /* constant length coding (CLC) */ 00246 numBits = CLCLengthTab[selector]; 00247 00248 if (selector > 1) { 00249 for (cnt = 0; cnt < numCodes; cnt++) { 00250 if (numBits) 00251 code = get_sbits(gb, numBits); 00252 else 00253 code = 0; 00254 mantissas[cnt] = code; 00255 } 00256 } else { 00257 for (cnt = 0; cnt < numCodes; cnt++) { 00258 if (numBits) 00259 code = get_bits(gb, numBits); //numBits is always 4 in this case 00260 else 00261 code = 0; 00262 mantissas[cnt*2] = seTab_0[code >> 2]; 00263 mantissas[cnt*2+1] = seTab_0[code & 3]; 00264 } 00265 } 00266 } else { 00267 /* variable length coding (VLC) */ 00268 if (selector != 1) { 00269 for (cnt = 0; cnt < numCodes; cnt++) { 00270 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3); 00271 huffSymb += 1; 00272 code = huffSymb >> 1; 00273 if (huffSymb & 1) 00274 code = -code; 00275 mantissas[cnt] = code; 00276 } 00277 } else { 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 mantissas[cnt*2] = decTable1[huffSymb*2]; 00281 mantissas[cnt*2+1] = decTable1[huffSymb*2+1]; 00282 } 00283 } 00284 } 00285 } 00286 00295 static int decodeSpectrum (GetBitContext *gb, float *pOut) 00296 { 00297 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn; 00298 int subband_vlc_index[32], SF_idxs[32]; 00299 int mantissas[128]; 00300 float SF; 00301 00302 numSubbands = get_bits(gb, 5); // number of coded subbands 00303 codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC 00304 00305 /* Get the VLC selector table for the subbands, 0 means not coded. */ 00306 for (cnt = 0; cnt <= numSubbands; cnt++) 00307 subband_vlc_index[cnt] = get_bits(gb, 3); 00308 00309 /* Read the scale factor indexes from the stream. */ 00310 for (cnt = 0; cnt <= numSubbands; cnt++) { 00311 if (subband_vlc_index[cnt] != 0) 00312 SF_idxs[cnt] = get_bits(gb, 6); 00313 } 00314 00315 for (cnt = 0; cnt <= numSubbands; cnt++) { 00316 first = subbandTab[cnt]; 00317 last = subbandTab[cnt+1]; 00318 00319 subbWidth = last - first; 00320 00321 if (subband_vlc_index[cnt] != 0) { 00322 /* Decode spectral coefficients for this subband. */ 00323 /* TODO: This can be done faster is several blocks share the 00324 * same VLC selector (subband_vlc_index) */ 00325 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth); 00326 00327 /* Decode the scale factor for this subband. */ 00328 SF = sf_table[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]]; 00329 00330 /* Inverse quantize the coefficients. */ 00331 for (pIn=mantissas ; first<last; first++, pIn++) 00332 pOut[first] = *pIn * SF; 00333 } else { 00334 /* This subband was not coded, so zero the entire subband. */ 00335 memset(pOut+first, 0, subbWidth*sizeof(float)); 00336 } 00337 } 00338 00339 /* Clear the subbands that were not coded. */ 00340 first = subbandTab[cnt]; 00341 memset(pOut+first, 0, (1024 - first) * sizeof(float)); 00342 return numSubbands; 00343 } 00344 00353 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands) 00354 { 00355 int i,j,k,cnt; 00356 int components, coding_mode_selector, coding_mode, coded_values_per_component; 00357 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components; 00358 int band_flags[4], mantissa[8]; 00359 float *pCoef; 00360 float scalefactor; 00361 int component_count = 0; 00362 00363 components = get_bits(gb,5); 00364 00365 /* no tonal components */ 00366 if (components == 0) 00367 return 0; 00368 00369 coding_mode_selector = get_bits(gb,2); 00370 if (coding_mode_selector == 2) 00371 return -1; 00372 00373 coding_mode = coding_mode_selector & 1; 00374 00375 for (i = 0; i < components; i++) { 00376 for (cnt = 0; cnt <= numBands; cnt++) 00377 band_flags[cnt] = get_bits1(gb); 00378 00379 coded_values_per_component = get_bits(gb,3); 00380 00381 quant_step_index = get_bits(gb,3); 00382 if (quant_step_index <= 1) 00383 return -1; 00384 00385 if (coding_mode_selector == 3) 00386 coding_mode = get_bits1(gb); 00387 00388 for (j = 0; j < (numBands + 1) * 4; j++) { 00389 if (band_flags[j >> 2] == 0) 00390 continue; 00391 00392 coded_components = get_bits(gb,3); 00393 00394 for (k=0; k<coded_components; k++) { 00395 sfIndx = get_bits(gb,6); 00396 pComponent[component_count].pos = j * 64 + (get_bits(gb,6)); 00397 max_coded_values = 1024 - pComponent[component_count].pos; 00398 coded_values = coded_values_per_component + 1; 00399 coded_values = FFMIN(max_coded_values,coded_values); 00400 00401 scalefactor = sf_table[sfIndx] * iMaxQuant[quant_step_index]; 00402 00403 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values); 00404 00405 pComponent[component_count].numCoefs = coded_values; 00406 00407 /* inverse quant */ 00408 pCoef = pComponent[component_count].coef; 00409 for (cnt = 0; cnt < coded_values; cnt++) 00410 pCoef[cnt] = mantissa[cnt] * scalefactor; 00411 00412 component_count++; 00413 } 00414 } 00415 } 00416 00417 return component_count; 00418 } 00419 00428 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands) 00429 { 00430 int i, cf, numData; 00431 int *pLevel, *pLoc; 00432 00433 gain_info *pGain = pGb->gBlock; 00434 00435 for (i=0 ; i<=numBands; i++) 00436 { 00437 numData = get_bits(gb,3); 00438 pGain[i].num_gain_data = numData; 00439 pLevel = pGain[i].levcode; 00440 pLoc = pGain[i].loccode; 00441 00442 for (cf = 0; cf < numData; cf++){ 00443 pLevel[cf]= get_bits(gb,4); 00444 pLoc [cf]= get_bits(gb,5); 00445 if(cf && pLoc[cf] <= pLoc[cf-1]) 00446 return -1; 00447 } 00448 } 00449 00450 /* Clear the unused blocks. */ 00451 for (; i<4 ; i++) 00452 pGain[i].num_gain_data = 0; 00453 00454 return 0; 00455 } 00456 00467 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2) 00468 { 00469 /* gain compensation function */ 00470 float gain1, gain2, gain_inc; 00471 int cnt, numdata, nsample, startLoc, endLoc; 00472 00473 00474 if (pGain2->num_gain_data == 0) 00475 gain1 = 1.0; 00476 else 00477 gain1 = gain_tab1[pGain2->levcode[0]]; 00478 00479 if (pGain1->num_gain_data == 0) { 00480 for (cnt = 0; cnt < 256; cnt++) 00481 pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt]; 00482 } else { 00483 numdata = pGain1->num_gain_data; 00484 pGain1->loccode[numdata] = 32; 00485 pGain1->levcode[numdata] = 4; 00486 00487 nsample = 0; // current sample = 0 00488 00489 for (cnt = 0; cnt < numdata; cnt++) { 00490 startLoc = pGain1->loccode[cnt] * 8; 00491 endLoc = startLoc + 8; 00492 00493 gain2 = gain_tab1[pGain1->levcode[cnt]]; 00494 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15]; 00495 00496 /* interpolate */ 00497 for (; nsample < startLoc; nsample++) 00498 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2; 00499 00500 /* interpolation is done over eight samples */ 00501 for (; nsample < endLoc; nsample++) { 00502 pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2; 00503 gain2 *= gain_inc; 00504 } 00505 } 00506 00507 for (; nsample < 256; nsample++) 00508 pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample]; 00509 } 00510 00511 /* Delay for the overlapping part. */ 00512 memcpy(pPrev, &pIn[256], 256*sizeof(float)); 00513 } 00514 00524 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent) 00525 { 00526 int cnt, i, lastPos = -1; 00527 float *pIn, *pOut; 00528 00529 for (cnt = 0; cnt < numComponents; cnt++){ 00530 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos); 00531 pIn = pComponent[cnt].coef; 00532 pOut = &(pSpectrum[pComponent[cnt].pos]); 00533 00534 for (i=0 ; i<pComponent[cnt].numCoefs ; i++) 00535 pOut[i] += pIn[i]; 00536 } 00537 00538 return lastPos; 00539 } 00540 00541 00542 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old))) 00543 00544 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode) 00545 { 00546 int i, band, nsample, s1, s2; 00547 float c1, c2; 00548 float mc1_l, mc1_r, mc2_l, mc2_r; 00549 00550 for (i=0,band = 0; band < 4*256; band+=256,i++) { 00551 s1 = pPrevCode[i]; 00552 s2 = pCurrCode[i]; 00553 nsample = 0; 00554 00555 if (s1 != s2) { 00556 /* Selector value changed, interpolation needed. */ 00557 mc1_l = matrixCoeffs[s1*2]; 00558 mc1_r = matrixCoeffs[s1*2+1]; 00559 mc2_l = matrixCoeffs[s2*2]; 00560 mc2_r = matrixCoeffs[s2*2+1]; 00561 00562 /* Interpolation is done over the first eight samples. */ 00563 for(; nsample < 8; nsample++) { 00564 c1 = su1[band+nsample]; 00565 c2 = su2[band+nsample]; 00566 c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample); 00567 su1[band+nsample] = c2; 00568 su2[band+nsample] = c1 * 2.0 - c2; 00569 } 00570 } 00571 00572 /* Apply the matrix without interpolation. */ 00573 switch (s2) { 00574 case 0: /* M/S decoding */ 00575 for (; nsample < 256; nsample++) { 00576 c1 = su1[band+nsample]; 00577 c2 = su2[band+nsample]; 00578 su1[band+nsample] = c2 * 2.0; 00579 su2[band+nsample] = (c1 - c2) * 2.0; 00580 } 00581 break; 00582 00583 case 1: 00584 for (; nsample < 256; nsample++) { 00585 c1 = su1[band+nsample]; 00586 c2 = su2[band+nsample]; 00587 su1[band+nsample] = (c1 + c2) * 2.0; 00588 su2[band+nsample] = c2 * -2.0; 00589 } 00590 break; 00591 case 2: 00592 case 3: 00593 for (; nsample < 256; nsample++) { 00594 c1 = su1[band+nsample]; 00595 c2 = su2[band+nsample]; 00596 su1[band+nsample] = c1 + c2; 00597 su2[band+nsample] = c1 - c2; 00598 } 00599 break; 00600 default: 00601 assert(0); 00602 } 00603 } 00604 } 00605 00606 static void getChannelWeights (int indx, int flag, float ch[2]){ 00607 00608 if (indx == 7) { 00609 ch[0] = 1.0; 00610 ch[1] = 1.0; 00611 } else { 00612 ch[0] = (float)(indx & 7) / 7.0; 00613 ch[1] = sqrt(2 - ch[0]*ch[0]); 00614 if(flag) 00615 FFSWAP(float, ch[0], ch[1]); 00616 } 00617 } 00618 00619 static void channelWeighting (float *su1, float *su2, int *p3) 00620 { 00621 int band, nsample; 00622 /* w[x][y] y=0 is left y=1 is right */ 00623 float w[2][2]; 00624 00625 if (p3[1] != 7 || p3[3] != 7){ 00626 getChannelWeights(p3[1], p3[0], w[0]); 00627 getChannelWeights(p3[3], p3[2], w[1]); 00628 00629 for(band = 1; band < 4; band++) { 00630 /* scale the channels by the weights */ 00631 for(nsample = 0; nsample < 8; nsample++) { 00632 su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample); 00633 su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample); 00634 } 00635 00636 for(; nsample < 256; nsample++) { 00637 su1[band*256+nsample] *= w[1][0]; 00638 su2[band*256+nsample] *= w[1][1]; 00639 } 00640 } 00641 } 00642 } 00643 00644 00656 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode) 00657 { 00658 int band, result=0, numSubbands, lastTonal, numBands; 00659 00660 if (codingMode == JOINT_STEREO && channelNum == 1) { 00661 if (get_bits(gb,2) != 3) { 00662 av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n"); 00663 return -1; 00664 } 00665 } else { 00666 if (get_bits(gb,6) != 0x28) { 00667 av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n"); 00668 return -1; 00669 } 00670 } 00671 00672 /* number of coded QMF bands */ 00673 pSnd->bandsCoded = get_bits(gb,2); 00674 00675 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded); 00676 if (result) return result; 00677 00678 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded); 00679 if (pSnd->numComponents == -1) return -1; 00680 00681 numSubbands = decodeSpectrum (gb, pSnd->spectrum); 00682 00683 /* Merge the decoded spectrum and tonal components. */ 00684 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components); 00685 00686 00687 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */ 00688 numBands = (subbandTab[numSubbands] - 1) >> 8; 00689 if (lastTonal >= 0) 00690 numBands = FFMAX((lastTonal + 256) >> 8, numBands); 00691 00692 00693 /* Reconstruct time domain samples. */ 00694 for (band=0; band<4; band++) { 00695 /* Perform the IMDCT step without overlapping. */ 00696 if (band <= numBands) { 00697 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1); 00698 } else 00699 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float)); 00700 00701 /* gain compensation and overlapping */ 00702 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]), 00703 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]), 00704 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band])); 00705 } 00706 00707 /* Swap the gain control buffers for the next frame. */ 00708 pSnd->gcBlkSwitch ^= 1; 00709 00710 return 0; 00711 } 00712 00720 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf) 00721 { 00722 int result, i; 00723 float *p1, *p2, *p3, *p4; 00724 uint8_t *ptr1; 00725 00726 if (q->codingMode == JOINT_STEREO) { 00727 00728 /* channel coupling mode */ 00729 /* decode Sound Unit 1 */ 00730 init_get_bits(&q->gb,databuf,q->bits_per_frame); 00731 00732 result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO); 00733 if (result != 0) 00734 return (result); 00735 00736 /* Framedata of the su2 in the joint-stereo mode is encoded in 00737 * reverse byte order so we need to swap it first. */ 00738 if (databuf == q->decoded_bytes_buffer) { 00739 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1; 00740 ptr1 = q->decoded_bytes_buffer; 00741 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) { 00742 FFSWAP(uint8_t,*ptr1,*ptr2); 00743 } 00744 } else { 00745 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1; 00746 for (i = 0; i < q->bytes_per_frame; i++) 00747 q->decoded_bytes_buffer[i] = *ptr2--; 00748 } 00749 00750 /* Skip the sync codes (0xF8). */ 00751 ptr1 = q->decoded_bytes_buffer; 00752 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { 00753 if (i >= q->bytes_per_frame) 00754 return -1; 00755 } 00756 00757 00758 /* set the bitstream reader at the start of the second Sound Unit*/ 00759 init_get_bits(&q->gb,ptr1,q->bits_per_frame); 00760 00761 /* Fill the Weighting coeffs delay buffer */ 00762 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int)); 00763 q->weighting_delay[4] = get_bits1(&q->gb); 00764 q->weighting_delay[5] = get_bits(&q->gb,3); 00765 00766 for (i = 0; i < 4; i++) { 00767 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i]; 00768 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i]; 00769 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2); 00770 } 00771 00772 /* Decode Sound Unit 2. */ 00773 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO); 00774 if (result != 0) 00775 return (result); 00776 00777 /* Reconstruct the channel coefficients. */ 00778 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now); 00779 00780 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay); 00781 00782 } else { 00783 /* normal stereo mode or mono */ 00784 /* Decode the channel sound units. */ 00785 for (i=0 ; i<q->channels ; i++) { 00786 00787 /* Set the bitstream reader at the start of a channel sound unit. */ 00788 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels); 00789 00790 result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode); 00791 if (result != 0) 00792 return (result); 00793 } 00794 } 00795 00796 /* Apply the iQMF synthesis filter. */ 00797 p1= q->outSamples; 00798 for (i=0 ; i<q->channels ; i++) { 00799 p2= p1+256; 00800 p3= p2+256; 00801 p4= p3+256; 00802 atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf); 00803 atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf); 00804 atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf); 00805 p1 +=1024; 00806 } 00807 00808 return 0; 00809 } 00810 00811 00818 static int atrac3_decode_frame(AVCodecContext *avctx, 00819 void *data, int *data_size, 00820 AVPacket *avpkt) { 00821 const uint8_t *buf = avpkt->data; 00822 int buf_size = avpkt->size; 00823 ATRAC3Context *q = avctx->priv_data; 00824 int result = 0, i; 00825 const uint8_t* databuf; 00826 int16_t* samples = data; 00827 00828 if (buf_size < avctx->block_align) 00829 return buf_size; 00830 00831 /* Check if we need to descramble and what buffer to pass on. */ 00832 if (q->scrambled_stream) { 00833 decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align); 00834 databuf = q->decoded_bytes_buffer; 00835 } else { 00836 databuf = buf; 00837 } 00838 00839 result = decodeFrame(q, databuf); 00840 00841 if (result != 0) { 00842 av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n"); 00843 return -1; 00844 } 00845 00846 if (q->channels == 1) { 00847 /* mono */ 00848 for (i = 0; i<1024; i++) 00849 samples[i] = av_clip_int16(round(q->outSamples[i])); 00850 *data_size = 1024 * sizeof(int16_t); 00851 } else { 00852 /* stereo */ 00853 for (i = 0; i < 1024; i++) { 00854 samples[i*2] = av_clip_int16(round(q->outSamples[i])); 00855 samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i])); 00856 } 00857 *data_size = 2048 * sizeof(int16_t); 00858 } 00859 00860 return avctx->block_align; 00861 } 00862 00863 00870 static av_cold int atrac3_decode_init(AVCodecContext *avctx) 00871 { 00872 int i; 00873 const uint8_t *edata_ptr = avctx->extradata; 00874 ATRAC3Context *q = avctx->priv_data; 00875 static VLC_TYPE atrac3_vlc_table[4096][2]; 00876 static int vlcs_initialized = 0; 00877 00878 /* Take data from the AVCodecContext (RM container). */ 00879 q->sample_rate = avctx->sample_rate; 00880 q->channels = avctx->channels; 00881 q->bit_rate = avctx->bit_rate; 00882 q->bits_per_frame = avctx->block_align * 8; 00883 q->bytes_per_frame = avctx->block_align; 00884 00885 /* Take care of the codec-specific extradata. */ 00886 if (avctx->extradata_size == 14) { 00887 /* Parse the extradata, WAV format */ 00888 av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value always 1 00889 q->samples_per_channel = bytestream_get_le32(&edata_ptr); 00890 q->codingMode = bytestream_get_le16(&edata_ptr); 00891 av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode 00892 q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1 00893 av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0 00894 00895 /* setup */ 00896 q->samples_per_frame = 1024 * q->channels; 00897 q->atrac3version = 4; 00898 q->delay = 0x88E; 00899 if (q->codingMode) 00900 q->codingMode = JOINT_STEREO; 00901 else 00902 q->codingMode = STEREO; 00903 00904 q->scrambled_stream = 0; 00905 00906 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)) { 00907 } else { 00908 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); 00909 return -1; 00910 } 00911 00912 } else if (avctx->extradata_size == 10) { 00913 /* Parse the extradata, RM format. */ 00914 q->atrac3version = bytestream_get_be32(&edata_ptr); 00915 q->samples_per_frame = bytestream_get_be16(&edata_ptr); 00916 q->delay = bytestream_get_be16(&edata_ptr); 00917 q->codingMode = bytestream_get_be16(&edata_ptr); 00918 00919 q->samples_per_channel = q->samples_per_frame / q->channels; 00920 q->scrambled_stream = 1; 00921 00922 } else { 00923 av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size); 00924 } 00925 /* Check the extradata. */ 00926 00927 if (q->atrac3version != 4) { 00928 av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version); 00929 return -1; 00930 } 00931 00932 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) { 00933 av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame); 00934 return -1; 00935 } 00936 00937 if (q->delay != 0x88E) { 00938 av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay); 00939 return -1; 00940 } 00941 00942 if (q->codingMode == STEREO) { 00943 av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n"); 00944 } else if (q->codingMode == JOINT_STEREO) { 00945 av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n"); 00946 } else { 00947 av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode); 00948 return -1; 00949 } 00950 00951 if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) { 00952 av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n"); 00953 return -1; 00954 } 00955 00956 00957 if(avctx->block_align >= UINT_MAX/2) 00958 return -1; 00959 00960 /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE, 00961 * this is for the bitstream reader. */ 00962 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL) 00963 return AVERROR(ENOMEM); 00964 00965 00966 /* Initialize the VLC tables. */ 00967 if (!vlcs_initialized) { 00968 for (i=0 ; i<7 ; i++) { 00969 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]]; 00970 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i]; 00971 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i], 00972 huff_bits[i], 1, 1, 00973 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC); 00974 } 00975 vlcs_initialized = 1; 00976 } 00977 00978 init_atrac3_transforms(q); 00979 00980 atrac_generate_tables(); 00981 00982 /* Generate gain tables. */ 00983 for (i=0 ; i<16 ; i++) 00984 gain_tab1[i] = powf (2.0, (4 - i)); 00985 00986 for (i=-15 ; i<16 ; i++) 00987 gain_tab2[i+15] = powf (2.0, i * -0.125); 00988 00989 /* init the joint-stereo decoding data */ 00990 q->weighting_delay[0] = 0; 00991 q->weighting_delay[1] = 7; 00992 q->weighting_delay[2] = 0; 00993 q->weighting_delay[3] = 7; 00994 q->weighting_delay[4] = 0; 00995 q->weighting_delay[5] = 7; 00996 00997 for (i=0; i<4; i++) { 00998 q->matrix_coeff_index_prev[i] = 3; 00999 q->matrix_coeff_index_now[i] = 3; 01000 q->matrix_coeff_index_next[i] = 3; 01001 } 01002 01003 dsputil_init(&dsp, avctx); 01004 01005 q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels); 01006 if (!q->pUnits) { 01007 av_free(q->decoded_bytes_buffer); 01008 return AVERROR(ENOMEM); 01009 } 01010 01011 avctx->sample_fmt = SAMPLE_FMT_S16; 01012 return 0; 01013 } 01014 01015 01016 AVCodec atrac3_decoder = 01017 { 01018 .name = "atrac3", 01019 .type = AVMEDIA_TYPE_AUDIO, 01020 .id = CODEC_ID_ATRAC3, 01021 .priv_data_size = sizeof(ATRAC3Context), 01022 .init = atrac3_decode_init, 01023 .close = atrac3_decode_close, 01024 .decode = atrac3_decode_frame, 01025 .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"), 01026 };