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