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.
44 #define EXPMAX ((19+EXPVLCBITS-1)/EXPVLCBITS)
46 #define HGAINVLCBITS 9
47 #define HGAINMAX ((13+HGAINVLCBITS-1)/HGAINVLCBITS)
49 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len);
52 static void dump_floats(WMACodecContext *s, const char *name, int prec, const float *tab, int n)
56 tprintf(s->avctx, "%s[%d]:\n", name, n);
59 tprintf(s->avctx, "%4d: ", i);
60 tprintf(s->avctx, " %8.*f", prec, tab[i]);
62 tprintf(s->avctx, "\n");
65 tprintf(s->avctx, "\n");
69 static int wma_decode_init(AVCodecContext * avctx)
71 WMACodecContext *s = avctx->priv_data;
75 if (!avctx->block_align) {
76 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
77 return AVERROR(EINVAL);
82 /* extract flag infos */
84 extradata = avctx->extradata;
85 if (avctx->codec->id == AV_CODEC_ID_WMAV1 && avctx->extradata_size >= 4) {
86 flags2 = AV_RL16(extradata+2);
87 } else if (avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 6) {
88 flags2 = AV_RL16(extradata+4);
91 s->use_exp_vlc = flags2 & 0x0001;
92 s->use_bit_reservoir = flags2 & 0x0002;
93 s->use_variable_block_len = flags2 & 0x0004;
95 if(avctx->codec->id == AV_CODEC_ID_WMAV2 && avctx->extradata_size >= 8){
96 if(AV_RL16(extradata+4)==0xd && s->use_variable_block_len){
97 av_log(avctx, AV_LOG_WARNING, "Disabling use_variable_block_len, if this fails contact the ffmpeg developers and send us the file\n");
98 s->use_variable_block_len= 0; // this fixes issue1503
102 if(ff_wma_init(avctx, flags2)<0)
106 for(i = 0; i < s->nb_block_sizes; i++)
107 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0 / 32768.0);
109 if (s->use_noise_coding) {
110 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
111 ff_wma_hgain_huffbits, 1, 1,
112 ff_wma_hgain_huffcodes, 2, 2, 0);
115 if (s->use_exp_vlc) {
116 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
117 ff_aac_scalefactor_bits, 1, 1,
118 ff_aac_scalefactor_code, 4, 4, 0);
120 wma_lsp_to_curve_init(s, s->frame_len);
123 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
129 * compute x^-0.25 with an exponent and mantissa table. We use linear
130 * interpolation to reduce the mantissa table size at a small speed
131 * expense (linear interpolation approximately doubles the number of
132 * bits of precision).
134 static inline float pow_m1_4(WMACodecContext *s, float x)
145 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
146 /* build interpolation scale: 1 <= t < 2. */
147 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
148 a = s->lsp_pow_m_table1[m];
149 b = s->lsp_pow_m_table2[m];
150 return s->lsp_pow_e_table[e] * (a + b * t.f);
153 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
158 wdel = M_PI / frame_len;
159 for(i=0;i<frame_len;i++)
160 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
162 /* tables for x^-0.25 computation */
165 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
168 /* NOTE: these two tables are needed to avoid two operations in
171 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
172 m = (1 << LSP_POW_BITS) + i;
173 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
175 s->lsp_pow_m_table1[i] = 2 * a - b;
176 s->lsp_pow_m_table2[i] = b - a;
182 * NOTE: We use the same code as Vorbis here
183 * @todo optimize it further with SSE/3Dnow
185 static void wma_lsp_to_curve(WMACodecContext *s,
186 float *out, float *val_max_ptr,
190 float p, q, w, v, val_max;
196 w = s->lsp_cos_table[i];
197 for(j=1;j<NB_LSP_COEFS;j+=2){
209 *val_max_ptr = val_max;
213 * decode exponents coded with LSP coefficients (same idea as Vorbis)
215 static void decode_exp_lsp(WMACodecContext *s, int ch)
217 float lsp_coefs[NB_LSP_COEFS];
220 for(i = 0; i < NB_LSP_COEFS; i++) {
221 if (i == 0 || i >= 8)
222 val = get_bits(&s->gb, 3);
224 val = get_bits(&s->gb, 4);
225 lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
228 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
229 s->block_len, lsp_coefs);
232 /** pow(10, i / 16.0) for i in -60..95 */
233 static const float pow_tab[] = {
234 1.7782794100389e-04, 2.0535250264571e-04,
235 2.3713737056617e-04, 2.7384196342644e-04,
236 3.1622776601684e-04, 3.6517412725484e-04,
237 4.2169650342858e-04, 4.8696752516586e-04,
238 5.6234132519035e-04, 6.4938163157621e-04,
239 7.4989420933246e-04, 8.6596432336006e-04,
240 1.0000000000000e-03, 1.1547819846895e-03,
241 1.3335214321633e-03, 1.5399265260595e-03,
242 1.7782794100389e-03, 2.0535250264571e-03,
243 2.3713737056617e-03, 2.7384196342644e-03,
244 3.1622776601684e-03, 3.6517412725484e-03,
245 4.2169650342858e-03, 4.8696752516586e-03,
246 5.6234132519035e-03, 6.4938163157621e-03,
247 7.4989420933246e-03, 8.6596432336006e-03,
248 1.0000000000000e-02, 1.1547819846895e-02,
249 1.3335214321633e-02, 1.5399265260595e-02,
250 1.7782794100389e-02, 2.0535250264571e-02,
251 2.3713737056617e-02, 2.7384196342644e-02,
252 3.1622776601684e-02, 3.6517412725484e-02,
253 4.2169650342858e-02, 4.8696752516586e-02,
254 5.6234132519035e-02, 6.4938163157621e-02,
255 7.4989420933246e-02, 8.6596432336007e-02,
256 1.0000000000000e-01, 1.1547819846895e-01,
257 1.3335214321633e-01, 1.5399265260595e-01,
258 1.7782794100389e-01, 2.0535250264571e-01,
259 2.3713737056617e-01, 2.7384196342644e-01,
260 3.1622776601684e-01, 3.6517412725484e-01,
261 4.2169650342858e-01, 4.8696752516586e-01,
262 5.6234132519035e-01, 6.4938163157621e-01,
263 7.4989420933246e-01, 8.6596432336007e-01,
264 1.0000000000000e+00, 1.1547819846895e+00,
265 1.3335214321633e+00, 1.5399265260595e+00,
266 1.7782794100389e+00, 2.0535250264571e+00,
267 2.3713737056617e+00, 2.7384196342644e+00,
268 3.1622776601684e+00, 3.6517412725484e+00,
269 4.2169650342858e+00, 4.8696752516586e+00,
270 5.6234132519035e+00, 6.4938163157621e+00,
271 7.4989420933246e+00, 8.6596432336007e+00,
272 1.0000000000000e+01, 1.1547819846895e+01,
273 1.3335214321633e+01, 1.5399265260595e+01,
274 1.7782794100389e+01, 2.0535250264571e+01,
275 2.3713737056617e+01, 2.7384196342644e+01,
276 3.1622776601684e+01, 3.6517412725484e+01,
277 4.2169650342858e+01, 4.8696752516586e+01,
278 5.6234132519035e+01, 6.4938163157621e+01,
279 7.4989420933246e+01, 8.6596432336007e+01,
280 1.0000000000000e+02, 1.1547819846895e+02,
281 1.3335214321633e+02, 1.5399265260595e+02,
282 1.7782794100389e+02, 2.0535250264571e+02,
283 2.3713737056617e+02, 2.7384196342644e+02,
284 3.1622776601684e+02, 3.6517412725484e+02,
285 4.2169650342858e+02, 4.8696752516586e+02,
286 5.6234132519035e+02, 6.4938163157621e+02,
287 7.4989420933246e+02, 8.6596432336007e+02,
288 1.0000000000000e+03, 1.1547819846895e+03,
289 1.3335214321633e+03, 1.5399265260595e+03,
290 1.7782794100389e+03, 2.0535250264571e+03,
291 2.3713737056617e+03, 2.7384196342644e+03,
292 3.1622776601684e+03, 3.6517412725484e+03,
293 4.2169650342858e+03, 4.8696752516586e+03,
294 5.6234132519035e+03, 6.4938163157621e+03,
295 7.4989420933246e+03, 8.6596432336007e+03,
296 1.0000000000000e+04, 1.1547819846895e+04,
297 1.3335214321633e+04, 1.5399265260595e+04,
298 1.7782794100389e+04, 2.0535250264571e+04,
299 2.3713737056617e+04, 2.7384196342644e+04,
300 3.1622776601684e+04, 3.6517412725484e+04,
301 4.2169650342858e+04, 4.8696752516586e+04,
302 5.6234132519035e+04, 6.4938163157621e+04,
303 7.4989420933246e+04, 8.6596432336007e+04,
304 1.0000000000000e+05, 1.1547819846895e+05,
305 1.3335214321633e+05, 1.5399265260595e+05,
306 1.7782794100389e+05, 2.0535250264571e+05,
307 2.3713737056617e+05, 2.7384196342644e+05,
308 3.1622776601684e+05, 3.6517412725484e+05,
309 4.2169650342858e+05, 4.8696752516586e+05,
310 5.6234132519035e+05, 6.4938163157621e+05,
311 7.4989420933246e+05, 8.6596432336007e+05,
315 * decode exponents coded with VLC codes
317 static int decode_exp_vlc(WMACodecContext *s, int ch)
319 int last_exp, n, code;
322 uint32_t *q, *q_end, iv;
323 const float *ptab = pow_tab + 60;
324 const uint32_t *iptab = (const uint32_t*)ptab;
326 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
327 q = (uint32_t *)s->exponents[ch];
328 q_end = q + s->block_len;
330 if (s->version == 1) {
331 last_exp = get_bits(&s->gb, 5) + 10;
333 iv = iptab[last_exp];
341 } while ((n -= 4) > 0);
346 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
348 av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
351 /* NOTE: this offset is the same as MPEG4 AAC ! */
352 last_exp += code - 60;
353 if ((unsigned)last_exp + 60 >= FF_ARRAY_ELEMS(pow_tab)) {
354 av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
359 iv = iptab[last_exp];
368 } while ((n -= 4) > 0);
370 s->max_exponent[ch] = max_scale;
376 * Apply MDCT window and add into output.
378 * We ensure that when the windows overlap their squared sum
379 * is always 1 (MDCT reconstruction rule).
381 static void wma_window(WMACodecContext *s, float *out)
383 float *in = s->output;
384 int block_len, bsize, n;
387 if (s->block_len_bits <= s->prev_block_len_bits) {
388 block_len = s->block_len;
389 bsize = s->frame_len_bits - s->block_len_bits;
391 s->fdsp.vector_fmul_add(out, in, s->windows[bsize],
395 block_len = 1 << s->prev_block_len_bits;
396 n = (s->block_len - block_len) / 2;
397 bsize = s->frame_len_bits - s->prev_block_len_bits;
399 s->fdsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
402 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
409 if (s->block_len_bits <= s->next_block_len_bits) {
410 block_len = s->block_len;
411 bsize = s->frame_len_bits - s->block_len_bits;
413 s->fdsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
416 block_len = 1 << s->next_block_len_bits;
417 n = (s->block_len - block_len) / 2;
418 bsize = s->frame_len_bits - s->next_block_len_bits;
420 memcpy(out, in, n*sizeof(float));
422 s->fdsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
424 memset(out+n+block_len, 0, n*sizeof(float));
430 * @return 0 if OK. 1 if last block of frame. return -1 if
431 * unrecorrable error.
433 static int wma_decode_block(WMACodecContext *s)
435 int n, v, a, ch, bsize;
436 int coef_nb_bits, total_gain;
437 int nb_coefs[MAX_CHANNELS];
442 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
445 /* compute current block length */
446 if (s->use_variable_block_len) {
447 n = av_log2(s->nb_block_sizes - 1) + 1;
449 if (s->reset_block_lengths) {
450 s->reset_block_lengths = 0;
451 v = get_bits(&s->gb, n);
452 if (v >= s->nb_block_sizes){
453 av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
456 s->prev_block_len_bits = s->frame_len_bits - v;
457 v = get_bits(&s->gb, n);
458 if (v >= s->nb_block_sizes){
459 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
462 s->block_len_bits = s->frame_len_bits - v;
464 /* update block lengths */
465 s->prev_block_len_bits = s->block_len_bits;
466 s->block_len_bits = s->next_block_len_bits;
468 v = get_bits(&s->gb, n);
469 if (v >= s->nb_block_sizes){
470 av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
473 s->next_block_len_bits = s->frame_len_bits - v;
475 /* fixed block len */
476 s->next_block_len_bits = s->frame_len_bits;
477 s->prev_block_len_bits = s->frame_len_bits;
478 s->block_len_bits = s->frame_len_bits;
481 if (s->frame_len_bits - s->block_len_bits >= s->nb_block_sizes){
482 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits not initialized to a valid value\n");
486 /* now check if the block length is coherent with the frame length */
487 s->block_len = 1 << s->block_len_bits;
488 if ((s->block_pos + s->block_len) > s->frame_len){
489 av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
493 if (s->avctx->channels == 2) {
494 s->ms_stereo = get_bits1(&s->gb);
497 for(ch = 0; ch < s->avctx->channels; ch++) {
498 a = get_bits1(&s->gb);
499 s->channel_coded[ch] = a;
503 bsize = s->frame_len_bits - s->block_len_bits;
505 /* if no channel coded, no need to go further */
506 /* XXX: fix potential framing problems */
510 /* read total gain and extract corresponding number of bits for
511 coef escape coding */
514 a = get_bits(&s->gb, 7);
520 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
522 /* compute number of coefficients */
523 n = s->coefs_end[bsize] - s->coefs_start;
524 for(ch = 0; ch < s->avctx->channels; ch++)
528 if (s->use_noise_coding) {
530 for(ch = 0; ch < s->avctx->channels; ch++) {
531 if (s->channel_coded[ch]) {
533 n = s->exponent_high_sizes[bsize];
535 a = get_bits1(&s->gb);
536 s->high_band_coded[ch][i] = a;
537 /* if noise coding, the coefficients are not transmitted */
539 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
543 for(ch = 0; ch < s->avctx->channels; ch++) {
544 if (s->channel_coded[ch]) {
547 n = s->exponent_high_sizes[bsize];
548 val = (int)0x80000000;
550 if (s->high_band_coded[ch][i]) {
551 if (val == (int)0x80000000) {
552 val = get_bits(&s->gb, 7) - 19;
554 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
556 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
561 s->high_band_values[ch][i] = val;
568 /* exponents can be reused in short blocks. */
569 if ((s->block_len_bits == s->frame_len_bits) ||
571 for(ch = 0; ch < s->avctx->channels; ch++) {
572 if (s->channel_coded[ch]) {
573 if (s->use_exp_vlc) {
574 if (decode_exp_vlc(s, ch) < 0)
577 decode_exp_lsp(s, ch);
579 s->exponents_bsize[ch] = bsize;
584 /* parse spectral coefficients : just RLE encoding */
585 for (ch = 0; ch < s->avctx->channels; ch++) {
586 if (s->channel_coded[ch]) {
588 WMACoef* ptr = &s->coefs1[ch][0];
590 /* special VLC tables are used for ms stereo because
591 there is potentially less energy there */
592 tindex = (ch == 1 && s->ms_stereo);
593 memset(ptr, 0, s->block_len * sizeof(WMACoef));
594 ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
595 s->level_table[tindex], s->run_table[tindex],
596 0, ptr, 0, nb_coefs[ch],
597 s->block_len, s->frame_len_bits, coef_nb_bits);
599 if (s->version == 1 && s->avctx->channels >= 2) {
600 align_get_bits(&s->gb);
606 int n4 = s->block_len / 2;
607 mdct_norm = 1.0 / (float)n4;
608 if (s->version == 1) {
609 mdct_norm *= sqrt(n4);
613 /* finally compute the MDCT coefficients */
614 for (ch = 0; ch < s->avctx->channels; ch++) {
615 if (s->channel_coded[ch]) {
617 float *coefs, *exponents, mult, mult1, noise;
618 int i, j, n, n1, last_high_band, esize;
619 float exp_power[HIGH_BAND_MAX_SIZE];
621 coefs1 = s->coefs1[ch];
622 exponents = s->exponents[ch];
623 esize = s->exponents_bsize[ch];
624 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
626 coefs = s->coefs[ch];
627 if (s->use_noise_coding) {
629 /* very low freqs : noise */
630 for(i = 0;i < s->coefs_start; i++) {
631 *coefs++ = s->noise_table[s->noise_index] *
632 exponents[i<<bsize>>esize] * mult1;
633 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
636 n1 = s->exponent_high_sizes[bsize];
638 /* compute power of high bands */
639 exponents = s->exponents[ch] +
640 (s->high_band_start[bsize]<<bsize>>esize);
641 last_high_band = 0; /* avoid warning */
643 n = s->exponent_high_bands[s->frame_len_bits -
644 s->block_len_bits][j];
645 if (s->high_band_coded[ch][j]) {
648 for(i = 0;i < n; i++) {
649 v = exponents[i<<bsize>>esize];
652 exp_power[j] = e2 / n;
654 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
656 exponents += n<<bsize>>esize;
659 /* main freqs and high freqs */
660 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
663 n = s->high_band_start[bsize] -
666 n = s->exponent_high_bands[s->frame_len_bits -
667 s->block_len_bits][j];
669 if (j >= 0 && s->high_band_coded[ch][j]) {
670 /* use noise with specified power */
671 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
672 /* XXX: use a table */
673 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
674 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
676 for(i = 0;i < n; i++) {
677 noise = s->noise_table[s->noise_index];
678 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
680 exponents[i<<bsize>>esize] * mult1;
682 exponents += n<<bsize>>esize;
684 /* coded values + small noise */
685 for(i = 0;i < n; i++) {
686 noise = s->noise_table[s->noise_index];
687 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
688 *coefs++ = ((*coefs1++) + noise) *
689 exponents[i<<bsize>>esize] * mult;
691 exponents += n<<bsize>>esize;
695 /* very high freqs : noise */
696 n = s->block_len - s->coefs_end[bsize];
697 mult1 = mult * exponents[((-1<<bsize))>>esize];
698 for(i = 0; i < n; i++) {
699 *coefs++ = s->noise_table[s->noise_index] * mult1;
700 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
703 /* XXX: optimize more */
704 for(i = 0;i < s->coefs_start; i++)
707 for(i = 0;i < n; i++) {
708 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
710 n = s->block_len - s->coefs_end[bsize];
711 for(i = 0;i < n; i++)
718 for (ch = 0; ch < s->avctx->channels; ch++) {
719 if (s->channel_coded[ch]) {
720 dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
721 dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
726 if (s->ms_stereo && s->channel_coded[1]) {
727 /* nominal case for ms stereo: we do it before mdct */
728 /* no need to optimize this case because it should almost
730 if (!s->channel_coded[0]) {
731 tprintf(s->avctx, "rare ms-stereo case happened\n");
732 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
733 s->channel_coded[0] = 1;
736 s->fdsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
740 mdct = &s->mdct_ctx[bsize];
742 for (ch = 0; ch < s->avctx->channels; ch++) {
745 n4 = s->block_len / 2;
746 if(s->channel_coded[ch]){
747 mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
748 }else if(!(s->ms_stereo && ch==1))
749 memset(s->output, 0, sizeof(s->output));
751 /* multiply by the window and add in the frame */
752 index = (s->frame_len / 2) + s->block_pos - n4;
753 wma_window(s, &s->frame_out[ch][index]);
756 /* update block number */
758 s->block_pos += s->block_len;
759 if (s->block_pos >= s->frame_len)
765 /* decode a frame of frame_len samples */
766 static int wma_decode_frame(WMACodecContext *s, float **samples,
772 tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
775 /* read each block */
779 ret = wma_decode_block(s);
786 for (ch = 0; ch < s->avctx->channels; ch++) {
787 /* copy current block to output */
788 memcpy(samples[ch] + samples_offset, s->frame_out[ch],
789 s->frame_len * sizeof(*s->frame_out[ch]));
790 /* prepare for next block */
791 memmove(&s->frame_out[ch][0], &s->frame_out[ch][s->frame_len],
792 s->frame_len * sizeof(*s->frame_out[ch]));
795 dump_floats(s, "samples", 6, samples[ch] + samples_offset, s->frame_len);
802 static int wma_decode_superframe(AVCodecContext *avctx, void *data,
803 int *got_frame_ptr, AVPacket *avpkt)
805 AVFrame *frame = data;
806 const uint8_t *buf = avpkt->data;
807 int buf_size = avpkt->size;
808 WMACodecContext *s = avctx->priv_data;
809 int nb_frames, bit_offset, i, pos, len, ret;
814 tprintf(avctx, "***decode_superframe:\n");
817 s->last_superframe_len = 0;
820 if (buf_size < avctx->block_align) {
821 av_log(avctx, AV_LOG_ERROR,
822 "Input packet size too small (%d < %d)\n",
823 buf_size, avctx->block_align);
824 return AVERROR_INVALIDDATA;
826 if(avctx->block_align)
827 buf_size = avctx->block_align;
829 init_get_bits(&s->gb, buf, buf_size*8);
831 if (s->use_bit_reservoir) {
832 /* read super frame header */
833 skip_bits(&s->gb, 4); /* super frame index */
834 nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
835 if (nb_frames <= 0) {
836 av_log(avctx, AV_LOG_ERROR, "nb_frames is %d\n", nb_frames);
837 return AVERROR_INVALIDDATA;
843 /* get output buffer */
844 frame->nb_samples = nb_frames * s->frame_len;
845 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
847 samples = (float **)frame->extended_data;
850 if (s->use_bit_reservoir) {
851 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
852 if (bit_offset > get_bits_left(&s->gb)) {
853 av_log(avctx, AV_LOG_ERROR,
854 "Invalid last frame bit offset %d > buf size %d (%d)\n",
855 bit_offset, get_bits_left(&s->gb), buf_size);
859 if (s->last_superframe_len > 0) {
860 /* add bit_offset bits to last frame */
861 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
862 MAX_CODED_SUPERFRAME_SIZE)
864 q = s->last_superframe + s->last_superframe_len;
867 *q++ = (get_bits)(&s->gb, 8);
871 *q++ = (get_bits)(&s->gb, len) << (8 - len);
873 memset(q, 0, FF_INPUT_BUFFER_PADDING_SIZE);
875 /* XXX: bit_offset bits into last frame */
876 init_get_bits(&s->gb, s->last_superframe, s->last_superframe_len * 8 + bit_offset);
877 /* skip unused bits */
878 if (s->last_bitoffset > 0)
879 skip_bits(&s->gb, s->last_bitoffset);
880 /* this frame is stored in the last superframe and in the
882 if (wma_decode_frame(s, samples, samples_offset) < 0)
884 samples_offset += s->frame_len;
888 /* read each frame starting from bit_offset */
889 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
890 if (pos >= MAX_CODED_SUPERFRAME_SIZE * 8 || pos > buf_size * 8)
891 return AVERROR_INVALIDDATA;
892 init_get_bits(&s->gb, buf + (pos >> 3), (buf_size - (pos >> 3))*8);
895 skip_bits(&s->gb, len);
897 s->reset_block_lengths = 1;
898 for(i=0;i<nb_frames;i++) {
899 if (wma_decode_frame(s, samples, samples_offset) < 0)
901 samples_offset += s->frame_len;
904 /* we copy the end of the frame in the last frame buffer */
905 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
906 s->last_bitoffset = pos & 7;
908 len = buf_size - pos;
909 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
910 av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
913 s->last_superframe_len = len;
914 memcpy(s->last_superframe, buf + pos, len);
916 /* single frame decode */
917 if (wma_decode_frame(s, samples, samples_offset) < 0)
919 samples_offset += s->frame_len;
922 av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
923 s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
924 (int8_t *)samples - (int8_t *)data, avctx->block_align);
930 /* when error, we reset the bit reservoir */
931 s->last_superframe_len = 0;
935 static av_cold void flush(AVCodecContext *avctx)
937 WMACodecContext *s = avctx->priv_data;
940 s->last_superframe_len= 0;
943 #if CONFIG_WMAV1_DECODER
944 AVCodec ff_wmav1_decoder = {
946 .type = AVMEDIA_TYPE_AUDIO,
947 .id = AV_CODEC_ID_WMAV1,
948 .priv_data_size = sizeof(WMACodecContext),
949 .init = wma_decode_init,
951 .decode = wma_decode_superframe,
953 .capabilities = CODEC_CAP_DR1,
954 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
955 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
956 AV_SAMPLE_FMT_NONE },
959 #if CONFIG_WMAV2_DECODER
960 AVCodec ff_wmav2_decoder = {
962 .type = AVMEDIA_TYPE_AUDIO,
963 .id = AV_CODEC_ID_WMAV2,
964 .priv_data_size = sizeof(WMACodecContext),
965 .init = wma_decode_init,
967 .decode = wma_decode_superframe,
969 .capabilities = CODEC_CAP_DR1,
970 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),
971 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
972 AV_SAMPLE_FMT_NONE },