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