45
45
"a sample of this file.";
47
47
typedef struct SubStream {
48
//! Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
48
/// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
49
49
uint8_t restart_seen;
52
52
/** restart header data */
53
//! The type of noise to be used in the rematrix stage.
53
/// The type of noise to be used in the rematrix stage.
54
54
uint16_t noise_type;
56
//! The index of the first channel coded in this substream.
56
/// The index of the first channel coded in this substream.
57
57
uint8_t min_channel;
58
//! The index of the last channel coded in this substream.
58
/// The index of the last channel coded in this substream.
59
59
uint8_t max_channel;
60
//! The number of channels input into the rematrix stage.
60
/// The number of channels input into the rematrix stage.
61
61
uint8_t max_matrix_channel;
62
//! For each channel output by the matrix, the output channel to map it to
62
/// For each channel output by the matrix, the output channel to map it to
63
63
uint8_t ch_assign[MAX_CHANNELS];
65
//! Channel coding parameters for channels in the substream
65
/// Channel coding parameters for channels in the substream
66
66
ChannelParams channel_params[MAX_CHANNELS];
68
//! The left shift applied to random noise in 0x31ea substreams.
68
/// The left shift applied to random noise in 0x31ea substreams.
69
69
uint8_t noise_shift;
70
//! The current seed value for the pseudorandom noise generator(s).
70
/// The current seed value for the pseudorandom noise generator(s).
71
71
uint32_t noisegen_seed;
73
//! Set if the substream contains extra info to check the size of VLC blocks.
73
/// Set if the substream contains extra info to check the size of VLC blocks.
74
74
uint8_t data_check_present;
76
//! Bitmask of which parameter sets are conveyed in a decoding parameter block.
76
/// Bitmask of which parameter sets are conveyed in a decoding parameter block.
77
77
uint8_t param_presence_flags;
78
78
#define PARAM_BLOCKSIZE (1 << 7)
79
79
#define PARAM_MATRIX (1 << 6)
91
//! Number of matrices to be applied.
91
/// Number of matrices to be applied.
92
92
uint8_t num_primitive_matrices;
94
//! matrix output channel
94
/// matrix output channel
95
95
uint8_t matrix_out_ch[MAX_MATRICES];
97
//! Whether the LSBs of the matrix output are encoded in the bitstream.
97
/// Whether the LSBs of the matrix output are encoded in the bitstream.
98
98
uint8_t lsb_bypass[MAX_MATRICES];
99
//! Matrix coefficients, stored as 2.14 fixed point.
99
/// Matrix coefficients, stored as 2.14 fixed point.
100
100
int32_t matrix_coeff[MAX_MATRICES][MAX_CHANNELS];
101
//! Left shift to apply to noise values in 0x31eb substreams.
101
/// Left shift to apply to noise values in 0x31eb substreams.
102
102
uint8_t matrix_noise_shift[MAX_MATRICES];
105
//! Left shift to apply to Huffman-decoded residuals.
105
/// Left shift to apply to Huffman-decoded residuals.
106
106
uint8_t quant_step_size[MAX_CHANNELS];
108
//! number of PCM samples in current audio block
108
/// number of PCM samples in current audio block
109
109
uint16_t blocksize;
110
//! Number of PCM samples decoded so far in this frame.
110
/// Number of PCM samples decoded so far in this frame.
111
111
uint16_t blockpos;
113
//! Left shift to apply to decoded PCM values to get final 24-bit output.
113
/// Left shift to apply to decoded PCM values to get final 24-bit output.
114
114
int8_t output_shift[MAX_CHANNELS];
116
//! Running XOR of all output samples.
116
/// Running XOR of all output samples.
117
117
int32_t lossless_check_data;
121
121
typedef struct MLPDecodeContext {
122
122
AVCodecContext *avctx;
124
//! Current access unit being read has a major sync.
125
/// Current access unit being read has a major sync.
125
126
int is_major_sync_unit;
127
//! Set if a valid major sync block has been read. Otherwise no decoding is possible.
128
/// Set if a valid major sync block has been read. Otherwise no decoding is possible.
128
129
uint8_t params_valid;
130
//! Number of substreams contained within this stream.
131
/// Number of substreams contained within this stream.
131
132
uint8_t num_substreams;
133
//! Index of the last substream to decode - further substreams are skipped.
134
/// Index of the last substream to decode - further substreams are skipped.
134
135
uint8_t max_decoded_substream;
136
//! number of PCM samples contained in each frame
137
/// number of PCM samples contained in each frame
137
138
int access_unit_size;
138
//! next power of two above the number of samples in each frame
139
/// next power of two above the number of samples in each frame
139
140
int access_unit_size_pow2;
141
142
SubStream substream[MAX_SUBSTREAMS];
250
254
static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
252
256
MLPHeaderInfo mh;
255
if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0)
259
if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
258
262
if (mh.group1_bits == 0) {
259
263
av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
264
return AVERROR_INVALIDDATA;
262
266
if (mh.group2_bits > mh.group1_bits) {
263
267
av_log(m->avctx, AV_LOG_ERROR,
264
268
"Channel group 2 cannot have more bits per sample than group 1.\n");
269
return AVERROR_INVALIDDATA;
268
272
if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
269
273
av_log(m->avctx, AV_LOG_ERROR,
270
274
"Channel groups with differing sample rates are not currently supported.\n");
275
return AVERROR_INVALIDDATA;
274
278
if (mh.group1_samplerate == 0) {
275
279
av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
280
return AVERROR_INVALIDDATA;
278
282
if (mh.group1_samplerate > MAX_SAMPLERATE) {
279
283
av_log(m->avctx, AV_LOG_ERROR,
280
284
"Sampling rate %d is greater than the supported maximum (%d).\n",
281
285
mh.group1_samplerate, MAX_SAMPLERATE);
286
return AVERROR_INVALIDDATA;
284
288
if (mh.access_unit_size > MAX_BLOCKSIZE) {
285
289
av_log(m->avctx, AV_LOG_ERROR,
286
290
"Block size %d is greater than the supported maximum (%d).\n",
287
291
mh.access_unit_size, MAX_BLOCKSIZE);
292
return AVERROR_INVALIDDATA;
290
294
if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
291
295
av_log(m->avctx, AV_LOG_ERROR,
292
296
"Block size pow2 %d is greater than the supported maximum (%d).\n",
293
297
mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
298
return AVERROR_INVALIDDATA;
297
301
if (mh.num_substreams == 0)
302
return AVERROR_INVALIDDATA;
299
303
if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
300
304
av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
305
return AVERROR_INVALIDDATA;
303
307
if (mh.num_substreams > MAX_SUBSTREAMS) {
304
308
av_log(m->avctx, AV_LOG_ERROR,
305
309
"Number of substreams %d is larger than the maximum supported "
306
310
"by the decoder. %s\n", mh.num_substreams, sample_message);
311
return AVERROR_INVALIDDATA;
310
314
m->access_unit_size = mh.access_unit_size;
351
355
if (sync_word != 0x31ea >> 1) {
352
356
av_log(m->avctx, AV_LOG_ERROR,
353
357
"restart header sync incorrect (got 0x%04x)\n", sync_word);
358
return AVERROR_INVALIDDATA;
357
361
s->noise_type = get_bits1(gbp);
359
363
if (m->avctx->codec_id == CODEC_ID_MLP && s->noise_type) {
360
364
av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
365
return AVERROR_INVALIDDATA;
364
368
skip_bits(gbp, 16); /* Output timestamp */
371
375
av_log(m->avctx, AV_LOG_ERROR,
372
376
"Max matrix channel cannot be greater than %d.\n",
373
377
max_matrix_channel);
378
return AVERROR_INVALIDDATA;
377
381
if (s->max_channel != s->max_matrix_channel) {
378
382
av_log(m->avctx, AV_LOG_ERROR,
379
383
"Max channel must be equal max matrix channel.\n");
384
return AVERROR_INVALIDDATA;
383
387
/* This should happen for TrueHD streams with >6 channels and MLP's noise
386
390
av_log(m->avctx, AV_LOG_ERROR,
387
391
"Number of channels %d is larger than the maximum supported "
388
392
"by the decoder. %s\n", s->max_channel+2, sample_message);
393
return AVERROR_INVALIDDATA;
392
396
if (s->min_channel > s->max_channel) {
393
397
av_log(m->avctx, AV_LOG_ERROR,
394
398
"Substream min channel cannot be greater than max channel.\n");
399
return AVERROR_INVALIDDATA;
398
402
if (m->avctx->request_channels > 0
511
515
av_log(m->avctx, AV_LOG_ERROR,
512
516
"%cIR filter coeff_bits must be between 1 and 16.\n",
518
return AVERROR_INVALIDDATA;
516
520
if (coeff_bits + coeff_shift > 16) {
517
521
av_log(m->avctx, AV_LOG_ERROR,
518
522
"Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
524
return AVERROR_INVALIDDATA;
523
527
for (i = 0; i < order; i++)
579
583
av_log(m->avctx, AV_LOG_ERROR,
580
584
"Invalid channel %d specified as output from matrix.\n",
581
585
s->matrix_out_ch[mat]);
586
return AVERROR_INVALIDDATA;
584
588
if (frac_bits > 14) {
585
589
av_log(m->avctx, AV_LOG_ERROR,
586
590
"Too many fractional bits specified.\n");
591
return AVERROR_INVALIDDATA;
590
594
max_chan = s->max_matrix_channel;
617
621
ChannelParams *cp = &s->channel_params[ch];
618
622
FilterParams *fir = &cp->filter_params[FIR];
619
623
FilterParams *iir = &cp->filter_params[IIR];
621
626
if (s->param_presence_flags & PARAM_FIR)
622
627
if (get_bits1(gbp))
623
if (read_filter_params(m, gbp, substr, ch, FIR) < 0)
628
if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
626
631
if (s->param_presence_flags & PARAM_IIR)
627
632
if (get_bits1(gbp))
628
if (read_filter_params(m, gbp, substr, ch, IIR) < 0)
633
if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
631
636
if (fir->order + iir->order > 8) {
632
637
av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
638
return AVERROR_INVALIDDATA;
636
641
if (fir->order && iir->order &&
637
642
fir->shift != iir->shift) {
638
643
av_log(m->avctx, AV_LOG_ERROR,
639
644
"FIR and IIR filters must use the same precision.\n");
645
return AVERROR_INVALIDDATA;
642
647
/* The FIR and IIR filters must have the same precision.
643
648
* To simplify the filtering code, only the precision of the
683
689
if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
684
690
av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");
685
691
s->blocksize = 0;
692
return AVERROR_INVALIDDATA;
690
696
if (s->param_presence_flags & PARAM_MATRIX)
691
697
if (get_bits1(gbp))
692
if (read_matrix_params(m, substr, gbp) < 0)
698
if ((ret = read_matrix_params(m, substr, gbp)) < 0)
695
701
if (s->param_presence_flags & PARAM_OUTSHIFT)
696
702
if (get_bits1(gbp))
763
770
if (s->blockpos + s->blocksize > m->access_unit_size) {
764
771
av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
772
return AVERROR_INVALIDDATA;
768
775
memset(&m->bypassed_lsbs[s->blockpos][0], 0,
769
776
s->blocksize * sizeof(m->bypassed_lsbs[0]));
771
778
for (i = 0; i < s->blocksize; i++)
772
if (read_huff_channels(m, gbp, substr, i) < 0)
779
if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
775
782
for (ch = s->min_channel; ch <= s->max_channel; ch++)
776
783
filter_channel(m, substr, ch);
902
909
/** Write the audio data into the output buffer. */
904
static int output_data_internal(MLPDecodeContext *m, unsigned int substr,
905
uint8_t *data, unsigned int *data_size, int is32)
911
static int output_data(MLPDecodeContext *m, unsigned int substr,
912
void *data, int *got_frame_ptr)
914
AVCodecContext *avctx = m->avctx;
907
915
SubStream *s = &m->substream[substr];
908
916
unsigned int i, out_ch = 0;
909
int32_t *data_32 = (int32_t*) data;
910
int16_t *data_16 = (int16_t*) data;
912
if (*data_size < (s->max_channel + 1) * s->blockpos * (is32 ? 4 : 2))
920
int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
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;
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");
933
data_32 = (int32_t *)m->frame.data[0];
934
data_16 = (int16_t *)m->frame.data[0];
915
936
for (i = 0; i < s->blockpos; i++) {
916
937
for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
926
*data_size = i * out_ch * (is32 ? 4 : 2);
948
*(AVFrame *)data = m->frame;
931
static int output_data(MLPDecodeContext *m, unsigned int substr,
932
uint8_t *data, unsigned int *data_size)
934
if (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
935
return output_data_internal(m, substr, data, data_size, 1);
937
return output_data_internal(m, substr, data, data_size, 0);
941
953
/** Read an access unit from the stream.
942
954
* @return negative on error, 0 if not enough data is present in the input stream,
943
955
* otherwise the number of bytes consumed. */
945
static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
957
static int read_access_unit(AVCodecContext *avctx, void* data,
958
int *got_frame_ptr, AVPacket *avpkt)
948
960
const uint8_t *buf = avpkt->data;
949
961
int buf_size = avpkt->size;
1083
1096
int shorten_by;
1085
1098
if (get_bits(&gb, 16) != 0xD234)
1099
return AVERROR_INVALIDDATA;
1088
1101
shorten_by = get_bits(&gb, 16);
1089
1102
if (m->avctx->codec_id == CODEC_ID_TRUEHD && shorten_by & 0x2000)
1090
1103
s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1091
1104
else if (m->avctx->codec_id == CODEC_ID_MLP && shorten_by != 0xD234)
1105
return AVERROR_INVALIDDATA;
1094
1107
if (substr == m->max_decoded_substream)
1095
1108
av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
1124
1137
rematrix_channels(m, m->max_decoded_substream);
1126
if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
1139
if ((ret = output_data(m, m->max_decoded_substream, data, got_frame_ptr)) < 0)
1131
1144
substream_length_mismatch:
1132
1145
av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1146
return AVERROR_INVALIDDATA;
1136
1149
m->params_valid = 0;
1150
return AVERROR_INVALIDDATA;
1140
1153
AVCodec ff_mlp_decoder = {
1144
sizeof(MLPDecodeContext),
1155
.type = AVMEDIA_TYPE_AUDIO,
1157
.priv_data_size = sizeof(MLPDecodeContext),
1158
.init = mlp_decode_init,
1159
.decode = read_access_unit,
1160
.capabilities = CODEC_CAP_DR1,
1149
1161
.long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
1152
1164
#if CONFIG_TRUEHD_DECODER
1153
1165
AVCodec ff_truehd_decoder = {
1157
sizeof(MLPDecodeContext),
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,
1172
.capabilities = CODEC_CAP_DR1,
1162
1173
.long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
1164
1175
#endif /* CONFIG_TRUEHD_DECODER */