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