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