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 "libavutil/attributes.h"
26 #include "libavutil/avassert.h"
27 
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "get_bits.h"
31 #include "put_bits.h"
32 #include "wma.h"
33 #include "wma_common.h"
34 
36 #define WMALL_MAX_CHANNELS 8
37 #define MAX_SUBFRAMES 32
38 #define MAX_BANDS 29
39 #define MAX_FRAMESIZE 32768
40 #define MAX_ORDER 256
41 
42 #define WMALL_BLOCK_MIN_BITS 6
43 #define WMALL_BLOCK_MAX_BITS 14
44 #define WMALL_BLOCK_MAX_SIZE (1 << WMALL_BLOCK_MAX_BITS)
45 #define WMALL_BLOCK_SIZES (WMALL_BLOCK_MAX_BITS - WMALL_BLOCK_MIN_BITS + 1)
46 
47 
51 typedef struct {
52  int16_t prev_block_len;
55  uint16_t subframe_len[MAX_SUBFRAMES];
56  uint16_t subframe_offsets[MAX_SUBFRAMES];
58  uint16_t decoded_samples;
59  int quant_step;
62 
66 typedef struct WmallDecodeCtx {
67  /* generic decoder variables */
72 
73  /* frame size dependent frame information (set during initialization) */
74  uint32_t decode_flags;
75  int len_prefix;
78  uint16_t samples_per_frame;
79  uint16_t log2_frame_size;
80  int8_t num_channels;
81  int8_t lfe_channel;
86 
87  /* packet decode state */
97 
98  /* frame decode state */
99  uint32_t frame_num;
105  int8_t skip_frame;
107 
108  /* subframe/block decode state */
109  int16_t subframe_len;
112 
114 
115  // WMA Lossless-specific
116 
122 
125  int64_t acfilter_coeffs[16];
127 
128  int8_t mclms_order;
130  int16_t mclms_coeffs[128];
131  int16_t mclms_coeffs_cur[4];
135 
138 
139  struct {
140  int order;
141  int scaling;
142  int coefsend;
143  int bitsend;
144  int16_t coefs[MAX_ORDER];
145  int16_t lms_prevvalues[MAX_ORDER * 2];
146  int16_t lms_updates[MAX_ORDER * 2];
147  int recent;
148  } cdlms[2][9];
149 
150  int cdlms_ttl[2];
151 
152  int bV3RTM;
153 
155  int update_speed[2];
156 
157  int transient[2];
160 
161  int ave_sum[2];
162 
164 
165  int lpc_coefs[2][40];
169 
172 
173 
175 {
176  WmallDecodeCtx *s = avctx->priv_data;
177  uint8_t *edata_ptr = avctx->extradata;
178  unsigned int channel_mask;
179  int i, log2_max_num_subframes;
180 
181  s->avctx = avctx;
183 
184  if (avctx->extradata_size >= 18) {
185  s->decode_flags = AV_RL16(edata_ptr + 14);
186  channel_mask = AV_RL32(edata_ptr + 2);
187  s->bits_per_sample = AV_RL16(edata_ptr);
188  if (s->bits_per_sample == 16)
190  else if (s->bits_per_sample == 24) {
192  avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
193  return AVERROR_PATCHWELCOME;
194  } else {
195  av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
196  s->bits_per_sample);
197  return AVERROR_INVALIDDATA;
198  }
199  /* dump the extradata */
200  for (i = 0; i < avctx->extradata_size; i++)
201  av_dlog(avctx, "[%x] ", avctx->extradata[i]);
202  av_dlog(avctx, "\n");
203 
204  } else {
205  avpriv_request_sample(avctx, "Unsupported extradata size");
206  return AVERROR_PATCHWELCOME;
207  }
208 
209  /* generic init */
210  s->log2_frame_size = av_log2(avctx->block_align) + 4;
211 
212  /* frame info */
213  s->skip_frame = 1; /* skip first frame */
214  s->packet_loss = 1;
215  s->len_prefix = s->decode_flags & 0x40;
216 
217  /* get frame len */
219  3, s->decode_flags);
221 
222  /* init previous block len */
223  for (i = 0; i < avctx->channels; i++)
225 
226  /* subframe info */
227  log2_max_num_subframes = (s->decode_flags & 0x38) >> 3;
228  s->max_num_subframes = 1 << log2_max_num_subframes;
229  s->max_subframe_len_bit = 0;
230  s->subframe_len_bits = av_log2(log2_max_num_subframes) + 1;
231 
234  s->bV3RTM = s->decode_flags & 0x100;
235 
236  if (s->max_num_subframes > MAX_SUBFRAMES) {
237  av_log(avctx, AV_LOG_ERROR, "invalid number of subframes %i\n",
238  s->max_num_subframes);
239  return AVERROR_INVALIDDATA;
240  }
241 
242  s->num_channels = avctx->channels;
243 
244  /* extract lfe channel position */
245  s->lfe_channel = -1;
246 
247  if (channel_mask & 8) {
248  unsigned int mask;
249  for (mask = 1; mask < 16; mask <<= 1)
250  if (channel_mask & mask)
251  ++s->lfe_channel;
252  }
253 
254  if (s->num_channels < 0) {
255  av_log(avctx, AV_LOG_ERROR, "invalid number of channels %d\n",
256  s->num_channels);
257  return AVERROR_INVALIDDATA;
258  } else if (s->num_channels > WMALL_MAX_CHANNELS) {
259  avpriv_request_sample(avctx,
260  "More than %d channels", WMALL_MAX_CHANNELS);
261  return AVERROR_PATCHWELCOME;
262  }
263 
264  s->frame = av_frame_alloc();
265  if (!s->frame)
266  return AVERROR(ENOMEM);
267 
268  avctx->channel_layout = channel_mask;
269  return 0;
270 }
271 
278 static int decode_subframe_length(WmallDecodeCtx *s, int offset)
279 {
280  int frame_len_ratio, subframe_len, len;
281 
282  /* no need to read from the bitstream when only one length is possible */
283  if (offset == s->samples_per_frame - s->min_samples_per_subframe)
284  return s->min_samples_per_subframe;
285 
286  len = av_log2(s->max_num_subframes - 1) + 1;
287  frame_len_ratio = get_bits(&s->gb, len);
288  subframe_len = s->min_samples_per_subframe * (frame_len_ratio + 1);
289 
290  /* sanity check the length */
291  if (subframe_len < s->min_samples_per_subframe ||
292  subframe_len > s->samples_per_frame) {
293  av_log(s->avctx, AV_LOG_ERROR, "broken frame: subframe_len %i\n",
294  subframe_len);
295  return AVERROR_INVALIDDATA;
296  }
297  return subframe_len;
298 }
299 
321 {
322  uint16_t num_samples[WMALL_MAX_CHANNELS] = { 0 }; /* sum of samples for all currently known subframes of a channel */
323  uint8_t contains_subframe[WMALL_MAX_CHANNELS]; /* flag indicating if a channel contains the current subframe */
324  int channels_for_cur_subframe = s->num_channels; /* number of channels that contain the current subframe */
325  int fixed_channel_layout = 0; /* flag indicating that all channels use the same subfra2me offsets and sizes */
326  int min_channel_len = 0; /* smallest sum of samples (channels with this length will be processed first) */
327  int c, tile_aligned;
328 
329  /* reset tiling information */
330  for (c = 0; c < s->num_channels; c++)
331  s->channel[c].num_subframes = 0;
332 
333  tile_aligned = get_bits1(&s->gb);
334  if (s->max_num_subframes == 1 || tile_aligned)
335  fixed_channel_layout = 1;
336 
337  /* loop until the frame data is split between the subframes */
338  do {
339  int subframe_len, in_use = 0;
340 
341  /* check which channels contain the subframe */
342  for (c = 0; c < s->num_channels; c++) {
343  if (num_samples[c] == min_channel_len) {
344  if (fixed_channel_layout || channels_for_cur_subframe == 1 ||
345  (min_channel_len == s->samples_per_frame - s->min_samples_per_subframe)) {
346  contains_subframe[c] = in_use = 1;
347  } else {
348  if (get_bits1(&s->gb))
349  contains_subframe[c] = in_use = 1;
350  }
351  } else
352  contains_subframe[c] = 0;
353  }
354 
355  if (!in_use) {
357  "Found empty subframe\n");
358  return AVERROR_INVALIDDATA;
359  }
360 
361  /* get subframe length, subframe_len == 0 is not allowed */
362  if ((subframe_len = decode_subframe_length(s, min_channel_len)) <= 0)
363  return AVERROR_INVALIDDATA;
364  /* add subframes to the individual channels and find new min_channel_len */
365  min_channel_len += subframe_len;
366  for (c = 0; c < s->num_channels; c++) {
367  WmallChannelCtx *chan = &s->channel[c];
368 
369  if (contains_subframe[c]) {
370  if (chan->num_subframes >= MAX_SUBFRAMES) {
372  "broken frame: num subframes > 31\n");
373  return AVERROR_INVALIDDATA;
374  }
375  chan->subframe_len[chan->num_subframes] = subframe_len;
376  num_samples[c] += subframe_len;
377  ++chan->num_subframes;
378  if (num_samples[c] > s->samples_per_frame) {
379  av_log(s->avctx, AV_LOG_ERROR, "broken frame: "
380  "channel len(%d) > samples_per_frame(%d)\n",
381  num_samples[c], s->samples_per_frame);
382  return AVERROR_INVALIDDATA;
383  }
384  } else if (num_samples[c] <= min_channel_len) {
385  if (num_samples[c] < min_channel_len) {
386  channels_for_cur_subframe = 0;
387  min_channel_len = num_samples[c];
388  }
389  ++channels_for_cur_subframe;
390  }
391  }
392  } while (min_channel_len < s->samples_per_frame);
393 
394  for (c = 0; c < s->num_channels; c++) {
395  int i, offset = 0;
396  for (i = 0; i < s->channel[c].num_subframes; i++) {
397  s->channel[c].subframe_offsets[i] = offset;
398  offset += s->channel[c].subframe_len[i];
399  }
400  }
401 
402  return 0;
403 }
404 
406 {
407  int i;
408  s->acfilter_order = get_bits(&s->gb, 4) + 1;
409  s->acfilter_scaling = get_bits(&s->gb, 4);
410 
411  for (i = 0; i < s->acfilter_order; i++)
412  s->acfilter_coeffs[i] = (s->acfilter_scaling ?
413  get_bits(&s->gb, s->acfilter_scaling) : 0) + 1;
414 }
415 
417 {
418  s->mclms_order = (get_bits(&s->gb, 4) + 1) * 2;
419  s->mclms_scaling = get_bits(&s->gb, 4);
420  if (get_bits1(&s->gb)) {
421  int i, send_coef_bits;
422  int cbits = av_log2(s->mclms_scaling + 1);
423  if (1 << cbits < s->mclms_scaling + 1)
424  cbits++;
425 
426  send_coef_bits = (cbits ? get_bits(&s->gb, cbits) : 0) + 2;
427 
428  for (i = 0; i < s->mclms_order * s->num_channels * s->num_channels; i++)
429  s->mclms_coeffs[i] = get_bits(&s->gb, send_coef_bits);
430 
431  for (i = 0; i < s->num_channels; i++) {
432  int c;
433  for (c = 0; c < i; c++)
434  s->mclms_coeffs_cur[i * s->num_channels + c] = get_bits(&s->gb, send_coef_bits);
435  }
436  }
437 }
438 
440 {
441  int c, i;
442  int cdlms_send_coef = get_bits1(&s->gb);
443 
444  for (c = 0; c < s->num_channels; c++) {
445  s->cdlms_ttl[c] = get_bits(&s->gb, 3) + 1;
446  for (i = 0; i < s->cdlms_ttl[c]; i++) {
447  s->cdlms[c][i].order = (get_bits(&s->gb, 7) + 1) * 8;
448  if (s->cdlms[c][i].order > MAX_ORDER) {
450  "Order[%d][%d] %d > max (%d), not supported\n",
451  c, i, s->cdlms[c][i].order, MAX_ORDER);
452  s->cdlms[0][0].order = 0;
453  return AVERROR_INVALIDDATA;
454  }
455  }
456 
457  for (i = 0; i < s->cdlms_ttl[c]; i++)
458  s->cdlms[c][i].scaling = get_bits(&s->gb, 4);
459 
460  if (cdlms_send_coef) {
461  for (i = 0; i < s->cdlms_ttl[c]; i++) {
462  int cbits, shift_l, shift_r, j;
463  cbits = av_log2(s->cdlms[c][i].order);
464  if ((1 << cbits) < s->cdlms[c][i].order)
465  cbits++;
466  s->cdlms[c][i].coefsend = get_bits(&s->gb, cbits) + 1;
467 
468  cbits = av_log2(s->cdlms[c][i].scaling + 1);
469  if ((1 << cbits) < s->cdlms[c][i].scaling + 1)
470  cbits++;
471 
472  s->cdlms[c][i].bitsend = get_bits(&s->gb, cbits) + 2;
473  shift_l = 32 - s->cdlms[c][i].bitsend;
474  shift_r = 32 - s->cdlms[c][i].scaling - 2;
475  for (j = 0; j < s->cdlms[c][i].coefsend; j++)
476  s->cdlms[c][i].coefs[j] =
477  (get_bits(&s->gb, s->cdlms[c][i].bitsend) << shift_l) >> shift_r;
478  }
479  }
480  }
481 
482  return 0;
483 }
484 
485 static int decode_channel_residues(WmallDecodeCtx *s, int ch, int tile_size)
486 {
487  int i = 0;
488  unsigned int ave_mean;
489  s->transient[ch] = get_bits1(&s->gb);
490  if (s->transient[ch]) {
491  s->transient_pos[ch] = get_bits(&s->gb, av_log2(tile_size));
492  if (s->transient_pos[ch])
493  s->transient[ch] = 0;
494  s->channel[ch].transient_counter =
496  } else if (s->channel[ch].transient_counter)
497  s->transient[ch] = 1;
498 
499  if (s->seekable_tile) {
500  ave_mean = get_bits(&s->gb, s->bits_per_sample);
501  s->ave_sum[ch] = ave_mean << (s->movave_scaling + 1);
502  }
503 
504  if (s->seekable_tile) {
505  if (s->do_inter_ch_decorr)
506  s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample + 1);
507  else
508  s->channel_residues[ch][0] = get_sbits(&s->gb, s->bits_per_sample);
509  i++;
510  }
511  for (; i < tile_size; i++) {
512  int quo = 0, rem, rem_bits, residue;
513  while(get_bits1(&s->gb)) {
514  quo++;
515  if (get_bits_left(&s->gb) <= 0)
516  return -1;
517  }
518  if (quo >= 32)
519  quo += get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1);
520 
521  ave_mean = (s->ave_sum[ch] + (1 << s->movave_scaling)) >> (s->movave_scaling + 1);
522  if (ave_mean <= 1)
523  residue = quo;
524  else {
525  rem_bits = av_ceil_log2(ave_mean);
526  rem = rem_bits ? get_bits_long(&s->gb, rem_bits) : 0;
527  residue = (quo << rem_bits) + rem;
528  }
529 
530  s->ave_sum[ch] = residue + s->ave_sum[ch] -
531  (s->ave_sum[ch] >> s->movave_scaling);
532 
533  if (residue & 1)
534  residue = -(residue >> 1) - 1;
535  else
536  residue = residue >> 1;
537  s->channel_residues[ch][i] = residue;
538  }
539 
540  return 0;
541 
542 }
543 
544 static void decode_lpc(WmallDecodeCtx *s)
545 {
546  int ch, i, cbits;
547  s->lpc_order = get_bits(&s->gb, 5) + 1;
548  s->lpc_scaling = get_bits(&s->gb, 4);
549  s->lpc_intbits = get_bits(&s->gb, 3) + 1;
550  cbits = s->lpc_scaling + s->lpc_intbits;
551  for (ch = 0; ch < s->num_channels; ch++)
552  for (i = 0; i < s->lpc_order; i++)
553  s->lpc_coefs[ch][i] = get_sbits(&s->gb, cbits);
554 }
555 
557 {
558  int ich, ilms;
559 
560  memset(s->acfilter_coeffs, 0, sizeof(s->acfilter_coeffs));
561  memset(s->acfilter_prevvalues, 0, sizeof(s->acfilter_prevvalues));
562  memset(s->lpc_coefs, 0, sizeof(s->lpc_coefs));
563 
564  memset(s->mclms_coeffs, 0, sizeof(s->mclms_coeffs));
565  memset(s->mclms_coeffs_cur, 0, sizeof(s->mclms_coeffs_cur));
566  memset(s->mclms_prevvalues, 0, sizeof(s->mclms_prevvalues));
567  memset(s->mclms_updates, 0, sizeof(s->mclms_updates));
568 
569  for (ich = 0; ich < s->num_channels; ich++) {
570  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++) {
571  memset(s->cdlms[ich][ilms].coefs, 0,
572  sizeof(s->cdlms[ich][ilms].coefs));
573  memset(s->cdlms[ich][ilms].lms_prevvalues, 0,
574  sizeof(s->cdlms[ich][ilms].lms_prevvalues));
575  memset(s->cdlms[ich][ilms].lms_updates, 0,
576  sizeof(s->cdlms[ich][ilms].lms_updates));
577  }
578  s->ave_sum[ich] = 0;
579  }
580 }
581 
586 {
587  int ich, ilms;
589  for (ich = 0; ich < s->num_channels; ich++) {
590  for (ilms = 0; ilms < s->cdlms_ttl[ich]; ilms++)
591  s->cdlms[ich][ilms].recent = s->cdlms[ich][ilms].order;
592  /* first sample of a seekable subframe is considered as the starting of
593  a transient area which is samples_per_frame samples long */
595  s->transient[ich] = 1;
596  s->transient_pos[ich] = 0;
597  }
598 }
599 
600 static void mclms_update(WmallDecodeCtx *s, int icoef, int *pred)
601 {
602  int i, j, ich, pred_error;
603  int order = s->mclms_order;
604  int num_channels = s->num_channels;
605  int range = 1 << (s->bits_per_sample - 1);
606 
607  for (ich = 0; ich < num_channels; ich++) {
608  pred_error = s->channel_residues[ich][icoef] - pred[ich];
609  if (pred_error > 0) {
610  for (i = 0; i < order * num_channels; i++)
611  s->mclms_coeffs[i + ich * order * num_channels] +=
612  s->mclms_updates[s->mclms_recent + i];
613  for (j = 0; j < ich; j++) {
614  if (s->channel_residues[j][icoef] > 0)
615  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
616  else if (s->channel_residues[j][icoef] < 0)
617  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
618  }
619  } else if (pred_error < 0) {
620  for (i = 0; i < order * num_channels; i++)
621  s->mclms_coeffs[i + ich * order * num_channels] -=
622  s->mclms_updates[s->mclms_recent + i];
623  for (j = 0; j < ich; j++) {
624  if (s->channel_residues[j][icoef] > 0)
625  s->mclms_coeffs_cur[ich * num_channels + j] -= 1;
626  else if (s->channel_residues[j][icoef] < 0)
627  s->mclms_coeffs_cur[ich * num_channels + j] += 1;
628  }
629  }
630  }
631 
632  for (ich = num_channels - 1; ich >= 0; ich--) {
633  s->mclms_recent--;
634  s->mclms_prevvalues[s->mclms_recent] = s->channel_residues[ich][icoef];
635  if (s->channel_residues[ich][icoef] > range - 1)
636  s->mclms_prevvalues[s->mclms_recent] = range - 1;
637  else if (s->channel_residues[ich][icoef] < -range)
638  s->mclms_prevvalues[s->mclms_recent] = -range;
639 
640  s->mclms_updates[s->mclms_recent] = 0;
641  if (s->channel_residues[ich][icoef] > 0)
642  s->mclms_updates[s->mclms_recent] = 1;
643  else if (s->channel_residues[ich][icoef] < 0)
644  s->mclms_updates[s->mclms_recent] = -1;
645  }
646 
647  if (s->mclms_recent == 0) {
648  memcpy(&s->mclms_prevvalues[order * num_channels],
649  s->mclms_prevvalues,
650  2 * order * num_channels);
651  memcpy(&s->mclms_updates[order * num_channels],
652  s->mclms_updates,
653  2 * order * num_channels);
654  s->mclms_recent = num_channels * order;
655  }
656 }
657 
658 static void mclms_predict(WmallDecodeCtx *s, int icoef, int *pred)
659 {
660  int ich, i;
661  int order = s->mclms_order;
662  int num_channels = s->num_channels;
663 
664  for (ich = 0; ich < num_channels; ich++) {
665  pred[ich] = 0;
666  if (!s->is_channel_coded[ich])
667  continue;
668  for (i = 0; i < order * num_channels; i++)
669  pred[ich] += s->mclms_prevvalues[i + s->mclms_recent] *
670  s->mclms_coeffs[i + order * num_channels * ich];
671  for (i = 0; i < ich; i++)
672  pred[ich] += s->channel_residues[i][icoef] *
673  s->mclms_coeffs_cur[i + num_channels * ich];
674  pred[ich] += 1 << s->mclms_scaling - 1;
675  pred[ich] >>= s->mclms_scaling;
676  s->channel_residues[ich][icoef] += pred[ich];
677  }
678 }
679 
680 static void revert_mclms(WmallDecodeCtx *s, int tile_size)
681 {
682  int icoef, pred[WMALL_MAX_CHANNELS] = { 0 };
683  for (icoef = 0; icoef < tile_size; icoef++) {
684  mclms_predict(s, icoef, pred);
685  mclms_update(s, icoef, pred);
686  }
687 }
688 
689 static int lms_predict(WmallDecodeCtx *s, int ich, int ilms)
690 {
691  int pred = 0, icoef;
692  int recent = s->cdlms[ich][ilms].recent;
693 
694  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
695  pred += s->cdlms[ich][ilms].coefs[icoef] *
696  s->cdlms[ich][ilms].lms_prevvalues[icoef + recent];
697 
698  return pred;
699 }
700 
701 static void lms_update(WmallDecodeCtx *s, int ich, int ilms,
702  int input, int residue)
703 {
704  int icoef;
705  int recent = s->cdlms[ich][ilms].recent;
706  int range = 1 << s->bits_per_sample - 1;
707 
708  if (residue < 0) {
709  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
710  s->cdlms[ich][ilms].coefs[icoef] -=
711  s->cdlms[ich][ilms].lms_updates[icoef + recent];
712  } else if (residue > 0) {
713  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
714  s->cdlms[ich][ilms].coefs[icoef] +=
715  s->cdlms[ich][ilms].lms_updates[icoef + recent];
716  }
717 
718  if (recent)
719  recent--;
720  else {
721  memcpy(&s->cdlms[ich][ilms].lms_prevvalues[s->cdlms[ich][ilms].order],
722  s->cdlms[ich][ilms].lms_prevvalues,
723  2 * s->cdlms[ich][ilms].order);
724  memcpy(&s->cdlms[ich][ilms].lms_updates[s->cdlms[ich][ilms].order],
725  s->cdlms[ich][ilms].lms_updates,
726  2 * s->cdlms[ich][ilms].order);
727  recent = s->cdlms[ich][ilms].order - 1;
728  }
729 
730  s->cdlms[ich][ilms].lms_prevvalues[recent] = av_clip(input, -range, range - 1);
731  if (!input)
732  s->cdlms[ich][ilms].lms_updates[recent] = 0;
733  else if (input < 0)
734  s->cdlms[ich][ilms].lms_updates[recent] = -s->update_speed[ich];
735  else
736  s->cdlms[ich][ilms].lms_updates[recent] = s->update_speed[ich];
737 
738  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 4)] >>= 2;
739  s->cdlms[ich][ilms].lms_updates[recent + (s->cdlms[ich][ilms].order >> 3)] >>= 1;
740  s->cdlms[ich][ilms].recent = recent;
741 }
742 
743 static void use_high_update_speed(WmallDecodeCtx *s, int ich)
744 {
745  int ilms, recent, icoef;
746  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
747  recent = s->cdlms[ich][ilms].recent;
748  if (s->update_speed[ich] == 16)
749  continue;
750  if (s->bV3RTM) {
751  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
752  s->cdlms[ich][ilms].lms_updates[icoef + recent] *= 2;
753  } else {
754  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
755  s->cdlms[ich][ilms].lms_updates[icoef] *= 2;
756  }
757  }
758  s->update_speed[ich] = 16;
759 }
760 
761 static void use_normal_update_speed(WmallDecodeCtx *s, int ich)
762 {
763  int ilms, recent, icoef;
764  for (ilms = s->cdlms_ttl[ich] - 1; ilms >= 0; ilms--) {
765  recent = s->cdlms[ich][ilms].recent;
766  if (s->update_speed[ich] == 8)
767  continue;
768  if (s->bV3RTM)
769  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
770  s->cdlms[ich][ilms].lms_updates[icoef + recent] /= 2;
771  else
772  for (icoef = 0; icoef < s->cdlms[ich][ilms].order; icoef++)
773  s->cdlms[ich][ilms].lms_updates[icoef] /= 2;
774  }
775  s->update_speed[ich] = 8;
776 }
777 
778 static void revert_cdlms(WmallDecodeCtx *s, int ch,
779  int coef_begin, int coef_end)
780 {
781  int icoef, pred, ilms, num_lms, residue, input;
782 
783  num_lms = s->cdlms_ttl[ch];
784  for (ilms = num_lms - 1; ilms >= 0; ilms--) {
785  for (icoef = coef_begin; icoef < coef_end; icoef++) {
786  pred = 1 << (s->cdlms[ch][ilms].scaling - 1);
787  residue = s->channel_residues[ch][icoef];
788  pred += lms_predict(s, ch, ilms);
789  input = residue + (pred >> s->cdlms[ch][ilms].scaling);
790  lms_update(s, ch, ilms, input, residue);
791  s->channel_residues[ch][icoef] = input;
792  }
793  }
794 }
795 
796 static void revert_inter_ch_decorr(WmallDecodeCtx *s, int tile_size)
797 {
798  if (s->num_channels != 2)
799  return;
800  else if (s->is_channel_coded[0] || s->is_channel_coded[1]) {
801  int icoef;
802  for (icoef = 0; icoef < tile_size; icoef++) {
803  s->channel_residues[0][icoef] -= s->channel_residues[1][icoef] >> 1;
804  s->channel_residues[1][icoef] += s->channel_residues[0][icoef];
805  }
806  }
807 }
808 
809 static void revert_acfilter(WmallDecodeCtx *s, int tile_size)
810 {
811  int ich, pred, i, j;
812  int64_t *filter_coeffs = s->acfilter_coeffs;
813  int scaling = s->acfilter_scaling;
814  int order = s->acfilter_order;
815 
816  for (ich = 0; ich < s->num_channels; ich++) {
817  int *prevvalues = s->acfilter_prevvalues[ich];
818  for (i = 0; i < order; i++) {
819  pred = 0;
820  for (j = 0; j < order; j++) {
821  if (i <= j)
822  pred += filter_coeffs[j] * prevvalues[j - i];
823  else
824  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
825  }
826  pred >>= scaling;
827  s->channel_residues[ich][i] += pred;
828  }
829  for (i = order; i < tile_size; i++) {
830  pred = 0;
831  for (j = 0; j < order; j++)
832  pred += s->channel_residues[ich][i - j - 1] * filter_coeffs[j];
833  pred >>= scaling;
834  s->channel_residues[ich][i] += pred;
835  }
836  for (j = 0; j < order; j++)
837  prevvalues[j] = s->channel_residues[ich][tile_size - j - 1];
838  }
839 }
840 
842 {
843  int offset = s->samples_per_frame;
844  int subframe_len = s->samples_per_frame;
845  int total_samples = s->samples_per_frame * s->num_channels;
846  int i, j, rawpcm_tile, padding_zeroes, res;
847 
849 
850  /* reset channel context and find the next block offset and size
851  == the next block of the channel with the smallest number of
852  decoded samples */
853  for (i = 0; i < s->num_channels; i++) {
854  if (offset > s->channel[i].decoded_samples) {
855  offset = s->channel[i].decoded_samples;
856  subframe_len =
858  }
859  }
860 
861  /* get a list of all channels that contain the estimated block */
863  for (i = 0; i < s->num_channels; i++) {
864  const int cur_subframe = s->channel[i].cur_subframe;
865  /* subtract already processed samples */
866  total_samples -= s->channel[i].decoded_samples;
867 
868  /* and count if there are multiple subframes that match our profile */
869  if (offset == s->channel[i].decoded_samples &&
870  subframe_len == s->channel[i].subframe_len[cur_subframe]) {
871  total_samples -= s->channel[i].subframe_len[cur_subframe];
872  s->channel[i].decoded_samples +=
873  s->channel[i].subframe_len[cur_subframe];
876  }
877  }
878 
879  /* check if the frame will be complete after processing the
880  estimated block */
881  if (!total_samples)
882  s->parsed_all_subframes = 1;
883 
884 
885  s->seekable_tile = get_bits1(&s->gb);
886  if (s->seekable_tile) {
888 
889  s->do_arith_coding = get_bits1(&s->gb);
890  if (s->do_arith_coding) {
891  avpriv_request_sample(s->avctx, "Arithmetic coding");
892  return AVERROR_PATCHWELCOME;
893  }
894  s->do_ac_filter = get_bits1(&s->gb);
895  s->do_inter_ch_decorr = get_bits1(&s->gb);
896  s->do_mclms = get_bits1(&s->gb);
897 
898  if (s->do_ac_filter)
899  decode_ac_filter(s);
900 
901  if (s->do_mclms)
902  decode_mclms(s);
903 
904  if ((res = decode_cdlms(s)) < 0)
905  return res;
906  s->movave_scaling = get_bits(&s->gb, 3);
907  s->quant_stepsize = get_bits(&s->gb, 8) + 1;
908 
909  reset_codec(s);
910  } else if (!s->cdlms[0][0].order) {
912  "Waiting for seekable tile\n");
913  av_frame_unref(s->frame);
914  return -1;
915  }
916 
917  rawpcm_tile = get_bits1(&s->gb);
918 
919  for (i = 0; i < s->num_channels; i++)
920  s->is_channel_coded[i] = 1;
921 
922  if (!rawpcm_tile) {
923  for (i = 0; i < s->num_channels; i++)
924  s->is_channel_coded[i] = get_bits1(&s->gb);
925 
926  if (s->bV3RTM) {
927  // LPC
928  s->do_lpc = get_bits1(&s->gb);
929  if (s->do_lpc) {
930  decode_lpc(s);
931  avpriv_request_sample(s->avctx, "Expect wrong output since "
932  "inverse LPC filter");
933  }
934  } else
935  s->do_lpc = 0;
936  }
937 
938 
939  if (get_bits1(&s->gb))
940  padding_zeroes = get_bits(&s->gb, 5);
941  else
942  padding_zeroes = 0;
943 
944  if (rawpcm_tile) {
945  int bits = s->bits_per_sample - padding_zeroes;
946  if (bits <= 0) {
948  "Invalid number of padding bits in raw PCM tile\n");
949  return AVERROR_INVALIDDATA;
950  }
951  av_dlog(s->avctx, "RAWPCM %d bits per sample. "
952  "total %d bits, remain=%d\n", bits,
953  bits * s->num_channels * subframe_len, get_bits_count(&s->gb));
954  for (i = 0; i < s->num_channels; i++)
955  for (j = 0; j < subframe_len; j++)
956  s->channel_coeffs[i][j] = get_sbits(&s->gb, bits);
957  } else {
958  for (i = 0; i < s->num_channels; i++)
959  if (s->is_channel_coded[i]) {
960  decode_channel_residues(s, i, subframe_len);
961  if (s->seekable_tile)
962  use_high_update_speed(s, i);
963  else
965  revert_cdlms(s, i, 0, subframe_len);
966  } else {
967  memset(s->channel_residues[i], 0, sizeof(**s->channel_residues) * subframe_len);
968  }
969  }
970  if (s->do_mclms)
971  revert_mclms(s, subframe_len);
972  if (s->do_inter_ch_decorr)
973  revert_inter_ch_decorr(s, subframe_len);
974  if (s->do_ac_filter)
975  revert_acfilter(s, subframe_len);
976 
977  /* Dequantize */
978  if (s->quant_stepsize != 1)
979  for (i = 0; i < s->num_channels; i++)
980  for (j = 0; j < subframe_len; j++)
981  s->channel_residues[i][j] *= s->quant_stepsize;
982 
983  /* Write to proper output buffer depending on bit-depth */
984  for (i = 0; i < s->channels_for_cur_subframe; i++) {
985  int c = s->channel_indexes_for_cur_subframe[i];
986  int subframe_len = s->channel[c].subframe_len[s->channel[c].cur_subframe];
987 
988  for (j = 0; j < subframe_len; j++) {
989  if (s->bits_per_sample == 16) {
990  *s->samples_16[c]++ = (int16_t) s->channel_residues[c][j] << padding_zeroes;
991  } else {
992  *s->samples_32[c]++ = s->channel_residues[c][j] << padding_zeroes;
993  }
994  }
995  }
996 
997  /* handled one subframe */
998  for (i = 0; i < s->channels_for_cur_subframe; i++) {
999  int c = s->channel_indexes_for_cur_subframe[i];
1000  if (s->channel[c].cur_subframe >= s->channel[c].num_subframes) {
1001  av_log(s->avctx, AV_LOG_ERROR, "broken subframe\n");
1002  return AVERROR_INVALIDDATA;
1003  }
1004  ++s->channel[c].cur_subframe;
1005  }
1006  return 0;
1007 }
1008 
1016 {
1017  GetBitContext* gb = &s->gb;
1018  int more_frames = 0, len = 0, i, ret;
1019 
1021  if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0) {
1022  /* return an error if no frame could be decoded at all */
1024  "not enough space for the output samples\n");
1025  s->packet_loss = 1;
1026  return ret;
1027  }
1028  for (i = 0; i < s->num_channels; i++) {
1029  s->samples_16[i] = (int16_t *)s->frame->extended_data[i];
1030  s->samples_32[i] = (int32_t *)s->frame->extended_data[i];
1031  }
1032 
1033  /* get frame length */
1034  if (s->len_prefix)
1035  len = get_bits(gb, s->log2_frame_size);
1036 
1037  /* decode tile information */
1038  if (decode_tilehdr(s)) {
1039  s->packet_loss = 1;
1040  return 0;
1041  }
1042 
1043  /* read drc info */
1045  s->drc_gain = get_bits(gb, 8);
1046 
1047  /* no idea what these are for, might be the number of samples
1048  that need to be skipped at the beginning or end of a stream */
1049  if (get_bits1(gb)) {
1050  int av_unused skip;
1051 
1052  /* usually true for the first frame */
1053  if (get_bits1(gb)) {
1054  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1055  av_dlog(s->avctx, "start skip: %i\n", skip);
1056  }
1057 
1058  /* sometimes true for the last frame */
1059  if (get_bits1(gb)) {
1060  skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1061  av_dlog(s->avctx, "end skip: %i\n", skip);
1062  }
1063 
1064  }
1065 
1066  /* reset subframe states */
1067  s->parsed_all_subframes = 0;
1068  for (i = 0; i < s->num_channels; i++) {
1069  s->channel[i].decoded_samples = 0;
1070  s->channel[i].cur_subframe = 0;
1071  }
1072 
1073  /* decode all subframes */
1074  while (!s->parsed_all_subframes) {
1075  if (decode_subframe(s) < 0) {
1076  s->packet_loss = 1;
1077  return 0;
1078  }
1079  }
1080 
1081  av_dlog(s->avctx, "Frame done\n");
1082 
1083  if (s->skip_frame)
1084  s->skip_frame = 0;
1085 
1086  if (s->len_prefix) {
1087  if (len != (get_bits_count(gb) - s->frame_offset) + 2) {
1088  /* FIXME: not sure if this is always an error */
1090  "frame[%i] would have to skip %i bits\n", s->frame_num,
1091  len - (get_bits_count(gb) - s->frame_offset) - 1);
1092  s->packet_loss = 1;
1093  return 0;
1094  }
1095 
1096  /* skip the rest of the frame data */
1097  skip_bits_long(gb, len - (get_bits_count(gb) - s->frame_offset) - 1);
1098  }
1099 
1100  /* decode trailer bit */
1101  more_frames = get_bits1(gb);
1102  ++s->frame_num;
1103  return more_frames;
1104 }
1105 
1113 {
1114  return s->buf_bit_size - get_bits_count(gb);
1115 }
1116 
1124 static void save_bits(WmallDecodeCtx *s, GetBitContext* gb, int len,
1125  int append)
1126 {
1127  int buflen;
1128  PutBitContext tmp;
1129 
1130  /* when the frame data does not need to be concatenated, the input buffer
1131  is reset and additional bits from the previous frame are copied
1132  and skipped later so that a fast byte copy is possible */
1133 
1134  if (!append) {
1135  s->frame_offset = get_bits_count(gb) & 7;
1136  s->num_saved_bits = s->frame_offset;
1138  }
1139 
1140  buflen = (s->num_saved_bits + len + 8) >> 3;
1141 
1142  if (len <= 0 || buflen > MAX_FRAMESIZE) {
1143  avpriv_request_sample(s->avctx, "Too small input buffer");
1144  s->packet_loss = 1;
1145  return;
1146  }
1147 
1148  s->num_saved_bits += len;
1149  if (!append) {
1150  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3),
1151  s->num_saved_bits);
1152  } else {
1153  int align = 8 - (get_bits_count(gb) & 7);
1154  align = FFMIN(align, len);
1155  put_bits(&s->pb, align, get_bits(gb, align));
1156  len -= align;
1157  avpriv_copy_bits(&s->pb, gb->buffer + (get_bits_count(gb) >> 3), len);
1158  }
1159  skip_bits_long(gb, len);
1160 
1161  tmp = s->pb;
1162  flush_put_bits(&tmp);
1163 
1165  skip_bits(&s->gb, s->frame_offset);
1166 }
1167 
1168 static int decode_packet(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1169  AVPacket* avpkt)
1170 {
1171  WmallDecodeCtx *s = avctx->priv_data;
1172  GetBitContext* gb = &s->pgb;
1173  const uint8_t* buf = avpkt->data;
1174  int buf_size = avpkt->size;
1175  int num_bits_prev_frame, packet_sequence_number, spliced_packet;
1176 
1177  s->frame->nb_samples = 0;
1178 
1179  if (s->packet_done || s->packet_loss) {
1180  s->packet_done = 0;
1181 
1182  /* sanity check for the buffer length */
1183  if (buf_size < avctx->block_align)
1184  return 0;
1185 
1186  s->next_packet_start = buf_size - avctx->block_align;
1187  buf_size = avctx->block_align;
1188  s->buf_bit_size = buf_size << 3;
1189 
1190  /* parse packet header */
1191  init_get_bits(gb, buf, s->buf_bit_size);
1192  packet_sequence_number = get_bits(gb, 4);
1193  skip_bits(gb, 1); // Skip seekable_frame_in_packet, currently ununused
1194  spliced_packet = get_bits1(gb);
1195  if (spliced_packet)
1196  avpriv_request_sample(avctx, "Bitstream splicing");
1197 
1198  /* get number of bits that need to be added to the previous frame */
1199  num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1200 
1201  /* check for packet loss */
1202  if (!s->packet_loss &&
1203  ((s->packet_sequence_number + 1) & 0xF) != packet_sequence_number) {
1204  s->packet_loss = 1;
1205  av_log(avctx, AV_LOG_ERROR, "Packet loss detected! seq %x vs %x\n",
1206  s->packet_sequence_number, packet_sequence_number);
1207  }
1208  s->packet_sequence_number = packet_sequence_number;
1209 
1210  if (num_bits_prev_frame > 0) {
1211  int remaining_packet_bits = s->buf_bit_size - get_bits_count(gb);
1212  if (num_bits_prev_frame >= remaining_packet_bits) {
1213  num_bits_prev_frame = remaining_packet_bits;
1214  s->packet_done = 1;
1215  }
1216 
1217  /* Append the previous frame data to the remaining data from the
1218  * previous packet to create a full frame. */
1219  save_bits(s, gb, num_bits_prev_frame, 1);
1220 
1221  /* decode the cross packet frame if it is valid */
1222  if (num_bits_prev_frame < remaining_packet_bits && !s->packet_loss)
1223  decode_frame(s);
1224  } else if (s->num_saved_bits - s->frame_offset) {
1225  av_dlog(avctx, "ignoring %x previously saved bits\n",
1226  s->num_saved_bits - s->frame_offset);
1227  }
1228 
1229  if (s->packet_loss) {
1230  /* Reset number of saved bits so that the decoder does not start
1231  * to decode incomplete frames in the s->len_prefix == 0 case. */
1232  s->num_saved_bits = 0;
1233  s->packet_loss = 0;
1235  }
1236 
1237  } else {
1238  int frame_size;
1239 
1240  s->buf_bit_size = (avpkt->size - s->next_packet_start) << 3;
1241  init_get_bits(gb, avpkt->data, s->buf_bit_size);
1242  skip_bits(gb, s->packet_offset);
1243 
1244  if (s->len_prefix && remaining_bits(s, gb) > s->log2_frame_size &&
1245  (frame_size = show_bits(gb, s->log2_frame_size)) &&
1246  frame_size <= remaining_bits(s, gb)) {
1247  save_bits(s, gb, frame_size, 0);
1248  s->packet_done = !decode_frame(s);
1249  } else if (!s->len_prefix
1250  && s->num_saved_bits > get_bits_count(&s->gb)) {
1251  /* when the frames do not have a length prefix, we don't know the
1252  * compressed length of the individual frames however, we know what
1253  * part of a new packet belongs to the previous frame therefore we
1254  * save the incoming packet first, then we append the "previous
1255  * frame" data from the next packet so that we get a buffer that
1256  * only contains full frames */
1257  s->packet_done = !decode_frame(s);
1258  } else {
1259  s->packet_done = 1;
1260  }
1261  }
1262 
1263  if (s->packet_done && !s->packet_loss &&
1264  remaining_bits(s, gb) > 0) {
1265  /* save the rest of the data so that it can be decoded
1266  * with the next packet */
1267  save_bits(s, gb, remaining_bits(s, gb), 0);
1268  }
1269 
1270  *got_frame_ptr = s->frame->nb_samples > 0;
1271  av_frame_move_ref(data, s->frame);
1272 
1273  s->packet_offset = get_bits_count(gb) & 7;
1274 
1275  return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1276 }
1277 
1278 static void flush(AVCodecContext *avctx)
1279 {
1280  WmallDecodeCtx *s = avctx->priv_data;
1281  s->packet_loss = 1;
1282  s->packet_done = 0;
1283  s->num_saved_bits = 0;
1284  s->frame_offset = 0;
1285  s->next_packet_start = 0;
1286  s->cdlms[0][0].order = 0;
1287  s->frame->nb_samples = 0;
1289 }
1290 
1292 {
1293  WmallDecodeCtx *s = avctx->priv_data;
1294 
1295  av_frame_free(&s->frame);
1296 
1297  return 0;
1298 }
1299 
1301  .name = "wmalossless",
1302  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Audio Lossless"),
1303  .type = AVMEDIA_TYPE_AUDIO,
1305  .priv_data_size = sizeof(WmallDecodeCtx),
1306  .init = decode_init,
1307  .close = decode_close,
1308  .decode = decode_packet,
1309  .flush = flush,
1311  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
1314 };