Mercurial > audlegacy
annotate Plugins/Input/wma/libffwma/wmadec.c @ 218:0bea7509d6ba trunk
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
author | chainsaw |
---|---|
date | Mon, 21 Nov 2005 14:59:13 -0800 |
parents | 12004b385a96 |
children | 28b73b5595d1 |
rev | line source |
---|---|
137 | 1 /* |
2 * WMA compatible decoder | |
3 * Copyright (c) 2002 The FFmpeg Project. | |
4 * | |
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. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
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 | |
18 */ | |
19 | |
20 /** | |
21 * @file wmadec.c | |
22 * WMA compatible decoder. | |
23 */ | |
24 | |
25 #include "avcodec.h" | |
26 #include "dsputil.h" | |
27 | |
28 /* size of blocks */ | |
29 #define BLOCK_MIN_BITS 7 | |
30 #define BLOCK_MAX_BITS 11 | |
31 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS) | |
32 | |
33 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1) | |
34 | |
35 /* XXX: find exact max size */ | |
36 #define HIGH_BAND_MAX_SIZE 16 | |
37 | |
38 #define NB_LSP_COEFS 10 | |
39 | |
40 /* XXX: is it a suitable value ? */ | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
41 #define MAX_CODED_SUPERFRAME_SIZE 4096 |
137 | 42 |
43 #define MAX_CHANNELS 2 | |
44 | |
45 #define NOISE_TAB_SIZE 8192 | |
46 | |
47 #define LSP_POW_BITS 7 | |
48 | |
49 typedef struct WMADecodeContext { | |
50 GetBitContext gb; | |
51 int sample_rate; | |
52 int nb_channels; | |
53 int bit_rate; | |
54 int version; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */ | |
55 int block_align; | |
56 int use_bit_reservoir; | |
57 int use_variable_block_len; | |
58 int use_exp_vlc; /* exponent coding: 0 = lsp, 1 = vlc + delta */ | |
59 int use_noise_coding; /* true if perceptual noise is added */ | |
60 int byte_offset_bits; | |
61 VLC exp_vlc; | |
62 int exponent_sizes[BLOCK_NB_SIZES]; | |
63 uint16_t exponent_bands[BLOCK_NB_SIZES][25]; | |
64 int high_band_start[BLOCK_NB_SIZES]; /* index of first coef in high band */ | |
65 int coefs_start; /* first coded coef */ | |
66 int coefs_end[BLOCK_NB_SIZES]; /* max number of coded coefficients */ | |
67 int exponent_high_sizes[BLOCK_NB_SIZES]; | |
68 int exponent_high_bands[BLOCK_NB_SIZES][HIGH_BAND_MAX_SIZE]; | |
69 VLC hgain_vlc; | |
70 | |
71 /* coded values in high bands */ | |
72 int high_band_coded[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]; | |
73 int high_band_values[MAX_CHANNELS][HIGH_BAND_MAX_SIZE]; | |
74 | |
75 /* there are two possible tables for spectral coefficients */ | |
76 VLC coef_vlc[2]; | |
77 uint16_t *run_table[2]; | |
78 uint16_t *level_table[2]; | |
79 /* frame info */ | |
80 int frame_len; /* frame length in samples */ | |
81 int frame_len_bits; /* frame_len = 1 << frame_len_bits */ | |
82 int nb_block_sizes; /* number of block sizes */ | |
83 /* block info */ | |
84 int reset_block_lengths; | |
85 int block_len_bits; /* log2 of current block length */ | |
86 int next_block_len_bits; /* log2 of next block length */ | |
87 int prev_block_len_bits; /* log2 of prev block length */ | |
88 int block_len; /* block length in samples */ | |
89 int block_num; /* block number in current frame */ | |
90 int block_pos; /* current position in frame */ | |
91 uint8_t ms_stereo; /* true if mid/side stereo mode */ | |
92 uint8_t channel_coded[MAX_CHANNELS]; /* true if channel is coded */ | |
93 float exponents[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16))); | |
94 float max_exponent[MAX_CHANNELS]; | |
95 int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE]; | |
96 float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16))); | |
97 MDCTContext mdct_ctx[BLOCK_NB_SIZES]; | |
98 float *windows[BLOCK_NB_SIZES]; | |
99 FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */ | |
100 /* output buffer for one frame and the last for IMDCT windowing */ | |
101 float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16))); | |
102 /* last frame info */ | |
103 uint8_t last_superframe[MAX_CODED_SUPERFRAME_SIZE + 4]; /* padding added */ | |
104 int last_bitoffset; | |
105 int last_superframe_len; | |
106 float noise_table[NOISE_TAB_SIZE]; | |
107 int noise_index; | |
108 float noise_mult; /* XXX: suppress that and integrate it in the noise array */ | |
109 /* lsp_to_curve tables */ | |
110 float lsp_cos_table[BLOCK_MAX_SIZE]; | |
111 float lsp_pow_e_table[256]; | |
112 float lsp_pow_m_table1[(1 << LSP_POW_BITS)]; | |
113 float lsp_pow_m_table2[(1 << LSP_POW_BITS)]; | |
114 | |
115 #ifdef TRACE | |
116 int frame_count; | |
117 #endif | |
118 } WMADecodeContext; | |
119 | |
120 typedef struct CoefVLCTable { | |
121 int n; /* total number of codes */ | |
122 const uint32_t *huffcodes; /* VLC bit values */ | |
123 const uint8_t *huffbits; /* VLC bit size */ | |
124 const uint16_t *levels; /* table to build run/level tables */ | |
125 } CoefVLCTable; | |
126 | |
127 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len); | |
128 | |
129 #include "wmadata.h" | |
130 | |
131 #ifdef TRACE | |
132 static void dump_shorts(const char *name, const short *tab, int n) | |
133 { | |
134 int i; | |
135 | |
136 tprintf("%s[%d]:\n", name, n); | |
137 for(i=0;i<n;i++) { | |
138 if ((i & 7) == 0) | |
139 tprintf("%4d: ", i); | |
140 tprintf(" %5d.0", tab[i]); | |
141 if ((i & 7) == 7) | |
142 tprintf("\n"); | |
143 } | |
144 } | |
145 | |
146 static void dump_floats(const char *name, int prec, const float *tab, int n) | |
147 { | |
148 int i; | |
149 | |
150 tprintf("%s[%d]:\n", name, n); | |
151 for(i=0;i<n;i++) { | |
152 if ((i & 7) == 0) | |
153 tprintf("%4d: ", i); | |
154 tprintf(" %8.*f", prec, tab[i]); | |
155 if ((i & 7) == 7) | |
156 tprintf("\n"); | |
157 } | |
158 if ((i & 7) != 0) | |
159 tprintf("\n"); | |
160 } | |
161 #endif | |
162 | |
163 /* XXX: use same run/length optimization as mpeg decoders */ | |
164 static void init_coef_vlc(VLC *vlc, | |
165 uint16_t **prun_table, uint16_t **plevel_table, | |
166 const CoefVLCTable *vlc_table) | |
167 { | |
168 int n = vlc_table->n; | |
169 const uint8_t *table_bits = vlc_table->huffbits; | |
170 const uint32_t *table_codes = vlc_table->huffcodes; | |
171 const uint16_t *levels_table = vlc_table->levels; | |
172 uint16_t *run_table, *level_table; | |
173 const uint16_t *p; | |
174 int i, l, j, level; | |
175 | |
176 init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4); | |
177 | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
178 run_table = malloc(n * sizeof(uint16_t)); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
179 level_table = malloc(n * sizeof(uint16_t)); |
137 | 180 p = levels_table; |
181 i = 2; | |
182 level = 1; | |
183 while (i < n) { | |
184 l = *p++; | |
185 for(j=0;j<l;j++) { | |
186 run_table[i] = j; | |
187 level_table[i] = level; | |
188 i++; | |
189 } | |
190 level++; | |
191 } | |
192 *prun_table = run_table; | |
193 *plevel_table = level_table; | |
194 } | |
195 | |
196 static int wma_decode_init(AVCodecContext * avctx) | |
197 { | |
198 WMADecodeContext *s = avctx->priv_data; | |
199 int i, flags1, flags2; | |
200 float *window; | |
201 uint8_t *extradata; | |
202 float bps1, high_freq, bps; | |
203 int sample_rate1; | |
204 int coef_vlc_table; | |
205 | |
206 | |
207 s->sample_rate = avctx->sample_rate; | |
208 s->nb_channels = avctx->channels; | |
209 s->bit_rate = avctx->bit_rate; | |
210 s->block_align = avctx->block_align; | |
211 | |
212 if (avctx->codec->id == CODEC_ID_WMAV1) { | |
213 s->version = 1; | |
214 } else { | |
215 s->version = 2; | |
216 } | |
217 | |
218 /* extract flag infos */ | |
219 flags1 = 0; | |
220 flags2 = 0; | |
221 extradata = avctx->extradata; | |
222 if (s->version == 1 && avctx->extradata_size >= 4) { | |
223 flags1 = extradata[0] | (extradata[1] << 8); | |
224 flags2 = extradata[2] | (extradata[3] << 8); | |
225 } else if (s->version == 2 && avctx->extradata_size >= 6) { | |
226 flags1 = extradata[0] | (extradata[1] << 8) | | |
227 (extradata[2] << 16) | (extradata[3] << 24); | |
228 flags2 = extradata[4] | (extradata[5] << 8); | |
229 } | |
230 s->use_exp_vlc = flags2 & 0x0001; | |
231 s->use_bit_reservoir = flags2 & 0x0002; | |
232 s->use_variable_block_len = flags2 & 0x0004; | |
233 | |
234 /* compute MDCT block size */ | |
235 if (s->sample_rate <= 16000) { | |
236 s->frame_len_bits = 9; | |
237 } else if (s->sample_rate <= 22050 || | |
238 (s->sample_rate <= 32000 && s->version == 1)) { | |
239 s->frame_len_bits = 10; | |
240 } else { | |
241 s->frame_len_bits = 11; | |
242 } | |
243 s->frame_len = 1 << s->frame_len_bits; | |
244 if (s->use_variable_block_len) { | |
245 int nb_max, nb; | |
246 nb = ((flags2 >> 3) & 3) + 1; | |
247 if ((s->bit_rate / s->nb_channels) >= 32000) | |
248 nb += 2; | |
249 nb_max = s->frame_len_bits - BLOCK_MIN_BITS; | |
250 if (nb > nb_max) | |
251 nb = nb_max; | |
252 s->nb_block_sizes = nb + 1; | |
253 } else { | |
254 s->nb_block_sizes = 1; | |
255 } | |
256 | |
257 /* init rate dependant parameters */ | |
258 s->use_noise_coding = 1; | |
259 high_freq = s->sample_rate * 0.5; | |
260 | |
261 /* if version 2, then the rates are normalized */ | |
262 sample_rate1 = s->sample_rate; | |
263 if (s->version == 2) { | |
264 if (sample_rate1 >= 44100) | |
265 sample_rate1 = 44100; | |
266 else if (sample_rate1 >= 22050) | |
267 sample_rate1 = 22050; | |
268 else if (sample_rate1 >= 16000) | |
269 sample_rate1 = 16000; | |
270 else if (sample_rate1 >= 11025) | |
271 sample_rate1 = 11025; | |
272 else if (sample_rate1 >= 8000) | |
273 sample_rate1 = 8000; | |
274 } | |
275 | |
276 bps = (float)s->bit_rate / (float)(s->nb_channels * s->sample_rate); | |
277 s->byte_offset_bits = av_log2((int)(bps * s->frame_len / 8.0)) + 2; | |
278 | |
279 /* compute high frequency value and choose if noise coding should | |
280 be activated */ | |
281 bps1 = bps; | |
282 if (s->nb_channels == 2) | |
283 bps1 = bps * 1.6; | |
284 if (sample_rate1 == 44100) { | |
285 if (bps1 >= 0.61) | |
286 s->use_noise_coding = 0; | |
287 else | |
288 high_freq = high_freq * 0.4; | |
289 } else if (sample_rate1 == 22050) { | |
290 if (bps1 >= 1.16) | |
291 s->use_noise_coding = 0; | |
292 else if (bps1 >= 0.72) | |
293 high_freq = high_freq * 0.7; | |
294 else | |
295 high_freq = high_freq * 0.6; | |
296 } else if (sample_rate1 == 16000) { | |
297 if (bps > 0.5) | |
298 high_freq = high_freq * 0.5; | |
299 else | |
300 high_freq = high_freq * 0.3; | |
301 } else if (sample_rate1 == 11025) { | |
302 high_freq = high_freq * 0.7; | |
303 } else if (sample_rate1 == 8000) { | |
304 if (bps <= 0.625) { | |
305 high_freq = high_freq * 0.5; | |
306 } else if (bps > 0.75) { | |
307 s->use_noise_coding = 0; | |
308 } else { | |
309 high_freq = high_freq * 0.65; | |
310 } | |
311 } else { | |
312 if (bps >= 0.8) { | |
313 high_freq = high_freq * 0.75; | |
314 } else if (bps >= 0.6) { | |
315 high_freq = high_freq * 0.6; | |
316 } else { | |
317 high_freq = high_freq * 0.5; | |
318 } | |
319 } | |
320 dprintf("flags1=0x%x flags2=0x%x\n", flags1, flags2); | |
321 dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n", | |
322 s->version, s->nb_channels, s->sample_rate, s->bit_rate, | |
323 s->block_align); | |
324 dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n", | |
325 bps, bps1, high_freq, s->byte_offset_bits); | |
326 dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n", | |
327 s->use_noise_coding, s->use_exp_vlc, s->nb_block_sizes); | |
328 | |
329 /* compute the scale factor band sizes for each MDCT block size */ | |
330 { | |
331 int a, b, pos, lpos, k, block_len, i, j, n; | |
332 const uint8_t *table; | |
333 | |
334 if (s->version == 1) { | |
335 s->coefs_start = 3; | |
336 } else { | |
337 s->coefs_start = 0; | |
338 } | |
339 for(k = 0; k < s->nb_block_sizes; k++) { | |
340 block_len = s->frame_len >> k; | |
341 | |
342 if (s->version == 1) { | |
343 lpos = 0; | |
344 for(i=0;i<25;i++) { | |
345 a = wma_critical_freqs[i]; | |
346 b = s->sample_rate; | |
347 pos = ((block_len * 2 * a) + (b >> 1)) / b; | |
348 if (pos > block_len) | |
349 pos = block_len; | |
350 s->exponent_bands[0][i] = pos - lpos; | |
351 if (pos >= block_len) { | |
352 i++; | |
353 break; | |
354 } | |
355 lpos = pos; | |
356 } | |
357 s->exponent_sizes[0] = i; | |
358 } else { | |
359 /* hardcoded tables */ | |
360 table = NULL; | |
361 a = s->frame_len_bits - BLOCK_MIN_BITS - k; | |
362 if (a < 3) { | |
363 if (s->sample_rate >= 44100) | |
364 table = exponent_band_44100[a]; | |
365 else if (s->sample_rate >= 32000) | |
366 table = exponent_band_32000[a]; | |
367 else if (s->sample_rate >= 22050) | |
368 table = exponent_band_22050[a]; | |
369 } | |
370 if (table) { | |
371 n = *table++; | |
372 for(i=0;i<n;i++) | |
373 s->exponent_bands[k][i] = table[i]; | |
374 s->exponent_sizes[k] = n; | |
375 } else { | |
376 j = 0; | |
377 lpos = 0; | |
378 for(i=0;i<25;i++) { | |
379 a = wma_critical_freqs[i]; | |
380 b = s->sample_rate; | |
381 pos = ((block_len * 2 * a) + (b << 1)) / (4 * b); | |
382 pos <<= 2; | |
383 if (pos > block_len) | |
384 pos = block_len; | |
385 if (pos > lpos) | |
386 s->exponent_bands[k][j++] = pos - lpos; | |
387 if (pos >= block_len) | |
388 break; | |
389 lpos = pos; | |
390 } | |
391 s->exponent_sizes[k] = j; | |
392 } | |
393 } | |
394 | |
395 /* max number of coefs */ | |
396 s->coefs_end[k] = (s->frame_len - ((s->frame_len * 9) / 100)) >> k; | |
397 /* high freq computation */ | |
398 s->high_band_start[k] = (int)((block_len * 2 * high_freq) / | |
399 s->sample_rate + 0.5); | |
400 n = s->exponent_sizes[k]; | |
401 j = 0; | |
402 pos = 0; | |
403 for(i=0;i<n;i++) { | |
404 int start, end; | |
405 start = pos; | |
406 pos += s->exponent_bands[k][i]; | |
407 end = pos; | |
408 if (start < s->high_band_start[k]) | |
409 start = s->high_band_start[k]; | |
410 if (end > s->coefs_end[k]) | |
411 end = s->coefs_end[k]; | |
412 if (end > start) | |
413 s->exponent_high_bands[k][j++] = end - start; | |
414 } | |
415 s->exponent_high_sizes[k] = j; | |
416 #if 0 | |
417 tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ", | |
418 s->frame_len >> k, | |
419 s->coefs_end[k], | |
420 s->high_band_start[k], | |
421 s->exponent_high_sizes[k]); | |
422 for(j=0;j<s->exponent_high_sizes[k];j++) | |
423 tprintf(" %d", s->exponent_high_bands[k][j]); | |
424 tprintf("\n"); | |
425 #endif | |
426 } | |
427 } | |
428 | |
429 #ifdef TRACE | |
430 { | |
431 int i, j; | |
432 for(i = 0; i < s->nb_block_sizes; i++) { | |
433 tprintf("%5d: n=%2d:", | |
434 s->frame_len >> i, | |
435 s->exponent_sizes[i]); | |
436 for(j=0;j<s->exponent_sizes[i];j++) | |
437 tprintf(" %d", s->exponent_bands[i][j]); | |
438 tprintf("\n"); | |
439 } | |
440 } | |
441 #endif | |
442 | |
443 /* init MDCT */ | |
444 for(i = 0; i < s->nb_block_sizes; i++) | |
445 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1); | |
446 | |
447 /* init MDCT windows : simple sinus window */ | |
448 for(i = 0; i < s->nb_block_sizes; i++) { | |
449 int n, j; | |
450 float alpha; | |
451 n = 1 << (s->frame_len_bits - i); | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
452 window = malloc(sizeof(float) * n); |
137 | 453 alpha = M_PI / (2.0 * n); |
454 for(j=0;j<n;j++) { | |
455 window[n - j - 1] = sin((j + 0.5) * alpha); | |
456 } | |
457 s->windows[i] = window; | |
458 } | |
459 | |
460 s->reset_block_lengths = 1; | |
461 | |
462 if (s->use_noise_coding) { | |
463 | |
464 /* init the noise generator */ | |
465 if (s->use_exp_vlc) | |
466 s->noise_mult = 0.02; | |
467 else | |
468 s->noise_mult = 0.04; | |
469 | |
470 #ifdef TRACE | |
471 for(i=0;i<NOISE_TAB_SIZE;i++) | |
472 s->noise_table[i] = 1.0 * s->noise_mult; | |
473 #else | |
474 { | |
475 unsigned int seed; | |
476 float norm; | |
477 seed = 1; | |
478 norm = (1.0 / (float)(1LL << 31)) * sqrt(3) * s->noise_mult; | |
479 for(i=0;i<NOISE_TAB_SIZE;i++) { | |
480 seed = seed * 314159 + 1; | |
481 s->noise_table[i] = (float)((int)seed) * norm; | |
482 } | |
483 } | |
484 #endif | |
485 init_vlc(&s->hgain_vlc, 9, sizeof(hgain_huffbits), | |
486 hgain_huffbits, 1, 1, | |
487 hgain_huffcodes, 2, 2); | |
488 } | |
489 | |
490 if (s->use_exp_vlc) { | |
491 init_vlc(&s->exp_vlc, 9, sizeof(scale_huffbits), | |
492 scale_huffbits, 1, 1, | |
493 scale_huffcodes, 4, 4); | |
494 } else { | |
495 wma_lsp_to_curve_init(s, s->frame_len); | |
496 } | |
497 | |
498 /* choose the VLC tables for the coefficients */ | |
499 coef_vlc_table = 2; | |
500 if (s->sample_rate >= 32000) { | |
501 if (bps1 < 0.72) | |
502 coef_vlc_table = 0; | |
503 else if (bps1 < 1.16) | |
504 coef_vlc_table = 1; | |
505 } | |
506 | |
507 init_coef_vlc(&s->coef_vlc[0], &s->run_table[0], &s->level_table[0], | |
508 &coef_vlcs[coef_vlc_table * 2]); | |
509 init_coef_vlc(&s->coef_vlc[1], &s->run_table[1], &s->level_table[1], | |
510 &coef_vlcs[coef_vlc_table * 2 + 1]); | |
511 return 0; | |
512 } | |
513 | |
514 /* interpolate values for a bigger or smaller block. The block must | |
515 have multiple sizes */ | |
516 static void interpolate_array(float *scale, int old_size, int new_size) | |
517 { | |
518 int i, j, jincr, k; | |
519 float v; | |
520 | |
521 if (new_size > old_size) { | |
522 jincr = new_size / old_size; | |
523 j = new_size; | |
524 for(i = old_size - 1; i >=0; i--) { | |
525 v = scale[i]; | |
526 k = jincr; | |
527 do { | |
528 scale[--j] = v; | |
529 } while (--k); | |
530 } | |
531 } else if (new_size < old_size) { | |
532 j = 0; | |
533 jincr = old_size / new_size; | |
534 for(i = 0; i < new_size; i++) { | |
535 scale[i] = scale[j]; | |
536 j += jincr; | |
537 } | |
538 } | |
539 } | |
540 | |
541 /* compute x^-0.25 with an exponent and mantissa table. We use linear | |
542 interpolation to reduce the mantissa table size at a small speed | |
543 expense (linear interpolation approximately doubles the number of | |
544 bits of precision). */ | |
545 static inline float pow_m1_4(WMADecodeContext *s, float x) | |
546 { | |
547 union { | |
548 float f; | |
549 unsigned int v; | |
550 } u, t; | |
551 unsigned int e, m; | |
552 float a, b; | |
553 | |
554 u.f = x; | |
555 e = u.v >> 23; | |
556 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1); | |
557 /* build interpolation scale: 1 <= t < 2. */ | |
558 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23); | |
559 a = s->lsp_pow_m_table1[m]; | |
560 b = s->lsp_pow_m_table2[m]; | |
561 return s->lsp_pow_e_table[e] * (a + b * t.f); | |
562 } | |
563 | |
564 static void wma_lsp_to_curve_init(WMADecodeContext *s, int frame_len) | |
565 { | |
566 float wdel, a, b; | |
567 int i, e, m; | |
568 | |
569 wdel = M_PI / frame_len; | |
570 for(i=0;i<frame_len;i++) | |
571 s->lsp_cos_table[i] = 2.0f * cos(wdel * i); | |
572 | |
573 /* tables for x^-0.25 computation */ | |
574 for(i=0;i<256;i++) { | |
575 e = i - 126; | |
576 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25); | |
577 } | |
578 | |
579 /* NOTE: these two tables are needed to avoid two operations in | |
580 pow_m1_4 */ | |
581 b = 1.0; | |
582 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) { | |
583 m = (1 << LSP_POW_BITS) + i; | |
584 a = (float)m * (0.5 / (1 << LSP_POW_BITS)); | |
585 a = pow(a, -0.25); | |
586 s->lsp_pow_m_table1[i] = 2 * a - b; | |
587 s->lsp_pow_m_table2[i] = b - a; | |
588 b = a; | |
589 } | |
590 #if 0 | |
591 for(i=1;i<20;i++) { | |
592 float v, r1, r2; | |
593 v = 5.0 / i; | |
594 r1 = pow_m1_4(s, v); | |
595 r2 = pow(v,-0.25); | |
596 printf("%f^-0.25=%f e=%f\n", v, r1, r2 - r1); | |
597 } | |
598 #endif | |
599 } | |
600 | |
601 /* NOTE: We use the same code as Vorbis here */ | |
602 /* XXX: optimize it further with SSE/3Dnow */ | |
603 static void wma_lsp_to_curve(WMADecodeContext *s, | |
604 float *out, float *val_max_ptr, | |
605 int n, float *lsp) | |
606 { | |
607 int i, j; | |
608 float p, q, w, v, val_max; | |
609 | |
610 val_max = 0; | |
611 for(i=0;i<n;i++) { | |
612 p = 0.5f; | |
613 q = 0.5f; | |
614 w = s->lsp_cos_table[i]; | |
615 for(j=1;j<NB_LSP_COEFS;j+=2){ | |
616 q *= w - lsp[j - 1]; | |
617 p *= w - lsp[j]; | |
618 } | |
619 p *= p * (2.0f - w); | |
620 q *= q * (2.0f + w); | |
621 v = p + q; | |
622 v = pow_m1_4(s, v); | |
623 if (v > val_max) | |
624 val_max = v; | |
625 out[i] = v; | |
626 } | |
627 *val_max_ptr = val_max; | |
628 } | |
629 | |
630 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */ | |
631 static void decode_exp_lsp(WMADecodeContext *s, int ch) | |
632 { | |
633 float lsp_coefs[NB_LSP_COEFS]; | |
634 int val, i; | |
635 | |
636 for(i = 0; i < NB_LSP_COEFS; i++) { | |
637 if (i == 0 || i >= 8) | |
638 val = get_bits(&s->gb, 3); | |
639 else | |
640 val = get_bits(&s->gb, 4); | |
641 lsp_coefs[i] = lsp_codebook[i][val]; | |
642 } | |
643 | |
644 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch], | |
645 s->block_len, lsp_coefs); | |
646 } | |
647 | |
648 /* decode exponents coded with VLC codes */ | |
649 static int decode_exp_vlc(WMADecodeContext *s, int ch) | |
650 { | |
651 int last_exp, n, code; | |
652 const uint16_t *ptr, *band_ptr; | |
653 float v, *q, max_scale, *q_end; | |
654 | |
655 band_ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits]; | |
656 ptr = band_ptr; | |
657 q = s->exponents[ch]; | |
658 q_end = q + s->block_len; | |
659 max_scale = 0; | |
660 if (s->version == 1) { | |
661 last_exp = get_bits(&s->gb, 5) + 10; | |
662 /* XXX: use a table */ | |
663 v = pow(10, last_exp * (1.0 / 16.0)); | |
664 max_scale = v; | |
665 n = *ptr++; | |
666 do { | |
667 *q++ = v; | |
668 } while (--n); | |
669 } | |
670 last_exp = 36; | |
671 while (q < q_end) { | |
672 code = get_vlc(&s->gb, &s->exp_vlc); | |
673 if (code < 0) | |
674 return -1; | |
675 /* NOTE: this offset is the same as MPEG4 AAC ! */ | |
676 last_exp += code - 60; | |
677 /* XXX: use a table */ | |
678 v = pow(10, last_exp * (1.0 / 16.0)); | |
679 if (v > max_scale) | |
680 max_scale = v; | |
681 n = *ptr++; | |
682 do { | |
683 *q++ = v; | |
684 } while (--n); | |
685 } | |
686 s->max_exponent[ch] = max_scale; | |
687 return 0; | |
688 } | |
689 | |
690 /* return 0 if OK. return 1 if last block of frame. return -1 if | |
691 unrecorrable error. */ | |
692 static int wma_decode_block(WMADecodeContext *s) | |
693 { | |
694 int n, v, a, ch, code, bsize; | |
695 int coef_nb_bits, total_gain, parse_exponents; | |
696 float window[BLOCK_MAX_SIZE * 2]; | |
697 int nb_coefs[MAX_CHANNELS]; | |
698 float mdct_norm; | |
699 | |
700 #ifdef TRACE | |
701 tprintf("***decode_block: %d:%d\n", s->frame_count - 1, s->block_num); | |
702 #endif | |
703 | |
704 /* compute current block length */ | |
705 if (s->use_variable_block_len) { | |
706 n = av_log2(s->nb_block_sizes - 1) + 1; | |
707 | |
708 if (s->reset_block_lengths) { | |
709 s->reset_block_lengths = 0; | |
710 v = get_bits(&s->gb, n); | |
711 if (v >= s->nb_block_sizes) | |
712 return -1; | |
713 s->prev_block_len_bits = s->frame_len_bits - v; | |
714 v = get_bits(&s->gb, n); | |
715 if (v >= s->nb_block_sizes) | |
716 return -1; | |
717 s->block_len_bits = s->frame_len_bits - v; | |
718 } else { | |
719 /* update block lengths */ | |
720 s->prev_block_len_bits = s->block_len_bits; | |
721 s->block_len_bits = s->next_block_len_bits; | |
722 } | |
723 v = get_bits(&s->gb, n); | |
724 if (v >= s->nb_block_sizes) | |
725 return -1; | |
726 s->next_block_len_bits = s->frame_len_bits - v; | |
727 } else { | |
728 /* fixed block len */ | |
729 s->next_block_len_bits = s->frame_len_bits; | |
730 s->prev_block_len_bits = s->frame_len_bits; | |
731 s->block_len_bits = s->frame_len_bits; | |
732 } | |
733 | |
734 /* now check if the block length is coherent with the frame length */ | |
735 s->block_len = 1 << s->block_len_bits; | |
736 if ((s->block_pos + s->block_len) > s->frame_len) | |
737 return -1; | |
738 | |
739 if (s->nb_channels == 2) { | |
740 s->ms_stereo = get_bits(&s->gb, 1); | |
741 } | |
742 v = 0; | |
743 for(ch = 0; ch < s->nb_channels; ch++) { | |
744 a = get_bits(&s->gb, 1); | |
745 s->channel_coded[ch] = a; | |
746 v |= a; | |
747 } | |
748 /* if no channel coded, no need to go further */ | |
749 /* XXX: fix potential framing problems */ | |
750 if (!v) | |
751 goto next; | |
752 | |
753 bsize = s->frame_len_bits - s->block_len_bits; | |
754 | |
755 /* read total gain and extract corresponding number of bits for | |
756 coef escape coding */ | |
757 total_gain = 1; | |
758 for(;;) { | |
759 a = get_bits(&s->gb, 7); | |
760 total_gain += a; | |
761 if (a != 127) | |
762 break; | |
763 } | |
764 | |
765 if (total_gain < 15) | |
766 coef_nb_bits = 13; | |
767 else if (total_gain < 32) | |
768 coef_nb_bits = 12; | |
769 else if (total_gain < 40) | |
770 coef_nb_bits = 11; | |
771 else if (total_gain < 45) | |
772 coef_nb_bits = 10; | |
773 else | |
774 coef_nb_bits = 9; | |
775 | |
776 /* compute number of coefficients */ | |
777 n = s->coefs_end[bsize] - s->coefs_start; | |
778 for(ch = 0; ch < s->nb_channels; ch++) | |
779 nb_coefs[ch] = n; | |
780 | |
781 /* complex coding */ | |
782 if (s->use_noise_coding) { | |
783 | |
784 for(ch = 0; ch < s->nb_channels; ch++) { | |
785 if (s->channel_coded[ch]) { | |
786 int i, n, a; | |
787 n = s->exponent_high_sizes[bsize]; | |
788 for(i=0;i<n;i++) { | |
789 a = get_bits(&s->gb, 1); | |
790 s->high_band_coded[ch][i] = a; | |
791 /* if noise coding, the coefficients are not transmitted */ | |
792 if (a) | |
793 nb_coefs[ch] -= s->exponent_high_bands[bsize][i]; | |
794 } | |
795 } | |
796 } | |
797 for(ch = 0; ch < s->nb_channels; ch++) { | |
798 if (s->channel_coded[ch]) { | |
799 int i, n, val, code; | |
800 | |
801 n = s->exponent_high_sizes[bsize]; | |
802 val = (int)0x80000000; | |
803 for(i=0;i<n;i++) { | |
804 if (s->high_band_coded[ch][i]) { | |
805 if (val == (int)0x80000000) { | |
806 val = get_bits(&s->gb, 7) - 19; | |
807 } else { | |
808 code = get_vlc(&s->gb, &s->hgain_vlc); | |
809 if (code < 0) | |
810 return -1; | |
811 val += code - 18; | |
812 } | |
813 s->high_band_values[ch][i] = val; | |
814 } | |
815 } | |
816 } | |
817 } | |
818 } | |
819 | |
820 /* exposant can be interpolated in short blocks. */ | |
821 parse_exponents = 1; | |
822 if (s->block_len_bits != s->frame_len_bits) { | |
823 parse_exponents = get_bits(&s->gb, 1); | |
824 } | |
825 | |
826 if (parse_exponents) { | |
827 for(ch = 0; ch < s->nb_channels; ch++) { | |
828 if (s->channel_coded[ch]) { | |
829 if (s->use_exp_vlc) { | |
830 if (decode_exp_vlc(s, ch) < 0) | |
831 return -1; | |
832 } else { | |
833 decode_exp_lsp(s, ch); | |
834 } | |
835 } | |
836 } | |
837 } else { | |
838 for(ch = 0; ch < s->nb_channels; ch++) { | |
839 if (s->channel_coded[ch]) { | |
840 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits, | |
841 s->block_len); | |
842 } | |
843 } | |
844 } | |
845 | |
846 /* parse spectral coefficients : just RLE encoding */ | |
847 for(ch = 0; ch < s->nb_channels; ch++) { | |
848 if (s->channel_coded[ch]) { | |
849 VLC *coef_vlc; | |
850 int level, run, sign, tindex; | |
851 int16_t *ptr, *eptr; | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
852 const int16_t *level_table, *run_table; |
137 | 853 |
854 /* special VLC tables are used for ms stereo because | |
855 there is potentially less energy there */ | |
856 tindex = (ch == 1 && s->ms_stereo); | |
857 coef_vlc = &s->coef_vlc[tindex]; | |
858 run_table = s->run_table[tindex]; | |
859 level_table = s->level_table[tindex]; | |
860 /* XXX: optimize */ | |
861 ptr = &s->coefs1[ch][0]; | |
862 eptr = ptr + nb_coefs[ch]; | |
863 memset(ptr, 0, s->block_len * sizeof(int16_t)); | |
864 for(;;) { | |
865 code = get_vlc(&s->gb, coef_vlc); | |
866 if (code < 0) | |
867 return -1; | |
868 if (code == 1) { | |
869 /* EOB */ | |
870 break; | |
871 } else if (code == 0) { | |
872 /* escape */ | |
873 level = get_bits(&s->gb, coef_nb_bits); | |
874 /* NOTE: this is rather suboptimal. reading | |
875 block_len_bits would be better */ | |
876 run = get_bits(&s->gb, s->frame_len_bits); | |
877 } else { | |
878 /* normal code */ | |
879 run = run_table[code]; | |
880 level = level_table[code]; | |
881 } | |
882 sign = get_bits(&s->gb, 1); | |
883 if (!sign) | |
884 level = -level; | |
885 ptr += run; | |
886 if (ptr >= eptr) | |
887 return -1; | |
888 *ptr++ = level; | |
889 /* NOTE: EOB can be omitted */ | |
890 if (ptr >= eptr) | |
891 break; | |
892 } | |
893 } | |
894 if (s->version == 1 && s->nb_channels >= 2) { | |
895 align_get_bits(&s->gb); | |
896 } | |
897 } | |
898 | |
899 /* normalize */ | |
900 { | |
901 int n4 = s->block_len / 2; | |
902 mdct_norm = 1.0 / (float)n4; | |
903 if (s->version == 1) { | |
904 mdct_norm *= sqrt(n4); | |
905 } | |
906 } | |
907 | |
908 /* finally compute the MDCT coefficients */ | |
909 for(ch = 0; ch < s->nb_channels; ch++) { | |
910 if (s->channel_coded[ch]) { | |
911 int16_t *coefs1; | |
912 float *coefs, *exponents, mult, mult1, noise, *exp_ptr; | |
913 int i, j, n, n1, last_high_band; | |
914 float exp_power[HIGH_BAND_MAX_SIZE]; | |
915 | |
916 coefs1 = s->coefs1[ch]; | |
917 exponents = s->exponents[ch]; | |
918 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; | |
919 mult *= mdct_norm; | |
920 coefs = s->coefs[ch]; | |
921 if (s->use_noise_coding) { | |
922 mult1 = mult; | |
923 /* very low freqs : noise */ | |
924 for(i = 0;i < s->coefs_start; i++) { | |
925 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1; | |
926 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
927 } | |
928 | |
929 n1 = s->exponent_high_sizes[bsize]; | |
930 | |
931 /* compute power of high bands */ | |
932 exp_ptr = exponents + | |
933 s->high_band_start[bsize] - | |
934 s->coefs_start; | |
935 last_high_band = 0; /* avoid warning */ | |
936 for(j=0;j<n1;j++) { | |
937 n = s->exponent_high_bands[s->frame_len_bits - | |
938 s->block_len_bits][j]; | |
939 if (s->high_band_coded[ch][j]) { | |
940 float e2, v; | |
941 e2 = 0; | |
942 for(i = 0;i < n; i++) { | |
943 v = exp_ptr[i]; | |
944 e2 += v * v; | |
945 } | |
946 exp_power[j] = e2 / n; | |
947 last_high_band = j; | |
948 tprintf("%d: power=%f (%d)\n", j, exp_power[j], n); | |
949 } | |
950 exp_ptr += n; | |
951 } | |
952 | |
953 /* main freqs and high freqs */ | |
954 for(j=-1;j<n1;j++) { | |
955 if (j < 0) { | |
956 n = s->high_band_start[bsize] - | |
957 s->coefs_start; | |
958 } else { | |
959 n = s->exponent_high_bands[s->frame_len_bits - | |
960 s->block_len_bits][j]; | |
961 } | |
962 if (j >= 0 && s->high_band_coded[ch][j]) { | |
963 /* use noise with specified power */ | |
964 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]); | |
965 /* XXX: use a table */ | |
966 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05); | |
967 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); | |
968 mult1 *= mdct_norm; | |
969 for(i = 0;i < n; i++) { | |
970 noise = s->noise_table[s->noise_index]; | |
971 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
972 *coefs++ = (*exponents++) * noise * mult1; | |
973 } | |
974 } else { | |
975 /* coded values + small noise */ | |
976 for(i = 0;i < n; i++) { | |
977 noise = s->noise_table[s->noise_index]; | |
978 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
979 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult; | |
980 } | |
981 } | |
982 } | |
983 | |
984 /* very high freqs : noise */ | |
985 n = s->block_len - s->coefs_end[bsize]; | |
986 mult1 = mult * exponents[-1]; | |
987 for(i = 0; i < n; i++) { | |
988 *coefs++ = s->noise_table[s->noise_index] * mult1; | |
989 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); | |
990 } | |
991 } else { | |
992 /* XXX: optimize more */ | |
993 for(i = 0;i < s->coefs_start; i++) | |
994 *coefs++ = 0.0; | |
995 n = nb_coefs[ch]; | |
996 for(i = 0;i < n; i++) { | |
997 *coefs++ = coefs1[i] * exponents[i] * mult; | |
998 } | |
999 n = s->block_len - s->coefs_end[bsize]; | |
1000 for(i = 0;i < n; i++) | |
1001 *coefs++ = 0.0; | |
1002 } | |
1003 } | |
1004 } | |
1005 | |
1006 #ifdef TRACE | |
1007 for(ch = 0; ch < s->nb_channels; ch++) { | |
1008 if (s->channel_coded[ch]) { | |
1009 dump_floats("exponents", 3, s->exponents[ch], s->block_len); | |
1010 dump_floats("coefs", 1, s->coefs[ch], s->block_len); | |
1011 } | |
1012 } | |
1013 #endif | |
1014 | |
1015 if (s->ms_stereo && s->channel_coded[1]) { | |
1016 float a, b; | |
1017 int i; | |
1018 | |
1019 /* nominal case for ms stereo: we do it before mdct */ | |
1020 /* no need to optimize this case because it should almost | |
1021 never happen */ | |
1022 if (!s->channel_coded[0]) { | |
1023 tprintf("rare ms-stereo case happened\n"); | |
1024 memset(s->coefs[0], 0, sizeof(float) * s->block_len); | |
1025 s->channel_coded[0] = 1; | |
1026 } | |
1027 | |
1028 for(i = 0; i < s->block_len; i++) { | |
1029 a = s->coefs[0][i]; | |
1030 b = s->coefs[1][i]; | |
1031 s->coefs[0][i] = a + b; | |
1032 s->coefs[1][i] = a - b; | |
1033 } | |
1034 } | |
1035 | |
1036 /* build the window : we ensure that when the windows overlap | |
1037 their squared sum is always 1 (MDCT reconstruction rule) */ | |
1038 /* XXX: merge with output */ | |
1039 { | |
1040 int i, next_block_len, block_len, prev_block_len, n; | |
1041 float *wptr; | |
1042 | |
1043 block_len = s->block_len; | |
1044 prev_block_len = 1 << s->prev_block_len_bits; | |
1045 next_block_len = 1 << s->next_block_len_bits; | |
1046 | |
1047 /* right part */ | |
1048 wptr = window + block_len; | |
1049 if (block_len <= next_block_len) { | |
1050 for(i=0;i<block_len;i++) | |
1051 *wptr++ = s->windows[bsize][i]; | |
1052 } else { | |
1053 /* overlap */ | |
1054 n = (block_len / 2) - (next_block_len / 2); | |
1055 for(i=0;i<n;i++) | |
1056 *wptr++ = 1.0; | |
1057 for(i=0;i<next_block_len;i++) | |
1058 *wptr++ = s->windows[s->frame_len_bits - s->next_block_len_bits][i]; | |
1059 for(i=0;i<n;i++) | |
1060 *wptr++ = 0.0; | |
1061 } | |
1062 | |
1063 /* left part */ | |
1064 wptr = window + block_len; | |
1065 if (block_len <= prev_block_len) { | |
1066 for(i=0;i<block_len;i++) | |
1067 *--wptr = s->windows[bsize][i]; | |
1068 } else { | |
1069 /* overlap */ | |
1070 n = (block_len / 2) - (prev_block_len / 2); | |
1071 for(i=0;i<n;i++) | |
1072 *--wptr = 1.0; | |
1073 for(i=0;i<prev_block_len;i++) | |
1074 *--wptr = s->windows[s->frame_len_bits - s->prev_block_len_bits][i]; | |
1075 for(i=0;i<n;i++) | |
1076 *--wptr = 0.0; | |
1077 } | |
1078 } | |
1079 | |
1080 | |
1081 for(ch = 0; ch < s->nb_channels; ch++) { | |
1082 if (s->channel_coded[ch]) { | |
1083 FFTSample output[BLOCK_MAX_SIZE * 2] __attribute__((aligned(16))); | |
1084 float *ptr; | |
1085 int i, n4, index, n; | |
1086 | |
1087 n = s->block_len; | |
1088 n4 = s->block_len / 2; | |
1089 ff_imdct_calc(&s->mdct_ctx[bsize], | |
1090 output, s->coefs[ch], s->mdct_tmp); | |
1091 | |
1092 /* XXX: optimize all that by build the window and | |
1093 multipying/adding at the same time */ | |
1094 /* multiply by the window */ | |
1095 for(i=0;i<n * 2;i++) { | |
1096 output[i] *= window[i]; | |
1097 } | |
1098 | |
1099 /* add in the frame */ | |
1100 index = (s->frame_len / 2) + s->block_pos - n4; | |
1101 ptr = &s->frame_out[ch][index]; | |
1102 for(i=0;i<n * 2;i++) { | |
1103 *ptr += output[i]; | |
1104 ptr++; | |
1105 } | |
1106 | |
1107 /* specific fast case for ms-stereo : add to second | |
1108 channel if it is not coded */ | |
1109 if (s->ms_stereo && !s->channel_coded[1]) { | |
1110 ptr = &s->frame_out[1][index]; | |
1111 for(i=0;i<n * 2;i++) { | |
1112 *ptr += output[i]; | |
1113 ptr++; | |
1114 } | |
1115 } | |
1116 } | |
1117 } | |
1118 next: | |
1119 /* update block number */ | |
1120 s->block_num++; | |
1121 s->block_pos += s->block_len; | |
1122 if (s->block_pos >= s->frame_len) | |
1123 return 1; | |
1124 else | |
1125 return 0; | |
1126 } | |
1127 | |
1128 /* decode a frame of frame_len samples */ | |
1129 static int wma_decode_frame(WMADecodeContext *s, int16_t *samples) | |
1130 { | |
1131 int ret, i, n, a, ch, incr; | |
1132 int16_t *ptr; | |
1133 float *iptr; | |
1134 | |
1135 #ifdef TRACE | |
1136 tprintf("***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len); | |
1137 #endif | |
1138 | |
1139 /* read each block */ | |
1140 s->block_num = 0; | |
1141 s->block_pos = 0; | |
1142 for(;;) { | |
1143 ret = wma_decode_block(s); | |
1144 if (ret < 0) | |
1145 return -1; | |
1146 if (ret) | |
1147 break; | |
1148 } | |
1149 | |
1150 /* convert frame to integer */ | |
1151 n = s->frame_len; | |
1152 incr = s->nb_channels; | |
1153 for(ch = 0; ch < s->nb_channels; ch++) { | |
1154 ptr = samples + ch; | |
1155 iptr = s->frame_out[ch]; | |
1156 | |
1157 for(i=0;i<n;i++) { | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1158 a = rintf(*iptr++); |
137 | 1159 if (a > 32767) |
1160 a = 32767; | |
1161 else if (a < -32768) | |
1162 a = -32768; | |
1163 *ptr = a; | |
1164 ptr += incr; | |
1165 } | |
1166 /* prepare for next block */ | |
1167 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len], | |
1168 s->frame_len * sizeof(float)); | |
1169 /* XXX: suppress this */ | |
1170 memset(&s->frame_out[ch][s->frame_len], 0, | |
1171 s->frame_len * sizeof(float)); | |
1172 } | |
1173 | |
1174 #ifdef TRACE | |
1175 dump_shorts("samples", samples, n * s->nb_channels); | |
1176 #endif | |
1177 return 0; | |
1178 } | |
1179 | |
1180 static int wma_decode_superframe(AVCodecContext *avctx, | |
1181 void *data, int *data_size, | |
1182 uint8_t *buf, int buf_size) | |
1183 { | |
1184 WMADecodeContext *s = avctx->priv_data; | |
1185 int nb_frames, bit_offset, i, pos, len; | |
1186 uint8_t *q; | |
1187 int16_t *samples; | |
1188 | |
1189 tprintf("***decode_superframe:\n"); | |
1190 | |
1191 if(buf_size==0){ | |
1192 s->last_superframe_len = 0; | |
1193 return 0; | |
1194 } | |
1195 | |
1196 samples = data; | |
1197 | |
1198 init_get_bits(&s->gb, buf, buf_size*8); | |
1199 | |
1200 if (s->use_bit_reservoir) { | |
1201 /* read super frame header */ | |
1202 get_bits(&s->gb, 4); /* super frame index */ | |
1203 nb_frames = get_bits(&s->gb, 4) - 1; | |
1204 | |
1205 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3); | |
1206 | |
1207 if (s->last_superframe_len > 0) { | |
1208 // printf("skip=%d\n", s->last_bitoffset); | |
1209 /* add bit_offset bits to last frame */ | |
1210 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) > | |
1211 MAX_CODED_SUPERFRAME_SIZE) | |
1212 goto fail; | |
1213 q = s->last_superframe + s->last_superframe_len; | |
1214 len = bit_offset; | |
1215 while (len > 0) { | |
1216 *q++ = (get_bits)(&s->gb, 8); | |
1217 len -= 8; | |
1218 } | |
1219 if (len > 0) { | |
1220 *q++ = (get_bits)(&s->gb, len) << (8 - len); | |
1221 } | |
1222 | |
1223 /* XXX: bit_offset bits into last frame */ | |
1224 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8); | |
1225 /* skip unused bits */ | |
1226 if (s->last_bitoffset > 0) | |
1227 skip_bits(&s->gb, s->last_bitoffset); | |
1228 /* this frame is stored in the last superframe and in the | |
1229 current one */ | |
1230 if (wma_decode_frame(s, samples) < 0) | |
1231 goto fail; | |
1232 samples += s->nb_channels * s->frame_len; | |
1233 } | |
1234 | |
1235 /* read each frame starting from bit_offset */ | |
1236 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3; | |
1237 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8); | |
1238 len = pos & 7; | |
1239 if (len > 0) | |
1240 skip_bits(&s->gb, len); | |
1241 | |
1242 s->reset_block_lengths = 1; | |
1243 for(i=0;i<nb_frames;i++) { | |
1244 if (wma_decode_frame(s, samples) < 0) | |
1245 goto fail; | |
1246 samples += s->nb_channels * s->frame_len; | |
1247 } | |
1248 | |
1249 /* we copy the end of the frame in the last frame buffer */ | |
1250 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7); | |
1251 s->last_bitoffset = pos & 7; | |
1252 pos >>= 3; | |
1253 len = buf_size - pos; | |
1254 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) { | |
1255 goto fail; | |
1256 } | |
1257 s->last_superframe_len = len; | |
1258 memcpy(s->last_superframe, buf + pos, len); | |
1259 } else { | |
1260 /* single frame decode */ | |
1261 if (wma_decode_frame(s, samples) < 0) | |
1262 goto fail; | |
1263 samples += s->nb_channels * s->frame_len; | |
1264 } | |
1265 *data_size = (int8_t *)samples - (int8_t *)data; | |
1266 return s->block_align; | |
1267 fail: | |
1268 /* when error, we reset the bit reservoir */ | |
1269 s->last_superframe_len = 0; | |
1270 return -1; | |
1271 } | |
1272 | |
1273 static int wma_decode_end(AVCodecContext *avctx) | |
1274 { | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1275 WMADecodeContext *s = avctx->priv_data; |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1276 int i; |
137 | 1277 |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1278 for (i = 0; i < s->nb_block_sizes; i++) |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1279 ff_mdct_end(&s->mdct_ctx[i]); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1280 |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1281 for (i = 0; i < s->nb_block_sizes; i++) |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1282 free(s->windows[i]); |
137 | 1283 |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1284 if (s->use_exp_vlc) { |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1285 free_vlc(&s->exp_vlc); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1286 } |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1287 |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1288 if (s->use_noise_coding) { |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1289 free_vlc(&s->hgain_vlc); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1290 } |
137 | 1291 |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1292 for (i = 0; i < 2; i++) { |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1293 free_vlc(&s->coef_vlc[i]); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1294 free(s->run_table[i]); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1295 free(s->level_table[i]); |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1296 } |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1297 |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1298 return 0; |
137 | 1299 } |
1300 | |
1301 AVCodec wmav1_decoder = | |
1302 { | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1303 "wmav1", |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1304 CODEC_TYPE_AUDIO, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1305 CODEC_ID_WMAV1, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1306 sizeof(WMADecodeContext), |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1307 wma_decode_init, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1308 NULL, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1309 wma_decode_end, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1310 wma_decode_superframe, |
137 | 1311 }; |
1312 | |
1313 AVCodec wmav2_decoder = | |
1314 { | |
218
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1315 "wmav2", |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1316 CODEC_TYPE_AUDIO, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1317 CODEC_ID_WMAV2, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1318 sizeof(WMADecodeContext), |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1319 wma_decode_init, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1320 NULL, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1321 wma_decode_end, |
0bea7509d6ba
[svn] Working WMA support. I never said it would be pretty, neno, I should said it would play.
chainsaw
parents:
210
diff
changeset
|
1322 wma_decode_superframe, |
137 | 1323 }; |