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