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