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