You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1090 lines
33KB

  1. /*
  2. * Atrac 3 compatible decoder
  3. * Copyright (c) 2006-2008 Maxim Poliakovski
  4. * Copyright (c) 2006-2008 Benjamin Larsson
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/atrac3.c
  24. * Atrac 3 compatible decoder.
  25. * This decoder handles Sony's ATRAC3 data.
  26. *
  27. * Container formats used to store atrac 3 data:
  28. * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
  29. *
  30. * To use this decoder, a calling application must supply the extradata
  31. * bytes provided in the containers above.
  32. */
  33. #include <math.h>
  34. #include <stddef.h>
  35. #include <stdio.h>
  36. #include "avcodec.h"
  37. #include "get_bits.h"
  38. #include "dsputil.h"
  39. #include "bytestream.h"
  40. #include "atrac3data.h"
  41. #define JOINT_STEREO 0x12
  42. #define STEREO 0x2
  43. /* These structures are needed to store the parsed gain control data. */
  44. typedef struct {
  45. int num_gain_data;
  46. int levcode[8];
  47. int loccode[8];
  48. } gain_info;
  49. typedef struct {
  50. gain_info gBlock[4];
  51. } gain_block;
  52. typedef struct {
  53. int pos;
  54. int numCoefs;
  55. float coef[8];
  56. } tonal_component;
  57. typedef struct {
  58. int bandsCoded;
  59. int numComponents;
  60. tonal_component components[64];
  61. float prevFrame[1024];
  62. int gcBlkSwitch;
  63. gain_block gainBlock[2];
  64. DECLARE_ALIGNED_16(float, spectrum[1024]);
  65. DECLARE_ALIGNED_16(float, IMDCT_buf[1024]);
  66. float delayBuf1[46]; ///<qmf delay buffers
  67. float delayBuf2[46];
  68. float delayBuf3[46];
  69. } channel_unit;
  70. typedef struct {
  71. GetBitContext gb;
  72. //@{
  73. /** stream data */
  74. int channels;
  75. int codingMode;
  76. int bit_rate;
  77. int sample_rate;
  78. int samples_per_channel;
  79. int samples_per_frame;
  80. int bits_per_frame;
  81. int bytes_per_frame;
  82. int pBs;
  83. channel_unit* pUnits;
  84. //@}
  85. //@{
  86. /** joint-stereo related variables */
  87. int matrix_coeff_index_prev[4];
  88. int matrix_coeff_index_now[4];
  89. int matrix_coeff_index_next[4];
  90. int weighting_delay[6];
  91. //@}
  92. //@{
  93. /** data buffers */
  94. float outSamples[2048];
  95. uint8_t* decoded_bytes_buffer;
  96. float tempBuf[1070];
  97. //@}
  98. //@{
  99. /** extradata */
  100. int atrac3version;
  101. int delay;
  102. int scrambled_stream;
  103. int frame_factor;
  104. //@}
  105. } ATRAC3Context;
  106. static DECLARE_ALIGNED_16(float,mdct_window[512]);
  107. static float qmf_window[48];
  108. static VLC spectral_coeff_tab[7];
  109. static float SFTable[64];
  110. static float gain_tab1[16];
  111. static float gain_tab2[31];
  112. static MDCTContext mdct_ctx;
  113. static DSPContext dsp;
  114. /* quadrature mirror synthesis filter */
  115. /**
  116. * Quadrature mirror synthesis filter.
  117. *
  118. * @param inlo lower part of spectrum
  119. * @param inhi higher part of spectrum
  120. * @param nIn size of spectrum buffer
  121. * @param pOut out buffer
  122. * @param delayBuf delayBuf buffer
  123. * @param temp temp buffer
  124. */
  125. static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp)
  126. {
  127. int i, j;
  128. float *p1, *p3;
  129. memcpy(temp, delayBuf, 46*sizeof(float));
  130. p3 = temp + 46;
  131. /* loop1 */
  132. for(i=0; i<nIn; i+=2){
  133. p3[2*i+0] = inlo[i ] + inhi[i ];
  134. p3[2*i+1] = inlo[i ] - inhi[i ];
  135. p3[2*i+2] = inlo[i+1] + inhi[i+1];
  136. p3[2*i+3] = inlo[i+1] - inhi[i+1];
  137. }
  138. /* loop2 */
  139. p1 = temp;
  140. for (j = nIn; j != 0; j--) {
  141. float s1 = 0.0;
  142. float s2 = 0.0;
  143. for (i = 0; i < 48; i += 2) {
  144. s1 += p1[i] * qmf_window[i];
  145. s2 += p1[i+1] * qmf_window[i+1];
  146. }
  147. pOut[0] = s2;
  148. pOut[1] = s1;
  149. p1 += 2;
  150. pOut += 2;
  151. }
  152. /* Update the delay buffer. */
  153. memcpy(delayBuf, temp + nIn*2, 46*sizeof(float));
  154. }
  155. /**
  156. * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
  157. * caused by the reverse spectra of the QMF.
  158. *
  159. * @param pInput float input
  160. * @param pOutput float output
  161. * @param odd_band 1 if the band is an odd band
  162. */
  163. static void IMLT(float *pInput, float *pOutput, int odd_band)
  164. {
  165. int i;
  166. if (odd_band) {
  167. /**
  168. * Reverse the odd bands before IMDCT, this is an effect of the QMF transform
  169. * or it gives better compression to do it this way.
  170. * FIXME: It should be possible to handle this in ff_imdct_calc
  171. * for that to happen a modification of the prerotation step of
  172. * all SIMD code and C code is needed.
  173. * Or fix the functions before so they generate a pre reversed spectrum.
  174. */
  175. for (i=0; i<128; i++)
  176. FFSWAP(float, pInput[i], pInput[255-i]);
  177. }
  178. ff_imdct_calc(&mdct_ctx,pOutput,pInput);
  179. /* Perform windowing on the output. */
  180. dsp.vector_fmul(pOutput,mdct_window,512);
  181. }
  182. /**
  183. * Atrac 3 indata descrambling, only used for data coming from the rm container
  184. *
  185. * @param in pointer to 8 bit array of indata
  186. * @param bits amount of bits
  187. * @param out pointer to 8 bit array of outdata
  188. */
  189. static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
  190. int i, off;
  191. uint32_t c;
  192. const uint32_t* buf;
  193. uint32_t* obuf = (uint32_t*) out;
  194. off = (intptr_t)inbuffer & 3;
  195. buf = (const uint32_t*) (inbuffer - off);
  196. c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
  197. bytes += 3 + off;
  198. for (i = 0; i < bytes/4; i++)
  199. obuf[i] = c ^ buf[i];
  200. if (off)
  201. av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
  202. return off;
  203. }
  204. static av_cold void init_atrac3_transforms(ATRAC3Context *q) {
  205. float enc_window[256];
  206. float s;
  207. int i;
  208. /* Generate the mdct window, for details see
  209. * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
  210. for (i=0 ; i<256; i++)
  211. enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
  212. if (!mdct_window[0])
  213. for (i=0 ; i<256; i++) {
  214. mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
  215. mdct_window[511-i] = mdct_window[i];
  216. }
  217. /* Generate the QMF window. */
  218. for (i=0 ; i<24; i++) {
  219. s = qmf_48tap_half[i] * 2.0;
  220. qmf_window[i] = s;
  221. qmf_window[47 - i] = s;
  222. }
  223. /* Initialize the MDCT transform. */
  224. ff_mdct_init(&mdct_ctx, 9, 1, 1.0);
  225. }
  226. /**
  227. * Atrac3 uninit, free all allocated memory
  228. */
  229. static av_cold int atrac3_decode_close(AVCodecContext *avctx)
  230. {
  231. ATRAC3Context *q = avctx->priv_data;
  232. av_free(q->pUnits);
  233. av_free(q->decoded_bytes_buffer);
  234. return 0;
  235. }
  236. /**
  237. / * Mantissa decoding
  238. *
  239. * @param gb the GetBit context
  240. * @param selector what table is the output values coded with
  241. * @param codingFlag constant length coding or variable length coding
  242. * @param mantissas mantissa output table
  243. * @param numCodes amount of values to get
  244. */
  245. static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
  246. {
  247. int numBits, cnt, code, huffSymb;
  248. if (selector == 1)
  249. numCodes /= 2;
  250. if (codingFlag != 0) {
  251. /* constant length coding (CLC) */
  252. numBits = CLCLengthTab[selector];
  253. if (selector > 1) {
  254. for (cnt = 0; cnt < numCodes; cnt++) {
  255. if (numBits)
  256. code = get_sbits(gb, numBits);
  257. else
  258. code = 0;
  259. mantissas[cnt] = code;
  260. }
  261. } else {
  262. for (cnt = 0; cnt < numCodes; cnt++) {
  263. if (numBits)
  264. code = get_bits(gb, numBits); //numBits is always 4 in this case
  265. else
  266. code = 0;
  267. mantissas[cnt*2] = seTab_0[code >> 2];
  268. mantissas[cnt*2+1] = seTab_0[code & 3];
  269. }
  270. }
  271. } else {
  272. /* variable length coding (VLC) */
  273. if (selector != 1) {
  274. for (cnt = 0; cnt < numCodes; cnt++) {
  275. huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
  276. huffSymb += 1;
  277. code = huffSymb >> 1;
  278. if (huffSymb & 1)
  279. code = -code;
  280. mantissas[cnt] = code;
  281. }
  282. } else {
  283. for (cnt = 0; cnt < numCodes; cnt++) {
  284. huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
  285. mantissas[cnt*2] = decTable1[huffSymb*2];
  286. mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
  287. }
  288. }
  289. }
  290. }
  291. /**
  292. * Restore the quantized band spectrum coefficients
  293. *
  294. * @param gb the GetBit context
  295. * @param pOut decoded band spectrum
  296. * @return outSubbands subband counter, fix for broken specification/files
  297. */
  298. static int decodeSpectrum (GetBitContext *gb, float *pOut)
  299. {
  300. int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
  301. int subband_vlc_index[32], SF_idxs[32];
  302. int mantissas[128];
  303. float SF;
  304. numSubbands = get_bits(gb, 5); // number of coded subbands
  305. codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
  306. /* Get the VLC selector table for the subbands, 0 means not coded. */
  307. for (cnt = 0; cnt <= numSubbands; cnt++)
  308. subband_vlc_index[cnt] = get_bits(gb, 3);
  309. /* Read the scale factor indexes from the stream. */
  310. for (cnt = 0; cnt <= numSubbands; cnt++) {
  311. if (subband_vlc_index[cnt] != 0)
  312. SF_idxs[cnt] = get_bits(gb, 6);
  313. }
  314. for (cnt = 0; cnt <= numSubbands; cnt++) {
  315. first = subbandTab[cnt];
  316. last = subbandTab[cnt+1];
  317. subbWidth = last - first;
  318. if (subband_vlc_index[cnt] != 0) {
  319. /* Decode spectral coefficients for this subband. */
  320. /* TODO: This can be done faster is several blocks share the
  321. * same VLC selector (subband_vlc_index) */
  322. readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
  323. /* Decode the scale factor for this subband. */
  324. SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
  325. /* Inverse quantize the coefficients. */
  326. for (pIn=mantissas ; first<last; first++, pIn++)
  327. pOut[first] = *pIn * SF;
  328. } else {
  329. /* This subband was not coded, so zero the entire subband. */
  330. memset(pOut+first, 0, subbWidth*sizeof(float));
  331. }
  332. }
  333. /* Clear the subbands that were not coded. */
  334. first = subbandTab[cnt];
  335. memset(pOut+first, 0, (1024 - first) * sizeof(float));
  336. return numSubbands;
  337. }
  338. /**
  339. * Restore the quantized tonal components
  340. *
  341. * @param gb the GetBit context
  342. * @param pComponent tone component
  343. * @param numBands amount of coded bands
  344. */
  345. static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
  346. {
  347. int i,j,k,cnt;
  348. int components, coding_mode_selector, coding_mode, coded_values_per_component;
  349. int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
  350. int band_flags[4], mantissa[8];
  351. float *pCoef;
  352. float scalefactor;
  353. int component_count = 0;
  354. components = get_bits(gb,5);
  355. /* no tonal components */
  356. if (components == 0)
  357. return 0;
  358. coding_mode_selector = get_bits(gb,2);
  359. if (coding_mode_selector == 2)
  360. return -1;
  361. coding_mode = coding_mode_selector & 1;
  362. for (i = 0; i < components; i++) {
  363. for (cnt = 0; cnt <= numBands; cnt++)
  364. band_flags[cnt] = get_bits1(gb);
  365. coded_values_per_component = get_bits(gb,3);
  366. quant_step_index = get_bits(gb,3);
  367. if (quant_step_index <= 1)
  368. return -1;
  369. if (coding_mode_selector == 3)
  370. coding_mode = get_bits1(gb);
  371. for (j = 0; j < (numBands + 1) * 4; j++) {
  372. if (band_flags[j >> 2] == 0)
  373. continue;
  374. coded_components = get_bits(gb,3);
  375. for (k=0; k<coded_components; k++) {
  376. sfIndx = get_bits(gb,6);
  377. pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
  378. max_coded_values = 1024 - pComponent[component_count].pos;
  379. coded_values = coded_values_per_component + 1;
  380. coded_values = FFMIN(max_coded_values,coded_values);
  381. scalefactor = SFTable[sfIndx] * iMaxQuant[quant_step_index];
  382. readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
  383. pComponent[component_count].numCoefs = coded_values;
  384. /* inverse quant */
  385. pCoef = pComponent[component_count].coef;
  386. for (cnt = 0; cnt < coded_values; cnt++)
  387. pCoef[cnt] = mantissa[cnt] * scalefactor;
  388. component_count++;
  389. }
  390. }
  391. }
  392. return component_count;
  393. }
  394. /**
  395. * Decode gain parameters for the coded bands
  396. *
  397. * @param gb the GetBit context
  398. * @param pGb the gainblock for the current band
  399. * @param numBands amount of coded bands
  400. */
  401. static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
  402. {
  403. int i, cf, numData;
  404. int *pLevel, *pLoc;
  405. gain_info *pGain = pGb->gBlock;
  406. for (i=0 ; i<=numBands; i++)
  407. {
  408. numData = get_bits(gb,3);
  409. pGain[i].num_gain_data = numData;
  410. pLevel = pGain[i].levcode;
  411. pLoc = pGain[i].loccode;
  412. for (cf = 0; cf < numData; cf++){
  413. pLevel[cf]= get_bits(gb,4);
  414. pLoc [cf]= get_bits(gb,5);
  415. if(cf && pLoc[cf] <= pLoc[cf-1])
  416. return -1;
  417. }
  418. }
  419. /* Clear the unused blocks. */
  420. for (; i<4 ; i++)
  421. pGain[i].num_gain_data = 0;
  422. return 0;
  423. }
  424. /**
  425. * Apply gain parameters and perform the MDCT overlapping part
  426. *
  427. * @param pIn input float buffer
  428. * @param pPrev previous float buffer to perform overlap against
  429. * @param pOut output float buffer
  430. * @param pGain1 current band gain info
  431. * @param pGain2 next band gain info
  432. */
  433. static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
  434. {
  435. /* gain compensation function */
  436. float gain1, gain2, gain_inc;
  437. int cnt, numdata, nsample, startLoc, endLoc;
  438. if (pGain2->num_gain_data == 0)
  439. gain1 = 1.0;
  440. else
  441. gain1 = gain_tab1[pGain2->levcode[0]];
  442. if (pGain1->num_gain_data == 0) {
  443. for (cnt = 0; cnt < 256; cnt++)
  444. pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
  445. } else {
  446. numdata = pGain1->num_gain_data;
  447. pGain1->loccode[numdata] = 32;
  448. pGain1->levcode[numdata] = 4;
  449. nsample = 0; // current sample = 0
  450. for (cnt = 0; cnt < numdata; cnt++) {
  451. startLoc = pGain1->loccode[cnt] * 8;
  452. endLoc = startLoc + 8;
  453. gain2 = gain_tab1[pGain1->levcode[cnt]];
  454. gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
  455. /* interpolate */
  456. for (; nsample < startLoc; nsample++)
  457. pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
  458. /* interpolation is done over eight samples */
  459. for (; nsample < endLoc; nsample++) {
  460. pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
  461. gain2 *= gain_inc;
  462. }
  463. }
  464. for (; nsample < 256; nsample++)
  465. pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
  466. }
  467. /* Delay for the overlapping part. */
  468. memcpy(pPrev, &pIn[256], 256*sizeof(float));
  469. }
  470. /**
  471. * Combine the tonal band spectrum and regular band spectrum
  472. * Return position of the last tonal coefficient
  473. *
  474. * @param pSpectrum output spectrum buffer
  475. * @param numComponents amount of tonal components
  476. * @param pComponent tonal components for this band
  477. */
  478. static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
  479. {
  480. int cnt, i, lastPos = -1;
  481. float *pIn, *pOut;
  482. for (cnt = 0; cnt < numComponents; cnt++){
  483. lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
  484. pIn = pComponent[cnt].coef;
  485. pOut = &(pSpectrum[pComponent[cnt].pos]);
  486. for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
  487. pOut[i] += pIn[i];
  488. }
  489. return lastPos;
  490. }
  491. #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
  492. static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
  493. {
  494. int i, band, nsample, s1, s2;
  495. float c1, c2;
  496. float mc1_l, mc1_r, mc2_l, mc2_r;
  497. for (i=0,band = 0; band < 4*256; band+=256,i++) {
  498. s1 = pPrevCode[i];
  499. s2 = pCurrCode[i];
  500. nsample = 0;
  501. if (s1 != s2) {
  502. /* Selector value changed, interpolation needed. */
  503. mc1_l = matrixCoeffs[s1*2];
  504. mc1_r = matrixCoeffs[s1*2+1];
  505. mc2_l = matrixCoeffs[s2*2];
  506. mc2_r = matrixCoeffs[s2*2+1];
  507. /* Interpolation is done over the first eight samples. */
  508. for(; nsample < 8; nsample++) {
  509. c1 = su1[band+nsample];
  510. c2 = su2[band+nsample];
  511. c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
  512. su1[band+nsample] = c2;
  513. su2[band+nsample] = c1 * 2.0 - c2;
  514. }
  515. }
  516. /* Apply the matrix without interpolation. */
  517. switch (s2) {
  518. case 0: /* M/S decoding */
  519. for (; nsample < 256; nsample++) {
  520. c1 = su1[band+nsample];
  521. c2 = su2[band+nsample];
  522. su1[band+nsample] = c2 * 2.0;
  523. su2[band+nsample] = (c1 - c2) * 2.0;
  524. }
  525. break;
  526. case 1:
  527. for (; nsample < 256; nsample++) {
  528. c1 = su1[band+nsample];
  529. c2 = su2[band+nsample];
  530. su1[band+nsample] = (c1 + c2) * 2.0;
  531. su2[band+nsample] = c2 * -2.0;
  532. }
  533. break;
  534. case 2:
  535. case 3:
  536. for (; nsample < 256; nsample++) {
  537. c1 = su1[band+nsample];
  538. c2 = su2[band+nsample];
  539. su1[band+nsample] = c1 + c2;
  540. su2[band+nsample] = c1 - c2;
  541. }
  542. break;
  543. default:
  544. assert(0);
  545. }
  546. }
  547. }
  548. static void getChannelWeights (int indx, int flag, float ch[2]){
  549. if (indx == 7) {
  550. ch[0] = 1.0;
  551. ch[1] = 1.0;
  552. } else {
  553. ch[0] = (float)(indx & 7) / 7.0;
  554. ch[1] = sqrt(2 - ch[0]*ch[0]);
  555. if(flag)
  556. FFSWAP(float, ch[0], ch[1]);
  557. }
  558. }
  559. static void channelWeighting (float *su1, float *su2, int *p3)
  560. {
  561. int band, nsample;
  562. /* w[x][y] y=0 is left y=1 is right */
  563. float w[2][2];
  564. if (p3[1] != 7 || p3[3] != 7){
  565. getChannelWeights(p3[1], p3[0], w[0]);
  566. getChannelWeights(p3[3], p3[2], w[1]);
  567. for(band = 1; band < 4; band++) {
  568. /* scale the channels by the weights */
  569. for(nsample = 0; nsample < 8; nsample++) {
  570. su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
  571. su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
  572. }
  573. for(; nsample < 256; nsample++) {
  574. su1[band*256+nsample] *= w[1][0];
  575. su2[band*256+nsample] *= w[1][1];
  576. }
  577. }
  578. }
  579. }
  580. /**
  581. * Decode a Sound Unit
  582. *
  583. * @param gb the GetBit context
  584. * @param pSnd the channel unit to be used
  585. * @param pOut the decoded samples before IQMF in float representation
  586. * @param channelNum channel number
  587. * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
  588. */
  589. static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
  590. {
  591. int band, result=0, numSubbands, lastTonal, numBands;
  592. if (codingMode == JOINT_STEREO && channelNum == 1) {
  593. if (get_bits(gb,2) != 3) {
  594. av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
  595. return -1;
  596. }
  597. } else {
  598. if (get_bits(gb,6) != 0x28) {
  599. av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
  600. return -1;
  601. }
  602. }
  603. /* number of coded QMF bands */
  604. pSnd->bandsCoded = get_bits(gb,2);
  605. result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
  606. if (result) return result;
  607. pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
  608. if (pSnd->numComponents == -1) return -1;
  609. numSubbands = decodeSpectrum (gb, pSnd->spectrum);
  610. /* Merge the decoded spectrum and tonal components. */
  611. lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
  612. /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
  613. numBands = (subbandTab[numSubbands] - 1) >> 8;
  614. if (lastTonal >= 0)
  615. numBands = FFMAX((lastTonal + 256) >> 8, numBands);
  616. /* Reconstruct time domain samples. */
  617. for (band=0; band<4; band++) {
  618. /* Perform the IMDCT step without overlapping. */
  619. if (band <= numBands) {
  620. IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
  621. } else
  622. memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
  623. /* gain compensation and overlapping */
  624. gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
  625. &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
  626. &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
  627. }
  628. /* Swap the gain control buffers for the next frame. */
  629. pSnd->gcBlkSwitch ^= 1;
  630. return 0;
  631. }
  632. /**
  633. * Frame handling
  634. *
  635. * @param q Atrac3 private context
  636. * @param databuf the input data
  637. */
  638. static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
  639. {
  640. int result, i;
  641. float *p1, *p2, *p3, *p4;
  642. uint8_t *ptr1;
  643. if (q->codingMode == JOINT_STEREO) {
  644. /* channel coupling mode */
  645. /* decode Sound Unit 1 */
  646. init_get_bits(&q->gb,databuf,q->bits_per_frame);
  647. result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
  648. if (result != 0)
  649. return (result);
  650. /* Framedata of the su2 in the joint-stereo mode is encoded in
  651. * reverse byte order so we need to swap it first. */
  652. if (databuf == q->decoded_bytes_buffer) {
  653. uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
  654. ptr1 = q->decoded_bytes_buffer;
  655. for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
  656. FFSWAP(uint8_t,*ptr1,*ptr2);
  657. }
  658. } else {
  659. const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
  660. for (i = 0; i < q->bytes_per_frame; i++)
  661. q->decoded_bytes_buffer[i] = *ptr2--;
  662. }
  663. /* Skip the sync codes (0xF8). */
  664. ptr1 = q->decoded_bytes_buffer;
  665. for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
  666. if (i >= q->bytes_per_frame)
  667. return -1;
  668. }
  669. /* set the bitstream reader at the start of the second Sound Unit*/
  670. init_get_bits(&q->gb,ptr1,q->bits_per_frame);
  671. /* Fill the Weighting coeffs delay buffer */
  672. memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
  673. q->weighting_delay[4] = get_bits1(&q->gb);
  674. q->weighting_delay[5] = get_bits(&q->gb,3);
  675. for (i = 0; i < 4; i++) {
  676. q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
  677. q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
  678. q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
  679. }
  680. /* Decode Sound Unit 2. */
  681. result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
  682. if (result != 0)
  683. return (result);
  684. /* Reconstruct the channel coefficients. */
  685. reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
  686. channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
  687. } else {
  688. /* normal stereo mode or mono */
  689. /* Decode the channel sound units. */
  690. for (i=0 ; i<q->channels ; i++) {
  691. /* Set the bitstream reader at the start of a channel sound unit. */
  692. init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
  693. result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
  694. if (result != 0)
  695. return (result);
  696. }
  697. }
  698. /* Apply the iQMF synthesis filter. */
  699. p1= q->outSamples;
  700. for (i=0 ; i<q->channels ; i++) {
  701. p2= p1+256;
  702. p3= p2+256;
  703. p4= p3+256;
  704. iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
  705. iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
  706. iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
  707. p1 +=1024;
  708. }
  709. return 0;
  710. }
  711. /**
  712. * Atrac frame decoding
  713. *
  714. * @param avctx pointer to the AVCodecContext
  715. */
  716. static int atrac3_decode_frame(AVCodecContext *avctx,
  717. void *data, int *data_size,
  718. AVPacket *avpkt) {
  719. const uint8_t *buf = avpkt->data;
  720. int buf_size = avpkt->size;
  721. ATRAC3Context *q = avctx->priv_data;
  722. int result = 0, i;
  723. const uint8_t* databuf;
  724. int16_t* samples = data;
  725. if (buf_size < avctx->block_align)
  726. return buf_size;
  727. /* Check if we need to descramble and what buffer to pass on. */
  728. if (q->scrambled_stream) {
  729. decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
  730. databuf = q->decoded_bytes_buffer;
  731. } else {
  732. databuf = buf;
  733. }
  734. result = decodeFrame(q, databuf);
  735. if (result != 0) {
  736. av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
  737. return -1;
  738. }
  739. if (q->channels == 1) {
  740. /* mono */
  741. for (i = 0; i<1024; i++)
  742. samples[i] = av_clip_int16(round(q->outSamples[i]));
  743. *data_size = 1024 * sizeof(int16_t);
  744. } else {
  745. /* stereo */
  746. for (i = 0; i < 1024; i++) {
  747. samples[i*2] = av_clip_int16(round(q->outSamples[i]));
  748. samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
  749. }
  750. *data_size = 2048 * sizeof(int16_t);
  751. }
  752. return avctx->block_align;
  753. }
  754. /**
  755. * Atrac3 initialization
  756. *
  757. * @param avctx pointer to the AVCodecContext
  758. */
  759. static av_cold int atrac3_decode_init(AVCodecContext *avctx)
  760. {
  761. int i;
  762. const uint8_t *edata_ptr = avctx->extradata;
  763. ATRAC3Context *q = avctx->priv_data;
  764. static VLC_TYPE atrac3_vlc_table[4096][2];
  765. static int vlcs_initialized = 0;
  766. /* Take data from the AVCodecContext (RM container). */
  767. q->sample_rate = avctx->sample_rate;
  768. q->channels = avctx->channels;
  769. q->bit_rate = avctx->bit_rate;
  770. q->bits_per_frame = avctx->block_align * 8;
  771. q->bytes_per_frame = avctx->block_align;
  772. /* Take care of the codec-specific extradata. */
  773. if (avctx->extradata_size == 14) {
  774. /* Parse the extradata, WAV format */
  775. av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value always 1
  776. q->samples_per_channel = bytestream_get_le32(&edata_ptr);
  777. q->codingMode = bytestream_get_le16(&edata_ptr);
  778. av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
  779. q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1
  780. av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0
  781. /* setup */
  782. q->samples_per_frame = 1024 * q->channels;
  783. q->atrac3version = 4;
  784. q->delay = 0x88E;
  785. if (q->codingMode)
  786. q->codingMode = JOINT_STEREO;
  787. else
  788. q->codingMode = STEREO;
  789. q->scrambled_stream = 0;
  790. 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)) {
  791. } else {
  792. 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);
  793. return -1;
  794. }
  795. } else if (avctx->extradata_size == 10) {
  796. /* Parse the extradata, RM format. */
  797. q->atrac3version = bytestream_get_be32(&edata_ptr);
  798. q->samples_per_frame = bytestream_get_be16(&edata_ptr);
  799. q->delay = bytestream_get_be16(&edata_ptr);
  800. q->codingMode = bytestream_get_be16(&edata_ptr);
  801. q->samples_per_channel = q->samples_per_frame / q->channels;
  802. q->scrambled_stream = 1;
  803. } else {
  804. av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
  805. }
  806. /* Check the extradata. */
  807. if (q->atrac3version != 4) {
  808. av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
  809. return -1;
  810. }
  811. if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
  812. av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
  813. return -1;
  814. }
  815. if (q->delay != 0x88E) {
  816. av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
  817. return -1;
  818. }
  819. if (q->codingMode == STEREO) {
  820. av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
  821. } else if (q->codingMode == JOINT_STEREO) {
  822. av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
  823. } else {
  824. av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
  825. return -1;
  826. }
  827. if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
  828. av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
  829. return -1;
  830. }
  831. if(avctx->block_align >= UINT_MAX/2)
  832. return -1;
  833. /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
  834. * this is for the bitstream reader. */
  835. if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE))) == NULL)
  836. return AVERROR(ENOMEM);
  837. /* Initialize the VLC tables. */
  838. if (!vlcs_initialized) {
  839. for (i=0 ; i<7 ; i++) {
  840. spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
  841. spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
  842. init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
  843. huff_bits[i], 1, 1,
  844. huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
  845. }
  846. vlcs_initialized = 1;
  847. }
  848. init_atrac3_transforms(q);
  849. /* Generate the scale factors. */
  850. for (i=0 ; i<64 ; i++)
  851. SFTable[i] = pow(2.0, (i - 15) / 3.0);
  852. /* Generate gain tables. */
  853. for (i=0 ; i<16 ; i++)
  854. gain_tab1[i] = powf (2.0, (4 - i));
  855. for (i=-15 ; i<16 ; i++)
  856. gain_tab2[i+15] = powf (2.0, i * -0.125);
  857. /* init the joint-stereo decoding data */
  858. q->weighting_delay[0] = 0;
  859. q->weighting_delay[1] = 7;
  860. q->weighting_delay[2] = 0;
  861. q->weighting_delay[3] = 7;
  862. q->weighting_delay[4] = 0;
  863. q->weighting_delay[5] = 7;
  864. for (i=0; i<4; i++) {
  865. q->matrix_coeff_index_prev[i] = 3;
  866. q->matrix_coeff_index_now[i] = 3;
  867. q->matrix_coeff_index_next[i] = 3;
  868. }
  869. dsputil_init(&dsp, avctx);
  870. q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
  871. if (!q->pUnits) {
  872. av_free(q->decoded_bytes_buffer);
  873. return AVERROR(ENOMEM);
  874. }
  875. avctx->sample_fmt = SAMPLE_FMT_S16;
  876. return 0;
  877. }
  878. AVCodec atrac3_decoder =
  879. {
  880. .name = "atrac3",
  881. .type = CODEC_TYPE_AUDIO,
  882. .id = CODEC_ID_ATRAC3,
  883. .priv_data_size = sizeof(ATRAC3Context),
  884. .init = atrac3_decode_init,
  885. .close = atrac3_decode_close,
  886. .decode = atrac3_decode_frame,
  887. .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
  888. };