FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpdec.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 470 655 71.8%
Branches: 278 427 65.1%

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