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  s->avctx = avctx;
183 
184  if (avctx->extradata_size >= 18) {
185  s->decode_flags = AV_RL16(edata_ptr + 14);
186  channel_mask = AV_RL32(edata_ptr + 2);
187  s->bits_per_sample = AV_RL16(edata_ptr);
188  if (s->bits_per_sample == 16)
190  else if (s->bits_per_sample == 24) {
192  avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
193  return AVERROR_PATCHWELCOME;
194  } else {
195  av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
196  s->bits_per_sample);
197  return AVERROR_INVALIDDATA;
198  }
199  /* dump the extradata */
200  for (i = 0; i < avctx->extradata_size; i++)
201  av_dlog(avctx, "[%x] ", avctx->extradata[i]);
202  av_dlog(avctx, "\n");
203 
204  } else {
205  avpriv_request_sample(avctx, "Unsupported extradata size");
206  return AVERROR_PATCHWELCOME;
207  }
208 
209  /* generic init */
210  s->log2_frame_size = av_log2(avctx->block_align) + 4;
211 
212  /* frame info */
213  s->skip_frame = 1; /* skip first frame */
214  s->packet_loss = 1;
215  s->len_prefix = s->decode_flags & 0x40;
216 
217  /* get frame len */
219  3, s->decode_flags);
221 
222  /* init previous block len */
223  for (i = 0; i < avctx->channels; i++)
225 
226  /* subframe info */
227  log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
228  s->max_num_subframes = 1 << log2_max_num_subframes;
229  s->max_subframe_len_bit = 0;
230  s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
231 
234  s->bV3RTM = s->decode_flags & 0x100;
235 
236  if (s->max_num_subframes > MAX_SUBFRAMES) {
237  av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
238  s->max_num_subframes);
239  return AVERROR_INVALIDDATA;
240  }
241 
242  s->num_channels = avctx->channels;
243 
244  /* extract lfe channel position */
245  s->lfe_channel = -1;
246 
247  if (channel_mask & 8) {
248  unsigned int mask;
249  for (mask = 1; mask < 16; mask <<= 1)
250  if (channel_mask & mask)
251  ++s->lfe_channel;
252  }
253 
254  if (s->num_channels < 0) {
255  av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
256  s->num_channels);
257  return AVERROR_INVALIDDATA;
258  } else if (s->num_channels > WMALL_MAX_CHANNELS) {
259  avpriv_request_sample(avctx,
260  "More than %d channels", WMALL_MAX_CHANNELS);
261  return AVERROR_PATCHWELCOME;
262  }
263 
265  avctx->coded_frame = &s->frame;
266  avctx->channel_layout = channel_mask;
267  return 0;
268 }
269 
270 /**
271  * @brief Decode the subframe length.
272  * @param s context
273  * @param offset sample offset in the frame
274  * @return decoded subframe length on success, < 0 in case of an error
275  */
277 {
278  int frame_len_ratio, subframe_len, len;
279 
280  /* no need to read from the bitstream when only one length is possible */
281  if (offset == s->samples_per_frame - s->min_samples_per_subframe)
282  return s->min_samples_per_subframe;
283 
284  len = av_log2(s->max_num_subframes - 1) + 1;
285  frame_len_ratio = get_bits(&s->gb, len);
286  subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
287 
288  /* sanity check the length */
289  if (subframe_len < s->min_samples_per_subframe ||
290  subframe_len > s->samples_per_frame) {
291  av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
292  subframe_len);
293  return AVERROR_INVALIDDATA;
294  }
295  return subframe_len;
296 }
297 
298 /**
299  * @brief Decode how the data in the frame is split into subframes.
300  * Every WMA frame contains the encoded data for a fixed number of
301  * samples per channel. The data for every channel might be split
302  * into several subframes. This function will reconstruct the list of
303  * subframes for every channel.
304  *
305  * If the subframes are not evenly split, the algorithm estimates the
306  * channels with the lowest number of total samples.
307  * Afterwards, for each of these channels a bit is read from the
308  * bitstream that indicates if the channel contains a subframe with the
309  * next subframe size that is going to be read from the bitstream or not.
310  * If a channel contains such a subframe, the subframe size gets added to
311  * the channel's subframe list.
312  * The algorithm repeats these steps until the frame is properly divided
313  * between the individual channels.
314  *
315  * @param s context
316  * @return 0 on success, < 0 in case of an error
317  */
319 {
320  uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
321  uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
322  int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
323  int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
324  int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
325  int c, tile_aligned;
326 
327  /* reset tiling information */
328  for (c = 0; c < s->num_channels; c++)
329  s->channel[c].num_subframes = 0;
330 
331  tile_aligned = get_bits1(&s->gb);
332  if (s->max_num_subframes == 1 || tile_aligned)
333  fixed_channel_layout = 1;
334 
335  /* loop until the frame data is split between the subframes */
336  do {
337  int subframe_len, in_use = 0;
338 
339  /* check which channels contain the subframe */
340  for (c = 0; c < s->num_channels; c++) {
341  if (num_samples[c] == min_channel_len) {
342  if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
343  (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
344  contains_subframe[c] = in_use = 1;
345  } else {
346  if (get_bits1(&s->gb))
347  contains_subframe[c] = in_use = 1;
348  }
349  } else
350  contains_subframe[c] = 0;
351  }
352 
353  if (!in_use) {
355  "Found empty subframe\n");
356  return AVERROR_INVALIDDATA;
357  }
358 
359  /* get subframe length, subframe_len == 0 is not allowed */
360  if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
361  return AVERROR_INVALIDDATA;
362  /* add subframes to the individual channels and find new min_channel_len */
363  min_channel_len += subframe_len;
364  for (c = 0; c < s->num_channels; c++) {
365  WmallChannelCtx *chan = &s->channel[c];
366 
367  if (contains_subframe[c]) {
368  if (chan->num_subframes >= MAX_SUBFRAMES) {
370  "broken frame: num subframes > 31\n");
371  return AVERROR_INVALIDDATA;
372  }
373  chan->subframe_len[chan->num_subframes] = subframe_len;
374  num_samples[c] += subframe_len;
375  ++chan->num_subframes;
376  if (num_samples[c] > s->samples_per_frame) {
377  av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
378  "channel len(%d) > samples_per_frame(%d)\n",
379  num_samples[c], s->samples_per_frame);
380  return AVERROR_INVALIDDATA;
381  }
382  } else if (num_samples[c] <= min_channel_len) {
383  if (num_samples[c] < min_channel_len) {
384  channels_for_cur_subframe = 0;
385  min_channel_len = num_samples[c];
386  }
388  }
389  }
390  } while (min_channel_len < s->samples_per_frame);
391 
392  for (c = 0; c < s->num_channels; c++) {
393  int i, offset = 0;
394  for (i = 0; i < s->channel[c].num_subframes; i++) {
396  offset += s->channel[c].subframe_len[i];
397  }
398  }
399 
400  return 0;
401 }
402 
404 {
405  int i;
406  s->acfilter_order = get_bits(&s->gb, 4) + 1;
407  s->acfilter_scaling = get_bits(&s->gb, 4);
408 
409  for (i = 0; i < s->acfilter_order; i++)
410  s->acfilter_coeffs[i] = (s->acfilter_scaling ?
411  get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
412 }
413 
415 {
416  s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
417  s->mclms_scaling = get_bits(&s->gb, 4);
418  if (get_bits1(&s->gb)) {
419  int i, send_coef_bits;
420  int cbits = av_log2(s->mclms_scaling + 1);
421  if (1 << cbits < s->mclms_scaling + 1)
422  cbits++;
423 
424  send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
425 
426  for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
427  s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
428 
429  for (i = 0; i < s->num_channels; i++) {
430  int c;
431  for (c = 0; c < i; c++)
432  s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
433  }
434  }
435 }
436 
438 {
439  int c, i;
440  int cdlms_send_coef = get_bits1(&s->gb);
441 
442  for (c = 0; c < s->num_channels; c++) {
443  s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
444  for (i = 0; i < s->cdlms_ttl[c]; i++) {
445  s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
446  if (s->cdlms[c][i].order > MAX_ORDER) {
448  "Order[%d][%d] %d > max (%d), not supported\n",
449  c, i, s->cdlms[c][i].order, MAX_ORDER);
450  s->cdlms[0][0].order = 0;
451  return AVERROR_INVALIDDATA;
452  }
453  }
454 
455  for (i = 0; i < s->cdlms_ttl[c]; i++)
456  s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
457 
458  if (cdlms_send_coef) {
459  for (i = 0; i < s->cdlms_ttl[c]; i++) {
460  int cbits, shift_l, shift_r, j;
461  cbits = av_log2(s->cdlms[c][i].order);
462  if ((1 << cbits) < s->cdlms[c][i].order)
463  cbits++;
464  s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
465 
466  cbits = av_log2(s->cdlms[c][i].scaling + 1);
467  if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
468  cbits++;
469 
470  s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
471  shift_l = 32 - s->cdlms[c][i].bitsend;
472  shift_r = 32 - s->cdlms[c][i].scaling - 2;
473  for (j = 0; j < s->cdlms[c][i].coefsend; j++)
474  s->cdlms[c][i].coefs[j] =
475  (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
476  }
477  }
478  }
479 
480  return 0;
481 }
482 
483 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
484 {
485  int i = 0;
486  unsigned int ave_mean;
487  s->transient[ch] = get_bits1(&s->gb);
488  if (s->transient[ch]) {
489  s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
490  if (s->transient_pos[ch])
491  s->transient[ch] = 0;
492  s->channel[ch].transient_counter =
494  } else if (s->channel[ch].transient_counter)
495  s->transient[ch] = 1;
496 
497  if (s->seekable_tile) {
498  ave_mean = get_bits(&s->gb, s->bits_per_sample);
499  s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
500  }
501 
502  if (s->seekable_tile) {
503  if (s->do_inter_ch_decorr)
504  s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
505  else
506  s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
507  i++;
508  }
509  for (; i < tile_size; i++) {
510  int quo = 0, rem, rem_bits, residue;
511  while(get_bits1(&s->gb)) {
512  quo++;
513  if (get_bits_left(&s->gb) <= 0)
514  return -1;
515  }
516  if (quo >= 32)
517  quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
518 
519  ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
520  if (ave_mean <= 1)
521  residue = quo;
522  else {
523  rem_bits = av_ceil_log2(ave_mean);
524  rem = get_bits_long(&s->gb, rem_bits);
525  residue = (quo << rem_bits) + rem;
526  }
527 
528  s->ave_sum[ch] = residue + s->ave_sum[ch] -
529  (s->ave_sum[ch] >> s->movave_scaling);
530 
531  if (residue & 1)
532  residue = -(residue >> 1) - 1;
533  else
534  residue = residue >> 1;
535  s->channel_residues[ch][i] = residue;
536  }
537 
538  return 0;
539 
540 }
541 
543 {
544  int ch, i, cbits;
545  s->lpc_order = get_bits(&s->gb, 5) + 1;
546  s->lpc_scaling = get_bits(&s->gb, 4);
547  s->lpc_intbits = get_bits(&s->gb, 3) + 1;
548  cbits = s->lpc_scaling + s->lpc_intbits;
549  for (ch = 0; ch < s->num_channels; ch++)
550  for (i = 0; i < s->lpc_order; i++)
551  s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
552 }
553 
555 {
556  int ich, ilms;
557 
558  memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
559  memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
560  memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
561 
562  memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
563  memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
564  memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
565  memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
566 
567  for (ich = 0; ich < s->num_channels; ich++) {
568  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
569  memset(s->cdlms[ich][ilms].coefs, 0,
570  sizeof(s->cdlms[ich][ilms].coefs));
571  memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
572  sizeof(s->cdlms[ich][ilms].lms_prevvalues));
573  memset(s->cdlms[ich][ilms].lms_updates, 0,
574  sizeof(s->cdlms[ich][ilms].lms_updates));
575  }
576  s->ave_sum[ich] = 0;
577  }
578 }
579 
580 /**
581  * @brief Reset filter parameters and transient area at new seekable tile.
582  */
584 {
585  int ich, ilms;
587  for (ich = 0; ich < s->num_channels; ich++) {
588  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
589  s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
590  /* first sample of a seekable subframe is considered as the starting of
591  a transient area which is samples_per_frame samples long */
593  s->transient[ich] = 1;
594  s->transient_pos[ich] = 0;
595  }
596 }
597 
598 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
599 {
600  int i, j, ich, pred_error;
601  int order = s->mclms_order;
602  int num_channels = s->num_channels;
603  int range = 1 << (s->bits_per_sample - 1);
604 
605  for (ich = 0; ich < num_channels; ich++) {
606  pred_error = s->channel_residues[ich][icoef] - pred[ich];
607  if (pred_error > 0) {
608  for (i = 0; i < order * num_channels; i++)
609  s->mclms_coeffs[i + ich * order * num_channels] +=
610  s->mclms_updates[s->mclms_recent + i];
611  for (j = 0; j < ich; j++) {
612  if (s->channel_residues[j][icoef] > 0)
613  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
614  else if (s->channel_residues[j][icoef] < 0)
615  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
616  }
617  } else if (pred_error < 0) {
618  for (i = 0; i < order * num_channels; i++)
619  s->mclms_coeffs[i + ich * order * num_channels] -=
620  s->mclms_updates[s->mclms_recent + i];
621  for (j = 0; j < ich; j++) {
622  if (s->channel_residues[j][icoef] > 0)
623  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
624  else if (s->channel_residues[j][icoef] < 0)
625  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
626  }
627  }
628  }
629 
630  for (ich = num_channels - 1; ich >= 0; ich--) {
631  s->mclms_recent--;
632  s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
633  if (s->channel_residues[ich][icoef] > range - 1)
634  s->mclms_prevvalues[s->mclms_recent] = range - 1;
635  else if (s->channel_residues[ich][icoef] < -range)
637 
638  s->mclms_updates[s->mclms_recent] = 0;
639  if (s->channel_residues[ich][icoef] > 0)
640  s->mclms_updates[s->mclms_recent] = 1;
641  else if (s->channel_residues[ich][icoef] < 0)
642  s->mclms_updates[s->mclms_recent] = -1;
643  }
644 
645  if (s->mclms_recent == 0) {
646  memcpy(&s->mclms_prevvalues[order * num_channels],
647  s->mclms_prevvalues,
648  2 * order * num_channels);
649  memcpy(&s->mclms_updates[order * num_channels],
650  s->mclms_updates,
651  2 * order * num_channels);
652  s->mclms_recent = num_channels * order;
653  }
654 }
655 
656 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
657 {
658  int ich, i;
659  int order = s->mclms_order;
660  int num_channels = s->num_channels;
661 
662  for (ich = 0; ich < num_channels; ich++) {
663  pred[ich] = 0;
664  if (!s->is_channel_coded[ich])
665  continue;
666  for (i = 0; i < order * num_channels; i++)
667  pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
668  s->mclms_coeffs[i + order * num_channels * ich];
669  for (i = 0; i < ich; i++)
670  pred[ich] += s->channel_residues[i][icoef] *
671  s->mclms_coeffs_cur[i + num_channels * ich];
672  pred[ich] += 1 << s->mclms_scaling - 1;
673  pred[ich] >>= s->mclms_scaling;
674  s->channel_residues[ich][icoef] += pred[ich];
675  }
676 }
677 
678 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
679 {
680  int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
681  for (icoef = 0; icoef < tile_size; icoef++) {
682  mclms_predict(s, icoef, pred);
683  mclms_update(s, icoef, pred);
684  }
685 }
686 
687 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
688 {
689  int pred = 0, icoef;
690  int recent = s->cdlms[ich][ilms].recent;
691 
692  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
693  pred += s->cdlms[ich][ilms].coefs[icoef] *
694  s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
695 
696  return pred;
697 }
698 
699 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
700  int input, int residue)
701 {
702  int icoef;
703  int recent = s->cdlms[ich][ilms].recent;
704  int range = 1 << s->bits_per_sample - 1;
705 
706  if (residue < 0) {
707  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
708  s->cdlms[ich][ilms].coefs[icoef] -=
709  s->cdlms[ich][ilms].lms_updates[icoef + recent];
710  } else if (residue > 0) {
711  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
712  s->cdlms[ich][ilms].coefs[icoef] +=
713  s->cdlms[ich][ilms].lms_updates[icoef + recent];
714  }
715 
716  if (recent)
717  recent--;
718  else {
719  memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
720  s->cdlms[ich][ilms].lms_prevvalues,
721  2 * s->cdlms[ich][ilms].order);
722  memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
723  s->cdlms[ich][ilms].lms_updates,
724  2 * s->cdlms[ich][ilms].order);
725  recent = s->cdlms[ich][ilms].order - 1;
726  }
727 
728  s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
729  if (!input)
730  s->cdlms[ich][ilms].lms_updates[recent] = 0;
731  else if (input < 0)
732  s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
733  else
734  s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
735 
736  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
737  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
738  s->cdlms[ich][ilms].recent = recent;
739 }
740 
741 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
742 {
743  int ilms, recent, icoef;
744  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
745  recent = s->cdlms[ich][ilms].recent;
746  if (s->update_speed[ich] == 16)
747  continue;
748  if (s->bV3RTM) {
749  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
750  s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
751  } else {
752  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
753  s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
754  }
755  }
756  s->update_speed[ich] = 16;
757 }
758 
760 {
761  int ilms, recent, icoef;
762  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
763  recent = s->cdlms[ich][ilms].recent;
764  if (s->update_speed[ich] == 8)
765  continue;
766  if (s->bV3RTM)
767  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
768  s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
769  else
770  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
771  s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
772  }
773  s->update_speed[ich] = 8;
774 }
775 
776 static void revert_cdlms(WmallDecodeCtx *s, int ch,
777  int coef_begin, int coef_end)
778 {
779  int icoef, pred, ilms, num_lms, residue, input;
780 
781  num_lms = s->cdlms_ttl[ch];
782  for (ilms = num_lms - 1; ilms >= 0; ilms--) {
783  for (icoef = coef_begin; icoef < coef_end; icoef++) {
784  pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
785  residue = s->channel_residues[ch][icoef];
786  pred += lms_predict(s, ch, ilms);
787  input = residue + (pred >> s->cdlms[ch][ilms].scaling);
788  lms_update(s, ch, ilms, input, residue);
789  s->channel_residues[ch][icoef] = input;
790  }
791  }
792 }
793 
794 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
795 {
796  if (s->num_channels != 2)
797  return;
798  else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
799  int icoef;
800  for (icoef = 0; icoef < tile_size; icoef++) {
801  s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
802  s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
803  }
804  }
805 }
806 
807 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
808 {
809  int ich, pred, i, j;
810  int64_t *filter_coeffs = s->acfilter_coeffs;
811  int scaling = s->acfilter_scaling;
812  int order = s->acfilter_order;
813 
814  for (ich = 0; ich < s->num_channels; ich++) {
815  int *prevvalues = s->acfilter_prevvalues[ich];
816  for (i = 0; i < order; i++) {
817  pred = 0;
818  for (j = 0; j < order; j++) {
819  if (i <= j)
820  pred += filter_coeffs[j] * prevvalues[j - i];
821  else
822  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
823  }
824  pred >>= scaling;
825  s->channel_residues[ich][i] += pred;
826  }
827  for (i = order; i < tile_size; i++) {
828  pred = 0;
829  for (j = 0; j < order; j++)
830  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
831  pred >>= scaling;
832  s->channel_residues[ich][i] += pred;
833  }
834  for (j = 0; j < order; j++)
835  prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
836  }
837 }
838 
840 {
841  int offset = s->samples_per_frame;
843  int total_samples = s->samples_per_frame * s->num_channels;
844  int i, j, rawpcm_tile, padding_zeroes, res;
845 
847 
848  /* reset channel context and find the next block offset and size
849  == the next block of the channel with the smallest number of
850  decoded samples */
851  for (i = 0; i < s->num_channels; i++) {
852  if (offset > s->channel[i].decoded_samples) {
853  offset = s->channel[i].decoded_samples;
854  subframe_len =
856  }
857  }
858 
859  /* get a list of all channels that contain the estimated block */
861  for (i = 0; i < s->num_channels; i++) {
862  const int cur_subframe = s->channel[i].cur_subframe;
863  /* subtract already processed samples */
864  total_samples -= s->channel[i].decoded_samples;
865 
866  /* and count if there are multiple subframes that match our profile */
867  if (offset == s->channel[i].decoded_samples &&
868  subframe_len == s->channel[i].subframe_len[cur_subframe]) {
869  total_samples -= s->channel[i].subframe_len[cur_subframe];
870  s->channel[i].decoded_samples +=
871  s->channel[i].subframe_len[cur_subframe];
874  }
875  }
876 
877  /* check if the frame will be complete after processing the
878  estimated block */
879  if (!total_samples)
880  s->parsed_all_subframes = 1;
881 
882 
883  s->seekable_tile = get_bits1(&s->gb);
884  if (s->seekable_tile) {
886 
887  s->do_arith_coding = get_bits1(&s->gb);
888  if (s->do_arith_coding) {
889  avpriv_request_sample(s->avctx, "Arithmetic coding");
890  return AVERROR_PATCHWELCOME;
891  }
892  s->do_ac_filter = get_bits1(&s->gb);
893  s->do_inter_ch_decorr = get_bits1(&s->gb);
894  s->do_mclms = get_bits1(&s->gb);
895 
896  if (s->do_ac_filter)
897  decode_ac_filter(s);
898 
899  if (s->do_mclms)
900  decode_mclms(s);
901 
902  if ((res = decode_cdlms(s)) < 0)
903  return res;
904  s->movave_scaling = get_bits(&s->gb, 3);
905  s->quant_stepsize = get_bits(&s->gb, 8) + 1;
906 
907  reset_codec(s);
908  } else if (!s->cdlms[0][0].order) {
910  "Waiting for seekable tile\n");
911  s->frame.nb_samples = 0;
912  return -1;
913  }
914 
915  rawpcm_tile = get_bits1(&s->gb);
916 
917  for (i = 0; i < s->num_channels; i++)
918  s->is_channel_coded[i] = 1;
919 
920  if (!rawpcm_tile) {
921  for (i = 0; i < s->num_channels; i++)
922  s->is_channel_coded[i] = get_bits1(&s->gb);
923 
924  if (s->bV3RTM) {
925  // LPC
926  s->do_lpc = get_bits1(&s->gb);
927  if (s->do_lpc) {
928  decode_lpc(s);
929  avpriv_request_sample(s->avctx, "Expect wrong output since "
930  "inverse LPC filter");
931  }
932  } else
933  s->do_lpc = 0;
934  }
935 
936 
937  if (get_bits1(&s->gb))
938  padding_zeroes = get_bits(&s->gb, 5);
939  else
940  padding_zeroes = 0;
941 
942  if (rawpcm_tile) {
943  int bits = s->bits_per_sample - padding_zeroes;
944  if (bits <= 0) {
946  "Invalid number of padding bits in raw PCM tile\n");
947  return AVERROR_INVALIDDATA;
948  }
949  av_dlog(s->avctx, "RAWPCM %d bits per sample. "
950  "total %d bits, remain=%d\n", bits,
951  bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
952  for (i = 0; i < s->num_channels; i++)
953  for (j = 0; j < subframe_len; j++)
954  s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
955  } else {
956  for (i = 0; i < s->num_channels; i++)
957  if (s->is_channel_coded[i]) {
958  decode_channel_residues(s, i, subframe_len);
959  if (s->seekable_tile)
960  use_high_update_speed(s, i);
961  else
963  revert_cdlms(s, i, 0, subframe_len);
964  } else {
965  memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
966  }
967  }
968  if (s->do_mclms)
969  revert_mclms(s, subframe_len);
970  if (s->do_inter_ch_decorr)
971  revert_inter_ch_decorr(s, subframe_len);
972  if (s->do_ac_filter)
973  revert_acfilter(s, subframe_len);
974 
975  /* Dequantize */
976  if (s->quant_stepsize != 1)
977  for (i = 0; i < s->num_channels; i++)
978  for (j = 0; j < subframe_len; j++)
979  s->channel_residues[i][j] *= s->quant_stepsize;
980 
981  /* Write to proper output buffer depending on bit-depth */
982  for (i = 0; i < s->channels_for_cur_subframe; i++) {
984  int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
985 
986  for (j = 0; j < subframe_len; j++) {
987  if (s->bits_per_sample == 16) {
988  *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
989  } else {
990  *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
991  }
992  }
993  }
994 
995  /* handled one subframe */
996  for (i = 0; i < s->channels_for_cur_subframe; i++) {
998  if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
999  av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1000  return AVERROR_INVALIDDATA;
1001  }
1002  ++s->channel[c].cur_subframe;
1003  }
1004  return 0;
1005 }
1006 
1007 /**
1008  * @brief Decode one WMA frame.
1009  * @param s codec context
1010  * @return 0 if the trailer bit indicates that this is the last frame,
1011  * 1 if there are additional frames
1012  */
1014 {
1015  GetBitContext* gb = &s->gb;
1016  int more_frames = 0, len = 0, i, ret;
1017 
1019  if ((ret = ff_get_buffer(s->avctx, &s->frame, 0)) < 0) {
1020  /* return an error if no frame could be decoded at all */
1021  s->packet_loss = 1;
1022  return ret;
1023  }
1024  for (i = 0; i < s->num_channels; i++) {
1025  s->samples_16[i] = (int16_t *)s->frame.extended_data[i];
1026  s->samples_32[i] = (int32_t *)s->frame.extended_data[i];
1027  }
1028 
1029  /* get frame length */
1030  if (s->len_prefix)
1031  len = get_bits(gb, s->log2_frame_size);
1032 
1033  /* decode tile information */
1034  if (decode_tilehdr(s)) {
1035  s->packet_loss = 1;
1036  return 0;
1037  }
1038 
1039  /* read drc info */
1041  s->drc_gain = get_bits(gb, 8);
1042 
1043  /* no idea what these are for, might be the number of samples
1044  that need to be skipped at the beginning or end of a stream */
1045  if (get_bits1(gb)) {
1046  int av_unused skip;
1047 
1048  /* usually true for the first frame */
1049  if (get_bits1(gb)) {
1050  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1051  av_dlog(s->avctx, "start skip: %i\n", skip);
1052  }
1053 
1054  /* sometimes true for the last frame */
1055  if (get_bits1(gb)) {
1056  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1057  av_dlog(s->avctx, "end skip: %i\n", skip);
1058  }
1059 
1060  }
1061 
1062  /* reset subframe states */
1063  s->parsed_all_subframes = 0;
1064  for (i = 0; i < s->num_channels; i++) {
1065  s->channel[i].decoded_samples = 0;
1066  s->channel[i].cur_subframe = 0;
1067  }
1068 
1069  /* decode all subframes */
1070  while (!s->parsed_all_subframes) {
1071  if (decode_subframe(s) < 0) {
1072  s->packet_loss = 1;
1073  return 0;
1074  }
1075  }
1076 
1077  av_dlog(s->avctx, "Frame done\n");
1078 
1079  if (s->skip_frame)
1080  s->skip_frame = 0;
1081 
1082  if (s->len_prefix) {
1083  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1084  /* FIXME: not sure if this is always an error */
1086  "frame[%i] would have to skip %i bits\n", s->frame_num,
1087  len - (get_bits_count(gb) - s->frame_offset) - 1);
1088  s->packet_loss = 1;
1089  return 0;
1090  }
1091 
1092  /* skip the rest of the frame data */
1093  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1094  }
1095 
1096  /* decode trailer bit */
1097  more_frames = get_bits1(gb);
1098  ++s->frame_num;
1099  return more_frames;
1100 }
1101 
1102 /**
1103  * @brief Calculate remaining input buffer length.
1104  * @param s codec context
1105  * @param gb bitstream reader context
1106  * @return remaining size in bits
1107  */
1109 {
1110  return s->buf_bit_size - get_bits_count(gb);
1111 }
1112 
1113 /**
1114  * @brief Fill the bit reservoir with a (partial) frame.
1115  * @param s codec context
1116  * @param gb bitstream reader context
1117  * @param len length of the partial frame
1118  * @param append decides whether to reset the buffer or not
1119  */
1121  int append)
1122 {
1123  int buflen;
1124  PutBitContext tmp;
1125 
1126  /* when the frame data does not need to be concatenated, the input buffer
1127  is reset and additional bits from the previous frame are copied
1128  and skipped later so that a fast byte copy is possible */
1129 
1130  if (!append) {
1131  s->frame_offset = get_bits_count(gb) & 7;
1132  s->num_saved_bits = s->frame_offset;
1134  }
1135 
1136  buflen = (s->num_saved_bits + len + 8) >> 3;
1137 
1138  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1139  avpriv_request_sample(s->avctx, "Too small input buffer");
1140  s->packet_loss = 1;
1141  return;
1142  }
1143 
1144  s->num_saved_bits += len;
1145  if (!append) {
1146  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1147  s->num_saved_bits);
1148  } else {
1149  int align = 8 - (get_bits_count(gb) & 7);
1150  align = FFMIN(align, len);
1151  put_bits(&s->pb, align, get_bits(gb, align));
1152  len -= align;
1153  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1154  }
1155  skip_bits_long(gb, len);
1156 
1157  tmp = s->pb;
1158  flush_put_bits(&tmp);
1159 
1161  skip_bits(&s->gb, s->frame_offset);
1162 }
1163 
1164 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1165  AVPacket* avpkt)
1166 {
1167  WmallDecodeCtx *s = avctx->priv_data;
1168  GetBitContext* gb = &s->pgb;
1169  const uint8_t* buf = avpkt->data;
1170  int buf_size = avpkt->size;
1171  int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1172 
1173  s->frame.nb_samples = 0;
1174 
1175  if (s->packet_done || s->packet_loss) {
1176  s->packet_done = 0;
1177 
1178  /* sanity check for the buffer length */
1179  if (buf_size < avctx->block_align)
1180  return 0;
1181 
1182  s->next_packet_start = buf_size - avctx->block_align;
1183  buf_size = avctx->block_align;
1184  s->buf_bit_size = buf_size << 3;
1185 
1186  /* parse packet header */
1187  init_get_bits(gb, buf, s->buf_bit_size);
1188  packet_sequence_number = get_bits(gb, 4);
1189  skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1190  spliced_packet = get_bits1(gb);
1191  if (spliced_packet)
1192  avpriv_request_sample(avctx, "Bitstream splicing");
1193 
1194  /* get number of bits that need to be added to the previous frame */
1195  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1196 
1197  /* check for packet loss */
1198  if (!s->packet_loss &&
1199  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1200  s->packet_loss = 1;
1201  av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1202  s->packet_sequence_number, packet_sequence_number);
1203  }
1205 
1206  if (num_bits_prev_frame > 0) {
1207  int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1208  if (num_bits_prev_frame >= remaining_packet_bits) {
1209  num_bits_prev_frame = remaining_packet_bits;
1210  s->packet_done = 1;
1211  }
1212 
1213  /* Append the previous frame data to the remaining data from the
1214  * previous packet to create a full frame. */
1215  save_bits(s, gb, num_bits_prev_frame, 1);
1216 
1217  /* decode the cross packet frame if it is valid */
1218  if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1219  decode_frame(s);
1220  } else if (s->num_saved_bits - s->frame_offset) {
1221  av_dlog(avctx, "ignoring %x previously saved bits\n",
1222  s->num_saved_bits - s->frame_offset);
1223  }
1224 
1225  if (s->packet_loss) {
1226  /* Reset number of saved bits so that the decoder does not start
1227  * to decode incomplete frames in the s->len_prefix == 0 case. */
1228  s->num_saved_bits = 0;
1229  s->packet_loss = 0;
1231  }
1232 
1233  } else {
1234  int frame_size;
1235 
1236  s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1237  init_get_bits(gb, avpkt->data, s->buf_bit_size);
1238  skip_bits(gb, s->packet_offset);
1239 
1240  if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1241  (frame_size = show_bits(gb, s->log2_frame_size)) &&
1242  frame_size <= remaining_bits(s, gb)) {
1243  save_bits(s, gb, frame_size, 0);
1244  s->packet_done = !decode_frame(s);
1245  } else if (!s->len_prefix
1246  && s->num_saved_bits > get_bits_count(&s->gb)) {
1247  /* when the frames do not have a length prefix, we don't know the
1248  * compressed length of the individual frames however, we know what
1249  * part of a new packet belongs to the previous frame therefore we
1250  * save the incoming packet first, then we append the "previous
1251  * frame" data from the next packet so that we get a buffer that
1252  * only contains full frames */
1253  s->packet_done = !decode_frame(s);
1254  } else {
1255  s->packet_done = 1;
1256  }
1257  }
1258 
1259  if (s->packet_done && !s->packet_loss &&
1260  remaining_bits(s, gb) > 0) {
1261  /* save the rest of the data so that it can be decoded
1262  * with the next packet */
1263  save_bits(s, gb, remaining_bits(s, gb), 0);
1264  }
1265 
1266  *got_frame_ptr = s->frame.nb_samples > 0;
1267  av_frame_move_ref(data, &s->frame);
1268 
1269  s->packet_offset = get_bits_count(gb) & 7;
1270 
1271  return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1272 }
1273 
1275 {
1276  WmallDecodeCtx *s = avctx->priv_data;
1277  s->packet_loss = 1;
1278  s->packet_done = 0;
1279  s->num_saved_bits = 0;
1280  s->frame_offset = 0;
1281  s->next_packet_start = 0;
1282  s->cdlms[0][0].order = 0;
1283  s->frame.nb_samples = 0;
1285 }
1286 
1288  .name = "wmalossless",
1289  .type = AVMEDIA_TYPE_AUDIO,
1291  .priv_data_size = sizeof(WmallDecodeCtx),
1292  .init = decode_init,
1293  .decode = decode_packet,
1294  .flush = flush,
1296  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1297  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1300 };
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_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
struct WmallDecodeCtx WmallDecodeCtx
main decoder context
struct WmallDecodeCtx::@100 cdlms[WMALL_MAX_CHANNELS][9]
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:76
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 unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
AVFrame * coded_frame
the picture in the bitstream
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t drc_gain
gain for the DRC tool
int acfilter_prevvalues[WMALL_MAX_CHANNELS][16]
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:61
location of range
const uint8_t * buffer
Definition: get_bits.h:55
PutBitContext pb
context for filling the frame_data buffer
#define AV_RL16
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)
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:225
#define MAX_ORDER
int8_t channels_for_cur_subframe
number of channels that contain the subframe
Macro definitions for various function/variable attributes.
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:344
uint8_t packet_sequence_number
current packet number
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
int16_t subframe_len
current subframe length
uint16_t decoded_samples
number of already processed samples
GetBitContext pgb
bitstream reader context for the packet
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:216
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
int8_t num_channels
number of channels in the stream (same as AVCodecContext.num_channels)
static void reset_codec(WmallDecodeCtx *s)
Reset filter parameters and transient area at new seekable tile.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int update_speed[WMALL_MAX_CHANNELS]
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
int is_channel_coded[WMALL_MAX_CHANNELS]
int frame_offset
frame offset in the bit reservoir
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
uint16_t min_samples_per_subframe
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:352
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
bitstream reader API header.
signed 32 bits, planar
Definition: samplefmt.h:59
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:557
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...
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. ...
static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
Spectrum Plot time data
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame This method is called when a frame is wanted on an output For an input
simple assert() macros that are a bit more flexible than ISO C assert().
int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS]
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
uint8_t frame_data[MAX_FRAMESIZE+FF_INPUT_BUFFER_PADDING_SIZE]
compressed frame data
static int decode_subframe(WmallDecodeCtx *s)
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
static void put_bits(J2kEncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
AVCodecContext * avctx
#define FFMAX(a, b)
Definition: common.h:56
external API header
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.
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...
static void use_high_update_speed(WmallDecodeCtx *s, int ich)
#define MAX_SUBFRAMES
max number of subframes per channel
uint8_t packet_loss
set in case of bitstream error
static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
static void clear_codec_buffers(WmallDecodeCtx *s)
#define FFMIN(a, b)
Definition: common.h:58
uint16_t log2_frame_size
ret
Definition: avfilter.c:821
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:255
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
#define AV_RL32
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
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
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
int sample_rate
samples per second
#define WMALL_MAX_CHANNELS
current decoder limitations
main external API structure.
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.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:265
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
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:306
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.
GetBitContext gb
bitstream reader context
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...
uint16_t subframe_len[MAX_SUBFRAMES]
subframe length in samples
int ave_sum[WMALL_MAX_CHANNELS]
WmallChannelCtx channel[WMALL_MAX_CHANNELS]
per channel data
#define MAX_FRAMESIZE
maximum compressed frame size
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:81
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]
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
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]
int16_t mclms_coeffs[128]
int len
int channels
number of audio channels
#define av_log2
Definition: intmath.h:89
AVCodec ff_wmalossless_decoder
signed 16 bits, planar
Definition: samplefmt.h:58
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
int transient[WMALL_MAX_CHANNELS]
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
int num_saved_bits
saved number of bits
int subframe_offset
subframe offset in the bit reservoir
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
int lpc_coefs[WMALL_MAX_CHANNELS][40]
for(j=16;j >0;--j)
#define av_unused
Definition: attributes.h:114
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