]> git.sesse.net Git - ffmpeg/blob - libavcodec/wmalosslessdec.c
Clean-up
[ffmpeg] / libavcodec / wmalosslessdec.c
1 /*
2  * Wmall compatible decoder
3  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5  * Copyright (c) 2011 Andreas Ă–man
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * @brief wmall decoder implementation
27  * Wmall is an MDCT based codec comparable to wma standard or AAC.
28  * The decoding therefore consists of the following steps:
29  * - bitstream decoding
30  * - reconstruction of per-channel data
31  * - rescaling and inverse quantization
32  * - IMDCT
33  * - windowing and overlapp-add
34  *
35  * The compressed wmall bitstream is split into individual packets.
36  * Every such packet contains one or more wma frames.
37  * The compressed frames may have a variable length and frames may
38  * cross packet boundaries.
39  * Common to all wmall frames is the number of samples that are stored in
40  * a frame.
41  * The number of samples and a few other decode flags are stored
42  * as extradata that has to be passed to the decoder.
43  *
44  * The wmall frames themselves are again split into a variable number of
45  * subframes. Every subframe contains the data for 2^N time domain samples
46  * where N varies between 7 and 12.
47  *
48  * Example wmall bitstream (in samples):
49  *
50  * ||   packet 0           || packet 1 || packet 2      packets
51  * ---------------------------------------------------
52  * || frame 0      || frame 1       || frame 2    ||    frames
53  * ---------------------------------------------------
54  * ||   |      |   ||   |   |   |   ||            ||    subframes of channel 0
55  * ---------------------------------------------------
56  * ||      |   |   ||   |   |   |   ||            ||    subframes of channel 1
57  * ---------------------------------------------------
58  *
59  * The frame layouts for the individual channels of a wma frame does not need
60  * to be the same.
61  *
62  * However, if the offsets and lengths of several subframes of a frame are the
63  * same, the subframes of the channels can be grouped.
64  * Every group may then use special coding techniques like M/S stereo coding
65  * to improve the compression ratio. These channel transformations do not
66  * need to be applied to a whole subframe. Instead, they can also work on
67  * individual scale factor bands (see below).
68  * The coefficients that carry the audio signal in the frequency domain
69  * are transmitted as huffman-coded vectors with 4, 2 and 1 elements.
70  * In addition to that, the encoder can switch to a runlevel coding scheme
71  * by transmitting subframe_length / 128 zero coefficients.
72  *
73  * Before the audio signal can be converted to the time domain, the
74  * coefficients have to be rescaled and inverse quantized.
75  * A subframe is therefore split into several scale factor bands that get
76  * scaled individually.
77  * Scale factors are submitted for every frame but they might be shared
78  * between the subframes of a channel. Scale factors are initially DPCM-coded.
79  * Once scale factors are shared, the differences are transmitted as runlevel
80  * codes.
81  * Every subframe length and offset combination in the frame layout shares a
82  * common quantization factor that can be adjusted for every channel by a
83  * modifier.
84  * After the inverse quantization, the coefficients get processed by an IMDCT.
85  * The resulting values are then windowed with a sine window and the first half
86  * of the values are added to the second half of the output from the previous
87  * subframe in order to reconstruct the output samples.
88  */
89
90 #include "avcodec.h"
91 #include "internal.h"
92 #include "get_bits.h"
93 #include "put_bits.h"
94 #include "dsputil.h"
95 #include "wma.h"
96
97 /** current decoder limitations */
98 #define WMALL_MAX_CHANNELS    8                             ///< max number of handled channels
99 #define MAX_SUBFRAMES  32                                    ///< max number of subframes per channel
100 #define MAX_BANDS      29                                    ///< max number of scale factor bands
101 #define MAX_FRAMESIZE  32768                                 ///< maximum compressed frame size
102
103 #define WMALL_BLOCK_MIN_BITS  6                                           ///< log2 of min block size
104 #define WMALL_BLOCK_MAX_BITS 12                                           ///< log2 of max block size
105 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)                 ///< maximum block size
106 #define WMALL_BLOCK_SIZES    (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1) ///< possible block sizes
107
108
109 #define VLCBITS            9
110 #define SCALEVLCBITS       8
111 #define VEC4MAXDEPTH    ((HUFF_VEC4_MAXBITS+VLCBITS-1)/VLCBITS)
112 #define VEC2MAXDEPTH    ((HUFF_VEC2_MAXBITS+VLCBITS-1)/VLCBITS)
113 #define VEC1MAXDEPTH    ((HUFF_VEC1_MAXBITS+VLCBITS-1)/VLCBITS)
114 #define SCALEMAXDEPTH   ((HUFF_SCALE_MAXBITS+SCALEVLCBITS-1)/SCALEVLCBITS)
115 #define SCALERLMAXDEPTH ((HUFF_SCALE_RL_MAXBITS+VLCBITS-1)/VLCBITS)
116
117 static float            sin64[33];        ///< sinus table for decorrelation
118
119 /**
120  * @brief frame specific decoder context for a single channel
121  */
122 typedef struct {
123     int16_t  prev_block_len;                          ///< length of the previous block
124     uint8_t  transmit_coefs;
125     uint8_t  num_subframes;
126     uint16_t subframe_len[MAX_SUBFRAMES];             ///< subframe length in samples
127     uint16_t subframe_offset[MAX_SUBFRAMES];          ///< subframe positions in the current frame
128     uint8_t  cur_subframe;                            ///< current subframe number
129     uint16_t decoded_samples;                         ///< number of already processed samples
130     uint8_t  grouped;                                 ///< channel is part of a group
131     int      quant_step;                              ///< quantization step for the current subframe
132     int8_t   reuse_sf;                                ///< share scale factors between subframes
133     int8_t   scale_factor_step;                       ///< scaling step for the current subframe
134     int      max_scale_factor;                        ///< maximum scale factor for the current subframe
135     int      saved_scale_factors[2][MAX_BANDS];       ///< resampled and (previously) transmitted scale factor values
136     int8_t   scale_factor_idx;                        ///< index for the transmitted scale factor values (used for resampling)
137     int*     scale_factors;                           ///< pointer to the scale factor values used for decoding
138     uint8_t  table_idx;                               ///< index in sf_offsets for the scale factor reference block
139     float*   coeffs;                                  ///< pointer to the subframe decode buffer
140     uint16_t num_vec_coeffs;                          ///< number of vector coded coefficients
141     DECLARE_ALIGNED(16, float, out)[WMALL_BLOCK_MAX_SIZE + WMALL_BLOCK_MAX_SIZE / 2]; ///< output buffer
142     int      transient_counter;                       ///< number of transient samples from the beginning of transient zone
143 } WmallChannelCtx;
144
145 /**
146  * @brief channel group for channel transformations
147  */
148 typedef struct {
149     uint8_t num_channels;                                     ///< number of channels in the group
150     int8_t  transform;                                        ///< transform on / off
151     int8_t  transform_band[MAX_BANDS];                        ///< controls if the transform is enabled for a certain band
152     float   decorrelation_matrix[WMALL_MAX_CHANNELS*WMALL_MAX_CHANNELS];
153     float*  channel_data[WMALL_MAX_CHANNELS];                ///< transformation coefficients
154 } WmallChannelGrp;
155
156 /**
157  * @brief main decoder context
158  */
159 typedef struct WmallDecodeCtx {
160     /* generic decoder variables */
161     AVCodecContext*  avctx;                         ///< codec context for av_log
162     DSPContext       dsp;                           ///< accelerated DSP functions
163     uint8_t          frame_data[MAX_FRAMESIZE +
164                       FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
165     PutBitContext    pb;                            ///< context for filling the frame_data buffer
166     FFTContext       mdct_ctx[WMALL_BLOCK_SIZES];  ///< MDCT context per block size
167     DECLARE_ALIGNED(16, float, tmp)[WMALL_BLOCK_MAX_SIZE]; ///< IMDCT output buffer
168     float*           windows[WMALL_BLOCK_SIZES];   ///< windows for the different block sizes
169
170     /* frame size dependent frame information (set during initialization) */
171     uint32_t         decode_flags;                  ///< used compression features
172     uint8_t          len_prefix;                    ///< frame is prefixed with its length
173     uint8_t          dynamic_range_compression;     ///< frame contains DRC data
174     uint8_t          bits_per_sample;               ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
175     uint16_t         samples_per_frame;             ///< number of samples to output
176     uint16_t         log2_frame_size;
177     int8_t           num_channels;                  ///< number of channels in the stream (same as AVCodecContext.num_channels)
178     int8_t           lfe_channel;                   ///< lfe channel index
179     uint8_t          max_num_subframes;
180     uint8_t          subframe_len_bits;             ///< number of bits used for the subframe length
181     uint8_t          max_subframe_len_bit;          ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
182     uint16_t         min_samples_per_subframe;
183     int8_t           num_sfb[WMALL_BLOCK_SIZES];   ///< scale factor bands per block size
184     int16_t          sfb_offsets[WMALL_BLOCK_SIZES][MAX_BANDS];                    ///< scale factor band offsets (multiples of 4)
185     int8_t           sf_offsets[WMALL_BLOCK_SIZES][WMALL_BLOCK_SIZES][MAX_BANDS]; ///< scale factor resample matrix
186     int16_t          subwoofer_cutoffs[WMALL_BLOCK_SIZES]; ///< subwoofer cutoff values
187
188     /* packet decode state */
189     GetBitContext    pgb;                           ///< bitstream reader context for the packet
190     int              next_packet_start;             ///< start offset of the next wma packet in the demuxer packet
191     uint8_t          packet_offset;                 ///< frame offset in the packet
192     uint8_t          packet_sequence_number;        ///< current packet number
193     int              num_saved_bits;                ///< saved number of bits
194     int              frame_offset;                  ///< frame offset in the bit reservoir
195     int              subframe_offset;               ///< subframe offset in the bit reservoir
196     uint8_t          packet_loss;                   ///< set in case of bitstream error
197     uint8_t          packet_done;                   ///< set when a packet is fully decoded
198
199     /* frame decode state */
200     uint32_t         frame_num;                     ///< current frame number (not used for decoding)
201     GetBitContext    gb;                            ///< bitstream reader context
202     int              buf_bit_size;                  ///< buffer size in bits
203     float*           samples;                       ///< current samplebuffer pointer
204     float*           samples_end;                   ///< maximum samplebuffer pointer
205     uint8_t          drc_gain;                      ///< gain for the DRC tool
206     int8_t           skip_frame;                    ///< skip output step
207     int8_t           parsed_all_subframes;          ///< all subframes decoded?
208
209     /* subframe/block decode state */
210     int16_t          subframe_len;                  ///< current subframe length
211     int8_t           channels_for_cur_subframe;     ///< number of channels that contain the subframe
212     int8_t           channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
213     int8_t           num_bands;                     ///< number of scale factor bands
214     int8_t           transmit_num_vec_coeffs;       ///< number of vector coded coefficients is part of the bitstream
215     int16_t*         cur_sfb_offsets;               ///< sfb offsets for the current block
216     uint8_t          table_idx;                     ///< index for the num_sfb, sfb_offsets, sf_offsets and subwoofer_cutoffs tables
217     int8_t           esc_len;                       ///< length of escaped coefficients
218
219     uint8_t          num_chgroups;                  ///< number of channel groups
220     WmallChannelGrp chgroup[WMALL_MAX_CHANNELS];    ///< channel group information
221
222     WmallChannelCtx channel[WMALL_MAX_CHANNELS];    ///< per channel data
223
224     // WMA lossless
225     
226     uint8_t do_arith_coding;
227     uint8_t do_ac_filter;
228     uint8_t do_inter_ch_decorr;
229     uint8_t do_mclms;
230     uint8_t do_lpc;
231
232     int8_t acfilter_order;
233     int8_t acfilter_scaling;
234     int acfilter_coeffs[16];
235
236     int8_t mclms_order;
237     int8_t mclms_scaling;
238     int16_t mclms_coeffs[128];
239     int16_t mclms_coeffs_cur[4];
240     int mclms_prevvalues[64];   // FIXME: should be 32-bit / 16-bit depending on bit-depth
241     int16_t mclms_updates[64];
242     int mclms_recent;
243
244     int movave_scaling;
245     int quant_stepsize;
246
247     struct {
248         int order;
249         int scaling;
250         int coefsend;
251         int bitsend;
252         int16_t coefs[256];
253     int lms_prevvalues[512];    // FIXME: see above
254     int16_t lms_updates[512];   // and here too
255     int recent;
256     } cdlms[2][9];              /* XXX: Here, 2 is the max. no. of channels allowed,
257                                         9 is the maximum no. of filters per channel.
258                                         Question is, why 2 if WMALL_MAX_CHANNELS == 8 */
259
260
261     int cdlms_ttl[2];
262
263     int bV3RTM;
264
265     int is_channel_coded[2];    // XXX: same question as above applies here too (and below)
266     int update_speed[2];
267
268     int transient[2];
269     int transient_pos[2];
270     int seekable_tile;
271
272     int ave_sum[2];
273
274     int channel_residues[2][2048];
275
276
277     int lpc_coefs[2][40];
278     int lpc_order;
279     int lpc_scaling;
280     int lpc_intbits;
281
282     int channel_coeffs[2][2048];
283
284 } WmallDecodeCtx;
285
286
287 #undef dprintf
288 #define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
289
290
291 static int num_logged_tiles = 0;
292
293 /**
294  *@brief helper function to print the most important members of the context
295  *@param s context
296  */
297 static void av_cold dump_context(WmallDecodeCtx *s)
298 {
299 #define PRINT(a, b)     av_log(s->avctx, AV_LOG_DEBUG, " %s = %d\n", a, b);
300 #define PRINT_HEX(a, b) av_log(s->avctx, AV_LOG_DEBUG, " %s = %x\n", a, b);
301
302     PRINT("ed sample bit depth", s->bits_per_sample);
303     PRINT_HEX("ed decode flags", s->decode_flags);
304     PRINT("samples per frame",   s->samples_per_frame);
305     PRINT("log2 frame size",     s->log2_frame_size);
306     PRINT("max num subframes",   s->max_num_subframes);
307     PRINT("len prefix",          s->len_prefix);
308     PRINT("num channels",        s->num_channels);
309 }
310
311 static int dump_int_buffer(int *buffer, int length, int delimiter)
312 {
313     int i;
314
315     for (i=0 ; i<length ; i++) {
316         if (!(i%delimiter))
317             av_log(0, 0, "\n[%d] ", i);
318         av_log(0, 0, "%d, ", buffer[i]);
319     }
320     av_log(0, 0, "\n");
321
322 }
323
324 /**
325  *@brief Uninitialize the decoder and free all resources.
326  *@param avctx codec context
327  *@return 0 on success, < 0 otherwise
328  */
329 static av_cold int decode_end(AVCodecContext *avctx)
330 {
331     WmallDecodeCtx *s = avctx->priv_data;
332     int i;
333
334     for (i = 0; i < WMALL_BLOCK_SIZES; i++)
335         ff_mdct_end(&s->mdct_ctx[i]);
336
337     return 0;
338 }
339
340 /**
341  *@brief Initialize the decoder.
342  *@param avctx codec context
343  *@return 0 on success, -1 otherwise
344  */
345 static av_cold int decode_init(AVCodecContext *avctx)
346 {
347     WmallDecodeCtx *s = avctx->priv_data;
348     uint8_t *edata_ptr = avctx->extradata;
349     unsigned int channel_mask;
350     int i;
351     int log2_max_num_subframes;
352     int num_possible_block_sizes;
353
354     s->avctx = avctx;
355     dsputil_init(&s->dsp, avctx);
356     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
357
358     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
359
360     if (avctx->extradata_size >= 18) {
361         s->decode_flags    = AV_RL16(edata_ptr+14);
362         channel_mask       = AV_RL32(edata_ptr+2);
363         s->bits_per_sample = AV_RL16(edata_ptr);
364         /** dump the extradata */
365         for (i = 0; i < avctx->extradata_size; i++)
366             dprintf(avctx, "[%x] ", avctx->extradata[i]);
367         dprintf(avctx, "\n");
368
369     } else {
370         av_log_ask_for_sample(avctx, "Unknown extradata size\n");
371         return AVERROR_INVALIDDATA;
372     }
373
374     /** generic init */
375     s->log2_frame_size = av_log2(avctx->block_align) + 4;
376
377     /** frame info */
378     s->skip_frame  = 1; /* skip first frame */
379     s->packet_loss = 1;
380     s->len_prefix  = (s->decode_flags & 0x40);
381
382     /** get frame len */
383     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
384                                                           3, s->decode_flags);
385
386     /** init previous block len */
387     for (i = 0; i < avctx->channels; i++)
388         s->channel[i].prev_block_len = s->samples_per_frame;
389
390     /** subframe info */
391     log2_max_num_subframes  = ((s->decode_flags & 0x38) >> 3);
392     s->max_num_subframes    = 1 << log2_max_num_subframes;
393     s->max_subframe_len_bit = 0;
394     s->subframe_len_bits    = av_log2(log2_max_num_subframes) + 1;
395
396     num_possible_block_sizes     = log2_max_num_subframes + 1;
397     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
398     s->dynamic_range_compression = (s->decode_flags & 0x80);
399
400     s->bV3RTM = s->decode_flags & 0x100;
401
402     if (s->max_num_subframes > MAX_SUBFRAMES) {
403         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
404                s->max_num_subframes);
405         return AVERROR_INVALIDDATA;
406     }
407
408     s->num_channels = avctx->channels;
409
410     /** extract lfe channel position */
411     s->lfe_channel = -1;
412
413     if (channel_mask & 8) {
414         unsigned int mask;
415         for (mask = 1; mask < 16; mask <<= 1) {
416             if (channel_mask & mask)
417                 ++s->lfe_channel;
418         }
419     }
420
421     if (s->num_channels < 0) {
422         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n", s->num_channels);
423         return AVERROR_INVALIDDATA;
424     } else if (s->num_channels > WMALL_MAX_CHANNELS) {
425         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
426         return AVERROR_PATCHWELCOME;
427     }
428
429     avctx->channel_layout = channel_mask;
430     return 0;
431 }
432
433 /**
434  *@brief Decode the subframe length.
435  *@param s context
436  *@param offset sample offset in the frame
437  *@return decoded subframe length on success, < 0 in case of an error
438  */
439 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
440 {
441     int frame_len_ratio;
442     int subframe_len, len;
443
444     /** no need to read from the bitstream when only one length is possible */
445     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
446         return s->min_samples_per_subframe;
447
448     len = av_log2(s->max_num_subframes - 1) + 1;
449     frame_len_ratio = get_bits(&s->gb, len);
450
451     subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
452
453     /** sanity check the length */
454     if (subframe_len < s->min_samples_per_subframe ||
455         subframe_len > s->samples_per_frame) {
456         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
457                subframe_len);
458         return AVERROR_INVALIDDATA;
459     }
460     return subframe_len;
461 }
462
463 /**
464  *@brief Decode how the data in the frame is split into subframes.
465  *       Every WMA frame contains the encoded data for a fixed number of
466  *       samples per channel. The data for every channel might be split
467  *       into several subframes. This function will reconstruct the list of
468  *       subframes for every channel.
469  *
470  *       If the subframes are not evenly split, the algorithm estimates the
471  *       channels with the lowest number of total samples.
472  *       Afterwards, for each of these channels a bit is read from the
473  *       bitstream that indicates if the channel contains a subframe with the
474  *       next subframe size that is going to be read from the bitstream or not.
475  *       If a channel contains such a subframe, the subframe size gets added to
476  *       the channel's subframe list.
477  *       The algorithm repeats these steps until the frame is properly divided
478  *       between the individual channels.
479  *
480  *@param s context
481  *@return 0 on success, < 0 in case of an error
482  */
483 static int decode_tilehdr(WmallDecodeCtx *s)
484 {
485     uint16_t num_samples[WMALL_MAX_CHANNELS];        /**< sum of samples for all currently known subframes of a channel */
486     uint8_t  contains_subframe[WMALL_MAX_CHANNELS];  /**< flag indicating if a channel contains the current subframe */
487     int channels_for_cur_subframe = s->num_channels;  /**< number of channels that contain the current subframe */
488     int fixed_channel_layout = 0;                     /**< flag indicating that all channels use the same subfra2me offsets and sizes */
489     int min_channel_len = 0;                          /**< smallest sum of samples (channels with this length will be processed first) */
490     int c;
491
492     /* Should never consume more than 3073 bits (256 iterations for the
493      * while loop when always the minimum amount of 128 samples is substracted
494      * from missing samples in the 8 channel case).
495      * 1 + BLOCK_MAX_SIZE * MAX_CHANNELS / BLOCK_MIN_SIZE * (MAX_CHANNELS  + 4)
496      */
497
498     /** reset tiling information */
499     for (c = 0; c < s->num_channels; c++)
500         s->channel[c].num_subframes = 0;
501
502     memset(num_samples, 0, sizeof(num_samples));
503
504     if (s->max_num_subframes == 1 || get_bits1(&s->gb))
505         fixed_channel_layout = 1;
506
507     /** loop until the frame data is split between the subframes */
508     do {
509         int subframe_len;
510
511         /** check which channels contain the subframe */
512         for (c = 0; c < s->num_channels; c++) {
513             if (num_samples[c] == min_channel_len) {
514                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
515                     (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
516                     contains_subframe[c] = 1;
517                 }                   
518                 else {
519                     contains_subframe[c] = get_bits1(&s->gb);
520                 }
521             } else
522                 contains_subframe[c] = 0;
523         }
524
525         /** get subframe length, subframe_len == 0 is not allowed */
526         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
527             return AVERROR_INVALIDDATA;
528         /** add subframes to the individual channels and find new min_channel_len */
529         min_channel_len += subframe_len;
530         for (c = 0; c < s->num_channels; c++) {
531             WmallChannelCtx* chan = &s->channel[c];
532
533             if (contains_subframe[c]) {
534                 if (chan->num_subframes >= MAX_SUBFRAMES) {
535                     av_log(s->avctx, AV_LOG_ERROR,
536                            "broken frame: num subframes > 31\n");
537                     return AVERROR_INVALIDDATA;
538                 }
539                 chan->subframe_len[chan->num_subframes] = subframe_len;
540                 num_samples[c] += subframe_len;
541                 ++chan->num_subframes;
542                 if (num_samples[c] > s->samples_per_frame) {
543                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
544                            "channel len(%d) > samples_per_frame(%d)\n",
545                            num_samples[c], s->samples_per_frame);
546                     return AVERROR_INVALIDDATA;
547                 }
548             } else if (num_samples[c] <= min_channel_len) {
549                 if (num_samples[c] < min_channel_len) {
550                     channels_for_cur_subframe = 0;
551                     min_channel_len = num_samples[c];
552                 }
553                 ++channels_for_cur_subframe;
554             }
555         }
556     } while (min_channel_len < s->samples_per_frame);
557
558     for (c = 0; c < s->num_channels; c++) {
559         int i;
560         int offset = 0;
561         for (i = 0; i < s->channel[c].num_subframes; i++) {
562             s->channel[c].subframe_offset[i] = offset;
563             offset += s->channel[c].subframe_len[i];
564         }
565     }
566
567     return 0;
568 }
569
570
571 static int my_log2(unsigned int i)
572 {
573     unsigned int iLog2 = 0;
574     while ((i >> iLog2) > 1)
575         iLog2++;
576     return iLog2;
577 }
578
579
580 /**
581  *
582  */
583 static void decode_ac_filter(WmallDecodeCtx *s)
584 {
585     int i;
586     s->acfilter_order = get_bits(&s->gb, 4) + 1;
587     s->acfilter_scaling = get_bits(&s->gb, 4);
588
589     for(i = 0; i < s->acfilter_order; i++) {
590         s->acfilter_coeffs[i] = get_bits(&s->gb, s->acfilter_scaling) + 1;
591     }
592 }
593
594
595 /**
596  *
597  */
598 static void decode_mclms(WmallDecodeCtx *s)
599 {
600     s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
601     s->mclms_scaling = get_bits(&s->gb, 4);
602     if(get_bits1(&s->gb)) {
603         // mclms_send_coef
604         int i;
605         int send_coef_bits;
606         int cbits = av_log2(s->mclms_scaling + 1);
607         assert(cbits == my_log2(s->mclms_scaling + 1));
608         if(1 << cbits < s->mclms_scaling + 1)
609             cbits++;
610
611         send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
612
613         for(i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++) {
614             s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
615         }           
616
617         for(i = 0; i < s->num_channels; i++) {
618             int c;
619             for(c = 0; c < i; c++) {
620                 s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
621             }
622         }
623     }
624 }
625
626
627 /**
628  *
629  */
630 static void decode_cdlms(WmallDecodeCtx *s)
631 {
632     int c, i;
633     int cdlms_send_coef = get_bits1(&s->gb);
634
635     for(c = 0; c < s->num_channels; c++) {
636         s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
637         for(i = 0; i < s->cdlms_ttl[c]; i++) {
638             s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
639         }
640
641         for(i = 0; i < s->cdlms_ttl[c]; i++) {
642             s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
643         }
644
645         if(cdlms_send_coef) {
646             for(i = 0; i < s->cdlms_ttl[c]; i++) {
647                 int cbits, shift_l, shift_r, j;
648                 cbits = av_log2(s->cdlms[c][i].order);
649                 if(1 << cbits < s->cdlms[c][i].order)
650                     cbits++;
651                 s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
652
653                 cbits = av_log2(s->cdlms[c][i].scaling + 1);
654                 if(1 << cbits < s->cdlms[c][i].scaling + 1)
655                     cbits++;
656                 
657                 s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
658                 shift_l = 32 - s->cdlms[c][i].bitsend;
659                 shift_r = 32 - 2 - s->cdlms[c][i].scaling;
660                 for(j = 0; j < s->cdlms[c][i].coefsend; j++) {
661                     s->cdlms[c][i].coefs[j] = 
662                         (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
663                 }
664             }
665         }
666     }
667 }
668
669 /**
670  *
671  */
672 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
673 {
674     int i = 0;
675     unsigned int ave_mean;
676     s->transient[ch] = get_bits1(&s->gb);
677     if(s->transient[ch]) {
678             s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
679         if (s->transient_pos[ch])
680                 s->transient[ch] = 0;
681             s->channel[ch].transient_counter =
682                 FFMAX(s->channel[ch].transient_counter, s->samples_per_frame / 2);
683         } else if (s->channel[ch].transient_counter)
684             s->transient[ch] = 1;
685
686     if(s->seekable_tile) {
687         ave_mean = get_bits(&s->gb, s->bits_per_sample);
688         s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
689 //      s->ave_sum[ch] *= 2;
690     }
691
692     if(s->seekable_tile) {
693         if(s->do_inter_ch_decorr)
694             s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
695         else
696             s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
697         i++;
698     }
699     //av_log(0, 0, "%8d: ", num_logged_tiles++);
700     for(; i < tile_size; i++) {
701         int quo = 0, rem, rem_bits, residue;
702         while(get_bits1(&s->gb))
703             quo++;
704         if(quo >= 32)
705             quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
706
707         ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
708         rem_bits = av_ceil_log2(ave_mean);
709         rem = rem_bits ? get_bits(&s->gb, rem_bits) : 0;
710         residue = (quo << rem_bits) + rem;
711
712         s->ave_sum[ch] = residue + s->ave_sum[ch] - (s->ave_sum[ch] >> s->movave_scaling);
713
714         if(residue & 1)
715             residue = -(residue >> 1) - 1;
716         else
717             residue = residue >> 1;
718         s->channel_residues[ch][i] = residue;
719
720     /*if (num_logged_tiles < 1)
721         av_log(0, 0, "%4d ", residue); */
722     }
723     dump_int_buffer(s->channel_residues[ch], tile_size, 16);
724
725     return 0;
726
727 }
728
729
730 /**
731  *
732  */
733 static void
734 decode_lpc(WmallDecodeCtx *s)
735 {
736     int ch, i, cbits;
737     s->lpc_order = get_bits(&s->gb, 5) + 1;
738     s->lpc_scaling = get_bits(&s->gb, 4);
739     s->lpc_intbits = get_bits(&s->gb, 3) + 1;
740     cbits = s->lpc_scaling + s->lpc_intbits;
741     for(ch = 0; ch < s->num_channels; ch++) {
742         for(i = 0; i < s->lpc_order; i++) {
743             s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
744         }
745     }
746 }
747
748
749 static void clear_codec_buffers(WmallDecodeCtx *s)
750 {
751     int ich, ilms;
752
753     memset(s->acfilter_coeffs, 0,     16 * sizeof(int));
754     memset(s->lpc_coefs      , 0, 40 * 2 * sizeof(int));
755
756     memset(s->mclms_coeffs    , 0, 128 * sizeof(int16_t));
757     memset(s->mclms_coeffs_cur, 0,   4 * sizeof(int16_t));
758     memset(s->mclms_prevvalues, 0,  64 * sizeof(int));
759     memset(s->mclms_updates   , 0,  64 * sizeof(int16_t));
760
761     for (ich = 0; ich < s->num_channels; ich++) {
762         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
763             memset(s->cdlms[ich][ilms].coefs         , 0, 256 * sizeof(int16_t));
764             memset(s->cdlms[ich][ilms].lms_prevvalues, 0, 512 * sizeof(int));
765             memset(s->cdlms[ich][ilms].lms_updates   , 0, 512 * sizeof(int16_t));
766         }
767         s->ave_sum[ich] = 0;
768     }
769 }
770
771 /**
772  *@brief Resets filter parameters and transient area at new seekable tile
773  */
774 static void reset_codec(WmallDecodeCtx *s)
775 {
776     int ich, ilms;
777     s->mclms_recent = s->mclms_order * s->num_channels;
778     for (ich = 0; ich < s->num_channels; ich++) {
779         for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
780             s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
781         /* first sample of a seekable subframe is considered as the starting of
782            a transient area which is samples_per_frame samples long */
783         s->channel[ich].transient_counter = s->samples_per_frame;
784         s->transient[ich] = 1;
785     }
786 }
787
788
789
790 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
791 {
792     int32_t pred, icoef;
793     int recent = s->cdlms[ich][ilms].recent;
794
795     for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
796         pred += s->cdlms[ich][ilms].coefs[icoef] *
797                     s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
798
799     pred += (1 << (s->cdlms[ich][ilms].scaling - 1));
800     /* XXX: Table 29 has:
801             iPred >= cdlms[iCh][ilms].scaling;
802        seems to me like a missing > */
803     pred >>= s->cdlms[ich][ilms].scaling;
804     return pred;
805 }
806
807 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int32_t input, int32_t pred)
808 {
809     int icoef;
810     int recent = s->cdlms[ich][ilms].recent;
811     int range = 1 << (s->bits_per_sample - 1);
812     int bps = s->bits_per_sample > 16 ? 4 : 2; // bytes per sample
813
814     if (input > pred) {
815         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
816             s->cdlms[ich][ilms].coefs[icoef] +=
817                 s->cdlms[ich][ilms].lms_updates[icoef + recent];
818     } else {
819         for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
820             s->cdlms[ich][ilms].coefs[icoef] -=
821                 s->cdlms[ich][ilms].lms_updates[icoef];     // XXX: [icoef + recent] ?
822     }
823     s->cdlms[ich][ilms].recent--;
824     s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
825
826     if (input > pred)
827         s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
828     else if (input < pred)
829         s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
830
831     /* XXX: spec says:
832     cdlms[iCh][ilms].updates[iRecent + cdlms[iCh][ilms].order >> 4] >>= 2;
833     lms_updates[iCh][ilms][iRecent + cdlms[iCh][ilms].order >> 3] >>= 1;
834
835         Questions is - are cdlms[iCh][ilms].updates[] and lms_updates[][][] two
836         seperate buffers? Here I've assumed that the two are same which makes
837         more sense to me.
838     */
839     s->cdlms[ich][ilms].lms_updates[recent + s->cdlms[ich][ilms].order >> 4] >>= 2;
840     s->cdlms[ich][ilms].lms_updates[recent + s->cdlms[ich][ilms].order >> 3] >>= 1;
841     /* XXX: recent + (s->cdlms[ich][ilms].order >> 4) ? */
842
843     if (s->cdlms[ich][ilms].recent == 0) {
844         /* XXX: This memcpy()s will probably fail if a fixed 32-bit buffer is used.
845                 follow kshishkov's suggestion of using a union. */
846         memcpy(s->cdlms[ich][ilms].lms_prevvalues + s->cdlms[ich][ilms].order,
847                s->cdlms[ich][ilms].lms_prevvalues,
848                bps * s->cdlms[ich][ilms].order);
849         memcpy(s->cdlms[ich][ilms].lms_updates + s->cdlms[ich][ilms].order,
850                s->cdlms[ich][ilms].lms_updates,
851                bps * s->cdlms[ich][ilms].order);
852         s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
853     }
854 }
855
856 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
857 {
858     int ilms, recent, icoef;
859     s->update_speed[ich] = 16;
860     for (ilms = s->cdlms_ttl[ich]; ilms >= 0; ilms--) {
861         recent = s->cdlms[ich][ilms].recent;
862         if (s->bV3RTM) {
863             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
864                 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
865         } else {
866             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
867                 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
868         }
869     }
870 }
871
872 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
873 {
874     int ilms, recent, icoef;
875     s->update_speed[ich] = 8;
876     for (ilms = s->cdlms_ttl[ich]; ilms >= 0; ilms--) {
877         recent = s->cdlms[ich][ilms].recent;
878         if (s->bV3RTM) {
879             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
880                 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
881         } else {
882             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
883                 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
884         }
885     }
886 }
887
888 static void revert_cdlms(WmallDecodeCtx *s, int tile_size)
889 {
890     int icoef, ich;
891     int32_t pred, channel_coeff;
892     int ilms, num_lms;
893
894     for (ich = 0; ich < s->num_channels; ich++) {
895         if (!s->is_channel_coded[ich])
896             continue;
897         for (icoef = 0; icoef < tile_size; icoef++) {
898             num_lms = s->cdlms_ttl[ich];
899             channel_coeff = s->channel_residues[ich][icoef];
900             if (icoef == s->transient_pos[ich]) {
901                 s->transient[ich] = 1;
902                 use_high_update_speed(s, ich);
903             }
904             for (ilms = num_lms; ilms >= 0; ilms--) {
905                 pred = lms_predict(s, ich, ilms);
906                 channel_coeff += pred;
907                 lms_update(s, ich, ilms, channel_coeff, pred);
908             }
909             if (s->transient[ich]) {
910                 --s->channel[ich].transient_counter;
911                 if(!s->channel[ich].transient_counter)
912                     use_normal_update_speed(s, ich);
913             }
914             s->channel_coeffs[ich][icoef] = channel_coeff;
915         }
916     }
917 }
918
919
920
921 /**
922  *@brief Decode a single subframe (block).
923  *@param s codec context
924  *@return 0 on success, < 0 when decoding failed
925  */
926 static int decode_subframe(WmallDecodeCtx *s)
927 {
928     int offset = s->samples_per_frame;
929     int subframe_len = s->samples_per_frame;
930     int i;
931     int total_samples   = s->samples_per_frame * s->num_channels;
932     int rawpcm_tile;
933     int padding_zeroes;
934
935     s->subframe_offset = get_bits_count(&s->gb);
936
937     /** reset channel context and find the next block offset and size
938         == the next block of the channel with the smallest number of
939         decoded samples
940     */
941     for (i = 0; i < s->num_channels; i++) {
942         s->channel[i].grouped = 0;
943         if (offset > s->channel[i].decoded_samples) {
944             offset = s->channel[i].decoded_samples;
945             subframe_len =
946                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
947         }
948     }
949
950     /** get a list of all channels that contain the estimated block */
951     s->channels_for_cur_subframe = 0;
952     for (i = 0; i < s->num_channels; i++) {
953         const int cur_subframe = s->channel[i].cur_subframe;
954         /** substract already processed samples */
955         total_samples -= s->channel[i].decoded_samples;
956
957         /** and count if there are multiple subframes that match our profile */
958         if (offset == s->channel[i].decoded_samples &&
959             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
960             total_samples -= s->channel[i].subframe_len[cur_subframe];
961             s->channel[i].decoded_samples +=
962                 s->channel[i].subframe_len[cur_subframe];
963             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
964             ++s->channels_for_cur_subframe;
965         }
966     }
967
968     /** check if the frame will be complete after processing the
969         estimated block */
970     if (!total_samples)
971         s->parsed_all_subframes = 1;
972
973
974     s->seekable_tile = get_bits1(&s->gb);
975     if(s->seekable_tile) {
976         clear_codec_buffers(s);
977
978         s->do_arith_coding    = get_bits1(&s->gb);
979         if(s->do_arith_coding) {
980             dprintf(s->avctx, "do_arith_coding == 1");
981             abort();
982         }
983         s->do_ac_filter       = get_bits1(&s->gb);
984         s->do_inter_ch_decorr = get_bits1(&s->gb);
985         s->do_mclms           = get_bits1(&s->gb);
986         
987         if(s->do_ac_filter)
988             decode_ac_filter(s);
989
990         if(s->do_mclms)
991             decode_mclms(s);
992
993         decode_cdlms(s);
994         s->movave_scaling = get_bits(&s->gb, 3);
995         s->quant_stepsize = get_bits(&s->gb, 8) + 1;
996
997             reset_codec(s);
998     }
999
1000     rawpcm_tile = get_bits1(&s->gb);
1001
1002     for(i = 0; i < s->num_channels; i++) {
1003         s->is_channel_coded[i] = 1;
1004     }
1005
1006     if(!rawpcm_tile) {
1007
1008         for(i = 0; i < s->num_channels; i++) {
1009             s->is_channel_coded[i] = get_bits1(&s->gb);
1010         }
1011
1012         if(s->bV3RTM) {
1013             // LPC
1014             s->do_lpc = get_bits1(&s->gb);
1015             if(s->do_lpc) {
1016                 decode_lpc(s);
1017             }
1018         } else {
1019             s->do_lpc = 0;
1020         }
1021     }
1022
1023
1024     if(get_bits1(&s->gb)) {
1025         padding_zeroes = get_bits(&s->gb, 5);
1026     } else {
1027         padding_zeroes = 0;
1028     }
1029
1030     if(rawpcm_tile) {
1031         
1032         int bits = s->bits_per_sample - padding_zeroes;
1033         int j;
1034         dprintf(s->avctx, "RAWPCM %d bits per sample. total %d bits, remain=%d\n", bits,
1035                 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
1036         for(i = 0; i < s->num_channels; i++) {
1037             for(j = 0; j < subframe_len; j++) {
1038                 s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
1039 //              dprintf(s->avctx, "PCM[%d][%d] = 0x%04x\n", i, j, s->channel_coeffs[i][j]);
1040             }
1041         }
1042     } else {
1043         for(i = 0; i < s->num_channels; i++)
1044             if(s->is_channel_coded[i])
1045                 decode_channel_residues(s, i, subframe_len);
1046     }
1047
1048     /** handled one subframe */
1049
1050     for (i = 0; i < s->channels_for_cur_subframe; i++) {
1051         int c = s->channel_indexes_for_cur_subframe[i];
1052         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1053             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1054             return AVERROR_INVALIDDATA;
1055         }
1056         ++s->channel[c].cur_subframe;
1057     }
1058     return 0;
1059 }
1060
1061 /**
1062  *@brief Decode one WMA frame.
1063  *@param s codec context
1064  *@return 0 if the trailer bit indicates that this is the last frame,
1065  *        1 if there are additional frames
1066  */
1067 static int decode_frame(WmallDecodeCtx *s)
1068 {
1069     GetBitContext* gb = &s->gb;
1070     int more_frames = 0;
1071     int len = 0;
1072     int i;
1073
1074     /** check for potential output buffer overflow */
1075     if (s->num_channels * s->samples_per_frame > s->samples_end - s->samples) {
1076         /** return an error if no frame could be decoded at all */
1077         av_log(s->avctx, AV_LOG_ERROR,
1078                "not enough space for the output samples\n");
1079         s->packet_loss = 1;
1080         return 0;
1081     }
1082
1083     /** get frame length */
1084     if (s->len_prefix)
1085         len = get_bits(gb, s->log2_frame_size);
1086
1087     /** decode tile information */
1088     if (decode_tilehdr(s)) {
1089         s->packet_loss = 1;
1090         return 0;
1091     }
1092
1093     /** read drc info */
1094     if (s->dynamic_range_compression) {
1095         s->drc_gain = get_bits(gb, 8);
1096     }
1097
1098     /** no idea what these are for, might be the number of samples
1099         that need to be skipped at the beginning or end of a stream */
1100     if (get_bits1(gb)) {
1101         int skip;
1102
1103         /** usually true for the first frame */
1104         if (get_bits1(gb)) {
1105             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1106             dprintf(s->avctx, "start skip: %i\n", skip);
1107         }
1108
1109         /** sometimes true for the last frame */
1110         if (get_bits1(gb)) {
1111             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1112             dprintf(s->avctx, "end skip: %i\n", skip);
1113         }
1114
1115     }
1116
1117     /** reset subframe states */
1118     s->parsed_all_subframes = 0;
1119     for (i = 0; i < s->num_channels; i++) {
1120         s->channel[i].decoded_samples = 0;
1121         s->channel[i].cur_subframe    = 0;
1122         s->channel[i].reuse_sf        = 0;
1123     }
1124
1125     /** decode all subframes */
1126     while (!s->parsed_all_subframes) {
1127         if (decode_subframe(s) < 0) {
1128             s->packet_loss = 1;
1129             return 0;
1130         }
1131     }
1132
1133     dprintf(s->avctx, "Frame done\n");
1134
1135     if (s->skip_frame) {
1136         s->skip_frame = 0;
1137     } else
1138         s->samples += s->num_channels * s->samples_per_frame;
1139
1140     if (s->len_prefix) {
1141         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1142             /** FIXME: not sure if this is always an error */
1143             av_log(s->avctx, AV_LOG_ERROR,
1144                    "frame[%i] would have to skip %i bits\n", s->frame_num,
1145                    len - (get_bits_count(gb) - s->frame_offset) - 1);
1146             s->packet_loss = 1;
1147             return 0;
1148         }
1149
1150         /** skip the rest of the frame data */
1151         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1152     } else {
1153 /*
1154         while (get_bits_count(gb) < s->num_saved_bits && get_bits1(gb) == 0) {
1155             dprintf(s->avctx, "skip1\n");
1156         }
1157 */
1158     }
1159
1160     /** decode trailer bit */
1161     more_frames = get_bits1(gb);
1162     ++s->frame_num;
1163     return more_frames;
1164 }
1165
1166 /**
1167  *@brief Calculate remaining input buffer length.
1168  *@param s codec context
1169  *@param gb bitstream reader context
1170  *@return remaining size in bits
1171  */
1172 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1173 {
1174     return s->buf_bit_size - get_bits_count(gb);
1175 }
1176
1177 /**
1178  *@brief Fill the bit reservoir with a (partial) frame.
1179  *@param s codec context
1180  *@param gb bitstream reader context
1181  *@param len length of the partial frame
1182  *@param append decides wether to reset the buffer or not
1183  */
1184 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1185                       int append)
1186 {
1187     int buflen;
1188
1189     /** when the frame data does not need to be concatenated, the input buffer
1190         is resetted and additional bits from the previous frame are copyed
1191         and skipped later so that a fast byte copy is possible */
1192
1193     if (!append) {
1194         s->frame_offset = get_bits_count(gb) & 7;
1195         s->num_saved_bits = s->frame_offset;
1196         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1197     }
1198
1199     buflen = (s->num_saved_bits + len + 8) >> 3;
1200
1201     if (len <= 0 || buflen > MAX_FRAMESIZE) {
1202         av_log_ask_for_sample(s->avctx, "input buffer too small\n");
1203         s->packet_loss = 1;
1204         return;
1205     }
1206
1207     s->num_saved_bits += len;
1208     if (!append) {
1209         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1210                      s->num_saved_bits);
1211     } else {
1212         int align = 8 - (get_bits_count(gb) & 7);
1213         align = FFMIN(align, len);
1214         put_bits(&s->pb, align, get_bits(gb, align));
1215         len -= align;
1216         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1217     }
1218     skip_bits_long(gb, len);
1219
1220     {
1221         PutBitContext tmp = s->pb;
1222         flush_put_bits(&tmp);
1223     }
1224
1225     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1226     skip_bits(&s->gb, s->frame_offset);
1227 }
1228
1229 /**
1230  *@brief Decode a single WMA packet.
1231  *@param avctx codec context
1232  *@param data the output buffer
1233  *@param data_size number of bytes that were written to the output buffer
1234  *@param avpkt input packet
1235  *@return number of bytes that were read from the input buffer
1236  */
1237 static int decode_packet(AVCodecContext *avctx,
1238                          void *data, int *data_size, AVPacket* avpkt)
1239 {
1240     WmallDecodeCtx *s = avctx->priv_data;
1241     GetBitContext* gb  = &s->pgb;
1242     const uint8_t* buf = avpkt->data;
1243     int buf_size       = avpkt->size;
1244     int num_bits_prev_frame;
1245     int packet_sequence_number;
1246
1247     s->samples       = data;
1248     s->samples_end   = (float*)((int8_t*)data + *data_size);
1249     *data_size = 0;
1250
1251     if (s->packet_done || s->packet_loss) {
1252         s->packet_done = 0;
1253
1254         /** sanity check for the buffer length */
1255         if (buf_size < avctx->block_align)
1256             return 0;
1257
1258         s->next_packet_start = buf_size - avctx->block_align;
1259         buf_size = avctx->block_align;
1260         s->buf_bit_size = buf_size << 3;
1261
1262         /** parse packet header */
1263         init_get_bits(gb, buf, s->buf_bit_size);
1264         packet_sequence_number = get_bits(gb, 4);
1265         int seekable_frame_in_packet = get_bits1(gb);
1266         int spliced_packet = get_bits1(gb);
1267
1268         /** get number of bits that need to be added to the previous frame */
1269         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1270
1271         /** check for packet loss */
1272         if (!s->packet_loss &&
1273             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1274             s->packet_loss = 1;
1275             av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1276                    s->packet_sequence_number, packet_sequence_number);
1277         }
1278         s->packet_sequence_number = packet_sequence_number;
1279
1280         if (num_bits_prev_frame > 0) {
1281             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1282             if (num_bits_prev_frame >= remaining_packet_bits) {
1283                 num_bits_prev_frame = remaining_packet_bits;
1284                 s->packet_done = 1;
1285             }
1286
1287             /** append the previous frame data to the remaining data from the
1288                 previous packet to create a full frame */
1289             save_bits(s, gb, num_bits_prev_frame, 1);
1290
1291             /** decode the cross packet frame if it is valid */
1292             if (!s->packet_loss)
1293                 decode_frame(s);
1294         } else if (s->num_saved_bits - s->frame_offset) {
1295             dprintf(avctx, "ignoring %x previously saved bits\n",
1296                     s->num_saved_bits - s->frame_offset);
1297         }
1298
1299         if (s->packet_loss) {
1300             /** reset number of saved bits so that the decoder
1301                 does not start to decode incomplete frames in the
1302                 s->len_prefix == 0 case */
1303             s->num_saved_bits = 0;
1304             s->packet_loss = 0;
1305         }
1306
1307     } else {
1308         int frame_size;
1309
1310         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1311         init_get_bits(gb, avpkt->data, s->buf_bit_size);
1312         skip_bits(gb, s->packet_offset);
1313         
1314         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1315             (frame_size = show_bits(gb, s->log2_frame_size)) &&
1316             frame_size <= remaining_bits(s, gb)) {
1317             save_bits(s, gb, frame_size, 0);
1318             s->packet_done = !decode_frame(s);
1319         } else if (!s->len_prefix
1320                    && s->num_saved_bits > get_bits_count(&s->gb)) {
1321             /** when the frames do not have a length prefix, we don't know
1322                 the compressed length of the individual frames
1323                 however, we know what part of a new packet belongs to the
1324                 previous frame
1325                 therefore we save the incoming packet first, then we append
1326                 the "previous frame" data from the next packet so that
1327                 we get a buffer that only contains full frames */
1328             s->packet_done = !decode_frame(s);
1329         } else {
1330             s->packet_done = 1;
1331         }
1332     }
1333
1334     if (s->packet_done && !s->packet_loss &&
1335         remaining_bits(s, gb) > 0) {
1336         /** save the rest of the data so that it can be decoded
1337             with the next packet */
1338         save_bits(s, gb, remaining_bits(s, gb), 0);
1339     }
1340
1341     *data_size = 0; // (int8_t *)s->samples - (int8_t *)data;
1342     s->packet_offset = get_bits_count(gb) & 7;
1343
1344     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1345 }
1346
1347 /**
1348  *@brief Clear decoder buffers (for seeking).
1349  *@param avctx codec context
1350  */
1351 static void flush(AVCodecContext *avctx)
1352 {
1353     WmallDecodeCtx *s = avctx->priv_data;
1354     int i;
1355     /** reset output buffer as a part of it is used during the windowing of a
1356         new frame */
1357     for (i = 0; i < s->num_channels; i++)
1358         memset(s->channel[i].out, 0, s->samples_per_frame *
1359                sizeof(*s->channel[i].out));
1360     s->packet_loss = 1;
1361 }
1362
1363
1364 /**
1365  *@brief wmall decoder
1366  */
1367 AVCodec ff_wmalossless_decoder = {
1368     "wmalossless",
1369     AVMEDIA_TYPE_AUDIO,
1370     CODEC_ID_WMALOSSLESS,
1371     sizeof(WmallDecodeCtx),
1372     decode_init,
1373     NULL,
1374     decode_end,
1375     decode_packet,
1376     .capabilities = CODEC_CAP_SUBFRAMES,
1377     .flush= flush,
1378     .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio 9 Lossless"),
1379 };