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