]> git.sesse.net Git - ffmpeg/blob - libavcodec/wmalosslessdec.c
Merge commit 'f56a08559334b7eb6b3fedbc0cc741887f6067ae'
[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 WmallChannelCtx {
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 + AV_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     int16_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 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
151         DECLARE_ALIGNED(16, int16_t, lms_updates)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
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 /** 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) {
189         av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
190         return AVERROR(EINVAL);
191     }
192
193     s->avctx = avctx;
194     ff_llauddsp_init(&s->dsp);
195     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
196
197     if (avctx->extradata_size >= 18) {
198         s->decode_flags    = AV_RL16(edata_ptr + 14);
199         channel_mask       = AV_RL32(edata_ptr +  2);
200         s->bits_per_sample = AV_RL16(edata_ptr);
201         if (s->bits_per_sample == 16)
202             avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
203         else if (s->bits_per_sample == 24) {
204             av_log(avctx, AV_LOG_WARNING, "Decoding audio at 24 bit-depth\n");
205             avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
206             avctx->bits_per_raw_sample = 24;
207         } else {
208             av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %"PRIu8"\n",
209                    s->bits_per_sample);
210             return AVERROR_INVALIDDATA;
211         }
212         /* dump the extradata */
213         for (i = 0; i < avctx->extradata_size; i++)
214             ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
215         ff_dlog(avctx, "\n");
216
217     } else {
218         avpriv_request_sample(avctx, "Unsupported extradata size");
219         return AVERROR_PATCHWELCOME;
220     }
221
222     /* generic init */
223     s->log2_frame_size = av_log2(avctx->block_align) + 4;
224
225     /* frame info */
226     s->skip_frame  = 1; /* skip first frame */
227     s->packet_loss = 1;
228     s->len_prefix  = s->decode_flags & 0x40;
229
230     /* get frame len */
231     s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate,
232                                                           3, s->decode_flags);
233     av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
234
235     /* init previous block len */
236     for (i = 0; i < avctx->channels; i++)
237         s->channel[i].prev_block_len = s->samples_per_frame;
238
239     /* subframe info */
240     log2_max_num_subframes  = (s->decode_flags & 0x38) >> 3;
241     s->max_num_subframes    = 1 << log2_max_num_subframes;
242     s->max_subframe_len_bit = 0;
243     s->subframe_len_bits    = av_log2(log2_max_num_subframes) + 1;
244
245     s->min_samples_per_subframe  = s->samples_per_frame / s->max_num_subframes;
246     s->dynamic_range_compression = s->decode_flags & 0x80;
247     s->bV3RTM                    = s->decode_flags & 0x100;
248
249     if (s->max_num_subframes > MAX_SUBFRAMES) {
250         av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRIu8"\n",
251                s->max_num_subframes);
252         return AVERROR_INVALIDDATA;
253     }
254
255     s->num_channels = avctx->channels;
256
257     /* extract lfe channel position */
258     s->lfe_channel = -1;
259
260     if (channel_mask & 8) {
261         unsigned int mask;
262         for (mask = 1; mask < 16; mask <<= 1)
263             if (channel_mask & mask)
264                 ++s->lfe_channel;
265     }
266
267     if (s->num_channels < 0) {
268         av_log(avctx, AV_LOG_ERROR, "invalid number of channels %"PRId8"\n",
269                s->num_channels);
270         return AVERROR_INVALIDDATA;
271     } else if (s->num_channels > WMALL_MAX_CHANNELS) {
272         avpriv_request_sample(avctx,
273                               "More than %d channels", WMALL_MAX_CHANNELS);
274         return AVERROR_PATCHWELCOME;
275     }
276
277     s->frame = av_frame_alloc();
278     if (!s->frame)
279         return AVERROR(ENOMEM);
280
281     avctx->channel_layout = channel_mask;
282     return 0;
283 }
284
285 /**
286  * @brief Decode the subframe length.
287  * @param s      context
288  * @param offset sample offset in the frame
289  * @return decoded subframe length on success, < 0 in case of an error
290  */
291 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
292 {
293     int frame_len_ratio, subframe_len, len;
294
295     /* no need to read from the bitstream when only one length is possible */
296     if (offset == s->samples_per_frame - s->min_samples_per_subframe)
297         return s->min_samples_per_subframe;
298
299     len             = av_log2(s->max_num_subframes - 1) + 1;
300     frame_len_ratio = get_bits(&s->gb, len);
301     subframe_len    = s->min_samples_per_subframe * (frame_len_ratio + 1);
302
303     /* sanity check the length */
304     if (subframe_len < s->min_samples_per_subframe ||
305         subframe_len > s->samples_per_frame) {
306         av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
307                subframe_len);
308         return AVERROR_INVALIDDATA;
309     }
310     return subframe_len;
311 }
312
313 /**
314  * @brief Decode how the data in the frame is split into subframes.
315  *       Every WMA frame contains the encoded data for a fixed number of
316  *       samples per channel. The data for every channel might be split
317  *       into several subframes. This function will reconstruct the list of
318  *       subframes for every channel.
319  *
320  *       If the subframes are not evenly split, the algorithm estimates the
321  *       channels with the lowest number of total samples.
322  *       Afterwards, for each of these channels a bit is read from the
323  *       bitstream that indicates if the channel contains a subframe with the
324  *       next subframe size that is going to be read from the bitstream or not.
325  *       If a channel contains such a subframe, the subframe size gets added to
326  *       the channel's subframe list.
327  *       The algorithm repeats these steps until the frame is properly divided
328  *       between the individual channels.
329  *
330  * @param s context
331  * @return 0 on success, < 0 in case of an error
332  */
333 static int decode_tilehdr(WmallDecodeCtx *s)
334 {
335     uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
336     uint8_t  contains_subframe[WMALL_MAX_CHANNELS];   /* flag indicating if a channel contains the current subframe */
337     int channels_for_cur_subframe = s->num_channels;  /* number of channels that contain the current subframe */
338     int fixed_channel_layout = 0;                     /* flag indicating that all channels use the same subfra2me offsets and sizes */
339     int min_channel_len = 0;                          /* smallest sum of samples (channels with this length will be processed first) */
340     int c, tile_aligned;
341
342     /* reset tiling information */
343     for (c = 0; c < s->num_channels; c++)
344         s->channel[c].num_subframes = 0;
345
346     tile_aligned = get_bits1(&s->gb);
347     if (s->max_num_subframes == 1 || tile_aligned)
348         fixed_channel_layout = 1;
349
350     /* loop until the frame data is split between the subframes */
351     do {
352         int subframe_len, in_use = 0;
353
354         /* check which channels contain the subframe */
355         for (c = 0; c < s->num_channels; c++) {
356             if (num_samples[c] == min_channel_len) {
357                 if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
358                    (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
359                     contains_subframe[c] = 1;
360                 } else {
361                     contains_subframe[c] = get_bits1(&s->gb);
362                 }
363                 in_use |= contains_subframe[c];
364             } else
365                 contains_subframe[c] = 0;
366         }
367
368         if (!in_use) {
369             av_log(s->avctx, AV_LOG_ERROR,
370                    "Found empty subframe\n");
371             return AVERROR_INVALIDDATA;
372         }
373
374         /* get subframe length, subframe_len == 0 is not allowed */
375         if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
376             return AVERROR_INVALIDDATA;
377         /* add subframes to the individual channels and find new min_channel_len */
378         min_channel_len += subframe_len;
379         for (c = 0; c < s->num_channels; c++) {
380             WmallChannelCtx *chan = &s->channel[c];
381
382             if (contains_subframe[c]) {
383                 if (chan->num_subframes >= MAX_SUBFRAMES) {
384                     av_log(s->avctx, AV_LOG_ERROR,
385                            "broken frame: num subframes > 31\n");
386                     return AVERROR_INVALIDDATA;
387                 }
388                 chan->subframe_len[chan->num_subframes] = subframe_len;
389                 num_samples[c] += subframe_len;
390                 ++chan->num_subframes;
391                 if (num_samples[c] > s->samples_per_frame) {
392                     av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
393                            "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
394                            num_samples[c], s->samples_per_frame);
395                     return AVERROR_INVALIDDATA;
396                 }
397             } else if (num_samples[c] <= min_channel_len) {
398                 if (num_samples[c] < min_channel_len) {
399                     channels_for_cur_subframe = 0;
400                     min_channel_len = num_samples[c];
401                 }
402                 ++channels_for_cur_subframe;
403             }
404         }
405     } while (min_channel_len < s->samples_per_frame);
406
407     for (c = 0; c < s->num_channels; c++) {
408         int i, offset = 0;
409         for (i = 0; i < s->channel[c].num_subframes; i++) {
410             s->channel[c].subframe_offsets[i] = offset;
411             offset += s->channel[c].subframe_len[i];
412         }
413     }
414
415     return 0;
416 }
417
418 static void decode_ac_filter(WmallDecodeCtx *s)
419 {
420     int i;
421     s->acfilter_order   = get_bits(&s->gb, 4) + 1;
422     s->acfilter_scaling = get_bits(&s->gb, 4);
423
424     for (i = 0; i < s->acfilter_order; i++)
425         s->acfilter_coeffs[i] = (s->acfilter_scaling ?
426                                  get_bits(&s->gb, s->acfilter_scaling) : 0) + 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 = (cbits ? get_bits(&s->gb, cbits) : 0) + 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) {
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 = (cbits ? get_bits(&s->gb, cbits) : 0) + 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 quo = 0, rem, rem_bits, residue;
537         while(get_bits1(&s->gb)) {
538             quo++;
539             if (get_bits_left(&s->gb) <= 0)
540                 return -1;
541         }
542         if (quo >= 32)
543             quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
544
545         ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
546         if (ave_mean <= 1)
547             residue = quo;
548         else {
549             rem_bits = av_ceil_log2(ave_mean);
550             rem      = get_bits_long(&s->gb, rem_bits);
551             residue  = (quo << rem_bits) + rem;
552         }
553
554         s->ave_sum[ch] = residue + s->ave_sum[ch] -
555                          (s->ave_sum[ch] >> s->movave_scaling);
556
557         residue = (residue >> 1) ^ -(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                 s->mclms_coeffs_cur[ich * num_channels + j] += WMASIGN(s->channel_residues[j][icoef]);
636         } else if (pred_error < 0) {
637             for (i = 0; i < order * num_channels; i++)
638                 s->mclms_coeffs[i + ich * order * num_channels] -=
639                     s->mclms_updates[s->mclms_recent + i];
640             for (j = 0; j < ich; j++)
641                 s->mclms_coeffs_cur[ich * num_channels + j] -= WMASIGN(s->channel_residues[j][icoef]);
642         }
643     }
644
645     for (ich = num_channels - 1; ich >= 0; ich--) {
646         s->mclms_recent--;
647         s->mclms_prevvalues[s->mclms_recent] = av_clip(s->channel_residues[ich][icoef],
648             -range, range - 1);
649         s->mclms_updates[s->mclms_recent] = WMASIGN(s->channel_residues[ich][icoef]);
650     }
651
652     if (s->mclms_recent == 0) {
653         memcpy(&s->mclms_prevvalues[order * num_channels],
654                s->mclms_prevvalues,
655                sizeof(int16_t) * order * num_channels);
656         memcpy(&s->mclms_updates[order * num_channels],
657                s->mclms_updates,
658                sizeof(int16_t) * order * num_channels);
659         s->mclms_recent = num_channels * order;
660     }
661 }
662
663 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
664 {
665     int ich, i;
666     int order        = s->mclms_order;
667     int num_channels = s->num_channels;
668
669     for (ich = 0; ich < num_channels; ich++) {
670         pred[ich] = 0;
671         if (!s->is_channel_coded[ich])
672             continue;
673         for (i = 0; i < order * num_channels; i++)
674             pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
675                          s->mclms_coeffs[i + order * num_channels * ich];
676         for (i = 0; i < ich; i++)
677             pred[ich] += s->channel_residues[i][icoef] *
678                          s->mclms_coeffs_cur[i + num_channels * ich];
679         pred[ich] += 1 << s->mclms_scaling - 1;
680         pred[ich] >>= s->mclms_scaling;
681         s->channel_residues[ich][icoef] += pred[ich];
682     }
683 }
684
685 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
686 {
687     int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
688     for (icoef = 0; icoef < tile_size; icoef++) {
689         mclms_predict(s, icoef, pred);
690         mclms_update(s, icoef, pred);
691     }
692 }
693
694 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input)
695 {
696     int recent = s->cdlms[ich][ilms].recent;
697     int range  = 1 << s->bits_per_sample - 1;
698     int order  = s->cdlms[ich][ilms].order;
699
700     if (recent)
701         recent--;
702     else {
703         memcpy(s->cdlms[ich][ilms].lms_prevvalues + order,
704                s->cdlms[ich][ilms].lms_prevvalues, sizeof(*s->cdlms[ich][ilms].lms_prevvalues) * order);
705         memcpy(s->cdlms[ich][ilms].lms_updates + order,
706                s->cdlms[ich][ilms].lms_updates, sizeof(*s->cdlms[ich][ilms].lms_updates) * order);
707         recent = order - 1;
708     }
709
710     s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
711     s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich];
712
713     s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2;
714     s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1;
715     s->cdlms[ich][ilms].recent = recent;
716     memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0,
717            sizeof(s->cdlms[ich][ilms].lms_updates) - 2*(recent+order));
718 }
719
720 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
721 {
722     int ilms, recent, icoef;
723     for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
724         recent = s->cdlms[ich][ilms].recent;
725         if (s->update_speed[ich] == 16)
726             continue;
727         if (s->bV3RTM) {
728             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
729                 s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
730         } else {
731             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
732                 s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
733         }
734     }
735     s->update_speed[ich] = 16;
736 }
737
738 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
739 {
740     int ilms, recent, icoef;
741     for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
742         recent = s->cdlms[ich][ilms].recent;
743         if (s->update_speed[ich] == 8)
744             continue;
745         if (s->bV3RTM)
746             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
747                 s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
748         else
749             for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
750                 s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
751     }
752     s->update_speed[ich] = 8;
753 }
754
755 static void revert_cdlms(WmallDecodeCtx *s, int ch,
756                          int coef_begin, int coef_end)
757 {
758     int icoef, pred, ilms, num_lms, residue, input;
759
760     num_lms = s->cdlms_ttl[ch];
761     for (ilms = num_lms - 1; ilms >= 0; ilms--) {
762         for (icoef = coef_begin; icoef < coef_end; icoef++) {
763             pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
764             residue = s->channel_residues[ch][icoef];
765             pred += s->dsp.scalarproduct_and_madd_int16(s->cdlms[ch][ilms].coefs,
766                                                         s->cdlms[ch][ilms].lms_prevvalues
767                                                             + s->cdlms[ch][ilms].recent,
768                                                         s->cdlms[ch][ilms].lms_updates
769                                                             + s->cdlms[ch][ilms].recent,
770                                                         FFALIGN(s->cdlms[ch][ilms].order,
771                                                                 WMALL_COEFF_PAD_SIZE),
772                                                         WMASIGN(residue));
773             input = residue + (pred >> s->cdlms[ch][ilms].scaling);
774             lms_update(s, ch, ilms, input);
775             s->channel_residues[ch][icoef] = input;
776         }
777     }
778     emms_c();
779 }
780
781 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
782 {
783     if (s->num_channels != 2)
784         return;
785     else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
786         int icoef;
787         for (icoef = 0; icoef < tile_size; icoef++) {
788             s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
789             s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
790         }
791     }
792 }
793
794 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
795 {
796     int ich, pred, i, j;
797     int16_t *filter_coeffs = s->acfilter_coeffs;
798     int scaling            = s->acfilter_scaling;
799     int order              = s->acfilter_order;
800
801     for (ich = 0; ich < s->num_channels; ich++) {
802         int *prevvalues = s->acfilter_prevvalues[ich];
803         for (i = 0; i < order; i++) {
804             pred = 0;
805             for (j = 0; j < order; j++) {
806                 if (i <= j)
807                     pred += filter_coeffs[j] * prevvalues[j - i];
808                 else
809                     pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
810             }
811             pred >>= scaling;
812             s->channel_residues[ich][i] += pred;
813         }
814         for (i = order; i < tile_size; i++) {
815             pred = 0;
816             for (j = 0; j < order; j++)
817                 pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
818             pred >>= scaling;
819             s->channel_residues[ich][i] += pred;
820         }
821         for (j = 0; j < order; j++)
822             prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
823     }
824 }
825
826 static int decode_subframe(WmallDecodeCtx *s)
827 {
828     int offset        = s->samples_per_frame;
829     int subframe_len  = s->samples_per_frame;
830     int total_samples = s->samples_per_frame * s->num_channels;
831     int i, j, rawpcm_tile, padding_zeroes, res;
832
833     s->subframe_offset = get_bits_count(&s->gb);
834
835     /* reset channel context and find the next block offset and size
836         == the next block of the channel with the smallest number of
837         decoded samples */
838     for (i = 0; i < s->num_channels; i++) {
839         if (offset > s->channel[i].decoded_samples) {
840             offset = s->channel[i].decoded_samples;
841             subframe_len =
842                 s->channel[i].subframe_len[s->channel[i].cur_subframe];
843         }
844     }
845
846     /* get a list of all channels that contain the estimated block */
847     s->channels_for_cur_subframe = 0;
848     for (i = 0; i < s->num_channels; i++) {
849         const int cur_subframe = s->channel[i].cur_subframe;
850         /* subtract already processed samples */
851         total_samples -= s->channel[i].decoded_samples;
852
853         /* and count if there are multiple subframes that match our profile */
854         if (offset == s->channel[i].decoded_samples &&
855             subframe_len == s->channel[i].subframe_len[cur_subframe]) {
856             total_samples -= s->channel[i].subframe_len[cur_subframe];
857             s->channel[i].decoded_samples +=
858                 s->channel[i].subframe_len[cur_subframe];
859             s->channel_indexes_for_cur_subframe[s->channels_for_cur_subframe] = i;
860             ++s->channels_for_cur_subframe;
861         }
862     }
863
864     /* check if the frame will be complete after processing the
865         estimated block */
866     if (!total_samples)
867         s->parsed_all_subframes = 1;
868
869
870     s->seekable_tile = get_bits1(&s->gb);
871     if (s->seekable_tile) {
872         clear_codec_buffers(s);
873
874         s->do_arith_coding    = get_bits1(&s->gb);
875         if (s->do_arith_coding) {
876             avpriv_request_sample(s->avctx, "Arithmetic coding");
877             return AVERROR_PATCHWELCOME;
878         }
879         s->do_ac_filter       = get_bits1(&s->gb);
880         s->do_inter_ch_decorr = get_bits1(&s->gb);
881         s->do_mclms           = get_bits1(&s->gb);
882
883         if (s->do_ac_filter)
884             decode_ac_filter(s);
885
886         if (s->do_mclms)
887             decode_mclms(s);
888
889         if ((res = decode_cdlms(s)) < 0)
890             return res;
891         s->movave_scaling = get_bits(&s->gb, 3);
892         s->quant_stepsize = get_bits(&s->gb, 8) + 1;
893
894         reset_codec(s);
895     } else if (!s->cdlms[0][0].order) {
896         av_log(s->avctx, AV_LOG_DEBUG,
897                "Waiting for seekable tile\n");
898         av_frame_unref(s->frame);
899         return -1;
900     }
901
902     rawpcm_tile = get_bits1(&s->gb);
903
904     for (i = 0; i < s->num_channels; i++)
905         s->is_channel_coded[i] = 1;
906
907     if (!rawpcm_tile) {
908         for (i = 0; i < s->num_channels; i++)
909             s->is_channel_coded[i] = get_bits1(&s->gb);
910
911         if (s->bV3RTM) {
912             // LPC
913             s->do_lpc = get_bits1(&s->gb);
914             if (s->do_lpc) {
915                 decode_lpc(s);
916                 avpriv_request_sample(s->avctx, "Expect wrong output since "
917                                       "inverse LPC filter");
918             }
919         } else
920             s->do_lpc = 0;
921     }
922
923
924     if (get_bits1(&s->gb))
925         padding_zeroes = get_bits(&s->gb, 5);
926     else
927         padding_zeroes = 0;
928
929     if (rawpcm_tile) {
930         int bits = s->bits_per_sample - padding_zeroes;
931         if (bits <= 0) {
932             av_log(s->avctx, AV_LOG_ERROR,
933                    "Invalid number of padding bits in raw PCM tile\n");
934             return AVERROR_INVALIDDATA;
935         }
936         ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
937                 "total %d bits, remain=%d\n", bits,
938                 bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
939         for (i = 0; i < s->num_channels; i++)
940             for (j = 0; j < subframe_len; j++)
941                 s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
942     } else {
943         for (i = 0; i < s->num_channels; i++)
944             if (s->is_channel_coded[i]) {
945                 decode_channel_residues(s, i, subframe_len);
946                 if (s->seekable_tile)
947                     use_high_update_speed(s, i);
948                 else
949                     use_normal_update_speed(s, i);
950                 revert_cdlms(s, i, 0, subframe_len);
951             } else {
952                 memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
953             }
954     }
955     if (s->do_mclms)
956         revert_mclms(s, subframe_len);
957     if (s->do_inter_ch_decorr)
958         revert_inter_ch_decorr(s, subframe_len);
959     if (s->do_ac_filter)
960         revert_acfilter(s, subframe_len);
961
962     /* Dequantize */
963     if (s->quant_stepsize != 1)
964         for (i = 0; i < s->num_channels; i++)
965             for (j = 0; j < subframe_len; j++)
966                 s->channel_residues[i][j] *= s->quant_stepsize;
967
968     /* Write to proper output buffer depending on bit-depth */
969     for (i = 0; i < s->channels_for_cur_subframe; i++) {
970         int c = s->channel_indexes_for_cur_subframe[i];
971         int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
972
973         for (j = 0; j < subframe_len; j++) {
974             if (s->bits_per_sample == 16) {
975                 *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
976             } else {
977                 *s->samples_32[c]++ = s->channel_residues[c][j] << (padding_zeroes + 8);
978             }
979         }
980     }
981
982     /* handled one subframe */
983     for (i = 0; i < s->channels_for_cur_subframe; i++) {
984         int c = s->channel_indexes_for_cur_subframe[i];
985         if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
986             av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
987             return AVERROR_INVALIDDATA;
988         }
989         ++s->channel[c].cur_subframe;
990     }
991     return 0;
992 }
993
994 /**
995  * @brief Decode one WMA frame.
996  * @param s codec context
997  * @return 0 if the trailer bit indicates that this is the last frame,
998  *         1 if there are additional frames
999  */
1000 static int decode_frame(WmallDecodeCtx *s)
1001 {
1002     GetBitContext* gb = &s->gb;
1003     int more_frames = 0, len = 0, i, ret;
1004
1005     s->frame->nb_samples = s->samples_per_frame;
1006     if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1007         /* return an error if no frame could be decoded at all */
1008         s->packet_loss = 1;
1009         s->frame->nb_samples = 0;
1010         return ret;
1011     }
1012     for (i = 0; i < s->num_channels; i++) {
1013         s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1014         s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1015     }
1016
1017     /* get frame length */
1018     if (s->len_prefix)
1019         len = get_bits(gb, s->log2_frame_size);
1020
1021     /* decode tile information */
1022     if ((ret = decode_tilehdr(s))) {
1023         s->packet_loss = 1;
1024         av_frame_unref(s->frame);
1025         return ret;
1026     }
1027
1028     /* read drc info */
1029     if (s->dynamic_range_compression)
1030         s->drc_gain = get_bits(gb, 8);
1031
1032     /* no idea what these are for, might be the number of samples
1033        that need to be skipped at the beginning or end of a stream */
1034     if (get_bits1(gb)) {
1035         int av_unused skip;
1036
1037         /* usually true for the first frame */
1038         if (get_bits1(gb)) {
1039             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1040             ff_dlog(s->avctx, "start skip: %i\n", skip);
1041         }
1042
1043         /* sometimes true for the last frame */
1044         if (get_bits1(gb)) {
1045             skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1046             ff_dlog(s->avctx, "end skip: %i\n", skip);
1047         }
1048
1049     }
1050
1051     /* reset subframe states */
1052     s->parsed_all_subframes = 0;
1053     for (i = 0; i < s->num_channels; i++) {
1054         s->channel[i].decoded_samples = 0;
1055         s->channel[i].cur_subframe    = 0;
1056     }
1057
1058     /* decode all subframes */
1059     while (!s->parsed_all_subframes) {
1060         int decoded_samples = s->channel[0].decoded_samples;
1061         if (decode_subframe(s) < 0) {
1062             s->packet_loss = 1;
1063             if (s->frame->nb_samples)
1064                 s->frame->nb_samples = decoded_samples;
1065             return 0;
1066         }
1067     }
1068
1069     ff_dlog(s->avctx, "Frame done\n");
1070
1071     s->skip_frame = 0;
1072
1073     if (s->len_prefix) {
1074         if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1075             /* FIXME: not sure if this is always an error */
1076             av_log(s->avctx, AV_LOG_ERROR,
1077                    "frame[%"PRIu32"] would have to skip %i bits\n",
1078                    s->frame_num,
1079                    len - (get_bits_count(gb) - s->frame_offset) - 1);
1080             s->packet_loss = 1;
1081             return 0;
1082         }
1083
1084         /* skip the rest of the frame data */
1085         skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1086     }
1087
1088     /* decode trailer bit */
1089     more_frames = get_bits1(gb);
1090     ++s->frame_num;
1091     return more_frames;
1092 }
1093
1094 /**
1095  * @brief Calculate remaining input buffer length.
1096  * @param s  codec context
1097  * @param gb bitstream reader context
1098  * @return remaining size in bits
1099  */
1100 static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
1101 {
1102     return s->buf_bit_size - get_bits_count(gb);
1103 }
1104
1105 /**
1106  * @brief Fill the bit reservoir with a (partial) frame.
1107  * @param s      codec context
1108  * @param gb     bitstream reader context
1109  * @param len    length of the partial frame
1110  * @param append decides whether to reset the buffer or not
1111  */
1112 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1113                       int append)
1114 {
1115     int buflen;
1116     PutBitContext tmp;
1117
1118     /* when the frame data does not need to be concatenated, the input buffer
1119         is reset and additional bits from the previous frame are copied
1120         and skipped later so that a fast byte copy is possible */
1121
1122     if (!append) {
1123         s->frame_offset   = get_bits_count(gb) & 7;
1124         s->num_saved_bits = s->frame_offset;
1125         init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1126     }
1127
1128     buflen = (s->num_saved_bits + len + 8) >> 3;
1129
1130     if (len <= 0 || buflen > MAX_FRAMESIZE) {
1131         avpriv_request_sample(s->avctx, "Too small input buffer");
1132         s->packet_loss = 1;
1133         return;
1134     }
1135
1136     s->num_saved_bits += len;
1137     if (!append) {
1138         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1139                          s->num_saved_bits);
1140     } else {
1141         int align = 8 - (get_bits_count(gb) & 7);
1142         align = FFMIN(align, len);
1143         put_bits(&s->pb, align, get_bits(gb, align));
1144         len -= align;
1145         avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1146     }
1147     skip_bits_long(gb, len);
1148
1149     tmp = s->pb;
1150     flush_put_bits(&tmp);
1151
1152     init_get_bits(&s->gb, s->frame_data, s->num_saved_bits);
1153     skip_bits(&s->gb, s->frame_offset);
1154 }
1155
1156 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1157                          AVPacket* avpkt)
1158 {
1159     WmallDecodeCtx *s = avctx->priv_data;
1160     GetBitContext* gb  = &s->pgb;
1161     const uint8_t* buf = avpkt->data;
1162     int buf_size       = avpkt->size;
1163     int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1164
1165     s->frame->nb_samples = 0;
1166
1167     if (s->packet_done || s->packet_loss) {
1168         s->packet_done = 0;
1169
1170         if (!buf_size)
1171             return 0;
1172         /* sanity check for the buffer length */
1173         if (buf_size < avctx->block_align) {
1174             av_log(avctx, AV_LOG_ERROR, "buf size %d invalid\n", buf_size);
1175             return AVERROR_INVALIDDATA;
1176         }
1177
1178         s->next_packet_start = buf_size - avctx->block_align;
1179         buf_size             = avctx->block_align;
1180         s->buf_bit_size      = buf_size << 3;
1181
1182         /* parse packet header */
1183         init_get_bits(gb, buf, s->buf_bit_size);
1184         packet_sequence_number = get_bits(gb, 4);
1185         skip_bits(gb, 1);   // Skip seekable_frame_in_packet, currently ununused
1186         spliced_packet = get_bits1(gb);
1187         if (spliced_packet)
1188             avpriv_request_sample(avctx, "Bitstream splicing");
1189
1190         /* get number of bits that need to be added to the previous frame */
1191         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1192
1193         /* check for packet loss */
1194         if (!s->packet_loss &&
1195             ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1196             s->packet_loss = 1;
1197             av_log(avctx, AV_LOG_ERROR,
1198                    "Packet loss detected! seq %"PRIx8" vs %x\n",
1199                    s->packet_sequence_number, packet_sequence_number);
1200         }
1201         s->packet_sequence_number = packet_sequence_number;
1202
1203         if (num_bits_prev_frame > 0) {
1204             int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1205             if (num_bits_prev_frame >= remaining_packet_bits) {
1206                 num_bits_prev_frame = remaining_packet_bits;
1207                 s->packet_done = 1;
1208             }
1209
1210             /* Append the previous frame data to the remaining data from the
1211              * previous packet to create a full frame. */
1212             save_bits(s, gb, num_bits_prev_frame, 1);
1213
1214             /* decode the cross packet frame if it is valid */
1215             if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1216                 decode_frame(s);
1217         } else if (s->num_saved_bits - s->frame_offset) {
1218             ff_dlog(avctx, "ignoring %x previously saved bits\n",
1219                     s->num_saved_bits - s->frame_offset);
1220         }
1221
1222         if (s->packet_loss) {
1223             /* Reset number of saved bits so that the decoder does not start
1224              * to decode incomplete frames in the s->len_prefix == 0 case. */
1225             s->num_saved_bits = 0;
1226             s->packet_loss    = 0;
1227             init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1228         }
1229
1230     } else {
1231         int frame_size;
1232
1233         s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1234         init_get_bits(gb, avpkt->data, s->buf_bit_size);
1235         skip_bits(gb, s->packet_offset);
1236
1237         if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1238             (frame_size = show_bits(gb, s->log2_frame_size)) &&
1239             frame_size <= remaining_bits(s, gb)) {
1240             save_bits(s, gb, frame_size, 0);
1241             s->packet_done = !decode_frame(s);
1242         } else if (!s->len_prefix
1243                    && s->num_saved_bits > get_bits_count(&s->gb)) {
1244             /* when the frames do not have a length prefix, we don't know the
1245              * compressed length of the individual frames however, we know what
1246              * part of a new packet belongs to the previous frame therefore we
1247              * save the incoming packet first, then we append the "previous
1248              * frame" data from the next packet so that we get a buffer that
1249              * only contains full frames */
1250             s->packet_done = !decode_frame(s);
1251         } else {
1252             s->packet_done = 1;
1253         }
1254     }
1255
1256     if (s->packet_done && !s->packet_loss &&
1257         remaining_bits(s, gb) > 0) {
1258         /* save the rest of the data so that it can be decoded
1259          * with the next packet */
1260         save_bits(s, gb, remaining_bits(s, gb), 0);
1261     }
1262
1263     *got_frame_ptr   = s->frame->nb_samples > 0;
1264     av_frame_move_ref(data, s->frame);
1265
1266     s->packet_offset = get_bits_count(gb) & 7;
1267
1268     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1269 }
1270
1271 static void flush(AVCodecContext *avctx)
1272 {
1273     WmallDecodeCtx *s    = avctx->priv_data;
1274     s->packet_loss       = 1;
1275     s->packet_done       = 0;
1276     s->num_saved_bits    = 0;
1277     s->frame_offset      = 0;
1278     s->next_packet_start = 0;
1279     s->cdlms[0][0].order = 0;
1280     s->frame->nb_samples = 0;
1281     init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
1282 }
1283
1284 static av_cold int decode_close(AVCodecContext *avctx)
1285 {
1286     WmallDecodeCtx *s = avctx->priv_data;
1287
1288     av_frame_free(&s->frame);
1289
1290     return 0;
1291 }
1292
1293 AVCodec ff_wmalossless_decoder = {
1294     .name           = "wmalossless",
1295     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1296     .type           = AVMEDIA_TYPE_AUDIO,
1297     .id             = AV_CODEC_ID_WMALOSSLESS,
1298     .priv_data_size = sizeof(WmallDecodeCtx),
1299     .init           = decode_init,
1300     .close          = decode_close,
1301     .decode         = decode_packet,
1302     .flush          = flush,
1303     .capabilities   = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1304     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1305                                                       AV_SAMPLE_FMT_S32P,
1306                                                       AV_SAMPLE_FMT_NONE },
1307 };