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