OSDN Git Service

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