FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
wmalosslessdec.c
Go to the documentation of this file.
1 /*
2  * Windows Media Audio Lossless decoder
3  * Copyright (c) 2007 Baptiste Coudurier, Benjamin Larsson, Ulion
4  * Copyright (c) 2008 - 2011 Sascha Sommer, Benjamin Larsson
5  * Copyright (c) 2011 Andreas Ă–man
6  * Copyright (c) 2011 - 2012 Mashiat Sarker Shakkhar
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <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
76  PutBitContext pb; ///< context for filling the frame_data buffer
77 
78  /* frame size dependent frame information (set during initialization) */
79  uint32_t decode_flags; ///< used compression features
80  int len_prefix; ///< frame is prefixed with its length
81  int dynamic_range_compression; ///< frame contains DRC data
82  uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
83  uint16_t samples_per_frame; ///< number of samples to output
84  uint16_t log2_frame_size;
85  int8_t num_channels; ///< number of channels in the stream (same as AVCodecContext.num_channels)
86  int8_t lfe_channel; ///< lfe channel index
88  uint8_t subframe_len_bits; ///< number of bits used for the subframe length
89  uint8_t max_subframe_len_bit; ///< flag indicating that the subframe is of maximum size when the first subframe length bit is 1
91 
92  /* packet decode state */
93  GetBitContext pgb; ///< bitstream reader context for the packet
94  int next_packet_start; ///< start offset of the next WMA packet in the demuxer packet
95  uint8_t packet_offset; ///< offset to the frame in the packet
96  uint8_t packet_sequence_number; ///< current packet number
97  int num_saved_bits; ///< saved number of bits
98  int frame_offset; ///< frame offset in the bit reservoir
99  int subframe_offset; ///< subframe offset in the bit reservoir
100  uint8_t packet_loss; ///< set in case of bitstream error
101  uint8_t packet_done; ///< set when a packet is fully decoded
102 
103  /* frame decode state */
104  uint32_t frame_num; ///< current frame number (not used for decoding)
105  GetBitContext gb; ///< bitstream reader context
106  int buf_bit_size; ///< buffer size in bits
107  int16_t *samples_16[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (16-bit)
108  int32_t *samples_32[WMALL_MAX_CHANNELS]; ///< current samplebuffer pointer (24-bit)
109  uint8_t drc_gain; ///< gain for the DRC tool
110  int8_t skip_frame; ///< skip output step
111  int8_t parsed_all_subframes; ///< all subframes decoded?
112 
113  /* subframe/block decode state */
114  int16_t subframe_len; ///< current subframe length
115  int8_t channels_for_cur_subframe; ///< number of channels that contain the subframe
117 
119 
120  // WMA Lossless-specific
121 
127 
130  int16_t acfilter_coeffs[16];
132 
133  int8_t mclms_order;
140 
143 
144  struct {
145  int order;
146  int scaling;
147  int coefsend;
148  int bitsend;
149  DECLARE_ALIGNED(16, int16_t, coefs)[MAX_ORDER + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
150  DECLARE_ALIGNED(16, int16_t, lms_prevvalues)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
151  DECLARE_ALIGNED(16, int16_t, lms_updates)[MAX_ORDER * 2 + WMALL_COEFF_PAD_SIZE/sizeof(int16_t)];
152  int recent;
154 
156 
157  int bV3RTM;
158 
161 
162  int transient[WMALL_MAX_CHANNELS];
165 
167 
169 
174 
177 
178 /** Get sign of integer (1 for positive, -1 for negative and 0 for zero) */
179 #define WMASIGN(x) (((x) > 0) - ((x) < 0))
180 
182 {
183  WmallDecodeCtx *s = avctx->priv_data;
184  uint8_t *edata_ptr = avctx->extradata;
185  unsigned int channel_mask;
186  int i, log2_max_num_subframes;
187 
188  if (avctx->block_align <= 0 || avctx->block_align > (1<<21)) {
189  av_log(avctx, AV_LOG_ERROR, "block_align is not set or invalid\n");
190  return AVERROR(EINVAL);
191  }
192 
193  if (avctx->channels < 0) {
194  av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
195  avctx->channels);
196  return AVERROR_INVALIDDATA;
197  } else if (avctx->channels > WMALL_MAX_CHANNELS) {
198  avpriv_request_sample(avctx,
199  "More than %d channels", WMALL_MAX_CHANNELS);
200  return AVERROR_PATCHWELCOME;
201  }
202 
203  s->avctx = avctx;
204  ff_llauddsp_init(&s->dsp);
206 
207  if (avctx->extradata_size >= 18) {
208  s->decode_flags = AV_RL16(edata_ptr + 14);
209  channel_mask = AV_RL32(edata_ptr + 2);
210  s->bits_per_sample = AV_RL16(edata_ptr);
211  if (s->bits_per_sample == 16)
213  else if (s->bits_per_sample == 24) {
215  avctx->bits_per_raw_sample = 24;
216  } else {
217  av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %"PRIu8"\n",
218  s->bits_per_sample);
219  return AVERROR_INVALIDDATA;
220  }
221  /* dump the extradata */
222  for (i = 0; i < avctx->extradata_size; i++)
223  ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
224  ff_dlog(avctx, "\n");
225 
226  } else {
227  avpriv_request_sample(avctx, "Unsupported extradata size");
228  return AVERROR_PATCHWELCOME;
229  }
230 
231  /* generic init */
232  s->log2_frame_size = av_log2(avctx->block_align) + 4;
233 
234  /* frame info */
235  s->skip_frame = 1; /* skip first frame */
236  s->packet_loss = 1;
237  s->len_prefix = s->decode_flags & 0x40;
238 
239  /* get frame len */
241  3, s->decode_flags);
243 
244  /* init previous block len */
245  for (i = 0; i < avctx->channels; i++)
247 
248  /* subframe info */
249  log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
250  s->max_num_subframes = 1 << log2_max_num_subframes;
251  s->max_subframe_len_bit = 0;
252  s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
253 
256  s->bV3RTM = s->decode_flags & 0x100;
257 
258  if (s->max_num_subframes > MAX_SUBFRAMES) {
259  av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRIu8"\n",
260  s->max_num_subframes);
261  return AVERROR_INVALIDDATA;
262  }
263 
264  s->num_channels = avctx->channels;
265 
266  /* extract lfe channel position */
267  s->lfe_channel = -1;
268 
269  if (channel_mask & 8) {
270  unsigned int mask;
271  for (mask = 1; mask < 16; mask <<= 1)
272  if (channel_mask & mask)
273  ++s->lfe_channel;
274  }
275 
276  s->frame = av_frame_alloc();
277  if (!s->frame)
278  return AVERROR(ENOMEM);
279 
280  avctx->channel_layout = channel_mask;
281  return 0;
282 }
283 
284 /**
285  * @brief Decode the subframe length.
286  * @param s context
287  * @param offset sample offset in the frame
288  * @return decoded subframe length on success, < 0 in case of an error
289  */
291 {
292  int frame_len_ratio, subframe_len, len;
293 
294  /* no need to read from the bitstream when only one length is possible */
295  if (offset == s->samples_per_frame - s->min_samples_per_subframe)
296  return s->min_samples_per_subframe;
297 
298  len = av_log2(s->max_num_subframes - 1) + 1;
299  frame_len_ratio = get_bits(&s->gb, len);
300  subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
301 
302  /* sanity check the length */
303  if (subframe_len < s->min_samples_per_subframe ||
304  subframe_len > s->samples_per_frame) {
305  av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
306  subframe_len);
307  return AVERROR_INVALIDDATA;
308  }
309  return subframe_len;
310 }
311 
312 /**
313  * @brief Decode how the data in the frame is split into subframes.
314  * Every WMA frame contains the encoded data for a fixed number of
315  * samples per channel. The data for every channel might be split
316  * into several subframes. This function will reconstruct the list of
317  * subframes for every channel.
318  *
319  * If the subframes are not evenly split, the algorithm estimates the
320  * channels with the lowest number of total samples.
321  * Afterwards, for each of these channels a bit is read from the
322  * bitstream that indicates if the channel contains a subframe with the
323  * next subframe size that is going to be read from the bitstream or not.
324  * If a channel contains such a subframe, the subframe size gets added to
325  * the channel's subframe list.
326  * The algorithm repeats these steps until the frame is properly divided
327  * between the individual channels.
328  *
329  * @param s context
330  * @return 0 on success, < 0 in case of an error
331  */
333 {
334  uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
335  uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
336  int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
337  int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
338  int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
339  int c, tile_aligned;
340 
341  /* reset tiling information */
342  for (c = 0; c < s->num_channels; c++)
343  s->channel[c].num_subframes = 0;
344 
345  tile_aligned = get_bits1(&s->gb);
346  if (s->max_num_subframes == 1 || tile_aligned)
347  fixed_channel_layout = 1;
348 
349  /* loop until the frame data is split between the subframes */
350  do {
351  int subframe_len, in_use = 0;
352 
353  /* check which channels contain the subframe */
354  for (c = 0; c < s->num_channels; c++) {
355  if (num_samples[c] == min_channel_len) {
356  if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
357  (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
358  contains_subframe[c] = 1;
359  } else {
360  contains_subframe[c] = get_bits1(&s->gb);
361  }
362  in_use |= contains_subframe[c];
363  } else
364  contains_subframe[c] = 0;
365  }
366 
367  if (!in_use) {
369  "Found empty subframe\n");
370  return AVERROR_INVALIDDATA;
371  }
372 
373  /* get subframe length, subframe_len == 0 is not allowed */
374  if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
375  return AVERROR_INVALIDDATA;
376  /* add subframes to the individual channels and find new min_channel_len */
377  min_channel_len += subframe_len;
378  for (c = 0; c < s->num_channels; c++) {
379  WmallChannelCtx *chan = &s->channel[c];
380 
381  if (contains_subframe[c]) {
382  if (chan->num_subframes >= MAX_SUBFRAMES) {
384  "broken frame: num subframes > 31\n");
385  return AVERROR_INVALIDDATA;
386  }
387  chan->subframe_len[chan->num_subframes] = subframe_len;
388  num_samples[c] += subframe_len;
389  ++chan->num_subframes;
390  if (num_samples[c] > s->samples_per_frame) {
391  av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
392  "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
393  num_samples[c], s->samples_per_frame);
394  return AVERROR_INVALIDDATA;
395  }
396  } else if (num_samples[c] <= min_channel_len) {
397  if (num_samples[c] < min_channel_len) {
398  channels_for_cur_subframe = 0;
399  min_channel_len = num_samples[c];
400  }
401  ++channels_for_cur_subframe;
402  }
403  }
404  } while (min_channel_len < s->samples_per_frame);
405 
406  for (c = 0; c < s->num_channels; c++) {
407  int i, offset = 0;
408  for (i = 0; i < s->channel[c].num_subframes; i++) {
409  s->channel[c].subframe_offsets[i] = offset;
410  offset += s->channel[c].subframe_len[i];
411  }
412  }
413 
414  return 0;
415 }
416 
418 {
419  int i;
420  s->acfilter_order = get_bits(&s->gb, 4) + 1;
421  s->acfilter_scaling = get_bits(&s->gb, 4);
422 
423  for (i = 0; i < s->acfilter_order; i++)
424  s->acfilter_coeffs[i] = (s->acfilter_scaling ?
425  get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
426 }
427 
429 {
430  s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
431  s->mclms_scaling = get_bits(&s->gb, 4);
432  if (get_bits1(&s->gb)) {
433  int i, send_coef_bits;
434  int cbits = av_log2(s->mclms_scaling + 1);
435  if (1 << cbits < s->mclms_scaling + 1)
436  cbits++;
437 
438  send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
439 
440  for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
441  s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
442 
443  for (i = 0; i < s->num_channels; i++) {
444  int c;
445  for (c = 0; c < i; c++)
446  s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
447  }
448  }
449 }
450 
452 {
453  int c, i;
454  int cdlms_send_coef = get_bits1(&s->gb);
455 
456  for (c = 0; c < s->num_channels; c++) {
457  s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
458  for (i = 0; i < s->cdlms_ttl[c]; i++) {
459  s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
460  if (s->cdlms[c][i].order > MAX_ORDER) {
462  "Order[%d][%d] %d > max (%d), not supported\n",
463  c, i, s->cdlms[c][i].order, MAX_ORDER);
464  s->cdlms[0][0].order = 0;
465  return AVERROR_INVALIDDATA;
466  }
467  if(s->cdlms[c][i].order & 8) {
468  static int warned;
469  if(!warned)
470  avpriv_request_sample(s->avctx, "CDLMS of order %d",
471  s->cdlms[c][i].order);
472  warned = 1;
473  }
474  }
475 
476  for (i = 0; i < s->cdlms_ttl[c]; i++)
477  s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
478 
479  if (cdlms_send_coef) {
480  for (i = 0; i < s->cdlms_ttl[c]; i++) {
481  int cbits, shift_l, shift_r, j;
482  cbits = av_log2(s->cdlms[c][i].order);
483  if ((1 << cbits) < s->cdlms[c][i].order)
484  cbits++;
485  s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
486 
487  cbits = av_log2(s->cdlms[c][i].scaling + 1);
488  if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
489  cbits++;
490 
491  s->cdlms[c][i].bitsend = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
492  shift_l = 32 - s->cdlms[c][i].bitsend;
493  shift_r = 32 - s->cdlms[c][i].scaling - 2;
494  for (j = 0; j < s->cdlms[c][i].coefsend; j++)
495  s->cdlms[c][i].coefs[j] =
496  (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
497  }
498  }
499 
500  for (i = 0; i < s->cdlms_ttl[c]; i++)
501  memset(s->cdlms[c][i].coefs + s->cdlms[c][i].order,
503  }
504 
505  return 0;
506 }
507 
508 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
509 {
510  int i = 0;
511  unsigned int ave_mean;
512  s->transient[ch] = get_bits1(&s->gb);
513  if (s->transient[ch]) {
514  s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
515  if (s->transient_pos[ch])
516  s->transient[ch] = 0;
517  s->channel[ch].transient_counter =
519  } else if (s->channel[ch].transient_counter)
520  s->transient[ch] = 1;
521 
522  if (s->seekable_tile) {
523  ave_mean = get_bits(&s->gb, s->bits_per_sample);
524  s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
525  }
526 
527  if (s->seekable_tile) {
528  if (s->do_inter_ch_decorr)
529  s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample + 1);
530  else
531  s->channel_residues[ch][0] = get_sbits_long(&s->gb, s->bits_per_sample);
532  i++;
533  }
534  for (; i < tile_size; i++) {
535  int quo = 0, rem, rem_bits, residue;
536  while(get_bits1(&s->gb)) {
537  quo++;
538  if (get_bits_left(&s->gb) <= 0)
539  return -1;
540  }
541  if (quo >= 32)
542  quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
543 
544  ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
545  if (ave_mean <= 1)
546  residue = quo;
547  else {
548  rem_bits = av_ceil_log2(ave_mean);
549  rem = get_bits_long(&s->gb, rem_bits);
550  residue = (quo << rem_bits) + rem;
551  }
552 
553  s->ave_sum[ch] = residue + s->ave_sum[ch] -
554  (s->ave_sum[ch] >> s->movave_scaling);
555 
556  residue = (residue >> 1) ^ -(residue & 1);
557  s->channel_residues[ch][i] = residue;
558  }
559 
560  return 0;
561 
562 }
563 
565 {
566  int ch, i, cbits;
567  s->lpc_order = get_bits(&s->gb, 5) + 1;
568  s->lpc_scaling = get_bits(&s->gb, 4);
569  s->lpc_intbits = get_bits(&s->gb, 3) + 1;
570  cbits = s->lpc_scaling + s->lpc_intbits;
571  for (ch = 0; ch < s->num_channels; ch++)
572  for (i = 0; i < s->lpc_order; i++)
573  s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
574 }
575 
577 {
578  int ich, ilms;
579 
580  memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
581  memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
582  memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
583 
584  memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
585  memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
586  memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
587  memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
588 
589  for (ich = 0; ich < s->num_channels; ich++) {
590  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
591  memset(s->cdlms[ich][ilms].coefs, 0,
592  sizeof(s->cdlms[ich][ilms].coefs));
593  memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
594  sizeof(s->cdlms[ich][ilms].lms_prevvalues));
595  memset(s->cdlms[ich][ilms].lms_updates, 0,
596  sizeof(s->cdlms[ich][ilms].lms_updates));
597  }
598  s->ave_sum[ich] = 0;
599  }
600 }
601 
602 /**
603  * @brief Reset filter parameters and transient area at new seekable tile.
604  */
606 {
607  int ich, ilms;
609  for (ich = 0; ich < s->num_channels; ich++) {
610  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
611  s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
612  /* first sample of a seekable subframe is considered as the starting of
613  a transient area which is samples_per_frame samples long */
615  s->transient[ich] = 1;
616  s->transient_pos[ich] = 0;
617  }
618 }
619 
620 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
621 {
622  int i, j, ich, pred_error;
623  int order = s->mclms_order;
624  int num_channels = s->num_channels;
625  int range = 1 << (s->bits_per_sample - 1);
626 
627  for (ich = 0; ich < num_channels; ich++) {
628  pred_error = s->channel_residues[ich][icoef] - (unsigned)pred[ich];
629  if (pred_error > 0) {
630  for (i = 0; i < order * num_channels; i++)
631  s->mclms_coeffs[i + ich * order * num_channels] +=
632  s->mclms_updates[s->mclms_recent + i];
633  for (j = 0; j < ich; j++)
634  s->mclms_coeffs_cur[ich * num_channels + j] += WMASIGN(s->channel_residues[j][icoef]);
635  } else if (pred_error < 0) {
636  for (i = 0; i < order * num_channels; i++)
637  s->mclms_coeffs[i + ich * order * num_channels] -=
638  s->mclms_updates[s->mclms_recent + i];
639  for (j = 0; j < ich; j++)
640  s->mclms_coeffs_cur[ich * num_channels + j] -= WMASIGN(s->channel_residues[j][icoef]);
641  }
642  }
643 
644  for (ich = num_channels - 1; ich >= 0; ich--) {
645  s->mclms_recent--;
646  s->mclms_prevvalues[s->mclms_recent] = av_clip(s->channel_residues[ich][icoef],
647  -range, range - 1);
648  s->mclms_updates[s->mclms_recent] = WMASIGN(s->channel_residues[ich][icoef]);
649  }
650 
651  if (s->mclms_recent == 0) {
652  memcpy(&s->mclms_prevvalues[order * num_channels],
653  s->mclms_prevvalues,
654  sizeof(int16_t) * order * num_channels);
655  memcpy(&s->mclms_updates[order * num_channels],
656  s->mclms_updates,
657  sizeof(int16_t) * order * num_channels);
658  s->mclms_recent = num_channels * order;
659  }
660 }
661 
662 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
663 {
664  int ich, i;
665  int order = s->mclms_order;
666  int num_channels = s->num_channels;
667 
668  for (ich = 0; ich < num_channels; ich++) {
669  pred[ich] = 0;
670  if (!s->is_channel_coded[ich])
671  continue;
672  for (i = 0; i < order * num_channels; i++)
673  pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
674  s->mclms_coeffs[i + order * num_channels * ich];
675  for (i = 0; i < ich; i++)
676  pred[ich] += s->channel_residues[i][icoef] *
677  s->mclms_coeffs_cur[i + num_channels * ich];
678  pred[ich] += (1U << s->mclms_scaling) >> 1;
679  pred[ich] >>= s->mclms_scaling;
680  s->channel_residues[ich][icoef] += (unsigned)pred[ich];
681  }
682 }
683 
684 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
685 {
686  int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
687  for (icoef = 0; icoef < tile_size; icoef++) {
688  mclms_predict(s, icoef, pred);
689  mclms_update(s, icoef, pred);
690  }
691 }
692 
693 static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input)
694 {
695  int recent = s->cdlms[ich][ilms].recent;
696  int range = 1 << s->bits_per_sample - 1;
697  int order = s->cdlms[ich][ilms].order;
698 
699  if (recent)
700  recent--;
701  else {
702  memcpy(s->cdlms[ich][ilms].lms_prevvalues + order,
703  s->cdlms[ich][ilms].lms_prevvalues, sizeof(*s->cdlms[ich][ilms].lms_prevvalues) * order);
704  memcpy(s->cdlms[ich][ilms].lms_updates + order,
705  s->cdlms[ich][ilms].lms_updates, sizeof(*s->cdlms[ich][ilms].lms_updates) * order);
706  recent = order - 1;
707  }
708 
709  s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
710  s->cdlms[ich][ilms].lms_updates[recent] = WMASIGN(input) * s->update_speed[ich];
711 
712  s->cdlms[ich][ilms].lms_updates[recent + (order >> 4)] >>= 2;
713  s->cdlms[ich][ilms].lms_updates[recent + (order >> 3)] >>= 1;
714  s->cdlms[ich][ilms].recent = recent;
715  memset(s->cdlms[ich][ilms].lms_updates + recent + order, 0,
716  sizeof(s->cdlms[ich][ilms].lms_updates) - 2*(recent+order));
717 }
718 
719 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
720 {
721  int ilms, recent, icoef;
722  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
723  recent = s->cdlms[ich][ilms].recent;
724  if (s->update_speed[ich] == 16)
725  continue;
726  if (s->bV3RTM) {
727  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
728  s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
729  } else {
730  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
731  s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
732  }
733  }
734  s->update_speed[ich] = 16;
735 }
736 
738 {
739  int ilms, recent, icoef;
740  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
741  recent = s->cdlms[ich][ilms].recent;
742  if (s->update_speed[ich] == 8)
743  continue;
744  if (s->bV3RTM)
745  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
746  s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
747  else
748  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
749  s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
750  }
751  s->update_speed[ich] = 8;
752 }
753 
754 static void revert_cdlms(WmallDecodeCtx *s, int ch,
755  int coef_begin, int coef_end)
756 {
757  int icoef, pred, ilms, num_lms, residue, input;
758 
759  num_lms = s->cdlms_ttl[ch];
760  for (ilms = num_lms - 1; ilms >= 0; ilms--) {
761  for (icoef = coef_begin; icoef < coef_end; icoef++) {
762  pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
763  residue = s->channel_residues[ch][icoef];
764  pred += s->dsp.scalarproduct_and_madd_int16(s->cdlms[ch][ilms].coefs,
765  s->cdlms[ch][ilms].lms_prevvalues
766  + s->cdlms[ch][ilms].recent,
767  s->cdlms[ch][ilms].lms_updates
768  + s->cdlms[ch][ilms].recent,
769  FFALIGN(s->cdlms[ch][ilms].order,
771  WMASIGN(residue));
772  input = residue + (pred >> s->cdlms[ch][ilms].scaling);
773  lms_update(s, ch, ilms, input);
774  s->channel_residues[ch][icoef] = input;
775  }
776  }
777  emms_c();
778 }
779 
780 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
781 {
782  if (s->num_channels != 2)
783  return;
784  else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
785  int icoef;
786  for (icoef = 0; icoef < tile_size; icoef++) {
787  s->channel_residues[0][icoef] -= (unsigned)(s->channel_residues[1][icoef] >> 1);
788  s->channel_residues[1][icoef] += (unsigned) s->channel_residues[0][icoef];
789  }
790  }
791 }
792 
793 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
794 {
795  int ich, pred, i, j;
796  int16_t *filter_coeffs = s->acfilter_coeffs;
797  int scaling = s->acfilter_scaling;
798  int order = s->acfilter_order;
799 
800  for (ich = 0; ich < s->num_channels; ich++) {
801  int *prevvalues = s->acfilter_prevvalues[ich];
802  for (i = 0; i < order; i++) {
803  pred = 0;
804  for (j = 0; j < order; j++) {
805  if (i <= j)
806  pred += (uint32_t)filter_coeffs[j] * prevvalues[j - i];
807  else
808  pred += (uint32_t)s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
809  }
810  pred >>= scaling;
811  s->channel_residues[ich][i] += (unsigned)pred;
812  }
813  for (i = order; i < tile_size; i++) {
814  pred = 0;
815  for (j = 0; j < order; j++)
816  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
817  pred >>= scaling;
818  s->channel_residues[ich][i] += (unsigned)pred;
819  }
820  for (j = order - 1; j >= 0; j--)
821  if (tile_size <= j) {
822  prevvalues[j] = prevvalues[j - tile_size];
823  }else
824  prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
825  }
826 }
827 
829 {
830  int offset = s->samples_per_frame;
831  int subframe_len = s->samples_per_frame;
832  int total_samples = s->samples_per_frame * s->num_channels;
833  int i, j, rawpcm_tile, padding_zeroes, res;
834 
836 
837  /* reset channel context and find the next block offset and size
838  == the next block of the channel with the smallest number of
839  decoded samples */
840  for (i = 0; i < s->num_channels; i++) {
841  if (offset > s->channel[i].decoded_samples) {
842  offset = s->channel[i].decoded_samples;
843  subframe_len =
845  }
846  }
847 
848  /* get a list of all channels that contain the estimated block */
850  for (i = 0; i < s->num_channels; i++) {
851  const int cur_subframe = s->channel[i].cur_subframe;
852  /* subtract already processed samples */
853  total_samples -= s->channel[i].decoded_samples;
854 
855  /* and count if there are multiple subframes that match our profile */
856  if (offset == s->channel[i].decoded_samples &&
857  subframe_len == s->channel[i].subframe_len[cur_subframe]) {
858  total_samples -= s->channel[i].subframe_len[cur_subframe];
859  s->channel[i].decoded_samples +=
860  s->channel[i].subframe_len[cur_subframe];
863  }
864  }
865 
866  /* check if the frame will be complete after processing the
867  estimated block */
868  if (!total_samples)
869  s->parsed_all_subframes = 1;
870 
871 
872  s->seekable_tile = get_bits1(&s->gb);
873  if (s->seekable_tile) {
875 
876  s->do_arith_coding = get_bits1(&s->gb);
877  if (s->do_arith_coding) {
878  avpriv_request_sample(s->avctx, "Arithmetic coding");
879  return AVERROR_PATCHWELCOME;
880  }
881  s->do_ac_filter = get_bits1(&s->gb);
882  s->do_inter_ch_decorr = get_bits1(&s->gb);
883  s->do_mclms = get_bits1(&s->gb);
884 
885  if (s->do_ac_filter)
886  decode_ac_filter(s);
887 
888  if (s->do_mclms)
889  decode_mclms(s);
890 
891  if ((res = decode_cdlms(s)) < 0)
892  return res;
893  s->movave_scaling = get_bits(&s->gb, 3);
894  s->quant_stepsize = get_bits(&s->gb, 8) + 1;
895 
896  reset_codec(s);
897  } else if (!s->cdlms[0][0].order) {
899  "Waiting for seekable tile\n");
900  av_frame_unref(s->frame);
901  return -1;
902  }
903 
904  rawpcm_tile = get_bits1(&s->gb);
905 
906  for (i = 0; i < s->num_channels; i++)
907  s->is_channel_coded[i] = 1;
908 
909  if (!rawpcm_tile) {
910  for (i = 0; i < s->num_channels; i++)
911  s->is_channel_coded[i] = get_bits1(&s->gb);
912 
913  if (s->bV3RTM) {
914  // LPC
915  s->do_lpc = get_bits1(&s->gb);
916  if (s->do_lpc) {
917  decode_lpc(s);
918  avpriv_request_sample(s->avctx, "Expect wrong output since "
919  "inverse LPC filter");
920  }
921  } else
922  s->do_lpc = 0;
923  }
924 
925 
926  if (get_bits1(&s->gb))
927  padding_zeroes = get_bits(&s->gb, 5);
928  else
929  padding_zeroes = 0;
930 
931  if (rawpcm_tile) {
932  int bits = s->bits_per_sample - padding_zeroes;
933  if (bits <= 0) {
935  "Invalid number of padding bits in raw PCM tile\n");
936  return AVERROR_INVALIDDATA;
937  }
938  ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
939  "total %d bits, remain=%d\n", bits,
940  bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
941  for (i = 0; i < s->num_channels; i++)
942  for (j = 0; j < subframe_len; j++)
943  s->channel_coeffs[i][j] = get_sbits_long(&s->gb, bits);
944  } else {
945  for (i = 0; i < s->num_channels; i++)
946  if (s->is_channel_coded[i]) {
947  decode_channel_residues(s, i, subframe_len);
948  if (s->seekable_tile)
949  use_high_update_speed(s, i);
950  else
952  revert_cdlms(s, i, 0, subframe_len);
953  } else {
954  memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
955  }
956  }
957  if (s->do_mclms)
958  revert_mclms(s, subframe_len);
959  if (s->do_inter_ch_decorr)
960  revert_inter_ch_decorr(s, subframe_len);
961  if (s->do_ac_filter)
962  revert_acfilter(s, subframe_len);
963 
964  /* Dequantize */
965  if (s->quant_stepsize != 1)
966  for (i = 0; i < s->num_channels; i++)
967  for (j = 0; j < subframe_len; j++)
968  s->channel_residues[i][j] *= s->quant_stepsize;
969 
970  /* Write to proper output buffer depending on bit-depth */
971  for (i = 0; i < s->channels_for_cur_subframe; i++) {
973  int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
974 
975  for (j = 0; j < subframe_len; j++) {
976  if (s->bits_per_sample == 16) {
977  *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
978  } else {
979  *s->samples_32[c]++ = s->channel_residues[c][j] << (padding_zeroes + 8);
980  }
981  }
982  }
983 
984  /* handled one subframe */
985  for (i = 0; i < s->channels_for_cur_subframe; i++) {
987  if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
988  av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
989  return AVERROR_INVALIDDATA;
990  }
991  ++s->channel[c].cur_subframe;
992  }
993  return 0;
994 }
995 
996 /**
997  * @brief Decode one WMA frame.
998  * @param s codec context
999  * @return 0 if the trailer bit indicates that this is the last frame,
1000  * 1 if there are additional frames
1001  */
1003 {
1004  GetBitContext* gb = &s->gb;
1005  int more_frames = 0, len = 0, i, ret;
1006 
1008  if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1009  /* return an error if no frame could be decoded at all */
1010  s->packet_loss = 1;
1011  s->frame->nb_samples = 0;
1012  return ret;
1013  }
1014  for (i = 0; i < s->num_channels; i++) {
1015  s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1016  s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1017  }
1018 
1019  /* get frame length */
1020  if (s->len_prefix)
1021  len = get_bits(gb, s->log2_frame_size);
1022 
1023  /* decode tile information */
1024  if ((ret = decode_tilehdr(s))) {
1025  s->packet_loss = 1;
1026  av_frame_unref(s->frame);
1027  return ret;
1028  }
1029 
1030  /* read drc info */
1032  s->drc_gain = get_bits(gb, 8);
1033 
1034  /* no idea what these are for, might be the number of samples
1035  that need to be skipped at the beginning or end of a stream */
1036  if (get_bits1(gb)) {
1037  int av_unused skip;
1038 
1039  /* usually true for the first frame */
1040  if (get_bits1(gb)) {
1041  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1042  ff_dlog(s->avctx, "start skip: %i\n", skip);
1043  }
1044 
1045  /* sometimes true for the last frame */
1046  if (get_bits1(gb)) {
1047  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1048  ff_dlog(s->avctx, "end skip: %i\n", skip);
1049  }
1050 
1051  }
1052 
1053  /* reset subframe states */
1054  s->parsed_all_subframes = 0;
1055  for (i = 0; i < s->num_channels; i++) {
1056  s->channel[i].decoded_samples = 0;
1057  s->channel[i].cur_subframe = 0;
1058  }
1059 
1060  /* decode all subframes */
1061  while (!s->parsed_all_subframes) {
1062  int decoded_samples = s->channel[0].decoded_samples;
1063  if (decode_subframe(s) < 0) {
1064  s->packet_loss = 1;
1065  if (s->frame->nb_samples)
1066  s->frame->nb_samples = decoded_samples;
1067  return 0;
1068  }
1069  }
1070 
1071  ff_dlog(s->avctx, "Frame done\n");
1072 
1073  s->skip_frame = 0;
1074 
1075  if (s->len_prefix) {
1076  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1077  /* FIXME: not sure if this is always an error */
1079  "frame[%"PRIu32"] would have to skip %i bits\n",
1080  s->frame_num,
1081  len - (get_bits_count(gb) - s->frame_offset) - 1);
1082  s->packet_loss = 1;
1083  return 0;
1084  }
1085 
1086  /* skip the rest of the frame data */
1087  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1088  }
1089 
1090  /* decode trailer bit */
1091  more_frames = get_bits1(gb);
1092  ++s->frame_num;
1093  return more_frames;
1094 }
1095 
1096 /**
1097  * @brief Calculate remaining input buffer length.
1098  * @param s codec context
1099  * @param gb bitstream reader context
1100  * @return remaining size in bits
1101  */
1103 {
1104  return s->buf_bit_size - get_bits_count(gb);
1105 }
1106 
1107 /**
1108  * @brief Fill the bit reservoir with a (partial) frame.
1109  * @param s codec context
1110  * @param gb bitstream reader context
1111  * @param len length of the partial frame
1112  * @param append decides whether to reset the buffer or not
1113  */
1114 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1115  int append)
1116 {
1117  int buflen;
1118  PutBitContext tmp;
1119 
1120  /* when the frame data does not need to be concatenated, the input buffer
1121  is reset and additional bits from the previous frame are copied
1122  and skipped later so that a fast byte copy is possible */
1123 
1124  if (!append) {
1125  s->frame_offset = get_bits_count(gb) & 7;
1126  s->num_saved_bits = s->frame_offset;
1128  }
1129 
1130  buflen = (s->num_saved_bits + len + 8) >> 3;
1131 
1132  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1133  avpriv_request_sample(s->avctx, "Too small input buffer");
1134  s->packet_loss = 1;
1135  s->num_saved_bits = 0;
1136  return;
1137  }
1138 
1139  s->num_saved_bits += len;
1140  if (!append) {
1141  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1142  s->num_saved_bits);
1143  } else {
1144  int align = 8 - (get_bits_count(gb) & 7);
1145  align = FFMIN(align, len);
1146  put_bits(&s->pb, align, get_bits(gb, align));
1147  len -= align;
1148  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1149  }
1150  skip_bits_long(gb, len);
1151 
1152  tmp = s->pb;
1153  flush_put_bits(&tmp);
1154 
1156  skip_bits(&s->gb, s->frame_offset);
1157 }
1158 
1159 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1160  AVPacket* avpkt)
1161 {
1162  WmallDecodeCtx *s = avctx->priv_data;
1163  GetBitContext* gb = &s->pgb;
1164  const uint8_t* buf = avpkt->data;
1165  int buf_size = avpkt->size;
1166  int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1167 
1168  s->frame->nb_samples = 0;
1169 
1170  if (s->packet_done || s->packet_loss) {
1171  s->packet_done = 0;
1172 
1173  if (!buf_size)
1174  return 0;
1175  /* sanity check for the buffer length */
1176  if (buf_size < avctx->block_align) {
1177  av_log(avctx, AV_LOG_ERROR, "buf size %d invalid\n", buf_size);
1178  return AVERROR_INVALIDDATA;
1179  }
1180 
1181  s->next_packet_start = buf_size - avctx->block_align;
1182  buf_size = avctx->block_align;
1183  s->buf_bit_size = buf_size << 3;
1184 
1185  /* parse packet header */
1186  init_get_bits(gb, buf, s->buf_bit_size);
1187  packet_sequence_number = get_bits(gb, 4);
1188  skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1189  spliced_packet = get_bits1(gb);
1190  if (spliced_packet)
1191  avpriv_request_sample(avctx, "Bitstream splicing");
1192 
1193  /* get number of bits that need to be added to the previous frame */
1194  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1195 
1196  /* check for packet loss */
1197  if (!s->packet_loss &&
1198  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1199  s->packet_loss = 1;
1200  av_log(avctx, AV_LOG_ERROR,
1201  "Packet loss detected! seq %"PRIx8" vs %x\n",
1202  s->packet_sequence_number, packet_sequence_number);
1203  }
1204  s->packet_sequence_number = packet_sequence_number;
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  ff_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 
1245  if (!s->packet_loss)
1246  s->packet_done = !decode_frame(s);
1247  } else if (!s->len_prefix
1248  && s->num_saved_bits > get_bits_count(&s->gb)) {
1249  /* when the frames do not have a length prefix, we don't know the
1250  * compressed length of the individual frames however, we know what
1251  * part of a new packet belongs to the previous frame therefore we
1252  * save the incoming packet first, then we append the "previous
1253  * frame" data from the next packet so that we get a buffer that
1254  * only contains full frames */
1255  s->packet_done = !decode_frame(s);
1256  } else {
1257  s->packet_done = 1;
1258  }
1259  }
1260 
1261  if (remaining_bits(s, gb) < 0) {
1262  av_log(avctx, AV_LOG_ERROR, "Overread %d\n", -remaining_bits(s, gb));
1263  s->packet_loss = 1;
1264  }
1265 
1266  if (s->packet_done && !s->packet_loss &&
1267  remaining_bits(s, gb) > 0) {
1268  /* save the rest of the data so that it can be decoded
1269  * with the next packet */
1270  save_bits(s, gb, remaining_bits(s, gb), 0);
1271  }
1272 
1273  *got_frame_ptr = s->frame->nb_samples > 0;
1274  av_frame_move_ref(data, s->frame);
1275 
1276  s->packet_offset = get_bits_count(gb) & 7;
1277 
1278  return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1279 }
1280 
1281 static void flush(AVCodecContext *avctx)
1282 {
1283  WmallDecodeCtx *s = avctx->priv_data;
1284  s->packet_loss = 1;
1285  s->packet_done = 0;
1286  s->num_saved_bits = 0;
1287  s->frame_offset = 0;
1288  s->next_packet_start = 0;
1289  s->cdlms[0][0].order = 0;
1290  s->frame->nb_samples = 0;
1292 }
1293 
1295 {
1296  WmallDecodeCtx *s = avctx->priv_data;
1297 
1298  av_frame_free(&s->frame);
1299 
1300  return 0;
1301 }
1302 
1304  .name = "wmalossless",
1305  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1306  .type = AVMEDIA_TYPE_AUDIO,
1308  .priv_data_size = sizeof(WmallDecodeCtx),
1309  .init = decode_init,
1310  .close = decode_close,
1311  .decode = decode_packet,
1312  .flush = flush,
1314  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1317 };
static void decode_ac_filter(WmallDecodeCtx *s)
int16_t prev_block_len
length of the previous block
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:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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:171
uint8_t max_subframe_len_bit
flag indicating that the subframe is of maximum size when the first subframe length bit is 1 ...
unsigned ave_sum[WMALL_MAX_CHANNELS]
uint8_t max_num_subframes
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint16_t subframe_offsets[MAX_SUBFRAMES]
subframe positions in the current frame
int16_t mclms_updates[WMALL_MAX_CHANNELS *2 *32]
int32_t * samples_32[WMALL_MAX_CHANNELS]
current samplebuffer pointer (24-bit)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:205
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:218
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t drc_gain
gain for the DRC tool
#define avpriv_request_sample(...)
static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input)
int acfilter_prevvalues[WMALL_MAX_CHANNELS][16]
int size
Definition: avcodec.h:1434
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:64
const uint8_t * buffer
Definition: get_bits.h:56
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:53
PutBitContext pb
context for filling the frame_data buffer
#define AV_RL16
Definition: intreadwrite.h:42
uint8_t cur_subframe
current subframe number
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:485
LLAudDSPContext dsp
accelerated DSP functions
static void decode_mclms(WmallDecodeCtx *s)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3013
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]
AVCodec.
Definition: avcodec.h:3482
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2309
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:246
#define MAX_ORDER
int8_t channels_for_cur_subframe
number of channels that contain the subframe
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:97
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:376
uint8_t packet_sequence_number
current packet number
int16_t subframe_len
current subframe length
uint16_t decoded_samples
number of already processed samples
#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:882
GetBitContext pgb
bitstream reader context for the packet
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int quant_step
quantization step for the current subframe
int16_t coefs[MAX_ORDER+WMALL_COEFF_PAD_SIZE/sizeof(int16_t)]
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2280
uint8_t
#define av_cold
Definition: attributes.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
int16_t acfilter_coeffs[16]
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.
#define emms_c()
Definition: internal.h:53
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
int update_speed[WMALL_MAX_CHANNELS]
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)
#define WMASIGN(x)
Get sign of integer (1 for positive, -1 for negative and 0 for zero)
uint16_t min_samples_per_subframe
uint8_t * data
Definition: avcodec.h:1433
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:213
#define ff_dlog(a,...)
bitstream reader API header.
AVFrame * frame
#define av_log(a,...)
frame-specific decoder context for a single channel
int8_t lfe_channel
lfe channel index
uint8_t do_arith_coding
#define U(x)
Definition: vp56_arith.h:37
int buf_bit_size
buffer size in bits
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:594
int16_t mclms_coeffs[WMALL_MAX_CHANNELS *WMALL_MAX_CHANNELS *32]
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
static av_cold int decode_init(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
simple assert() macros that are a bit more flexible than ISO C assert().
int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS]
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
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 const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVCodecContext * avctx
#define FFMAX(a, b)
Definition: common.h:90
Libavcodec 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.
Definition: avcodec.h:2333
static int decode_tilehdr(WmallDecodeCtx *s)
Decode how the data in the frame is split into subframes.
static void use_high_update_speed(WmallDecodeCtx *s, int ich)
#define MAX_SUBFRAMES
max number of subframes per channel
static av_cold int decode_close(AVCodecContext *avctx)
uint8_t packet_loss
set in case of bitstream error
static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
static void clear_codec_buffers(WmallDecodeCtx *s)
#define FFMIN(a, b)
Definition: common.h:92
uint16_t log2_frame_size
signed 32 bits, planar
Definition: samplefmt.h:69
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:288
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
uint8_t frame_data[MAX_FRAMESIZE+AV_INPUT_BUFFER_PADDING_SIZE]
compressed frame data
#define AV_RL32
Definition: intreadwrite.h:146
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
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)
int16_t mclms_coeffs_cur[WMALL_MAX_CHANNELS *WMALL_MAX_CHANNELS]
#define av_log2
Definition: intmath.h:100
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:658
int next_packet_start
start offset of the next WMA packet in the demuxer packet
int frame_size
Definition: mxfenc.c:1819
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int sample_rate
samples per second
Definition: avcodec.h:2272
#define WMALL_MAX_CHANNELS
current decoder limitations
main external API structure.
Definition: avcodec.h:1512
int16_t * samples_16[WMALL_MAX_CHANNELS]
current samplebuffer pointer (16-bit)
static void decode_lpc(WmallDecodeCtx *s)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1048
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1628
struct WmallDecodeCtx::@117 cdlms[WMALL_MAX_CHANNELS][9]
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:305
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:298
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:907
int32_t(* scalarproduct_and_madd_int16)(int16_t *v1, const int16_t *v2, const int16_t *v3, int len, int mul)
Calculate scalar product of v1 and v2, and v1[i] += v3[i] * mul.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:415
int16_t mclms_prevvalues[WMALL_MAX_CHANNELS *2 *32]
uint8_t num_subframes
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:338
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 av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:465
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
uint8_t do_inter_ch_decorr
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:521
uint16_t subframe_len[MAX_SUBFRAMES]
subframe length in samples
WmallChannelCtx channel[WMALL_MAX_CHANNELS]
per channel data
#define MAX_FRAMESIZE
maximum compressed frame size
common internal api header.
#define WMALL_COEFF_PAD_SIZE
pad coef buffers with 0 for use with SIMD
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
if(ret< 0)
Definition: vf_mcdeint.c:280
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]
int16_t lms_prevvalues[MAX_ORDER *2+WMALL_COEFF_PAD_SIZE/sizeof(int16_t)]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static void save_bits(WmallDecodeCtx *s, GetBitContext *gb, int len, int append)
Fill the bit reservoir with a (partial) frame.
int transient_pos[WMALL_MAX_CHANNELS]
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
void * priv_data
Definition: avcodec.h:1554
int len
int channels
number of audio channels
Definition: avcodec.h:2273
int16_t lms_updates[MAX_ORDER *2+WMALL_COEFF_PAD_SIZE/sizeof(int16_t)]
AVCodec ff_wmalossless_decoder
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
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.
signed 16 bits, planar
Definition: samplefmt.h:68
int transient[WMALL_MAX_CHANNELS]
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int num_saved_bits
saved number of bits
int subframe_offset
subframe offset in the bit reservoir
This structure stores compressed data.
Definition: avcodec.h:1410
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
int lpc_coefs[WMALL_MAX_CHANNELS][40]
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857
for(j=16;j >0;--j)
#define av_unused
Definition: attributes.h:118
static void revert_mclms(WmallDecodeCtx *s, int tile_size)
bitstream writer API