FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpdec.c
Date: 2026-01-23 19:11:46
Exec Total Coverage
Lines: 480 677 70.9%
Functions: 18 19 94.7%
Branches: 291 457 63.7%

Line Branch Exec Source
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 "config_components.h"
28
29 #include <stdint.h>
30
31 #include "avcodec.h"
32 #include "libavutil/attributes.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/mem_internal.h"
37 #include "libavutil/thread.h"
38 #include "libavutil/opt.h"
39 #include "codec_internal.h"
40 #include "decode.h"
41 #include "get_bits.h"
42 #include "mlp_parse.h"
43 #include "mlpdsp.h"
44 #include "mlp.h"
45 #include "config.h"
46 #include "profiles.h"
47
48 /** number of bits used for VLC lookup - longest Huffman code is 9 */
49 #if ARCH_ARM
50 #define VLC_BITS 5
51 #define VLC_STATIC_SIZE 64
52 #else
53 #define VLC_BITS 9
54 #define VLC_STATIC_SIZE 512
55 #endif
56
57 typedef struct SubStream {
58 /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
59 uint8_t restart_seen;
60 /// Set if end of stream is encountered
61 uint8_t end_of_stream;
62
63 //@{
64 /** restart header data */
65 /// The type of noise to be used in the rematrix stage.
66 uint16_t noise_type;
67
68 /// The index of the first channel coded in this substream.
69 uint8_t min_channel;
70 /// The index of the last channel coded in this substream.
71 uint8_t max_channel;
72 /// The coded channels mask in this substream.
73 uint64_t coded_channels;
74 /// The number of channels input into the rematrix stage.
75 uint8_t max_matrix_channel;
76 /// For each channel output by the matrix, the output channel to map it to
77 uint8_t ch_assign[MAX_CHANNELS];
78 /// The channel layout for this substream
79 uint64_t mask;
80 /// The matrix encoding mode for this substream
81 enum AVMatrixEncoding matrix_encoding;
82 enum AVMatrixEncoding prev_matrix_encoding;
83
84 /// Channel coding parameters for channels in the substream
85 ChannelParams channel_params[MAX_CHANNELS];
86
87 /// The left shift applied to random noise in 0x31ea substreams.
88 uint8_t noise_shift;
89 /// The current seed value for the pseudorandom noise generator(s).
90 uint32_t noisegen_seed;
91
92 /// Set if the substream contains extra info to check the size of VLC blocks.
93 uint8_t data_check_present;
94
95 /// Bitmask of which parameter sets are conveyed in a decoding parameter block.
96 uint8_t param_presence_flags;
97 //@}
98
99 //@{
100 /** matrix data */
101
102 /// Number of matrices to be applied.
103 uint8_t num_primitive_matrices;
104
105 /// matrix output channel
106 uint8_t matrix_out_ch[MAX_MATRICES];
107
108 /// Whether the LSBs of the matrix output are encoded in the bitstream.
109 uint8_t lsb_bypass[MAX_MATRICES];
110 /// Matrix coefficients, stored as 2.14 fixed point.
111 DECLARE_ALIGNED(32, int32_t, matrix_coeff)[MAX_MATRICES][MAX_CHANNELS];
112 /// Left shift to apply to noise values in 0x31eb substreams.
113 uint8_t matrix_noise_shift[MAX_MATRICES];
114 //@}
115
116 /// Left shift to apply to Huffman-decoded residuals.
117 uint8_t quant_step_size[MAX_CHANNELS];
118
119 /// number of PCM samples in current audio block
120 uint16_t blocksize;
121 /// Number of PCM samples decoded so far in this frame.
122 uint16_t blockpos;
123
124 /// Left shift to apply to decoded PCM values to get final 24-bit output.
125 int8_t output_shift[MAX_CHANNELS];
126
127 /// Running XOR of all output samples.
128 int32_t lossless_check_data;
129
130 } SubStream;
131
132 typedef struct MLPDecodeContext {
133 const AVClass *class;
134 AVCodecContext *avctx;
135
136 AVChannelLayout downmix_layout;
137
138 /// Current access unit being read has a major sync.
139 int is_major_sync_unit;
140
141 /// Size of the major sync unit, in bytes
142 int major_sync_header_size;
143
144 /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
145 uint8_t params_valid;
146
147 /// Number of substreams contained within this stream.
148 uint8_t num_substreams;
149
150 /// Which substream of substreams carry 16-channel presentation
151 uint8_t extended_substream_info;
152
153 /// Which substream of substreams carry 2/6/8-channel presentation
154 uint8_t substream_info;
155
156 /// Index of the last substream to decode - further substreams are skipped.
157 uint8_t max_decoded_substream;
158
159 /// Stream needs channel reordering to comply with FFmpeg's channel order
160 uint8_t needs_reordering;
161
162 /// number of PCM samples contained in each frame
163 int access_unit_size;
164 /// next power of two above the number of samples in each frame
165 int access_unit_size_pow2;
166
167 SubStream substream[MAX_SUBSTREAMS];
168
169 int matrix_changed;
170 int filter_changed[MAX_CHANNELS][NUM_FILTERS];
171
172 int8_t noise_buffer[MAX_BLOCKSIZE_POW2];
173 int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
174 DECLARE_ALIGNED(32, int32_t, sample_buffer)[MAX_BLOCKSIZE][MAX_CHANNELS];
175
176 MLPDSPContext dsp;
177 int32_t (*pack_output)(int32_t lossless_check_data,
178 uint16_t blockpos,
179 int32_t (*sample_buffer)[MAX_CHANNELS],
180 void *data,
181 uint8_t *ch_assign,
182 int8_t *output_shift,
183 uint8_t max_matrix_channel,
184 int is32);
185 } MLPDecodeContext;
186
187 static const enum AVChannel thd_channel_order[] = {
188 AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, // LR
189 AV_CHAN_FRONT_CENTER, // C
190 AV_CHAN_LOW_FREQUENCY, // LFE
191 AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT, // LRs
192 AV_CHAN_TOP_FRONT_LEFT, AV_CHAN_TOP_FRONT_RIGHT, // LRvh
193 AV_CHAN_FRONT_LEFT_OF_CENTER, AV_CHAN_FRONT_RIGHT_OF_CENTER, // LRc
194 AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT, // LRrs
195 AV_CHAN_BACK_CENTER, // Cs
196 AV_CHAN_TOP_CENTER, // Ts
197 AV_CHAN_SURROUND_DIRECT_LEFT, AV_CHAN_SURROUND_DIRECT_RIGHT, // LRsd
198 AV_CHAN_WIDE_LEFT, AV_CHAN_WIDE_RIGHT, // LRw
199 AV_CHAN_TOP_FRONT_CENTER, // Cvh
200 AV_CHAN_LOW_FREQUENCY_2, // LFE2
201 };
202
203 2220 static int mlp_channel_layout_subset(AVChannelLayout *layout, uint64_t mask)
204 {
205
3/4
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 2005 times.
✓ Branch 3 taken 215 times.
✗ Branch 4 not taken.
2435 return av_channel_layout_check(layout) &&
206 215 av_channel_layout_subset(layout, mask) ==
207 215 av_channel_layout_subset(layout, UINT64_MAX);
208 }
209
210 3958 static enum AVChannel thd_channel_layout_extract_channel(uint64_t channel_layout,
211 int index)
212 {
213 int i;
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3958 times.
3958 if (av_popcount64(channel_layout) <= index)
216 return AV_CHAN_NONE;
217
218
1/2
✓ Branch 0 taken 11285 times.
✗ Branch 1 not taken.
11285 for (i = 0; i < FF_ARRAY_ELEMS(thd_channel_order); i++)
219
4/4
✓ Branch 0 taken 11237 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 3958 times.
✓ Branch 3 taken 7279 times.
11285 if (channel_layout & (1ULL << thd_channel_order[i]) && !index--)
220 3958 return thd_channel_order[i];
221 return AV_CHAN_NONE;
222 }
223
224 static VLC huff_vlc[3];
225
226 /** Initialize static data, constant between all invocations of the codec. */
227
228 9 static av_cold void init_static(void)
229 {
230
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 9 times.
36 for (int i = 0; i < 3; i++) {
231 static VLCElem vlc_buf[3 * VLC_STATIC_SIZE];
232 27 huff_vlc[i].table = &vlc_buf[i * VLC_STATIC_SIZE];
233 27 huff_vlc[i].table_allocated = VLC_STATIC_SIZE;
234 27 vlc_init(&huff_vlc[i], VLC_BITS, 18,
235 &ff_mlp_huffman_tables[i][0][1], 2, 1,
236 &ff_mlp_huffman_tables[i][0][0], 2, 1, VLC_INIT_USE_STATIC);
237 }
238
239 9 ff_mlp_init_crc();
240 9 }
241
242 16834 static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
243 unsigned int substr, unsigned int ch)
244 {
245 16834 SubStream *s = &m->substream[substr];
246 16834 ChannelParams *cp = &s->channel_params[ch];
247 16834 int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
248
2/2
✓ Branch 0 taken 11899 times.
✓ Branch 1 taken 4935 times.
16834 int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
249 16834 int32_t sign_huff_offset = cp->huff_offset;
250
251
2/2
✓ Branch 0 taken 11899 times.
✓ Branch 1 taken 4935 times.
16834 if (cp->codebook > 0)
252 11899 sign_huff_offset -= 7 << lsb_bits;
253
254
2/2
✓ Branch 0 taken 15281 times.
✓ Branch 1 taken 1553 times.
16834 if (sign_shift >= 0)
255 15281 sign_huff_offset -= 1 << sign_shift;
256
257 16834 return sign_huff_offset;
258 }
259
260 /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
261 * and plain LSBs. */
262
263 1121160 static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
264 unsigned int substr, unsigned int pos)
265 {
266 1121160 SubStream *s = &m->substream[substr];
267 unsigned int mat, channel;
268
269
2/2
✓ Branch 0 taken 2054520 times.
✓ Branch 1 taken 1121160 times.
3175680 for (mat = 0; mat < s->num_primitive_matrices; mat++)
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2054520 times.
2054520 if (s->lsb_bypass[mat])
271 m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
272
273
2/2
✓ Branch 0 taken 2734080 times.
✓ Branch 1 taken 1121160 times.
3855240 for (channel = s->min_channel; channel <= s->max_channel; channel++) {
274 2734080 ChannelParams *cp = &s->channel_params[channel];
275 2734080 int codebook = cp->codebook;
276 2734080 int quant_step_size = s->quant_step_size[channel];
277 2734080 int lsb_bits = cp->huff_lsbs - quant_step_size;
278 2734080 int result = 0;
279
280
2/2
✓ Branch 0 taken 2490712 times.
✓ Branch 1 taken 243368 times.
2734080 if (codebook > 0)
281 2490712 result = get_vlc2(gbp, huff_vlc[codebook-1].table,
282 VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
283
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2734080 times.
2734080 if (result < 0)
285 return AVERROR_INVALIDDATA;
286
287
2/2
✓ Branch 0 taken 2549944 times.
✓ Branch 1 taken 184136 times.
2734080 if (lsb_bits > 0)
288 2549944 result = (result << lsb_bits) + get_bits_long(gbp, lsb_bits);
289
290 2734080 result += cp->sign_huff_offset;
291 2734080 result *= 1 << quant_step_size;
292
293 2734080 m->sample_buffer[pos + s->blockpos][channel] = result;
294 }
295
296 1121160 return 0;
297 }
298
299 15 static av_cold int mlp_decode_init(AVCodecContext *avctx)
300 {
301 static AVOnce init_static_once = AV_ONCE_INIT;
302 15 MLPDecodeContext *m = avctx->priv_data;
303 int substr;
304
305 15 m->avctx = avctx;
306
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 15 times.
75 for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
307 60 m->substream[substr].lossless_check_data = 0xffffffff;
308 15 ff_mlpdsp_init(&m->dsp);
309
310
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (m->downmix_layout.nb_channels) {
311
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 if (!av_channel_layout_compare(&m->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) ||
312 !av_channel_layout_compare(&m->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)) {
313 2 av_channel_layout_uninit(&avctx->ch_layout);
314 2 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
315 } else if (!av_channel_layout_compare(&m->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0)) {
316 av_channel_layout_uninit(&avctx->ch_layout);
317 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0;
318 } else if (!av_channel_layout_compare(&m->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1)) {
319 av_channel_layout_uninit(&avctx->ch_layout);
320 avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1;
321 }
322 else {
323 av_log(avctx, AV_LOG_WARNING, "Invalid downmix layout\n");
324 av_channel_layout_uninit(&m->downmix_layout);
325 }
326 }
327
328 15 ff_thread_once(&init_static_once, init_static);
329
330 15 return 0;
331 }
332
333 /** Read a major sync info header - contains high level information about
334 * the stream - sample rate, channel arrangement etc. Most of this
335 * information is not actually necessary for decoding, only for playback.
336 */
337
338 1774 static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
339 {
340 MLPHeaderInfo mh;
341 int substr, ret;
342
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1774 times.
1774 if ((ret = ff_mlp_read_major_sync(m->avctx, &mh, gb)) != 0)
344 return ret;
345
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.group1_bits == 0) {
347 av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
348 return AVERROR_INVALIDDATA;
349 }
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.group2_bits > mh.group1_bits) {
351 av_log(m->avctx, AV_LOG_ERROR,
352 "Channel group 2 cannot have more bits per sample than group 1.\n");
353 return AVERROR_INVALIDDATA;
354 }
355
356
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1774 if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
357 av_log(m->avctx, AV_LOG_ERROR,
358 "Channel groups with differing sample rates are not currently supported.\n");
359 return AVERROR_INVALIDDATA;
360 }
361
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.group1_samplerate == 0) {
363 av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
364 return AVERROR_INVALIDDATA;
365 }
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.group1_samplerate > MAX_SAMPLERATE) {
367 av_log(m->avctx, AV_LOG_ERROR,
368 "Sampling rate %d is greater than the supported maximum (%d).\n",
369 mh.group1_samplerate, MAX_SAMPLERATE);
370 return AVERROR_INVALIDDATA;
371 }
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.access_unit_size > MAX_BLOCKSIZE) {
373 av_log(m->avctx, AV_LOG_ERROR,
374 "Block size %d is greater than the supported maximum (%d).\n",
375 mh.access_unit_size, MAX_BLOCKSIZE);
376 return AVERROR_INVALIDDATA;
377 }
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
379 av_log(m->avctx, AV_LOG_ERROR,
380 "Block size pow2 %d is greater than the supported maximum (%d).\n",
381 mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
382 return AVERROR_INVALIDDATA;
383 }
384
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.num_substreams == 0)
386 return AVERROR_INVALIDDATA;
387
3/4
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 657 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1117 times.
1774 if (m->avctx->codec_id == AV_CODEC_ID_MLP && mh.num_substreams > 2) {
388 av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
389 return AVERROR_INVALIDDATA;
390 }
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
1774 if (mh.num_substreams > MAX_SUBSTREAMS) {
392 avpriv_request_sample(m->avctx,
393 "%d substreams (more than the "
394 "maximum supported by the decoder)",
395 mh.num_substreams);
396 return AVERROR_PATCHWELCOME;
397 }
398
399 1774 m->major_sync_header_size = mh.header_size;
400
401 1774 m->access_unit_size = mh.access_unit_size;
402 1774 m->access_unit_size_pow2 = mh.access_unit_size_pow2;
403
404 1774 m->num_substreams = mh.num_substreams;
405 1774 m->extended_substream_info = mh.extended_substream_info;
406 1774 m->substream_info = mh.substream_info;
407
408 /* If there is a 4th substream and the MSB of substream_info is set,
409 * there is a 16-channel spatial presentation (Atmos in TrueHD).
410 */
411
2/2
✓ Branch 0 taken 657 times.
✓ Branch 1 taken 1117 times.
1774 if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD
412
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 653 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
657 && m->num_substreams == 4 && m->substream_info >> 7 == 1) {
413 4 m->avctx->profile = AV_PROFILE_TRUEHD_ATMOS;
414 }
415
416 /* limit to decoding 3 substreams, as the 4th is used by Dolby Atmos for non-audio data */
417 1774 m->max_decoded_substream = FFMIN(m->num_substreams - 1, 2);
418
419 1774 m->avctx->sample_rate = mh.group1_samplerate;
420 1774 m->avctx->frame_size = mh.access_unit_size;
421
422 1774 m->avctx->bits_per_raw_sample = mh.group1_bits;
423
2/2
✓ Branch 0 taken 657 times.
✓ Branch 1 taken 1117 times.
1774 if (mh.group1_bits > 16)
424 657 m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
425 else
426 1117 m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
427 3548 m->pack_output = m->dsp.mlp_select_pack_output(m->substream[m->max_decoded_substream].ch_assign,
428 1774 m->substream[m->max_decoded_substream].output_shift,
429 1774 m->substream[m->max_decoded_substream].max_matrix_channel,
430 1774 m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
431
432 1774 m->params_valid = 1;
433
2/2
✓ Branch 0 taken 7096 times.
✓ Branch 1 taken 1774 times.
8870 for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
434 7096 m->substream[substr].restart_seen = 0;
435
436 /* Set the layout for each substream. When there's more than one, the first
437 * substream is Stereo. Subsequent substreams' layouts are indicated in the
438 * major sync. */
439
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 657 times.
1774 if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1117 times.
1117 if (mh.stream_type != SYNC_MLP) {
441 avpriv_request_sample(m->avctx,
442 "unexpected stream_type %X in MLP",
443 mh.stream_type);
444 return AVERROR_PATCHWELCOME;
445 }
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1117 times.
1117 if ((substr = (mh.num_substreams > 1)))
447 m->substream[0].mask = AV_CH_LAYOUT_STEREO;
448 1117 m->substream[substr].mask = mh.channel_layout_mlp;
449 } else {
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 if (mh.stream_type != SYNC_TRUEHD) {
451 avpriv_request_sample(m->avctx,
452 "unexpected stream_type %X in !MLP",
453 mh.stream_type);
454 return AVERROR_PATCHWELCOME;
455 }
456 657 m->substream[1].mask = mh.channel_layout_thd_stream1;
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 if (mh.channels_thd_stream1 == 2 &&
458 mh.channels_thd_stream2 == 2)
459 m->substream[0].mask = AV_CH_LAYOUT_STEREO;
460
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
657 if ((substr = (mh.num_substreams > 1)))
461 657 m->substream[0].mask = AV_CH_LAYOUT_STEREO;
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 657 times.
657 if (mh.num_substreams == 1 &&
463 mh.channels_thd_stream1 == 1 &&
464 mh.channels_thd_stream2 == 1)
465 m->substream[0].mask = AV_CH_LAYOUT_MONO;
466
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 653 times.
657 if (mh.num_substreams > 2)
467
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (mh.channel_layout_thd_stream2)
468 4 m->substream[2].mask = mh.channel_layout_thd_stream2;
469 else
470 m->substream[2].mask = mh.channel_layout_thd_stream1;
471
4/4
✓ Branch 0 taken 653 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 215 times.
✓ Branch 3 taken 438 times.
657 if (mh.num_substreams == 2 && (!m->downmix_layout.nb_channels ||
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
215 m->downmix_layout.nb_channels > 2))
473 438 m->substream[1].mask = mh.channel_layout_thd_stream2;
474 }
475
476
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1774 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1774 m->needs_reordering = mh.channel_arrangement >= 18 && mh.channel_arrangement <= 20;
477
478 /* Parse the TrueHD decoder channel modifiers and set each substream's
479 * AVMatrixEncoding accordingly.
480 *
481 * The meaning of the modifiers depends on the channel layout:
482 *
483 * - THD_CH_MODIFIER_LTRT, THD_CH_MODIFIER_LBINRBIN only apply to 2-channel
484 *
485 * - THD_CH_MODIFIER_MONO applies to 1-channel or 2-channel (dual mono)
486 *
487 * - THD_CH_MODIFIER_SURROUNDEX, THD_CH_MODIFIER_NOTSURROUNDEX only apply to
488 * layouts with an Ls/Rs channel pair
489 */
490
2/2
✓ Branch 0 taken 7096 times.
✓ Branch 1 taken 1774 times.
8870 for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
491 7096 m->substream[substr].matrix_encoding = AV_MATRIX_ENCODING_NONE;
492
2/2
✓ Branch 0 taken 657 times.
✓ Branch 1 taken 1117 times.
1774 if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
493
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 653 times.
657 if (mh.num_substreams > 2 &&
494
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 mh.channel_layout_thd_stream2 & AV_CH_SIDE_LEFT &&
495
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 mh.channel_layout_thd_stream2 & AV_CH_SIDE_RIGHT &&
496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 mh.channel_modifier_thd_stream2 == THD_CH_MODIFIER_SURROUNDEX)
497 m->substream[2].matrix_encoding = AV_MATRIX_ENCODING_DOLBYEX;
498
499
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
657 if (mh.num_substreams > 1 &&
500
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 8 times.
657 mh.channel_layout_thd_stream1 & AV_CH_SIDE_LEFT &&
501
1/2
✓ Branch 0 taken 649 times.
✗ Branch 1 not taken.
649 mh.channel_layout_thd_stream1 & AV_CH_SIDE_RIGHT &&
502
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 645 times.
649 mh.channel_modifier_thd_stream1 == THD_CH_MODIFIER_SURROUNDEX)
503 4 m->substream[1].matrix_encoding = AV_MATRIX_ENCODING_DOLBYEX;
504
505
1/2
✓ Branch 0 taken 657 times.
✗ Branch 1 not taken.
657 if (mh.num_substreams > 0)
506
2/3
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 653 times.
657 switch (mh.channel_modifier_thd_stream0) {
507 4 case THD_CH_MODIFIER_LTRT:
508 4 m->substream[0].matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
509 4 break;
510 case THD_CH_MODIFIER_LBINRBIN:
511 m->substream[0].matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
512 break;
513 653 default:
514 653 break;
515 }
516 }
517
518 1774 return 0;
519 }
520
521 /** Read a restart header from a block in a substream. This contains parameters
522 * required to decode the audio that do not change very often. Generally
523 * (always) present only in blocks following a major sync. */
524
525 2220 static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
526 const uint8_t *buf, unsigned int substr)
527 {
528 2220 SubStream *s = &m->substream[substr];
529 unsigned int ch;
530 int sync_word, tmp;
531 uint8_t checksum;
532 uint8_t lossless_check;
533 2220 int start_count = get_bits_count(gbp);
534 int min_channel, max_channel, max_matrix_channel, noise_type;
535 4440 const int std_max_matrix_channel = m->avctx->codec_id == AV_CODEC_ID_MLP
536 ? MAX_MATRIX_CHANNEL_MLP
537
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 1103 times.
2220 : MAX_MATRIX_CHANNEL_TRUEHD;
538
539 2220 sync_word = get_bits(gbp, 13);
540
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2220 times.
2220 if (sync_word != 0x31ea >> 1) {
542 av_log(m->avctx, AV_LOG_ERROR,
543 "restart header sync incorrect (got 0x%04x)\n", sync_word);
544 return AVERROR_INVALIDDATA;
545 }
546
547 2220 noise_type = get_bits1(gbp);
548
549
3/4
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 1103 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1117 times.
2220 if (m->avctx->codec_id == AV_CODEC_ID_MLP && noise_type) {
550 av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
551 return AVERROR_INVALIDDATA;
552 }
553
554 2220 skip_bits(gbp, 16); /* Output timestamp */
555
556 2220 min_channel = get_bits(gbp, 4);
557 2220 max_channel = get_bits(gbp, 4);
558 2220 max_matrix_channel = get_bits(gbp, 4);
559
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2220 times.
2220 if (max_matrix_channel > std_max_matrix_channel) {
561 av_log(m->avctx, AV_LOG_ERROR,
562 "Max matrix channel cannot be greater than %d.\n",
563 std_max_matrix_channel);
564 return AVERROR_INVALIDDATA;
565 }
566
567 /* This should happen for TrueHD streams with >6 channels and MLP's noise
568 * type. It is not yet known if this is allowed. */
569
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2216 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
2220 if (max_matrix_channel > MAX_MATRIX_CHANNEL_MLP && !noise_type) {
570 avpriv_request_sample(m->avctx,
571 "%d channels (more than the "
572 "maximum supported by the decoder)",
573 max_channel + 2);
574 return AVERROR_PATCHWELCOME;
575 }
576
577
2/4
✓ Branch 0 taken 2220 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2220 times.
2220 if (max_channel + 1 > MAX_CHANNELS || max_channel + 1 < min_channel)
578 return AVERROR_INVALIDDATA;
579
580 2220 s->min_channel = min_channel;
581 2220 s->max_channel = max_channel;
582 2220 s->coded_channels = ((1LL << (max_channel - min_channel + 1)) - 1) << min_channel;
583 2220 s->max_matrix_channel = max_matrix_channel;
584 2220 s->noise_type = noise_type;
585
586
2/2
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 2005 times.
2220 if (mlp_channel_layout_subset(&m->downmix_layout, s->mask) &&
587
1/2
✓ Branch 0 taken 215 times.
✗ Branch 1 not taken.
215 m->max_decoded_substream > substr) {
588 215 av_log(m->avctx, AV_LOG_DEBUG,
589 "Extracting %d-channel downmix (0x%"PRIx64") from substream %d. "
590 "Further substreams will be skipped.\n",
591 215 s->max_channel + 1, s->mask, substr);
592 215 m->max_decoded_substream = substr;
593 }
594
595 2220 s->noise_shift = get_bits(gbp, 4);
596 2220 s->noisegen_seed = get_bits(gbp, 23);
597
598 2220 skip_bits(gbp, 19);
599
600 2220 s->data_check_present = get_bits1(gbp);
601 2220 lossless_check = get_bits(gbp, 8);
602
2/2
✓ Branch 0 taken 1774 times.
✓ Branch 1 taken 446 times.
2220 if (substr == m->max_decoded_substream
603
2/2
✓ Branch 0 taken 1759 times.
✓ Branch 1 taken 15 times.
1774 && s->lossless_check_data != 0xffffffff) {
604 1759 tmp = xor_32_to_8(s->lossless_check_data);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1759 times.
1759 if (tmp != lossless_check)
606 av_log(m->avctx, AV_LOG_WARNING,
607 "Lossless check failed - expected %02x, calculated %02x.\n",
608 lossless_check, tmp);
609 }
610
611 2220 skip_bits(gbp, 16);
612
613 2220 memset(s->ch_assign, 0, sizeof(s->ch_assign));
614
615
2/2
✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 2220 times.
8412 for (ch = 0; ch <= s->max_matrix_channel; ch++) {
616 6192 int ch_assign = get_bits(gbp, 6);
617
2/2
✓ Branch 0 taken 3958 times.
✓ Branch 1 taken 2234 times.
6192 if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
618 AVChannelLayout l;
619 3958 enum AVChannel channel = thd_channel_layout_extract_channel(s->mask, ch_assign);
620
621 3958 av_channel_layout_from_mask(&l, s->mask);
622 3958 ch_assign = av_channel_layout_index_from_channel(&l, channel);
623 }
624
2/4
✓ Branch 0 taken 6192 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6192 times.
6192 if (ch_assign < 0 || ch_assign > s->max_matrix_channel) {
625 avpriv_request_sample(m->avctx,
626 "Assignment of matrix channel %d to invalid output channel %d",
627 ch, ch_assign);
628 return AVERROR_PATCHWELCOME;
629 }
630 6192 s->ch_assign[ch_assign] = ch;
631 }
632
633 2220 checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
634
635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2220 times.
2220 if (checksum != get_bits(gbp, 8))
636 av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
637
638 /* Set default decoding parameters. */
639 2220 s->param_presence_flags = 0xff;
640 2220 s->num_primitive_matrices = 0;
641 2220 s->blocksize = 8;
642 2220 s->lossless_check_data = 0;
643
644 2220 memset(s->output_shift , 0, sizeof(s->output_shift ));
645 2220 memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
646
647
2/2
✓ Branch 0 taken 5292 times.
✓ Branch 1 taken 2220 times.
7512 for (ch = s->min_channel; ch <= s->max_channel; ch++) {
648 5292 ChannelParams *cp = &s->channel_params[ch];
649 5292 cp->filter_params[FIR].order = 0;
650 5292 cp->filter_params[IIR].order = 0;
651 5292 cp->filter_params[FIR].shift = 0;
652 5292 cp->filter_params[IIR].shift = 0;
653
654 /* Default audio coding is 24-bit raw PCM. */
655 5292 cp->huff_offset = 0;
656 5292 cp->sign_huff_offset = -(1 << 23);
657 5292 cp->codebook = 0;
658 5292 cp->huff_lsbs = 24;
659 }
660
661
2/2
✓ Branch 0 taken 1774 times.
✓ Branch 1 taken 446 times.
2220 if (substr == m->max_decoded_substream) {
662 1774 av_channel_layout_uninit(&m->avctx->ch_layout);
663 1774 av_channel_layout_from_mask(&m->avctx->ch_layout, s->mask);
664 3548 m->pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,
665 1774 s->output_shift,
666 1774 s->max_matrix_channel,
667 1774 m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
668
669
3/4
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 657 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1117 times.
1774 if (m->avctx->codec_id == AV_CODEC_ID_MLP && m->needs_reordering) {
670 if (s->mask == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
671 s->mask == AV_CH_LAYOUT_5POINT0_BACK) {
672 int i = s->ch_assign[4];
673 s->ch_assign[4] = s->ch_assign[3];
674 s->ch_assign[3] = s->ch_assign[2];
675 s->ch_assign[2] = i;
676 } else if (s->mask == AV_CH_LAYOUT_5POINT1_BACK) {
677 FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
678 FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
679 }
680 }
681
682 }
683
684 2220 return 0;
685 }
686
687 /** Read parameters for one of the prediction filters. */
688
689 5829 static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
690 unsigned int substr, unsigned int channel,
691 unsigned int filter)
692 {
693 5829 SubStream *s = &m->substream[substr];
694 5829 FilterParams *fp = &s->channel_params[channel].filter_params[filter];
695
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 5304 times.
5829 const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
696
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 5304 times.
5829 const char fchar = filter ? 'I' : 'F';
697 int i, order;
698
699 // Filter is 0 for FIR, 1 for IIR.
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5829 times.
5829 av_assert0(filter < 2);
701
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5829 times.
5829 if (m->filter_changed[channel][filter]++ > 1) {
703 av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
704 return AVERROR_INVALIDDATA;
705 }
706
707 5829 order = get_bits(gbp, 4);
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5829 times.
5829 if (order > max_order) {
709 av_log(m->avctx, AV_LOG_ERROR,
710 "%cIR filter order %d is greater than maximum %d.\n",
711 fchar, order, max_order);
712 return AVERROR_INVALIDDATA;
713 }
714 5829 fp->order = order;
715
716
2/2
✓ Branch 0 taken 5824 times.
✓ Branch 1 taken 5 times.
5829 if (order > 0) {
717 5824 int32_t *fcoeff = s->channel_params[channel].coeff[filter];
718 int coeff_bits, coeff_shift;
719
720 5824 fp->shift = get_bits(gbp, 4);
721
722 5824 coeff_bits = get_bits(gbp, 5);
723 5824 coeff_shift = get_bits(gbp, 3);
724
2/4
✓ Branch 0 taken 5824 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5824 times.
5824 if (coeff_bits < 1 || coeff_bits > 16) {
725 av_log(m->avctx, AV_LOG_ERROR,
726 "%cIR filter coeff_bits must be between 1 and 16.\n",
727 fchar);
728 return AVERROR_INVALIDDATA;
729 }
730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5824 times.
5824 if (coeff_bits + coeff_shift > 16) {
731 av_log(m->avctx, AV_LOG_ERROR,
732 "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
733 fchar);
734 return AVERROR_INVALIDDATA;
735 }
736
737
2/2
✓ Branch 0 taken 24259 times.
✓ Branch 1 taken 5824 times.
30083 for (i = 0; i < order; i++)
738 24259 fcoeff[i] = get_sbits(gbp, coeff_bits) * (1 << coeff_shift);
739
740
2/2
✓ Branch 1 taken 525 times.
✓ Branch 2 taken 5299 times.
5824 if (get_bits1(gbp)) {
741 int state_bits, state_shift;
742
743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 525 times.
525 if (filter == FIR) {
744 av_log(m->avctx, AV_LOG_ERROR,
745 "FIR filter has state data specified.\n");
746 return AVERROR_INVALIDDATA;
747 }
748
749 525 state_bits = get_bits(gbp, 4);
750 525 state_shift = get_bits(gbp, 4);
751
752 /* TODO: Check validity of state data. */
753
754
2/2
✓ Branch 0 taken 972 times.
✓ Branch 1 taken 525 times.
1497 for (i = 0; i < order; i++)
755
1/2
✓ Branch 0 taken 972 times.
✗ Branch 1 not taken.
972 fp->state[i] = state_bits ? get_sbits(gbp, state_bits) * (1 << state_shift) : 0;
756 }
757 }
758
759 5829 return 0;
760 }
761
762 /** Read parameters for primitive matrices. */
763
764 1927 static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
765 {
766 1927 SubStream *s = &m->substream[substr];
767 unsigned int mat, ch;
768 3854 const int max_primitive_matrices = m->avctx->codec_id == AV_CODEC_ID_MLP
769 ? MAX_MATRICES_MLP
770
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 1095 times.
1927 : MAX_MATRICES_TRUEHD;
771
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1927 times.
1927 if (m->matrix_changed++ > 1) {
773 av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
774 return AVERROR_INVALIDDATA;
775 }
776
777 1927 s->num_primitive_matrices = get_bits(gbp, 4);
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1927 times.
1927 if (s->num_primitive_matrices > max_primitive_matrices) {
780 av_log(m->avctx, AV_LOG_ERROR,
781 "Number of primitive matrices cannot be greater than %d.\n",
782 max_primitive_matrices);
783 goto error;
784 }
785
786
2/2
✓ Branch 0 taken 3494 times.
✓ Branch 1 taken 1927 times.
5421 for (mat = 0; mat < s->num_primitive_matrices; mat++) {
787 int frac_bits, max_chan;
788 3494 s->matrix_out_ch[mat] = get_bits(gbp, 4);
789 3494 frac_bits = get_bits(gbp, 4);
790 3494 s->lsb_bypass [mat] = get_bits1(gbp);
791
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3494 times.
3494 if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
793 av_log(m->avctx, AV_LOG_ERROR,
794 "Invalid channel %d specified as output from matrix.\n",
795 s->matrix_out_ch[mat]);
796 goto error;
797 }
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3494 times.
3494 if (frac_bits > 14) {
799 av_log(m->avctx, AV_LOG_ERROR,
800 "Too many fractional bits specified.\n");
801 goto error;
802 }
803
804 3494 max_chan = s->max_matrix_channel;
805
2/2
✓ Branch 0 taken 2146 times.
✓ Branch 1 taken 1348 times.
3494 if (!s->noise_type)
806 2146 max_chan+=2;
807
808
2/2
✓ Branch 0 taken 16736 times.
✓ Branch 1 taken 3494 times.
20230 for (ch = 0; ch <= max_chan; ch++) {
809 16736 int coeff_val = 0;
810
2/2
✓ Branch 1 taken 9693 times.
✓ Branch 2 taken 7043 times.
16736 if (get_bits1(gbp))
811 9693 coeff_val = get_sbits(gbp, frac_bits + 2);
812
813 16736 s->matrix_coeff[mat][ch] = coeff_val * (1 << (14 - frac_bits));
814 }
815
816
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 2146 times.
3494 if (s->noise_type)
817 1348 s->matrix_noise_shift[mat] = get_bits(gbp, 4);
818 else
819 2146 s->matrix_noise_shift[mat] = 0;
820 }
821
822 1927 return 0;
823 error:
824 s->num_primitive_matrices = 0;
825 memset(s->matrix_out_ch, 0, sizeof(s->matrix_out_ch));
826
827 return AVERROR_INVALIDDATA;
828 }
829
830 /** Read channel parameters. */
831
832 15911 static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
833 GetBitContext *gbp, unsigned int ch)
834 {
835 15911 SubStream *s = &m->substream[substr];
836 15911 ChannelParams *cp = &s->channel_params[ch];
837 15911 FilterParams *fir = &cp->filter_params[FIR];
838 15911 FilterParams *iir = &cp->filter_params[IIR];
839 int ret;
840
841
1/2
✓ Branch 0 taken 15911 times.
✗ Branch 1 not taken.
15911 if (s->param_presence_flags & PARAM_FIR)
842
2/2
✓ Branch 1 taken 5304 times.
✓ Branch 2 taken 10607 times.
15911 if (get_bits1(gbp))
843
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5304 times.
5304 if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
844 return ret;
845
846
1/2
✓ Branch 0 taken 15911 times.
✗ Branch 1 not taken.
15911 if (s->param_presence_flags & PARAM_IIR)
847
2/2
✓ Branch 1 taken 525 times.
✓ Branch 2 taken 15386 times.
15911 if (get_bits1(gbp))
848
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 525 times.
525 if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
849 return ret;
850
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15911 times.
15911 if (fir->order + iir->order > 8) {
852 av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
853 return AVERROR_INVALIDDATA;
854 }
855
856
4/4
✓ Branch 0 taken 10614 times.
✓ Branch 1 taken 5297 times.
✓ Branch 2 taken 990 times.
✓ Branch 3 taken 9624 times.
15911 if (fir->order && iir->order &&
857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
990 fir->shift != iir->shift) {
858 av_log(m->avctx, AV_LOG_ERROR,
859 "FIR and IIR filters must use the same precision.\n");
860 return AVERROR_INVALIDDATA;
861 }
862 /* The FIR and IIR filters must have the same precision.
863 * To simplify the filtering code, only the precision of the
864 * FIR filter is considered. If only the IIR filter is employed,
865 * the FIR filter precision is set to that of the IIR filter, so
866 * that the filtering code can use it. */
867
3/4
✓ Branch 0 taken 5297 times.
✓ Branch 1 taken 10614 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5297 times.
15911 if (!fir->order && iir->order)
868 fir->shift = iir->shift;
869
870
1/2
✓ Branch 0 taken 15911 times.
✗ Branch 1 not taken.
15911 if (s->param_presence_flags & PARAM_HUFFOFFSET)
871
2/2
✓ Branch 1 taken 3516 times.
✓ Branch 2 taken 12395 times.
15911 if (get_bits1(gbp))
872 3516 cp->huff_offset = get_sbits(gbp, 15);
873
874 15911 cp->codebook = get_bits(gbp, 2);
875 15911 cp->huff_lsbs = get_bits(gbp, 5);
876
877
3/4
✓ Branch 0 taken 11899 times.
✓ Branch 1 taken 4012 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11899 times.
15911 if (cp->codebook > 0 && cp->huff_lsbs > 24) {
878 av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
879 cp->huff_lsbs = 0;
880 return AVERROR_INVALIDDATA;
881 }
882
883 15911 return 0;
884 }
885
886 /** Read decoding parameters that change more often than those in the restart
887 * header. */
888
889 16036 static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
890 unsigned int substr)
891 {
892 16036 SubStream *s = &m->substream[substr];
893 unsigned int ch;
894 16036 int ret = 0;
895 16036 unsigned recompute_sho = 0;
896
897
1/2
✓ Branch 0 taken 16036 times.
✗ Branch 1 not taken.
16036 if (s->param_presence_flags & PARAM_PRESENCE)
898
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16036 times.
16036 if (get_bits1(gbp))
899 s->param_presence_flags = get_bits(gbp, 8);
900
901
1/2
✓ Branch 0 taken 16036 times.
✗ Branch 1 not taken.
16036 if (s->param_presence_flags & PARAM_BLOCKSIZE)
902
2/2
✓ Branch 1 taken 4422 times.
✓ Branch 2 taken 11614 times.
16036 if (get_bits1(gbp)) {
903 4422 s->blocksize = get_bits(gbp, 9);
904
2/4
✓ Branch 0 taken 4422 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4422 times.
4422 if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
905 av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.\n");
906 s->blocksize = 0;
907 return AVERROR_INVALIDDATA;
908 }
909 }
910
911
1/2
✓ Branch 0 taken 16036 times.
✗ Branch 1 not taken.
16036 if (s->param_presence_flags & PARAM_MATRIX)
912
2/2
✓ Branch 1 taken 1927 times.
✓ Branch 2 taken 14109 times.
16036 if (get_bits1(gbp))
913
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1927 times.
1927 if ((ret = read_matrix_params(m, substr, gbp)) < 0)
914 return ret;
915
916
1/2
✓ Branch 0 taken 16036 times.
✗ Branch 1 not taken.
16036 if (s->param_presence_flags & PARAM_OUTSHIFT)
917
2/2
✓ Branch 1 taken 1403 times.
✓ Branch 2 taken 14633 times.
16036 if (get_bits1(gbp)) {
918
2/2
✓ Branch 0 taken 4558 times.
✓ Branch 1 taken 1403 times.
5961 for (ch = 0; ch <= s->max_matrix_channel; ch++) {
919 4558 s->output_shift[ch] = get_sbits(gbp, 4);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4558 times.
4558 if (s->output_shift[ch] < 0) {
921 avpriv_request_sample(m->avctx, "Negative output_shift");
922 s->output_shift[ch] = 0;
923 }
924 }
925
2/2
✓ Branch 0 taken 1371 times.
✓ Branch 1 taken 32 times.
1403 if (substr == m->max_decoded_substream)
926 1371 m->pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,
927 1371 s->output_shift,
928 1371 s->max_matrix_channel,
929 1371 m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
930 }
931
932
1/2
✓ Branch 0 taken 16036 times.
✗ Branch 1 not taken.
16036 if (s->param_presence_flags & PARAM_QUANTSTEP)
933
2/2
✓ Branch 1 taken 2236 times.
✓ Branch 2 taken 13800 times.
16036 if (get_bits1(gbp))
934
2/2
✓ Branch 0 taken 6192 times.
✓ Branch 1 taken 2236 times.
8428 for (ch = 0; ch <= s->max_channel; ch++) {
935 6192 s->quant_step_size[ch] = get_bits(gbp, 4);
936
937 6192 recompute_sho |= 1<<ch;
938 }
939
940
2/2
✓ Branch 0 taken 38080 times.
✓ Branch 1 taken 16036 times.
54116 for (ch = s->min_channel; ch <= s->max_channel; ch++)
941
2/2
✓ Branch 1 taken 15911 times.
✓ Branch 2 taken 22169 times.
38080 if (get_bits1(gbp)) {
942 15911 recompute_sho |= 1<<ch;
943
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15911 times.
15911 if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
944 goto fail;
945 }
946
947
948 16036 fail:
949
2/2
✓ Branch 0 taken 44382 times.
✓ Branch 1 taken 16036 times.
60418 for (ch = 0; ch <= s->max_channel; ch++) {
950
2/2
✓ Branch 0 taken 16834 times.
✓ Branch 1 taken 27548 times.
44382 if (recompute_sho & (1<<ch)) {
951 16834 ChannelParams *cp = &s->channel_params[ch];
952
953
3/4
✓ Branch 0 taken 11899 times.
✓ Branch 1 taken 4935 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11899 times.
16834 if (cp->codebook > 0 && cp->huff_lsbs < s->quant_step_size[ch]) {
954 if (ret >= 0) {
955 av_log(m->avctx, AV_LOG_ERROR, "quant_step_size larger than huff_lsbs\n");
956 ret = AVERROR_INVALIDDATA;
957 }
958 s->quant_step_size[ch] = 0;
959 }
960
961 16834 cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
962 }
963 }
964 16036 return ret;
965 }
966
967 #define MSB_MASK(bits) (-(1 << (bits)))
968
969 /** Generate PCM samples using the prediction filters and residual values
970 * read from the data stream, and update the filter state. */
971
972 73644 static void filter_channel(MLPDecodeContext *m, unsigned int substr,
973 unsigned int channel)
974 {
975 73644 SubStream *s = &m->substream[substr];
976 73644 const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
977 int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
978 73644 int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
979 73644 int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
980 73644 FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
981 73644 FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
982 73644 unsigned int filter_shift = fir->shift;
983 73644 int32_t mask = MSB_MASK(s->quant_step_size[channel]);
984
985 73644 memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
986 73644 memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
987
988 73644 m->dsp.mlp_filter_channel(firbuf, fircoeff,
989 73644 fir->order, iir->order,
990 73644 filter_shift, mask, s->blocksize,
991 73644 &m->sample_buffer[s->blockpos][channel]);
992
993 73644 memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
994 73644 memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
995 73644 }
996
997 /** Read a block of PCM residual data (or actual if no filtering active). */
998
999 30249 static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
1000 unsigned int substr)
1001 {
1002 30249 SubStream *s = &m->substream[substr];
1003 30249 unsigned int i, ch, expected_stream_pos = 0;
1004 int ret;
1005
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30249 times.
30249 if (s->data_check_present) {
1007 expected_stream_pos = get_bits_count(gbp);
1008 expected_stream_pos += get_bits(gbp, 16);
1009 avpriv_request_sample(m->avctx,
1010 "Substreams with VLC block size check info");
1011 }
1012
1013
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30249 times.
30249 if (s->blockpos + s->blocksize > m->access_unit_size) {
1014 av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
1015 return AVERROR_INVALIDDATA;
1016 }
1017
1018 30249 memset(&m->bypassed_lsbs[s->blockpos][0], 0,
1019 30249 s->blocksize * sizeof(m->bypassed_lsbs[0]));
1020
1021
2/2
✓ Branch 0 taken 1121160 times.
✓ Branch 1 taken 30249 times.
1151409 for (i = 0; i < s->blocksize; i++)
1022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1121160 times.
1121160 if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
1023 return ret;
1024
1025
2/2
✓ Branch 0 taken 73644 times.
✓ Branch 1 taken 30249 times.
103893 for (ch = s->min_channel; ch <= s->max_channel; ch++)
1026 73644 filter_channel(m, substr, ch);
1027
1028 30249 s->blockpos += s->blocksize;
1029
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30249 times.
30249 if (s->data_check_present) {
1031 if (get_bits_count(gbp) != expected_stream_pos)
1032 av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
1033 skip_bits(gbp, 8);
1034 }
1035
1036 30249 return 0;
1037 }
1038
1039 /** Data table used for TrueHD noise generation function. */
1040
1041 static const int8_t noise_table[256] = {
1042 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
1043 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
1044 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
1045 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
1046 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
1047 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
1048 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
1049 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
1050 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
1051 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
1052 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
1053 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
1054 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
1055 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
1056 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
1057 -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
1058 };
1059
1060 /** Noise generation functions.
1061 * I'm not sure what these are for - they seem to be some kind of pseudorandom
1062 * sequence generators, used to generate noise data which is used when the
1063 * channels are rematrixed. I'm not sure if they provide a practical benefit
1064 * to compression, or just obfuscate the decoder. Are they for some kind of
1065 * dithering? */
1066
1067 /** Generate two channels of noise, used in the matrix when
1068 * restart sync word == 0x31ea. */
1069
1070 12380 static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
1071 {
1072 12380 SubStream *s = &m->substream[substr];
1073 unsigned int i;
1074 12380 uint32_t seed = s->noisegen_seed;
1075 12380 unsigned int maxchan = s->max_matrix_channel;
1076
1077
2/2
✓ Branch 0 taken 495200 times.
✓ Branch 1 taken 12380 times.
507580 for (i = 0; i < s->blockpos; i++) {
1078 495200 uint16_t seed_shr7 = seed >> 7;
1079 495200 m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) * (1 << s->noise_shift);
1080 495200 m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) * (1 << s->noise_shift);
1081
1082 495200 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1083 }
1084
1085 12380 s->noisegen_seed = seed;
1086 12380 }
1087
1088 /** Generate a block of noise, used when restart sync word == 0x31eb. */
1089
1090 7759 static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
1091 {
1092 7759 SubStream *s = &m->substream[substr];
1093 unsigned int i;
1094 7759 uint32_t seed = s->noisegen_seed;
1095
1096
2/2
✓ Branch 0 taken 496576 times.
✓ Branch 1 taken 7759 times.
504335 for (i = 0; i < m->access_unit_size_pow2; i++) {
1097 496576 uint8_t seed_shr15 = seed >> 15;
1098 496576 m->noise_buffer[i] = noise_table[seed_shr15];
1099 496576 seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
1100 }
1101
1102 7759 s->noisegen_seed = seed;
1103 7759 }
1104
1105 /** Write the audio data into the output buffer. */
1106
1107 20139 static int output_data(MLPDecodeContext *m, unsigned int substr,
1108 AVFrame *frame, int *got_frame_ptr)
1109 {
1110 20139 AVCodecContext *avctx = m->avctx;
1111 20139 SubStream *s = &m->substream[substr];
1112 unsigned int mat;
1113 unsigned int maxchan;
1114 int ret;
1115 20139 int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
1116
1117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if (m->avctx->ch_layout.nb_channels != s->max_matrix_channel + 1) {
1118 av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
1119 return AVERROR_INVALIDDATA;
1120 }
1121
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if (!s->blockpos) {
1123 av_log(avctx, AV_LOG_ERROR, "No samples to output.\n");
1124 return AVERROR_INVALIDDATA;
1125 }
1126
1127 20139 maxchan = s->max_matrix_channel;
1128
2/2
✓ Branch 0 taken 12380 times.
✓ Branch 1 taken 7759 times.
20139 if (!s->noise_type) {
1129 12380 generate_2_noise_channels(m, substr);
1130 12380 maxchan += 2;
1131 } else {
1132 7759 fill_noise_buffer(m, substr);
1133 }
1134
1135 /* Apply the channel matrices in turn to reconstruct the original audio
1136 * samples. */
1137
2/2
✓ Branch 0 taken 35059 times.
✓ Branch 1 taken 20139 times.
55198 for (mat = 0; mat < s->num_primitive_matrices; mat++) {
1138 35059 unsigned int dest_ch = s->matrix_out_ch[mat];
1139 35059 m->dsp.mlp_rematrix_channel(&m->sample_buffer[0][0],
1140 35059 s->matrix_coeff[mat],
1141 35059 &m->bypassed_lsbs[0][mat],
1142 35059 m->noise_buffer,
1143 35059 s->num_primitive_matrices - mat,
1144 dest_ch,
1145 35059 s->blockpos,
1146 maxchan,
1147 35059 s->matrix_noise_shift[mat],
1148 m->access_unit_size_pow2,
1149 35059 MSB_MASK(s->quant_step_size[dest_ch]));
1150 }
1151
1152 /* get output buffer */
1153 20139 frame->nb_samples = s->blockpos;
1154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20139 times.
20139 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1155 return ret;
1156 40278 s->lossless_check_data = m->pack_output(s->lossless_check_data,
1157 20139 s->blockpos,
1158 20139 m->sample_buffer,
1159 20139 frame->data[0],
1160 20139 s->ch_assign,
1161 20139 s->output_shift,
1162 20139 s->max_matrix_channel,
1163 is32);
1164
1165 /* Update matrix encoding side data */
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if (s->matrix_encoding != s->prev_matrix_encoding) {
1167 if ((ret = ff_side_data_update_matrix_encoding(frame, s->matrix_encoding)) < 0)
1168 return ret;
1169
1170 s->prev_matrix_encoding = s->matrix_encoding;
1171 }
1172
1173 20139 *got_frame_ptr = 1;
1174
1175 20139 return 0;
1176 }
1177
1178 /** Read an access unit from the stream.
1179 * @return negative on error, 0 if not enough data is present in the input stream,
1180 * otherwise the number of bytes consumed. */
1181
1182 20139 static int read_access_unit(AVCodecContext *avctx, AVFrame *frame,
1183 int *got_frame_ptr, AVPacket *avpkt)
1184 {
1185 20139 const uint8_t *buf = avpkt->data;
1186 20139 int buf_size = avpkt->size;
1187 20139 MLPDecodeContext *m = avctx->priv_data;
1188 GetBitContext gb;
1189 unsigned int length, substr;
1190 unsigned int substream_start;
1191 20139 unsigned int header_size = 4;
1192 20139 unsigned int substr_header_size = 0;
1193 uint8_t substream_parity_present[MAX_SUBSTREAMS];
1194 uint16_t substream_data_len[MAX_SUBSTREAMS];
1195 uint8_t parity_bits;
1196 int ret;
1197
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if (buf_size < 4)
1199 return AVERROR_INVALIDDATA;
1200
1201 20139 length = (AV_RB16(buf) & 0xfff) * 2;
1202
1203
2/4
✓ Branch 0 taken 20139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20139 times.
20139 if (length < 4 || length > buf_size)
1204 return AVERROR_INVALIDDATA;
1205
1206 20139 init_get_bits(&gb, (buf + 4), (length - 4) * 8);
1207
1208 20139 m->is_major_sync_unit = 0;
1209
2/2
✓ Branch 1 taken 1774 times.
✓ Branch 2 taken 18365 times.
20139 if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
1210
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1774 times.
1774 if (read_major_sync(m, &gb) < 0)
1211 goto error;
1212 1774 m->is_major_sync_unit = 1;
1213 1774 header_size += m->major_sync_header_size;
1214 1774 frame->flags |= AV_FRAME_FLAG_KEY;
1215 }
1216
1217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if (!m->params_valid) {
1218 av_log(m->avctx, AV_LOG_WARNING,
1219 "Stream parameters not seen; skipping frame.\n");
1220 *got_frame_ptr = 0;
1221 return length;
1222 }
1223
1224 20139 substream_start = 0;
1225
1226
2/2
✓ Branch 0 taken 31571 times.
✓ Branch 1 taken 20139 times.
51710 for (substr = 0; substr < m->num_substreams; substr++) {
1227 int extraword_present, checkdata_present, end, nonrestart_substr;
1228
1229 31571 extraword_present = get_bits1(&gb);
1230 31571 nonrestart_substr = get_bits1(&gb);
1231 31571 checkdata_present = get_bits1(&gb);
1232 31571 skip_bits1(&gb);
1233
1234 31571 end = get_bits(&gb, 12) * 2;
1235
1236 31571 substr_header_size += 2;
1237
1238
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 31395 times.
31571 if (extraword_present) {
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
1240 av_log(m->avctx, AV_LOG_ERROR, "There must be no extraword for MLP.\n");
1241 goto error;
1242 }
1243 176 skip_bits(&gb, 16);
1244 176 substr_header_size += 2;
1245 }
1246
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31571 times.
31571 if (length < header_size + substr_header_size) {
1248 av_log(m->avctx, AV_LOG_ERROR, "Insufficient data for headers\n");
1249 goto error;
1250 }
1251
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31571 times.
31571 if (!(nonrestart_substr ^ m->is_major_sync_unit)) {
1253 av_log(m->avctx, AV_LOG_ERROR, "Invalid nonrestart_substr.\n");
1254 goto error;
1255 }
1256
1257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31571 times.
31571 if (end + header_size + substr_header_size > length) {
1258 av_log(m->avctx, AV_LOG_ERROR,
1259 "Indicated length of substream %d data goes off end of "
1260 "packet.\n", substr);
1261 end = length - header_size - substr_header_size;
1262 }
1263
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31571 times.
31571 if (end < substream_start) {
1265 av_log(avctx, AV_LOG_ERROR,
1266 "Indicated end offset of substream %d data "
1267 "is smaller than calculated start offset.\n",
1268 substr);
1269 goto error;
1270 }
1271
1272
2/2
✓ Branch 0 taken 3327 times.
✓ Branch 1 taken 28244 times.
31571 if (substr > m->max_decoded_substream)
1273 3327 continue;
1274
1275 28244 substream_parity_present[substr] = checkdata_present;
1276 28244 substream_data_len[substr] = end - substream_start;
1277 28244 substream_start = end;
1278 }
1279
1280 20139 parity_bits = ff_mlp_calculate_parity(buf, 4);
1281 20139 parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
1282
1283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20139 times.
20139 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1284 av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
1285 goto error;
1286 }
1287
1288 20139 buf += header_size + substr_header_size;
1289
1290
2/2
✓ Branch 0 taken 28029 times.
✓ Branch 1 taken 20139 times.
48168 for (substr = 0; substr <= m->max_decoded_substream; substr++) {
1291 28029 SubStream *s = &m->substream[substr];
1292
1293 28029 init_get_bits(&gb, buf, substream_data_len[substr] * 8);
1294
1295 28029 m->matrix_changed = 0;
1296 28029 memset(m->filter_changed, 0, sizeof(m->filter_changed));
1297
1298 28029 s->blockpos = 0;
1299 do {
1300
2/2
✓ Branch 1 taken 16036 times.
✓ Branch 2 taken 14213 times.
30249 if (get_bits1(&gb)) {
1301
2/2
✓ Branch 1 taken 2220 times.
✓ Branch 2 taken 13816 times.
16036 if (get_bits1(&gb)) {
1302 /* A restart header should be present. */
1303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2220 times.
2220 if (read_restart_header(m, &gb, buf, substr) < 0)
1304 goto next_substr;
1305 2220 s->restart_seen = 1;
1306 }
1307
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16036 times.
16036 if (!s->restart_seen)
1309 goto next_substr;
1310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16036 times.
16036 if (read_decoding_params(m, &gb, substr) < 0)
1311 goto next_substr;
1312 }
1313
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30249 times.
30249 if (!s->restart_seen)
1315 goto next_substr;
1316
1317
2/2
✓ Branch 0 taken 14504 times.
✓ Branch 1 taken 15745 times.
30249 if (((avctx->ch_layout.nb_channels == 6 &&
1318
1/2
✓ Branch 0 taken 14504 times.
✗ Branch 1 not taken.
14504 ((m->substream_info >> 2) & 0x3) != 0x3) ||
1319
2/2
✓ Branch 0 taken 405 times.
✓ Branch 1 taken 29844 times.
30249 (avctx->ch_layout.nb_channels == 8 &&
1320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 405 times.
405 ((m->substream_info >> 4) & 0x7) != 0x7 &&
1321 ((m->substream_info >> 4) & 0x7) != 0x6 &&
1322 ((m->substream_info >> 4) & 0x7) != 0x3)) &&
1323 substr > 0 && substr < m->max_decoded_substream &&
1324 (s->min_channel <= m->substream[substr - 1].max_channel)) {
1325 av_log(avctx, AV_LOG_DEBUG,
1326 "Previous substream(%d) channels overlaps current substream(%d) channels, skipping.\n",
1327 substr - 1, substr);
1328 goto next_substr;
1329 }
1330
1331
2/2
✓ Branch 0 taken 8336 times.
✓ Branch 1 taken 21913 times.
30249 if (substr != m->max_decoded_substream &&
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8336 times.
8336 ((s->coded_channels & m->substream[m->max_decoded_substream].coded_channels) != 0)) {
1333 av_log(avctx, AV_LOG_DEBUG,
1334 "Current substream(%d) channels overlaps final substream(%d) channels, skipping.\n",
1335 substr, m->max_decoded_substream);
1336 goto next_substr;
1337 }
1338
1339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30249 times.
30249 if ((ret = read_block_data(m, &gb, substr)) < 0)
1340 return ret;
1341
1342
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30249 times.
30249 if (get_bits_count(&gb) >= substream_data_len[substr] * 8)
1343 goto substream_length_mismatch;
1344
1345
2/2
✓ Branch 1 taken 2220 times.
✓ Branch 2 taken 28029 times.
30249 } while (!get_bits1(&gb));
1346
1347 28029 skip_bits(&gb, (-get_bits_count(&gb)) & 15);
1348
1349
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28029 times.
28029 if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32) {
1350 int shorten_by;
1351
1352 if (get_bits(&gb, 16) != 0xD234)
1353 return AVERROR_INVALIDDATA;
1354
1355 shorten_by = get_bits(&gb, 16);
1356 if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD && shorten_by & 0x2000)
1357 s->blockpos -= FFMIN(shorten_by & 0x1FFF, s->blockpos);
1358 else if (m->avctx->codec_id == AV_CODEC_ID_MLP && shorten_by != 0xD234)
1359 return AVERROR_INVALIDDATA;
1360
1361 av_log(m->avctx, AV_LOG_DEBUG, "End of stream indicated.\n");
1362 s->end_of_stream = 1;
1363 }
1364
1365
1/2
✓ Branch 0 taken 28029 times.
✗ Branch 1 not taken.
28029 if (substream_parity_present[substr]) {
1366 uint8_t parity, checksum;
1367
1368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28029 times.
28029 if (substream_data_len[substr] * 8 - get_bits_count(&gb) != 16)
1369 goto substream_length_mismatch;
1370
1371 28029 parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
1372 28029 checksum = ff_mlp_checksum8 (buf, substream_data_len[substr] - 2);
1373
1374
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28029 times.
28029 if ((get_bits(&gb, 8) ^ parity) != 0xa9 )
1375 av_log(m->avctx, AV_LOG_ERROR, "Substream %d parity check failed.\n", substr);
1376
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28029 times.
28029 if ( get_bits(&gb, 8) != checksum)
1377 av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n" , substr);
1378 }
1379
1380
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28029 times.
28029 if (substream_data_len[substr] * 8 != get_bits_count(&gb))
1381 goto substream_length_mismatch;
1382
1383 28029 next_substr:
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28029 times.
28029 if (!s->restart_seen)
1385 av_log(m->avctx, AV_LOG_ERROR,
1386 "No restart header present in substream %d.\n", substr);
1387
1388 28029 buf += substream_data_len[substr];
1389 }
1390
1391
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20139 times.
20139 if ((ret = output_data(m, m->max_decoded_substream, frame, got_frame_ptr)) < 0)
1392 return ret;
1393
1394
2/2
✓ Branch 0 taken 28029 times.
✓ Branch 1 taken 20139 times.
48168 for (substr = 0; substr <= m->max_decoded_substream; substr++){
1395 28029 SubStream *s = &m->substream[substr];
1396
1397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28029 times.
28029 if (s->end_of_stream) {
1398 s->lossless_check_data = 0xffffffff;
1399 s->end_of_stream = 0;
1400 m->params_valid = 0;
1401 }
1402 }
1403
1404 20139 return length;
1405
1406 substream_length_mismatch:
1407 av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n", substr);
1408 return AVERROR_INVALIDDATA;
1409
1410 error:
1411 m->params_valid = 0;
1412 return AVERROR_INVALIDDATA;
1413 }
1414
1415 static av_cold void mlp_decode_flush(AVCodecContext *avctx)
1416 {
1417 MLPDecodeContext *m = avctx->priv_data;
1418
1419 m->params_valid = 0;
1420 for (int substr = 0; substr <= m->max_decoded_substream; substr++){
1421 SubStream *s = &m->substream[substr];
1422
1423 s->lossless_check_data = 0xffffffff;
1424 s->prev_matrix_encoding = 0;
1425 }
1426 }
1427
1428 #define OFFSET(x) offsetof(MLPDecodeContext, x)
1429 #define FLAGS (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1430 static const AVOption options[] = {
1431 { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout),
1432 AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = FLAGS },
1433 { NULL },
1434 };
1435
1436 static const AVClass mlp_decoder_class = {
1437 .class_name = "MLP decoder",
1438 .item_name = av_default_item_name,
1439 .option = options,
1440 .version = LIBAVUTIL_VERSION_INT,
1441 };
1442
1443 static const AVClass truehd_decoder_class = {
1444 .class_name = "TrueHD decoder",
1445 .item_name = av_default_item_name,
1446 .option = options,
1447 .version = LIBAVUTIL_VERSION_INT,
1448 };
1449
1450 #if CONFIG_MLP_DECODER
1451 const FFCodec ff_mlp_decoder = {
1452 .p.name = "mlp",
1453 CODEC_LONG_NAME("MLP (Meridian Lossless Packing)"),
1454 .p.type = AVMEDIA_TYPE_AUDIO,
1455 .p.id = AV_CODEC_ID_MLP,
1456 .priv_data_size = sizeof(MLPDecodeContext),
1457 .p.priv_class = &mlp_decoder_class,
1458 .init = mlp_decode_init,
1459 FF_CODEC_DECODE_CB(read_access_unit),
1460 .flush = mlp_decode_flush,
1461 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1462 };
1463 #endif
1464 #if CONFIG_TRUEHD_DECODER
1465 const FFCodec ff_truehd_decoder = {
1466 .p.name = "truehd",
1467 CODEC_LONG_NAME("TrueHD"),
1468 .p.type = AVMEDIA_TYPE_AUDIO,
1469 .p.id = AV_CODEC_ID_TRUEHD,
1470 .priv_data_size = sizeof(MLPDecodeContext),
1471 .p.priv_class = &truehd_decoder_class,
1472 .init = mlp_decode_init,
1473 FF_CODEC_DECODE_CB(read_access_unit),
1474 .flush = mlp_decode_flush,
1475 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1476 .p.profiles = NULL_IF_CONFIG_SMALL(ff_truehd_profiles),
1477 };
1478 #endif /* CONFIG_TRUEHD_DECODER */
1479