2 * WMA compatible decoder
3 * Copyright (c) 2002 The FFmpeg Project.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * WMA compatible decoder.
23 * This decoder handles Microsoft Windows Media Audio data, versions 1 & 2.
24 * WMA v1 is identified by audio format 0x160 in Microsoft media files
25 * (ASF/AVI/WAV). WMA v2 is identified by audio format 0x161.
27 * To use this decoder, a calling application must supply the extra data
28 * bytes provided with the WMA data. These are the extra, codec-specific
29 * bytes at the end of a WAVEFORMATEX data structure. Transmit these bytes
30 * to the decoder using the extradata[_size] fields in AVCodecContext. There
31 * should be 4 extra bytes for v1 data and 6 extra bytes for v2 data.
35 #include "bitstream.h"
39 #define BLOCK_MIN_BITS 7
40 #define BLOCK_MAX_BITS 11
41 #define BLOCK_MAX_SIZE (1 << BLOCK_MAX_BITS)
43 #define BLOCK_NB_SIZES (BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1)
45 /* XXX: find exact max size */
46 #define HIGH_BAND_MAX_SIZE 16
48 #define NB_LSP_COEFS 10
50 /* XXX: is it a suitable value ? */
51 #define MAX_CODED_SUPERFRAME_SIZE 16384
53 #define MAX_CHANNELS 2
55 #define NOISE_TAB_SIZE 8192
57 #define LSP_POW_BITS 7
59 typedef struct WMADecodeContext
{
64 int version
; /* 1 = 0x160 (WMAV1), 2 = 0x161 (WMAV2) */
66 int use_bit_reservoir
;
67 int use_variable_block_len
;
68 int use_exp_vlc
; /* exponent coding: 0 = lsp, 1 = vlc + delta */
69 int use_noise_coding
; /* true if perceptual noise is added */
72 int exponent_sizes
[BLOCK_NB_SIZES
];
73 uint16_t exponent_bands
[BLOCK_NB_SIZES
][25];
74 int high_band_start
[BLOCK_NB_SIZES
]; /* index of first coef in high band */
75 int coefs_start
; /* first coded coef */
76 int coefs_end
[BLOCK_NB_SIZES
]; /* max number of coded coefficients */
77 int exponent_high_sizes
[BLOCK_NB_SIZES
];
78 int exponent_high_bands
[BLOCK_NB_SIZES
][HIGH_BAND_MAX_SIZE
];
81 /* coded values in high bands */
82 int high_band_coded
[MAX_CHANNELS
][HIGH_BAND_MAX_SIZE
];
83 int high_band_values
[MAX_CHANNELS
][HIGH_BAND_MAX_SIZE
];
85 /* there are two possible tables for spectral coefficients */
87 uint16_t *run_table
[2];
88 uint16_t *level_table
[2];
90 int frame_len
; /* frame length in samples */
91 int frame_len_bits
; /* frame_len = 1 << frame_len_bits */
92 int nb_block_sizes
; /* number of block sizes */
94 int reset_block_lengths
;
95 int block_len_bits
; /* log2 of current block length */
96 int next_block_len_bits
; /* log2 of next block length */
97 int prev_block_len_bits
; /* log2 of prev block length */
98 int block_len
; /* block length in samples */
99 int block_num
; /* block number in current frame */
100 int block_pos
; /* current position in frame */
101 uint8_t ms_stereo
; /* true if mid/side stereo mode */
102 uint8_t channel_coded
[MAX_CHANNELS
]; /* true if channel is coded */
103 float exponents
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] __attribute__((aligned(16)));
104 float max_exponent
[MAX_CHANNELS
];
105 int16_t coefs1
[MAX_CHANNELS
][BLOCK_MAX_SIZE
];
106 float coefs
[MAX_CHANNELS
][BLOCK_MAX_SIZE
] __attribute__((aligned(16)));
107 MDCTContext mdct_ctx
[BLOCK_NB_SIZES
];
108 float *windows
[BLOCK_NB_SIZES
];
109 FFTSample mdct_tmp
[BLOCK_MAX_SIZE
] __attribute__((aligned(16))); /* temporary storage for imdct */
110 /* output buffer for one frame and the last for IMDCT windowing */
111 float frame_out
[MAX_CHANNELS
][BLOCK_MAX_SIZE
* 2] __attribute__((aligned(16)));
112 /* last frame info */
113 uint8_t last_superframe
[MAX_CODED_SUPERFRAME_SIZE
+ 4]; /* padding added */
115 int last_superframe_len
;
116 float noise_table
[NOISE_TAB_SIZE
];
118 float noise_mult
; /* XXX: suppress that and integrate it in the noise array */
119 /* lsp_to_curve tables */
120 float lsp_cos_table
[BLOCK_MAX_SIZE
];
121 float lsp_pow_e_table
[256];
122 float lsp_pow_m_table1
[(1 << LSP_POW_BITS
)];
123 float lsp_pow_m_table2
[(1 << LSP_POW_BITS
)];
130 typedef struct CoefVLCTable
{
131 int n
; /* total number of codes */
132 const uint32_t *huffcodes
; /* VLC bit values */
133 const uint8_t *huffbits
; /* VLC bit size */
134 const uint16_t *levels
; /* table to build run/level tables */
137 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
);
142 static void dump_shorts(const char *name
, const short *tab
, int n
)
146 tprintf("%s[%d]:\n", name
, n
);
150 tprintf(" %5d.0", tab
[i
]);
156 static void dump_floats(const char *name
, int prec
, const float *tab
, int n
)
160 tprintf("%s[%d]:\n", name
, n
);
164 tprintf(" %8.*f", prec
, tab
[i
]);
173 /* XXX: use same run/length optimization as mpeg decoders */
174 static void init_coef_vlc(VLC
*vlc
,
175 uint16_t **prun_table
, uint16_t **plevel_table
,
176 const CoefVLCTable
*vlc_table
)
178 int n
= vlc_table
->n
;
179 const uint8_t *table_bits
= vlc_table
->huffbits
;
180 const uint32_t *table_codes
= vlc_table
->huffcodes
;
181 const uint16_t *levels_table
= vlc_table
->levels
;
182 uint16_t *run_table
, *level_table
;
186 init_vlc(vlc
, 9, n
, table_bits
, 1, 1, table_codes
, 4, 4, 0);
188 run_table
= av_malloc(n
* sizeof(uint16_t));
189 level_table
= av_malloc(n
* sizeof(uint16_t));
197 level_table
[i
] = level
;
202 *prun_table
= run_table
;
203 *plevel_table
= level_table
;
206 static int wma_decode_init(AVCodecContext
* avctx
)
208 WMADecodeContext
*s
= avctx
->priv_data
;
209 int i
, flags1
, flags2
;
212 float bps1
, high_freq
;
217 s
->sample_rate
= avctx
->sample_rate
;
218 s
->nb_channels
= avctx
->channels
;
219 s
->bit_rate
= avctx
->bit_rate
;
220 s
->block_align
= avctx
->block_align
;
222 if (avctx
->codec
->id
== CODEC_ID_WMAV1
) {
228 /* extract flag infos */
231 extradata
= avctx
->extradata
;
232 if (s
->version
== 1 && avctx
->extradata_size
>= 4) {
233 flags1
= extradata
[0] | (extradata
[1] << 8);
234 flags2
= extradata
[2] | (extradata
[3] << 8);
235 } else if (s
->version
== 2 && avctx
->extradata_size
>= 6) {
236 flags1
= extradata
[0] | (extradata
[1] << 8) |
237 (extradata
[2] << 16) | (extradata
[3] << 24);
238 flags2
= extradata
[4] | (extradata
[5] << 8);
240 s
->use_exp_vlc
= flags2
& 0x0001;
241 s
->use_bit_reservoir
= flags2
& 0x0002;
242 s
->use_variable_block_len
= flags2
& 0x0004;
244 /* compute MDCT block size */
245 if (s
->sample_rate
<= 16000) {
246 s
->frame_len_bits
= 9;
247 } else if (s
->sample_rate
<= 22050 ||
248 (s
->sample_rate
<= 32000 && s
->version
== 1)) {
249 s
->frame_len_bits
= 10;
251 s
->frame_len_bits
= 11;
253 s
->frame_len
= 1 << s
->frame_len_bits
;
254 if (s
->use_variable_block_len
) {
256 nb
= ((flags2
>> 3) & 3) + 1;
257 if ((s
->bit_rate
/ s
->nb_channels
) >= 32000)
259 nb_max
= s
->frame_len_bits
- BLOCK_MIN_BITS
;
262 s
->nb_block_sizes
= nb
+ 1;
264 s
->nb_block_sizes
= 1;
267 /* init rate dependant parameters */
268 s
->use_noise_coding
= 1;
269 high_freq
= s
->sample_rate
* 0.5;
271 /* if version 2, then the rates are normalized */
272 sample_rate1
= s
->sample_rate
;
273 if (s
->version
== 2) {
274 if (sample_rate1
>= 44100)
275 sample_rate1
= 44100;
276 else if (sample_rate1
>= 22050)
277 sample_rate1
= 22050;
278 else if (sample_rate1
>= 16000)
279 sample_rate1
= 16000;
280 else if (sample_rate1
>= 11025)
281 sample_rate1
= 11025;
282 else if (sample_rate1
>= 8000)
286 bps
= (float)s
->bit_rate
/ (float)(s
->nb_channels
* s
->sample_rate
);
287 s
->byte_offset_bits
= av_log2((int)(bps
* s
->frame_len
/ 8.0)) + 2;
289 /* compute high frequency value and choose if noise coding should
292 if (s
->nb_channels
== 2)
294 if (sample_rate1
== 44100) {
296 s
->use_noise_coding
= 0;
298 high_freq
= high_freq
* 0.4;
299 } else if (sample_rate1
== 22050) {
301 s
->use_noise_coding
= 0;
302 else if (bps1
>= 0.72)
303 high_freq
= high_freq
* 0.7;
305 high_freq
= high_freq
* 0.6;
306 } else if (sample_rate1
== 16000) {
308 high_freq
= high_freq
* 0.5;
310 high_freq
= high_freq
* 0.3;
311 } else if (sample_rate1
== 11025) {
312 high_freq
= high_freq
* 0.7;
313 } else if (sample_rate1
== 8000) {
315 high_freq
= high_freq
* 0.5;
316 } else if (bps
> 0.75) {
317 s
->use_noise_coding
= 0;
319 high_freq
= high_freq
* 0.65;
323 high_freq
= high_freq
* 0.75;
324 } else if (bps
>= 0.6) {
325 high_freq
= high_freq
* 0.6;
327 high_freq
= high_freq
* 0.5;
330 dprintf("flags1=0x%x flags2=0x%x\n", flags1
, flags2
);
331 dprintf("version=%d channels=%d sample_rate=%d bitrate=%d block_align=%d\n",
332 s
->version
, s
->nb_channels
, s
->sample_rate
, s
->bit_rate
,
334 dprintf("bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
335 bps
, bps1
, high_freq
, s
->byte_offset_bits
);
336 dprintf("use_noise_coding=%d use_exp_vlc=%d nb_block_sizes=%d\n",
337 s
->use_noise_coding
, s
->use_exp_vlc
, s
->nb_block_sizes
);
339 /* compute the scale factor band sizes for each MDCT block size */
341 int a
, b
, pos
, lpos
, k
, block_len
, i
, j
, n
;
342 const uint8_t *table
;
344 if (s
->version
== 1) {
349 for(k
= 0; k
< s
->nb_block_sizes
; k
++) {
350 block_len
= s
->frame_len
>> k
;
352 if (s
->version
== 1) {
355 a
= wma_critical_freqs
[i
];
357 pos
= ((block_len
* 2 * a
) + (b
>> 1)) / b
;
360 s
->exponent_bands
[0][i
] = pos
- lpos
;
361 if (pos
>= block_len
) {
367 s
->exponent_sizes
[0] = i
;
369 /* hardcoded tables */
371 a
= s
->frame_len_bits
- BLOCK_MIN_BITS
- k
;
373 if (s
->sample_rate
>= 44100)
374 table
= exponent_band_44100
[a
];
375 else if (s
->sample_rate
>= 32000)
376 table
= exponent_band_32000
[a
];
377 else if (s
->sample_rate
>= 22050)
378 table
= exponent_band_22050
[a
];
383 s
->exponent_bands
[k
][i
] = table
[i
];
384 s
->exponent_sizes
[k
] = n
;
389 a
= wma_critical_freqs
[i
];
391 pos
= ((block_len
* 2 * a
) + (b
<< 1)) / (4 * b
);
396 s
->exponent_bands
[k
][j
++] = pos
- lpos
;
397 if (pos
>= block_len
)
401 s
->exponent_sizes
[k
] = j
;
405 /* max number of coefs */
406 s
->coefs_end
[k
] = (s
->frame_len
- ((s
->frame_len
* 9) / 100)) >> k
;
407 /* high freq computation */
408 s
->high_band_start
[k
] = (int)((block_len
* 2 * high_freq
) /
409 s
->sample_rate
+ 0.5);
410 n
= s
->exponent_sizes
[k
];
416 pos
+= s
->exponent_bands
[k
][i
];
418 if (start
< s
->high_band_start
[k
])
419 start
= s
->high_band_start
[k
];
420 if (end
> s
->coefs_end
[k
])
421 end
= s
->coefs_end
[k
];
423 s
->exponent_high_bands
[k
][j
++] = end
- start
;
425 s
->exponent_high_sizes
[k
] = j
;
427 tprintf("%5d: coefs_end=%d high_band_start=%d nb_high_bands=%d: ",
430 s
->high_band_start
[k
],
431 s
->exponent_high_sizes
[k
]);
432 for(j
=0;j
<s
->exponent_high_sizes
[k
];j
++)
433 tprintf(" %d", s
->exponent_high_bands
[k
][j
]);
442 for(i
= 0; i
< s
->nb_block_sizes
; i
++) {
443 tprintf("%5d: n=%2d:",
445 s
->exponent_sizes
[i
]);
446 for(j
=0;j
<s
->exponent_sizes
[i
];j
++)
447 tprintf(" %d", s
->exponent_bands
[i
][j
]);
454 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
455 ff_mdct_init(&s
->mdct_ctx
[i
], s
->frame_len_bits
- i
+ 1, 1);
457 /* init MDCT windows : simple sinus window */
458 for(i
= 0; i
< s
->nb_block_sizes
; i
++) {
461 n
= 1 << (s
->frame_len_bits
- i
);
462 window
= av_malloc(sizeof(float) * n
);
463 alpha
= M_PI
/ (2.0 * n
);
465 window
[n
- j
- 1] = sin((j
+ 0.5) * alpha
);
467 s
->windows
[i
] = window
;
470 s
->reset_block_lengths
= 1;
472 if (s
->use_noise_coding
) {
474 /* init the noise generator */
476 s
->noise_mult
= 0.02;
478 s
->noise_mult
= 0.04;
481 for(i
=0;i
<NOISE_TAB_SIZE
;i
++)
482 s
->noise_table
[i
] = 1.0 * s
->noise_mult
;
488 norm
= (1.0 / (float)(1LL << 31)) * sqrt(3) * s
->noise_mult
;
489 for(i
=0;i
<NOISE_TAB_SIZE
;i
++) {
490 seed
= seed
* 314159 + 1;
491 s
->noise_table
[i
] = (float)((int)seed
) * norm
;
495 init_vlc(&s
->hgain_vlc
, 9, sizeof(hgain_huffbits
),
496 hgain_huffbits
, 1, 1,
497 hgain_huffcodes
, 2, 2, 0);
500 if (s
->use_exp_vlc
) {
501 init_vlc(&s
->exp_vlc
, 9, sizeof(scale_huffbits
),
502 scale_huffbits
, 1, 1,
503 scale_huffcodes
, 4, 4, 0);
505 wma_lsp_to_curve_init(s
, s
->frame_len
);
508 /* choose the VLC tables for the coefficients */
510 if (s
->sample_rate
>= 32000) {
513 else if (bps1
< 1.16)
517 init_coef_vlc(&s
->coef_vlc
[0], &s
->run_table
[0], &s
->level_table
[0],
518 &coef_vlcs
[coef_vlc_table
* 2]);
519 init_coef_vlc(&s
->coef_vlc
[1], &s
->run_table
[1], &s
->level_table
[1],
520 &coef_vlcs
[coef_vlc_table
* 2 + 1]);
524 /* interpolate values for a bigger or smaller block. The block must
525 have multiple sizes */
526 static void interpolate_array(float *scale
, int old_size
, int new_size
)
531 if (new_size
> old_size
) {
532 jincr
= new_size
/ old_size
;
534 for(i
= old_size
- 1; i
>=0; i
--) {
541 } else if (new_size
< old_size
) {
543 jincr
= old_size
/ new_size
;
544 for(i
= 0; i
< new_size
; i
++) {
551 /* compute x^-0.25 with an exponent and mantissa table. We use linear
552 interpolation to reduce the mantissa table size at a small speed
553 expense (linear interpolation approximately doubles the number of
554 bits of precision). */
555 static inline float pow_m1_4(WMADecodeContext
*s
, float x
)
566 m
= (u
.v
>> (23 - LSP_POW_BITS
)) & ((1 << LSP_POW_BITS
) - 1);
567 /* build interpolation scale: 1 <= t < 2. */
568 t
.v
= ((u
.v
<< LSP_POW_BITS
) & ((1 << 23) - 1)) | (127 << 23);
569 a
= s
->lsp_pow_m_table1
[m
];
570 b
= s
->lsp_pow_m_table2
[m
];
571 return s
->lsp_pow_e_table
[e
] * (a
+ b
* t
.f
);
574 static void wma_lsp_to_curve_init(WMADecodeContext
*s
, int frame_len
)
579 wdel
= M_PI
/ frame_len
;
580 for(i
=0;i
<frame_len
;i
++)
581 s
->lsp_cos_table
[i
] = 2.0f
* cos(wdel
* i
);
583 /* tables for x^-0.25 computation */
586 s
->lsp_pow_e_table
[i
] = pow(2.0, e
* -0.25);
589 /* NOTE: these two tables are needed to avoid two operations in
592 for(i
=(1 << LSP_POW_BITS
) - 1;i
>=0;i
--) {
593 m
= (1 << LSP_POW_BITS
) + i
;
594 a
= (float)m
* (0.5 / (1 << LSP_POW_BITS
));
596 s
->lsp_pow_m_table1
[i
] = 2 * a
- b
;
597 s
->lsp_pow_m_table2
[i
] = b
- a
;
606 printf("%f^-0.25=%f e=%f\n", v
, r1
, r2
- r1
);
611 /* NOTE: We use the same code as Vorbis here */
612 /* XXX: optimize it further with SSE/3Dnow */
613 static void wma_lsp_to_curve(WMADecodeContext
*s
,
614 float *out
, float *val_max_ptr
,
618 float p
, q
, w
, v
, val_max
;
624 w
= s
->lsp_cos_table
[i
];
625 for(j
=1;j
<NB_LSP_COEFS
;j
+=2){
637 *val_max_ptr
= val_max
;
640 /* decode exponents coded with LSP coefficients (same idea as Vorbis) */
641 static void decode_exp_lsp(WMADecodeContext
*s
, int ch
)
643 float lsp_coefs
[NB_LSP_COEFS
];
646 for(i
= 0; i
< NB_LSP_COEFS
; i
++) {
647 if (i
== 0 || i
>= 8)
648 val
= get_bits(&s
->gb
, 3);
650 val
= get_bits(&s
->gb
, 4);
651 lsp_coefs
[i
] = lsp_codebook
[i
][val
];
654 wma_lsp_to_curve(s
, s
->exponents
[ch
], &s
->max_exponent
[ch
],
655 s
->block_len
, lsp_coefs
);
658 /* decode exponents coded with VLC codes */
659 static int decode_exp_vlc(WMADecodeContext
*s
, int ch
)
661 int last_exp
, n
, code
;
662 const uint16_t *ptr
, *band_ptr
;
663 float v
, *q
, max_scale
, *q_end
;
665 band_ptr
= s
->exponent_bands
[s
->frame_len_bits
- s
->block_len_bits
];
667 q
= s
->exponents
[ch
];
668 q_end
= q
+ s
->block_len
;
670 if (s
->version
== 1) {
671 last_exp
= get_bits(&s
->gb
, 5) + 10;
672 /* XXX: use a table */
673 v
= pow(10, last_exp
* (1.0 / 16.0));
682 code
= get_vlc(&s
->gb
, &s
->exp_vlc
);
685 /* NOTE: this offset is the same as MPEG4 AAC ! */
686 last_exp
+= code
- 60;
687 /* XXX: use a table */
688 v
= pow(10, last_exp
* (1.0 / 16.0));
696 s
->max_exponent
[ch
] = max_scale
;
700 /* return 0 if OK. return 1 if last block of frame. return -1 if
701 unrecorrable error. */
702 static int wma_decode_block(WMADecodeContext
*s
)
704 int n
, v
, a
, ch
, code
, bsize
;
705 int coef_nb_bits
, total_gain
, parse_exponents
;
706 float window
[BLOCK_MAX_SIZE
* 2];
707 // XXX: FIXME!! there's a bug somewhere which makes this mandatory under altivec
709 volatile int nb_coefs
[MAX_CHANNELS
] __attribute__((aligned(16)));
711 int nb_coefs
[MAX_CHANNELS
];
716 tprintf("***decode_block: %d:%d\n", s
->frame_count
- 1, s
->block_num
);
719 /* compute current block length */
720 if (s
->use_variable_block_len
) {
721 n
= av_log2(s
->nb_block_sizes
- 1) + 1;
723 if (s
->reset_block_lengths
) {
724 s
->reset_block_lengths
= 0;
725 v
= get_bits(&s
->gb
, n
);
726 if (v
>= s
->nb_block_sizes
)
728 s
->prev_block_len_bits
= s
->frame_len_bits
- v
;
729 v
= get_bits(&s
->gb
, n
);
730 if (v
>= s
->nb_block_sizes
)
732 s
->block_len_bits
= s
->frame_len_bits
- v
;
734 /* update block lengths */
735 s
->prev_block_len_bits
= s
->block_len_bits
;
736 s
->block_len_bits
= s
->next_block_len_bits
;
738 v
= get_bits(&s
->gb
, n
);
739 if (v
>= s
->nb_block_sizes
)
741 s
->next_block_len_bits
= s
->frame_len_bits
- v
;
743 /* fixed block len */
744 s
->next_block_len_bits
= s
->frame_len_bits
;
745 s
->prev_block_len_bits
= s
->frame_len_bits
;
746 s
->block_len_bits
= s
->frame_len_bits
;
749 /* now check if the block length is coherent with the frame length */
750 s
->block_len
= 1 << s
->block_len_bits
;
751 if ((s
->block_pos
+ s
->block_len
) > s
->frame_len
)
754 if (s
->nb_channels
== 2) {
755 s
->ms_stereo
= get_bits(&s
->gb
, 1);
758 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
759 a
= get_bits(&s
->gb
, 1);
760 s
->channel_coded
[ch
] = a
;
763 /* if no channel coded, no need to go further */
764 /* XXX: fix potential framing problems */
768 bsize
= s
->frame_len_bits
- s
->block_len_bits
;
770 /* read total gain and extract corresponding number of bits for
771 coef escape coding */
774 a
= get_bits(&s
->gb
, 7);
782 else if (total_gain
< 32)
784 else if (total_gain
< 40)
786 else if (total_gain
< 45)
791 /* compute number of coefficients */
792 n
= s
->coefs_end
[bsize
] - s
->coefs_start
;
793 for(ch
= 0; ch
< s
->nb_channels
; ch
++)
797 if (s
->use_noise_coding
) {
799 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
800 if (s
->channel_coded
[ch
]) {
802 n
= s
->exponent_high_sizes
[bsize
];
804 a
= get_bits(&s
->gb
, 1);
805 s
->high_band_coded
[ch
][i
] = a
;
806 /* if noise coding, the coefficients are not transmitted */
808 nb_coefs
[ch
] -= s
->exponent_high_bands
[bsize
][i
];
812 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
813 if (s
->channel_coded
[ch
]) {
816 n
= s
->exponent_high_sizes
[bsize
];
817 val
= (int)0x80000000;
819 if (s
->high_band_coded
[ch
][i
]) {
820 if (val
== (int)0x80000000) {
821 val
= get_bits(&s
->gb
, 7) - 19;
823 code
= get_vlc(&s
->gb
, &s
->hgain_vlc
);
828 s
->high_band_values
[ch
][i
] = val
;
835 /* exposant can be interpolated in short blocks. */
837 if (s
->block_len_bits
!= s
->frame_len_bits
) {
838 parse_exponents
= get_bits(&s
->gb
, 1);
841 if (parse_exponents
) {
842 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
843 if (s
->channel_coded
[ch
]) {
844 if (s
->use_exp_vlc
) {
845 if (decode_exp_vlc(s
, ch
) < 0)
848 decode_exp_lsp(s
, ch
);
853 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
854 if (s
->channel_coded
[ch
]) {
855 interpolate_array(s
->exponents
[ch
], 1 << s
->prev_block_len_bits
,
861 /* parse spectral coefficients : just RLE encoding */
862 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
863 if (s
->channel_coded
[ch
]) {
865 int level
, run
, sign
, tindex
;
867 const int16_t *level_table
, *run_table
;
869 /* special VLC tables are used for ms stereo because
870 there is potentially less energy there */
871 tindex
= (ch
== 1 && s
->ms_stereo
);
872 coef_vlc
= &s
->coef_vlc
[tindex
];
873 run_table
= s
->run_table
[tindex
];
874 level_table
= s
->level_table
[tindex
];
876 ptr
= &s
->coefs1
[ch
][0];
877 eptr
= ptr
+ nb_coefs
[ch
];
878 memset(ptr
, 0, s
->block_len
* sizeof(int16_t));
880 code
= get_vlc(&s
->gb
, coef_vlc
);
886 } else if (code
== 0) {
888 level
= get_bits(&s
->gb
, coef_nb_bits
);
889 /* NOTE: this is rather suboptimal. reading
890 block_len_bits would be better */
891 run
= get_bits(&s
->gb
, s
->frame_len_bits
);
894 run
= run_table
[code
];
895 level
= level_table
[code
];
897 sign
= get_bits(&s
->gb
, 1);
904 /* NOTE: EOB can be omitted */
909 if (s
->version
== 1 && s
->nb_channels
>= 2) {
910 align_get_bits(&s
->gb
);
916 int n4
= s
->block_len
/ 2;
917 mdct_norm
= 1.0 / (float)n4
;
918 if (s
->version
== 1) {
919 mdct_norm
*= sqrt(n4
);
923 /* finally compute the MDCT coefficients */
924 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
925 if (s
->channel_coded
[ch
]) {
927 float *coefs
, *exponents
, mult
, mult1
, noise
, *exp_ptr
;
928 int i
, j
, n
, n1
, last_high_band
;
929 float exp_power
[HIGH_BAND_MAX_SIZE
];
931 coefs1
= s
->coefs1
[ch
];
932 exponents
= s
->exponents
[ch
];
933 mult
= pow(10, total_gain
* 0.05) / s
->max_exponent
[ch
];
935 coefs
= s
->coefs
[ch
];
936 if (s
->use_noise_coding
) {
938 /* very low freqs : noise */
939 for(i
= 0;i
< s
->coefs_start
; i
++) {
940 *coefs
++ = s
->noise_table
[s
->noise_index
] * (*exponents
++) * mult1
;
941 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
944 n1
= s
->exponent_high_sizes
[bsize
];
946 /* compute power of high bands */
947 exp_ptr
= exponents
+
948 s
->high_band_start
[bsize
] -
950 last_high_band
= 0; /* avoid warning */
952 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
953 s
->block_len_bits
][j
];
954 if (s
->high_band_coded
[ch
][j
]) {
957 for(i
= 0;i
< n
; i
++) {
961 exp_power
[j
] = e2
/ n
;
963 tprintf("%d: power=%f (%d)\n", j
, exp_power
[j
], n
);
968 /* main freqs and high freqs */
971 n
= s
->high_band_start
[bsize
] -
974 n
= s
->exponent_high_bands
[s
->frame_len_bits
-
975 s
->block_len_bits
][j
];
977 if (j
>= 0 && s
->high_band_coded
[ch
][j
]) {
978 /* use noise with specified power */
979 mult1
= sqrt(exp_power
[j
] / exp_power
[last_high_band
]);
980 /* XXX: use a table */
981 mult1
= mult1
* pow(10, s
->high_band_values
[ch
][j
] * 0.05);
982 mult1
= mult1
/ (s
->max_exponent
[ch
] * s
->noise_mult
);
984 for(i
= 0;i
< n
; i
++) {
985 noise
= s
->noise_table
[s
->noise_index
];
986 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
987 *coefs
++ = (*exponents
++) * noise
* mult1
;
990 /* coded values + small noise */
991 for(i
= 0;i
< n
; i
++) {
992 noise
= s
->noise_table
[s
->noise_index
];
993 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
994 *coefs
++ = ((*coefs1
++) + noise
) * (*exponents
++) * mult
;
999 /* very high freqs : noise */
1000 n
= s
->block_len
- s
->coefs_end
[bsize
];
1001 mult1
= mult
* exponents
[-1];
1002 for(i
= 0; i
< n
; i
++) {
1003 *coefs
++ = s
->noise_table
[s
->noise_index
] * mult1
;
1004 s
->noise_index
= (s
->noise_index
+ 1) & (NOISE_TAB_SIZE
- 1);
1007 /* XXX: optimize more */
1008 for(i
= 0;i
< s
->coefs_start
; i
++)
1011 for(i
= 0;i
< n
; i
++) {
1012 *coefs
++ = coefs1
[i
] * exponents
[i
] * mult
;
1014 n
= s
->block_len
- s
->coefs_end
[bsize
];
1015 for(i
= 0;i
< n
; i
++)
1022 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
1023 if (s
->channel_coded
[ch
]) {
1024 dump_floats("exponents", 3, s
->exponents
[ch
], s
->block_len
);
1025 dump_floats("coefs", 1, s
->coefs
[ch
], s
->block_len
);
1030 if (s
->ms_stereo
&& s
->channel_coded
[1]) {
1034 /* nominal case for ms stereo: we do it before mdct */
1035 /* no need to optimize this case because it should almost
1037 if (!s
->channel_coded
[0]) {
1038 tprintf("rare ms-stereo case happened\n");
1039 memset(s
->coefs
[0], 0, sizeof(float) * s
->block_len
);
1040 s
->channel_coded
[0] = 1;
1043 for(i
= 0; i
< s
->block_len
; i
++) {
1046 s
->coefs
[0][i
] = a
+ b
;
1047 s
->coefs
[1][i
] = a
- b
;
1051 /* build the window : we ensure that when the windows overlap
1052 their squared sum is always 1 (MDCT reconstruction rule) */
1053 /* XXX: merge with output */
1055 int i
, next_block_len
, block_len
, prev_block_len
, n
;
1058 block_len
= s
->block_len
;
1059 prev_block_len
= 1 << s
->prev_block_len_bits
;
1060 next_block_len
= 1 << s
->next_block_len_bits
;
1063 wptr
= window
+ block_len
;
1064 if (block_len
<= next_block_len
) {
1065 for(i
=0;i
<block_len
;i
++)
1066 *wptr
++ = s
->windows
[bsize
][i
];
1069 n
= (block_len
/ 2) - (next_block_len
/ 2);
1072 for(i
=0;i
<next_block_len
;i
++)
1073 *wptr
++ = s
->windows
[s
->frame_len_bits
- s
->next_block_len_bits
][i
];
1079 wptr
= window
+ block_len
;
1080 if (block_len
<= prev_block_len
) {
1081 for(i
=0;i
<block_len
;i
++)
1082 *--wptr
= s
->windows
[bsize
][i
];
1085 n
= (block_len
/ 2) - (prev_block_len
/ 2);
1088 for(i
=0;i
<prev_block_len
;i
++)
1089 *--wptr
= s
->windows
[s
->frame_len_bits
- s
->prev_block_len_bits
][i
];
1096 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
1097 if (s
->channel_coded
[ch
]) {
1098 FFTSample output
[BLOCK_MAX_SIZE
* 2] __attribute__((aligned(16)));
1100 int i
, n4
, index
, n
;
1103 n4
= s
->block_len
/ 2;
1104 ff_imdct_calc(&s
->mdct_ctx
[bsize
],
1105 output
, s
->coefs
[ch
], s
->mdct_tmp
);
1107 /* XXX: optimize all that by build the window and
1108 multipying/adding at the same time */
1109 /* multiply by the window */
1110 for(i
=0;i
<n
* 2;i
++) {
1111 output
[i
] *= window
[i
];
1114 /* add in the frame */
1115 index
= (s
->frame_len
/ 2) + s
->block_pos
- n4
;
1116 ptr
= &s
->frame_out
[ch
][index
];
1117 for(i
=0;i
<n
* 2;i
++) {
1122 /* specific fast case for ms-stereo : add to second
1123 channel if it is not coded */
1124 if (s
->ms_stereo
&& !s
->channel_coded
[1]) {
1125 ptr
= &s
->frame_out
[1][index
];
1126 for(i
=0;i
<n
* 2;i
++) {
1134 /* update block number */
1136 s
->block_pos
+= s
->block_len
;
1137 if (s
->block_pos
>= s
->frame_len
)
1143 /* decode a frame of frame_len samples */
1144 static int wma_decode_frame(WMADecodeContext
*s
, int16_t *samples
)
1146 int ret
, i
, n
, a
, ch
, incr
;
1151 tprintf("***decode_frame: %d size=%d\n", s
->frame_count
++, s
->frame_len
);
1154 /* read each block */
1158 ret
= wma_decode_block(s
);
1165 /* convert frame to integer */
1167 incr
= s
->nb_channels
;
1168 for(ch
= 0; ch
< s
->nb_channels
; ch
++) {
1170 iptr
= s
->frame_out
[ch
];
1173 a
= lrintf(*iptr
++);
1176 else if (a
< -32768)
1181 /* prepare for next block */
1182 memmove(&s
->frame_out
[ch
][0], &s
->frame_out
[ch
][s
->frame_len
],
1183 s
->frame_len
* sizeof(float));
1184 /* XXX: suppress this */
1185 memset(&s
->frame_out
[ch
][s
->frame_len
], 0,
1186 s
->frame_len
* sizeof(float));
1190 dump_shorts("samples", samples
, n
* s
->nb_channels
);
1195 static int wma_decode_superframe(AVCodecContext
*avctx
,
1196 void *data
, int *data_size
,
1197 uint8_t *buf
, int buf_size
)
1199 WMADecodeContext
*s
= avctx
->priv_data
;
1200 int nb_frames
, bit_offset
, i
, pos
, len
;
1204 tprintf("***decode_superframe:\n");
1207 s
->last_superframe_len
= 0;
1213 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1215 if (s
->use_bit_reservoir
) {
1216 /* read super frame header */
1217 get_bits(&s
->gb
, 4); /* super frame index */
1218 nb_frames
= get_bits(&s
->gb
, 4) - 1;
1220 bit_offset
= get_bits(&s
->gb
, s
->byte_offset_bits
+ 3);
1222 if (s
->last_superframe_len
> 0) {
1223 // printf("skip=%d\n", s->last_bitoffset);
1224 /* add bit_offset bits to last frame */
1225 if ((s
->last_superframe_len
+ ((bit_offset
+ 7) >> 3)) >
1226 MAX_CODED_SUPERFRAME_SIZE
)
1228 q
= s
->last_superframe
+ s
->last_superframe_len
;
1231 *q
++ = (get_bits
)(&s
->gb
, 8);
1235 *q
++ = (get_bits
)(&s
->gb
, len
) << (8 - len
);
1238 /* XXX: bit_offset bits into last frame */
1239 init_get_bits(&s
->gb
, s
->last_superframe
, MAX_CODED_SUPERFRAME_SIZE
*8);
1240 /* skip unused bits */
1241 if (s
->last_bitoffset
> 0)
1242 skip_bits(&s
->gb
, s
->last_bitoffset
);
1243 /* this frame is stored in the last superframe and in the
1245 if (wma_decode_frame(s
, samples
) < 0)
1247 samples
+= s
->nb_channels
* s
->frame_len
;
1250 /* read each frame starting from bit_offset */
1251 pos
= bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3;
1252 init_get_bits(&s
->gb
, buf
+ (pos
>> 3), (MAX_CODED_SUPERFRAME_SIZE
- (pos
>> 3))*8);
1255 skip_bits(&s
->gb
, len
);
1257 s
->reset_block_lengths
= 1;
1258 for(i
=0;i
<nb_frames
;i
++) {
1259 if (wma_decode_frame(s
, samples
) < 0)
1261 samples
+= s
->nb_channels
* s
->frame_len
;
1264 /* we copy the end of the frame in the last frame buffer */
1265 pos
= get_bits_count(&s
->gb
) + ((bit_offset
+ 4 + 4 + s
->byte_offset_bits
+ 3) & ~7);
1266 s
->last_bitoffset
= pos
& 7;
1268 len
= buf_size
- pos
;
1269 if (len
> MAX_CODED_SUPERFRAME_SIZE
|| len
< 0) {
1272 s
->last_superframe_len
= len
;
1273 memcpy(s
->last_superframe
, buf
+ pos
, len
);
1275 /* single frame decode */
1276 if (wma_decode_frame(s
, samples
) < 0)
1278 samples
+= s
->nb_channels
* s
->frame_len
;
1280 *data_size
= (int8_t *)samples
- (int8_t *)data
;
1281 return s
->block_align
;
1283 /* when error, we reset the bit reservoir */
1284 s
->last_superframe_len
= 0;
1288 static int wma_decode_end(AVCodecContext
*avctx
)
1290 WMADecodeContext
*s
= avctx
->priv_data
;
1293 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
1294 ff_mdct_end(&s
->mdct_ctx
[i
]);
1295 for(i
= 0; i
< s
->nb_block_sizes
; i
++)
1296 av_free(s
->windows
[i
]);
1298 if (s
->use_exp_vlc
) {
1299 free_vlc(&s
->exp_vlc
);
1301 if (s
->use_noise_coding
) {
1302 free_vlc(&s
->hgain_vlc
);
1304 for(i
= 0;i
< 2; i
++) {
1305 free_vlc(&s
->coef_vlc
[i
]);
1306 av_free(s
->run_table
[i
]);
1307 av_free(s
->level_table
[i
]);
1313 AVCodec wmav1_decoder
=
1318 sizeof(WMADecodeContext
),
1322 wma_decode_superframe
,
1325 AVCodec wmav2_decoder
=
1330 sizeof(WMADecodeContext
),
1334 wma_decode_superframe
,