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