]> git.sesse.net Git - ffmpeg/blob - libavcodec/wmadec.c
cosmetics: iff: fix typo
[ffmpeg] / libavcodec / wmadec.c
1 /*
2  * WMA compatible decoder
3  * Copyright (c) 2002 The FFmpeg Project
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * WMA compatible decoder.
25  * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
26  * WMA v1 is identified by audio format 0x160 in Microsoft media files
27  * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
28  *
29  * To use this decoder, a calling application must supply the extra data
30  * bytes provided with the WMA data. These are the extra, codec-specific
31  * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
32  * to the decoder using the extradata[_size] fields in AVCodecContext. There
33  * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
34  */
35
36 #include "avcodec.h"
37 #include "wma.h"
38
39 #undef NDEBUG
40 #include <assert.h>
41
42 #define EXPVLCBITS 8
43 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
44
45 #define HGAINVLCBITS 9
46 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
47
48 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
49
50 #ifdef TRACE
51 static void dump_shorts(WMACodecContext *s, const char *name, const short *tab, int n)
52 {
53     int i;
54
55     tprintf(s->avctx, "%s[%d]:\n", name, n);
56     for(i=0;i<n;i++) {
57         if ((i & 7) == 0)
58             tprintf(s->avctx, "%4d: ", i);
59         tprintf(s->avctx, " %5d.0", tab[i]);
60         if ((i & 7) == 7)
61             tprintf(s->avctx, "\n");
62     }
63 }
64
65 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
66 {
67     int i;
68
69     tprintf(s->avctx, "%s[%d]:\n", name, n);
70     for(i=0;i<n;i++) {
71         if ((i & 7) == 0)
72             tprintf(s->avctx, "%4d: ", i);
73         tprintf(s->avctx, " %8.*f", prec, tab[i]);
74         if ((i & 7) == 7)
75             tprintf(s->avctx, "\n");
76     }
77     if ((i & 7) != 0)
78         tprintf(s->avctx, "\n");
79 }
80 #endif
81
82 static int wma_decode_init(AVCodecContext * avctx)
83 {
84     WMACodecContext *s = avctx->priv_data;
85     int i, flags2;
86     uint8_t *extradata;
87
88     s->avctx = avctx;
89
90     /* extract flag infos */
91     flags2 = 0;
92     extradata = avctx->extradata;
93     if (avctx->codec->id == CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
94         flags2 = AV_RL16(extradata+2);
95     } else if (avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
96         flags2 = AV_RL16(extradata+4);
97     }
98 // for(i=0; i<avctx->extradata_size; i++)
99 //     av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
100
101     s->use_exp_vlc = flags2 & 0x0001;
102     s->use_bit_reservoir = flags2 & 0x0002;
103     s->use_variable_block_len = flags2 & 0x0004;
104
105     if(avctx->codec->id == CODEC_ID_WMAV2 && avctx->extradata_size >= 8){
106         if(AV_RL16(extradata+4)==0xd && s->use_variable_block_len){
107             av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
108             s->use_variable_block_len= 0; // this fixes issue1503
109         }
110     }
111
112     if(ff_wma_init(avctx, flags2)<0)
113         return -1;
114
115     /* init MDCT */
116     for(i = 0; i < s->nb_block_sizes; i++)
117         ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0);
118
119     if (s->use_noise_coding) {
120         init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
121                  ff_wma_hgain_huffbits, 1, 1,
122                  ff_wma_hgain_huffcodes, 2, 2, 0);
123     }
124
125     if (s->use_exp_vlc) {
126         init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
127                  ff_aac_scalefactor_bits, 1, 1,
128                  ff_aac_scalefactor_code, 4, 4, 0);
129     } else {
130         wma_lsp_to_curve_init(s, s->frame_len);
131     }
132
133     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
134
135     avcodec_get_frame_defaults(&s->frame);
136     avctx->coded_frame = &s->frame;
137
138     return 0;
139 }
140
141 /**
142  * compute x^-0.25 with an exponent and mantissa table. We use linear
143  * interpolation to reduce the mantissa table size at a small speed
144  * expense (linear interpolation approximately doubles the number of
145  * bits of precision).
146  */
147 static inline float pow_m1_4(WMACodecContext *s, float x)
148 {
149     union {
150         float f;
151         unsigned int v;
152     } u, t;
153     unsigned int e, m;
154     float a, b;
155
156     u.f = x;
157     e = u.v >> 23;
158     m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
159     /* build interpolation scale: 1 <= t < 2. */
160     t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
161     a = s->lsp_pow_m_table1[m];
162     b = s->lsp_pow_m_table2[m];
163     return s->lsp_pow_e_table[e] * (a + b * t.f);
164 }
165
166 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
167 {
168     float wdel, a, b;
169     int i, e, m;
170
171     wdel = M_PI / frame_len;
172     for(i=0;i<frame_len;i++)
173         s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
174
175     /* tables for x^-0.25 computation */
176     for(i=0;i<256;i++) {
177         e = i - 126;
178         s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
179     }
180
181     /* NOTE: these two tables are needed to avoid two operations in
182        pow_m1_4 */
183     b = 1.0;
184     for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
185         m = (1 << LSP_POW_BITS) + i;
186         a = (float)m * (0.5 / (1 << LSP_POW_BITS));
187         a = pow(a, -0.25);
188         s->lsp_pow_m_table1[i] = 2 * a - b;
189         s->lsp_pow_m_table2[i] = b - a;
190         b = a;
191     }
192 }
193
194 /**
195  * NOTE: We use the same code as Vorbis here
196  * @todo optimize it further with SSE/3Dnow
197  */
198 static void wma_lsp_to_curve(WMACodecContext *s,
199                              float *out, float *val_max_ptr,
200                              int n, float *lsp)
201 {
202     int i, j;
203     float p, q, w, v, val_max;
204
205     val_max = 0;
206     for(i=0;i<n;i++) {
207         p = 0.5f;
208         q = 0.5f;
209         w = s->lsp_cos_table[i];
210         for(j=1;j<NB_LSP_COEFS;j+=2){
211             q *= w - lsp[j - 1];
212             p *= w - lsp[j];
213         }
214         p *= p * (2.0f - w);
215         q *= q * (2.0f + w);
216         v = p + q;
217         v = pow_m1_4(s, v);
218         if (v > val_max)
219             val_max = v;
220         out[i] = v;
221     }
222     *val_max_ptr = val_max;
223 }
224
225 /**
226  * decode exponents coded with LSP coefficients (same idea as Vorbis)
227  */
228 static void decode_exp_lsp(WMACodecContext *s, int ch)
229 {
230     float lsp_coefs[NB_LSP_COEFS];
231     int val, i;
232
233     for(i = 0; i < NB_LSP_COEFS; i++) {
234         if (i == 0 || i >= 8)
235             val = get_bits(&s->gb, 3);
236         else
237             val = get_bits(&s->gb, 4);
238         lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
239     }
240
241     wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
242                      s->block_len, lsp_coefs);
243 }
244
245 /** pow(10, i / 16.0) for i in -60..95 */
246 static const float pow_tab[] = {
247     1.7782794100389e-04, 2.0535250264571e-04,
248     2.3713737056617e-04, 2.7384196342644e-04,
249     3.1622776601684e-04, 3.6517412725484e-04,
250     4.2169650342858e-04, 4.8696752516586e-04,
251     5.6234132519035e-04, 6.4938163157621e-04,
252     7.4989420933246e-04, 8.6596432336006e-04,
253     1.0000000000000e-03, 1.1547819846895e-03,
254     1.3335214321633e-03, 1.5399265260595e-03,
255     1.7782794100389e-03, 2.0535250264571e-03,
256     2.3713737056617e-03, 2.7384196342644e-03,
257     3.1622776601684e-03, 3.6517412725484e-03,
258     4.2169650342858e-03, 4.8696752516586e-03,
259     5.6234132519035e-03, 6.4938163157621e-03,
260     7.4989420933246e-03, 8.6596432336006e-03,
261     1.0000000000000e-02, 1.1547819846895e-02,
262     1.3335214321633e-02, 1.5399265260595e-02,
263     1.7782794100389e-02, 2.0535250264571e-02,
264     2.3713737056617e-02, 2.7384196342644e-02,
265     3.1622776601684e-02, 3.6517412725484e-02,
266     4.2169650342858e-02, 4.8696752516586e-02,
267     5.6234132519035e-02, 6.4938163157621e-02,
268     7.4989420933246e-02, 8.6596432336007e-02,
269     1.0000000000000e-01, 1.1547819846895e-01,
270     1.3335214321633e-01, 1.5399265260595e-01,
271     1.7782794100389e-01, 2.0535250264571e-01,
272     2.3713737056617e-01, 2.7384196342644e-01,
273     3.1622776601684e-01, 3.6517412725484e-01,
274     4.2169650342858e-01, 4.8696752516586e-01,
275     5.6234132519035e-01, 6.4938163157621e-01,
276     7.4989420933246e-01, 8.6596432336007e-01,
277     1.0000000000000e+00, 1.1547819846895e+00,
278     1.3335214321633e+00, 1.5399265260595e+00,
279     1.7782794100389e+00, 2.0535250264571e+00,
280     2.3713737056617e+00, 2.7384196342644e+00,
281     3.1622776601684e+00, 3.6517412725484e+00,
282     4.2169650342858e+00, 4.8696752516586e+00,
283     5.6234132519035e+00, 6.4938163157621e+00,
284     7.4989420933246e+00, 8.6596432336007e+00,
285     1.0000000000000e+01, 1.1547819846895e+01,
286     1.3335214321633e+01, 1.5399265260595e+01,
287     1.7782794100389e+01, 2.0535250264571e+01,
288     2.3713737056617e+01, 2.7384196342644e+01,
289     3.1622776601684e+01, 3.6517412725484e+01,
290     4.2169650342858e+01, 4.8696752516586e+01,
291     5.6234132519035e+01, 6.4938163157621e+01,
292     7.4989420933246e+01, 8.6596432336007e+01,
293     1.0000000000000e+02, 1.1547819846895e+02,
294     1.3335214321633e+02, 1.5399265260595e+02,
295     1.7782794100389e+02, 2.0535250264571e+02,
296     2.3713737056617e+02, 2.7384196342644e+02,
297     3.1622776601684e+02, 3.6517412725484e+02,
298     4.2169650342858e+02, 4.8696752516586e+02,
299     5.6234132519035e+02, 6.4938163157621e+02,
300     7.4989420933246e+02, 8.6596432336007e+02,
301     1.0000000000000e+03, 1.1547819846895e+03,
302     1.3335214321633e+03, 1.5399265260595e+03,
303     1.7782794100389e+03, 2.0535250264571e+03,
304     2.3713737056617e+03, 2.7384196342644e+03,
305     3.1622776601684e+03, 3.6517412725484e+03,
306     4.2169650342858e+03, 4.8696752516586e+03,
307     5.6234132519035e+03, 6.4938163157621e+03,
308     7.4989420933246e+03, 8.6596432336007e+03,
309     1.0000000000000e+04, 1.1547819846895e+04,
310     1.3335214321633e+04, 1.5399265260595e+04,
311     1.7782794100389e+04, 2.0535250264571e+04,
312     2.3713737056617e+04, 2.7384196342644e+04,
313     3.1622776601684e+04, 3.6517412725484e+04,
314     4.2169650342858e+04, 4.8696752516586e+04,
315     5.6234132519035e+04, 6.4938163157621e+04,
316     7.4989420933246e+04, 8.6596432336007e+04,
317     1.0000000000000e+05, 1.1547819846895e+05,
318     1.3335214321633e+05, 1.5399265260595e+05,
319     1.7782794100389e+05, 2.0535250264571e+05,
320     2.3713737056617e+05, 2.7384196342644e+05,
321     3.1622776601684e+05, 3.6517412725484e+05,
322     4.2169650342858e+05, 4.8696752516586e+05,
323     5.6234132519035e+05, 6.4938163157621e+05,
324     7.4989420933246e+05, 8.6596432336007e+05,
325 };
326
327 /**
328  * decode exponents coded with VLC codes
329  */
330 static int decode_exp_vlc(WMACodecContext *s, int ch)
331 {
332     int last_exp, n, code;
333     const uint16_t *ptr;
334     float v, max_scale;
335     uint32_t *q, *q_end, iv;
336     const float *ptab = pow_tab + 60;
337     const uint32_t *iptab = (const uint32_t*)ptab;
338
339     ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
340     q = (uint32_t *)s->exponents[ch];
341     q_end = q + s->block_len;
342     max_scale = 0;
343     if (s->version == 1) {
344         last_exp = get_bits(&s->gb, 5) + 10;
345         v = ptab[last_exp];
346         iv = iptab[last_exp];
347         max_scale = v;
348         n = *ptr++;
349         switch (n & 3) do {
350         case 0: *q++ = iv;
351         case 3: *q++ = iv;
352         case 2: *q++ = iv;
353         case 1: *q++ = iv;
354         } while ((n -= 4) > 0);
355     }else
356         last_exp = 36;
357
358     while (q < q_end) {
359         code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
360         if (code < 0){
361             av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
362             return -1;
363         }
364         /* NOTE: this offset is the same as MPEG4 AAC ! */
365         last_exp += code - 60;
366         if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
367             av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
368                    last_exp);
369             return -1;
370         }
371         v = ptab[last_exp];
372         iv = iptab[last_exp];
373         if (v > max_scale)
374             max_scale = v;
375         n = *ptr++;
376         switch (n & 3) do {
377         case 0: *q++ = iv;
378         case 3: *q++ = iv;
379         case 2: *q++ = iv;
380         case 1: *q++ = iv;
381         } while ((n -= 4) > 0);
382     }
383     s->max_exponent[ch] = max_scale;
384     return 0;
385 }
386
387
388 /**
389  * Apply MDCT window and add into output.
390  *
391  * We ensure that when the windows overlap their squared sum
392  * is always 1 (MDCT reconstruction rule).
393  */
394 static void wma_window(WMACodecContext *s, float *out)
395 {
396     float *in = s->output;
397     int block_len, bsize, n;
398
399     /* left part */
400     if (s->block_len_bits <= s->prev_block_len_bits) {
401         block_len = s->block_len;
402         bsize = s->frame_len_bits - s->block_len_bits;
403
404         s->dsp.vector_fmul_add(out, in, s->windows[bsize],
405                                out, block_len);
406
407     } else {
408         block_len = 1 << s->prev_block_len_bits;
409         n = (s->block_len - block_len) / 2;
410         bsize = s->frame_len_bits - s->prev_block_len_bits;
411
412         s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
413                                out+n, block_len);
414
415         memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
416     }
417
418     out += s->block_len;
419     in += s->block_len;
420
421     /* right part */
422     if (s->block_len_bits <= s->next_block_len_bits) {
423         block_len = s->block_len;
424         bsize = s->frame_len_bits - s->block_len_bits;
425
426         s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
427
428     } else {
429         block_len = 1 << s->next_block_len_bits;
430         n = (s->block_len - block_len) / 2;
431         bsize = s->frame_len_bits - s->next_block_len_bits;
432
433         memcpy(out, in, n*sizeof(float));
434
435         s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
436
437         memset(out+n+block_len, 0, n*sizeof(float));
438     }
439 }
440
441
442 /**
443  * @return 0 if OK. 1 if last block of frame. return -1 if
444  * unrecorrable error.
445  */
446 static int wma_decode_block(WMACodecContext *s)
447 {
448     int n, v, a, ch, bsize;
449     int coef_nb_bits, total_gain;
450     int nb_coefs[MAX_CHANNELS];
451     float mdct_norm;
452     FFTContext *mdct;
453
454 #ifdef TRACE
455     tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
456 #endif
457
458     /* compute current block length */
459     if (s->use_variable_block_len) {
460         n = av_log2(s->nb_block_sizes - 1) + 1;
461
462         if (s->reset_block_lengths) {
463             s->reset_block_lengths = 0;
464             v = get_bits(&s->gb, n);
465             if (v >= s->nb_block_sizes){
466                 av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
467                 return -1;
468             }
469             s->prev_block_len_bits = s->frame_len_bits - v;
470             v = get_bits(&s->gb, n);
471             if (v >= s->nb_block_sizes){
472                 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
473                 return -1;
474             }
475             s->block_len_bits = s->frame_len_bits - v;
476         } else {
477             /* update block lengths */
478             s->prev_block_len_bits = s->block_len_bits;
479             s->block_len_bits = s->next_block_len_bits;
480         }
481         v = get_bits(&s->gb, n);
482         if (v >= s->nb_block_sizes){
483             av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
484             return -1;
485         }
486         s->next_block_len_bits = s->frame_len_bits - v;
487     } else {
488         /* fixed block len */
489         s->next_block_len_bits = s->frame_len_bits;
490         s->prev_block_len_bits = s->frame_len_bits;
491         s->block_len_bits = s->frame_len_bits;
492     }
493
494     if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){
495         av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");
496         return -1;
497     }
498
499     /* now check if the block length is coherent with the frame length */
500     s->block_len = 1 << s->block_len_bits;
501     if ((s->block_pos + s->block_len) > s->frame_len){
502         av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
503         return -1;
504     }
505
506     if (s->nb_channels == 2) {
507         s->ms_stereo = get_bits1(&s->gb);
508     }
509     v = 0;
510     for(ch = 0; ch < s->nb_channels; ch++) {
511         a = get_bits1(&s->gb);
512         s->channel_coded[ch] = a;
513         v |= a;
514     }
515
516     bsize = s->frame_len_bits - s->block_len_bits;
517
518     /* if no channel coded, no need to go further */
519     /* XXX: fix potential framing problems */
520     if (!v)
521         goto next;
522
523     /* read total gain and extract corresponding number of bits for
524        coef escape coding */
525     total_gain = 1;
526     for(;;) {
527         a = get_bits(&s->gb, 7);
528         total_gain += a;
529         if (a != 127)
530             break;
531     }
532
533     coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
534
535     /* compute number of coefficients */
536     n = s->coefs_end[bsize] - s->coefs_start;
537     for(ch = 0; ch < s->nb_channels; ch++)
538         nb_coefs[ch] = n;
539
540     /* complex coding */
541     if (s->use_noise_coding) {
542
543         for(ch = 0; ch < s->nb_channels; ch++) {
544             if (s->channel_coded[ch]) {
545                 int i, n, a;
546                 n = s->exponent_high_sizes[bsize];
547                 for(i=0;i<n;i++) {
548                     a = get_bits1(&s->gb);
549                     s->high_band_coded[ch][i] = a;
550                     /* if noise coding, the coefficients are not transmitted */
551                     if (a)
552                         nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
553                 }
554             }
555         }
556         for(ch = 0; ch < s->nb_channels; ch++) {
557             if (s->channel_coded[ch]) {
558                 int i, n, val, code;
559
560                 n = s->exponent_high_sizes[bsize];
561                 val = (int)0x80000000;
562                 for(i=0;i<n;i++) {
563                     if (s->high_band_coded[ch][i]) {
564                         if (val == (int)0x80000000) {
565                             val = get_bits(&s->gb, 7) - 19;
566                         } else {
567                             code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
568                             if (code < 0){
569                                 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
570                                 return -1;
571                             }
572                             val += code - 18;
573                         }
574                         s->high_band_values[ch][i] = val;
575                     }
576                 }
577             }
578         }
579     }
580
581     /* exponents can be reused in short blocks. */
582     if ((s->block_len_bits == s->frame_len_bits) ||
583         get_bits1(&s->gb)) {
584         for(ch = 0; ch < s->nb_channels; ch++) {
585             if (s->channel_coded[ch]) {
586                 if (s->use_exp_vlc) {
587                     if (decode_exp_vlc(s, ch) < 0)
588                         return -1;
589                 } else {
590                     decode_exp_lsp(s, ch);
591                 }
592                 s->exponents_bsize[ch] = bsize;
593             }
594         }
595     }
596
597     /* parse spectral coefficients : just RLE encoding */
598     for(ch = 0; ch < s->nb_channels; ch++) {
599         if (s->channel_coded[ch]) {
600             int tindex;
601             WMACoef* ptr = &s->coefs1[ch][0];
602
603             /* special VLC tables are used for ms stereo because
604                there is potentially less energy there */
605             tindex = (ch == 1 && s->ms_stereo);
606             memset(ptr, 0, s->block_len * sizeof(WMACoef));
607             ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
608                   s->level_table[tindex], s->run_table[tindex],
609                   0, ptr, 0, nb_coefs[ch],
610                   s->block_len, s->frame_len_bits, coef_nb_bits);
611         }
612         if (s->version == 1 && s->nb_channels >= 2) {
613             align_get_bits(&s->gb);
614         }
615     }
616
617     /* normalize */
618     {
619         int n4 = s->block_len / 2;
620         mdct_norm = 1.0 / (float)n4;
621         if (s->version == 1) {
622             mdct_norm *= sqrt(n4);
623         }
624     }
625
626     /* finally compute the MDCT coefficients */
627     for(ch = 0; ch < s->nb_channels; ch++) {
628         if (s->channel_coded[ch]) {
629             WMACoef *coefs1;
630             float *coefs, *exponents, mult, mult1, noise;
631             int i, j, n, n1, last_high_band, esize;
632             float exp_power[HIGH_BAND_MAX_SIZE];
633
634             coefs1 = s->coefs1[ch];
635             exponents = s->exponents[ch];
636             esize = s->exponents_bsize[ch];
637             mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
638             mult *= mdct_norm;
639             coefs = s->coefs[ch];
640             if (s->use_noise_coding) {
641                 mult1 = mult;
642                 /* very low freqs : noise */
643                 for(i = 0;i < s->coefs_start; i++) {
644                     *coefs++ = s->noise_table[s->noise_index] *
645                       exponents[i<<bsize>>esize] * mult1;
646                     s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
647                 }
648
649                 n1 = s->exponent_high_sizes[bsize];
650
651                 /* compute power of high bands */
652                 exponents = s->exponents[ch] +
653                     (s->high_band_start[bsize]<<bsize>>esize);
654                 last_high_band = 0; /* avoid warning */
655                 for(j=0;j<n1;j++) {
656                     n = s->exponent_high_bands[s->frame_len_bits -
657                                               s->block_len_bits][j];
658                     if (s->high_band_coded[ch][j]) {
659                         float e2, v;
660                         e2 = 0;
661                         for(i = 0;i < n; i++) {
662                             v = exponents[i<<bsize>>esize];
663                             e2 += v * v;
664                         }
665                         exp_power[j] = e2 / n;
666                         last_high_band = j;
667                         tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
668                     }
669                     exponents += n<<bsize>>esize;
670                 }
671
672                 /* main freqs and high freqs */
673                 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
674                 for(j=-1;j<n1;j++) {
675                     if (j < 0) {
676                         n = s->high_band_start[bsize] -
677                             s->coefs_start;
678                     } else {
679                         n = s->exponent_high_bands[s->frame_len_bits -
680                                                   s->block_len_bits][j];
681                     }
682                     if (j >= 0 && s->high_band_coded[ch][j]) {
683                         /* use noise with specified power */
684                         mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
685                         /* XXX: use a table */
686                         mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
687                         mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
688                         mult1 *= mdct_norm;
689                         for(i = 0;i < n; i++) {
690                             noise = s->noise_table[s->noise_index];
691                             s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
692                             *coefs++ =  noise *
693                                 exponents[i<<bsize>>esize] * mult1;
694                         }
695                         exponents += n<<bsize>>esize;
696                     } else {
697                         /* coded values + small noise */
698                         for(i = 0;i < n; i++) {
699                             noise = s->noise_table[s->noise_index];
700                             s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
701                             *coefs++ = ((*coefs1++) + noise) *
702                                 exponents[i<<bsize>>esize] * mult;
703                         }
704                         exponents += n<<bsize>>esize;
705                     }
706                 }
707
708                 /* very high freqs : noise */
709                 n = s->block_len - s->coefs_end[bsize];
710                 mult1 = mult * exponents[((-1<<bsize))>>esize];
711                 for(i = 0; i < n; i++) {
712                     *coefs++ = s->noise_table[s->noise_index] * mult1;
713                     s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
714                 }
715             } else {
716                 /* XXX: optimize more */
717                 for(i = 0;i < s->coefs_start; i++)
718                     *coefs++ = 0.0;
719                 n = nb_coefs[ch];
720                 for(i = 0;i < n; i++) {
721                     *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
722                 }
723                 n = s->block_len - s->coefs_end[bsize];
724                 for(i = 0;i < n; i++)
725                     *coefs++ = 0.0;
726             }
727         }
728     }
729
730 #ifdef TRACE
731     for(ch = 0; ch < s->nb_channels; ch++) {
732         if (s->channel_coded[ch]) {
733             dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
734             dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
735         }
736     }
737 #endif
738
739     if (s->ms_stereo && s->channel_coded[1]) {
740         /* nominal case for ms stereo: we do it before mdct */
741         /* no need to optimize this case because it should almost
742            never happen */
743         if (!s->channel_coded[0]) {
744             tprintf(s->avctx, "rare ms-stereo case happened\n");
745             memset(s->coefs[0], 0, sizeof(float) * s->block_len);
746             s->channel_coded[0] = 1;
747         }
748
749         s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
750     }
751
752 next:
753     mdct = &s->mdct_ctx[bsize];
754
755     for(ch = 0; ch < s->nb_channels; ch++) {
756         int n4, index;
757
758         n4 = s->block_len / 2;
759         if(s->channel_coded[ch]){
760             mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
761         }else if(!(s->ms_stereo && ch==1))
762             memset(s->output, 0, sizeof(s->output));
763
764         /* multiply by the window and add in the frame */
765         index = (s->frame_len / 2) + s->block_pos - n4;
766         wma_window(s, &s->frame_out[ch][index]);
767     }
768
769     /* update block number */
770     s->block_num++;
771     s->block_pos += s->block_len;
772     if (s->block_pos >= s->frame_len)
773         return 1;
774     else
775         return 0;
776 }
777
778 /* decode a frame of frame_len samples */
779 static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
780 {
781     int ret, n, ch, incr;
782     const float *output[MAX_CHANNELS];
783
784 #ifdef TRACE
785     tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
786 #endif
787
788     /* read each block */
789     s->block_num = 0;
790     s->block_pos = 0;
791     for(;;) {
792         ret = wma_decode_block(s);
793         if (ret < 0)
794             return -1;
795         if (ret)
796             break;
797     }
798
799     /* convert frame to integer */
800     n = s->frame_len;
801     incr = s->nb_channels;
802     for (ch = 0; ch < MAX_CHANNELS; ch++)
803         output[ch] = s->frame_out[ch];
804     s->fmt_conv.float_to_int16_interleave(samples, output, n, incr);
805     for (ch = 0; ch < incr; ch++) {
806         /* prepare for next block */
807         memmove(&s->frame_out[ch][0], &s->frame_out[ch][n], n * sizeof(float));
808     }
809
810 #ifdef TRACE
811     dump_shorts(s, "samples", samples, n * s->nb_channels);
812 #endif
813     return 0;
814 }
815
816 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
817                                  int *got_frame_ptr, AVPacket *avpkt)
818 {
819     const uint8_t *buf = avpkt->data;
820     int buf_size = avpkt->size;
821     WMACodecContext *s = avctx->priv_data;
822     int nb_frames, bit_offset, i, pos, len, ret;
823     uint8_t *q;
824     int16_t *samples;
825
826     tprintf(avctx, "***decode_superframe:\n");
827
828     if(buf_size==0){
829         s->last_superframe_len = 0;
830         return 0;
831     }
832     if (buf_size < s->block_align) {
833         av_log(avctx, AV_LOG_ERROR,
834                "Input packet size too small (%d < %d)\n",
835                buf_size, s->block_align);
836         return AVERROR_INVALIDDATA;
837     }
838     if(s->block_align)
839         buf_size = s->block_align;
840
841     init_get_bits(&s->gb, buf, buf_size*8);
842
843     if (s->use_bit_reservoir) {
844         /* read super frame header */
845         skip_bits(&s->gb, 4); /* super frame index */
846         nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
847     } else {
848         nb_frames = 1;
849     }
850
851     /* get output buffer */
852     s->frame.nb_samples = nb_frames * s->frame_len;
853     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
854         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
855         return ret;
856     }
857     samples = (int16_t *)s->frame.data[0];
858
859     if (s->use_bit_reservoir) {
860         bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
861         if (bit_offset > get_bits_left(&s->gb)) {
862             av_log(avctx, AV_LOG_ERROR,
863                    "Invalid last frame bit offset %d > buf size %d (%d)\n",
864                    bit_offset, get_bits_left(&s->gb), buf_size);
865             goto fail;
866         }
867
868         if (s->last_superframe_len > 0) {
869             //        printf("skip=%d\n", s->last_bitoffset);
870             /* add bit_offset bits to last frame */
871             if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
872                 MAX_CODED_SUPERFRAME_SIZE)
873                 goto fail;
874             q = s->last_superframe + s->last_superframe_len;
875             len = bit_offset;
876             while (len > 7) {
877                 *q++ = (get_bits)(&s->gb, 8);
878                 len -= 8;
879             }
880             if (len > 0) {
881                 *q++ = (get_bits)(&s->gb, len) << (8 - len);
882             }
883             memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
884
885             /* XXX: bit_offset bits into last frame */
886             init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
887             /* skip unused bits */
888             if (s->last_bitoffset > 0)
889                 skip_bits(&s->gb, s->last_bitoffset);
890             /* this frame is stored in the last superframe and in the
891                current one */
892             if (wma_decode_frame(s, samples) < 0)
893                 goto fail;
894             samples += s->nb_channels * s->frame_len;
895             nb_frames--;
896         }
897
898         /* read each frame starting from bit_offset */
899         pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
900         if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
901             return AVERROR_INVALIDDATA;
902         init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
903         len = pos & 7;
904         if (len > 0)
905             skip_bits(&s->gb, len);
906
907         s->reset_block_lengths = 1;
908         for(i=0;i<nb_frames;i++) {
909             if (wma_decode_frame(s, samples) < 0)
910                 goto fail;
911             samples += s->nb_channels * s->frame_len;
912         }
913
914         /* we copy the end of the frame in the last frame buffer */
915         pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
916         s->last_bitoffset = pos & 7;
917         pos >>= 3;
918         len = buf_size - pos;
919         if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
920             av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
921             goto fail;
922         }
923         s->last_superframe_len = len;
924         memcpy(s->last_superframe, buf + pos, len);
925     } else {
926         /* single frame decode */
927         if (wma_decode_frame(s, samples) < 0)
928             goto fail;
929         samples += s->nb_channels * s->frame_len;
930     }
931
932 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d outbytes:%d eaten:%d\n", s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,        (int8_t *)samples - (int8_t *)data, s->block_align);
933
934     *got_frame_ptr   = 1;
935     *(AVFrame *)data = s->frame;
936
937     return buf_size;
938  fail:
939     /* when error, we reset the bit reservoir */
940     s->last_superframe_len = 0;
941     return -1;
942 }
943
944 static av_cold void flush(AVCodecContext *avctx)
945 {
946     WMACodecContext *s = avctx->priv_data;
947
948     s->last_bitoffset=
949     s->last_superframe_len= 0;
950 }
951
952 #if CONFIG_WMAV1_DECODER
953 AVCodec ff_wmav1_decoder = {
954     .name           = "wmav1",
955     .type           = AVMEDIA_TYPE_AUDIO,
956     .id             = CODEC_ID_WMAV1,
957     .priv_data_size = sizeof(WMACodecContext),
958     .init           = wma_decode_init,
959     .close          = ff_wma_end,
960     .decode         = wma_decode_superframe,
961     .flush          = flush,
962     .capabilities   = CODEC_CAP_DR1,
963     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
964 };
965 #endif
966 #if CONFIG_WMAV2_DECODER
967 AVCodec ff_wmav2_decoder = {
968     .name           = "wmav2",
969     .type           = AVMEDIA_TYPE_AUDIO,
970     .id             = CODEC_ID_WMAV2,
971     .priv_data_size = sizeof(WMACodecContext),
972     .init           = wma_decode_init,
973     .close          = ff_wma_end,
974     .decode         = wma_decode_superframe,
975     .flush          = flush,
976     .capabilities   = CODEC_CAP_DR1,
977     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
978 };
979 #endif