Fix playback of atrac3 joint stereo encoded files. Define the interpolation macro...
[kugel-rb.git] / apps / codecs / libatrac / atrac3.c
blob5c0bc824ff0f15a66edd8f22c4a2ca0aff9816a8
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"
44 #define JOINT_STEREO 0x12
45 #define STEREO 0x2
47 #ifdef ROCKBOX
48 #undef DEBUGF
49 #define DEBUGF(...)
50 #endif /* ROCKBOX */
52 /* FFMAX/MIN/SWAP and av_clip were taken from libavutil/common.h */
53 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
54 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
55 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
57 static VLC spectral_coeff_tab[7];
58 static int32_t qmf_window[48] IBSS_ATTR;
59 static int32_t atrac3_spectrum [2][1024] IBSS_ATTR __attribute__((aligned(16)));
60 static int32_t atrac3_IMDCT_buf[2][ 512] IBSS_ATTR __attribute__((aligned(16)));
61 static int32_t atrac3_prevFrame[2][1024] IBSS_ATTR;
62 static channel_unit channel_units[2] IBSS_ATTR_LARGE_IRAM;
65 /**
66 * Matrixing within quadrature mirror synthesis filter.
68 * @param p3 output buffer
69 * @param inlo lower part of spectrum
70 * @param inhi higher part of spectrum
71 * @param nIn size of spectrum buffer
74 #if defined(CPU_ARM)
75 extern void
76 atrac3_iqmf_matrixing(int32_t *p3,
77 int32_t *inlo,
78 int32_t *inhi,
79 unsigned int nIn);
80 #else
81 static inline void
82 atrac3_iqmf_matrixing(int32_t *p3,
83 int32_t *inlo,
84 int32_t *inhi,
85 unsigned int nIn)
87 uint32_t i;
88 for(i=0; i<nIn; i+=2){
89 p3[2*i+0] = inlo[i ] + inhi[i ];
90 p3[2*i+1] = inlo[i ] - inhi[i ];
91 p3[2*i+2] = inlo[i+1] + inhi[i+1];
92 p3[2*i+3] = inlo[i+1] - inhi[i+1];
95 #endif
98 /**
99 * Matrixing within quadrature mirror synthesis filter.
101 * @param out output buffer
102 * @param in input buffer
103 * @param win windowing coefficients
104 * @param nIn size of spectrum buffer
105 * Reference implementation:
107 * for (j = nIn; j != 0; j--) {
108 * s1 = fixmul32(in[0], win[0]);
109 * s2 = fixmul32(in[1], win[1]);
110 * for (i = 2; i < 48; i += 2) {
111 * s1 += fixmul31(in[i ], win[i ]);
112 * s2 += fixmul31(in[i+1], win[i+1]);
114 * out[0] = s2;
115 * out[1] = s1;
116 * in += 2;
117 * out += 2;
121 #if defined(CPU_ARM)
122 extern void
123 atrac3_iqmf_dewindowing(int32_t *out,
124 int32_t *in,
125 int32_t *win,
126 unsigned int nIn);
127 #else
128 static inline void
129 atrac3_iqmf_dewindowing(int32_t *out,
130 int32_t *in,
131 int32_t *win,
132 unsigned int nIn)
134 int32_t i, j, s1, s2;
136 for (j = nIn; j != 0; j--) {
137 i = 0;
138 /* 0.. 7 */
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 s1 += fixmul31(win[i], in[i]); i++;
144 s2 += fixmul31(win[i], in[i]); i++;
145 s1 += fixmul31(win[i], in[i]); i++;
146 s2 += fixmul31(win[i], in[i]); i++;
147 /* 8..15 */
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 s1 += fixmul31(win[i], in[i]); i++;
153 s2 += fixmul31(win[i], in[i]); i++;
154 s1 += fixmul31(win[i], in[i]); i++;
155 s2 += fixmul31(win[i], in[i]); i++;
156 /* 16..23 */
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 s1 += fixmul31(win[i], in[i]); i++;
162 s2 += fixmul31(win[i], in[i]); i++;
163 s1 += fixmul31(win[i], in[i]); i++;
164 s2 += fixmul31(win[i], in[i]); i++;
165 /* 24..31 */
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 s1 += fixmul31(win[i], in[i]); i++;
171 s2 += fixmul31(win[i], in[i]); i++;
172 s1 += fixmul31(win[i], in[i]); i++;
173 s2 += fixmul31(win[i], in[i]); i++;
174 /* 32..39 */
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 s1 += fixmul31(win[i], in[i]); i++;
180 s2 += fixmul31(win[i], in[i]); i++;
181 s1 += fixmul31(win[i], in[i]); i++;
182 s2 += fixmul31(win[i], in[i]); i++;
183 /* 40..47 */
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]); i++;
188 s1 += fixmul31(win[i], in[i]); i++;
189 s2 += fixmul31(win[i], in[i]); i++;
190 s1 += fixmul31(win[i], in[i]); i++;
191 s2 += fixmul31(win[i], in[i]);
193 out[0] = s2;
194 out[1] = s1;
196 in += 2;
197 out += 2;
200 #endif
204 * IMDCT windowing.
206 * @param buffer sample buffer
207 * @param win window coefficients
210 static inline void
211 atrac3_imdct_windowing(int32_t *buffer,
212 const int32_t *win)
214 int32_t i;
215 /* win[0..127] = win[511..384], win[128..383] = 1 */
216 for(i = 0; i<128; i++) {
217 buffer[ i] = fixmul31(win[i], buffer[ i]);
218 buffer[511-i] = fixmul31(win[i], buffer[511-i]);
224 * Quadrature mirror synthesis filter.
226 * @param inlo lower part of spectrum
227 * @param inhi higher part of spectrum
228 * @param nIn size of spectrum buffer
229 * @param pOut out buffer
230 * @param delayBuf delayBuf buffer
231 * @param temp temp buffer
234 static void iqmf (int32_t *inlo, int32_t *inhi, unsigned int nIn, int32_t *pOut, int32_t *delayBuf, int32_t *temp)
236 /* Restore the delay buffer */
237 memcpy(temp, delayBuf, 46*sizeof(int32_t));
239 /* loop1: matrixing */
240 atrac3_iqmf_matrixing(temp + 46, inlo, inhi, nIn);
242 /* loop2: dewindowing */
243 atrac3_iqmf_dewindowing(pOut, temp, qmf_window, nIn);
245 /* Save the delay buffer */
246 memcpy(delayBuf, temp + (nIn << 1), 46*sizeof(int32_t));
251 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
252 * caused by the reverse spectra of the QMF.
254 * @param pInput input
255 * @param pOutput output
256 * @param odd_band 1 if the band is an odd band
259 static void IMLT(int32_t *pInput, int32_t *pOutput)
261 /* Apply the imdct. */
262 ff_imdct_calc(9, pOutput, pInput);
264 /* Windowing. */
265 atrac3_imdct_windowing(pOutput, window_lookup);
270 * Atrac 3 indata descrambling, only used for data coming from the rm container
272 * @param in pointer to 8 bit array of indata
273 * @param bits amount of bits
274 * @param out pointer to 8 bit array of outdata
277 static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
278 int i, off;
279 uint32_t c;
280 const uint32_t* buf;
281 uint32_t* obuf = (uint32_t*) out;
283 #if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM))
284 off = 0; /* no check for memory alignment of inbuffer */
285 #else
286 off = (intptr_t)inbuffer & 3;
287 #endif /* TEST */
288 buf = (const uint32_t*) (inbuffer - off);
290 c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
291 bytes += 3 + off;
292 for (i = 0; i < bytes/4; i++)
293 obuf[i] = c ^ buf[i];
295 return off;
299 static void init_atrac3_transforms(void)
301 int32_t s;
302 int i;
304 /* Generate the mdct window, for details see
305 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
307 /* mdct window had been generated and saved as a lookup table in atrac3data_fixed.h */
309 /* Generate the QMF window. */
310 for (i=0 ; i<24; i++) {
311 s = qmf_48tap_half_fix[i] << 1;
312 qmf_window[i] = s;
313 qmf_window[47 - i] = s;
319 * Mantissa decoding
321 * @param gb the GetBit context
322 * @param selector what table is the output values coded with
323 * @param codingFlag constant length coding or variable length coding
324 * @param mantissas mantissa output table
325 * @param numCodes amount of values to get
328 static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
330 int numBits, cnt, code, huffSymb;
332 if (selector == 1)
333 numCodes /= 2;
335 if (codingFlag != 0) {
336 /* constant length coding (CLC) */
337 numBits = CLCLengthTab[selector];
339 if (selector > 1) {
340 for (cnt = 0; cnt < numCodes; cnt++) {
341 if (numBits)
342 code = get_sbits(gb, numBits);
343 else
344 code = 0;
345 mantissas[cnt] = code;
347 } else {
348 for (cnt = 0; cnt < numCodes; cnt++) {
349 if (numBits)
350 code = get_bits(gb, numBits); /* numBits is always 4 in this case */
351 else
352 code = 0;
353 mantissas[cnt*2] = seTab_0[code >> 2];
354 mantissas[cnt*2+1] = seTab_0[code & 3];
357 } else {
358 /* variable length coding (VLC) */
359 if (selector != 1) {
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 huffSymb += 1;
363 code = huffSymb >> 1;
364 if (huffSymb & 1)
365 code = -code;
366 mantissas[cnt] = code;
368 } else {
369 for (cnt = 0; cnt < numCodes; cnt++) {
370 huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3);
371 mantissas[cnt*2] = decTable1[huffSymb*2];
372 mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
380 * Requantize the spectrum.
382 * @param *mantissas pointer to mantissas for each spectral line
383 * @param pOut requantized band spectrum
384 * @param first first spectral line in subband
385 * @param last last spectral line in subband
386 * @param SF scalefactor for all spectral lines of this band
389 static void inverseQuantizeSpectrum(int *mantissas, int32_t *pOut,
390 int32_t first, int32_t last, int32_t SF)
392 int *pIn = mantissas;
394 /* Inverse quantize the coefficients. */
395 if((first/256) &1) {
396 /* Odd band - Reverse coefficients */
397 do {
398 pOut[last--] = fixmul16(*pIn++, SF);
399 pOut[last--] = fixmul16(*pIn++, SF);
400 pOut[last--] = fixmul16(*pIn++, SF);
401 pOut[last--] = fixmul16(*pIn++, SF);
402 pOut[last--] = fixmul16(*pIn++, SF);
403 pOut[last--] = fixmul16(*pIn++, SF);
404 pOut[last--] = fixmul16(*pIn++, SF);
405 pOut[last--] = fixmul16(*pIn++, SF);
406 } while (last>first);
407 } else {
408 /* Even band - Do not reverse coefficients */
409 do {
410 pOut[first++] = fixmul16(*pIn++, SF);
411 pOut[first++] = fixmul16(*pIn++, SF);
412 pOut[first++] = fixmul16(*pIn++, SF);
413 pOut[first++] = fixmul16(*pIn++, SF);
414 pOut[first++] = fixmul16(*pIn++, SF);
415 pOut[first++] = fixmul16(*pIn++, SF);
416 pOut[first++] = fixmul16(*pIn++, SF);
417 pOut[first++] = fixmul16(*pIn++, SF);
418 } while (first<last);
424 * Restore the quantized band spectrum coefficients
426 * @param gb the GetBit context
427 * @param pOut decoded band spectrum
428 * @return outSubbands subband counter, fix for broken specification/files
431 int decodeSpectrum (GetBitContext *gb, int32_t *pOut) ICODE_ATTR_LARGE_IRAM;
432 int decodeSpectrum (GetBitContext *gb, int32_t *pOut)
434 int numSubbands, codingMode, cnt, first, last, subbWidth;
435 int subband_vlc_index[32], SF_idxs[32];
436 int mantissas[128];
437 int32_t SF;
439 numSubbands = get_bits(gb, 5); /* number of coded subbands */
440 codingMode = get_bits1(gb); /* coding Mode: 0 - VLC/ 1-CLC */
442 /* Get the VLC selector table for the subbands, 0 means not coded. */
443 for (cnt = 0; cnt <= numSubbands; cnt++)
444 subband_vlc_index[cnt] = get_bits(gb, 3);
446 /* Read the scale factor indexes from the stream. */
447 for (cnt = 0; cnt <= numSubbands; cnt++) {
448 if (subband_vlc_index[cnt] != 0)
449 SF_idxs[cnt] = get_bits(gb, 6);
452 for (cnt = 0; cnt <= numSubbands; cnt++) {
453 first = subbandTab[cnt];
454 last = subbandTab[cnt+1];
456 subbWidth = last - first;
458 if (subband_vlc_index[cnt] != 0) {
459 /* Decode spectral coefficients for this subband. */
460 /* TODO: This can be done faster is several blocks share the
461 * same VLC selector (subband_vlc_index) */
462 readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
464 /* Decode the scale factor for this subband. */
465 SF = fixmul31(SFTable_fixed[SF_idxs[cnt]], iMaxQuant_fix[subband_vlc_index[cnt]]);
466 /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample
467 * representation. Needed for higher accuracy in internal calculations as
468 * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH
470 SF <<= 2;
472 /* Inverse quantize the coefficients. */
473 inverseQuantizeSpectrum(mantissas, pOut, first, last, SF);
475 } else {
476 /* This subband was not coded, so zero the entire subband. */
477 memset(pOut+first, 0, subbWidth*sizeof(int32_t));
481 /* Clear the subbands that were not coded. */
482 first = subbandTab[cnt];
483 memset(pOut+first, 0, (1024 - first) * sizeof(int32_t));
484 return numSubbands;
489 * Restore the quantized tonal components
491 * @param gb the GetBit context
492 * @param pComponent tone component
493 * @param numBands amount of coded bands
496 static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands)
498 int i,j,k,cnt;
499 int components, coding_mode_selector, coding_mode, coded_values_per_component;
500 int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components;
501 int band_flags[4], mantissa[8];
502 int32_t *pCoef;
503 int32_t scalefactor;
504 int component_count = 0;
506 components = get_bits(gb,5);
508 /* no tonal components */
509 if (components == 0)
510 return 0;
512 coding_mode_selector = get_bits(gb,2);
513 if (coding_mode_selector == 2)
514 return -1;
516 coding_mode = coding_mode_selector & 1;
518 for (i = 0; i < components; i++) {
519 for (cnt = 0; cnt <= numBands; cnt++)
520 band_flags[cnt] = get_bits1(gb);
522 coded_values_per_component = get_bits(gb,3);
524 quant_step_index = get_bits(gb,3);
525 if (quant_step_index <= 1)
526 return -1;
528 if (coding_mode_selector == 3)
529 coding_mode = get_bits1(gb);
531 for (j = 0; j < (numBands + 1) * 4; j++) {
532 if (band_flags[j >> 2] == 0)
533 continue;
535 coded_components = get_bits(gb,3);
537 for (k=0; k<coded_components; k++) {
538 sfIndx = get_bits(gb,6);
539 pComponent[component_count].pos = j * 64 + (get_bits(gb,6));
540 max_coded_values = 1024 - pComponent[component_count].pos;
541 coded_values = coded_values_per_component + 1;
542 coded_values = FFMIN(max_coded_values,coded_values);
544 scalefactor = fixmul31(SFTable_fixed[sfIndx], iMaxQuant_fix[quant_step_index]);
545 /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample
546 * representation. Needed for higher accuracy in internal calculations as
547 * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH
549 scalefactor <<= 2;
551 readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values);
553 pComponent[component_count].numCoefs = coded_values;
555 /* inverse quant */
556 pCoef = pComponent[component_count].coef;
557 for (cnt = 0; cnt < coded_values; cnt++)
558 pCoef[cnt] = fixmul16(mantissa[cnt], scalefactor);
560 component_count++;
565 return component_count;
570 * Decode gain parameters for the coded bands
572 * @param gb the GetBit context
573 * @param pGb the gainblock for the current band
574 * @param numBands amount of coded bands
577 static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands)
579 int i, cf, numData;
580 int *pLevel, *pLoc;
582 gain_info *pGain = pGb->gBlock;
584 for (i=0 ; i<=numBands; i++)
586 numData = get_bits(gb,3);
587 pGain[i].num_gain_data = numData;
588 pLevel = pGain[i].levcode;
589 pLoc = pGain[i].loccode;
591 for (cf = 0; cf < numData; cf++){
592 pLevel[cf]= get_bits(gb,4);
593 pLoc [cf]= get_bits(gb,5);
594 if(cf && pLoc[cf] <= pLoc[cf-1])
595 return -1;
599 /* Clear the unused blocks. */
600 for (; i<4 ; i++)
601 pGain[i].num_gain_data = 0;
603 return 0;
608 * Apply fix (constant) gain and overlap for sample[start...255].
610 * @param pIn input buffer
611 * @param pPrev previous buffer to perform overlap against
612 * @param pOut output buffer
613 * @param start index to start with (always a multiple of 8)
614 * @param gain gain to apply
617 static void applyFixGain (int32_t *pIn, int32_t *pPrev, int32_t *pOut,
618 int32_t start, int32_t gain)
620 int32_t i = start;
622 /* start is always a multiple of 8 and therefore allows us to unroll the
623 * loop to 8 calculation per loop
625 if (ONE_16 == gain) {
626 /* gain1 = 1.0 -> no multiplication needed, just adding */
627 /* Remark: This path is called >90%. */
628 do {
629 pOut[i] = pIn[i] + pPrev[i]; i++;
630 pOut[i] = pIn[i] + pPrev[i]; i++;
631 pOut[i] = pIn[i] + pPrev[i]; i++;
632 pOut[i] = pIn[i] + pPrev[i]; i++;
633 pOut[i] = pIn[i] + pPrev[i]; i++;
634 pOut[i] = pIn[i] + pPrev[i]; i++;
635 pOut[i] = pIn[i] + pPrev[i]; i++;
636 pOut[i] = pIn[i] + pPrev[i]; i++;
637 } while (i<256);
638 } else {
639 /* gain1 != 1.0 -> we need to do a multiplication */
640 /* Remark: This path is called seldom. */
641 do {
642 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
643 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
644 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
645 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
646 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
647 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
648 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
649 pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++;
650 } while (i<256);
656 * Apply variable gain and overlap. Returns sample index after applying gain,
657 * resulting sample index is always a multiple of 8.
659 * @param pIn input buffer
660 * @param pPrev previous buffer to perform overlap against
661 * @param pOut output buffer
662 * @param start index to start with (always a multiple of 8)
663 * @param end end index for first loop (always a multiple of 8)
664 * @param gain1 current bands gain to apply
665 * @param gain2 next bands gain to apply
666 * @param gain_inc stepwise adaption from gain1 to gain2
669 static int applyVariableGain (int32_t *pIn, int32_t *pPrev, int32_t *pOut,
670 int32_t start, int32_t end,
671 int32_t gain1, int32_t gain2, int32_t gain_inc)
673 int32_t i = start;
675 /* Apply fix gains until end index is reached */
676 do {
677 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
678 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
679 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
680 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
681 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
682 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
683 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
684 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
685 } while (i < end);
687 /* Interpolation is done over next eight samples */
688 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
689 gain2 = fixmul16(gain2, gain_inc);
690 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
691 gain2 = fixmul16(gain2, gain_inc);
692 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
693 gain2 = fixmul16(gain2, gain_inc);
694 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
695 gain2 = fixmul16(gain2, gain_inc);
696 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
697 gain2 = fixmul16(gain2, gain_inc);
698 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
699 gain2 = fixmul16(gain2, gain_inc);
700 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
701 gain2 = fixmul16(gain2, gain_inc);
702 pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++;
703 gain2 = fixmul16(gain2, gain_inc);
705 return i;
710 * Apply gain parameters and perform the MDCT overlapping part
712 * @param pIn input buffer
713 * @param pPrev previous buffer to perform overlap against
714 * @param pOut output buffer
715 * @param pGain1 current band gain info
716 * @param pGain2 next band gain info
719 static void gainCompensateAndOverlap (int32_t *pIn, int32_t *pPrev, int32_t *pOut,
720 gain_info *pGain1, gain_info *pGain2)
722 /* gain compensation function */
723 int32_t gain1, gain2, gain_inc;
724 int cnt, numdata, nsample, startLoc;
726 if (pGain2->num_gain_data == 0)
727 gain1 = ONE_16;
728 else
729 gain1 = (ONE_16<<4)>>(pGain2->levcode[0]);
731 if (pGain1->num_gain_data == 0) {
732 /* Remark: This path is called >90%. */
733 /* Apply gain for all samples from 0...255 */
734 applyFixGain(pIn, pPrev, pOut, 0, gain1);
735 } else {
736 /* Remark: This path is called seldom. */
737 numdata = pGain1->num_gain_data;
738 pGain1->loccode[numdata] = 32;
739 pGain1->levcode[numdata] = 4;
741 nsample = 0; /* starting loop with =0 */
743 for (cnt = 0; cnt < numdata; cnt++) {
744 startLoc = pGain1->loccode[cnt] * 8;
746 gain2 = (ONE_16<<4)>>(pGain1->levcode[cnt]);
747 gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
749 /* Apply variable gain (gain1 -> gain2) to samples */
750 nsample = applyVariableGain(pIn, pPrev, pOut, nsample, startLoc, gain1, gain2, gain_inc);
752 /* Apply gain for the residual samples from nsample...255 */
753 applyFixGain(pIn, pPrev, pOut, nsample, gain1);
756 /* Delay for the overlapping part. */
757 memcpy(pPrev, &pIn[256], 256*sizeof(int32_t));
762 * Combine the tonal band spectrum and regular band spectrum
763 * Return position of the last tonal coefficient
766 * @param pSpectrum output spectrum buffer
767 * @param numComponents amount of tonal components
768 * @param pComponent tonal components for this band
771 static int addTonalComponents (int32_t *pSpectrum, int numComponents, tonal_component *pComponent)
773 int cnt, i, lastPos = -1;
774 int32_t *pOut;
775 int32_t *pIn;
777 for (cnt = 0; cnt < numComponents; cnt++){
778 lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos);
779 pIn = pComponent[cnt].coef;
780 pOut = &(pSpectrum[pComponent[cnt].pos]);
782 for (i=0 ; i<pComponent[cnt].numCoefs ; i++)
783 pOut[i] += pIn[i];
786 return lastPos;
791 * Linear equidistant interpolation between two points x and y. 7 interpolation
792 * points can be calculated. Result is scaled by <<16.
793 * Result for s=0 is x*ONE_16
794 * Result for s=8 is y*ONE_16
796 * @param x first input point
797 * @param y second input point
798 * @param s index of interpolation point (0..7)
801 /* rockbox: Not used anymore. Faster version defined below.
802 #define INTERPOLATE_FP16(x, y, s) ((x) + fixmul16(((s*ONE_16)>>3), (((y) - (x)))))
804 #define INTERPOLATE_FP16(x, y, s) ((x) + ((s*((y)-(x)))>>3))
806 static void reverseMatrixing(int32_t *su1, int32_t *su2, int *pPrevCode, int *pCurrCode)
808 int i, band, nsample, s1, s2;
809 int32_t c1, c2;
810 int32_t mc1_l, mc1_r, mc2_l, mc2_r;
812 for (i=0,band = 0; band < 4*256; band+=256,i++) {
813 s1 = pPrevCode[i];
814 s2 = pCurrCode[i];
815 nsample = 0;
817 if (s1 != s2) {
818 /* Selector value changed, interpolation needed. */
819 mc1_l = matrixCoeffs_fix[s1<<1];
820 mc1_r = matrixCoeffs_fix[(s1<<1)+1];
821 mc2_l = matrixCoeffs_fix[s2<<1];
822 mc2_r = matrixCoeffs_fix[(s2<<1)+1];
824 /* Interpolation is done over the first eight samples. */
825 for(; nsample < 8; nsample++) {
826 c1 = su1[band+nsample];
827 c2 = su2[band+nsample];
828 c2 = fixmul16(c1, INTERPOLATE_FP16(mc1_l, mc2_l, nsample)) + fixmul16(c2, INTERPOLATE_FP16(mc1_r, mc2_r, nsample));
829 su1[band+nsample] = c2;
830 su2[band+nsample] = (c1 << 1) - c2;
834 /* Apply the matrix without interpolation. */
835 switch (s2) {
836 case 0: /* M/S decoding */
837 for (; nsample < 256; nsample++) {
838 c1 = su1[band+nsample];
839 c2 = su2[band+nsample];
840 su1[band+nsample] = c2 << 1;
841 su2[band+nsample] = (c1 - c2) << 1;
843 break;
845 case 1:
846 for (; nsample < 256; nsample++) {
847 c1 = su1[band+nsample];
848 c2 = su2[band+nsample];
849 su1[band+nsample] = (c1 + c2) << 1;
850 su2[band+nsample] = -1*(c2 << 1);
852 break;
853 case 2:
854 case 3:
855 for (; nsample < 256; nsample++) {
856 c1 = su1[band+nsample];
857 c2 = su2[band+nsample];
858 su1[band+nsample] = c1 + c2;
859 su2[band+nsample] = c1 - c2;
861 break;
862 default:
863 /* assert(0) */;
864 break;
869 static void getChannelWeights (int indx, int flag, int32_t ch[2]){
870 /* Read channel weights from table */
871 if (flag) {
872 /* Swap channel weights */
873 ch[1] = channelWeights0[indx&7];
874 ch[0] = channelWeights1[indx&7];
875 } else {
876 ch[0] = channelWeights0[indx&7];
877 ch[1] = channelWeights1[indx&7];
881 static void channelWeighting (int32_t *su1, int32_t *su2, int *p3)
883 int band, nsample;
884 /* w[x][y] y=0 is left y=1 is right */
885 int32_t w[2][2];
887 if (p3[1] != 7 || p3[3] != 7){
888 getChannelWeights(p3[1], p3[0], w[0]);
889 getChannelWeights(p3[3], p3[2], w[1]);
891 for(band = 1; band < 4; band++) {
892 /* scale the channels by the weights */
893 for(nsample = 0; nsample < 8; nsample++) {
894 su1[band*256+nsample] = fixmul16(su1[band*256+nsample], INTERPOLATE_FP16(w[0][0], w[0][1], nsample));
895 su2[band*256+nsample] = fixmul16(su2[band*256+nsample], INTERPOLATE_FP16(w[1][0], w[1][1], nsample));
898 for(; nsample < 256; nsample++) {
899 su1[band*256+nsample] = fixmul16(su1[band*256+nsample], w[1][0]);
900 su2[band*256+nsample] = fixmul16(su2[band*256+nsample], w[1][1]);
907 * Decode a Sound Unit
909 * @param gb the GetBit context
910 * @param pSnd the channel unit to be used
911 * @param pOut the decoded samples before IQMF
912 * @param channelNum channel number
913 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
916 static int decodeChannelSoundUnit (GetBitContext *gb, channel_unit *pSnd, int32_t *pOut, int channelNum, int codingMode)
918 int band, result=0, numSubbands, lastTonal, numBands;
919 if (codingMode == JOINT_STEREO && channelNum == 1) {
920 if (get_bits(gb,2) != 3) {
921 DEBUGF("JS mono Sound Unit id != 3.\n");
922 return -1;
924 } else {
925 if (get_bits(gb,6) != 0x28) {
926 DEBUGF("Sound Unit id != 0x28.\n");
927 return -1;
931 /* number of coded QMF bands */
932 pSnd->bandsCoded = get_bits(gb,2);
934 result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
935 if (result) return result;
937 pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded);
938 if (pSnd->numComponents == -1) return -1;
940 numSubbands = decodeSpectrum (gb, pSnd->spectrum);
942 /* Merge the decoded spectrum and tonal components. */
943 lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components);
946 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
947 numBands = (subbandTab[numSubbands] - 1) >> 8;
948 if (lastTonal >= 0)
949 numBands = FFMAX((lastTonal + 256) >> 8, numBands);
951 /* Reconstruct time domain samples. */
952 for (band=0; band<4; band++) {
953 /* Perform the IMDCT step without overlapping. */
954 if (band <= numBands) {
955 IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf);
956 } else {
957 memset(pSnd->IMDCT_buf, 0, 512 * sizeof(int32_t));
960 /* gain compensation and overlapping */
961 gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
962 &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
963 &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
966 /* Swap the gain control buffers for the next frame. */
967 pSnd->gcBlkSwitch ^= 1;
969 return 0;
973 * Frame handling
975 * @param q Atrac3 private context
976 * @param databuf the input data
979 static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf, int off)
981 int result, i;
982 int32_t *p1, *p2, *p3, *p4;
983 uint8_t *ptr1;
985 if (q->codingMode == JOINT_STEREO) {
987 /* channel coupling mode */
988 /* decode Sound Unit 1 */
989 init_get_bits(&q->gb,databuf,q->bits_per_frame);
991 result = decodeChannelSoundUnit(&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO);
992 if (result != 0)
993 return (result);
995 /* Framedata of the su2 in the joint-stereo mode is encoded in
996 * reverse byte order so we need to swap it first. */
997 if (databuf == q->decoded_bytes_buffer) {
998 uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1;
999 ptr1 = q->decoded_bytes_buffer;
1000 for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
1001 FFSWAP(uint8_t,*ptr1,*ptr2);
1003 } else {
1004 const uint8_t *ptr2 = databuf+q->bytes_per_frame-1;
1005 for (i = 0; i < q->bytes_per_frame; i++)
1006 q->decoded_bytes_buffer[i] = *ptr2--;
1009 /* Skip the sync codes (0xF8). */
1010 ptr1 = q->decoded_bytes_buffer;
1011 for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
1012 if (i >= q->bytes_per_frame)
1013 return -1;
1017 /* set the bitstream reader at the start of the second Sound Unit*/
1018 init_get_bits(&q->gb,ptr1,q->bits_per_frame);
1020 /* Fill the Weighting coeffs delay buffer */
1021 memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int));
1022 q->weighting_delay[4] = get_bits1(&q->gb);
1023 q->weighting_delay[5] = get_bits(&q->gb,3);
1025 for (i = 0; i < 4; i++) {
1026 q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i];
1027 q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i];
1028 q->matrix_coeff_index_next[i] = get_bits(&q->gb,2);
1031 /* Decode Sound Unit 2. */
1032 result = decodeChannelSoundUnit(&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
1033 if (result != 0)
1034 return (result);
1036 /* Reconstruct the channel coefficients. */
1037 reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now);
1039 channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay);
1041 } else {
1042 /* normal stereo mode or mono */
1043 /* Decode the channel sound units. */
1044 for (i=0 ; i<q->channels ; i++) {
1046 /* Set the bitstream reader at the start of a channel sound unit. */
1047 init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels)+off, (q->bits_per_frame)/q->channels);
1049 result = decodeChannelSoundUnit(&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
1050 if (result != 0)
1051 return (result);
1055 /* Apply the iQMF synthesis filter. */
1056 p1= q->outSamples;
1057 for (i=0 ; i<q->channels ; i++) {
1058 p2= p1+256;
1059 p3= p2+256;
1060 p4= p3+256;
1061 iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf);
1062 iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf);
1063 iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf);
1064 p1 +=1024;
1067 return 0;
1072 * Atrac frame decoding
1074 * @param rmctx pointer to the AVCodecContext
1077 int atrac3_decode_frame(unsigned long block_align, ATRAC3Context *q,
1078 int *data_size, const uint8_t *buf, int buf_size) {
1079 int result = 0, off = 0;
1080 const uint8_t* databuf;
1082 if ((unsigned)buf_size < block_align)
1083 return buf_size;
1085 /* Check if we need to descramble and what buffer to pass on. */
1086 if (q->scrambled_stream) {
1087 off = decode_bytes(buf, q->decoded_bytes_buffer, block_align);
1088 databuf = q->decoded_bytes_buffer;
1089 } else {
1090 databuf = buf;
1093 result = decodeFrame(q, databuf, off);
1095 if (result != 0) {
1096 DEBUGF("Frame decoding error!\n");
1097 return -1;
1100 if (q->channels == 1)
1101 *data_size = 1024 * sizeof(int32_t);
1102 else
1103 *data_size = 2048 * sizeof(int32_t);
1105 return block_align;
1110 * Atrac3 initialization
1112 * @param rmctx pointer to the RMContext
1114 int atrac3_decode_init(ATRAC3Context *q, struct mp3entry *id3)
1116 int i;
1117 uint8_t *edata_ptr = (uint8_t*)&id3->id3v2buf;
1118 static VLC_TYPE atrac3_vlc_table[4096][2];
1119 static int vlcs_initialized = 0;
1121 /* Take data from the RM container. */
1122 q->sample_rate = id3->frequency;
1123 q->channels = id3->channels;
1124 q->bit_rate = id3->bitrate * 1000;
1125 q->bits_per_frame = id3->bytesperframe * 8;
1126 q->bytes_per_frame = id3->bytesperframe;
1128 /* Take care of the codec-specific extradata. */
1130 if (id3->extradata_size == 14) {
1131 /* Parse the extradata, WAV format */
1132 DEBUGF("[0-1] %d\n",rm_get_uint16le(&edata_ptr[0])); /* Unknown value always 1 */
1133 q->samples_per_channel = rm_get_uint32le(&edata_ptr[2]);
1134 q->codingMode = rm_get_uint16le(&edata_ptr[6]);
1135 DEBUGF("[8-9] %d\n",rm_get_uint16le(&edata_ptr[8])); /* Dupe of coding mode */
1136 q->frame_factor = rm_get_uint16le(&edata_ptr[10]); /* Unknown always 1 */
1137 DEBUGF("[12-13] %d\n",rm_get_uint16le(&edata_ptr[12])); /* Unknown always 0 */
1139 /* setup */
1140 q->samples_per_frame = 1024 * q->channels;
1141 q->atrac3version = 4;
1142 q->delay = 0x88E;
1143 if (q->codingMode)
1144 q->codingMode = JOINT_STEREO;
1145 else
1146 q->codingMode = STEREO;
1147 q->scrambled_stream = 0;
1149 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)) {
1150 } else {
1151 DEBUGF("Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor);
1152 return -1;
1155 } else if (id3->extradata_size == 10) {
1156 /* Parse the extradata, RM format. */
1157 q->atrac3version = rm_get_uint32be(&edata_ptr[0]);
1158 q->samples_per_frame = rm_get_uint16be(&edata_ptr[4]);
1159 q->delay = rm_get_uint16be(&edata_ptr[6]);
1160 q->codingMode = rm_get_uint16be(&edata_ptr[8]);
1162 q->samples_per_channel = q->samples_per_frame / q->channels;
1163 q->scrambled_stream = 1;
1165 } else {
1166 DEBUGF("Unknown extradata size %d.\n",id3->extradata_size);
1168 /* Check the extradata. */
1170 if (q->atrac3version != 4) {
1171 DEBUGF("Version %d != 4.\n",q->atrac3version);
1172 return -1;
1175 if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) {
1176 DEBUGF("Unknown amount of samples per frame %d.\n",q->samples_per_frame);
1177 return -1;
1180 if (q->delay != 0x88E) {
1181 DEBUGF("Unknown amount of delay %x != 0x88E.\n",q->delay);
1182 return -1;
1185 if (q->codingMode == STEREO) {
1186 DEBUGF("Normal stereo detected.\n");
1187 } else if (q->codingMode == JOINT_STEREO) {
1188 DEBUGF("Joint stereo detected.\n");
1189 } else {
1190 DEBUGF("Unknown channel coding mode %x!\n",q->codingMode);
1191 return -1;
1194 if (id3->channels <= 0 || id3->channels > 2 ) {
1195 DEBUGF("Channel configuration error!\n");
1196 return -1;
1200 if(id3->bytesperframe >= UINT16_MAX/2)
1201 return -1;
1204 /* Initialize the VLC tables. */
1205 if (!vlcs_initialized) {
1206 for (i=0 ; i<7 ; i++) {
1207 spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]];
1208 spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i];
1209 init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i],
1210 huff_bits[i], 1, 1,
1211 huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
1214 vlcs_initialized = 1;
1218 init_atrac3_transforms();
1220 /* init the joint-stereo decoding data */
1221 q->weighting_delay[0] = 0;
1222 q->weighting_delay[1] = 7;
1223 q->weighting_delay[2] = 0;
1224 q->weighting_delay[3] = 7;
1225 q->weighting_delay[4] = 0;
1226 q->weighting_delay[5] = 7;
1228 for (i=0; i<4; i++) {
1229 q->matrix_coeff_index_prev[i] = 3;
1230 q->matrix_coeff_index_now[i] = 3;
1231 q->matrix_coeff_index_next[i] = 3;
1234 /* Link the iram'ed arrays to the decoder's data structure */
1235 q->pUnits = channel_units;
1236 q->pUnits[0].spectrum = &atrac3_spectrum [0][0];
1237 q->pUnits[1].spectrum = &atrac3_spectrum [1][0];
1238 q->pUnits[0].IMDCT_buf = &atrac3_IMDCT_buf[0][0];
1239 q->pUnits[1].IMDCT_buf = &atrac3_IMDCT_buf[1][0];
1240 q->pUnits[0].prevFrame = &atrac3_prevFrame[0][0];
1241 q->pUnits[1].prevFrame = &atrac3_prevFrame[1][0];
1243 return 0;