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