Work on atrac Joint Stereo mode. Correct calculation in getChannelWeights(), introduc...
[kugel-rb.git] / apps / codecs / libatrac / atrac3.c
blob5ff3a8587b6b89e0374fc69c120ac524db0ecfdf
1 /*
2 * Atrac 3 compatible decoder
3 * Copyright (c) 2006-2008 Maxim Poliakovski
4 * Copyright (c) 2006-2008 Benjamin Larsson
6 * This file is part of FFmpeg.
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.
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.
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
23 /**
24 * @file libavcodec/atrac3.c
25 * Atrac 3 compatible decoder.
26 * This decoder handles Sony's ATRAC3 data.
28 * Container formats used to store atrac 3 data:
29 * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
31 * To use this decoder, a calling application must supply the extradata
32 * bytes provided in the containers above.
35 #include <math.h>
36 #include <stddef.h>
37 #include <stdio.h>
39 #include "atrac3.h"
40 #include "atrac3data.h"
41 #include "atrac3data_fixed.h"
42 #include "fixp_math.h"
43 #include "../lib/mdct2.h"
45 #define JOINT_STEREO 0x12
46 #define STEREO 0x2
48 #ifdef ROCKBOX
49 #undef DEBUGF
50 #define DEBUGF(...)
51 #endif /* ROCKBOX */
53 /* FFMAX/MIN/SWAP and av_clip were taken from libavutil/common.h */
54 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
55 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
56 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
58 static int32_t qmf_window[48] IBSS_ATTR;
59 static VLC spectral_coeff_tab[7];
60 static channel_unit channel_units[2] IBSS_ATTR_LARGE_IRAM;
62 /**
63 * Matrixing within quadrature mirror synthesis filter.
65 * @param p3 output buffer
66 * @param inlo lower part of spectrum
67 * @param inhi higher part of spectrum
68 * @param nIn size of spectrum buffer
71 #if defined(CPU_ARM)
72 extern void
73 atrac3_iqmf_matrixing(int32_t *p3,
74 int32_t *inlo,
75 int32_t *inhi,
76 unsigned int nIn);
77 #else
78 static inline void
79 atrac3_iqmf_matrixing(int32_t *p3,
80 int32_t *inlo,
81 int32_t *inhi,
82 unsigned int nIn)
84 uint32_t i;
85 for(i=0; i<nIn; i+=2){
86 p3[2*i+0] = inlo[i ] + inhi[i ];
87 p3[2*i+1] = inlo[i ] - inhi[i ];
88 p3[2*i+2] = inlo[i+1] + inhi[i+1];
89 p3[2*i+3] = inlo[i+1] - inhi[i+1];
92 #endif
94 /**
95 * Matrixing within quadrature mirror synthesis filter.
97 * @param out output buffer
98 * @param in input buffer
99 * @param win windowing coefficients
100 * @param nIn size of spectrum buffer
101 * Reference implementation:
103 * for (j = nIn; j != 0; j--) {
104 * s1 = fixmul32(in[0], win[0]);
105 * s2 = fixmul32(in[1], win[1]);
106 * for (i = 2; i < 48; i += 2) {
107 * s1 += fixmul31(in[i ], win[i ]);
108 * s2 += fixmul31(in[i+1], win[i+1]);
110 * out[0] = s2;
111 * out[1] = s1;
112 * in += 2;
113 * out += 2;
117 #if defined(CPU_ARM)
118 extern void
119 atrac3_iqmf_dewindowing(int32_t *out,
120 int32_t *in,
121 int32_t *win,
122 unsigned int nIn);
123 #else
124 static inline void
125 atrac3_iqmf_dewindowing(int32_t *out,
126 int32_t *in,
127 int32_t *win,
128 unsigned int nIn)
130 int32_t i, j, s1, s2;
132 for (j = nIn; j != 0; j--) {
133 i = 0;
134 /* 0.. 7 */
135 s1 = fixmul31(win[i], in[i]); i++;
136 s2 = fixmul31(win[i], in[i]); i++;
137 s1 += fixmul31(win[i], in[i]); i++;
138 s2 += fixmul31(win[i], in[i]); i++;
139 s1 += fixmul31(win[i], in[i]); i++;
140 s2 += fixmul31(win[i], in[i]); i++;
141 s1 += fixmul31(win[i], in[i]); i++;
142 s2 += fixmul31(win[i], in[i]); i++;
143 /* 8..15 */
144 s1 += fixmul31(win[i], in[i]); i++;
145 s2 += fixmul31(win[i], in[i]); i++;
146 s1 += fixmul31(win[i], in[i]); i++;
147 s2 += fixmul31(win[i], in[i]); i++;
148 s1 += fixmul31(win[i], in[i]); i++;
149 s2 += fixmul31(win[i], in[i]); i++;
150 s1 += fixmul31(win[i], in[i]); i++;
151 s2 += fixmul31(win[i], in[i]); i++;
152 /* 16..23 */
153 s1 += fixmul31(win[i], in[i]); i++;
154 s2 += fixmul31(win[i], in[i]); i++;
155 s1 += fixmul31(win[i], in[i]); i++;
156 s2 += fixmul31(win[i], in[i]); i++;
157 s1 += fixmul31(win[i], in[i]); i++;
158 s2 += fixmul31(win[i], in[i]); i++;
159 s1 += fixmul31(win[i], in[i]); i++;
160 s2 += fixmul31(win[i], in[i]); i++;
161 /* 24..31 */
162 s1 += fixmul31(win[i], in[i]); i++;
163 s2 += fixmul31(win[i], in[i]); i++;
164 s1 += fixmul31(win[i], in[i]); i++;
165 s2 += fixmul31(win[i], in[i]); i++;
166 s1 += fixmul31(win[i], in[i]); i++;
167 s2 += fixmul31(win[i], in[i]); i++;
168 s1 += fixmul31(win[i], in[i]); i++;
169 s2 += fixmul31(win[i], in[i]); i++;
170 /* 32..39 */
171 s1 += fixmul31(win[i], in[i]); i++;
172 s2 += fixmul31(win[i], in[i]); i++;
173 s1 += fixmul31(win[i], in[i]); i++;
174 s2 += fixmul31(win[i], in[i]); i++;
175 s1 += fixmul31(win[i], in[i]); i++;
176 s2 += fixmul31(win[i], in[i]); i++;
177 s1 += fixmul31(win[i], in[i]); i++;
178 s2 += fixmul31(win[i], in[i]); i++;
179 /* 40..47 */
180 s1 += fixmul31(win[i], in[i]); i++;
181 s2 += fixmul31(win[i], in[i]); i++;
182 s1 += fixmul31(win[i], in[i]); i++;
183 s2 += fixmul31(win[i], in[i]); i++;
184 s1 += fixmul31(win[i], in[i]); i++;
185 s2 += fixmul31(win[i], in[i]); i++;
186 s1 += fixmul31(win[i], in[i]); i++;
187 s2 += fixmul31(win[i], in[i]);
189 out[0] = s2;
190 out[1] = s1;
192 in += 2;
193 out += 2;
196 #endif
199 * IMDCT windowing.
201 * @param buffer sample buffer
202 * @param win window coefficients
205 static inline void
206 atrac3_imdct_windowing(int32_t *buffer,
207 const int32_t *win)
209 int32_t i;
210 /* win[0..127] = win[511..384], win[128..383] = 1 */
211 for(i = 0; i<128; i++) {
212 buffer[ i] = fixmul31(win[i], buffer[ i]);
213 buffer[511-i] = fixmul31(win[i], buffer[511-i]);
218 * Quadrature mirror synthesis filter.
220 * @param inlo lower part of spectrum
221 * @param inhi higher part of spectrum
222 * @param nIn size of spectrum buffer
223 * @param pOut out buffer
224 * @param delayBuf delayBuf buffer
225 * @param temp temp buffer
228 static void iqmf (int32_t *inlo, int32_t *inhi, unsigned int nIn, int32_t *pOut, int32_t *delayBuf, int32_t *temp)
230 /* Restore the delay buffer */
231 memcpy(temp, delayBuf, 46*sizeof(int32_t));
233 /* loop1: matrixing */
234 atrac3_iqmf_matrixing(temp + 46, inlo, inhi, nIn);
236 /* loop2: dewindowing */
237 atrac3_iqmf_dewindowing(pOut, temp, qmf_window, nIn);
239 /* Save the delay buffer */
240 memcpy(delayBuf, temp + (nIn << 1), 46*sizeof(int32_t));
244 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
245 * caused by the reverse spectra of the QMF.
247 * @param pInput float input
248 * @param pOutput float output
249 * @param odd_band 1 if the band is an odd band
252 static void IMLT(int32_t *pInput, int32_t *pOutput)
254 /* Apply the imdct. */
255 mdct_backward(512, pInput, pOutput);
257 /* Windowing. */
258 atrac3_imdct_windowing(pOutput, window_lookup);
263 * Atrac 3 indata descrambling, only used for data coming from the rm container
265 * @param in pointer to 8 bit array of indata
266 * @param bits amount of bits
267 * @param out pointer to 8 bit array of outdata
270 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
271 int i, off;
272 uint32_t c;
273 const uint32_t* buf;
274 uint32_t* obuf = (uint32_t*) out;
276 #if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM))
277 off = 0; //no check for memory alignment of inbuffer
278 #else
279 off = (intptr_t)inbuffer & 3;
280 #endif /* TEST */
281 buf = (const uint32_t*) (inbuffer - off);
283 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
284 bytes += 3 + off;
285 for (i = 0; i < bytes/4; i++)
286 obuf[i] = c ^ buf[i];
288 return off;
292 static void init_atrac3_transforms(void) {
293 int32_t s;
294 int i;
296 /* Generate the mdct window, for details see
297 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
299 /* mdct window had been generated and saved as a lookup table in atrac3data_fixed.h */
301 /* Generate the QMF window. */
302 for (i=0 ; i<24; i++) {
303 s = qmf_48tap_half_fix[i] << 1;
304 qmf_window[i] = s;
305 qmf_window[47 - i] = s;
310 * Mantissa decoding
312 * @param gb the GetBit context
313 * @param selector what table is the output values coded with
314 * @param codingFlag constant length coding or variable length coding
315 * @param mantissas mantissa output table
316 * @param numCodes amount of values to get
319 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
321 int numBits, cnt, code, huffSymb;
323 if (selector == 1)
324 numCodes /= 2;
326 if (codingFlag != 0) {
327 /* constant length coding (CLC) */
328 numBits = CLCLengthTab[selector];
330 if (selector > 1) {
331 for (cnt = 0; cnt < numCodes; cnt++) {
332 if (numBits)
333 code = get_sbits(gb, numBits);
334 else
335 code = 0;
336 mantissas[cnt] = code;
338 } else {
339 for (cnt = 0; cnt < numCodes; cnt++) {
340 if (numBits)
341 code = get_bits(gb, numBits); //numBits is always 4 in this case
342 else
343 code = 0;
344 mantissas[cnt*2] = seTab_0[code >> 2];
345 mantissas[cnt*2+1] = seTab_0[code & 3];
348 } else {
349 /* variable length coding (VLC) */
350 if (selector != 1) {
351 for (cnt = 0; cnt < numCodes; cnt++) {
352 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
353 huffSymb += 1;
354 code = huffSymb >> 1;
355 if (huffSymb & 1)
356 code = -code;
357 mantissas[cnt] = code;
359 } else {
360 for (cnt = 0; cnt < numCodes; cnt++) {
361 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
362 mantissas[cnt*2] = decTable1[huffSymb*2];
363 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
370 * Restore the quantized band spectrum coefficients
372 * @param gb the GetBit context
373 * @param pOut decoded band spectrum
374 * @return outSubbands subband counter, fix for broken specification/files
377 static int decodeSpectrum (GetBitContext *gb, int32_t *pOut)
379 int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
380 int subband_vlc_index[32], SF_idxs[32];
381 int mantissas[128];
382 int32_t SF;
384 numSubbands = get_bits(gb, 5); // number of coded subbands
385 codingMode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC
387 /* Get the VLC selector table for the subbands, 0 means not coded. */
388 for (cnt = 0; cnt <= numSubbands; cnt++)
389 subband_vlc_index[cnt] = get_bits(gb, 3);
391 /* Read the scale factor indexes from the stream. */
392 for (cnt = 0; cnt <= numSubbands; cnt++) {
393 if (subband_vlc_index[cnt] != 0)
394 SF_idxs[cnt] = get_bits(gb, 6);
397 for (cnt = 0; cnt <= numSubbands; cnt++) {
398 first = subbandTab[cnt];
399 last = subbandTab[cnt+1];
401 subbWidth = last - first;
403 if (subband_vlc_index[cnt] != 0) {
404 /* Decode spectral coefficients for this subband. */
405 /* TODO: This can be done faster is several blocks share the
406 * same VLC selector (subband_vlc_index) */
407 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
409 /* Decode the scale factor for this subband. */
410 SF = fixmul31(SFTable_fixed[SF_idxs[cnt]], iMaxQuant_fix[subband_vlc_index[cnt]]);
412 /* Inverse quantize the coefficients. */
413 if((first/256) &1) {
414 /* Odd band - Reverse coefficients */
415 for (pIn=mantissas ; last>first; last--, pIn++)
416 pOut[last] = fixmul16(*pIn, SF);
417 } else {
418 for (pIn=mantissas ; first<last; first++, pIn++)
419 pOut[first] = fixmul16(*pIn, SF);
422 } else {
423 /* This subband was not coded, so zero the entire subband. */
424 memset(pOut+first, 0, subbWidth*sizeof(int32_t));
428 /* Clear the subbands that were not coded. */
429 first = subbandTab[cnt];
430 memset(pOut+first, 0, (1024 - first) * sizeof(int32_t));
431 return numSubbands;
435 * Restore the quantized tonal components
437 * @param gb the GetBit context
438 * @param pComponent tone component
439 * @param numBands amount of coded bands
442 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
444 int i,j,k,cnt;
445 int components, coding_mode_selector, coding_mode, coded_values_per_component;
446 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
447 int band_flags[4], mantissa[8];
448 int32_t *pCoef;
449 int32_t scalefactor;
450 int component_count = 0;
452 components = get_bits(gb,5);
454 /* no tonal components */
455 if (components == 0)
456 return 0;
458 coding_mode_selector = get_bits(gb,2);
459 if (coding_mode_selector == 2)
460 return -1;
462 coding_mode = coding_mode_selector & 1;
464 for (i = 0; i < components; i++) {
465 for (cnt = 0; cnt <= numBands; cnt++)
466 band_flags[cnt] = get_bits1(gb);
468 coded_values_per_component = get_bits(gb,3);
470 quant_step_index = get_bits(gb,3);
471 if (quant_step_index <= 1)
472 return -1;
474 if (coding_mode_selector == 3)
475 coding_mode = get_bits1(gb);
477 for (j = 0; j < (numBands + 1) * 4; j++) {
478 if (band_flags[j >> 2] == 0)
479 continue;
481 coded_components = get_bits(gb,3);
483 for (k=0; k<coded_components; k++) {
484 sfIndx = get_bits(gb,6);
485 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
486 max_coded_values = 1024 - pComponent[component_count].pos;
487 coded_values = coded_values_per_component + 1;
488 coded_values = FFMIN(max_coded_values,coded_values);
490 scalefactor = fixmul31(SFTable_fixed[sfIndx], iMaxQuant_fix[quant_step_index]);
492 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
494 pComponent[component_count].numCoefs = coded_values;
496 /* inverse quant */
497 pCoef = pComponent[component_count].coef;
498 for (cnt = 0; cnt < coded_values; cnt++)
499 pCoef[cnt] = fixmul16(mantissa[cnt], scalefactor);
501 component_count++;
506 return component_count;
510 * Decode gain parameters for the coded bands
512 * @param gb the GetBit context
513 * @param pGb the gainblock for the current band
514 * @param numBands amount of coded bands
517 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
519 int i, cf, numData;
520 int *pLevel, *pLoc;
522 gain_info *pGain = pGb->gBlock;
524 for (i=0 ; i<=numBands; i++)
526 numData = get_bits(gb,3);
527 pGain[i].num_gain_data = numData;
528 pLevel = pGain[i].levcode;
529 pLoc = pGain[i].loccode;
531 for (cf = 0; cf < numData; cf++){
532 pLevel[cf]= get_bits(gb,4);
533 pLoc [cf]= get_bits(gb,5);
534 if(cf && pLoc[cf] <= pLoc[cf-1])
535 return -1;
539 /* Clear the unused blocks. */
540 for (; i<4 ; i++)
541 pGain[i].num_gain_data = 0;
543 return 0;
547 * Apply gain parameters and perform the MDCT overlapping part
549 * @param pIn input float buffer
550 * @param pPrev previous float buffer to perform overlap against
551 * @param pOut output float buffer
552 * @param pGain1 current band gain info
553 * @param pGain2 next band gain info
556 static void gainCompensateAndOverlap (int32_t *pIn, int32_t *pPrev, int32_t *pOut, gain_info *pGain1, gain_info *pGain2)
558 /* gain compensation function */
559 int32_t gain1, gain2, gain_inc;
560 int cnt, numdata, nsample, startLoc, endLoc;
562 if (pGain2->num_gain_data == 0)
563 gain1 = ONE_16;
564 else
565 gain1 = gain_tab1[pGain2->levcode[0]];
567 if (pGain1->num_gain_data == 0) {
568 for (cnt = 0; cnt < 256; cnt++)
569 pOut[cnt] = fixmul16(pIn[cnt], gain1) + pPrev[cnt];
570 } else {
571 numdata = pGain1->num_gain_data;
572 pGain1->loccode[numdata] = 32;
573 pGain1->levcode[numdata] = 4;
575 nsample = 0; // current sample = 0
577 for (cnt = 0; cnt < numdata; cnt++) {
578 startLoc = pGain1->loccode[cnt] * 8;
579 endLoc = startLoc + 8;
581 gain2 = gain_tab1[pGain1->levcode[cnt]];
582 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
584 /* interpolate */
585 for (; nsample < startLoc; nsample++)
586 pOut[nsample] = fixmul16((fixmul16(pIn[nsample], gain1) + pPrev[nsample]), gain2);
588 /* interpolation is done over eight samples */
589 for (; nsample < endLoc; nsample++) {
590 pOut[nsample] = fixmul16((fixmul16(pIn[nsample], gain1) + pPrev[nsample]),gain2);
591 gain2 = fixmul16(gain2, gain_inc);
595 for (; nsample < 256; nsample++)
596 pOut[nsample] = fixmul16(pIn[nsample], gain1) + pPrev[nsample];
599 /* Delay for the overlapping part. */
600 memcpy(pPrev, &pIn[256], 256*sizeof(int32_t));
604 * Combine the tonal band spectrum and regular band spectrum
605 * Return position of the last tonal coefficient
608 * @param pSpectrum output spectrum buffer
609 * @param numComponents amount of tonal components
610 * @param pComponent tonal components for this band
613 static int addTonalComponents (int32_t *pSpectrum, int numComponents, tonal_component *pComponent)
615 int cnt, i, lastPos = -1;
616 int32_t *pOut;
617 int32_t *pIn;
619 for (cnt = 0; cnt < numComponents; cnt++){
620 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
621 pIn = pComponent[cnt].coef;
622 pOut = &(pSpectrum[pComponent[cnt].pos]);
624 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
625 pOut[i] += pIn[i];
628 return lastPos;
632 * Linear equidistant interpolation between two points x and y. 7 interpolation
633 * points can be calculated. Result is scaled by <<16.
634 * Result for s=0 is x*ONE_16
635 * Result for s=8 is y*ONE_16
637 * @param x first input point
638 * @param y second input point
639 * @param s index of interpolation point (0..7)
643 #define INTERPOLATE(x, y, s) ((x*ONE_16) + fixmul16(((s*ONE_16)>>3), (((x) - (y))*ONE_16)))
645 #define INTERPOLATE(x, y, s) ((((x)<<3) + s*((y)-(x)))<<13)
647 static void reverseMatrixing(int32_t *su1, int32_t *su2, int *pPrevCode, int *pCurrCode)
649 int i, band, nsample, s1, s2;
650 int32_t c1, c2;
651 int32_t mc1_l, mc1_r, mc2_l, mc2_r;
653 for (i=0,band = 0; band < 4*256; band+=256,i++) {
654 s1 = pPrevCode[i];
655 s2 = pCurrCode[i];
656 nsample = 0;
658 if (s1 != s2) {
659 /* Selector value changed, interpolation needed. */
660 mc1_l = matrixCoeffs_fix[s1<<1];
661 mc1_r = matrixCoeffs_fix[(s1<<1)+1];
662 mc2_l = matrixCoeffs_fix[s2<<1];
663 mc2_r = matrixCoeffs_fix[(s2<<1)+1];
665 /* Interpolation is done over the first eight samples. */
666 for(; nsample < 8; nsample++) {
667 c1 = su1[band+nsample];
668 c2 = su2[band+nsample];
669 c2 = fixmul16(c1, INTERPOLATE(mc1_l, mc2_l, nsample)) + fixmul16(c2, INTERPOLATE(mc1_r, mc2_r, nsample));
670 su1[band+nsample] = c2;
671 su2[band+nsample] = (c1 << 1) - c2;
675 /* Apply the matrix without interpolation. */
676 switch (s2) {
677 case 0: /* M/S decoding */
678 for (; nsample < 256; nsample++) {
679 c1 = su1[band+nsample];
680 c2 = su2[band+nsample];
681 su1[band+nsample] = c2 << 1;
682 su2[band+nsample] = (c1 - c2) << 1;
684 break;
686 case 1:
687 for (; nsample < 256; nsample++) {
688 c1 = su1[band+nsample];
689 c2 = su2[band+nsample];
690 su1[band+nsample] = (c1 + c2) << 1;
691 su2[band+nsample] = -1*(c2 << 1);
693 break;
694 case 2:
695 case 3:
696 for (; nsample < 256; nsample++) {
697 c1 = su1[band+nsample];
698 c2 = su2[band+nsample];
699 su1[band+nsample] = c1 + c2;
700 su2[band+nsample] = c1 - c2;
702 break;
703 default:
704 //assert(0);
705 break;
710 static void getChannelWeights (int indx, int flag, int32_t ch[2]){
711 /* Read channel weights from table */
712 if (flag) {
713 /* Swap channel weights */
714 ch[1] = channelWeights0[indx&7];
715 ch[0] = channelWeights1[indx&7];
716 } else {
717 ch[0] = channelWeights0[indx&7];
718 ch[1] = channelWeights1[indx&7];
722 static void channelWeighting (int32_t *su1, int32_t *su2, int *p3)
724 int band, nsample;
725 /* w[x][y] y=0 is left y=1 is right */
726 int32_t w[2][2];
728 if (p3[1] != 7 || p3[3] != 7){
729 getChannelWeights(p3[1], p3[0], w[0]);
730 getChannelWeights(p3[3], p3[2], w[1]);
732 for(band = 1; band < 4; band++) {
733 /* scale the channels by the weights */
734 for(nsample = 0; nsample < 8; nsample++) {
735 su1[band*256+nsample] = fixmul16(su1[band*256+nsample], INTERPOLATE(w[0][0], w[0][1], nsample));
736 su2[band*256+nsample] = fixmul16(su2[band*256+nsample], INTERPOLATE(w[1][0], w[1][1], nsample));
739 for(; nsample < 256; nsample++) {
740 su1[band*256+nsample] = fixmul16(su1[band*256+nsample], w[1][0]);
741 su2[band*256+nsample] = fixmul16(su2[band*256+nsample], w[1][1]);
749 * Decode a Sound Unit
751 * @param gb the GetBit context
752 * @param pSnd the channel unit to be used
753 * @param pOut the decoded samples before IQMF in float representation
754 * @param channelNum channel number
755 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
759 static int decodeChannelSoundUnit (GetBitContext *gb, channel_unit *pSnd, int32_t *pOut, int channelNum, int codingMode)
761 int band, result=0, numSubbands, lastTonal, numBands;
762 if (codingMode == JOINT_STEREO && channelNum == 1) {
763 if (get_bits(gb,2) != 3) {
764 DEBUGF("JS mono Sound Unit id != 3.\n");
765 return -1;
767 } else {
768 if (get_bits(gb,6) != 0x28) {
769 DEBUGF("Sound Unit id != 0x28.\n");
770 return -1;
774 /* number of coded QMF bands */
775 pSnd->bandsCoded = get_bits(gb,2);
777 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
778 if (result) return result;
780 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
781 if (pSnd->numComponents == -1) return -1;
783 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
785 /* Merge the decoded spectrum and tonal components. */
786 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
789 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
790 numBands = (subbandTab[numSubbands] - 1) >> 8;
791 if (lastTonal >= 0)
792 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
794 /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample
795 * representation. Needed for higher accuracy in internal calculations as
796 * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH
797 * Todo: Check spectral requantisation for using and outputting samples with
798 * fract part. */
799 int32_t i;
800 for (i=0; i<1024; ++i) {
801 pSnd->spectrum[i] <<= 2;
804 /* Reconstruct time domain samples. */
805 for (band=0; band<4; band++) {
806 /* Perform the IMDCT step without overlapping. */
807 if (band <= numBands) {
808 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf);
809 } else
810 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(int32_t));
812 /* gain compensation and overlapping */
813 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
814 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
815 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
818 /* Swap the gain control buffers for the next frame. */
819 pSnd->gcBlkSwitch ^= 1;
821 return 0;
825 * Frame handling
827 * @param q Atrac3 private context
828 * @param databuf the input data
831 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf, int off)
833 int result, i;
834 int32_t *p1, *p2, *p3, *p4;
835 uint8_t *ptr1;
837 if (q->codingMode == JOINT_STEREO) {
839 /* channel coupling mode */
840 /* decode Sound Unit 1 */
841 init_get_bits(&q->gb,databuf,q->bits_per_frame);
843 result = decodeChannelSoundUnit(&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
844 if (result != 0)
845 return (result);
847 /* Framedata of the su2 in the joint-stereo mode is encoded in
848 * reverse byte order so we need to swap it first. */
849 if (databuf == q->decoded_bytes_buffer) {
850 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
851 ptr1 = q->decoded_bytes_buffer;
852 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
853 FFSWAP(uint8_t,*ptr1,*ptr2);
855 } else {
856 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
857 for (i = 0; i < q->bytes_per_frame; i++)
858 q->decoded_bytes_buffer[i] = *ptr2--;
861 /* Skip the sync codes (0xF8). */
862 ptr1 = q->decoded_bytes_buffer;
863 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
864 if (i >= q->bytes_per_frame)
865 return -1;
869 /* set the bitstream reader at the start of the second Sound Unit*/
870 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
872 /* Fill the Weighting coeffs delay buffer */
873 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
874 q->weighting_delay[4] = get_bits1(&q->gb);
875 q->weighting_delay[5] = get_bits(&q->gb,3);
877 for (i = 0; i < 4; i++) {
878 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
879 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
880 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
883 /* Decode Sound Unit 2. */
884 result = decodeChannelSoundUnit(&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
885 if (result != 0)
886 return (result);
888 /* Reconstruct the channel coefficients. */
889 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
891 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
893 } else {
894 /* normal stereo mode or mono */
895 /* Decode the channel sound units. */
896 for (i=0 ; i<q->channels ; i++) {
898 /* Set the bitstream reader at the start of a channel sound unit. */
899 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels)+off, (q->bits_per_frame)/q->channels);
901 result = decodeChannelSoundUnit(&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
902 if (result != 0)
903 return (result);
907 /* Apply the iQMF synthesis filter. */
908 p1= q->outSamples;
909 for (i=0 ; i<q->channels ; i++) {
910 p2= p1+256;
911 p3= p2+256;
912 p4= p3+256;
913 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
914 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
915 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
916 p1 +=1024;
919 return 0;
924 * Atrac frame decoding
926 * @param rmctx pointer to the AVCodecContext
929 int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
930 int *data_size, const uint8_t *buf, int buf_size) {
931 int result = 0, off = 0;
932 const uint8_t* databuf;
934 if (buf_size < rmctx->block_align)
935 return buf_size;
937 /* Check if we need to descramble and what buffer to pass on. */
938 if (q->scrambled_stream) {
939 off = decode_bytes(buf, q->decoded_bytes_buffer, rmctx->block_align);
940 databuf = q->decoded_bytes_buffer;
941 } else {
942 databuf = buf;
945 result = decodeFrame(q, databuf, off);
947 if (result != 0) {
948 DEBUGF("Frame decoding error!\n");
949 return -1;
952 if (q->channels == 1)
953 *data_size = 1024 * sizeof(int32_t);
954 else
955 *data_size = 2048 * sizeof(int32_t);
957 return rmctx->block_align;
962 * Atrac3 initialization
964 * @param rmctx pointer to the RMContext
967 int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx)
969 int i;
970 uint8_t *edata_ptr = rmctx->codec_extradata;
971 static VLC_TYPE atrac3_vlc_table[4096][2];
972 static int vlcs_initialized = 0;
974 /* Take data from the AVCodecContext (RM container). */
975 q->sample_rate = rmctx->sample_rate;
976 q->channels = rmctx->nb_channels;
977 q->bit_rate = rmctx->bit_rate;
978 q->bits_per_frame = rmctx->block_align * 8;
979 q->bytes_per_frame = rmctx->block_align;
981 /* Take care of the codec-specific extradata. */
982 if (rmctx->extradata_size == 14) {
983 /* Parse the extradata, WAV format */
984 DEBUGF("[0-1] %d\n",rm_get_uint16le(&edata_ptr[0])); //Unknown value always 1
985 q->samples_per_channel = rm_get_uint32le(&edata_ptr[2]);
986 q->codingMode = rm_get_uint16le(&edata_ptr[6]);
987 DEBUGF("[8-9] %d\n",rm_get_uint16le(&edata_ptr[8])); //Dupe of coding mode
988 q->frame_factor = rm_get_uint16le(&edata_ptr[10]); //Unknown always 1
989 DEBUGF("[12-13] %d\n",rm_get_uint16le(&edata_ptr[12])); //Unknown always 0
991 /* setup */
992 q->samples_per_frame = 1024 * q->channels;
993 q->atrac3version = 4;
994 q->delay = 0x88E;
995 if (q->codingMode)
996 q->codingMode = JOINT_STEREO;
997 else
998 q->codingMode = STEREO;
999 q->scrambled_stream = 0;
1001 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)) {
1002 } else {
1003 DEBUGF("Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor);
1004 return -1;
1007 } else if (rmctx->extradata_size == 10) {
1008 /* Parse the extradata, RM format. */
1009 q->atrac3version = rm_get_uint32be(&edata_ptr[0]);
1010 q->samples_per_frame = rm_get_uint16be(&edata_ptr[4]);
1011 q->delay = rm_get_uint16be(&edata_ptr[6]);
1012 q->codingMode = rm_get_uint16be(&edata_ptr[8]);
1014 q->samples_per_channel = q->samples_per_frame / q->channels;
1015 q->scrambled_stream = 1;
1017 } else {
1018 DEBUGF("Unknown extradata size %d.\n",rmctx->extradata_size);
1020 /* Check the extradata. */
1022 if (q->atrac3version != 4) {
1023 DEBUGF("Version %d != 4.\n",q->atrac3version);
1024 return -1;
1027 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
1028 DEBUGF("Unknown amount of samples per frame %d.\n",q->samples_per_frame);
1029 return -1;
1032 if (q->delay != 0x88E) {
1033 DEBUGF("Unknown amount of delay %x != 0x88E.\n",q->delay);
1034 return -1;
1037 if (q->codingMode == STEREO) {
1038 DEBUGF("Normal stereo detected.\n");
1039 } else if (q->codingMode == JOINT_STEREO) {
1040 DEBUGF("Joint stereo detected.\n");
1041 } else {
1042 DEBUGF("Unknown channel coding mode %x!\n",q->codingMode);
1043 return -1;
1046 if (rmctx->nb_channels <= 0 || rmctx->nb_channels > 2 /*|| ((rmctx->channels * 1024) != q->samples_per_frame)*/) {
1047 DEBUGF("Channel configuration error!\n");
1048 return -1;
1052 if(rmctx->block_align >= UINT16_MAX/2)
1053 return -1;
1056 /* Initialize the VLC tables. */
1057 if (!vlcs_initialized) {
1058 for (i=0 ; i<7 ; i++) {
1059 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
1060 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
1061 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
1062 huff_bits[i], 1, 1,
1063 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
1066 vlcs_initialized = 1;
1070 init_atrac3_transforms();
1072 /* init the joint-stereo decoding data */
1073 q->weighting_delay[0] = 0;
1074 q->weighting_delay[1] = 7;
1075 q->weighting_delay[2] = 0;
1076 q->weighting_delay[3] = 7;
1077 q->weighting_delay[4] = 0;
1078 q->weighting_delay[5] = 7;
1080 for (i=0; i<4; i++) {
1081 q->matrix_coeff_index_prev[i] = 3;
1082 q->matrix_coeff_index_now[i] = 3;
1083 q->matrix_coeff_index_next[i] = 3;
1086 q->pUnits = channel_units;
1088 return 0;