FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamf_parse.c
Date: 2025-07-01 21:35:40
Exec Total Coverage
Lines: 555 781 71.1%
Functions: 13 14 92.9%
Branches: 259 441 58.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, sizeof(buf));
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 = get_bits_left(&gb);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
309 return AVERROR_INVALIDDATA;
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 ret = FFMIN(ret, put_bits_left(&pb));
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 while (ret >= 32) {
312 put_bits32(&pb, get_bits_long(&gb, 32));
313 ret -= 32;
314 }
315 2 put_bits(&pb, ret, get_bits_long(&gb, ret));
316 2 flush_put_bits(&pb);
317
318 2 memcpy(codecpar->extradata, buf, put_bytes_output(&pb));
319 2 break;
320 }
321 106 case AV_CODEC_ID_FLAC: {
322 uint8_t buf[13];
323 106 int size = FFMIN(codecpar->extradata_size, sizeof(buf));
324
325 106 init_put_bits(&pb, buf, sizeof(buf));
326 106 ret = init_get_bits8(&gb, codecpar->extradata, size);
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (ret < 0)
328 return ret;
329
330 106 put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
331 106 put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
332 106 put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
333 106 skip_bits(&gb, 3);
334 106 put_bits(&pb, 3, codecpar->ch_layout.nb_channels - 1);
335 106 ret = get_bits_left(&gb);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106 times.
106 if (ret < 0)
337 return AVERROR_INVALIDDATA;
338
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 106 times.
106 ret = FFMIN(ret, put_bits_left(&pb));
339 106 put_bits(&pb, ret, get_bits(&gb, ret));
340 106 flush_put_bits(&pb);
341
342 106 memcpy(codecpar->extradata, buf, put_bytes_output(&pb));
343 106 break;
344 }
345 }
346
347 128 return 0;
348 }
349
350 25 static int scalable_channel_layout_config(void *s, AVIOContext *pb,
351 IAMFAudioElement *audio_element,
352 const IAMFCodecConfig *codec_config)
353 {
354 25 int nb_layers, k = 0;
355
356 25 nb_layers = avio_r8(pb) >> 5; // get_bits(&gb, 3);
357 // skip_bits(&gb, 5); //reserved
358
359
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)
360 return AVERROR_INVALIDDATA;
361
362 25 audio_element->layers = av_calloc(nb_layers, sizeof(*audio_element->layers));
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!audio_element->layers)
364 return AVERROR(ENOMEM);
365
366 25 audio_element->nb_layers = nb_layers;
367
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 25 times.
85 for (int i = 0, n = 0; i < nb_layers; i++) {
368 60 AVChannelLayout ch_layout = { 0 };
369 AVIAMFLayer *layer;
370 int loudspeaker_layout, output_gain_is_present_flag;
371 int substream_count, coupled_substream_count;
372 60 int expanded_loudspeaker_layout = -1;
373 60 int ret, byte = avio_r8(pb);
374
375 60 layer = av_iamf_audio_element_add_layer(audio_element->element);
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!layer)
377 return AVERROR(ENOMEM);
378
379 60 loudspeaker_layout = byte >> 4; // get_bits(&gb, 4);
380 60 output_gain_is_present_flag = (byte >> 3) & 1; //get_bits1(&gb);
381
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 55 times.
60 if ((byte >> 2) & 1)
382 5 layer->flags |= AV_IAMF_LAYER_FLAG_RECON_GAIN;
383 60 substream_count = avio_r8(pb);
384 60 coupled_substream_count = avio_r8(pb);
385
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (substream_count + k > audio_element->nb_substreams)
387 return AVERROR_INVALIDDATA;
388
389 60 audio_element->layers[i].substream_count = substream_count;
390 60 audio_element->layers[i].coupled_substream_count = coupled_substream_count;
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (output_gain_is_present_flag) {
392 layer->output_gain_flags = avio_r8(pb) >> 2; // get_bits(&gb, 6);
393 layer->output_gain = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 8);
394 }
395
396
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)
397 2 expanded_loudspeaker_layout = avio_r8(pb);
398
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) {
399 2 av_channel_layout_copy(&ch_layout, &ff_iamf_expanded_scalable_ch_layouts[expanded_loudspeaker_layout]);
400
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 } else if (loudspeaker_layout < 10) {
401 58 av_channel_layout_copy(&ch_layout, &ff_iamf_scalable_ch_layouts[loudspeaker_layout]);
402
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 23 times.
58 if (i)
403 35 ch_layout.u.mask &= ~av_channel_layout_subset(&audio_element->element->layers[i-1]->ch_layout, UINT64_MAX);
404 } else
405 ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_UNSPEC,
406 .nb_channels = substream_count +
407 coupled_substream_count };
408
409
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 60 times.
172 for (int j = 0; j < substream_count; j++) {
410 112 IAMFSubStream *substream = &audio_element->substreams[k++];
411
412
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 :
413 (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
414
415 112 ret = update_extradata(substream->codecpar);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (ret < 0)
417 return ret;
418 }
419
420
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (ch_layout.order == AV_CHANNEL_ORDER_NATIVE) {
421 60 ret = av_channel_layout_custom_init(&layer->ch_layout, ch_layout.nb_channels);
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (ret < 0)
423 return ret;
424
425
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 60 times.
242 for (int j = 0; j < n; j++)
426 182 layer->ch_layout.u.map[j].id = av_channel_layout_channel_from_index(&audio_element->element->layers[i-1]->ch_layout, j);
427
428 60 coupled_substream_count = audio_element->layers[i].coupled_substream_count;
429
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 60 times.
138 while (coupled_substream_count--) {
430
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 53 times.
78 if (ch_layout.u.mask & AV_CH_LAYOUT_STEREO) {
431 25 layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_LEFT;
432 25 layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_RIGHT;
433 25 ch_layout.u.mask &= ~AV_CH_LAYOUT_STEREO;
434
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 51 times.
53 } else if (ch_layout.u.mask & (AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)) {
435 2 layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_LEFT_OF_CENTER;
436 2 layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_RIGHT_OF_CENTER;
437 2 ch_layout.u.mask &= ~(AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER);
438
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 34 times.
51 } else if (ch_layout.u.mask & (AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT)) {
439 17 layer->ch_layout.u.map[n++].id = AV_CHAN_SIDE_LEFT;
440 17 layer->ch_layout.u.map[n++].id = AV_CHAN_SIDE_RIGHT;
441 17 ch_layout.u.mask &= ~(AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT);
442
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 26 times.
34 } else if (ch_layout.u.mask & (AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)) {
443 8 layer->ch_layout.u.map[n++].id = AV_CHAN_BACK_LEFT;
444 8 layer->ch_layout.u.map[n++].id = AV_CHAN_BACK_RIGHT;
445 8 ch_layout.u.mask &= ~(AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT);
446
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
26 } else if (ch_layout.u.mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)) {
447 12 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_FRONT_LEFT;
448 12 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_FRONT_RIGHT;
449 12 ch_layout.u.mask &= ~(AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT);
450
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 } else if (ch_layout.u.mask & (AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT)) {
451 2 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_SIDE_LEFT;
452 2 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_SIDE_RIGHT;
453 2 ch_layout.u.mask &= ~(AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT);
454
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 } else if (ch_layout.u.mask & (AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)) {
455 12 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_BACK_LEFT;
456 12 layer->ch_layout.u.map[n++].id = AV_CHAN_TOP_BACK_RIGHT;
457 12 ch_layout.u.mask &= ~(AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT);
458 }
459 }
460
461 60 substream_count -= audio_element->layers[i].coupled_substream_count;
462
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 60 times.
94 while (substream_count--) {
463
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 if (ch_layout.u.mask & AV_CH_FRONT_CENTER) {
464 17 layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_CENTER;
465 17 ch_layout.u.mask &= ~AV_CH_FRONT_CENTER;
466 }
467
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 if (ch_layout.u.mask & AV_CH_LOW_FREQUENCY) {
468 17 layer->ch_layout.u.map[n++].id = AV_CHAN_LOW_FREQUENCY;
469 17 ch_layout.u.mask &= ~AV_CH_LOW_FREQUENCY;
470 }
471 }
472
473 60 ret = av_channel_layout_retype(&layer->ch_layout, AV_CHANNEL_ORDER_NATIVE, 0);
474
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
60 if (ret < 0 && ret != AVERROR(ENOSYS))
475 return ret;
476 } else // AV_CHANNEL_ORDER_UNSPEC
477 av_channel_layout_copy(&layer->ch_layout, &ch_layout);
478 }
479
480 25 return 0;
481 }
482
483 4 static int ambisonics_config(void *s, AVIOContext *pb,
484 IAMFAudioElement *audio_element,
485 const IAMFCodecConfig *codec_config)
486 {
487 AVIAMFLayer *layer;
488 unsigned ambisonics_mode;
489 int output_channel_count, substream_count, order;
490 int ret;
491
492 4 ambisonics_mode = ffio_read_leb(pb);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ambisonics_mode > 1)
494 return AVERROR_INVALIDDATA;
495
496 4 output_channel_count = avio_r8(pb); // C
497 4 substream_count = avio_r8(pb); // N
498
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)
499 return AVERROR_INVALIDDATA;
500
501 4 order = floor(sqrt(output_channel_count - 1));
502 /* incomplete order - some harmonics are missing */
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if ((order + 1) * (order + 1) != output_channel_count)
504 return AVERROR_INVALIDDATA;
505
506 4 audio_element->layers = av_mallocz(sizeof(*audio_element->layers));
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!audio_element->layers)
508 return AVERROR(ENOMEM);
509
510 4 audio_element->nb_layers = 1;
511 4 audio_element->layers->substream_count = substream_count;
512
513 4 layer = av_iamf_audio_element_add_layer(audio_element->element);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!layer)
515 return AVERROR(ENOMEM);
516
517 4 layer->ambisonics_mode = ambisonics_mode;
518
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ambisonics_mode == 0) {
519
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < substream_count; i++) {
520 16 IAMFSubStream *substream = &audio_element->substreams[i];
521
522 16 substream->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
523
524 16 ret = update_extradata(substream->codecpar);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (ret < 0)
526 return ret;
527 }
528
529 4 ret = av_channel_layout_custom_init(&layer->ch_layout, output_channel_count);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
531 return ret;
532
533
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < output_channel_count; i++)
534 16 layer->ch_layout.u.map[i].id = avio_r8(pb) + AV_CHAN_AMBISONIC_BASE;
535
536 4 ret = av_channel_layout_retype(&layer->ch_layout, AV_CHANNEL_ORDER_AMBISONIC, 0);
537
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (ret < 0 && ret != AVERROR(ENOSYS))
538 return ret;
539 } else {
540 int coupled_substream_count = avio_r8(pb); // M
541 int nb_demixing_matrix = substream_count + coupled_substream_count;
542 int demixing_matrix_size = nb_demixing_matrix * output_channel_count;
543
544 audio_element->layers->coupled_substream_count = coupled_substream_count;
545
546 layer->ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = output_channel_count };
547 layer->demixing_matrix = av_malloc_array(demixing_matrix_size, sizeof(*layer->demixing_matrix));
548 if (!layer->demixing_matrix)
549 return AVERROR(ENOMEM);
550
551 for (int i = 0; i < demixing_matrix_size; i++)
552 layer->demixing_matrix[i] = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 8);
553
554 for (int i = 0; i < substream_count; i++) {
555 IAMFSubStream *substream = &audio_element->substreams[i];
556
557 substream->codecpar->ch_layout = coupled_substream_count-- > 0 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
558 (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
559
560
561 ret = update_extradata(substream->codecpar);
562 if (ret < 0)
563 return ret;
564 }
565 }
566
567 4 return 0;
568 }
569
570 84 static int param_parse(void *s, IAMFContext *c, AVIOContext *pb,
571 unsigned int type,
572 const IAMFAudioElement *audio_element,
573 AVIAMFParamDefinition **out_param_definition)
574 {
575 84 IAMFParamDefinition *param_definition = NULL;
576 AVIAMFParamDefinition *param;
577 unsigned int parameter_id, parameter_rate, mode;
578 84 unsigned int duration = 0, constant_subblock_duration = 0, nb_subblocks = 0;
579 84 unsigned int total_duration = 0;
580 size_t param_size;
581
582 84 parameter_id = ffio_read_leb(pb);
583
584
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 47 times.
129 for (int i = 0; i < c->nb_param_definitions; i++)
585
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 45 times.
82 if (c->param_definitions[i]->param->parameter_id == parameter_id) {
586 37 param_definition = c->param_definitions[i];
587 37 break;
588 }
589
590 84 parameter_rate = ffio_read_leb(pb);
591 84 mode = avio_r8(pb) >> 7;
592
593
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 64 times.
84 if (mode == 0) {
594 20 duration = ffio_read_leb(pb);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!duration)
596 return AVERROR_INVALIDDATA;
597 20 constant_subblock_duration = ffio_read_leb(pb);
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (constant_subblock_duration == 0)
599 nb_subblocks = ffio_read_leb(pb);
600 else {
601 20 nb_subblocks = duration / constant_subblock_duration;
602 20 total_duration = duration;
603 }
604 }
605
606 84 param = av_iamf_param_definition_alloc(type, nb_subblocks, &param_size);
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 if (!param)
608 return AVERROR(ENOMEM);
609
610
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 84 times.
104 for (int i = 0; i < nb_subblocks; i++) {
611 20 void *subblock = av_iamf_param_definition_get_subblock(param, i);
612 20 unsigned int subblock_duration = constant_subblock_duration;
613
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (constant_subblock_duration == 0) {
615 subblock_duration = ffio_read_leb(pb);
616 total_duration += subblock_duration;
617
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 } else if (i == nb_subblocks - 1)
618 20 subblock_duration = duration - i * constant_subblock_duration;
619
620
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
20 switch (type) {
621 case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
622 AVIAMFMixGain *mix = subblock;
623 mix->subblock_duration = subblock_duration;
624 break;
625 }
626 15 case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
627 15 AVIAMFDemixingInfo *demix = subblock;
628 15 demix->subblock_duration = subblock_duration;
629 // DefaultDemixingInfoParameterData
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_assert0(audio_element);
631 15 demix->dmixp_mode = avio_r8(pb) >> 5;
632 15 audio_element->element->default_w = avio_r8(pb) >> 4;
633 15 break;
634 }
635 5 case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
636 5 AVIAMFReconGain *recon = subblock;
637 5 recon->subblock_duration = subblock_duration;
638 5 break;
639 }
640 default:
641 av_free(param);
642 return AVERROR_INVALIDDATA;
643 }
644 }
645
646
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) {
647 av_log(s, AV_LOG_ERROR, "Invalid subblock durations in parameter_id %u\n", parameter_id);
648 av_free(param);
649 return AVERROR_INVALIDDATA;
650 }
651
652 84 param->parameter_id = parameter_id;
653 84 param->parameter_rate = parameter_rate;
654 84 param->duration = duration;
655 84 param->constant_subblock_duration = constant_subblock_duration;
656 84 param->nb_subblocks = nb_subblocks;
657
658
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 47 times.
84 if (param_definition) {
659
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)) {
660 av_log(s, AV_LOG_ERROR, "Incosistent parameters for parameter_id %u\n", parameter_id);
661 av_free(param);
662 return AVERROR_INVALIDDATA;
663 }
664 } else {
665 47 IAMFParamDefinition **tmp = av_realloc_array(c->param_definitions, c->nb_param_definitions + 1,
666 sizeof(*c->param_definitions));
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!tmp) {
668 av_free(param);
669 return AVERROR(ENOMEM);
670 }
671 47 c->param_definitions = tmp;
672
673 47 param_definition = av_mallocz(sizeof(*param_definition));
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (!param_definition) {
675 av_free(param);
676 return AVERROR(ENOMEM);
677 }
678 47 param_definition->param = param;
679 47 param_definition->mode = !mode;
680 47 param_definition->param_size = param_size;
681 47 param_definition->audio_element = audio_element;
682
683 47 c->param_definitions[c->nb_param_definitions++] = param_definition;
684 }
685
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84 times.
84 av_assert0(out_param_definition);
687 84 *out_param_definition = param;
688
689 84 return 0;
690 }
691
692 29 static int audio_element_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
693 {
694 const IAMFCodecConfig *codec_config;
695 AVIAMFAudioElement *element;
696 29 IAMFAudioElement **tmp, *audio_element = NULL;
697 FFIOContext b;
698 AVIOContext *pbc;
699 uint8_t *buf;
700 unsigned audio_element_id, nb_substreams, codec_config_id, num_parameters;
701 int audio_element_type, ret;
702
703 29 buf = av_malloc(len);
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!buf)
705 return AVERROR(ENOMEM);
706
707 29 ret = ffio_read_size(pb, buf, len);
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
709 goto fail;
710
711 29 ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
712 29 pbc = &b.pub;
713
714 29 audio_element_id = ffio_read_leb(pbc);
715
716
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 for (int i = 0; i < c->nb_audio_elements; i++)
717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (c->audio_elements[i]->audio_element_id == audio_element_id) {
718 av_log(s, AV_LOG_ERROR, "Duplicate audio_element_id %d\n", audio_element_id);
719 ret = AVERROR_INVALIDDATA;
720 goto fail;
721 }
722
723 29 audio_element_type = avio_r8(pbc) >> 5;
724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (audio_element_type > AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
725 av_log(s, AV_LOG_DEBUG, "Unknown audio_element_type referenced in an audio element. Ignoring\n");
726 ret = 0;
727 goto fail;
728 }
729
730 29 codec_config_id = ffio_read_leb(pbc);
731
732 29 codec_config = ff_iamf_get_codec_config(c, codec_config_id);
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!codec_config) {
734 av_log(s, AV_LOG_ERROR, "Non existant codec config id %d referenced in an audio element\n", codec_config_id);
735 ret = AVERROR_INVALIDDATA;
736 goto fail;
737 }
738
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (codec_config->codec_id == AV_CODEC_ID_NONE) {
740 av_log(s, AV_LOG_DEBUG, "Unknown codec id referenced in an audio element. Ignoring\n");
741 ret = 0;
742 goto fail;
743 }
744
745 29 tmp = av_realloc_array(c->audio_elements, c->nb_audio_elements + 1, sizeof(*c->audio_elements));
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!tmp) {
747 ret = AVERROR(ENOMEM);
748 goto fail;
749 }
750 29 c->audio_elements = tmp;
751
752 29 audio_element = av_mallocz(sizeof(*audio_element));
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!audio_element) {
754 ret = AVERROR(ENOMEM);
755 goto fail;
756 }
757
758 29 nb_substreams = ffio_read_leb(pbc);
759 29 audio_element->codec_config_id = codec_config_id;
760 29 audio_element->audio_element_id = audio_element_id;
761 29 audio_element->substreams = av_calloc(nb_substreams, sizeof(*audio_element->substreams));
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!audio_element->substreams) {
763 ret = AVERROR(ENOMEM);
764 goto fail;
765 }
766 29 audio_element->nb_substreams = nb_substreams;
767
768 29 element = audio_element->element = av_iamf_audio_element_alloc();
769
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (!element) {
770 ret = AVERROR(ENOMEM);
771 goto fail;
772 }
773 29 audio_element->celement = element;
774
775 29 element->audio_element_type = audio_element_type;
776
777
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 29 times.
157 for (int i = 0; i < audio_element->nb_substreams; i++) {
778 128 IAMFSubStream *substream = &audio_element->substreams[i];
779
780 128 substream->codecpar = avcodec_parameters_alloc();
781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (!substream->codecpar) {
782 ret = AVERROR(ENOMEM);
783 goto fail;
784 }
785
786 128 substream->audio_substream_id = ffio_read_leb(pbc);
787
788 128 substream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
789 128 substream->codecpar->codec_id = codec_config->codec_id;
790 128 substream->codecpar->frame_size = codec_config->nb_samples;
791 128 substream->codecpar->sample_rate = codec_config->sample_rate;
792 128 substream->codecpar->seek_preroll = -codec_config->audio_roll_distance * codec_config->nb_samples;
793
794
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 switch(substream->codecpar->codec_id) {
795 128 case AV_CODEC_ID_AAC:
796 case AV_CODEC_ID_FLAC:
797 case AV_CODEC_ID_OPUS:
798 128 substream->codecpar->extradata = av_malloc(codec_config->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
128 if (!substream->codecpar->extradata) {
800 ret = AVERROR(ENOMEM);
801 goto fail;
802 }
803 128 memcpy(substream->codecpar->extradata, codec_config->extradata, codec_config->extradata_size);
804 128 memset(substream->codecpar->extradata + codec_config->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
805 128 substream->codecpar->extradata_size = codec_config->extradata_size;
806 128 break;
807 }
808 }
809
810 29 num_parameters = ffio_read_leb(pbc);
811
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) {
812 av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
813 " for Channel representations\n", num_parameters);
814 ret = AVERROR_INVALIDDATA;
815 goto fail;
816 }
817
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) {
818 av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
819 " for Scene representations\n", num_parameters);
820 ret = AVERROR_INVALIDDATA;
821 goto fail;
822 }
823
824
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 29 times.
49 for (int i = 0; i < num_parameters; i++) {
825 unsigned type;
826
827 20 type = ffio_read_leb(pbc);
828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (type == AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN)
829 ret = AVERROR_INVALIDDATA;
830
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 else if (type == AV_IAMF_PARAMETER_DEFINITION_DEMIXING) {
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (element->demixing_info) {
832 ret = AVERROR_INVALIDDATA;
833 goto fail;
834 }
835 15 ret = param_parse(s, c, pbc, type, audio_element, &element->demixing_info);
836
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (type == AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (element->recon_gain_info) {
838 ret = AVERROR_INVALIDDATA;
839 goto fail;
840 }
841 5 ret = param_parse(s, c, pbc, type, audio_element, &element->recon_gain_info);
842 } else {
843 unsigned param_definition_size = ffio_read_leb(pbc);
844 avio_skip(pbc, param_definition_size);
845 }
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
847 goto fail;
848 }
849
850
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 4 times.
29 if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL) {
851 25 ret = scalable_channel_layout_config(s, pbc, audio_element, codec_config);
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
853 goto fail;
854
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
855 4 ret = ambisonics_config(s, pbc, audio_element, codec_config);
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
857 goto fail;
858 } else {
859 av_assert0(0);
860 }
861
862 29 c->audio_elements[c->nb_audio_elements++] = audio_element;
863
864 29 len -= avio_tell(pbc);
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (len)
866 av_log(s, AV_LOG_WARNING, "Underread in audio_element_obu. %d bytes left at the end\n", len);
867
868 29 ret = 0;
869 29 fail:
870 29 av_free(buf);
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (ret < 0)
872 ff_iamf_free_audio_element(&audio_element);
873 29 return ret;
874 }
875
876 87 static int label_string(AVIOContext *pb, char **label)
877 {
878 uint8_t buf[128];
879
880 87 avio_get_str(pb, sizeof(buf), buf, sizeof(buf));
881
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (pb->error)
883 return pb->error;
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (pb->eof_reached)
885 return AVERROR_INVALIDDATA;
886 87 *label = av_strdup(buf);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 87 times.
87 if (!*label)
888 return AVERROR(ENOMEM);
889
890 87 return 0;
891 }
892
893 27 static int mix_presentation_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
894 {
895 AVIAMFMixPresentation *mix;
896 27 IAMFMixPresentation **tmp, *mix_presentation = NULL;
897 FFIOContext b;
898 AVIOContext *pbc;
899 uint8_t *buf;
900 unsigned nb_submixes, mix_presentation_id;
901 int ret;
902
903 27 buf = av_malloc(len);
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!buf)
905 return AVERROR(ENOMEM);
906
907 27 ret = ffio_read_size(pb, buf, len);
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
909 goto fail;
910
911 27 ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
912 27 pbc = &b.pub;
913
914 27 mix_presentation_id = ffio_read_leb(pbc);
915
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 for (int i = 0; i < c->nb_mix_presentations; i++)
917 if (c->mix_presentations[i]->mix_presentation_id == mix_presentation_id) {
918 av_log(s, AV_LOG_ERROR, "Duplicate mix_presentation_id %d\n", mix_presentation_id);
919 ret = AVERROR_INVALIDDATA;
920 goto fail;
921 }
922
923 27 tmp = av_realloc_array(c->mix_presentations, c->nb_mix_presentations + 1, sizeof(*c->mix_presentations));
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!tmp) {
925 ret = AVERROR(ENOMEM);
926 goto fail;
927 }
928 27 c->mix_presentations = tmp;
929
930 27 mix_presentation = av_mallocz(sizeof(*mix_presentation));
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix_presentation) {
932 ret = AVERROR(ENOMEM);
933 goto fail;
934 }
935
936 27 mix_presentation->mix_presentation_id = mix_presentation_id;
937 27 mix = mix_presentation->mix = av_iamf_mix_presentation_alloc();
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix) {
939 ret = AVERROR(ENOMEM);
940 goto fail;
941 }
942 27 mix_presentation->cmix = mix;
943
944 27 mix_presentation->count_label = ffio_read_leb(pbc);
945 27 mix_presentation->language_label = av_calloc(mix_presentation->count_label,
946 sizeof(*mix_presentation->language_label));
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (!mix_presentation->language_label) {
948 mix_presentation->count_label = 0;
949 ret = AVERROR(ENOMEM);
950 goto fail;
951 }
952
953
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < mix_presentation->count_label; i++) {
954 27 ret = label_string(pbc, &mix_presentation->language_label[i]);
955
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
956 goto fail;
957 }
958
959
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 for (int i = 0; i < mix_presentation->count_label; i++) {
960 27 char *annotation = NULL;
961 27 ret = label_string(pbc, &annotation);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
963 goto fail;
964 27 ret = av_dict_set(&mix->annotations, mix_presentation->language_label[i], annotation,
965 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
967 goto fail;
968 }
969
970 27 nb_submixes = ffio_read_leb(pbc);
971
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 27 times.
58 for (int i = 0; i < nb_submixes; i++) {
972 AVIAMFSubmix *sub_mix;
973 unsigned nb_elements, nb_layouts;
974
975 31 sub_mix = av_iamf_mix_presentation_add_submix(mix);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (!sub_mix) {
977 ret = AVERROR(ENOMEM);
978 goto fail;
979 }
980
981 31 nb_elements = ffio_read_leb(pbc);
982
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 31 times.
64 for (int j = 0; j < nb_elements; j++) {
983 AVIAMFSubmixElement *submix_element;
984 33 IAMFAudioElement *audio_element = NULL;
985 unsigned int rendering_config_extension_size;
986
987 33 submix_element = av_iamf_submix_add_element(sub_mix);
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!submix_element) {
989 ret = AVERROR(ENOMEM);
990 goto fail;
991 }
992
993 33 submix_element->audio_element_id = ffio_read_leb(pbc);
994
995
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 for (int k = 0; k < c->nb_audio_elements; k++)
996
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) {
997 33 audio_element = c->audio_elements[k];
998 33 break;
999 }
1000
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (!audio_element) {
1002 av_log(s, AV_LOG_ERROR, "Invalid Audio Element with id %u referenced by Mix Parameters %u\n",
1003 submix_element->audio_element_id, mix_presentation_id);
1004 ret = AVERROR_INVALIDDATA;
1005 goto fail;
1006 }
1007
1008
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 for (int k = 0; k < mix_presentation->count_label; k++) {
1009 33 char *annotation = NULL;
1010 33 ret = label_string(pbc, &annotation);
1011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
1012 goto fail;
1013 33 ret = av_dict_set(&submix_element->annotations, mix_presentation->language_label[k], annotation,
1014 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
1016 goto fail;
1017 }
1018
1019 33 submix_element->headphones_rendering_mode = avio_r8(pbc) >> 6;
1020
1021 33 rendering_config_extension_size = ffio_read_leb(pbc);
1022 33 avio_skip(pbc, rendering_config_extension_size);
1023
1024 33 ret = param_parse(s, c, pbc, AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN,
1025 NULL,
1026 &submix_element->element_mix_config);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (ret < 0)
1028 goto fail;
1029 33 submix_element->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1030 }
1031
1032 31 ret = param_parse(s, c, pbc, AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN, NULL, &sub_mix->output_mix_config);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
1034 goto fail;
1035 31 sub_mix->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1036
1037 31 nb_layouts = ffio_read_leb(pbc);
1038
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 31 times.
99 for (int j = 0; j < nb_layouts; j++) {
1039 AVIAMFSubmixLayout *submix_layout;
1040 int info_type;
1041 68 int byte = avio_r8(pbc);
1042
1043 68 submix_layout = av_iamf_submix_add_layout(sub_mix);
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (!submix_layout) {
1045 ret = AVERROR(ENOMEM);
1046 goto fail;
1047 }
1048
1049 68 submix_layout->layout_type = byte >> 6;
1050
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (submix_layout->layout_type < AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS ||
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 submix_layout->layout_type > AV_IAMF_SUBMIX_LAYOUT_TYPE_BINAURAL) {
1052 av_log(s, AV_LOG_ERROR, "Invalid Layout type %u in a submix from Mix Presentation %u\n",
1053 submix_layout->layout_type, mix_presentation_id);
1054 ret = AVERROR_INVALIDDATA;
1055 goto fail;
1056 }
1057
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
68 if (submix_layout->layout_type == 2) {
1058 int sound_system;
1059 64 sound_system = (byte >> 2) & 0xF;
1060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (sound_system >= FF_ARRAY_ELEMS(ff_iamf_sound_system_map)) {
1061 ret = AVERROR_INVALIDDATA;
1062 goto fail;
1063 }
1064 64 av_channel_layout_copy(&submix_layout->sound_system, &ff_iamf_sound_system_map[sound_system].layout);
1065 } else
1066 4 submix_layout->sound_system = (AVChannelLayout)AV_CHANNEL_LAYOUT_BINAURAL;
1067
1068 68 info_type = avio_r8(pbc);
1069 68 submix_layout->integrated_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1070 68 submix_layout->digital_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1071
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 1)
1073 submix_layout->true_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 2) {
1075 unsigned int num_anchored_loudness = avio_r8(pbc);
1076
1077 for (int k = 0; k < num_anchored_loudness; k++) {
1078 unsigned int anchor_element = avio_r8(pbc);
1079 AVRational anchored_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1080 if (anchor_element == IAMF_ANCHOR_ELEMENT_DIALOGUE)
1081 submix_layout->dialogue_anchored_loudness = anchored_loudness;
1082 else if (anchor_element <= IAMF_ANCHOR_ELEMENT_ALBUM)
1083 submix_layout->album_anchored_loudness = anchored_loudness;
1084 else
1085 av_log(s, AV_LOG_DEBUG, "Unknown anchor_element. Ignoring\n");
1086 }
1087 }
1088
1089
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (info_type & 0xFC) {
1090 unsigned int info_type_size = ffio_read_leb(pbc);
1091 avio_skip(pbc, info_type_size);
1092 }
1093 }
1094 }
1095
1096 27 c->mix_presentations[c->nb_mix_presentations++] = mix_presentation;
1097
1098 27 len -= avio_tell(pbc);
1099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (len)
1100 av_log(s, AV_LOG_WARNING, "Underread in mix_presentation_obu. %d bytes left at the end\n", len);
1101
1102 27 ret = 0;
1103 27 fail:
1104 27 av_free(buf);
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
1106 ff_iamf_free_mix_presentation(&mix_presentation);
1107 27 return ret;
1108 }
1109
1110 8217 int ff_iamf_parse_obu_header(const uint8_t *buf, int buf_size,
1111 unsigned *obu_size, int *start_pos, enum IAMF_OBU_Type *type,
1112 unsigned *skip_samples, unsigned *discard_padding)
1113 {
1114 GetBitContext gb;
1115 int ret, extension_flag, trimming, start;
1116 8217 unsigned skip = 0, discard = 0;
1117 unsigned size;
1118
1119 8217 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_IAMF_OBU_HEADER_SIZE));
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8217 times.
8217 if (ret < 0)
1121 return ret;
1122
1123 8217 *type = get_bits(&gb, 5);
1124 8217 /*redundant =*/ get_bits1(&gb);
1125 8217 trimming = get_bits1(&gb);
1126 8217 extension_flag = get_bits1(&gb);
1127
1128 8217 *obu_size = get_leb(&gb);
1129
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8208 times.
8217 if (*obu_size > INT_MAX)
1130 9 return AVERROR_INVALIDDATA;
1131
1132 8208 start = get_bits_count(&gb) / 8;
1133
1134
2/2
✓ Branch 0 taken 2343 times.
✓ Branch 1 taken 5865 times.
8208 if (trimming) {
1135 2343 discard = get_leb(&gb); // num_samples_to_trim_at_end
1136 2343 skip = get_leb(&gb); // num_samples_to_trim_at_start
1137 }
1138
1139
2/2
✓ Branch 0 taken 789 times.
✓ Branch 1 taken 7419 times.
8208 if (skip_samples)
1140 789 *skip_samples = skip;
1141
2/2
✓ Branch 0 taken 789 times.
✓ Branch 1 taken 7419 times.
8208 if (discard_padding)
1142 789 *discard_padding = discard;
1143
1144
2/2
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 7475 times.
8208 if (extension_flag) {
1145 unsigned int extension_bytes;
1146 733 extension_bytes = get_leb(&gb);
1147
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 715 times.
733 if (extension_bytes > INT_MAX / 8)
1148 18 return AVERROR_INVALIDDATA;
1149 715 skip_bits_long(&gb, extension_bytes * 8);
1150 }
1151
1152
2/2
✓ Branch 1 taken 531 times.
✓ Branch 2 taken 7659 times.
8190 if (get_bits_left(&gb) < 0)
1153 531 return AVERROR_INVALIDDATA;
1154
1155 7659 size = *obu_size + start;
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7659 times.
7659 if (size > INT_MAX)
1157 return AVERROR_INVALIDDATA;
1158
1159 7659 *obu_size -= get_bits_count(&gb) / 8 - start;
1160 7659 *start_pos = size - *obu_size;
1161
1162 7659 return size;
1163 }
1164
1165 27 int ff_iamfdec_read_descriptors(IAMFContext *c, AVIOContext *pb,
1166 int max_size, void *log_ctx)
1167 {
1168 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
1169 int ret;
1170
1171 100 while (1) {
1172 unsigned obu_size;
1173 enum IAMF_OBU_Type type;
1174 int start_pos, len, size;
1175
1176
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)
1177 return ret;
1178 127 size = avio_read(pb, header, FFMIN(MAX_IAMF_OBU_HEADER_SIZE, max_size));
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (size < 0)
1180 return size;
1181 127 memset(header + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1182
1183 127 len = ff_iamf_parse_obu_header(header, size, &obu_size, &start_pos, &type, NULL, NULL);
1184
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) {
1185 av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu header\n");
1186 avio_seek(pb, -size, SEEK_CUR);
1187 return len;
1188 }
1189
1190
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) {
1191 17 avio_seek(pb, -size, SEEK_CUR);
1192 17 break;
1193 }
1194
1195 110 avio_seek(pb, -(size - start_pos), SEEK_CUR);
1196
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) {
1197 27 case IAMF_OBU_IA_CODEC_CONFIG:
1198 27 ret = codec_config_obu(log_ctx, c, pb, obu_size);
1199 27 break;
1200 29 case IAMF_OBU_IA_AUDIO_ELEMENT:
1201 29 ret = audio_element_obu(log_ctx, c, pb, obu_size);
1202 29 break;
1203 27 case IAMF_OBU_IA_MIX_PRESENTATION:
1204 27 ret = mix_presentation_obu(log_ctx, c, pb, obu_size);
1205 27 break;
1206 27 default: {
1207 27 int64_t offset = avio_skip(pb, obu_size);
1208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (offset < 0)
1209 ret = offset;
1210 27 break;
1211 }
1212 }
1213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (ret < 0) {
1214 av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu type %d\n", type);
1215 return ret;
1216 }
1217 110 max_size -= obu_size + start_pos;
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (max_size < 0)
1219 return AVERROR_INVALIDDATA;
1220
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 100 times.
110 if (!max_size)
1221 10 break;
1222 }
1223
1224 27 return 0;
1225 }
1226