Refinement of initial lowmem handling for libwmapro (r27593). Set maximum supported...
[kugel-rb.git] / apps / codecs / libwmapro / wmaprodec.c
blobf3920f9d76edcac673a9510afdf910d346eae834
1 /*
2 * Wmapro compatible decoder
3 * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4 * Copyright (c) 2008 - 2009 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
23 /**
24 * @file libavcodec/wmaprodec.c
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 "ffmpeg_get_bits.h"
90 #include "ffmpeg_put_bits.h"
91 #include "wmaprodata.h"
92 #include "wma.h"
93 #include "wmaprodec.h"
94 #include "wmapro_mdct.h"
95 #include "mdct_tables.h"
96 #include "quant.h"
97 #include "wmapro_math.h"
98 #include "codecs.h"
99 #include "codeclib.h"
100 #include "../libasf/asf.h"
102 /* Uncomment the following line to enable some debug output */
103 //#define WMAPRO_DUMP_CTX_EN
105 #undef DEBUGF
106 #ifdef WMAPRO_DUMP_CTX_EN
107 # define DEBUGF printf
108 #else
109 # define DEBUGF(...)
110 #endif
112 /* Some defines to make it compile */
113 #define AVERROR_INVALIDDATA -1
114 #define AVERROR_PATCHWELCOME -2
115 #define av_log_ask_for_sample(...)
117 /* Taken from avcodec.h */
118 #define FF_INPUT_BUFFER_PADDING_SIZE 8
120 /* Taken from libavutil/mem.h */
121 #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
123 /* Taken from libavutil/common.h */
124 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
125 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
127 /* Enable multichannel for large-memory targets only */
128 #if (MEMORYSIZE > 2)
129 #define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
130 #else
131 #define WMAPRO_MAX_CHANNELS 2 ///< max number of handled channels
132 #endif
134 /* Current decoder limitations */
135 #define MAX_SUBFRAMES 32 ///< max number of subframes per channel
136 #define MAX_BANDS 29 ///< max number of scale factor bands
137 #define MAX_FRAMESIZE 32768 ///< maximum compressed frame size
139 #define WMAPRO_BLOCK_MAX_BITS 12 ///< log2 of max block size
140 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS) ///< maximum block size
141 #define WMAPRO_BLOCK_SIZES (WMAPRO_BLOCK_MAX_BITS - BLOCK_MIN_BITS + 1) ///< possible block sizes
142 #define WMAPRO_OUT_BUF_SIZE (WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2)
145 #define VLCBITS 9
146 #define SCALEVLCBITS 8
147 #define VEC4MAXDEPTH ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
148 #define VEC2MAXDEPTH ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
149 #define VEC1MAXDEPTH ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
150 #define SCALEMAXDEPTH ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
151 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
153 static VLC sf_vlc; ///< scale factor DPCM vlc
154 static VLC sf_rl_vlc; ///< scale factor run length vlc
155 static VLC vec4_vlc; ///< 4 coefficients per symbol
156 static VLC vec2_vlc; ///< 2 coefficients per symbol
157 static VLC vec1_vlc; ///< 1 coefficient per symbol
158 static VLC coef_vlc[2]; ///< coefficient run length vlc codes
159 //static float sin64[33]; ///< sinus table for decorrelation
161 /* Global defined arrays to allow IRAM usage for some models. */
162 static int32_t g_tmp[WMAPRO_BLOCK_MAX_SIZE] IBSS_ATTR_WMAPRO_LARGE_IRAM;
163 static int32_t g_out_ch0[WMAPRO_OUT_BUF_SIZE] IBSS_ATTR;
164 static int32_t g_out_ch1[WMAPRO_OUT_BUF_SIZE] IBSS_ATTR_WMAPRO_LARGE_IRAM;
165 #if (WMAPRO_MAX_CHANNELS > 2)
166 static int32_t g_out_multichannel[WMAPRO_MAX_CHANNELS-2][WMAPRO_OUT_BUF_SIZE];
167 #endif
170 * @brief frame specific decoder context for a single channel
172 typedef struct {
173 int16_t prev_block_len; ///< length of the previous block
174 uint8_t transmit_coefs;
175 uint8_t num_subframes;
176 uint16_t subframe_len[MAX_SUBFRAMES]; ///< subframe length in samples
177 uint16_t subframe_offset[MAX_SUBFRAMES]; ///< subframe positions in the current frame
178 uint8_t cur_subframe; ///< current subframe number
179 uint16_t decoded_samples; ///< number of already processed samples
180 uint8_t grouped; ///< channel is part of a group
181 int quant_step; ///< quantization step for the current subframe
182 int8_t reuse_sf; ///< share scale factors between subframes
183 int8_t scale_factor_step; ///< scaling step for the current subframe
184 int max_scale_factor; ///< maximum scale factor for the current subframe
185 int saved_scale_factors[2][MAX_BANDS]; ///< resampled and (previously) transmitted scale factor values
186 int8_t scale_factor_idx; ///< index for the transmitted scale factor values (used for resampling)
187 int* scale_factors; ///< pointer to the scale factor values used for decoding
188 uint8_t table_idx; ///< index in sf_offsets for the scale factor reference block
189 int32_t* coeffs; ///< pointer to the subframe decode buffer
190 int32_t* out; ///< output buffer
191 } WMAProChannelCtx;
194 * @brief channel group for channel transformations
196 typedef struct {
197 uint8_t num_channels; ///< number of channels in the group
198 int8_t transform; ///< transform on / off
199 int8_t transform_band[MAX_BANDS]; ///< controls if the transform is enabled for a certain band
200 //float decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
201 int32_t* channel_data[WMAPRO_MAX_CHANNELS]; ///< transformation coefficients
202 int32_t fixdecorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
203 } WMAProChannelGrp;
206 * @brief main decoder context
208 typedef struct WMAProDecodeCtx {
209 /* generic decoder variables */
210 uint8_t frame_data[MAX_FRAMESIZE +
211 FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
212 PutBitContext pb; ///< context for filling the frame_data buffer
213 int32_t* tmp; ///< IMDCT input buffer
215 /* frame size dependent frame information (set during initialization) */
216 uint32_t decode_flags; ///< used compression features
217 uint8_t len_prefix; ///< frame is prefixed with its length
218 uint8_t dynamic_range_compression; ///< frame contains DRC data
219 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
220 uint16_t samples_per_frame; ///< number of samples to output
221 uint16_t log2_frame_size;
222 int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
223 int8_t lfe_channel; ///< lfe channel index
224 uint8_t max_num_subframes;
225 uint8_t subframe_len_bits; ///< number of bits used for the subframe length
226 uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
227 uint16_t min_samples_per_subframe;
228 int8_t num_sfb[WMAPRO_BLOCK_SIZES]; ///< scale factor bands per block size
229 int16_t sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor band offsets (multiples of 4)
230 int8_t sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
231 int16_t subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
233 /* packet decode state */
234 GetBitContext pgb; ///< bitstream reader context for the packet
235 uint8_t packet_offset; ///< frame offset in the packet
236 uint8_t packet_sequence_number; ///< current packet number
237 int num_saved_bits; ///< saved number of bits
238 int frame_offset; ///< frame offset in the bit reservoir
239 int subframe_offset; ///< subframe offset in the bit reservoir
240 uint8_t packet_loss; ///< set in case of bitstream error
241 uint8_t packet_done; ///< set when a packet is fully decoded
243 /* frame decode state */
244 uint32_t frame_num; ///< current frame number
245 GetBitContext gb; ///< bitstream reader context
246 int buf_bit_size; ///< buffer size in bits
247 int32_t samples;
248 int32_t* samples_end; ///< maximum samplebuffer pointer
249 uint8_t drc_gain; ///< gain for the DRC tool
250 int8_t skip_frame; ///< skip output step
251 int8_t parsed_all_subframes; ///< all subframes decoded?
253 /* subframe/block decode state */
254 int16_t subframe_len; ///< current subframe length
255 int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
256 int8_t channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
257 int8_t num_bands; ///< number of scale factor bands
258 int16_t* cur_sfb_offsets; ///< sfb offsets for the current block
259 uint8_t table_idx; ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
260 int8_t esc_len; ///< length of escaped coefficients
262 uint8_t num_chgroups; ///< number of channel groups
263 WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS]; ///< channel group information
265 WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS]; ///< per channel data
266 } WMAProDecodeCtx;
268 /* static decode context, to avoid malloc */
269 static WMAProDecodeCtx globWMAProDecCtx;
272 *@brief helper function to print the most important members of the context
273 *@param s context
275 #ifdef WMAPRO_DUMP_CTX_EN
276 static void dump_context(WMAProDecodeCtx *s)
278 #define PRINT(a, b) printf(" %s = %d\n", a, b);
279 #define PRINT_HEX(a, b) printf(" %s = %x\n", a, b);
281 PRINT("ed sample bit depth", s->bits_per_sample);
282 PRINT_HEX("ed decode flags", s->decode_flags);
283 PRINT("samples per frame", s->samples_per_frame);
284 PRINT("log2 frame size", s->log2_frame_size);
285 PRINT("max num subframes", s->max_num_subframes);
286 PRINT("len prefix", s->len_prefix);
287 PRINT("num channels", s->num_channels);
289 #endif
292 *@brief Initialize the decoder.
293 *@param avctx codec context
294 *@return 0 on success, -1 otherwise
296 int decode_init(asf_waveformatex_t *wfx)
298 memset(&globWMAProDecCtx, 0, sizeof(WMAProDecodeCtx));
299 WMAProDecodeCtx *s = &globWMAProDecCtx;
300 uint8_t *edata_ptr = wfx->data;
301 unsigned int channel_mask;
302 int i;
303 int log2_max_num_subframes;
304 int num_possible_block_sizes;
306 /* Use globally defined array. Allows IRAM usage for models with large IRAM. */
307 s->tmp = g_tmp;
309 /* Use globally defined arrays. Allows IRAM usage for up to 2 channels. */
310 s->channel[0].out = g_out_ch0;
311 s->channel[1].out = g_out_ch1;
312 #if (WMAPRO_MAX_CHANNELS > 2)
313 for (i=2; i<WMAPRO_MAX_CHANNELS; ++i)
314 s->channel[i].out = g_out_multichannel[i-2];
315 #endif
317 #if defined(CPU_COLDFIRE)
318 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
319 #endif
321 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
323 if (wfx->datalen >= 18) {
324 s->decode_flags = AV_RL16(edata_ptr+14);
325 channel_mask = AV_RL32(edata_ptr+2);
326 s->bits_per_sample = AV_RL16(edata_ptr);
327 /** dump the extradata */
328 for (i = 0; i < wfx->datalen; i++)
329 DEBUGF("[%x] ", wfx->data[i]);
330 DEBUGF("\n");
332 } else {
333 DEBUGF("Unknown extradata size\n");
334 return AVERROR_INVALIDDATA;
337 /** generic init */
338 s->log2_frame_size = av_log2(wfx->blockalign) + 4;
340 /** frame info */
341 s->skip_frame = 1; /** skip first frame */
342 s->packet_loss = 1;
343 s->len_prefix = (s->decode_flags & 0x40);
345 if (!s->len_prefix) {
346 DEBUGF("no length prefix\n");
347 return AVERROR_INVALIDDATA;
350 /** get frame len */
351 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(wfx->rate,
352 3, s->decode_flags);
354 /** init previous block len */
355 for (i = 0; i < wfx->channels; i++)
356 s->channel[i].prev_block_len = s->samples_per_frame;
358 /** subframe info */
359 log2_max_num_subframes = ((s->decode_flags & 0x38) >> 3);
360 s->max_num_subframes = 1 << log2_max_num_subframes;
361 if (s->max_num_subframes == 16)
362 s->max_subframe_len_bit = 1;
363 s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
365 num_possible_block_sizes = log2_max_num_subframes + 1;
366 s->min_samples_per_subframe = s->samples_per_frame / s->max_num_subframes;
367 s->dynamic_range_compression = (s->decode_flags & 0x80);
369 if (s->max_num_subframes > MAX_SUBFRAMES) {
370 DEBUGF("invalid number of subframes %i\n",
371 s->max_num_subframes);
372 return AVERROR_INVALIDDATA;
375 s->num_channels = wfx->channels;
377 /** extract lfe channel position */
378 s->lfe_channel = -1;
380 if (channel_mask & 8) {
381 unsigned int mask;
382 for (mask = 1; mask < 16; mask <<= 1) {
383 if (channel_mask & mask)
384 ++s->lfe_channel;
388 if (s->num_channels < 0) {
389 DEBUGF("invalid number of channels %d\n", s->num_channels);
390 return AVERROR_INVALIDDATA;
391 } else if (s->num_channels > WMAPRO_MAX_CHANNELS) {
392 DEBUGF("unsupported number of channels\n");
393 return AVERROR_PATCHWELCOME;
396 INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
397 scale_huffbits, 1, 1,
398 scale_huffcodes, 2, 2, 616);
400 INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
401 scale_rl_huffbits, 1, 1,
402 scale_rl_huffcodes, 4, 4, 1406);
404 INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
405 coef0_huffbits, 1, 1,
406 coef0_huffcodes, 4, 4, 2108);
408 INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
409 coef1_huffbits, 1, 1,
410 coef1_huffcodes, 4, 4, 3912);
412 INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
413 vec4_huffbits, 1, 1,
414 vec4_huffcodes, 2, 2, 604);
416 INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
417 vec2_huffbits, 1, 1,
418 vec2_huffcodes, 2, 2, 562);
420 INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
421 vec1_huffbits, 1, 1,
422 vec1_huffcodes, 2, 2, 562);
424 /** calculate number of scale factor bands and their offsets
425 for every possible block size */
426 for (i = 0; i < num_possible_block_sizes; i++) {
427 int subframe_len = s->samples_per_frame >> i;
428 int x;
429 int band = 1;
431 s->sfb_offsets[i][0] = 0;
433 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
434 int offset = (subframe_len * 2 * critical_freq[x])
435 / wfx->rate + 2;
436 offset &= ~3;
437 if (offset > s->sfb_offsets[i][band - 1])
438 s->sfb_offsets[i][band++] = offset;
440 s->sfb_offsets[i][band - 1] = subframe_len;
441 s->num_sfb[i] = band - 1;
445 /** Scale factors can be shared between blocks of different size
446 as every block has a different scale factor band layout.
447 The matrix sf_offsets is needed to find the correct scale factor.
450 for (i = 0; i < num_possible_block_sizes; i++) {
451 int b;
452 for (b = 0; b < s->num_sfb[i]; b++) {
453 int x;
454 int offset = ((s->sfb_offsets[i][b]
455 + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
456 for (x = 0; x < num_possible_block_sizes; x++) {
457 int v = 0;
458 while (s->sfb_offsets[x][v + 1] << x < offset)
459 ++v;
460 s->sf_offsets[i][x][b] = v;
465 /** calculate subwoofer cutoff values */
466 for (i = 0; i < num_possible_block_sizes; i++) {
467 int block_size = s->samples_per_frame >> i;
468 int cutoff = (440*block_size + 3 * (wfx->rate >> 1) - 1)
469 / wfx->rate;
470 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
473 #if 0
474 /** calculate sine values for the decorrelation matrix */
475 for (i = 0; i < 33; i++)
476 sin64[i] = sin(i*M_PI / 64.0);
477 #endif
479 #ifdef WMAPRO_DUMP_CTX_EN
480 dump_context(s);
481 #endif
482 return 0;
486 *@brief Decode the subframe length.
487 *@param s context
488 *@param offset sample offset in the frame
489 *@return decoded subframe length on success, < 0 in case of an error
491 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
493 int frame_len_shift = 0;
494 int subframe_len;
496 /** no need to read from the bitstream when only one length is possible */
497 if (offset == s->samples_per_frame - s->min_samples_per_subframe)
498 return s->min_samples_per_subframe;
500 /** 1 bit indicates if the subframe is of maximum length */
501 if (s->max_subframe_len_bit) {
502 if (get_bits1(&s->gb))
503 frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
504 } else
505 frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
507 subframe_len = s->samples_per_frame >> frame_len_shift;
509 /** sanity check the length */
510 if (subframe_len < s->min_samples_per_subframe ||
511 subframe_len > s->samples_per_frame) {
512 DEBUGF("broken frame: subframe_len %i\n",
513 subframe_len);
514 return AVERROR_INVALIDDATA;
516 return subframe_len;
520 *@brief Decode how the data in the frame is split into subframes.
521 * Every WMA frame contains the encoded data for a fixed number of
522 * samples per channel. The data for every channel might be split
523 * into several subframes. This function will reconstruct the list of
524 * subframes for every channel.
526 * If the subframes are not evenly split, the algorithm estimates the
527 * channels with the lowest number of total samples.
528 * Afterwards, for each of these channels a bit is read from the
529 * bitstream that indicates if the channel contains a subframe with the
530 * next subframe size that is going to be read from the bitstream or not.
531 * If a channel contains such a subframe, the subframe size gets added to
532 * the channel's subframe list.
533 * The algorithm repeats these steps until the frame is properly divided
534 * between the individual channels.
536 *@param s context
537 *@return 0 on success, < 0 in case of an error
539 static int decode_tilehdr(WMAProDecodeCtx *s)
541 uint16_t num_samples[WMAPRO_MAX_CHANNELS]; /** sum of samples for all currently known subframes of a channel */
542 uint8_t contains_subframe[WMAPRO_MAX_CHANNELS]; /** flag indicating if a channel contains the current subframe */
543 int channels_for_cur_subframe = s->num_channels; /** number of channels that contain the current subframe */
544 int fixed_channel_layout = 0; /** flag indicating that all channels use the same subframe offsets and sizes */
545 int min_channel_len = 0; /** smallest sum of samples (channels with this length will be processed first) */
546 int c;
548 /* Should never consume more than 3073 bits (256 iterations for the
549 * while loop when always the minimum amount of 128 samples is substracted
550 * from missing samples in the 8 channel case).
551 * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS + 4)
554 /** reset tiling information */
555 for (c = 0; c < s->num_channels; c++)
556 s->channel[c].num_subframes = 0;
558 memset(num_samples, 0, sizeof(num_samples));
560 if (s->max_num_subframes == 1 || get_bits1(&s->gb))
561 fixed_channel_layout = 1;
563 /** loop until the frame data is split between the subframes */
564 do {
565 int subframe_len;
567 /** check which channels contain the subframe */
568 for (c = 0; c < s->num_channels; c++) {
569 if (num_samples[c] == min_channel_len) {
570 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
571 (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
572 contains_subframe[c] = 1;
573 else
574 contains_subframe[c] = get_bits1(&s->gb);
575 } else
576 contains_subframe[c] = 0;
579 /** get subframe length, subframe_len == 0 is not allowed */
580 if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
581 return AVERROR_INVALIDDATA;
583 /** add subframes to the individual channels and find new min_channel_len */
584 min_channel_len += subframe_len;
585 for (c = 0; c < s->num_channels; c++) {
586 WMAProChannelCtx* chan = &s->channel[c];
588 if (contains_subframe[c]) {
589 if (chan->num_subframes >= MAX_SUBFRAMES) {
590 DEBUGF("broken frame: num subframes > 31\n");
591 return AVERROR_INVALIDDATA;
593 chan->subframe_len[chan->num_subframes] = subframe_len;
594 num_samples[c] += subframe_len;
595 ++chan->num_subframes;
596 if (num_samples[c] > s->samples_per_frame) {
597 DEBUGF("broken frame: "
598 "channel len > samples_per_frame\n");
599 return AVERROR_INVALIDDATA;
601 } else if (num_samples[c] <= min_channel_len) {
602 if (num_samples[c] < min_channel_len) {
603 channels_for_cur_subframe = 0;
604 min_channel_len = num_samples[c];
606 ++channels_for_cur_subframe;
609 } while (min_channel_len < s->samples_per_frame);
611 for (c = 0; c < s->num_channels; c++) {
612 int i;
613 int offset = 0;
614 for (i = 0; i < s->channel[c].num_subframes; i++) {
615 DEBUGF("frame[%i] channel[%i] subframe[%i]"
616 " len %i\n", s->frame_num, c, i,
617 s->channel[c].subframe_len[i]);
618 s->channel[c].subframe_offset[i] = offset;
619 offset += s->channel[c].subframe_len[i];
623 return 0;
626 #if 0
628 *@brief Calculate a decorrelation matrix from the bitstream parameters.
629 *@param s codec context
630 *@param chgroup channel group for which the matrix needs to be calculated
632 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
633 WMAProChannelGrp *chgroup)
635 int i;
636 int offset = 0;
637 int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
638 memset(chgroup->decorrelation_matrix, 0, s->num_channels *
639 s->num_channels * sizeof(*chgroup->decorrelation_matrix));
641 for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
642 rotation_offset[i] = get_bits(&s->gb, 6);
644 for (i = 0; i < chgroup->num_channels; i++) {
645 chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
646 get_bits1(&s->gb) ? 1.0 : -1.0;
648 if(chgroup->decorrelation_matrix[chgroup->num_channels * i + i] > 0)
649 chgroup->fixdecorrelation_matrix[chgroup->num_channels * i + i] = 0x10000;
650 else
651 chgroup->fixdecorrelation_matrix[chgroup->num_channels * i + i] = -0x10000;
654 for (i = 1; i < chgroup->num_channels; i++) {
655 int x;
656 for (x = 0; x < i; x++) {
657 int y;
658 for (y = 0; y < i + 1; y++) {
659 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
660 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
661 int32_t f1 = chgroup->fixdecorrelation_matrix[x * chgroup->num_channels + y];
662 int32_t f2 = chgroup->fixdecorrelation_matrix[i * chgroup->num_channels + y];
663 int n = rotation_offset[offset + x];
664 float sinv;
665 float cosv;
666 int32_t fixsinv;
667 int32_t fixcosv;
669 if (n < 32) {
670 sinv = sin64[n];
671 cosv = sin64[32 - n];
672 fixsinv = fixed_sin64[n];
673 fixcosv = fixed_sin64[32-n];
674 } else {
675 sinv = sin64[64 - n];
676 cosv = -sin64[n - 32];
677 fixsinv = fixed_sin64[64-n];
678 fixcosv = -fixed_sin64[n-32];
681 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
682 (v1 * sinv) - (v2 * cosv);
683 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
684 (v1 * cosv) + (v2 * sinv);
685 chgroup->fixdecorrelation_matrix[y + x * chgroup->num_channels] =
686 fixmul31(f1, fixsinv) - fixmul31(f2, fixcosv);
687 chgroup->fixdecorrelation_matrix[y + i * chgroup->num_channels] =
688 fixmul31(f1, fixcosv) + fixmul31(f2, fixsinv);
692 offset += i;
695 #endif
698 *@brief Decode channel transformation parameters
699 *@param s codec context
700 *@return 0 in case of success, < 0 in case of bitstream errors
702 static int decode_channel_transform(WMAProDecodeCtx* s)
704 int i;
705 /* should never consume more than 1921 bits for the 8 channel case
706 * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
707 * + MAX_CHANNELS + MAX_BANDS + 1)
710 /** in the one channel case channel transforms are pointless */
711 s->num_chgroups = 0;
712 if (s->num_channels > 1) {
713 int remaining_channels = s->channels_for_cur_subframe;
715 if (get_bits1(&s->gb)) {
716 DEBUGF("unsupported channel transform bit\n");
717 return AVERROR_INVALIDDATA;
720 for (s->num_chgroups = 0; remaining_channels &&
721 s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
722 WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
723 int32_t** channel_data = chgroup->channel_data;
724 chgroup->num_channels = 0;
725 chgroup->transform = 0;
727 /** decode channel mask */
728 if (remaining_channels > 2) {
729 for (i = 0; i < s->channels_for_cur_subframe; i++) {
730 int channel_idx = s->channel_indexes_for_cur_subframe[i];
731 if (!s->channel[channel_idx].grouped
732 && get_bits1(&s->gb)) {
733 ++chgroup->num_channels;
734 s->channel[channel_idx].grouped = 1;
735 *channel_data++ = s->channel[channel_idx].coeffs;
738 } else {
739 chgroup->num_channels = remaining_channels;
740 for (i = 0; i < s->channels_for_cur_subframe; i++) {
741 int channel_idx = s->channel_indexes_for_cur_subframe[i];
742 if (!s->channel[channel_idx].grouped)
743 *channel_data++ = s->channel[channel_idx].coeffs;
744 s->channel[channel_idx].grouped = 1;
748 /** decode transform type */
749 if (chgroup->num_channels == 2) {
750 if (get_bits1(&s->gb)) {
751 if (get_bits1(&s->gb)) {
752 DEBUGF("unsupported channel transform type\n");
754 } else {
755 chgroup->transform = 1;
756 if (s->num_channels == 2) {
757 chgroup->fixdecorrelation_matrix[0] = 0x10000;
758 chgroup->fixdecorrelation_matrix[1] = -0x10000;
759 chgroup->fixdecorrelation_matrix[2] = 0x10000;
760 chgroup->fixdecorrelation_matrix[3] = 0x10000;
761 } else {
762 /** cos(pi/4) */
763 chgroup->fixdecorrelation_matrix[0] = 0xB500;
764 chgroup->fixdecorrelation_matrix[1] = -0xB500;
765 chgroup->fixdecorrelation_matrix[2] = 0xB500;
766 chgroup->fixdecorrelation_matrix[3] = 0xB500;
769 } else if (chgroup->num_channels > 2) {
770 DEBUGF("in wmaprodec.c: Multichannel streams still not supported\n");
771 return -1;
772 #if 0
773 if (get_bits1(&s->gb)) {
774 chgroup->transform = 1;
775 if (get_bits1(&s->gb)) {
776 decode_decorrelation_matrix(s, chgroup);
777 } else {
778 /** FIXME: more than 6 coupled channels not supported */
779 if (chgroup->num_channels > 6) {
780 av_log_ask_for_sample(s->avctx,
781 "coupled channels > 6\n");
782 } else {
783 memcpy(chgroup->decorrelation_matrix,
784 default_decorrelation[chgroup->num_channels],
785 chgroup->num_channels * chgroup->num_channels *
786 sizeof(*chgroup->decorrelation_matrix));
790 #endif
793 /** decode transform on / off */
794 if (chgroup->transform) {
795 if (!get_bits1(&s->gb)) {
796 int i;
797 /** transform can be enabled for individual bands */
798 for (i = 0; i < s->num_bands; i++) {
799 chgroup->transform_band[i] = get_bits1(&s->gb);
801 } else {
802 memset(chgroup->transform_band, 1, s->num_bands);
805 remaining_channels -= chgroup->num_channels;
808 return 0;
812 *@brief Extract the coefficients from the bitstream.
813 *@param s codec context
814 *@param c current channel number
815 *@return 0 on success, < 0 in case of bitstream errors
817 static int decode_coeffs(WMAProDecodeCtx *s, int c)
819 int vlctable;
820 VLC* vlc;
821 WMAProChannelCtx* ci = &s->channel[c];
822 int rl_mode = 0;
823 int cur_coeff = 0;
824 int num_zeros = 0;
825 const uint16_t* run;
826 const int32_t* level;
828 DEBUGF("decode coefficients for channel %i\n", c);
830 vlctable = get_bits1(&s->gb);
831 vlc = &coef_vlc[vlctable];
833 if (vlctable) {
834 run = coef1_run;
835 level = coef1_level;
836 } else {
837 run = coef0_run;
838 level = coef0_level;
841 /** decode vector coefficients (consumes up to 167 bits per iteration for
842 4 vector coded large values) */
843 while (!rl_mode && cur_coeff + 3 < s->subframe_len) {
844 int32_t vals[4];
845 int i;
846 unsigned int idx;
848 idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
850 if (idx == HUFF_VEC4_SIZE - 1) {
851 for (i = 0; i < 4; i += 2) {
852 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
853 if (idx == HUFF_VEC2_SIZE - 1) {
854 int v0, v1;
855 v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
856 if (v0 == HUFF_VEC1_SIZE - 1)
857 v0 += ff_wma_get_large_val(&s->gb);
858 v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
859 if (v1 == HUFF_VEC1_SIZE - 1)
860 v1 += ff_wma_get_large_val(&s->gb);
862 vals[i] = v0;
863 vals[i+1] = v1;
864 } else {
865 vals[i] = symbol_to_vec2[idx] >> 4;
866 vals[i+1] = symbol_to_vec2[idx] & 0xF;
869 } else {
870 vals[0] = symbol_to_vec4[idx] >> 12;
871 vals[1] = (symbol_to_vec4[idx] >> 8) & 0xF;
872 vals[2] = (symbol_to_vec4[idx] >> 4) & 0xF;
873 vals[3] = symbol_to_vec4[idx] & 0xF;
876 /** decode sign */
877 for (i = 0; i < 4; i++) {
878 if (vals[i]) {
879 int sign = get_bits1(&s->gb) - 1;
880 ci->coeffs[cur_coeff] = (sign == -1)? -vals[i]<<17 : vals[i]<<17;
881 num_zeros = 0;
882 } else {
883 ci->coeffs[cur_coeff] = 0;
884 /** switch to run level mode when subframe_len / 128 zeros
885 were found in a row */
886 rl_mode |= (++num_zeros > s->subframe_len >> 8);
888 ++cur_coeff;
892 /** decode run level coded coefficients */
893 if (rl_mode) {
894 memset(&ci->coeffs[cur_coeff], 0,
895 sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
897 if (ff_wma_run_level_decode(&s->gb, vlc,
898 level, run, 1, ci->coeffs,
899 cur_coeff, s->subframe_len,
900 s->subframe_len, s->esc_len, 0))
901 return AVERROR_INVALIDDATA;
904 return 0;
908 *@brief Extract scale factors from the bitstream.
909 *@param s codec context
910 *@return 0 on success, < 0 in case of bitstream errors
912 static int decode_scale_factors(WMAProDecodeCtx* s)
914 int i;
916 /** should never consume more than 5344 bits
917 * MAX_CHANNELS * (1 + MAX_BANDS * 23)
920 for (i = 0; i < s->channels_for_cur_subframe; i++) {
921 int c = s->channel_indexes_for_cur_subframe[i];
922 int* sf;
923 int* sf_end;
924 s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
925 sf_end = s->channel[c].scale_factors + s->num_bands;
927 /** resample scale factors for the new block size
928 * as the scale factors might need to be resampled several times
929 * before some new values are transmitted, a backup of the last
930 * transmitted scale factors is kept in saved_scale_factors
932 if (s->channel[c].reuse_sf) {
933 const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
934 int b;
935 for (b = 0; b < s->num_bands; b++)
936 s->channel[c].scale_factors[b] =
937 s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
940 if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
942 if (!s->channel[c].reuse_sf) {
943 int val;
944 /** decode DPCM coded scale factors */
945 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
946 val = 45 / s->channel[c].scale_factor_step;
947 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
948 val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
949 *sf = val;
951 } else {
952 int i;
953 /** run level decode differences to the resampled factors */
954 for (i = 0; i < s->num_bands; i++) {
955 int idx;
956 int skip;
957 int val;
958 int sign;
960 idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
962 if (!idx) {
963 uint32_t code = get_bits(&s->gb, 14);
964 val = code >> 6;
965 sign = (code & 1) - 1;
966 skip = (code & 0x3f) >> 1;
967 } else if (idx == 1) {
968 break;
969 } else {
970 skip = scale_rl_run[idx];
971 val = scale_rl_level[idx];
972 sign = get_bits1(&s->gb)-1;
975 i += skip;
976 if (i >= s->num_bands) {
977 DEBUGF("invalid scale factor coding\n");
978 return AVERROR_INVALIDDATA;
980 s->channel[c].scale_factors[i] += (val ^ sign) - sign;
984 /** swap buffers */
985 s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
986 s->channel[c].table_idx = s->table_idx;
987 s->channel[c].reuse_sf = 1;
990 /** calculate new scale factor maximum */
991 s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
992 for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
993 s->channel[c].max_scale_factor =
994 FFMAX(s->channel[c].max_scale_factor, *sf);
998 return 0;
1002 *@brief Reconstruct the individual channel data.
1003 *@param s codec context
1005 static void inverse_channel_transform(WMAProDecodeCtx *s)
1007 int i;
1009 for (i = 0; i < s->num_chgroups; i++) {
1010 if (s->chgroup[i].transform) {
1011 const int num_channels = s->chgroup[i].num_channels;
1012 int32_t data[WMAPRO_MAX_CHANNELS];
1013 int32_t** ch_data = s->chgroup[i].channel_data;
1014 int32_t** ch_end = ch_data + num_channels;
1015 const int8_t* tb = s->chgroup[i].transform_band;
1016 int16_t* sfb;
1018 /** multichannel decorrelation */
1019 for (sfb = s->cur_sfb_offsets;
1020 sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
1021 int y;
1022 if (*tb++ == 1) {
1023 /** multiply values with the decorrelation_matrix */
1024 for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
1025 const int32_t* mat = s->chgroup[i].fixdecorrelation_matrix;
1026 const int32_t* data_end = data + num_channels;
1027 int32_t* data_ptr = data;
1028 int32_t** ch;
1030 for (ch = ch_data; ch < ch_end; ch++)
1031 *data_ptr++ = (*ch)[y];
1033 for (ch = ch_data; ch < ch_end; ch++) {
1034 int32_t sum = 0;
1035 data_ptr = data;
1037 while (data_ptr < data_end)
1038 sum += fixmul16(*data_ptr++, *mat++);
1040 (*ch)[y] = sum;
1043 } else if (s->num_channels == 2) {
1045 /* Scale with sqrt(2). 0x016A09E6 = (sqrt(2)*(1<<24)) */
1046 int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1047 vector_fixmul_scalar(ch_data[0] + sfb[0],
1048 ch_data[0] + sfb[0],
1049 0x016A09E6, len);
1050 vector_fixmul_scalar(ch_data[1] + sfb[0],
1051 ch_data[1] + sfb[0],
1052 0x016A09E6, len);
1061 *@brief Apply sine window and reconstruct the output buffer.
1062 *@param s codec context
1064 static void wmapro_window(WMAProDecodeCtx *s)
1066 int i;
1068 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1069 int c = s->channel_indexes_for_cur_subframe[i];
1070 const int32_t* window;
1071 int winlen = s->channel[c].prev_block_len;
1072 int32_t *xstart= s->channel[c].coeffs - (winlen >> 1);
1074 if (s->subframe_len < winlen) {
1075 xstart += (winlen - s->subframe_len) >> 1;
1076 winlen = s->subframe_len;
1079 window = sine_windows[av_log2(winlen) - BLOCK_MIN_BITS];
1081 winlen >>= 1;
1083 vector_fixmul_window(xstart, xstart, xstart + winlen,
1084 window, winlen);
1086 s->channel[c].prev_block_len = s->subframe_len;
1092 *@brief Decode a single subframe (block).
1093 *@param s codec context
1094 *@return 0 on success, < 0 when decoding failed
1096 static int decode_subframe(WMAProDecodeCtx *s)
1098 int offset = s->samples_per_frame;
1099 int subframe_len = s->samples_per_frame;
1100 int i;
1101 int total_samples = s->samples_per_frame * s->num_channels;
1102 int transmit_coeffs = 0;
1103 int cur_subwoofer_cutoff;
1105 s->subframe_offset = get_bits_count(&s->gb);
1107 /** reset channel context and find the next block offset and size
1108 == the next block of the channel with the smallest number of
1109 decoded samples
1111 for (i = 0; i < s->num_channels; i++) {
1112 s->channel[i].grouped = 0;
1113 if (offset > s->channel[i].decoded_samples) {
1114 offset = s->channel[i].decoded_samples;
1115 subframe_len =
1116 s->channel[i].subframe_len[s->channel[i].cur_subframe];
1120 DEBUGF("processing subframe with offset %i len %i\n", offset, subframe_len);
1122 /** get a list of all channels that contain the estimated block */
1123 s->channels_for_cur_subframe = 0;
1124 for (i = 0; i < s->num_channels; i++) {
1125 const int cur_subframe = s->channel[i].cur_subframe;
1126 /** substract already processed samples */
1127 total_samples -= s->channel[i].decoded_samples;
1129 /** and count if there are multiple subframes that match our profile */
1130 if (offset == s->channel[i].decoded_samples &&
1131 subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1132 total_samples -= s->channel[i].subframe_len[cur_subframe];
1133 s->channel[i].decoded_samples +=
1134 s->channel[i].subframe_len[cur_subframe];
1135 s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
1136 ++s->channels_for_cur_subframe;
1140 /** check if the frame will be complete after processing the
1141 estimated block */
1142 if (!total_samples)
1143 s->parsed_all_subframes = 1;
1146 DEBUGF("subframe is part of %i channels\n", s->channels_for_cur_subframe);
1148 /** calculate number of scale factor bands and their offsets */
1149 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
1150 s->num_bands = s->num_sfb[s->table_idx];
1151 s->cur_sfb_offsets = s->sfb_offsets[s->table_idx];
1152 cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
1154 /** configure the decoder for the current subframe */
1155 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1156 int c = s->channel_indexes_for_cur_subframe[i];
1158 s->channel[c].coeffs = &s->channel[c].out[(s->samples_per_frame >> 1)
1159 + offset];
1162 s->subframe_len = subframe_len;
1163 s->esc_len = av_log2(s->subframe_len - 1) + 1;
1165 /** skip extended header if any */
1166 if (get_bits1(&s->gb)) {
1167 int num_fill_bits;
1168 if (!(num_fill_bits = get_bits(&s->gb, 2))) {
1169 int len = get_bits(&s->gb, 4);
1170 num_fill_bits = get_bits(&s->gb, len) + 1;
1173 if (num_fill_bits >= 0) {
1174 if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
1175 DEBUGF("invalid number of fill bits\n");
1176 return AVERROR_INVALIDDATA;
1179 skip_bits_long(&s->gb, num_fill_bits);
1183 /** no idea for what the following bit is used */
1184 if (get_bits1(&s->gb)) {
1185 DEBUGF("reserved bit set\n");
1186 return AVERROR_INVALIDDATA;
1189 if (decode_channel_transform(s) < 0)
1190 return AVERROR_INVALIDDATA;
1192 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1193 int c = s->channel_indexes_for_cur_subframe[i];
1194 if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
1195 transmit_coeffs = 1;
1198 if (transmit_coeffs) {
1199 int step;
1200 int quant_step = 90 * s->bits_per_sample >> 4;
1201 if ((get_bits1(&s->gb))) {
1202 /** FIXME: might change run level mode decision */
1203 DEBUGF("unsupported quant step coding\n");
1204 return AVERROR_INVALIDDATA;
1206 /** decode quantization step */
1207 step = get_sbits(&s->gb, 6);
1208 quant_step += step;
1209 if (step == -32 || step == 31) {
1210 const int sign = (step == 31) - 1;
1211 int quant = 0;
1212 while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
1213 (step = get_bits(&s->gb, 5)) == 31) {
1214 quant += 31;
1216 quant_step += ((quant + step) ^ sign) - sign;
1218 if (quant_step < 0) {
1219 DEBUGF("negative quant step\n");
1222 /** decode quantization step modifiers for every channel */
1224 if (s->channels_for_cur_subframe == 1) {
1225 s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1226 } else {
1227 int modifier_len = get_bits(&s->gb, 3);
1228 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1229 int c = s->channel_indexes_for_cur_subframe[i];
1230 s->channel[c].quant_step = quant_step;
1231 if (get_bits1(&s->gb)) {
1232 if (modifier_len) {
1233 s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
1234 } else
1235 ++s->channel[c].quant_step;
1240 /** decode scale factors */
1241 if (decode_scale_factors(s) < 0)
1242 return AVERROR_INVALIDDATA;
1245 DEBUGF("BITSTREAM: subframe header length was %i\n",
1246 get_bits_count(&s->gb) - s->subframe_offset);
1248 /** parse coefficients */
1249 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1250 int c = s->channel_indexes_for_cur_subframe[i];
1251 if (s->channel[c].transmit_coefs &&
1252 get_bits_count(&s->gb) < s->num_saved_bits) {
1253 decode_coeffs(s, c);
1254 } else {
1255 memset(s->channel[c].coeffs, 0,
1256 sizeof(*s->channel[c].coeffs) * subframe_len);
1260 DEBUGF("BITSTREAM: subframe length was %i\n",
1261 get_bits_count(&s->gb) - s->subframe_offset);
1263 if (transmit_coeffs) {
1264 /** reconstruct the per channel data */
1265 inverse_channel_transform(s);
1266 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1267 int c = s->channel_indexes_for_cur_subframe[i];
1268 const int* sf = s->channel[c].scale_factors;
1269 int b;
1271 if (c == s->lfe_channel)
1272 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
1273 (subframe_len - cur_subwoofer_cutoff));
1275 /** inverse quantization and rescaling */
1276 for (b = 0; b < s->num_bands; b++) {
1277 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
1278 const int exp = s->channel[c].quant_step -
1279 (s->channel[c].max_scale_factor - *sf++) *
1280 s->channel[c].scale_factor_step;
1282 if(exp < EXP_MIN || exp > EXP_MAX) {
1283 DEBUGF("in wmaprodec.c : unhandled value for exp (%d), please report sample.\n", exp);
1284 return -1;
1286 const int32_t quant = QUANT(exp);
1287 int start = s->cur_sfb_offsets[b];
1289 vector_fixmul_scalar(s->tmp+start,
1290 s->channel[c].coeffs + start,
1291 quant, end-start);
1296 /** apply imdct (ff_imdct_half == DCTIV with reverse) */
1297 imdct_half(av_log2(subframe_len)+1,
1298 s->channel[c].coeffs, s->tmp);
1303 /** window and overlapp-add */
1304 wmapro_window(s);
1306 /** handled one subframe */
1307 for (i = 0; i < s->channels_for_cur_subframe; i++) {
1308 int c = s->channel_indexes_for_cur_subframe[i];
1309 if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1310 DEBUGF("broken subframe\n");
1311 return AVERROR_INVALIDDATA;
1313 ++s->channel[c].cur_subframe;
1316 return 0;
1320 *@brief Decode one WMA frame.
1321 *@param s codec context
1322 *@return 0 if the trailer bit indicates that this is the last frame,
1323 * 1 if there are additional frames
1325 static int decode_frame(WMAProDecodeCtx *s)
1327 GetBitContext* gb = &s->gb;
1328 int more_frames = 0;
1329 int len = 0;
1330 int i;
1333 #if 0
1334 /** check for potential output buffer overflow */
1335 /* Rockbox : No need to check that anymore since we work directly on the
1336 buffers in the WMAProDecCtx */
1337 if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
1338 /** return an error if no frame could be decoded at all */
1339 DEBUGF("not enough space for the output samples\n");
1340 s->packet_loss = 1;
1341 return 0;
1343 #endif
1345 /** get frame length */
1346 if (s->len_prefix)
1347 len = get_bits(gb, s->log2_frame_size);
1349 DEBUGF("decoding frame with length %x\n", len);
1351 /** decode tile information */
1352 if (decode_tilehdr(s)) {
1353 s->packet_loss = 1;
1354 return 0;
1357 /** read postproc transform */
1358 if (s->num_channels > 1 && get_bits1(gb)) {
1359 DEBUGF("Unsupported postproc transform found\n");
1360 s->packet_loss = 1;
1361 return 0;
1364 /** read drc info */
1365 if (s->dynamic_range_compression) {
1366 s->drc_gain = get_bits(gb, 8);
1367 DEBUGF("drc_gain %i\n", s->drc_gain);
1370 /** no idea what these are for, might be the number of samples
1371 that need to be skipped at the beginning or end of a stream */
1372 if (get_bits1(gb)) {
1373 int skip;
1375 /** usually true for the first frame */
1376 if (get_bits1(gb)) {
1377 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1378 DEBUGF("start skip: %i\n", skip);
1381 /** sometimes true for the last frame */
1382 if (get_bits1(gb)) {
1383 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1384 DEBUGF("end skip: %i\n", skip);
1389 DEBUGF("BITSTREAM: frame header length was %i\n",
1390 get_bits_count(gb) - s->frame_offset);
1392 /** reset subframe states */
1393 s->parsed_all_subframes = 0;
1394 for (i = 0; i < s->num_channels; i++) {
1395 s->channel[i].decoded_samples = 0;
1396 s->channel[i].cur_subframe = 0;
1397 s->channel[i].reuse_sf = 0;
1400 /** decode all subframes */
1401 while (!s->parsed_all_subframes) {
1402 if (decode_subframe(s) < 0) {
1403 s->packet_loss = 1;
1404 return 0;
1407 s->samples += s->num_channels * s->samples_per_frame;
1409 if (s->skip_frame) {
1410 s->skip_frame = 0;
1411 } else
1412 s->samples += s->num_channels * s->samples_per_frame;
1414 if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1415 /** FIXME: not sure if this is always an error */
1416 DEBUGF("frame[%i] would have to skip %i bits\n",
1417 (int)s->frame_num, len - (get_bits_count(gb) - s->frame_offset) - 1);
1418 s->packet_loss = 1;
1419 return 0;
1422 /** skip the rest of the frame data */
1423 skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1425 /** decode trailer bit */
1426 more_frames = get_bits1(gb);
1428 ++s->frame_num;
1429 return more_frames;
1433 *@brief Calculate remaining input buffer length.
1434 *@param s codec context
1435 *@param gb bitstream reader context
1436 *@return remaining size in bits
1438 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
1440 return s->buf_bit_size - get_bits_count(gb);
1444 *@brief Fill the bit reservoir with a (partial) frame.
1445 *@param s codec context
1446 *@param gb bitstream reader context
1447 *@param len length of the partial frame
1448 *@param append decides wether to reset the buffer or not
1450 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1451 int append)
1453 int buflen;
1455 /** when the frame data does not need to be concatenated, the input buffer
1456 is resetted and additional bits from the previous frame are copyed
1457 and skipped later so that a fast byte copy is possible */
1459 if (!append) {
1460 s->frame_offset = get_bits_count(gb) & 7;
1461 s->num_saved_bits = s->frame_offset;
1462 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1465 buflen = (s->num_saved_bits + len + 8) >> 3;
1467 if (len <= 0 || buflen > MAX_FRAMESIZE) {
1468 DEBUGF("input buffer too small\n");
1469 s->packet_loss = 1;
1470 return;
1473 s->num_saved_bits += len;
1474 if (!append) {
1475 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1476 s->num_saved_bits);
1477 } else {
1478 int align = 8 - (get_bits_count(gb) & 7);
1479 align = FFMIN(align, len);
1480 put_bits(&s->pb, align, get_bits(gb, align));
1481 len -= align;
1482 ff_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1484 skip_bits_long(gb, len);
1487 PutBitContext tmp = s->pb;
1488 flush_put_bits(&tmp);
1491 init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1492 skip_bits(&s->gb, s->frame_offset);
1496 *@brief Decode a single WMA packet.
1497 *@param avctx codec context
1498 *@param data the output buffer
1499 *@param data_size number of bytes that were written to the output buffer
1500 *@param avpkt input packet
1501 *@return number of bytes that were read from the input buffer
1503 int decode_packet(asf_waveformatex_t *wfx, int32_t *dec[2], int *data_size,
1504 void* pktdata, int size)
1506 WMAProDecodeCtx *s = &globWMAProDecCtx;
1507 GetBitContext* gb = &s->pgb;
1508 const uint8_t* buf = pktdata;
1509 int buf_size = size;
1510 int num_bits_prev_frame;
1511 int packet_sequence_number;\
1512 int i;
1514 /** reuse second half of the IMDCT output for the next frame */
1515 /* NOTE : Relies on the WMAProDecCtx being static */
1516 for(i = 0; i < s->num_channels; i++)
1517 memcpy(&s->channel[i].out[0],
1518 &s->channel[i].out[s->samples_per_frame],
1519 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
1522 s->samples = 0;
1523 *data_size = 0;
1525 if (s->packet_done || s->packet_loss) {
1526 s->packet_done = 0;
1527 s->buf_bit_size = buf_size << 3;
1529 /** sanity check for the buffer length */
1530 if (buf_size < wfx->blockalign)
1531 return 0;
1533 buf_size = wfx->blockalign;
1535 /** parse packet header */
1536 init_get_bits(gb, buf, s->buf_bit_size);
1537 packet_sequence_number = get_bits(gb, 4);
1538 skip_bits(gb, 2);
1540 /** get number of bits that need to be added to the previous frame */
1541 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1542 DEBUGF("packet[%d]: nbpf %x\n", s->frame_num,
1543 num_bits_prev_frame);
1545 /** check for packet loss */
1546 if (!s->packet_loss &&
1547 ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1548 s->packet_loss = 1;
1549 DEBUGF("Packet loss detected! seq %x vs %x\n",
1550 s->packet_sequence_number, packet_sequence_number);
1552 s->packet_sequence_number = packet_sequence_number;
1554 if (num_bits_prev_frame > 0) {
1555 /** append the previous frame data to the remaining data from the
1556 previous packet to create a full frame */
1557 save_bits(s, gb, num_bits_prev_frame, 1);
1558 DEBUGF("accumulated %x bits of frame data\n",
1559 s->num_saved_bits - s->frame_offset);
1561 /** decode the cross packet frame if it is valid */
1562 if (!s->packet_loss)
1563 decode_frame(s);
1564 } else if (s->num_saved_bits - s->frame_offset) {
1565 DEBUGF("ignoring %x previously saved bits\n",
1566 s->num_saved_bits - s->frame_offset);
1569 s->packet_loss = 0;
1571 } else {
1572 int frame_size;
1573 s->buf_bit_size = size << 3;
1574 init_get_bits(gb, pktdata, s->buf_bit_size);
1575 skip_bits(gb, s->packet_offset);
1576 if (remaining_bits(s, gb) > s->log2_frame_size &&
1577 (frame_size = show_bits(gb, s->log2_frame_size)) &&
1578 frame_size <= remaining_bits(s, gb)) {
1579 save_bits(s, gb, frame_size, 0);
1580 s->packet_done = !decode_frame(s);
1581 } else
1582 s->packet_done = 1;
1585 if (s->packet_done && !s->packet_loss &&
1586 remaining_bits(s, gb) > 0) {
1587 /** save the rest of the data so that it can be decoded
1588 with the next packet */
1589 save_bits(s, gb, remaining_bits(s, gb), 0);
1592 dec[0] = s->channel[0].out;
1593 dec[1] = s->channel[1].out;
1595 *data_size = s->samples;
1596 s->packet_offset = get_bits_count(gb) & 7;
1598 s->frame_num++;
1599 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1602 #if 0
1604 *@brief wmapro decoder
1606 AVCodec wmapro_decoder = {
1607 "wmapro",
1608 AVMEDIA_TYPE_AUDIO,
1609 CODEC_ID_WMAPRO,
1610 sizeof(WMAProDecodeCtx),
1611 decode_init,
1612 NULL,
1613 decode_end,
1614 decode_packet,
1615 .capabilities = CODEC_CAP_SUBFRAMES,
1616 .flush= flush,
1617 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
1619 #endif