]> git.sesse.net Git - ffmpeg/blob - libavcodec/wmaprodec.c
avfilter/af_dynaudnorm: remove wasteful pow
[ffmpeg] / libavcodec / wmaprodec.c
1 /*
2  * Wmapro compatible decoder
3  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
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
33  *
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.
42  *
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.
46  *
47  * Example wmapro bitstream (in samples):
48  *
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  * ---------------------------------------------------
57  *
58  * The frame layouts for the individual channels of a wma frame does not need
59  * to be the same.
60  *
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.
71  *
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.
87  */
88
89 #include <inttypes.h>
90
91 #include "libavutil/float_dsp.h"
92 #include "libavutil/intfloat.h"
93 #include "libavutil/intreadwrite.h"
94 #include "avcodec.h"
95 #include "internal.h"
96 #include "get_bits.h"
97 #include "put_bits.h"
98 #include "wmaprodata.h"
99 #include "sinewin.h"
100 #include "wma.h"
101 #include "wma_common.h"
102
103 /** current decoder limitations */
104 #define WMAPRO_MAX_CHANNELS    8                             ///< max number of handled channels
105 #define MAX_SUBFRAMES  32                                    ///< max number of subframes per channel
106 #define MAX_BANDS      29                                    ///< max number of scale factor bands
107 #define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
108
109 #define WMAPRO_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
110 #define WMAPRO_BLOCK_MAX_BITS 13                                           ///< log2 of max block size
111 #define WMAPRO_BLOCK_MIN_SIZE (1 << WMAPRO_BLOCK_MIN_BITS)                 ///< minimum block size
112 #define WMAPRO_BLOCK_MAX_SIZE (1 << WMAPRO_BLOCK_MAX_BITS)                 ///< maximum block size
113 #define WMAPRO_BLOCK_SIZES    (WMAPRO_BLOCK_MAX_BITS - WMAPRO_BLOCK_MIN_BITS + 1) ///< possible block sizes
114
115
116 #define VLCBITS            9
117 #define SCALEVLCBITS       8
118 #define VEC4MAXDEPTH    ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
119 #define VEC2MAXDEPTH    ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
120 #define VEC1MAXDEPTH    ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
121 #define SCALEMAXDEPTH   ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
122 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
123
124 static VLC              sf_vlc;           ///< scale factor DPCM vlc
125 static VLC              sf_rl_vlc;        ///< scale factor run length vlc
126 static VLC              vec4_vlc;         ///< 4 coefficients per symbol
127 static VLC              vec2_vlc;         ///< 2 coefficients per symbol
128 static VLC              vec1_vlc;         ///< 1 coefficient per symbol
129 static VLC              coef_vlc[2];      ///< coefficient run length vlc codes
130 static float            sin64[33];        ///< sine table for decorrelation
131
132 /**
133  * @brief frame specific decoder context for a single channel
134  */
135 typedef struct WMAProChannelCtx {
136     int16_t  prev_block_len;                          ///< length of the previous block
137     uint8_t  transmit_coefs;
138     uint8_t  num_subframes;
139     uint16_t subframe_len[MAX_SUBFRAMES];             ///< subframe length in samples
140     uint16_t subframe_offset[MAX_SUBFRAMES];          ///< subframe positions in the current frame
141     uint8_t  cur_subframe;                            ///< current subframe number
142     uint16_t decoded_samples;                         ///< number of already processed samples
143     uint8_t  grouped;                                 ///< channel is part of a group
144     int      quant_step;                              ///< quantization step for the current subframe
145     int8_t   reuse_sf;                                ///< share scale factors between subframes
146     int8_t   scale_factor_step;                       ///< scaling step for the current subframe
147     int      max_scale_factor;                        ///< maximum scale factor for the current subframe
148     int      saved_scale_factors[2][MAX_BANDS];       ///< resampled and (previously) transmitted scale factor values
149     int8_t   scale_factor_idx;                        ///< index for the transmitted scale factor values (used for resampling)
150     int*     scale_factors;                           ///< pointer to the scale factor values used for decoding
151     uint8_t  table_idx;                               ///< index in sf_offsets for the scale factor reference block
152     float*   coeffs;                                  ///< pointer to the subframe decode buffer
153     uint16_t num_vec_coeffs;                          ///< number of vector coded coefficients
154     DECLARE_ALIGNED(32, float, out)[WMAPRO_BLOCK_MAX_SIZE + WMAPRO_BLOCK_MAX_SIZE / 2]; ///< output buffer
155 } WMAProChannelCtx;
156
157 /**
158  * @brief channel group for channel transformations
159  */
160 typedef struct WMAProChannelGrp {
161     uint8_t num_channels;                                     ///< number of channels in the group
162     int8_t  transform;                                        ///< transform on / off
163     int8_t  transform_band[MAX_BANDS];                        ///< controls if the transform is enabled for a certain band
164     float   decorrelation_matrix[WMAPRO_MAX_CHANNELS*WMAPRO_MAX_CHANNELS];
165     float*  channel_data[WMAPRO_MAX_CHANNELS];                ///< transformation coefficients
166 } WMAProChannelGrp;
167
168 /**
169  * @brief main decoder context
170  */
171 typedef struct WMAProDecodeCtx {
172     /* generic decoder variables */
173     AVCodecContext*  avctx;                         ///< codec context for av_log
174     AVFloatDSPContext *fdsp;
175     uint8_t          frame_data[MAX_FRAMESIZE +
176                       AV_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
177     PutBitContext    pb;                            ///< context for filling the frame_data buffer
178     FFTContext       mdct_ctx[WMAPRO_BLOCK_SIZES];  ///< MDCT context per block size
179     DECLARE_ALIGNED(32, float, tmp)[WMAPRO_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
180     const float*     windows[WMAPRO_BLOCK_SIZES];   ///< windows for the different block sizes
181
182     /* frame size dependent frame information (set during initialization) */
183     uint32_t         decode_flags;                  ///< used compression features
184     uint8_t          len_prefix;                    ///< frame is prefixed with its length
185     uint8_t          dynamic_range_compression;     ///< frame contains DRC data
186     uint8_t          bits_per_sample;               ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
187     uint16_t         samples_per_frame;             ///< number of samples to output
188     uint16_t         log2_frame_size;
189     int8_t           lfe_channel;                   ///< lfe channel index
190     uint8_t          max_num_subframes;
191     uint8_t          subframe_len_bits;             ///< number of bits used for the subframe length
192     uint8_t          max_subframe_len_bit;          ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
193     uint16_t         min_samples_per_subframe;
194     int8_t           num_sfb[WMAPRO_BLOCK_SIZES];   ///< scale factor bands per block size
195     int16_t          sfb_offsets[WMAPRO_BLOCK_SIZES][MAX_BANDS];                    ///< scale factor band offsets (multiples of 4)
196     int8_t           sf_offsets[WMAPRO_BLOCK_SIZES][WMAPRO_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
197     int16_t          subwoofer_cutoffs[WMAPRO_BLOCK_SIZES]; ///< subwoofer cutoff values
198
199     /* packet decode state */
200     GetBitContext    pgb;                           ///< bitstream reader context for the packet
201     int              next_packet_start;             ///< start offset of the next wma packet in the demuxer packet
202     uint8_t          packet_offset;                 ///< frame offset in the packet
203     uint8_t          packet_sequence_number;        ///< current packet number
204     int              num_saved_bits;                ///< saved number of bits
205     int              frame_offset;                  ///< frame offset in the bit reservoir
206     int              subframe_offset;               ///< subframe offset in the bit reservoir
207     uint8_t          packet_loss;                   ///< set in case of bitstream error
208     uint8_t          packet_done;                   ///< set when a packet is fully decoded
209     uint8_t          skip_packets;
210
211     /* frame decode state */
212     uint32_t         frame_num;                     ///< current frame number (not used for decoding)
213     int              num_frames;
214     GetBitContext    gb;                            ///< bitstream reader context
215     int              buf_bit_size;                  ///< buffer size in bits
216     uint8_t          drc_gain;                      ///< gain for the DRC tool
217     int8_t           skip_frame;                    ///< skip output step
218     int8_t           parsed_all_subframes;          ///< all subframes decoded?
219
220     /* subframe/block decode state */
221     int16_t          subframe_len;                  ///< current subframe length
222     int8_t           channels_for_cur_subframe;     ///< number of channels that contain the subframe
223     int8_t           channel_indexes_for_cur_subframe[WMAPRO_MAX_CHANNELS];
224     int8_t           num_bands;                     ///< number of scale factor bands
225     int8_t           transmit_num_vec_coeffs;       ///< number of vector coded coefficients is part of the bitstream
226     int16_t*         cur_sfb_offsets;               ///< sfb offsets for the current block
227     uint8_t          table_idx;                     ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
228     int8_t           esc_len;                       ///< length of escaped coefficients
229
230     uint8_t          num_chgroups;                  ///< number of channel groups
231     WMAProChannelGrp chgroup[WMAPRO_MAX_CHANNELS];  ///< channel group information
232
233     WMAProChannelCtx channel[WMAPRO_MAX_CHANNELS];  ///< per channel data
234 } WMAProDecodeCtx;
235
236
237 /**
238  *@brief helper function to print the most important members of the context
239  *@param s context
240  */
241 static av_cold void dump_context(WMAProDecodeCtx *s)
242 {
243 #define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
244 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %"PRIx32"\n", a, b);
245
246     PRINT("ed sample bit depth", s->bits_per_sample);
247     PRINT_HEX("ed decode flags", s->decode_flags);
248     PRINT("samples per frame",   s->samples_per_frame);
249     PRINT("log2 frame size",     s->log2_frame_size);
250     PRINT("max num subframes",   s->max_num_subframes);
251     PRINT("len prefix",          s->len_prefix);
252     PRINT("num channels",        s->avctx->channels);
253 }
254
255 /**
256  *@brief Uninitialize the decoder and free all resources.
257  *@param avctx codec context
258  *@return 0 on success, < 0 otherwise
259  */
260 static av_cold int decode_end(AVCodecContext *avctx)
261 {
262     WMAProDecodeCtx *s = avctx->priv_data;
263     int i;
264
265     av_freep(&s->fdsp);
266
267     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
268         ff_mdct_end(&s->mdct_ctx[i]);
269
270     return 0;
271 }
272
273 static av_cold int get_rate(AVCodecContext *avctx)
274 {
275     if (avctx->codec_id != AV_CODEC_ID_WMAPRO) { // XXX: is this really only for XMA?
276         if (avctx->sample_rate > 44100)
277             return 48000;
278         else if (avctx->sample_rate > 32000)
279             return 44100;
280         else if (avctx->sample_rate > 24000)
281             return 32000;
282         return 24000;
283     }
284
285     return avctx->sample_rate;
286 }
287
288 /**
289  *@brief Initialize the decoder.
290  *@param avctx codec context
291  *@return 0 on success, -1 otherwise
292  */
293 static av_cold int decode_init(AVCodecContext *avctx)
294 {
295     WMAProDecodeCtx *s = avctx->priv_data;
296     uint8_t *edata_ptr = avctx->extradata;
297     unsigned int channel_mask;
298     int i, bits;
299     int log2_max_num_subframes;
300     int num_possible_block_sizes;
301
302     if (avctx->codec_id == AV_CODEC_ID_XMA1 || avctx->codec_id == AV_CODEC_ID_XMA2)
303         avctx->block_align = 2048;
304
305     if (!avctx->block_align) {
306         av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
307         return AVERROR(EINVAL);
308     }
309
310     s->avctx = avctx;
311     s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
312     if (!s->fdsp)
313         return AVERROR(ENOMEM);
314
315     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
316
317     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
318
319     if (avctx->codec_id == AV_CODEC_ID_XMA2 && avctx->extradata_size >= 34) {
320         s->decode_flags    = 0x10d6;
321         channel_mask       = AV_RL32(edata_ptr+2);
322         s->bits_per_sample = 16;
323         /** dump the extradata */
324         for (i = 0; i < avctx->extradata_size; i++)
325             ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
326         ff_dlog(avctx, "\n");
327
328      } else if (avctx->codec_id == AV_CODEC_ID_XMA1 && avctx->extradata_size >= 28) {
329         s->decode_flags    = 0x10d6;
330         s->bits_per_sample = 16;
331         channel_mask       = 0;
332         /** dump the extradata */
333         for (i = 0; i < avctx->extradata_size; i++)
334             ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
335         ff_dlog(avctx, "\n");
336
337      } else if (avctx->extradata_size >= 18) {
338         s->decode_flags    = AV_RL16(edata_ptr+14);
339         channel_mask       = AV_RL32(edata_ptr+2);
340         s->bits_per_sample = AV_RL16(edata_ptr);
341         /** dump the extradata */
342         for (i = 0; i < avctx->extradata_size; i++)
343             ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
344         ff_dlog(avctx, "\n");
345
346     } else {
347         avpriv_request_sample(avctx, "Unknown extradata size");
348         return AVERROR_PATCHWELCOME;
349     }
350
351     if (avctx->codec_id != AV_CODEC_ID_WMAPRO && avctx->channels > 2) {
352         avpriv_report_missing_feature(avctx, ">2 channels support");
353         return AVERROR_PATCHWELCOME;
354     }
355
356     /** generic init */
357     s->log2_frame_size = av_log2(avctx->block_align) + 4;
358     if (s->log2_frame_size > 25) {
359         avpriv_request_sample(avctx, "Large block align");
360         return AVERROR_PATCHWELCOME;
361     }
362
363     /** frame info */
364     if (avctx->codec_id != AV_CODEC_ID_WMAPRO)
365         s->skip_frame = 0;
366     else
367         s->skip_frame = 1; /* skip first frame */
368
369     s->packet_loss = 1;
370     s->len_prefix  = (s->decode_flags & 0x40);
371
372     /** get frame len */
373     if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
374         bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
375         if (bits > WMAPRO_BLOCK_MAX_BITS) {
376             avpriv_request_sample(avctx, "14-bit block sizes");
377             return AVERROR_PATCHWELCOME;
378         }
379         s->samples_per_frame = 1 << bits;
380     } else {
381         s->samples_per_frame = 512;
382     }
383
384     /** subframe info */
385     log2_max_num_subframes       = ((s->decode_flags & 0x38) >> 3);
386     s->max_num_subframes         = 1 << log2_max_num_subframes;
387     if (s->max_num_subframes == 16 || s->max_num_subframes == 4)
388         s->max_subframe_len_bit = 1;
389     s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
390
391     num_possible_block_sizes     = log2_max_num_subframes + 1;
392     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
393     s->dynamic_range_compression = (s->decode_flags & 0x80);
394
395     if (s->max_num_subframes > MAX_SUBFRAMES) {
396         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRId8"\n",
397                s->max_num_subframes);
398         return AVERROR_INVALIDDATA;
399     }
400
401     if (s->min_samples_per_subframe < WMAPRO_BLOCK_MIN_SIZE) {
402         av_log(avctx, AV_LOG_ERROR, "min_samples_per_subframe of %d too small\n",
403                s->min_samples_per_subframe);
404         return AVERROR_INVALIDDATA;
405     }
406
407     if (s->avctx->sample_rate <= 0) {
408         av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");
409         return AVERROR_INVALIDDATA;
410     }
411
412     if (avctx->channels < 0) {
413         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
414                avctx->channels);
415         return AVERROR_INVALIDDATA;
416     } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
417         avpriv_request_sample(avctx,
418                               "More than %d channels", WMAPRO_MAX_CHANNELS);
419         return AVERROR_PATCHWELCOME;
420     }
421
422     /** init previous block len */
423     for (i = 0; i < avctx->channels; i++)
424         s->channel[i].prev_block_len = s->samples_per_frame;
425
426     /** extract lfe channel position */
427     s->lfe_channel = -1;
428
429     if (channel_mask & 8) {
430         unsigned int mask;
431         for (mask = 1; mask < 16; mask <<= 1) {
432             if (channel_mask & mask)
433                 ++s->lfe_channel;
434         }
435     }
436
437     INIT_VLC_STATIC(&sf_vlc, SCALEVLCBITS, HUFF_SCALE_SIZE,
438                     scale_huffbits, 1, 1,
439                     scale_huffcodes, 2, 2, 616);
440
441     INIT_VLC_STATIC(&sf_rl_vlc, VLCBITS, HUFF_SCALE_RL_SIZE,
442                     scale_rl_huffbits, 1, 1,
443                     scale_rl_huffcodes, 4, 4, 1406);
444
445     INIT_VLC_STATIC(&coef_vlc[0], VLCBITS, HUFF_COEF0_SIZE,
446                     coef0_huffbits, 1, 1,
447                     coef0_huffcodes, 4, 4, 2108);
448
449     INIT_VLC_STATIC(&coef_vlc[1], VLCBITS, HUFF_COEF1_SIZE,
450                     coef1_huffbits, 1, 1,
451                     coef1_huffcodes, 4, 4, 3912);
452
453     INIT_VLC_STATIC(&vec4_vlc, VLCBITS, HUFF_VEC4_SIZE,
454                     vec4_huffbits, 1, 1,
455                     vec4_huffcodes, 2, 2, 604);
456
457     INIT_VLC_STATIC(&vec2_vlc, VLCBITS, HUFF_VEC2_SIZE,
458                     vec2_huffbits, 1, 1,
459                     vec2_huffcodes, 2, 2, 562);
460
461     INIT_VLC_STATIC(&vec1_vlc, VLCBITS, HUFF_VEC1_SIZE,
462                     vec1_huffbits, 1, 1,
463                     vec1_huffcodes, 2, 2, 562);
464
465     /** calculate number of scale factor bands and their offsets
466         for every possible block size */
467     for (i = 0; i < num_possible_block_sizes; i++) {
468         int subframe_len = s->samples_per_frame >> i;
469         int x;
470         int band = 1;
471         int rate = get_rate(avctx);
472
473         s->sfb_offsets[i][0] = 0;
474
475         for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
476             int offset = (subframe_len * 2 * critical_freq[x]) / rate + 2;
477             offset &= ~3;
478             if (offset > s->sfb_offsets[i][band - 1])
479                 s->sfb_offsets[i][band++] = offset;
480
481             if (offset >= subframe_len)
482                 break;
483         }
484         s->sfb_offsets[i][band - 1] = subframe_len;
485         s->num_sfb[i]               = band - 1;
486         if (s->num_sfb[i] <= 0) {
487             av_log(avctx, AV_LOG_ERROR, "num_sfb invalid\n");
488             return AVERROR_INVALIDDATA;
489         }
490     }
491
492
493     /** Scale factors can be shared between blocks of different size
494         as every block has a different scale factor band layout.
495         The matrix sf_offsets is needed to find the correct scale factor.
496      */
497
498     for (i = 0; i < num_possible_block_sizes; i++) {
499         int b;
500         for (b = 0; b < s->num_sfb[i]; b++) {
501             int x;
502             int offset = ((s->sfb_offsets[i][b]
503                            + s->sfb_offsets[i][b + 1] - 1) << i) >> 1;
504             for (x = 0; x < num_possible_block_sizes; x++) {
505                 int v = 0;
506                 while (s->sfb_offsets[x][v + 1] << x < offset) {
507                     v++;
508                     av_assert0(v < MAX_BANDS);
509                 }
510                 s->sf_offsets[i][x][b] = v;
511             }
512         }
513     }
514
515     /** init MDCT, FIXME: only init needed sizes */
516     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++)
517         ff_mdct_init(&s->mdct_ctx[i], WMAPRO_BLOCK_MIN_BITS+1+i, 1,
518                      1.0 / (1 << (WMAPRO_BLOCK_MIN_BITS + i - 1))
519                      / (1 << (s->bits_per_sample - 1)));
520
521     /** init MDCT windows: simple sine window */
522     for (i = 0; i < WMAPRO_BLOCK_SIZES; i++) {
523         const int win_idx = WMAPRO_BLOCK_MAX_BITS - i;
524         ff_init_ff_sine_windows(win_idx);
525         s->windows[WMAPRO_BLOCK_SIZES - i - 1] = ff_sine_windows[win_idx];
526     }
527
528     /** calculate subwoofer cutoff values */
529     for (i = 0; i < num_possible_block_sizes; i++) {
530         int block_size = s->samples_per_frame >> i;
531         int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1)
532                      / s->avctx->sample_rate;
533         s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
534     }
535
536     /** calculate sine values for the decorrelation matrix */
537     for (i = 0; i < 33; i++)
538         sin64[i] = sin(i*M_PI / 64.0);
539
540     if (avctx->debug & FF_DEBUG_BITSTREAM)
541         dump_context(s);
542
543     avctx->channel_layout = channel_mask;
544
545     return 0;
546 }
547
548 /**
549  *@brief Decode the subframe length.
550  *@param s context
551  *@param offset sample offset in the frame
552  *@return decoded subframe length on success, < 0 in case of an error
553  */
554 static int decode_subframe_length(WMAProDecodeCtx *s, int offset)
555 {
556     int frame_len_shift = 0;
557     int subframe_len;
558
559     /** no need to read from the bitstream when only one length is possible */
560     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
561         return s->min_samples_per_subframe;
562
563     if (get_bits_left(&s->gb) < 1)
564         return AVERROR_INVALIDDATA;
565
566     /** 1 bit indicates if the subframe is of maximum length */
567     if (s->max_subframe_len_bit) {
568         if (get_bits1(&s->gb))
569             frame_len_shift = 1 + get_bits(&s->gb, s->subframe_len_bits-1);
570     } else
571         frame_len_shift = get_bits(&s->gb, s->subframe_len_bits);
572
573     subframe_len = s->samples_per_frame >> frame_len_shift;
574
575     /** sanity check the length */
576     if (subframe_len < s->min_samples_per_subframe ||
577         subframe_len > s->samples_per_frame) {
578         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
579                subframe_len);
580         return AVERROR_INVALIDDATA;
581     }
582     return subframe_len;
583 }
584
585 /**
586  *@brief Decode how the data in the frame is split into subframes.
587  *       Every WMA frame contains the encoded data for a fixed number of
588  *       samples per channel. The data for every channel might be split
589  *       into several subframes. This function will reconstruct the list of
590  *       subframes for every channel.
591  *
592  *       If the subframes are not evenly split, the algorithm estimates the
593  *       channels with the lowest number of total samples.
594  *       Afterwards, for each of these channels a bit is read from the
595  *       bitstream that indicates if the channel contains a subframe with the
596  *       next subframe size that is going to be read from the bitstream or not.
597  *       If a channel contains such a subframe, the subframe size gets added to
598  *       the channel's subframe list.
599  *       The algorithm repeats these steps until the frame is properly divided
600  *       between the individual channels.
601  *
602  *@param s context
603  *@return 0 on success, < 0 in case of an error
604  */
605 static int decode_tilehdr(WMAProDecodeCtx *s)
606 {
607     uint16_t num_samples[WMAPRO_MAX_CHANNELS] = { 0 };/**< sum of samples for all currently known subframes of a channel */
608     uint8_t  contains_subframe[WMAPRO_MAX_CHANNELS];  /**< flag indicating if a channel contains the current subframe */
609     int channels_for_cur_subframe = s->avctx->channels; /**< number of channels that contain the current subframe */
610     int fixed_channel_layout = 0;                     /**< flag indicating that all channels use the same subframe offsets and sizes */
611     int min_channel_len = 0;                          /**< smallest sum of samples (channels with this length will be processed first) */
612     int c;
613
614     /* Should never consume more than 3073 bits (256 iterations for the
615      * while loop when always the minimum amount of 128 samples is subtracted
616      * from missing samples in the 8 channel case).
617      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
618      */
619
620     /** reset tiling information */
621     for (c = 0; c < s->avctx->channels; c++)
622         s->channel[c].num_subframes = 0;
623
624     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
625         fixed_channel_layout = 1;
626
627     /** loop until the frame data is split between the subframes */
628     do {
629         int subframe_len;
630
631         /** check which channels contain the subframe */
632         for (c = 0; c < s->avctx->channels; c++) {
633             if (num_samples[c] == min_channel_len) {
634                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
635                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe))
636                     contains_subframe[c] = 1;
637                 else
638                     contains_subframe[c] = get_bits1(&s->gb);
639             } else
640                 contains_subframe[c] = 0;
641         }
642
643         /** get subframe length, subframe_len == 0 is not allowed */
644         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
645             return AVERROR_INVALIDDATA;
646
647         /** add subframes to the individual channels and find new min_channel_len */
648         min_channel_len += subframe_len;
649         for (c = 0; c < s->avctx->channels; c++) {
650             WMAProChannelCtx* chan = &s->channel[c];
651
652             if (contains_subframe[c]) {
653                 if (chan->num_subframes >= MAX_SUBFRAMES) {
654                     av_log(s->avctx, AV_LOG_ERROR,
655                            "broken frame: num subframes > 31\n");
656                     return AVERROR_INVALIDDATA;
657                 }
658                 chan->subframe_len[chan->num_subframes] = subframe_len;
659                 num_samples[c] += subframe_len;
660                 ++chan->num_subframes;
661                 if (num_samples[c] > s->samples_per_frame) {
662                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
663                            "channel len > samples_per_frame\n");
664                     return AVERROR_INVALIDDATA;
665                 }
666             } else if (num_samples[c] <= min_channel_len) {
667                 if (num_samples[c] < min_channel_len) {
668                     channels_for_cur_subframe = 0;
669                     min_channel_len = num_samples[c];
670                 }
671                 ++channels_for_cur_subframe;
672             }
673         }
674     } while (min_channel_len < s->samples_per_frame);
675
676     for (c = 0; c < s->avctx->channels; c++) {
677         int i;
678         int offset = 0;
679         for (i = 0; i < s->channel[c].num_subframes; i++) {
680             ff_dlog(s->avctx, "frame[%i] channel[%i] subframe[%i]"
681                     " len %i\n", s->frame_num, c, i,
682                     s->channel[c].subframe_len[i]);
683             s->channel[c].subframe_offset[i] = offset;
684             offset += s->channel[c].subframe_len[i];
685         }
686     }
687
688     return 0;
689 }
690
691 /**
692  *@brief Calculate a decorrelation matrix from the bitstream parameters.
693  *@param s codec context
694  *@param chgroup channel group for which the matrix needs to be calculated
695  */
696 static void decode_decorrelation_matrix(WMAProDecodeCtx *s,
697                                         WMAProChannelGrp *chgroup)
698 {
699     int i;
700     int offset = 0;
701     int8_t rotation_offset[WMAPRO_MAX_CHANNELS * WMAPRO_MAX_CHANNELS];
702     memset(chgroup->decorrelation_matrix, 0, s->avctx->channels *
703            s->avctx->channels * sizeof(*chgroup->decorrelation_matrix));
704
705     for (i = 0; i < chgroup->num_channels * (chgroup->num_channels - 1) >> 1; i++)
706         rotation_offset[i] = get_bits(&s->gb, 6);
707
708     for (i = 0; i < chgroup->num_channels; i++)
709         chgroup->decorrelation_matrix[chgroup->num_channels * i + i] =
710             get_bits1(&s->gb) ? 1.0 : -1.0;
711
712     for (i = 1; i < chgroup->num_channels; i++) {
713         int x;
714         for (x = 0; x < i; x++) {
715             int y;
716             for (y = 0; y < i + 1; y++) {
717                 float v1 = chgroup->decorrelation_matrix[x * chgroup->num_channels + y];
718                 float v2 = chgroup->decorrelation_matrix[i * chgroup->num_channels + y];
719                 int n = rotation_offset[offset + x];
720                 float sinv;
721                 float cosv;
722
723                 if (n < 32) {
724                     sinv = sin64[n];
725                     cosv = sin64[32 - n];
726                 } else {
727                     sinv =  sin64[64 -  n];
728                     cosv = -sin64[n  - 32];
729                 }
730
731                 chgroup->decorrelation_matrix[y + x * chgroup->num_channels] =
732                                                (v1 * sinv) - (v2 * cosv);
733                 chgroup->decorrelation_matrix[y + i * chgroup->num_channels] =
734                                                (v1 * cosv) + (v2 * sinv);
735             }
736         }
737         offset += i;
738     }
739 }
740
741 /**
742  *@brief Decode channel transformation parameters
743  *@param s codec context
744  *@return >= 0 in case of success, < 0 in case of bitstream errors
745  */
746 static int decode_channel_transform(WMAProDecodeCtx* s)
747 {
748     int i;
749     /* should never consume more than 1921 bits for the 8 channel case
750      * 1 + MAX_CHANNELS * (MAX_CHANNELS + 2 + 3 * MAX_CHANNELS * MAX_CHANNELS
751      * + MAX_CHANNELS + MAX_BANDS + 1)
752      */
753
754     /** in the one channel case channel transforms are pointless */
755     s->num_chgroups = 0;
756     if (s->avctx->channels > 1) {
757         int remaining_channels = s->channels_for_cur_subframe;
758
759         if (get_bits1(&s->gb)) {
760             avpriv_request_sample(s->avctx,
761                                   "Channel transform bit");
762             return AVERROR_PATCHWELCOME;
763         }
764
765         for (s->num_chgroups = 0; remaining_channels &&
766              s->num_chgroups < s->channels_for_cur_subframe; s->num_chgroups++) {
767             WMAProChannelGrp* chgroup = &s->chgroup[s->num_chgroups];
768             float** channel_data = chgroup->channel_data;
769             chgroup->num_channels = 0;
770             chgroup->transform = 0;
771
772             /** decode channel mask */
773             if (remaining_channels > 2) {
774                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
775                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
776                     if (!s->channel[channel_idx].grouped
777                         && get_bits1(&s->gb)) {
778                         ++chgroup->num_channels;
779                         s->channel[channel_idx].grouped = 1;
780                         *channel_data++ = s->channel[channel_idx].coeffs;
781                     }
782                 }
783             } else {
784                 chgroup->num_channels = remaining_channels;
785                 for (i = 0; i < s->channels_for_cur_subframe; i++) {
786                     int channel_idx = s->channel_indexes_for_cur_subframe[i];
787                     if (!s->channel[channel_idx].grouped)
788                         *channel_data++ = s->channel[channel_idx].coeffs;
789                     s->channel[channel_idx].grouped = 1;
790                 }
791             }
792
793             /** decode transform type */
794             if (chgroup->num_channels == 2) {
795                 if (get_bits1(&s->gb)) {
796                     if (get_bits1(&s->gb)) {
797                         avpriv_request_sample(s->avctx,
798                                               "Unknown channel transform type");
799                         return AVERROR_PATCHWELCOME;
800                     }
801                 } else {
802                     chgroup->transform = 1;
803                     if (s->avctx->channels == 2) {
804                         chgroup->decorrelation_matrix[0] =  1.0;
805                         chgroup->decorrelation_matrix[1] = -1.0;
806                         chgroup->decorrelation_matrix[2] =  1.0;
807                         chgroup->decorrelation_matrix[3] =  1.0;
808                     } else {
809                         /** cos(pi/4) */
810                         chgroup->decorrelation_matrix[0] =  0.70703125;
811                         chgroup->decorrelation_matrix[1] = -0.70703125;
812                         chgroup->decorrelation_matrix[2] =  0.70703125;
813                         chgroup->decorrelation_matrix[3] =  0.70703125;
814                     }
815                 }
816             } else if (chgroup->num_channels > 2) {
817                 if (get_bits1(&s->gb)) {
818                     chgroup->transform = 1;
819                     if (get_bits1(&s->gb)) {
820                         decode_decorrelation_matrix(s, chgroup);
821                     } else {
822                         /** FIXME: more than 6 coupled channels not supported */
823                         if (chgroup->num_channels > 6) {
824                             avpriv_request_sample(s->avctx,
825                                                   "Coupled channels > 6");
826                         } else {
827                             memcpy(chgroup->decorrelation_matrix,
828                                    default_decorrelation[chgroup->num_channels],
829                                    chgroup->num_channels * chgroup->num_channels *
830                                    sizeof(*chgroup->decorrelation_matrix));
831                         }
832                     }
833                 }
834             }
835
836             /** decode transform on / off */
837             if (chgroup->transform) {
838                 if (!get_bits1(&s->gb)) {
839                     int i;
840                     /** transform can be enabled for individual bands */
841                     for (i = 0; i < s->num_bands; i++) {
842                         chgroup->transform_band[i] = get_bits1(&s->gb);
843                     }
844                 } else {
845                     memset(chgroup->transform_band, 1, s->num_bands);
846                 }
847             }
848             remaining_channels -= chgroup->num_channels;
849         }
850     }
851     return 0;
852 }
853
854 /**
855  *@brief Extract the coefficients from the bitstream.
856  *@param s codec context
857  *@param c current channel number
858  *@return 0 on success, < 0 in case of bitstream errors
859  */
860 static int decode_coeffs(WMAProDecodeCtx *s, int c)
861 {
862     /* Integers 0..15 as single-precision floats.  The table saves a
863        costly int to float conversion, and storing the values as
864        integers allows fast sign-flipping. */
865     static const uint32_t fval_tab[16] = {
866         0x00000000, 0x3f800000, 0x40000000, 0x40400000,
867         0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,
868         0x41000000, 0x41100000, 0x41200000, 0x41300000,
869         0x41400000, 0x41500000, 0x41600000, 0x41700000,
870     };
871     int vlctable;
872     VLC* vlc;
873     WMAProChannelCtx* ci = &s->channel[c];
874     int rl_mode = 0;
875     int cur_coeff = 0;
876     int num_zeros = 0;
877     const uint16_t* run;
878     const float* level;
879
880     ff_dlog(s->avctx, "decode coefficients for channel %i\n", c);
881
882     vlctable = get_bits1(&s->gb);
883     vlc = &coef_vlc[vlctable];
884
885     if (vlctable) {
886         run = coef1_run;
887         level = coef1_level;
888     } else {
889         run = coef0_run;
890         level = coef0_level;
891     }
892
893     /** decode vector coefficients (consumes up to 167 bits per iteration for
894       4 vector coded large values) */
895     while ((s->transmit_num_vec_coeffs || !rl_mode) &&
896            (cur_coeff + 3 < ci->num_vec_coeffs)) {
897         uint32_t vals[4];
898         int i;
899         unsigned int idx;
900
901         idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);
902
903         if (idx == HUFF_VEC4_SIZE - 1) {
904             for (i = 0; i < 4; i += 2) {
905                 idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);
906                 if (idx == HUFF_VEC2_SIZE - 1) {
907                     uint32_t v0, v1;
908                     v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
909                     if (v0 == HUFF_VEC1_SIZE - 1)
910                         v0 += ff_wma_get_large_val(&s->gb);
911                     v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);
912                     if (v1 == HUFF_VEC1_SIZE - 1)
913                         v1 += ff_wma_get_large_val(&s->gb);
914                     vals[i  ] = av_float2int(v0);
915                     vals[i+1] = av_float2int(v1);
916                 } else {
917                     vals[i]   = fval_tab[symbol_to_vec2[idx] >> 4 ];
918                     vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];
919                 }
920             }
921         } else {
922             vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12      ];
923             vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];
924             vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];
925             vals[3] = fval_tab[ symbol_to_vec4[idx]       & 0xF];
926         }
927
928         /** decode sign */
929         for (i = 0; i < 4; i++) {
930             if (vals[i]) {
931                 uint32_t sign = get_bits1(&s->gb) - 1;
932                 AV_WN32A(&ci->coeffs[cur_coeff], vals[i] ^ sign << 31);
933                 num_zeros = 0;
934             } else {
935                 ci->coeffs[cur_coeff] = 0;
936                 /** switch to run level mode when subframe_len / 128 zeros
937                     were found in a row */
938                 rl_mode |= (++num_zeros > s->subframe_len >> 8);
939             }
940             ++cur_coeff;
941         }
942     }
943
944     /** decode run level coded coefficients */
945     if (cur_coeff < s->subframe_len) {
946         memset(&ci->coeffs[cur_coeff], 0,
947                sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));
948         if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,
949                                     level, run, 1, ci->coeffs,
950                                     cur_coeff, s->subframe_len,
951                                     s->subframe_len, s->esc_len, 0))
952             return AVERROR_INVALIDDATA;
953     }
954
955     return 0;
956 }
957
958 /**
959  *@brief Extract scale factors from the bitstream.
960  *@param s codec context
961  *@return 0 on success, < 0 in case of bitstream errors
962  */
963 static int decode_scale_factors(WMAProDecodeCtx* s)
964 {
965     int i;
966
967     /** should never consume more than 5344 bits
968      *  MAX_CHANNELS * (1 +  MAX_BANDS * 23)
969      */
970
971     for (i = 0; i < s->channels_for_cur_subframe; i++) {
972         int c = s->channel_indexes_for_cur_subframe[i];
973         int* sf;
974         int* sf_end;
975         s->channel[c].scale_factors = s->channel[c].saved_scale_factors[!s->channel[c].scale_factor_idx];
976         sf_end = s->channel[c].scale_factors + s->num_bands;
977
978         /** resample scale factors for the new block size
979          *  as the scale factors might need to be resampled several times
980          *  before some  new values are transmitted, a backup of the last
981          *  transmitted scale factors is kept in saved_scale_factors
982          */
983         if (s->channel[c].reuse_sf) {
984             const int8_t* sf_offsets = s->sf_offsets[s->table_idx][s->channel[c].table_idx];
985             int b;
986             for (b = 0; b < s->num_bands; b++)
987                 s->channel[c].scale_factors[b] =
988                     s->channel[c].saved_scale_factors[s->channel[c].scale_factor_idx][*sf_offsets++];
989         }
990
991         if (!s->channel[c].cur_subframe || get_bits1(&s->gb)) {
992
993             if (!s->channel[c].reuse_sf) {
994                 int val;
995                 /** decode DPCM coded scale factors */
996                 s->channel[c].scale_factor_step = get_bits(&s->gb, 2) + 1;
997                 val = 45 / s->channel[c].scale_factor_step;
998                 for (sf = s->channel[c].scale_factors; sf < sf_end; sf++) {
999                     val += get_vlc2(&s->gb, sf_vlc.table, SCALEVLCBITS, SCALEMAXDEPTH) - 60;
1000                     *sf = val;
1001                 }
1002             } else {
1003                 int i;
1004                 /** run level decode differences to the resampled factors */
1005                 for (i = 0; i < s->num_bands; i++) {
1006                     int idx;
1007                     int skip;
1008                     int val;
1009                     int sign;
1010
1011                     idx = get_vlc2(&s->gb, sf_rl_vlc.table, VLCBITS, SCALERLMAXDEPTH);
1012
1013                     if (!idx) {
1014                         uint32_t code = get_bits(&s->gb, 14);
1015                         val  =  code >> 6;
1016                         sign = (code & 1) - 1;
1017                         skip = (code & 0x3f) >> 1;
1018                     } else if (idx == 1) {
1019                         break;
1020                     } else {
1021                         skip = scale_rl_run[idx];
1022                         val  = scale_rl_level[idx];
1023                         sign = get_bits1(&s->gb)-1;
1024                     }
1025
1026                     i += skip;
1027                     if (i >= s->num_bands) {
1028                         av_log(s->avctx, AV_LOG_ERROR,
1029                                "invalid scale factor coding\n");
1030                         return AVERROR_INVALIDDATA;
1031                     }
1032                     s->channel[c].scale_factors[i] += (val ^ sign) - sign;
1033                 }
1034             }
1035             /** swap buffers */
1036             s->channel[c].scale_factor_idx = !s->channel[c].scale_factor_idx;
1037             s->channel[c].table_idx = s->table_idx;
1038             s->channel[c].reuse_sf  = 1;
1039         }
1040
1041         /** calculate new scale factor maximum */
1042         s->channel[c].max_scale_factor = s->channel[c].scale_factors[0];
1043         for (sf = s->channel[c].scale_factors + 1; sf < sf_end; sf++) {
1044             s->channel[c].max_scale_factor =
1045                 FFMAX(s->channel[c].max_scale_factor, *sf);
1046         }
1047
1048     }
1049     return 0;
1050 }
1051
1052 /**
1053  *@brief Reconstruct the individual channel data.
1054  *@param s codec context
1055  */
1056 static void inverse_channel_transform(WMAProDecodeCtx *s)
1057 {
1058     int i;
1059
1060     for (i = 0; i < s->num_chgroups; i++) {
1061         if (s->chgroup[i].transform) {
1062             float data[WMAPRO_MAX_CHANNELS];
1063             const int num_channels = s->chgroup[i].num_channels;
1064             float** ch_data = s->chgroup[i].channel_data;
1065             float** ch_end = ch_data + num_channels;
1066             const int8_t* tb = s->chgroup[i].transform_band;
1067             int16_t* sfb;
1068
1069             /** multichannel decorrelation */
1070             for (sfb = s->cur_sfb_offsets;
1071                  sfb < s->cur_sfb_offsets + s->num_bands; sfb++) {
1072                 int y;
1073                 if (*tb++ == 1) {
1074                     /** multiply values with the decorrelation_matrix */
1075                     for (y = sfb[0]; y < FFMIN(sfb[1], s->subframe_len); y++) {
1076                         const float* mat = s->chgroup[i].decorrelation_matrix;
1077                         const float* data_end = data + num_channels;
1078                         float* data_ptr = data;
1079                         float** ch;
1080
1081                         for (ch = ch_data; ch < ch_end; ch++)
1082                             *data_ptr++ = (*ch)[y];
1083
1084                         for (ch = ch_data; ch < ch_end; ch++) {
1085                             float sum = 0;
1086                             data_ptr = data;
1087                             while (data_ptr < data_end)
1088                                 sum += *data_ptr++ * *mat++;
1089
1090                             (*ch)[y] = sum;
1091                         }
1092                     }
1093                 } else if (s->avctx->channels == 2) {
1094                     int len = FFMIN(sfb[1], s->subframe_len) - sfb[0];
1095                     s->fdsp->vector_fmul_scalar(ch_data[0] + sfb[0],
1096                                                ch_data[0] + sfb[0],
1097                                                181.0 / 128, len);
1098                     s->fdsp->vector_fmul_scalar(ch_data[1] + sfb[0],
1099                                                ch_data[1] + sfb[0],
1100                                                181.0 / 128, len);
1101                 }
1102             }
1103         }
1104     }
1105 }
1106
1107 /**
1108  *@brief Apply sine window and reconstruct the output buffer.
1109  *@param s codec context
1110  */
1111 static void wmapro_window(WMAProDecodeCtx *s)
1112 {
1113     int i;
1114     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1115         int c = s->channel_indexes_for_cur_subframe[i];
1116         const float* window;
1117         int winlen = s->channel[c].prev_block_len;
1118         float* start = s->channel[c].coeffs - (winlen >> 1);
1119
1120         if (s->subframe_len < winlen) {
1121             start += (winlen - s->subframe_len) >> 1;
1122             winlen = s->subframe_len;
1123         }
1124
1125         window = s->windows[av_log2(winlen) - WMAPRO_BLOCK_MIN_BITS];
1126
1127         winlen >>= 1;
1128
1129         s->fdsp->vector_fmul_window(start, start, start + winlen,
1130                                    window, winlen);
1131
1132         s->channel[c].prev_block_len = s->subframe_len;
1133     }
1134 }
1135
1136 /**
1137  *@brief Decode a single subframe (block).
1138  *@param s codec context
1139  *@return 0 on success, < 0 when decoding failed
1140  */
1141 static int decode_subframe(WMAProDecodeCtx *s)
1142 {
1143     int offset = s->samples_per_frame;
1144     int subframe_len = s->samples_per_frame;
1145     int i;
1146     int total_samples   = s->samples_per_frame * s->avctx->channels;
1147     int transmit_coeffs = 0;
1148     int cur_subwoofer_cutoff;
1149
1150     s->subframe_offset = get_bits_count(&s->gb);
1151
1152     /** reset channel context and find the next block offset and size
1153         == the next block of the channel with the smallest number of
1154         decoded samples
1155     */
1156     for (i = 0; i < s->avctx->channels; i++) {
1157         s->channel[i].grouped = 0;
1158         if (offset > s->channel[i].decoded_samples) {
1159             offset = s->channel[i].decoded_samples;
1160             subframe_len =
1161                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
1162         }
1163     }
1164
1165     ff_dlog(s->avctx,
1166             "processing subframe with offset %i len %i\n", offset, subframe_len);
1167
1168     /** get a list of all channels that contain the estimated block */
1169     s->channels_for_cur_subframe = 0;
1170     for (i = 0; i < s->avctx->channels; i++) {
1171         const int cur_subframe = s->channel[i].cur_subframe;
1172         /** subtract already processed samples */
1173         total_samples -= s->channel[i].decoded_samples;
1174
1175         /** and count if there are multiple subframes that match our profile */
1176         if (offset == s->channel[i].decoded_samples &&
1177             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
1178             total_samples -= s->channel[i].subframe_len[cur_subframe];
1179             s->channel[i].decoded_samples +=
1180                 s->channel[i].subframe_len[cur_subframe];
1181             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
1182             ++s->channels_for_cur_subframe;
1183         }
1184     }
1185
1186     /** check if the frame will be complete after processing the
1187         estimated block */
1188     if (!total_samples)
1189         s->parsed_all_subframes = 1;
1190
1191
1192     ff_dlog(s->avctx, "subframe is part of %i channels\n",
1193             s->channels_for_cur_subframe);
1194
1195     /** calculate number of scale factor bands and their offsets */
1196     s->table_idx         = av_log2(s->samples_per_frame/subframe_len);
1197     s->num_bands         = s->num_sfb[s->table_idx];
1198     s->cur_sfb_offsets   = s->sfb_offsets[s->table_idx];
1199     cur_subwoofer_cutoff = s->subwoofer_cutoffs[s->table_idx];
1200
1201     /** configure the decoder for the current subframe */
1202     offset += s->samples_per_frame >> 1;
1203
1204     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1205         int c = s->channel_indexes_for_cur_subframe[i];
1206
1207         s->channel[c].coeffs = &s->channel[c].out[offset];
1208     }
1209
1210     s->subframe_len = subframe_len;
1211     s->esc_len = av_log2(s->subframe_len - 1) + 1;
1212
1213     /** skip extended header if any */
1214     if (get_bits1(&s->gb)) {
1215         int num_fill_bits;
1216         if (!(num_fill_bits = get_bits(&s->gb, 2))) {
1217             int len = get_bits(&s->gb, 4);
1218             num_fill_bits = (len ? get_bits(&s->gb, len) : 0) + 1;
1219         }
1220
1221         if (num_fill_bits >= 0) {
1222             if (get_bits_count(&s->gb) + num_fill_bits > s->num_saved_bits) {
1223                 av_log(s->avctx, AV_LOG_ERROR, "invalid number of fill bits\n");
1224                 return AVERROR_INVALIDDATA;
1225             }
1226
1227             skip_bits_long(&s->gb, num_fill_bits);
1228         }
1229     }
1230
1231     /** no idea for what the following bit is used */
1232     if (get_bits1(&s->gb)) {
1233         avpriv_request_sample(s->avctx, "Reserved bit");
1234         return AVERROR_PATCHWELCOME;
1235     }
1236
1237
1238     if (decode_channel_transform(s) < 0)
1239         return AVERROR_INVALIDDATA;
1240
1241
1242     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1243         int c = s->channel_indexes_for_cur_subframe[i];
1244         if ((s->channel[c].transmit_coefs = get_bits1(&s->gb)))
1245             transmit_coeffs = 1;
1246     }
1247
1248     av_assert0(s->subframe_len <= WMAPRO_BLOCK_MAX_SIZE);
1249     if (transmit_coeffs) {
1250         int step;
1251         int quant_step = 90 * s->bits_per_sample >> 4;
1252
1253         /** decode number of vector coded coefficients */
1254         if ((s->transmit_num_vec_coeffs = get_bits1(&s->gb))) {
1255             int num_bits = av_log2((s->subframe_len + 3)/4) + 1;
1256             for (i = 0; i < s->channels_for_cur_subframe; i++) {
1257                 int c = s->channel_indexes_for_cur_subframe[i];
1258                 int num_vec_coeffs = get_bits(&s->gb, num_bits) << 2;
1259                 if (num_vec_coeffs > s->subframe_len) {
1260                     av_log(s->avctx, AV_LOG_ERROR, "num_vec_coeffs %d is too large\n", num_vec_coeffs);
1261                     return AVERROR_INVALIDDATA;
1262                 }
1263                 av_assert0(num_vec_coeffs + offset <= FF_ARRAY_ELEMS(s->channel[c].out));
1264                 s->channel[c].num_vec_coeffs = num_vec_coeffs;
1265             }
1266         } else {
1267             for (i = 0; i < s->channels_for_cur_subframe; i++) {
1268                 int c = s->channel_indexes_for_cur_subframe[i];
1269                 s->channel[c].num_vec_coeffs = s->subframe_len;
1270             }
1271         }
1272         /** decode quantization step */
1273         step = get_sbits(&s->gb, 6);
1274         quant_step += step;
1275         if (step == -32 || step == 31) {
1276             const int sign = (step == 31) - 1;
1277             int quant = 0;
1278             while (get_bits_count(&s->gb) + 5 < s->num_saved_bits &&
1279                    (step = get_bits(&s->gb, 5)) == 31) {
1280                 quant += 31;
1281             }
1282             quant_step += ((quant + step) ^ sign) - sign;
1283         }
1284         if (quant_step < 0) {
1285             av_log(s->avctx, AV_LOG_DEBUG, "negative quant step\n");
1286         }
1287
1288         /** decode quantization step modifiers for every channel */
1289
1290         if (s->channels_for_cur_subframe == 1) {
1291             s->channel[s->channel_indexes_for_cur_subframe[0]].quant_step = quant_step;
1292         } else {
1293             int modifier_len = get_bits(&s->gb, 3);
1294             for (i = 0; i < s->channels_for_cur_subframe; i++) {
1295                 int c = s->channel_indexes_for_cur_subframe[i];
1296                 s->channel[c].quant_step = quant_step;
1297                 if (get_bits1(&s->gb)) {
1298                     if (modifier_len) {
1299                         s->channel[c].quant_step += get_bits(&s->gb, modifier_len) + 1;
1300                     } else
1301                         ++s->channel[c].quant_step;
1302                 }
1303             }
1304         }
1305
1306         /** decode scale factors */
1307         if (decode_scale_factors(s) < 0)
1308             return AVERROR_INVALIDDATA;
1309     }
1310
1311     ff_dlog(s->avctx, "BITSTREAM: subframe header length was %i\n",
1312             get_bits_count(&s->gb) - s->subframe_offset);
1313
1314     /** parse coefficients */
1315     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1316         int c = s->channel_indexes_for_cur_subframe[i];
1317         if (s->channel[c].transmit_coefs &&
1318             get_bits_count(&s->gb) < s->num_saved_bits) {
1319             decode_coeffs(s, c);
1320         } else
1321             memset(s->channel[c].coeffs, 0,
1322                    sizeof(*s->channel[c].coeffs) * subframe_len);
1323     }
1324
1325     ff_dlog(s->avctx, "BITSTREAM: subframe length was %i\n",
1326             get_bits_count(&s->gb) - s->subframe_offset);
1327
1328     if (transmit_coeffs) {
1329         FFTContext *mdct = &s->mdct_ctx[av_log2(subframe_len) - WMAPRO_BLOCK_MIN_BITS];
1330         /** reconstruct the per channel data */
1331         inverse_channel_transform(s);
1332         for (i = 0; i < s->channels_for_cur_subframe; i++) {
1333             int c = s->channel_indexes_for_cur_subframe[i];
1334             const int* sf = s->channel[c].scale_factors;
1335             int b;
1336
1337             if (c == s->lfe_channel)
1338                 memset(&s->tmp[cur_subwoofer_cutoff], 0, sizeof(*s->tmp) *
1339                        (subframe_len - cur_subwoofer_cutoff));
1340
1341             /** inverse quantization and rescaling */
1342             for (b = 0; b < s->num_bands; b++) {
1343                 const int end = FFMIN(s->cur_sfb_offsets[b+1], s->subframe_len);
1344                 const int exp = s->channel[c].quant_step -
1345                             (s->channel[c].max_scale_factor - *sf++) *
1346                             s->channel[c].scale_factor_step;
1347                 const float quant = pow(10.0, exp / 20.0);
1348                 int start = s->cur_sfb_offsets[b];
1349                 s->fdsp->vector_fmul_scalar(s->tmp + start,
1350                                            s->channel[c].coeffs + start,
1351                                            quant, end - start);
1352             }
1353
1354             /** apply imdct (imdct_half == DCTIV with reverse) */
1355             mdct->imdct_half(mdct, s->channel[c].coeffs, s->tmp);
1356         }
1357     }
1358
1359     /** window and overlapp-add */
1360     wmapro_window(s);
1361
1362     /** handled one subframe */
1363     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1364         int c = s->channel_indexes_for_cur_subframe[i];
1365         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1366             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1367             return AVERROR_INVALIDDATA;
1368         }
1369         ++s->channel[c].cur_subframe;
1370     }
1371
1372     return 0;
1373 }
1374
1375 /**
1376  *@brief Decode one WMA frame.
1377  *@param s codec context
1378  *@return 0 if the trailer bit indicates that this is the last frame,
1379  *        1 if there are additional frames
1380  */
1381 static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr)
1382 {
1383     AVCodecContext *avctx = s->avctx;
1384     GetBitContext* gb = &s->gb;
1385     int more_frames = 0;
1386     int len = 0;
1387     int i, ret;
1388
1389     /** get frame length */
1390     if (s->len_prefix)
1391         len = get_bits(gb, s->log2_frame_size);
1392
1393     ff_dlog(s->avctx, "decoding frame with length %x\n", len);
1394
1395     /** decode tile information */
1396     if (decode_tilehdr(s)) {
1397         s->packet_loss = 1;
1398         return 0;
1399     }
1400
1401     /** read postproc transform */
1402     if (s->avctx->channels > 1 && get_bits1(gb)) {
1403         if (get_bits1(gb)) {
1404             for (i = 0; i < avctx->channels * avctx->channels; i++)
1405                 skip_bits(gb, 4);
1406         }
1407     }
1408
1409     /** read drc info */
1410     if (s->dynamic_range_compression) {
1411         s->drc_gain = get_bits(gb, 8);
1412         ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
1413     }
1414
1415     /** no idea what these are for, might be the number of samples
1416         that need to be skipped at the beginning or end of a stream */
1417     if (get_bits1(gb)) {
1418         int av_unused skip;
1419
1420         /** usually true for the first frame */
1421         if (get_bits1(gb)) {
1422             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1423             ff_dlog(s->avctx, "start skip: %i\n", skip);
1424         }
1425
1426         /** sometimes true for the last frame */
1427         if (get_bits1(gb)) {
1428             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1429             ff_dlog(s->avctx, "end skip: %i\n", skip);
1430         }
1431
1432     }
1433
1434     ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n",
1435             get_bits_count(gb) - s->frame_offset);
1436
1437     /** reset subframe states */
1438     s->parsed_all_subframes = 0;
1439     for (i = 0; i < avctx->channels; i++) {
1440         s->channel[i].decoded_samples = 0;
1441         s->channel[i].cur_subframe    = 0;
1442         s->channel[i].reuse_sf        = 0;
1443     }
1444
1445     /** decode all subframes */
1446     while (!s->parsed_all_subframes) {
1447         if (decode_subframe(s) < 0) {
1448             s->packet_loss = 1;
1449             return 0;
1450         }
1451     }
1452
1453     /* get output buffer */
1454     frame->nb_samples = s->samples_per_frame;
1455     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1456         s->packet_loss = 1;
1457         return 0;
1458     }
1459
1460     /** copy samples to the output buffer */
1461     for (i = 0; i < avctx->channels; i++)
1462         memcpy(frame->extended_data[i], s->channel[i].out,
1463                s->samples_per_frame * sizeof(*s->channel[i].out));
1464
1465     for (i = 0; i < avctx->channels; i++) {
1466         /** reuse second half of the IMDCT output for the next frame */
1467         memcpy(&s->channel[i].out[0],
1468                &s->channel[i].out[s->samples_per_frame],
1469                s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
1470     }
1471
1472     if (s->skip_frame) {
1473         s->skip_frame = 0;
1474         *got_frame_ptr = 0;
1475         av_frame_unref(frame);
1476     } else {
1477         *got_frame_ptr = 1;
1478     }
1479
1480     if (s->len_prefix) {
1481         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1482             /** FIXME: not sure if this is always an error */
1483             av_log(s->avctx, AV_LOG_ERROR,
1484                    "frame[%"PRIu32"] would have to skip %i bits\n",
1485                    s->frame_num,
1486                    len - (get_bits_count(gb) - s->frame_offset) - 1);
1487             s->packet_loss = 1;
1488             return 0;
1489         }
1490
1491         /** skip the rest of the frame data */
1492         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1493     } else {
1494         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
1495         }
1496     }
1497
1498     /** decode trailer bit */
1499     more_frames = get_bits1(gb);
1500
1501     ++s->frame_num;
1502     return more_frames;
1503 }
1504
1505 /**
1506  *@brief Calculate remaining input buffer length.
1507  *@param s codec context
1508  *@param gb bitstream reader context
1509  *@return remaining size in bits
1510  */
1511 static int remaining_bits(WMAProDecodeCtx *s, GetBitContext *gb)
1512 {
1513     return s->buf_bit_size - get_bits_count(gb);
1514 }
1515
1516 /**
1517  *@brief Fill the bit reservoir with a (partial) frame.
1518  *@param s codec context
1519  *@param gb bitstream reader context
1520  *@param len length of the partial frame
1521  *@param append decides whether to reset the buffer or not
1522  */
1523 static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1524                       int append)
1525 {
1526     int buflen;
1527
1528     /** when the frame data does not need to be concatenated, the input buffer
1529         is reset and additional bits from the previous frame are copied
1530         and skipped later so that a fast byte copy is possible */
1531
1532     if (!append) {
1533         s->frame_offset = get_bits_count(gb) & 7;
1534         s->num_saved_bits = s->frame_offset;
1535         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1536     }
1537
1538     buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
1539
1540     if (len <= 0 || buflen > MAX_FRAMESIZE) {
1541         avpriv_request_sample(s->avctx, "Too small input buffer");
1542         s->packet_loss = 1;
1543         return;
1544     }
1545
1546     av_assert0(len <= put_bits_left(&s->pb));
1547
1548     s->num_saved_bits += len;
1549     if (!append) {
1550         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1551                      s->num_saved_bits);
1552     } else {
1553         int align = 8 - (get_bits_count(gb) & 7);
1554         align = FFMIN(align, len);
1555         put_bits(&s->pb, align, get_bits(gb, align));
1556         len -= align;
1557         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1558     }
1559     skip_bits_long(gb, len);
1560
1561     {
1562         PutBitContext tmp = s->pb;
1563         flush_put_bits(&tmp);
1564     }
1565
1566     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1567     skip_bits(&s->gb, s->frame_offset);
1568 }
1569
1570 /**
1571  *@brief Decode a single WMA packet.
1572  *@param avctx codec context
1573  *@param data the output buffer
1574  *@param avpkt input packet
1575  *@return number of bytes that were read from the input buffer
1576  */
1577 static int decode_packet(AVCodecContext *avctx, void *data,
1578                          int *got_frame_ptr, AVPacket* avpkt)
1579 {
1580     WMAProDecodeCtx *s = avctx->priv_data;
1581     GetBitContext* gb  = &s->pgb;
1582     const uint8_t* buf = avpkt->data;
1583     int buf_size       = avpkt->size;
1584     int num_bits_prev_frame;
1585     int packet_sequence_number;
1586
1587     *got_frame_ptr = 0;
1588
1589     if (s->skip_packets > 0) {
1590         s->skip_packets--;
1591         return FFMIN(avpkt->size, avctx->block_align);
1592     }
1593
1594     if (s->packet_done || s->packet_loss) {
1595         s->packet_done = 0;
1596
1597         /** sanity check for the buffer length */
1598         if (avctx->codec_id == AV_CODEC_ID_WMAPRO && buf_size < avctx->block_align) {
1599             av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
1600                    buf_size, avctx->block_align);
1601             return AVERROR_INVALIDDATA;
1602         }
1603
1604         if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
1605             s->next_packet_start = buf_size - avctx->block_align;
1606             buf_size = avctx->block_align;
1607         } else {
1608             s->next_packet_start = buf_size - FFMIN(buf_size, avctx->block_align);
1609             buf_size = FFMIN(buf_size, avctx->block_align);
1610         }
1611         s->buf_bit_size = buf_size << 3;
1612
1613         /** parse packet header */
1614         init_get_bits(gb, buf, s->buf_bit_size);
1615         if (avctx->codec_id != AV_CODEC_ID_XMA2) {
1616             packet_sequence_number = get_bits(gb, 4);
1617             skip_bits(gb, 2);
1618         } else {
1619             s->num_frames = get_bits(gb, 6);
1620             packet_sequence_number = 0;
1621         }
1622
1623         /** get number of bits that need to be added to the previous frame */
1624         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1625         if (avctx->codec_id != AV_CODEC_ID_WMAPRO) {
1626             skip_bits(gb, 3);
1627             s->skip_packets = get_bits(gb, 8);
1628         }
1629
1630         ff_dlog(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number,
1631                 num_bits_prev_frame);
1632
1633         /** check for packet loss */
1634         if (avctx->codec_id != AV_CODEC_ID_XMA2 && !s->packet_loss &&
1635             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1636             s->packet_loss = 1;
1637             av_log(avctx, AV_LOG_ERROR,
1638                    "Packet loss detected! seq %"PRIx8" vs %x\n",
1639                    s->packet_sequence_number, packet_sequence_number);
1640         }
1641         s->packet_sequence_number = packet_sequence_number;
1642
1643         if (num_bits_prev_frame > 0) {
1644             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1645             if (num_bits_prev_frame >= remaining_packet_bits) {
1646                 num_bits_prev_frame = remaining_packet_bits;
1647                 s->packet_done = 1;
1648             }
1649
1650             /** append the previous frame data to the remaining data from the
1651                 previous packet to create a full frame */
1652             save_bits(s, gb, num_bits_prev_frame, 1);
1653             ff_dlog(avctx, "accumulated %x bits of frame data\n",
1654                     s->num_saved_bits - s->frame_offset);
1655
1656             /** decode the cross packet frame if it is valid */
1657             if (!s->packet_loss)
1658                 decode_frame(s, data, got_frame_ptr);
1659         } else if (s->num_saved_bits - s->frame_offset) {
1660             ff_dlog(avctx, "ignoring %x previously saved bits\n",
1661                     s->num_saved_bits - s->frame_offset);
1662         }
1663
1664         if (s->packet_loss) {
1665             /** reset number of saved bits so that the decoder
1666                 does not start to decode incomplete frames in the
1667                 s->len_prefix == 0 case */
1668             s->num_saved_bits = 0;
1669             s->packet_loss = 0;
1670         }
1671
1672     } else {
1673         int frame_size;
1674         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1675         init_get_bits(gb, avpkt->data, s->buf_bit_size);
1676         skip_bits(gb, s->packet_offset);
1677         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1678             (frame_size = show_bits(gb, s->log2_frame_size)) &&
1679             frame_size <= remaining_bits(s, gb)) {
1680             save_bits(s, gb, frame_size, 0);
1681             if (!s->packet_loss)
1682                 s->packet_done = !decode_frame(s, data, got_frame_ptr);
1683         } else if (!s->len_prefix
1684                    && s->num_saved_bits > get_bits_count(&s->gb)) {
1685             /** when the frames do not have a length prefix, we don't know
1686                 the compressed length of the individual frames
1687                 however, we know what part of a new packet belongs to the
1688                 previous frame
1689                 therefore we save the incoming packet first, then we append
1690                 the "previous frame" data from the next packet so that
1691                 we get a buffer that only contains full frames */
1692             s->packet_done = !decode_frame(s, data, got_frame_ptr);
1693         } else
1694             s->packet_done = 1;
1695     }
1696
1697     if (remaining_bits(s, gb) < 0) {
1698         av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -remaining_bits(s, gb));
1699         s->packet_loss = 1;
1700     }
1701
1702     if (s->packet_done && !s->packet_loss &&
1703         remaining_bits(s, gb) > 0) {
1704         /** save the rest of the data so that it can be decoded
1705             with the next packet */
1706         save_bits(s, gb, remaining_bits(s, gb), 0);
1707     }
1708
1709     s->packet_offset = get_bits_count(gb) & 7;
1710     if (s->packet_loss)
1711         return AVERROR_INVALIDDATA;
1712
1713     return get_bits_count(gb) >> 3;
1714 }
1715
1716 /**
1717  *@brief Clear decoder buffers (for seeking).
1718  *@param avctx codec context
1719  */
1720 static void flush(AVCodecContext *avctx)
1721 {
1722     WMAProDecodeCtx *s = avctx->priv_data;
1723     int i;
1724     /** reset output buffer as a part of it is used during the windowing of a
1725         new frame */
1726     for (i = 0; i < avctx->channels; i++)
1727         memset(s->channel[i].out, 0, s->samples_per_frame *
1728                sizeof(*s->channel[i].out));
1729     s->packet_loss = 1;
1730 }
1731
1732
1733 /**
1734  *@brief wmapro decoder
1735  */
1736 AVCodec ff_wmapro_decoder = {
1737     .name           = "wmapro",
1738     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Professional"),
1739     .type           = AVMEDIA_TYPE_AUDIO,
1740     .id             = AV_CODEC_ID_WMAPRO,
1741     .priv_data_size = sizeof(WMAProDecodeCtx),
1742     .init           = decode_init,
1743     .close          = decode_end,
1744     .decode         = decode_packet,
1745     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1746     .flush          = flush,
1747     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1748                                                       AV_SAMPLE_FMT_NONE },
1749 };
1750
1751 AVCodec ff_xma1_decoder = {
1752     .name           = "xma1",
1753     .long_name      = NULL_IF_CONFIG_SMALL("Xbox Media Audio 1"),
1754     .type           = AVMEDIA_TYPE_AUDIO,
1755     .id             = AV_CODEC_ID_XMA1,
1756     .priv_data_size = sizeof(WMAProDecodeCtx),
1757     .init           = decode_init,
1758     .close          = decode_end,
1759     .decode         = decode_packet,
1760     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1761     .flush          = flush,
1762     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1763                                                       AV_SAMPLE_FMT_NONE },
1764 };
1765
1766 AVCodec ff_xma2_decoder = {
1767     .name           = "xma2",
1768     .long_name      = NULL_IF_CONFIG_SMALL("Xbox Media Audio 2"),
1769     .type           = AVMEDIA_TYPE_AUDIO,
1770     .id             = AV_CODEC_ID_XMA2,
1771     .priv_data_size = sizeof(WMAProDecodeCtx),
1772     .init           = decode_init,
1773     .close          = decode_end,
1774     .decode         = decode_packet,
1775     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
1776     .flush          = flush,
1777     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1778                                                       AV_SAMPLE_FMT_NONE },
1779 };