2 * WMA compatible decoder
3 * Copyright (c) 2002 The Libav Project
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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(ff_wma_init(avctx, flags2)<0)
109 for(i = 0; i < s->nb_block_sizes; i++)
110 ff_mdct_init(&s->mdct_ctx[i], s->frame_len_bits - i + 1, 1, 1.0);
112 if (s->use_noise_coding) {
113 init_vlc(&s->hgain_vlc, HGAINVLCBITS, sizeof(ff_wma_hgain_huffbits),
114 ff_wma_hgain_huffbits, 1, 1,
115 ff_wma_hgain_huffcodes, 2, 2, 0);
118 if (s->use_exp_vlc) {
119 init_vlc(&s->exp_vlc, EXPVLCBITS, sizeof(ff_aac_scalefactor_bits), //FIXME move out of context
120 ff_aac_scalefactor_bits, 1, 1,
121 ff_aac_scalefactor_code, 4, 4, 0);
123 wma_lsp_to_curve_init(s, s->frame_len);
126 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
131 * compute x^-0.25 with an exponent and mantissa table. We use linear
132 * interpolation to reduce the mantissa table size at a small speed
133 * expense (linear interpolation approximately doubles the number of
134 * bits of precision).
136 static inline float pow_m1_4(WMACodecContext *s, float x)
147 m = (u.v >> (23 - LSP_POW_BITS)) & ((1 << LSP_POW_BITS) - 1);
148 /* build interpolation scale: 1 <= t < 2. */
149 t.v = ((u.v << LSP_POW_BITS) & ((1 << 23) - 1)) | (127 << 23);
150 a = s->lsp_pow_m_table1[m];
151 b = s->lsp_pow_m_table2[m];
152 return s->lsp_pow_e_table[e] * (a + b * t.f);
155 static void wma_lsp_to_curve_init(WMACodecContext *s, int frame_len)
160 wdel = M_PI / frame_len;
161 for(i=0;i<frame_len;i++)
162 s->lsp_cos_table[i] = 2.0f * cos(wdel * i);
164 /* tables for x^-0.25 computation */
167 s->lsp_pow_e_table[i] = pow(2.0, e * -0.25);
170 /* NOTE: these two tables are needed to avoid two operations in
173 for(i=(1 << LSP_POW_BITS) - 1;i>=0;i--) {
174 m = (1 << LSP_POW_BITS) + i;
175 a = (float)m * (0.5 / (1 << LSP_POW_BITS));
177 s->lsp_pow_m_table1[i] = 2 * a - b;
178 s->lsp_pow_m_table2[i] = b - a;
184 * NOTE: We use the same code as Vorbis here
185 * @todo optimize it further with SSE/3Dnow
187 static void wma_lsp_to_curve(WMACodecContext *s,
188 float *out, float *val_max_ptr,
192 float p, q, w, v, val_max;
198 w = s->lsp_cos_table[i];
199 for(j=1;j<NB_LSP_COEFS;j+=2){
211 *val_max_ptr = val_max;
215 * decode exponents coded with LSP coefficients (same idea as Vorbis)
217 static void decode_exp_lsp(WMACodecContext *s, int ch)
219 float lsp_coefs[NB_LSP_COEFS];
222 for(i = 0; i < NB_LSP_COEFS; i++) {
223 if (i == 0 || i >= 8)
224 val = get_bits(&s->gb, 3);
226 val = get_bits(&s->gb, 4);
227 lsp_coefs[i] = ff_wma_lsp_codebook[i][val];
230 wma_lsp_to_curve(s, s->exponents[ch], &s->max_exponent[ch],
231 s->block_len, lsp_coefs);
234 /** pow(10, i / 16.0) for i in -60..95 */
235 static const float pow_tab[] = {
236 1.7782794100389e-04, 2.0535250264571e-04,
237 2.3713737056617e-04, 2.7384196342644e-04,
238 3.1622776601684e-04, 3.6517412725484e-04,
239 4.2169650342858e-04, 4.8696752516586e-04,
240 5.6234132519035e-04, 6.4938163157621e-04,
241 7.4989420933246e-04, 8.6596432336006e-04,
242 1.0000000000000e-03, 1.1547819846895e-03,
243 1.3335214321633e-03, 1.5399265260595e-03,
244 1.7782794100389e-03, 2.0535250264571e-03,
245 2.3713737056617e-03, 2.7384196342644e-03,
246 3.1622776601684e-03, 3.6517412725484e-03,
247 4.2169650342858e-03, 4.8696752516586e-03,
248 5.6234132519035e-03, 6.4938163157621e-03,
249 7.4989420933246e-03, 8.6596432336006e-03,
250 1.0000000000000e-02, 1.1547819846895e-02,
251 1.3335214321633e-02, 1.5399265260595e-02,
252 1.7782794100389e-02, 2.0535250264571e-02,
253 2.3713737056617e-02, 2.7384196342644e-02,
254 3.1622776601684e-02, 3.6517412725484e-02,
255 4.2169650342858e-02, 4.8696752516586e-02,
256 5.6234132519035e-02, 6.4938163157621e-02,
257 7.4989420933246e-02, 8.6596432336007e-02,
258 1.0000000000000e-01, 1.1547819846895e-01,
259 1.3335214321633e-01, 1.5399265260595e-01,
260 1.7782794100389e-01, 2.0535250264571e-01,
261 2.3713737056617e-01, 2.7384196342644e-01,
262 3.1622776601684e-01, 3.6517412725484e-01,
263 4.2169650342858e-01, 4.8696752516586e-01,
264 5.6234132519035e-01, 6.4938163157621e-01,
265 7.4989420933246e-01, 8.6596432336007e-01,
266 1.0000000000000e+00, 1.1547819846895e+00,
267 1.3335214321633e+00, 1.5399265260595e+00,
268 1.7782794100389e+00, 2.0535250264571e+00,
269 2.3713737056617e+00, 2.7384196342644e+00,
270 3.1622776601684e+00, 3.6517412725484e+00,
271 4.2169650342858e+00, 4.8696752516586e+00,
272 5.6234132519035e+00, 6.4938163157621e+00,
273 7.4989420933246e+00, 8.6596432336007e+00,
274 1.0000000000000e+01, 1.1547819846895e+01,
275 1.3335214321633e+01, 1.5399265260595e+01,
276 1.7782794100389e+01, 2.0535250264571e+01,
277 2.3713737056617e+01, 2.7384196342644e+01,
278 3.1622776601684e+01, 3.6517412725484e+01,
279 4.2169650342858e+01, 4.8696752516586e+01,
280 5.6234132519035e+01, 6.4938163157621e+01,
281 7.4989420933246e+01, 8.6596432336007e+01,
282 1.0000000000000e+02, 1.1547819846895e+02,
283 1.3335214321633e+02, 1.5399265260595e+02,
284 1.7782794100389e+02, 2.0535250264571e+02,
285 2.3713737056617e+02, 2.7384196342644e+02,
286 3.1622776601684e+02, 3.6517412725484e+02,
287 4.2169650342858e+02, 4.8696752516586e+02,
288 5.6234132519035e+02, 6.4938163157621e+02,
289 7.4989420933246e+02, 8.6596432336007e+02,
290 1.0000000000000e+03, 1.1547819846895e+03,
291 1.3335214321633e+03, 1.5399265260595e+03,
292 1.7782794100389e+03, 2.0535250264571e+03,
293 2.3713737056617e+03, 2.7384196342644e+03,
294 3.1622776601684e+03, 3.6517412725484e+03,
295 4.2169650342858e+03, 4.8696752516586e+03,
296 5.6234132519035e+03, 6.4938163157621e+03,
297 7.4989420933246e+03, 8.6596432336007e+03,
298 1.0000000000000e+04, 1.1547819846895e+04,
299 1.3335214321633e+04, 1.5399265260595e+04,
300 1.7782794100389e+04, 2.0535250264571e+04,
301 2.3713737056617e+04, 2.7384196342644e+04,
302 3.1622776601684e+04, 3.6517412725484e+04,
303 4.2169650342858e+04, 4.8696752516586e+04,
304 5.6234132519035e+04, 6.4938163157621e+04,
305 7.4989420933246e+04, 8.6596432336007e+04,
306 1.0000000000000e+05, 1.1547819846895e+05,
307 1.3335214321633e+05, 1.5399265260595e+05,
308 1.7782794100389e+05, 2.0535250264571e+05,
309 2.3713737056617e+05, 2.7384196342644e+05,
310 3.1622776601684e+05, 3.6517412725484e+05,
311 4.2169650342858e+05, 4.8696752516586e+05,
312 5.6234132519035e+05, 6.4938163157621e+05,
313 7.4989420933246e+05, 8.6596432336007e+05,
317 * decode exponents coded with VLC codes
319 static int decode_exp_vlc(WMACodecContext *s, int ch)
321 int last_exp, n, code;
324 uint32_t *q, *q_end, iv;
325 const float *ptab = pow_tab + 60;
326 const uint32_t *iptab = (const uint32_t*)ptab;
328 ptr = s->exponent_bands[s->frame_len_bits - s->block_len_bits];
329 q = (uint32_t *)s->exponents[ch];
330 q_end = q + s->block_len;
332 if (s->version == 1) {
333 last_exp = get_bits(&s->gb, 5) + 10;
335 iv = iptab[last_exp];
343 } while ((n -= 4) > 0);
348 code = get_vlc2(&s->gb, s->exp_vlc.table, EXPVLCBITS, EXPMAX);
350 av_log(s->avctx, AV_LOG_ERROR, "Exponent vlc invalid\n");
353 /* NOTE: this offset is the same as MPEG4 AAC ! */
354 last_exp += code - 60;
355 if ((unsigned)last_exp + 60 > FF_ARRAY_ELEMS(pow_tab)) {
356 av_log(s->avctx, AV_LOG_ERROR, "Exponent out of range: %d\n",
361 iv = iptab[last_exp];
370 } while ((n -= 4) > 0);
372 s->max_exponent[ch] = max_scale;
378 * Apply MDCT window and add into output.
380 * We ensure that when the windows overlap their squared sum
381 * is always 1 (MDCT reconstruction rule).
383 static void wma_window(WMACodecContext *s, float *out)
385 float *in = s->output;
386 int block_len, bsize, n;
389 if (s->block_len_bits <= s->prev_block_len_bits) {
390 block_len = s->block_len;
391 bsize = s->frame_len_bits - s->block_len_bits;
393 s->dsp.vector_fmul_add(out, in, s->windows[bsize],
397 block_len = 1 << s->prev_block_len_bits;
398 n = (s->block_len - block_len) / 2;
399 bsize = s->frame_len_bits - s->prev_block_len_bits;
401 s->dsp.vector_fmul_add(out+n, in+n, s->windows[bsize],
404 memcpy(out+n+block_len, in+n+block_len, n*sizeof(float));
411 if (s->block_len_bits <= s->next_block_len_bits) {
412 block_len = s->block_len;
413 bsize = s->frame_len_bits - s->block_len_bits;
415 s->dsp.vector_fmul_reverse(out, in, s->windows[bsize], block_len);
418 block_len = 1 << s->next_block_len_bits;
419 n = (s->block_len - block_len) / 2;
420 bsize = s->frame_len_bits - s->next_block_len_bits;
422 memcpy(out, in, n*sizeof(float));
424 s->dsp.vector_fmul_reverse(out+n, in+n, s->windows[bsize], block_len);
426 memset(out+n+block_len, 0, n*sizeof(float));
432 * @return 0 if OK. 1 if last block of frame. return -1 if
433 * unrecorrable error.
435 static int wma_decode_block(WMACodecContext *s)
437 int n, v, a, ch, bsize;
438 int coef_nb_bits, total_gain;
439 int nb_coefs[MAX_CHANNELS];
444 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
447 /* compute current block length */
448 if (s->use_variable_block_len) {
449 n = av_log2(s->nb_block_sizes - 1) + 1;
451 if (s->reset_block_lengths) {
452 s->reset_block_lengths = 0;
453 v = get_bits(&s->gb, n);
454 if (v >= s->nb_block_sizes){
455 av_log(s->avctx, AV_LOG_ERROR, "prev_block_len_bits %d out of range\n", s->frame_len_bits - v);
458 s->prev_block_len_bits = s->frame_len_bits - v;
459 v = get_bits(&s->gb, n);
460 if (v >= s->nb_block_sizes){
461 av_log(s->avctx, AV_LOG_ERROR, "block_len_bits %d out of range\n", s->frame_len_bits - v);
464 s->block_len_bits = s->frame_len_bits - v;
466 /* update block lengths */
467 s->prev_block_len_bits = s->block_len_bits;
468 s->block_len_bits = s->next_block_len_bits;
470 v = get_bits(&s->gb, n);
471 if (v >= s->nb_block_sizes){
472 av_log(s->avctx, AV_LOG_ERROR, "next_block_len_bits %d out of range\n", s->frame_len_bits - v);
475 s->next_block_len_bits = s->frame_len_bits - v;
477 /* fixed block len */
478 s->next_block_len_bits = s->frame_len_bits;
479 s->prev_block_len_bits = s->frame_len_bits;
480 s->block_len_bits = s->frame_len_bits;
483 /* now check if the block length is coherent with the frame length */
484 s->block_len = 1 << s->block_len_bits;
485 if ((s->block_pos + s->block_len) > s->frame_len){
486 av_log(s->avctx, AV_LOG_ERROR, "frame_len overflow\n");
490 if (s->nb_channels == 2) {
491 s->ms_stereo = get_bits1(&s->gb);
494 for(ch = 0; ch < s->nb_channels; ch++) {
495 a = get_bits1(&s->gb);
496 s->channel_coded[ch] = a;
500 bsize = s->frame_len_bits - s->block_len_bits;
502 /* if no channel coded, no need to go further */
503 /* XXX: fix potential framing problems */
507 /* read total gain and extract corresponding number of bits for
508 coef escape coding */
511 a = get_bits(&s->gb, 7);
517 coef_nb_bits= ff_wma_total_gain_to_bits(total_gain);
519 /* compute number of coefficients */
520 n = s->coefs_end[bsize] - s->coefs_start;
521 for(ch = 0; ch < s->nb_channels; ch++)
525 if (s->use_noise_coding) {
527 for(ch = 0; ch < s->nb_channels; ch++) {
528 if (s->channel_coded[ch]) {
530 n = s->exponent_high_sizes[bsize];
532 a = get_bits1(&s->gb);
533 s->high_band_coded[ch][i] = a;
534 /* if noise coding, the coefficients are not transmitted */
536 nb_coefs[ch] -= s->exponent_high_bands[bsize][i];
540 for(ch = 0; ch < s->nb_channels; ch++) {
541 if (s->channel_coded[ch]) {
544 n = s->exponent_high_sizes[bsize];
545 val = (int)0x80000000;
547 if (s->high_band_coded[ch][i]) {
548 if (val == (int)0x80000000) {
549 val = get_bits(&s->gb, 7) - 19;
551 code = get_vlc2(&s->gb, s->hgain_vlc.table, HGAINVLCBITS, HGAINMAX);
553 av_log(s->avctx, AV_LOG_ERROR, "hgain vlc invalid\n");
558 s->high_band_values[ch][i] = val;
565 /* exponents can be reused in short blocks. */
566 if ((s->block_len_bits == s->frame_len_bits) ||
568 for(ch = 0; ch < s->nb_channels; ch++) {
569 if (s->channel_coded[ch]) {
570 if (s->use_exp_vlc) {
571 if (decode_exp_vlc(s, ch) < 0)
574 decode_exp_lsp(s, ch);
576 s->exponents_bsize[ch] = bsize;
581 /* parse spectral coefficients : just RLE encoding */
582 for(ch = 0; ch < s->nb_channels; ch++) {
583 if (s->channel_coded[ch]) {
585 WMACoef* ptr = &s->coefs1[ch][0];
587 /* special VLC tables are used for ms stereo because
588 there is potentially less energy there */
589 tindex = (ch == 1 && s->ms_stereo);
590 memset(ptr, 0, s->block_len * sizeof(WMACoef));
591 ff_wma_run_level_decode(s->avctx, &s->gb, &s->coef_vlc[tindex],
592 s->level_table[tindex], s->run_table[tindex],
593 0, ptr, 0, nb_coefs[ch],
594 s->block_len, s->frame_len_bits, coef_nb_bits);
596 if (s->version == 1 && s->nb_channels >= 2) {
597 align_get_bits(&s->gb);
603 int n4 = s->block_len / 2;
604 mdct_norm = 1.0 / (float)n4;
605 if (s->version == 1) {
606 mdct_norm *= sqrt(n4);
610 /* finally compute the MDCT coefficients */
611 for(ch = 0; ch < s->nb_channels; ch++) {
612 if (s->channel_coded[ch]) {
614 float *coefs, *exponents, mult, mult1, noise;
615 int i, j, n, n1, last_high_band, esize;
616 float exp_power[HIGH_BAND_MAX_SIZE];
618 coefs1 = s->coefs1[ch];
619 exponents = s->exponents[ch];
620 esize = s->exponents_bsize[ch];
621 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
623 coefs = s->coefs[ch];
624 if (s->use_noise_coding) {
626 /* very low freqs : noise */
627 for(i = 0;i < s->coefs_start; i++) {
628 *coefs++ = s->noise_table[s->noise_index] *
629 exponents[i<<bsize>>esize] * mult1;
630 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
633 n1 = s->exponent_high_sizes[bsize];
635 /* compute power of high bands */
636 exponents = s->exponents[ch] +
637 (s->high_band_start[bsize]<<bsize>>esize);
638 last_high_band = 0; /* avoid warning */
640 n = s->exponent_high_bands[s->frame_len_bits -
641 s->block_len_bits][j];
642 if (s->high_band_coded[ch][j]) {
645 for(i = 0;i < n; i++) {
646 v = exponents[i<<bsize>>esize];
649 exp_power[j] = e2 / n;
651 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
653 exponents += n<<bsize>>esize;
656 /* main freqs and high freqs */
657 exponents = s->exponents[ch] + (s->coefs_start<<bsize>>esize);
660 n = s->high_band_start[bsize] -
663 n = s->exponent_high_bands[s->frame_len_bits -
664 s->block_len_bits][j];
666 if (j >= 0 && s->high_band_coded[ch][j]) {
667 /* use noise with specified power */
668 mult1 = sqrt(exp_power[j] / exp_power[last_high_band]);
669 /* XXX: use a table */
670 mult1 = mult1 * pow(10, s->high_band_values[ch][j] * 0.05);
671 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
673 for(i = 0;i < n; i++) {
674 noise = s->noise_table[s->noise_index];
675 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
677 exponents[i<<bsize>>esize] * mult1;
679 exponents += n<<bsize>>esize;
681 /* coded values + small noise */
682 for(i = 0;i < n; i++) {
683 noise = s->noise_table[s->noise_index];
684 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
685 *coefs++ = ((*coefs1++) + noise) *
686 exponents[i<<bsize>>esize] * mult;
688 exponents += n<<bsize>>esize;
692 /* very high freqs : noise */
693 n = s->block_len - s->coefs_end[bsize];
694 mult1 = mult * exponents[((-1<<bsize))>>esize];
695 for(i = 0; i < n; i++) {
696 *coefs++ = s->noise_table[s->noise_index] * mult1;
697 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
700 /* XXX: optimize more */
701 for(i = 0;i < s->coefs_start; i++)
704 for(i = 0;i < n; i++) {
705 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
707 n = s->block_len - s->coefs_end[bsize];
708 for(i = 0;i < n; i++)
715 for(ch = 0; ch < s->nb_channels; ch++) {
716 if (s->channel_coded[ch]) {
717 dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
718 dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
723 if (s->ms_stereo && s->channel_coded[1]) {
724 /* nominal case for ms stereo: we do it before mdct */
725 /* no need to optimize this case because it should almost
727 if (!s->channel_coded[0]) {
728 tprintf(s->avctx, "rare ms-stereo case happened\n");
729 memset(s->coefs[0], 0, sizeof(float) * s->block_len);
730 s->channel_coded[0] = 1;
733 s->dsp.butterflies_float(s->coefs[0], s->coefs[1], s->block_len);
737 mdct = &s->mdct_ctx[bsize];
739 for(ch = 0; ch < s->nb_channels; ch++) {
742 n4 = s->block_len / 2;
743 if(s->channel_coded[ch]){
744 mdct->imdct_calc(mdct, s->output, s->coefs[ch]);
745 }else if(!(s->ms_stereo && ch==1))
746 memset(s->output, 0, sizeof(s->output));
748 /* multiply by the window and add in the frame */
749 index = (s->frame_len / 2) + s->block_pos - n4;
750 wma_window(s, &s->frame_out[ch][index]);
753 /* update block number */
755 s->block_pos += s->block_len;
756 if (s->block_pos >= s->frame_len)
762 /* decode a frame of frame_len samples */
763 static int wma_decode_frame(WMACodecContext *s, int16_t *samples)
765 int ret, n, ch, incr;
766 const float *output[MAX_CHANNELS];
769 tprintf(s->avctx, "***decode_frame: %d size=%d\n", s->frame_count++, s->frame_len);
772 /* read each block */
776 ret = wma_decode_block(s);
783 /* convert frame to integer */
785 incr = s->nb_channels;
786 for (ch = 0; ch < MAX_CHANNELS; ch++)
787 output[ch] = s->frame_out[ch];
788 s->fmt_conv.float_to_int16_interleave(samples, output, n, incr);
789 for (ch = 0; ch < incr; ch++) {
790 /* prepare for next block */
791 memmove(&s->frame_out[ch][0], &s->frame_out[ch][n], n * sizeof(float));
795 dump_shorts(s, "samples", samples, n * s->nb_channels);
800 static int wma_decode_superframe(AVCodecContext *avctx,
801 void *data, int *data_size,
804 const uint8_t *buf = avpkt->data;
805 int buf_size = avpkt->size;
806 WMACodecContext *s = avctx->priv_data;
807 int nb_frames, bit_offset, i, pos, len;
811 tprintf(avctx, "***decode_superframe:\n");
814 s->last_superframe_len = 0;
817 if (buf_size < s->block_align)
819 buf_size = s->block_align;
823 init_get_bits(&s->gb, buf, buf_size*8);
825 if (s->use_bit_reservoir) {
826 /* read super frame header */
827 skip_bits(&s->gb, 4); /* super frame index */
828 nb_frames = get_bits(&s->gb, 4) - 1;
830 if((nb_frames+1) * s->nb_channels * s->frame_len * sizeof(int16_t) > *data_size){
831 av_log(s->avctx, AV_LOG_ERROR, "Insufficient output space\n");
835 bit_offset = get_bits(&s->gb, s->byte_offset_bits + 3);
837 if (s->last_superframe_len > 0) {
838 // printf("skip=%d\n", s->last_bitoffset);
839 /* add bit_offset bits to last frame */
840 if ((s->last_superframe_len + ((bit_offset + 7) >> 3)) >
841 MAX_CODED_SUPERFRAME_SIZE)
843 q = s->last_superframe + s->last_superframe_len;
846 *q++ = (get_bits)(&s->gb, 8);
850 *q++ = (get_bits)(&s->gb, len) << (8 - len);
853 /* XXX: bit_offset bits into last frame */
854 init_get_bits(&s->gb, s->last_superframe, MAX_CODED_SUPERFRAME_SIZE*8);
855 /* skip unused bits */
856 if (s->last_bitoffset > 0)
857 skip_bits(&s->gb, s->last_bitoffset);
858 /* this frame is stored in the last superframe and in the
860 if (wma_decode_frame(s, samples) < 0)
862 samples += s->nb_channels * s->frame_len;
865 /* read each frame starting from bit_offset */
866 pos = bit_offset + 4 + 4 + s->byte_offset_bits + 3;
867 init_get_bits(&s->gb, buf + (pos >> 3), (MAX_CODED_SUPERFRAME_SIZE - (pos >> 3))*8);
870 skip_bits(&s->gb, len);
872 s->reset_block_lengths = 1;
873 for(i=0;i<nb_frames;i++) {
874 if (wma_decode_frame(s, samples) < 0)
876 samples += s->nb_channels * s->frame_len;
879 /* we copy the end of the frame in the last frame buffer */
880 pos = get_bits_count(&s->gb) + ((bit_offset + 4 + 4 + s->byte_offset_bits + 3) & ~7);
881 s->last_bitoffset = pos & 7;
883 len = buf_size - pos;
884 if (len > MAX_CODED_SUPERFRAME_SIZE || len < 0) {
885 av_log(s->avctx, AV_LOG_ERROR, "len %d invalid\n", len);
888 s->last_superframe_len = len;
889 memcpy(s->last_superframe, buf + pos, len);
891 if(s->nb_channels * s->frame_len * sizeof(int16_t) > *data_size){
892 av_log(s->avctx, AV_LOG_ERROR, "Insufficient output space\n");
895 /* single frame decode */
896 if (wma_decode_frame(s, samples) < 0)
898 samples += s->nb_channels * s->frame_len;
901 //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);
903 *data_size = (int8_t *)samples - (int8_t *)data;
904 return s->block_align;
906 /* when error, we reset the bit reservoir */
907 s->last_superframe_len = 0;
911 static av_cold void flush(AVCodecContext *avctx)
913 WMACodecContext *s = avctx->priv_data;
916 s->last_superframe_len= 0;
919 AVCodec ff_wmav1_decoder =
924 sizeof(WMACodecContext),
928 wma_decode_superframe,
930 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 1"),
933 AVCodec ff_wmav2_decoder =
938 sizeof(WMACodecContext),
942 wma_decode_superframe,
944 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 2"),