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