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