Mercurial > libavcodec.hg
comparison mpegaudiodec.c @ 449:7b2f23b5dcdd libavcodec
fixed layer1/2 overflow if very loud sound - fixed broken free format decoding to pass all mpeg audio standard decoding tests (please avoid patching the parser without having all test streams available - contact me if necessary)
author | bellard |
---|---|
date | Sat, 01 Jun 2002 14:34:29 +0000 |
parents | 000aeeac27a2 |
children | 1e23eae32087 |
comparison
equal
deleted
inserted
replaced
448:e8c8ca9106aa | 449:7b2f23b5dcdd |
---|---|
26 * - test lsf / mpeg25 extensively. | 26 * - test lsf / mpeg25 extensively. |
27 */ | 27 */ |
28 | 28 |
29 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg | 29 /* define USE_HIGHPRECISION to have a bit exact (but slower) mpeg |
30 audio decoder */ | 30 audio decoder */ |
31 //#define USE_HIGHPRECISION | 31 #ifdef CONFIG_MPEGAUDIO_HP |
32 #define USE_HIGHPRECISION | |
33 #endif | |
32 | 34 |
33 #ifdef USE_HIGHPRECISION | 35 #ifdef USE_HIGHPRECISION |
34 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */ | 36 #define FRAC_BITS 23 /* fractional bits for sb_samples and dct */ |
35 #define WFRAC_BITS 16 /* fractional bits for window */ | 37 #define WFRAC_BITS 16 /* fractional bits for window */ |
36 #else | 38 #else |
147 | 149 |
148 #define SCALE_GEN(v) \ | 150 #define SCALE_GEN(v) \ |
149 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } | 151 { FIXR(1.0 * (v)), FIXR(0.7937005259 * (v)), FIXR(0.6299605249 * (v)) } |
150 | 152 |
151 static INT32 scale_factor_mult2[3][3] = { | 153 static INT32 scale_factor_mult2[3][3] = { |
152 SCALE_GEN(1.0 / 3.0), /* 3 steps */ | 154 SCALE_GEN(4.0 / 3.0), /* 3 steps */ |
153 SCALE_GEN(1.0 / 5.0), /* 5 steps */ | 155 SCALE_GEN(4.0 / 5.0), /* 5 steps */ |
154 SCALE_GEN(1.0 / 9.0), /* 9 steps */ | 156 SCALE_GEN(4.0 / 9.0), /* 9 steps */ |
155 }; | 157 }; |
156 | 158 |
157 /* 2^(n/4) */ | 159 /* 2^(n/4) */ |
158 static UINT32 scale_factor_mult3[4] = { | 160 static UINT32 scale_factor_mult3[4] = { |
159 FIXR(1.0), | 161 FIXR(1.0), |
174 shift = scale_factor_modshift[scale_factor]; | 176 shift = scale_factor_modshift[scale_factor]; |
175 mod = shift & 3; | 177 mod = shift & 3; |
176 shift >>= 2; | 178 shift >>= 2; |
177 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]); | 179 val = MUL64(mant + (-1 << n) + 1, scale_factor_mult[n-1][mod]); |
178 shift += n; | 180 shift += n; |
179 return (int)((val + (1 << (shift - 1))) >> shift); | 181 /* NOTE: at this point, 1 <= shift >= 21 + 15 */ |
182 return (int)((val + (1LL << (shift - 1))) >> shift); | |
180 } | 183 } |
181 | 184 |
182 static inline int l2_unscale_group(int steps, int mant, int scale_factor) | 185 static inline int l2_unscale_group(int steps, int mant, int scale_factor) |
183 { | 186 { |
184 int shift, mod, val; | 187 int shift, mod, val; |
185 | 188 |
186 shift = scale_factor_modshift[scale_factor]; | 189 shift = scale_factor_modshift[scale_factor]; |
187 mod = shift & 3; | 190 mod = shift & 3; |
188 shift >>= 2; | 191 shift >>= 2; |
189 /* XXX: store the result directly */ | 192 |
190 val = (2 * (mant - (steps >> 1))) * scale_factor_mult2[steps >> 2][mod]; | 193 val = (mant - (steps >> 1)) * scale_factor_mult2[steps >> 2][mod]; |
191 return (val + (1 << (shift - 1))) >> shift; | 194 /* NOTE: at this point, 0 <= shift <= 21 */ |
195 if (shift > 0) | |
196 val = (val + (1 << (shift - 1))) >> shift; | |
197 return val; | |
192 } | 198 } |
193 | 199 |
194 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */ | 200 /* compute value^(4/3) * 2^(exponent/4). It normalized to FRAC_BITS */ |
195 static inline int l3_unscale(int value, int exponent) | 201 static inline int l3_unscale(int value, int exponent) |
196 { | 202 { |
278 while (a < POW_FRAC_ONE) { | 284 while (a < POW_FRAC_ONE) { |
279 a = a << 1; | 285 a = a << 1; |
280 eq--; | 286 eq--; |
281 } | 287 } |
282 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */ | 288 /* now POW_FRAC_ONE <= a < 2 * POW_FRAC_ONE */ |
283 #if (POW_FRAC_BITS - 1) > FRAC_BITS | 289 #if POW_FRAC_BITS > FRAC_BITS |
284 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS); | 290 a = (a + (1 << (POW_FRAC_BITS - FRAC_BITS - 1))) >> (POW_FRAC_BITS - FRAC_BITS); |
285 /* correct overflow */ | 291 /* correct overflow */ |
286 if (a >= 2 * (1 << FRAC_BITS)) { | 292 if (a >= 2 * (1 << FRAC_BITS)) { |
287 a = a >> 1; | 293 a = a >> 1; |
288 eq++; | 294 eq++; |
301 if(!init) { | 307 if(!init) { |
302 /* scale factors table for layer 1/2 */ | 308 /* scale factors table for layer 1/2 */ |
303 for(i=0;i<64;i++) { | 309 for(i=0;i<64;i++) { |
304 int shift, mod; | 310 int shift, mod; |
305 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ | 311 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ |
306 shift = (i / 3) - 1; | 312 shift = (i / 3); |
307 mod = i % 3; | 313 mod = i % 3; |
308 #if FRAC_BITS <= 15 | |
309 if (shift > 31) | |
310 shift = 31; | |
311 #endif | |
312 scale_factor_modshift[i] = mod | (shift << 2); | 314 scale_factor_modshift[i] = mod | (shift << 2); |
313 } | 315 } |
314 | 316 |
315 /* scale factor multiply for layer 1 */ | 317 /* scale factor multiply for layer 1 */ |
316 for(i=0;i<15;i++) { | 318 for(i=0;i<15;i++) { |
317 int n, norm; | 319 int n, norm; |
318 n = i + 2; | 320 n = i + 2; |
319 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); | 321 norm = ((INT64_C(1) << n) * FRAC_ONE) / ((1 << n) - 1); |
320 scale_factor_mult[i][0] = MULL(FIXR(1.0), norm); | 322 scale_factor_mult[i][0] = MULL(FIXR(1.0 * 2.0), norm); |
321 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259), norm); | 323 scale_factor_mult[i][1] = MULL(FIXR(0.7937005259 * 2.0), norm); |
322 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249), norm); | 324 scale_factor_mult[i][2] = MULL(FIXR(0.6299605249 * 2.0), norm); |
323 dprintf("%d: norm=%x s=%x %x %x\n", | 325 dprintf("%d: norm=%x s=%x %x %x\n", |
324 i, norm, | 326 i, norm, |
325 scale_factor_mult[i][0], | 327 scale_factor_mult[i][0], |
326 scale_factor_mult[i][1], | 328 scale_factor_mult[i][1], |
327 scale_factor_mult[i][2]); | 329 scale_factor_mult[i][2]); |
807 synth_buf = s1->synth_buf[ch] + offset; | 809 synth_buf = s1->synth_buf[ch] + offset; |
808 | 810 |
809 for(j=0;j<32;j++) { | 811 for(j=0;j<32;j++) { |
810 v = tmp[j]; | 812 v = tmp[j]; |
811 #if FRAC_BITS <= 15 | 813 #if FRAC_BITS <= 15 |
814 /* NOTE: can cause a loss in precision if very high amplitude | |
815 sound */ | |
812 if (v > 32767) | 816 if (v > 32767) |
813 v = 32767; | 817 v = 32767; |
814 else if (v < -32768) | 818 else if (v < -32768) |
815 v = -32768; | 819 v = -32768; |
816 #endif | 820 #endif |
1067 | 1071 |
1068 s->layer = 4 - ((header >> 17) & 3); | 1072 s->layer = 4 - ((header >> 17) & 3); |
1069 /* extract frequency */ | 1073 /* extract frequency */ |
1070 sample_rate_index = (header >> 10) & 3; | 1074 sample_rate_index = (header >> 10) & 3; |
1071 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); | 1075 sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25); |
1072 if (sample_rate == 0) | |
1073 return 1; | |
1074 sample_rate_index += 3 * (s->lsf + mpeg25); | 1076 sample_rate_index += 3 * (s->lsf + mpeg25); |
1075 s->sample_rate_index = sample_rate_index; | 1077 s->sample_rate_index = sample_rate_index; |
1076 s->error_protection = ((header >> 16) & 1) ^ 1; | 1078 s->error_protection = ((header >> 16) & 1) ^ 1; |
1079 s->sample_rate = sample_rate; | |
1077 | 1080 |
1078 bitrate_index = (header >> 12) & 0xf; | 1081 bitrate_index = (header >> 12) & 0xf; |
1079 padding = (header >> 9) & 1; | 1082 padding = (header >> 9) & 1; |
1080 //extension = (header >> 8) & 1; | 1083 //extension = (header >> 8) & 1; |
1081 s->mode = (header >> 6) & 3; | 1084 s->mode = (header >> 6) & 3; |
1129 s->frame_size += padding; | 1132 s->frame_size += padding; |
1130 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000; | 1133 s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000; |
1131 break; | 1134 break; |
1132 } | 1135 } |
1133 } | 1136 } |
1134 s->sample_rate = sample_rate; | |
1135 | 1137 |
1136 #if defined(DEBUG) | 1138 #if defined(DEBUG) |
1137 printf("layer%d, %d Hz, %d kbits/s, ", | 1139 printf("layer%d, %d Hz, %d kbits/s, ", |
1138 s->layer, s->sample_rate, s->bit_rate); | 1140 s->layer, s->sample_rate, s->bit_rate); |
1139 if (s->nb_channels == 2) { | 1141 if (s->nb_channels == 2) { |
1960 #if defined(DEBUG) | 1962 #if defined(DEBUG) |
1961 void sample_dump(int fnum, INT32 *tab, int n) | 1963 void sample_dump(int fnum, INT32 *tab, int n) |
1962 { | 1964 { |
1963 static FILE *files[16], *f; | 1965 static FILE *files[16], *f; |
1964 char buf[512]; | 1966 char buf[512]; |
1965 | 1967 int i; |
1968 INT32 v; | |
1969 | |
1966 f = files[fnum]; | 1970 f = files[fnum]; |
1967 if (!f) { | 1971 if (!f) { |
1968 sprintf(buf, "/tmp/out%d.pcm", fnum); | 1972 sprintf(buf, "/tmp/out%d.%s.pcm", |
1973 fnum, | |
1974 #ifdef USE_HIGHPRECISION | |
1975 "hp" | |
1976 #else | |
1977 "lp" | |
1978 #endif | |
1979 ); | |
1969 f = fopen(buf, "w"); | 1980 f = fopen(buf, "w"); |
1970 if (!f) | 1981 if (!f) |
1971 return; | 1982 return; |
1972 files[fnum] = f; | 1983 files[fnum] = f; |
1973 } | 1984 } |
1974 | 1985 |
1975 if (fnum == 0) { | 1986 if (fnum == 0) { |
1976 int i; | |
1977 static int pos = 0; | 1987 static int pos = 0; |
1978 printf("pos=%d\n", pos); | 1988 printf("pos=%d\n", pos); |
1979 for(i=0;i<n;i++) { | 1989 for(i=0;i<n;i++) { |
1980 printf(" %f", (double)tab[i] / 32768.0); | 1990 printf(" %0.4f", (double)tab[i] / FRAC_ONE); |
1981 if ((i % 18) == 17) | 1991 if ((i % 18) == 17) |
1982 printf("\n"); | 1992 printf("\n"); |
1983 } | 1993 } |
1984 pos += n; | 1994 pos += n; |
1985 } | 1995 } |
1986 | 1996 for(i=0;i<n;i++) { |
1987 fwrite(tab, 1, n * sizeof(INT32), f); | 1997 /* normalize to 23 frac bits */ |
1998 v = tab[i] << (23 - FRAC_BITS); | |
1999 fwrite(&v, 1, sizeof(INT32), f); | |
2000 } | |
1988 } | 2001 } |
1989 #endif | 2002 #endif |
1990 | 2003 |
1991 | 2004 |
1992 /* main layer3 decoding function */ | 2005 /* main layer3 decoding function */ |
2271 reorder_block(s, g); | 2284 reorder_block(s, g); |
2272 #if defined(DEBUG) | 2285 #if defined(DEBUG) |
2273 sample_dump(0, g->sb_hybrid, 576); | 2286 sample_dump(0, g->sb_hybrid, 576); |
2274 #endif | 2287 #endif |
2275 compute_antialias(s, g); | 2288 compute_antialias(s, g); |
2276 #ifdef DEBUG | 2289 #if defined(DEBUG) |
2277 sample_dump(1, g->sb_hybrid, 576); | 2290 sample_dump(1, g->sb_hybrid, 576); |
2278 #endif | 2291 #endif |
2279 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); | 2292 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); |
2280 #ifdef DEBUG | 2293 #if defined(DEBUG) |
2281 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576); | 2294 sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576); |
2282 #endif | 2295 #endif |
2283 } | 2296 } |
2284 } /* gr */ | 2297 } /* gr */ |
2285 return nb_granules * 18; | 2298 return nb_granules * 18; |
2387 /* reset free format frame size to give a chance | 2400 /* reset free format frame size to give a chance |
2388 to get a new bitrate */ | 2401 to get a new bitrate */ |
2389 s->free_format_frame_size = 0; | 2402 s->free_format_frame_size = 0; |
2390 } else { | 2403 } else { |
2391 if (decode_header(s, header) == 1) { | 2404 if (decode_header(s, header) == 1) { |
2392 /* free format: compute frame size */ | 2405 /* free format: prepare to compute frame size */ |
2393 s->frame_size = -1; | 2406 s->frame_size = -1; |
2394 memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); | |
2395 s->inbuf_ptr--; | |
2396 } else { | |
2397 /* update codec info */ | |
2398 avctx->sample_rate = s->sample_rate; | |
2399 avctx->channels = s->nb_channels; | |
2400 avctx->bit_rate = s->bit_rate; | |
2401 avctx->frame_size = s->frame_size; | |
2402 } | 2407 } |
2408 /* update codec info */ | |
2409 avctx->sample_rate = s->sample_rate; | |
2410 avctx->channels = s->nb_channels; | |
2411 avctx->bit_rate = s->bit_rate; | |
2412 avctx->frame_size = s->frame_size; | |
2403 } | 2413 } |
2404 } | 2414 } |
2405 } else if (s->frame_size == -1) { | 2415 } else if (s->frame_size == -1) { |
2406 /* free format : find next sync to compute frame size */ | 2416 /* free format : find next sync to compute frame size */ |
2407 len = MPA_MAX_CODED_FRAME_SIZE - len; | 2417 len = MPA_MAX_CODED_FRAME_SIZE - len; |