3 * Copyright (c) 2007-2008 Ian Caulfield
5 * This file is part of FFmpeg.
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.
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.
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
31 #include "libavutil/intreadwrite.h"
33 #include "libavutil/crc.h"
35 #include "mlp_parser.h"
38 /** number of bits used for VLC lookup - longest Huffman code is 9 */
41 typedef struct SubStream {
42 /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
46 /** restart header data */
47 /// The type of noise to be used in the rematrix stage.
50 /// The index of the first channel coded in this substream.
52 /// The index of the last channel coded in this substream.
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];
59 /// Channel coding parameters for channels in the substream
60 ChannelParams channel_params[MAX_CHANNELS];
62 /// The left shift applied to random noise in 0x31ea substreams.
64 /// The current seed value for the pseudorandom noise generator(s).
65 uint32_t noisegen_seed;
67 /// Set if the substream contains extra info to check the size of VLC blocks.
68 uint8_t data_check_present;
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)
85 /// Number of matrices to be applied.
86 uint8_t num_primitive_matrices;
88 /// matrix output channel
89 uint8_t matrix_out_ch[MAX_MATRICES];
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];
99 /// Left shift to apply to Huffman-decoded residuals.
100 uint8_t quant_step_size[MAX_CHANNELS];
102 /// number of PCM samples in current audio block
104 /// Number of PCM samples decoded so far in this frame.
107 /// Left shift to apply to decoded PCM values to get final 24-bit output.
108 int8_t output_shift[MAX_CHANNELS];
110 /// Running XOR of all output samples.
111 int32_t lossless_check_data;
115 typedef struct MLPDecodeContext {
116 AVCodecContext *avctx;
119 /// Current access unit being read has a major sync.
120 int is_major_sync_unit;
122 /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
123 uint8_t params_valid;
125 /// Number of substreams contained within this stream.
126 uint8_t num_substreams;
128 /// Index of the last substream to decode - further substreams are skipped.
129 uint8_t max_decoded_substream;
131 /// Stream needs channel reordering to comply with FFmpeg's channel order
132 uint8_t needs_reordering;
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;
139 SubStream substream[MAX_SUBSTREAMS];
142 int filter_changed[MAX_CHANNELS][NUM_FILTERS];
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];
151 static VLC huff_vlc[3];
153 /** Initialize static data, constant between all invocations of the codec. */
155 static av_cold void init_static(void)
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);
172 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
173 unsigned int substr, unsigned int ch)
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;
181 if (cp->codebook > 0)
182 sign_huff_offset -= 7 << lsb_bits;
185 sign_huff_offset -= 1 << sign_shift;
187 return sign_huff_offset;
190 /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
193 static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
194 unsigned int substr, unsigned int pos)
196 SubStream *s = &m->substream[substr];
197 unsigned int mat, channel;
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);
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;
211 result = get_vlc2(gbp, huff_vlc[codebook-1].table,
212 VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
215 return AVERROR_INVALIDDATA;
218 result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
220 result += cp->sign_huff_offset;
221 result <<= quant_step_size;
223 m->sample_buffer[pos + s->blockpos][channel] = result;
229 static av_cold int mlp_decode_init(AVCodecContext *avctx)
231 MLPDecodeContext *m = avctx->priv_data;
236 for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
237 m->substream[substr].lossless_check_data = 0xffffffff;
238 ff_dsputil_init(&m->dsp, avctx);
240 avcodec_get_frame_defaults(&m->frame);
241 avctx->coded_frame = &m->frame;
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.
251 static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
256 if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
259 if (mh.group1_bits == 0) {
260 av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
261 return AVERROR_INVALIDDATA;
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;
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;
275 if (mh.group1_samplerate == 0) {
276 av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
277 return AVERROR_INVALIDDATA;
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;
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;
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;
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;
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;
311 m->access_unit_size = mh.access_unit_size;
312 m->access_unit_size_pow2 = mh.access_unit_size_pow2;
314 m->num_substreams = mh.num_substreams;
315 m->max_decoded_substream = m->num_substreams - 1;
317 m->avctx->sample_rate = mh.group1_samplerate;
318 m->avctx->frame_size = mh.access_unit_size;
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;
324 m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
327 for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
328 m->substream[substr].restart_seen = 0;
330 if (mh.stream_type == 0xbb) {
332 m->avctx->channel_layout = ff_mlp_layout[mh.channels_mlp];
333 } else { /* mh.stream_type == 0xba */
335 if (mh.channels_thd_stream2) {
336 m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream2);
338 m->avctx->channel_layout = ff_truehd_layout(mh.channels_thd_stream1);
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.");
348 m->needs_reordering = mh.channels_mlp >= 18 && mh.channels_mlp <= 20;
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. */
357 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
358 const uint8_t *buf, unsigned int substr)
360 SubStream *s = &m->substream[substr];
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;
371 sync_word = get_bits(gbp, 13);
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;
379 s->noise_type = get_bits1(gbp);
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;
386 skip_bits(gbp, 16); /* Output timestamp */
388 min_channel = get_bits(gbp, 4);
389 max_channel = get_bits(gbp, 4);
390 matrix_channel = get_bits(gbp, 4);
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",
396 return AVERROR_INVALIDDATA;
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;
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;
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;
420 s->min_channel = min_channel;
421 s->max_channel = max_channel;
422 s->max_matrix_channel = matrix_channel;
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;
434 s->noise_shift = get_bits(gbp, 4);
435 s->noisegen_seed = get_bits(gbp, 23);
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);
452 memset(s->ch_assign, 0, sizeof(s->ch_assign));
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",
460 return AVERROR_PATCHWELCOME;
462 s->ch_assign[ch_assign] = ch;
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];
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]);
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];
492 checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
494 if (checksum != get_bits(gbp, 8))
495 av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
497 /* Set default decoding parameters. */
498 s->param_presence_flags = 0xff;
499 s->num_primitive_matrices = 0;
501 s->lossless_check_data = 0;
503 memset(s->output_shift , 0, sizeof(s->output_shift ));
504 memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
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;
513 /* Default audio coding is 24-bit raw PCM. */
515 cp->sign_huff_offset = (-1) << 23;
520 if (substr == m->max_decoded_substream)
521 m->avctx->channels = s->max_matrix_channel + 1;
526 /** Read parameters for one of the prediction filters. */
528 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
529 unsigned int substr, unsigned int channel,
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';
538 // Filter is 0 for FIR, 1 for IIR.
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;
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;
556 int32_t *fcoeff = s->channel_params[channel].coeff[filter];
557 int coeff_bits, coeff_shift;
559 fp->shift = get_bits(gbp, 4);
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",
567 return AVERROR_INVALIDDATA;
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",
573 return AVERROR_INVALIDDATA;
576 for (i = 0; i < order; i++)
577 fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
579 if (get_bits1(gbp)) {
580 int state_bits, state_shift;
583 av_log(m->avctx, AV_LOG_ERROR,
584 "FIR filter has state data specified.\n");
585 return AVERROR_INVALIDDATA;
588 state_bits = get_bits(gbp, 4);
589 state_shift = get_bits(gbp, 4);
591 /* TODO: Check validity of state data. */
593 for (i = 0; i < order; i++)
594 fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
601 /** Read parameters for primitive matrices. */
603 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
605 SubStream *s = &m->substream[substr];
606 unsigned int mat, ch;
607 const int max_primitive_matrices = m->avctx->codec_id == CODEC_ID_MLP
609 : MAX_MATRICES_TRUEHD;
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;
616 s->num_primitive_matrices = get_bits(gbp, 4);
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;
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);
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;
637 if (frac_bits > 14) {
638 av_log(m->avctx, AV_LOG_ERROR,
639 "Too many fractional bits specified.\n");
640 return AVERROR_INVALIDDATA;
643 max_chan = s->max_matrix_channel;
647 for (ch = 0; ch <= max_chan; ch++) {
650 coeff_val = get_sbits(gbp, frac_bits + 2);
652 s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
656 s->matrix_noise_shift[mat] = get_bits(gbp, 4);
658 s->matrix_noise_shift[mat] = 0;
664 /** Read channel parameters. */
666 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
667 GetBitContext *gbp, unsigned int ch)
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];
675 if (s->param_presence_flags & PARAM_FIR)
677 if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
680 if (s->param_presence_flags & PARAM_IIR)
682 if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
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;
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;
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;
704 if (s->param_presence_flags & PARAM_HUFFOFFSET)
706 cp->huff_offset = get_sbits(gbp, 15);
708 cp->codebook = get_bits(gbp, 2);
709 cp->huff_lsbs = get_bits(gbp, 5);
711 if (cp->huff_lsbs > 24) {
712 av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
713 return AVERROR_INVALIDDATA;
716 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
721 /** Read decoding parameters that change more often than those in the restart
724 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
727 SubStream *s = &m->substream[substr];
731 if (s->param_presence_flags & PARAM_PRESENCE)
733 s->param_presence_flags = get_bits(gbp, 8);
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.");
741 return AVERROR_INVALIDDATA;
745 if (s->param_presence_flags & PARAM_MATRIX)
747 if ((ret = read_matrix_params(m, substr, gbp)) < 0)
750 if (s->param_presence_flags & PARAM_OUTSHIFT)
752 for (ch = 0; ch <= s->max_matrix_channel; ch++)
753 s->output_shift[ch] = get_sbits(gbp, 4);
755 if (s->param_presence_flags & PARAM_QUANTSTEP)
757 for (ch = 0; ch <= s->max_channel; ch++) {
758 ChannelParams *cp = &s->channel_params[ch];
760 s->quant_step_size[ch] = get_bits(gbp, 4);
762 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
765 for (ch = s->min_channel; ch <= s->max_channel; ch++)
767 if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
773 #define MSB_MASK(bits) (-1u << bits)
775 /** Generate PCM samples using the prediction filters and residual values
776 * read from the data stream, and update the filter state. */
778 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
779 unsigned int channel)
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]);
791 memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
792 memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
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]);
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));
803 /** Read a block of PCM residual data (or actual if no filtering active). */
805 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
808 SubStream *s = &m->substream[substr];
809 unsigned int i, ch, expected_stream_pos = 0;
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");
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;
824 memset(&m->bypassed_lsbs[s->blockpos][0], 0,
825 s->blocksize * sizeof(m->bypassed_lsbs[0]));
827 for (i = 0; i < s->blocksize; i++)
828 if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
831 for (ch = s->min_channel; ch <= s->max_channel; ch++)
832 filter_channel(m, substr, ch);
834 s->blockpos += s->blocksize;
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");
845 /** Data table used for TrueHD noise generation function. */
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,
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
873 /** Generate two channels of noise, used in the matrix when
874 * restart sync word == 0x31ea. */
876 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
878 SubStream *s = &m->substream[substr];
880 uint32_t seed = s->noisegen_seed;
881 unsigned int maxchan = s->max_matrix_channel;
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;
888 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
891 s->noisegen_seed = seed;
894 /** Generate a block of noise, used when restart sync word == 0x31eb. */
896 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
898 SubStream *s = &m->substream[substr];
900 uint32_t seed = s->noisegen_seed;
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);
908 s->noisegen_seed = seed;
912 /** Apply the channel matrices in turn to reconstruct the original audio
915 static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
917 SubStream *s = &m->substream[substr];
918 unsigned int mat, src_ch, i;
919 unsigned int maxchan;
921 maxchan = s->max_matrix_channel;
922 if (!s->noise_type) {
923 generate_2_noise_channels(m, substr);
926 fill_noise_buffer(m, substr);
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;
937 /* TODO: DSPContext? */
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];
944 for (src_ch = 0; src_ch <= maxchan; src_ch++)
945 accum += (int64_t) samples[src_ch] * coeffs[src_ch];
947 if (matrix_noise_shift) {
948 index &= m->access_unit_size_pow2 - 1;
949 accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
953 samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
958 /** Write the audio data into the output buffer. */
960 static int output_data(MLPDecodeContext *m, unsigned int substr,
961 void *data, int *got_frame_ptr)
963 AVCodecContext *avctx = m->avctx;
964 SubStream *s = &m->substream[substr];
965 unsigned int i, out_ch = 0;
969 int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
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;
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");
982 data_32 = (int32_t *)m->frame.data[0];
983 data_16 = (int16_t *)m->frame.data[0];
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;
997 *(AVFrame *)data = m->frame;
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. */
1006 static int read_access_unit(AVCodecContext *avctx, void* data,
1007 int *got_frame_ptr, AVPacket *avpkt)
1009 const uint8_t *buf = avpkt->data;
1010 int buf_size = avpkt->size;
1011 MLPDecodeContext *m = avctx->priv_data;
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;
1025 length = (AV_RB16(buf) & 0xfff) * 2;
1027 if (length < 4 || length > buf_size)
1028 return AVERROR_INVALIDDATA;
1030 init_get_bits(&gb, (buf + 4), (length - 4) * 8);
1032 m->is_major_sync_unit = 0;
1033 if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
1034 if (read_major_sync(m, &gb) < 0)
1036 m->is_major_sync_unit = 1;
1040 if (!m->params_valid) {
1041 av_log(m->avctx, AV_LOG_WARNING,
1042 "Stream parameters not seen; skipping frame.\n");
1047 substream_start = 0;
1049 for (substr = 0; substr < m->num_substreams; substr++) {
1050 int extraword_present, checkdata_present, end, nonrestart_substr;
1052 extraword_present = get_bits1(&gb);
1053 nonrestart_substr = get_bits1(&gb);
1054 checkdata_present = get_bits1(&gb);
1057 end = get_bits(&gb, 12) * 2;
1059 substr_header_size += 2;
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");
1067 substr_header_size += 2;
1070 if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1071 av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
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;
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",
1090 if (substr > m->max_decoded_substream)
1093 substream_parity_present[substr] = checkdata_present;
1094 substream_data_len[substr] = end - substream_start;
1095 substream_start = end;
1098 parity_bits = ff_mlp_calculate_parity(buf, 4);
1099 parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1101 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1102 av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1106 buf += header_size + substr_header_size;
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);
1112 m->matrix_changed = 0;
1113 memset(m->filter_changed, 0, sizeof(m->filter_changed));
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)
1122 s->restart_seen = 1;
1125 if (!s->restart_seen)
1127 if (read_decoding_params(m, &gb, substr) < 0)
1131 if (!s->restart_seen)
1134 if ((ret = read_block_data(m, &gb, substr)) < 0)
1137 if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1138 goto substream_length_mismatch;
1140 } while (!get_bits1(&gb));
1142 skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1144 if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1147 if (get_bits(&gb, 16) != 0xD234)
1148 return AVERROR_INVALIDDATA;
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;
1156 if (substr == m->max_decoded_substream)
1157 av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1160 if (substream_parity_present[substr]) {
1161 uint8_t parity, checksum;
1163 if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1164 goto substream_length_mismatch;
1166 parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1167 checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
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);
1175 if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1176 goto substream_length_mismatch;
1179 if (!s->restart_seen)
1180 av_log(m->avctx, AV_LOG_ERROR,
1181 "No restart header present in substream %d.\n", substr);
1183 buf += substream_data_len[substr];
1186 rematrix_channels(m, m->max_decoded_substream);
1188 if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
1193 substream_length_mismatch:
1194 av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1195 return AVERROR_INVALIDDATA;
1198 m->params_valid = 0;
1199 return AVERROR_INVALIDDATA;
1202 AVCodec ff_mlp_decoder = {
1204 .type = AVMEDIA_TYPE_AUDIO,
1206 .priv_data_size = sizeof(MLPDecodeContext),
1207 .init = mlp_decode_init,
1208 .decode = read_access_unit,
1209 .capabilities = CODEC_CAP_DR1,
1210 .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1213 #if CONFIG_TRUEHD_DECODER
1214 AVCodec ff_truehd_decoder = {
1216 .type = AVMEDIA_TYPE_AUDIO,
1217 .id = CODEC_ID_TRUEHD,
1218 .priv_data_size = sizeof(MLPDecodeContext),
1219 .init = mlp_decode_init,
1220 .decode = read_access_unit,
1221 .capabilities = CODEC_CAP_DR1,
1222 .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
1224 #endif /* CONFIG_TRUEHD_DECODER */