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