FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpdec.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 483 681 70.9%
Functions: 18 19 94.7%
Branches: 292 461 63.3%

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