aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / wmaprodec.c
blob4eaeed610ee241c634f3cf6dcab9daa020fd598d
1 /*
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 Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /**
24 * @file
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
31 * - IMDCT
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
39 * a frame.
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
59 * to be the same.
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
79 * codes.
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
82 * modifier.
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.
89 #include <inttypes.h>
91 #include "libavutil/float_dsp.h"
92 #include "libavutil/intfloat.h"
93 #include "libavutil/intreadwrite.h"
95 #include "avcodec.h"
96 #include "bitstream.h"
97 #include "internal.h"
98 #include "put_bits.h"
99 #include "wmaprodata.h"
100 #include "sinewin.h"
101 #include "wma.h"
102 #include "wma_common.h"
104 /** current decoder limitations */
105 #define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
106 #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
107 #define MAX_BANDS 29 ///< max number of scale factor bands
108 #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
110 #define WMAPRO_BLOCK_MIN_BITS 6 ///< log2 of min block size
111 #define WMAPRO_BLOCK_MAX_BITS 13 ///< log2 of max block size
112 #define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS) ///< minimum block size
113 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS) ///< maximum block size
114 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
117 #define VLCBITS 9
118 #define SCALEVLCBITS 8
119 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
120 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
121 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
122 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
123 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
125 static VLC sf_vlc; ///< scale factor DPCM vlc
126 static VLC sf_rl_vlc; ///< scale factor run length vlc
127 static VLC vec4_vlc; ///< 4 coefficients per symbol
128 static VLC vec2_vlc; ///< 2 coefficients per symbol
129 static VLC vec1_vlc; ///< 1 coefficient per symbol
130 static VLC coef_vlc[2]; ///< coefficient run length vlc codes
131 static float sin64[33]; ///< sine table for decorrelation
134 * @brief frame specific decoder context for a single channel
136 typedef struct WMAProChannelCtx {
137 int16_t prev_block_len; ///< length of the previous block
138 uint8_t transmit_coefs;
139 uint8_t num_subframes;
140 uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
141 uint16_t subframe_offset[MAX_SUBFRAMES]; ///< subframe positions in the current frame
142 uint8_t cur_subframe; ///< current subframe number
143 uint16_t decoded_samples; ///< number of already processed samples
144 uint8_t grouped; ///< channel is part of a group
145 int quant_step; ///< quantization step for the current subframe
146 int8_t reuse_sf; ///< share scale factors between subframes
147 int8_t scale_factor_step; ///< scaling step for the current subframe
148 int max_scale_factor; ///< maximum scale factor for the current subframe
149 int saved_scale_factors[2][MAX_BANDS]; ///< resampled and (previously) transmitted scale factor values
150 int8_t scale_factor_idx; ///< index for the transmitted scale factor values (used for resampling)
151 int* scale_factors; ///< pointer to the scale factor values used for decoding
152 uint8_t table_idx; ///< index in sf_offsets for the scale factor reference block
153 float* coeffs; ///< pointer to the subframe decode buffer
154 uint16_t num_vec_coeffs; ///< number of vector coded coefficients
155 DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; ///< output buffer
156 } WMAProChannelCtx;
159 * @brief channel group for channel transformations
161 typedef struct WMAProChannelGrp {
162 uint8_t num_channels; ///< number of channels in the group
163 int8_t transform; ///< transform on / off
164 int8_t transform_band[MAX_BANDS]; ///< controls if the transform is enabled for a certain band
165 float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
166 float* channel_data[WMAPRO_MAX_CHANNELS]; ///< transformation coefficients
167 } WMAProChannelGrp;
170 * @brief main decoder context
172 typedef struct WMAProDecodeCtx {
173 /* generic decoder variables */
174 AVCodecContext* avctx; ///< codec context for av_log
175 AVFloatDSPContext fdsp;
176 uint8_t frame_data[MAX_FRAMESIZE +
177 AV_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
178 PutBitContext pb; ///< context for filling the frame_data buffer
179 FFTContext mdct_ctx[WMAPRO_BLOCK_SIZES]; ///< MDCT context per block size
180 DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
181 float* windows[WMAPRO_BLOCK_SIZES]; ///< windows for the different block sizes
183 /* frame size dependent frame information (set during initialization) */
184 uint32_t decode_flags; ///< used compression features
185 uint8_t len_prefix; ///< frame is prefixed with its length
186 uint8_t dynamic_range_compression; ///< frame contains DRC data
187 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
188 uint16_t samples_per_frame; ///< number of samples to output
189 uint16_t log2_frame_size;
190 int8_t lfe_channel; ///< lfe channel index
191 uint8_t max_num_subframes;
192 uint8_t subframe_len_bits; ///< number of bits used for the subframe length
193 uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
194 uint16_t min_samples_per_subframe;
195 int8_t num_sfb[WMAPRO_BLOCK_SIZES]; ///< scale factor bands per block size
196 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor band offsets (multiples of 4)
197 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
198 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
200 /* packet decode state */
201 BitstreamContext pbc; ///< bitstream reader context for the packet
202 int next_packet_start; ///< start offset of the next wma packet in the demuxer packet
203 uint8_t packet_offset; ///< frame offset in the packet
204 uint8_t packet_sequence_number; ///< current packet number
205 int num_saved_bits; ///< saved number of bits
206 int frame_offset; ///< frame offset in the bit reservoir
207 int subframe_offset; ///< subframe offset in the bit reservoir
208 uint8_t packet_loss; ///< set in case of bitstream error
209 uint8_t packet_done; ///< set when a packet is fully decoded
211 /* frame decode state */
212 uint32_t frame_num; ///< current frame number (not used for decoding)
213 BitstreamContext bc; ///< bitstream reader context
214 int buf_bit_size; ///< buffer size in bits
215 uint8_t drc_gain; ///< gain for the DRC tool
216 int8_t skip_frame; ///< skip output step
217 int8_t parsed_all_subframes; ///< all subframes decoded?
219 /* subframe/block decode state */
220 int16_t subframe_len; ///< current subframe length
221 int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
222 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
223 int8_t num_bands; ///< number of scale factor bands
224 int8_t transmit_num_vec_coeffs; ///< number of vector coded coefficients is part of the bitstream
225 int16_t* cur_sfb_offsets; ///< sfb offsets for the current block
226 uint8_t table_idx; ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
227 int8_t esc_len; ///< length of escaped coefficients
229 uint8_t num_chgroups; ///< number of channel groups
230 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]; ///< channel group information
232 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]; ///< per channel data
233 } WMAProDecodeCtx;
237 *@brief helper function to print the most important members of the context
238 *@param s context
240 static av_cold void dump_context(WMAProDecodeCtx *s)
242 #define PRINT(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
243 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %"PRIx32"\n", a, b);
245 PRINT("ed sample bit depth", s->bits_per_sample);
246 PRINT_HEX("ed decode flags", s->decode_flags);
247 PRINT("samples per frame", s->samples_per_frame);
248 PRINT("log2 frame size", s->log2_frame_size);
249 PRINT("max num subframes", s->max_num_subframes);
250 PRINT("len prefix", s->len_prefix);
251 PRINT("num channels", s->avctx->channels);
255 *@brief Uninitialize the decoder and free all resources.
256 *@param avctx codec context
257 *@return 0 on success, < 0 otherwise
259 static av_cold int decode_end(AVCodecContext *avctx)
261 WMAProDecodeCtx *s = avctx->priv_data;
262 int i;
264 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
265 ff_mdct_end(&s->mdct_ctx[i]);
267 return 0;
271 *@brief Initialize the decoder.
272 *@param avctx codec context
273 *@return 0 on success, -1 otherwise
275 static av_cold int decode_init(AVCodecContext *avctx)
277 WMAProDecodeCtx *s = avctx->priv_data;
278 uint8_t *edata_ptr = avctx->extradata;
279 unsigned int channel_mask;
280 int i, bits;
281 int log2_max_num_subframes;
282 int num_possible_block_sizes;
284 if (!avctx->block_align) {
285 av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
286 return AVERROR(EINVAL);
289 s->avctx = avctx;
290 avpriv_float_dsp_init(&s->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
292 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
294 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
296 if (avctx->extradata_size >= 18) {
297 s->decode_flags = AV_RL16(edata_ptr+14);
298 channel_mask = AV_RL32(edata_ptr+2);
299 s->bits_per_sample = AV_RL16(edata_ptr);
300 /** dump the extradata */
301 for (i = 0; i < avctx->extradata_size; i++)
302 ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
303 ff_dlog(avctx, "\n");
305 } else {
306 avpriv_request_sample(avctx, "Unknown extradata size");
307 return AVERROR_PATCHWELCOME;
310 /** generic init */
311 s->log2_frame_size = av_log2(avctx->block_align) + 4;
313 /** frame info */
314 s->skip_frame = 1; /* skip first frame */
315 s->packet_loss = 1;
316 s->len_prefix = (s->decode_flags & 0x40);
318 /** get frame len */
319 bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
320 if (bits > WMAPRO_BLOCK_MAX_BITS) {
321 avpriv_request_sample(avctx, "14-bit block sizes");
322 return AVERROR_PATCHWELCOME;
324 s->samples_per_frame = 1 << bits;
326 /** subframe info */
327 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
328 s->max_num_subframes = 1 << log2_max_num_subframes;
329 if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
330 s->max_subframe_len_bit = 1;
331 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
333 num_possible_block_sizes = log2_max_num_subframes + 1;
334 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
335 s->dynamic_range_compression = (s->decode_flags & 0x80);
337 if (s->max_num_subframes > MAX_SUBFRAMES) {
338 av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRId8"\n",
339 s->max_num_subframes);
340 return AVERROR_INVALIDDATA;
343 if (s->min_samples_per_subframe < WMAPRO_BLOCK_MIN_SIZE) {
344 av_log(avctx, AV_LOG_ERROR, "Invalid minimum block size %"PRId8"\n",
345 s->max_num_subframes);
346 return AVERROR_INVALIDDATA;
349 if (s->avctx->sample_rate <= 0) {
350 av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
351 return AVERROR_INVALIDDATA;
354 if (avctx->channels < 0) {
355 av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
356 avctx->channels);
357 return AVERROR_INVALIDDATA;
358 } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
359 avpriv_request_sample(avctx,
360 "More than %d channels", WMAPRO_MAX_CHANNELS);
361 return AVERROR_PATCHWELCOME;
364 /** init previous block len */
365 for (i = 0; i < avctx->channels; i++)
366 s->channel[i].prev_block_len = s->samples_per_frame;
368 /** extract lfe channel position */
369 s->lfe_channel = -1;
371 if (channel_mask & 8) {
372 unsigned int mask;
373 for (mask = 1; mask < 16; mask <<= 1) {
374 if (channel_mask & mask)
375 ++s->lfe_channel;
379 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
380 scale_huffbits, 1, 1,
381 scale_huffcodes, 2, 2, 616);
383 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
384 scale_rl_huffbits, 1, 1,
385 scale_rl_huffcodes, 4, 4, 1406);
387 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
388 coef0_huffbits, 1, 1,
389 coef0_huffcodes, 4, 4, 2108);
391 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
392 coef1_huffbits, 1, 1,
393 coef1_huffcodes, 4, 4, 3912);
395 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
396 vec4_huffbits, 1, 1,
397 vec4_huffcodes, 2, 2, 604);
399 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
400 vec2_huffbits, 1, 1,
401 vec2_huffcodes, 2, 2, 562);
403 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
404 vec1_huffbits, 1, 1,
405 vec1_huffcodes, 2, 2, 562);
407 /** calculate number of scale factor bands and their offsets
408 for every possible block size */
409 for (i = 0; i < num_possible_block_sizes; i++) {
410 int subframe_len = s->samples_per_frame >> i;
411 int x;
412 int band = 1;
414 s->sfb_offsets[i][0] = 0;
416 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
417 int offset = (subframe_len * 2 * critical_freq[x])
418 / s->avctx->sample_rate + 2;
419 offset &= ~3;
420 if (offset > s->sfb_offsets[i][band - 1])
421 s->sfb_offsets[i][band++] = offset;
423 s->sfb_offsets[i][band - 1] = subframe_len;
424 s->num_sfb[i] = band - 1;
428 /** Scale factors can be shared between blocks of different size
429 as every block has a different scale factor band layout.
430 The matrix sf_offsets is needed to find the correct scale factor.
433 for (i = 0; i < num_possible_block_sizes; i++) {
434 int b;
435 for (b = 0; b < s->num_sfb[i]; b++) {
436 int x;
437 int offset = ((s->sfb_offsets[i][b]
438 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
439 for (x = 0; x < num_possible_block_sizes; x++) {
440 int v = 0;
441 while (s->sfb_offsets[x][v + 1] << x < offset)
442 if (++v >= MAX_BANDS)
443 return AVERROR_INVALIDDATA;
444 s->sf_offsets[i][x][b] = v;
449 /** init MDCT, FIXME: only init needed sizes */
450 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
451 ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
452 1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
453 / (1 << (s->bits_per_sample - 1)));
455 /** init MDCT windows: simple sine window */
456 for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
457 const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
458 ff_init_ff_sine_windows(win_idx);
459 s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
462 /** calculate subwoofer cutoff values */
463 for (i = 0; i < num_possible_block_sizes; i++) {
464 int block_size = s->samples_per_frame >> i;
465 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
466 / s->avctx->sample_rate;
467 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
470 /** calculate sine values for the decorrelation matrix */
471 for (i = 0; i < 33; i++)
472 sin64[i] = sin(i*M_PI / 64.0);
474 if (avctx->debug & FF_DEBUG_BITSTREAM)
475 dump_context(s);
477 avctx->channel_layout = channel_mask;
479 return 0;
483 *@brief Decode the subframe length.
484 *@param s context
485 *@param offset sample offset in the frame
486 *@return decoded subframe length on success, < 0 in case of an error
488 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
490 int frame_len_shift = 0;
491 int subframe_len;
493 /** no need to read from the bitstream when only one length is possible */
494 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
495 return s->min_samples_per_subframe;
497 /** 1 bit indicates if the subframe is of maximum length */
498 if (s->max_subframe_len_bit) {
499 if (bitstream_read_bit(&s->bc))
500 frame_len_shift = 1 + bitstream_read(&s->bc,
501 s->subframe_len_bits - 1);
502 } else
503 frame_len_shift = bitstream_read(&s->bc, s->subframe_len_bits);
505 subframe_len = s->samples_per_frame >> frame_len_shift;
507 /** sanity check the length */
508 if (subframe_len < s->min_samples_per_subframe ||
509 subframe_len > s->samples_per_frame) {
510 av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
511 subframe_len);
512 return AVERROR_INVALIDDATA;
514 return subframe_len;
518 *@brief Decode how the data in the frame is split into subframes.
519 * Every WMA frame contains the encoded data for a fixed number of
520 * samples per channel. The data for every channel might be split
521 * into several subframes. This function will reconstruct the list of
522 * subframes for every channel.
524 * If the subframes are not evenly split, the algorithm estimates the
525 * channels with the lowest number of total samples.
526 * Afterwards, for each of these channels a bit is read from the
527 * bitstream that indicates if the channel contains a subframe with the
528 * next subframe size that is going to be read from the bitstream or not.
529 * If a channel contains such a subframe, the subframe size gets added to
530 * the channel's subframe list.
531 * The algorithm repeats these steps until the frame is properly divided
532 * between the individual channels.
534 *@param s context
535 *@return 0 on success, < 0 in case of an error
537 static int decode_tilehdr(WMAProDecodeCtx *s)
539 uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };/**< sum of samples for all currently known subframes of a channel */
540 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS]; /**< flag indicating if a channel contains the current subframe */
541 int channels_for_cur_subframe = s->avctx->channels; /**< number of channels that contain the current subframe */
542 int fixed_channel_layout = 0; /**< flag indicating that all channels use the same subframe offsets and sizes */
543 int min_channel_len = 0; /**< smallest sum of samples (channels with this length will be processed first) */
544 int c;
546 /* Should never consume more than 3073 bits (256 iterations for the
547 * while loop when always the minimum amount of 128 samples is subtracted
548 * from missing samples in the 8 channel case).
549 * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
552 /** reset tiling information */
553 for (c = 0; c < s->avctx->channels; c++)
554 s->channel[c].num_subframes = 0;
556 if (s->max_num_subframes == 1 || bitstream_read_bit(&s->bc))
557 fixed_channel_layout = 1;
559 /** loop until the frame data is split between the subframes */
560 do {
561 int subframe_len;
563 /** check which channels contain the subframe */
564 for (c = 0; c < s->avctx->channels; c++) {
565 if (num_samples[c] == min_channel_len) {
566 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
567 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
568 contains_subframe[c] = 1;
569 else
570 contains_subframe[c] = bitstream_read_bit(&s->bc);
571 } else
572 contains_subframe[c] = 0;
575 /** get subframe length, subframe_len == 0 is not allowed */
576 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
577 return AVERROR_INVALIDDATA;
579 /** add subframes to the individual channels and find new min_channel_len */
580 min_channel_len += subframe_len;
581 for (c = 0; c < s->avctx->channels; c++) {
582 WMAProChannelCtx* chan = &s->channel[c];
584 if (contains_subframe[c]) {
585 if (chan->num_subframes >= MAX_SUBFRAMES) {
586 av_log(s->avctx, AV_LOG_ERROR,
587 "broken frame: num subframes > 31\n");
588 return AVERROR_INVALIDDATA;
590 chan->subframe_len[chan->num_subframes] = subframe_len;
591 num_samples[c] += subframe_len;
592 ++chan->num_subframes;
593 if (num_samples[c] > s->samples_per_frame) {
594 av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
595 "channel len > samples_per_frame\n");
596 return AVERROR_INVALIDDATA;
598 } else if (num_samples[c] <= min_channel_len) {
599 if (num_samples[c] < min_channel_len) {
600 channels_for_cur_subframe = 0;
601 min_channel_len = num_samples[c];
603 ++channels_for_cur_subframe;
606 } while (min_channel_len < s->samples_per_frame);
608 for (c = 0; c < s->avctx->channels; c++) {
609 int i;
610 int offset = 0;
611 for (i = 0; i < s->channel[c].num_subframes; i++) {
612 ff_dlog(s->avctx, "frame[%"PRIi32"] channel[%i] subframe[%i]"
613 " len %"PRIu16"\n", s->frame_num, c, i,
614 s->channel[c].subframe_len[i]);
615 s->channel[c].subframe_offset[i] = offset;
616 offset += s->channel[c].subframe_len[i];
620 return 0;
624 *@brief Calculate a decorrelation matrix from the bitstream parameters.
625 *@param s codec context
626 *@param chgroup channel group for which the matrix needs to be calculated
628 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
629 WMAProChannelGrp *chgroup)
631 int i;
632 int offset = 0;
633 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
634 memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
635 s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
637 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
638 rotation_offset[i] = bitstream_read(&s->bc, 6);
640 for (i = 0; i < chgroup->num_channels; i++)
641 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
642 bitstream_read_bit(&s->bc) ? 1.0 : -1.0;
644 for (i = 1; i < chgroup->num_channels; i++) {
645 int x;
646 for (x = 0; x < i; x++) {
647 int y;
648 for (y = 0; y < i + 1; y++) {
649 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
650 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
651 int n = rotation_offset[offset + x];
652 float sinv;
653 float cosv;
655 if (n < 32) {
656 sinv = sin64[n];
657 cosv = sin64[32 - n];
658 } else {
659 sinv = sin64[64 - n];
660 cosv = -sin64[n - 32];
663 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
664 (v1 * sinv) - (v2 * cosv);
665 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
666 (v1 * cosv) + (v2 * sinv);
669 offset += i;
674 *@brief Decode channel transformation parameters
675 *@param s codec context
676 *@return 0 in case of success, < 0 in case of bitstream errors
678 static int decode_channel_transform(WMAProDecodeCtx* s)
680 int i;
681 /* should never consume more than 1921 bits for the 8 channel case
682 * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
683 * + MAX_CHANNELS + MAX_BANDS + 1)
686 /** in the one channel case channel transforms are pointless */
687 s->num_chgroups = 0;
688 if (s->avctx->channels > 1) {
689 int remaining_channels = s->channels_for_cur_subframe;
691 if (bitstream_read_bit(&s->bc)) {
692 avpriv_request_sample(s->avctx,
693 "Channel transform bit");
694 return AVERROR_PATCHWELCOME;
697 for (s->num_chgroups = 0; remaining_channels &&
698 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
699 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
700 float** channel_data = chgroup->channel_data;
701 chgroup->num_channels = 0;
702 chgroup->transform = 0;
704 /** decode channel mask */
705 if (remaining_channels > 2) {
706 for (i = 0; i < s->channels_for_cur_subframe; i++) {
707 int channel_idx = s->channel_indexes_for_cur_subframe[i];
708 if (!s->channel[channel_idx].grouped
709 && bitstream_read_bit(&s->bc)) {
710 ++chgroup->num_channels;
711 s->channel[channel_idx].grouped = 1;
712 *channel_data++ = s->channel[channel_idx].coeffs;
715 } else {
716 chgroup->num_channels = remaining_channels;
717 for (i = 0; i < s->channels_for_cur_subframe; i++) {
718 int channel_idx = s->channel_indexes_for_cur_subframe[i];
719 if (!s->channel[channel_idx].grouped)
720 *channel_data++ = s->channel[channel_idx].coeffs;
721 s->channel[channel_idx].grouped = 1;
725 /** decode transform type */
726 if (chgroup->num_channels == 2) {
727 if (bitstream_read_bit(&s->bc)) {
728 if (bitstream_read_bit(&s->bc)) {
729 avpriv_request_sample(s->avctx,
730 "Unknown channel transform type");
731 return AVERROR_PATCHWELCOME;
733 } else {
734 chgroup->transform = 1;
735 if (s->avctx->channels == 2) {
736 chgroup->decorrelation_matrix[0] = 1.0;
737 chgroup->decorrelation_matrix[1] = -1.0;
738 chgroup->decorrelation_matrix[2] = 1.0;
739 chgroup->decorrelation_matrix[3] = 1.0;
740 } else {
741 /** cos(pi/4) */
742 chgroup->decorrelation_matrix[0] = 0.70703125;
743 chgroup->decorrelation_matrix[1] = -0.70703125;
744 chgroup->decorrelation_matrix[2] = 0.70703125;
745 chgroup->decorrelation_matrix[3] = 0.70703125;
748 } else if (chgroup->num_channels > 2) {
749 if (bitstream_read_bit(&s->bc)) {
750 chgroup->transform = 1;
751 if (bitstream_read_bit(&s->bc)) {
752 decode_decorrelation_matrix(s, chgroup);
753 } else {
754 /** FIXME: more than 6 coupled channels not supported */
755 if (chgroup->num_channels > 6) {
756 avpriv_request_sample(s->avctx,
757 "Coupled channels > 6");
758 } else {
759 memcpy(chgroup->decorrelation_matrix,
760 default_decorrelation[chgroup->num_channels],
761 chgroup->num_channels * chgroup->num_channels *
762 sizeof(*chgroup->decorrelation_matrix));
768 /** decode transform on / off */
769 if (chgroup->transform) {
770 if (!bitstream_read_bit(&s->bc)) {
771 int i;
772 /** transform can be enabled for individual bands */
773 for (i = 0; i < s->num_bands; i++) {
774 chgroup->transform_band[i] = bitstream_read_bit(&s->bc);
776 } else {
777 memset(chgroup->transform_band, 1, s->num_bands);
780 remaining_channels -= chgroup->num_channels;
783 return 0;
787 *@brief Extract the coefficients from the bitstream.
788 *@param s codec context
789 *@param c current channel number
790 *@return 0 on success, < 0 in case of bitstream errors
792 static int decode_coeffs(WMAProDecodeCtx *s, int c)
794 /* Integers 0..15 as single-precision floats. The table saves a
795 costly int to float conversion, and storing the values as
796 integers allows fast sign-flipping. */
797 static const uint32_t fval_tab[16] = {
798 0x00000000, 0x3f800000, 0x40000000, 0x40400000,
799 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
800 0x41000000, 0x41100000, 0x41200000, 0x41300000,
801 0x41400000, 0x41500000, 0x41600000, 0x41700000,
803 int vlctable;
804 VLC* vlc;
805 WMAProChannelCtx* ci = &s->channel[c];
806 int rl_mode = 0;
807 int cur_coeff = 0;
808 int num_zeros = 0;
809 const uint16_t* run;
810 const float* level;
812 ff_dlog(s->avctx, "decode coefficients for channel %i\n", c);
814 vlctable = bitstream_read_bit(&s->bc);
815 vlc = &coef_vlc[vlctable];
817 if (vlctable) {
818 run = coef1_run;
819 level = coef1_level;
820 } else {
821 run = coef0_run;
822 level = coef0_level;
825 /** decode vector coefficients (consumes up to 167 bits per iteration for
826 4 vector coded large values) */
827 while ((s->transmit_num_vec_coeffs || !rl_mode) &&
828 (cur_coeff + 3 < ci->num_vec_coeffs)) {
829 uint32_t vals[4];
830 int i;
831 unsigned int idx;
833 idx = bitstream_read_vlc(&s->bc, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
835 if (idx == HUFF_VEC4_SIZE - 1) {
836 for (i = 0; i < 4; i += 2) {
837 idx = bitstream_read_vlc(&s->bc, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
838 if (idx == HUFF_VEC2_SIZE - 1) {
839 uint32_t v0, v1;
840 v0 = bitstream_read_vlc(&s->bc, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
841 if (v0 == HUFF_VEC1_SIZE - 1)
842 v0 += ff_wma_get_large_val(&s->bc);
843 v1 = bitstream_read_vlc(&s->bc, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
844 if (v1 == HUFF_VEC1_SIZE - 1)
845 v1 += ff_wma_get_large_val(&s->bc);
846 vals[i ] = av_float2int(v0);
847 vals[i+1] = av_float2int(v1);
848 } else {
849 vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];
850 vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
853 } else {
854 vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];
855 vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
856 vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
857 vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];
860 /** decode sign */
861 for (i = 0; i < 4; i++) {
862 if (vals[i]) {
863 uint32_t sign = bitstream_read_bit(&s->bc) - 1;
864 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
865 num_zeros = 0;
866 } else {
867 ci->coeffs[cur_coeff] = 0;
868 /** switch to run level mode when subframe_len / 128 zeros
869 were found in a row */
870 rl_mode |= (++num_zeros > s->subframe_len >> 8);
872 ++cur_coeff;
876 /** decode run level coded coefficients */
877 if (cur_coeff < s->subframe_len) {
878 memset(&ci->coeffs[cur_coeff], 0,
879 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
880 if (ff_wma_run_level_decode(s->avctx, &s->bc, vlc,
881 level, run, 1, ci->coeffs,
882 cur_coeff, s->subframe_len,
883 s->subframe_len, s->esc_len, 0))
884 return AVERROR_INVALIDDATA;
887 return 0;
891 *@brief Extract scale factors from the bitstream.
892 *@param s codec context
893 *@return 0 on success, < 0 in case of bitstream errors
895 static int decode_scale_factors(WMAProDecodeCtx* s)
897 int i;
899 /** should never consume more than 5344 bits
900 * MAX_CHANNELS * (1 + MAX_BANDS * 23)
903 for (i = 0; i < s->channels_for_cur_subframe; i++) {
904 int c = s->channel_indexes_for_cur_subframe[i];
905 int* sf;
906 int* sf_end;
907 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
908 sf_end = s->channel[c].scale_factors + s->num_bands;
910 /** resample scale factors for the new block size
911 * as the scale factors might need to be resampled several times
912 * before some new values are transmitted, a backup of the last
913 * transmitted scale factors is kept in saved_scale_factors
915 if (s->channel[c].reuse_sf) {
916 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
917 int b;
918 for (b = 0; b < s->num_bands; b++)
919 s->channel[c].scale_factors[b] =
920 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
923 if (!s->channel[c].cur_subframe || bitstream_read_bit(&s->bc)) {
924 if (!s->channel[c].reuse_sf) {
925 int val;
926 /** decode DPCM coded scale factors */
927 s->channel[c].scale_factor_step = bitstream_read(&s->bc, 2) + 1;
928 val = 45 / s->channel[c].scale_factor_step;
929 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
930 val += bitstream_read_vlc(&s->bc, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
931 *sf = val;
933 } else {
934 int i;
935 /** run level decode differences to the resampled factors */
936 for (i = 0; i < s->num_bands; i++) {
937 int idx;
938 int skip;
939 int val;
940 int sign;
942 idx = bitstream_read_vlc(&s->bc, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
944 if (!idx) {
945 uint32_t code = bitstream_read(&s->bc, 14);
946 val = code >> 6;
947 sign = (code & 1) - 1;
948 skip = (code & 0x3f) >> 1;
949 } else if (idx == 1) {
950 break;
951 } else {
952 skip = scale_rl_run[idx];
953 val = scale_rl_level[idx];
954 sign = bitstream_read_bit(&s->bc)-1;
957 i += skip;
958 if (i >= s->num_bands) {
959 av_log(s->avctx, AV_LOG_ERROR,
960 "invalid scale factor coding\n");
961 return AVERROR_INVALIDDATA;
963 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
966 /** swap buffers */
967 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
968 s->channel[c].table_idx = s->table_idx;
969 s->channel[c].reuse_sf = 1;
972 /** calculate new scale factor maximum */
973 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
974 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
975 s->channel[c].max_scale_factor =
976 FFMAX(s->channel[c].max_scale_factor, *sf);
980 return 0;
984 *@brief Reconstruct the individual channel data.
985 *@param s codec context
987 static void inverse_channel_transform(WMAProDecodeCtx *s)
989 int i;
991 for (i = 0; i < s->num_chgroups; i++) {
992 if (s->chgroup[i].transform) {
993 float data[WMAPRO_MAX_CHANNELS];
994 const int num_channels = s->chgroup[i].num_channels;
995 float** ch_data = s->chgroup[i].channel_data;
996 float** ch_end = ch_data + num_channels;
997 const int8_t* tb = s->chgroup[i].transform_band;
998 int16_t* sfb;
1000 /** multichannel decorrelation */
1001 for (sfb = s->cur_sfb_offsets;
1002 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
1003 int y;
1004 if (*tb++ == 1) {
1005 /** multiply values with the decorrelation_matrix */
1006 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
1007 const float* mat = s->chgroup[i].decorrelation_matrix;
1008 const float* data_end = data + num_channels;
1009 float* data_ptr = data;
1010 float** ch;
1012 for (ch = ch_data; ch < ch_end; ch++)
1013 *data_ptr++ = (*ch)[y];
1015 for (ch = ch_data; ch < ch_end; ch++) {
1016 float sum = 0;
1017 data_ptr = data;
1018 while (data_ptr < data_end)
1019 sum += *data_ptr++ * *mat++;
1021 (*ch)[y] = sum;
1024 } else if (s->avctx->channels == 2) {
1025 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1026 s->fdsp.vector_fmul_scalar(ch_data[0] + sfb[0],
1027 ch_data[0] + sfb[0],
1028 181.0 / 128, len);
1029 s->fdsp.vector_fmul_scalar(ch_data[1] + sfb[0],
1030 ch_data[1] + sfb[0],
1031 181.0 / 128, len);
1039 *@brief Apply sine window and reconstruct the output buffer.
1040 *@param s codec context
1042 static void wmapro_window(WMAProDecodeCtx *s)
1044 int i;
1045 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1046 int c = s->channel_indexes_for_cur_subframe[i];
1047 float* window;
1048 int winlen = s->channel[c].prev_block_len;
1049 float* start = s->channel[c].coeffs - (winlen >> 1);
1051 if (s->subframe_len < winlen) {
1052 start += (winlen - s->subframe_len) >> 1;
1053 winlen = s->subframe_len;
1056 window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
1058 winlen >>= 1;
1060 s->fdsp.vector_fmul_window(start, start, start + winlen,
1061 window, winlen);
1063 s->channel[c].prev_block_len = s->subframe_len;
1068 *@brief Decode a single subframe (block).
1069 *@param s codec context
1070 *@return 0 on success, < 0 when decoding failed
1072 static int decode_subframe(WMAProDecodeCtx *s)
1074 int offset = s->samples_per_frame;
1075 int subframe_len = s->samples_per_frame;
1076 int i;
1077 int total_samples = s->samples_per_frame * s->avctx->channels;
1078 int transmit_coeffs = 0;
1079 int cur_subwoofer_cutoff;
1081 s->subframe_offset = bitstream_tell(&s->bc);
1083 /** reset channel context and find the next block offset and size
1084 == the next block of the channel with the smallest number of
1085 decoded samples
1087 for (i = 0; i < s->avctx->channels; i++) {
1088 s->channel[i].grouped = 0;
1089 if (offset > s->channel[i].decoded_samples) {
1090 offset = s->channel[i].decoded_samples;
1091 subframe_len =
1092 s->channel[i].subframe_len[s->channel[i].cur_subframe];
1096 ff_dlog(s->avctx,
1097 "processing subframe with offset %i len %i\n", offset, subframe_len);
1099 /** get a list of all channels that contain the estimated block */
1100 s->channels_for_cur_subframe = 0;
1101 for (i = 0; i < s->avctx->channels; i++) {
1102 const int cur_subframe = s->channel[i].cur_subframe;
1103 /** subtract already processed samples */
1104 total_samples -= s->channel[i].decoded_samples;
1106 /** and count if there are multiple subframes that match our profile */
1107 if (offset == s->channel[i].decoded_samples &&
1108 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1109 total_samples -= s->channel[i].subframe_len[cur_subframe];
1110 s->channel[i].decoded_samples +=
1111 s->channel[i].subframe_len[cur_subframe];
1112 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
1113 ++s->channels_for_cur_subframe;
1117 /** check if the frame will be complete after processing the
1118 estimated block */
1119 if (!total_samples)
1120 s->parsed_all_subframes = 1;
1123 ff_dlog(s->avctx, "subframe is part of %i channels\n",
1124 s->channels_for_cur_subframe);
1126 /** calculate number of scale factor bands and their offsets */
1127 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
1128 s->num_bands = s->num_sfb[s->table_idx];
1129 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
1130 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
1132 /** configure the decoder for the current subframe */
1133 offset += s->samples_per_frame >> 1;
1135 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1136 int c = s->channel_indexes_for_cur_subframe[i];
1138 s->channel[c].coeffs = &s->channel[c].out[offset];
1141 s->subframe_len = subframe_len;
1142 s->esc_len = av_log2(s->subframe_len - 1) + 1;
1144 /** skip extended header if any */
1145 if (bitstream_read_bit(&s->bc)) {
1146 int num_fill_bits;
1147 if (!(num_fill_bits = bitstream_read(&s->bc, 2))) {
1148 int len = bitstream_read(&s->bc, 4);
1149 num_fill_bits = bitstream_read(&s->bc, len) + 1;
1152 if (num_fill_bits >= 0) {
1153 if (bitstream_tell(&s->bc) + num_fill_bits > s->num_saved_bits) {
1154 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
1155 return AVERROR_INVALIDDATA;
1158 bitstream_skip(&s->bc, num_fill_bits);
1162 /** no idea for what the following bit is used */
1163 if (bitstream_read_bit(&s->bc)) {
1164 avpriv_request_sample(s->avctx, "Reserved bit");
1165 return AVERROR_PATCHWELCOME;
1169 if (decode_channel_transform(s) < 0)
1170 return AVERROR_INVALIDDATA;
1173 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1174 int c = s->channel_indexes_for_cur_subframe[i];
1175 if ((s->channel[c].transmit_coefs = bitstream_read_bit(&s->bc)))
1176 transmit_coeffs = 1;
1179 if (transmit_coeffs) {
1180 int step;
1181 int quant_step = 90 * s->bits_per_sample >> 4;
1183 /** decode number of vector coded coefficients */
1184 if ((s->transmit_num_vec_coeffs = bitstream_read_bit(&s->bc))) {
1185 int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
1186 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1187 int c = s->channel_indexes_for_cur_subframe[i];
1188 int num_vec_coeffs = bitstream_read(&s->bc, num_bits) << 2;
1189 if (num_vec_coeffs + offset > FF_ARRAY_ELEMS(s->channel[c].out)) {
1190 av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
1191 return AVERROR_INVALIDDATA;
1193 s->channel[c].num_vec_coeffs = num_vec_coeffs;
1195 } else {
1196 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1197 int c = s->channel_indexes_for_cur_subframe[i];
1198 s->channel[c].num_vec_coeffs = s->subframe_len;
1201 /** decode quantization step */
1202 step = bitstream_read_signed(&s->bc, 6);
1203 quant_step += step;
1204 if (step == -32 || step == 31) {
1205 const int sign = (step == 31) - 1;
1206 int quant = 0;
1207 while (bitstream_tell(&s->bc) + 5 < s->num_saved_bits &&
1208 (step = bitstream_read(&s->bc, 5)) == 31) {
1209 quant += 31;
1211 quant_step += ((quant + step) ^ sign) - sign;
1213 if (quant_step < 0) {
1214 av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
1217 /** decode quantization step modifiers for every channel */
1219 if (s->channels_for_cur_subframe == 1) {
1220 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1221 } else {
1222 int modifier_len = bitstream_read(&s->bc, 3);
1223 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1224 int c = s->channel_indexes_for_cur_subframe[i];
1225 s->channel[c].quant_step = quant_step;
1226 if (bitstream_read_bit(&s->bc)) {
1227 if (modifier_len) {
1228 s->channel[c].quant_step += bitstream_read(&s->bc, modifier_len) + 1;
1229 } else
1230 ++s->channel[c].quant_step;
1235 /** decode scale factors */
1236 if (decode_scale_factors(s) < 0)
1237 return AVERROR_INVALIDDATA;
1240 ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
1241 bitstream_tell(&s->bc) - s->subframe_offset);
1243 /** parse coefficients */
1244 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1245 int c = s->channel_indexes_for_cur_subframe[i];
1246 if (s->channel[c].transmit_coefs &&
1247 bitstream_tell(&s->bc) < s->num_saved_bits) {
1248 decode_coeffs(s, c);
1249 } else
1250 memset(s->channel[c].coeffs, 0,
1251 sizeof(*s->channel[c].coeffs) * subframe_len);
1254 ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
1255 bitstream_tell(&s->bc) - s->subframe_offset);
1257 if (transmit_coeffs) {
1258 FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
1259 /** reconstruct the per channel data */
1260 inverse_channel_transform(s);
1261 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1262 int c = s->channel_indexes_for_cur_subframe[i];
1263 const int* sf = s->channel[c].scale_factors;
1264 int b;
1266 if (c == s->lfe_channel)
1267 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
1268 (subframe_len - cur_subwoofer_cutoff));
1270 /** inverse quantization and rescaling */
1271 for (b = 0; b < s->num_bands; b++) {
1272 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
1273 const int exp = s->channel[c].quant_step -
1274 (s->channel[c].max_scale_factor - *sf++) *
1275 s->channel[c].scale_factor_step;
1276 const float quant = pow(10.0, exp / 20.0);
1277 int start = s->cur_sfb_offsets[b];
1278 s->fdsp.vector_fmul_scalar(s->tmp + start,
1279 s->channel[c].coeffs + start,
1280 quant, end - start);
1283 /** apply imdct (imdct_half == DCTIV with reverse) */
1284 mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
1288 /** window and overlapp-add */
1289 wmapro_window(s);
1291 /** handled one subframe */
1292 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1293 int c = s->channel_indexes_for_cur_subframe[i];
1294 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1295 av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1296 return AVERROR_INVALIDDATA;
1298 ++s->channel[c].cur_subframe;
1301 return 0;
1305 *@brief Decode one WMA frame.
1306 *@param s codec context
1307 *@return 0 if the trailer bit indicates that this is the last frame,
1308 * 1 if there are additional frames
1310 static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
1312 AVCodecContext *avctx = s->avctx;
1313 BitstreamContext *bc = &s->bc;
1314 int more_frames = 0;
1315 int len = 0;
1316 int i, ret;
1318 /** get frame length */
1319 if (s->len_prefix)
1320 len = bitstream_read(bc, s->log2_frame_size);
1322 ff_dlog(s->avctx, "decoding frame with length %x\n", len);
1324 /** decode tile information */
1325 if (decode_tilehdr(s)) {
1326 s->packet_loss = 1;
1327 return 0;
1330 /** read postproc transform */
1331 if (s->avctx->channels > 1 && bitstream_read_bit(bc)) {
1332 if (bitstream_read_bit(bc)) {
1333 for (i = 0; i < avctx->channels * avctx->channels; i++)
1334 bitstream_skip(bc, 4);
1338 /** read drc info */
1339 if (s->dynamic_range_compression) {
1340 s->drc_gain = bitstream_read(bc, 8);
1341 ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
1344 /** no idea what these are for, might be the number of samples
1345 that need to be skipped at the beginning or end of a stream */
1346 if (bitstream_read_bit(bc)) {
1347 int av_unused skip;
1349 /** usually true for the first frame */
1350 if (bitstream_read_bit(bc)) {
1351 skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
1352 ff_dlog(s->avctx, "start skip: %i\n", skip);
1355 /** sometimes true for the last frame */
1356 if (bitstream_read_bit(bc)) {
1357 skip = bitstream_read(bc, av_log2(s->samples_per_frame * 2));
1358 ff_dlog(s->avctx, "end skip: %i\n", skip);
1363 ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
1364 bitstream_tell(bc) - s->frame_offset);
1366 /** reset subframe states */
1367 s->parsed_all_subframes = 0;
1368 for (i = 0; i < avctx->channels; i++) {
1369 s->channel[i].decoded_samples = 0;
1370 s->channel[i].cur_subframe = 0;
1371 s->channel[i].reuse_sf = 0;
1374 /** decode all subframes */
1375 while (!s->parsed_all_subframes) {
1376 if (decode_subframe(s) < 0) {
1377 s->packet_loss = 1;
1378 return 0;
1382 /* get output buffer */
1383 frame->nb_samples = s->samples_per_frame;
1384 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1385 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1386 s->packet_loss = 1;
1387 return 0;
1390 /** copy samples to the output buffer */
1391 for (i = 0; i < avctx->channels; i++)
1392 memcpy(frame->extended_data[i], s->channel[i].out,
1393 s->samples_per_frame * sizeof(*s->channel[i].out));
1395 for (i = 0; i < avctx->channels; i++) {
1396 /** reuse second half of the IMDCT output for the next frame */
1397 memcpy(&s->channel[i].out[0],
1398 &s->channel[i].out[s->samples_per_frame],
1399 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
1402 if (s->skip_frame) {
1403 s->skip_frame = 0;
1404 *got_frame_ptr = 0;
1405 av_frame_unref(frame);
1406 } else {
1407 *got_frame_ptr = 1;
1410 if (s->len_prefix) {
1411 if (len != (bitstream_tell(bc) - s->frame_offset) + 2) {
1412 /** FIXME: not sure if this is always an error */
1413 av_log(s->avctx, AV_LOG_ERROR,
1414 "frame[%"PRIu32"] would have to skip %i bits\n",
1415 s->frame_num,
1416 len - (bitstream_tell(bc) - s->frame_offset) - 1);
1417 s->packet_loss = 1;
1418 return 0;
1421 /** skip the rest of the frame data */
1422 bitstream_skip(bc, len - (bitstream_tell(bc) - s->frame_offset) - 1);
1423 } else {
1424 while (bitstream_tell(bc) < s->num_saved_bits && bitstream_read_bit(bc) == 0) {
1428 /** decode trailer bit */
1429 more_frames = bitstream_read_bit(bc);
1431 ++s->frame_num;
1432 return more_frames;
1436 *@brief Calculate remaining input buffer length.
1437 *@param s codec context
1438 *@param bc bitstream reader context
1439 *@return remaining size in bits
1441 static int remaining_bits(WMAProDecodeCtx *s, BitstreamContext *bc)
1443 return s->buf_bit_size - bitstream_tell(bc);
1447 *@brief Fill the bit reservoir with a (partial) frame.
1448 *@param s codec context
1449 *@param bc bitstream reader context
1450 *@param len length of the partial frame
1451 *@param append decides whether to reset the buffer or not
1453 static void save_bits(WMAProDecodeCtx *s, BitstreamContext *bc, int len,
1454 int append)
1456 int buflen;
1458 /** when the frame data does not need to be concatenated, the input buffer
1459 is reset and additional bits from the previous frame are copied
1460 and skipped later so that a fast byte copy is possible */
1462 if (!append) {
1463 s->frame_offset = bitstream_tell(bc) & 7;
1464 s->num_saved_bits = s->frame_offset;
1465 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1468 buflen = (s->num_saved_bits + len + 8) >> 3;
1470 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1471 avpriv_request_sample(s->avctx, "Too small input buffer");
1472 s->packet_loss = 1;
1473 return;
1476 if (len > put_bits_left(&s->pb)) {
1477 av_log(s->avctx, AV_LOG_ERROR,
1478 "Cannot append %d bits, only %d bits available.\n",
1479 len, put_bits_left(&s->pb));
1480 s->packet_loss = 1;
1481 return;
1484 s->num_saved_bits += len;
1485 if (!append) {
1486 avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3),
1487 s->num_saved_bits);
1488 } else {
1489 int align = 8 - (bitstream_tell(bc) & 7);
1490 align = FFMIN(align, len);
1491 put_bits(&s->pb, align, bitstream_read(bc, align));
1492 len -= align;
1493 avpriv_copy_bits(&s->pb, bc->buffer + (bitstream_tell(bc) >> 3), len);
1495 bitstream_skip(bc, len);
1498 PutBitContext tmp = s->pb;
1499 flush_put_bits(&tmp);
1502 bitstream_init(&s->bc, s->frame_data, s->num_saved_bits);
1503 bitstream_skip(&s->bc, s->frame_offset);
1507 *@brief Decode a single WMA packet.
1508 *@param avctx codec context
1509 *@param data the output buffer
1510 *@param avpkt input packet
1511 *@return number of bytes that were read from the input buffer
1513 static int decode_packet(AVCodecContext *avctx, void *data,
1514 int *got_frame_ptr, AVPacket* avpkt)
1516 WMAProDecodeCtx *s = avctx->priv_data;
1517 BitstreamContext *bc = &s->pbc;
1518 const uint8_t* buf = avpkt->data;
1519 int buf_size = avpkt->size;
1520 int num_bits_prev_frame;
1521 int packet_sequence_number;
1523 *got_frame_ptr = 0;
1525 if (s->packet_done || s->packet_loss) {
1526 s->packet_done = 0;
1528 /** sanity check for the buffer length */
1529 if (buf_size < avctx->block_align) {
1530 av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
1531 buf_size, avctx->block_align);
1532 return AVERROR_INVALIDDATA;
1535 s->next_packet_start = buf_size - avctx->block_align;
1536 buf_size = avctx->block_align;
1537 s->buf_bit_size = buf_size << 3;
1539 /** parse packet header */
1540 bitstream_init(bc, buf, s->buf_bit_size);
1541 packet_sequence_number = bitstream_read(bc, 4);
1542 bitstream_skip(bc, 2);
1544 /** get number of bits that need to be added to the previous frame */
1545 num_bits_prev_frame = bitstream_read(bc, s->log2_frame_size);
1546 ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
1547 num_bits_prev_frame);
1549 /** check for packet loss */
1550 if (!s->packet_loss &&
1551 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1552 s->packet_loss = 1;
1553 av_log(avctx, AV_LOG_ERROR,
1554 "Packet loss detected! seq %"PRIx8" vs %x\n",
1555 s->packet_sequence_number, packet_sequence_number);
1557 s->packet_sequence_number = packet_sequence_number;
1559 if (num_bits_prev_frame > 0) {
1560 int remaining_packet_bits = s->buf_bit_size - bitstream_tell(bc);
1561 if (num_bits_prev_frame >= remaining_packet_bits) {
1562 num_bits_prev_frame = remaining_packet_bits;
1563 s->packet_done = 1;
1566 /** append the previous frame data to the remaining data from the
1567 previous packet to create a full frame */
1568 save_bits(s, bc, num_bits_prev_frame, 1);
1569 ff_dlog(avctx, "accumulated %x bits of frame data\n",
1570 s->num_saved_bits - s->frame_offset);
1572 /** decode the cross packet frame if it is valid */
1573 if (!s->packet_loss)
1574 decode_frame(s, data, got_frame_ptr);
1575 } else if (s->num_saved_bits - s->frame_offset) {
1576 ff_dlog(avctx, "ignoring %x previously saved bits\n",
1577 s->num_saved_bits - s->frame_offset);
1580 if (s->packet_loss) {
1581 /** reset number of saved bits so that the decoder
1582 does not start to decode incomplete frames in the
1583 s->len_prefix == 0 case */
1584 s->num_saved_bits = 0;
1585 s->packet_loss = 0;
1588 } else {
1589 int frame_size;
1590 s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1591 bitstream_init(bc, avpkt->data, s->buf_bit_size);
1592 bitstream_skip(bc, s->packet_offset);
1593 if (s->len_prefix && remaining_bits(s, bc) > s->log2_frame_size &&
1594 (frame_size = bitstream_peek(bc, s->log2_frame_size)) &&
1595 frame_size <= remaining_bits(s, bc)) {
1596 save_bits(s, bc, frame_size, 0);
1597 s->packet_done = !decode_frame(s, data, got_frame_ptr);
1598 } else if (!s->len_prefix
1599 && s->num_saved_bits > bitstream_tell(&s->bc)) {
1600 /** when the frames do not have a length prefix, we don't know
1601 the compressed length of the individual frames
1602 however, we know what part of a new packet belongs to the
1603 previous frame
1604 therefore we save the incoming packet first, then we append
1605 the "previous frame" data from the next packet so that
1606 we get a buffer that only contains full frames */
1607 s->packet_done = !decode_frame(s, data, got_frame_ptr);
1608 } else
1609 s->packet_done = 1;
1612 if (s->packet_done && !s->packet_loss &&
1613 remaining_bits(s, bc) > 0) {
1614 /** save the rest of the data so that it can be decoded
1615 with the next packet */
1616 save_bits(s, bc, remaining_bits(s, bc), 0);
1619 s->packet_offset = bitstream_tell(bc) & 7;
1620 if (s->packet_loss)
1621 return AVERROR_INVALIDDATA;
1623 return bitstream_tell(bc) >> 3;
1627 *@brief Clear decoder buffers (for seeking).
1628 *@param avctx codec context
1630 static void flush(AVCodecContext *avctx)
1632 WMAProDecodeCtx *s = avctx->priv_data;
1633 int i;
1634 /** reset output buffer as a part of it is used during the windowing of a
1635 new frame */
1636 for (i = 0; i < avctx->channels; i++)
1637 memset(s->channel[i].out, 0, s->samples_per_frame *
1638 sizeof(*s->channel[i].out));
1639 s->packet_loss = 1;
1644 *@brief wmapro decoder
1646 AVCodec ff_wmapro_decoder = {
1647 .name = "wmapro",
1648 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
1649 .type = AVMEDIA_TYPE_AUDIO,
1650 .id = AV_CODEC_ID_WMAPRO,
1651 .priv_data_size = sizeof(WMAProDecodeCtx),
1652 .init = decode_init,
1653 .close = decode_end,
1654 .decode = decode_packet,
1655 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1656 .flush = flush,
1657 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1658 AV_SAMPLE_FMT_NONE },