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