FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpdec.c
Date: 2022-11-28 23:49:43
Exec Total Coverage
Lines: 470 655 71.8%
Branches: 281 431 65.2%

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