Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * MPEG Audio decoder | |
ff4ec49e | 3 | * Copyright (c) 2001, 2002 Fabrice Bellard. |
de6d9b64 | 4 | * |
ff4ec49e FB |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
de6d9b64 | 9 | * |
ff4ec49e | 10 | * This library is distributed in the hope that it will be useful, |
de6d9b64 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ff4ec49e FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
de6d9b64 | 14 | * |
ff4ec49e FB |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
de6d9b64 | 18 | */ |
983e3246 MN |
19 | |
20 | /** | |
21 | * @file mpegaudiodec.c | |
22 | * MPEG Audio decoder. | |
23 | */ | |
24 | ||
239c2f4c | 25 | //#define DEBUG |
de6d9b64 | 26 | #include "avcodec.h" |
239c2f4c | 27 | #include "mpegaudio.h" |
de6d9b64 FB |
28 | |
29 | /* | |
239c2f4c FB |
30 | * TODO: |
31 | * - in low precision mode, use more 16 bit multiplies in synth filter | |
32 | * - test lsf / mpeg25 extensively. | |
de6d9b64 FB |
33 | */ |
34 | ||
239c2f4c FB |
35 | /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg |
36 | audio decoder */ | |
81552334 FB |
37 | #ifdef CONFIG_MPEGAUDIO_HP |
38 | #define USE_HIGHPRECISION | |
39 | #endif | |
239c2f4c FB |
40 | |
41 | #ifdef USE_HIGHPRECISION | |
42 | #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */ | |
43 | #define WFRAC_BITS 16 /* fractional bits for window */ | |
44 | #else | |
45 | #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */ | |
46 | #define WFRAC_BITS 14 /* fractional bits for window */ | |
47 | #endif | |
48 | ||
49 | #define FRAC_ONE (1 << FRAC_BITS) | |
50 | ||
0c1a9eda ZK |
51 | #define MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS) |
52 | #define MUL64(a,b) ((int64_t)(a) * (int64_t)(b)) | |
239c2f4c FB |
53 | #define FIX(a) ((int)((a) * FRAC_ONE)) |
54 | /* WARNING: only correct for posititive numbers */ | |
55 | #define FIXR(a) ((int)((a) * FRAC_ONE + 0.5)) | |
56 | #define FRAC_RND(a) (((a) + (FRAC_ONE/2)) >> FRAC_BITS) | |
57 | ||
58 | #if FRAC_BITS <= 15 | |
0c1a9eda | 59 | typedef int16_t MPA_INT; |
239c2f4c | 60 | #else |
0c1a9eda | 61 | typedef int32_t MPA_INT; |
239c2f4c FB |
62 | #endif |
63 | ||
64 | /****************/ | |
65 | ||
de6d9b64 FB |
66 | #define HEADER_SIZE 4 |
67 | #define BACKSTEP_SIZE 512 | |
68 | ||
69 | typedef struct MPADecodeContext { | |
0c1a9eda | 70 | uint8_t inbuf1[2][MPA_MAX_CODED_FRAME_SIZE + BACKSTEP_SIZE]; /* input buffer */ |
de6d9b64 | 71 | int inbuf_index; |
0c1a9eda | 72 | uint8_t *inbuf_ptr, *inbuf; |
de6d9b64 | 73 | int frame_size; |
239c2f4c FB |
74 | int free_format_frame_size; /* frame size in case of free format |
75 | (zero if currently unknown) */ | |
76 | /* next header (used in free format parsing) */ | |
0c1a9eda | 77 | uint32_t free_format_next_header; |
de6d9b64 FB |
78 | int error_protection; |
79 | int layer; | |
80 | int sample_rate; | |
239c2f4c | 81 | int sample_rate_index; /* between 0 and 8 */ |
de6d9b64 FB |
82 | int bit_rate; |
83 | int old_frame_size; | |
84 | GetBitContext gb; | |
239c2f4c FB |
85 | int nb_channels; |
86 | int mode; | |
87 | int mode_ext; | |
88 | int lsf; | |
a05c8d71 | 89 | MPA_INT synth_buf[MPA_MAX_CHANNELS][512 * 2] __attribute__((aligned(16))); |
239c2f4c | 90 | int synth_buf_offset[MPA_MAX_CHANNELS]; |
a05c8d71 | 91 | int32_t sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT] __attribute__((aligned(16))); |
0c1a9eda | 92 | int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */ |
239c2f4c FB |
93 | #ifdef DEBUG |
94 | int frame_count; | |
95 | #endif | |
de6d9b64 FB |
96 | } MPADecodeContext; |
97 | ||
239c2f4c FB |
98 | /* layer 3 "granule" */ |
99 | typedef struct GranuleDef { | |
0c1a9eda | 100 | uint8_t scfsi; |
239c2f4c FB |
101 | int part2_3_length; |
102 | int big_values; | |
103 | int global_gain; | |
104 | int scalefac_compress; | |
0c1a9eda ZK |
105 | uint8_t block_type; |
106 | uint8_t switch_point; | |
239c2f4c FB |
107 | int table_select[3]; |
108 | int subblock_gain[3]; | |
0c1a9eda ZK |
109 | uint8_t scalefac_scale; |
110 | uint8_t count1table_select; | |
239c2f4c FB |
111 | int region_size[3]; /* number of huffman codes in each region */ |
112 | int preflag; | |
113 | int short_start, long_end; /* long/short band indexes */ | |
0c1a9eda ZK |
114 | uint8_t scale_factors[40]; |
115 | int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ | |
239c2f4c | 116 | } GranuleDef; |
de6d9b64 | 117 | |
239c2f4c FB |
118 | #define MODE_EXT_MS_STEREO 2 |
119 | #define MODE_EXT_I_STEREO 1 | |
120 | ||
121 | /* layer 3 huffman tables */ | |
122 | typedef struct HuffTable { | |
123 | int xsize; | |
0c1a9eda ZK |
124 | const uint8_t *bits; |
125 | const uint16_t *codes; | |
239c2f4c FB |
126 | } HuffTable; |
127 | ||
128 | #include "mpegaudiodectab.h" | |
129 | ||
130 | /* vlc structure for decoding layer 3 huffman tables */ | |
131 | static VLC huff_vlc[16]; | |
0c1a9eda | 132 | static uint8_t *huff_code_table[16]; |
239c2f4c FB |
133 | static VLC huff_quad_vlc[2]; |
134 | /* computed from band_size_long */ | |
0c1a9eda | 135 | static uint16_t band_index_long[9][23]; |
239c2f4c FB |
136 | /* XXX: free when all decoders are closed */ |
137 | #define TABLE_4_3_SIZE (8191 + 16) | |
0c1a9eda | 138 | static int8_t *table_4_3_exp; |
239c2f4c | 139 | #if FRAC_BITS <= 15 |
0c1a9eda | 140 | static uint16_t *table_4_3_value; |
239c2f4c | 141 | #else |
0c1a9eda | 142 | static uint32_t *table_4_3_value; |
239c2f4c FB |
143 | #endif |
144 | /* intensity stereo coef table */ | |
0c1a9eda ZK |
145 | static int32_t is_table[2][16]; |
146 | static int32_t is_table_lsf[2][2][16]; | |
147 | static int32_t csa_table[8][2]; | |
148 | static int32_t mdct_win[8][36]; | |
239c2f4c FB |
149 | |
150 | /* lower 2 bits: modulo 3, higher bits: shift */ | |
0c1a9eda | 151 | static uint16_t scale_factor_modshift[64]; |
239c2f4c | 152 | /* [i][j]: 2^(-j/3) * FRAC_ONE * 2^(i+2) / (2^(i+2) - 1) */ |
0c1a9eda | 153 | static int32_t scale_factor_mult[15][3]; |
239c2f4c FB |
154 | /* mult table for layer 2 group quantization */ |
155 | ||
156 | #define SCALE_GEN(v) \ | |
157 | { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } | |
158 | ||
0c1a9eda | 159 | static int32_t scale_factor_mult2[3][3] = { |
81552334 FB |
160 | SCALE_GEN(4.0 / 3.0), /* 3 steps */ |
161 | SCALE_GEN(4.0 / 5.0), /* 5 steps */ | |
162 | SCALE_GEN(4.0 / 9.0), /* 9 steps */ | |
239c2f4c FB |
163 | }; |
164 | ||
165 | /* 2^(n/4) */ | |
0c1a9eda | 166 | static uint32_t scale_factor_mult3[4] = { |
239c2f4c FB |
167 | FIXR(1.0), |
168 | FIXR(1.18920711500272106671), | |
169 | FIXR(1.41421356237309504880), | |
170 | FIXR(1.68179283050742908605), | |
de6d9b64 FB |
171 | }; |
172 | ||
a05c8d71 | 173 | static MPA_INT window[512] __attribute__((aligned(16))); |
239c2f4c FB |
174 | |
175 | /* layer 1 unscaling */ | |
176 | /* n = number of bits of the mantissa minus 1 */ | |
177 | static inline int l1_unscale(int n, int mant, int scale_factor) | |
178 | { | |
179 | int shift, mod; | |
0c1a9eda | 180 | int64_t val; |
239c2f4c FB |
181 | |
182 | shift = scale_factor_modshift[scale_factor]; | |
183 | mod = shift & 3; | |
184 | shift >>= 2; | |
185 | val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]); | |
186 | shift += n; | |
81552334 FB |
187 | /* NOTE: at this point, 1 <= shift >= 21 + 15 */ |
188 | return (int)((val + (1LL << (shift - 1))) >> shift); | |
239c2f4c FB |
189 | } |
190 | ||
191 | static inline int l2_unscale_group(int steps, int mant, int scale_factor) | |
192 | { | |
193 | int shift, mod, val; | |
194 | ||
195 | shift = scale_factor_modshift[scale_factor]; | |
196 | mod = shift & 3; | |
197 | shift >>= 2; | |
81552334 FB |
198 | |
199 | val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod]; | |
200 | /* NOTE: at this point, 0 <= shift <= 21 */ | |
201 | if (shift > 0) | |
202 | val = (val + (1 << (shift - 1))) >> shift; | |
203 | return val; | |
239c2f4c FB |
204 | } |
205 | ||
206 | /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */ | |
207 | static inline int l3_unscale(int value, int exponent) | |
208 | { | |
209 | #if FRAC_BITS <= 15 | |
210 | unsigned int m; | |
211 | #else | |
0c1a9eda | 212 | uint64_t m; |
239c2f4c FB |
213 | #endif |
214 | int e; | |
215 | ||
216 | e = table_4_3_exp[value]; | |
217 | e += (exponent >> 2); | |
218 | e = FRAC_BITS - e; | |
219 | #if FRAC_BITS <= 15 | |
220 | if (e > 31) | |
221 | e = 31; | |
222 | #endif | |
223 | m = table_4_3_value[value]; | |
224 | #if FRAC_BITS <= 15 | |
225 | m = (m * scale_factor_mult3[exponent & 3]); | |
226 | m = (m + (1 << (e-1))) >> e; | |
227 | return m; | |
228 | #else | |
229 | m = MUL64(m, scale_factor_mult3[exponent & 3]); | |
0c1a9eda | 230 | m = (m + (uint64_t_C(1) << (e-1))) >> e; |
239c2f4c FB |
231 | return m; |
232 | #endif | |
233 | } | |
234 | ||
f9ed4f88 FB |
235 | /* all integer n^(4/3) computation code */ |
236 | #define DEV_ORDER 13 | |
237 | ||
238 | #define POW_FRAC_BITS 24 | |
239 | #define POW_FRAC_ONE (1 << POW_FRAC_BITS) | |
240 | #define POW_FIX(a) ((int)((a) * POW_FRAC_ONE)) | |
0c1a9eda | 241 | #define POW_MULL(a,b) (((int64_t)(a) * (int64_t)(b)) >> POW_FRAC_BITS) |
f9ed4f88 FB |
242 | |
243 | static int dev_4_3_coefs[DEV_ORDER]; | |
244 | ||
245 | static int pow_mult3[3] = { | |
246 | POW_FIX(1.0), | |
247 | POW_FIX(1.25992104989487316476), | |
248 | POW_FIX(1.58740105196819947474), | |
249 | }; | |
250 | ||
251 | static void int_pow_init(void) | |
252 | { | |
253 | int i, a; | |
254 | ||
255 | a = POW_FIX(1.0); | |
256 | for(i=0;i<DEV_ORDER;i++) { | |
257 | a = POW_MULL(a, POW_FIX(4.0 / 3.0) - i * POW_FIX(1.0)) / (i + 1); | |
258 | dev_4_3_coefs[i] = a; | |
259 | } | |
260 | } | |
261 | ||
262 | /* return the mantissa and the binary exponent */ | |
263 | static int int_pow(int i, int *exp_ptr) | |
264 | { | |
265 | int e, er, eq, j; | |
266 | int a, a1; | |
267 | ||
268 | /* renormalize */ | |
269 | a = i; | |
270 | e = POW_FRAC_BITS; | |
271 | while (a < (1 << (POW_FRAC_BITS - 1))) { | |
272 | a = a << 1; | |
273 | e--; | |
274 | } | |
275 | a -= (1 << POW_FRAC_BITS); | |
276 | a1 = 0; | |
277 | for(j = DEV_ORDER - 1; j >= 0; j--) | |
278 | a1 = POW_MULL(a, dev_4_3_coefs[j] + a1); | |
279 | a = (1 << POW_FRAC_BITS) + a1; | |
280 | /* exponent compute (exact) */ | |
281 | e = e * 4; | |
282 | er = e % 3; | |
283 | eq = e / 3; | |
284 | a = POW_MULL(a, pow_mult3[er]); | |
285 | while (a >= 2 * POW_FRAC_ONE) { | |
286 | a = a >> 1; | |
287 | eq++; | |
288 | } | |
289 | /* convert to float */ | |
290 | while (a < POW_FRAC_ONE) { | |
291 | a = a << 1; | |
292 | eq--; | |
293 | } | |
59d3e367 | 294 | /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */ |
81552334 | 295 | #if POW_FRAC_BITS > FRAC_BITS |
59d3e367 FB |
296 | a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS); |
297 | /* correct overflow */ | |
298 | if (a >= 2 * (1 << FRAC_BITS)) { | |
299 | a = a >> 1; | |
300 | eq++; | |
301 | } | |
302 | #endif | |
f9ed4f88 | 303 | *exp_ptr = eq; |
f9ed4f88 | 304 | return a; |
f9ed4f88 | 305 | } |
de6d9b64 FB |
306 | |
307 | static int decode_init(AVCodecContext * avctx) | |
308 | { | |
309 | MPADecodeContext *s = avctx->priv_data; | |
b587a7cb | 310 | static int init=0; |
239c2f4c | 311 | int i, j, k; |
de6d9b64 | 312 | |
8c5b5683 | 313 | if (!init && !avctx->parse_only) { |
239c2f4c FB |
314 | /* scale factors table for layer 1/2 */ |
315 | for(i=0;i<64;i++) { | |
316 | int shift, mod; | |
317 | /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ | |
81552334 | 318 | shift = (i / 3); |
239c2f4c | 319 | mod = i % 3; |
239c2f4c FB |
320 | scale_factor_modshift[i] = mod | (shift << 2); |
321 | } | |
322 | ||
323 | /* scale factor multiply for layer 1 */ | |
324 | for(i=0;i<15;i++) { | |
325 | int n, norm; | |
326 | n = i + 2; | |
0c1a9eda | 327 | norm = ((int64_t_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); |
81552334 FB |
328 | scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); |
329 | scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); | |
330 | scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); | |
239c2f4c FB |
331 | dprintf("%d: norm=%x s=%x %x %x\n", |
332 | i, norm, | |
333 | scale_factor_mult[i][0], | |
334 | scale_factor_mult[i][1], | |
335 | scale_factor_mult[i][2]); | |
336 | } | |
337 | ||
338 | /* window */ | |
339 | /* max = 18760, max sum over all 16 coefs : 44736 */ | |
340 | for(i=0;i<257;i++) { | |
341 | int v; | |
342 | v = mpa_enwindow[i]; | |
343 | #if WFRAC_BITS < 16 | |
344 | v = (v + (1 << (16 - WFRAC_BITS - 1))) >> (16 - WFRAC_BITS); | |
345 | #endif | |
346 | window[i] = v; | |
347 | if ((i & 63) != 0) | |
348 | v = -v; | |
349 | if (i != 0) | |
350 | window[512 - i] = v; | |
351 | } | |
352 | ||
353 | /* huffman decode tables */ | |
354 | huff_code_table[0] = NULL; | |
355 | for(i=1;i<16;i++) { | |
356 | const HuffTable *h = &mpa_huff_tables[i]; | |
5c91a675 ZK |
357 | int xsize, x, y; |
358 | unsigned int n; | |
0c1a9eda | 359 | uint8_t *code_table; |
239c2f4c FB |
360 | |
361 | xsize = h->xsize; | |
362 | n = xsize * xsize; | |
363 | /* XXX: fail test */ | |
364 | init_vlc(&huff_vlc[i], 8, n, | |
365 | h->bits, 1, 1, h->codes, 2, 2); | |
366 | ||
367 | code_table = av_mallocz(n); | |
368 | j = 0; | |
369 | for(x=0;x<xsize;x++) { | |
370 | for(y=0;y<xsize;y++) | |
371 | code_table[j++] = (x << 4) | y; | |
372 | } | |
373 | huff_code_table[i] = code_table; | |
374 | } | |
375 | for(i=0;i<2;i++) { | |
376 | init_vlc(&huff_quad_vlc[i], i == 0 ? 7 : 4, 16, | |
377 | mpa_quad_bits[i], 1, 1, mpa_quad_codes[i], 1, 1); | |
378 | } | |
379 | ||
380 | for(i=0;i<9;i++) { | |
381 | k = 0; | |
382 | for(j=0;j<22;j++) { | |
383 | band_index_long[i][j] = k; | |
384 | k += band_size_long[i][j]; | |
385 | } | |
386 | band_index_long[i][22] = k; | |
387 | } | |
388 | ||
855ea723 ZK |
389 | /* compute n ^ (4/3) and store it in mantissa/exp format */ |
390 | if (!av_mallocz_static(&table_4_3_exp, | |
391 | TABLE_4_3_SIZE * sizeof(table_4_3_exp[0]))) | |
392 | return -1; | |
393 | if (!av_mallocz_static(&table_4_3_value, | |
394 | TABLE_4_3_SIZE * sizeof(table_4_3_value[0]))) | |
239c2f4c | 395 | return -1; |
239c2f4c | 396 | |
f9ed4f88 | 397 | int_pow_init(); |
239c2f4c | 398 | for(i=1;i<TABLE_4_3_SIZE;i++) { |
239c2f4c | 399 | int e, m; |
f9ed4f88 | 400 | m = int_pow(i, &e); |
f9ed4f88 FB |
401 | #if 0 |
402 | /* test code */ | |
403 | { | |
404 | double f, fm; | |
405 | int e1, m1; | |
406 | f = pow((double)i, 4.0 / 3.0); | |
407 | fm = frexp(f, &e1); | |
408 | m1 = FIXR(2 * fm); | |
409 | #if FRAC_BITS <= 15 | |
59d3e367 FB |
410 | if ((unsigned short)m1 != m1) { |
411 | m1 = m1 >> 1; | |
412 | e1++; | |
413 | } | |
f9ed4f88 FB |
414 | #endif |
415 | e1--; | |
416 | if (m != m1 || e != e1) { | |
417 | printf("%4d: m=%x m1=%x e=%d e1=%d\n", | |
418 | i, m, m1, e, e1); | |
419 | } | |
420 | } | |
421 | #endif | |
239c2f4c FB |
422 | /* normalized to FRAC_BITS */ |
423 | table_4_3_value[i] = m; | |
59d3e367 | 424 | table_4_3_exp[i] = e; |
239c2f4c | 425 | } |
239c2f4c FB |
426 | |
427 | for(i=0;i<7;i++) { | |
428 | float f; | |
429 | int v; | |
430 | if (i != 6) { | |
431 | f = tan((double)i * M_PI / 12.0); | |
432 | v = FIXR(f / (1.0 + f)); | |
433 | } else { | |
434 | v = FIXR(1.0); | |
435 | } | |
436 | is_table[0][i] = v; | |
437 | is_table[1][6 - i] = v; | |
438 | } | |
439 | /* invalid values */ | |
440 | for(i=7;i<16;i++) | |
441 | is_table[0][i] = is_table[1][i] = 0.0; | |
442 | ||
443 | for(i=0;i<16;i++) { | |
444 | double f; | |
445 | int e, k; | |
446 | ||
447 | for(j=0;j<2;j++) { | |
448 | e = -(j + 1) * ((i + 1) >> 1); | |
449 | f = pow(2.0, e / 4.0); | |
450 | k = i & 1; | |
451 | is_table_lsf[j][k ^ 1][i] = FIXR(f); | |
452 | is_table_lsf[j][k][i] = FIXR(1.0); | |
453 | dprintf("is_table_lsf %d %d: %x %x\n", | |
454 | i, j, is_table_lsf[j][0][i], is_table_lsf[j][1][i]); | |
455 | } | |
456 | } | |
457 | ||
458 | for(i=0;i<8;i++) { | |
459 | float ci, cs, ca; | |
460 | ci = ci_table[i]; | |
461 | cs = 1.0 / sqrt(1.0 + ci * ci); | |
462 | ca = cs * ci; | |
463 | csa_table[i][0] = FIX(cs); | |
464 | csa_table[i][1] = FIX(ca); | |
465 | } | |
466 | ||
467 | /* compute mdct windows */ | |
468 | for(i=0;i<36;i++) { | |
469 | int v; | |
470 | v = FIXR(sin(M_PI * (i + 0.5) / 36.0)); | |
471 | mdct_win[0][i] = v; | |
472 | mdct_win[1][i] = v; | |
473 | mdct_win[3][i] = v; | |
474 | } | |
475 | for(i=0;i<6;i++) { | |
476 | mdct_win[1][18 + i] = FIXR(1.0); | |
477 | mdct_win[1][24 + i] = FIXR(sin(M_PI * ((i + 6) + 0.5) / 12.0)); | |
478 | mdct_win[1][30 + i] = FIXR(0.0); | |
479 | ||
480 | mdct_win[3][i] = FIXR(0.0); | |
481 | mdct_win[3][6 + i] = FIXR(sin(M_PI * (i + 0.5) / 12.0)); | |
482 | mdct_win[3][12 + i] = FIXR(1.0); | |
483 | } | |
484 | ||
485 | for(i=0;i<12;i++) | |
486 | mdct_win[2][i] = FIXR(sin(M_PI * (i + 0.5) / 12.0)); | |
487 | ||
488 | /* NOTE: we do frequency inversion adter the MDCT by changing | |
489 | the sign of the right window coefs */ | |
490 | for(j=0;j<4;j++) { | |
491 | for(i=0;i<36;i+=2) { | |
492 | mdct_win[j + 4][i] = mdct_win[j][i]; | |
493 | mdct_win[j + 4][i + 1] = -mdct_win[j][i + 1]; | |
494 | } | |
495 | } | |
496 | ||
497 | #if defined(DEBUG) | |
498 | for(j=0;j<8;j++) { | |
499 | printf("win%d=\n", j); | |
500 | for(i=0;i<36;i++) | |
501 | printf("%f, ", (double)mdct_win[j][i] / FRAC_ONE); | |
502 | printf("\n"); | |
503 | } | |
504 | #endif | |
de6d9b64 | 505 | init = 1; |
de6d9b64 FB |
506 | } |
507 | ||
508 | s->inbuf_index = 0; | |
509 | s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE]; | |
510 | s->inbuf_ptr = s->inbuf; | |
239c2f4c FB |
511 | #ifdef DEBUG |
512 | s->frame_count = 0; | |
513 | #endif | |
de6d9b64 FB |
514 | return 0; |
515 | } | |
516 | ||
ef9f7306 | 517 | /* tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) */ |
239c2f4c FB |
518 | |
519 | /* cos(i*pi/64) */ | |
520 | ||
521 | #define COS0_0 FIXR(0.50060299823519630134) | |
522 | #define COS0_1 FIXR(0.50547095989754365998) | |
523 | #define COS0_2 FIXR(0.51544730992262454697) | |
524 | #define COS0_3 FIXR(0.53104259108978417447) | |
525 | #define COS0_4 FIXR(0.55310389603444452782) | |
526 | #define COS0_5 FIXR(0.58293496820613387367) | |
527 | #define COS0_6 FIXR(0.62250412303566481615) | |
528 | #define COS0_7 FIXR(0.67480834145500574602) | |
529 | #define COS0_8 FIXR(0.74453627100229844977) | |
530 | #define COS0_9 FIXR(0.83934964541552703873) | |
531 | #define COS0_10 FIXR(0.97256823786196069369) | |
532 | #define COS0_11 FIXR(1.16943993343288495515) | |
533 | #define COS0_12 FIXR(1.48416461631416627724) | |
534 | #define COS0_13 FIXR(2.05778100995341155085) | |
535 | #define COS0_14 FIXR(3.40760841846871878570) | |
536 | #define COS0_15 FIXR(10.19000812354805681150) | |
537 | ||
538 | #define COS1_0 FIXR(0.50241928618815570551) | |
539 | #define COS1_1 FIXR(0.52249861493968888062) | |
540 | #define COS1_2 FIXR(0.56694403481635770368) | |
541 | #define COS1_3 FIXR(0.64682178335999012954) | |
542 | #define COS1_4 FIXR(0.78815462345125022473) | |
543 | #define COS1_5 FIXR(1.06067768599034747134) | |
544 | #define COS1_6 FIXR(1.72244709823833392782) | |
545 | #define COS1_7 FIXR(5.10114861868916385802) | |
546 | ||
547 | #define COS2_0 FIXR(0.50979557910415916894) | |
548 | #define COS2_1 FIXR(0.60134488693504528054) | |
549 | #define COS2_2 FIXR(0.89997622313641570463) | |
550 | #define COS2_3 FIXR(2.56291544774150617881) | |
551 | ||
552 | #define COS3_0 FIXR(0.54119610014619698439) | |
553 | #define COS3_1 FIXR(1.30656296487637652785) | |
554 | ||
555 | #define COS4_0 FIXR(0.70710678118654752439) | |
556 | ||
557 | /* butterfly operator */ | |
558 | #define BF(a, b, c)\ | |
559 | {\ | |
560 | tmp0 = tab[a] + tab[b];\ | |
561 | tmp1 = tab[a] - tab[b];\ | |
562 | tab[a] = tmp0;\ | |
563 | tab[b] = MULL(tmp1, c);\ | |
564 | } | |
565 | ||
566 | #define BF1(a, b, c, d)\ | |
567 | {\ | |
568 | BF(a, b, COS4_0);\ | |
569 | BF(c, d, -COS4_0);\ | |
570 | tab[c] += tab[d];\ | |
571 | } | |
572 | ||
573 | #define BF2(a, b, c, d)\ | |
574 | {\ | |
575 | BF(a, b, COS4_0);\ | |
576 | BF(c, d, -COS4_0);\ | |
577 | tab[c] += tab[d];\ | |
578 | tab[a] += tab[c];\ | |
579 | tab[c] += tab[b];\ | |
580 | tab[b] += tab[d];\ | |
581 | } | |
582 | ||
583 | #define ADD(a, b) tab[a] += tab[b] | |
584 | ||
585 | /* DCT32 without 1/sqrt(2) coef zero scaling. */ | |
0c1a9eda | 586 | static void dct32(int32_t *out, int32_t *tab) |
239c2f4c FB |
587 | { |
588 | int tmp0, tmp1; | |
589 | ||
590 | /* pass 1 */ | |
591 | BF(0, 31, COS0_0); | |
592 | BF(1, 30, COS0_1); | |
593 | BF(2, 29, COS0_2); | |
594 | BF(3, 28, COS0_3); | |
595 | BF(4, 27, COS0_4); | |
596 | BF(5, 26, COS0_5); | |
597 | BF(6, 25, COS0_6); | |
598 | BF(7, 24, COS0_7); | |
599 | BF(8, 23, COS0_8); | |
600 | BF(9, 22, COS0_9); | |
601 | BF(10, 21, COS0_10); | |
602 | BF(11, 20, COS0_11); | |
603 | BF(12, 19, COS0_12); | |
604 | BF(13, 18, COS0_13); | |
605 | BF(14, 17, COS0_14); | |
606 | BF(15, 16, COS0_15); | |
607 | ||
608 | /* pass 2 */ | |
609 | BF(0, 15, COS1_0); | |
610 | BF(1, 14, COS1_1); | |
611 | BF(2, 13, COS1_2); | |
612 | BF(3, 12, COS1_3); | |
613 | BF(4, 11, COS1_4); | |
614 | BF(5, 10, COS1_5); | |
615 | BF(6, 9, COS1_6); | |
616 | BF(7, 8, COS1_7); | |
617 | ||
618 | BF(16, 31, -COS1_0); | |
619 | BF(17, 30, -COS1_1); | |
620 | BF(18, 29, -COS1_2); | |
621 | BF(19, 28, -COS1_3); | |
622 | BF(20, 27, -COS1_4); | |
623 | BF(21, 26, -COS1_5); | |
624 | BF(22, 25, -COS1_6); | |
625 | BF(23, 24, -COS1_7); | |
626 | ||
627 | /* pass 3 */ | |
628 | BF(0, 7, COS2_0); | |
629 | BF(1, 6, COS2_1); | |
630 | BF(2, 5, COS2_2); | |
631 | BF(3, 4, COS2_3); | |
632 | ||
633 | BF(8, 15, -COS2_0); | |
634 | BF(9, 14, -COS2_1); | |
635 | BF(10, 13, -COS2_2); | |
636 | BF(11, 12, -COS2_3); | |
637 | ||
638 | BF(16, 23, COS2_0); | |
639 | BF(17, 22, COS2_1); | |
640 | BF(18, 21, COS2_2); | |
641 | BF(19, 20, COS2_3); | |
642 | ||
643 | BF(24, 31, -COS2_0); | |
644 | BF(25, 30, -COS2_1); | |
645 | BF(26, 29, -COS2_2); | |
646 | BF(27, 28, -COS2_3); | |
647 | ||
648 | /* pass 4 */ | |
649 | BF(0, 3, COS3_0); | |
650 | BF(1, 2, COS3_1); | |
651 | ||
652 | BF(4, 7, -COS3_0); | |
653 | BF(5, 6, -COS3_1); | |
654 | ||
655 | BF(8, 11, COS3_0); | |
656 | BF(9, 10, COS3_1); | |
657 | ||
658 | BF(12, 15, -COS3_0); | |
659 | BF(13, 14, -COS3_1); | |
660 | ||
661 | BF(16, 19, COS3_0); | |
662 | BF(17, 18, COS3_1); | |
663 | ||
664 | BF(20, 23, -COS3_0); | |
665 | BF(21, 22, -COS3_1); | |
666 | ||
667 | BF(24, 27, COS3_0); | |
668 | BF(25, 26, COS3_1); | |
669 | ||
670 | BF(28, 31, -COS3_0); | |
671 | BF(29, 30, -COS3_1); | |
672 | ||
673 | /* pass 5 */ | |
674 | BF1(0, 1, 2, 3); | |
675 | BF2(4, 5, 6, 7); | |
676 | BF1(8, 9, 10, 11); | |
677 | BF2(12, 13, 14, 15); | |
678 | BF1(16, 17, 18, 19); | |
679 | BF2(20, 21, 22, 23); | |
680 | BF1(24, 25, 26, 27); | |
681 | BF2(28, 29, 30, 31); | |
682 | ||
683 | /* pass 6 */ | |
684 | ||
685 | ADD( 8, 12); | |
686 | ADD(12, 10); | |
687 | ADD(10, 14); | |
688 | ADD(14, 9); | |
689 | ADD( 9, 13); | |
690 | ADD(13, 11); | |
691 | ADD(11, 15); | |
692 | ||
693 | out[ 0] = tab[0]; | |
694 | out[16] = tab[1]; | |
695 | out[ 8] = tab[2]; | |
696 | out[24] = tab[3]; | |
697 | out[ 4] = tab[4]; | |
698 | out[20] = tab[5]; | |
699 | out[12] = tab[6]; | |
700 | out[28] = tab[7]; | |
701 | out[ 2] = tab[8]; | |
702 | out[18] = tab[9]; | |
703 | out[10] = tab[10]; | |
704 | out[26] = tab[11]; | |
705 | out[ 6] = tab[12]; | |
706 | out[22] = tab[13]; | |
707 | out[14] = tab[14]; | |
708 | out[30] = tab[15]; | |
709 | ||
710 | ADD(24, 28); | |
711 | ADD(28, 26); | |
712 | ADD(26, 30); | |
713 | ADD(30, 25); | |
714 | ADD(25, 29); | |
715 | ADD(29, 27); | |
716 | ADD(27, 31); | |
717 | ||
718 | out[ 1] = tab[16] + tab[24]; | |
719 | out[17] = tab[17] + tab[25]; | |
720 | out[ 9] = tab[18] + tab[26]; | |
721 | out[25] = tab[19] + tab[27]; | |
722 | out[ 5] = tab[20] + tab[28]; | |
723 | out[21] = tab[21] + tab[29]; | |
724 | out[13] = tab[22] + tab[30]; | |
725 | out[29] = tab[23] + tab[31]; | |
726 | out[ 3] = tab[24] + tab[20]; | |
727 | out[19] = tab[25] + tab[21]; | |
728 | out[11] = tab[26] + tab[22]; | |
729 | out[27] = tab[27] + tab[23]; | |
730 | out[ 7] = tab[28] + tab[18]; | |
731 | out[23] = tab[29] + tab[19]; | |
732 | out[15] = tab[30] + tab[17]; | |
733 | out[31] = tab[31]; | |
734 | } | |
735 | ||
736 | #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15) | |
737 | ||
738 | #if FRAC_BITS <= 15 | |
739 | ||
8c5b5683 FB |
740 | static inline int round_sample(int sum) |
741 | { | |
742 | int sum1; | |
743 | sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT; | |
744 | if (sum1 < -32768) | |
745 | sum1 = -32768; | |
746 | else if (sum1 > 32767) | |
747 | sum1 = 32767; | |
748 | return sum1; | |
239c2f4c FB |
749 | } |
750 | ||
8c5b5683 FB |
751 | #if defined(ARCH_POWERPC_405) |
752 | ||
753 | /* signed 16x16 -> 32 multiply add accumulate */ | |
754 | #define MACS(rt, ra, rb) \ | |
755 | asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb)); | |
756 | ||
757 | /* signed 16x16 -> 32 multiply */ | |
758 | #define MULS(ra, rb) \ | |
759 | ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; }) | |
239c2f4c FB |
760 | |
761 | #else | |
762 | ||
8c5b5683 FB |
763 | /* signed 16x16 -> 32 multiply add accumulate */ |
764 | #define MACS(rt, ra, rb) rt += (ra) * (rb) | |
765 | ||
766 | /* signed 16x16 -> 32 multiply */ | |
767 | #define MULS(ra, rb) ((ra) * (rb)) | |
768 | ||
769 | #endif | |
770 | ||
771 | #else | |
772 | ||
773 | static inline int round_sample(int64_t sum) | |
774 | { | |
775 | int sum1; | |
776 | sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT); | |
777 | if (sum1 < -32768) | |
778 | sum1 = -32768; | |
779 | else if (sum1 > 32767) | |
780 | sum1 = 32767; | |
781 | return sum1; | |
239c2f4c FB |
782 | } |
783 | ||
8c5b5683 FB |
784 | #define MULS(ra, rb) MUL64(ra, rb) |
785 | ||
786 | #endif | |
787 | ||
788 | #define SUM8(sum, op, w, p) \ | |
239c2f4c | 789 | { \ |
8c5b5683 FB |
790 | sum op MULS((w)[0 * 64], p[0 * 64]);\ |
791 | sum op MULS((w)[1 * 64], p[1 * 64]);\ | |
792 | sum op MULS((w)[2 * 64], p[2 * 64]);\ | |
793 | sum op MULS((w)[3 * 64], p[3 * 64]);\ | |
794 | sum op MULS((w)[4 * 64], p[4 * 64]);\ | |
795 | sum op MULS((w)[5 * 64], p[5 * 64]);\ | |
796 | sum op MULS((w)[6 * 64], p[6 * 64]);\ | |
797 | sum op MULS((w)[7 * 64], p[7 * 64]);\ | |
798 | } | |
799 | ||
800 | #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \ | |
801 | { \ | |
802 | int tmp;\ | |
803 | tmp = p[0 * 64];\ | |
804 | sum1 op1 MULS((w1)[0 * 64], tmp);\ | |
805 | sum2 op2 MULS((w2)[0 * 64], tmp);\ | |
806 | tmp = p[1 * 64];\ | |
807 | sum1 op1 MULS((w1)[1 * 64], tmp);\ | |
808 | sum2 op2 MULS((w2)[1 * 64], tmp);\ | |
809 | tmp = p[2 * 64];\ | |
810 | sum1 op1 MULS((w1)[2 * 64], tmp);\ | |
811 | sum2 op2 MULS((w2)[2 * 64], tmp);\ | |
812 | tmp = p[3 * 64];\ | |
813 | sum1 op1 MULS((w1)[3 * 64], tmp);\ | |
814 | sum2 op2 MULS((w2)[3 * 64], tmp);\ | |
815 | tmp = p[4 * 64];\ | |
816 | sum1 op1 MULS((w1)[4 * 64], tmp);\ | |
817 | sum2 op2 MULS((w2)[4 * 64], tmp);\ | |
818 | tmp = p[5 * 64];\ | |
819 | sum1 op1 MULS((w1)[5 * 64], tmp);\ | |
820 | sum2 op2 MULS((w2)[5 * 64], tmp);\ | |
821 | tmp = p[6 * 64];\ | |
822 | sum1 op1 MULS((w1)[6 * 64], tmp);\ | |
823 | sum2 op2 MULS((w2)[6 * 64], tmp);\ | |
824 | tmp = p[7 * 64];\ | |
825 | sum1 op1 MULS((w1)[7 * 64], tmp);\ | |
826 | sum2 op2 MULS((w2)[7 * 64], tmp);\ | |
239c2f4c FB |
827 | } |
828 | ||
239c2f4c FB |
829 | |
830 | /* 32 sub band synthesis filter. Input: 32 sub band samples, Output: | |
831 | 32 samples. */ | |
832 | /* XXX: optimize by avoiding ring buffer usage */ | |
833 | static void synth_filter(MPADecodeContext *s1, | |
0c1a9eda ZK |
834 | int ch, int16_t *samples, int incr, |
835 | int32_t sb_samples[SBLIMIT]) | |
239c2f4c | 836 | { |
0c1a9eda | 837 | int32_t tmp[32]; |
8c5b5683 FB |
838 | register MPA_INT *synth_buf; |
839 | const register MPA_INT *w, *w2, *p; | |
239c2f4c | 840 | int j, offset, v; |
8c5b5683 | 841 | int16_t *samples2; |
239c2f4c | 842 | #if FRAC_BITS <= 15 |
8c5b5683 | 843 | int sum, sum2; |
239c2f4c | 844 | #else |
8c5b5683 | 845 | int64_t sum, sum2; |
239c2f4c | 846 | #endif |
8c5b5683 | 847 | |
239c2f4c FB |
848 | dct32(tmp, sb_samples); |
849 | ||
850 | offset = s1->synth_buf_offset[ch]; | |
851 | synth_buf = s1->synth_buf[ch] + offset; | |
852 | ||
853 | for(j=0;j<32;j++) { | |
854 | v = tmp[j]; | |
855 | #if FRAC_BITS <= 15 | |
81552334 FB |
856 | /* NOTE: can cause a loss in precision if very high amplitude |
857 | sound */ | |
239c2f4c FB |
858 | if (v > 32767) |
859 | v = 32767; | |
860 | else if (v < -32768) | |
861 | v = -32768; | |
862 | #endif | |
863 | synth_buf[j] = v; | |
864 | } | |
865 | /* copy to avoid wrap */ | |
866 | memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); | |
867 | ||
8c5b5683 | 868 | samples2 = samples + 31 * incr; |
239c2f4c | 869 | w = window; |
8c5b5683 FB |
870 | w2 = window + 31; |
871 | ||
239c2f4c | 872 | sum = 0; |
8c5b5683 FB |
873 | p = synth_buf + 16; |
874 | SUM8(sum, +=, w, p); | |
875 | p = synth_buf + 48; | |
876 | SUM8(sum, -=, w + 32, p); | |
877 | *samples = round_sample(sum); | |
878 | samples += incr; | |
239c2f4c FB |
879 | w++; |
880 | ||
8c5b5683 FB |
881 | /* we calculate two samples at the same time to avoid one memory |
882 | access per two sample */ | |
883 | for(j=1;j<16;j++) { | |
239c2f4c | 884 | sum = 0; |
8c5b5683 FB |
885 | sum2 = 0; |
886 | p = synth_buf + 16 + j; | |
887 | SUM8P2(sum, +=, sum2, -=, w, w2, p); | |
888 | p = synth_buf + 48 - j; | |
889 | SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p); | |
890 | ||
891 | *samples = round_sample(sum); | |
892 | samples += incr; | |
893 | *samples2 = round_sample(sum2); | |
894 | samples2 -= incr; | |
239c2f4c | 895 | w++; |
8c5b5683 | 896 | w2--; |
239c2f4c | 897 | } |
8c5b5683 FB |
898 | |
899 | p = synth_buf + 32; | |
900 | sum = 0; | |
901 | SUM8(sum, -=, w + 32, p); | |
902 | *samples = round_sample(sum); | |
903 | ||
239c2f4c FB |
904 | offset = (offset - 32) & 511; |
905 | s1->synth_buf_offset[ch] = offset; | |
906 | } | |
907 | ||
908 | /* cos(pi*i/24) */ | |
909 | #define C1 FIXR(0.99144486137381041114) | |
910 | #define C3 FIXR(0.92387953251128675612) | |
911 | #define C5 FIXR(0.79335334029123516458) | |
912 | #define C7 FIXR(0.60876142900872063941) | |
913 | #define C9 FIXR(0.38268343236508977173) | |
914 | #define C11 FIXR(0.13052619222005159154) | |
915 | ||
916 | /* 12 points IMDCT. We compute it "by hand" by factorizing obvious | |
917 | cases. */ | |
918 | static void imdct12(int *out, int *in) | |
919 | { | |
920 | int tmp; | |
0c1a9eda | 921 | int64_t in1_3, in1_9, in4_3, in4_9; |
239c2f4c FB |
922 | |
923 | in1_3 = MUL64(in[1], C3); | |
924 | in1_9 = MUL64(in[1], C9); | |
925 | in4_3 = MUL64(in[4], C3); | |
926 | in4_9 = MUL64(in[4], C9); | |
927 | ||
928 | tmp = FRAC_RND(MUL64(in[0], C7) - in1_3 - MUL64(in[2], C11) + | |
929 | MUL64(in[3], C1) - in4_9 - MUL64(in[5], C5)); | |
930 | out[0] = tmp; | |
931 | out[5] = -tmp; | |
932 | tmp = FRAC_RND(MUL64(in[0] - in[3], C9) - in1_3 + | |
933 | MUL64(in[2] + in[5], C3) - in4_9); | |
934 | out[1] = tmp; | |
935 | out[4] = -tmp; | |
936 | tmp = FRAC_RND(MUL64(in[0], C11) - in1_9 + MUL64(in[2], C7) - | |
937 | MUL64(in[3], C5) + in4_3 - MUL64(in[5], C1)); | |
938 | out[2] = tmp; | |
939 | out[3] = -tmp; | |
940 | tmp = FRAC_RND(MUL64(-in[0], C5) + in1_9 + MUL64(in[2], C1) + | |
941 | MUL64(in[3], C11) - in4_3 - MUL64(in[5], C7)); | |
942 | out[6] = tmp; | |
943 | out[11] = tmp; | |
944 | tmp = FRAC_RND(MUL64(-in[0] + in[3], C3) - in1_9 + | |
945 | MUL64(in[2] + in[5], C9) + in4_3); | |
946 | out[7] = tmp; | |
947 | out[10] = tmp; | |
948 | tmp = FRAC_RND(-MUL64(in[0], C1) - in1_3 - MUL64(in[2], C5) - | |
949 | MUL64(in[3], C7) - in4_9 - MUL64(in[5], C11)); | |
950 | out[8] = tmp; | |
951 | out[9] = tmp; | |
952 | } | |
953 | ||
954 | #undef C1 | |
955 | #undef C3 | |
956 | #undef C5 | |
957 | #undef C7 | |
958 | #undef C9 | |
959 | #undef C11 | |
960 | ||
961 | /* cos(pi*i/18) */ | |
962 | #define C1 FIXR(0.98480775301220805936) | |
963 | #define C2 FIXR(0.93969262078590838405) | |
964 | #define C3 FIXR(0.86602540378443864676) | |
965 | #define C4 FIXR(0.76604444311897803520) | |
966 | #define C5 FIXR(0.64278760968653932632) | |
967 | #define C6 FIXR(0.5) | |
968 | #define C7 FIXR(0.34202014332566873304) | |
969 | #define C8 FIXR(0.17364817766693034885) | |
970 | ||
971 | /* 0.5 / cos(pi*(2*i+1)/36) */ | |
972 | static const int icos36[9] = { | |
973 | FIXR(0.50190991877167369479), | |
974 | FIXR(0.51763809020504152469), | |
975 | FIXR(0.55168895948124587824), | |
976 | FIXR(0.61038729438072803416), | |
977 | FIXR(0.70710678118654752439), | |
978 | FIXR(0.87172339781054900991), | |
979 | FIXR(1.18310079157624925896), | |
980 | FIXR(1.93185165257813657349), | |
981 | FIXR(5.73685662283492756461), | |
982 | }; | |
983 | ||
984 | static const int icos72[18] = { | |
985 | /* 0.5 / cos(pi*(2*i+19)/72) */ | |
986 | FIXR(0.74009361646113053152), | |
987 | FIXR(0.82133981585229078570), | |
988 | FIXR(0.93057949835178895673), | |
989 | FIXR(1.08284028510010010928), | |
990 | FIXR(1.30656296487637652785), | |
991 | FIXR(1.66275476171152078719), | |
992 | FIXR(2.31011315767264929558), | |
993 | FIXR(3.83064878777019433457), | |
994 | FIXR(11.46279281302667383546), | |
995 | ||
996 | /* 0.5 / cos(pi*(2*(i + 18) +19)/72) */ | |
997 | FIXR(-0.67817085245462840086), | |
998 | FIXR(-0.63023620700513223342), | |
999 | FIXR(-0.59284452371708034528), | |
1000 | FIXR(-0.56369097343317117734), | |
1001 | FIXR(-0.54119610014619698439), | |
1002 | FIXR(-0.52426456257040533932), | |
1003 | FIXR(-0.51213975715725461845), | |
1004 | FIXR(-0.50431448029007636036), | |
1005 | FIXR(-0.50047634258165998492), | |
1006 | }; | |
1007 | ||
1008 | /* using Lee like decomposition followed by hand coded 9 points DCT */ | |
1009 | static void imdct36(int *out, int *in) | |
1010 | { | |
1011 | int i, j, t0, t1, t2, t3, s0, s1, s2, s3; | |
1012 | int tmp[18], *tmp1, *in1; | |
0c1a9eda | 1013 | int64_t in3_3, in6_6; |
239c2f4c FB |
1014 | |
1015 | for(i=17;i>=1;i--) | |
1016 | in[i] += in[i-1]; | |
1017 | for(i=17;i>=3;i-=2) | |
1018 | in[i] += in[i-2]; | |
1019 | ||
1020 | for(j=0;j<2;j++) { | |
1021 | tmp1 = tmp + j; | |
1022 | in1 = in + j; | |
1023 | ||
1024 | in3_3 = MUL64(in1[2*3], C3); | |
1025 | in6_6 = MUL64(in1[2*6], C6); | |
1026 | ||
1027 | tmp1[0] = FRAC_RND(MUL64(in1[2*1], C1) + in3_3 + | |
1028 | MUL64(in1[2*5], C5) + MUL64(in1[2*7], C7)); | |
1029 | tmp1[2] = in1[2*0] + FRAC_RND(MUL64(in1[2*2], C2) + | |
1030 | MUL64(in1[2*4], C4) + in6_6 + | |
1031 | MUL64(in1[2*8], C8)); | |
1032 | tmp1[4] = FRAC_RND(MUL64(in1[2*1] - in1[2*5] - in1[2*7], C3)); | |
1033 | tmp1[6] = FRAC_RND(MUL64(in1[2*2] - in1[2*4] - in1[2*8], C6)) - | |
1034 | in1[2*6] + in1[2*0]; | |
1035 | tmp1[8] = FRAC_RND(MUL64(in1[2*1], C5) - in3_3 - | |
1036 | MUL64(in1[2*5], C7) + MUL64(in1[2*7], C1)); | |
1037 | tmp1[10] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C8) - | |
1038 | MUL64(in1[2*4], C2) + in6_6 + | |
1039 | MUL64(in1[2*8], C4)); | |
1040 | tmp1[12] = FRAC_RND(MUL64(in1[2*1], C7) - in3_3 + | |
1041 | MUL64(in1[2*5], C1) - | |
1042 | MUL64(in1[2*7], C5)); | |
1043 | tmp1[14] = in1[2*0] + FRAC_RND(MUL64(-in1[2*2], C4) + | |
1044 | MUL64(in1[2*4], C8) + in6_6 - | |
1045 | MUL64(in1[2*8], C2)); | |
1046 | tmp1[16] = in1[2*0] - in1[2*2] + in1[2*4] - in1[2*6] + in1[2*8]; | |
1047 | } | |
1048 | ||
1049 | i = 0; | |
1050 | for(j=0;j<4;j++) { | |
1051 | t0 = tmp[i]; | |
1052 | t1 = tmp[i + 2]; | |
1053 | s0 = t1 + t0; | |
1054 | s2 = t1 - t0; | |
1055 | ||
1056 | t2 = tmp[i + 1]; | |
1057 | t3 = tmp[i + 3]; | |
1058 | s1 = MULL(t3 + t2, icos36[j]); | |
1059 | s3 = MULL(t3 - t2, icos36[8 - j]); | |
1060 | ||
1061 | t0 = MULL(s0 + s1, icos72[9 + 8 - j]); | |
1062 | t1 = MULL(s0 - s1, icos72[8 - j]); | |
1063 | out[18 + 9 + j] = t0; | |
1064 | out[18 + 8 - j] = t0; | |
1065 | out[9 + j] = -t1; | |
1066 | out[8 - j] = t1; | |
1067 | ||
1068 | t0 = MULL(s2 + s3, icos72[9+j]); | |
1069 | t1 = MULL(s2 - s3, icos72[j]); | |
1070 | out[18 + 9 + (8 - j)] = t0; | |
1071 | out[18 + j] = t0; | |
1072 | out[9 + (8 - j)] = -t1; | |
1073 | out[j] = t1; | |
1074 | i += 4; | |
1075 | } | |
1076 | ||
1077 | s0 = tmp[16]; | |
1078 | s1 = MULL(tmp[17], icos36[4]); | |
1079 | t0 = MULL(s0 + s1, icos72[9 + 4]); | |
1080 | t1 = MULL(s0 - s1, icos72[4]); | |
1081 | out[18 + 9 + 4] = t0; | |
1082 | out[18 + 8 - 4] = t0; | |
1083 | out[9 + 4] = -t1; | |
1084 | out[8 - 4] = t1; | |
1085 | } | |
1086 | ||
de6d9b64 | 1087 | /* fast header check for resync */ |
0c1a9eda | 1088 | static int check_header(uint32_t header) |
de6d9b64 FB |
1089 | { |
1090 | /* header */ | |
1091 | if ((header & 0xffe00000) != 0xffe00000) | |
1092 | return -1; | |
1093 | /* layer check */ | |
1094 | if (((header >> 17) & 3) == 0) | |
1095 | return -1; | |
239c2f4c FB |
1096 | /* bit rate */ |
1097 | if (((header >> 12) & 0xf) == 0xf) | |
de6d9b64 FB |
1098 | return -1; |
1099 | /* frequency */ | |
1100 | if (((header >> 10) & 3) == 3) | |
1101 | return -1; | |
1102 | return 0; | |
1103 | } | |
1104 | ||
239c2f4c FB |
1105 | /* header + layer + bitrate + freq + lsf/mpeg25 */ |
1106 | #define SAME_HEADER_MASK \ | |
1107 | (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19)) | |
1108 | ||
de6d9b64 | 1109 | /* header decoding. MUST check the header before because no |
239c2f4c FB |
1110 | consistency check is done there. Return 1 if free format found and |
1111 | that the frame size must be computed externally */ | |
0c1a9eda | 1112 | static int decode_header(MPADecodeContext *s, uint32_t header) |
de6d9b64 | 1113 | { |
239c2f4c FB |
1114 | int sample_rate, frame_size, mpeg25, padding; |
1115 | int sample_rate_index, bitrate_index; | |
de6d9b64 | 1116 | if (header & (1<<20)) { |
239c2f4c FB |
1117 | s->lsf = (header & (1<<19)) ? 0 : 1; |
1118 | mpeg25 = 0; | |
de6d9b64 | 1119 | } else { |
239c2f4c FB |
1120 | s->lsf = 1; |
1121 | mpeg25 = 1; | |
de6d9b64 FB |
1122 | } |
1123 | ||
1124 | s->layer = 4 - ((header >> 17) & 3); | |
1125 | /* extract frequency */ | |
239c2f4c FB |
1126 | sample_rate_index = (header >> 10) & 3; |
1127 | sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); | |
1128 | sample_rate_index += 3 * (s->lsf + mpeg25); | |
1129 | s->sample_rate_index = sample_rate_index; | |
1130 | s->error_protection = ((header >> 16) & 1) ^ 1; | |
81552334 | 1131 | s->sample_rate = sample_rate; |
de6d9b64 | 1132 | |
239c2f4c FB |
1133 | bitrate_index = (header >> 12) & 0xf; |
1134 | padding = (header >> 9) & 1; | |
1135 | //extension = (header >> 8) & 1; | |
1136 | s->mode = (header >> 6) & 3; | |
1137 | s->mode_ext = (header >> 4) & 3; | |
1138 | //copyright = (header >> 3) & 1; | |
1139 | //original = (header >> 2) & 1; | |
1140 | //emphasis = header & 3; | |
de6d9b64 | 1141 | |
239c2f4c FB |
1142 | if (s->mode == MPA_MONO) |
1143 | s->nb_channels = 1; | |
1144 | else | |
1145 | s->nb_channels = 2; | |
de6d9b64 | 1146 | |
239c2f4c FB |
1147 | if (bitrate_index != 0) { |
1148 | frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index]; | |
1149 | s->bit_rate = frame_size * 1000; | |
1150 | switch(s->layer) { | |
1151 | case 1: | |
1152 | frame_size = (frame_size * 12000) / sample_rate; | |
1153 | frame_size = (frame_size + padding) * 4; | |
1154 | break; | |
1155 | case 2: | |
1156 | frame_size = (frame_size * 144000) / sample_rate; | |
1157 | frame_size += padding; | |
1158 | break; | |
1159 | default: | |
1160 | case 3: | |
1161 | frame_size = (frame_size * 144000) / (sample_rate << s->lsf); | |
1162 | frame_size += padding; | |
1163 | break; | |
1164 | } | |
1165 | s->frame_size = frame_size; | |
1166 | } else { | |
1167 | /* if no frame size computed, signal it */ | |
1168 | if (!s->free_format_frame_size) | |
1169 | return 1; | |
1170 | /* free format: compute bitrate and real frame size from the | |
1171 | frame size we extracted by reading the bitstream */ | |
1172 | s->frame_size = s->free_format_frame_size; | |
1173 | switch(s->layer) { | |
1174 | case 1: | |
1175 | s->frame_size += padding * 4; | |
1176 | s->bit_rate = (s->frame_size * sample_rate) / 48000; | |
1177 | break; | |
1178 | case 2: | |
1179 | s->frame_size += padding; | |
1180 | s->bit_rate = (s->frame_size * sample_rate) / 144000; | |
1181 | break; | |
1182 | default: | |
1183 | case 3: | |
1184 | s->frame_size += padding; | |
1185 | s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000; | |
1186 | break; | |
1187 | } | |
de6d9b64 | 1188 | } |
239c2f4c | 1189 | |
fad9f495 | 1190 | #if defined(DEBUG) |
239c2f4c FB |
1191 | printf("layer%d, %d Hz, %d kbits/s, ", |
1192 | s->layer, s->sample_rate, s->bit_rate); | |
1193 | if (s->nb_channels == 2) { | |
1194 | if (s->layer == 3) { | |
1195 | if (s->mode_ext & MODE_EXT_MS_STEREO) | |
1196 | printf("ms-"); | |
1197 | if (s->mode_ext & MODE_EXT_I_STEREO) | |
1198 | printf("i-"); | |
1199 | } | |
1200 | printf("stereo"); | |
1201 | } else { | |
1202 | printf("mono"); | |
1203 | } | |
1204 | printf("\n"); | |
de6d9b64 | 1205 | #endif |
239c2f4c | 1206 | return 0; |
de6d9b64 FB |
1207 | } |
1208 | ||
8c5b5683 | 1209 | /* useful helper to get mpeg audio stream infos. Return -1 if error in |
962d6ae6 FB |
1210 | header, otherwise the coded frame size in bytes */ |
1211 | int mpa_decode_header(AVCodecContext *avctx, uint32_t head) | |
8c5b5683 FB |
1212 | { |
1213 | MPADecodeContext s1, *s = &s1; | |
8c5b5683 FB |
1214 | |
1215 | if (check_header(head) != 0) | |
1216 | return -1; | |
1217 | ||
1218 | if (decode_header(s, head) != 0) { | |
1219 | return -1; | |
1220 | } | |
1221 | ||
1222 | switch(s->layer) { | |
1223 | case 1: | |
962d6ae6 | 1224 | avctx->frame_size = 384; |
8c5b5683 FB |
1225 | break; |
1226 | case 2: | |
962d6ae6 | 1227 | avctx->frame_size = 1152; |
8c5b5683 FB |
1228 | break; |
1229 | default: | |
1230 | case 3: | |
1231 | if (s->lsf) | |
962d6ae6 | 1232 | avctx->frame_size = 576; |
8c5b5683 | 1233 | else |
962d6ae6 | 1234 | avctx->frame_size = 1152; |
8c5b5683 FB |
1235 | break; |
1236 | } | |
1237 | ||
962d6ae6 FB |
1238 | avctx->sample_rate = s->sample_rate; |
1239 | avctx->channels = s->nb_channels; | |
1240 | avctx->bit_rate = s->bit_rate; | |
1241 | avctx->sub_id = s->layer; | |
1242 | return s->frame_size; | |
8c5b5683 FB |
1243 | } |
1244 | ||
239c2f4c FB |
1245 | /* return the number of decoded frames */ |
1246 | static int mp_decode_layer1(MPADecodeContext *s) | |
de6d9b64 | 1247 | { |
239c2f4c | 1248 | int bound, i, v, n, ch, j, mant; |
0c1a9eda ZK |
1249 | uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; |
1250 | uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; | |
239c2f4c FB |
1251 | |
1252 | if (s->mode == MPA_JSTEREO) | |
1253 | bound = (s->mode_ext + 1) * 4; | |
1254 | else | |
1255 | bound = SBLIMIT; | |
1256 | ||
1257 | /* allocation bits */ | |
1258 | for(i=0;i<bound;i++) { | |
1259 | for(ch=0;ch<s->nb_channels;ch++) { | |
1260 | allocation[ch][i] = get_bits(&s->gb, 4); | |
1261 | } | |
1262 | } | |
1263 | for(i=bound;i<SBLIMIT;i++) { | |
1264 | allocation[0][i] = get_bits(&s->gb, 4); | |
1265 | } | |
1266 | ||
1267 | /* scale factors */ | |
1268 | for(i=0;i<bound;i++) { | |
1269 | for(ch=0;ch<s->nb_channels;ch++) { | |
1270 | if (allocation[ch][i]) | |
1271 | scale_factors[ch][i] = get_bits(&s->gb, 6); | |
1272 | } | |
1273 | } | |
1274 | for(i=bound;i<SBLIMIT;i++) { | |
1275 | if (allocation[0][i]) { | |
1276 | scale_factors[0][i] = get_bits(&s->gb, 6); | |
1277 | scale_factors[1][i] = get_bits(&s->gb, 6); | |
1278 | } | |
1279 | } | |
de6d9b64 | 1280 | |
239c2f4c FB |
1281 | /* compute samples */ |
1282 | for(j=0;j<12;j++) { | |
1283 | for(i=0;i<bound;i++) { | |
1284 | for(ch=0;ch<s->nb_channels;ch++) { | |
1285 | n = allocation[ch][i]; | |
1286 | if (n) { | |
1287 | mant = get_bits(&s->gb, n + 1); | |
1288 | v = l1_unscale(n, mant, scale_factors[ch][i]); | |
1289 | } else { | |
1290 | v = 0; | |
1291 | } | |
1292 | s->sb_samples[ch][j][i] = v; | |
1293 | } | |
1294 | } | |
1295 | for(i=bound;i<SBLIMIT;i++) { | |
1296 | n = allocation[0][i]; | |
1297 | if (n) { | |
1298 | mant = get_bits(&s->gb, n + 1); | |
1299 | v = l1_unscale(n, mant, scale_factors[0][i]); | |
1300 | s->sb_samples[0][j][i] = v; | |
1301 | v = l1_unscale(n, mant, scale_factors[1][i]); | |
1302 | s->sb_samples[1][j][i] = v; | |
1303 | } else { | |
1304 | s->sb_samples[0][j][i] = 0; | |
1305 | s->sb_samples[1][j][i] = 0; | |
1306 | } | |
1307 | } | |
1308 | } | |
1309 | return 12; | |
1310 | } | |
1311 | ||
1312 | /* bitrate is in kb/s */ | |
1313 | int l2_select_table(int bitrate, int nb_channels, int freq, int lsf) | |
1314 | { | |
1315 | int ch_bitrate, table; | |
de6d9b64 | 1316 | |
239c2f4c FB |
1317 | ch_bitrate = bitrate / nb_channels; |
1318 | if (!lsf) { | |
1319 | if ((freq == 48000 && ch_bitrate >= 56) || | |
1320 | (ch_bitrate >= 56 && ch_bitrate <= 80)) | |
1321 | table = 0; | |
1322 | else if (freq != 48000 && ch_bitrate >= 96) | |
1323 | table = 1; | |
1324 | else if (freq != 32000 && ch_bitrate <= 48) | |
1325 | table = 2; | |
1326 | else | |
1327 | table = 3; | |
1328 | } else { | |
1329 | table = 4; | |
1330 | } | |
1331 | return table; | |
1332 | } | |
de6d9b64 | 1333 | |
239c2f4c FB |
1334 | static int mp_decode_layer2(MPADecodeContext *s) |
1335 | { | |
1336 | int sblimit; /* number of used subbands */ | |
1337 | const unsigned char *alloc_table; | |
1338 | int table, bit_alloc_bits, i, j, ch, bound, v; | |
1339 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; | |
1340 | unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; | |
1341 | unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; | |
1342 | int scale, qindex, bits, steps, k, l, m, b; | |
de6d9b64 | 1343 | |
239c2f4c FB |
1344 | /* select decoding table */ |
1345 | table = l2_select_table(s->bit_rate / 1000, s->nb_channels, | |
1346 | s->sample_rate, s->lsf); | |
1347 | sblimit = sblimit_table[table]; | |
1348 | alloc_table = alloc_tables[table]; | |
1349 | ||
1350 | if (s->mode == MPA_JSTEREO) | |
1351 | bound = (s->mode_ext + 1) * 4; | |
1352 | else | |
1353 | bound = sblimit; | |
1354 | ||
1355 | dprintf("bound=%d sblimit=%d\n", bound, sblimit); | |
1356 | /* parse bit allocation */ | |
1357 | j = 0; | |
1358 | for(i=0;i<bound;i++) { | |
1359 | bit_alloc_bits = alloc_table[j]; | |
1360 | for(ch=0;ch<s->nb_channels;ch++) { | |
1361 | bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits); | |
1362 | } | |
1363 | j += 1 << bit_alloc_bits; | |
1364 | } | |
1365 | for(i=bound;i<sblimit;i++) { | |
1366 | bit_alloc_bits = alloc_table[j]; | |
1367 | v = get_bits(&s->gb, bit_alloc_bits); | |
1368 | bit_alloc[0][i] = v; | |
1369 | bit_alloc[1][i] = v; | |
1370 | j += 1 << bit_alloc_bits; | |
de6d9b64 | 1371 | } |
239c2f4c FB |
1372 | |
1373 | #ifdef DEBUG | |
1374 | { | |
1375 | for(ch=0;ch<s->nb_channels;ch++) { | |
1376 | for(i=0;i<sblimit;i++) | |
1377 | printf(" %d", bit_alloc[ch][i]); | |
1378 | printf("\n"); | |
1379 | } | |
1380 | } | |
1381 | #endif | |
1382 | ||
1383 | /* scale codes */ | |
1384 | for(i=0;i<sblimit;i++) { | |
1385 | for(ch=0;ch<s->nb_channels;ch++) { | |
1386 | if (bit_alloc[ch][i]) | |
1387 | scale_code[ch][i] = get_bits(&s->gb, 2); | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | /* scale factors */ | |
1392 | for(i=0;i<sblimit;i++) { | |
1393 | for(ch=0;ch<s->nb_channels;ch++) { | |
1394 | if (bit_alloc[ch][i]) { | |
1395 | sf = scale_factors[ch][i]; | |
1396 | switch(scale_code[ch][i]) { | |
1397 | default: | |
1398 | case 0: | |
1399 | sf[0] = get_bits(&s->gb, 6); | |
1400 | sf[1] = get_bits(&s->gb, 6); | |
1401 | sf[2] = get_bits(&s->gb, 6); | |
1402 | break; | |
1403 | case 2: | |
1404 | sf[0] = get_bits(&s->gb, 6); | |
1405 | sf[1] = sf[0]; | |
1406 | sf[2] = sf[0]; | |
1407 | break; | |
1408 | case 1: | |
1409 | sf[0] = get_bits(&s->gb, 6); | |
1410 | sf[2] = get_bits(&s->gb, 6); | |
1411 | sf[1] = sf[0]; | |
1412 | break; | |
1413 | case 3: | |
1414 | sf[0] = get_bits(&s->gb, 6); | |
1415 | sf[2] = get_bits(&s->gb, 6); | |
1416 | sf[1] = sf[2]; | |
1417 | break; | |
1418 | } | |
1419 | } | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | #ifdef DEBUG | |
1424 | for(ch=0;ch<s->nb_channels;ch++) { | |
1425 | for(i=0;i<sblimit;i++) { | |
1426 | if (bit_alloc[ch][i]) { | |
1427 | sf = scale_factors[ch][i]; | |
1428 | printf(" %d %d %d", sf[0], sf[1], sf[2]); | |
1429 | } else { | |
1430 | printf(" -"); | |
1431 | } | |
1432 | } | |
1433 | printf("\n"); | |
1434 | } | |
1435 | #endif | |
1436 | ||
1437 | /* samples */ | |
1438 | for(k=0;k<3;k++) { | |
1439 | for(l=0;l<12;l+=3) { | |
1440 | j = 0; | |
1441 | for(i=0;i<bound;i++) { | |
1442 | bit_alloc_bits = alloc_table[j]; | |
1443 | for(ch=0;ch<s->nb_channels;ch++) { | |
1444 | b = bit_alloc[ch][i]; | |
1445 | if (b) { | |
1446 | scale = scale_factors[ch][i][k]; | |
1447 | qindex = alloc_table[j+b]; | |
1448 | bits = quant_bits[qindex]; | |
1449 | if (bits < 0) { | |
1450 | /* 3 values at the same time */ | |
1451 | v = get_bits(&s->gb, -bits); | |
1452 | steps = quant_steps[qindex]; | |
1453 | s->sb_samples[ch][k * 12 + l + 0][i] = | |
1454 | l2_unscale_group(steps, v % steps, scale); | |
1455 | v = v / steps; | |
1456 | s->sb_samples[ch][k * 12 + l + 1][i] = | |
1457 | l2_unscale_group(steps, v % steps, scale); | |
1458 | v = v / steps; | |
1459 | s->sb_samples[ch][k * 12 + l + 2][i] = | |
1460 | l2_unscale_group(steps, v, scale); | |
1461 | } else { | |
1462 | for(m=0;m<3;m++) { | |
1463 | v = get_bits(&s->gb, bits); | |
1464 | v = l1_unscale(bits - 1, v, scale); | |
1465 | s->sb_samples[ch][k * 12 + l + m][i] = v; | |
1466 | } | |
1467 | } | |
1468 | } else { | |
1469 | s->sb_samples[ch][k * 12 + l + 0][i] = 0; | |
1470 | s->sb_samples[ch][k * 12 + l + 1][i] = 0; | |
1471 | s->sb_samples[ch][k * 12 + l + 2][i] = 0; | |
1472 | } | |
1473 | } | |
1474 | /* next subband in alloc table */ | |
1475 | j += 1 << bit_alloc_bits; | |
1476 | } | |
1477 | /* XXX: find a way to avoid this duplication of code */ | |
1478 | for(i=bound;i<sblimit;i++) { | |
1479 | bit_alloc_bits = alloc_table[j]; | |
1480 | b = bit_alloc[0][i]; | |
1481 | if (b) { | |
1482 | int mant, scale0, scale1; | |
1483 | scale0 = scale_factors[0][i][k]; | |
1484 | scale1 = scale_factors[1][i][k]; | |
1485 | qindex = alloc_table[j+b]; | |
1486 | bits = quant_bits[qindex]; | |
1487 | if (bits < 0) { | |
1488 | /* 3 values at the same time */ | |
1489 | v = get_bits(&s->gb, -bits); | |
1490 | steps = quant_steps[qindex]; | |
1491 | mant = v % steps; | |
1492 | v = v / steps; | |
1493 | s->sb_samples[0][k * 12 + l + 0][i] = | |
1494 | l2_unscale_group(steps, mant, scale0); | |
1495 | s->sb_samples[1][k * 12 + l + 0][i] = | |
1496 | l2_unscale_group(steps, mant, scale1); | |
1497 | mant = v % steps; | |
1498 | v = v / steps; | |
1499 | s->sb_samples[0][k * 12 + l + 1][i] = | |
1500 | l2_unscale_group(steps, mant, scale0); | |
1501 | s->sb_samples[1][k * 12 + l + 1][i] = | |
1502 | l2_unscale_group(steps, mant, scale1); | |
1503 | s->sb_samples[0][k * 12 + l + 2][i] = | |
1504 | l2_unscale_group(steps, v, scale0); | |
1505 | s->sb_samples[1][k * 12 + l + 2][i] = | |
1506 | l2_unscale_group(steps, v, scale1); | |
1507 | } else { | |
1508 | for(m=0;m<3;m++) { | |
1509 | mant = get_bits(&s->gb, bits); | |
1510 | s->sb_samples[0][k * 12 + l + m][i] = | |
1511 | l1_unscale(bits - 1, mant, scale0); | |
1512 | s->sb_samples[1][k * 12 + l + m][i] = | |
1513 | l1_unscale(bits - 1, mant, scale1); | |
1514 | } | |
1515 | } | |
1516 | } else { | |
1517 | s->sb_samples[0][k * 12 + l + 0][i] = 0; | |
1518 | s->sb_samples[0][k * 12 + l + 1][i] = 0; | |
1519 | s->sb_samples[0][k * 12 + l + 2][i] = 0; | |
1520 | s->sb_samples[1][k * 12 + l + 0][i] = 0; | |
1521 | s->sb_samples[1][k * 12 + l + 1][i] = 0; | |
1522 | s->sb_samples[1][k * 12 + l + 2][i] = 0; | |
1523 | } | |
1524 | /* next subband in alloc table */ | |
1525 | j += 1 << bit_alloc_bits; | |
1526 | } | |
1527 | /* fill remaining samples to zero */ | |
1528 | for(i=sblimit;i<SBLIMIT;i++) { | |
1529 | for(ch=0;ch<s->nb_channels;ch++) { | |
1530 | s->sb_samples[ch][k * 12 + l + 0][i] = 0; | |
1531 | s->sb_samples[ch][k * 12 + l + 1][i] = 0; | |
1532 | s->sb_samples[ch][k * 12 + l + 2][i] = 0; | |
1533 | } | |
1534 | } | |
1535 | } | |
1536 | } | |
1537 | return 3 * 12; | |
de6d9b64 FB |
1538 | } |
1539 | ||
1540 | /* | |
239c2f4c | 1541 | * Seek back in the stream for backstep bytes (at most 511 bytes) |
de6d9b64 | 1542 | */ |
5c91a675 | 1543 | static void seek_to_maindata(MPADecodeContext *s, unsigned int backstep) |
de6d9b64 | 1544 | { |
0c1a9eda | 1545 | uint8_t *ptr; |
de6d9b64 FB |
1546 | |
1547 | /* compute current position in stream */ | |
228ef9dd | 1548 | ptr = (uint8_t *)(s->gb.buffer + (get_bits_count(&s->gb)>>3)); |
8db1a1dd | 1549 | |
de6d9b64 FB |
1550 | /* copy old data before current one */ |
1551 | ptr -= backstep; | |
239c2f4c FB |
1552 | memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + |
1553 | BACKSTEP_SIZE + s->old_frame_size - backstep, backstep); | |
de6d9b64 | 1554 | /* init get bits again */ |
68f593b4 | 1555 | init_get_bits(&s->gb, ptr, (s->frame_size + backstep)*8); |
de6d9b64 | 1556 | |
239c2f4c FB |
1557 | /* prepare next buffer */ |
1558 | s->inbuf_index ^= 1; | |
1559 | s->inbuf = &s->inbuf1[s->inbuf_index][BACKSTEP_SIZE]; | |
1560 | s->old_frame_size = s->frame_size; | |
1561 | } | |
1562 | ||
1563 | static inline void lsf_sf_expand(int *slen, | |
1564 | int sf, int n1, int n2, int n3) | |
1565 | { | |
1566 | if (n3) { | |
1567 | slen[3] = sf % n3; | |
1568 | sf /= n3; | |
1569 | } else { | |
1570 | slen[3] = 0; | |
1571 | } | |
1572 | if (n2) { | |
1573 | slen[2] = sf % n2; | |
1574 | sf /= n2; | |
1575 | } else { | |
1576 | slen[2] = 0; | |
1577 | } | |
1578 | slen[1] = sf % n1; | |
1579 | sf /= n1; | |
1580 | slen[0] = sf; | |
1581 | } | |
1582 | ||
1583 | static void exponents_from_scale_factors(MPADecodeContext *s, | |
1584 | GranuleDef *g, | |
0c1a9eda | 1585 | int16_t *exponents) |
239c2f4c | 1586 | { |
0c1a9eda | 1587 | const uint8_t *bstab, *pretab; |
239c2f4c | 1588 | int len, i, j, k, l, v0, shift, gain, gains[3]; |
0c1a9eda | 1589 | int16_t *exp_ptr; |
239c2f4c FB |
1590 | |
1591 | exp_ptr = exponents; | |
1592 | gain = g->global_gain - 210; | |
1593 | shift = g->scalefac_scale + 1; | |
1594 | ||
1595 | bstab = band_size_long[s->sample_rate_index]; | |
1596 | pretab = mpa_pretab[g->preflag]; | |
1597 | for(i=0;i<g->long_end;i++) { | |
1598 | v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift); | |
1599 | len = bstab[i]; | |
1600 | for(j=len;j>0;j--) | |
1601 | *exp_ptr++ = v0; | |
1602 | } | |
1603 | ||
1604 | if (g->short_start < 13) { | |
1605 | bstab = band_size_short[s->sample_rate_index]; | |
1606 | gains[0] = gain - (g->subblock_gain[0] << 3); | |
1607 | gains[1] = gain - (g->subblock_gain[1] << 3); | |
1608 | gains[2] = gain - (g->subblock_gain[2] << 3); | |
1609 | k = g->long_end; | |
1610 | for(i=g->short_start;i<13;i++) { | |
1611 | len = bstab[i]; | |
1612 | for(l=0;l<3;l++) { | |
1613 | v0 = gains[l] - (g->scale_factors[k++] << shift); | |
1614 | for(j=len;j>0;j--) | |
1615 | *exp_ptr++ = v0; | |
1616 | } | |
1617 | } | |
1618 | } | |
1619 | } | |
1620 | ||
1621 | /* handle n = 0 too */ | |
1622 | static inline int get_bitsz(GetBitContext *s, int n) | |
1623 | { | |
1624 | if (n == 0) | |
1625 | return 0; | |
1626 | else | |
1627 | return get_bits(s, n); | |
1628 | } | |
1629 | ||
1630 | static int huffman_decode(MPADecodeContext *s, GranuleDef *g, | |
0c1a9eda | 1631 | int16_t *exponents, int end_pos) |
239c2f4c FB |
1632 | { |
1633 | int s_index; | |
1634 | int linbits, code, x, y, l, v, i, j, k, pos; | |
8db1a1dd | 1635 | GetBitContext last_gb; |
239c2f4c | 1636 | VLC *vlc; |
0c1a9eda | 1637 | uint8_t *code_table; |
239c2f4c FB |
1638 | |
1639 | /* low frequencies (called big values) */ | |
1640 | s_index = 0; | |
1641 | for(i=0;i<3;i++) { | |
1642 | j = g->region_size[i]; | |
1643 | if (j == 0) | |
1644 | continue; | |
1645 | /* select vlc table */ | |
1646 | k = g->table_select[i]; | |
1647 | l = mpa_huff_data[k][0]; | |
1648 | linbits = mpa_huff_data[k][1]; | |
1649 | vlc = &huff_vlc[l]; | |
1650 | code_table = huff_code_table[l]; | |
1651 | ||
1652 | /* read huffcode and compute each couple */ | |
1653 | for(;j>0;j--) { | |
1654 | if (get_bits_count(&s->gb) >= end_pos) | |
1655 | break; | |
1656 | if (code_table) { | |
1657 | code = get_vlc(&s->gb, vlc); | |
1658 | if (code < 0) | |
1659 | return -1; | |
1660 | y = code_table[code]; | |
1661 | x = y >> 4; | |
1662 | y = y & 0x0f; | |
1663 | } else { | |
1664 | x = 0; | |
1665 | y = 0; | |
1666 | } | |
1667 | dprintf("region=%d n=%d x=%d y=%d exp=%d\n", | |
1668 | i, g->region_size[i] - j, x, y, exponents[s_index]); | |
1669 | if (x) { | |
1670 | if (x == 15) | |
1671 | x += get_bitsz(&s->gb, linbits); | |
1672 | v = l3_unscale(x, exponents[s_index]); | |
1673 | if (get_bits1(&s->gb)) | |
1674 | v = -v; | |
1675 | } else { | |
1676 | v = 0; | |
1677 | } | |
1678 | g->sb_hybrid[s_index++] = v; | |
1679 | if (y) { | |
1680 | if (y == 15) | |
1681 | y += get_bitsz(&s->gb, linbits); | |
1682 | v = l3_unscale(y, exponents[s_index]); | |
1683 | if (get_bits1(&s->gb)) | |
1684 | v = -v; | |
1685 | } else { | |
1686 | v = 0; | |
1687 | } | |
1688 | g->sb_hybrid[s_index++] = v; | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | /* high frequencies */ | |
1693 | vlc = &huff_quad_vlc[g->count1table_select]; | |
8db1a1dd | 1694 | last_gb.buffer = NULL; |
239c2f4c FB |
1695 | while (s_index <= 572) { |
1696 | pos = get_bits_count(&s->gb); | |
1697 | if (pos >= end_pos) { | |
8db1a1dd | 1698 | if (pos > end_pos && last_gb.buffer != NULL) { |
239c2f4c FB |
1699 | /* some encoders generate an incorrect size for this |
1700 | part. We must go back into the data */ | |
1701 | s_index -= 4; | |
8db1a1dd | 1702 | s->gb = last_gb; |
239c2f4c FB |
1703 | } |
1704 | break; | |
1705 | } | |
8db1a1dd MN |
1706 | last_gb= s->gb; |
1707 | ||
239c2f4c FB |
1708 | code = get_vlc(&s->gb, vlc); |
1709 | dprintf("t=%d code=%d\n", g->count1table_select, code); | |
1710 | if (code < 0) | |
1711 | return -1; | |
1712 | for(i=0;i<4;i++) { | |
1713 | if (code & (8 >> i)) { | |
1714 | /* non zero value. Could use a hand coded function for | |
1715 | 'one' value */ | |
1716 | v = l3_unscale(1, exponents[s_index]); | |
1717 | if(get_bits1(&s->gb)) | |
1718 | v = -v; | |
1719 | } else { | |
1720 | v = 0; | |
1721 | } | |
1722 | g->sb_hybrid[s_index++] = v; | |
1723 | } | |
1724 | } | |
1725 | while (s_index < 576) | |
1726 | g->sb_hybrid[s_index++] = 0; | |
de6d9b64 FB |
1727 | return 0; |
1728 | } | |
1729 | ||
239c2f4c FB |
1730 | /* Reorder short blocks from bitstream order to interleaved order. It |
1731 | would be faster to do it in parsing, but the code would be far more | |
1732 | complicated */ | |
1733 | static void reorder_block(MPADecodeContext *s, GranuleDef *g) | |
1734 | { | |
1735 | int i, j, k, len; | |
0c1a9eda ZK |
1736 | int32_t *ptr, *dst, *ptr1; |
1737 | int32_t tmp[576]; | |
239c2f4c FB |
1738 | |
1739 | if (g->block_type != 2) | |
1740 | return; | |
1741 | ||
1742 | if (g->switch_point) { | |
1743 | if (s->sample_rate_index != 8) { | |
1744 | ptr = g->sb_hybrid + 36; | |
1745 | } else { | |
1746 | ptr = g->sb_hybrid + 48; | |
1747 | } | |
1748 | } else { | |
1749 | ptr = g->sb_hybrid; | |
1750 | } | |
1751 | ||
1752 | for(i=g->short_start;i<13;i++) { | |
1753 | len = band_size_short[s->sample_rate_index][i]; | |
1754 | ptr1 = ptr; | |
1755 | for(k=0;k<3;k++) { | |
1756 | dst = tmp + k; | |
1757 | for(j=len;j>0;j--) { | |
1758 | *dst = *ptr++; | |
1759 | dst += 3; | |
1760 | } | |
1761 | } | |
0c1a9eda | 1762 | memcpy(ptr1, tmp, len * 3 * sizeof(int32_t)); |
239c2f4c FB |
1763 | } |
1764 | } | |
1765 | ||
1766 | #define ISQRT2 FIXR(0.70710678118654752440) | |
1767 | ||
1768 | static void compute_stereo(MPADecodeContext *s, | |
1769 | GranuleDef *g0, GranuleDef *g1) | |
1770 | { | |
1771 | int i, j, k, l; | |
0c1a9eda | 1772 | int32_t v1, v2; |
239c2f4c | 1773 | int sf_max, tmp0, tmp1, sf, len, non_zero_found; |
0c1a9eda ZK |
1774 | int32_t (*is_tab)[16]; |
1775 | int32_t *tab0, *tab1; | |
239c2f4c FB |
1776 | int non_zero_found_short[3]; |
1777 | ||
1778 | /* intensity stereo */ | |
1779 | if (s->mode_ext & MODE_EXT_I_STEREO) { | |
1780 | if (!s->lsf) { | |
1781 | is_tab = is_table; | |
1782 | sf_max = 7; | |
1783 | } else { | |
1784 | is_tab = is_table_lsf[g1->scalefac_compress & 1]; | |
1785 | sf_max = 16; | |
1786 | } | |
1787 | ||
1788 | tab0 = g0->sb_hybrid + 576; | |
1789 | tab1 = g1->sb_hybrid + 576; | |
1790 | ||
1791 | non_zero_found_short[0] = 0; | |
1792 | non_zero_found_short[1] = 0; | |
1793 | non_zero_found_short[2] = 0; | |
1794 | k = (13 - g1->short_start) * 3 + g1->long_end - 3; | |
1795 | for(i = 12;i >= g1->short_start;i--) { | |
1796 | /* for last band, use previous scale factor */ | |
1797 | if (i != 11) | |
1798 | k -= 3; | |
1799 | len = band_size_short[s->sample_rate_index][i]; | |
1800 | for(l=2;l>=0;l--) { | |
1801 | tab0 -= len; | |
1802 | tab1 -= len; | |
1803 | if (!non_zero_found_short[l]) { | |
1804 | /* test if non zero band. if so, stop doing i-stereo */ | |
1805 | for(j=0;j<len;j++) { | |
1806 | if (tab1[j] != 0) { | |
1807 | non_zero_found_short[l] = 1; | |
1808 | goto found1; | |
1809 | } | |
1810 | } | |
1811 | sf = g1->scale_factors[k + l]; | |
1812 | if (sf >= sf_max) | |
1813 | goto found1; | |
1814 | ||
1815 | v1 = is_tab[0][sf]; | |
1816 | v2 = is_tab[1][sf]; | |
1817 | for(j=0;j<len;j++) { | |
1818 | tmp0 = tab0[j]; | |
1819 | tab0[j] = MULL(tmp0, v1); | |
1820 | tab1[j] = MULL(tmp0, v2); | |
1821 | } | |
1822 | } else { | |
1823 | found1: | |
1824 | if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1825 | /* lower part of the spectrum : do ms stereo | |
1826 | if enabled */ | |
1827 | for(j=0;j<len;j++) { | |
1828 | tmp0 = tab0[j]; | |
1829 | tmp1 = tab1[j]; | |
1830 | tab0[j] = MULL(tmp0 + tmp1, ISQRT2); | |
1831 | tab1[j] = MULL(tmp0 - tmp1, ISQRT2); | |
1832 | } | |
1833 | } | |
1834 | } | |
1835 | } | |
1836 | } | |
1837 | ||
1838 | non_zero_found = non_zero_found_short[0] | | |
1839 | non_zero_found_short[1] | | |
1840 | non_zero_found_short[2]; | |
1841 | ||
1842 | for(i = g1->long_end - 1;i >= 0;i--) { | |
1843 | len = band_size_long[s->sample_rate_index][i]; | |
1844 | tab0 -= len; | |
1845 | tab1 -= len; | |
1846 | /* test if non zero band. if so, stop doing i-stereo */ | |
1847 | if (!non_zero_found) { | |
1848 | for(j=0;j<len;j++) { | |
1849 | if (tab1[j] != 0) { | |
1850 | non_zero_found = 1; | |
1851 | goto found2; | |
1852 | } | |
1853 | } | |
1854 | /* for last band, use previous scale factor */ | |
1855 | k = (i == 21) ? 20 : i; | |
1856 | sf = g1->scale_factors[k]; | |
1857 | if (sf >= sf_max) | |
1858 | goto found2; | |
1859 | v1 = is_tab[0][sf]; | |
1860 | v2 = is_tab[1][sf]; | |
1861 | for(j=0;j<len;j++) { | |
1862 | tmp0 = tab0[j]; | |
1863 | tab0[j] = MULL(tmp0, v1); | |
1864 | tab1[j] = MULL(tmp0, v2); | |
1865 | } | |
1866 | } else { | |
1867 | found2: | |
1868 | if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1869 | /* lower part of the spectrum : do ms stereo | |
1870 | if enabled */ | |
1871 | for(j=0;j<len;j++) { | |
1872 | tmp0 = tab0[j]; | |
1873 | tmp1 = tab1[j]; | |
1874 | tab0[j] = MULL(tmp0 + tmp1, ISQRT2); | |
1875 | tab1[j] = MULL(tmp0 - tmp1, ISQRT2); | |
1876 | } | |
1877 | } | |
1878 | } | |
1879 | } | |
1880 | } else if (s->mode_ext & MODE_EXT_MS_STEREO) { | |
1881 | /* ms stereo ONLY */ | |
1882 | /* NOTE: the 1/sqrt(2) normalization factor is included in the | |
1883 | global gain */ | |
1884 | tab0 = g0->sb_hybrid; | |
1885 | tab1 = g1->sb_hybrid; | |
1886 | for(i=0;i<576;i++) { | |
1887 | tmp0 = tab0[i]; | |
1888 | tmp1 = tab1[i]; | |
1889 | tab0[i] = tmp0 + tmp1; | |
1890 | tab1[i] = tmp0 - tmp1; | |
1891 | } | |
1892 | } | |
1893 | } | |
1894 | ||
1895 | static void compute_antialias(MPADecodeContext *s, | |
1896 | GranuleDef *g) | |
1897 | { | |
0c1a9eda | 1898 | int32_t *ptr, *p0, *p1, *csa; |
239c2f4c FB |
1899 | int n, tmp0, tmp1, i, j; |
1900 | ||
1901 | /* we antialias only "long" bands */ | |
1902 | if (g->block_type == 2) { | |
1903 | if (!g->switch_point) | |
1904 | return; | |
1905 | /* XXX: check this for 8000Hz case */ | |
1906 | n = 1; | |
1907 | } else { | |
1908 | n = SBLIMIT - 1; | |
1909 | } | |
1910 | ||
1911 | ptr = g->sb_hybrid + 18; | |
1912 | for(i = n;i > 0;i--) { | |
1913 | p0 = ptr - 1; | |
1914 | p1 = ptr; | |
1915 | csa = &csa_table[0][0]; | |
1916 | for(j=0;j<8;j++) { | |
1917 | tmp0 = *p0; | |
1918 | tmp1 = *p1; | |
1919 | *p0 = FRAC_RND(MUL64(tmp0, csa[0]) - MUL64(tmp1, csa[1])); | |
1920 | *p1 = FRAC_RND(MUL64(tmp0, csa[1]) + MUL64(tmp1, csa[0])); | |
1921 | p0--; | |
1922 | p1++; | |
1923 | csa += 2; | |
1924 | } | |
1925 | ptr += 18; | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | static void compute_imdct(MPADecodeContext *s, | |
1930 | GranuleDef *g, | |
0c1a9eda ZK |
1931 | int32_t *sb_samples, |
1932 | int32_t *mdct_buf) | |
239c2f4c | 1933 | { |
0c1a9eda ZK |
1934 | int32_t *ptr, *win, *win1, *buf, *buf2, *out_ptr, *ptr1; |
1935 | int32_t in[6]; | |
1936 | int32_t out[36]; | |
1937 | int32_t out2[12]; | |
239c2f4c FB |
1938 | int i, j, k, mdct_long_end, v, sblimit; |
1939 | ||
1940 | /* find last non zero block */ | |
1941 | ptr = g->sb_hybrid + 576; | |
1942 | ptr1 = g->sb_hybrid + 2 * 18; | |
1943 | while (ptr >= ptr1) { | |
1944 | ptr -= 6; | |
1945 | v = ptr[0] | ptr[1] | ptr[2] | ptr[3] | ptr[4] | ptr[5]; | |
1946 | if (v != 0) | |
1947 | break; | |
1948 | } | |
1949 | sblimit = ((ptr - g->sb_hybrid) / 18) + 1; | |
1950 | ||
1951 | if (g->block_type == 2) { | |
1952 | /* XXX: check for 8000 Hz */ | |
1953 | if (g->switch_point) | |
1954 | mdct_long_end = 2; | |
1955 | else | |
1956 | mdct_long_end = 0; | |
1957 | } else { | |
1958 | mdct_long_end = sblimit; | |
1959 | } | |
1960 | ||
1961 | buf = mdct_buf; | |
1962 | ptr = g->sb_hybrid; | |
1963 | for(j=0;j<mdct_long_end;j++) { | |
1964 | imdct36(out, ptr); | |
1965 | /* apply window & overlap with previous buffer */ | |
1966 | out_ptr = sb_samples + j; | |
1967 | /* select window */ | |
1968 | if (g->switch_point && j < 2) | |
1969 | win1 = mdct_win[0]; | |
1970 | else | |
1971 | win1 = mdct_win[g->block_type]; | |
1972 | /* select frequency inversion */ | |
1973 | win = win1 + ((4 * 36) & -(j & 1)); | |
1974 | for(i=0;i<18;i++) { | |
1975 | *out_ptr = MULL(out[i], win[i]) + buf[i]; | |
1976 | buf[i] = MULL(out[i + 18], win[i + 18]); | |
1977 | out_ptr += SBLIMIT; | |
1978 | } | |
1979 | ptr += 18; | |
1980 | buf += 18; | |
1981 | } | |
1982 | for(j=mdct_long_end;j<sblimit;j++) { | |
1983 | for(i=0;i<6;i++) { | |
1984 | out[i] = 0; | |
1985 | out[6 + i] = 0; | |
1986 | out[30+i] = 0; | |
1987 | } | |
1988 | /* select frequency inversion */ | |
1989 | win = mdct_win[2] + ((4 * 36) & -(j & 1)); | |
1990 | buf2 = out + 6; | |
1991 | for(k=0;k<3;k++) { | |
1992 | /* reorder input for short mdct */ | |
1993 | ptr1 = ptr + k; | |
1994 | for(i=0;i<6;i++) { | |
1995 | in[i] = *ptr1; | |
1996 | ptr1 += 3; | |
1997 | } | |
1998 | imdct12(out2, in); | |
1999 | /* apply 12 point window and do small overlap */ | |
2000 | for(i=0;i<6;i++) { | |
2001 | buf2[i] = MULL(out2[i], win[i]) + buf2[i]; | |
2002 | buf2[i + 6] = MULL(out2[i + 6], win[i + 6]); | |
2003 | } | |
2004 | buf2 += 6; | |
2005 | } | |
2006 | /* overlap */ | |
2007 | out_ptr = sb_samples + j; | |
2008 | for(i=0;i<18;i++) { | |
2009 | *out_ptr = out[i] + buf[i]; | |
2010 | buf[i] = out[i + 18]; | |
2011 | out_ptr += SBLIMIT; | |
2012 | } | |
2013 | ptr += 18; | |
2014 | buf += 18; | |
2015 | } | |
2016 | /* zero bands */ | |
2017 | for(j=sblimit;j<SBLIMIT;j++) { | |
2018 | /* overlap */ | |
2019 | out_ptr = sb_samples + j; | |
2020 | for(i=0;i<18;i++) { | |
2021 | *out_ptr = buf[i]; | |
2022 | buf[i] = 0; | |
2023 | out_ptr += SBLIMIT; | |
2024 | } | |
2025 | buf += 18; | |
2026 | } | |
2027 | } | |
2028 | ||
747a67fb | 2029 | #if defined(DEBUG) |
0c1a9eda | 2030 | void sample_dump(int fnum, int32_t *tab, int n) |
239c2f4c FB |
2031 | { |
2032 | static FILE *files[16], *f; | |
2033 | char buf[512]; | |
81552334 | 2034 | int i; |
0c1a9eda | 2035 | int32_t v; |
81552334 | 2036 | |
239c2f4c FB |
2037 | f = files[fnum]; |
2038 | if (!f) { | |
81552334 FB |
2039 | sprintf(buf, "/tmp/out%d.%s.pcm", |
2040 | fnum, | |
2041 | #ifdef USE_HIGHPRECISION | |
2042 | "hp" | |
2043 | #else | |
2044 | "lp" | |
2045 | #endif | |
2046 | ); | |
239c2f4c FB |
2047 | f = fopen(buf, "w"); |
2048 | if (!f) | |
2049 | return; | |
2050 | files[fnum] = f; | |
2051 | } | |
2052 | ||
2053 | if (fnum == 0) { | |
239c2f4c FB |
2054 | static int pos = 0; |
2055 | printf("pos=%d\n", pos); | |
2056 | for(i=0;i<n;i++) { | |
81552334 | 2057 | printf(" %0.4f", (double)tab[i] / FRAC_ONE); |
239c2f4c FB |
2058 | if ((i % 18) == 17) |
2059 | printf("\n"); | |
2060 | } | |
2061 | pos += n; | |
2062 | } | |
81552334 FB |
2063 | for(i=0;i<n;i++) { |
2064 | /* normalize to 23 frac bits */ | |
2065 | v = tab[i] << (23 - FRAC_BITS); | |
0c1a9eda | 2066 | fwrite(&v, 1, sizeof(int32_t), f); |
81552334 | 2067 | } |
239c2f4c FB |
2068 | } |
2069 | #endif | |
2070 | ||
2071 | ||
2072 | /* main layer3 decoding function */ | |
2073 | static int mp_decode_layer3(MPADecodeContext *s) | |
2074 | { | |
2075 | int nb_granules, main_data_begin, private_bits; | |
2076 | int gr, ch, blocksplit_flag, i, j, k, n, bits_pos, bits_left; | |
2077 | GranuleDef granules[2][2], *g; | |
0c1a9eda | 2078 | int16_t exponents[576]; |
239c2f4c FB |
2079 | |
2080 | /* read side info */ | |
2081 | if (s->lsf) { | |
2082 | main_data_begin = get_bits(&s->gb, 8); | |
2083 | if (s->nb_channels == 2) | |
2084 | private_bits = get_bits(&s->gb, 2); | |
2085 | else | |
2086 | private_bits = get_bits(&s->gb, 1); | |
2087 | nb_granules = 1; | |
2088 | } else { | |
2089 | main_data_begin = get_bits(&s->gb, 9); | |
2090 | if (s->nb_channels == 2) | |
2091 | private_bits = get_bits(&s->gb, 3); | |
2092 | else | |
2093 | private_bits = get_bits(&s->gb, 5); | |
2094 | nb_granules = 2; | |
2095 | for(ch=0;ch<s->nb_channels;ch++) { | |
2096 | granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ | |
2097 | granules[ch][1].scfsi = get_bits(&s->gb, 4); | |
2098 | } | |
2099 | } | |
2100 | ||
2101 | for(gr=0;gr<nb_granules;gr++) { | |
2102 | for(ch=0;ch<s->nb_channels;ch++) { | |
2103 | dprintf("gr=%d ch=%d: side_info\n", gr, ch); | |
2104 | g = &granules[ch][gr]; | |
2105 | g->part2_3_length = get_bits(&s->gb, 12); | |
2106 | g->big_values = get_bits(&s->gb, 9); | |
2107 | g->global_gain = get_bits(&s->gb, 8); | |
2108 | /* if MS stereo only is selected, we precompute the | |
2109 | 1/sqrt(2) renormalization factor */ | |
2110 | if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == | |
2111 | MODE_EXT_MS_STEREO) | |
2112 | g->global_gain -= 2; | |
2113 | if (s->lsf) | |
2114 | g->scalefac_compress = get_bits(&s->gb, 9); | |
2115 | else | |
2116 | g->scalefac_compress = get_bits(&s->gb, 4); | |
2117 | blocksplit_flag = get_bits(&s->gb, 1); | |
2118 | if (blocksplit_flag) { | |
2119 | g->block_type = get_bits(&s->gb, 2); | |
2120 | if (g->block_type == 0) | |
2121 | return -1; | |
2122 | g->switch_point = get_bits(&s->gb, 1); | |
2123 | for(i=0;i<2;i++) | |
2124 | g->table_select[i] = get_bits(&s->gb, 5); | |
2125 | for(i=0;i<3;i++) | |
2126 | g->subblock_gain[i] = get_bits(&s->gb, 3); | |
2127 | /* compute huffman coded region sizes */ | |
2128 | if (g->block_type == 2) | |
2129 | g->region_size[0] = (36 / 2); | |
2130 | else { | |
2131 | if (s->sample_rate_index <= 2) | |
2132 | g->region_size[0] = (36 / 2); | |
2133 | else if (s->sample_rate_index != 8) | |
2134 | g->region_size[0] = (54 / 2); | |
2135 | else | |
2136 | g->region_size[0] = (108 / 2); | |
2137 | } | |
2138 | g->region_size[1] = (576 / 2); | |
2139 | } else { | |
2140 | int region_address1, region_address2, l; | |
2141 | g->block_type = 0; | |
2142 | g->switch_point = 0; | |
2143 | for(i=0;i<3;i++) | |
2144 | g->table_select[i] = get_bits(&s->gb, 5); | |
2145 | /* compute huffman coded region sizes */ | |
2146 | region_address1 = get_bits(&s->gb, 4); | |
2147 | region_address2 = get_bits(&s->gb, 3); | |
2148 | dprintf("region1=%d region2=%d\n", | |
2149 | region_address1, region_address2); | |
2150 | g->region_size[0] = | |
2151 | band_index_long[s->sample_rate_index][region_address1 + 1] >> 1; | |
2152 | l = region_address1 + region_address2 + 2; | |
2153 | /* should not overflow */ | |
2154 | if (l > 22) | |
2155 | l = 22; | |
2156 | g->region_size[1] = | |
2157 | band_index_long[s->sample_rate_index][l] >> 1; | |
2158 | } | |
2159 | /* convert region offsets to region sizes and truncate | |
2160 | size to big_values */ | |
2161 | g->region_size[2] = (576 / 2); | |
2162 | j = 0; | |
2163 | for(i=0;i<3;i++) { | |
2164 | k = g->region_size[i]; | |
2165 | if (k > g->big_values) | |
2166 | k = g->big_values; | |
2167 | g->region_size[i] = k - j; | |
2168 | j = k; | |
2169 | } | |
2170 | ||
2171 | /* compute band indexes */ | |
2172 | if (g->block_type == 2) { | |
2173 | if (g->switch_point) { | |
2174 | /* if switched mode, we handle the 36 first samples as | |
2175 | long blocks. For 8000Hz, we handle the 48 first | |
2176 | exponents as long blocks (XXX: check this!) */ | |
2177 | if (s->sample_rate_index <= 2) | |
2178 | g->long_end = 8; | |
2179 | else if (s->sample_rate_index != 8) | |
2180 | g->long_end = 6; | |
2181 | else | |
2182 | g->long_end = 4; /* 8000 Hz */ | |
2183 | ||
2184 | if (s->sample_rate_index != 8) | |
2185 | g->short_start = 3; | |
2186 | else | |
2187 | g->short_start = 2; | |
2188 | } else { | |
2189 | g->long_end = 0; | |
2190 | g->short_start = 0; | |
2191 | } | |
2192 | } else { | |
2193 | g->short_start = 13; | |
2194 | g->long_end = 22; | |
2195 | } | |
2196 | ||
2197 | g->preflag = 0; | |
2198 | if (!s->lsf) | |
2199 | g->preflag = get_bits(&s->gb, 1); | |
2200 | g->scalefac_scale = get_bits(&s->gb, 1); | |
2201 | g->count1table_select = get_bits(&s->gb, 1); | |
2202 | dprintf("block_type=%d switch_point=%d\n", | |
2203 | g->block_type, g->switch_point); | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | /* now we get bits from the main_data_begin offset */ | |
2208 | dprintf("seekback: %d\n", main_data_begin); | |
2209 | seek_to_maindata(s, main_data_begin); | |
2210 | ||
2211 | for(gr=0;gr<nb_granules;gr++) { | |
2212 | for(ch=0;ch<s->nb_channels;ch++) { | |
2213 | g = &granules[ch][gr]; | |
2214 | ||
2215 | bits_pos = get_bits_count(&s->gb); | |
2216 | ||
2217 | if (!s->lsf) { | |
0c1a9eda | 2218 | uint8_t *sc; |
239c2f4c FB |
2219 | int slen, slen1, slen2; |
2220 | ||
2221 | /* MPEG1 scale factors */ | |
2222 | slen1 = slen_table[0][g->scalefac_compress]; | |
2223 | slen2 = slen_table[1][g->scalefac_compress]; | |
2224 | dprintf("slen1=%d slen2=%d\n", slen1, slen2); | |
2225 | if (g->block_type == 2) { | |
2226 | n = g->switch_point ? 17 : 18; | |
2227 | j = 0; | |
2228 | for(i=0;i<n;i++) | |
2229 | g->scale_factors[j++] = get_bitsz(&s->gb, slen1); | |
2230 | for(i=0;i<18;i++) | |
2231 | g->scale_factors[j++] = get_bitsz(&s->gb, slen2); | |
2232 | for(i=0;i<3;i++) | |
2233 | g->scale_factors[j++] = 0; | |
2234 | } else { | |
2235 | sc = granules[ch][0].scale_factors; | |
2236 | j = 0; | |
2237 | for(k=0;k<4;k++) { | |
2238 | n = (k == 0 ? 6 : 5); | |
2239 | if ((g->scfsi & (0x8 >> k)) == 0) { | |
2240 | slen = (k < 2) ? slen1 : slen2; | |
2241 | for(i=0;i<n;i++) | |
2242 | g->scale_factors[j++] = get_bitsz(&s->gb, slen); | |
2243 | } else { | |
2244 | /* simply copy from last granule */ | |
2245 | for(i=0;i<n;i++) { | |
2246 | g->scale_factors[j] = sc[j]; | |
2247 | j++; | |
2248 | } | |
2249 | } | |
2250 | } | |
2251 | g->scale_factors[j++] = 0; | |
2252 | } | |
747a67fb | 2253 | #if defined(DEBUG) |
239c2f4c FB |
2254 | { |
2255 | printf("scfsi=%x gr=%d ch=%d scale_factors:\n", | |
2256 | g->scfsi, gr, ch); | |
2257 | for(i=0;i<j;i++) | |
2258 | printf(" %d", g->scale_factors[i]); | |
2259 | printf("\n"); | |
2260 | } | |
2261 | #endif | |
2262 | } else { | |
2263 | int tindex, tindex2, slen[4], sl, sf; | |
2264 | ||
2265 | /* LSF scale factors */ | |
2266 | if (g->block_type == 2) { | |
2267 | tindex = g->switch_point ? 2 : 1; | |
2268 | } else { | |
2269 | tindex = 0; | |
2270 | } | |
2271 | sf = g->scalefac_compress; | |
2272 | if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) { | |
2273 | /* intensity stereo case */ | |
2274 | sf >>= 1; | |
2275 | if (sf < 180) { | |
2276 | lsf_sf_expand(slen, sf, 6, 6, 0); | |
2277 | tindex2 = 3; | |
2278 | } else if (sf < 244) { | |
2279 | lsf_sf_expand(slen, sf - 180, 4, 4, 0); | |
2280 | tindex2 = 4; | |
2281 | } else { | |
2282 | lsf_sf_expand(slen, sf - 244, 3, 0, 0); | |
2283 | tindex2 = 5; | |
2284 | } | |
2285 | } else { | |
2286 | /* normal case */ | |
2287 | if (sf < 400) { | |
2288 | lsf_sf_expand(slen, sf, 5, 4, 4); | |
2289 | tindex2 = 0; | |
2290 | } else if (sf < 500) { | |
2291 | lsf_sf_expand(slen, sf - 400, 5, 4, 0); | |
2292 | tindex2 = 1; | |
2293 | } else { | |
2294 | lsf_sf_expand(slen, sf - 500, 3, 0, 0); | |
2295 | tindex2 = 2; | |
2296 | g->preflag = 1; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | j = 0; | |
2301 | for(k=0;k<4;k++) { | |
2302 | n = lsf_nsf_table[tindex2][tindex][k]; | |
2303 | sl = slen[k]; | |
2304 | for(i=0;i<n;i++) | |
2305 | g->scale_factors[j++] = get_bitsz(&s->gb, sl); | |
2306 | } | |
2307 | /* XXX: should compute exact size */ | |
2308 | for(;j<40;j++) | |
2309 | g->scale_factors[j] = 0; | |
747a67fb | 2310 | #if defined(DEBUG) |
239c2f4c FB |
2311 | { |
2312 | printf("gr=%d ch=%d scale_factors:\n", | |
2313 | gr, ch); | |
2314 | for(i=0;i<40;i++) | |
2315 | printf(" %d", g->scale_factors[i]); | |
2316 | printf("\n"); | |
2317 | } | |
2318 | #endif | |
2319 | } | |
2320 | ||
2321 | exponents_from_scale_factors(s, g, exponents); | |
2322 | ||
2323 | /* read Huffman coded residue */ | |
2324 | if (huffman_decode(s, g, exponents, | |
2325 | bits_pos + g->part2_3_length) < 0) | |
2326 | return -1; | |
747a67fb FB |
2327 | #if defined(DEBUG) |
2328 | sample_dump(0, g->sb_hybrid, 576); | |
239c2f4c FB |
2329 | #endif |
2330 | ||
2331 | /* skip extension bits */ | |
2332 | bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos); | |
2333 | if (bits_left < 0) { | |
2334 | dprintf("bits_left=%d\n", bits_left); | |
2335 | return -1; | |
2336 | } | |
2337 | while (bits_left >= 16) { | |
2338 | skip_bits(&s->gb, 16); | |
2339 | bits_left -= 16; | |
2340 | } | |
2341 | if (bits_left > 0) | |
2342 | skip_bits(&s->gb, bits_left); | |
2343 | } /* ch */ | |
2344 | ||
2345 | if (s->nb_channels == 2) | |
2346 | compute_stereo(s, &granules[0][gr], &granules[1][gr]); | |
2347 | ||
2348 | for(ch=0;ch<s->nb_channels;ch++) { | |
2349 | g = &granules[ch][gr]; | |
2350 | ||
2351 | reorder_block(s, g); | |
747a67fb | 2352 | #if defined(DEBUG) |
239c2f4c FB |
2353 | sample_dump(0, g->sb_hybrid, 576); |
2354 | #endif | |
2355 | compute_antialias(s, g); | |
81552334 | 2356 | #if defined(DEBUG) |
239c2f4c FB |
2357 | sample_dump(1, g->sb_hybrid, 576); |
2358 | #endif | |
2359 | compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); | |
81552334 | 2360 | #if defined(DEBUG) |
239c2f4c FB |
2361 | sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576); |
2362 | #endif | |
2363 | } | |
2364 | } /* gr */ | |
2365 | return nb_granules * 18; | |
2366 | } | |
2367 | ||
2368 | static int mp_decode_frame(MPADecodeContext *s, | |
2369 | short *samples) | |
2370 | { | |
2371 | int i, nb_frames, ch; | |
2372 | short *samples_ptr; | |
2373 | ||
2374 | init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, | |
68f593b4 | 2375 | (s->inbuf_ptr - s->inbuf - HEADER_SIZE)*8); |
239c2f4c FB |
2376 | |
2377 | /* skip error protection field */ | |
2378 | if (s->error_protection) | |
2379 | get_bits(&s->gb, 16); | |
2380 | ||
2381 | dprintf("frame %d:\n", s->frame_count); | |
2382 | switch(s->layer) { | |
2383 | case 1: | |
2384 | nb_frames = mp_decode_layer1(s); | |
2385 | break; | |
2386 | case 2: | |
2387 | nb_frames = mp_decode_layer2(s); | |
2388 | break; | |
2389 | case 3: | |
2390 | default: | |
2391 | nb_frames = mp_decode_layer3(s); | |
2392 | break; | |
2393 | } | |
2394 | #if defined(DEBUG) | |
2395 | for(i=0;i<nb_frames;i++) { | |
2396 | for(ch=0;ch<s->nb_channels;ch++) { | |
2397 | int j; | |
2398 | printf("%d-%d:", i, ch); | |
2399 | for(j=0;j<SBLIMIT;j++) | |
2400 | printf(" %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE); | |
2401 | printf("\n"); | |
2402 | } | |
2403 | } | |
2404 | #endif | |
2405 | /* apply the synthesis filter */ | |
2406 | for(ch=0;ch<s->nb_channels;ch++) { | |
2407 | samples_ptr = samples + ch; | |
2408 | for(i=0;i<nb_frames;i++) { | |
2409 | synth_filter(s, ch, samples_ptr, s->nb_channels, | |
2410 | s->sb_samples[ch][i]); | |
2411 | samples_ptr += 32 * s->nb_channels; | |
2412 | } | |
2413 | } | |
2414 | #ifdef DEBUG | |
2415 | s->frame_count++; | |
2416 | #endif | |
2417 | return nb_frames * 32 * sizeof(short) * s->nb_channels; | |
2418 | } | |
2419 | ||
de6d9b64 FB |
2420 | static int decode_frame(AVCodecContext * avctx, |
2421 | void *data, int *data_size, | |
0c1a9eda | 2422 | uint8_t * buf, int buf_size) |
de6d9b64 FB |
2423 | { |
2424 | MPADecodeContext *s = avctx->priv_data; | |
0c1a9eda ZK |
2425 | uint32_t header; |
2426 | uint8_t *buf_ptr; | |
de6d9b64 FB |
2427 | int len, out_size; |
2428 | short *out_samples = data; | |
2429 | ||
2430 | *data_size = 0; | |
2431 | buf_ptr = buf; | |
2432 | while (buf_size > 0) { | |
2433 | len = s->inbuf_ptr - s->inbuf; | |
2434 | if (s->frame_size == 0) { | |
239c2f4c FB |
2435 | /* special case for next header for first frame in free |
2436 | format case (XXX: find a simpler method) */ | |
2437 | if (s->free_format_next_header != 0) { | |
2438 | s->inbuf[0] = s->free_format_next_header >> 24; | |
2439 | s->inbuf[1] = s->free_format_next_header >> 16; | |
2440 | s->inbuf[2] = s->free_format_next_header >> 8; | |
2441 | s->inbuf[3] = s->free_format_next_header; | |
2442 | s->inbuf_ptr = s->inbuf + 4; | |
2443 | s->free_format_next_header = 0; | |
2444 | goto got_header; | |
2445 | } | |
2446 | /* no header seen : find one. We need at least HEADER_SIZE | |
2447 | bytes to parse it */ | |
de6d9b64 FB |
2448 | len = HEADER_SIZE - len; |
2449 | if (len > buf_size) | |
2450 | len = buf_size; | |
92d24f49 | 2451 | if (len > 0) { |
2d83f323 ZK |
2452 | memcpy(s->inbuf_ptr, buf_ptr, len); |
2453 | buf_ptr += len; | |
2454 | buf_size -= len; | |
c152c983 ZK |
2455 | s->inbuf_ptr += len; |
2456 | } | |
2457 | if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) { | |
239c2f4c | 2458 | got_header: |
de6d9b64 FB |
2459 | header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | |
2460 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
92d24f49 | 2461 | |
de6d9b64 FB |
2462 | if (check_header(header) < 0) { |
2463 | /* no sync found : move by one byte (inefficient, but simple!) */ | |
228ef9dd | 2464 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
de6d9b64 | 2465 | s->inbuf_ptr--; |
239c2f4c FB |
2466 | dprintf("skip %x\n", header); |
2467 | /* reset free format frame size to give a chance | |
2468 | to get a new bitrate */ | |
2469 | s->free_format_frame_size = 0; | |
de6d9b64 | 2470 | } else { |
239c2f4c | 2471 | if (decode_header(s, header) == 1) { |
81552334 | 2472 | /* free format: prepare to compute frame size */ |
2d83f323 | 2473 | s->frame_size = -1; |
239c2f4c | 2474 | } |
81552334 FB |
2475 | /* update codec info */ |
2476 | avctx->sample_rate = s->sample_rate; | |
2477 | avctx->channels = s->nb_channels; | |
2478 | avctx->bit_rate = s->bit_rate; | |
98ce5991 | 2479 | avctx->sub_id = s->layer; |
8c5b5683 FB |
2480 | switch(s->layer) { |
2481 | case 1: | |
2482 | avctx->frame_size = 384; | |
2483 | break; | |
2484 | case 2: | |
2485 | avctx->frame_size = 1152; | |
2486 | break; | |
2487 | case 3: | |
2488 | if (s->lsf) | |
2489 | avctx->frame_size = 576; | |
2490 | else | |
2491 | avctx->frame_size = 1152; | |
2492 | break; | |
2493 | } | |
de6d9b64 FB |
2494 | } |
2495 | } | |
239c2f4c FB |
2496 | } else if (s->frame_size == -1) { |
2497 | /* free format : find next sync to compute frame size */ | |
2498 | len = MPA_MAX_CODED_FRAME_SIZE - len; | |
2499 | if (len > buf_size) | |
2500 | len = buf_size; | |
2501 | if (len == 0) { | |
3625e88a | 2502 | /* frame too long: resync */ |
239c2f4c | 2503 | s->frame_size = 0; |
228ef9dd | 2504 | memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); |
3625e88a | 2505 | s->inbuf_ptr--; |
239c2f4c | 2506 | } else { |
0c1a9eda ZK |
2507 | uint8_t *p, *pend; |
2508 | uint32_t header1; | |
239c2f4c FB |
2509 | int padding; |
2510 | ||
2511 | memcpy(s->inbuf_ptr, buf_ptr, len); | |
2512 | /* check for header */ | |
2513 | p = s->inbuf_ptr - 3; | |
2514 | pend = s->inbuf_ptr + len - 4; | |
2515 | while (p <= pend) { | |
2516 | header = (p[0] << 24) | (p[1] << 16) | | |
2517 | (p[2] << 8) | p[3]; | |
2518 | header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) | | |
2519 | (s->inbuf[2] << 8) | s->inbuf[3]; | |
2520 | /* check with high probability that we have a | |
2521 | valid header */ | |
2522 | if ((header & SAME_HEADER_MASK) == | |
2523 | (header1 & SAME_HEADER_MASK)) { | |
2524 | /* header found: update pointers */ | |
2525 | len = (p + 4) - s->inbuf_ptr; | |
2526 | buf_ptr += len; | |
2527 | buf_size -= len; | |
2528 | s->inbuf_ptr = p; | |
2529 | /* compute frame size */ | |
2530 | s->free_format_next_header = header; | |
2531 | s->free_format_frame_size = s->inbuf_ptr - s->inbuf; | |
2532 | padding = (header1 >> 9) & 1; | |
2533 | if (s->layer == 1) | |
2534 | s->free_format_frame_size -= padding * 4; | |
2535 | else | |
2536 | s->free_format_frame_size -= padding; | |
2537 | dprintf("free frame size=%d padding=%d\n", | |
2538 | s->free_format_frame_size, padding); | |
2539 | decode_header(s, header1); | |
2540 | goto next_data; | |
2541 | } | |
2542 | p++; | |
2543 | } | |
2544 | /* not found: simply increase pointers */ | |
2545 | buf_ptr += len; | |
2546 | s->inbuf_ptr += len; | |
2547 | buf_size -= len; | |
2548 | } | |
de6d9b64 | 2549 | } else if (len < s->frame_size) { |
de5123dc ZK |
2550 | if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) |
2551 | s->frame_size = MPA_MAX_CODED_FRAME_SIZE; | |
de6d9b64 FB |
2552 | len = s->frame_size - len; |
2553 | if (len > buf_size) | |
2554 | len = buf_size; | |
de6d9b64 FB |
2555 | memcpy(s->inbuf_ptr, buf_ptr, len); |
2556 | buf_ptr += len; | |
2557 | s->inbuf_ptr += len; | |
2558 | buf_size -= len; | |
8c5b5683 FB |
2559 | } |
2560 | next_data: | |
2561 | if (s->frame_size > 0 && | |
2562 | (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
2563 | if (avctx->parse_only) { | |
2564 | /* simply return the frame data */ | |
2565 | *(uint8_t **)data = s->inbuf; | |
2566 | out_size = s->inbuf_ptr - s->inbuf; | |
2567 | } else { | |
2568 | out_size = mp_decode_frame(s, out_samples); | |
2569 | } | |
de6d9b64 FB |
2570 | s->inbuf_ptr = s->inbuf; |
2571 | s->frame_size = 0; | |
2572 | *data_size = out_size; | |
2573 | break; | |
2574 | } | |
2575 | } | |
2576 | return buf_ptr - buf; | |
2577 | } | |
2578 | ||
4b1f4f23 | 2579 | AVCodec mp2_decoder = |
de6d9b64 | 2580 | { |
4b1f4f23 | 2581 | "mp2", |
de6d9b64 FB |
2582 | CODEC_TYPE_AUDIO, |
2583 | CODEC_ID_MP2, | |
2584 | sizeof(MPADecodeContext), | |
2585 | decode_init, | |
2586 | NULL, | |
2587 | NULL, | |
2588 | decode_frame, | |
8c5b5683 | 2589 | CODEC_CAP_PARSE_ONLY, |
de6d9b64 | 2590 | }; |
4b1f4f23 J |
2591 | |
2592 | AVCodec mp3_decoder = | |
2593 | { | |
2594 | "mp3", | |
2595 | CODEC_TYPE_AUDIO, | |
80783dc2 | 2596 | CODEC_ID_MP3, |
4b1f4f23 J |
2597 | sizeof(MPADecodeContext), |
2598 | decode_init, | |
2599 | NULL, | |
2600 | NULL, | |
2601 | decode_frame, | |
8c5b5683 | 2602 | CODEC_CAP_PARSE_ONLY, |
4b1f4f23 | 2603 | }; |