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