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