wma_decode_block(): Kill useless variable 'sign'.
[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 struct vlc *coef_vlc;
826 int level, run, tindex;
827 int16_t *ptr, *eptr;
828 const uint16_t *level_table, *run_table;
829
830 if (!pwd->channel_coded[ch])
831 continue;
832 /*
833 * special VLC tables are used for ms stereo because there is
834 * potentially less energy there
835 */
836 tindex = (ch == 1 && pwd->ms_stereo);
837 coef_vlc = &pwd->coef_vlc[tindex];
838 run_table = pwd->run_table[tindex];
839 level_table = pwd->level_table[tindex];
840 /* XXX: optimize */
841 ptr = &pwd->coefs1[ch][0];
842 eptr = ptr + nb_coefs[ch];
843 memset(ptr, 0, pwd->block_len * sizeof(int16_t));
844 for (;;) {
845 code = get_vlc(&pwd->gb, coef_vlc->table,
846 VLCBITS, VLCMAX);
847 if (code < 0)
848 return -1;
849 if (code == 1) /* EOB */
850 break;
851 if (code == 0) { /* escape */
852 level = get_bits(&pwd->gb, coef_nb_bits);
853 /* reading block_len_bits would be better */
854 run = get_bits(&pwd->gb, pwd->frame_len_bits);
855 } else { /* normal code */
856 run = run_table[code];
857 level = level_table[code];
858 }
859 if (!get_bit(&pwd->gb))
860 level = -level;
861 ptr += run;
862 if (ptr >= eptr) {
863 PARA_ERROR_LOG("overflow in spectral RLE, ignoring\n");
864 break;
865 }
866 *ptr++ = level;
867 if (ptr >= eptr) /* EOB can be omitted */
868 break;
869 }
870 }
871
872 /* normalize */
873 {
874 int n4 = pwd->block_len / 2;
875 mdct_norm = 1.0 / (float) n4;
876 }
877
878 /* finally compute the MDCT coefficients */
879 for (ch = 0; ch < pwd->ahi.channels; ch++) {
880 if (pwd->channel_coded[ch]) {
881 int16_t *coefs1;
882 float *coefs, *exponents, mult, mult1, noise;
883 int i, j, n1, last_high_band, esize;
884 float exp_power[HIGH_BAND_MAX_SIZE];
885
886 coefs1 = pwd->coefs1[ch];
887 exponents = pwd->exponents[ch];
888 esize = pwd->exponents_bsize[ch];
889 mult = pow(10, total_gain * 0.05) / pwd->max_exponent[ch];
890 mult *= mdct_norm;
891 coefs = pwd->coefs[ch];
892 if (pwd->use_noise_coding) {
893 mult1 = mult;
894 /* very low freqs : noise */
895 for (i = 0; i < pwd->coefs_start; i++) {
896 *coefs++ =
897 pwd->noise_table[pwd->noise_index] *
898 exponents[i << bsize >> esize] *
899 mult1;
900 pwd->noise_index =
901 (pwd->noise_index +
902 1) & (NOISE_TAB_SIZE - 1);
903 }
904
905 n1 = pwd->exponent_high_sizes[bsize];
906
907 /* compute power of high bands */
908 exponents = pwd->exponents[ch] +
909 (pwd->high_band_start[bsize] << bsize);
910 last_high_band = 0; /* avoid warning */
911 for (j = 0; j < n1; j++) {
912 n = pwd->exponent_high_bands[pwd->
913 frame_len_bits
914 -
915 pwd->
916 block_len_bits]
917 [j];
918 if (pwd->high_band_coded[ch][j]) {
919 float e2, val;
920 e2 = 0;
921 for (i = 0; i < n; i++) {
922 val = exponents[i << bsize
923 >> esize];
924 e2 += val * val;
925 }
926 exp_power[j] = e2 / n;
927 last_high_band = j;
928 }
929 exponents += n << bsize;
930 }
931
932 /* main freqs and high freqs */
933 exponents =
934 pwd->exponents[ch] +
935 (pwd->coefs_start << bsize);
936 for (j = -1; j < n1; j++) {
937 if (j < 0) {
938 n = pwd->high_band_start[bsize] -
939 pwd->coefs_start;
940 } else {
941 n = pwd->exponent_high_bands[pwd->
942 frame_len_bits
943 -
944 pwd->
945 block_len_bits]
946 [j];
947 }
948 if (j >= 0 && pwd->high_band_coded[ch][j]) {
949 /* use noise with specified power */
950 mult1 =
951 sqrt(exp_power[j] /
952 exp_power
953 [last_high_band]);
954 /* XXX: use a table */
955 mult1 =
956 mult1 * pow(10,
957 pwd->
958 high_band_values
959 [ch][j] * 0.05);
960 mult1 =
961 mult1 /
962 (pwd->max_exponent[ch] *
963 pwd->noise_mult);
964 mult1 *= mdct_norm;
965 for (i = 0; i < n; i++) {
966 noise =
967 pwd->noise_table[pwd->
968 noise_index];
969 pwd->noise_index =
970 (pwd->noise_index +
971 1) &
972 (NOISE_TAB_SIZE -
973 1);
974 *coefs++ =
975 noise *
976 exponents[i << bsize
977 >> esize]
978 * mult1;
979 }
980 exponents += n << bsize;
981 } else {
982 /* coded values + small noise */
983 for (i = 0; i < n; i++) {
984 noise =
985 pwd->noise_table[pwd->
986 noise_index];
987 pwd->noise_index =
988 (pwd->noise_index +
989 1) &
990 (NOISE_TAB_SIZE -
991 1);
992 *coefs++ =
993 ((*coefs1++) +
994 noise) *
995 exponents[i << bsize
996 >> esize]
997 * mult;
998 }
999 exponents += n << bsize;
1000 }
1001 }
1002
1003 /* very high freqs : noise */
1004 n = pwd->block_len - pwd->coefs_end[bsize];
1005 mult1 =
1006 mult * exponents[((-1 << bsize)) >> esize];
1007 for (i = 0; i < n; i++) {
1008 *coefs++ =
1009 pwd->noise_table[pwd->noise_index] *
1010 mult1;
1011 pwd->noise_index =
1012 (pwd->noise_index +
1013 1) & (NOISE_TAB_SIZE - 1);
1014 }
1015 } else {
1016 /* XXX: optimize more */
1017 for (i = 0; i < pwd->coefs_start; i++)
1018 *coefs++ = 0.0;
1019 n = nb_coefs[ch];
1020 for (i = 0; i < n; i++) {
1021 *coefs++ =
1022 coefs1[i] *
1023 exponents[i << bsize >> esize] *
1024 mult;
1025 }
1026 n = pwd->block_len - pwd->coefs_end[bsize];
1027 for (i = 0; i < n; i++)
1028 *coefs++ = 0.0;
1029 }
1030 }
1031 }
1032
1033 if (pwd->ms_stereo && pwd->channel_coded[1]) {
1034 float a, b;
1035 int i;
1036
1037 /*
1038 * Nominal case for ms stereo: we do it before mdct.
1039 *
1040 * No need to optimize this case because it should almost never
1041 * happen.
1042 */
1043 if (!pwd->channel_coded[0]) {
1044 PARA_NOTICE_LOG("rare ms-stereo\n");
1045 memset(pwd->coefs[0], 0, sizeof(float) * pwd->block_len);
1046 pwd->channel_coded[0] = 1;
1047 }
1048 for (i = 0; i < pwd->block_len; i++) {
1049 a = pwd->coefs[0][i];
1050 b = pwd->coefs[1][i];
1051 pwd->coefs[0][i] = a + b;
1052 pwd->coefs[1][i] = a - b;
1053 }
1054 }
1055
1056 next:
1057 for (ch = 0; ch < pwd->ahi.channels; ch++) {
1058 int n4, index;
1059
1060 n = pwd->block_len;
1061 n4 = pwd->block_len / 2;
1062 if (pwd->channel_coded[ch])
1063 imdct(pwd->mdct_ctx[bsize], pwd->output, pwd->coefs[ch]);
1064 else if (!(pwd->ms_stereo && ch == 1))
1065 memset(pwd->output, 0, sizeof(pwd->output));
1066
1067 /* multiply by the window and add in the frame */
1068 index = (pwd->frame_len / 2) + pwd->block_pos - n4;
1069 wma_window(pwd, &pwd->frame_out[ch][index]);
1070 }
1071
1072 /* update block number */
1073 pwd->block_pos += pwd->block_len;
1074 if (pwd->block_pos >= pwd->frame_len)
1075 return 1;
1076 else
1077 return 0;
1078 }
1079
1080 /*
1081 * Clip a signed integer value into the -32768,32767 range.
1082 *
1083 * \param a The value to clip.
1084 *
1085 * \return The clipped value.
1086 */
1087 static inline int16_t av_clip_int16(int a)
1088 {
1089 if ((a + 32768) & ~65535)
1090 return (a >> 31) ^ 32767;
1091 else
1092 return a;
1093 }
1094
1095 /* Decode a frame of frame_len samples. */
1096 static int wma_decode_frame(struct private_wmadec_data *pwd, int16_t *samples)
1097 {
1098 int ret, i, n, ch, incr;
1099 int16_t *ptr;
1100 float *iptr;
1101
1102 /* read each block */
1103 pwd->block_pos = 0;
1104 for (;;) {
1105 ret = wma_decode_block(pwd);
1106 if (ret < 0)
1107 return -1;
1108 if (ret)
1109 break;
1110 }
1111
1112 /* convert frame to integer */
1113 n = pwd->frame_len;
1114 incr = pwd->ahi.channels;
1115 for (ch = 0; ch < pwd->ahi.channels; ch++) {
1116 ptr = samples + ch;
1117 iptr = pwd->frame_out[ch];
1118
1119 for (i = 0; i < n; i++) {
1120 *ptr = av_clip_int16(lrintf(*iptr++));
1121 ptr += incr;
1122 }
1123 /* prepare for next block */
1124 memmove(&pwd->frame_out[ch][0], &pwd->frame_out[ch][pwd->frame_len],
1125 pwd->frame_len * sizeof(float));
1126 }
1127 return 0;
1128 }
1129
1130 static int wma_decode_superframe(struct private_wmadec_data *pwd, void *data,
1131 int *data_size, const uint8_t *buf, int buf_size)
1132 {
1133 int ret;
1134 int16_t *samples;
1135 static int frame_count;
1136
1137 if (buf_size == 0) {
1138 pwd->last_superframe_len = 0;
1139 return 0;
1140 }
1141 if (buf_size < pwd->ahi.block_align)
1142 return 0;
1143 buf_size = pwd->ahi.block_align;
1144 samples = data;
1145 init_get_bits(&pwd->gb, buf, buf_size);
1146 if (pwd->use_bit_reservoir) {
1147 int i, nb_frames, bit_offset, pos, len;
1148 uint8_t *q;
1149
1150 /* read super frame header */
1151 skip_bits(&pwd->gb, 4); /* super frame index */
1152 nb_frames = get_bits(&pwd->gb, 4) - 1;
1153 // PARA_DEBUG_LOG("have %d frames\n", nb_frames);
1154 ret = -E_WMA_OUTPUT_SPACE;
1155 if ((nb_frames + 1) * pwd->ahi.channels * pwd->frame_len
1156 * sizeof(int16_t) > *data_size)
1157 goto fail;
1158
1159 bit_offset = get_bits(&pwd->gb, pwd->byte_offset_bits + 3);
1160
1161 if (pwd->last_superframe_len > 0) {
1162 /* add bit_offset bits to last frame */
1163 ret = -E_WMA_BAD_SUPERFRAME;
1164 if ((pwd->last_superframe_len + ((bit_offset + 7) >> 3)) >
1165 MAX_CODED_SUPERFRAME_SIZE)
1166 goto fail;
1167 q = pwd->last_superframe + pwd->last_superframe_len;
1168 len = bit_offset;
1169 while (len > 7) {
1170 *q++ = get_bits(&pwd->gb, 8);
1171 len -= 8;
1172 }
1173 if (len > 0)
1174 *q++ = get_bits(&pwd->gb, len) << (8 - len);
1175
1176 /* XXX: bit_offset bits into last frame */
1177 init_get_bits(&pwd->gb, pwd->last_superframe,
1178 MAX_CODED_SUPERFRAME_SIZE);
1179 /* skip unused bits */
1180 if (pwd->last_bitoffset > 0)
1181 skip_bits(&pwd->gb, pwd->last_bitoffset);
1182 /*
1183 * This frame is stored in the last superframe and in
1184 * the current one.
1185 */
1186 ret = -E_WMA_DECODE;
1187 if (wma_decode_frame(pwd, samples) < 0)
1188 goto fail;
1189 frame_count++;
1190 samples += pwd->ahi.channels * pwd->frame_len;
1191 }
1192
1193 /* read each frame starting from bit_offset */
1194 pos = bit_offset + 4 + 4 + pwd->byte_offset_bits + 3;
1195 init_get_bits(&pwd->gb, buf + (pos >> 3),
1196 (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3)));
1197 len = pos & 7;
1198 if (len > 0)
1199 skip_bits(&pwd->gb, len);
1200
1201 pwd->reset_block_lengths = 1;
1202 for (i = 0; i < nb_frames; i++) {
1203 ret = -E_WMA_DECODE;
1204 if (wma_decode_frame(pwd, samples) < 0)
1205 goto fail;
1206 frame_count++;
1207 samples += pwd->ahi.channels * pwd->frame_len;
1208 }
1209
1210 /* we copy the end of the frame in the last frame buffer */
1211 pos = get_bits_count(&pwd->gb) +
1212 ((bit_offset + 4 + 4 + pwd->byte_offset_bits + 3) & ~7);
1213 pwd->last_bitoffset = pos & 7;
1214 pos >>= 3;
1215 len = buf_size - pos;
1216 ret = -E_WMA_BAD_SUPERFRAME;
1217 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0)
1218 goto fail;
1219 pwd->last_superframe_len = len;
1220 memcpy(pwd->last_superframe, buf + pos, len);
1221 } else {
1222 PARA_DEBUG_LOG("not using bit reservoir\n");
1223 ret = -E_WMA_OUTPUT_SPACE;
1224 if (pwd->ahi.channels * pwd->frame_len * sizeof(int16_t) > *data_size)
1225 goto fail;
1226 /* single frame decode */
1227 ret = -E_WMA_DECODE;
1228 if (wma_decode_frame(pwd, samples) < 0)
1229 goto fail;
1230 frame_count++;
1231 samples += pwd->ahi.channels * pwd->frame_len;
1232 }
1233 PARA_DEBUG_LOG("frame_count: %d frame_len: %d, block_len: %d, "
1234 "outbytes: %d, eaten: %d\n",
1235 frame_count, pwd->frame_len, pwd->block_len,
1236 (int8_t *) samples - (int8_t *) data, pwd->ahi.block_align);
1237 *data_size = (int8_t *)samples - (int8_t *)data;
1238 return pwd->ahi.block_align;
1239 fail:
1240 /* reset the bit reservoir on errors */
1241 pwd->last_superframe_len = 0;
1242 return ret;
1243 }
1244
1245 static ssize_t wmadec_convert(char *inbuffer, size_t len,
1246 struct filter_node *fn)
1247 {
1248 int ret, out_size = fn->bufsize - fn->loaded;
1249 struct private_wmadec_data *pwd = fn->private_data;
1250
1251 if (out_size < 128 * 1024)
1252 return 0;
1253 if (!pwd) {
1254 ret = wma_decode_init(inbuffer, len, &pwd);
1255 if (ret <= 0)
1256 return ret;
1257 fn->private_data = pwd;
1258 fn->fc->channels = pwd->ahi.channels;
1259 fn->fc->samplerate = pwd->ahi.sample_rate;
1260 return pwd->ahi.header_len;
1261 }
1262 /* skip 31 bytes */
1263 if (len <= WMA_FRAME_SKIP + pwd->ahi.block_align)
1264 return 0;
1265 ret = wma_decode_superframe(pwd, fn->buf + fn->loaded,
1266 &out_size, (uint8_t *)inbuffer + WMA_FRAME_SKIP,
1267 len - WMA_FRAME_SKIP);
1268 if (ret < 0)
1269 return ret;
1270 fn->loaded += out_size;
1271 return ret + WMA_FRAME_SKIP;
1272 }
1273
1274 static void wmadec_close(struct filter_node *fn)
1275 {
1276 struct private_wmadec_data *pwd = fn->private_data;
1277
1278 if (!pwd)
1279 return;
1280 wmadec_cleanup(pwd);
1281 free(fn->buf);
1282 fn->buf = NULL;
1283 free(fn->private_data);
1284 fn->private_data = NULL;
1285 }
1286
1287 static void wmadec_open(struct filter_node *fn)
1288 {
1289 fn->bufsize = 1024 * 1024;
1290 fn->buf = para_malloc(fn->bufsize);
1291 fn->private_data = NULL;
1292 fn->loaded = 0;
1293 }
1294
1295 /**
1296 * The init function of the wma decoder.
1297 *
1298 * \param f Its fields are filled in by the function.
1299 */
1300 void wmadec_filter_init(struct filter *f)
1301 {
1302 f->open = wmadec_open;
1303 f->close = wmadec_close;
1304 f->convert = wmadec_convert;
1305 }