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