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 "libavutil/intreadwrite.h"
32 #include "get_bits.h"
33 #include "internal.h"
34 #include "libavutil/crc.h"
35 #include "parser.h"
36 #include "mlp_parser.h"
37 #include "mlpdsp.h"
38 #include "mlp.h"
39 #include "config.h"
40 
42 #if ARCH_ARM
43 #define VLC_BITS 5
44 #define VLC_STATIC_SIZE 64
45 #else
46 #define VLC_BITS 9
47 #define VLC_STATIC_SIZE 512
48 #endif
49 
50 typedef struct SubStream {
53 
55 
56 
57  uint16_t noise_type;
58 
68  uint64_t ch_layout;
69 
72 
76  uint32_t noisegen_seed;
77 
80 
83 #define PARAM_BLOCKSIZE (1 << 7)
84 #define PARAM_MATRIX (1 << 6)
85 #define PARAM_OUTSHIFT (1 << 5)
86 #define PARAM_QUANTSTEP (1 << 4)
87 #define PARAM_FIR (1 << 3)
88 #define PARAM_IIR (1 << 2)
89 #define PARAM_HUFFOFFSET (1 << 1)
90 #define PARAM_PRESENCE (1 << 0)
91 
92 
94 
96 
98 
101 
109 
112 
114  uint16_t blocksize;
116  uint16_t blockpos;
117 
120 
123 
124 } SubStream;
125 
126 typedef struct MLPDecodeContext {
129 
132 
135 
138 
141 
146 
148 
151 
155 
158 
159 static const uint64_t thd_channel_order[] = {
161  AV_CH_FRONT_CENTER, // C
162  AV_CH_LOW_FREQUENCY, // LFE
167  AV_CH_BACK_CENTER, // Cs
168  AV_CH_TOP_CENTER, // Ts
171  AV_CH_TOP_FRONT_CENTER, // Cvh
172  AV_CH_LOW_FREQUENCY_2, // LFE2
173 };
174 
175 static uint64_t thd_channel_layout_extract_channel(uint64_t channel_layout,
176  int index)
177 {
178  int i;
179 
180  if (av_get_channel_layout_nb_channels(channel_layout) <= index)
181  return 0;
182 
183  for (i = 0; i < FF_ARRAY_ELEMS(thd_channel_order); i++)
184  if (channel_layout & thd_channel_order[i] && !index--)
185  return thd_channel_order[i];
186  return 0;
187 }
188 
189 static VLC huff_vlc[3];
190 
193 static av_cold void init_static(void)
194 {
195  if (!huff_vlc[0].bits) {
196  INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
197  &ff_mlp_huffman_tables[0][0][1], 2, 1,
198  &ff_mlp_huffman_tables[0][0][0], 2, 1, VLC_STATIC_SIZE);
199  INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
200  &ff_mlp_huffman_tables[1][0][1], 2, 1,
201  &ff_mlp_huffman_tables[1][0][0], 2, 1, VLC_STATIC_SIZE);
202  INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
203  &ff_mlp_huffman_tables[2][0][1], 2, 1,
204  &ff_mlp_huffman_tables[2][0][0], 2, 1, VLC_STATIC_SIZE);
205  }
206 
207  ff_mlp_init_crc();
208 }
209 
211  unsigned int substr, unsigned int ch)
212 {
213  SubStream *s = &m->substream[substr];
214  ChannelParams *cp = &s->channel_params[ch];
215  int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
216  int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
217  int32_t sign_huff_offset = cp->huff_offset;
218 
219  if (cp->codebook > 0)
220  sign_huff_offset -= 7 << lsb_bits;
221 
222  if (sign_shift >= 0)
223  sign_huff_offset -= 1 << sign_shift;
224 
225  return sign_huff_offset;
226 }
227 
232  unsigned int substr, unsigned int pos)
233 {
234  SubStream *s = &m->substream[substr];
235  unsigned int mat, channel;
236 
237  for (mat = 0; mat < s->num_primitive_matrices; mat++)
238  if (s->lsb_bypass[mat])
239  m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
240 
241  for (channel = s->min_channel; channel <= s->max_channel; channel++) {
242  ChannelParams *cp = &s->channel_params[channel];
243  int codebook = cp->codebook;
244  int quant_step_size = s->quant_step_size[channel];
245  int lsb_bits = cp->huff_lsbs - quant_step_size;
246  int result = 0;
247 
248  if (codebook > 0)
249  result = get_vlc2(gbp, huff_vlc[codebook-1].table,
250  VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
251 
252  if (result < 0)
253  return AVERROR_INVALIDDATA;
254 
255  if (lsb_bits > 0)
256  result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
257 
258  result += cp->sign_huff_offset;
259  result <<= quant_step_size;
260 
261  m->sample_buffer[pos + s->blockpos][channel] = result;
262  }
263 
264  return 0;
265 }
266 
268 {
269  MLPDecodeContext *m = avctx->priv_data;
270  int substr;
271 
272  init_static();
273  m->avctx = avctx;
274  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
275  m->substream[substr].lossless_check_data = 0xffffffff;
276  ff_mlpdsp_init(&m->dsp);
277 
279  avctx->coded_frame = &m->frame;
280 
281  return 0;
282 }
283 
290 {
291  MLPHeaderInfo mh;
292  int substr, ret;
293 
294  if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
295  return ret;
296 
297  if (mh.group1_bits == 0) {
298  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
299  return AVERROR_INVALIDDATA;
300  }
301  if (mh.group2_bits > mh.group1_bits) {
303  "Channel group 2 cannot have more bits per sample than group 1.\n");
304  return AVERROR_INVALIDDATA;
305  }
306 
309  "Channel groups with differing sample rates are not currently supported.\n");
310  return AVERROR_INVALIDDATA;
311  }
312 
313  if (mh.group1_samplerate == 0) {
314  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
315  return AVERROR_INVALIDDATA;
316  }
319  "Sampling rate %d is greater than the supported maximum (%d).\n",
321  return AVERROR_INVALIDDATA;
322  }
323  if (mh.access_unit_size > MAX_BLOCKSIZE) {
325  "Block size %d is greater than the supported maximum (%d).\n",
327  return AVERROR_INVALIDDATA;
328  }
331  "Block size pow2 %d is greater than the supported maximum (%d).\n",
333  return AVERROR_INVALIDDATA;
334  }
335 
336  if (mh.num_substreams == 0)
337  return AVERROR_INVALIDDATA;
338  if (m->avctx->codec_id == AV_CODEC_ID_MLP && mh.num_substreams > 2) {
339  av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
340  return AVERROR_INVALIDDATA;
341  }
342  if (mh.num_substreams > MAX_SUBSTREAMS) {
344  "Number of substreams %d is larger than the maximum supported "
345  "by the decoder.\n", mh.num_substreams);
346  return AVERROR_PATCHWELCOME;
347  }
348 
351 
354 
357 
359  if (mh.group1_bits > 16)
361  else
367 
368  m->params_valid = 1;
369  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
370  m->substream[substr].restart_seen = 0;
371 
372  /* Set the layout for each substream. When there's more than one, the first
373  * substream is Stereo. Subsequent substreams' layouts are indicated in the
374  * major sync. */
375  if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
376  if ((substr = (mh.num_substreams > 1)))
378  m->substream[substr].ch_layout = mh.channel_layout_mlp;
379  } else {
380  if ((substr = (mh.num_substreams > 1)))
382  if (mh.num_substreams > 2)
385  else
388  }
389 
390  return 0;
391 }
392 
398  const uint8_t *buf, unsigned int substr)
399 {
400  SubStream *s = &m->substream[substr];
401  unsigned int ch;
402  int sync_word, tmp;
403  uint8_t checksum;
404  uint8_t lossless_check;
405  int start_count = get_bits_count(gbp);
406  int min_channel, max_channel, max_matrix_channel;
407  const int std_max_matrix_channel = m->avctx->codec_id == AV_CODEC_ID_MLP
410 
411  sync_word = get_bits(gbp, 13);
412 
413  if (sync_word != 0x31ea >> 1) {
415  "restart header sync incorrect (got 0x%04x)\n", sync_word);
416  return AVERROR_INVALIDDATA;
417  }
418 
419  s->noise_type = get_bits1(gbp);
420 
421  if (m->avctx->codec_id == AV_CODEC_ID_MLP && s->noise_type) {
422  av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
423  return AVERROR_INVALIDDATA;
424  }
425 
426  skip_bits(gbp, 16); /* Output timestamp */
427 
428  min_channel = get_bits(gbp, 4);
429  max_channel = get_bits(gbp, 4);
430  max_matrix_channel = get_bits(gbp, 4);
431 
432  if (max_matrix_channel > std_max_matrix_channel) {
434  "Max matrix channel cannot be greater than %d.\n",
435  max_matrix_channel);
436  return AVERROR_INVALIDDATA;
437  }
438 
439  if (max_channel != max_matrix_channel) {
441  "Max channel must be equal max matrix channel.\n");
442  return AVERROR_INVALIDDATA;
443  }
444 
445  /* This should happen for TrueHD streams with >6 channels and MLP's noise
446  * type. It is not yet known if this is allowed. */
449  "Number of channels %d is larger than the maximum supported "
450  "by the decoder.\n", s->max_channel + 2);
451  return AVERROR_PATCHWELCOME;
452  }
453 
454  if (min_channel > max_channel) {
456  "Substream min channel cannot be greater than max channel.\n");
457  return AVERROR_INVALIDDATA;
458  }
459 
460 
461  s->min_channel = min_channel;
462  s->max_channel = max_channel;
463  s->max_matrix_channel = max_matrix_channel;
464 
465  if (m->avctx->request_channels > 0 &&
466  m->avctx->request_channels <= s->max_channel + 1 &&
467  m->max_decoded_substream > substr) {
469  "Extracting %d channel downmix from substream %d. "
470  "Further substreams will be skipped.\n",
471  s->max_channel + 1, substr);
472  m->max_decoded_substream = substr;
473  }
474 
475  s->noise_shift = get_bits(gbp, 4);
476  s->noisegen_seed = get_bits(gbp, 23);
477 
478  skip_bits(gbp, 19);
479 
480  s->data_check_present = get_bits1(gbp);
481  lossless_check = get_bits(gbp, 8);
482  if (substr == m->max_decoded_substream
483  && s->lossless_check_data != 0xffffffff) {
485  if (tmp != lossless_check)
487  "Lossless check failed - expected %02x, calculated %02x.\n",
488  lossless_check, tmp);
489  }
490 
491  skip_bits(gbp, 16);
492 
493  memset(s->ch_assign, 0, sizeof(s->ch_assign));
494 
495  for (ch = 0; ch <= s->max_matrix_channel; ch++) {
496  int ch_assign = get_bits(gbp, 6);
497  if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
498  uint64_t channel = thd_channel_layout_extract_channel(s->ch_layout,
499  ch_assign);
501  channel);
502  }
503  if (ch_assign > s->max_matrix_channel) {
505  "Assignment of matrix channel %d to invalid output channel %d.\n",
506  ch, ch_assign);
507  return AVERROR_PATCHWELCOME;
508  }
509  s->ch_assign[ch_assign] = ch;
510  }
511 
512  checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
513 
514  if (checksum != get_bits(gbp, 8))
515  av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
516 
517  /* Set default decoding parameters. */
518  s->param_presence_flags = 0xff;
519  s->num_primitive_matrices = 0;
520  s->blocksize = 8;
521  s->lossless_check_data = 0;
522 
523  memset(s->output_shift , 0, sizeof(s->output_shift ));
524  memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
525 
526  for (ch = s->min_channel; ch <= s->max_channel; ch++) {
527  ChannelParams *cp = &s->channel_params[ch];
528  cp->filter_params[FIR].order = 0;
529  cp->filter_params[IIR].order = 0;
530  cp->filter_params[FIR].shift = 0;
531  cp->filter_params[IIR].shift = 0;
532 
533  /* Default audio coding is 24-bit raw PCM. */
534  cp->huff_offset = 0;
535  cp->sign_huff_offset = (-1) << 23;
536  cp->codebook = 0;
537  cp->huff_lsbs = 24;
538  }
539 
540  if (substr == m->max_decoded_substream) {
541  m->avctx->channels = s->max_matrix_channel + 1;
542  m->avctx->channel_layout = s->ch_layout;
544  s->output_shift,
547  }
548 
549  return 0;
550 }
551 
555  unsigned int substr, unsigned int channel,
556  unsigned int filter)
557 {
558  SubStream *s = &m->substream[substr];
560  const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
561  const char fchar = filter ? 'I' : 'F';
562  int i, order;
563 
564  // Filter is 0 for FIR, 1 for IIR.
565  assert(filter < 2);
566 
567  if (m->filter_changed[channel][filter]++ > 1) {
568  av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
569  return AVERROR_INVALIDDATA;
570  }
571 
572  order = get_bits(gbp, 4);
573  if (order > max_order) {
575  "%cIR filter order %d is greater than maximum %d.\n",
576  fchar, order, max_order);
577  return AVERROR_INVALIDDATA;
578  }
579  fp->order = order;
580 
581  if (order > 0) {
582  int32_t *fcoeff = s->channel_params[channel].coeff[filter];
583  int coeff_bits, coeff_shift;
584 
585  fp->shift = get_bits(gbp, 4);
586 
587  coeff_bits = get_bits(gbp, 5);
588  coeff_shift = get_bits(gbp, 3);
589  if (coeff_bits < 1 || coeff_bits > 16) {
591  "%cIR filter coeff_bits must be between 1 and 16.\n",
592  fchar);
593  return AVERROR_INVALIDDATA;
594  }
595  if (coeff_bits + coeff_shift > 16) {
597  "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
598  fchar);
599  return AVERROR_INVALIDDATA;
600  }
601 
602  for (i = 0; i < order; i++)
603  fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
604 
605  if (get_bits1(gbp)) {
606  int state_bits, state_shift;
607 
608  if (filter == FIR) {
610  "FIR filter has state data specified.\n");
611  return AVERROR_INVALIDDATA;
612  }
613 
614  state_bits = get_bits(gbp, 4);
615  state_shift = get_bits(gbp, 4);
616 
617  /* TODO: Check validity of state data. */
618 
619  for (i = 0; i < order; i++)
620  fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
621  }
622  }
623 
624  return 0;
625 }
626 
629 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
630 {
631  SubStream *s = &m->substream[substr];
632  unsigned int mat, ch;
633  const int max_primitive_matrices = m->avctx->codec_id == AV_CODEC_ID_MLP
636 
637  if (m->matrix_changed++ > 1) {
638  av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
639  return AVERROR_INVALIDDATA;
640  }
641 
642  s->num_primitive_matrices = get_bits(gbp, 4);
643 
644  if (s->num_primitive_matrices > max_primitive_matrices) {
646  "Number of primitive matrices cannot be greater than %d.\n",
647  max_primitive_matrices);
648  return AVERROR_INVALIDDATA;
649  }
650 
651  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
652  int frac_bits, max_chan;
653  s->matrix_out_ch[mat] = get_bits(gbp, 4);
654  frac_bits = get_bits(gbp, 4);
655  s->lsb_bypass [mat] = get_bits1(gbp);
656 
657  if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
659  "Invalid channel %d specified as output from matrix.\n",
660  s->matrix_out_ch[mat]);
661  return AVERROR_INVALIDDATA;
662  }
663  if (frac_bits > 14) {
665  "Too many fractional bits specified.\n");
666  return AVERROR_INVALIDDATA;
667  }
668 
669  max_chan = s->max_matrix_channel;
670  if (!s->noise_type)
671  max_chan+=2;
672 
673  for (ch = 0; ch <= max_chan; ch++) {
674  int coeff_val = 0;
675  if (get_bits1(gbp))
676  coeff_val = get_sbits(gbp, frac_bits + 2);
677 
678  s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
679  }
680 
681  if (s->noise_type)
682  s->matrix_noise_shift[mat] = get_bits(gbp, 4);
683  else
684  s->matrix_noise_shift[mat] = 0;
685  }
686 
687  return 0;
688 }
689 
692 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
693  GetBitContext *gbp, unsigned int ch)
694 {
695  SubStream *s = &m->substream[substr];
696  ChannelParams *cp = &s->channel_params[ch];
697  FilterParams *fir = &cp->filter_params[FIR];
698  FilterParams *iir = &cp->filter_params[IIR];
699  int ret;
700 
702  if (get_bits1(gbp))
703  if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
704  return ret;
705 
707  if (get_bits1(gbp))
708  if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
709  return ret;
710 
711  if (fir->order + iir->order > 8) {
712  av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
713  return AVERROR_INVALIDDATA;
714  }
715 
716  if (fir->order && iir->order &&
717  fir->shift != iir->shift) {
719  "FIR and IIR filters must use the same precision.\n");
720  return AVERROR_INVALIDDATA;
721  }
722  /* The FIR and IIR filters must have the same precision.
723  * To simplify the filtering code, only the precision of the
724  * FIR filter is considered. If only the IIR filter is employed,
725  * the FIR filter precision is set to that of the IIR filter, so
726  * that the filtering code can use it. */
727  if (!fir->order && iir->order)
728  fir->shift = iir->shift;
729 
731  if (get_bits1(gbp))
732  cp->huff_offset = get_sbits(gbp, 15);
733 
734  cp->codebook = get_bits(gbp, 2);
735  cp->huff_lsbs = get_bits(gbp, 5);
736 
737  if (cp->huff_lsbs > 24) {
738  av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
739  return AVERROR_INVALIDDATA;
740  }
741 
742  cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
743 
744  return 0;
745 }
746 
751  unsigned int substr)
752 {
753  SubStream *s = &m->substream[substr];
754  unsigned int ch;
755  int ret;
756 
758  if (get_bits1(gbp))
759  s->param_presence_flags = get_bits(gbp, 8);
760 
762  if (get_bits1(gbp)) {
763  s->blocksize = get_bits(gbp, 9);
764  if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
765  av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
766  s->blocksize = 0;
767  return AVERROR_INVALIDDATA;
768  }
769  }
770 
772  if (get_bits1(gbp))
773  if ((ret = read_matrix_params(m, substr, gbp)) < 0)
774  return ret;
775 
777  if (get_bits1(gbp)) {
778  for (ch = 0; ch <= s->max_matrix_channel; ch++)
779  s->output_shift[ch] = get_sbits(gbp, 4);
780  if (substr == m->max_decoded_substream)
782  s->output_shift,
785  }
786 
788  if (get_bits1(gbp))
789  for (ch = 0; ch <= s->max_channel; ch++) {
790  ChannelParams *cp = &s->channel_params[ch];
791 
792  s->quant_step_size[ch] = get_bits(gbp, 4);
793 
794  cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
795  }
796 
797  for (ch = s->min_channel; ch <= s->max_channel; ch++)
798  if (get_bits1(gbp))
799  if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
800  return ret;
801 
802  return 0;
803 }
804 
805 #define MSB_MASK(bits) (-1u << bits)
806 
810 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
811  unsigned int channel)
812 {
813  SubStream *s = &m->substream[substr];
814  const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
816  int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
817  int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
818  FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
819  FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
820  unsigned int filter_shift = fir->shift;
821  int32_t mask = MSB_MASK(s->quant_step_size[channel]);
822 
823  memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
824  memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
825 
826  m->dsp.mlp_filter_channel(firbuf, fircoeff,
827  fir->order, iir->order,
828  filter_shift, mask, s->blocksize,
829  &m->sample_buffer[s->blockpos][channel]);
830 
831  memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
832  memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
833 }
834 
838  unsigned int substr)
839 {
840  SubStream *s = &m->substream[substr];
841  unsigned int i, ch, expected_stream_pos = 0;
842  int ret;
843 
844  if (s->data_check_present) {
845  expected_stream_pos = get_bits_count(gbp);
846  expected_stream_pos += get_bits(gbp, 16);
847  av_log_ask_for_sample(m->avctx, "This file contains some features "
848  "we have not tested yet.\n");
849  }
850 
851  if (s->blockpos + s->blocksize > m->access_unit_size) {
852  av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
853  return AVERROR_INVALIDDATA;
854  }
855 
856  memset(&m->bypassed_lsbs[s->blockpos][0], 0,
857  s->blocksize * sizeof(m->bypassed_lsbs[0]));
858 
859  for (i = 0; i < s->blocksize; i++)
860  if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
861  return ret;
862 
863  for (ch = s->min_channel; ch <= s->max_channel; ch++)
864  filter_channel(m, substr, ch);
865 
866  s->blockpos += s->blocksize;
867 
868  if (s->data_check_present) {
869  if (get_bits_count(gbp) != expected_stream_pos)
870  av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
871  skip_bits(gbp, 8);
872  }
873 
874  return 0;
875 }
876 
879 static const int8_t noise_table[256] = {
880  30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
881  52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
882  10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
883  51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
884  38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
885  61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
886  67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
887  48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
888  0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
889  16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
890  13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
891  89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
892  36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
893  39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
894  45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
895  -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
896 };
897 
908 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
909 {
910  SubStream *s = &m->substream[substr];
911  unsigned int i;
912  uint32_t seed = s->noisegen_seed;
913  unsigned int maxchan = s->max_matrix_channel;
914 
915  for (i = 0; i < s->blockpos; i++) {
916  uint16_t seed_shr7 = seed >> 7;
917  m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
918  m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift;
919 
920  seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
921  }
922 
923  s->noisegen_seed = seed;
924 }
925 
928 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
929 {
930  SubStream *s = &m->substream[substr];
931  unsigned int i;
932  uint32_t seed = s->noisegen_seed;
933 
934  for (i = 0; i < m->access_unit_size_pow2; i++) {
935  uint8_t seed_shr15 = seed >> 15;
936  m->noise_buffer[i] = noise_table[seed_shr15];
937  seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
938  }
939 
940  s->noisegen_seed = seed;
941 }
942 
943 
947 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
948 {
949  SubStream *s = &m->substream[substr];
950  unsigned int mat;
951  unsigned int maxchan;
952 
953  maxchan = s->max_matrix_channel;
954  if (!s->noise_type) {
955  generate_2_noise_channels(m, substr);
956  maxchan += 2;
957  } else {
958  fill_noise_buffer(m, substr);
959  }
960 
961  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
962  unsigned int dest_ch = s->matrix_out_ch[mat];
964  s->matrix_coeff[mat],
965  &m->bypassed_lsbs[0][mat],
966  m->noise_buffer,
967  s->num_primitive_matrices - mat,
968  dest_ch,
969  s->blockpos,
970  maxchan,
971  s->matrix_noise_shift[mat],
973  MSB_MASK(s->quant_step_size[dest_ch]));
974  }
975 }
976 
979 static int output_data(MLPDecodeContext *m, unsigned int substr,
980  void *data, int *got_frame_ptr)
981 {
982  AVCodecContext *avctx = m->avctx;
983  SubStream *s = &m->substream[substr];
984  int ret;
985  int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
986 
987  if (m->avctx->channels != s->max_matrix_channel + 1) {
988  av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
989  return AVERROR_INVALIDDATA;
990  }
991 
992  if (!s->blockpos) {
993  av_log(avctx, AV_LOG_ERROR, "No samples to output.\n");
994  return AVERROR_INVALIDDATA;
995  }
996 
997  /* get output buffer */
998  m->frame.nb_samples = s->blockpos;
999  if ((ret = ff_get_buffer(avctx, &m->frame)) < 0) {
1000  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1001  return ret;
1002  }
1004  s->blockpos,
1005  m->sample_buffer,
1006  m->frame.data[0],
1007  s->ch_assign,
1008  s->output_shift,
1009  s->max_matrix_channel,
1010  is32);
1011 
1012  *got_frame_ptr = 1;
1013  *(AVFrame *)data = m->frame;
1014 
1015  return 0;
1016 }
1017 
1022 static int read_access_unit(AVCodecContext *avctx, void* data,
1023  int *got_frame_ptr, AVPacket *avpkt)
1024 {
1025  const uint8_t *buf = avpkt->data;
1026  int buf_size = avpkt->size;
1027  MLPDecodeContext *m = avctx->priv_data;
1028  GetBitContext gb;
1029  unsigned int length, substr;
1030  unsigned int substream_start;
1031  unsigned int header_size = 4;
1032  unsigned int substr_header_size = 0;
1033  uint8_t substream_parity_present[MAX_SUBSTREAMS];
1034  uint16_t substream_data_len[MAX_SUBSTREAMS];
1035  uint8_t parity_bits;
1036  int ret;
1037 
1038  if (buf_size < 4)
1039  return 0;
1040 
1041  length = (AV_RB16(buf) & 0xfff) * 2;
1042 
1043  if (length < 4 || length > buf_size)
1044  return AVERROR_INVALIDDATA;
1045 
1046  init_get_bits(&gb, (buf + 4), (length - 4) * 8);
1047 
1048  m->is_major_sync_unit = 0;
1049  if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
1050  if (read_major_sync(m, &gb) < 0)
1051  goto error;
1052  m->is_major_sync_unit = 1;
1053  header_size += 28;
1054  }
1055 
1056  if (!m->params_valid) {
1058  "Stream parameters not seen; skipping frame.\n");
1059  *got_frame_ptr = 0;
1060  return length;
1061  }
1062 
1063  substream_start = 0;
1064 
1065  for (substr = 0; substr < m->num_substreams; substr++) {
1066  int extraword_present, checkdata_present, end, nonrestart_substr;
1067 
1068  extraword_present = get_bits1(&gb);
1069  nonrestart_substr = get_bits1(&gb);
1070  checkdata_present = get_bits1(&gb);
1071  skip_bits1(&gb);
1072 
1073  end = get_bits(&gb, 12) * 2;
1074 
1075  substr_header_size += 2;
1076 
1077  if (extraword_present) {
1078  if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
1079  av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
1080  goto error;
1081  }
1082  skip_bits(&gb, 16);
1083  substr_header_size += 2;
1084  }
1085 
1086  if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1087  av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
1088  goto error;
1089  }
1090 
1091  if (end + header_size + substr_header_size > length) {
1093  "Indicated length of substream %d data goes off end of "
1094  "packet.\n", substr);
1095  end = length - header_size - substr_header_size;
1096  }
1097 
1098  if (end < substream_start) {
1099  av_log(avctx, AV_LOG_ERROR,
1100  "Indicated end offset of substream %d data "
1101  "is smaller than calculated start offset.\n",
1102  substr);
1103  goto error;
1104  }
1105 
1106  if (substr > m->max_decoded_substream)
1107  continue;
1108 
1109  substream_parity_present[substr] = checkdata_present;
1110  substream_data_len[substr] = end - substream_start;
1111  substream_start = end;
1112  }
1113 
1114  parity_bits = ff_mlp_calculate_parity(buf, 4);
1115  parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1116 
1117  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1118  av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1119  goto error;
1120  }
1121 
1122  buf += header_size + substr_header_size;
1123 
1124  for (substr = 0; substr <= m->max_decoded_substream; substr++) {
1125  SubStream *s = &m->substream[substr];
1126  init_get_bits(&gb, buf, substream_data_len[substr] * 8);
1127 
1128  m->matrix_changed = 0;
1129  memset(m->filter_changed, 0, sizeof(m->filter_changed));
1130 
1131  s->blockpos = 0;
1132  do {
1133  if (get_bits1(&gb)) {
1134  if (get_bits1(&gb)) {
1135  /* A restart header should be present. */
1136  if (read_restart_header(m, &gb, buf, substr) < 0)
1137  goto next_substr;
1138  s->restart_seen = 1;
1139  }
1140 
1141  if (!s->restart_seen)
1142  goto next_substr;
1143  if (read_decoding_params(m, &gb, substr) < 0)
1144  goto next_substr;
1145  }
1146 
1147  if (!s->restart_seen)
1148  goto next_substr;
1149 
1150  if ((ret = read_block_data(m, &gb, substr)) < 0)
1151  return ret;
1152 
1153  if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1154  goto substream_length_mismatch;
1155 
1156  } while (!get_bits1(&gb));
1157 
1158  skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1159 
1160  if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1161  int shorten_by;
1162 
1163  if (get_bits(&gb, 16) != 0xD234)
1164  return AVERROR_INVALIDDATA;
1165 
1166  shorten_by = get_bits(&gb, 16);
1167  if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD && shorten_by & 0x2000)
1168  s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1169  else if (m->avctx->codec_id == AV_CODEC_ID_MLP && shorten_by != 0xD234)
1170  return AVERROR_INVALIDDATA;
1171 
1172  if (substr == m->max_decoded_substream)
1173  av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1174  }
1175 
1176  if (substream_parity_present[substr]) {
1177  uint8_t parity, checksum;
1178 
1179  if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1180  goto substream_length_mismatch;
1181 
1182  parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1183  checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
1184 
1185  if ((get_bits(&gb, 8) ^ parity) != 0xa9 )
1186  av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
1187  if ( get_bits(&gb, 8) != checksum)
1188  av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n" , substr);
1189  }
1190 
1191  if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1192  goto substream_length_mismatch;
1193 
1194 next_substr:
1195  if (!s->restart_seen)
1197  "No restart header present in substream %d.\n", substr);
1198 
1199  buf += substream_data_len[substr];
1200  }
1201 
1203 
1204  if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
1205  return ret;
1206 
1207  return length;
1208 
1209 substream_length_mismatch:
1210  av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1211  return AVERROR_INVALIDDATA;
1212 
1213 error:
1214  m->params_valid = 0;
1215  return AVERROR_INVALIDDATA;
1216 }
1217 
1219  .name = "mlp",
1220  .type = AVMEDIA_TYPE_AUDIO,
1221  .id = AV_CODEC_ID_MLP,
1222  .priv_data_size = sizeof(MLPDecodeContext),
1223  .init = mlp_decode_init,
1225  .capabilities = CODEC_CAP_DR1,
1226  .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1227 };
1228 
1229 #if CONFIG_TRUEHD_DECODER
1230 AVCodec ff_truehd_decoder = {
1231  .name = "truehd",
1232  .type = AVMEDIA_TYPE_AUDIO,
1233  .id = AV_CODEC_ID_TRUEHD,
1234  .priv_data_size = sizeof(MLPDecodeContext),
1235  .init = mlp_decode_init,
1237  .capabilities = CODEC_CAP_DR1,
1238  .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
1239 };
1240 #endif /* CONFIG_TRUEHD_DECODER */