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