FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpdec.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 457 634 72.1%
Branches: 268 409 65.5%

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