Mercurial > libavcodec.hg
annotate wmadec.c @ 4524:722e3199c56e libavcodec
Move xvmc_render.h to libavcodec where it belongs. Blessed by Ivan.
author | diego |
---|---|
date | Wed, 14 Feb 2007 23:33:16 +0000 |
parents | 3975e734e07e |
children | 6ac364a4ce2b |
rev | line source |
---|---|
783 | 1 /* |
2 * WMA compatible decoder | |
3 * Copyright (c) 2002 The FFmpeg Project. | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
783 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
783 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
783 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3776
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
3022
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
783 | 20 */ |
1106 | 21 |
22 /** | |
23 * @file wmadec.c | |
24 * WMA compatible decoder. | |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
25 * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2. |
2967 | 26 * WMA v1 is identified by audio format 0x160 in Microsoft media files |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
27 * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161. |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
28 * |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
29 * To use this decoder, a calling application must supply the extra data |
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
30 * bytes provided with the WMA data. These are the extra, codec-specific |
2967 | 31 * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes |
32 * to the decoder using the extradata[_size] fields in AVCodecContext. There | |
1967
2b0fc6b25ab8
add the minimal documentation to make this decoder useful
melanson
parents:
1750
diff
changeset
|
33 * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data. |
1106 | 34 */ |
35 | |
783 | 36 #include "avcodec.h" |
4490 | 37 #include "wma.h" |
783 | 38 |
4490 | 39 #undef NDEBUG |
40 #include <assert.h> | |
3113 | 41 |
42 #define EXPVLCBITS 8 | |
43 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS) | |
44 | |
45 #define HGAINVLCBITS 9 | |
46 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS) | |
3022 | 47 |
783 | 48 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len); |
49 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
50 #ifdef TRACE |
783 | 51 static void dump_shorts(const char *name, const short *tab, int n) |
52 { | |
53 int i; | |
54 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
55 tprintf("%s[%d]:\n", name, n); |
783 | 56 for(i=0;i<n;i++) { |
57 if ((i & 7) == 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
58 tprintf("%4d: ", i); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
59 tprintf(" %5d.0", tab[i]); |
783 | 60 if ((i & 7) == 7) |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
61 tprintf("\n"); |
783 | 62 } |
63 } | |
64 | |
65 static void dump_floats(const char *name, int prec, const float *tab, int n) | |
66 { | |
67 int i; | |
68 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
69 tprintf("%s[%d]:\n", name, n); |
783 | 70 for(i=0;i<n;i++) { |
71 if ((i & 7) == 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
72 tprintf("%4d: ", i); |
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
73 tprintf(" %8.*f", prec, tab[i]); |
783 | 74 if ((i & 7) == 7) |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
75 tprintf("\n"); |
783 | 76 } |
77 if ((i & 7) != 0) | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
78 tprintf("\n"); |
783 | 79 } |
80 #endif | |
81 | |
82 static int wma_decode_init(AVCodecContext * avctx) | |
83 { | |
84 WMADecodeContext *s = avctx->priv_data; | |
85 int i, flags1, flags2; | |
86 uint8_t *extradata; | |
2967 | 87 |
783 | 88 /* extract flag infos */ |
89 flags1 = 0; | |
90 flags2 = 0; | |
91 extradata = avctx->extradata; | |
4490 | 92 if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) { |
783 | 93 flags1 = extradata[0] | (extradata[1] << 8); |
94 flags2 = extradata[2] | (extradata[3] << 8); | |
4490 | 95 } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) { |
2967 | 96 flags1 = extradata[0] | (extradata[1] << 8) | |
783 | 97 (extradata[2] << 16) | (extradata[3] << 24); |
98 flags2 = extradata[4] | (extradata[5] << 8); | |
99 } | |
4491 | 100 // for(i=0; i<avctx->extradata_size; i++) |
101 // av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]); | |
4490 | 102 |
783 | 103 s->use_exp_vlc = flags2 & 0x0001; |
104 s->use_bit_reservoir = flags2 & 0x0002; | |
105 s->use_variable_block_len = flags2 & 0x0004; | |
106 | |
4490 | 107 ff_wma_init(avctx, flags2); |
783 | 108 |
109 /* init MDCT */ | |
110 for(i = 0; i < s->nb_block_sizes; i++) | |
795
55add0e7eafb
avoid name clash - fixed again block size selection
bellard
parents:
785
diff
changeset
|
111 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1); |
2967 | 112 |
783 | 113 if (s->use_noise_coding) { |
4490 | 114 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits), |
115 ff_wma_hgain_huffbits, 1, 1, | |
116 ff_wma_hgain_huffcodes, 2, 2, 0); | |
783 | 117 } |
118 | |
119 if (s->use_exp_vlc) { | |
4490 | 120 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_wma_scale_huffbits), //FIXME move out of context |
121 ff_wma_scale_huffbits, 1, 1, | |
122 ff_wma_scale_huffcodes, 4, 4, 0); | |
783 | 123 } else { |
124 wma_lsp_to_curve_init(s, s->frame_len); | |
125 } | |
126 | |
127 return 0; | |
128 } | |
129 | |
4497 | 130 /** |
131 * interpolate values for a bigger or smaller block. The block must | |
132 * have multiple sizes | |
133 */ | |
783 | 134 static void interpolate_array(float *scale, int old_size, int new_size) |
135 { | |
136 int i, j, jincr, k; | |
137 float v; | |
138 | |
139 if (new_size > old_size) { | |
140 jincr = new_size / old_size; | |
141 j = new_size; | |
142 for(i = old_size - 1; i >=0; i--) { | |
143 v = scale[i]; | |
144 k = jincr; | |
145 do { | |
146 scale[--j] = v; | |
147 } while (--k); | |
148 } | |
149 } else if (new_size < old_size) { | |
150 j = 0; | |
151 jincr = old_size / new_size; | |
152 for(i = 0; i < new_size; i++) { | |
153 scale[i] = scale[j]; | |
154 j += jincr; | |
155 } | |
156 } | |
157 } | |
158 | |
4497 | 159 /** |
160 * compute x^-0.25 with an exponent and mantissa table. We use linear | |
161 * interpolation to reduce the mantissa table size at a small speed | |
162 * expense (linear interpolation approximately doubles the number of | |
163 * bits of precision). | |
164 */ | |
783 | 165 static inline float pow_m1_4(WMADecodeContext *s, float x) |
166 { | |
167 union { | |
168 float f; | |
169 unsigned int v; | |
170 } u, t; | |
171 unsigned int e, m; | |
172 float a, b; | |
173 | |
174 u.f = x; | |
175 e = u.v >> 23; | |
176 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1); | |
177 /* build interpolation scale: 1 <= t < 2. */ | |
178 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23); | |
179 a = s->lsp_pow_m_table1[m]; | |
180 b = s->lsp_pow_m_table2[m]; | |
181 return s->lsp_pow_e_table[e] * (a + b * t.f); | |
182 } | |
183 | |
184 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len) | |
2967 | 185 { |
783 | 186 float wdel, a, b; |
187 int i, e, m; | |
188 | |
189 wdel = M_PI / frame_len; | |
190 for(i=0;i<frame_len;i++) | |
191 s->lsp_cos_table[i] = 2.0f * cos(wdel * i); | |
192 | |
193 /* tables for x^-0.25 computation */ | |
194 for(i=0;i<256;i++) { | |
195 e = i - 126; | |
196 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25); | |
197 } | |
198 | |
199 /* NOTE: these two tables are needed to avoid two operations in | |
200 pow_m1_4 */ | |
201 b = 1.0; | |
202 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) { | |
203 m = (1 << LSP_POW_BITS) + i; | |
204 a = (float)m * (0.5 / (1 << LSP_POW_BITS)); | |
205 a = pow(a, -0.25); | |
206 s->lsp_pow_m_table1[i] = 2 * a - b; | |
207 s->lsp_pow_m_table2[i] = b - a; | |
208 b = a; | |
209 } | |
210 #if 0 | |
211 for(i=1;i<20;i++) { | |
212 float v, r1, r2; | |
213 v = 5.0 / i; | |
214 r1 = pow_m1_4(s, v); | |
215 r2 = pow(v,-0.25); | |
216 printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1); | |
217 } | |
218 #endif | |
219 } | |
220 | |
4497 | 221 /** |
222 * NOTE: We use the same code as Vorbis here | |
223 * @todo optimize it further with SSE/3Dnow | |
224 */ | |
2967 | 225 static void wma_lsp_to_curve(WMADecodeContext *s, |
226 float *out, float *val_max_ptr, | |
783 | 227 int n, float *lsp) |
228 { | |
229 int i, j; | |
230 float p, q, w, v, val_max; | |
231 | |
232 val_max = 0; | |
233 for(i=0;i<n;i++) { | |
234 p = 0.5f; | |
235 q = 0.5f; | |
236 w = s->lsp_cos_table[i]; | |
237 for(j=1;j<NB_LSP_COEFS;j+=2){ | |
238 q *= w - lsp[j - 1]; | |
239 p *= w - lsp[j]; | |
240 } | |
241 p *= p * (2.0f - w); | |
242 q *= q * (2.0f + w); | |
243 v = p + q; | |
244 v = pow_m1_4(s, v); | |
245 if (v > val_max) | |
246 val_max = v; | |
247 out[i] = v; | |
248 } | |
249 *val_max_ptr = val_max; | |
250 } | |
251 | |
4497 | 252 /** |
253 * decode exponents coded with LSP coefficients (same idea as Vorbis) | |
254 */ | |
783 | 255 static void decode_exp_lsp(WMADecodeContext *s, int ch) |
256 { | |
257 float lsp_coefs[NB_LSP_COEFS]; | |
258 int val, i; | |
259 | |
260 for(i = 0; i < NB_LSP_COEFS; i++) { | |
261 if (i == 0 || i >= 8) | |
262 val = get_bits(&s->gb, 3); | |
263 else | |
264 val = get_bits(&s->gb, 4); | |
4490 | 265 lsp_coefs[i] = ff_wma_lsp_codebook[i][val]; |
783 | 266 } |
267 | |
268 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch], | |
269 s->block_len, lsp_coefs); | |
270 } | |
271 | |
4497 | 272 /** |
273 * decode exponents coded with VLC codes | |
274 */ | |
783 | 275 static int decode_exp_vlc(WMADecodeContext *s, int ch) |
276 { | |
277 int last_exp, n, code; | |
278 const uint16_t *ptr, *band_ptr; | |
279 float v, *q, max_scale, *q_end; | |
2967 | 280 |
783 | 281 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits]; |
282 ptr = band_ptr; | |
283 q = s->exponents[ch]; | |
284 q_end = q + s->block_len; | |
285 max_scale = 0; | |
286 if (s->version == 1) { | |
287 last_exp = get_bits(&s->gb, 5) + 10; | |
3235 | 288 /* XXX: use a table */ |
289 v = pow(10, last_exp * (1.0 / 16.0)); | |
783 | 290 max_scale = v; |
291 n = *ptr++; | |
292 do { | |
293 *q++ = v; | |
294 } while (--n); | |
4490 | 295 }else |
296 last_exp = 36; | |
297 | |
783 | 298 while (q < q_end) { |
3113 | 299 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX); |
783 | 300 if (code < 0) |
301 return -1; | |
302 /* NOTE: this offset is the same as MPEG4 AAC ! */ | |
303 last_exp += code - 60; | |
3235 | 304 /* XXX: use a table */ |
305 v = pow(10, last_exp * (1.0 / 16.0)); | |
783 | 306 if (v > max_scale) |
307 max_scale = v; | |
308 n = *ptr++; | |
309 do { | |
310 *q++ = v; | |
311 } while (--n); | |
312 } | |
313 s->max_exponent[ch] = max_scale; | |
314 return 0; | |
315 } | |
316 | |
4497 | 317 /** |
318 * @return 0 if OK. 1 if last block of frame. return -1 if | |
319 * unrecorrable error. | |
320 */ | |
783 | 321 static int wma_decode_block(WMADecodeContext *s) |
322 { | |
323 int n, v, a, ch, code, bsize; | |
324 int coef_nb_bits, total_gain, parse_exponents; | |
325 int nb_coefs[MAX_CHANNELS]; | |
326 float mdct_norm; | |
327 | |
1343 | 328 #ifdef TRACE |
329 tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num); | |
330 #endif | |
783 | 331 |
332 /* compute current block length */ | |
333 if (s->use_variable_block_len) { | |
334 n = av_log2(s->nb_block_sizes - 1) + 1; | |
2967 | 335 |
783 | 336 if (s->reset_block_lengths) { |
337 s->reset_block_lengths = 0; | |
338 v = get_bits(&s->gb, n); | |
339 if (v >= s->nb_block_sizes) | |
340 return -1; | |
341 s->prev_block_len_bits = s->frame_len_bits - v; | |
342 v = get_bits(&s->gb, n); | |
343 if (v >= s->nb_block_sizes) | |
344 return -1; | |
345 s->block_len_bits = s->frame_len_bits - v; | |
346 } else { | |
347 /* update block lengths */ | |
348 s->prev_block_len_bits = s->block_len_bits; | |
349 s->block_len_bits = s->next_block_len_bits; | |
350 } | |
351 v = get_bits(&s->gb, n); | |
352 if (v >= s->nb_block_sizes) | |
353 return -1; | |
354 s->next_block_len_bits = s->frame_len_bits - v; | |
355 } else { | |
356 /* fixed block len */ | |
357 s->next_block_len_bits = s->frame_len_bits; | |
358 s->prev_block_len_bits = s->frame_len_bits; | |
359 s->block_len_bits = s->frame_len_bits; | |
360 } | |
361 | |
362 /* now check if the block length is coherent with the frame length */ | |
363 s->block_len = 1 << s->block_len_bits; | |
364 if ((s->block_pos + s->block_len) > s->frame_len) | |
365 return -1; | |
366 | |
367 if (s->nb_channels == 2) { | |
368 s->ms_stereo = get_bits(&s->gb, 1); | |
369 } | |
370 v = 0; | |
371 for(ch = 0; ch < s->nb_channels; ch++) { | |
372 a = get_bits(&s->gb, 1); | |
373 s->channel_coded[ch] = a; | |
374 v |= a; | |
375 } | |
376 /* if no channel coded, no need to go further */ | |
377 /* XXX: fix potential framing problems */ | |
378 if (!v) | |
379 goto next; | |
380 | |
381 bsize = s->frame_len_bits - s->block_len_bits; | |
382 | |
383 /* read total gain and extract corresponding number of bits for | |
384 coef escape coding */ | |
385 total_gain = 1; | |
386 for(;;) { | |
387 a = get_bits(&s->gb, 7); | |
388 total_gain += a; | |
389 if (a != 127) | |
390 break; | |
391 } | |
2967 | 392 |
4490 | 393 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain); |
783 | 394 |
395 /* compute number of coefficients */ | |
396 n = s->coefs_end[bsize] - s->coefs_start; | |
397 for(ch = 0; ch < s->nb_channels; ch++) | |
398 nb_coefs[ch] = n; | |
399 | |
400 /* complex coding */ | |
401 if (s->use_noise_coding) { | |
402 | |
403 for(ch = 0; ch < s->nb_channels; ch++) { | |
404 if (s->channel_coded[ch]) { | |
405 int i, n, a; | |
406 n = s->exponent_high_sizes[bsize]; | |
407 for(i=0;i<n;i++) { | |
408 a = get_bits(&s->gb, 1); | |
409 s->high_band_coded[ch][i] = a; | |
410 /* if noise coding, the coefficients are not transmitted */ | |
411 if (a) | |
412 nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; | |
413 } | |
414 } | |
415 } | |
416 for(ch = 0; ch < s->nb_channels; ch++) { | |
417 if (s->channel_coded[ch]) { | |
418 int i, n, val, code; | |
419 | |
420 n = s->exponent_high_sizes[bsize]; | |
421 val = (int)0x80000000; | |
422 for(i=0;i<n;i++) { | |
423 if (s->high_band_coded[ch][i]) { | |
424 if (val == (int)0x80000000) { | |
425 val = get_bits(&s->gb, 7) - 19; | |
426 } else { | |
3113 | 427 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX); |
783 | 428 if (code < 0) |
429 return -1; | |
430 val += code - 18; | |
431 } | |
432 s->high_band_values[ch][i] = val; | |
433 } | |
434 } | |
435 } | |
436 } | |
437 } | |
2967 | 438 |
783 | 439 /* exposant can be interpolated in short blocks. */ |
440 parse_exponents = 1; | |
441 if (s->block_len_bits != s->frame_len_bits) { | |
442 parse_exponents = get_bits(&s->gb, 1); | |
443 } | |
2967 | 444 |
783 | 445 if (parse_exponents) { |
446 for(ch = 0; ch < s->nb_channels; ch++) { | |
447 if (s->channel_coded[ch]) { | |
448 if (s->use_exp_vlc) { | |
449 if (decode_exp_vlc(s, ch) < 0) | |
450 return -1; | |
451 } else { | |
452 decode_exp_lsp(s, ch); | |
453 } | |
454 } | |
455 } | |
456 } else { | |
457 for(ch = 0; ch < s->nb_channels; ch++) { | |
458 if (s->channel_coded[ch]) { | |
2967 | 459 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits, |
783 | 460 s->block_len); |
461 } | |
462 } | |
463 } | |
464 | |
465 /* parse spectral coefficients : just RLE encoding */ | |
466 for(ch = 0; ch < s->nb_channels; ch++) { | |
467 if (s->channel_coded[ch]) { | |
468 VLC *coef_vlc; | |
469 int level, run, sign, tindex; | |
470 int16_t *ptr, *eptr; | |
3776 | 471 const uint16_t *level_table, *run_table; |
783 | 472 |
473 /* special VLC tables are used for ms stereo because | |
474 there is potentially less energy there */ | |
475 tindex = (ch == 1 && s->ms_stereo); | |
476 coef_vlc = &s->coef_vlc[tindex]; | |
477 run_table = s->run_table[tindex]; | |
478 level_table = s->level_table[tindex]; | |
479 /* XXX: optimize */ | |
480 ptr = &s->coefs1[ch][0]; | |
481 eptr = ptr + nb_coefs[ch]; | |
482 memset(ptr, 0, s->block_len * sizeof(int16_t)); | |
483 for(;;) { | |
3113 | 484 code = get_vlc2(&s->gb, coef_vlc->table, VLCBITS, VLCMAX); |
783 | 485 if (code < 0) |
486 return -1; | |
487 if (code == 1) { | |
488 /* EOB */ | |
489 break; | |
490 } else if (code == 0) { | |
491 /* escape */ | |
492 level = get_bits(&s->gb, coef_nb_bits); | |
493 /* NOTE: this is rather suboptimal. reading | |
494 block_len_bits would be better */ | |
495 run = get_bits(&s->gb, s->frame_len_bits); | |
496 } else { | |
497 /* normal code */ | |
498 run = run_table[code]; | |
499 level = level_table[code]; | |
500 } | |
501 sign = get_bits(&s->gb, 1); | |
502 if (!sign) | |
503 level = -level; | |
504 ptr += run; | |
505 if (ptr >= eptr) | |
3361 | 506 { |
507 av_log(NULL, AV_LOG_ERROR, "overflow in spectral RLE, ignoring\n"); | |
508 break; | |
509 } | |
783 | 510 *ptr++ = level; |
511 /* NOTE: EOB can be omitted */ | |
512 if (ptr >= eptr) | |
513 break; | |
514 } | |
515 } | |
516 if (s->version == 1 && s->nb_channels >= 2) { | |
517 align_get_bits(&s->gb); | |
518 } | |
519 } | |
2967 | 520 |
783 | 521 /* normalize */ |
522 { | |
523 int n4 = s->block_len / 2; | |
524 mdct_norm = 1.0 / (float)n4; | |
525 if (s->version == 1) { | |
526 mdct_norm *= sqrt(n4); | |
527 } | |
528 } | |
529 | |
530 /* finally compute the MDCT coefficients */ | |
531 for(ch = 0; ch < s->nb_channels; ch++) { | |
532 if (s->channel_coded[ch]) { | |
533 int16_t *coefs1; | |
534 float *coefs, *exponents, mult, mult1, noise, *exp_ptr; | |
535 int i, j, n, n1, last_high_band; | |
536 float exp_power[HIGH_BAND_MAX_SIZE]; | |
537 | |
538 coefs1 = s->coefs1[ch]; | |
539 exponents = s->exponents[ch]; | |
3235 | 540 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; |
783 | 541 mult *= mdct_norm; |
542 coefs = s->coefs[ch]; | |
543 if (s->use_noise_coding) { | |
544 mult1 = mult; | |
545 /* very low freqs : noise */ | |
546 for(i = 0;i < s->coefs_start; i++) { | |
547 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1; | |
548 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
549 } | |
2967 | 550 |
783 | 551 n1 = s->exponent_high_sizes[bsize]; |
552 | |
553 /* compute power of high bands */ | |
2967 | 554 exp_ptr = exponents + |
555 s->high_band_start[bsize] - | |
783 | 556 s->coefs_start; |
557 last_high_band = 0; /* avoid warning */ | |
558 for(j=0;j<n1;j++) { | |
2967 | 559 n = s->exponent_high_bands[s->frame_len_bits - |
783 | 560 s->block_len_bits][j]; |
561 if (s->high_band_coded[ch][j]) { | |
562 float e2, v; | |
563 e2 = 0; | |
564 for(i = 0;i < n; i++) { | |
565 v = exp_ptr[i]; | |
566 e2 += v * v; | |
567 } | |
568 exp_power[j] = e2 / n; | |
569 last_high_band = j; | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
570 tprintf("%d: power=%f (%d)\n", j, exp_power[j], n); |
783 | 571 } |
572 exp_ptr += n; | |
573 } | |
574 | |
575 /* main freqs and high freqs */ | |
576 for(j=-1;j<n1;j++) { | |
577 if (j < 0) { | |
2967 | 578 n = s->high_band_start[bsize] - |
783 | 579 s->coefs_start; |
580 } else { | |
2967 | 581 n = s->exponent_high_bands[s->frame_len_bits - |
783 | 582 s->block_len_bits][j]; |
583 } | |
584 if (j >= 0 && s->high_band_coded[ch][j]) { | |
585 /* use noise with specified power */ | |
586 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); | |
3235 | 587 /* XXX: use a table */ |
588 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05); | |
783 | 589 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); |
590 mult1 *= mdct_norm; | |
591 for(i = 0;i < n; i++) { | |
592 noise = s->noise_table[s->noise_index]; | |
593 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
594 *coefs++ = (*exponents++) * noise * mult1; | |
595 } | |
596 } else { | |
597 /* coded values + small noise */ | |
598 for(i = 0;i < n; i++) { | |
599 noise = s->noise_table[s->noise_index]; | |
600 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
601 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult; | |
602 } | |
603 } | |
604 } | |
605 | |
606 /* very high freqs : noise */ | |
607 n = s->block_len - s->coefs_end[bsize]; | |
608 mult1 = mult * exponents[-1]; | |
609 for(i = 0; i < n; i++) { | |
610 *coefs++ = s->noise_table[s->noise_index] * mult1; | |
611 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
612 } | |
613 } else { | |
614 /* XXX: optimize more */ | |
615 for(i = 0;i < s->coefs_start; i++) | |
616 *coefs++ = 0.0; | |
617 n = nb_coefs[ch]; | |
618 for(i = 0;i < n; i++) { | |
619 *coefs++ = coefs1[i] * exponents[i] * mult; | |
620 } | |
621 n = s->block_len - s->coefs_end[bsize]; | |
622 for(i = 0;i < n; i++) | |
623 *coefs++ = 0.0; | |
624 } | |
625 } | |
626 } | |
627 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
628 #ifdef TRACE |
783 | 629 for(ch = 0; ch < s->nb_channels; ch++) { |
630 if (s->channel_coded[ch]) { | |
631 dump_floats("exponents", 3, s->exponents[ch], s->block_len); | |
632 dump_floats("coefs", 1, s->coefs[ch], s->block_len); | |
633 } | |
634 } | |
635 #endif | |
2967 | 636 |
783 | 637 if (s->ms_stereo && s->channel_coded[1]) { |
638 float a, b; | |
639 int i; | |
640 | |
641 /* nominal case for ms stereo: we do it before mdct */ | |
642 /* no need to optimize this case because it should almost | |
643 never happen */ | |
644 if (!s->channel_coded[0]) { | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
645 tprintf("rare ms-stereo case happened\n"); |
783 | 646 memset(s->coefs[0], 0, sizeof(float) * s->block_len); |
647 s->channel_coded[0] = 1; | |
648 } | |
2967 | 649 |
783 | 650 for(i = 0; i < s->block_len; i++) { |
651 a = s->coefs[0][i]; | |
652 b = s->coefs[1][i]; | |
653 s->coefs[0][i] = a + b; | |
654 s->coefs[1][i] = a - b; | |
655 } | |
656 } | |
657 | |
658 /* build the window : we ensure that when the windows overlap | |
659 their squared sum is always 1 (MDCT reconstruction rule) */ | |
660 /* XXX: merge with output */ | |
661 { | |
662 int i, next_block_len, block_len, prev_block_len, n; | |
663 float *wptr; | |
664 | |
665 block_len = s->block_len; | |
666 prev_block_len = 1 << s->prev_block_len_bits; | |
667 next_block_len = 1 << s->next_block_len_bits; | |
668 | |
669 /* right part */ | |
4301
b43bd0c56eaa
Bug fix for crashes when SSE is used on unaligned arrays.
banan
parents:
3947
diff
changeset
|
670 wptr = s->window + block_len; |
783 | 671 if (block_len <= next_block_len) { |
672 for(i=0;i<block_len;i++) | |
673 *wptr++ = s->windows[bsize][i]; | |
674 } else { | |
675 /* overlap */ | |
676 n = (block_len / 2) - (next_block_len / 2); | |
677 for(i=0;i<n;i++) | |
678 *wptr++ = 1.0; | |
679 for(i=0;i<next_block_len;i++) | |
680 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i]; | |
681 for(i=0;i<n;i++) | |
682 *wptr++ = 0.0; | |
683 } | |
684 | |
685 /* left part */ | |
4301
b43bd0c56eaa
Bug fix for crashes when SSE is used on unaligned arrays.
banan
parents:
3947
diff
changeset
|
686 wptr = s->window + block_len; |
783 | 687 if (block_len <= prev_block_len) { |
688 for(i=0;i<block_len;i++) | |
689 *--wptr = s->windows[bsize][i]; | |
690 } else { | |
691 /* overlap */ | |
692 n = (block_len / 2) - (prev_block_len / 2); | |
693 for(i=0;i<n;i++) | |
694 *--wptr = 1.0; | |
695 for(i=0;i<prev_block_len;i++) | |
696 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i]; | |
697 for(i=0;i<n;i++) | |
698 *--wptr = 0.0; | |
699 } | |
700 } | |
701 | |
2967 | 702 |
783 | 703 for(ch = 0; ch < s->nb_channels; ch++) { |
704 if (s->channel_coded[ch]) { | |
705 float *ptr; | |
3592
6a358dccf2ab
SIMD vector optimizations. 3% faster overall decoding.
banan
parents:
3555
diff
changeset
|
706 int n4, index, n; |
783 | 707 |
708 n = s->block_len; | |
709 n4 = s->block_len / 2; | |
3555 | 710 s->mdct_ctx[bsize].fft.imdct_calc(&s->mdct_ctx[bsize], |
4301
b43bd0c56eaa
Bug fix for crashes when SSE is used on unaligned arrays.
banan
parents:
3947
diff
changeset
|
711 s->output, s->coefs[ch], s->mdct_tmp); |
783 | 712 |
713 /* XXX: optimize all that by build the window and | |
714 multipying/adding at the same time */ | |
715 | |
3592
6a358dccf2ab
SIMD vector optimizations. 3% faster overall decoding.
banan
parents:
3555
diff
changeset
|
716 /* multiply by the window and add in the frame */ |
783 | 717 index = (s->frame_len / 2) + s->block_pos - n4; |
718 ptr = &s->frame_out[ch][index]; | |
4301
b43bd0c56eaa
Bug fix for crashes when SSE is used on unaligned arrays.
banan
parents:
3947
diff
changeset
|
719 s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1); |
783 | 720 |
721 /* specific fast case for ms-stereo : add to second | |
722 channel if it is not coded */ | |
723 if (s->ms_stereo && !s->channel_coded[1]) { | |
724 ptr = &s->frame_out[1][index]; | |
4301
b43bd0c56eaa
Bug fix for crashes when SSE is used on unaligned arrays.
banan
parents:
3947
diff
changeset
|
725 s->dsp.vector_fmul_add_add(ptr,s->window,s->output,ptr,0,2*n,1); |
783 | 726 } |
727 } | |
728 } | |
729 next: | |
730 /* update block number */ | |
731 s->block_num++; | |
732 s->block_pos += s->block_len; | |
733 if (s->block_pos >= s->frame_len) | |
734 return 1; | |
735 else | |
736 return 0; | |
737 } | |
738 | |
739 /* decode a frame of frame_len samples */ | |
740 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples) | |
741 { | |
742 int ret, i, n, a, ch, incr; | |
743 int16_t *ptr; | |
744 float *iptr; | |
745 | |
1343 | 746 #ifdef TRACE |
747 tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len); | |
748 #endif | |
783 | 749 |
750 /* read each block */ | |
751 s->block_num = 0; | |
752 s->block_pos = 0; | |
753 for(;;) { | |
754 ret = wma_decode_block(s); | |
2967 | 755 if (ret < 0) |
783 | 756 return -1; |
757 if (ret) | |
758 break; | |
759 } | |
760 | |
761 /* convert frame to integer */ | |
762 n = s->frame_len; | |
763 incr = s->nb_channels; | |
764 for(ch = 0; ch < s->nb_channels; ch++) { | |
765 ptr = samples + ch; | |
766 iptr = s->frame_out[ch]; | |
767 | |
768 for(i=0;i<n;i++) { | |
797 | 769 a = lrintf(*iptr++); |
783 | 770 if (a > 32767) |
771 a = 32767; | |
772 else if (a < -32768) | |
773 a = -32768; | |
774 *ptr = a; | |
775 ptr += incr; | |
776 } | |
777 /* prepare for next block */ | |
778 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len], | |
779 s->frame_len * sizeof(float)); | |
780 /* XXX: suppress this */ | |
2967 | 781 memset(&s->frame_out[ch][s->frame_len], 0, |
783 | 782 s->frame_len * sizeof(float)); |
783 } | |
784 | |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
785 #ifdef TRACE |
783 | 786 dump_shorts("samples", samples, n * s->nb_channels); |
787 #endif | |
788 return 0; | |
789 } | |
790 | |
2967 | 791 static int wma_decode_superframe(AVCodecContext *avctx, |
783 | 792 void *data, int *data_size, |
1064 | 793 uint8_t *buf, int buf_size) |
783 | 794 { |
795 WMADecodeContext *s = avctx->priv_data; | |
796 int nb_frames, bit_offset, i, pos, len; | |
797 uint8_t *q; | |
798 int16_t *samples; | |
2967 | 799 |
1342
f574934c4219
uniformization (now it uses the same trace functions as h264, defined in common.h)
al3x
parents:
1303
diff
changeset
|
800 tprintf("***decode_superframe:\n"); |
783 | 801 |
1750 | 802 if(buf_size==0){ |
803 s->last_superframe_len = 0; | |
804 return 0; | |
805 } | |
2967 | 806 |
783 | 807 samples = data; |
808 | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
809 init_get_bits(&s->gb, buf, buf_size*8); |
2967 | 810 |
783 | 811 if (s->use_bit_reservoir) { |
812 /* read super frame header */ | |
813 get_bits(&s->gb, 4); /* super frame index */ | |
814 nb_frames = get_bits(&s->gb, 4) - 1; | |
815 | |
816 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); | |
817 | |
818 if (s->last_superframe_len > 0) { | |
819 // printf("skip=%d\n", s->last_bitoffset); | |
820 /* add bit_offset bits to last frame */ | |
2967 | 821 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > |
783 | 822 MAX_CODED_SUPERFRAME_SIZE) |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
823 goto fail; |
783 | 824 q = s->last_superframe + s->last_superframe_len; |
825 len = bit_offset; | |
3362
c43fcf831f7c
Do not read full byte when less than 8 bits are still to be read.
reimar
parents:
3361
diff
changeset
|
826 while (len > 7) { |
783 | 827 *q++ = (get_bits)(&s->gb, 8); |
828 len -= 8; | |
829 } | |
830 if (len > 0) { | |
831 *q++ = (get_bits)(&s->gb, len) << (8 - len); | |
832 } | |
2967 | 833 |
783 | 834 /* XXX: bit_offset bits into last frame */ |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
835 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8); |
783 | 836 /* skip unused bits */ |
837 if (s->last_bitoffset > 0) | |
838 skip_bits(&s->gb, s->last_bitoffset); | |
839 /* this frame is stored in the last superframe and in the | |
840 current one */ | |
841 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
842 goto fail; |
783 | 843 samples += s->nb_channels * s->frame_len; |
844 } | |
845 | |
846 /* read each frame starting from bit_offset */ | |
847 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; | |
1025
1f9afd8b9131
GetBitContext.size is allways multiplied by 8 -> use size_in_bits to avoid useless *8 in a few inner loops
michaelni
parents:
972
diff
changeset
|
848 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8); |
783 | 849 len = pos & 7; |
850 if (len > 0) | |
851 skip_bits(&s->gb, len); | |
2967 | 852 |
783 | 853 s->reset_block_lengths = 1; |
854 for(i=0;i<nb_frames;i++) { | |
855 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
856 goto fail; |
783 | 857 samples += s->nb_channels * s->frame_len; |
858 } | |
859 | |
860 /* we copy the end of the frame in the last frame buffer */ | |
861 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); | |
862 s->last_bitoffset = pos & 7; | |
863 pos >>= 3; | |
864 len = buf_size - pos; | |
819 | 865 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) { |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
866 goto fail; |
783 | 867 } |
868 s->last_superframe_len = len; | |
869 memcpy(s->last_superframe, buf + pos, len); | |
870 } else { | |
871 /* single frame decode */ | |
872 if (wma_decode_frame(s, samples) < 0) | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
873 goto fail; |
783 | 874 samples += s->nb_channels * s->frame_len; |
875 } | |
4490 | 876 |
877 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d outbytes:%d eaten:%d\n", s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len, (int8_t *)samples - (int8_t *)data, s->block_align); | |
878 | |
783 | 879 *data_size = (int8_t *)samples - (int8_t *)data; |
880 return s->block_align; | |
964
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
881 fail: |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
882 /* when error, we reset the bit reservoir */ |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
883 s->last_superframe_len = 0; |
6e6773512288
oops : better error resilience - should fix most wma decoding problems
bellard
parents:
819
diff
changeset
|
884 return -1; |
783 | 885 } |
886 | |
887 AVCodec wmav1_decoder = | |
888 { | |
889 "wmav1", | |
890 CODEC_TYPE_AUDIO, | |
891 CODEC_ID_WMAV1, | |
892 sizeof(WMADecodeContext), | |
893 wma_decode_init, | |
894 NULL, | |
4490 | 895 ff_wma_end, |
783 | 896 wma_decode_superframe, |
897 }; | |
898 | |
899 AVCodec wmav2_decoder = | |
900 { | |
901 "wmav2", | |
902 CODEC_TYPE_AUDIO, | |
903 CODEC_ID_WMAV2, | |
904 sizeof(WMADecodeContext), | |
905 wma_decode_init, | |
906 NULL, | |
4490 | 907 ff_wma_end, |
783 | 908 wma_decode_superframe, |
909 }; |