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