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