FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamf_parse.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 502 722 69.5%
Functions: 13 14 92.9%
Branches: 224 395 56.7%

Line Branch Exec Source
1 /*
2 * Immersive Audio Model and Formats parsing
3 * Copyright (c) 2023 James Almer <jamrial@gmail.com>
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 #include "libavutil/avassert.h"
23 #include "libavutil/iamf.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavcodec/get_bits.h"
28 #include "libavcodec/flac.h"
29 #include "libavcodec/leb.h"
30 #include "libavcodec/mpeg4audio.h"
31 #include "libavcodec/put_bits.h"
32 #include "avio_internal.h"
33 #include "iamf_parse.h"
34 #include "isom.h"
35
36 5 static int opus_decoder_config(IAMFCodecConfig *codec_config,
37 AVIOContext *pb, int len)
38 {
39 5 int ret, left = len - avio_tell(pb);
40
41
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (left < 11 || codec_config->audio_roll_distance >= 0)
42 return AVERROR_INVALIDDATA;
43
44 5 codec_config->extradata = av_malloc(left + 8);
45
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!codec_config->extradata)
46 return AVERROR(ENOMEM);
47
48 5 AV_WB32A(codec_config->extradata, MKBETAG('O','p','u','s'));
49 5 AV_WB32A(codec_config->extradata + 4, MKBETAG('H','e','a','d'));
50 5 ret = ffio_read_size(pb, codec_config->extradata + 8, left);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (ret < 0)
52 return ret;
53
54 5 codec_config->extradata_size = left + 8;
55 5 codec_config->sample_rate = 48000;
56
57 5 return 0;
58 }
59
60 2 static int aac_decoder_config(IAMFCodecConfig *codec_config,
61 AVIOContext *pb, int len, void *logctx)
62 {
63 2 MPEG4AudioConfig cfg = { 0 };
64 int object_type_id, codec_id, stream_type;
65 int ret, tag, left;
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (codec_config->audio_roll_distance >= 0)
68 return AVERROR_INVALIDDATA;
69
70 2 ff_mp4_read_descr(logctx, pb, &tag);
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tag != MP4DecConfigDescrTag)
72 return AVERROR_INVALIDDATA;
73
74 2 object_type_id = avio_r8(pb);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (object_type_id != 0x40)
76 return AVERROR_INVALIDDATA;
77
78 2 stream_type = avio_r8(pb);
79
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (((stream_type >> 2) != 5) || ((stream_type >> 1) & 1))
80 return AVERROR_INVALIDDATA;
81
82 2 avio_skip(pb, 3); // buffer size db
83 2 avio_skip(pb, 4); // rc_max_rate
84 2 avio_skip(pb, 4); // avg bitrate
85
86 2 codec_id = ff_codec_get_id(ff_mp4_obj_type, object_type_id);
87
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (codec_id && codec_id != codec_config->codec_id)
88 return AVERROR_INVALIDDATA;
89
90 2 left = ff_mp4_read_descr(logctx, pb, &tag);
91
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (tag != MP4DecSpecificDescrTag ||
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 !left || left > (len - avio_tell(pb)))
93 return AVERROR_INVALIDDATA;
94
95 // We pad extradata here because avpriv_mpeg4audio_get_config2() needs it.
96 2 codec_config->extradata = av_malloc((size_t)left + AV_INPUT_BUFFER_PADDING_SIZE);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!codec_config->extradata)
98 return AVERROR(ENOMEM);
99
100 2 ret = ffio_read_size(pb, codec_config->extradata, left);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
102 return ret;
103 2 codec_config->extradata_size = left;
104 2 memset(codec_config->extradata + codec_config->extradata_size, 0,
105 AV_INPUT_BUFFER_PADDING_SIZE);
106
107 2 ret = avpriv_mpeg4audio_get_config2(&cfg, codec_config->extradata,
108 codec_config->extradata_size, 1, logctx);
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
110 return ret;
111
112 2 codec_config->sample_rate = cfg.sample_rate;
113
114 2 return 0;
115 }
116
117 20 static int flac_decoder_config(IAMFCodecConfig *codec_config,
118 AVIOContext *pb, int len)
119 {
120 int ret, left;
121
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (codec_config->audio_roll_distance)
123 return AVERROR_INVALIDDATA;
124
125 20 avio_skip(pb, 4); // METADATA_BLOCK_HEADER
126
127 20 left = len - avio_tell(pb);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (left < FLAC_STREAMINFO_SIZE)
129 return AVERROR_INVALIDDATA;
130
131 20 codec_config->extradata = av_malloc(left);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!codec_config->extradata)
133 return AVERROR(ENOMEM);
134
135 20 ret = ffio_read_size(pb, codec_config->extradata, left);
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
137 return ret;
138
139 20 codec_config->extradata_size = left;
140 20 codec_config->sample_rate = AV_RB24(codec_config->extradata + 10) >> 4;
141
142 20 return 0;
143 }
144
145 static int ipcm_decoder_config(IAMFCodecConfig *codec_config,
146 AVIOContext *pb, int len)
147 {
148 static const enum AVCodecID sample_fmt[2][3] = {
149 { AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S24BE, AV_CODEC_ID_PCM_S32BE },
150 { AV_CODEC_ID_PCM_S16LE, AV_CODEC_ID_PCM_S24LE, AV_CODEC_ID_PCM_S32LE },
151 };
152 int sample_format = avio_r8(pb); // 0 = BE, 1 = LE
153 int sample_size = (avio_r8(pb) / 8 - 2); // 16, 24, 32
154 if (sample_format > 1 || sample_size > 2U || codec_config->audio_roll_distance)
155 return AVERROR_INVALIDDATA;
156
157 codec_config->codec_id = sample_fmt[sample_format][sample_size];
158 codec_config->sample_rate = avio_rb32(pb);
159
160 if (len - avio_tell(pb))
161 return AVERROR_INVALIDDATA;
162
163 return 0;
164 }
165
166 27 static int codec_config_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
167 {
168 27 IAMFCodecConfig **tmp, *codec_config = NULL;
169 FFIOContext b;
170 AVIOContext *pbc;
171 uint8_t *buf;
172 enum AVCodecID avcodec_id;
173 unsigned codec_config_id, nb_samples, codec_id;
174 int16_t audio_roll_distance;
175 int ret;
176
177 27 buf = av_malloc(len);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!buf)
179 return AVERROR(ENOMEM);
180
181 27 ret = ffio_read_size(pb, buf, len);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
183 goto fail;
184
185 27 ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
186 27 pbc = &b.pub;
187
188 27 codec_config_id = ffio_read_leb(pbc);
189 27 codec_id = avio_rb32(pbc);
190 27 nb_samples = ffio_read_leb(pbc);
191 27 audio_roll_distance = avio_rb16(pbc);
192
193
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
27 switch(codec_id) {
194 5 case MKBETAG('O','p','u','s'):
195 5 avcodec_id = AV_CODEC_ID_OPUS;
196 5 break;
197 2 case MKBETAG('m','p','4','a'):
198 2 avcodec_id = AV_CODEC_ID_AAC;
199 2 break;
200 20 case MKBETAG('f','L','a','C'):
201 20 avcodec_id = AV_CODEC_ID_FLAC;
202 20 break;
203 default:
204 avcodec_id = AV_CODEC_ID_NONE;
205 break;
206 }
207
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 for (int i = 0; i < c->nb_codec_configs; i++)
209 if (c->codec_configs[i]->codec_config_id == codec_config_id) {
210 ret = AVERROR_INVALIDDATA;
211 goto fail;
212 }
213
214 27 tmp = av_realloc_array(c->codec_configs, c->nb_codec_configs + 1, sizeof(*c->codec_configs));
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!tmp) {
216 ret = AVERROR(ENOMEM);
217 goto fail;
218 }
219 27 c->codec_configs = tmp;
220
221 27 codec_config = av_mallocz(sizeof(*codec_config));
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!codec_config) {
223 ret = AVERROR(ENOMEM);
224 goto fail;
225 }
226
227 27 codec_config->codec_config_id = codec_config_id;
228 27 codec_config->codec_id = avcodec_id;
229 27 codec_config->nb_samples = nb_samples;
230 27 codec_config->audio_roll_distance = audio_roll_distance;
231
232
3/5
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
27 switch(codec_id) {
233 5 case MKBETAG('O','p','u','s'):
234 5 ret = opus_decoder_config(codec_config, pbc, len);
235 5 break;
236 2 case MKBETAG('m','p','4','a'):
237 2 ret = aac_decoder_config(codec_config, pbc, len, s);
238 2 break;
239 20 case MKBETAG('f','L','a','C'):
240 20 ret = flac_decoder_config(codec_config, pbc, len);
241 20 break;
242 case MKBETAG('i','p','c','m'):
243 ret = ipcm_decoder_config(codec_config, pbc, len);
244 break;
245 default:
246 break;
247 }
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
249 goto fail;
250
251
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
27 if ((codec_config->nb_samples > INT_MAX) || codec_config->nb_samples <= 0 ||
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 (-codec_config->audio_roll_distance > INT_MAX / codec_config->nb_samples)) {
253 ret = AVERROR_INVALIDDATA;
254 goto fail;
255 }
256
257 27 c->codec_configs[c->nb_codec_configs++] = codec_config;
258
259 27 len -= avio_tell(pbc);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (len)
261 av_log(s, AV_LOG_WARNING, "Underread in codec_config_obu. %d bytes left at the end\n", len);
262
263 27 ret = 0;
264 27 fail:
265 27 av_free(buf);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0) {
267 if (codec_config)
268 av_free(codec_config->extradata);
269 av_free(codec_config);
270 }
271 27 return ret;
272 }
273
274 128 static int update_extradata(AVCodecParameters *codecpar)
275 {
276 GetBitContext gb;
277 PutBitContext pb;
278 int ret;
279
280
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
128 switch(codecpar->codec_id) {
281 20 case AV_CODEC_ID_OPUS:
282 20 AV_WB8(codecpar->extradata + 9, codecpar->ch_layout.nb_channels);
283 20 AV_WL16A(codecpar->extradata + 10, AV_RB16A(codecpar->extradata + 10)); // Byte swap pre-skip
284 20 AV_WL32A(codecpar->extradata + 12, AV_RB32A(codecpar->extradata + 12)); // Byte swap sample rate
285 20 AV_WL16A(codecpar->extradata + 16, AV_RB16A(codecpar->extradata + 16)); // Byte swap Output Gain
286 20 break;
287 2 case AV_CODEC_ID_AAC: {
288 uint8_t buf[6];
289 2 int size = FFMIN(codecpar->extradata_size, sizeof(buf));
290
291 2 init_put_bits(&pb, buf, size);
292 2 ret = init_get_bits8(&gb, codecpar->extradata, size);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
294 return ret;
295
296 2 ret = get_bits(&gb, 5);
297 2 put_bits(&pb, 5, ret);
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret == AOT_ESCAPE) // violates section 3.11.2, but better check for it
299 put_bits(&pb, 6, get_bits(&gb, 6));
300 2 ret = get_bits(&gb, 4);
301 2 put_bits(&pb, 4, ret);
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret == 0x0f)
303 put_bits(&pb, 24, get_bits(&gb, 24));
304
305 2 skip_bits(&gb, 4);
306 2 put_bits(&pb, 4, codecpar->ch_layout.nb_channels); // set channel config
307 2 ret = put_bits_left(&pb);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 while (ret >= 32) {
309 put_bits32(&pb, get_bits_long(&gb, 32));
310 ret -= 32;
311 }
312 2 put_bits(&pb, ret, get_bits_long(&gb, ret));
313 2 flush_put_bits(&pb);
314
315 2 memcpy(codecpar->extradata, buf, put_bytes_output(&pb));
316 2 break;
317 }
318 106 case AV_CODEC_ID_FLAC: {
319 uint8_t buf[13];
320
321 106 init_put_bits(&pb, buf, sizeof(buf));
322 106 ret = init_get_bits8(&gb, codecpar->extradata, codecpar->extradata_size);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (ret < 0)
324 return ret;
325
326 106 put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
327 106 put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
328 106 put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
329 106 skip_bits(&gb, 3);
330 106 put_bits(&pb, 3, codecpar->ch_layout.nb_channels - 1);
331 106 ret = put_bits_left(&pb);
332 106 put_bits(&pb, ret, get_bits(&gb, ret));
333 106 flush_put_bits(&pb);
334
335 106 memcpy(codecpar->extradata, buf, sizeof(buf));
336 106 break;
337 }
338 }
339
340 128 return 0;
341 }
342
343 25 static int scalable_channel_layout_config(void *s, AVIOContext *pb,
344 IAMFAudioElement *audio_element,
345 const IAMFCodecConfig *codec_config)
346 {
347 25 int nb_layers, k = 0;
348
349 25 nb_layers = avio_r8(pb) >> 5; // get_bits(&gb, 3);
350 // skip_bits(&gb, 5); //reserved
351
352
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
25 if (nb_layers > 6 || nb_layers == 0)
353 return AVERROR_INVALIDDATA;
354
355 25 audio_element->layers = av_calloc(nb_layers, sizeof(*audio_element->layers));
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!audio_element->layers)
357 return AVERROR(ENOMEM);
358
359 25 audio_element->nb_layers = nb_layers;
360
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 25 times.
85 for (int i = 0; i < nb_layers; i++) {
361 AVIAMFLayer *layer;
362 int loudspeaker_layout, output_gain_is_present_flag;
363 int substream_count, coupled_substream_count;
364 60 int expanded_loudspeaker_layout = -1;
365 60 int ret, byte = avio_r8(pb);
366
367 60 layer = av_iamf_audio_element_add_layer(audio_element->element);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!layer)
369 return AVERROR(ENOMEM);
370
371 60 loudspeaker_layout = byte >> 4; // get_bits(&gb, 4);
372 60 output_gain_is_present_flag = (byte >> 3) & 1; //get_bits1(&gb);
373
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 55 times.
60 if ((byte >> 2) & 1)
374 5 layer->flags |= AV_IAMF_LAYER_FLAG_RECON_GAIN;
375 60 substream_count = avio_r8(pb);
376 60 coupled_substream_count = avio_r8(pb);
377
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (substream_count + k > audio_element->nb_substreams)
379 return AVERROR_INVALIDDATA;
380
381 60 audio_element->layers[i].substream_count = substream_count;
382 60 audio_element->layers[i].coupled_substream_count = coupled_substream_count;
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (output_gain_is_present_flag) {
384 layer->output_gain_flags = avio_r8(pb) >> 2; // get_bits(&gb, 6);
385 layer->output_gain = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 8);
386 }
387
388
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 23 times.
60 if (!i && loudspeaker_layout == 15)
389 2 expanded_loudspeaker_layout = avio_r8(pb);
390
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
60 if (expanded_loudspeaker_layout > 0 && expanded_loudspeaker_layout < 13)
391 2 av_channel_layout_copy(&layer->ch_layout, &ff_iamf_expanded_scalable_ch_layouts[expanded_loudspeaker_layout]);
392
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 else if (loudspeaker_layout < 10)
393 58 av_channel_layout_copy(&layer->ch_layout, &ff_iamf_scalable_ch_layouts[loudspeaker_layout]);
394 else
395 layer->ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_UNSPEC,
396 .nb_channels = substream_count +
397 coupled_substream_count };
398
399
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 60 times.
172 for (int j = 0; j < substream_count; j++) {
400 112 IAMFSubStream *substream = &audio_element->substreams[k++];
401
402
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 34 times.
112 substream->codecpar->ch_layout = coupled_substream_count-- > 0 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
403 (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
404
405 112 ret = update_extradata(substream->codecpar);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (ret < 0)
407 return ret;
408 }
409
410 }
411
412 25 return 0;
413 }
414
415 4 static int ambisonics_config(void *s, AVIOContext *pb,
416 IAMFAudioElement *audio_element,
417 const IAMFCodecConfig *codec_config)
418 {
419 AVIAMFLayer *layer;
420 unsigned ambisonics_mode;
421 int output_channel_count, substream_count, order;
422 int ret;
423
424 4 ambisonics_mode = ffio_read_leb(pb);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ambisonics_mode > 1)
426 return AVERROR_INVALIDDATA;
427
428 4 output_channel_count = avio_r8(pb); // C
429 4 substream_count = avio_r8(pb); // N
430
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (audio_element->nb_substreams != substream_count || output_channel_count == 0)
431 return AVERROR_INVALIDDATA;
432
433 4 order = floor(sqrt(output_channel_count - 1));
434 /* incomplete order - some harmonics are missing */
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if ((order + 1) * (order + 1) != output_channel_count)
436 return AVERROR_INVALIDDATA;
437
438 4 audio_element->layers = av_mallocz(sizeof(*audio_element->layers));
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!audio_element->layers)
440 return AVERROR(ENOMEM);
441
442 4 audio_element->nb_layers = 1;
443 4 audio_element->layers->substream_count = substream_count;
444
445 4 layer = av_iamf_audio_element_add_layer(audio_element->element);
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!layer)
447 return AVERROR(ENOMEM);
448
449 4 layer->ambisonics_mode = ambisonics_mode;
450
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ambisonics_mode == 0) {
451
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < substream_count; i++) {
452 16 IAMFSubStream *substream = &audio_element->substreams[i];
453
454 16 substream->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
455
456 16 ret = update_extradata(substream->codecpar);
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
458 return ret;
459 }
460
461 4 layer->ch_layout.order = AV_CHANNEL_ORDER_CUSTOM;
462 4 layer->ch_layout.nb_channels = output_channel_count;
463 4 layer->ch_layout.u.map = av_calloc(output_channel_count, sizeof(*layer->ch_layout.u.map));
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!layer->ch_layout.u.map)
465 return AVERROR(ENOMEM);
466
467
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < output_channel_count; i++)
468 16 layer->ch_layout.u.map[i].id = avio_r8(pb) + AV_CHAN_AMBISONIC_BASE;
469 } else {
470 int coupled_substream_count = avio_r8(pb); // M
471 int nb_demixing_matrix = substream_count + coupled_substream_count;
472 int demixing_matrix_size = nb_demixing_matrix * output_channel_count;
473
474 audio_element->layers->coupled_substream_count = coupled_substream_count;
475
476 layer->ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = output_channel_count };
477 layer->demixing_matrix = av_malloc_array(demixing_matrix_size, sizeof(*layer->demixing_matrix));
478 if (!layer->demixing_matrix)
479 return AVERROR(ENOMEM);
480
481 for (int i = 0; i < demixing_matrix_size; i++)
482 layer->demixing_matrix[i] = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 8);
483
484 for (int i = 0; i < substream_count; i++) {
485 IAMFSubStream *substream = &audio_element->substreams[i];
486
487 substream->codecpar->ch_layout = coupled_substream_count-- > 0 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
488 (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
489
490
491 ret = update_extradata(substream->codecpar);
492 if (ret < 0)
493 return ret;
494 }
495 }
496
497 4 return 0;
498 }
499
500 84 static int param_parse(void *s, IAMFContext *c, AVIOContext *pb,
501 unsigned int type,
502 const IAMFAudioElement *audio_element,
503 AVIAMFParamDefinition **out_param_definition)
504 {
505 84 IAMFParamDefinition *param_definition = NULL;
506 AVIAMFParamDefinition *param;
507 unsigned int parameter_id, parameter_rate, mode;
508 84 unsigned int duration = 0, constant_subblock_duration = 0, nb_subblocks = 0;
509 84 unsigned int total_duration = 0;
510 size_t param_size;
511
512 84 parameter_id = ffio_read_leb(pb);
513
514
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 47 times.
129 for (int i = 0; i < c->nb_param_definitions; i++)
515
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 45 times.
82 if (c->param_definitions[i]->param->parameter_id == parameter_id) {
516 37 param_definition = c->param_definitions[i];
517 37 break;
518 }
519
520 84 parameter_rate = ffio_read_leb(pb);
521 84 mode = avio_r8(pb) >> 7;
522
523
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 64 times.
84 if (mode == 0) {
524 20 duration = ffio_read_leb(pb);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!duration)
526 return AVERROR_INVALIDDATA;
527 20 constant_subblock_duration = ffio_read_leb(pb);
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (constant_subblock_duration == 0)
529 nb_subblocks = ffio_read_leb(pb);
530 else {
531 20 nb_subblocks = duration / constant_subblock_duration;
532 20 total_duration = duration;
533 }
534 }
535
536 84 param = av_iamf_param_definition_alloc(type, nb_subblocks, &param_size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (!param)
538 return AVERROR(ENOMEM);
539
540
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 84 times.
104 for (int i = 0; i < nb_subblocks; i++) {
541 20 void *subblock = av_iamf_param_definition_get_subblock(param, i);
542 20 unsigned int subblock_duration = constant_subblock_duration;
543
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (constant_subblock_duration == 0) {
545 subblock_duration = ffio_read_leb(pb);
546 total_duration += subblock_duration;
547
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 } else if (i == nb_subblocks - 1)
548 20 subblock_duration = duration - i * constant_subblock_duration;
549
550
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
20 switch (type) {
551 case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
552 AVIAMFMixGain *mix = subblock;
553 mix->subblock_duration = subblock_duration;
554 break;
555 }
556 15 case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
557 15 AVIAMFDemixingInfo *demix = subblock;
558 15 demix->subblock_duration = subblock_duration;
559 // DefaultDemixingInfoParameterData
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_assert0(audio_element);
561 15 demix->dmixp_mode = avio_r8(pb) >> 5;
562 15 audio_element->element->default_w = avio_r8(pb) >> 4;
563 15 break;
564 }
565 5 case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
566 5 AVIAMFReconGain *recon = subblock;
567 5 recon->subblock_duration = subblock_duration;
568 5 break;
569 }
570 default:
571 av_free(param);
572 return AVERROR_INVALIDDATA;
573 }
574 }
575
576
3/6
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
84 if (!mode && !constant_subblock_duration && total_duration != duration) {
577 av_log(s, AV_LOG_ERROR, "Invalid subblock durations in parameter_id %u\n", parameter_id);
578 av_free(param);
579 return AVERROR_INVALIDDATA;
580 }
581
582 84 param->parameter_id = parameter_id;
583 84 param->parameter_rate = parameter_rate;
584 84 param->duration = duration;
585 84 param->constant_subblock_duration = constant_subblock_duration;
586 84 param->nb_subblocks = nb_subblocks;
587
588
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 47 times.
84 if (param_definition) {
589
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
37 if (param_definition->param_size != param_size || memcmp(param_definition->param, param, param_size)) {
590 av_log(s, AV_LOG_ERROR, "Incosistent parameters for parameter_id %u\n", parameter_id);
591 av_free(param);
592 return AVERROR_INVALIDDATA;
593 }
594 } else {
595 47 IAMFParamDefinition **tmp = av_realloc_array(c->param_definitions, c->nb_param_definitions + 1,
596 sizeof(*c->param_definitions));
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!tmp) {
598 av_free(param);
599 return AVERROR(ENOMEM);
600 }
601 47 c->param_definitions = tmp;
602
603 47 param_definition = av_mallocz(sizeof(*param_definition));
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!param_definition) {
605 av_free(param);
606 return AVERROR(ENOMEM);
607 }
608 47 param_definition->param = param;
609 47 param_definition->mode = !mode;
610 47 param_definition->param_size = param_size;
611 47 param_definition->audio_element = audio_element;
612
613 47 c->param_definitions[c->nb_param_definitions++] = param_definition;
614 }
615
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 av_assert0(out_param_definition);
617 84 *out_param_definition = param;
618
619 84 return 0;
620 }
621
622 29 static int audio_element_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
623 {
624 const IAMFCodecConfig *codec_config;
625 AVIAMFAudioElement *element;
626 29 IAMFAudioElement **tmp, *audio_element = NULL;
627 FFIOContext b;
628 AVIOContext *pbc;
629 uint8_t *buf;
630 unsigned audio_element_id, nb_substreams, codec_config_id, num_parameters;
631 int audio_element_type, ret;
632
633 29 buf = av_malloc(len);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!buf)
635 return AVERROR(ENOMEM);
636
637 29 ret = ffio_read_size(pb, buf, len);
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
639 goto fail;
640
641 29 ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
642 29 pbc = &b.pub;
643
644 29 audio_element_id = ffio_read_leb(pbc);
645
646
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 for (int i = 0; i < c->nb_audio_elements; i++)
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (c->audio_elements[i]->audio_element_id == audio_element_id) {
648 av_log(s, AV_LOG_ERROR, "Duplicate audio_element_id %d\n", audio_element_id);
649 ret = AVERROR_INVALIDDATA;
650 goto fail;
651 }
652
653 29 audio_element_type = avio_r8(pbc) >> 5;
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (audio_element_type > AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
655 av_log(s, AV_LOG_DEBUG, "Unknown audio_element_type referenced in an audio element. Ignoring\n");
656 ret = 0;
657 goto fail;
658 }
659
660 29 codec_config_id = ffio_read_leb(pbc);
661
662 29 codec_config = ff_iamf_get_codec_config(c, codec_config_id);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!codec_config) {
664 av_log(s, AV_LOG_ERROR, "Non existant codec config id %d referenced in an audio element\n", codec_config_id);
665 ret = AVERROR_INVALIDDATA;
666 goto fail;
667 }
668
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (codec_config->codec_id == AV_CODEC_ID_NONE) {
670 av_log(s, AV_LOG_DEBUG, "Unknown codec id referenced in an audio element. Ignoring\n");
671 ret = 0;
672 goto fail;
673 }
674
675 29 tmp = av_realloc_array(c->audio_elements, c->nb_audio_elements + 1, sizeof(*c->audio_elements));
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!tmp) {
677 ret = AVERROR(ENOMEM);
678 goto fail;
679 }
680 29 c->audio_elements = tmp;
681
682 29 audio_element = av_mallocz(sizeof(*audio_element));
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!audio_element) {
684 ret = AVERROR(ENOMEM);
685 goto fail;
686 }
687
688 29 nb_substreams = ffio_read_leb(pbc);
689 29 audio_element->codec_config_id = codec_config_id;
690 29 audio_element->audio_element_id = audio_element_id;
691 29 audio_element->substreams = av_calloc(nb_substreams, sizeof(*audio_element->substreams));
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!audio_element->substreams) {
693 ret = AVERROR(ENOMEM);
694 goto fail;
695 }
696 29 audio_element->nb_substreams = nb_substreams;
697
698 29 element = audio_element->element = av_iamf_audio_element_alloc();
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!element) {
700 ret = AVERROR(ENOMEM);
701 goto fail;
702 }
703 29 audio_element->celement = element;
704
705 29 element->audio_element_type = audio_element_type;
706
707
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 29 times.
157 for (int i = 0; i < audio_element->nb_substreams; i++) {
708 128 IAMFSubStream *substream = &audio_element->substreams[i];
709
710 128 substream->codecpar = avcodec_parameters_alloc();
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (!substream->codecpar) {
712 ret = AVERROR(ENOMEM);
713 goto fail;
714 }
715
716 128 substream->audio_substream_id = ffio_read_leb(pbc);
717
718 128 substream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
719 128 substream->codecpar->codec_id = codec_config->codec_id;
720 128 substream->codecpar->frame_size = codec_config->nb_samples;
721 128 substream->codecpar->sample_rate = codec_config->sample_rate;
722 128 substream->codecpar->seek_preroll = -codec_config->audio_roll_distance * codec_config->nb_samples;
723
724
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 switch(substream->codecpar->codec_id) {
725 128 case AV_CODEC_ID_AAC:
726 case AV_CODEC_ID_FLAC:
727 case AV_CODEC_ID_OPUS:
728 128 substream->codecpar->extradata = av_malloc(codec_config->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (!substream->codecpar->extradata) {
730 ret = AVERROR(ENOMEM);
731 goto fail;
732 }
733 128 memcpy(substream->codecpar->extradata, codec_config->extradata, codec_config->extradata_size);
734 128 memset(substream->codecpar->extradata + codec_config->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
735 128 substream->codecpar->extradata_size = codec_config->extradata_size;
736 128 break;
737 }
738 }
739
740 29 num_parameters = ffio_read_leb(pbc);
741
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29 if (num_parameters > 2 && audio_element_type == 0) {
742 av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
743 " for Channel representations\n", num_parameters);
744 ret = AVERROR_INVALIDDATA;
745 goto fail;
746 }
747
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
29 if (num_parameters && audio_element_type != 0) {
748 av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
749 " for Scene representations\n", num_parameters);
750 ret = AVERROR_INVALIDDATA;
751 goto fail;
752 }
753
754
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 29 times.
49 for (int i = 0; i < num_parameters; i++) {
755 unsigned type;
756
757 20 type = ffio_read_leb(pbc);
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (type == AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN)
759 ret = AVERROR_INVALIDDATA;
760
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 else if (type == AV_IAMF_PARAMETER_DEFINITION_DEMIXING) {
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (element->demixing_info) {
762 ret = AVERROR_INVALIDDATA;
763 goto fail;
764 }
765 15 ret = param_parse(s, c, pbc, type, audio_element, &element->demixing_info);
766
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (type == AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (element->recon_gain_info) {
768 ret = AVERROR_INVALIDDATA;
769 goto fail;
770 }
771 5 ret = param_parse(s, c, pbc, type, audio_element, &element->recon_gain_info);
772 } else {
773 unsigned param_definition_size = ffio_read_leb(pbc);
774 avio_skip(pbc, param_definition_size);
775 }
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
777 goto fail;
778 }
779
780
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL) {
781 25 ret = scalable_channel_layout_config(s, pbc, audio_element, codec_config);
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
783 goto fail;
784
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
785 4 ret = ambisonics_config(s, pbc, audio_element, codec_config);
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
787 goto fail;
788 } else {
789 av_assert0(0);
790 }
791
792 29 c->audio_elements[c->nb_audio_elements++] = audio_element;
793
794 29 len -= avio_tell(pbc);
795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (len)
796 av_log(s, AV_LOG_WARNING, "Underread in audio_element_obu. %d bytes left at the end\n", len);
797
798 29 ret = 0;
799 29 fail:
800 29 av_free(buf);
801
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
802 ff_iamf_free_audio_element(&audio_element);
803 29 return ret;
804 }
805
806 87 static int label_string(AVIOContext *pb, char **label)
807 {
808 uint8_t buf[128];
809
810 87 avio_get_str(pb, sizeof(buf), buf, sizeof(buf));
811
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (pb->error)
813 return pb->error;
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (pb->eof_reached)
815 return AVERROR_INVALIDDATA;
816 87 *label = av_strdup(buf);
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!*label)
818 return AVERROR(ENOMEM);
819
820 87 return 0;
821 }
822
823 27 static int mix_presentation_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
824 {
825 AVIAMFMixPresentation *mix;
826 27 IAMFMixPresentation **tmp, *mix_presentation = NULL;
827 FFIOContext b;
828 AVIOContext *pbc;
829 uint8_t *buf;
830 unsigned nb_submixes, mix_presentation_id;
831 int ret;
832
833 27 buf = av_malloc(len);
834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!buf)
835 return AVERROR(ENOMEM);
836
837 27 ret = ffio_read_size(pb, buf, len);
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
839 goto fail;
840
841 27 ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
842 27 pbc = &b.pub;
843
844 27 mix_presentation_id = ffio_read_leb(pbc);
845
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 for (int i = 0; i < c->nb_mix_presentations; i++)
847 if (c->mix_presentations[i]->mix_presentation_id == mix_presentation_id) {
848 av_log(s, AV_LOG_ERROR, "Duplicate mix_presentation_id %d\n", mix_presentation_id);
849 ret = AVERROR_INVALIDDATA;
850 goto fail;
851 }
852
853 27 tmp = av_realloc_array(c->mix_presentations, c->nb_mix_presentations + 1, sizeof(*c->mix_presentations));
854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!tmp) {
855 ret = AVERROR(ENOMEM);
856 goto fail;
857 }
858 27 c->mix_presentations = tmp;
859
860 27 mix_presentation = av_mallocz(sizeof(*mix_presentation));
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix_presentation) {
862 ret = AVERROR(ENOMEM);
863 goto fail;
864 }
865
866 27 mix_presentation->mix_presentation_id = mix_presentation_id;
867 27 mix = mix_presentation->mix = av_iamf_mix_presentation_alloc();
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix) {
869 ret = AVERROR(ENOMEM);
870 goto fail;
871 }
872 27 mix_presentation->cmix = mix;
873
874 27 mix_presentation->count_label = ffio_read_leb(pbc);
875 27 mix_presentation->language_label = av_calloc(mix_presentation->count_label,
876 sizeof(*mix_presentation->language_label));
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix_presentation->language_label) {
878 mix_presentation->count_label = 0;
879 ret = AVERROR(ENOMEM);
880 goto fail;
881 }
882
883
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < mix_presentation->count_label; i++) {
884 27 ret = label_string(pbc, &mix_presentation->language_label[i]);
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
886 goto fail;
887 }
888
889
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < mix_presentation->count_label; i++) {
890 27 char *annotation = NULL;
891 27 ret = label_string(pbc, &annotation);
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
893 goto fail;
894 27 ret = av_dict_set(&mix->annotations, mix_presentation->language_label[i], annotation,
895 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
897 goto fail;
898 }
899
900 27 nb_submixes = ffio_read_leb(pbc);
901
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 27 times.
58 for (int i = 0; i < nb_submixes; i++) {
902 AVIAMFSubmix *sub_mix;
903 unsigned nb_elements, nb_layouts;
904
905 31 sub_mix = av_iamf_mix_presentation_add_submix(mix);
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!sub_mix) {
907 ret = AVERROR(ENOMEM);
908 goto fail;
909 }
910
911 31 nb_elements = ffio_read_leb(pbc);
912
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 31 times.
64 for (int j = 0; j < nb_elements; j++) {
913 AVIAMFSubmixElement *submix_element;
914 33 IAMFAudioElement *audio_element = NULL;
915 unsigned int rendering_config_extension_size;
916
917 33 submix_element = av_iamf_submix_add_element(sub_mix);
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!submix_element) {
919 ret = AVERROR(ENOMEM);
920 goto fail;
921 }
922
923 33 submix_element->audio_element_id = ffio_read_leb(pbc);
924
925
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 for (int k = 0; k < c->nb_audio_elements; k++)
926
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (c->audio_elements[k]->audio_element_id == submix_element->audio_element_id) {
927 33 audio_element = c->audio_elements[k];
928 33 break;
929 }
930
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!audio_element) {
932 av_log(s, AV_LOG_ERROR, "Invalid Audio Element with id %u referenced by Mix Parameters %u\n",
933 submix_element->audio_element_id, mix_presentation_id);
934 ret = AVERROR_INVALIDDATA;
935 goto fail;
936 }
937
938
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 for (int k = 0; k < mix_presentation->count_label; k++) {
939 33 char *annotation = NULL;
940 33 ret = label_string(pbc, &annotation);
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
942 goto fail;
943 33 ret = av_dict_set(&submix_element->annotations, mix_presentation->language_label[k], annotation,
944 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
946 goto fail;
947 }
948
949 33 submix_element->headphones_rendering_mode = avio_r8(pbc) >> 6;
950
951 33 rendering_config_extension_size = ffio_read_leb(pbc);
952 33 avio_skip(pbc, rendering_config_extension_size);
953
954 33 ret = param_parse(s, c, pbc, AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN,
955 NULL,
956 &submix_element->element_mix_config);
957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
958 goto fail;
959 33 submix_element->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
960 }
961
962 31 ret = param_parse(s, c, pbc, AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, NULL, &sub_mix->output_mix_config);
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
964 goto fail;
965 31 sub_mix->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
966
967 31 nb_layouts = ffio_read_leb(pbc);
968
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 31 times.
99 for (int j = 0; j < nb_layouts; j++) {
969 AVIAMFSubmixLayout *submix_layout;
970 int info_type;
971 68 int byte = avio_r8(pbc);
972
973 68 submix_layout = av_iamf_submix_add_layout(sub_mix);
974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!submix_layout) {
975 ret = AVERROR(ENOMEM);
976 goto fail;
977 }
978
979 68 submix_layout->layout_type = byte >> 6;
980
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (submix_layout->layout_type < AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS ||
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 submix_layout->layout_type > AV_IAMF_SUBMIX_LAYOUT_TYPE_BINAURAL) {
982 av_log(s, AV_LOG_ERROR, "Invalid Layout type %u in a submix from Mix Presentation %u\n",
983 submix_layout->layout_type, mix_presentation_id);
984 ret = AVERROR_INVALIDDATA;
985 goto fail;
986 }
987
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
68 if (submix_layout->layout_type == 2) {
988 int sound_system;
989 64 sound_system = (byte >> 2) & 0xF;
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (sound_system >= FF_ARRAY_ELEMS(ff_iamf_sound_system_map)) {
991 ret = AVERROR_INVALIDDATA;
992 goto fail;
993 }
994 64 av_channel_layout_copy(&submix_layout->sound_system, &ff_iamf_sound_system_map[sound_system].layout);
995 } else
996 4 submix_layout->sound_system = (AVChannelLayout)AV_CHANNEL_LAYOUT_BINAURAL;
997
998 68 info_type = avio_r8(pbc);
999 68 submix_layout->integrated_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1000 68 submix_layout->digital_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1001
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 1)
1003 submix_layout->true_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 2) {
1005 unsigned int num_anchored_loudness = avio_r8(pbc);
1006
1007 for (int k = 0; k < num_anchored_loudness; k++) {
1008 unsigned int anchor_element = avio_r8(pbc);
1009 AVRational anchored_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1010 if (anchor_element == IAMF_ANCHOR_ELEMENT_DIALOGUE)
1011 submix_layout->dialogue_anchored_loudness = anchored_loudness;
1012 else if (anchor_element <= IAMF_ANCHOR_ELEMENT_ALBUM)
1013 submix_layout->album_anchored_loudness = anchored_loudness;
1014 else
1015 av_log(s, AV_LOG_DEBUG, "Unknown anchor_element. Ignoring\n");
1016 }
1017 }
1018
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 0xFC) {
1020 unsigned int info_type_size = ffio_read_leb(pbc);
1021 avio_skip(pbc, info_type_size);
1022 }
1023 }
1024 }
1025
1026 27 c->mix_presentations[c->nb_mix_presentations++] = mix_presentation;
1027
1028 27 len -= avio_tell(pbc);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (len)
1030 av_log(s, AV_LOG_WARNING, "Underread in mix_presentation_obu. %d bytes left at the end\n", len);
1031
1032 27 ret = 0;
1033 27 fail:
1034 27 av_free(buf);
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
1036 ff_iamf_free_mix_presentation(&mix_presentation);
1037 27 return ret;
1038 }
1039
1040 8210 int ff_iamf_parse_obu_header(const uint8_t *buf, int buf_size,
1041 unsigned *obu_size, int *start_pos, enum IAMF_OBU_Type *type,
1042 unsigned *skip_samples, unsigned *discard_padding)
1043 {
1044 GetBitContext gb;
1045 int ret, extension_flag, trimming, start;
1046 8210 unsigned skip = 0, discard = 0;
1047 unsigned size;
1048
1049 8210 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_IAMF_OBU_HEADER_SIZE));
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8210 times.
8210 if (ret < 0)
1051 return ret;
1052
1053 8210 *type = get_bits(&gb, 5);
1054 8210 /*redundant =*/ get_bits1(&gb);
1055 8210 trimming = get_bits1(&gb);
1056 8210 extension_flag = get_bits1(&gb);
1057
1058 8210 *obu_size = get_leb(&gb);
1059
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8201 times.
8210 if (*obu_size > INT_MAX)
1060 9 return AVERROR_INVALIDDATA;
1061
1062 8201 start = get_bits_count(&gb) / 8;
1063
1064
2/2
✓ Branch 0 taken 2343 times.
✓ Branch 1 taken 5858 times.
8201 if (trimming) {
1065 2343 discard = get_leb(&gb); // num_samples_to_trim_at_end
1066 2343 skip = get_leb(&gb); // num_samples_to_trim_at_start
1067 }
1068
1069
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 7418 times.
8201 if (skip_samples)
1070 783 *skip_samples = skip;
1071
2/2
✓ Branch 0 taken 783 times.
✓ Branch 1 taken 7418 times.
8201 if (discard_padding)
1072 783 *discard_padding = discard;
1073
1074
2/2
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 7468 times.
8201 if (extension_flag) {
1075 unsigned int extension_bytes;
1076 733 extension_bytes = get_leb(&gb);
1077
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 715 times.
733 if (extension_bytes > INT_MAX / 8)
1078 18 return AVERROR_INVALIDDATA;
1079 715 skip_bits_long(&gb, extension_bytes * 8);
1080 }
1081
1082
2/2
✓ Branch 1 taken 531 times.
✓ Branch 2 taken 7652 times.
8183 if (get_bits_left(&gb) < 0)
1083 531 return AVERROR_INVALIDDATA;
1084
1085 7652 size = *obu_size + start;
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7652 times.
7652 if (size > INT_MAX)
1087 return AVERROR_INVALIDDATA;
1088
1089 7652 *obu_size -= get_bits_count(&gb) / 8 - start;
1090 7652 *start_pos = size - *obu_size;
1091
1092 7652 return size;
1093 }
1094
1095 27 int ff_iamfdec_read_descriptors(IAMFContext *c, AVIOContext *pb,
1096 int max_size, void *log_ctx)
1097 {
1098 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
1099 int ret;
1100
1101 100 while (1) {
1102 unsigned obu_size;
1103 enum IAMF_OBU_Type type;
1104 int start_pos, len, size;
1105
1106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
127 if ((ret = ffio_ensure_seekback(pb, FFMIN(MAX_IAMF_OBU_HEADER_SIZE, max_size))) < 0)
1107 return ret;
1108 127 size = avio_read(pb, header, FFMIN(MAX_IAMF_OBU_HEADER_SIZE, max_size));
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (size < 0)
1110 return size;
1111 127 memset(header + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1112
1113 127 len = ff_iamf_parse_obu_header(header, size, &obu_size, &start_pos, &type, NULL, NULL);
1114
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 127 times.
127 if (len < 0 || obu_size > max_size) {
1115 av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu header\n");
1116 avio_seek(pb, -size, SEEK_CUR);
1117 return len;
1118 }
1119
1120
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 27 times.
127 if (type >= IAMF_OBU_IA_PARAMETER_BLOCK && type < IAMF_OBU_IA_SEQUENCE_HEADER) {
1121 17 avio_seek(pb, -size, SEEK_CUR);
1122 17 break;
1123 }
1124
1125 110 avio_seek(pb, -(size - start_pos), SEEK_CUR);
1126
4/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 27 times.
110 switch (type) {
1127 27 case IAMF_OBU_IA_CODEC_CONFIG:
1128 27 ret = codec_config_obu(log_ctx, c, pb, obu_size);
1129 27 break;
1130 29 case IAMF_OBU_IA_AUDIO_ELEMENT:
1131 29 ret = audio_element_obu(log_ctx, c, pb, obu_size);
1132 29 break;
1133 27 case IAMF_OBU_IA_MIX_PRESENTATION:
1134 27 ret = mix_presentation_obu(log_ctx, c, pb, obu_size);
1135 27 break;
1136 27 default: {
1137 27 int64_t offset = avio_skip(pb, obu_size);
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (offset < 0)
1139 ret = offset;
1140 27 break;
1141 }
1142 }
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0) {
1144 av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu type %d\n", type);
1145 return ret;
1146 }
1147 110 max_size -= obu_size + start_pos;
1148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (max_size < 0)
1149 return AVERROR_INVALIDDATA;
1150
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 100 times.
110 if (!max_size)
1151 10 break;
1152 }
1153
1154 27 return 0;
1155 }
1156