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