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