]> git.sesse.net Git - ffmpeg/blob - libavcodec/mlpdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / mlpdec.c
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 <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
38 /** number of bits used for VLC lookup - longest Huffman code is 9 */
39 #define VLC_BITS            9
40
41 typedef struct SubStream {
42     /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
43     uint8_t     restart_seen;
44
45     //@{
46     /** restart header data */
47     /// The type of noise to be used in the rematrix stage.
48     uint16_t    noise_type;
49
50     /// The index of the first channel coded in this substream.
51     uint8_t     min_channel;
52     /// The index of the last channel coded in this substream.
53     uint8_t     max_channel;
54     /// The number of channels input into the rematrix stage.
55     uint8_t     max_matrix_channel;
56     /// For each channel output by the matrix, the output channel to map it to
57     uint8_t     ch_assign[MAX_CHANNELS];
58
59     /// Channel coding parameters for channels in the substream
60     ChannelParams channel_params[MAX_CHANNELS];
61
62     /// The left shift applied to random noise in 0x31ea substreams.
63     uint8_t     noise_shift;
64     /// The current seed value for the pseudorandom noise generator(s).
65     uint32_t    noisegen_seed;
66
67     /// Set if the substream contains extra info to check the size of VLC blocks.
68     uint8_t     data_check_present;
69
70     /// Bitmask of which parameter sets are conveyed in a decoding parameter block.
71     uint8_t     param_presence_flags;
72 #define PARAM_BLOCKSIZE     (1 << 7)
73 #define PARAM_MATRIX        (1 << 6)
74 #define PARAM_OUTSHIFT      (1 << 5)
75 #define PARAM_QUANTSTEP     (1 << 4)
76 #define PARAM_FIR           (1 << 3)
77 #define PARAM_IIR           (1 << 2)
78 #define PARAM_HUFFOFFSET    (1 << 1)
79 #define PARAM_PRESENCE      (1 << 0)
80     //@}
81
82     //@{
83     /** matrix data */
84
85     /// Number of matrices to be applied.
86     uint8_t     num_primitive_matrices;
87
88     /// matrix output channel
89     uint8_t     matrix_out_ch[MAX_MATRICES];
90
91     /// Whether the LSBs of the matrix output are encoded in the bitstream.
92     uint8_t     lsb_bypass[MAX_MATRICES];
93     /// Matrix coefficients, stored as 2.14 fixed point.
94     int32_t     matrix_coeff[MAX_MATRICES][MAX_CHANNELS];
95     /// Left shift to apply to noise values in 0x31eb substreams.
96     uint8_t     matrix_noise_shift[MAX_MATRICES];
97     //@}
98
99     /// Left shift to apply to Huffman-decoded residuals.
100     uint8_t     quant_step_size[MAX_CHANNELS];
101
102     /// number of PCM samples in current audio block
103     uint16_t    blocksize;
104     /// Number of PCM samples decoded so far in this frame.
105     uint16_t    blockpos;
106
107     /// Left shift to apply to decoded PCM values to get final 24-bit output.
108     int8_t      output_shift[MAX_CHANNELS];
109
110     /// Running XOR of all output samples.
111     int32_t     lossless_check_data;
112
113 } SubStream;
114
115 typedef struct MLPDecodeContext {
116     AVCodecContext *avctx;
117     AVFrame     frame;
118
119     /// Current access unit being read has a major sync.
120     int         is_major_sync_unit;
121
122     /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
123     uint8_t     params_valid;
124
125     /// Number of substreams contained within this stream.
126     uint8_t     num_substreams;
127
128     /// Index of the last substream to decode - further substreams are skipped.
129     uint8_t     max_decoded_substream;
130
131     /// Stream needs channel reordering to comply with FFmpeg's channel order
132     uint8_t     needs_reordering;
133
134     /// number of PCM samples contained in each frame
135     int         access_unit_size;
136     /// next power of two above the number of samples in each frame
137     int         access_unit_size_pow2;
138
139     SubStream   substream[MAX_SUBSTREAMS];
140
141     int         matrix_changed;
142     int         filter_changed[MAX_CHANNELS][NUM_FILTERS];
143
144     int8_t      noise_buffer[MAX_BLOCKSIZE_POW2];
145     int8_t      bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
146     int32_t     sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS];
147
148     DSPContext  dsp;
149 } MLPDecodeContext;
150
151 static VLC huff_vlc[3];
152
153 /** Initialize static data, constant between all invocations of the codec. */
154
155 static av_cold void init_static(void)
156 {
157     if (!huff_vlc[0].bits) {
158         INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
159                     &ff_mlp_huffman_tables[0][0][1], 2, 1,
160                     &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
161         INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
162                     &ff_mlp_huffman_tables[1][0][1], 2, 1,
163                     &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
164         INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
165                     &ff_mlp_huffman_tables[2][0][1], 2, 1,
166                     &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
167     }
168
169     ff_mlp_init_crc();
170 }
171
172 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
173                                           unsigned int substr, unsigned int ch)
174 {
175     SubStream *s = &m->substream[substr];
176     ChannelParams *cp = &s->channel_params[ch];
177     int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
178     int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
179     int32_t sign_huff_offset = cp->huff_offset;
180
181     if (cp->codebook > 0)
182         sign_huff_offset -= 7 << lsb_bits;
183
184     if (sign_shift >= 0)
185         sign_huff_offset -= 1 << sign_shift;
186
187     return sign_huff_offset;
188 }
189
190 /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
191  *  and plain LSBs. */
192
193 static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
194                                      unsigned int substr, unsigned int pos)
195 {
196     SubStream *s = &m->substream[substr];
197     unsigned int mat, channel;
198
199     for (mat = 0; mat < s->num_primitive_matrices; mat++)
200         if (s->lsb_bypass[mat])
201             m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
202
203     for (channel = s->min_channel; channel <= s->max_channel; channel++) {
204         ChannelParams *cp = &s->channel_params[channel];
205         int codebook = cp->codebook;
206         int quant_step_size = s->quant_step_size[channel];
207         int lsb_bits = cp->huff_lsbs - quant_step_size;
208         int result = 0;
209
210         if (codebook > 0)
211             result = get_vlc2(gbp, huff_vlc[codebook-1].table,
212                             VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
213
214         if (result < 0)
215             return AVERROR_INVALIDDATA;
216
217         if (lsb_bits > 0)
218             result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
219
220         result  += cp->sign_huff_offset;
221         result <<= quant_step_size;
222
223         m->sample_buffer[pos + s->blockpos][channel] = result;
224     }
225
226     return 0;
227 }
228
229 static av_cold int mlp_decode_init(AVCodecContext *avctx)
230 {
231     MLPDecodeContext *m = avctx->priv_data;
232     int substr;
233
234     init_static();
235     m->avctx = avctx;
236     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
237         m->substream[substr].lossless_check_data = 0xffffffff;
238     ff_dsputil_init(&m->dsp, avctx);
239
240     avcodec_get_frame_defaults(&m->frame);
241     avctx->coded_frame = &m->frame;
242
243     return 0;
244 }
245
246 /** Read a major sync info header - contains high level information about
247  *  the stream - sample rate, channel arrangement etc. Most of this
248  *  information is not actually necessary for decoding, only for playback.
249  */
250
251 static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
252 {
253     MLPHeaderInfo mh;
254     int substr, ret;
255
256     if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
257         return ret;
258
259     if (mh.group1_bits == 0) {
260         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
261         return AVERROR_INVALIDDATA;
262     }
263     if (mh.group2_bits > mh.group1_bits) {
264         av_log(m->avctx, AV_LOG_ERROR,
265                "Channel group 2 cannot have more bits per sample than group 1.\n");
266         return AVERROR_INVALIDDATA;
267     }
268
269     if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
270         av_log(m->avctx, AV_LOG_ERROR,
271                "Channel groups with differing sample rates are not currently supported.\n");
272         return AVERROR_INVALIDDATA;
273     }
274
275     if (mh.group1_samplerate == 0) {
276         av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
277         return AVERROR_INVALIDDATA;
278     }
279     if (mh.group1_samplerate > MAX_SAMPLERATE) {
280         av_log(m->avctx, AV_LOG_ERROR,
281                "Sampling rate %d is greater than the supported maximum (%d).\n",
282                mh.group1_samplerate, MAX_SAMPLERATE);
283         return AVERROR_INVALIDDATA;
284     }
285     if (mh.access_unit_size > MAX_BLOCKSIZE) {
286         av_log(m->avctx, AV_LOG_ERROR,
287                "Block size %d is greater than the supported maximum (%d).\n",
288                mh.access_unit_size, MAX_BLOCKSIZE);
289         return AVERROR_INVALIDDATA;
290     }
291     if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
292         av_log(m->avctx, AV_LOG_ERROR,
293                "Block size pow2 %d is greater than the supported maximum (%d).\n",
294                mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
295         return AVERROR_INVALIDDATA;
296     }
297
298     if (mh.num_substreams == 0)
299         return AVERROR_INVALIDDATA;
300     if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
301         av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
302         return AVERROR_INVALIDDATA;
303     }
304     if (mh.num_substreams > MAX_SUBSTREAMS) {
305         av_log_ask_for_sample(m->avctx,
306                "Number of substreams %d is larger than the maximum supported "
307                "by the decoder.\n", mh.num_substreams);
308         return AVERROR_PATCHWELCOME;
309     }
310
311     m->access_unit_size      = mh.access_unit_size;
312     m->access_unit_size_pow2 = mh.access_unit_size_pow2;
313
314     m->num_substreams        = mh.num_substreams;
315     m->max_decoded_substream = m->num_substreams - 1;
316
317     m->avctx->sample_rate    = mh.group1_samplerate;
318     m->avctx->frame_size     = mh.access_unit_size;
319
320     m->avctx->bits_per_raw_sample = mh.group1_bits;
321     if (mh.group1_bits > 16)
322         m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
323     else
324         m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
325
326     m->params_valid = 1;
327     for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
328         m->substream[substr].restart_seen = 0;
329
330     if (mh.stream_type == 0xbb) {
331         /* MLP stream */
332         m->avctx->channel_layout = ff_mlp_layout[mh.channels_mlp];
333     } else { /* mh.stream_type == 0xba */
334         /* TrueHD stream */
335         if (mh.channels_thd_stream2) {
336             m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream2);
337         } else {
338             m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream1);
339         }
340         if (m->avctx->channels &&
341             !m->avctx->request_channels && !m->avctx->request_channel_layout &&
342             av_get_channel_layout_nb_channels(m->avctx->channel_layout) != m->avctx->channels) {
343             m->avctx->channel_layout = 0;
344             av_log_ask_for_sample(m->avctx, "Unknown channel layout.");
345         }
346     }
347
348     m->needs_reordering = mh.channels_mlp >= 18 && mh.channels_mlp <= 20;
349
350     return 0;
351 }
352
353 /** Read a restart header from a block in a substream. This contains parameters
354  *  required to decode the audio that do not change very often. Generally
355  *  (always) present only in blocks following a major sync. */
356
357 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
358                                const uint8_t *buf, unsigned int substr)
359 {
360     SubStream *s = &m->substream[substr];
361     unsigned int ch;
362     int sync_word, tmp;
363     uint8_t checksum;
364     uint8_t lossless_check;
365     int start_count = get_bits_count(gbp);
366     const int max_matrix_channel = m->avctx->codec_id == CODEC_ID_MLP
367                                  ? MAX_MATRIX_CHANNEL_MLP
368                                  : MAX_MATRIX_CHANNEL_TRUEHD;
369     int max_channel, min_channel, matrix_channel;
370
371     sync_word = get_bits(gbp, 13);
372
373     if (sync_word != 0x31ea >> 1) {
374         av_log(m->avctx, AV_LOG_ERROR,
375                "restart header sync incorrect (got 0x%04x)\n", sync_word);
376         return AVERROR_INVALIDDATA;
377     }
378
379     s->noise_type = get_bits1(gbp);
380
381     if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
382         av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
383         return AVERROR_INVALIDDATA;
384     }
385
386     skip_bits(gbp, 16); /* Output timestamp */
387
388     min_channel    = get_bits(gbp, 4);
389     max_channel    = get_bits(gbp, 4);
390     matrix_channel = get_bits(gbp, 4);
391
392     if (matrix_channel > max_matrix_channel) {
393         av_log(m->avctx, AV_LOG_ERROR,
394                "Max matrix channel cannot be greater than %d.\n",
395                max_matrix_channel);
396         return AVERROR_INVALIDDATA;
397     }
398
399     if (max_channel != matrix_channel) {
400         av_log(m->avctx, AV_LOG_ERROR,
401                "Max channel must be equal max matrix channel.\n");
402         return AVERROR_INVALIDDATA;
403     }
404
405     /* This should happen for TrueHD streams with >6 channels and MLP's noise
406      * type. It is not yet known if this is allowed. */
407     if (max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
408         av_log_ask_for_sample(m->avctx,
409                "Number of channels %d is larger than the maximum supported "
410                "by the decoder.\n", max_channel + 2);
411         return AVERROR_PATCHWELCOME;
412     }
413
414     if (min_channel > max_channel) {
415         av_log(m->avctx, AV_LOG_ERROR,
416                "Substream min channel cannot be greater than max channel.\n");
417         return AVERROR_INVALIDDATA;
418     }
419
420     s->min_channel = min_channel;
421     s->max_channel = max_channel;
422     s->max_matrix_channel = matrix_channel;
423
424     if (m->avctx->request_channels > 0
425         && s->max_channel + 1 >= m->avctx->request_channels
426         && substr < m->max_decoded_substream) {
427         av_log(m->avctx, AV_LOG_DEBUG,
428                "Extracting %d channel downmix from substream %d. "
429                "Further substreams will be skipped.\n",
430                s->max_channel + 1, substr);
431         m->max_decoded_substream = substr;
432     }
433
434     s->noise_shift   = get_bits(gbp,  4);
435     s->noisegen_seed = get_bits(gbp, 23);
436
437     skip_bits(gbp, 19);
438
439     s->data_check_present = get_bits1(gbp);
440     lossless_check = get_bits(gbp, 8);
441     if (substr == m->max_decoded_substream
442         && s->lossless_check_data != 0xffffffff) {
443         tmp = xor_32_to_8(s->lossless_check_data);
444         if (tmp != lossless_check)
445             av_log(m->avctx, AV_LOG_WARNING,
446                    "Lossless check failed - expected %02x, calculated %02x.\n",
447                    lossless_check, tmp);
448     }
449
450     skip_bits(gbp, 16);
451
452     memset(s->ch_assign, 0, sizeof(s->ch_assign));
453
454     for (ch = 0; ch <= s->max_matrix_channel; ch++) {
455         int ch_assign = get_bits(gbp, 6);
456         if (ch_assign > s->max_matrix_channel) {
457             av_log_ask_for_sample(m->avctx,
458                    "Assignment of matrix channel %d to invalid output channel %d.\n",
459                    ch, ch_assign);
460             return AVERROR_PATCHWELCOME;
461         }
462         s->ch_assign[ch_assign] = ch;
463     }
464
465     if (m->avctx->codec_id == CODEC_ID_MLP && m->needs_reordering) {
466         if (m->avctx->channel_layout == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
467             m->avctx->channel_layout == AV_CH_LAYOUT_5POINT0_BACK) {
468             int i = s->ch_assign[4];
469             s->ch_assign[4] = s->ch_assign[3];
470             s->ch_assign[3] = s->ch_assign[2];
471             s->ch_assign[2] = i;
472         } else if (m->avctx->channel_layout == AV_CH_LAYOUT_5POINT1_BACK) {
473             FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
474             FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
475         }
476     }
477     if (m->avctx->codec_id == CODEC_ID_TRUEHD &&
478         (m->avctx->channel_layout == AV_CH_LAYOUT_7POINT1 ||
479         m->avctx->channel_layout == AV_CH_LAYOUT_7POINT1_WIDE)) {
480         FFSWAP(int, s->ch_assign[4], s->ch_assign[6]);
481         FFSWAP(int, s->ch_assign[5], s->ch_assign[7]);
482     } else if (m->avctx->codec_id == CODEC_ID_TRUEHD &&
483         (m->avctx->channel_layout == AV_CH_LAYOUT_6POINT1 ||
484         m->avctx->channel_layout == (AV_CH_LAYOUT_6POINT1 | AV_CH_TOP_CENTER) ||
485         m->avctx->channel_layout == (AV_CH_LAYOUT_6POINT1 | AV_CH_TOP_FRONT_CENTER))) {
486         int i = s->ch_assign[6];
487         s->ch_assign[6] = s->ch_assign[5];
488         s->ch_assign[5] = s->ch_assign[4];
489         s->ch_assign[4] = i;
490     }
491
492     checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
493
494     if (checksum != get_bits(gbp, 8))
495         av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
496
497     /* Set default decoding parameters. */
498     s->param_presence_flags   = 0xff;
499     s->num_primitive_matrices = 0;
500     s->blocksize              = 8;
501     s->lossless_check_data    = 0;
502
503     memset(s->output_shift   , 0, sizeof(s->output_shift   ));
504     memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
505
506     for (ch = s->min_channel; ch <= s->max_channel; ch++) {
507         ChannelParams *cp = &s->channel_params[ch];
508         cp->filter_params[FIR].order = 0;
509         cp->filter_params[IIR].order = 0;
510         cp->filter_params[FIR].shift = 0;
511         cp->filter_params[IIR].shift = 0;
512
513         /* Default audio coding is 24-bit raw PCM. */
514         cp->huff_offset      = 0;
515         cp->sign_huff_offset = (-1) << 23;
516         cp->codebook         = 0;
517         cp->huff_lsbs        = 24;
518     }
519
520     if (substr == m->max_decoded_substream)
521         m->avctx->channels = s->max_matrix_channel + 1;
522
523     return 0;
524 }
525
526 /** Read parameters for one of the prediction filters. */
527
528 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
529                               unsigned int substr, unsigned int channel,
530                               unsigned int filter)
531 {
532     SubStream *s = &m->substream[substr];
533     FilterParams *fp = &s->channel_params[channel].filter_params[filter];
534     const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
535     const char fchar = filter ? 'I' : 'F';
536     int i, order;
537
538     // Filter is 0 for FIR, 1 for IIR.
539     assert(filter < 2);
540
541     if (m->filter_changed[channel][filter]++ > 1) {
542         av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
543         return AVERROR_INVALIDDATA;
544     }
545
546     order = get_bits(gbp, 4);
547     if (order > max_order) {
548         av_log(m->avctx, AV_LOG_ERROR,
549                "%cIR filter order %d is greater than maximum %d.\n",
550                fchar, order, max_order);
551         return AVERROR_INVALIDDATA;
552     }
553     fp->order = order;
554
555     if (order > 0) {
556         int32_t *fcoeff = s->channel_params[channel].coeff[filter];
557         int coeff_bits, coeff_shift;
558
559         fp->shift = get_bits(gbp, 4);
560
561         coeff_bits  = get_bits(gbp, 5);
562         coeff_shift = get_bits(gbp, 3);
563         if (coeff_bits < 1 || coeff_bits > 16) {
564             av_log(m->avctx, AV_LOG_ERROR,
565                    "%cIR filter coeff_bits must be between 1 and 16.\n",
566                    fchar);
567             return AVERROR_INVALIDDATA;
568         }
569         if (coeff_bits + coeff_shift > 16) {
570             av_log(m->avctx, AV_LOG_ERROR,
571                    "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
572                    fchar);
573             return AVERROR_INVALIDDATA;
574         }
575
576         for (i = 0; i < order; i++)
577             fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
578
579         if (get_bits1(gbp)) {
580             int state_bits, state_shift;
581
582             if (filter == FIR) {
583                 av_log(m->avctx, AV_LOG_ERROR,
584                        "FIR filter has state data specified.\n");
585                 return AVERROR_INVALIDDATA;
586             }
587
588             state_bits  = get_bits(gbp, 4);
589             state_shift = get_bits(gbp, 4);
590
591             /* TODO: Check validity of state data. */
592
593             for (i = 0; i < order; i++)
594                 fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
595         }
596     }
597
598     return 0;
599 }
600
601 /** Read parameters for primitive matrices. */
602
603 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
604 {
605     SubStream *s = &m->substream[substr];
606     unsigned int mat, ch;
607     const int max_primitive_matrices = m->avctx->codec_id == CODEC_ID_MLP
608                                      ? MAX_MATRICES_MLP
609                                      : MAX_MATRICES_TRUEHD;
610
611     if (m->matrix_changed++ > 1) {
612         av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
613         return AVERROR_INVALIDDATA;
614     }
615
616     s->num_primitive_matrices = get_bits(gbp, 4);
617
618     if (s->num_primitive_matrices > max_primitive_matrices) {
619         av_log(m->avctx, AV_LOG_ERROR,
620                "Number of primitive matrices cannot be greater than %d.\n",
621                max_primitive_matrices);
622         return AVERROR_INVALIDDATA;
623     }
624
625     for (mat = 0; mat < s->num_primitive_matrices; mat++) {
626         int frac_bits, max_chan;
627         s->matrix_out_ch[mat] = get_bits(gbp, 4);
628         frac_bits             = get_bits(gbp, 4);
629         s->lsb_bypass   [mat] = get_bits1(gbp);
630
631         if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
632             av_log(m->avctx, AV_LOG_ERROR,
633                     "Invalid channel %d specified as output from matrix.\n",
634                     s->matrix_out_ch[mat]);
635             return AVERROR_INVALIDDATA;
636         }
637         if (frac_bits > 14) {
638             av_log(m->avctx, AV_LOG_ERROR,
639                     "Too many fractional bits specified.\n");
640             return AVERROR_INVALIDDATA;
641         }
642
643         max_chan = s->max_matrix_channel;
644         if (!s->noise_type)
645             max_chan+=2;
646
647         for (ch = 0; ch <= max_chan; ch++) {
648             int coeff_val = 0;
649             if (get_bits1(gbp))
650                 coeff_val = get_sbits(gbp, frac_bits + 2);
651
652             s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
653         }
654
655         if (s->noise_type)
656             s->matrix_noise_shift[mat] = get_bits(gbp, 4);
657         else
658             s->matrix_noise_shift[mat] = 0;
659     }
660
661     return 0;
662 }
663
664 /** Read channel parameters. */
665
666 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
667                                GetBitContext *gbp, unsigned int ch)
668 {
669     SubStream *s = &m->substream[substr];
670     ChannelParams *cp = &s->channel_params[ch];
671     FilterParams *fir = &cp->filter_params[FIR];
672     FilterParams *iir = &cp->filter_params[IIR];
673     int ret;
674
675     if (s->param_presence_flags & PARAM_FIR)
676         if (get_bits1(gbp))
677             if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
678                 return ret;
679
680     if (s->param_presence_flags & PARAM_IIR)
681         if (get_bits1(gbp))
682             if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
683                 return ret;
684
685     if (fir->order + iir->order > 8) {
686         av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
687         return AVERROR_INVALIDDATA;
688     }
689
690     if (fir->order && iir->order &&
691         fir->shift != iir->shift) {
692         av_log(m->avctx, AV_LOG_ERROR,
693                 "FIR and IIR filters must use the same precision.\n");
694         return AVERROR_INVALIDDATA;
695     }
696     /* The FIR and IIR filters must have the same precision.
697      * To simplify the filtering code, only the precision of the
698      * FIR filter is considered. If only the IIR filter is employed,
699      * the FIR filter precision is set to that of the IIR filter, so
700      * that the filtering code can use it. */
701     if (!fir->order && iir->order)
702         fir->shift = iir->shift;
703
704     if (s->param_presence_flags & PARAM_HUFFOFFSET)
705         if (get_bits1(gbp))
706             cp->huff_offset = get_sbits(gbp, 15);
707
708     cp->codebook  = get_bits(gbp, 2);
709     cp->huff_lsbs = get_bits(gbp, 5);
710
711     if (cp->huff_lsbs > 24) {
712         av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
713         return AVERROR_INVALIDDATA;
714     }
715
716     cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
717
718     return 0;
719 }
720
721 /** Read decoding parameters that change more often than those in the restart
722  *  header. */
723
724 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
725                                 unsigned int substr)
726 {
727     SubStream *s = &m->substream[substr];
728     unsigned int ch;
729     int ret;
730
731     if (s->param_presence_flags & PARAM_PRESENCE)
732         if (get_bits1(gbp))
733             s->param_presence_flags = get_bits(gbp, 8);
734
735     if (s->param_presence_flags & PARAM_BLOCKSIZE)
736         if (get_bits1(gbp)) {
737             s->blocksize = get_bits(gbp, 9);
738             if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
739                 av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
740                 s->blocksize = 0;
741                 return AVERROR_INVALIDDATA;
742             }
743         }
744
745     if (s->param_presence_flags & PARAM_MATRIX)
746         if (get_bits1(gbp))
747             if ((ret = read_matrix_params(m, substr, gbp)) < 0)
748                 return ret;
749
750     if (s->param_presence_flags & PARAM_OUTSHIFT)
751         if (get_bits1(gbp))
752             for (ch = 0; ch <= s->max_matrix_channel; ch++)
753                 s->output_shift[ch] = get_sbits(gbp, 4);
754
755     if (s->param_presence_flags & PARAM_QUANTSTEP)
756         if (get_bits1(gbp))
757             for (ch = 0; ch <= s->max_channel; ch++) {
758                 ChannelParams *cp = &s->channel_params[ch];
759
760                 s->quant_step_size[ch] = get_bits(gbp, 4);
761
762                 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
763             }
764
765     for (ch = s->min_channel; ch <= s->max_channel; ch++)
766         if (get_bits1(gbp))
767             if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
768                 return ret;
769
770     return 0;
771 }
772
773 #define MSB_MASK(bits)  (-1u << bits)
774
775 /** Generate PCM samples using the prediction filters and residual values
776  *  read from the data stream, and update the filter state. */
777
778 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
779                            unsigned int channel)
780 {
781     SubStream *s = &m->substream[substr];
782     const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
783     int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
784     int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
785     int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
786     FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
787     FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
788     unsigned int filter_shift = fir->shift;
789     int32_t mask = MSB_MASK(s->quant_step_size[channel]);
790
791     memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
792     memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
793
794     m->dsp.mlp_filter_channel(firbuf, fircoeff,
795                               fir->order, iir->order,
796                               filter_shift, mask, s->blocksize,
797                               &m->sample_buffer[s->blockpos][channel]);
798
799     memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
800     memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
801 }
802
803 /** Read a block of PCM residual data (or actual if no filtering active). */
804
805 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
806                            unsigned int substr)
807 {
808     SubStream *s = &m->substream[substr];
809     unsigned int i, ch, expected_stream_pos = 0;
810     int ret;
811
812     if (s->data_check_present) {
813         expected_stream_pos  = get_bits_count(gbp);
814         expected_stream_pos += get_bits(gbp, 16);
815         av_log_ask_for_sample(m->avctx, "This file contains some features "
816                               "we have not tested yet.\n");
817     }
818
819     if (s->blockpos + s->blocksize > m->access_unit_size) {
820         av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
821         return AVERROR_INVALIDDATA;
822     }
823
824     memset(&m->bypassed_lsbs[s->blockpos][0], 0,
825            s->blocksize * sizeof(m->bypassed_lsbs[0]));
826
827     for (i = 0; i < s->blocksize; i++)
828         if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
829             return ret;
830
831     for (ch = s->min_channel; ch <= s->max_channel; ch++)
832         filter_channel(m, substr, ch);
833
834     s->blockpos += s->blocksize;
835
836     if (s->data_check_present) {
837         if (get_bits_count(gbp) != expected_stream_pos)
838             av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
839         skip_bits(gbp, 8);
840     }
841
842     return 0;
843 }
844
845 /** Data table used for TrueHD noise generation function. */
846
847 static const int8_t noise_table[256] = {
848      30,  51,  22,  54,   3,   7,  -4,  38,  14,  55,  46,  81,  22,  58,  -3,   2,
849      52,  31,  -7,  51,  15,  44,  74,  30,  85, -17,  10,  33,  18,  80,  28,  62,
850      10,  32,  23,  69,  72,  26,  35,  17,  73,  60,   8,  56,   2,   6,  -2,  -5,
851      51,   4,  11,  50,  66,  76,  21,  44,  33,  47,   1,  26,  64,  48,  57,  40,
852      38,  16, -10, -28,  92,  22, -18,  29, -10,   5, -13,  49,  19,  24,  70,  34,
853      61,  48,  30,  14,  -6,  25,  58,  33,  42,  60,  67,  17,  54,  17,  22,  30,
854      67,  44,  -9,  50, -11,  43,  40,  32,  59,  82,  13,  49, -14,  55,  60,  36,
855      48,  49,  31,  47,  15,  12,   4,  65,   1,  23,  29,  39,  45,  -2,  84,  69,
856       0,  72,  37,  57,  27,  41, -15, -16,  35,  31,  14,  61,  24,   0,  27,  24,
857      16,  41,  55,  34,  53,   9,  56,  12,  25,  29,  53,   5,  20, -20,  -8,  20,
858      13,  28,  -3,  78,  38,  16,  11,  62,  46,  29,  21,  24,  46,  65,  43, -23,
859      89,  18,  74,  21,  38, -12,  19,  12, -19,   8,  15,  33,   4,  57,   9,  -8,
860      36,  35,  26,  28,   7,  83,  63,  79,  75,  11,   3,  87,  37,  47,  34,  40,
861      39,  19,  20,  42,  27,  34,  39,  77,  13,  42,  59,  64,  45,  -1,  32,  37,
862      45,  -5,  53,  -6,   7,  36,  50,  23,   6,  32,   9, -21,  18,  71,  27,  52,
863     -25,  31,  35,  42,  -1,  68,  63,  52,  26,  43,  66,  37,  41,  25,  40,  70,
864 };
865
866 /** Noise generation functions.
867  *  I'm not sure what these are for - they seem to be some kind of pseudorandom
868  *  sequence generators, used to generate noise data which is used when the
869  *  channels are rematrixed. I'm not sure if they provide a practical benefit
870  *  to compression, or just obfuscate the decoder. Are they for some kind of
871  *  dithering? */
872
873 /** Generate two channels of noise, used in the matrix when
874  *  restart sync word == 0x31ea. */
875
876 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
877 {
878     SubStream *s = &m->substream[substr];
879     unsigned int i;
880     uint32_t seed = s->noisegen_seed;
881     unsigned int maxchan = s->max_matrix_channel;
882
883     for (i = 0; i < s->blockpos; i++) {
884         uint16_t seed_shr7 = seed >> 7;
885         m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
886         m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7)   << s->noise_shift;
887
888         seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
889     }
890
891     s->noisegen_seed = seed;
892 }
893
894 /** Generate a block of noise, used when restart sync word == 0x31eb. */
895
896 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
897 {
898     SubStream *s = &m->substream[substr];
899     unsigned int i;
900     uint32_t seed = s->noisegen_seed;
901
902     for (i = 0; i < m->access_unit_size_pow2; i++) {
903         uint8_t seed_shr15 = seed >> 15;
904         m->noise_buffer[i] = noise_table[seed_shr15];
905         seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
906     }
907
908     s->noisegen_seed = seed;
909 }
910
911
912 /** Apply the channel matrices in turn to reconstruct the original audio
913  *  samples. */
914
915 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
916 {
917     SubStream *s = &m->substream[substr];
918     unsigned int mat, src_ch, i;
919     unsigned int maxchan;
920
921     maxchan = s->max_matrix_channel;
922     if (!s->noise_type) {
923         generate_2_noise_channels(m, substr);
924         maxchan += 2;
925     } else {
926         fill_noise_buffer(m, substr);
927     }
928
929     for (mat = 0; mat < s->num_primitive_matrices; mat++) {
930         int matrix_noise_shift = s->matrix_noise_shift[mat];
931         unsigned int dest_ch = s->matrix_out_ch[mat];
932         int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
933         int32_t *coeffs = s->matrix_coeff[mat];
934         int index  = s->num_primitive_matrices - mat;
935         int index2 = 2 * index + 1;
936
937         /* TODO: DSPContext? */
938
939         for (i = 0; i < s->blockpos; i++) {
940             int32_t bypassed_lsb = m->bypassed_lsbs[i][mat];
941             int32_t *samples = m->sample_buffer[i];
942             int64_t accum = 0;
943
944             for (src_ch = 0; src_ch <= maxchan; src_ch++)
945                 accum += (int64_t) samples[src_ch] * coeffs[src_ch];
946
947             if (matrix_noise_shift) {
948                 index &= m->access_unit_size_pow2 - 1;
949                 accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
950                 index += index2;
951             }
952
953             samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
954         }
955     }
956 }
957
958 /** Write the audio data into the output buffer. */
959
960 static int output_data(MLPDecodeContext *m, unsigned int substr,
961                        void *data, int *got_frame_ptr)
962 {
963     AVCodecContext *avctx = m->avctx;
964     SubStream *s = &m->substream[substr];
965     unsigned int i, out_ch = 0;
966     int32_t *data_32;
967     int16_t *data_16;
968     int ret;
969     int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
970
971     if (m->avctx->channels != s->max_matrix_channel + 1) {
972         av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
973         return AVERROR_INVALIDDATA;
974     }
975
976     /* get output buffer */
977     m->frame.nb_samples = s->blockpos;
978     if ((ret = avctx->get_buffer(avctx, &m->frame)) < 0) {
979         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
980         return ret;
981     }
982     data_32 = (int32_t *)m->frame.data[0];
983     data_16 = (int16_t *)m->frame.data[0];
984
985     for (i = 0; i < s->blockpos; i++) {
986         for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
987             int mat_ch = s->ch_assign[out_ch];
988             int32_t sample = m->sample_buffer[i][mat_ch]
989                           << s->output_shift[mat_ch];
990             s->lossless_check_data ^= (sample & 0xffffff) << mat_ch;
991             if (is32) *data_32++ = sample << 8;
992             else      *data_16++ = sample >> 8;
993         }
994     }
995
996     *got_frame_ptr   = 1;
997     *(AVFrame *)data = m->frame;
998
999     return 0;
1000 }
1001
1002 /** Read an access unit from the stream.
1003  *  @return negative on error, 0 if not enough data is present in the input stream,
1004  *  otherwise the number of bytes consumed. */
1005
1006 static int read_access_unit(AVCodecContext *avctx, void* data,
1007                             int *got_frame_ptr, AVPacket *avpkt)
1008 {
1009     const uint8_t *buf = avpkt->data;
1010     int buf_size = avpkt->size;
1011     MLPDecodeContext *m = avctx->priv_data;
1012     GetBitContext gb;
1013     unsigned int length, substr;
1014     unsigned int substream_start;
1015     unsigned int header_size = 4;
1016     unsigned int substr_header_size = 0;
1017     uint8_t substream_parity_present[MAX_SUBSTREAMS];
1018     uint16_t substream_data_len[MAX_SUBSTREAMS];
1019     uint8_t parity_bits;
1020     int ret;
1021
1022     if (buf_size < 4)
1023         return 0;
1024
1025     length = (AV_RB16(buf) & 0xfff) * 2;
1026
1027     if (length < 4 || length > buf_size)
1028         return AVERROR_INVALIDDATA;
1029
1030     init_get_bits(&gb, (buf + 4), (length - 4) * 8);
1031
1032     m->is_major_sync_unit = 0;
1033     if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
1034         if (read_major_sync(m, &gb) < 0)
1035             goto error;
1036         m->is_major_sync_unit = 1;
1037         header_size += 28;
1038     }
1039
1040     if (!m->params_valid) {
1041         av_log(m->avctx, AV_LOG_WARNING,
1042                "Stream parameters not seen; skipping frame.\n");
1043         *got_frame_ptr = 0;
1044         return length;
1045     }
1046
1047     substream_start = 0;
1048
1049     for (substr = 0; substr < m->num_substreams; substr++) {
1050         int extraword_present, checkdata_present, end, nonrestart_substr;
1051
1052         extraword_present = get_bits1(&gb);
1053         nonrestart_substr = get_bits1(&gb);
1054         checkdata_present = get_bits1(&gb);
1055         skip_bits1(&gb);
1056
1057         end = get_bits(&gb, 12) * 2;
1058
1059         substr_header_size += 2;
1060
1061         if (extraword_present) {
1062             if (m->avctx->codec_id == CODEC_ID_MLP) {
1063                 av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
1064                 goto error;
1065             }
1066             skip_bits(&gb, 16);
1067             substr_header_size += 2;
1068         }
1069
1070         if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1071             av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
1072             goto error;
1073         }
1074
1075         if (end + header_size + substr_header_size > length) {
1076             av_log(m->avctx, AV_LOG_ERROR,
1077                    "Indicated length of substream %d data goes off end of "
1078                    "packet.\n", substr);
1079             end = length - header_size - substr_header_size;
1080         }
1081
1082         if (end < substream_start) {
1083             av_log(avctx, AV_LOG_ERROR,
1084                    "Indicated end offset of substream %d data "
1085                    "is smaller than calculated start offset.\n",
1086                    substr);
1087             goto error;
1088         }
1089
1090         if (substr > m->max_decoded_substream)
1091             continue;
1092
1093         substream_parity_present[substr] = checkdata_present;
1094         substream_data_len[substr] = end - substream_start;
1095         substream_start = end;
1096     }
1097
1098     parity_bits  = ff_mlp_calculate_parity(buf, 4);
1099     parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1100
1101     if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1102         av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1103         goto error;
1104     }
1105
1106     buf += header_size + substr_header_size;
1107
1108     for (substr = 0; substr <= m->max_decoded_substream; substr++) {
1109         SubStream *s = &m->substream[substr];
1110         init_get_bits(&gb, buf, substream_data_len[substr] * 8);
1111
1112         m->matrix_changed = 0;
1113         memset(m->filter_changed, 0, sizeof(m->filter_changed));
1114
1115         s->blockpos = 0;
1116         do {
1117             if (get_bits1(&gb)) {
1118                 if (get_bits1(&gb)) {
1119                     /* A restart header should be present. */
1120                     if (read_restart_header(m, &gb, buf, substr) < 0)
1121                         goto next_substr;
1122                     s->restart_seen = 1;
1123                 }
1124
1125                 if (!s->restart_seen)
1126                     goto next_substr;
1127                 if (read_decoding_params(m, &gb, substr) < 0)
1128                     goto next_substr;
1129             }
1130
1131             if (!s->restart_seen)
1132                 goto next_substr;
1133
1134             if ((ret = read_block_data(m, &gb, substr)) < 0)
1135                 return ret;
1136
1137             if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1138                 goto substream_length_mismatch;
1139
1140         } while (!get_bits1(&gb));
1141
1142         skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1143
1144         if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1145             int shorten_by;
1146
1147             if (get_bits(&gb, 16) != 0xD234)
1148                 return AVERROR_INVALIDDATA;
1149
1150             shorten_by = get_bits(&gb, 16);
1151             if      (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by  & 0x2000)
1152                 s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1153             else if (m->avctx->codec_id == CODEC_ID_MLP    && shorten_by != 0xD234)
1154                 return AVERROR_INVALIDDATA;
1155
1156             if (substr == m->max_decoded_substream)
1157                 av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1158         }
1159
1160         if (substream_parity_present[substr]) {
1161             uint8_t parity, checksum;
1162
1163             if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1164                 goto substream_length_mismatch;
1165
1166             parity   = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1167             checksum = ff_mlp_checksum8       (buf, substream_data_len[substr] - 2);
1168
1169             if ((get_bits(&gb, 8) ^ parity) != 0xa9    )
1170                 av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
1171             if ( get_bits(&gb, 8)           != checksum)
1172                 av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n"    , substr);
1173         }
1174
1175         if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1176             goto substream_length_mismatch;
1177
1178 next_substr:
1179         if (!s->restart_seen)
1180             av_log(m->avctx, AV_LOG_ERROR,
1181                    "No restart header present in substream %d.\n", substr);
1182
1183         buf += substream_data_len[substr];
1184     }
1185
1186     rematrix_channels(m, m->max_decoded_substream);
1187
1188     if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
1189         return ret;
1190
1191     return length;
1192
1193 substream_length_mismatch:
1194     av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1195     return AVERROR_INVALIDDATA;
1196
1197 error:
1198     m->params_valid = 0;
1199     return AVERROR_INVALIDDATA;
1200 }
1201
1202 #if CONFIG_MLP_DECODER
1203 AVCodec ff_mlp_decoder = {
1204     .name           = "mlp",
1205     .type           = AVMEDIA_TYPE_AUDIO,
1206     .id             = CODEC_ID_MLP,
1207     .priv_data_size = sizeof(MLPDecodeContext),
1208     .init           = mlp_decode_init,
1209     .decode         = read_access_unit,
1210     .capabilities   = CODEC_CAP_DR1,
1211     .long_name      = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1212 };
1213 #endif
1214 #if CONFIG_TRUEHD_DECODER
1215 AVCodec ff_truehd_decoder = {
1216     .name           = "truehd",
1217     .type           = AVMEDIA_TYPE_AUDIO,
1218     .id             = CODEC_ID_TRUEHD,
1219     .priv_data_size = sizeof(MLPDecodeContext),
1220     .init           = mlp_decode_init,
1221     .decode         = read_access_unit,
1222     .capabilities   = CODEC_CAP_DR1,
1223     .long_name      = NULL_IF_CONFIG_SMALL("TrueHD"),
1224 };
1225 #endif /* CONFIG_TRUEHD_DECODER */