atrac3.c
Go to the documentation of this file.
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 
39 #include "avcodec.h"
40 #include "internal.h"
41 #include "get_bits.h"
42 #include "dsputil.h"
43 #include "bytestream.h"
44 #include "fft.h"
45 #include "fmtconvert.h"
46 
47 #include "atrac.h"
48 #include "atrac3data.h"
49 
50 #define JOINT_STEREO 0x12
51 #define STEREO 0x2
52 
53 #define SAMPLES_PER_FRAME 1024
54 #define MDCT_SIZE 512
55 
56 /* These structures are needed to store the parsed gain control data. */
57 typedef struct {
59  int levcode[8];
60  int loccode[8];
61 } gain_info;
62 
63 typedef struct {
64  gain_info gBlock[4];
65 } gain_block;
66 
67 typedef struct {
68  int pos;
69  int numCoefs;
70  float coef[8];
72 
73 typedef struct {
76  tonal_component components[64];
77  float prevFrame[SAMPLES_PER_FRAME];
79  gain_block gainBlock[2];
80 
81  DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME];
82  DECLARE_ALIGNED(32, float, IMDCT_buf)[SAMPLES_PER_FRAME];
83 
84  float delayBuf1[46];
85  float delayBuf2[46];
86  float delayBuf3[46];
87 } channel_unit;
88 
89 typedef struct {
93 
94  int channels;
96  int bit_rate;
100 
103  int pBs;
106 
107 
108  int matrix_coeff_index_prev[4];
109  int matrix_coeff_index_now[4];
110  int matrix_coeff_index_next[4];
111  int weighting_delay[6];
113 
114 
115  float *outSamples[2];
117  float tempBuf[1070];
119 
120 
122  int delay;
126 
129 } ATRAC3Context;
130 
133 static float gain_tab1[16];
134 static float gain_tab2[31];
136 
137 
147 static void IMLT(ATRAC3Context *q, float *pInput, float *pOutput, int odd_band)
148 {
149  int i;
150 
151  if (odd_band) {
161  for (i=0; i<128; i++)
162  FFSWAP(float, pInput[i], pInput[255-i]);
163  }
164 
165  q->mdct_ctx.imdct_calc(&q->mdct_ctx,pOutput,pInput);
166 
167  /* Perform windowing on the output. */
168  dsp.vector_fmul(pOutput, pOutput, mdct_window, MDCT_SIZE);
169 
170 }
171 
172 
181 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
182  int i, off;
183  uint32_t c;
184  const uint32_t* buf;
185  uint32_t* obuf = (uint32_t*) out;
186 
187  off = (intptr_t)inbuffer & 3;
188  buf = (const uint32_t *)(inbuffer - off);
189  if (off)
190  c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
191  else
192  c = av_be2ne32(0x537F6103U);
193  bytes += 3 + off;
194  for (i = 0; i < bytes/4; i++)
195  obuf[i] = c ^ buf[i];
196 
197  if (off)
198  av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
199 
200  return off;
201 }
202 
203 
204 static av_cold int init_atrac3_transforms(ATRAC3Context *q, int is_float) {
205  float enc_window[256];
206  int i;
207 
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 
213  if (!mdct_window[0])
214  for (i=0 ; i<256; i++) {
215  mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
216  mdct_window[511-i] = mdct_window[i];
217  }
218 
219  /* Initialize the MDCT transform. */
220  return ff_mdct_init(&q->mdct_ctx, 9, 1, is_float ? 1.0 / 32768 : 1.0);
221 }
222 
228 {
229  ATRAC3Context *q = avctx->priv_data;
230 
231  av_free(q->pUnits);
233  av_freep(&q->outSamples[0]);
234 
235  ff_mdct_end(&q->mdct_ctx);
236 
237  return 0;
238 }
239 
250 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
251 {
252  int numBits, cnt, code, huffSymb;
253 
254  if (selector == 1)
255  numCodes /= 2;
256 
257  if (codingFlag != 0) {
258  /* constant length coding (CLC) */
259  numBits = CLCLengthTab[selector];
260 
261  if (selector > 1) {
262  for (cnt = 0; cnt < numCodes; cnt++) {
263  if (numBits)
264  code = get_sbits(gb, numBits);
265  else
266  code = 0;
267  mantissas[cnt] = code;
268  }
269  } else {
270  for (cnt = 0; cnt < numCodes; cnt++) {
271  if (numBits)
272  code = get_bits(gb, numBits); //numBits is always 4 in this case
273  else
274  code = 0;
275  mantissas[cnt*2] = seTab_0[code >> 2];
276  mantissas[cnt*2+1] = seTab_0[code & 3];
277  }
278  }
279  } else {
280  /* variable length coding (VLC) */
281  if (selector != 1) {
282  for (cnt = 0; cnt < numCodes; cnt++) {
283  huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
284  huffSymb += 1;
285  code = huffSymb >> 1;
286  if (huffSymb & 1)
287  code = -code;
288  mantissas[cnt] = code;
289  }
290  } else {
291  for (cnt = 0; cnt < numCodes; cnt++) {
292  huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
293  mantissas[cnt*2] = decTable1[huffSymb*2];
294  mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
295  }
296  }
297  }
298 }
299 
308 static int decodeSpectrum (GetBitContext *gb, float *pOut)
309 {
310  int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
311  int subband_vlc_index[32], SF_idxs[32];
312  int mantissas[128];
313  float SF;
314 
315  numSubbands = get_bits(gb, 5); // number of coded subbands
316  codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
317 
318  /* Get the VLC selector table for the subbands, 0 means not coded. */
319  for (cnt = 0; cnt <= numSubbands; cnt++)
320  subband_vlc_index[cnt] = get_bits(gb, 3);
321 
322  /* Read the scale factor indexes from the stream. */
323  for (cnt = 0; cnt <= numSubbands; cnt++) {
324  if (subband_vlc_index[cnt] != 0)
325  SF_idxs[cnt] = get_bits(gb, 6);
326  }
327 
328  for (cnt = 0; cnt <= numSubbands; cnt++) {
329  first = subbandTab[cnt];
330  last = subbandTab[cnt+1];
331 
332  subbWidth = last - first;
333 
334  if (subband_vlc_index[cnt] != 0) {
335  /* Decode spectral coefficients for this subband. */
336  /* TODO: This can be done faster is several blocks share the
337  * same VLC selector (subband_vlc_index) */
338  readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
339 
340  /* Decode the scale factor for this subband. */
341  SF = ff_atrac_sf_table[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
342 
343  /* Inverse quantize the coefficients. */
344  for (pIn=mantissas ; first<last; first++, pIn++)
345  pOut[first] = *pIn * SF;
346  } else {
347  /* This subband was not coded, so zero the entire subband. */
348  memset(pOut+first, 0, subbWidth*sizeof(float));
349  }
350  }
351 
352  /* Clear the subbands that were not coded. */
353  first = subbandTab[cnt];
354  memset(pOut+first, 0, (SAMPLES_PER_FRAME - first) * sizeof(float));
355  return numSubbands;
356 }
357 
366 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
367 {
368  int i,j,k,cnt;
369  int components, coding_mode_selector, coding_mode, coded_values_per_component;
370  int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
371  int band_flags[4], mantissa[8];
372  float *pCoef;
373  float scalefactor;
374  int component_count = 0;
375 
376  components = get_bits(gb,5);
377 
378  /* no tonal components */
379  if (components == 0)
380  return 0;
381 
382  coding_mode_selector = get_bits(gb,2);
383  if (coding_mode_selector == 2)
384  return AVERROR_INVALIDDATA;
385 
386  coding_mode = coding_mode_selector & 1;
387 
388  for (i = 0; i < components; i++) {
389  for (cnt = 0; cnt <= numBands; cnt++)
390  band_flags[cnt] = get_bits1(gb);
391 
392  coded_values_per_component = get_bits(gb,3);
393 
394  quant_step_index = get_bits(gb,3);
395  if (quant_step_index <= 1)
396  return AVERROR_INVALIDDATA;
397 
398  if (coding_mode_selector == 3)
399  coding_mode = get_bits1(gb);
400 
401  for (j = 0; j < (numBands + 1) * 4; j++) {
402  if (band_flags[j >> 2] == 0)
403  continue;
404 
405  coded_components = get_bits(gb,3);
406 
407  for (k=0; k<coded_components; k++) {
408  sfIndx = get_bits(gb,6);
409  if (component_count >= 64)
410  return AVERROR_INVALIDDATA;
411  pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
412  max_coded_values = SAMPLES_PER_FRAME - pComponent[component_count].pos;
413  coded_values = coded_values_per_component + 1;
414  coded_values = FFMIN(max_coded_values,coded_values);
415 
416  scalefactor = ff_atrac_sf_table[sfIndx] * iMaxQuant[quant_step_index];
417 
418  readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
419 
420  pComponent[component_count].numCoefs = coded_values;
421 
422  /* inverse quant */
423  pCoef = pComponent[component_count].coef;
424  for (cnt = 0; cnt < coded_values; cnt++)
425  pCoef[cnt] = mantissa[cnt] * scalefactor;
426 
427  component_count++;
428  }
429  }
430  }
431 
432  return component_count;
433 }
434 
443 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
444 {
445  int i, cf, numData;
446  int *pLevel, *pLoc;
447 
448  gain_info *pGain = pGb->gBlock;
449 
450  for (i=0 ; i<=numBands; i++)
451  {
452  numData = get_bits(gb,3);
453  pGain[i].num_gain_data = numData;
454  pLevel = pGain[i].levcode;
455  pLoc = pGain[i].loccode;
456 
457  for (cf = 0; cf < numData; cf++){
458  pLevel[cf]= get_bits(gb,4);
459  pLoc [cf]= get_bits(gb,5);
460  if(cf && pLoc[cf] <= pLoc[cf-1])
461  return AVERROR_INVALIDDATA;
462  }
463  }
464 
465  /* Clear the unused blocks. */
466  for (; i<4 ; i++)
467  pGain[i].num_gain_data = 0;
468 
469  return 0;
470 }
471 
482 static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, gain_info *pGain1, gain_info *pGain2)
483 {
484  /* gain compensation function */
485  float gain1, gain2, gain_inc;
486  int cnt, numdata, nsample, startLoc, endLoc;
487 
488 
489  if (pGain2->num_gain_data == 0)
490  gain1 = 1.0;
491  else
492  gain1 = gain_tab1[pGain2->levcode[0]];
493 
494  if (pGain1->num_gain_data == 0) {
495  for (cnt = 0; cnt < 256; cnt++)
496  pOut[cnt] = pIn[cnt] * gain1 + pPrev[cnt];
497  } else {
498  numdata = pGain1->num_gain_data;
499  pGain1->loccode[numdata] = 32;
500  pGain1->levcode[numdata] = 4;
501 
502  nsample = 0; // current sample = 0
503 
504  for (cnt = 0; cnt < numdata; cnt++) {
505  startLoc = pGain1->loccode[cnt] * 8;
506  endLoc = startLoc + 8;
507 
508  gain2 = gain_tab1[pGain1->levcode[cnt]];
509  gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
510 
511  /* interpolate */
512  for (; nsample < startLoc; nsample++)
513  pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
514 
515  /* interpolation is done over eight samples */
516  for (; nsample < endLoc; nsample++) {
517  pOut[nsample] = (pIn[nsample] * gain1 + pPrev[nsample]) * gain2;
518  gain2 *= gain_inc;
519  }
520  }
521 
522  for (; nsample < 256; nsample++)
523  pOut[nsample] = (pIn[nsample] * gain1) + pPrev[nsample];
524  }
525 
526  /* Delay for the overlapping part. */
527  memcpy(pPrev, &pIn[256], 256*sizeof(float));
528 }
529 
539 static int addTonalComponents (float *pSpectrum, int numComponents, tonal_component *pComponent)
540 {
541  int cnt, i, lastPos = -1;
542  float *pIn, *pOut;
543 
544  for (cnt = 0; cnt < numComponents; cnt++){
545  lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
546  pIn = pComponent[cnt].coef;
547  pOut = &(pSpectrum[pComponent[cnt].pos]);
548 
549  for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
550  pOut[i] += pIn[i];
551  }
552 
553  return lastPos;
554 }
555 
556 
557 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
558 
559 static void reverseMatrixing(float *su1, float *su2, int *pPrevCode, int *pCurrCode)
560 {
561  int i, band, nsample, s1, s2;
562  float c1, c2;
563  float mc1_l, mc1_r, mc2_l, mc2_r;
564 
565  for (i=0,band = 0; band < 4*256; band+=256,i++) {
566  s1 = pPrevCode[i];
567  s2 = pCurrCode[i];
568  nsample = 0;
569 
570  if (s1 != s2) {
571  /* Selector value changed, interpolation needed. */
572  mc1_l = matrixCoeffs[s1*2];
573  mc1_r = matrixCoeffs[s1*2+1];
574  mc2_l = matrixCoeffs[s2*2];
575  mc2_r = matrixCoeffs[s2*2+1];
576 
577  /* Interpolation is done over the first eight samples. */
578  for(; nsample < 8; nsample++) {
579  c1 = su1[band+nsample];
580  c2 = su2[band+nsample];
581  c2 = c1 * INTERPOLATE(mc1_l,mc2_l,nsample) + c2 * INTERPOLATE(mc1_r,mc2_r,nsample);
582  su1[band+nsample] = c2;
583  su2[band+nsample] = c1 * 2.0 - c2;
584  }
585  }
586 
587  /* Apply the matrix without interpolation. */
588  switch (s2) {
589  case 0: /* M/S decoding */
590  for (; nsample < 256; nsample++) {
591  c1 = su1[band+nsample];
592  c2 = su2[band+nsample];
593  su1[band+nsample] = c2 * 2.0;
594  su2[band+nsample] = (c1 - c2) * 2.0;
595  }
596  break;
597 
598  case 1:
599  for (; nsample < 256; nsample++) {
600  c1 = su1[band+nsample];
601  c2 = su2[band+nsample];
602  su1[band+nsample] = (c1 + c2) * 2.0;
603  su2[band+nsample] = c2 * -2.0;
604  }
605  break;
606  case 2:
607  case 3:
608  for (; nsample < 256; nsample++) {
609  c1 = su1[band+nsample];
610  c2 = su2[band+nsample];
611  su1[band+nsample] = c1 + c2;
612  su2[band+nsample] = c1 - c2;
613  }
614  break;
615  default:
616  assert(0);
617  }
618  }
619 }
620 
621 static void getChannelWeights (int indx, int flag, float ch[2]){
622 
623  if (indx == 7) {
624  ch[0] = 1.0;
625  ch[1] = 1.0;
626  } else {
627  ch[0] = (float)(indx & 7) / 7.0;
628  ch[1] = sqrt(2 - ch[0]*ch[0]);
629  if(flag)
630  FFSWAP(float, ch[0], ch[1]);
631  }
632 }
633 
634 static void channelWeighting (float *su1, float *su2, int *p3)
635 {
636  int band, nsample;
637  /* w[x][y] y=0 is left y=1 is right */
638  float w[2][2];
639 
640  if (p3[1] != 7 || p3[3] != 7){
641  getChannelWeights(p3[1], p3[0], w[0]);
642  getChannelWeights(p3[3], p3[2], w[1]);
643 
644  for(band = 1; band < 4; band++) {
645  /* scale the channels by the weights */
646  for(nsample = 0; nsample < 8; nsample++) {
647  su1[band*256+nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample);
648  su2[band*256+nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample);
649  }
650 
651  for(; nsample < 256; nsample++) {
652  su1[band*256+nsample] *= w[1][0];
653  su2[band*256+nsample] *= w[1][1];
654  }
655  }
656  }
657 }
658 
659 
671 static int decodeChannelSoundUnit (ATRAC3Context *q, GetBitContext *gb, channel_unit *pSnd, float *pOut, int channelNum, int codingMode)
672 {
673  int band, result=0, numSubbands, lastTonal, numBands;
674 
675  if (codingMode == JOINT_STEREO && channelNum == 1) {
676  if (get_bits(gb,2) != 3) {
677  av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
678  return AVERROR_INVALIDDATA;
679  }
680  } else {
681  if (get_bits(gb,6) != 0x28) {
682  av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
683  return AVERROR_INVALIDDATA;
684  }
685  }
686 
687  /* number of coded QMF bands */
688  pSnd->bandsCoded = get_bits(gb,2);
689 
690  result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
691  if (result) return result;
692 
693  pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
694  if (pSnd->numComponents < 0)
695  return pSnd->numComponents;
696 
697  numSubbands = decodeSpectrum (gb, pSnd->spectrum);
698 
699  /* Merge the decoded spectrum and tonal components. */
700  lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
701 
702 
703  /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
704  numBands = (subbandTab[numSubbands] - 1) >> 8;
705  if (lastTonal >= 0)
706  numBands = FFMAX((lastTonal + 256) >> 8, numBands);
707 
708 
709  /* Reconstruct time domain samples. */
710  for (band=0; band<4; band++) {
711  /* Perform the IMDCT step without overlapping. */
712  if (band <= numBands) {
713  IMLT(q, &(pSnd->spectrum[band*256]), pSnd->IMDCT_buf, band&1);
714  } else
715  memset(pSnd->IMDCT_buf, 0, 512 * sizeof(float));
716 
717  /* gain compensation and overlapping */
718  gainCompensateAndOverlap(pSnd->IMDCT_buf, &pSnd->prevFrame[band * 256],
719  &pOut[band * 256],
720  &pSnd->gainBlock[1 - pSnd->gcBlkSwitch].gBlock[band],
721  &pSnd->gainBlock[ pSnd->gcBlkSwitch].gBlock[band]);
722  }
723 
724  /* Swap the gain control buffers for the next frame. */
725  pSnd->gcBlkSwitch ^= 1;
726 
727  return 0;
728 }
729 
737 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf,
738  float **out_samples)
739 {
740  int result, i;
741  float *p1, *p2, *p3, *p4;
742  uint8_t *ptr1;
743 
744  if (q->codingMode == JOINT_STEREO) {
745 
746  /* channel coupling mode */
747  /* decode Sound Unit 1 */
748  init_get_bits(&q->gb,databuf,q->bits_per_frame);
749 
750  result = decodeChannelSoundUnit(q,&q->gb, q->pUnits, out_samples[0], 0, JOINT_STEREO);
751  if (result != 0)
752  return result;
753 
754  /* Framedata of the su2 in the joint-stereo mode is encoded in
755  * reverse byte order so we need to swap it first. */
756  if (databuf == q->decoded_bytes_buffer) {
757  uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
758  ptr1 = q->decoded_bytes_buffer;
759  for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
760  FFSWAP(uint8_t,*ptr1,*ptr2);
761  }
762  } else {
763  const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
764  for (i = 0; i < q->bytes_per_frame; i++)
765  q->decoded_bytes_buffer[i] = *ptr2--;
766  }
767 
768  /* Skip the sync codes (0xF8). */
769  ptr1 = q->decoded_bytes_buffer;
770  for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
771  if (i >= q->bytes_per_frame)
772  return AVERROR_INVALIDDATA;
773  }
774 
775 
776  /* set the bitstream reader at the start of the second Sound Unit*/
777  init_get_bits(&q->gb, ptr1, (q->bytes_per_frame - i) * 8);
778 
779  /* Fill the Weighting coeffs delay buffer */
780  memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
781  q->weighting_delay[4] = get_bits1(&q->gb);
782  q->weighting_delay[5] = get_bits(&q->gb,3);
783 
784  for (i = 0; i < 4; i++) {
787  q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
788  }
789 
790  /* Decode Sound Unit 2. */
791  result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[1], out_samples[1], 1, JOINT_STEREO);
792  if (result != 0)
793  return result;
794 
795  /* Reconstruct the channel coefficients. */
796  reverseMatrixing(out_samples[0], out_samples[1], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
797 
798  channelWeighting(out_samples[0], out_samples[1], q->weighting_delay);
799 
800  } else {
801  /* normal stereo mode or mono */
802  /* Decode the channel sound units. */
803  for (i=0 ; i<q->channels ; i++) {
804 
805  /* Set the bitstream reader at the start of a channel sound unit. */
806  init_get_bits(&q->gb,
807  databuf + i * q->bytes_per_frame / q->channels,
808  q->bits_per_frame / q->channels);
809 
810  result = decodeChannelSoundUnit(q,&q->gb, &q->pUnits[i], out_samples[i], i, q->codingMode);
811  if (result != 0)
812  return result;
813  }
814  }
815 
816  /* Apply the iQMF synthesis filter. */
817  for (i=0 ; i<q->channels ; i++) {
818  p1 = out_samples[i];
819  p2= p1+256;
820  p3= p2+256;
821  p4= p3+256;
822  atrac_iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
823  atrac_iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
824  atrac_iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
825  }
826 
827  return 0;
828 }
829 
830 
837 static int atrac3_decode_frame(AVCodecContext *avctx, void *data,
838  int *got_frame_ptr, AVPacket *avpkt)
839 {
840  const uint8_t *buf = avpkt->data;
841  int buf_size = avpkt->size;
842  ATRAC3Context *q = avctx->priv_data;
843  int result;
844  const uint8_t* databuf;
845  float *samples_flt;
846  int16_t *samples_s16;
847 
848  if (buf_size < avctx->block_align) {
849  av_log(avctx, AV_LOG_ERROR,
850  "Frame too small (%d bytes). Truncated file?\n", buf_size);
851  return AVERROR_INVALIDDATA;
852  }
853 
854  /* get output buffer */
856  if ((result = ff_get_buffer(avctx, &q->frame)) < 0) {
857  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
858  return result;
859  }
860  samples_flt = (float *)q->frame.data[0];
861  samples_s16 = (int16_t *)q->frame.data[0];
862 
863  /* Check if we need to descramble and what buffer to pass on. */
864  if (q->scrambled_stream) {
866  databuf = q->decoded_bytes_buffer;
867  } else {
868  databuf = buf;
869  }
870 
871  if (q->channels == 1 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
872  result = decodeFrame(q, databuf, &samples_flt);
873  else
874  result = decodeFrame(q, databuf, q->outSamples);
875 
876  if (result != 0) {
877  av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
878  return result;
879  }
880 
881  /* interleave */
882  if (q->channels == 2 && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
883  q->fmt_conv.float_interleave(samples_flt,
884  (const float **)q->outSamples,
885  SAMPLES_PER_FRAME, 2);
886  } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
887  q->fmt_conv.float_to_int16_interleave(samples_s16,
888  (const float **)q->outSamples,
890  }
891 
892  *got_frame_ptr = 1;
893  *(AVFrame *)data = q->frame;
894 
895  return avctx->block_align;
896 }
897 
898 
906 {
907  int i, ret;
908  const uint8_t *edata_ptr = avctx->extradata;
909  ATRAC3Context *q = avctx->priv_data;
910  static VLC_TYPE atrac3_vlc_table[4096][2];
911  static int vlcs_initialized = 0;
912 
913  /* Take data from the AVCodecContext (RM container). */
914  q->sample_rate = avctx->sample_rate;
915  q->channels = avctx->channels;
916  q->bit_rate = avctx->bit_rate;
917  q->bits_per_frame = avctx->block_align * 8;
918  q->bytes_per_frame = avctx->block_align;
919 
920  /* Take care of the codec-specific extradata. */
921  if (avctx->extradata_size == 14) {
922  /* Parse the extradata, WAV format */
923  av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value always 1
924  q->samples_per_channel = bytestream_get_le32(&edata_ptr);
925  q->codingMode = bytestream_get_le16(&edata_ptr);
926  av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
927  q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1
928  av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0
929 
930  /* setup */
932  q->atrac3version = 4;
933  q->delay = 0x88E;
934  if (q->codingMode)
936  else
937  q->codingMode = STEREO;
938 
939  q->scrambled_stream = 0;
940 
941  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)) {
942  } else {
943  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);
944  return AVERROR_INVALIDDATA;
945  }
946 
947  } else if (avctx->extradata_size == 10) {
948  /* Parse the extradata, RM format. */
949  q->atrac3version = bytestream_get_be32(&edata_ptr);
950  q->samples_per_frame = bytestream_get_be16(&edata_ptr);
951  q->delay = bytestream_get_be16(&edata_ptr);
952  q->codingMode = bytestream_get_be16(&edata_ptr);
953 
955  q->scrambled_stream = 1;
956 
957  } else {
958  av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
959  }
960  /* Check the extradata. */
961 
962  if (q->atrac3version != 4) {
963  av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
964  return AVERROR_INVALIDDATA;
965  }
966 
968  av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
969  return AVERROR_INVALIDDATA;
970  }
971 
972  if (q->delay != 0x88E) {
973  av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
974  return AVERROR_INVALIDDATA;
975  }
976 
977  if (q->codingMode == STEREO) {
978  av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
979  } else if (q->codingMode == JOINT_STEREO) {
980  if (avctx->channels != 2)
981  return AVERROR_INVALIDDATA;
982  av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
983  } else {
984  av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
985  return AVERROR_INVALIDDATA;
986  }
987 
988  if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
989  av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
990  return AVERROR(EINVAL);
991  }
992 
993 
994  if(avctx->block_align >= UINT_MAX/2)
995  return AVERROR(EINVAL);
996 
997  /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
998  * this is for the bitstream reader. */
1000  return AVERROR(ENOMEM);
1001 
1002 
1003  /* Initialize the VLC tables. */
1004  if (!vlcs_initialized) {
1005  for (i=0 ; i<7 ; i++) {
1006  spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
1007  spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
1008  init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
1009  huff_bits[i], 1, 1,
1011  }
1012  vlcs_initialized = 1;
1013  }
1014 
1015  if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT)
1016  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1017  else
1018  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1019 
1020  if ((ret = init_atrac3_transforms(q, avctx->sample_fmt == AV_SAMPLE_FMT_FLT))) {
1021  av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
1023  return ret;
1024  }
1025 
1027 
1028  /* Generate gain tables. */
1029  for (i=0 ; i<16 ; i++)
1030  gain_tab1[i] = powf (2.0, (4 - i));
1031 
1032  for (i=-15 ; i<16 ; i++)
1033  gain_tab2[i+15] = powf (2.0, i * -0.125);
1034 
1035  /* init the joint-stereo decoding data */
1036  q->weighting_delay[0] = 0;
1037  q->weighting_delay[1] = 7;
1038  q->weighting_delay[2] = 0;
1039  q->weighting_delay[3] = 7;
1040  q->weighting_delay[4] = 0;
1041  q->weighting_delay[5] = 7;
1042 
1043  for (i=0; i<4; i++) {
1044  q->matrix_coeff_index_prev[i] = 3;
1045  q->matrix_coeff_index_now[i] = 3;
1046  q->matrix_coeff_index_next[i] = 3;
1047  }
1048 
1049  dsputil_init(&dsp, avctx);
1050  ff_fmt_convert_init(&q->fmt_conv, avctx);
1051 
1052  q->pUnits = av_mallocz(sizeof(channel_unit)*q->channels);
1053  if (!q->pUnits) {
1054  atrac3_decode_close(avctx);
1055  return AVERROR(ENOMEM);
1056  }
1057 
1058  if (avctx->channels > 1 || avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
1059  q->outSamples[0] = av_mallocz(SAMPLES_PER_FRAME * avctx->channels * sizeof(*q->outSamples[0]));
1060  q->outSamples[1] = q->outSamples[0] + SAMPLES_PER_FRAME;
1061  if (!q->outSamples[0]) {
1062  atrac3_decode_close(avctx);
1063  return AVERROR(ENOMEM);
1064  }
1065  }
1066 
1068  avctx->coded_frame = &q->frame;
1069 
1070  return 0;
1071 }
1072 
1073 
1075 {
1076  .name = "atrac3",
1077  .type = AVMEDIA_TYPE_AUDIO,
1078  .id = CODEC_ID_ATRAC3,
1079  .priv_data_size = sizeof(ATRAC3Context),
1083  .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1084  .long_name = NULL_IF_CONFIG_SMALL("Atrac 3 (Adaptive TRansform Acoustic Coding 3)"),
1085 };