2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project
5 * This file is part of FFmpeg.
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.
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.
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
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.
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.
43 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
45 #define HGAINVLCBITS 9
46 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
48 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
51 static void dump_shorts(WMACodecContext *s, const char *name, const short *tab, int n)
55 tprintf(s->avctx, "%s[%d]:\n", name, n);
58 tprintf(s->avctx, "%4d: ", i);
59 tprintf(s->avctx, " %5d.0", tab[i]);
61 tprintf(s->avctx, "\n");
65 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
69 tprintf(s->avctx, "%s[%d]:\n", name, n);
72 tprintf(s->avctx, "%4d: ", i);
73 tprintf(s->avctx, " %8.*f", prec, tab[i]);
75 tprintf(s->avctx, "\n");
78 tprintf(s->avctx, "\n");
82 static int wma_decode_init(AVCodecContext * avctx)
84 WMACodecContext *s = avctx->priv_data;
90 /* extract flag infos */
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);
98 // for(i=0; i<avctx->extradata_size; i++)
99 // av_log(NULL, AV_LOG_ERROR, "%02X ", extradata[i]);
101 s->use_exp_vlc = flags2 & 0x0001;
102 s->use_bit_reservoir = flags2 & 0x0002;
103 s->use_variable_block_len = flags2 & 0x0004;
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
112 if(ff_wma_init(avctx, flags2)<0)
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);
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);
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);
130 wma_lsp_to_curve_init(s, s->frame_len);
133 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
135 avcodec_get_frame_defaults(&s->frame);
136 avctx->coded_frame = &s->frame;
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).
147 static inline float pow_m1_4(WMACodecContext *s, float x)
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);
166 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
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);
175 /* tables for x^-0.25 computation */
178 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
181 /* NOTE: these two tables are needed to avoid two operations in
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));
188 s->lsp_pow_m_table1[i] = 2 * a - b;
189 s->lsp_pow_m_table2[i] = b - a;
195 * NOTE: We use the same code as Vorbis here
196 * @todo optimize it further with SSE/3Dnow
198 static void wma_lsp_to_curve(WMACodecContext *s,
199 float *out, float *val_max_ptr,
203 float p, q, w, v, val_max;
209 w = s->lsp_cos_table[i];
210 for(j=1;j<NB_LSP_COEFS;j+=2){
222 *val_max_ptr = val_max;
226 * decode exponents coded with LSP coefficients (same idea as Vorbis)
228 static void decode_exp_lsp(WMACodecContext *s, int ch)
230 float lsp_coefs[NB_LSP_COEFS];
233 for(i = 0; i < NB_LSP_COEFS; i++) {
234 if (i == 0 || i >= 8)
235 val = get_bits(&s->gb, 3);
237 val = get_bits(&s->gb, 4);
238 lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
241 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
242 s->block_len, lsp_coefs);
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,
328 * decode exponents coded with VLC codes
330 static int decode_exp_vlc(WMACodecContext *s, int ch)
332 int last_exp, n, code;
335 uint32_t *q, *q_end, iv;
336 const float *ptab = pow_tab + 60;
337 const uint32_t *iptab = (const uint32_t*)ptab;
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;
343 if (s->version == 1) {
344 last_exp = get_bits(&s->gb, 5) + 10;
346 iv = iptab[last_exp];
354 } while ((n -= 4) > 0);
359 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
361 av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
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",
372 iv = iptab[last_exp];
381 } while ((n -= 4) > 0);
383 s->max_exponent[ch] = max_scale;
389 * Apply MDCT window and add into output.
391 * We ensure that when the windows overlap their squared sum
392 * is always 1 (MDCT reconstruction rule).
394 static void wma_window(WMACodecContext *s, float *out)
396 float *in = s->output;
397 int block_len, bsize, n;
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;
404 s->dsp.vector_fmul_add(out, in, s->windows[bsize],
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;
412 s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
415 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
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;
426 s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
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;
433 memcpy(out, in, n*sizeof(float));
435 s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
437 memset(out+n+block_len, 0, n*sizeof(float));
443 * @return 0 if OK. 1 if last block of frame. return -1 if
444 * unrecorrable error.
446 static int wma_decode_block(WMACodecContext *s)
448 int n, v, a, ch, bsize;
449 int coef_nb_bits, total_gain;
450 int nb_coefs[MAX_CHANNELS];
455 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
458 /* compute current block length */
459 if (s->use_variable_block_len) {
460 n = av_log2(s->nb_block_sizes - 1) + 1;
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);
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);
475 s->block_len_bits = s->frame_len_bits - v;
477 /* update block lengths */
478 s->prev_block_len_bits = s->block_len_bits;
479 s->block_len_bits = s->next_block_len_bits;
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);
486 s->next_block_len_bits = s->frame_len_bits - v;
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;
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");
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");
506 if (s->nb_channels == 2) {
507 s->ms_stereo = get_bits1(&s->gb);
510 for(ch = 0; ch < s->nb_channels; ch++) {
511 a = get_bits1(&s->gb);
512 s->channel_coded[ch] = a;
516 bsize = s->frame_len_bits - s->block_len_bits;
518 /* if no channel coded, no need to go further */
519 /* XXX: fix potential framing problems */
523 /* read total gain and extract corresponding number of bits for
524 coef escape coding */
527 a = get_bits(&s->gb, 7);
533 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
535 /* compute number of coefficients */
536 n = s->coefs_end[bsize] - s->coefs_start;
537 for(ch = 0; ch < s->nb_channels; ch++)
541 if (s->use_noise_coding) {
543 for(ch = 0; ch < s->nb_channels; ch++) {
544 if (s->channel_coded[ch]) {
546 n = s->exponent_high_sizes[bsize];
548 a = get_bits1(&s->gb);
549 s->high_band_coded[ch][i] = a;
550 /* if noise coding, the coefficients are not transmitted */
552 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
556 for(ch = 0; ch < s->nb_channels; ch++) {
557 if (s->channel_coded[ch]) {
560 n = s->exponent_high_sizes[bsize];
561 val = (int)0x80000000;
563 if (s->high_band_coded[ch][i]) {
564 if (val == (int)0x80000000) {
565 val = get_bits(&s->gb, 7) - 19;
567 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
569 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
574 s->high_band_values[ch][i] = val;
581 /* exponents can be reused in short blocks. */
582 if ((s->block_len_bits == s->frame_len_bits) ||
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)
590 decode_exp_lsp(s, ch);
592 s->exponents_bsize[ch] = bsize;
597 /* parse spectral coefficients : just RLE encoding */
598 for(ch = 0; ch < s->nb_channels; ch++) {
599 if (s->channel_coded[ch]) {
601 WMACoef* ptr = &s->coefs1[ch][0];
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);
612 if (s->version == 1 && s->nb_channels >= 2) {
613 align_get_bits(&s->gb);
619 int n4 = s->block_len / 2;
620 mdct_norm = 1.0 / (float)n4;
621 if (s->version == 1) {
622 mdct_norm *= sqrt(n4);
626 /* finally compute the MDCT coefficients */
627 for(ch = 0; ch < s->nb_channels; ch++) {
628 if (s->channel_coded[ch]) {
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];
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];
639 coefs = s->coefs[ch];
640 if (s->use_noise_coding) {
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);
649 n1 = s->exponent_high_sizes[bsize];
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 */
656 n = s->exponent_high_bands[s->frame_len_bits -
657 s->block_len_bits][j];
658 if (s->high_band_coded[ch][j]) {
661 for(i = 0;i < n; i++) {
662 v = exponents[i<<bsize>>esize];
665 exp_power[j] = e2 / n;
667 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
669 exponents += n<<bsize>>esize;
672 /* main freqs and high freqs */
673 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
676 n = s->high_band_start[bsize] -
679 n = s->exponent_high_bands[s->frame_len_bits -
680 s->block_len_bits][j];
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);
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);
693 exponents[i<<bsize>>esize] * mult1;
695 exponents += n<<bsize>>esize;
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;
704 exponents += n<<bsize>>esize;
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);
716 /* XXX: optimize more */
717 for(i = 0;i < s->coefs_start; i++)
720 for(i = 0;i < n; i++) {
721 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
723 n = s->block_len - s->coefs_end[bsize];
724 for(i = 0;i < n; i++)
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);
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
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;
749 s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
753 mdct = &s->mdct_ctx[bsize];
755 for(ch = 0; ch < s->nb_channels; ch++) {
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));
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]);
769 /* update block number */
771 s->block_pos += s->block_len;
772 if (s->block_pos >= s->frame_len)
778 /* decode a frame of frame_len samples */
779 static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
781 int ret, n, ch, incr;
782 const float *output[MAX_CHANNELS];
785 tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
788 /* read each block */
792 ret = wma_decode_block(s);
799 /* convert frame to integer */
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));
811 dump_shorts(s, "samples", samples, n * s->nb_channels);
816 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
817 int *got_frame_ptr, AVPacket *avpkt)
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;
826 tprintf(avctx, "***decode_superframe:\n");
829 s->last_superframe_len = 0;
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;
839 buf_size = s->block_align;
841 init_get_bits(&s->gb, buf, buf_size*8);
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);
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");
857 samples = (int16_t *)s->frame.data[0];
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);
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)
874 q = s->last_superframe + s->last_superframe_len;
877 *q++ = (get_bits)(&s->gb, 8);
881 *q++ = (get_bits)(&s->gb, len) << (8 - len);
883 memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
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
892 if (wma_decode_frame(s, samples) < 0)
894 samples += s->nb_channels * s->frame_len;
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);
905 skip_bits(&s->gb, len);
907 s->reset_block_lengths = 1;
908 for(i=0;i<nb_frames;i++) {
909 if (wma_decode_frame(s, samples) < 0)
911 samples += s->nb_channels * s->frame_len;
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;
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);
923 s->last_superframe_len = len;
924 memcpy(s->last_superframe, buf + pos, len);
926 /* single frame decode */
927 if (wma_decode_frame(s, samples) < 0)
929 samples += s->nb_channels * s->frame_len;
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);
935 *(AVFrame *)data = s->frame;
939 /* when error, we reset the bit reservoir */
940 s->last_superframe_len = 0;
944 static av_cold void flush(AVCodecContext *avctx)
946 WMACodecContext *s = avctx->priv_data;
949 s->last_superframe_len= 0;
952 #if CONFIG_WMAV1_DECODER
953 AVCodec ff_wmav1_decoder = {
955 .type = AVMEDIA_TYPE_AUDIO,
956 .id = CODEC_ID_WMAV1,
957 .priv_data_size = sizeof(WMACodecContext),
958 .init = wma_decode_init,
960 .decode = wma_decode_superframe,
962 .capabilities = CODEC_CAP_DR1,
963 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
966 #if CONFIG_WMAV2_DECODER
967 AVCodec ff_wmav2_decoder = {
969 .type = AVMEDIA_TYPE_AUDIO,
970 .id = CODEC_ID_WMAV2,
971 .priv_data_size = sizeof(WMACodecContext),
972 .init = wma_decode_init,
974 .decode = wma_decode_superframe,
976 .capabilities = CODEC_CAP_DR1,
977 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),