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