FFmpeg
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * MLP decoder
25  */
26 
27 #include "config_components.h"
28 
29 #include <stdint.h>
30 
31 #include "avcodec.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mem_internal.h"
36 #include "libavutil/thread.h"
37 #include "libavutil/opt.h"
38 #include "codec_internal.h"
39 #include "decode.h"
40 #include "get_bits.h"
41 #include "mlp_parse.h"
42 #include "mlpdsp.h"
43 #include "mlp.h"
44 #include "config.h"
45 #include "profiles.h"
46 
47 /** number of bits used for VLC lookup - longest Huffman code is 9 */
48 #if ARCH_ARM
49 #define VLC_BITS 5
50 #define VLC_STATIC_SIZE 64
51 #else
52 #define VLC_BITS 9
53 #define VLC_STATIC_SIZE 512
54 #endif
55 
56 typedef struct SubStream {
57  /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
58  uint8_t restart_seen;
59  /// Set if end of stream is encountered
60  uint8_t end_of_stream;
61 
62  //@{
63  /** restart header data */
64  /// The type of noise to be used in the rematrix stage.
65  uint16_t noise_type;
66 
67  /// The index of the first channel coded in this substream.
68  uint8_t min_channel;
69  /// The index of the last channel coded in this substream.
70  uint8_t max_channel;
71  /// The coded channels mask in this substream.
72  uint64_t coded_channels;
73  /// The number of channels input into the rematrix stage.
75  /// For each channel output by the matrix, the output channel to map it to
77  /// The channel layout for this substream
78  uint64_t mask;
79  /// The matrix encoding mode for this substream
82 
83  /// Channel coding parameters for channels in the substream
85 
86  /// The left shift applied to random noise in 0x31ea substreams.
87  uint8_t noise_shift;
88  /// The current seed value for the pseudorandom noise generator(s).
89  uint32_t noisegen_seed;
90 
91  /// Set if the substream contains extra info to check the size of VLC blocks.
93 
94  /// Bitmask of which parameter sets are conveyed in a decoding parameter block.
96  //@}
97 
98  //@{
99  /** matrix data */
100 
101  /// Number of matrices to be applied.
103 
104  /// matrix output channel
106 
107  /// Whether the LSBs of the matrix output are encoded in the bitstream.
109  /// Matrix coefficients, stored as 2.14 fixed point.
111  /// Left shift to apply to noise values in 0x31eb substreams.
113  //@}
114 
115  /// Left shift to apply to Huffman-decoded residuals.
117 
118  /// number of PCM samples in current audio block
119  uint16_t blocksize;
120  /// Number of PCM samples decoded so far in this frame.
121  uint16_t blockpos;
122 
123  /// Left shift to apply to decoded PCM values to get final 24-bit output.
125 
126  /// Running XOR of all output samples.
128 
129 } SubStream;
130 
131 typedef struct MLPDecodeContext {
132  const AVClass *class;
134 
136 
137  /// Current access unit being read has a major sync.
139 
140  /// Size of the major sync unit, in bytes
142 
143  /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
144  uint8_t params_valid;
145 
146  /// Number of substreams contained within this stream.
147  uint8_t num_substreams;
148 
149  /// Which substream of substreams carry 16-channel presentation
151 
152  /// Which substream of substreams carry 2/6/8-channel presentation
153  uint8_t substream_info;
154 
155  /// Index of the last substream to decode - further substreams are skipped.
157 
158  /// Stream needs channel reordering to comply with FFmpeg's channel order
160 
161  /// number of PCM samples contained in each frame
163  /// next power of two above the number of samples in each frame
165 
167 
170 
174 
177 
178 static const enum AVChannel thd_channel_order[] = {
181  AV_CHAN_LOW_FREQUENCY, // LFE
186  AV_CHAN_BACK_CENTER, // Cs
187  AV_CHAN_TOP_CENTER, // Ts
191  AV_CHAN_LOW_FREQUENCY_2, // LFE2
192 };
193 
195 {
198  av_channel_layout_subset(layout, UINT64_MAX);
199 }
200 
201 static enum AVChannel thd_channel_layout_extract_channel(uint64_t channel_layout,
202  int index)
203 {
204  int i;
205 
206  if (av_popcount64(channel_layout) <= index)
207  return AV_CHAN_NONE;
208 
209  for (i = 0; i < FF_ARRAY_ELEMS(thd_channel_order); i++)
210  if (channel_layout & (1ULL << thd_channel_order[i]) && !index--)
211  return thd_channel_order[i];
212  return AV_CHAN_NONE;
213 }
214 
215 static VLC huff_vlc[3];
216 
217 /** Initialize static data, constant between all invocations of the codec. */
218 
219 static av_cold void init_static(void)
220 {
221  for (int i = 0; i < 3; i++) {
222  static VLCElem vlc_buf[3 * VLC_STATIC_SIZE];
225  vlc_init(&huff_vlc[i], VLC_BITS, 18,
226  &ff_mlp_huffman_tables[i][0][1], 2, 1,
228  }
229 
230  ff_mlp_init_crc();
231 }
232 
234  unsigned int substr, unsigned int ch)
235 {
236  SubStream *s = &m->substream[substr];
237  ChannelParams *cp = &s->channel_params[ch];
238  int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
239  int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
240  int32_t sign_huff_offset = cp->huff_offset;
241 
242  if (cp->codebook > 0)
243  sign_huff_offset -= 7 << lsb_bits;
244 
245  if (sign_shift >= 0)
246  sign_huff_offset -= 1 << sign_shift;
247 
248  return sign_huff_offset;
249 }
250 
251 /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
252  * and plain LSBs. */
253 
255  unsigned int substr, unsigned int pos)
256 {
257  SubStream *s = &m->substream[substr];
258  unsigned int mat, channel;
259 
260  for (mat = 0; mat < s->num_primitive_matrices; mat++)
261  if (s->lsb_bypass[mat])
262  m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
263 
264  for (channel = s->min_channel; channel <= s->max_channel; channel++) {
265  ChannelParams *cp = &s->channel_params[channel];
266  int codebook = cp->codebook;
267  int quant_step_size = s->quant_step_size[channel];
268  int lsb_bits = cp->huff_lsbs - quant_step_size;
269  int result = 0;
270 
271  if (codebook > 0)
273  VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
274 
275  if (result < 0)
276  return AVERROR_INVALIDDATA;
277 
278  if (lsb_bits > 0)
279  result = (result << lsb_bits) + get_bits_long(gbp, lsb_bits);
280 
281  result += cp->sign_huff_offset;
282  result *= 1 << quant_step_size;
283 
284  m->sample_buffer[pos + s->blockpos][channel] = result;
285  }
286 
287  return 0;
288 }
289 
291 {
292  static AVOnce init_static_once = AV_ONCE_INIT;
293  MLPDecodeContext *m = avctx->priv_data;
294  int substr;
295 
296  m->avctx = avctx;
297  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
298  m->substream[substr].lossless_check_data = 0xffffffff;
299  ff_mlpdsp_init(&m->dsp);
300 
301 #if FF_API_OLD_CHANNEL_LAYOUT
303  if (avctx->request_channel_layout) {
305  av_channel_layout_from_mask(&m->downmix_layout, avctx->request_channel_layout);
306  }
308 #endif
309  ff_thread_once(&init_static_once, init_static);
310 
311  return 0;
312 }
313 
314 /** Read a major sync info header - contains high level information about
315  * the stream - sample rate, channel arrangement etc. Most of this
316  * information is not actually necessary for decoding, only for playback.
317  */
318 
320 {
322  int substr, ret;
323 
324  if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
325  return ret;
326 
327  if (mh.group1_bits == 0) {
328  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
329  return AVERROR_INVALIDDATA;
330  }
331  if (mh.group2_bits > mh.group1_bits) {
333  "Channel group 2 cannot have more bits per sample than group 1.\n");
334  return AVERROR_INVALIDDATA;
335  }
336 
337  if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
339  "Channel groups with differing sample rates are not currently supported.\n");
340  return AVERROR_INVALIDDATA;
341  }
342 
343  if (mh.group1_samplerate == 0) {
344  av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
345  return AVERROR_INVALIDDATA;
346  }
347  if (mh.group1_samplerate > MAX_SAMPLERATE) {
349  "Sampling rate %d is greater than the supported maximum (%d).\n",
350  mh.group1_samplerate, MAX_SAMPLERATE);
351  return AVERROR_INVALIDDATA;
352  }
353  if (mh.access_unit_size > MAX_BLOCKSIZE) {
355  "Block size %d is greater than the supported maximum (%d).\n",
356  mh.access_unit_size, MAX_BLOCKSIZE);
357  return AVERROR_INVALIDDATA;
358  }
359  if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
361  "Block size pow2 %d is greater than the supported maximum (%d).\n",
362  mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
363  return AVERROR_INVALIDDATA;
364  }
365 
366  if (mh.num_substreams == 0)
367  return AVERROR_INVALIDDATA;
368  if (m->avctx->codec_id == AV_CODEC_ID_MLP && mh.num_substreams > 2) {
369  av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
370  return AVERROR_INVALIDDATA;
371  }
372  if (mh.num_substreams > MAX_SUBSTREAMS) {
374  "%d substreams (more than the "
375  "maximum supported by the decoder)",
376  mh.num_substreams);
377  return AVERROR_PATCHWELCOME;
378  }
379 
380  m->major_sync_header_size = mh.header_size;
381 
382  m->access_unit_size = mh.access_unit_size;
383  m->access_unit_size_pow2 = mh.access_unit_size_pow2;
384 
385  m->num_substreams = mh.num_substreams;
386  m->extended_substream_info = mh.extended_substream_info;
387  m->substream_info = mh.substream_info;
388 
389  /* If there is a 4th substream and the MSB of substream_info is set,
390  * there is a 16-channel spatial presentation (Atmos in TrueHD).
391  */
393  && m->num_substreams == 4 && m->substream_info >> 7 == 1) {
395  }
396 
397  /* limit to decoding 3 substreams, as the 4th is used by Dolby Atmos for non-audio data */
399 
400  m->avctx->sample_rate = mh.group1_samplerate;
401  m->avctx->frame_size = mh.access_unit_size;
402 
403  m->avctx->bits_per_raw_sample = mh.group1_bits;
404  if (mh.group1_bits > 16)
406  else
412 
413  m->params_valid = 1;
414  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
415  m->substream[substr].restart_seen = 0;
416 
417  /* Set the layout for each substream. When there's more than one, the first
418  * substream is Stereo. Subsequent substreams' layouts are indicated in the
419  * major sync. */
420  if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
421  if (mh.stream_type != SYNC_MLP) {
423  "unexpected stream_type %X in MLP",
424  mh.stream_type);
425  return AVERROR_PATCHWELCOME;
426  }
427  if ((substr = (mh.num_substreams > 1)))
429  m->substream[substr].mask = mh.channel_layout_mlp;
430  } else {
431  if (mh.stream_type != SYNC_TRUEHD) {
433  "unexpected stream_type %X in !MLP",
434  mh.stream_type);
435  return AVERROR_PATCHWELCOME;
436  }
437  m->substream[1].mask = mh.channel_layout_thd_stream1;
438  if (mh.channels_thd_stream1 == 2 &&
439  mh.channels_thd_stream2 == 2 &&
440  m->avctx->ch_layout.nb_channels == 2)
442  if ((substr = (mh.num_substreams > 1)))
444  if (mh.num_substreams == 1 &&
445  mh.channels_thd_stream1 == 1 &&
446  mh.channels_thd_stream2 == 1 &&
447  m->avctx->ch_layout.nb_channels == 1)
449  if (mh.num_substreams > 2)
450  if (mh.channel_layout_thd_stream2)
451  m->substream[2].mask = mh.channel_layout_thd_stream2;
452  else
453  m->substream[2].mask = mh.channel_layout_thd_stream1;
454  if (m->avctx->ch_layout.nb_channels > 2)
455  if (mh.num_substreams > 2)
456  m->substream[1].mask = mh.channel_layout_thd_stream1;
457  else
458  m->substream[mh.num_substreams > 1].mask = mh.channel_layout_thd_stream2;
459  }
460 
461  m->needs_reordering = mh.channel_arrangement >= 18 && mh.channel_arrangement <= 20;
462 
463  /* Parse the TrueHD decoder channel modifiers and set each substream's
464  * AVMatrixEncoding accordingly.
465  *
466  * The meaning of the modifiers depends on the channel layout:
467  *
468  * - THD_CH_MODIFIER_LTRT, THD_CH_MODIFIER_LBINRBIN only apply to 2-channel
469  *
470  * - THD_CH_MODIFIER_MONO applies to 1-channel or 2-channel (dual mono)
471  *
472  * - THD_CH_MODIFIER_SURROUNDEX, THD_CH_MODIFIER_NOTSURROUNDEX only apply to
473  * layouts with an Ls/Rs channel pair
474  */
475  for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
478  if (mh.num_substreams > 2 &&
479  mh.channel_layout_thd_stream2 & AV_CH_SIDE_LEFT &&
480  mh.channel_layout_thd_stream2 & AV_CH_SIDE_RIGHT &&
481  mh.channel_modifier_thd_stream2 == THD_CH_MODIFIER_SURROUNDEX)
483 
484  if (mh.num_substreams > 1 &&
485  mh.channel_layout_thd_stream1 & AV_CH_SIDE_LEFT &&
486  mh.channel_layout_thd_stream1 & AV_CH_SIDE_RIGHT &&
487  mh.channel_modifier_thd_stream1 == THD_CH_MODIFIER_SURROUNDEX)
489 
490  if (mh.num_substreams > 0)
491  switch (mh.channel_modifier_thd_stream0) {
494  break;
497  break;
498  default:
499  break;
500  }
501  }
502 
503  return 0;
504 }
505 
506 /** Read a restart header from a block in a substream. This contains parameters
507  * required to decode the audio that do not change very often. Generally
508  * (always) present only in blocks following a major sync. */
509 
511  const uint8_t *buf, unsigned int substr)
512 {
513  SubStream *s = &m->substream[substr];
514  unsigned int ch;
515  int sync_word, tmp;
516  uint8_t checksum;
517  uint8_t lossless_check;
518  int start_count = get_bits_count(gbp);
519  int min_channel, max_channel, max_matrix_channel, noise_type;
520  const int std_max_matrix_channel = m->avctx->codec_id == AV_CODEC_ID_MLP
523 
524  sync_word = get_bits(gbp, 13);
525 
526  if (sync_word != 0x31ea >> 1) {
528  "restart header sync incorrect (got 0x%04x)\n", sync_word);
529  return AVERROR_INVALIDDATA;
530  }
531 
532  noise_type = get_bits1(gbp);
533 
534  if (m->avctx->codec_id == AV_CODEC_ID_MLP && noise_type) {
535  av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
536  return AVERROR_INVALIDDATA;
537  }
538 
539  skip_bits(gbp, 16); /* Output timestamp */
540 
541  min_channel = get_bits(gbp, 4);
542  max_channel = get_bits(gbp, 4);
543  max_matrix_channel = get_bits(gbp, 4);
544 
545  if (max_matrix_channel > std_max_matrix_channel) {
547  "Max matrix channel cannot be greater than %d.\n",
548  std_max_matrix_channel);
549  return AVERROR_INVALIDDATA;
550  }
551 
552  /* This should happen for TrueHD streams with >6 channels and MLP's noise
553  * type. It is not yet known if this is allowed. */
554  if (max_matrix_channel > MAX_MATRIX_CHANNEL_MLP && !noise_type) {
556  "%d channels (more than the "
557  "maximum supported by the decoder)",
558  max_channel + 2);
559  return AVERROR_PATCHWELCOME;
560  }
561 
562  if (max_channel + 1 > MAX_CHANNELS || max_channel + 1 < min_channel)
563  return AVERROR_INVALIDDATA;
564 
565  s->min_channel = min_channel;
566  s->max_channel = max_channel;
567  s->coded_channels = ((1LL << (max_channel - min_channel + 1)) - 1) << min_channel;
568  s->max_matrix_channel = max_matrix_channel;
569  s->noise_type = noise_type;
570 
571  if (mlp_channel_layout_subset(&m->downmix_layout, s->mask) &&
572  m->max_decoded_substream > substr) {
574  "Extracting %d-channel downmix (0x%"PRIx64") from substream %d. "
575  "Further substreams will be skipped.\n",
576  s->max_channel + 1, s->mask, substr);
577  m->max_decoded_substream = substr;
578  }
579 
580  s->noise_shift = get_bits(gbp, 4);
581  s->noisegen_seed = get_bits(gbp, 23);
582 
583  skip_bits(gbp, 19);
584 
585  s->data_check_present = get_bits1(gbp);
586  lossless_check = get_bits(gbp, 8);
587  if (substr == m->max_decoded_substream
588  && s->lossless_check_data != 0xffffffff) {
589  tmp = xor_32_to_8(s->lossless_check_data);
590  if (tmp != lossless_check)
592  "Lossless check failed - expected %02x, calculated %02x.\n",
593  lossless_check, tmp);
594  }
595 
596  skip_bits(gbp, 16);
597 
598  memset(s->ch_assign, 0, sizeof(s->ch_assign));
599 
600  for (ch = 0; ch <= s->max_matrix_channel; ch++) {
601  int ch_assign = get_bits(gbp, 6);
602  if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
603  AVChannelLayout l;
604  enum AVChannel channel = thd_channel_layout_extract_channel(s->mask, ch_assign);
605 
606  av_channel_layout_from_mask(&l, s->mask);
608  }
609  if (ch_assign < 0 || ch_assign > s->max_matrix_channel) {
611  "Assignment of matrix channel %d to invalid output channel %d",
612  ch, ch_assign);
613  return AVERROR_PATCHWELCOME;
614  }
615  s->ch_assign[ch_assign] = ch;
616  }
617 
618  checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
619 
620  if (checksum != get_bits(gbp, 8))
621  av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
622 
623  /* Set default decoding parameters. */
624  s->param_presence_flags = 0xff;
625  s->num_primitive_matrices = 0;
626  s->blocksize = 8;
627  s->lossless_check_data = 0;
628 
629  memset(s->output_shift , 0, sizeof(s->output_shift ));
630  memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
631 
632  for (ch = s->min_channel; ch <= s->max_channel; ch++) {
633  ChannelParams *cp = &s->channel_params[ch];
634  cp->filter_params[FIR].order = 0;
635  cp->filter_params[IIR].order = 0;
636  cp->filter_params[FIR].shift = 0;
637  cp->filter_params[IIR].shift = 0;
638 
639  /* Default audio coding is 24-bit raw PCM. */
640  cp->huff_offset = 0;
641  cp->sign_huff_offset = -(1 << 23);
642  cp->codebook = 0;
643  cp->huff_lsbs = 24;
644  }
645 
646  if (substr == m->max_decoded_substream) {
649  m->dsp.mlp_pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,
650  s->output_shift,
651  s->max_matrix_channel,
653 
654  if (m->avctx->codec_id == AV_CODEC_ID_MLP && m->needs_reordering) {
655  if (s->mask == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
656  s->mask == AV_CH_LAYOUT_5POINT0_BACK) {
657  int i = s->ch_assign[4];
658  s->ch_assign[4] = s->ch_assign[3];
659  s->ch_assign[3] = s->ch_assign[2];
660  s->ch_assign[2] = i;
661  } else if (s->mask == AV_CH_LAYOUT_5POINT1_BACK) {
662  FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
663  FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
664  }
665  }
666 
667  }
668 
669  return 0;
670 }
671 
672 /** Read parameters for one of the prediction filters. */
673 
675  unsigned int substr, unsigned int channel,
676  unsigned int filter)
677 {
678  SubStream *s = &m->substream[substr];
679  FilterParams *fp = &s->channel_params[channel].filter_params[filter];
680  const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
681  const char fchar = filter ? 'I' : 'F';
682  int i, order;
683 
684  // Filter is 0 for FIR, 1 for IIR.
685  av_assert0(filter < 2);
686 
687  if (m->filter_changed[channel][filter]++ > 1) {
688  av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
689  return AVERROR_INVALIDDATA;
690  }
691 
692  order = get_bits(gbp, 4);
693  if (order > max_order) {
695  "%cIR filter order %d is greater than maximum %d.\n",
696  fchar, order, max_order);
697  return AVERROR_INVALIDDATA;
698  }
699  fp->order = order;
700 
701  if (order > 0) {
702  int32_t *fcoeff = s->channel_params[channel].coeff[filter];
703  int coeff_bits, coeff_shift;
704 
705  fp->shift = get_bits(gbp, 4);
706 
707  coeff_bits = get_bits(gbp, 5);
708  coeff_shift = get_bits(gbp, 3);
709  if (coeff_bits < 1 || coeff_bits > 16) {
711  "%cIR filter coeff_bits must be between 1 and 16.\n",
712  fchar);
713  return AVERROR_INVALIDDATA;
714  }
715  if (coeff_bits + coeff_shift > 16) {
717  "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
718  fchar);
719  return AVERROR_INVALIDDATA;
720  }
721 
722  for (i = 0; i < order; i++)
723  fcoeff[i] = get_sbits(gbp, coeff_bits) * (1 << coeff_shift);
724 
725  if (get_bits1(gbp)) {
726  int state_bits, state_shift;
727 
728  if (filter == FIR) {
730  "FIR filter has state data specified.\n");
731  return AVERROR_INVALIDDATA;
732  }
733 
734  state_bits = get_bits(gbp, 4);
735  state_shift = get_bits(gbp, 4);
736 
737  /* TODO: Check validity of state data. */
738 
739  for (i = 0; i < order; i++)
740  fp->state[i] = state_bits ? get_sbits(gbp, state_bits) * (1 << state_shift) : 0;
741  }
742  }
743 
744  return 0;
745 }
746 
747 /** Read parameters for primitive matrices. */
748 
749 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
750 {
751  SubStream *s = &m->substream[substr];
752  unsigned int mat, ch;
753  const int max_primitive_matrices = m->avctx->codec_id == AV_CODEC_ID_MLP
756 
757  if (m->matrix_changed++ > 1) {
758  av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
759  return AVERROR_INVALIDDATA;
760  }
761 
762  s->num_primitive_matrices = get_bits(gbp, 4);
763 
764  if (s->num_primitive_matrices > max_primitive_matrices) {
766  "Number of primitive matrices cannot be greater than %d.\n",
767  max_primitive_matrices);
768  goto error;
769  }
770 
771  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
772  int frac_bits, max_chan;
773  s->matrix_out_ch[mat] = get_bits(gbp, 4);
774  frac_bits = get_bits(gbp, 4);
775  s->lsb_bypass [mat] = get_bits1(gbp);
776 
777  if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
779  "Invalid channel %d specified as output from matrix.\n",
780  s->matrix_out_ch[mat]);
781  goto error;
782  }
783  if (frac_bits > 14) {
785  "Too many fractional bits specified.\n");
786  goto error;
787  }
788 
789  max_chan = s->max_matrix_channel;
790  if (!s->noise_type)
791  max_chan+=2;
792 
793  for (ch = 0; ch <= max_chan; ch++) {
794  int coeff_val = 0;
795  if (get_bits1(gbp))
796  coeff_val = get_sbits(gbp, frac_bits + 2);
797 
798  s->matrix_coeff[mat][ch] = coeff_val * (1 << (14 - frac_bits));
799  }
800 
801  if (s->noise_type)
802  s->matrix_noise_shift[mat] = get_bits(gbp, 4);
803  else
804  s->matrix_noise_shift[mat] = 0;
805  }
806 
807  return 0;
808 error:
809  s->num_primitive_matrices = 0;
810  memset(s->matrix_out_ch, 0, sizeof(s->matrix_out_ch));
811 
812  return AVERROR_INVALIDDATA;
813 }
814 
815 /** Read channel parameters. */
816 
817 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
818  GetBitContext *gbp, unsigned int ch)
819 {
820  SubStream *s = &m->substream[substr];
821  ChannelParams *cp = &s->channel_params[ch];
822  FilterParams *fir = &cp->filter_params[FIR];
823  FilterParams *iir = &cp->filter_params[IIR];
824  int ret;
825 
826  if (s->param_presence_flags & PARAM_FIR)
827  if (get_bits1(gbp))
828  if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
829  return ret;
830 
831  if (s->param_presence_flags & PARAM_IIR)
832  if (get_bits1(gbp))
833  if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
834  return ret;
835 
836  if (fir->order + iir->order > 8) {
837  av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
838  return AVERROR_INVALIDDATA;
839  }
840 
841  if (fir->order && iir->order &&
842  fir->shift != iir->shift) {
844  "FIR and IIR filters must use the same precision.\n");
845  return AVERROR_INVALIDDATA;
846  }
847  /* The FIR and IIR filters must have the same precision.
848  * To simplify the filtering code, only the precision of the
849  * FIR filter is considered. If only the IIR filter is employed,
850  * the FIR filter precision is set to that of the IIR filter, so
851  * that the filtering code can use it. */
852  if (!fir->order && iir->order)
853  fir->shift = iir->shift;
854 
855  if (s->param_presence_flags & PARAM_HUFFOFFSET)
856  if (get_bits1(gbp))
857  cp->huff_offset = get_sbits(gbp, 15);
858 
859  cp->codebook = get_bits(gbp, 2);
860  cp->huff_lsbs = get_bits(gbp, 5);
861 
862  if (cp->codebook > 0 && cp->huff_lsbs > 24) {
863  av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
864  cp->huff_lsbs = 0;
865  return AVERROR_INVALIDDATA;
866  }
867 
868  return 0;
869 }
870 
871 /** Read decoding parameters that change more often than those in the restart
872  * header. */
873 
875  unsigned int substr)
876 {
877  SubStream *s = &m->substream[substr];
878  unsigned int ch;
879  int ret = 0;
880  unsigned recompute_sho = 0;
881 
882  if (s->param_presence_flags & PARAM_PRESENCE)
883  if (get_bits1(gbp))
884  s->param_presence_flags = get_bits(gbp, 8);
885 
886  if (s->param_presence_flags & PARAM_BLOCKSIZE)
887  if (get_bits1(gbp)) {
888  s->blocksize = get_bits(gbp, 9);
889  if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
890  av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.\n");
891  s->blocksize = 0;
892  return AVERROR_INVALIDDATA;
893  }
894  }
895 
896  if (s->param_presence_flags & PARAM_MATRIX)
897  if (get_bits1(gbp))
898  if ((ret = read_matrix_params(m, substr, gbp)) < 0)
899  return ret;
900 
901  if (s->param_presence_flags & PARAM_OUTSHIFT)
902  if (get_bits1(gbp)) {
903  for (ch = 0; ch <= s->max_matrix_channel; ch++) {
904  s->output_shift[ch] = get_sbits(gbp, 4);
905  if (s->output_shift[ch] < 0) {
906  avpriv_request_sample(m->avctx, "Negative output_shift");
907  s->output_shift[ch] = 0;
908  }
909  }
910  if (substr == m->max_decoded_substream)
911  m->dsp.mlp_pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,
912  s->output_shift,
913  s->max_matrix_channel,
915  }
916 
917  if (s->param_presence_flags & PARAM_QUANTSTEP)
918  if (get_bits1(gbp))
919  for (ch = 0; ch <= s->max_channel; ch++) {
920  s->quant_step_size[ch] = get_bits(gbp, 4);
921 
922  recompute_sho |= 1<<ch;
923  }
924 
925  for (ch = s->min_channel; ch <= s->max_channel; ch++)
926  if (get_bits1(gbp)) {
927  recompute_sho |= 1<<ch;
928  if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
929  goto fail;
930  }
931 
932 
933 fail:
934  for (ch = 0; ch <= s->max_channel; ch++) {
935  if (recompute_sho & (1<<ch)) {
936  ChannelParams *cp = &s->channel_params[ch];
937 
938  if (cp->codebook > 0 && cp->huff_lsbs < s->quant_step_size[ch]) {
939  if (ret >= 0) {
940  av_log(m->avctx, AV_LOG_ERROR, "quant_step_size larger than huff_lsbs\n");
942  }
943  s->quant_step_size[ch] = 0;
944  }
945 
946  cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
947  }
948  }
949  return ret;
950 }
951 
952 #define MSB_MASK(bits) (-(1 << (bits)))
953 
954 /** Generate PCM samples using the prediction filters and residual values
955  * read from the data stream, and update the filter state. */
956 
957 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
958  unsigned int channel)
959 {
960  SubStream *s = &m->substream[substr];
961  const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
963  int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
964  int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
965  FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
966  FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
967  unsigned int filter_shift = fir->shift;
968  int32_t mask = MSB_MASK(s->quant_step_size[channel]);
969 
970  memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
971  memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
972 
973  m->dsp.mlp_filter_channel(firbuf, fircoeff,
974  fir->order, iir->order,
975  filter_shift, mask, s->blocksize,
976  &m->sample_buffer[s->blockpos][channel]);
977 
978  memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
979  memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
980 }
981 
982 /** Read a block of PCM residual data (or actual if no filtering active). */
983 
985  unsigned int substr)
986 {
987  SubStream *s = &m->substream[substr];
988  unsigned int i, ch, expected_stream_pos = 0;
989  int ret;
990 
991  if (s->data_check_present) {
992  expected_stream_pos = get_bits_count(gbp);
993  expected_stream_pos += get_bits(gbp, 16);
995  "Substreams with VLC block size check info");
996  }
997 
998  if (s->blockpos + s->blocksize > m->access_unit_size) {
999  av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
1000  return AVERROR_INVALIDDATA;
1001  }
1002 
1003  memset(&m->bypassed_lsbs[s->blockpos][0], 0,
1004  s->blocksize * sizeof(m->bypassed_lsbs[0]));
1005 
1006  for (i = 0; i < s->blocksize; i++)
1007  if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
1008  return ret;
1009 
1010  for (ch = s->min_channel; ch <= s->max_channel; ch++)
1011  filter_channel(m, substr, ch);
1012 
1013  s->blockpos += s->blocksize;
1014 
1015  if (s->data_check_present) {
1016  if (get_bits_count(gbp) != expected_stream_pos)
1017  av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
1018  skip_bits(gbp, 8);
1019  }
1020 
1021  return 0;
1022 }
1023 
1024 /** Data table used for TrueHD noise generation function. */
1025 
1026 static const int8_t noise_table[256] = {
1027  30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
1028  52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
1029  10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
1030  51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
1031  38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
1032  61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
1033  67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
1034  48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
1035  0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
1036  16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
1037  13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
1038  89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
1039  36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
1040  39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
1041  45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
1042  -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
1043 };
1044 
1045 /** Noise generation functions.
1046  * I'm not sure what these are for - they seem to be some kind of pseudorandom
1047  * sequence generators, used to generate noise data which is used when the
1048  * channels are rematrixed. I'm not sure if they provide a practical benefit
1049  * to compression, or just obfuscate the decoder. Are they for some kind of
1050  * dithering? */
1051 
1052 /** Generate two channels of noise, used in the matrix when
1053  * restart sync word == 0x31ea. */
1054 
1055 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
1056 {
1057  SubStream *s = &m->substream[substr];
1058  unsigned int i;
1059  uint32_t seed = s->noisegen_seed;
1060  unsigned int maxchan = s->max_matrix_channel;
1061 
1062  for (i = 0; i < s->blockpos; i++) {
1063  uint16_t seed_shr7 = seed >> 7;
1064  m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) * (1 << s->noise_shift);
1065  m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) * (1 << s->noise_shift);
1066 
1067  seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1068  }
1069 
1070  s->noisegen_seed = seed;
1071 }
1072 
1073 /** Generate a block of noise, used when restart sync word == 0x31eb. */
1074 
1075 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
1076 {
1077  SubStream *s = &m->substream[substr];
1078  unsigned int i;
1079  uint32_t seed = s->noisegen_seed;
1080 
1081  for (i = 0; i < m->access_unit_size_pow2; i++) {
1082  uint8_t seed_shr15 = seed >> 15;
1083  m->noise_buffer[i] = noise_table[seed_shr15];
1084  seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
1085  }
1086 
1087  s->noisegen_seed = seed;
1088 }
1089 
1090 /** Write the audio data into the output buffer. */
1091 
1092 static int output_data(MLPDecodeContext *m, unsigned int substr,
1093  AVFrame *frame, int *got_frame_ptr)
1094 {
1095  AVCodecContext *avctx = m->avctx;
1096  SubStream *s = &m->substream[substr];
1097  unsigned int mat;
1098  unsigned int maxchan;
1099  int ret;
1100  int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
1101 
1102  if (m->avctx->ch_layout.nb_channels != s->max_matrix_channel + 1) {
1103  av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
1104  return AVERROR_INVALIDDATA;
1105  }
1106 
1107  if (!s->blockpos) {
1108  av_log(avctx, AV_LOG_ERROR, "No samples to output.\n");
1109  return AVERROR_INVALIDDATA;
1110  }
1111 
1112  maxchan = s->max_matrix_channel;
1113  if (!s->noise_type) {
1114  generate_2_noise_channels(m, substr);
1115  maxchan += 2;
1116  } else {
1117  fill_noise_buffer(m, substr);
1118  }
1119 
1120  /* Apply the channel matrices in turn to reconstruct the original audio
1121  * samples. */
1122  for (mat = 0; mat < s->num_primitive_matrices; mat++) {
1123  unsigned int dest_ch = s->matrix_out_ch[mat];
1124  m->dsp.mlp_rematrix_channel(&m->sample_buffer[0][0],
1125  s->matrix_coeff[mat],
1126  &m->bypassed_lsbs[0][mat],
1127  m->noise_buffer,
1128  s->num_primitive_matrices - mat,
1129  dest_ch,
1130  s->blockpos,
1131  maxchan,
1132  s->matrix_noise_shift[mat],
1134  MSB_MASK(s->quant_step_size[dest_ch]));
1135  }
1136 
1137  /* get output buffer */
1138  frame->nb_samples = s->blockpos;
1139  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1140  return ret;
1141  s->lossless_check_data = m->dsp.mlp_pack_output(s->lossless_check_data,
1142  s->blockpos,
1143  m->sample_buffer,
1144  frame->data[0],
1145  s->ch_assign,
1146  s->output_shift,
1147  s->max_matrix_channel,
1148  is32);
1149 
1150  /* Update matrix encoding side data */
1151  if (s->matrix_encoding != s->prev_matrix_encoding) {
1152  if ((ret = ff_side_data_update_matrix_encoding(frame, s->matrix_encoding)) < 0)
1153  return ret;
1154 
1155  s->prev_matrix_encoding = s->matrix_encoding;
1156  }
1157 
1158  *got_frame_ptr = 1;
1159 
1160  return 0;
1161 }
1162 
1163 /** Read an access unit from the stream.
1164  * @return negative on error, 0 if not enough data is present in the input stream,
1165  * otherwise the number of bytes consumed. */
1166 
1168  int *got_frame_ptr, AVPacket *avpkt)
1169 {
1170  const uint8_t *buf = avpkt->data;
1171  int buf_size = avpkt->size;
1172  MLPDecodeContext *m = avctx->priv_data;
1173  GetBitContext gb;
1174  unsigned int length, substr;
1175  unsigned int substream_start;
1176  unsigned int header_size = 4;
1177  unsigned int substr_header_size = 0;
1178  uint8_t substream_parity_present[MAX_SUBSTREAMS];
1179  uint16_t substream_data_len[MAX_SUBSTREAMS];
1180  uint8_t parity_bits;
1181  int ret;
1182 
1183  if (buf_size < 4)
1184  return AVERROR_INVALIDDATA;
1185 
1186  length = (AV_RB16(buf) & 0xfff) * 2;
1187 
1188  if (length < 4 || length > buf_size)
1189  return AVERROR_INVALIDDATA;
1190 
1191  init_get_bits(&gb, (buf + 4), (length - 4) * 8);
1192 
1193  m->is_major_sync_unit = 0;
1194  if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
1195  if (read_major_sync(m, &gb) < 0)
1196  goto error;
1197  m->is_major_sync_unit = 1;
1198  header_size += m->major_sync_header_size;
1199  }
1200 
1201  if (!m->params_valid) {
1203  "Stream parameters not seen; skipping frame.\n");
1204  *got_frame_ptr = 0;
1205  return length;
1206  }
1207 
1208  substream_start = 0;
1209 
1210  for (substr = 0; substr < m->num_substreams; substr++) {
1211  int extraword_present, checkdata_present, end, nonrestart_substr;
1212 
1213  extraword_present = get_bits1(&gb);
1214  nonrestart_substr = get_bits1(&gb);
1215  checkdata_present = get_bits1(&gb);
1216  skip_bits1(&gb);
1217 
1218  end = get_bits(&gb, 12) * 2;
1219 
1220  substr_header_size += 2;
1221 
1222  if (extraword_present) {
1223  if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
1224  av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
1225  goto error;
1226  }
1227  skip_bits(&gb, 16);
1228  substr_header_size += 2;
1229  }
1230 
1231  if (length < header_size + substr_header_size) {
1232  av_log(m->avctx, AV_LOG_ERROR, "Insufficient data for headers\n");
1233  goto error;
1234  }
1235 
1236  if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1237  av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
1238  goto error;
1239  }
1240 
1241  if (end + header_size + substr_header_size > length) {
1243  "Indicated length of substream %d data goes off end of "
1244  "packet.\n", substr);
1245  end = length - header_size - substr_header_size;
1246  }
1247 
1248  if (end < substream_start) {
1249  av_log(avctx, AV_LOG_ERROR,
1250  "Indicated end offset of substream %d data "
1251  "is smaller than calculated start offset.\n",
1252  substr);
1253  goto error;
1254  }
1255 
1256  if (substr > m->max_decoded_substream)
1257  continue;
1258 
1259  substream_parity_present[substr] = checkdata_present;
1260  substream_data_len[substr] = end - substream_start;
1261  substream_start = end;
1262  }
1263 
1264  parity_bits = ff_mlp_calculate_parity(buf, 4);
1265  parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1266 
1267  if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1268  av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1269  goto error;
1270  }
1271 
1272  buf += header_size + substr_header_size;
1273 
1274  for (substr = 0; substr <= m->max_decoded_substream; substr++) {
1275  SubStream *s = &m->substream[substr];
1276 
1277  init_get_bits(&gb, buf, substream_data_len[substr] * 8);
1278 
1279  m->matrix_changed = 0;
1280  memset(m->filter_changed, 0, sizeof(m->filter_changed));
1281 
1282  s->blockpos = 0;
1283  do {
1284  if (get_bits1(&gb)) {
1285  if (get_bits1(&gb)) {
1286  /* A restart header should be present. */
1287  if (read_restart_header(m, &gb, buf, substr) < 0)
1288  goto next_substr;
1289  s->restart_seen = 1;
1290  }
1291 
1292  if (!s->restart_seen)
1293  goto next_substr;
1294  if (read_decoding_params(m, &gb, substr) < 0)
1295  goto next_substr;
1296  }
1297 
1298  if (!s->restart_seen)
1299  goto next_substr;
1300 
1301  if (((avctx->ch_layout.nb_channels == 6 &&
1302  ((m->substream_info >> 2) & 0x3) != 0x3) ||
1303  (avctx->ch_layout.nb_channels == 8 &&
1304  ((m->substream_info >> 4) & 0x7) != 0x7 &&
1305  ((m->substream_info >> 4) & 0x7) != 0x6 &&
1306  ((m->substream_info >> 4) & 0x7) != 0x3)) &&
1307  substr > 0 && substr < m->max_decoded_substream &&
1308  (s->min_channel <= m->substream[substr - 1].max_channel)) {
1309  av_log(avctx, AV_LOG_DEBUG,
1310  "Previous substream(%d) channels overlaps current substream(%d) channels, skipping.\n",
1311  substr - 1, substr);
1312  goto next_substr;
1313  }
1314 
1315  if (substr != m->max_decoded_substream &&
1316  ((s->coded_channels & m->substream[m->max_decoded_substream].coded_channels) != 0)) {
1317  av_log(avctx, AV_LOG_DEBUG,
1318  "Current substream(%d) channels overlaps final substream(%d) channels, skipping.\n",
1319  substr, m->max_decoded_substream);
1320  goto next_substr;
1321  }
1322 
1323  if ((ret = read_block_data(m, &gb, substr)) < 0)
1324  return ret;
1325 
1326  if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1327  goto substream_length_mismatch;
1328 
1329  } while (!get_bits1(&gb));
1330 
1331  skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1332 
1333  if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1334  int shorten_by;
1335 
1336  if (get_bits(&gb, 16) != 0xD234)
1337  return AVERROR_INVALIDDATA;
1338 
1339  shorten_by = get_bits(&gb, 16);
1340  if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD && shorten_by & 0x2000)
1341  s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1342  else if (m->avctx->codec_id == AV_CODEC_ID_MLP && shorten_by != 0xD234)
1343  return AVERROR_INVALIDDATA;
1344 
1345  av_log(m->avctx, AV_LOG_DEBUG, "End of stream indicated.\n");
1346  s->end_of_stream = 1;
1347  }
1348 
1349  if (substream_parity_present[substr]) {
1350  uint8_t parity, checksum;
1351 
1352  if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1353  goto substream_length_mismatch;
1354 
1355  parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1356  checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
1357 
1358  if ((get_bits(&gb, 8) ^ parity) != 0xa9 )
1359  av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
1360  if ( get_bits(&gb, 8) != checksum)
1361  av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n" , substr);
1362  }
1363 
1364  if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1365  goto substream_length_mismatch;
1366 
1367 next_substr:
1368  if (!s->restart_seen)
1370  "No restart header present in substream %d.\n", substr);
1371 
1372  buf += substream_data_len[substr];
1373  }
1374 
1375  if ((ret = output_data(m, m->max_decoded_substream, frame, got_frame_ptr)) < 0)
1376  return ret;
1377 
1378  for (substr = 0; substr <= m->max_decoded_substream; substr++){
1379  SubStream *s = &m->substream[substr];
1380 
1381  if (s->end_of_stream) {
1382  s->lossless_check_data = 0xffffffff;
1383  s->end_of_stream = 0;
1384  m->params_valid = 0;
1385  }
1386  }
1387 
1388  return length;
1389 
1390 substream_length_mismatch:
1391  av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1392  return AVERROR_INVALIDDATA;
1393 
1394 error:
1395  m->params_valid = 0;
1396  return AVERROR_INVALIDDATA;
1397 }
1398 
1400 {
1401  MLPDecodeContext *m = avctx->priv_data;
1402 
1403  m->params_valid = 0;
1404  for (int substr = 0; substr <= m->max_decoded_substream; substr++){
1405  SubStream *s = &m->substream[substr];
1406 
1407  s->lossless_check_data = 0xffffffff;
1408  s->prev_matrix_encoding = 0;
1409  }
1410 }
1411 
1412 #define OFFSET(x) offsetof(MLPDecodeContext, x)
1413 #define FLAGS (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1414 static const AVOption options[] = {
1415  { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout),
1416  AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = FLAGS },
1417  { NULL },
1418 };
1419 
1420 static const AVClass mlp_decoder_class = {
1421  .class_name = "MLP decoder",
1422  .item_name = av_default_item_name,
1423  .option = options,
1424  .version = LIBAVUTIL_VERSION_INT,
1425 };
1426 
1428  .class_name = "TrueHD decoder",
1429  .item_name = av_default_item_name,
1430  .option = options,
1431  .version = LIBAVUTIL_VERSION_INT,
1432 };
1433 
1434 #if CONFIG_MLP_DECODER
1435 const FFCodec ff_mlp_decoder = {
1436  .p.name = "mlp",
1437  CODEC_LONG_NAME("MLP (Meridian Lossless Packing)"),
1438  .p.type = AVMEDIA_TYPE_AUDIO,
1439  .p.id = AV_CODEC_ID_MLP,
1440  .priv_data_size = sizeof(MLPDecodeContext),
1441  .p.priv_class = &mlp_decoder_class,
1442  .init = mlp_decode_init,
1444  .flush = mlp_decode_flush,
1445  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1446 };
1447 #endif
1448 #if CONFIG_TRUEHD_DECODER
1449 const FFCodec ff_truehd_decoder = {
1450  .p.name = "truehd",
1451  CODEC_LONG_NAME("TrueHD"),
1452  .p.type = AVMEDIA_TYPE_AUDIO,
1453  .p.id = AV_CODEC_ID_TRUEHD,
1454  .priv_data_size = sizeof(MLPDecodeContext),
1455  .p.priv_class = &truehd_decoder_class,
1456  .init = mlp_decode_init,
1458  .flush = mlp_decode_flush,
1459  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1461 };
1462 #endif /* CONFIG_TRUEHD_DECODER */
MLPDecodeContext::params_valid
uint8_t params_valid
Set if a valid major sync block has been read. Otherwise no decoding is possible.
Definition: mlpdec.c:144
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1092
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
noise_table
static const int8_t noise_table[256]
Data table used for TrueHD noise generation function.
Definition: mlpdec.c:1026
AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:222
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:495
ChannelParams::codebook
uint8_t codebook
Which VLC codebook to use to read residuals.
Definition: mlp.h:103
opt.h
xor_32_to_8
static uint8_t xor_32_to_8(uint32_t value)
XOR four bytes into one.
Definition: mlp.h:175
SubStream::matrix_coeff
int32_t matrix_coeff[MAX_MATRICES][MAX_CHANNELS]
Matrix coefficients, stored as 2.14 fixed point.
Definition: mlpdec.c:110
mem_internal.h
SubStream::prev_matrix_encoding
enum AVMatrixEncoding prev_matrix_encoding
Definition: mlpdec.c:81
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1064
av_popcount64
#define av_popcount64
Definition: common.h:153
FLAGS
#define FLAGS
Definition: mlpdec.c:1413
thread.h
SubStream::end_of_stream
uint8_t end_of_stream
Set if end of stream is encountered.
Definition: mlpdec.c:60
AV_CHAN_WIDE_LEFT
@ AV_CHAN_WIDE_LEFT
Definition: channel_layout.h:72
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:210
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
SubStream::output_shift
int8_t output_shift[MAX_CHANNELS]
Left shift to apply to decoded PCM values to get final 24-bit output.
Definition: mlpdec.c:124
THD_CH_MODIFIER_SURROUNDEX
@ THD_CH_MODIFIER_SURROUNDEX
Definition: mlp.h:189
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
read_decoding_params
static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp, unsigned int substr)
Read decoding parameters that change more often than those in the restart header.
Definition: mlpdec.c:874
PARAM_HUFFOFFSET
#define PARAM_HUFFOFFSET
Definition: mlp.h:79
MAX_SAMPLERATE
#define MAX_SAMPLERATE
maximum sample frequency seen in files
Definition: mlp.h:56
init_static
static av_cold void init_static(void)
Initialize static data, constant between all invocations of the codec.
Definition: mlpdec.c:219
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:491
ff_mlp_calculate_parity
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
XOR together all the bytes of a buffer.
Definition: mlp.c:133
AVOption
AVOption.
Definition: opt.h:251
mh
#define mh
Definition: vf_colormatrix.c:105
table
static const uint16_t table[]
Definition: prosumer.c:205
filter_channel
static void filter_channel(MLPDecodeContext *m, unsigned int substr, unsigned int channel)
Generate PCM samples using the prediction filters and residual values read from the data stream,...
Definition: mlpdec.c:957
SubStream::restart_seen
uint8_t restart_seen
Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
Definition: mlpdec.c:58
FFCodec
Definition: codec_internal.h:127
output_data
static int output_data(MLPDecodeContext *m, unsigned int substr, AVFrame *frame, int *got_frame_ptr)
Write the audio data into the output buffer.
Definition: mlpdec.c:1092
SubStream::mask
uint64_t mask
The channel layout for this substream.
Definition: mlpdec.c:78
SubStream::min_channel
uint8_t min_channel
The index of the first channel coded in this substream.
Definition: mlpdec.c:68
MLPDecodeContext::major_sync_header_size
int major_sync_header_size
Size of the major sync unit, in bytes.
Definition: mlpdec.c:141
OFFSET
#define OFFSET(x)
Definition: mlpdec.c:1412
SubStream
Definition: mlpdec.c:56
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:317
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:486
ff_mlpdsp_init
av_cold void ff_mlpdsp_init(MLPDSPContext *c)
Definition: mlpdsp.c:128
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
SubStream::max_channel
uint8_t max_channel
The index of the last channel coded in this substream.
Definition: mlpdec.c:70
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
SubStream::ch_assign
uint8_t ch_assign[MAX_CHANNELS]
For each channel output by the matrix, the output channel to map it to.
Definition: mlpdec.c:76
PARAM_MATRIX
#define PARAM_MATRIX
Definition: mlp.h:74
MLPDSPContext::mlp_pack_output
int32_t(* mlp_pack_output)(int32_t lossless_check_data, uint16_t blockpos, int32_t(*sample_buffer)[MAX_CHANNELS], void *data, uint8_t *ch_assign, int8_t *output_shift, uint8_t max_matrix_channel, int is32)
Definition: mlpdsp.h:69
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
MLPDecodeContext::substream_info
uint8_t substream_info
Which substream of substreams carry 2/6/8-channel presentation.
Definition: mlpdec.c:153
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
AV_CHAN_SURROUND_DIRECT_LEFT
@ AV_CHAN_SURROUND_DIRECT_LEFT
Definition: channel_layout.h:74
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ChannelParams::filter_params
FilterParams filter_params[NUM_FILTERS]
Definition: mlp.h:98
ff_mlp_checksum8
uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
MLP uses checksums that seem to be based on the standard CRC algorithm, but are not (in implementatio...
Definition: mlp.c:107
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
fail
#define fail()
Definition: checkasm.h:138
MAX_MATRICES
#define MAX_MATRICES
Definition: mlp.h:46
ChannelParams::huff_lsbs
uint8_t huff_lsbs
Size of residual suffix not encoded using VLC.
Definition: mlp.h:104
GetBitContext
Definition: get_bits.h:108
MAX_IIR_ORDER
#define MAX_IIR_ORDER
Definition: mlp.h:68
SYNC_TRUEHD
#define SYNC_TRUEHD
Definition: mlp.h:30
AV_PROFILE_TRUEHD_ATMOS
#define AV_PROFILE_TRUEHD_ATMOS
Definition: defs.h:97
calculate_sign_huff
static int32_t calculate_sign_huff(MLPDecodeContext *m, unsigned int substr, unsigned int ch)
Definition: mlpdec.c:233
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:211
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:219
MAX_MATRICES_MLP
#define MAX_MATRICES_MLP
Maximum number of matrices used in decoding; most streams have one matrix per output channel,...
Definition: mlp.h:44
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:203
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
MLPDecodeContext::needs_reordering
uint8_t needs_reordering
Stream needs channel reordering to comply with FFmpeg's channel order.
Definition: mlpdec.c:159
FIR
#define FIR
Definition: mlp.h:82
mlp_decoder_class
static const AVClass mlp_decoder_class
Definition: mlpdec.c:1420
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
ff_side_data_update_matrix_encoding
int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding)
Add or update AV_FRAME_DATA_MATRIXENCODING side data.
Definition: utils.c:124
AV_MATRIX_ENCODING_DOLBY
@ AV_MATRIX_ENCODING_DOLBY
Definition: channel_layout.h:250
MLPDecodeContext::dsp
MLPDSPContext dsp
Definition: mlpdec.c:175
SubStream::channel_params
ChannelParams channel_params[MAX_CHANNELS]
Channel coding parameters for channels in the substream.
Definition: mlpdec.c:84
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:167
mask
static const uint16_t mask[17]
Definition: lzw.c:38
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
PARAM_IIR
#define PARAM_IIR
Definition: mlp.h:78
MLPDecodeContext::matrix_changed
int matrix_changed
Definition: mlpdec.c:168
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_channel_layout_from_mask
FF_ENABLE_DEPRECATION_WARNINGS int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:399
MLPDSPContext::mlp_filter_channel
void(* mlp_filter_channel)(int32_t *state, const int32_t *coeff, int firorder, int iirorder, unsigned int filter_shift, int32_t mask, int blocksize, int32_t *sample_buffer)
Definition: mlpdsp.h:50
AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_RIGHT
Definition: channel_layout.h:60
vlc_buf
static VLCElem vlc_buf[16716]
Definition: clearvideo.c:80
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
vlc_init
#define vlc_init(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:56
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1517
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
get_bits.h
THD_CH_MODIFIER_LTRT
@ THD_CH_MODIFIER_LTRT
Definition: mlp.h:185
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
MLPDSPContext::mlp_select_pack_output
int32_t(*(* mlp_select_pack_output)(uint8_t *ch_assign, int8_t *output_shift, uint8_t max_matrix_channel, int is32))(int32_t
Definition: mlpdsp.h:65
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:451
if
if(ret)
Definition: filter_design.txt:179
AVMatrixEncoding
AVMatrixEncoding
Definition: channel_layout.h:248
AV_MATRIX_ENCODING_DOLBYHEADPHONE
@ AV_MATRIX_ENCODING_DOLBYHEADPHONE
Definition: channel_layout.h:255
read_access_unit
static int read_access_unit(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Read an access unit from the stream.
Definition: mlpdec.c:1167
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:201
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
SubStream::lossless_check_data
int32_t lossless_check_data
Running XOR of all output samples.
Definition: mlpdec.c:127
MLPDecodeContext::bypassed_lsbs
int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS]
Definition: mlpdec.c:172
SubStream::quant_step_size
uint8_t quant_step_size[MAX_CHANNELS]
Left shift to apply to Huffman-decoded residuals.
Definition: mlpdec.c:116
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
profiles.h
quant_step_size
static const float quant_step_size[]
Definition: hca_data.h:125
MLPDecodeContext::filter_changed
int filter_changed[MAX_CHANNELS][NUM_FILTERS]
Definition: mlpdec.c:169
AV_CHAN_TOP_CENTER
@ AV_CHAN_TOP_CENTER
Definition: channel_layout.h:61
AV_OPT_TYPE_CHLAYOUT
@ AV_OPT_TYPE_CHLAYOUT
Definition: opt.h:245
fp
#define fp
Definition: regdef.h:44
seed
static unsigned int seed
Definition: videogen.c:78
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
AVOnce
#define AVOnce
Definition: thread.h:200
MLPDecodeContext::access_unit_size
int access_unit_size
number of PCM samples contained in each frame
Definition: mlpdec.c:162
index
int index
Definition: gxfenc.c:89
AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:57
SubStream::max_matrix_channel
uint8_t max_matrix_channel
The number of channels input into the rematrix stage.
Definition: mlpdec.c:74
MLPDecodeContext::extended_substream_info
uint8_t extended_substream_info
Which substream of substreams carry 16-channel presentation.
Definition: mlpdec.c:150
THD_CH_MODIFIER_LBINRBIN
@ THD_CH_MODIFIER_LBINRBIN
Definition: mlp.h:186
AV_CHAN_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
Definition: channel_layout.h:51
AV_CHAN_FRONT_CENTER
@ AV_CHAN_FRONT_CENTER
Definition: channel_layout.h:52
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
FilterParams
filter data
Definition: mlp.h:86
VLC::table_allocated
int table_allocated
Definition: vlc.h:36
MLPDecodeContext::sample_buffer
int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS]
Definition: mlpdec.c:173
huff_vlc
static VLC huff_vlc[3]
Definition: mlpdec.c:215
ff_mlp_restart_checksum
uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
Calculate an 8-bit checksum over a restart header – a non-multiple-of-8 number of bits,...
Definition: mlp.c:114
options
static const AVOption options[]
Definition: mlpdec.c:1414
thd_channel_order
static enum AVChannel thd_channel_order[]
Definition: mlpdec.c:178
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
MAX_MATRIX_CHANNEL_MLP
#define MAX_MATRIX_CHANNEL_MLP
Last possible matrix channel for each codec.
Definition: mlp.h:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
SubStream::noise_type
uint16_t noise_type
restart header data
Definition: mlpdec.c:65
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:87
MLPDecodeContext::num_substreams
uint8_t num_substreams
Number of substreams contained within this stream.
Definition: mlpdec.c:147
MLPDecodeContext::avctx
AVCodecContext * avctx
Definition: mlpdec.c:133
MLPDecodeContext::substream
SubStream substream[MAX_SUBSTREAMS]
Definition: mlpdec.c:166
MSB_MASK
#define MSB_MASK(bits)
Definition: mlpdec.c:952
AV_MATRIX_ENCODING_NONE
@ AV_MATRIX_ENCODING_NONE
Definition: channel_layout.h:249
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
mlpdsp.h
AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_LOW_FREQUENCY
Definition: channel_layout.h:53
MAX_SUBSTREAMS
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
Definition: mlp.h:51
MLPDecodeContext::is_major_sync_unit
int is_major_sync_unit
Current access unit being read has a major sync.
Definition: mlpdec.c:138
MAX_MATRIX_CHANNEL_TRUEHD
#define MAX_MATRIX_CHANNEL_TRUEHD
Definition: mlp.h:34
mlp_decode_init
static av_cold int mlp_decode_init(AVCodecContext *avctx)
Definition: mlpdec.c:290
VLCElem
Definition: vlc.h:29
AV_CHAN_BACK_RIGHT
@ AV_CHAN_BACK_RIGHT
Definition: channel_layout.h:55
parity
mcdeint parity
Definition: vf_mcdeint.c:281
MAX_MATRICES_TRUEHD
#define MAX_MATRICES_TRUEHD
Definition: mlp.h:45
AV_CHAN_SIDE_LEFT
@ AV_CHAN_SIDE_LEFT
Definition: channel_layout.h:59
NUM_FILTERS
#define NUM_FILTERS
number of allowed filters
Definition: mlp.h:64
FilterParams::order
uint8_t order
number of taps in filter
Definition: mlp.h:87
MLPDecodeContext
Definition: mlpdec.c:131
ff_mlp_read_major_sync
int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, GetBitContext *gb)
Read a major sync info header - contains high level information about the stream - sample rate,...
Definition: mlp_parse.c:86
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:223
AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_TOP_FRONT_RIGHT
Definition: channel_layout.h:64
ChannelParams::huff_offset
int16_t huff_offset
Offset to apply to residual values.
Definition: mlp.h:101
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
MLPDecodeContext::downmix_layout
AVChannelLayout downmix_layout
Definition: mlpdec.c:135
SubStream::noisegen_seed
uint32_t noisegen_seed
The current seed value for the pseudorandom noise generator(s).
Definition: mlpdec.c:89
AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:56
MLPDecodeContext::max_decoded_substream
uint8_t max_decoded_substream
Index of the last substream to decode - further substreams are skipped.
Definition: mlpdec.c:156
SubStream::lsb_bypass
uint8_t lsb_bypass[MAX_MATRICES]
Whether the LSBs of the matrix output are encoded in the bitstream.
Definition: mlpdec.c:108
ff_mlp_decoder
const FFCodec ff_mlp_decoder
SubStream::matrix_out_ch
uint8_t matrix_out_ch[MAX_MATRICES]
matrix output channel
Definition: mlpdec.c:105
MAX_CHANNELS
#define MAX_CHANNELS
Definition: aac.h:46
MAX_FIR_ORDER
#define MAX_FIR_ORDER
The maximum number of taps in IIR and FIR filters.
Definition: mlp.h:67
MLPDSPContext::mlp_rematrix_channel
void(* mlp_rematrix_channel)(int32_t *samples, const int32_t *coeffs, const uint8_t *bypassed_lsbs, const int8_t *noise_buffer, int index, unsigned int dest_ch, uint16_t blockpos, unsigned int maxchan, int matrix_noise_shift, int access_unit_size_pow2, int32_t mask)
Definition: mlpdsp.h:54
layout
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
Definition: filter_design.txt:18
AVChannel
AVChannel
Definition: channel_layout.h:47
read_restart_header
static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp, const uint8_t *buf, unsigned int substr)
Read a restart header from a block in a substream.
Definition: mlpdec.c:510
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
mlp_parse.h
SubStream::coded_channels
uint64_t coded_channels
The coded channels mask in this substream.
Definition: mlpdec.c:72
AV_CHAN_SURROUND_DIRECT_RIGHT
@ AV_CHAN_SURROUND_DIRECT_RIGHT
Definition: channel_layout.h:75
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
SubStream::blockpos
uint16_t blockpos
Number of PCM samples decoded so far in this frame.
Definition: mlpdec.c:121
SubStream::noise_shift
uint8_t noise_shift
The left shift applied to random noise in 0x31ea substreams.
Definition: mlpdec.c:87
SubStream::blocksize
uint16_t blocksize
number of PCM samples in current audio block
Definition: mlpdec.c:119
internal.h
PARAM_PRESENCE
#define PARAM_PRESENCE
Definition: mlp.h:80
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MLPHeaderInfo
Definition: mlp_parse.h:30
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
read_channel_params
static int read_channel_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp, unsigned int ch)
Read channel parameters.
Definition: mlpdec.c:817
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ChannelParams
sample data coding information
Definition: mlp.h:97
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:174
MAX_BLOCKSIZE
#define MAX_BLOCKSIZE
Definition: diracdec.c:54
fill_noise_buffer
static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
Generate a block of noise, used when restart sync word == 0x31eb.
Definition: mlpdec.c:1075
ff_mlp_init_crc
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:92
ChannelParams::sign_huff_offset
int32_t sign_huff_offset
sign/rounding-corrected version of huff_offset
Definition: mlp.h:102
mlp_channel_layout_subset
static int mlp_channel_layout_subset(AVChannelLayout *layout, uint64_t mask)
Definition: mlpdec.c:194
MLPDecodeContext::access_unit_size_pow2
int access_unit_size_pow2
next power of two above the number of samples in each frame
Definition: mlpdec.c:164
thd_channel_layout_extract_channel
static enum AVChannel thd_channel_layout_extract_channel(uint64_t channel_layout, int index)
Definition: mlpdec.c:201
avcodec.h
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
mlp_decode_flush
static void mlp_decode_flush(AVCodecContext *avctx)
Definition: mlpdec.c:1399
ff_truehd_decoder
const FFCodec ff_truehd_decoder
SubStream::num_primitive_matrices
uint8_t num_primitive_matrices
matrix data
Definition: mlpdec.c:102
av_channel_layout_check
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
Definition: channel_layout.c:916
pos
unsigned int pos
Definition: spdifenc.c:413
FilterParams::state
int32_t state[MAX_FIR_ORDER]
Definition: mlp.h:90
SYNC_MLP
#define SYNC_MLP
Definition: mlp.h:29
AV_CHAN_BACK_CENTER
@ AV_CHAN_BACK_CENTER
Definition: channel_layout.h:58
SubStream::data_check_present
uint8_t data_check_present
Set if the substream contains extra info to check the size of VLC blocks.
Definition: mlpdec.c:92
AV_CHAN_NONE
@ AV_CHAN_NONE
Invalid channel index.
Definition: channel_layout.h:49
SubStream::matrix_noise_shift
uint8_t matrix_noise_shift[MAX_MATRICES]
Left shift to apply to noise values in 0x31eb substreams.
Definition: mlpdec.c:112
AVCodecContext
main external API structure.
Definition: avcodec.h:441
VLC_BITS
#define VLC_BITS
number of bits used for VLC lookup - longest Huffman code is 9
Definition: mlpdec.c:52
channel_layout.h
AV_CHAN_LOW_FREQUENCY_2
@ AV_CHAN_LOW_FREQUENCY_2
Definition: channel_layout.h:76
VLC_STATIC_SIZE
#define VLC_STATIC_SIZE
Definition: mlpdec.c:53
av_channel_layout_subset
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
Definition: channel_layout.c:998
AV_MATRIX_ENCODING_DOLBYEX
@ AV_MATRIX_ENCODING_DOLBYEX
Definition: channel_layout.h:254
av_channel_layout_index_from_channel
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
Definition: channel_layout.c:846
VLC
Definition: vlc.h:33
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1596
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:640
PARAM_QUANTSTEP
#define PARAM_QUANTSTEP
Definition: mlp.h:76
AV_CHAN_BACK_LEFT
@ AV_CHAN_BACK_LEFT
Definition: channel_layout.h:54
VLC::table
VLCElem * table
Definition: vlc.h:35
IIR
#define IIR
Definition: mlp.h:83
PARAM_BLOCKSIZE
#define PARAM_BLOCKSIZE
Definition: mlp.h:73
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AV_CHAN_TOP_FRONT_CENTER
@ AV_CHAN_TOP_FRONT_CENTER
Definition: channel_layout.h:63
read_filter_params
static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp, unsigned int substr, unsigned int channel, unsigned int filter)
Read parameters for one of the prediction filters.
Definition: mlpdec.c:674
AV_CHAN_WIDE_RIGHT
@ AV_CHAN_WIDE_RIGHT
Definition: channel_layout.h:73
PARAM_FIR
#define PARAM_FIR
Definition: mlp.h:77
read_huff_channels
static int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp, unsigned int substr, unsigned int pos)
Read a sample, consisting of either, both or neither of entropy-coded MSBs and plain LSBs.
Definition: mlpdec.c:254
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
read_major_sync
static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
Read a major sync info header - contains high level information about the stream - sample rate,...
Definition: mlpdec.c:319
mlp.h
ff_mlp_huffman_tables
const uint8_t ff_mlp_huffman_tables[3][18][2]
Tables defining the Huffman codes.
Definition: mlp.c:30
SubStream::param_presence_flags
uint8_t param_presence_flags
Bitmask of which parameter sets are conveyed in a decoding parameter block.
Definition: mlpdec.c:95
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
PARAM_OUTSHIFT
#define PARAM_OUTSHIFT
Definition: mlp.h:75
AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_FRONT_LEFT
Definition: channel_layout.h:62
int32_t
int32_t
Definition: audioconvert.c:56
truehd_decoder_class
static const AVClass truehd_decoder_class
Definition: mlpdec.c:1427
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
read_matrix_params
static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
Read parameters for primitive matrices.
Definition: mlpdec.c:749
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
VLC_INIT_USE_STATIC
#define VLC_INIT_USE_STATIC
Definition: vlc.h:176
AV_CHAN_FRONT_LEFT
@ AV_CHAN_FRONT_LEFT
Definition: channel_layout.h:50
MLPDSPContext
Definition: mlpdsp.h:49
ff_truehd_profiles
const AVProfile ff_truehd_profiles[]
Definition: profiles.c:55
FilterParams::shift
uint8_t shift
Right shift to apply to output of filter.
Definition: mlp.h:88
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
generate_2_noise_channels
static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
Noise generation functions.
Definition: mlpdec.c:1055
SubStream::matrix_encoding
enum AVMatrixEncoding matrix_encoding
The matrix encoding mode for this substream.
Definition: mlpdec.c:80
channel
channel
Definition: ebur128.h:39
MAX_BLOCKSIZE_POW2
#define MAX_BLOCKSIZE_POW2
next power of two greater than MAX_BLOCKSIZE
Definition: mlp.h:61
MLPDecodeContext::noise_buffer
int8_t noise_buffer[MAX_BLOCKSIZE_POW2]
Definition: mlpdec.c:171
codebook
static const unsigned codebook[256][2]
Definition: cfhdenc.c:42
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:471
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:173
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
read_block_data
static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp, unsigned int substr)
Read a block of PCM residual data (or actual if no filtering active).
Definition: mlpdec.c:984