FFmpeg  2.1.1
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;
130  int16_t mclms_coeffs[128];
131  int16_t mclms_coeffs_cur[4];
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] = in_use = 1;
352  } else {
353  if (get_bits1(&s->gb))
354  contains_subframe[c] = in_use = 1;
355  }
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  2 * order * num_channels);
656  memcpy(&s->mclms_updates[order * num_channels],
657  s->mclms_updates,
658  2 * 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 (decode_tilehdr(s)) {
1042  s->packet_loss = 1;
1043  return 0;
1044  }
1045 
1046  /* read drc info */
1048  s->drc_gain = get_bits(gb, 8);
1049 
1050  /* no idea what these are for, might be the number of samples
1051  that need to be skipped at the beginning or end of a stream */
1052  if (get_bits1(gb)) {
1053  int av_unused skip;
1054 
1055  /* usually true for the first frame */
1056  if (get_bits1(gb)) {
1057  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1058  av_dlog(s->avctx, "start skip: %i\n", skip);
1059  }
1060 
1061  /* sometimes true for the last frame */
1062  if (get_bits1(gb)) {
1063  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1064  av_dlog(s->avctx, "end skip: %i\n", skip);
1065  }
1066 
1067  }
1068 
1069  /* reset subframe states */
1070  s->parsed_all_subframes = 0;
1071  for (i = 0; i < s->num_channels; i++) {
1072  s->channel[i].decoded_samples = 0;
1073  s->channel[i].cur_subframe = 0;
1074  }
1075 
1076  /* decode all subframes */
1077  while (!s->parsed_all_subframes) {
1078  if (decode_subframe(s) < 0) {
1079  s->packet_loss = 1;
1080  return 0;
1081  }
1082  }
1083 
1084  av_dlog(s->avctx, "Frame done\n");
1085 
1086  if (s->skip_frame)
1087  s->skip_frame = 0;
1088 
1089  if (s->len_prefix) {
1090  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1091  /* FIXME: not sure if this is always an error */
1093  "frame[%i] would have to skip %i bits\n", s->frame_num,
1094  len - (get_bits_count(gb) - s->frame_offset) - 1);
1095  s->packet_loss = 1;
1096  return 0;
1097  }
1098 
1099  /* skip the rest of the frame data */
1100  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1101  }
1102 
1103  /* decode trailer bit */
1104  more_frames = get_bits1(gb);
1105  ++s->frame_num;
1106  return more_frames;
1107 }
1108 
1109 /**
1110  * @brief Calculate remaining input buffer length.
1111  * @param s codec context
1112  * @param gb bitstream reader context
1113  * @return remaining size in bits
1114  */
1116 {
1117  return s->buf_bit_size - get_bits_count(gb);
1118 }
1119 
1120 /**
1121  * @brief Fill the bit reservoir with a (partial) frame.
1122  * @param s codec context
1123  * @param gb bitstream reader context
1124  * @param len length of the partial frame
1125  * @param append decides whether to reset the buffer or not
1126  */
1127 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1128  int append)
1129 {
1130  int buflen;
1131  PutBitContext tmp;
1132 
1133  /* when the frame data does not need to be concatenated, the input buffer
1134  is reset and additional bits from the previous frame are copied
1135  and skipped later so that a fast byte copy is possible */
1136 
1137  if (!append) {
1138  s->frame_offset = get_bits_count(gb) & 7;
1139  s->num_saved_bits = s->frame_offset;
1141  }
1142 
1143  buflen = (s->num_saved_bits + len + 8) >> 3;
1144 
1145  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1146  avpriv_request_sample(s->avctx, "Too small input buffer");
1147  s->packet_loss = 1;
1148  return;
1149  }
1150 
1151  s->num_saved_bits += len;
1152  if (!append) {
1153  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1154  s->num_saved_bits);
1155  } else {
1156  int align = 8 - (get_bits_count(gb) & 7);
1157  align = FFMIN(align, len);
1158  put_bits(&s->pb, align, get_bits(gb, align));
1159  len -= align;
1160  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1161  }
1162  skip_bits_long(gb, len);
1163 
1164  tmp = s->pb;
1165  flush_put_bits(&tmp);
1166 
1168  skip_bits(&s->gb, s->frame_offset);
1169 }
1170 
1171 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1172  AVPacket* avpkt)
1173 {
1174  WmallDecodeCtx *s = avctx->priv_data;
1175  GetBitContext* gb = &s->pgb;
1176  const uint8_t* buf = avpkt->data;
1177  int buf_size = avpkt->size;
1178  int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1179 
1180  s->frame->nb_samples = 0;
1181 
1182  if (s->packet_done || s->packet_loss) {
1183  s->packet_done = 0;
1184 
1185  if (!buf_size)
1186  return 0;
1187  /* sanity check for the buffer length */
1188  if (buf_size < avctx->block_align) {
1189  av_log(avctx, AV_LOG_ERROR, "buf size %d invalid\n", buf_size);
1190  return AVERROR_INVALIDDATA;
1191  }
1192 
1193  s->next_packet_start = buf_size - avctx->block_align;
1194  buf_size = avctx->block_align;
1195  s->buf_bit_size = buf_size << 3;
1196 
1197  /* parse packet header */
1198  init_get_bits(gb, buf, s->buf_bit_size);
1199  packet_sequence_number = get_bits(gb, 4);
1200  skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1201  spliced_packet = get_bits1(gb);
1202  if (spliced_packet)
1203  avpriv_request_sample(avctx, "Bitstream splicing");
1204 
1205  /* get number of bits that need to be added to the previous frame */
1206  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1207 
1208  /* check for packet loss */
1209  if (!s->packet_loss &&
1210  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1211  s->packet_loss = 1;
1212  av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1213  s->packet_sequence_number, packet_sequence_number);
1214  }
1215  s->packet_sequence_number = packet_sequence_number;
1216 
1217  if (num_bits_prev_frame > 0) {
1218  int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1219  if (num_bits_prev_frame >= remaining_packet_bits) {
1220  num_bits_prev_frame = remaining_packet_bits;
1221  s->packet_done = 1;
1222  }
1223 
1224  /* Append the previous frame data to the remaining data from the
1225  * previous packet to create a full frame. */
1226  save_bits(s, gb, num_bits_prev_frame, 1);
1227 
1228  /* decode the cross packet frame if it is valid */
1229  if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1230  decode_frame(s);
1231  } else if (s->num_saved_bits - s->frame_offset) {
1232  av_dlog(avctx, "ignoring %x previously saved bits\n",
1233  s->num_saved_bits - s->frame_offset);
1234  }
1235 
1236  if (s->packet_loss) {
1237  /* Reset number of saved bits so that the decoder does not start
1238  * to decode incomplete frames in the s->len_prefix == 0 case. */
1239  s->num_saved_bits = 0;
1240  s->packet_loss = 0;
1242  }
1243 
1244  } else {
1245  int frame_size;
1246 
1247  s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1248  init_get_bits(gb, avpkt->data, s->buf_bit_size);
1249  skip_bits(gb, s->packet_offset);
1250 
1251  if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1252  (frame_size = show_bits(gb, s->log2_frame_size)) &&
1253  frame_size <= remaining_bits(s, gb)) {
1254  save_bits(s, gb, frame_size, 0);
1255  s->packet_done = !decode_frame(s);
1256  } else if (!s->len_prefix
1257  && s->num_saved_bits > get_bits_count(&s->gb)) {
1258  /* when the frames do not have a length prefix, we don't know the
1259  * compressed length of the individual frames however, we know what
1260  * part of a new packet belongs to the previous frame therefore we
1261  * save the incoming packet first, then we append the "previous
1262  * frame" data from the next packet so that we get a buffer that
1263  * only contains full frames */
1264  s->packet_done = !decode_frame(s);
1265  } else {
1266  s->packet_done = 1;
1267  }
1268  }
1269 
1270  if (s->packet_done && !s->packet_loss &&
1271  remaining_bits(s, gb) > 0) {
1272  /* save the rest of the data so that it can be decoded
1273  * with the next packet */
1274  save_bits(s, gb, remaining_bits(s, gb), 0);
1275  }
1276 
1277  *got_frame_ptr = s->frame->nb_samples > 0;
1278  av_frame_move_ref(data, s->frame);
1279 
1280  s->packet_offset = get_bits_count(gb) & 7;
1281 
1282  return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1283 }
1284 
1285 static void flush(AVCodecContext *avctx)
1286 {
1287  WmallDecodeCtx *s = avctx->priv_data;
1288  s->packet_loss = 1;
1289  s->packet_done = 0;
1290  s->num_saved_bits = 0;
1291  s->frame_offset = 0;
1292  s->next_packet_start = 0;
1293  s->cdlms[0][0].order = 0;
1294  s->frame->nb_samples = 0;
1296 }
1297 
1299 {
1300  WmallDecodeCtx *s = avctx->priv_data;
1301 
1302  av_frame_free(&s->frame);
1303 
1304  return 0;
1305 }
1306 
1308  .name = "wmalossless",
1309  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1310  .type = AVMEDIA_TYPE_AUDIO,
1312  .priv_data_size = sizeof(WmallDecodeCtx),
1313  .init = decode_init,
1314  .close = decode_close,
1315  .decode = decode_packet,
1316  .flush = flush,
1318  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1321 };
static void decode_ac_filter(WmallDecodeCtx *s)
int16_t prev_block_len
length of the previous block
int16_t lms_prevvalues[MAX_ORDER *2]
uint8_t subframe_len_bits
number of bits used for the subframe length
uint8_t bits_per_sample
integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
const char * s
Definition: avisynth_c.h:668
#define AVERROR_PATCHWELCOME
static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
uint8_t max_subframe_len_bit
flag indicating that the subframe is of maximum size when the first subframe length bit is 1 ...
uint8_t max_num_subframes
uint16_t subframe_offsets[MAX_SUBFRAMES]
subframe positions in the current frame
int16_t mclms_updates[WMALL_MAX_CHANNELS *2 *32]
int32_t * samples_32[WMALL_MAX_CHANNELS]
current samplebuffer pointer (24-bit)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:212
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t drc_gain
gain for the DRC tool
#define AV_RL16(x)
Definition: intreadwrite.h:245
int acfilter_prevvalues[WMALL_MAX_CHANNELS][16]
int size
Definition: avcodec.h:1064
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:140
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:63
const uint8_t * buffer
Definition: get_bits.h:55
PutBitContext pb
context for filling the frame_data buffer
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
uint8_t cur_subframe
current subframe number
static void decode_mclms(WmallDecodeCtx *s)
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int cdlms_ttl[WMALL_MAX_CHANNELS]
static int decode_subframe_length(WmallDecodeCtx *s, int offset)
Decode the subframe length.
int channel_coeffs[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE]
static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
AVCodec.
Definition: avcodec.h:2922
#define av_cold
Definition: avcodec.h:653
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1910
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:240
#define MAX_ORDER
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
int8_t channels_for_cur_subframe
number of channels that contain the subframe
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:370
uint8_t packet_sequence_number
current packet number
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
int16_t subframe_len
current subframe length
uint16_t decoded_samples
number of already processed samples
GetBitContext pgb
bitstream reader context for the packet
static uint8_t * res
Definition: ffhash.c:43
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int quant_step
quantization step for the current subframe
uint8_t bits
Definition: crc.c:260
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1881
uint8_t
signed 32 bits, planar
Definition: samplefmt.h:59
int8_t num_channels
number of channels in the stream (same as AVCodecContext.num_channels)
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
static void reset_codec(WmallDecodeCtx *s)
Reset filter parameters and transient area at new seekable tile.
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
int update_speed[WMALL_MAX_CHANNELS]
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
int is_channel_coded[WMALL_MAX_CHANNELS]
int frame_offset
frame offset in the bit reservoir
const char data[16]
Definition: mxf.c:68
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
uint16_t min_samples_per_subframe
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:207
bitstream reader API header.
AVFrame * frame
frame-specific decoder context for a single channel
int8_t lfe_channel
lfe channel index
uint8_t do_arith_coding
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
int buf_bit_size
buffer size in bits
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:583
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
static const uint16_t mask[17]
Definition: lzw.c:37
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:769
int16_t coefs[MAX_ORDER]
static av_cold int decode_init(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:110
static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS]
uint8_t frame_data[MAX_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE]
compressed frame data
static int decode_subframe(WmallDecodeCtx *s)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
static int decode_cdlms(WmallDecodeCtx *s)
uint8_t transmit_coefs
static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
#define WMALL_BLOCK_MAX_SIZE
maximum block size
AVCodecContext * avctx
Libavcodec external API header.
#define av_unused
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
Definition: avcodec.h:697
int channel_residues[WMALL_MAX_CHANNELS][WMALL_BLOCK_MAX_SIZE]
int8_t parsed_all_subframes
all subframes decoded?
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
static int decode_tilehdr(WmallDecodeCtx *s)
Decode how the data in the frame is split into subframes.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
static void use_high_update_speed(WmallDecodeCtx *s, int ich)
#define MAX_SUBFRAMES
max number of subframes per channel
static av_cold int decode_close(AVCodecContext *avctx)
uint8_t packet_loss
set in case of bitstream error
static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
static void clear_codec_buffers(WmallDecodeCtx *s)
uint16_t log2_frame_size
ret
Definition: avfilter.c:961
uint32_t decode_flags
used compression features
int32_t
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:282
#define FFMIN(a, b)
Definition: avcodec.h:925
static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
int8_t skip_frame
skip output step
uint8_t packet_offset
offset to the frame in the packet
static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
int16_t mclms_coeffs_cur[4]
uint8_t packet_done
set when a packet is fully decoded
main decoder context
static void revert_cdlms(WmallDecodeCtx *s, int ch, int coef_begin, int coef_end)
uint8_t do_ac_filter
static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
uint16_t samples_per_frame
number of samples to output
static const float pred[4]
Definition: siprdata.h:259
const AVS_VideoInfo int align
Definition: avisynth_c.h:695
int next_packet_start
start offset of the next WMA packet in the demuxer packet
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
#define FFMAX(a, b)
Definition: avcodec.h:923
int sample_rate
samples per second
Definition: avcodec.h:1873
#define WMALL_MAX_CHANNELS
current decoder limitations
main external API structure.
Definition: avcodec.h:1146
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:538
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:373
int16_t * samples_16[WMALL_MAX_CHANNELS]
current samplebuffer pointer (16-bit)
static void decode_lpc(WmallDecodeCtx *s)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
uint8_t * data
Definition: avcodec.h:1063
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:405
int16_t mclms_prevvalues[WMALL_MAX_CHANNELS *2 *32]
int64_t acfilter_coeffs[16]
uint8_t num_subframes
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:332
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
GetBitContext gb
bitstream reader context
void * priv_data
Definition: avcodec.h:1182
uint32_t frame_num
current frame number (not used for decoding)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
Definition: wma_common.c:31
uint8_t do_inter_ch_decorr
#define CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:792
uint16_t subframe_len[MAX_SUBFRAMES]
subframe length in samples
int ave_sum[WMALL_MAX_CHANNELS]
WmallChannelCtx channel[WMALL_MAX_CHANNELS]
per channel data
struct WmallDecodeCtx::@106 cdlms[WMALL_MAX_CHANNELS][9]
#define MAX_FRAMESIZE
maximum compressed frame size
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:89
int dynamic_range_compression
frame contains DRC data
int transient_counter
number of transient samples from the beginning of the transient zone
static double c[64]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:54
static void save_bits(WmallDecodeCtx *s, GetBitContext *gb, int len, int append)
Fill the bit reservoir with a (partial) frame.
int transient_pos[WMALL_MAX_CHANNELS]
#define AVERROR_INVALIDDATA
int16_t mclms_coeffs[128]
#define AV_RL32(x)
Definition: intreadwrite.h:275
int len
int channels
number of audio channels
Definition: avcodec.h:1874
#define av_log2
Definition: intmath.h:89
AVCodec ff_wmalossless_decoder
#define AVERROR(e)
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int16_t lms_updates[MAX_ORDER *2]
static void flush(AVCodecContext *avctx)
int8_t acfilter_scaling
int len_prefix
frame is prefixed with its length
static int decode_frame(WmallDecodeCtx *s)
Decode one WMA frame.
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
signed 16 bits, planar
Definition: samplefmt.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int transient[WMALL_MAX_CHANNELS]
int num_saved_bits
saved number of bits
int subframe_offset
subframe offset in the bit reservoir
This structure stores compressed data.
Definition: avcodec.h:1040
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:150
int lpc_coefs[WMALL_MAX_CHANNELS][40]
for(j=16;j >0;--j)
static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input, int residue)
static void revert_mclms(WmallDecodeCtx *s, int tile_size)
bitstream writer API