diff options
Diffstat (limited to 'lib/rbcodec/codecs/libatrac/atrac3.c')
-rw-r--r-- | lib/rbcodec/codecs/libatrac/atrac3.c | 1293 |
1 files changed, 1293 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libatrac/atrac3.c b/lib/rbcodec/codecs/libatrac/atrac3.c new file mode 100644 index 0000000000..bb52dd4cf0 --- /dev/null +++ b/lib/rbcodec/codecs/libatrac/atrac3.c | |||
@@ -0,0 +1,1293 @@ | |||
1 | /* | ||
2 | * Atrac 3 compatible decoder | ||
3 | * Copyright (c) 2006-2008 Maxim Poliakovski | ||
4 | * Copyright (c) 2006-2008 Benjamin Larsson | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file libavcodec/atrac3.c | ||
25 | * Atrac 3 compatible decoder. | ||
26 | * This decoder handles Sony's ATRAC3 data. | ||
27 | * | ||
28 | * Container formats used to store atrac 3 data: | ||
29 | * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3). | ||
30 | * | ||
31 | * To use this decoder, a calling application must supply the extradata | ||
32 | * bytes provided in the containers above. | ||
33 | */ | ||
34 | |||
35 | #include <math.h> | ||
36 | #include <stddef.h> | ||
37 | #include <stdio.h> | ||
38 | |||
39 | #include "atrac3.h" | ||
40 | #include "atrac3data.h" | ||
41 | #include "atrac3data_fixed.h" | ||
42 | #include "fixp_math.h" | ||
43 | |||
44 | #define JOINT_STEREO 0x12 | ||
45 | #define STEREO 0x2 | ||
46 | |||
47 | #ifdef ROCKBOX | ||
48 | #undef DEBUGF | ||
49 | #define DEBUGF(...) | ||
50 | #endif /* ROCKBOX */ | ||
51 | |||
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) | ||
56 | |||
57 | #if defined(CPU_ARM) && (ARM_ARCH >= 5) | ||
58 | #define QMFWIN_TYPE int16_t /* ARMv5e+ uses 32x16 multiplication */ | ||
59 | #else | ||
60 | #define QMFWIN_TYPE int32_t | ||
61 | #endif | ||
62 | |||
63 | static VLC spectral_coeff_tab[7] IBSS_ATTR_LARGE_IRAM; | ||
64 | static QMFWIN_TYPE qmf_window[48] IBSS_ATTR MEM_ALIGN_ATTR; | ||
65 | static int32_t atrac3_spectrum [2][1024] IBSS_ATTR MEM_ALIGN_ATTR; | ||
66 | static int32_t atrac3_IMDCT_buf[2][ 512] IBSS_ATTR MEM_ALIGN_ATTR; | ||
67 | static int32_t atrac3_prevFrame[2][1024] IBSS_ATTR MEM_ALIGN_ATTR; | ||
68 | static channel_unit channel_units[2] IBSS_ATTR_LARGE_IRAM; | ||
69 | static VLC_TYPE atrac3_vlc_table[4096][2] IBSS_ATTR_LARGE_IRAM; | ||
70 | static int vlcs_initialized = 0; | ||
71 | |||
72 | |||
73 | |||
74 | /** | ||
75 | * Matrixing within quadrature mirror synthesis filter. | ||
76 | * | ||
77 | * @param p3 output buffer | ||
78 | * @param inlo lower part of spectrum | ||
79 | * @param inhi higher part of spectrum | ||
80 | * @param nIn size of spectrum buffer | ||
81 | */ | ||
82 | |||
83 | #if defined(CPU_ARM) | ||
84 | extern void | ||
85 | atrac3_iqmf_matrixing(int32_t *p3, | ||
86 | int32_t *inlo, | ||
87 | int32_t *inhi, | ||
88 | unsigned int nIn); | ||
89 | #else | ||
90 | static inline void | ||
91 | atrac3_iqmf_matrixing(int32_t *p3, | ||
92 | int32_t *inlo, | ||
93 | int32_t *inhi, | ||
94 | unsigned int nIn) | ||
95 | { | ||
96 | uint32_t i; | ||
97 | for(i=0; i<nIn; i+=2){ | ||
98 | p3[2*i+0] = inlo[i ] + inhi[i ]; | ||
99 | p3[2*i+1] = inlo[i ] - inhi[i ]; | ||
100 | p3[2*i+2] = inlo[i+1] + inhi[i+1]; | ||
101 | p3[2*i+3] = inlo[i+1] - inhi[i+1]; | ||
102 | } | ||
103 | } | ||
104 | #endif | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Matrixing within quadrature mirror synthesis filter. | ||
109 | * | ||
110 | * @param out output buffer | ||
111 | * @param in input buffer | ||
112 | * @param win windowing coefficients | ||
113 | * @param nIn size of spectrum buffer | ||
114 | * Reference implementation: | ||
115 | * | ||
116 | * for (j = nIn; j != 0; j--) { | ||
117 | * s1 = fixmul32(in[0], win[0]); | ||
118 | * s2 = fixmul32(in[1], win[1]); | ||
119 | * for (i = 2; i < 48; i += 2) { | ||
120 | * s1 += fixmul31(in[i ], win[i ]); | ||
121 | * s2 += fixmul31(in[i+1], win[i+1]); | ||
122 | * } | ||
123 | * out[0] = s2; | ||
124 | * out[1] = s1; | ||
125 | * in += 2; | ||
126 | * out += 2; | ||
127 | * } | ||
128 | */ | ||
129 | |||
130 | #if defined(CPU_ARM) && (ARM_ARCH >= 5) | ||
131 | extern void | ||
132 | atrac3_iqmf_dewindowing_armv5e(int32_t *out, | ||
133 | int32_t *in, | ||
134 | int16_t *win, | ||
135 | unsigned int nIn); | ||
136 | static inline void | ||
137 | atrac3_iqmf_dewindowing(int32_t *out, | ||
138 | int32_t *in, | ||
139 | int16_t *win, | ||
140 | unsigned int nIn) | ||
141 | { | ||
142 | atrac3_iqmf_dewindowing_armv5e(out, in, win, nIn); | ||
143 | |||
144 | } | ||
145 | |||
146 | |||
147 | #elif defined(CPU_ARM) | ||
148 | extern void | ||
149 | atrac3_iqmf_dewindowing(int32_t *out, | ||
150 | int32_t *in, | ||
151 | int32_t *win, | ||
152 | unsigned int nIn); | ||
153 | |||
154 | #elif defined (CPU_COLDFIRE) | ||
155 | #define MULTIPLY_ADD_BLOCK \ | ||
156 | "movem.l (%[win]), %%d0-%%d7 \n\t" \ | ||
157 | "lea.l (8*4, %[win]), %[win] \n\t" \ | ||
158 | "mac.l %%d0, %%a5, (%[in])+, %%a5, %%acc0\n\t" \ | ||
159 | "mac.l %%d1, %%a5, (%[in])+, %%a5, %%acc1\n\t" \ | ||
160 | "mac.l %%d2, %%a5, (%[in])+, %%a5, %%acc0\n\t" \ | ||
161 | "mac.l %%d3, %%a5, (%[in])+, %%a5, %%acc1\n\t" \ | ||
162 | "mac.l %%d4, %%a5, (%[in])+, %%a5, %%acc0\n\t" \ | ||
163 | "mac.l %%d5, %%a5, (%[in])+, %%a5, %%acc1\n\t" \ | ||
164 | "mac.l %%d6, %%a5, (%[in])+, %%a5, %%acc0\n\t" \ | ||
165 | "mac.l %%d7, %%a5, (%[in])+, %%a5, %%acc1\n\t" \ | ||
166 | |||
167 | |||
168 | static inline void | ||
169 | atrac3_iqmf_dewindowing(int32_t *out, | ||
170 | int32_t *in, | ||
171 | int32_t *win, | ||
172 | unsigned int nIn) | ||
173 | { | ||
174 | int32_t j; | ||
175 | int32_t *_in, *_win; | ||
176 | for (j = nIn; j != 0; j--, in+=2, out+=2) { | ||
177 | _in = in; | ||
178 | _win = win; | ||
179 | |||
180 | asm volatile ( | ||
181 | "move.l (%[in])+, %%a5 \n\t" /* preload frist in value */ | ||
182 | MULTIPLY_ADD_BLOCK /* 0.. 7 */ | ||
183 | MULTIPLY_ADD_BLOCK /* 8..15 */ | ||
184 | MULTIPLY_ADD_BLOCK /* 16..23 */ | ||
185 | MULTIPLY_ADD_BLOCK /* 24..31 */ | ||
186 | MULTIPLY_ADD_BLOCK /* 32..39 */ | ||
187 | /* 40..47 */ | ||
188 | "movem.l (%[win]), %%d0-%%d7 \n\t" | ||
189 | "mac.l %%d0, %%a5, (%[in])+, %%a5, %%acc0 \n\t" | ||
190 | "mac.l %%d1, %%a5, (%[in])+, %%a5, %%acc1 \n\t" | ||
191 | "mac.l %%d2, %%a5, (%[in])+, %%a5, %%acc0 \n\t" | ||
192 | "mac.l %%d3, %%a5, (%[in])+, %%a5, %%acc1 \n\t" | ||
193 | "mac.l %%d4, %%a5, (%[in])+, %%a5, %%acc0 \n\t" | ||
194 | "mac.l %%d5, %%a5, (%[in])+, %%a5, %%acc1 \n\t" | ||
195 | "mac.l %%d6, %%a5, (%[in])+, %%a5, %%acc0 \n\t" | ||
196 | "mac.l %%d7, %%a5, %%acc1 \n\t" | ||
197 | "movclr.l %%acc0, %%d1 \n\t" /* s1 */ | ||
198 | "movclr.l %%acc1, %%d0 \n\t" /* s2 */ | ||
199 | "movem.l %%d0-%%d1, (%[out]) \n\t" | ||
200 | : [in] "+a" (_in), [win] "+a" (_win) | ||
201 | : [out] "a" (out) | ||
202 | : "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a5", "memory"); | ||
203 | } | ||
204 | } | ||
205 | #else | ||
206 | #define MULTIPLY_ADD_BLOCK(y1, y2, x, c, k) \ | ||
207 | y1 += fixmul31(c[k], x[k]); k++; \ | ||
208 | y2 += fixmul31(c[k], x[k]); k++; \ | ||
209 | y1 += fixmul31(c[k], x[k]); k++; \ | ||
210 | y2 += fixmul31(c[k], x[k]); k++; \ | ||
211 | y1 += fixmul31(c[k], x[k]); k++; \ | ||
212 | y2 += fixmul31(c[k], x[k]); k++; \ | ||
213 | y1 += fixmul31(c[k], x[k]); k++; \ | ||
214 | y2 += fixmul31(c[k], x[k]); k++; | ||
215 | |||
216 | static inline void | ||
217 | atrac3_iqmf_dewindowing(int32_t *out, | ||
218 | int32_t *in, | ||
219 | int32_t *win, | ||
220 | unsigned int nIn) | ||
221 | { | ||
222 | int32_t i, j, s1, s2; | ||
223 | |||
224 | for (j = nIn; j != 0; j--, in+=2, out+=2) { | ||
225 | s1 = s2 = i = 0; | ||
226 | |||
227 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 0.. 7 */ | ||
228 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 8..15 */ | ||
229 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 16..23 */ | ||
230 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 24..31 */ | ||
231 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 32..39 */ | ||
232 | MULTIPLY_ADD_BLOCK(s1, s2, in, win, i); /* 40..47 */ | ||
233 | |||
234 | out[0] = s2; | ||
235 | out[1] = s1; | ||
236 | |||
237 | } | ||
238 | |||
239 | } | ||
240 | #endif | ||
241 | |||
242 | |||
243 | /** | ||
244 | * IMDCT windowing. | ||
245 | * | ||
246 | * @param buffer sample buffer | ||
247 | * @param win window coefficients | ||
248 | */ | ||
249 | |||
250 | static inline void | ||
251 | atrac3_imdct_windowing(int32_t *buffer, | ||
252 | const int32_t *win) | ||
253 | { | ||
254 | int32_t i; | ||
255 | /* win[0..127] = win[511..384], win[128..383] = 1 */ | ||
256 | for(i = 0; i<128; i++) { | ||
257 | buffer[ i] = fixmul31(win[i], buffer[ i]); | ||
258 | buffer[511-i] = fixmul31(win[i], buffer[511-i]); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | |||
263 | /** | ||
264 | * Quadrature mirror synthesis filter. | ||
265 | * | ||
266 | * @param inlo lower part of spectrum | ||
267 | * @param inhi higher part of spectrum | ||
268 | * @param nIn size of spectrum buffer | ||
269 | * @param pOut out buffer | ||
270 | * @param delayBuf delayBuf buffer | ||
271 | * @param temp temp buffer | ||
272 | */ | ||
273 | |||
274 | static void iqmf (int32_t *inlo, int32_t *inhi, unsigned int nIn, int32_t *pOut, int32_t *delayBuf, int32_t *temp) | ||
275 | { | ||
276 | |||
277 | /* Restore the delay buffer */ | ||
278 | memcpy(temp, delayBuf, 46*sizeof(int32_t)); | ||
279 | |||
280 | /* loop1: matrixing */ | ||
281 | atrac3_iqmf_matrixing(temp + 46, inlo, inhi, nIn); | ||
282 | |||
283 | /* loop2: dewindowing */ | ||
284 | atrac3_iqmf_dewindowing(pOut, temp, qmf_window, nIn); | ||
285 | |||
286 | /* Save the delay buffer */ | ||
287 | memcpy(delayBuf, temp + (nIn << 1), 46*sizeof(int32_t)); | ||
288 | } | ||
289 | |||
290 | |||
291 | /** | ||
292 | * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands | ||
293 | * caused by the reverse spectra of the QMF. | ||
294 | * | ||
295 | * @param pInput input | ||
296 | * @param pOutput output | ||
297 | * @param odd_band 1 if the band is an odd band | ||
298 | */ | ||
299 | |||
300 | static void IMLT(int32_t *pInput, int32_t *pOutput) | ||
301 | { | ||
302 | /* Apply the imdct. */ | ||
303 | ff_imdct_calc(9, pOutput, pInput); | ||
304 | |||
305 | /* Windowing. */ | ||
306 | atrac3_imdct_windowing(pOutput, window_lookup); | ||
307 | |||
308 | } | ||
309 | |||
310 | |||
311 | /** | ||
312 | * Atrac 3 indata descrambling, only used for data coming from the rm container | ||
313 | * | ||
314 | * @param in pointer to 8 bit array of indata | ||
315 | * @param bits amount of bits | ||
316 | * @param out pointer to 8 bit array of outdata | ||
317 | */ | ||
318 | |||
319 | static int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ | ||
320 | int i, off; | ||
321 | uint32_t c; | ||
322 | const uint32_t* buf; | ||
323 | uint32_t* obuf = (uint32_t*) out; | ||
324 | |||
325 | #if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM)) | ||
326 | off = 0; /* no check for memory alignment of inbuffer */ | ||
327 | #else | ||
328 | off = (intptr_t)inbuffer & 3; | ||
329 | #endif /* TEST */ | ||
330 | buf = (const uint32_t*) (inbuffer - off); | ||
331 | |||
332 | c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8)))); | ||
333 | bytes += 3 + off; | ||
334 | for (i = 0; i < bytes/4; i++) | ||
335 | obuf[i] = c ^ buf[i]; | ||
336 | |||
337 | return off; | ||
338 | } | ||
339 | |||
340 | |||
341 | static void init_atrac3_transforms(void) | ||
342 | { | ||
343 | int32_t s; | ||
344 | int i; | ||
345 | |||
346 | /* Generate the mdct window, for details see | ||
347 | * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */ | ||
348 | |||
349 | /* mdct window had been generated and saved as a lookup table in atrac3data_fixed.h */ | ||
350 | |||
351 | /* Generate the QMF window. */ | ||
352 | for (i=0 ; i<24; i++) { | ||
353 | s = qmf_48tap_half_fix[i] << 1; | ||
354 | #if defined(CPU_ARM) && (ARM_ARCH >= 5) | ||
355 | qmf_window[i] = qmf_window[47-i] = (int16_t)((s+(1<<15))>>16); | ||
356 | #else | ||
357 | qmf_window[i] = qmf_window[47-i] = s; | ||
358 | #endif | ||
359 | } | ||
360 | |||
361 | } | ||
362 | |||
363 | |||
364 | /** | ||
365 | * Mantissa decoding | ||
366 | * | ||
367 | * @param gb the GetBit context | ||
368 | * @param selector what table is the output values coded with | ||
369 | * @param codingFlag constant length coding or variable length coding | ||
370 | * @param mantissas mantissa output table | ||
371 | * @param numCodes amount of values to get | ||
372 | */ | ||
373 | |||
374 | static void readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes) | ||
375 | { | ||
376 | int numBits, cnt, code, huffSymb; | ||
377 | |||
378 | if (selector == 1) | ||
379 | numCodes /= 2; | ||
380 | |||
381 | if (codingFlag != 0) { | ||
382 | /* constant length coding (CLC) */ | ||
383 | numBits = CLCLengthTab[selector]; | ||
384 | |||
385 | if (selector > 1) { | ||
386 | for (cnt = 0; cnt < numCodes; cnt++) { | ||
387 | if (numBits) | ||
388 | code = get_sbits(gb, numBits); | ||
389 | else | ||
390 | code = 0; | ||
391 | mantissas[cnt] = code; | ||
392 | } | ||
393 | } else { | ||
394 | for (cnt = 0; cnt < numCodes; cnt++) { | ||
395 | if (numBits) | ||
396 | code = get_bits(gb, numBits); /* numBits is always 4 in this case */ | ||
397 | else | ||
398 | code = 0; | ||
399 | mantissas[cnt*2] = seTab_0[code >> 2]; | ||
400 | mantissas[cnt*2+1] = seTab_0[code & 3]; | ||
401 | } | ||
402 | } | ||
403 | } else { | ||
404 | /* variable length coding (VLC) */ | ||
405 | if (selector != 1) { | ||
406 | for (cnt = 0; cnt < numCodes; cnt++) { | ||
407 | huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3); | ||
408 | huffSymb += 1; | ||
409 | code = huffSymb >> 1; | ||
410 | if (huffSymb & 1) | ||
411 | code = -code; | ||
412 | mantissas[cnt] = code; | ||
413 | } | ||
414 | } else { | ||
415 | for (cnt = 0; cnt < numCodes; cnt++) { | ||
416 | huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table, spectral_coeff_tab[selector-1].bits, 3); | ||
417 | mantissas[cnt*2] = decTable1[huffSymb*2]; | ||
418 | mantissas[cnt*2+1] = decTable1[huffSymb*2+1]; | ||
419 | } | ||
420 | } | ||
421 | } | ||
422 | } | ||
423 | |||
424 | |||
425 | /** | ||
426 | * Requantize the spectrum. | ||
427 | * | ||
428 | * @param *mantissas pointer to mantissas for each spectral line | ||
429 | * @param pOut requantized band spectrum | ||
430 | * @param first first spectral line in subband | ||
431 | * @param last last spectral line in subband | ||
432 | * @param SF scalefactor for all spectral lines of this band | ||
433 | */ | ||
434 | |||
435 | static void inverseQuantizeSpectrum(int *mantissas, int32_t *pOut, | ||
436 | int32_t first, int32_t last, int32_t SF) | ||
437 | { | ||
438 | int *pIn = mantissas; | ||
439 | |||
440 | /* Inverse quantize the coefficients. */ | ||
441 | if((first/256) &1) { | ||
442 | /* Odd band - Reverse coefficients */ | ||
443 | do { | ||
444 | pOut[last--] = fixmul16(*pIn++, SF); | ||
445 | pOut[last--] = fixmul16(*pIn++, SF); | ||
446 | pOut[last--] = fixmul16(*pIn++, SF); | ||
447 | pOut[last--] = fixmul16(*pIn++, SF); | ||
448 | pOut[last--] = fixmul16(*pIn++, SF); | ||
449 | pOut[last--] = fixmul16(*pIn++, SF); | ||
450 | pOut[last--] = fixmul16(*pIn++, SF); | ||
451 | pOut[last--] = fixmul16(*pIn++, SF); | ||
452 | } while (last>first); | ||
453 | } else { | ||
454 | /* Even band - Do not reverse coefficients */ | ||
455 | do { | ||
456 | pOut[first++] = fixmul16(*pIn++, SF); | ||
457 | pOut[first++] = fixmul16(*pIn++, SF); | ||
458 | pOut[first++] = fixmul16(*pIn++, SF); | ||
459 | pOut[first++] = fixmul16(*pIn++, SF); | ||
460 | pOut[first++] = fixmul16(*pIn++, SF); | ||
461 | pOut[first++] = fixmul16(*pIn++, SF); | ||
462 | pOut[first++] = fixmul16(*pIn++, SF); | ||
463 | pOut[first++] = fixmul16(*pIn++, SF); | ||
464 | } while (first<last); | ||
465 | } | ||
466 | } | ||
467 | |||
468 | |||
469 | /** | ||
470 | * Restore the quantized band spectrum coefficients | ||
471 | * | ||
472 | * @param gb the GetBit context | ||
473 | * @param pOut decoded band spectrum | ||
474 | * @return outSubbands subband counter, fix for broken specification/files | ||
475 | */ | ||
476 | |||
477 | static int decodeSpectrum (GetBitContext *gb, int32_t *pOut) ICODE_ATTR_LARGE_IRAM; | ||
478 | static int decodeSpectrum (GetBitContext *gb, int32_t *pOut) | ||
479 | { | ||
480 | int numSubbands, codingMode, cnt, first, last, subbWidth; | ||
481 | int subband_vlc_index[32], SF_idxs[32]; | ||
482 | int mantissas[128]; | ||
483 | int32_t SF; | ||
484 | |||
485 | numSubbands = get_bits(gb, 5); /* number of coded subbands */ | ||
486 | codingMode = get_bits1(gb); /* coding Mode: 0 - VLC/ 1-CLC */ | ||
487 | |||
488 | /* Get the VLC selector table for the subbands, 0 means not coded. */ | ||
489 | for (cnt = 0; cnt <= numSubbands; cnt++) | ||
490 | subband_vlc_index[cnt] = get_bits(gb, 3); | ||
491 | |||
492 | /* Read the scale factor indexes from the stream. */ | ||
493 | for (cnt = 0; cnt <= numSubbands; cnt++) { | ||
494 | if (subband_vlc_index[cnt] != 0) | ||
495 | SF_idxs[cnt] = get_bits(gb, 6); | ||
496 | } | ||
497 | |||
498 | for (cnt = 0; cnt <= numSubbands; cnt++) { | ||
499 | first = subbandTab[cnt]; | ||
500 | last = subbandTab[cnt+1]; | ||
501 | |||
502 | subbWidth = last - first; | ||
503 | |||
504 | if (subband_vlc_index[cnt] != 0) { | ||
505 | /* Decode spectral coefficients for this subband. */ | ||
506 | /* TODO: This can be done faster is several blocks share the | ||
507 | * same VLC selector (subband_vlc_index) */ | ||
508 | readQuantSpectralCoeffs (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth); | ||
509 | |||
510 | /* Decode the scale factor for this subband. */ | ||
511 | SF = fixmul31(SFTable_fixed[SF_idxs[cnt]], iMaxQuant_fix[subband_vlc_index[cnt]]); | ||
512 | /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample | ||
513 | * representation. Needed for higher accuracy in internal calculations as | ||
514 | * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH | ||
515 | */ | ||
516 | SF <<= 2; | ||
517 | |||
518 | /* Inverse quantize the coefficients. */ | ||
519 | inverseQuantizeSpectrum(mantissas, pOut, first, last, SF); | ||
520 | |||
521 | } else { | ||
522 | /* This subband was not coded, so zero the entire subband. */ | ||
523 | memset(pOut+first, 0, subbWidth*sizeof(int32_t)); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | /* Clear the subbands that were not coded. */ | ||
528 | first = subbandTab[cnt]; | ||
529 | memset(pOut+first, 0, (1024 - first) * sizeof(int32_t)); | ||
530 | return numSubbands; | ||
531 | } | ||
532 | |||
533 | |||
534 | /** | ||
535 | * Restore the quantized tonal components | ||
536 | * | ||
537 | * @param gb the GetBit context | ||
538 | * @param pComponent tone component | ||
539 | * @param numBands amount of coded bands | ||
540 | */ | ||
541 | |||
542 | static int decodeTonalComponents (GetBitContext *gb, tonal_component *pComponent, int numBands) | ||
543 | { | ||
544 | int i,j,k,cnt; | ||
545 | int components, coding_mode_selector, coding_mode, coded_values_per_component; | ||
546 | int sfIndx, coded_values, max_coded_values, quant_step_index, coded_components; | ||
547 | int band_flags[4], mantissa[8]; | ||
548 | int32_t *pCoef; | ||
549 | int32_t scalefactor; | ||
550 | int component_count = 0; | ||
551 | |||
552 | components = get_bits(gb,5); | ||
553 | |||
554 | /* no tonal components */ | ||
555 | if (components == 0) | ||
556 | return 0; | ||
557 | |||
558 | coding_mode_selector = get_bits(gb,2); | ||
559 | if (coding_mode_selector == 2) | ||
560 | return -1; | ||
561 | |||
562 | coding_mode = coding_mode_selector & 1; | ||
563 | |||
564 | for (i = 0; i < components; i++) { | ||
565 | for (cnt = 0; cnt <= numBands; cnt++) | ||
566 | band_flags[cnt] = get_bits1(gb); | ||
567 | |||
568 | coded_values_per_component = get_bits(gb,3); | ||
569 | |||
570 | quant_step_index = get_bits(gb,3); | ||
571 | if (quant_step_index <= 1) | ||
572 | return -1; | ||
573 | |||
574 | if (coding_mode_selector == 3) | ||
575 | coding_mode = get_bits1(gb); | ||
576 | |||
577 | for (j = 0; j < (numBands + 1) * 4; j++) { | ||
578 | if (band_flags[j >> 2] == 0) | ||
579 | continue; | ||
580 | |||
581 | coded_components = get_bits(gb,3); | ||
582 | |||
583 | for (k=0; k<coded_components; k++) { | ||
584 | sfIndx = get_bits(gb,6); | ||
585 | pComponent[component_count].pos = j * 64 + (get_bits(gb,6)); | ||
586 | max_coded_values = 1024 - pComponent[component_count].pos; | ||
587 | coded_values = coded_values_per_component + 1; | ||
588 | coded_values = FFMIN(max_coded_values,coded_values); | ||
589 | |||
590 | scalefactor = fixmul31(SFTable_fixed[sfIndx], iMaxQuant_fix[quant_step_index]); | ||
591 | /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample | ||
592 | * representation. Needed for higher accuracy in internal calculations as | ||
593 | * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH | ||
594 | */ | ||
595 | scalefactor <<= 2; | ||
596 | |||
597 | readQuantSpectralCoeffs(gb, quant_step_index, coding_mode, mantissa, coded_values); | ||
598 | |||
599 | pComponent[component_count].numCoefs = coded_values; | ||
600 | |||
601 | /* inverse quant */ | ||
602 | pCoef = pComponent[component_count].coef; | ||
603 | for (cnt = 0; cnt < coded_values; cnt++) | ||
604 | pCoef[cnt] = fixmul16(mantissa[cnt], scalefactor); | ||
605 | |||
606 | component_count++; | ||
607 | } | ||
608 | } | ||
609 | } | ||
610 | |||
611 | return component_count; | ||
612 | } | ||
613 | |||
614 | |||
615 | /** | ||
616 | * Decode gain parameters for the coded bands | ||
617 | * | ||
618 | * @param gb the GetBit context | ||
619 | * @param pGb the gainblock for the current band | ||
620 | * @param numBands amount of coded bands | ||
621 | */ | ||
622 | |||
623 | static int decodeGainControl (GetBitContext *gb, gain_block *pGb, int numBands) | ||
624 | { | ||
625 | int i, cf, numData; | ||
626 | int *pLevel, *pLoc; | ||
627 | |||
628 | gain_info *pGain = pGb->gBlock; | ||
629 | |||
630 | for (i=0 ; i<=numBands; i++) | ||
631 | { | ||
632 | numData = get_bits(gb,3); | ||
633 | pGain[i].num_gain_data = numData; | ||
634 | pLevel = pGain[i].levcode; | ||
635 | pLoc = pGain[i].loccode; | ||
636 | |||
637 | for (cf = 0; cf < numData; cf++){ | ||
638 | pLevel[cf]= get_bits(gb,4); | ||
639 | pLoc [cf]= get_bits(gb,5); | ||
640 | if(cf && pLoc[cf] <= pLoc[cf-1]) | ||
641 | return -1; | ||
642 | } | ||
643 | } | ||
644 | |||
645 | /* Clear the unused blocks. */ | ||
646 | for (; i<4 ; i++) | ||
647 | pGain[i].num_gain_data = 0; | ||
648 | |||
649 | return 0; | ||
650 | } | ||
651 | |||
652 | |||
653 | /** | ||
654 | * Apply fix (constant) gain and overlap for sample[start...255]. | ||
655 | * | ||
656 | * @param pIn input buffer | ||
657 | * @param pPrev previous buffer to perform overlap against | ||
658 | * @param pOut output buffer | ||
659 | * @param start index to start with (always a multiple of 8) | ||
660 | * @param gain gain to apply | ||
661 | */ | ||
662 | |||
663 | static void applyFixGain (int32_t *pIn, int32_t *pPrev, int32_t *pOut, | ||
664 | int32_t start, int32_t gain) | ||
665 | { | ||
666 | int32_t i = start; | ||
667 | |||
668 | /* start is always a multiple of 8 and therefore allows us to unroll the | ||
669 | * loop to 8 calculation per loop | ||
670 | */ | ||
671 | if (ONE_16 == gain) { | ||
672 | /* gain1 = 1.0 -> no multiplication needed, just adding */ | ||
673 | /* Remark: This path is called >90%. */ | ||
674 | while (i<256) { | ||
675 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
676 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
677 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
678 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
679 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
680 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
681 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
682 | pOut[i] = pIn[i] + pPrev[i]; i++; | ||
683 | }; | ||
684 | } else { | ||
685 | /* gain1 != 1.0 -> we need to do a multiplication */ | ||
686 | /* Remark: This path is called seldom. */ | ||
687 | while (i<256) { | ||
688 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
689 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
690 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
691 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
692 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
693 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
694 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
695 | pOut[i] = fixmul16(pIn[i], gain) + pPrev[i]; i++; | ||
696 | }; | ||
697 | } | ||
698 | } | ||
699 | |||
700 | |||
701 | /** | ||
702 | * Apply variable gain and overlap. Returns sample index after applying gain, | ||
703 | * resulting sample index is always a multiple of 8. | ||
704 | * | ||
705 | * @param pIn input buffer | ||
706 | * @param pPrev previous buffer to perform overlap against | ||
707 | * @param pOut output buffer | ||
708 | * @param start index to start with (always a multiple of 8) | ||
709 | * @param end end index for first loop (always a multiple of 8) | ||
710 | * @param gain1 current bands gain to apply | ||
711 | * @param gain2 next bands gain to apply | ||
712 | * @param gain_inc stepwise adaption from gain1 to gain2 | ||
713 | */ | ||
714 | |||
715 | static int applyVariableGain (int32_t *pIn, int32_t *pPrev, int32_t *pOut, | ||
716 | int32_t start, int32_t end, | ||
717 | int32_t gain1, int32_t gain2, int32_t gain_inc) | ||
718 | { | ||
719 | int32_t i = start; | ||
720 | |||
721 | /* Apply fix gains until end index is reached */ | ||
722 | do { | ||
723 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
724 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
725 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
726 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
727 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
728 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
729 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
730 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
731 | } while (i < end); | ||
732 | |||
733 | /* Interpolation is done over next eight samples */ | ||
734 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
735 | gain2 = fixmul16(gain2, gain_inc); | ||
736 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
737 | gain2 = fixmul16(gain2, gain_inc); | ||
738 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
739 | gain2 = fixmul16(gain2, gain_inc); | ||
740 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
741 | gain2 = fixmul16(gain2, gain_inc); | ||
742 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
743 | gain2 = fixmul16(gain2, gain_inc); | ||
744 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
745 | gain2 = fixmul16(gain2, gain_inc); | ||
746 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
747 | gain2 = fixmul16(gain2, gain_inc); | ||
748 | pOut[i] = fixmul16((fixmul16(pIn[i], gain1) + pPrev[i]), gain2); i++; | ||
749 | gain2 = fixmul16(gain2, gain_inc); | ||
750 | |||
751 | return i; | ||
752 | } | ||
753 | |||
754 | |||
755 | /** | ||
756 | * Apply gain parameters and perform the MDCT overlapping part | ||
757 | * | ||
758 | * @param pIn input buffer | ||
759 | * @param pPrev previous buffer to perform overlap against | ||
760 | * @param pOut output buffer | ||
761 | * @param pGain1 current band gain info | ||
762 | * @param pGain2 next band gain info | ||
763 | */ | ||
764 | |||
765 | static void gainCompensateAndOverlap (int32_t *pIn, int32_t *pPrev, int32_t *pOut, | ||
766 | gain_info *pGain1, gain_info *pGain2) | ||
767 | { | ||
768 | /* gain compensation function */ | ||
769 | int32_t gain1, gain2, gain_inc; | ||
770 | int cnt, numdata, nsample, startLoc; | ||
771 | |||
772 | if (pGain2->num_gain_data == 0) | ||
773 | gain1 = ONE_16; | ||
774 | else | ||
775 | gain1 = (ONE_16<<4)>>(pGain2->levcode[0]); | ||
776 | |||
777 | if (pGain1->num_gain_data == 0) { | ||
778 | /* Remark: This path is called >90%. */ | ||
779 | /* Apply gain for all samples from 0...255 */ | ||
780 | applyFixGain(pIn, pPrev, pOut, 0, gain1); | ||
781 | } else { | ||
782 | /* Remark: This path is called seldom. */ | ||
783 | numdata = pGain1->num_gain_data; | ||
784 | pGain1->loccode[numdata] = 32; | ||
785 | pGain1->levcode[numdata] = 4; | ||
786 | |||
787 | nsample = 0; /* starting loop with =0 */ | ||
788 | |||
789 | for (cnt = 0; cnt < numdata; cnt++) { | ||
790 | startLoc = pGain1->loccode[cnt] * 8; | ||
791 | |||
792 | gain2 = (ONE_16<<4)>>(pGain1->levcode[cnt]); | ||
793 | gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15]; | ||
794 | |||
795 | /* Apply variable gain (gain1 -> gain2) to samples */ | ||
796 | nsample = applyVariableGain(pIn, pPrev, pOut, nsample, startLoc, gain1, gain2, gain_inc); | ||
797 | } | ||
798 | /* Apply gain for the residual samples from nsample...255 */ | ||
799 | applyFixGain(pIn, pPrev, pOut, nsample, gain1); | ||
800 | } | ||
801 | |||
802 | /* Delay for the overlapping part. */ | ||
803 | memcpy(pPrev, &pIn[256], 256*sizeof(int32_t)); | ||
804 | } | ||
805 | |||
806 | |||
807 | /** | ||
808 | * Combine the tonal band spectrum and regular band spectrum | ||
809 | * Return position of the last tonal coefficient | ||
810 | |||
811 | * | ||
812 | * @param pSpectrum output spectrum buffer | ||
813 | * @param numComponents amount of tonal components | ||
814 | * @param pComponent tonal components for this band | ||
815 | */ | ||
816 | |||
817 | static int addTonalComponents (int32_t *pSpectrum, int numComponents, tonal_component *pComponent) | ||
818 | { | ||
819 | int cnt, i, lastPos = -1; | ||
820 | int32_t *pOut; | ||
821 | int32_t *pIn; | ||
822 | |||
823 | for (cnt = 0; cnt < numComponents; cnt++){ | ||
824 | lastPos = FFMAX(pComponent[cnt].pos + pComponent[cnt].numCoefs, lastPos); | ||
825 | pIn = pComponent[cnt].coef; | ||
826 | pOut = &(pSpectrum[pComponent[cnt].pos]); | ||
827 | |||
828 | for (i=0 ; i<pComponent[cnt].numCoefs ; i++) | ||
829 | pOut[i] += pIn[i]; | ||
830 | } | ||
831 | |||
832 | return lastPos; | ||
833 | } | ||
834 | |||
835 | |||
836 | /** | ||
837 | * Linear equidistant interpolation between two points x and y. 7 interpolation | ||
838 | * points can be calculated. | ||
839 | * Result for s=0 is x | ||
840 | * Result for s=8 is y | ||
841 | * | ||
842 | * @param x first input point | ||
843 | * @param y second input point | ||
844 | * @param s index of interpolation point (0..7) | ||
845 | */ | ||
846 | |||
847 | /* rockbox: Not used anymore. Faster version defined below. | ||
848 | #define INTERPOLATE_FP16(x, y, s) ((x) + fixmul16(((s*ONE_16)>>3), (((y) - (x))))) | ||
849 | */ | ||
850 | #define INTERPOLATE_FP16(x, y, s) ((x) + ((s*((y)-(x)))>>3)) | ||
851 | |||
852 | static void reverseMatrixing(int32_t *su1, int32_t *su2, int *pPrevCode, int *pCurrCode) | ||
853 | { | ||
854 | int i, band, nsample, s1, s2; | ||
855 | int32_t c1, c2; | ||
856 | int32_t mc1_l, mc1_r, mc2_l, mc2_r; | ||
857 | |||
858 | for (i=0,band = 0; band < 4*256; band+=256,i++) { | ||
859 | s1 = pPrevCode[i]; | ||
860 | s2 = pCurrCode[i]; | ||
861 | nsample = 0; | ||
862 | |||
863 | if (s1 != s2) { | ||
864 | /* Selector value changed, interpolation needed. */ | ||
865 | mc1_l = matrixCoeffs_fix[s1<<1]; | ||
866 | mc1_r = matrixCoeffs_fix[(s1<<1)+1]; | ||
867 | mc2_l = matrixCoeffs_fix[s2<<1]; | ||
868 | mc2_r = matrixCoeffs_fix[(s2<<1)+1]; | ||
869 | |||
870 | /* Interpolation is done over the first eight samples. */ | ||
871 | for(; nsample < 8; nsample++) { | ||
872 | c1 = su1[band+nsample]; | ||
873 | c2 = su2[band+nsample]; | ||
874 | c2 = fixmul16(c1, INTERPOLATE_FP16(mc1_l, mc2_l, nsample)) + fixmul16(c2, INTERPOLATE_FP16(mc1_r, mc2_r, nsample)); | ||
875 | su1[band+nsample] = c2; | ||
876 | su2[band+nsample] = (c1 << 1) - c2; | ||
877 | } | ||
878 | } | ||
879 | |||
880 | /* Apply the matrix without interpolation. */ | ||
881 | switch (s2) { | ||
882 | case 0: /* M/S decoding */ | ||
883 | for (; nsample < 256; nsample++) { | ||
884 | c1 = su1[band+nsample]; | ||
885 | c2 = su2[band+nsample]; | ||
886 | su1[band+nsample] = c2 << 1; | ||
887 | su2[band+nsample] = (c1 - c2) << 1; | ||
888 | } | ||
889 | break; | ||
890 | |||
891 | case 1: | ||
892 | for (; nsample < 256; nsample++) { | ||
893 | c1 = su1[band+nsample]; | ||
894 | c2 = su2[band+nsample]; | ||
895 | su1[band+nsample] = (c1 + c2) << 1; | ||
896 | su2[band+nsample] = -1*(c2 << 1); | ||
897 | } | ||
898 | break; | ||
899 | case 2: | ||
900 | case 3: | ||
901 | for (; nsample < 256; nsample++) { | ||
902 | c1 = su1[band+nsample]; | ||
903 | c2 = su2[band+nsample]; | ||
904 | su1[band+nsample] = c1 + c2; | ||
905 | su2[band+nsample] = c1 - c2; | ||
906 | } | ||
907 | break; | ||
908 | default: | ||
909 | /* assert(0) */; | ||
910 | break; | ||
911 | } | ||
912 | } | ||
913 | } | ||
914 | |||
915 | static void getChannelWeights (int indx, int flag, int32_t ch[2]){ | ||
916 | /* Read channel weights from table */ | ||
917 | if (flag) { | ||
918 | /* Swap channel weights */ | ||
919 | ch[1] = channelWeights0[indx&7]; | ||
920 | ch[0] = channelWeights1[indx&7]; | ||
921 | } else { | ||
922 | ch[0] = channelWeights0[indx&7]; | ||
923 | ch[1] = channelWeights1[indx&7]; | ||
924 | } | ||
925 | } | ||
926 | |||
927 | static void channelWeighting (int32_t *su1, int32_t *su2, int *p3) | ||
928 | { | ||
929 | int band, nsample; | ||
930 | /* w[x][y] y=0 is left y=1 is right */ | ||
931 | int32_t w[2][2]; | ||
932 | |||
933 | if (p3[1] != 7 || p3[3] != 7){ | ||
934 | getChannelWeights(p3[1], p3[0], w[0]); | ||
935 | getChannelWeights(p3[3], p3[2], w[1]); | ||
936 | |||
937 | for(band = 1; band < 4; band++) { | ||
938 | /* scale the channels by the weights */ | ||
939 | for(nsample = 0; nsample < 8; nsample++) { | ||
940 | su1[band*256+nsample] = fixmul16(su1[band*256+nsample], INTERPOLATE_FP16(w[0][0], w[0][1], nsample)); | ||
941 | su2[band*256+nsample] = fixmul16(su2[band*256+nsample], INTERPOLATE_FP16(w[1][0], w[1][1], nsample)); | ||
942 | } | ||
943 | |||
944 | for(; nsample < 256; nsample++) { | ||
945 | su1[band*256+nsample] = fixmul16(su1[band*256+nsample], w[1][0]); | ||
946 | su2[band*256+nsample] = fixmul16(su2[band*256+nsample], w[1][1]); | ||
947 | } | ||
948 | } | ||
949 | } | ||
950 | } | ||
951 | |||
952 | /** | ||
953 | * Decode a Sound Unit | ||
954 | * | ||
955 | * @param gb the GetBit context | ||
956 | * @param pSnd the channel unit to be used | ||
957 | * @param pOut the decoded samples before IQMF | ||
958 | * @param channelNum channel number | ||
959 | * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono) | ||
960 | */ | ||
961 | |||
962 | static int decodeChannelSoundUnit (GetBitContext *gb, channel_unit *pSnd, int32_t *pOut, int channelNum, int codingMode) | ||
963 | { | ||
964 | int band, result=0, numSubbands, lastTonal, numBands; | ||
965 | if (codingMode == JOINT_STEREO && channelNum == 1) { | ||
966 | if (get_bits(gb,2) != 3) { | ||
967 | DEBUGF("JS mono Sound Unit id != 3.\n"); | ||
968 | return -1; | ||
969 | } | ||
970 | } else { | ||
971 | if (get_bits(gb,6) != 0x28) { | ||
972 | DEBUGF("Sound Unit id != 0x28.\n"); | ||
973 | return -1; | ||
974 | } | ||
975 | } | ||
976 | |||
977 | /* number of coded QMF bands */ | ||
978 | pSnd->bandsCoded = get_bits(gb,2); | ||
979 | |||
980 | result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded); | ||
981 | if (result) return result; | ||
982 | |||
983 | pSnd->numComponents = decodeTonalComponents (gb, pSnd->components, pSnd->bandsCoded); | ||
984 | if (pSnd->numComponents == -1) return -1; | ||
985 | |||
986 | numSubbands = decodeSpectrum (gb, pSnd->spectrum); | ||
987 | |||
988 | /* Merge the decoded spectrum and tonal components. */ | ||
989 | lastTonal = addTonalComponents (pSnd->spectrum, pSnd->numComponents, pSnd->components); | ||
990 | |||
991 | |||
992 | /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */ | ||
993 | numBands = (subbandTab[numSubbands] - 1) >> 8; | ||
994 | if (lastTonal >= 0) | ||
995 | numBands = FFMAX((lastTonal + 256) >> 8, numBands); | ||
996 | |||
997 | /* Reconstruct time domain samples. */ | ||
998 | for (band=0; band<4; band++) { | ||
999 | /* Perform the IMDCT step without overlapping. */ | ||
1000 | if (band <= numBands) { | ||
1001 | IMLT(&(pSnd->spectrum[band*256]), pSnd->IMDCT_buf); | ||
1002 | } else { | ||
1003 | memset(pSnd->IMDCT_buf, 0, 512 * sizeof(int32_t)); | ||
1004 | } | ||
1005 | |||
1006 | /* gain compensation and overlapping */ | ||
1007 | gainCompensateAndOverlap (pSnd->IMDCT_buf, &(pSnd->prevFrame[band*256]), &(pOut[band*256]), | ||
1008 | &((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]), | ||
1009 | &((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band])); | ||
1010 | } | ||
1011 | |||
1012 | /* Swap the gain control buffers for the next frame. */ | ||
1013 | pSnd->gcBlkSwitch ^= 1; | ||
1014 | |||
1015 | return 0; | ||
1016 | } | ||
1017 | |||
1018 | /** | ||
1019 | * Frame handling | ||
1020 | * | ||
1021 | * @param q Atrac3 private context | ||
1022 | * @param databuf the input data | ||
1023 | */ | ||
1024 | |||
1025 | static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf, int off) | ||
1026 | { | ||
1027 | int result, i; | ||
1028 | int32_t *p1, *p2, *p3, *p4; | ||
1029 | uint8_t *ptr1; | ||
1030 | |||
1031 | if (q->codingMode == JOINT_STEREO) { | ||
1032 | |||
1033 | /* channel coupling mode */ | ||
1034 | /* decode Sound Unit 1 */ | ||
1035 | init_get_bits(&q->gb,databuf,q->bits_per_frame); | ||
1036 | |||
1037 | result = decodeChannelSoundUnit(&q->gb, q->pUnits, q->outSamples, 0, JOINT_STEREO); | ||
1038 | if (result != 0) | ||
1039 | return (result); | ||
1040 | |||
1041 | /* Framedata of the su2 in the joint-stereo mode is encoded in | ||
1042 | * reverse byte order so we need to swap it first. */ | ||
1043 | if (databuf == q->decoded_bytes_buffer) { | ||
1044 | uint8_t *ptr2 = q->decoded_bytes_buffer+q->bytes_per_frame-1; | ||
1045 | ptr1 = q->decoded_bytes_buffer; | ||
1046 | for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) { | ||
1047 | FFSWAP(uint8_t,*ptr1,*ptr2); | ||
1048 | } | ||
1049 | } else { | ||
1050 | const uint8_t *ptr2 = databuf+q->bytes_per_frame-1; | ||
1051 | for (i = 0; i < q->bytes_per_frame; i++) | ||
1052 | q->decoded_bytes_buffer[i] = *ptr2--; | ||
1053 | } | ||
1054 | |||
1055 | /* Skip the sync codes (0xF8). */ | ||
1056 | ptr1 = q->decoded_bytes_buffer; | ||
1057 | for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { | ||
1058 | if (i >= q->bytes_per_frame) | ||
1059 | return -1; | ||
1060 | } | ||
1061 | |||
1062 | |||
1063 | /* set the bitstream reader at the start of the second Sound Unit*/ | ||
1064 | init_get_bits(&q->gb,ptr1,q->bits_per_frame); | ||
1065 | |||
1066 | /* Fill the Weighting coeffs delay buffer */ | ||
1067 | memmove(q->weighting_delay,&(q->weighting_delay[2]),4*sizeof(int)); | ||
1068 | q->weighting_delay[4] = get_bits1(&q->gb); | ||
1069 | q->weighting_delay[5] = get_bits(&q->gb,3); | ||
1070 | |||
1071 | for (i = 0; i < 4; i++) { | ||
1072 | q->matrix_coeff_index_prev[i] = q->matrix_coeff_index_now[i]; | ||
1073 | q->matrix_coeff_index_now[i] = q->matrix_coeff_index_next[i]; | ||
1074 | q->matrix_coeff_index_next[i] = get_bits(&q->gb,2); | ||
1075 | } | ||
1076 | |||
1077 | /* Decode Sound Unit 2. */ | ||
1078 | result = decodeChannelSoundUnit(&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO); | ||
1079 | if (result != 0) | ||
1080 | return (result); | ||
1081 | |||
1082 | /* Reconstruct the channel coefficients. */ | ||
1083 | reverseMatrixing(q->outSamples, &q->outSamples[1024], q->matrix_coeff_index_prev, q->matrix_coeff_index_now); | ||
1084 | |||
1085 | channelWeighting(q->outSamples, &q->outSamples[1024], q->weighting_delay); | ||
1086 | |||
1087 | } else { | ||
1088 | /* normal stereo mode or mono */ | ||
1089 | /* Decode the channel sound units. */ | ||
1090 | for (i=0 ; i<q->channels ; i++) { | ||
1091 | |||
1092 | /* Set the bitstream reader at the start of a channel sound unit. */ | ||
1093 | init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels)+off, (q->bits_per_frame)/q->channels); | ||
1094 | |||
1095 | result = decodeChannelSoundUnit(&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode); | ||
1096 | if (result != 0) | ||
1097 | return (result); | ||
1098 | } | ||
1099 | } | ||
1100 | |||
1101 | /* Apply the iQMF synthesis filter. */ | ||
1102 | p1= q->outSamples; | ||
1103 | for (i=0 ; i<q->channels ; i++) { | ||
1104 | p2= p1+256; | ||
1105 | p3= p2+256; | ||
1106 | p4= p3+256; | ||
1107 | iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf); | ||
1108 | iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf); | ||
1109 | iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf); | ||
1110 | p1 +=1024; | ||
1111 | } | ||
1112 | |||
1113 | return 0; | ||
1114 | } | ||
1115 | |||
1116 | |||
1117 | /** | ||
1118 | * Atrac frame decoding | ||
1119 | * | ||
1120 | * @param rmctx pointer to the AVCodecContext | ||
1121 | */ | ||
1122 | |||
1123 | int atrac3_decode_frame(unsigned long block_align, ATRAC3Context *q, | ||
1124 | int *data_size, const uint8_t *buf, int buf_size) { | ||
1125 | int result = 0, off = 0; | ||
1126 | const uint8_t* databuf; | ||
1127 | |||
1128 | if ((unsigned)buf_size < block_align) | ||
1129 | return buf_size; | ||
1130 | |||
1131 | /* Check if we need to descramble and what buffer to pass on. */ | ||
1132 | if (q->scrambled_stream) { | ||
1133 | off = decode_bytes(buf, q->decoded_bytes_buffer, block_align); | ||
1134 | databuf = q->decoded_bytes_buffer; | ||
1135 | } else { | ||
1136 | databuf = buf; | ||
1137 | } | ||
1138 | |||
1139 | result = decodeFrame(q, databuf, off); | ||
1140 | |||
1141 | if (result != 0) { | ||
1142 | DEBUGF("Frame decoding error!\n"); | ||
1143 | return -1; | ||
1144 | } | ||
1145 | |||
1146 | if (q->channels == 1) | ||
1147 | *data_size = 1024 * sizeof(int32_t); | ||
1148 | else | ||
1149 | *data_size = 2048 * sizeof(int32_t); | ||
1150 | |||
1151 | return block_align; | ||
1152 | } | ||
1153 | |||
1154 | |||
1155 | /** | ||
1156 | * Atrac3 initialization | ||
1157 | * | ||
1158 | * @param rmctx pointer to the RMContext | ||
1159 | */ | ||
1160 | int atrac3_decode_init(ATRAC3Context *q, struct mp3entry *id3) | ||
1161 | { | ||
1162 | int i; | ||
1163 | uint8_t *edata_ptr = (uint8_t*)&id3->id3v2buf; | ||
1164 | |||
1165 | #if defined(CPU_COLDFIRE) | ||
1166 | coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE); | ||
1167 | #endif | ||
1168 | |||
1169 | /* Take data from the RM container. */ | ||
1170 | q->sample_rate = id3->frequency; | ||
1171 | q->channels = id3->channels; | ||
1172 | q->bit_rate = id3->bitrate * 1000; | ||
1173 | q->bits_per_frame = id3->bytesperframe * 8; | ||
1174 | q->bytes_per_frame = id3->bytesperframe; | ||
1175 | |||
1176 | /* Take care of the codec-specific extradata. */ | ||
1177 | |||
1178 | if (id3->extradata_size == 14) { | ||
1179 | /* Parse the extradata, WAV format */ | ||
1180 | DEBUGF("[0-1] %d\n",rm_get_uint16le(&edata_ptr[0])); /* Unknown value always 1 */ | ||
1181 | q->samples_per_channel = rm_get_uint32le(&edata_ptr[2]); | ||
1182 | q->codingMode = rm_get_uint16le(&edata_ptr[6]); | ||
1183 | DEBUGF("[8-9] %d\n",rm_get_uint16le(&edata_ptr[8])); /* Dupe of coding mode */ | ||
1184 | q->frame_factor = rm_get_uint16le(&edata_ptr[10]); /* Unknown always 1 */ | ||
1185 | DEBUGF("[12-13] %d\n",rm_get_uint16le(&edata_ptr[12])); /* Unknown always 0 */ | ||
1186 | |||
1187 | /* setup */ | ||
1188 | q->samples_per_frame = 1024 * q->channels; | ||
1189 | q->atrac3version = 4; | ||
1190 | q->delay = 0x88E; | ||
1191 | if (q->codingMode) | ||
1192 | q->codingMode = JOINT_STEREO; | ||
1193 | else | ||
1194 | q->codingMode = STEREO; | ||
1195 | q->scrambled_stream = 0; | ||
1196 | |||
1197 | 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)) { | ||
1198 | } else { | ||
1199 | DEBUGF("Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q->bytes_per_frame, q->channels, q->frame_factor); | ||
1200 | return -1; | ||
1201 | } | ||
1202 | |||
1203 | } else if (id3->extradata_size == 10) { | ||
1204 | /* Parse the extradata, RM format. */ | ||
1205 | q->atrac3version = rm_get_uint32be(&edata_ptr[0]); | ||
1206 | q->samples_per_frame = rm_get_uint16be(&edata_ptr[4]); | ||
1207 | q->delay = rm_get_uint16be(&edata_ptr[6]); | ||
1208 | q->codingMode = rm_get_uint16be(&edata_ptr[8]); | ||
1209 | |||
1210 | q->samples_per_channel = q->samples_per_frame / q->channels; | ||
1211 | q->scrambled_stream = 1; | ||
1212 | |||
1213 | } else { | ||
1214 | DEBUGF("Unknown extradata size %d.\n",id3->extradata_size); | ||
1215 | } | ||
1216 | /* Check the extradata. */ | ||
1217 | |||
1218 | if (q->atrac3version != 4) { | ||
1219 | DEBUGF("Version %d != 4.\n",q->atrac3version); | ||
1220 | return -1; | ||
1221 | } | ||
1222 | |||
1223 | if (q->samples_per_frame != 1024 && q->samples_per_frame != 2048) { | ||
1224 | DEBUGF("Unknown amount of samples per frame %d.\n",q->samples_per_frame); | ||
1225 | return -1; | ||
1226 | } | ||
1227 | |||
1228 | if (q->delay != 0x88E) { | ||
1229 | DEBUGF("Unknown amount of delay %x != 0x88E.\n",q->delay); | ||
1230 | return -1; | ||
1231 | } | ||
1232 | |||
1233 | if (q->codingMode == STEREO) { | ||
1234 | DEBUGF("Normal stereo detected.\n"); | ||
1235 | } else if (q->codingMode == JOINT_STEREO) { | ||
1236 | DEBUGF("Joint stereo detected.\n"); | ||
1237 | } else { | ||
1238 | DEBUGF("Unknown channel coding mode %x!\n",q->codingMode); | ||
1239 | return -1; | ||
1240 | } | ||
1241 | |||
1242 | if (id3->channels <= 0 || id3->channels > 2 ) { | ||
1243 | DEBUGF("Channel configuration error!\n"); | ||
1244 | return -1; | ||
1245 | } | ||
1246 | |||
1247 | |||
1248 | if(id3->bytesperframe >= UINT16_MAX/2) | ||
1249 | return -1; | ||
1250 | |||
1251 | |||
1252 | /* Initialize the VLC tables. */ | ||
1253 | if (!vlcs_initialized) { | ||
1254 | for (i=0 ; i<7 ; i++) { | ||
1255 | spectral_coeff_tab[i].table = &atrac3_vlc_table[atrac3_vlc_offs[i]]; | ||
1256 | spectral_coeff_tab[i].table_allocated = atrac3_vlc_offs[i + 1] - atrac3_vlc_offs[i]; | ||
1257 | init_vlc (&spectral_coeff_tab[i], 9, huff_tab_sizes[i], | ||
1258 | huff_bits[i], 1, 1, | ||
1259 | huff_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC); | ||
1260 | } | ||
1261 | |||
1262 | vlcs_initialized = 1; | ||
1263 | |||
1264 | } | ||
1265 | |||
1266 | init_atrac3_transforms(); | ||
1267 | |||
1268 | /* init the joint-stereo decoding data */ | ||
1269 | q->weighting_delay[0] = 0; | ||
1270 | q->weighting_delay[1] = 7; | ||
1271 | q->weighting_delay[2] = 0; | ||
1272 | q->weighting_delay[3] = 7; | ||
1273 | q->weighting_delay[4] = 0; | ||
1274 | q->weighting_delay[5] = 7; | ||
1275 | |||
1276 | for (i=0; i<4; i++) { | ||
1277 | q->matrix_coeff_index_prev[i] = 3; | ||
1278 | q->matrix_coeff_index_now[i] = 3; | ||
1279 | q->matrix_coeff_index_next[i] = 3; | ||
1280 | } | ||
1281 | |||
1282 | /* Link the iram'ed arrays to the decoder's data structure */ | ||
1283 | q->pUnits = channel_units; | ||
1284 | q->pUnits[0].spectrum = &atrac3_spectrum [0][0]; | ||
1285 | q->pUnits[1].spectrum = &atrac3_spectrum [1][0]; | ||
1286 | q->pUnits[0].IMDCT_buf = &atrac3_IMDCT_buf[0][0]; | ||
1287 | q->pUnits[1].IMDCT_buf = &atrac3_IMDCT_buf[1][0]; | ||
1288 | q->pUnits[0].prevFrame = &atrac3_prevFrame[0][0]; | ||
1289 | q->pUnits[1].prevFrame = &atrac3_prevFrame[1][0]; | ||
1290 | |||
1291 | return 0; | ||
1292 | } | ||
1293 | |||