2 * Wmapro compatible decoder
3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4 * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @brief wmapro decoder implementation
26 * Wmapro is an MDCT based codec comparable to wma standard or AAC.
27 * The decoding therefore consists of the following steps:
28 * - bitstream decoding
29 * - reconstruction of per-channel data
30 * - rescaling and inverse quantization
32 * - windowing and overlapp-add
34 * The compressed wmapro bitstream is split into individual packets.
35 * Every such packet contains one or more wma frames.
36 * The compressed frames may have a variable length and frames may
37 * cross packet boundaries.
38 * Common to all wmapro frames is the number of samples that are stored in
40 * The number of samples and a few other decode flags are stored
41 * as extradata that has to be passed to the decoder.
43 * The wmapro frames themselves are again split into a variable number of
44 * subframes. Every subframe contains the data for 2^N time domain samples
45 * where N varies between 7 and 12.
47 * Example wmapro bitstream (in samples):
49 * || packet 0 || packet 1 || packet 2 packets
50 * ---------------------------------------------------
51 * || frame 0 || frame 1 || frame 2 || frames
52 * ---------------------------------------------------
53 * || | | || | | | || || subframes of channel 0
54 * ---------------------------------------------------
55 * || | | || | | | || || subframes of channel 1
56 * ---------------------------------------------------
58 * The frame layouts for the individual channels of a wma frame does not need
61 * However, if the offsets and lengths of several subframes of a frame are the
62 * same, the subframes of the channels can be grouped.
63 * Every group may then use special coding techniques like M/S stereo coding
64 * to improve the compression ratio. These channel transformations do not
65 * need to be applied to a whole subframe. Instead, they can also work on
66 * individual scale factor bands (see below).
67 * The coefficients that carry the audio signal in the frequency domain
68 * are transmitted as huffman-coded vectors with 4, 2 and 1 elements.
69 * In addition to that, the encoder can switch to a runlevel coding scheme
70 * by transmitting subframe_length / 128 zero coefficients.
72 * Before the audio signal can be converted to the time domain, the
73 * coefficients have to be rescaled and inverse quantized.
74 * A subframe is therefore split into several scale factor bands that get
75 * scaled individually.
76 * Scale factors are submitted for every frame but they might be shared
77 * between the subframes of a channel. Scale factors are initially DPCM-coded.
78 * Once scale factors are shared, the differences are transmitted as runlevel
80 * Every subframe length and offset combination in the frame layout shares a
81 * common quantization factor that can be adjusted for every channel by a
83 * After the inverse quantization, the coefficients get processed by an IMDCT.
84 * The resulting values are then windowed with a sine window and the first half
85 * of the values are added to the second half of the output from the previous
86 * subframe in order to reconstruct the output samples.
91 #include "libavutil/float_dsp.h"
92 #include "libavutil/intfloat.h"
93 #include "libavutil/intreadwrite.h"
98 #include "wmaprodata.h"
101 #include "wma_common.h"
103 /** current decoder limitations */
104 #define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
105 #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
106 #define MAX_BANDS 29 ///< max number of scale factor bands
107 #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
109 #define WMAPRO_BLOCK_MIN_BITS 6 ///< log2 of min block size
110 #define WMAPRO_BLOCK_MAX_BITS 13 ///< log2 of max block size
111 #define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS) ///< minimum block size
112 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS) ///< maximum block size
113 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
117 #define SCALEVLCBITS 8
118 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
119 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
120 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
121 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
122 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
124 static VLC sf_vlc; ///< scale factor DPCM vlc
125 static VLC sf_rl_vlc; ///< scale factor run length vlc
126 static VLC vec4_vlc; ///< 4 coefficients per symbol
127 static VLC vec2_vlc; ///< 2 coefficients per symbol
128 static VLC vec1_vlc; ///< 1 coefficient per symbol
129 static VLC coef_vlc[2]; ///< coefficient run length vlc codes
130 static float sin64[33]; ///< sine table for decorrelation
133 * @brief frame specific decoder context for a single channel
135 typedef struct WMAProChannelCtx {
136 int16_t prev_block_len; ///< length of the previous block
137 uint8_t transmit_coefs;
138 uint8_t num_subframes;
139 uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
140 uint16_t subframe_offset[MAX_SUBFRAMES]; ///< subframe positions in the current frame
141 uint8_t cur_subframe; ///< current subframe number
142 uint16_t decoded_samples; ///< number of already processed samples
143 uint8_t grouped; ///< channel is part of a group
144 int quant_step; ///< quantization step for the current subframe
145 int8_t reuse_sf; ///< share scale factors between subframes
146 int8_t scale_factor_step; ///< scaling step for the current subframe
147 int max_scale_factor; ///< maximum scale factor for the current subframe
148 int saved_scale_factors[2][MAX_BANDS]; ///< resampled and (previously) transmitted scale factor values
149 int8_t scale_factor_idx; ///< index for the transmitted scale factor values (used for resampling)
150 int* scale_factors; ///< pointer to the scale factor values used for decoding
151 uint8_t table_idx; ///< index in sf_offsets for the scale factor reference block
152 float* coeffs; ///< pointer to the subframe decode buffer
153 uint16_t num_vec_coeffs; ///< number of vector coded coefficients
154 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; ///< output buffer
158 * @brief channel group for channel transformations
160 typedef struct WMAProChannelGrp {
161 uint8_t num_channels; ///< number of channels in the group
162 int8_t transform; ///< transform on / off
163 int8_t transform_band[MAX_BANDS]; ///< controls if the transform is enabled for a certain band
164 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
165 float* channel_data[WMAPRO_MAX_CHANNELS]; ///< transformation coefficients
169 * @brief main decoder context
171 typedef struct WMAProDecodeCtx {
172 /* generic decoder variables */
173 AVCodecContext* avctx; ///< codec context for av_log
174 AVFloatDSPContext *fdsp;
175 uint8_t frame_data[MAX_FRAMESIZE +
176 AV_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
177 PutBitContext pb; ///< context for filling the frame_data buffer
178 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES]; ///< MDCT context per block size
179 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
180 const float* windows[WMAPRO_BLOCK_SIZES]; ///< windows for the different block sizes
182 /* frame size dependent frame information (set during initialization) */
183 uint32_t decode_flags; ///< used compression features
184 uint8_t len_prefix; ///< frame is prefixed with its length
185 uint8_t dynamic_range_compression; ///< frame contains DRC data
186 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
187 uint16_t samples_per_frame; ///< number of samples to output
188 uint16_t log2_frame_size;
189 int8_t lfe_channel; ///< lfe channel index
190 uint8_t max_num_subframes;
191 uint8_t subframe_len_bits; ///< number of bits used for the subframe length
192 uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
193 uint16_t min_samples_per_subframe;
194 int8_t num_sfb[WMAPRO_BLOCK_SIZES]; ///< scale factor bands per block size
195 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor band offsets (multiples of 4)
196 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
197 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
199 /* packet decode state */
200 GetBitContext pgb; ///< bitstream reader context for the packet
201 int next_packet_start; ///< start offset of the next wma packet in the demuxer packet
202 uint8_t packet_offset; ///< frame offset in the packet
203 uint8_t packet_sequence_number; ///< current packet number
204 int num_saved_bits; ///< saved number of bits
205 int frame_offset; ///< frame offset in the bit reservoir
206 int subframe_offset; ///< subframe offset in the bit reservoir
207 uint8_t packet_loss; ///< set in case of bitstream error
208 uint8_t packet_done; ///< set when a packet is fully decoded
209 uint8_t skip_packets;
211 /* frame decode state */
212 uint32_t frame_num; ///< current frame number (not used for decoding)
214 GetBitContext gb; ///< bitstream reader context
215 int buf_bit_size; ///< buffer size in bits
216 uint8_t drc_gain; ///< gain for the DRC tool
217 int8_t skip_frame; ///< skip output step
218 int8_t parsed_all_subframes; ///< all subframes decoded?
220 /* subframe/block decode state */
221 int16_t subframe_len; ///< current subframe length
222 int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
223 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
224 int8_t num_bands; ///< number of scale factor bands
225 int8_t transmit_num_vec_coeffs; ///< number of vector coded coefficients is part of the bitstream
226 int16_t* cur_sfb_offsets; ///< sfb offsets for the current block
227 uint8_t table_idx; ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
228 int8_t esc_len; ///< length of escaped coefficients
230 uint8_t num_chgroups; ///< number of channel groups
231 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]; ///< channel group information
233 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]; ///< per channel data
238 *@brief helper function to print the most important members of the context
241 static av_cold void dump_context(WMAProDecodeCtx *s)
243 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
244 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %"PRIx32"\n", a, b);
246 PRINT("ed sample bit depth", s->bits_per_sample);
247 PRINT_HEX("ed decode flags", s->decode_flags);
248 PRINT("samples per frame", s->samples_per_frame);
249 PRINT("log2 frame size", s->log2_frame_size);
250 PRINT("max num subframes", s->max_num_subframes);
251 PRINT("len prefix", s->len_prefix);
252 PRINT("num channels", s->avctx->channels);
256 *@brief Uninitialize the decoder and free all resources.
257 *@param avctx codec context
258 *@return 0 on success, < 0 otherwise
260 static av_cold int decode_end(AVCodecContext *avctx)
262 WMAProDecodeCtx *s = avctx->priv_data;
267 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
268 ff_mdct_end(&s->mdct_ctx[i]);
273 static av_cold int get_rate(AVCodecContext *avctx)
275 if (avctx->codec_id != AV_CODEC_ID_WMAPRO) { // XXX: is this really only for XMA?
276 if (avctx->sample_rate > 44100)
278 else if (avctx->sample_rate > 32000)
280 else if (avctx->sample_rate > 24000)
285 return avctx->sample_rate;
289 *@brief Initialize the decoder.
290 *@param avctx codec context
291 *@return 0 on success, -1 otherwise
293 static av_cold int decode_init(AVCodecContext *avctx)
295 WMAProDecodeCtx *s = avctx->priv_data;
296 uint8_t *edata_ptr = avctx->extradata;
297 unsigned int channel_mask;
299 int log2_max_num_subframes;
300 int num_possible_block_sizes;
302 if (avctx->codec_id == AV_CODEC_ID_XMA1 || avctx->codec_id == AV_CODEC_ID_XMA2)
303 avctx->block_align = 2048;
305 if (!avctx->block_align) {
306 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
307 return AVERROR(EINVAL);
311 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
313 return AVERROR(ENOMEM);
315 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
317 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
319 if (avctx->codec_id == AV_CODEC_ID_XMA2 && avctx->extradata_size >= 34) {
320 s->decode_flags = 0x10d6;
321 channel_mask = AV_RL32(edata_ptr+2);
322 s->bits_per_sample = 16;
323 /** dump the extradata */
324 for (i = 0; i < avctx->extradata_size; i++)
325 ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
326 ff_dlog(avctx, "\n");
328 } else if (avctx->codec_id == AV_CODEC_ID_XMA1 && avctx->extradata_size >= 28) {
329 s->decode_flags = 0x10d6;
330 s->bits_per_sample = 16;
332 /** dump the extradata */
333 for (i = 0; i < avctx->extradata_size; i++)
334 ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
335 ff_dlog(avctx, "\n");
337 } else if (avctx->extradata_size >= 18) {
338 s->decode_flags = AV_RL16(edata_ptr+14);
339 channel_mask = AV_RL32(edata_ptr+2);
340 s->bits_per_sample = AV_RL16(edata_ptr);
341 /** dump the extradata */
342 for (i = 0; i < avctx->extradata_size; i++)
343 ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
344 ff_dlog(avctx, "\n");
347 avpriv_request_sample(avctx, "Unknown extradata size");
348 return AVERROR_PATCHWELCOME;
351 if (avctx->codec_id != AV_CODEC_ID_WMAPRO && avctx->channels > 2) {
352 avpriv_report_missing_feature(avctx, ">2 channels support");
353 return AVERROR_PATCHWELCOME;
357 s->log2_frame_size = av_log2(avctx->block_align) + 4;
358 if (s->log2_frame_size > 25) {
359 avpriv_request_sample(avctx, "Large block align");
360 return AVERROR_PATCHWELCOME;
364 if (avctx->codec_id != AV_CODEC_ID_WMAPRO)
367 s->skip_frame = 1; /* skip first frame */
370 s->len_prefix = (s->decode_flags & 0x40);
373 if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
374 bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
375 if (bits > WMAPRO_BLOCK_MAX_BITS) {
376 avpriv_request_sample(avctx, "14-bit block sizes");
377 return AVERROR_PATCHWELCOME;
379 s->samples_per_frame = 1 << bits;
381 s->samples_per_frame = 512;
385 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
386 s->max_num_subframes = 1 << log2_max_num_subframes;
387 if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
388 s->max_subframe_len_bit = 1;
389 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
391 num_possible_block_sizes = log2_max_num_subframes + 1;
392 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
393 s->dynamic_range_compression = (s->decode_flags & 0x80);
395 if (s->max_num_subframes > MAX_SUBFRAMES) {
396 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRId8"\n",
397 s->max_num_subframes);
398 return AVERROR_INVALIDDATA;
401 if (s->min_samples_per_subframe < WMAPRO_BLOCK_MIN_SIZE) {
402 av_log(avctx, AV_LOG_ERROR, "min_samples_per_subframe of %d too small\n",
403 s->min_samples_per_subframe);
404 return AVERROR_INVALIDDATA;
407 if (s->avctx->sample_rate <= 0) {
408 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
409 return AVERROR_INVALIDDATA;
412 if (avctx->channels < 0) {
413 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
415 return AVERROR_INVALIDDATA;
416 } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
417 avpriv_request_sample(avctx,
418 "More than %d channels", WMAPRO_MAX_CHANNELS);
419 return AVERROR_PATCHWELCOME;
422 /** init previous block len */
423 for (i = 0; i < avctx->channels; i++)
424 s->channel[i].prev_block_len = s->samples_per_frame;
426 /** extract lfe channel position */
429 if (channel_mask & 8) {
431 for (mask = 1; mask < 16; mask <<= 1) {
432 if (channel_mask & mask)
437 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
438 scale_huffbits, 1, 1,
439 scale_huffcodes, 2, 2, 616);
441 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
442 scale_rl_huffbits, 1, 1,
443 scale_rl_huffcodes, 4, 4, 1406);
445 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
446 coef0_huffbits, 1, 1,
447 coef0_huffcodes, 4, 4, 2108);
449 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
450 coef1_huffbits, 1, 1,
451 coef1_huffcodes, 4, 4, 3912);
453 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
455 vec4_huffcodes, 2, 2, 604);
457 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
459 vec2_huffcodes, 2, 2, 562);
461 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
463 vec1_huffcodes, 2, 2, 562);
465 /** calculate number of scale factor bands and their offsets
466 for every possible block size */
467 for (i = 0; i < num_possible_block_sizes; i++) {
468 int subframe_len = s->samples_per_frame >> i;
471 int rate = get_rate(avctx);
473 s->sfb_offsets[i][0] = 0;
475 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
476 int offset = (subframe_len * 2 * critical_freq[x]) / rate + 2;
478 if (offset > s->sfb_offsets[i][band - 1])
479 s->sfb_offsets[i][band++] = offset;
481 if (offset >= subframe_len)
484 s->sfb_offsets[i][band - 1] = subframe_len;
485 s->num_sfb[i] = band - 1;
486 if (s->num_sfb[i] <= 0) {
487 av_log(avctx, AV_LOG_ERROR, "num_sfb invalid\n");
488 return AVERROR_INVALIDDATA;
493 /** Scale factors can be shared between blocks of different size
494 as every block has a different scale factor band layout.
495 The matrix sf_offsets is needed to find the correct scale factor.
498 for (i = 0; i < num_possible_block_sizes; i++) {
500 for (b = 0; b < s->num_sfb[i]; b++) {
502 int offset = ((s->sfb_offsets[i][b]
503 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
504 for (x = 0; x < num_possible_block_sizes; x++) {
506 while (s->sfb_offsets[x][v + 1] << x < offset) {
508 av_assert0(v < MAX_BANDS);
510 s->sf_offsets[i][x][b] = v;
515 /** init MDCT, FIXME: only init needed sizes */
516 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
517 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
518 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
519 / (1 << (s->bits_per_sample - 1)));
521 /** init MDCT windows: simple sine window */
522 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
523 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
524 ff_init_ff_sine_windows(win_idx);
525 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
528 /** calculate subwoofer cutoff values */
529 for (i = 0; i < num_possible_block_sizes; i++) {
530 int block_size = s->samples_per_frame >> i;
531 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
532 / s->avctx->sample_rate;
533 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
536 /** calculate sine values for the decorrelation matrix */
537 for (i = 0; i < 33; i++)
538 sin64[i] = sin(i*M_PI / 64.0);
540 if (avctx->debug & FF_DEBUG_BITSTREAM)
543 avctx->channel_layout = channel_mask;
549 *@brief Decode the subframe length.
551 *@param offset sample offset in the frame
552 *@return decoded subframe length on success, < 0 in case of an error
554 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
556 int frame_len_shift = 0;
559 /** no need to read from the bitstream when only one length is possible */
560 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
561 return s->min_samples_per_subframe;
563 if (get_bits_left(&s->gb) < 1)
564 return AVERROR_INVALIDDATA;
566 /** 1 bit indicates if the subframe is of maximum length */
567 if (s->max_subframe_len_bit) {
568 if (get_bits1(&s->gb))
569 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
571 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
573 subframe_len = s->samples_per_frame >> frame_len_shift;
575 /** sanity check the length */
576 if (subframe_len < s->min_samples_per_subframe ||
577 subframe_len > s->samples_per_frame) {
578 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
580 return AVERROR_INVALIDDATA;
586 *@brief Decode how the data in the frame is split into subframes.
587 * Every WMA frame contains the encoded data for a fixed number of
588 * samples per channel. The data for every channel might be split
589 * into several subframes. This function will reconstruct the list of
590 * subframes for every channel.
592 * If the subframes are not evenly split, the algorithm estimates the
593 * channels with the lowest number of total samples.
594 * Afterwards, for each of these channels a bit is read from the
595 * bitstream that indicates if the channel contains a subframe with the
596 * next subframe size that is going to be read from the bitstream or not.
597 * If a channel contains such a subframe, the subframe size gets added to
598 * the channel's subframe list.
599 * The algorithm repeats these steps until the frame is properly divided
600 * between the individual channels.
603 *@return 0 on success, < 0 in case of an error
605 static int decode_tilehdr(WMAProDecodeCtx *s)
607 uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };/**< sum of samples for all currently known subframes of a channel */
608 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS]; /**< flag indicating if a channel contains the current subframe */
609 int channels_for_cur_subframe = s->avctx->channels; /**< number of channels that contain the current subframe */
610 int fixed_channel_layout = 0; /**< flag indicating that all channels use the same subframe offsets and sizes */
611 int min_channel_len = 0; /**< smallest sum of samples (channels with this length will be processed first) */
614 /* Should never consume more than 3073 bits (256 iterations for the
615 * while loop when always the minimum amount of 128 samples is subtracted
616 * from missing samples in the 8 channel case).
617 * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
620 /** reset tiling information */
621 for (c = 0; c < s->avctx->channels; c++)
622 s->channel[c].num_subframes = 0;
624 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
625 fixed_channel_layout = 1;
627 /** loop until the frame data is split between the subframes */
631 /** check which channels contain the subframe */
632 for (c = 0; c < s->avctx->channels; c++) {
633 if (num_samples[c] == min_channel_len) {
634 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
635 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
636 contains_subframe[c] = 1;
638 contains_subframe[c] = get_bits1(&s->gb);
640 contains_subframe[c] = 0;
643 /** get subframe length, subframe_len == 0 is not allowed */
644 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
645 return AVERROR_INVALIDDATA;
647 /** add subframes to the individual channels and find new min_channel_len */
648 min_channel_len += subframe_len;
649 for (c = 0; c < s->avctx->channels; c++) {
650 WMAProChannelCtx* chan = &s->channel[c];
652 if (contains_subframe[c]) {
653 if (chan->num_subframes >= MAX_SUBFRAMES) {
654 av_log(s->avctx, AV_LOG_ERROR,
655 "broken frame: num subframes > 31\n");
656 return AVERROR_INVALIDDATA;
658 chan->subframe_len[chan->num_subframes] = subframe_len;
659 num_samples[c] += subframe_len;
660 ++chan->num_subframes;
661 if (num_samples[c] > s->samples_per_frame) {
662 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
663 "channel len > samples_per_frame\n");
664 return AVERROR_INVALIDDATA;
666 } else if (num_samples[c] <= min_channel_len) {
667 if (num_samples[c] < min_channel_len) {
668 channels_for_cur_subframe = 0;
669 min_channel_len = num_samples[c];
671 ++channels_for_cur_subframe;
674 } while (min_channel_len < s->samples_per_frame);
676 for (c = 0; c < s->avctx->channels; c++) {
679 for (i = 0; i < s->channel[c].num_subframes; i++) {
680 ff_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
681 " len %i\n", s->frame_num, c, i,
682 s->channel[c].subframe_len[i]);
683 s->channel[c].subframe_offset[i] = offset;
684 offset += s->channel[c].subframe_len[i];
692 *@brief Calculate a decorrelation matrix from the bitstream parameters.
693 *@param s codec context
694 *@param chgroup channel group for which the matrix needs to be calculated
696 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
697 WMAProChannelGrp *chgroup)
701 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
702 memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
703 s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
705 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
706 rotation_offset[i] = get_bits(&s->gb, 6);
708 for (i = 0; i < chgroup->num_channels; i++)
709 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
710 get_bits1(&s->gb) ? 1.0 : -1.0;
712 for (i = 1; i < chgroup->num_channels; i++) {
714 for (x = 0; x < i; x++) {
716 for (y = 0; y < i + 1; y++) {
717 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
718 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
719 int n = rotation_offset[offset + x];
725 cosv = sin64[32 - n];
727 sinv = sin64[64 - n];
728 cosv = -sin64[n - 32];
731 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
732 (v1 * sinv) - (v2 * cosv);
733 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
734 (v1 * cosv) + (v2 * sinv);
742 *@brief Decode channel transformation parameters
743 *@param s codec context
744 *@return >= 0 in case of success, < 0 in case of bitstream errors
746 static int decode_channel_transform(WMAProDecodeCtx* s)
749 /* should never consume more than 1921 bits for the 8 channel case
750 * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
751 * + MAX_CHANNELS + MAX_BANDS + 1)
754 /** in the one channel case channel transforms are pointless */
756 if (s->avctx->channels > 1) {
757 int remaining_channels = s->channels_for_cur_subframe;
759 if (get_bits1(&s->gb)) {
760 avpriv_request_sample(s->avctx,
761 "Channel transform bit");
762 return AVERROR_PATCHWELCOME;
765 for (s->num_chgroups = 0; remaining_channels &&
766 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
767 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
768 float** channel_data = chgroup->channel_data;
769 chgroup->num_channels = 0;
770 chgroup->transform = 0;
772 /** decode channel mask */
773 if (remaining_channels > 2) {
774 for (i = 0; i < s->channels_for_cur_subframe; i++) {
775 int channel_idx = s->channel_indexes_for_cur_subframe[i];
776 if (!s->channel[channel_idx].grouped
777 && get_bits1(&s->gb)) {
778 ++chgroup->num_channels;
779 s->channel[channel_idx].grouped = 1;
780 *channel_data++ = s->channel[channel_idx].coeffs;
784 chgroup->num_channels = remaining_channels;
785 for (i = 0; i < s->channels_for_cur_subframe; i++) {
786 int channel_idx = s->channel_indexes_for_cur_subframe[i];
787 if (!s->channel[channel_idx].grouped)
788 *channel_data++ = s->channel[channel_idx].coeffs;
789 s->channel[channel_idx].grouped = 1;
793 /** decode transform type */
794 if (chgroup->num_channels == 2) {
795 if (get_bits1(&s->gb)) {
796 if (get_bits1(&s->gb)) {
797 avpriv_request_sample(s->avctx,
798 "Unknown channel transform type");
799 return AVERROR_PATCHWELCOME;
802 chgroup->transform = 1;
803 if (s->avctx->channels == 2) {
804 chgroup->decorrelation_matrix[0] = 1.0;
805 chgroup->decorrelation_matrix[1] = -1.0;
806 chgroup->decorrelation_matrix[2] = 1.0;
807 chgroup->decorrelation_matrix[3] = 1.0;
810 chgroup->decorrelation_matrix[0] = 0.70703125;
811 chgroup->decorrelation_matrix[1] = -0.70703125;
812 chgroup->decorrelation_matrix[2] = 0.70703125;
813 chgroup->decorrelation_matrix[3] = 0.70703125;
816 } else if (chgroup->num_channels > 2) {
817 if (get_bits1(&s->gb)) {
818 chgroup->transform = 1;
819 if (get_bits1(&s->gb)) {
820 decode_decorrelation_matrix(s, chgroup);
822 /** FIXME: more than 6 coupled channels not supported */
823 if (chgroup->num_channels > 6) {
824 avpriv_request_sample(s->avctx,
825 "Coupled channels > 6");
827 memcpy(chgroup->decorrelation_matrix,
828 default_decorrelation[chgroup->num_channels],
829 chgroup->num_channels * chgroup->num_channels *
830 sizeof(*chgroup->decorrelation_matrix));
836 /** decode transform on / off */
837 if (chgroup->transform) {
838 if (!get_bits1(&s->gb)) {
840 /** transform can be enabled for individual bands */
841 for (i = 0; i < s->num_bands; i++) {
842 chgroup->transform_band[i] = get_bits1(&s->gb);
845 memset(chgroup->transform_band, 1, s->num_bands);
848 remaining_channels -= chgroup->num_channels;
855 *@brief Extract the coefficients from the bitstream.
856 *@param s codec context
857 *@param c current channel number
858 *@return 0 on success, < 0 in case of bitstream errors
860 static int decode_coeffs(WMAProDecodeCtx *s, int c)
862 /* Integers 0..15 as single-precision floats. The table saves a
863 costly int to float conversion, and storing the values as
864 integers allows fast sign-flipping. */
865 static const uint32_t fval_tab[16] = {
866 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
867 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
868 0x41000000, 0x41100000, 0x41200000, 0x41300000,
869 0x41400000, 0x41500000, 0x41600000, 0x41700000,
873 WMAProChannelCtx* ci = &s->channel[c];
880 ff_dlog(s->avctx, "decode coefficients for channel %i\n", c);
882 vlctable = get_bits1(&s->gb);
883 vlc = &coef_vlc[vlctable];
893 /** decode vector coefficients (consumes up to 167 bits per iteration for
894 4 vector coded large values) */
895 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
896 (cur_coeff + 3 < ci->num_vec_coeffs)) {
901 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
903 if (idx == HUFF_VEC4_SIZE - 1) {
904 for (i = 0; i < 4; i += 2) {
905 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
906 if (idx == HUFF_VEC2_SIZE - 1) {
908 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
909 if (v0 == HUFF_VEC1_SIZE - 1)
910 v0 += ff_wma_get_large_val(&s->gb);
911 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
912 if (v1 == HUFF_VEC1_SIZE - 1)
913 v1 += ff_wma_get_large_val(&s->gb);
914 vals[i ] = av_float2int(v0);
915 vals[i+1] = av_float2int(v1);
917 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
918 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
922 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
923 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
924 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
925 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
929 for (i = 0; i < 4; i++) {
931 uint32_t sign = get_bits1(&s->gb) - 1;
932 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
935 ci->coeffs[cur_coeff] = 0;
936 /** switch to run level mode when subframe_len / 128 zeros
937 were found in a row */
938 rl_mode |= (++num_zeros > s->subframe_len >> 8);
944 /** decode run level coded coefficients */
945 if (cur_coeff < s->subframe_len) {
946 memset(&ci->coeffs[cur_coeff], 0,
947 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
948 if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
949 level, run, 1, ci->coeffs,
950 cur_coeff, s->subframe_len,
951 s->subframe_len, s->esc_len, 0))
952 return AVERROR_INVALIDDATA;
959 *@brief Extract scale factors from the bitstream.
960 *@param s codec context
961 *@return 0 on success, < 0 in case of bitstream errors
963 static int decode_scale_factors(WMAProDecodeCtx* s)
967 /** should never consume more than 5344 bits
968 * MAX_CHANNELS * (1 + MAX_BANDS * 23)
971 for (i = 0; i < s->channels_for_cur_subframe; i++) {
972 int c = s->channel_indexes_for_cur_subframe[i];
975 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
976 sf_end = s->channel[c].scale_factors + s->num_bands;
978 /** resample scale factors for the new block size
979 * as the scale factors might need to be resampled several times
980 * before some new values are transmitted, a backup of the last
981 * transmitted scale factors is kept in saved_scale_factors
983 if (s->channel[c].reuse_sf) {
984 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
986 for (b = 0; b < s->num_bands; b++)
987 s->channel[c].scale_factors[b] =
988 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
991 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
993 if (!s->channel[c].reuse_sf) {
995 /** decode DPCM coded scale factors */
996 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
997 val = 45 / s->channel[c].scale_factor_step;
998 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
999 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
1004 /** run level decode differences to the resampled factors */
1005 for (i = 0; i < s->num_bands; i++) {
1011 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
1014 uint32_t code = get_bits(&s->gb, 14);
1016 sign = (code & 1) - 1;
1017 skip = (code & 0x3f) >> 1;
1018 } else if (idx == 1) {
1021 skip = scale_rl_run[idx];
1022 val = scale_rl_level[idx];
1023 sign = get_bits1(&s->gb)-1;
1027 if (i >= s->num_bands) {
1028 av_log(s->avctx, AV_LOG_ERROR,
1029 "invalid scale factor coding\n");
1030 return AVERROR_INVALIDDATA;
1032 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
1036 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
1037 s->channel[c].table_idx = s->table_idx;
1038 s->channel[c].reuse_sf = 1;
1041 /** calculate new scale factor maximum */
1042 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
1043 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
1044 s->channel[c].max_scale_factor =
1045 FFMAX(s->channel[c].max_scale_factor, *sf);
1053 *@brief Reconstruct the individual channel data.
1054 *@param s codec context
1056 static void inverse_channel_transform(WMAProDecodeCtx *s)
1060 for (i = 0; i < s->num_chgroups; i++) {
1061 if (s->chgroup[i].transform) {
1062 float data[WMAPRO_MAX_CHANNELS];
1063 const int num_channels = s->chgroup[i].num_channels;
1064 float** ch_data = s->chgroup[i].channel_data;
1065 float** ch_end = ch_data + num_channels;
1066 const int8_t* tb = s->chgroup[i].transform_band;
1069 /** multichannel decorrelation */
1070 for (sfb = s->cur_sfb_offsets;
1071 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
1074 /** multiply values with the decorrelation_matrix */
1075 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
1076 const float* mat = s->chgroup[i].decorrelation_matrix;
1077 const float* data_end = data + num_channels;
1078 float* data_ptr = data;
1081 for (ch = ch_data; ch < ch_end; ch++)
1082 *data_ptr++ = (*ch)[y];
1084 for (ch = ch_data; ch < ch_end; ch++) {
1087 while (data_ptr < data_end)
1088 sum += *data_ptr++ * *mat++;
1093 } else if (s->avctx->channels == 2) {
1094 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1095 s->fdsp->vector_fmul_scalar(ch_data[0] + sfb[0],
1096 ch_data[0] + sfb[0],
1098 s->fdsp->vector_fmul_scalar(ch_data[1] + sfb[0],
1099 ch_data[1] + sfb[0],
1108 *@brief Apply sine window and reconstruct the output buffer.
1109 *@param s codec context
1111 static void wmapro_window(WMAProDecodeCtx *s)
1114 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1115 int c = s->channel_indexes_for_cur_subframe[i];
1116 const float* window;
1117 int winlen = s->channel[c].prev_block_len;
1118 float* start = s->channel[c].coeffs - (winlen >> 1);
1120 if (s->subframe_len < winlen) {
1121 start += (winlen - s->subframe_len) >> 1;
1122 winlen = s->subframe_len;
1125 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
1129 s->fdsp->vector_fmul_window(start, start, start + winlen,
1132 s->channel[c].prev_block_len = s->subframe_len;
1137 *@brief Decode a single subframe (block).
1138 *@param s codec context
1139 *@return 0 on success, < 0 when decoding failed
1141 static int decode_subframe(WMAProDecodeCtx *s)
1143 int offset = s->samples_per_frame;
1144 int subframe_len = s->samples_per_frame;
1146 int total_samples = s->samples_per_frame * s->avctx->channels;
1147 int transmit_coeffs = 0;
1148 int cur_subwoofer_cutoff;
1150 s->subframe_offset = get_bits_count(&s->gb);
1152 /** reset channel context and find the next block offset and size
1153 == the next block of the channel with the smallest number of
1156 for (i = 0; i < s->avctx->channels; i++) {
1157 s->channel[i].grouped = 0;
1158 if (offset > s->channel[i].decoded_samples) {
1159 offset = s->channel[i].decoded_samples;
1161 s->channel[i].subframe_len[s->channel[i].cur_subframe];
1166 "processing subframe with offset %i len %i\n", offset, subframe_len);
1168 /** get a list of all channels that contain the estimated block */
1169 s->channels_for_cur_subframe = 0;
1170 for (i = 0; i < s->avctx->channels; i++) {
1171 const int cur_subframe = s->channel[i].cur_subframe;
1172 /** subtract already processed samples */
1173 total_samples -= s->channel[i].decoded_samples;
1175 /** and count if there are multiple subframes that match our profile */
1176 if (offset == s->channel[i].decoded_samples &&
1177 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1178 total_samples -= s->channel[i].subframe_len[cur_subframe];
1179 s->channel[i].decoded_samples +=
1180 s->channel[i].subframe_len[cur_subframe];
1181 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
1182 ++s->channels_for_cur_subframe;
1186 /** check if the frame will be complete after processing the
1189 s->parsed_all_subframes = 1;
1192 ff_dlog(s->avctx, "subframe is part of %i channels\n",
1193 s->channels_for_cur_subframe);
1195 /** calculate number of scale factor bands and their offsets */
1196 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
1197 s->num_bands = s->num_sfb[s->table_idx];
1198 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
1199 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
1201 /** configure the decoder for the current subframe */
1202 offset += s->samples_per_frame >> 1;
1204 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1205 int c = s->channel_indexes_for_cur_subframe[i];
1207 s->channel[c].coeffs = &s->channel[c].out[offset];
1210 s->subframe_len = subframe_len;
1211 s->esc_len = av_log2(s->subframe_len - 1) + 1;
1213 /** skip extended header if any */
1214 if (get_bits1(&s->gb)) {
1216 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
1217 int len = get_bits(&s->gb, 4);
1218 num_fill_bits = (len ? get_bits(&s->gb, len) : 0) + 1;
1221 if (num_fill_bits >= 0) {
1222 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
1223 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
1224 return AVERROR_INVALIDDATA;
1227 skip_bits_long(&s->gb, num_fill_bits);
1231 /** no idea for what the following bit is used */
1232 if (get_bits1(&s->gb)) {
1233 avpriv_request_sample(s->avctx, "Reserved bit");
1234 return AVERROR_PATCHWELCOME;
1238 if (decode_channel_transform(s) < 0)
1239 return AVERROR_INVALIDDATA;
1242 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1243 int c = s->channel_indexes_for_cur_subframe[i];
1244 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
1245 transmit_coeffs = 1;
1248 av_assert0(s->subframe_len <= WMAPRO_BLOCK_MAX_SIZE);
1249 if (transmit_coeffs) {
1251 int quant_step = 90 * s->bits_per_sample >> 4;
1253 /** decode number of vector coded coefficients */
1254 if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
1255 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
1256 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1257 int c = s->channel_indexes_for_cur_subframe[i];
1258 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
1259 if (num_vec_coeffs > s->subframe_len) {
1260 av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
1261 return AVERROR_INVALIDDATA;
1263 av_assert0(num_vec_coeffs + offset <= FF_ARRAY_ELEMS(s->channel[c].out));
1264 s->channel[c].num_vec_coeffs = num_vec_coeffs;
1267 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1268 int c = s->channel_indexes_for_cur_subframe[i];
1269 s->channel[c].num_vec_coeffs = s->subframe_len;
1272 /** decode quantization step */
1273 step = get_sbits(&s->gb, 6);
1275 if (step == -32 || step == 31) {
1276 const int sign = (step == 31) - 1;
1278 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
1279 (step = get_bits(&s->gb, 5)) == 31) {
1282 quant_step += ((quant + step) ^ sign) - sign;
1284 if (quant_step < 0) {
1285 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
1288 /** decode quantization step modifiers for every channel */
1290 if (s->channels_for_cur_subframe == 1) {
1291 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1293 int modifier_len = get_bits(&s->gb, 3);
1294 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1295 int c = s->channel_indexes_for_cur_subframe[i];
1296 s->channel[c].quant_step = quant_step;
1297 if (get_bits1(&s->gb)) {
1299 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
1301 ++s->channel[c].quant_step;
1306 /** decode scale factors */
1307 if (decode_scale_factors(s) < 0)
1308 return AVERROR_INVALIDDATA;
1311 ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
1312 get_bits_count(&s->gb) - s->subframe_offset);
1314 /** parse coefficients */
1315 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1316 int c = s->channel_indexes_for_cur_subframe[i];
1317 if (s->channel[c].transmit_coefs &&
1318 get_bits_count(&s->gb) < s->num_saved_bits) {
1319 decode_coeffs(s, c);
1321 memset(s->channel[c].coeffs, 0,
1322 sizeof(*s->channel[c].coeffs) * subframe_len);
1325 ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
1326 get_bits_count(&s->gb) - s->subframe_offset);
1328 if (transmit_coeffs) {
1329 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
1330 /** reconstruct the per channel data */
1331 inverse_channel_transform(s);
1332 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1333 int c = s->channel_indexes_for_cur_subframe[i];
1334 const int* sf = s->channel[c].scale_factors;
1337 if (c == s->lfe_channel)
1338 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
1339 (subframe_len - cur_subwoofer_cutoff));
1341 /** inverse quantization and rescaling */
1342 for (b = 0; b < s->num_bands; b++) {
1343 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
1344 const int exp = s->channel[c].quant_step -
1345 (s->channel[c].max_scale_factor - *sf++) *
1346 s->channel[c].scale_factor_step;
1347 const float quant = pow(10.0, exp / 20.0);
1348 int start = s->cur_sfb_offsets[b];
1349 s->fdsp->vector_fmul_scalar(s->tmp + start,
1350 s->channel[c].coeffs + start,
1351 quant, end - start);
1354 /** apply imdct (imdct_half == DCTIV with reverse) */
1355 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
1359 /** window and overlapp-add */
1362 /** handled one subframe */
1363 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1364 int c = s->channel_indexes_for_cur_subframe[i];
1365 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1366 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1367 return AVERROR_INVALIDDATA;
1369 ++s->channel[c].cur_subframe;
1376 *@brief Decode one WMA frame.
1377 *@param s codec context
1378 *@return 0 if the trailer bit indicates that this is the last frame,
1379 * 1 if there are additional frames
1381 static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
1383 AVCodecContext *avctx = s->avctx;
1384 GetBitContext* gb = &s->gb;
1385 int more_frames = 0;
1389 /** get frame length */
1391 len = get_bits(gb, s->log2_frame_size);
1393 ff_dlog(s->avctx, "decoding frame with length %x\n", len);
1395 /** decode tile information */
1396 if (decode_tilehdr(s)) {
1401 /** read postproc transform */
1402 if (s->avctx->channels > 1 && get_bits1(gb)) {
1403 if (get_bits1(gb)) {
1404 for (i = 0; i < avctx->channels * avctx->channels; i++)
1409 /** read drc info */
1410 if (s->dynamic_range_compression) {
1411 s->drc_gain = get_bits(gb, 8);
1412 ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
1415 /** no idea what these are for, might be the number of samples
1416 that need to be skipped at the beginning or end of a stream */
1417 if (get_bits1(gb)) {
1420 /** usually true for the first frame */
1421 if (get_bits1(gb)) {
1422 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1423 ff_dlog(s->avctx, "start skip: %i\n", skip);
1426 /** sometimes true for the last frame */
1427 if (get_bits1(gb)) {
1428 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1429 ff_dlog(s->avctx, "end skip: %i\n", skip);
1434 ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
1435 get_bits_count(gb) - s->frame_offset);
1437 /** reset subframe states */
1438 s->parsed_all_subframes = 0;
1439 for (i = 0; i < avctx->channels; i++) {
1440 s->channel[i].decoded_samples = 0;
1441 s->channel[i].cur_subframe = 0;
1442 s->channel[i].reuse_sf = 0;
1445 /** decode all subframes */
1446 while (!s->parsed_all_subframes) {
1447 if (decode_subframe(s) < 0) {
1453 /* get output buffer */
1454 frame->nb_samples = s->samples_per_frame;
1455 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1460 /** copy samples to the output buffer */
1461 for (i = 0; i < avctx->channels; i++)
1462 memcpy(frame->extended_data[i], s->channel[i].out,
1463 s->samples_per_frame * sizeof(*s->channel[i].out));
1465 for (i = 0; i < avctx->channels; i++) {
1466 /** reuse second half of the IMDCT output for the next frame */
1467 memcpy(&s->channel[i].out[0],
1468 &s->channel[i].out[s->samples_per_frame],
1469 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
1472 if (s->skip_frame) {
1475 av_frame_unref(frame);
1480 if (s->len_prefix) {
1481 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1482 /** FIXME: not sure if this is always an error */
1483 av_log(s->avctx, AV_LOG_ERROR,
1484 "frame[%"PRIu32"] would have to skip %i bits\n",
1486 len - (get_bits_count(gb) - s->frame_offset) - 1);
1491 /** skip the rest of the frame data */
1492 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1494 while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
1498 /** decode trailer bit */
1499 more_frames = get_bits1(gb);
1506 *@brief Calculate remaining input buffer length.
1507 *@param s codec context
1508 *@param gb bitstream reader context
1509 *@return remaining size in bits
1511 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
1513 return s->buf_bit_size - get_bits_count(gb);
1517 *@brief Fill the bit reservoir with a (partial) frame.
1518 *@param s codec context
1519 *@param gb bitstream reader context
1520 *@param len length of the partial frame
1521 *@param append decides whether to reset the buffer or not
1523 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1528 /** when the frame data does not need to be concatenated, the input buffer
1529 is reset and additional bits from the previous frame are copied
1530 and skipped later so that a fast byte copy is possible */
1533 s->frame_offset = get_bits_count(gb) & 7;
1534 s->num_saved_bits = s->frame_offset;
1535 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1538 buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
1540 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1541 avpriv_request_sample(s->avctx, "Too small input buffer");
1546 av_assert0(len <= put_bits_left(&s->pb));
1548 s->num_saved_bits += len;
1550 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1553 int align = 8 - (get_bits_count(gb) & 7);
1554 align = FFMIN(align, len);
1555 put_bits(&s->pb, align, get_bits(gb, align));
1557 avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1559 skip_bits_long(gb, len);
1562 PutBitContext tmp = s->pb;
1563 flush_put_bits(&tmp);
1566 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1567 skip_bits(&s->gb, s->frame_offset);
1571 *@brief Decode a single WMA packet.
1572 *@param avctx codec context
1573 *@param data the output buffer
1574 *@param avpkt input packet
1575 *@return number of bytes that were read from the input buffer
1577 static int decode_packet(AVCodecContext *avctx, void *data,
1578 int *got_frame_ptr, AVPacket* avpkt)
1580 WMAProDecodeCtx *s = avctx->priv_data;
1581 GetBitContext* gb = &s->pgb;
1582 const uint8_t* buf = avpkt->data;
1583 int buf_size = avpkt->size;
1584 int num_bits_prev_frame;
1585 int packet_sequence_number;
1589 if (s->skip_packets > 0) {
1591 return FFMIN(avpkt->size, avctx->block_align);
1594 if (s->packet_done || s->packet_loss) {
1597 /** sanity check for the buffer length */
1598 if (avctx->codec_id == AV_CODEC_ID_WMAPRO && buf_size < avctx->block_align) {
1599 av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
1600 buf_size, avctx->block_align);
1601 return AVERROR_INVALIDDATA;
1604 if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
1605 s->next_packet_start = buf_size - avctx->block_align;
1606 buf_size = avctx->block_align;
1608 s->next_packet_start = buf_size - FFMIN(buf_size, avctx->block_align);
1609 buf_size = FFMIN(buf_size, avctx->block_align);
1611 s->buf_bit_size = buf_size << 3;
1613 /** parse packet header */
1614 init_get_bits(gb, buf, s->buf_bit_size);
1615 if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
1616 packet_sequence_number = get_bits(gb, 4);
1619 s->num_frames = get_bits(gb, 6);
1620 packet_sequence_number = 0;
1623 /** get number of bits that need to be added to the previous frame */
1624 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1625 if (avctx->codec_id != AV_CODEC_ID_WMAPRO) {
1627 s->skip_packets = get_bits(gb, 8);
1630 ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
1631 num_bits_prev_frame);
1633 /** check for packet loss */
1634 if (avctx->codec_id == AV_CODEC_ID_WMAPRO && !s->packet_loss &&
1635 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1637 av_log(avctx, AV_LOG_ERROR,
1638 "Packet loss detected! seq %"PRIx8" vs %x\n",
1639 s->packet_sequence_number, packet_sequence_number);
1641 s->packet_sequence_number = packet_sequence_number;
1643 if (num_bits_prev_frame > 0) {
1644 int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1645 if (num_bits_prev_frame >= remaining_packet_bits) {
1646 num_bits_prev_frame = remaining_packet_bits;
1650 /** append the previous frame data to the remaining data from the
1651 previous packet to create a full frame */
1652 save_bits(s, gb, num_bits_prev_frame, 1);
1653 ff_dlog(avctx, "accumulated %x bits of frame data\n",
1654 s->num_saved_bits - s->frame_offset);
1656 /** decode the cross packet frame if it is valid */
1657 if (!s->packet_loss)
1658 decode_frame(s, data, got_frame_ptr);
1659 } else if (s->num_saved_bits - s->frame_offset) {
1660 ff_dlog(avctx, "ignoring %x previously saved bits\n",
1661 s->num_saved_bits - s->frame_offset);
1664 if (s->packet_loss) {
1665 /** reset number of saved bits so that the decoder
1666 does not start to decode incomplete frames in the
1667 s->len_prefix == 0 case */
1668 s->num_saved_bits = 0;
1674 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1675 init_get_bits(gb, avpkt->data, s->buf_bit_size);
1676 skip_bits(gb, s->packet_offset);
1677 if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1678 (frame_size = show_bits(gb, s->log2_frame_size)) &&
1679 frame_size <= remaining_bits(s, gb)) {
1680 save_bits(s, gb, frame_size, 0);
1681 if (!s->packet_loss)
1682 s->packet_done = !decode_frame(s, data, got_frame_ptr);
1683 } else if (!s->len_prefix
1684 && s->num_saved_bits > get_bits_count(&s->gb)) {
1685 /** when the frames do not have a length prefix, we don't know
1686 the compressed length of the individual frames
1687 however, we know what part of a new packet belongs to the
1689 therefore we save the incoming packet first, then we append
1690 the "previous frame" data from the next packet so that
1691 we get a buffer that only contains full frames */
1692 s->packet_done = !decode_frame(s, data, got_frame_ptr);
1697 if (s->packet_done && !s->packet_loss &&
1698 remaining_bits(s, gb) > 0) {
1699 /** save the rest of the data so that it can be decoded
1700 with the next packet */
1701 save_bits(s, gb, remaining_bits(s, gb), 0);
1704 s->packet_offset = get_bits_count(gb) & 7;
1706 return AVERROR_INVALIDDATA;
1708 return get_bits_count(gb) >> 3;
1712 *@brief Clear decoder buffers (for seeking).
1713 *@param avctx codec context
1715 static void flush(AVCodecContext *avctx)
1717 WMAProDecodeCtx *s = avctx->priv_data;
1719 /** reset output buffer as a part of it is used during the windowing of a
1721 for (i = 0; i < avctx->channels; i++)
1722 memset(s->channel[i].out, 0, s->samples_per_frame *
1723 sizeof(*s->channel[i].out));
1729 *@brief wmapro decoder
1731 AVCodec ff_wmapro_decoder = {
1733 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
1734 .type = AVMEDIA_TYPE_AUDIO,
1735 .id = AV_CODEC_ID_WMAPRO,
1736 .priv_data_size = sizeof(WMAProDecodeCtx),
1737 .init = decode_init,
1738 .close = decode_end,
1739 .decode = decode_packet,
1740 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1742 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1743 AV_SAMPLE_FMT_NONE },
1746 AVCodec ff_xma1_decoder = {
1748 .long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 1"),
1749 .type = AVMEDIA_TYPE_AUDIO,
1750 .id = AV_CODEC_ID_XMA1,
1751 .priv_data_size = sizeof(WMAProDecodeCtx),
1752 .init = decode_init,
1753 .close = decode_end,
1754 .decode = decode_packet,
1755 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1757 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1758 AV_SAMPLE_FMT_NONE },
1761 AVCodec ff_xma2_decoder = {
1763 .long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 2"),
1764 .type = AVMEDIA_TYPE_AUDIO,
1765 .id = AV_CODEC_ID_XMA2,
1766 .priv_data_size = sizeof(WMAProDecodeCtx),
1767 .init = decode_init,
1768 .close = decode_end,
1769 .decode = decode_packet,
1770 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1772 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1773 AV_SAMPLE_FMT_NONE },