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