mlpdec.c
Go to the documentation of this file.
1 /*
2  * MLP decoder
3  * Copyright (c) 2007-2008 Ian Caulfield
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include <stdint.h>
28 
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "dsputil.h"
32 #include "libavutil/intreadwrite.h"
33 #include "get_bits.h"
34 #include "libavutil/crc.h"
35 #include "parser.h"
36 #include "mlp_parser.h"
37 #include "mlp.h"
38 
40 #define VLC_BITS 9
41 
42 
43 static const char* sample_message =
44  "Please file a bug report following the instructions at "
45  "http://libav.org/bugreports.html and include "
46  "a sample of this file.";
47 
48 typedef struct SubStream {
50  uint8_t restart_seen;
51 
53 
54 
55  uint16_t noise_type;
56 
58  uint8_t min_channel;
60  uint8_t max_channel;
65 
68 
70  uint8_t noise_shift;
72  uint32_t noisegen_seed;
73 
76 
79 #define PARAM_BLOCKSIZE (1 << 7)
80 #define PARAM_MATRIX (1 << 6)
81 #define PARAM_OUTSHIFT (1 << 5)
82 #define PARAM_QUANTSTEP (1 << 4)
83 #define PARAM_FIR (1 << 3)
84 #define PARAM_IIR (1 << 2)
85 #define PARAM_HUFFOFFSET (1 << 1)
86 #define PARAM_PRESENCE (1 << 0)
87 
88 
90 
92 
94 
97 
105 
108 
110  uint16_t blocksize;
112  uint16_t blockpos;
113 
116 
119 
120 } SubStream;
121 
122 typedef struct MLPDecodeContext {
125 
128 
130  uint8_t params_valid;
131 
133  uint8_t num_substreams;
134 
137 
142 
144 
147 
151 
154 
155 static VLC huff_vlc[3];
156 
159 static av_cold void init_static(void)
160 {
161  if (!huff_vlc[0].bits) {
162  INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
163  &ff_mlp_huffman_tables[0][0][1], 2, 1,
164  &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
165  INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
166  &ff_mlp_huffman_tables[1][0][1], 2, 1,
167  &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
168  INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
169  &ff_mlp_huffman_tables[2][0][1], 2, 1,
170  &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
171  }
172 
173  ff_mlp_init_crc();
174 }
175 
176 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
177  unsigned int substr, unsigned int ch)
178 {
179  SubStream *s = &m->substream[substr];
180  ChannelParams *cp = &s->channel_params[ch];
181  int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
182  int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
183  int32_t sign_huff_offset = cp->huff_offset;
184 
185  if (cp->codebook > 0)
186  sign_huff_offset -= 7 << lsb_bits;
187 
188  if (sign_shift >= 0)
189  sign_huff_offset -= 1 << sign_shift;
190 
191  return sign_huff_offset;
192 }
193 
198  unsigned int substr, unsigned int pos)
199 {
200  SubStream *s = &m->substream[substr];
201  unsigned int mat, channel;
202 
203  for (mat = 0; mat < s->num_primitive_matrices; mat++)
204  if (s->lsb_bypass[mat])
205  m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
206 
207  for (channel = s->min_channel; channel <= s->max_channel; channel++) {
208  ChannelParams *cp = &s->channel_params[channel];
209  int codebook = cp->codebook;
210  int quant_step_size = s->quant_step_size[channel];
211  int lsb_bits = cp->huff_lsbs - quant_step_size;
212  int result = 0;
213 
214  if (codebook > 0)
215  result = get_vlc2(gbp, huff_vlc[codebook-1].table,
216  VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
217 
218  if (result < 0)
219  return AVERROR_INVALIDDATA;
220 
221  if (lsb_bits > 0)
222  result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
223 
224  result += cp->sign_huff_offset;
225  result <<= quant_step_size;
226 
227  m->sample_buffer[pos + s->blockpos][channel] = result;
228  }
229 
230  return 0;
231 }
232 
234 {
235  MLPDecodeContext *m = avctx->priv_data;
236  int substr;
237 
238  init_static();
239  m->avctx = avctx;
240  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
241  m->substream[substr].lossless_check_data = 0xffffffff;
242  dsputil_init(&m->dsp, avctx);
243 
245  avctx->coded_frame = &m->frame;
246 
247  return 0;
248 }
249 
256 {
257  MLPHeaderInfo mh;
258  int substr, ret;
259 
260  if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
261  return ret;
262 
263  if (mh.group1_bits == 0) {
264  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
265  return AVERROR_INVALIDDATA;
266  }
267  if (mh.group2_bits > mh.group1_bits) {
269  "Channel group 2 cannot have more bits per sample than group 1.\n");
270  return AVERROR_INVALIDDATA;
271  }
272 
275  "Channel groups with differing sample rates are not currently supported.\n");
276  return AVERROR_INVALIDDATA;
277  }
278 
279  if (mh.group1_samplerate == 0) {
280  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
281  return AVERROR_INVALIDDATA;
282  }
285  "Sampling rate %d is greater than the supported maximum (%d).\n",
287  return AVERROR_INVALIDDATA;
288  }
289  if (mh.access_unit_size > MAX_BLOCKSIZE) {
291  "Block size %d is greater than the supported maximum (%d).\n",
293  return AVERROR_INVALIDDATA;
294  }
297  "Block size pow2 %d is greater than the supported maximum (%d).\n",
299  return AVERROR_INVALIDDATA;
300  }
301 
302  if (mh.num_substreams == 0)
303  return AVERROR_INVALIDDATA;
304  if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
305  av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
306  return AVERROR_INVALIDDATA;
307  }
308  if (mh.num_substreams > MAX_SUBSTREAMS) {
310  "Number of substreams %d is larger than the maximum supported "
311  "by the decoder. %s\n", mh.num_substreams, sample_message);
312  return AVERROR_INVALIDDATA;
313  }
314 
317 
320 
323 
325  if (mh.group1_bits > 16)
327  else
329 
330  m->params_valid = 1;
331  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
332  m->substream[substr].restart_seen = 0;
333 
334  return 0;
335 }
336 
342  const uint8_t *buf, unsigned int substr)
343 {
344  SubStream *s = &m->substream[substr];
345  unsigned int ch;
346  int sync_word, tmp;
347  uint8_t checksum;
348  uint8_t lossless_check;
349  int start_count = get_bits_count(gbp);
350  int min_channel, max_channel, max_matrix_channel;
351  const int std_max_matrix_channel = m->avctx->codec_id == CODEC_ID_MLP
354 
355  sync_word = get_bits(gbp, 13);
356 
357  if (sync_word != 0x31ea >> 1) {
359  "restart header sync incorrect (got 0x%04x)\n", sync_word);
360  return AVERROR_INVALIDDATA;
361  }
362 
363  s->noise_type = get_bits1(gbp);
364 
365  if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
366  av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
367  return AVERROR_INVALIDDATA;
368  }
369 
370  skip_bits(gbp, 16); /* Output timestamp */
371 
372  min_channel = get_bits(gbp, 4);
373  max_channel = get_bits(gbp, 4);
374  max_matrix_channel = get_bits(gbp, 4);
375 
376  if (max_matrix_channel > std_max_matrix_channel) {
378  "Max matrix channel cannot be greater than %d.\n",
379  max_matrix_channel);
380  return AVERROR_INVALIDDATA;
381  }
382 
383  if (max_channel != max_matrix_channel) {
385  "Max channel must be equal max matrix channel.\n");
386  return AVERROR_INVALIDDATA;
387  }
388 
389  /* This should happen for TrueHD streams with >6 channels and MLP's noise
390  * type. It is not yet known if this is allowed. */
393  "Number of channels %d is larger than the maximum supported "
394  "by the decoder. %s\n", s->max_channel+2, sample_message);
395  return AVERROR_INVALIDDATA;
396  }
397 
398  if (min_channel > max_channel) {
400  "Substream min channel cannot be greater than max channel.\n");
401  return AVERROR_INVALIDDATA;
402  }
403 
404 
405  s->min_channel = min_channel;
406  s->max_channel = max_channel;
407  s->max_matrix_channel = max_matrix_channel;
408 
409  if (m->avctx->request_channels > 0 &&
410  m->avctx->request_channels <= s->max_channel + 1 &&
411  m->max_decoded_substream > substr) {
413  "Extracting %d channel downmix from substream %d. "
414  "Further substreams will be skipped.\n",
415  s->max_channel + 1, substr);
416  m->max_decoded_substream = substr;
417  }
418 
419  s->noise_shift = get_bits(gbp, 4);
420  s->noisegen_seed = get_bits(gbp, 23);
421 
422  skip_bits(gbp, 19);
423 
424  s->data_check_present = get_bits1(gbp);
425  lossless_check = get_bits(gbp, 8);
426  if (substr == m->max_decoded_substream
427  && s->lossless_check_data != 0xffffffff) {
429  if (tmp != lossless_check)
431  "Lossless check failed - expected %02x, calculated %02x.\n",
432  lossless_check, tmp);
433  }
434 
435  skip_bits(gbp, 16);
436 
437  memset(s->ch_assign, 0, sizeof(s->ch_assign));
438 
439  for (ch = 0; ch <= s->max_matrix_channel; ch++) {
440  int ch_assign = get_bits(gbp, 6);
441  if (ch_assign > s->max_matrix_channel) {
443  "Assignment of matrix channel %d to invalid output channel %d. %s\n",
444  ch, ch_assign, sample_message);
445  return AVERROR_INVALIDDATA;
446  }
447  s->ch_assign[ch_assign] = ch;
448  }
449 
450  checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
451 
452  if (checksum != get_bits(gbp, 8))
453  av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
454 
455  /* Set default decoding parameters. */
456  s->param_presence_flags = 0xff;
457  s->num_primitive_matrices = 0;
458  s->blocksize = 8;
459  s->lossless_check_data = 0;
460 
461  memset(s->output_shift , 0, sizeof(s->output_shift ));
462  memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
463 
464  for (ch = s->min_channel; ch <= s->max_channel; ch++) {
465  ChannelParams *cp = &s->channel_params[ch];
466  cp->filter_params[FIR].order = 0;
467  cp->filter_params[IIR].order = 0;
468  cp->filter_params[FIR].shift = 0;
469  cp->filter_params[IIR].shift = 0;
470 
471  /* Default audio coding is 24-bit raw PCM. */
472  cp->huff_offset = 0;
473  cp->sign_huff_offset = (-1) << 23;
474  cp->codebook = 0;
475  cp->huff_lsbs = 24;
476  }
477 
478  if (substr == m->max_decoded_substream)
479  m->avctx->channels = s->max_matrix_channel + 1;
480 
481  return 0;
482 }
483 
487  unsigned int substr, unsigned int channel,
488  unsigned int filter)
489 {
490  SubStream *s = &m->substream[substr];
492  const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
493  const char fchar = filter ? 'I' : 'F';
494  int i, order;
495 
496  // Filter is 0 for FIR, 1 for IIR.
497  assert(filter < 2);
498 
499  if (m->filter_changed[channel][filter]++ > 1) {
500  av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
501  return AVERROR_INVALIDDATA;
502  }
503 
504  order = get_bits(gbp, 4);
505  if (order > max_order) {
507  "%cIR filter order %d is greater than maximum %d.\n",
508  fchar, order, max_order);
509  return AVERROR_INVALIDDATA;
510  }
511  fp->order = order;
512 
513  if (order > 0) {
514  int32_t *fcoeff = s->channel_params[channel].coeff[filter];
515  int coeff_bits, coeff_shift;
516 
517  fp->shift = get_bits(gbp, 4);
518 
519  coeff_bits = get_bits(gbp, 5);
520  coeff_shift = get_bits(gbp, 3);
521  if (coeff_bits < 1 || coeff_bits > 16) {
523  "%cIR filter coeff_bits must be between 1 and 16.\n",
524  fchar);
525  return AVERROR_INVALIDDATA;
526  }
527  if (coeff_bits + coeff_shift > 16) {
529  "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
530  fchar);
531  return AVERROR_INVALIDDATA;
532  }
533 
534  for (i = 0; i < order; i++)
535  fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
536 
537  if (get_bits1(gbp)) {
538  int state_bits, state_shift;
539 
540  if (filter == FIR) {
542  "FIR filter has state data specified.\n");
543  return AVERROR_INVALIDDATA;
544  }
545 
546  state_bits = get_bits(gbp, 4);
547  state_shift = get_bits(gbp, 4);
548 
549  /* TODO: Check validity of state data. */
550 
551  for (i = 0; i < order; i++)
552  fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
553  }
554  }
555 
556  return 0;
557 }
558 
561 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
562 {
563  SubStream *s = &m->substream[substr];
564  unsigned int mat, ch;
565  const int max_primitive_matrices = m->avctx->codec_id == CODEC_ID_MLP
568 
569  if (m->matrix_changed++ > 1) {
570  av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
571  return AVERROR_INVALIDDATA;
572  }
573 
574  s->num_primitive_matrices = get_bits(gbp, 4);
575 
576  if (s->num_primitive_matrices > max_primitive_matrices) {
578  "Number of primitive matrices cannot be greater than %d.\n",
579  max_primitive_matrices);
580  return AVERROR_INVALIDDATA;
581  }
582 
583  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
584  int frac_bits, max_chan;
585  s->matrix_out_ch[mat] = get_bits(gbp, 4);
586  frac_bits = get_bits(gbp, 4);
587  s->lsb_bypass [mat] = get_bits1(gbp);
588 
589  if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
591  "Invalid channel %d specified as output from matrix.\n",
592  s->matrix_out_ch[mat]);
593  return AVERROR_INVALIDDATA;
594  }
595  if (frac_bits > 14) {
597  "Too many fractional bits specified.\n");
598  return AVERROR_INVALIDDATA;
599  }
600 
601  max_chan = s->max_matrix_channel;
602  if (!s->noise_type)
603  max_chan+=2;
604 
605  for (ch = 0; ch <= max_chan; ch++) {
606  int coeff_val = 0;
607  if (get_bits1(gbp))
608  coeff_val = get_sbits(gbp, frac_bits + 2);
609 
610  s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
611  }
612 
613  if (s->noise_type)
614  s->matrix_noise_shift[mat] = get_bits(gbp, 4);
615  else
616  s->matrix_noise_shift[mat] = 0;
617  }
618 
619  return 0;
620 }
621 
624 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
625  GetBitContext *gbp, unsigned int ch)
626 {
627  SubStream *s = &m->substream[substr];
628  ChannelParams *cp = &s->channel_params[ch];
629  FilterParams *fir = &cp->filter_params[FIR];
630  FilterParams *iir = &cp->filter_params[IIR];
631  int ret;
632 
634  if (get_bits1(gbp))
635  if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
636  return ret;
637 
639  if (get_bits1(gbp))
640  if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
641  return ret;
642 
643  if (fir->order + iir->order > 8) {
644  av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
645  return AVERROR_INVALIDDATA;
646  }
647 
648  if (fir->order && iir->order &&
649  fir->shift != iir->shift) {
651  "FIR and IIR filters must use the same precision.\n");
652  return AVERROR_INVALIDDATA;
653  }
654  /* The FIR and IIR filters must have the same precision.
655  * To simplify the filtering code, only the precision of the
656  * FIR filter is considered. If only the IIR filter is employed,
657  * the FIR filter precision is set to that of the IIR filter, so
658  * that the filtering code can use it. */
659  if (!fir->order && iir->order)
660  fir->shift = iir->shift;
661 
663  if (get_bits1(gbp))
664  cp->huff_offset = get_sbits(gbp, 15);
665 
666  cp->codebook = get_bits(gbp, 2);
667  cp->huff_lsbs = get_bits(gbp, 5);
668 
669  if (cp->huff_lsbs > 24) {
670  av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
671  return AVERROR_INVALIDDATA;
672  }
673 
674  cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
675 
676  return 0;
677 }
678 
683  unsigned int substr)
684 {
685  SubStream *s = &m->substream[substr];
686  unsigned int ch;
687  int ret;
688 
690  if (get_bits1(gbp))
691  s->param_presence_flags = get_bits(gbp, 8);
692 
694  if (get_bits1(gbp)) {
695  s->blocksize = get_bits(gbp, 9);
696  if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
697  av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
698  s->blocksize = 0;
699  return AVERROR_INVALIDDATA;
700  }
701  }
702 
704  if (get_bits1(gbp))
705  if ((ret = read_matrix_params(m, substr, gbp)) < 0)
706  return ret;
707 
709  if (get_bits1(gbp))
710  for (ch = 0; ch <= s->max_matrix_channel; ch++)
711  s->output_shift[ch] = get_sbits(gbp, 4);
712 
714  if (get_bits1(gbp))
715  for (ch = 0; ch <= s->max_channel; ch++) {
716  ChannelParams *cp = &s->channel_params[ch];
717 
718  s->quant_step_size[ch] = get_bits(gbp, 4);
719 
720  cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
721  }
722 
723  for (ch = s->min_channel; ch <= s->max_channel; ch++)
724  if (get_bits1(gbp))
725  if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
726  return ret;
727 
728  return 0;
729 }
730 
731 #define MSB_MASK(bits) (-1u << bits)
732 
736 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
737  unsigned int channel)
738 {
739  SubStream *s = &m->substream[substr];
740  const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
741  int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
742  int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
743  int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
744  FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
745  FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
746  unsigned int filter_shift = fir->shift;
747  int32_t mask = MSB_MASK(s->quant_step_size[channel]);
748 
749  memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
750  memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
751 
752  m->dsp.mlp_filter_channel(firbuf, fircoeff,
753  fir->order, iir->order,
754  filter_shift, mask, s->blocksize,
755  &m->sample_buffer[s->blockpos][channel]);
756 
757  memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
758  memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
759 }
760 
764  unsigned int substr)
765 {
766  SubStream *s = &m->substream[substr];
767  unsigned int i, ch, expected_stream_pos = 0;
768  int ret;
769 
770  if (s->data_check_present) {
771  expected_stream_pos = get_bits_count(gbp);
772  expected_stream_pos += get_bits(gbp, 16);
773  av_log(m->avctx, AV_LOG_WARNING, "This file contains some features "
774  "we have not tested yet. %s\n", sample_message);
775  }
776 
777  if (s->blockpos + s->blocksize > m->access_unit_size) {
778  av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
779  return AVERROR_INVALIDDATA;
780  }
781 
782  memset(&m->bypassed_lsbs[s->blockpos][0], 0,
783  s->blocksize * sizeof(m->bypassed_lsbs[0]));
784 
785  for (i = 0; i < s->blocksize; i++)
786  if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
787  return ret;
788 
789  for (ch = s->min_channel; ch <= s->max_channel; ch++)
790  filter_channel(m, substr, ch);
791 
792  s->blockpos += s->blocksize;
793 
794  if (s->data_check_present) {
795  if (get_bits_count(gbp) != expected_stream_pos)
796  av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
797  skip_bits(gbp, 8);
798  }
799 
800  return 0;
801 }
802 
805 static const int8_t noise_table[256] = {
806  30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
807  52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
808  10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
809  51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
810  38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
811  61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
812  67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
813  48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
814  0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
815  16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
816  13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
817  89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
818  36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
819  39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
820  45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
821  -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
822 };
823 
834 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
835 {
836  SubStream *s = &m->substream[substr];
837  unsigned int i;
838  uint32_t seed = s->noisegen_seed;
839  unsigned int maxchan = s->max_matrix_channel;
840 
841  for (i = 0; i < s->blockpos; i++) {
842  uint16_t seed_shr7 = seed >> 7;
843  m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
844  m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift;
845 
846  seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
847  }
848 
849  s->noisegen_seed = seed;
850 }
851 
854 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
855 {
856  SubStream *s = &m->substream[substr];
857  unsigned int i;
858  uint32_t seed = s->noisegen_seed;
859 
860  for (i = 0; i < m->access_unit_size_pow2; i++) {
861  uint8_t seed_shr15 = seed >> 15;
862  m->noise_buffer[i] = noise_table[seed_shr15];
863  seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
864  }
865 
866  s->noisegen_seed = seed;
867 }
868 
869 
873 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
874 {
875  SubStream *s = &m->substream[substr];
876  unsigned int mat, src_ch, i;
877  unsigned int maxchan;
878 
879  maxchan = s->max_matrix_channel;
880  if (!s->noise_type) {
881  generate_2_noise_channels(m, substr);
882  maxchan += 2;
883  } else {
884  fill_noise_buffer(m, substr);
885  }
886 
887  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
888  int matrix_noise_shift = s->matrix_noise_shift[mat];
889  unsigned int dest_ch = s->matrix_out_ch[mat];
890  int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
891  int32_t *coeffs = s->matrix_coeff[mat];
892  int index = s->num_primitive_matrices - mat;
893  int index2 = 2 * index + 1;
894 
895  /* TODO: DSPContext? */
896 
897  for (i = 0; i < s->blockpos; i++) {
898  int32_t bypassed_lsb = m->bypassed_lsbs[i][mat];
899  int32_t *samples = m->sample_buffer[i];
900  int64_t accum = 0;
901 
902  for (src_ch = 0; src_ch <= maxchan; src_ch++)
903  accum += (int64_t) samples[src_ch] * coeffs[src_ch];
904 
905  if (matrix_noise_shift) {
906  index &= m->access_unit_size_pow2 - 1;
907  accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
908  index += index2;
909  }
910 
911  samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
912  }
913  }
914 }
915 
918 static int output_data(MLPDecodeContext *m, unsigned int substr,
919  void *data, int *got_frame_ptr)
920 {
921  AVCodecContext *avctx = m->avctx;
922  SubStream *s = &m->substream[substr];
923  unsigned int i, out_ch = 0;
924  int32_t *data_32;
925  int16_t *data_16;
926  int ret;
927  int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
928 
929  if (m->avctx->channels != s->max_matrix_channel + 1) {
930  av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
931  return AVERROR_INVALIDDATA;
932  }
933 
934  /* get output buffer */
935  m->frame.nb_samples = s->blockpos;
936  if ((ret = ff_get_buffer(avctx, &m->frame)) < 0) {
937  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
938  return ret;
939  }
940  data_32 = (int32_t *)m->frame.data[0];
941  data_16 = (int16_t *)m->frame.data[0];
942 
943  for (i = 0; i < s->blockpos; i++) {
944  for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
945  int mat_ch = s->ch_assign[out_ch];
946  int32_t sample = m->sample_buffer[i][mat_ch]
947  << s->output_shift[mat_ch];
948  s->lossless_check_data ^= (sample & 0xffffff) << mat_ch;
949  if (is32) *data_32++ = sample << 8;
950  else *data_16++ = sample >> 8;
951  }
952  }
953 
954  *got_frame_ptr = 1;
955  *(AVFrame *)data = m->frame;
956 
957  return 0;
958 }
959 
964 static int read_access_unit(AVCodecContext *avctx, void* data,
965  int *got_frame_ptr, AVPacket *avpkt)
966 {
967  const uint8_t *buf = avpkt->data;
968  int buf_size = avpkt->size;
969  MLPDecodeContext *m = avctx->priv_data;
970  GetBitContext gb;
971  unsigned int length, substr;
972  unsigned int substream_start;
973  unsigned int header_size = 4;
974  unsigned int substr_header_size = 0;
975  uint8_t substream_parity_present[MAX_SUBSTREAMS];
976  uint16_t substream_data_len[MAX_SUBSTREAMS];
977  uint8_t parity_bits;
978  int ret;
979 
980  if (buf_size < 4)
981  return 0;
982 
983  length = (AV_RB16(buf) & 0xfff) * 2;
984 
985  if (length < 4 || length > buf_size)
986  return AVERROR_INVALIDDATA;
987 
988  init_get_bits(&gb, (buf + 4), (length - 4) * 8);
989 
990  m->is_major_sync_unit = 0;
991  if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
992  if (read_major_sync(m, &gb) < 0)
993  goto error;
994  m->is_major_sync_unit = 1;
995  header_size += 28;
996  }
997 
998  if (!m->params_valid) {
1000  "Stream parameters not seen; skipping frame.\n");
1001  *got_frame_ptr = 0;
1002  return length;
1003  }
1004 
1005  substream_start = 0;
1006 
1007  for (substr = 0; substr < m->num_substreams; substr++) {
1008  int extraword_present, checkdata_present, end, nonrestart_substr;
1009 
1010  extraword_present = get_bits1(&gb);
1011  nonrestart_substr = get_bits1(&gb);
1012  checkdata_present = get_bits1(&gb);
1013  skip_bits1(&gb);
1014 
1015  end = get_bits(&gb, 12) * 2;
1016 
1017  substr_header_size += 2;
1018 
1019  if (extraword_present) {
1020  if (m->avctx->codec_id == CODEC_ID_MLP) {
1021  av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
1022  goto error;
1023  }
1024  skip_bits(&gb, 16);
1025  substr_header_size += 2;
1026  }
1027 
1028  if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1029  av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
1030  goto error;
1031  }
1032 
1033  if (end + header_size + substr_header_size > length) {
1035  "Indicated length of substream %d data goes off end of "
1036  "packet.\n", substr);
1037  end = length - header_size - substr_header_size;
1038  }
1039 
1040  if (end < substream_start) {
1041  av_log(avctx, AV_LOG_ERROR,
1042  "Indicated end offset of substream %d data "
1043  "is smaller than calculated start offset.\n",
1044  substr);
1045  goto error;
1046  }
1047 
1048  if (substr > m->max_decoded_substream)
1049  continue;
1050 
1051  substream_parity_present[substr] = checkdata_present;
1052  substream_data_len[substr] = end - substream_start;
1053  substream_start = end;
1054  }
1055 
1056  parity_bits = ff_mlp_calculate_parity(buf, 4);
1057  parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1058 
1059  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1060  av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1061  goto error;
1062  }
1063 
1064  buf += header_size + substr_header_size;
1065 
1066  for (substr = 0; substr <= m->max_decoded_substream; substr++) {
1067  SubStream *s = &m->substream[substr];
1068  init_get_bits(&gb, buf, substream_data_len[substr] * 8);
1069 
1070  m->matrix_changed = 0;
1071  memset(m->filter_changed, 0, sizeof(m->filter_changed));
1072 
1073  s->blockpos = 0;
1074  do {
1075  if (get_bits1(&gb)) {
1076  if (get_bits1(&gb)) {
1077  /* A restart header should be present. */
1078  if (read_restart_header(m, &gb, buf, substr) < 0)
1079  goto next_substr;
1080  s->restart_seen = 1;
1081  }
1082 
1083  if (!s->restart_seen)
1084  goto next_substr;
1085  if (read_decoding_params(m, &gb, substr) < 0)
1086  goto next_substr;
1087  }
1088 
1089  if (!s->restart_seen)
1090  goto next_substr;
1091 
1092  if ((ret = read_block_data(m, &gb, substr)) < 0)
1093  return ret;
1094 
1095  if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1096  goto substream_length_mismatch;
1097 
1098  } while (!get_bits1(&gb));
1099 
1100  skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1101 
1102  if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1103  int shorten_by;
1104 
1105  if (get_bits(&gb, 16) != 0xD234)
1106  return AVERROR_INVALIDDATA;
1107 
1108  shorten_by = get_bits(&gb, 16);
1109  if (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by & 0x2000)
1110  s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1111  else if (m->avctx->codec_id == CODEC_ID_MLP && shorten_by != 0xD234)
1112  return AVERROR_INVALIDDATA;
1113 
1114  if (substr == m->max_decoded_substream)
1115  av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1116  }
1117 
1118  if (substream_parity_present[substr]) {
1119  uint8_t parity, checksum;
1120 
1121  if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1122  goto substream_length_mismatch;
1123 
1124  parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1125  checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
1126 
1127  if ((get_bits(&gb, 8) ^ parity) != 0xa9 )
1128  av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
1129  if ( get_bits(&gb, 8) != checksum)
1130  av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n" , substr);
1131  }
1132 
1133  if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1134  goto substream_length_mismatch;
1135 
1136 next_substr:
1137  if (!s->restart_seen)
1139  "No restart header present in substream %d.\n", substr);
1140 
1141  buf += substream_data_len[substr];
1142  }
1143 
1145 
1146  if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
1147  return ret;
1148 
1149  return length;
1150 
1151 substream_length_mismatch:
1152  av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1153  return AVERROR_INVALIDDATA;
1154 
1155 error:
1156  m->params_valid = 0;
1157  return AVERROR_INVALIDDATA;
1158 }
1159 
1161  .name = "mlp",
1162  .type = AVMEDIA_TYPE_AUDIO,
1163  .id = CODEC_ID_MLP,
1164  .priv_data_size = sizeof(MLPDecodeContext),
1165  .init = mlp_decode_init,
1167  .capabilities = CODEC_CAP_DR1,
1168  .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1169 };
1170 
1171 #if CONFIG_TRUEHD_DECODER
1172 AVCodec ff_truehd_decoder = {
1173  .name = "truehd",
1174  .type = AVMEDIA_TYPE_AUDIO,
1175  .id = CODEC_ID_TRUEHD,
1176  .priv_data_size = sizeof(MLPDecodeContext),
1177  .init = mlp_decode_init,
1179  .capabilities = CODEC_CAP_DR1,
1180  .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
1181 };
1182 #endif /* CONFIG_TRUEHD_DECODER */