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