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