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