Libav
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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; 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 "wma.h"
35 #include "wma_common.h"
36 
38 #define WMALL_MAX_CHANNELS 8
39 #define MAX_SUBFRAMES 32
40 #define MAX_BANDS 29
41 #define MAX_FRAMESIZE 32768
42 #define MAX_ORDER 256
43 
44 #define WMALL_BLOCK_MIN_BITS 6
45 #define WMALL_BLOCK_MAX_BITS 14
46 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)
47 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1)
48 
49 
53 typedef struct WmallChannelCtx {
54  int16_t prev_block_len;
60  uint16_t decoded_samples;
61  int quant_step;
64 
68 typedef struct WmallDecodeCtx {
69  /* generic decoder variables */
74 
75  /* frame size dependent frame information (set during initialization) */
76  uint32_t decode_flags;
77  int len_prefix;
80  uint16_t samples_per_frame;
81  uint16_t log2_frame_size;
82  int8_t num_channels;
83  int8_t lfe_channel;
88 
89  /* packet decode state */
99 
100  /* frame decode state */
101  uint32_t frame_num;
104  int16_t *samples_16[WMALL_MAX_CHANNELS];
107  int8_t skip_frame;
109 
110  /* subframe/block decode state */
111  int16_t subframe_len;
113  int8_t channel_indexes_for_cur_subframe[WMALL_MAX_CHANNELS];
114 
116 
117  // WMA Lossless-specific
118 
124 
127  int64_t acfilter_coeffs[16];
128  int acfilter_prevvalues[2][16];
129 
130  int8_t mclms_order;
132  int16_t mclms_coeffs[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS * 32];
133  int16_t mclms_coeffs_cur[WMALL_MAX_CHANNELS * WMALL_MAX_CHANNELS];
134  int16_t mclms_prevvalues[WMALL_MAX_CHANNELS * 2 * 32];
135  int16_t mclms_updates[WMALL_MAX_CHANNELS * 2 * 32];
137 
140 
141  struct {
142  int order;
143  int scaling;
144  int coefsend;
145  int bitsend;
146  int16_t coefs[MAX_ORDER];
147  int16_t lms_prevvalues[MAX_ORDER * 2];
148  int16_t lms_updates[MAX_ORDER * 2];
149  int recent;
150  } cdlms[2][9];
151 
152  int cdlms_ttl[2];
153 
154  int bV3RTM;
155 
156  int is_channel_coded[2];
157  int update_speed[2];
158 
159  int transient[2];
160  int transient_pos[2];
162 
163  int ave_sum[2];
164 
165  int channel_residues[2][WMALL_BLOCK_MAX_SIZE];
166 
167  int lpc_coefs[2][40];
171 
172  int channel_coeffs[2][WMALL_BLOCK_MAX_SIZE];
174 
175 
177 {
178  WmallDecodeCtx *s = avctx->priv_data;
179  uint8_t *edata_ptr = avctx->extradata;
180  unsigned int channel_mask;
181  int i, log2_max_num_subframes;
182 
183  s->avctx = avctx;
185 
186  if (avctx->extradata_size >= 18) {
187  s->decode_flags = AV_RL16(edata_ptr + 14);
188  channel_mask = AV_RL32(edata_ptr + 2);
189  s->bits_per_sample = AV_RL16(edata_ptr);
190  if (s->bits_per_sample == 16)
192  else if (s->bits_per_sample == 24) {
194  avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
195  return AVERROR_PATCHWELCOME;
196  } else {
197  av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %"PRIu8"\n",
198  s->bits_per_sample);
199  return AVERROR_INVALIDDATA;
200  }
201  /* dump the extradata */
202  for (i = 0; i < avctx->extradata_size; i++)
203  ff_dlog(avctx, "[%x] ", avctx->extradata[i]);
204  ff_dlog(avctx, "\n");
205 
206  } else {
207  avpriv_request_sample(avctx, "Unsupported extradata size");
208  return AVERROR_PATCHWELCOME;
209  }
210 
211  /* generic init */
212  s->log2_frame_size = av_log2(avctx->block_align) + 4;
213 
214  /* frame info */
215  s->skip_frame = 1; /* skip first frame */
216  s->packet_loss = 1;
217  s->len_prefix = s->decode_flags & 0x40;
218 
219  /* get frame len */
221  3, s->decode_flags);
223 
224  /* init previous block len */
225  for (i = 0; i < avctx->channels; i++)
227 
228  /* subframe info */
229  log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
230  s->max_num_subframes = 1 << log2_max_num_subframes;
231  s->max_subframe_len_bit = 0;
232  s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
233 
236  s->bV3RTM = s->decode_flags & 0x100;
237 
238  if (s->max_num_subframes > MAX_SUBFRAMES) {
239  av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %"PRIu8"\n",
240  s->max_num_subframes);
241  return AVERROR_INVALIDDATA;
242  }
243 
244  s->num_channels = avctx->channels;
245 
246  /* extract lfe channel position */
247  s->lfe_channel = -1;
248 
249  if (channel_mask & 8) {
250  unsigned int mask;
251  for (mask = 1; mask < 16; mask <<= 1)
252  if (channel_mask & mask)
253  ++s->lfe_channel;
254  }
255 
256  if (s->num_channels < 0) {
257  av_log(avctx, AV_LOG_ERROR, "invalid number of channels %"PRId8"\n",
258  s->num_channels);
259  return AVERROR_INVALIDDATA;
260  } else if (s->num_channels > WMALL_MAX_CHANNELS) {
261  avpriv_request_sample(avctx,
262  "More than %d channels", WMALL_MAX_CHANNELS);
263  return AVERROR_PATCHWELCOME;
264  }
265 
266  s->frame = av_frame_alloc();
267  if (!s->frame)
268  return AVERROR(ENOMEM);
269 
270  avctx->channel_layout = channel_mask;
271  return 0;
272 }
273 
280 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
281 {
282  int frame_len_ratio, subframe_len, len;
283 
284  /* no need to read from the bitstream when only one length is possible */
285  if (offset == s->samples_per_frame - s->min_samples_per_subframe)
286  return s->min_samples_per_subframe;
287 
288  len = av_log2(s->max_num_subframes - 1) + 1;
289  frame_len_ratio = get_bits(&s->gb, len);
290  subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
291 
292  /* sanity check the length */
293  if (subframe_len < s->min_samples_per_subframe ||
294  subframe_len > s->samples_per_frame) {
295  av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
296  subframe_len);
297  return AVERROR_INVALIDDATA;
298  }
299  return subframe_len;
300 }
301 
323 {
324  uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
325  uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
326  int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
327  int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
328  int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
329  int c, tile_aligned;
330 
331  /* reset tiling information */
332  for (c = 0; c < s->num_channels; c++)
333  s->channel[c].num_subframes = 0;
334 
335  tile_aligned = get_bits1(&s->gb);
336  if (s->max_num_subframes == 1 || tile_aligned)
337  fixed_channel_layout = 1;
338 
339  /* loop until the frame data is split between the subframes */
340  do {
341  int subframe_len, in_use = 0;
342 
343  /* check which channels contain the subframe */
344  for (c = 0; c < s->num_channels; c++) {
345  if (num_samples[c] == min_channel_len) {
346  if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
347  (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
348  contains_subframe[c] = in_use = 1;
349  } else {
350  if (get_bits1(&s->gb))
351  contains_subframe[c] = in_use = 1;
352  }
353  } else
354  contains_subframe[c] = 0;
355  }
356 
357  if (!in_use) {
359  "Found empty subframe\n");
360  return AVERROR_INVALIDDATA;
361  }
362 
363  /* get subframe length, subframe_len == 0 is not allowed */
364  if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
365  return AVERROR_INVALIDDATA;
366  /* add subframes to the individual channels and find new min_channel_len */
367  min_channel_len += subframe_len;
368  for (c = 0; c < s->num_channels; c++) {
369  WmallChannelCtx *chan = &s->channel[c];
370 
371  if (contains_subframe[c]) {
372  if (chan->num_subframes >= MAX_SUBFRAMES) {
374  "broken frame: num subframes > 31\n");
375  return AVERROR_INVALIDDATA;
376  }
377  chan->subframe_len[chan->num_subframes] = subframe_len;
378  num_samples[c] += subframe_len;
379  ++chan->num_subframes;
380  if (num_samples[c] > s->samples_per_frame) {
381  av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
382  "channel len(%"PRIu16") > samples_per_frame(%"PRIu16")\n",
383  num_samples[c], s->samples_per_frame);
384  return AVERROR_INVALIDDATA;
385  }
386  } else if (num_samples[c] <= min_channel_len) {
387  if (num_samples[c] < min_channel_len) {
388  channels_for_cur_subframe = 0;
389  min_channel_len = num_samples[c];
390  }
391  ++channels_for_cur_subframe;
392  }
393  }
394  } while (min_channel_len < s->samples_per_frame);
395 
396  for (c = 0; c < s->num_channels; c++) {
397  int i, offset = 0;
398  for (i = 0; i < s->channel[c].num_subframes; i++) {
399  s->channel[c].subframe_offsets[i] = offset;
400  offset += s->channel[c].subframe_len[i];
401  }
402  }
403 
404  return 0;
405 }
406 
408 {
409  int i;
410  s->acfilter_order = get_bits(&s->gb, 4) + 1;
411  s->acfilter_scaling = get_bits(&s->gb, 4);
412 
413  for (i = 0; i < s->acfilter_order; i++)
414  s->acfilter_coeffs[i] = get_bitsz(&s->gb, s->acfilter_scaling) + 1;
415 }
416 
418 {
419  s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
420  s->mclms_scaling = get_bits(&s->gb, 4);
421  if (get_bits1(&s->gb)) {
422  int i, send_coef_bits;
423  int cbits = av_log2(s->mclms_scaling + 1);
424  if (1 << cbits < s->mclms_scaling + 1)
425  cbits++;
426 
427  send_coef_bits = get_bitsz(&s->gb, cbits) + 2;
428 
429  for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
430  s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
431 
432  for (i = 0; i < s->num_channels; i++) {
433  int c;
434  for (c = 0; c < i; c++)
435  s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
436  }
437  }
438 }
439 
441 {
442  int c, i;
443  int cdlms_send_coef = get_bits1(&s->gb);
444 
445  for (c = 0; c < s->num_channels; c++) {
446  s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
447  for (i = 0; i < s->cdlms_ttl[c]; i++) {
448  s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
449  if (s->cdlms[c][i].order > MAX_ORDER) {
451  "Order[%d][%d] %d > max (%d), not supported\n",
452  c, i, s->cdlms[c][i].order, MAX_ORDER);
453  s->cdlms[0][0].order = 0;
454  return AVERROR_INVALIDDATA;
455  }
456  }
457 
458  for (i = 0; i < s->cdlms_ttl[c]; i++)
459  s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
460 
461  if (cdlms_send_coef) {
462  for (i = 0; i < s->cdlms_ttl[c]; i++) {
463  int cbits, shift_l, shift_r, j;
464  cbits = av_log2(s->cdlms[c][i].order);
465  if ((1 << cbits) < s->cdlms[c][i].order)
466  cbits++;
467  s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
468 
469  cbits = av_log2(s->cdlms[c][i].scaling + 1);
470  if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
471  cbits++;
472 
473  s->cdlms[c][i].bitsend = get_bitsz(&s->gb, cbits) + 2;
474  shift_l = 32 - s->cdlms[c][i].bitsend;
475  shift_r = 32 - s->cdlms[c][i].scaling - 2;
476  for (j = 0; j < s->cdlms[c][i].coefsend; j++)
477  s->cdlms[c][i].coefs[j] =
478  (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
479  }
480  }
481  }
482 
483  return 0;
484 }
485 
486 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
487 {
488  int i = 0;
489  unsigned int ave_mean;
490  s->transient[ch] = get_bits1(&s->gb);
491  if (s->transient[ch]) {
492  s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
493  if (s->transient_pos[ch])
494  s->transient[ch] = 0;
495  s->channel[ch].transient_counter =
497  } else if (s->channel[ch].transient_counter)
498  s->transient[ch] = 1;
499 
500  if (s->seekable_tile) {
501  ave_mean = get_bits(&s->gb, s->bits_per_sample);
502  s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
503  }
504 
505  if (s->seekable_tile) {
506  if (s->do_inter_ch_decorr)
507  s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
508  else
509  s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
510  i++;
511  }
512  for (; i < tile_size; i++) {
513  int quo = 0, rem, rem_bits, residue;
514  while(get_bits1(&s->gb)) {
515  quo++;
516  if (get_bits_left(&s->gb) <= 0)
517  return -1;
518  }
519  if (quo >= 32)
520  quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
521 
522  ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
523  if (ave_mean <= 1)
524  residue = quo;
525  else {
526  rem_bits = av_ceil_log2(ave_mean);
527  rem = rem_bits ? get_bits_long(&s->gb, rem_bits) : 0;
528  residue = (quo << rem_bits) + rem;
529  }
530 
531  s->ave_sum[ch] = residue + s->ave_sum[ch] -
532  (s->ave_sum[ch] >> s->movave_scaling);
533 
534  if (residue & 1)
535  residue = -(residue >> 1) - 1;
536  else
537  residue = residue >> 1;
538  s->channel_residues[ch][i] = residue;
539  }
540 
541  return 0;
542 
543 }
544 
545 static void decode_lpc(WmallDecodeCtx *s)
546 {
547  int ch, i, cbits;
548  s->lpc_order = get_bits(&s->gb, 5) + 1;
549  s->lpc_scaling = get_bits(&s->gb, 4);
550  s->lpc_intbits = get_bits(&s->gb, 3) + 1;
551  cbits = s->lpc_scaling + s->lpc_intbits;
552  for (ch = 0; ch < s->num_channels; ch++)
553  for (i = 0; i < s->lpc_order; i++)
554  s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
555 }
556 
558 {
559  int ich, ilms;
560 
561  memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
562  memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
563  memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
564 
565  memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
566  memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
567  memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
568  memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
569 
570  for (ich = 0; ich < s->num_channels; ich++) {
571  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
572  memset(s->cdlms[ich][ilms].coefs, 0,
573  sizeof(s->cdlms[ich][ilms].coefs));
574  memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
575  sizeof(s->cdlms[ich][ilms].lms_prevvalues));
576  memset(s->cdlms[ich][ilms].lms_updates, 0,
577  sizeof(s->cdlms[ich][ilms].lms_updates));
578  }
579  s->ave_sum[ich] = 0;
580  }
581 }
582 
587 {
588  int ich, ilms;
590  for (ich = 0; ich < s->num_channels; ich++) {
591  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
592  s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
593  /* first sample of a seekable subframe is considered as the starting of
594  a transient area which is samples_per_frame samples long */
596  s->transient[ich] = 1;
597  s->transient_pos[ich] = 0;
598  }
599 }
600 
601 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
602 {
603  int i, j, ich, pred_error;
604  int order = s->mclms_order;
605  int num_channels = s->num_channels;
606  int range = 1 << (s->bits_per_sample - 1);
607 
608  for (ich = 0; ich < num_channels; ich++) {
609  pred_error = s->channel_residues[ich][icoef] - pred[ich];
610  if (pred_error > 0) {
611  for (i = 0; i < order * num_channels; i++)
612  s->mclms_coeffs[i + ich * order * num_channels] +=
613  s->mclms_updates[s->mclms_recent + i];
614  for (j = 0; j < ich; j++) {
615  if (s->channel_residues[j][icoef] > 0)
616  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
617  else if (s->channel_residues[j][icoef] < 0)
618  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
619  }
620  } else if (pred_error < 0) {
621  for (i = 0; i < order * num_channels; i++)
622  s->mclms_coeffs[i + ich * order * num_channels] -=
623  s->mclms_updates[s->mclms_recent + i];
624  for (j = 0; j < ich; j++) {
625  if (s->channel_residues[j][icoef] > 0)
626  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
627  else if (s->channel_residues[j][icoef] < 0)
628  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
629  }
630  }
631  }
632 
633  for (ich = num_channels - 1; ich >= 0; ich--) {
634  s->mclms_recent--;
635  s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
636  if (s->channel_residues[ich][icoef] > range - 1)
637  s->mclms_prevvalues[s->mclms_recent] = range - 1;
638  else if (s->channel_residues[ich][icoef] < -range)
639  s->mclms_prevvalues[s->mclms_recent] = -range;
640 
641  s->mclms_updates[s->mclms_recent] = 0;
642  if (s->channel_residues[ich][icoef] > 0)
643  s->mclms_updates[s->mclms_recent] = 1;
644  else if (s->channel_residues[ich][icoef] < 0)
645  s->mclms_updates[s->mclms_recent] = -1;
646  }
647 
648  if (s->mclms_recent == 0) {
649  memcpy(&s->mclms_prevvalues[order * num_channels],
650  s->mclms_prevvalues,
651  2 * order * num_channels);
652  memcpy(&s->mclms_updates[order * num_channels],
653  s->mclms_updates,
654  2 * order * num_channels);
655  s->mclms_recent = num_channels * order;
656  }
657 }
658 
659 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
660 {
661  int ich, i;
662  int order = s->mclms_order;
663  int num_channels = s->num_channels;
664 
665  for (ich = 0; ich < num_channels; ich++) {
666  pred[ich] = 0;
667  if (!s->is_channel_coded[ich])
668  continue;
669  for (i = 0; i < order * num_channels; i++)
670  pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
671  s->mclms_coeffs[i + order * num_channels * ich];
672  for (i = 0; i < ich; i++)
673  pred[ich] += s->channel_residues[i][icoef] *
674  s->mclms_coeffs_cur[i + num_channels * ich];
675  pred[ich] += 1 << s->mclms_scaling - 1;
676  pred[ich] >>= s->mclms_scaling;
677  s->channel_residues[ich][icoef] += pred[ich];
678  }
679 }
680 
681 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
682 {
683  int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
684  for (icoef = 0; icoef < tile_size; icoef++) {
685  mclms_predict(s, icoef, pred);
686  mclms_update(s, icoef, pred);
687  }
688 }
689 
690 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
691 {
692  int pred = 0, icoef;
693  int recent = s->cdlms[ich][ilms].recent;
694 
695  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
696  pred += s->cdlms[ich][ilms].coefs[icoef] *
697  s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
698 
699  return pred;
700 }
701 
702 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
703  int input, int residue)
704 {
705  int icoef;
706  int recent = s->cdlms[ich][ilms].recent;
707  int range = 1 << s->bits_per_sample - 1;
708 
709  if (residue < 0) {
710  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
711  s->cdlms[ich][ilms].coefs[icoef] -=
712  s->cdlms[ich][ilms].lms_updates[icoef + recent];
713  } else if (residue > 0) {
714  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
715  s->cdlms[ich][ilms].coefs[icoef] +=
716  s->cdlms[ich][ilms].lms_updates[icoef + recent];
717  }
718 
719  if (recent)
720  recent--;
721  else {
722  memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
723  s->cdlms[ich][ilms].lms_prevvalues,
724  2 * s->cdlms[ich][ilms].order);
725  memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
726  s->cdlms[ich][ilms].lms_updates,
727  2 * s->cdlms[ich][ilms].order);
728  recent = s->cdlms[ich][ilms].order - 1;
729  }
730 
731  s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
732  if (!input)
733  s->cdlms[ich][ilms].lms_updates[recent] = 0;
734  else if (input < 0)
735  s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
736  else
737  s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
738 
739  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
740  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
741  s->cdlms[ich][ilms].recent = recent;
742 }
743 
744 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
745 {
746  int ilms, recent, icoef;
747  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
748  recent = s->cdlms[ich][ilms].recent;
749  if (s->update_speed[ich] == 16)
750  continue;
751  if (s->bV3RTM) {
752  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
753  s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
754  } else {
755  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
756  s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
757  }
758  }
759  s->update_speed[ich] = 16;
760 }
761 
762 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
763 {
764  int ilms, recent, icoef;
765  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
766  recent = s->cdlms[ich][ilms].recent;
767  if (s->update_speed[ich] == 8)
768  continue;
769  if (s->bV3RTM)
770  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
771  s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
772  else
773  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
774  s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
775  }
776  s->update_speed[ich] = 8;
777 }
778 
779 static void revert_cdlms(WmallDecodeCtx *s, int ch,
780  int coef_begin, int coef_end)
781 {
782  int icoef, pred, ilms, num_lms, residue, input;
783 
784  num_lms = s->cdlms_ttl[ch];
785  for (ilms = num_lms - 1; ilms >= 0; ilms--) {
786  for (icoef = coef_begin; icoef < coef_end; icoef++) {
787  pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
788  residue = s->channel_residues[ch][icoef];
789  pred += lms_predict(s, ch, ilms);
790  input = residue + (pred >> s->cdlms[ch][ilms].scaling);
791  lms_update(s, ch, ilms, input, residue);
792  s->channel_residues[ch][icoef] = input;
793  }
794  }
795 }
796 
797 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
798 {
799  if (s->num_channels != 2)
800  return;
801  else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
802  int icoef;
803  for (icoef = 0; icoef < tile_size; icoef++) {
804  s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
805  s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
806  }
807  }
808 }
809 
810 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
811 {
812  int ich, pred, i, j;
813  int64_t *filter_coeffs = s->acfilter_coeffs;
814  int scaling = s->acfilter_scaling;
815  int order = s->acfilter_order;
816 
817  for (ich = 0; ich < s->num_channels; ich++) {
818  int *prevvalues = s->acfilter_prevvalues[ich];
819  for (i = 0; i < order; i++) {
820  pred = 0;
821  for (j = 0; j < order; j++) {
822  if (i <= j)
823  pred += filter_coeffs[j] * prevvalues[j - i];
824  else
825  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
826  }
827  pred >>= scaling;
828  s->channel_residues[ich][i] += pred;
829  }
830  for (i = order; i < tile_size; i++) {
831  pred = 0;
832  for (j = 0; j < order; j++)
833  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
834  pred >>= scaling;
835  s->channel_residues[ich][i] += pred;
836  }
837  for (j = 0; j < order; j++)
838  prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
839  }
840 }
841 
843 {
844  int offset = s->samples_per_frame;
846  int total_samples = s->samples_per_frame * s->num_channels;
847  int i, j, rawpcm_tile, padding_zeroes, res;
848 
850 
851  /* reset channel context and find the next block offset and size
852  == the next block of the channel with the smallest number of
853  decoded samples */
854  for (i = 0; i < s->num_channels; i++) {
855  if (offset > s->channel[i].decoded_samples) {
856  offset = s->channel[i].decoded_samples;
857  subframe_len =
859  }
860  }
861 
862  /* get a list of all channels that contain the estimated block */
864  for (i = 0; i < s->num_channels; i++) {
865  const int cur_subframe = s->channel[i].cur_subframe;
866  /* subtract already processed samples */
867  total_samples -= s->channel[i].decoded_samples;
868 
869  /* and count if there are multiple subframes that match our profile */
870  if (offset == s->channel[i].decoded_samples &&
871  subframe_len == s->channel[i].subframe_len[cur_subframe]) {
872  total_samples -= s->channel[i].subframe_len[cur_subframe];
873  s->channel[i].decoded_samples +=
877  }
878  }
879 
880  /* check if the frame will be complete after processing the
881  estimated block */
882  if (!total_samples)
883  s->parsed_all_subframes = 1;
884 
885 
886  s->seekable_tile = get_bits1(&s->gb);
887  if (s->seekable_tile) {
889 
890  s->do_arith_coding = get_bits1(&s->gb);
891  if (s->do_arith_coding) {
892  avpriv_request_sample(s->avctx, "Arithmetic coding");
893  return AVERROR_PATCHWELCOME;
894  }
895  s->do_ac_filter = get_bits1(&s->gb);
896  s->do_inter_ch_decorr = get_bits1(&s->gb);
897  s->do_mclms = get_bits1(&s->gb);
898 
899  if (s->do_ac_filter)
900  decode_ac_filter(s);
901 
902  if (s->do_mclms)
903  decode_mclms(s);
904 
905  if ((res = decode_cdlms(s)) < 0)
906  return res;
907  s->movave_scaling = get_bits(&s->gb, 3);
908  s->quant_stepsize = get_bits(&s->gb, 8) + 1;
909 
910  reset_codec(s);
911  } else if (!s->cdlms[0][0].order) {
913  "Waiting for seekable tile\n");
914  av_frame_unref(s->frame);
915  return -1;
916  }
917 
918  rawpcm_tile = get_bits1(&s->gb);
919 
920  for (i = 0; i < s->num_channels; i++)
921  s->is_channel_coded[i] = 1;
922 
923  if (!rawpcm_tile) {
924  for (i = 0; i < s->num_channels; i++)
925  s->is_channel_coded[i] = get_bits1(&s->gb);
926 
927  if (s->bV3RTM) {
928  // LPC
929  s->do_lpc = get_bits1(&s->gb);
930  if (s->do_lpc) {
931  decode_lpc(s);
932  avpriv_request_sample(s->avctx, "Expect wrong output since "
933  "inverse LPC filter");
934  }
935  } else
936  s->do_lpc = 0;
937  }
938 
939 
940  if (get_bits1(&s->gb))
941  padding_zeroes = get_bits(&s->gb, 5);
942  else
943  padding_zeroes = 0;
944 
945  if (rawpcm_tile) {
946  int bits = s->bits_per_sample - padding_zeroes;
947  if (bits <= 0) {
949  "Invalid number of padding bits in raw PCM tile\n");
950  return AVERROR_INVALIDDATA;
951  }
952  ff_dlog(s->avctx, "RAWPCM %d bits per sample. "
953  "total %d bits, remain=%d\n", bits,
954  bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
955  for (i = 0; i < s->num_channels; i++)
956  for (j = 0; j < subframe_len; j++)
957  s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
958  } else {
959  for (i = 0; i < s->num_channels; i++)
960  if (s->is_channel_coded[i]) {
961  decode_channel_residues(s, i, subframe_len);
962  if (s->seekable_tile)
963  use_high_update_speed(s, i);
964  else
966  revert_cdlms(s, i, 0, subframe_len);
967  } else {
968  memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
969  }
970  }
971  if (s->do_mclms)
972  revert_mclms(s, subframe_len);
973  if (s->do_inter_ch_decorr)
974  revert_inter_ch_decorr(s, subframe_len);
975  if (s->do_ac_filter)
976  revert_acfilter(s, subframe_len);
977 
978  /* Dequantize */
979  if (s->quant_stepsize != 1)
980  for (i = 0; i < s->num_channels; i++)
981  for (j = 0; j < subframe_len; j++)
982  s->channel_residues[i][j] *= s->quant_stepsize;
983 
984  /* Write to proper output buffer depending on bit-depth */
985  for (i = 0; i < s->channels_for_cur_subframe; i++) {
986  int c = s->channel_indexes_for_cur_subframe[i];
987  int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
988 
989  for (j = 0; j < subframe_len; j++) {
990  if (s->bits_per_sample == 16) {
991  *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
992  } else {
993  *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
994  }
995  }
996  }
997 
998  /* handled one subframe */
999  for (i = 0; i < s->channels_for_cur_subframe; i++) {
1000  int c = s->channel_indexes_for_cur_subframe[i];
1001  if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1002  av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1003  return AVERROR_INVALIDDATA;
1004  }
1005  ++s->channel[c].cur_subframe;
1006  }
1007  return 0;
1008 }
1009 
1017 {
1018  GetBitContext* gb = &s->gb;
1019  int more_frames = 0, len = 0, i, ret;
1020 
1022  if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1023  /* return an error if no frame could be decoded at all */
1025  "not enough space for the output samples\n");
1026  s->packet_loss = 1;
1027  return ret;
1028  }
1029  for (i = 0; i < s->num_channels; i++) {
1030  s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1031  s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1032  }
1033 
1034  /* get frame length */
1035  if (s->len_prefix)
1036  len = get_bits(gb, s->log2_frame_size);
1037 
1038  /* decode tile information */
1039  if (decode_tilehdr(s)) {
1040  s->packet_loss = 1;
1041  return 0;
1042  }
1043 
1044  /* read drc info */
1046  s->drc_gain = get_bits(gb, 8);
1047 
1048  /* no idea what these are for, might be the number of samples
1049  that need to be skipped at the beginning or end of a stream */
1050  if (get_bits1(gb)) {
1051  int av_unused skip;
1052 
1053  /* usually true for the first frame */
1054  if (get_bits1(gb)) {
1055  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1056  ff_dlog(s->avctx, "start skip: %i\n", skip);
1057  }
1058 
1059  /* sometimes true for the last frame */
1060  if (get_bits1(gb)) {
1061  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1062  ff_dlog(s->avctx, "end skip: %i\n", skip);
1063  }
1064 
1065  }
1066 
1067  /* reset subframe states */
1068  s->parsed_all_subframes = 0;
1069  for (i = 0; i < s->num_channels; i++) {
1070  s->channel[i].decoded_samples = 0;
1071  s->channel[i].cur_subframe = 0;
1072  }
1073 
1074  /* decode all subframes */
1075  while (!s->parsed_all_subframes) {
1076  if (decode_subframe(s) < 0) {
1077  s->packet_loss = 1;
1078  return 0;
1079  }
1080  }
1081 
1082  ff_dlog(s->avctx, "Frame done\n");
1083 
1084  if (s->skip_frame)
1085  s->skip_frame = 0;
1086 
1087  if (s->len_prefix) {
1088  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1089  /* FIXME: not sure if this is always an error */
1091  "frame[%"PRIu32"] would have to skip %i bits\n",
1092  s->frame_num,
1093  len - (get_bits_count(gb) - s->frame_offset) - 1);
1094  s->packet_loss = 1;
1095  return 0;
1096  }
1097 
1098  /* skip the rest of the frame data */
1099  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1100  }
1101 
1102  /* decode trailer bit */
1103  more_frames = get_bits1(gb);
1104  ++s->frame_num;
1105  return more_frames;
1106 }
1107 
1115 {
1116  return s->buf_bit_size - get_bits_count(gb);
1117 }
1118 
1126 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1127  int append)
1128 {
1129  int buflen;
1131 
1132  /* when the frame data does not need to be concatenated, the input buffer
1133  is reset and additional bits from the previous frame are copied
1134  and skipped later so that a fast byte copy is possible */
1135 
1136  if (!append) {
1137  s->frame_offset = get_bits_count(gb) & 7;
1138  s->num_saved_bits = s->frame_offset;
1140  }
1141 
1142  buflen = (s->num_saved_bits + len + 8) >> 3;
1143 
1144  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1145  avpriv_request_sample(s->avctx, "Too small input buffer");
1146  s->packet_loss = 1;
1147  return;
1148  }
1149 
1150  s->num_saved_bits += len;
1151  if (!append) {
1152  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1153  s->num_saved_bits);
1154  } else {
1155  int align = 8 - (get_bits_count(gb) & 7);
1156  align = FFMIN(align, len);
1157  put_bits(&s->pb, align, get_bits(gb, align));
1158  len -= align;
1159  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1160  }
1161  skip_bits_long(gb, len);
1162 
1163  tmp = s->pb;
1164  flush_put_bits(&tmp);
1165 
1167  skip_bits(&s->gb, s->frame_offset);
1168 }
1169 
1170 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1171  AVPacket* avpkt)
1172 {
1173  WmallDecodeCtx *s = avctx->priv_data;
1174  GetBitContext* gb = &s->pgb;
1175  const uint8_t* buf = avpkt->data;
1176  int buf_size = avpkt->size;
1177  int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1178 
1179  s->frame->nb_samples = 0;
1180 
1181  if (s->packet_done || s->packet_loss) {
1182  s->packet_done = 0;
1183 
1184  /* sanity check for the buffer length */
1185  if (buf_size < avctx->block_align)
1186  return 0;
1187 
1188  s->next_packet_start = buf_size - avctx->block_align;
1189  buf_size = avctx->block_align;
1190  s->buf_bit_size = buf_size << 3;
1191 
1192  /* parse packet header */
1193  init_get_bits(gb, buf, s->buf_bit_size);
1194  packet_sequence_number = get_bits(gb, 4);
1195  skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently unused
1196  spliced_packet = get_bits1(gb);
1197  if (spliced_packet)
1198  avpriv_request_sample(avctx, "Bitstream splicing");
1199 
1200  /* get number of bits that need to be added to the previous frame */
1201  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1202 
1203  /* check for packet loss */
1204  if (!s->packet_loss &&
1205  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1206  s->packet_loss = 1;
1207  av_log(avctx, AV_LOG_ERROR,
1208  "Packet loss detected! seq %"PRIx8" vs %x\n",
1209  s->packet_sequence_number, packet_sequence_number);
1210  }
1211  s->packet_sequence_number = packet_sequence_number;
1212 
1213  if (num_bits_prev_frame > 0) {
1214  int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1215  if (num_bits_prev_frame >= remaining_packet_bits) {
1216  num_bits_prev_frame = remaining_packet_bits;
1217  s->packet_done = 1;
1218  }
1219 
1220  /* Append the previous frame data to the remaining data from the
1221  * previous packet to create a full frame. */
1222  save_bits(s, gb, num_bits_prev_frame, 1);
1223 
1224  /* decode the cross packet frame if it is valid */
1225  if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1226  decode_frame(s);
1227  } else if (s->num_saved_bits - s->frame_offset) {
1228  ff_dlog(avctx, "ignoring %x previously saved bits\n",
1229  s->num_saved_bits - s->frame_offset);
1230  }
1231 
1232  if (s->packet_loss) {
1233  /* Reset number of saved bits so that the decoder does not start
1234  * to decode incomplete frames in the s->len_prefix == 0 case. */
1235  s->num_saved_bits = 0;
1236  s->packet_loss = 0;
1238  }
1239 
1240  } else {
1241  int frame_size;
1242 
1243  s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1244  init_get_bits(gb, avpkt->data, s->buf_bit_size);
1245  skip_bits(gb, s->packet_offset);
1246 
1247  if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1248  (frame_size = show_bits(gb, s->log2_frame_size)) &&
1249  frame_size <= remaining_bits(s, gb)) {
1250  save_bits(s, gb, frame_size, 0);
1251  s->packet_done = !decode_frame(s);
1252  } else if (!s->len_prefix
1253  && s->num_saved_bits > get_bits_count(&s->gb)) {
1254  /* when the frames do not have a length prefix, we don't know the
1255  * compressed length of the individual frames however, we know what
1256  * part of a new packet belongs to the previous frame therefore we
1257  * save the incoming packet first, then we append the "previous
1258  * frame" data from the next packet so that we get a buffer that
1259  * only contains full frames */
1260  s->packet_done = !decode_frame(s);
1261  } else {
1262  s->packet_done = 1;
1263  }
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
int16_t lms_prevvalues[MAX_ORDER *2]
uint8_t subframe_len_bits
number of bits used for the subframe length
uint8_t bits_per_sample
integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
struct WmallDecodeCtx::@111 cdlms[2][9]
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
uint8_t max_subframe_len_bit
flag indicating that the subframe is of maximum size when the first subframe length bit is 1 ...
uint8_t max_num_subframes
uint16_t subframe_offsets[MAX_SUBFRAMES]
subframe positions in the current frame
int16_t mclms_updates[WMALL_MAX_CHANNELS *2 *32]
int32_t * samples_32[WMALL_MAX_CHANNELS]
current sample buffer pointer (24-bit)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
int channel_coeffs[2][WMALL_BLOCK_MAX_SIZE]
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:187
static const uint8_t frame_size[4]
Definition: g723_1.h:219
uint8_t drc_gain
gain for the DRC tool
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
void avpriv_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition: bitstream.c:62
const uint8_t * buffer
Definition: get_bits.h:55
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:319
static void decode_mclms(WmallDecodeCtx *s)
int lpc_coefs[2][40]
static int decode_subframe_length(WmallDecodeCtx *s, int offset)
Decode the subframe length.
static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
AVCodec.
Definition: avcodec.h:3120
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2189
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:214
#define MAX_ORDER
int8_t channels_for_cur_subframe
number of channels that contain the subframe
Macro definitions for various function/variable attributes.
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:863
GetBitContext pgb
bitstream reader context for the packet
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int quant_step
quantization step for the current subframe
uint8_t bits
Definition: crc.c:252
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
int8_t num_channels
number of channels in the stream (same as AVCodecContext.num_channels)
static void reset_codec(WmallDecodeCtx *s)
Reset filter parameters and transient area at new seekable tile.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
int frame_offset
frame offset in the bit reservoir
const char data[16]
Definition: mxf.c:70
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
uint16_t min_samples_per_subframe
uint8_t * data
Definition: avcodec.h:1346
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
bitstream reader API header.
AVFrame * frame
frame-specific decoder context for a single channel
int8_t lfe_channel
lfe channel index
uint8_t do_arith_coding
int buf_bit_size
buffer size in bits
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
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:124
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
int16_t coefs[MAX_ORDER]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
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:148
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:145
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:3127
static int decode_subframe(WmallDecodeCtx *s)
static int decode_cdlms(WmallDecodeCtx *s)
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
uint8_t transmit_coefs
static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
#define WMALL_BLOCK_MAX_SIZE
maximum block size
AVCodecContext * avctx
#define FFMAX(a, b)
Definition: common.h:64
int8_t parsed_all_subframes
all subframes decoded?
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
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:66
uint16_t log2_frame_size
signed 32 bits, planar
Definition: samplefmt.h:70
uint32_t decode_flags
used compression features
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
int32_t
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:250
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
static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
int acfilter_prevvalues[2][16]
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]
uint16_t samples_per_frame
number of samples to output
if(ac->has_optimized_func)
static const float pred[4]
Definition: siprdata.h:259
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int next_packet_start
start offset of the next WMA packet in the demuxer packet
Libavcodec external API header.
#define ff_dlog(ctx,...)
Definition: internal.h:60
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
int sample_rate
samples per second
Definition: avcodec.h:2152
#define WMALL_MAX_CHANNELS
current decoder limitations
main external API structure.
Definition: avcodec.h:1409
int16_t * samples_16[WMALL_MAX_CHANNELS]
current sample buffer 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:589
int extradata_size
Definition: avcodec.h:1524
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time...
Definition: avcodec.h:880
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
int16_t mclms_prevvalues[WMALL_MAX_CHANNELS *2 *32]
int64_t acfilter_coeffs[16]
int is_channel_coded[2]
uint8_t num_subframes
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
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:302
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
av_cold int ff_wma_get_frame_len_bits(int sample_rate, int version, unsigned int decode_flags)
Get the samples per frame for this stream.
Definition: wma_common.c:32
uint8_t do_inter_ch_decorr
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.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
int dynamic_range_compression
frame contains DRC data
int transient_counter
number of transient samples from the beginning of the transient zone
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.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:1451
int len
int channels
number of audio channels
Definition: avcodec.h:2153
#define av_log2
Definition: intmath.h:85
static uint8_t tmp[8]
Definition: des.c:38
AVCodec ff_wmalossless_decoder
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int16_t lms_updates[MAX_ORDER *2]
static void flush(AVCodecContext *avctx)
int8_t acfilter_scaling
int len_prefix
frame is prefixed with its length
static int decode_frame(WmallDecodeCtx *s)
Decode one WMA frame.
signed 16 bits, planar
Definition: samplefmt.h:69
int channel_residues[2][WMALL_BLOCK_MAX_SIZE]
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
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:1323
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
for(j=16;j >0;--j)
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:242
#define av_unused
Definition: attributes.h:86
static void lms_update(WmallDecodeCtx *s, int ich, int ilms, int input, int residue)
static void revert_mclms(WmallDecodeCtx *s, int tile_size)
bitstream writer API