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