FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamf_writer.c
Date: 2026-04-23 02:20:26
Exec Total Coverage
Lines: 581 828 70.2%
Functions: 19 19 100.0%
Branches: 291 465 62.6%

Line Branch Exec Source
1 /*
2 * Immersive Audio Model and Formats muxing helpers and structs
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/bprint.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/iamf.h"
26 #include "libavutil/mem.h"
27 #include "libavcodec/get_bits.h"
28 #include "libavcodec/put_bits.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "iamf.h"
32 #include "iamf_writer.h"
33
34
35 71 static int update_extradata(IAMFCodecConfig *codec_config)
36 {
37 GetBitContext gb;
38 PutBitContext pb;
39 int ret;
40
41
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
71 switch(codec_config->codec_id) {
42 2 case AV_CODEC_ID_OPUS:
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (codec_config->extradata_size != 19)
44 return AVERROR_INVALIDDATA;
45 2 codec_config->extradata_size -= 8;
46 2 AV_WB8(codec_config->extradata + 0, AV_RL8(codec_config->extradata + 8)); // version
47 2 AV_WB8(codec_config->extradata + 1, 2); // set channels to stereo
48 2 AV_WB16A(codec_config->extradata + 2, AV_RL16A(codec_config->extradata + 10)); // Byte swap pre-skip
49 2 AV_WB32A(codec_config->extradata + 4, AV_RL32A(codec_config->extradata + 12)); // Byte swap sample rate
50 2 AV_WB16A(codec_config->extradata + 8, 0); // set Output Gain to 0
51 2 AV_WB8(codec_config->extradata + 10, AV_RL8(codec_config->extradata + 18)); // Mapping family
52 2 break;
53 69 case AV_CODEC_ID_FLAC: {
54 uint8_t buf[13];
55
56 69 init_put_bits(&pb, buf, sizeof(buf));
57 69 ret = init_get_bits8(&gb, codec_config->extradata, codec_config->extradata_size);
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 if (ret < 0)
59 return ret;
60
61 69 put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
62 69 put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
63 69 put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
64 69 skip_bits(&gb, 3);
65 69 put_bits(&pb, 3, 1); // set channels to stereo
66 69 ret = put_bits_left(&pb);
67 69 put_bits(&pb, ret, get_bits(&gb, ret));
68 69 flush_put_bits(&pb);
69
70 69 memcpy(codec_config->extradata, buf, sizeof(buf));
71 69 break;
72 }
73 default:
74 break;
75 }
76
77 71 return 0;
78 }
79
80 14 static int populate_audio_roll_distance(IAMFCodecConfig *codec_config)
81 {
82
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
14 switch (codec_config->codec_id) {
83 2 case AV_CODEC_ID_OPUS:
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!codec_config->nb_samples)
85 return AVERROR(EINVAL);
86 // ceil(3840 / nb_samples)
87 2 codec_config->audio_roll_distance = -(1 + ((3840 - 1) / codec_config->nb_samples));
88 2 break;
89 case AV_CODEC_ID_AAC:
90 codec_config->audio_roll_distance = -1;
91 break;
92 12 case AV_CODEC_ID_FLAC:
93 case AV_CODEC_ID_PCM_S16BE:
94 case AV_CODEC_ID_PCM_S24BE:
95 case AV_CODEC_ID_PCM_S32BE:
96 case AV_CODEC_ID_PCM_S16LE:
97 case AV_CODEC_ID_PCM_S24LE:
98 case AV_CODEC_ID_PCM_S32LE:
99 12 codec_config->audio_roll_distance = 0;
100 12 break;
101 default:
102 return AVERROR(EINVAL);
103 }
104
105 14 return 0;
106 }
107
108 14 static int fill_codec_config(IAMFContext *iamf, const AVStreamGroup *stg,
109 IAMFCodecConfig *codec_config)
110 {
111 14 const AVStream *st = stg->streams[0];
112 IAMFCodecConfig **tmp;
113 14 int j, ret = 0;
114
115 14 codec_config->codec_id = st->codecpar->codec_id;
116 14 codec_config->codec_tag = st->codecpar->codec_tag;
117
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 switch (codec_config->codec_id) {
118 2 case AV_CODEC_ID_OPUS:
119 2 codec_config->sample_rate = 48000;
120 2 codec_config->nb_samples = av_rescale(st->codecpar->frame_size, 48000, st->codecpar->sample_rate);
121 2 break;
122 12 default:
123 12 codec_config->sample_rate = st->codecpar->sample_rate;
124 12 codec_config->nb_samples = st->codecpar->frame_size;
125 12 break;
126 }
127 14 populate_audio_roll_distance(codec_config);
128
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (st->codecpar->extradata_size) {
129 14 codec_config->extradata = av_memdup(st->codecpar->extradata, st->codecpar->extradata_size);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!codec_config->extradata)
131 return AVERROR(ENOMEM);
132 14 codec_config->extradata_size = st->codecpar->extradata_size;
133 14 ret = update_extradata(codec_config);
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
135 goto fail;
136 }
137
138
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 for (j = 0; j < iamf->nb_codec_configs; j++) {
139
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!memcmp(iamf->codec_configs[j], codec_config, offsetof(IAMFCodecConfig, extradata)) &&
140
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (!codec_config->extradata_size || !memcmp(iamf->codec_configs[j]->extradata,
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 codec_config->extradata, codec_config->extradata_size)))
142 break;
143 }
144
145
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (j < iamf->nb_codec_configs) {
146 1 av_free(iamf->codec_configs[j]->extradata);
147 1 av_free(iamf->codec_configs[j]);
148 1 iamf->codec_configs[j] = codec_config;
149 1 return j;
150 }
151
152 13 tmp = av_realloc_array(iamf->codec_configs, iamf->nb_codec_configs + 1, sizeof(*iamf->codec_configs));
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!tmp) {
154 ret = AVERROR(ENOMEM);
155 goto fail;
156 }
157
158 13 iamf->codec_configs = tmp;
159 13 iamf->codec_configs[iamf->nb_codec_configs] = codec_config;
160 13 codec_config->codec_config_id = iamf->nb_codec_configs;
161
162 13 return iamf->nb_codec_configs++;
163
164 fail:
165 av_freep(&codec_config->extradata);
166 return ret;
167 }
168
169 36 static int add_param_definition(IAMFContext *iamf, AVIAMFParamDefinition *param,
170 const IAMFAudioElement *audio_element, void *log_ctx)
171 {
172 IAMFParamDefinition **tmp, *param_definition;
173 36 IAMFCodecConfig *codec_config = NULL;
174
175 36 tmp = av_realloc_array(iamf->param_definitions, iamf->nb_param_definitions + 1,
176 sizeof(*iamf->param_definitions));
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!tmp)
178 return AVERROR(ENOMEM);
179
180 36 iamf->param_definitions = tmp;
181
182
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 14 times.
36 if (audio_element)
183 22 codec_config = iamf->codec_configs[audio_element->codec_config_id];
184
185
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16 times.
36 if (!param->parameter_rate) {
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!codec_config) {
187 av_log(log_ctx, AV_LOG_ERROR, "parameter_rate needed but not set for parameter_id %u\n",
188 param->parameter_id);
189 return AVERROR(EINVAL);
190 }
191 20 param->parameter_rate = codec_config->sample_rate;
192 }
193
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 14 times.
36 if (codec_config) {
194
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
22 if (!param->duration)
195 20 param->duration = av_rescale(codec_config->nb_samples, param->parameter_rate, codec_config->sample_rate);
196
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
22 if (!param->constant_subblock_duration)
197 20 param->constant_subblock_duration = av_rescale(codec_config->nb_samples, param->parameter_rate, codec_config->sample_rate);
198 }
199
200 36 param_definition = av_mallocz(sizeof(*param_definition));
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!param_definition)
202 return AVERROR(ENOMEM);
203
204 36 param_definition->mode = !!param->duration;
205 36 param_definition->param = param;
206 36 param_definition->audio_element = audio_element;
207 36 iamf->param_definitions[iamf->nb_param_definitions++] = param_definition;
208
209 36 return 0;
210 }
211
212 14 int ff_iamf_add_audio_element(IAMFContext *iamf, const AVStreamGroup *stg, void *log_ctx)
213 {
214 const AVIAMFAudioElement *iamf_audio_element;
215 IAMFAudioElement **tmp, *audio_element;
216 IAMFCodecConfig *codec_config;
217 int ret;
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
220 return AVERROR(EINVAL);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!stg->nb_streams) {
222 av_log(log_ctx, AV_LOG_ERROR, "Audio Element id %"PRId64" has no streams\n", stg->id);
223 return AVERROR(EINVAL);
224 }
225
226 14 iamf_audio_element = stg->params.iamf_audio_element;
227
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 if (iamf_audio_element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
228 4 const AVIAMFLayer *layer = iamf_audio_element->layers[0];
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (iamf_audio_element->nb_layers != 1) {
230 av_log(log_ctx, AV_LOG_ERROR, "Invalid amount of layers for SCENE_BASED audio element. Must be 1\n");
231 return AVERROR(EINVAL);
232 }
233
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (layer->ch_layout.order != AV_CHANNEL_ORDER_CUSTOM &&
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 layer->ch_layout.order != AV_CHANNEL_ORDER_AMBISONIC) {
235 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout for SCENE_BASED audio element\n");
236 return AVERROR(EINVAL);
237 }
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (layer->ambisonics_mode > AV_IAMF_AMBISONICS_MODE_PROJECTION) {
239 av_log(log_ctx, AV_LOG_ERROR, "Unsupported ambisonics mode %d\n", layer->ambisonics_mode);
240 return AVERROR_PATCHWELCOME;
241 }
242
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < stg->nb_streams; i++) {
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (stg->streams[i]->codecpar->ch_layout.nb_channels > 1) {
244 av_log(log_ctx, AV_LOG_ERROR, "Invalid amount of channels in a stream for MONO mode ambisonics\n");
245 return AVERROR(EINVAL);
246 }
247 }
248 } else {
249 AVBPrint bp;
250
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (iamf_audio_element->nb_layers < 1) {
252 av_log(log_ctx, AV_LOG_ERROR, "Invalid amount of layers for CHANNEL_BASED audio element. Must be >= 1\n");
253 return AVERROR(EINVAL);
254 }
255
256
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 10 times.
36 for (int j, i = 0; i < iamf_audio_element->nb_layers; i++) {
257 26 const AVIAMFLayer *layer = iamf_audio_element->layers[i];
258
259
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1 times.
128 for (j = 0; j < FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts); j++)
260
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 102 times.
254 if (av_channel_layout_subset(&layer->ch_layout, UINT64_MAX) ==
261 127 av_channel_layout_subset(&ff_iamf_scalable_ch_layouts[j], UINT64_MAX))
262 25 break;
263
264
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 25 times.
26 if (j >= FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts)) {
265
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 for (j = 0; j < FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts); j++)
266
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8 times.
18 if (av_channel_layout_subset(&layer->ch_layout, UINT64_MAX) ==
267 9 av_channel_layout_subset(&ff_iamf_expanded_scalable_ch_layouts[j], UINT64_MAX))
268 1 break;
269
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (j >= FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts)) {
271 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
272 av_channel_layout_describe_bprint(&layer->ch_layout, &bp);
273 av_log(log_ctx, AV_LOG_ERROR, "Unsupported channel layout in Audio Element id %"PRId64
274 ", Layer %d: %s\n",
275 stg->id, i, bp.str);
276 av_bprint_finalize(&bp, NULL);
277 return AVERROR(EINVAL);
278 }
279 }
280
281
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 16 times.
26 if (!i)
282 10 continue;
283
284 16 const AVIAMFLayer *prev_layer = iamf_audio_element->layers[i-1];
285 16 uint64_t prev_mask = av_channel_layout_subset(&prev_layer->ch_layout, UINT64_MAX);
286
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 if (av_channel_layout_subset(&layer->ch_layout, prev_mask) != prev_mask || (layer->ch_layout.nb_channels <=
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 prev_layer->ch_layout.nb_channels)) {
288 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
289 av_bprintf(&bp, "Channel layout \"");
290 av_channel_layout_describe_bprint(&layer->ch_layout, &bp);
291 av_bprintf(&bp, "\" can't follow channel layout \"");
292 av_channel_layout_describe_bprint(&prev_layer->ch_layout, &bp);
293 av_bprintf(&bp, "\" in Scalable Audio Element id %"PRId64, stg->id);
294 av_log(log_ctx, AV_LOG_ERROR, "%s\n", bp.str);
295 av_bprint_finalize(&bp, NULL);
296 return AVERROR(EINVAL);
297 }
298 }
299 }
300
301
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 for (int i = 0; i < iamf->nb_audio_elements; i++) {
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (stg->id == iamf->audio_elements[i]->audio_element_id) {
303 av_log(log_ctx, AV_LOG_ERROR, "Duplicated Audio Element id %"PRId64"\n", stg->id);
304 return AVERROR(EINVAL);
305 }
306 }
307
308 14 codec_config = av_mallocz(sizeof(*codec_config));
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!codec_config)
310 return AVERROR(ENOMEM);
311
312 14 ret = fill_codec_config(iamf, stg, codec_config);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0) {
314 av_free(codec_config);
315 return ret;
316 }
317
318 14 audio_element = av_mallocz(sizeof(*audio_element));
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!audio_element)
320 return AVERROR(ENOMEM);
321
322 14 audio_element->celement = stg->params.iamf_audio_element;
323 14 audio_element->audio_element_id = stg->id;
324 14 audio_element->codec_config_id = ret;
325
326 14 audio_element->substreams = av_calloc(stg->nb_streams, sizeof(*audio_element->substreams));
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!audio_element->substreams) {
328 ret = AVERROR(ENOMEM);
329 goto fail;
330 }
331 14 audio_element->nb_substreams = stg->nb_streams;
332
333 14 audio_element->layers = av_calloc(iamf_audio_element->nb_layers, sizeof(*audio_element->layers));
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!audio_element->layers) {
335 ret = AVERROR(ENOMEM);
336 goto fail;
337 }
338
339
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 14 times.
44 for (int i = 0, j = 0; i < iamf_audio_element->nb_layers; i++) {
340 30 int nb_channels = iamf_audio_element->layers[i]->ch_layout.nb_channels;
341
342 30 IAMFLayer *layer = &audio_element->layers[i];
343
344
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 14 times.
30 if (i)
345 16 nb_channels -= iamf_audio_element->layers[i - 1]->ch_layout.nb_channels;
346
3/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
95 for (; nb_channels > 0 && j < stg->nb_streams; j++) {
347 65 const AVStream *st = stg->streams[j];
348 65 IAMFSubStream *substream = &audio_element->substreams[j];
349
350 65 substream->audio_substream_id = st->id;
351 65 layer->substream_count++;
352 65 layer->coupled_substream_count += st->codecpar->ch_layout.nb_channels == 2;
353 65 nb_channels -= st->codecpar->ch_layout.nb_channels;
354 }
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (nb_channels) {
356 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel count across substreams in layer %u from stream group %u\n",
357 i, stg->index);
358 ret = AVERROR(EINVAL);
359 goto fail;
360 }
361 }
362
363
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 14 times.
79 for (int i = 0; i < audio_element->nb_substreams; i++) {
364
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 65 times.
224 for (int j = i + 1; j < audio_element->nb_substreams; j++)
365 159 if (audio_element->substreams[i].audio_substream_id ==
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 159 times.
159 audio_element->substreams[j].audio_substream_id) {
367 av_log(log_ctx, AV_LOG_ERROR, "Duplicate id %u in streams %u and %u from stream group %u\n",
368 audio_element->substreams[i].audio_substream_id, i, j, stg->index);
369 ret = AVERROR(EINVAL);
370 goto fail;
371 }
372 }
373
374
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (iamf_audio_element->demixing_info) {
375 14 AVIAMFParamDefinition *param = iamf_audio_element->demixing_info;
376 14 const IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
377
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (param->nb_subblocks != 1) {
379 av_log(log_ctx, AV_LOG_ERROR, "nb_subblocks in demixing_info for stream group %u is not 1\n", stg->index);
380 ret = AVERROR(EINVAL);
381 goto fail;
382 }
383
384
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 if (!param_definition) {
385 13 ret = add_param_definition(iamf, param, audio_element, log_ctx);
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
387 goto fail;
388 }
389 }
390
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (iamf_audio_element->recon_gain_info) {
391 14 AVIAMFParamDefinition *param = iamf_audio_element->recon_gain_info;
392 14 const IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
393
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (param->nb_subblocks != 1) {
395 av_log(log_ctx, AV_LOG_ERROR, "nb_subblocks in recon_gain_info for stream group %u is not 1\n", stg->index);
396 ret = AVERROR(EINVAL);
397 goto fail;
398 }
399
400
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 5 times.
14 if (!param_definition) {
401 9 ret = add_param_definition(iamf, param, audio_element, log_ctx);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
403 goto fail;
404 }
405 }
406
407 14 tmp = av_realloc_array(iamf->audio_elements, iamf->nb_audio_elements + 1, sizeof(*iamf->audio_elements));
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!tmp) {
409 ret = AVERROR(ENOMEM);
410 goto fail;
411 }
412
413 14 iamf->audio_elements = tmp;
414 14 iamf->audio_elements[iamf->nb_audio_elements++] = audio_element;
415
416 14 return 0;
417 fail:
418 ff_iamf_free_audio_element(&audio_element);
419 return ret;
420 }
421
422 13 int ff_iamf_add_mix_presentation(IAMFContext *iamf, const AVStreamGroup *stg, void *log_ctx)
423 {
424 IAMFMixPresentation **tmp, *mix_presentation;
425 int ret;
426
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
428 return AVERROR(EINVAL);
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!stg->nb_streams) {
430 av_log(log_ctx, AV_LOG_ERROR, "Mix Presentation id %"PRId64" has no streams\n", stg->id);
431 return AVERROR(EINVAL);
432 }
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 for (int i = 0; i < iamf->nb_mix_presentations; i++) {
435 if (stg->id == iamf->mix_presentations[i]->mix_presentation_id) {
436 av_log(log_ctx, AV_LOG_ERROR, "Duplicate Mix Presentation id %"PRId64"\n", stg->id);
437 return AVERROR(EINVAL);
438 }
439 }
440
441 13 mix_presentation = av_mallocz(sizeof(*mix_presentation));
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!mix_presentation)
443 return AVERROR(ENOMEM);
444
445 13 mix_presentation->cmix = stg->params.iamf_mix_presentation;
446 13 mix_presentation->mix_presentation_id = stg->id;
447
448
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 13 times.
28 for (int i = 0; i < mix_presentation->cmix->nb_submixes; i++) {
449 15 const AVIAMFSubmix *submix = mix_presentation->cmix->submixes[i];
450 15 AVIAMFParamDefinition *param = submix->output_mix_config;
451 IAMFParamDefinition *param_definition;
452
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!param) {
454 av_log(log_ctx, AV_LOG_ERROR, "output_mix_config is not present in submix %u from "
455 "Mix Presentation ID %"PRId64"\n", i, stg->id);
456 ret = AVERROR(EINVAL);
457 goto fail;
458 }
459
460 15 param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
461
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 if (!param_definition) {
462 13 ret = add_param_definition(iamf, param, NULL, log_ctx);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
464 goto fail;
465 }
466
467
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 15 times.
31 for (int j = 0; j < submix->nb_elements; j++) {
468 16 const AVIAMFSubmixElement *element = submix->elements[j];
469 16 param = element->element_mix_config;
470
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (!param) {
472 av_log(log_ctx, AV_LOG_ERROR, "element_mix_config is not present for element %u in submix %u from "
473 "Mix Presentation ID %"PRId64"\n", j, i, stg->id);
474 ret = AVERROR(EINVAL);
475 goto fail;
476 }
477 16 param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
478
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (!param_definition) {
479 1 ret = add_param_definition(iamf, param, NULL, log_ctx);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
481 goto fail;
482 }
483 }
484 }
485
486 13 tmp = av_realloc_array(iamf->mix_presentations, iamf->nb_mix_presentations + 1, sizeof(*iamf->mix_presentations));
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!tmp) {
488 ret = AVERROR(ENOMEM);
489 goto fail;
490 }
491
492 13 iamf->mix_presentations = tmp;
493 13 iamf->mix_presentations[iamf->nb_mix_presentations++] = mix_presentation;
494
495 13 return 0;
496 fail:
497 ff_iamf_free_mix_presentation(&mix_presentation);
498 return ret;
499 }
500
501 19 static int iamf_write_codec_config(const IAMFContext *iamf,
502 const IAMFCodecConfig *codec_config,
503 AVIOContext *pb)
504 {
505 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
506 AVIOContext *dyn_bc;
507 19 uint8_t *dyn_buf = NULL;
508 PutBitContext pbc;
509 int dyn_size;
510
511 19 int ret = avio_open_dyn_buf(&dyn_bc);
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
513 return ret;
514
515 19 ffio_write_leb(dyn_bc, codec_config->codec_config_id);
516 19 avio_wl32(dyn_bc, codec_config->codec_tag);
517
518 19 ffio_write_leb(dyn_bc, codec_config->nb_samples);
519 19 avio_wb16(dyn_bc, codec_config->audio_roll_distance);
520
521
2/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
19 switch(codec_config->codec_id) {
522 2 case AV_CODEC_ID_OPUS:
523 2 avio_write(dyn_bc, codec_config->extradata, codec_config->extradata_size);
524 2 break;
525 case AV_CODEC_ID_AAC:
526 return AVERROR_PATCHWELCOME;
527 17 case AV_CODEC_ID_FLAC:
528 17 avio_w8(dyn_bc, 0x80);
529 17 avio_wb24(dyn_bc, codec_config->extradata_size);
530 17 avio_write(dyn_bc, codec_config->extradata, codec_config->extradata_size);
531 17 break;
532 case AV_CODEC_ID_PCM_S16LE:
533 avio_w8(dyn_bc, 1);
534 avio_w8(dyn_bc, 16);
535 avio_wb32(dyn_bc, codec_config->sample_rate);
536 break;
537 case AV_CODEC_ID_PCM_S24LE:
538 avio_w8(dyn_bc, 1);
539 avio_w8(dyn_bc, 24);
540 avio_wb32(dyn_bc, codec_config->sample_rate);
541 break;
542 case AV_CODEC_ID_PCM_S32LE:
543 avio_w8(dyn_bc, 1);
544 avio_w8(dyn_bc, 32);
545 avio_wb32(dyn_bc, codec_config->sample_rate);
546 break;
547 case AV_CODEC_ID_PCM_S16BE:
548 avio_w8(dyn_bc, 0);
549 avio_w8(dyn_bc, 16);
550 avio_wb32(dyn_bc, codec_config->sample_rate);
551 break;
552 case AV_CODEC_ID_PCM_S24BE:
553 avio_w8(dyn_bc, 0);
554 avio_w8(dyn_bc, 24);
555 avio_wb32(dyn_bc, codec_config->sample_rate);
556 break;
557 case AV_CODEC_ID_PCM_S32BE:
558 avio_w8(dyn_bc, 0);
559 avio_w8(dyn_bc, 32);
560 avio_wb32(dyn_bc, codec_config->sample_rate);
561 break;
562 default:
563 break;
564 }
565
566 19 init_put_bits(&pbc, header, sizeof(header));
567 19 put_bits(&pbc, 5, IAMF_OBU_IA_CODEC_CONFIG);
568 19 put_bits(&pbc, 3, 0);
569 19 flush_put_bits(&pbc);
570
571 19 dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
572 19 avio_write(pb, header, put_bytes_count(&pbc, 1));
573 19 ffio_write_leb(pb, dyn_size);
574 19 avio_write(pb, dyn_buf, dyn_size);
575 19 ffio_free_dyn_buf(&dyn_bc);
576
577 19 return 0;
578 }
579
580 212 static inline int rescale_rational(AVRational q, int b)
581 {
582 212 return av_clip_int16(av_rescale(q.num, b, q.den));
583 }
584
585 52 static void get_loudspeaker_layout(const AVIAMFLayer *layer,
586 int *playout, int *pexpanded_layout)
587 {
588 52 int layout, expanded_layout = -1;
589
590
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 5 times.
239 for (layout = 0; layout < FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts); layout++) {
591
2/2
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 187 times.
234 if (!av_channel_layout_compare(&layer->ch_layout, &ff_iamf_scalable_ch_layouts[layout]))
592 47 break;
593 }
594
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 47 times.
52 if (layout >= FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts)) {
595
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 4 times.
47 for (layout = 0; layout < FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts); layout++)
596
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 42 times.
86 if (av_channel_layout_subset(&layer->ch_layout, UINT64_MAX) ==
597 43 av_channel_layout_subset(&ff_iamf_scalable_ch_layouts[layout], UINT64_MAX))
598 1 break;
599 }
600
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 48 times.
52 if (layout >= FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts)) {
601 4 layout = 15;
602
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 for (expanded_layout = 0; expanded_layout < FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts); expanded_layout++) {
603
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 32 times.
36 if (!av_channel_layout_compare(&layer->ch_layout, &ff_iamf_expanded_scalable_ch_layouts[expanded_layout]))
604 4 break;
605 }
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (expanded_layout >= FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts)) {
607 for (expanded_layout = 0; expanded_layout < FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts); expanded_layout++)
608 if (av_channel_layout_subset(&layer->ch_layout, UINT64_MAX) ==
609 av_channel_layout_subset(&ff_iamf_expanded_scalable_ch_layouts[expanded_layout], UINT64_MAX))
610 break;
611 }
612 }
613
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
52 av_assert0((expanded_layout > 0 && expanded_layout < FF_ARRAY_ELEMS(ff_iamf_expanded_scalable_ch_layouts)) ||
614 layout < FF_ARRAY_ELEMS(ff_iamf_scalable_ch_layouts));
615
616 52 *playout = layout;
617 52 *pexpanded_layout = expanded_layout;
618 52 }
619
620 15 static int scalable_channel_layout_config(const IAMFAudioElement *audio_element,
621 AVIOContext *dyn_bc)
622 {
623 15 const AVIAMFAudioElement *element = audio_element->celement;
624 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
625 PutBitContext pb;
626
627 15 init_put_bits(&pb, header, sizeof(header));
628 15 put_bits(&pb, 3, element->nb_layers);
629 15 put_bits(&pb, 5, 0);
630 15 flush_put_bits(&pb);
631 15 avio_write(dyn_bc, header, put_bytes_count(&pb, 1));
632
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 15 times.
52 for (int i = 0; i < element->nb_layers; i++) {
633 37 const AVIAMFLayer *layer = element->layers[i];
634 int layout, expanded_layout;
635
636 37 get_loudspeaker_layout(layer, &layout, &expanded_layout);
637 37 init_put_bits(&pb, header, sizeof(header));
638 37 put_bits(&pb, 4, layout);
639 37 put_bits(&pb, 1, !!layer->output_gain_flags);
640 37 put_bits(&pb, 1, !!(layer->flags & AV_IAMF_LAYER_FLAG_RECON_GAIN));
641 37 put_bits(&pb, 2, 0); // reserved
642 37 put_bits(&pb, 8, audio_element->layers[i].substream_count);
643 37 put_bits(&pb, 8, audio_element->layers[i].coupled_substream_count);
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (layer->output_gain_flags) {
645 put_bits(&pb, 6, layer->output_gain_flags);
646 put_bits(&pb, 2, 0);
647 put_bits(&pb, 16, rescale_rational(layer->output_gain, 1 << 8));
648 }
649
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 35 times.
37 if (expanded_layout >= 0)
650 2 put_bits(&pb, 8, expanded_layout);
651 37 flush_put_bits(&pb);
652 37 avio_write(dyn_bc, header, put_bytes_count(&pb, 1));
653 }
654
655 15 return 0;
656 }
657
658 6 static int ambisonics_config(const IAMFAudioElement *audio_element,
659 AVIOContext *dyn_bc)
660 {
661 6 const AVIAMFAudioElement *element = audio_element->celement;
662 6 const IAMFLayer *ilayer = &audio_element->layers[0];
663 6 const AVIAMFLayer *layer = element->layers[0];
664
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (audio_element->nb_substreams != ilayer->substream_count)
666 return AVERROR(EINVAL);
667
668 6 ffio_write_leb(dyn_bc, layer->ambisonics_mode);
669 6 avio_w8(dyn_bc, layer->ch_layout.nb_channels); // output_channel_count
670 6 avio_w8(dyn_bc, audio_element->nb_substreams); // substream_count
671
672
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (layer->ambisonics_mode == AV_IAMF_AMBISONICS_MODE_MONO) {
673
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (layer->ch_layout.order == AV_CHANNEL_ORDER_AMBISONIC)
674
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (int i = 0; i < layer->ch_layout.nb_channels; i++)
675 12 avio_w8(dyn_bc, i);
676 else
677 for (int i = 0; i < layer->ch_layout.nb_channels; i++)
678 avio_w8(dyn_bc, layer->ch_layout.u.map[i].id);
679 } else {
680 3 int nb_demixing_matrix = (ilayer->coupled_substream_count + ilayer->substream_count) * layer->ch_layout.nb_channels;
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (nb_demixing_matrix != layer->nb_demixing_matrix)
682 return AVERROR(EINVAL);
683 3 avio_w8(dyn_bc, ilayer->coupled_substream_count);
684
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 3 times.
51 for (int i = 0; i < layer->nb_demixing_matrix; i++)
685 48 avio_wb16(dyn_bc, rescale_rational(layer->demixing_matrix[i], 1 << 15));
686 }
687
688 6 return 0;
689 }
690
691 55 static int param_definition(const IAMFContext *iamf,
692 const IAMFParamDefinition *param_def,
693 AVIOContext *dyn_bc, void *log_ctx)
694 {
695 55 const AVIAMFParamDefinition *param = param_def->param;
696
697 55 ffio_write_leb(dyn_bc, param->parameter_id);
698 55 ffio_write_leb(dyn_bc, param->parameter_rate);
699
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 46 times.
55 avio_w8(dyn_bc, param->duration ? 0 : 1 << 7);
700
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 46 times.
55 if (param->duration) {
701 9 ffio_write_leb(dyn_bc, param->duration);
702 9 ffio_write_leb(dyn_bc, param->constant_subblock_duration);
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (param->constant_subblock_duration == 0) {
704 ffio_write_leb(dyn_bc, param->nb_subblocks);
705 for (int i = 0; i < param->nb_subblocks; i++) {
706 const void *subblock = av_iamf_param_definition_get_subblock(param, i);
707
708 switch (param->type) {
709 case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
710 const AVIAMFMixGain *mix = subblock;
711 ffio_write_leb(dyn_bc, mix->subblock_duration);
712 break;
713 }
714 case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
715 const AVIAMFDemixingInfo *demix = subblock;
716 ffio_write_leb(dyn_bc, demix->subblock_duration);
717 break;
718 }
719 case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
720 const AVIAMFReconGain *recon = subblock;
721 ffio_write_leb(dyn_bc, recon->subblock_duration);
722 break;
723 }
724 }
725 }
726 }
727 }
728
729 55 return 0;
730 }
731
732 21 static int iamf_write_audio_element(const IAMFContext *iamf,
733 const IAMFAudioElement *audio_element,
734 AVIOContext *pb, void *log_ctx)
735 {
736 21 const AVIAMFAudioElement *element = audio_element->celement;
737 21 const IAMFCodecConfig *codec_config = iamf->codec_configs[audio_element->codec_config_id];
738 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
739 AVIOContext *dyn_bc;
740 21 uint8_t *dyn_buf = NULL;
741 PutBitContext pbc;
742 21 int param_definition_types = AV_IAMF_PARAMETER_DEFINITION_DEMIXING, dyn_size;
743
744 21 int ret = avio_open_dyn_buf(&dyn_bc);
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
746 return ret;
747
748 21 ffio_write_leb(dyn_bc, audio_element->audio_element_id);
749
750 21 init_put_bits(&pbc, header, sizeof(header));
751 21 put_bits(&pbc, 3, element->audio_element_type);
752 21 put_bits(&pbc, 5, 0);
753 21 flush_put_bits(&pbc);
754 21 avio_write(dyn_bc, header, put_bytes_count(&pbc, 1));
755
756 21 ffio_write_leb(dyn_bc, audio_element->codec_config_id);
757 21 ffio_write_leb(dyn_bc, audio_element->nb_substreams);
758
759
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 21 times.
118 for (int i = 0; i < audio_element->nb_substreams; i++)
760 97 ffio_write_leb(dyn_bc, audio_element->substreams[i].audio_substream_id);
761
762 /* When audio_element_type = 1, num_parameters SHALL be set to 0 */
763
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 15 times.
21 if (element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE)
764 6 param_definition_types = 0;
765 else {
766 15 int layout = 0, expanded_layout = 0;
767 15 get_loudspeaker_layout(element->layers[0], &layout, &expanded_layout);
768 /* When the loudspeaker_layout = 15, the type PARAMETER_DEFINITION_DEMIXING SHALL NOT be present. */
769
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 13 times.
15 if (layout == 15) {
770 2 param_definition_types &= ~AV_IAMF_PARAMETER_DEFINITION_DEMIXING;
771 /* expanded_loudspeaker_layout SHALL only be present when num_layers = 1 and loudspeaker_layout is set to 15 */
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (element->nb_layers > 1) {
773 av_log(log_ctx, AV_LOG_ERROR, "expanded_loudspeaker_layout present when using more than one layer in "
774 "Stream Group #%u\n",
775 audio_element->audio_element_id);
776 return AVERROR(EINVAL);
777 }
778 }
779 /* When the loudspeaker_layout of the (non-)scalable channel audio (i.e., num_layers = 1) is less than or equal to 3.1.2ch,
780 * (i.e., Mono, Stereo, or 3.1.2ch), the type PARAMETER_DEFINITION_DEMIXING SHALL NOT be present. */
781
4/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
13 else if (element->nb_layers == 1 && (layout == 0 || layout == 1 || layout == 8))
782 5 param_definition_types &= ~AV_IAMF_PARAMETER_DEFINITION_DEMIXING;
783 /* When num_layers > 1, the type PARAMETER_DEFINITION_RECON_GAIN SHALL be present */
784
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 if (element->nb_layers > 1)
785 8 param_definition_types |= AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN;
786 /* When codec_id = fLaC or ipcm, the type PARAMETER_DEFINITION_RECON_GAIN SHALL NOT be present. */
787
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (codec_config->codec_tag == MKTAG('f','L','a','C') ||
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 codec_config->codec_tag == MKTAG('i','p','c','m'))
789 14 param_definition_types &= ~AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN;
790
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
15 if ((param_definition_types & AV_IAMF_PARAMETER_DEFINITION_DEMIXING) && !element->demixing_info) {
791 if (element->nb_layers > 1) {
792 get_loudspeaker_layout(element->layers[element->nb_layers-1], &layout, &expanded_layout);
793 /* When the highest loudspeaker_layout of the scalable channel audio (i.e., num_layers > 1) is greater than 3.1.2ch,
794 * (i.e., 5.1.2ch, 5.1.4ch, 7.1.2ch, or 7.1.4ch), type PARAMETER_DEFINITION_DEMIXING SHALL be present. */
795 if (layout == 3 || layout == 4 || layout == 6 || layout == 7) {
796 av_log(log_ctx, AV_LOG_ERROR, "demixing_info needed but not set in Stream Group #%u\n",
797 audio_element->audio_element_id);
798 return AVERROR(EINVAL);
799 }
800 }
801 param_definition_types &= ~AV_IAMF_PARAMETER_DEFINITION_DEMIXING;
802 }
803 }
804
805 21 ffio_write_leb(dyn_bc, av_popcount(param_definition_types)); // num_parameters
806
807
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 13 times.
21 if (param_definition_types & AV_IAMF_PARAMETER_DEFINITION_DEMIXING) {
808 8 const AVIAMFParamDefinition *param = element->demixing_info;
809 const IAMFParamDefinition *param_def;
810 const AVIAMFDemixingInfo *demix;
811
812 8 demix = av_iamf_param_definition_get_subblock(param, 0);
813 8 ffio_write_leb(dyn_bc, AV_IAMF_PARAMETER_DEFINITION_DEMIXING); // type
814
815 8 param_def = ff_iamf_get_param_definition(iamf, param->parameter_id);
816 8 ret = param_definition(iamf, param_def, dyn_bc, log_ctx);
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
818 return ret;
819
820 8 avio_w8(dyn_bc, demix->dmixp_mode << 5); // dmixp_mode
821 8 avio_w8(dyn_bc, element->default_w << 4); // default_w
822 }
823
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (param_definition_types & AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
824 1 const AVIAMFParamDefinition *param = element->recon_gain_info;
825 const IAMFParamDefinition *param_def;
826
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!param) {
828 av_log(log_ctx, AV_LOG_ERROR, "recon_gain_info needed but not set in Stream Group #%u\n",
829 audio_element->audio_element_id);
830 return AVERROR(EINVAL);
831 }
832 1 ffio_write_leb(dyn_bc, AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN); // type
833
834 1 param_def = ff_iamf_get_param_definition(iamf, param->parameter_id);
835 1 ret = param_definition(iamf, param_def, dyn_bc, log_ctx);
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
837 return ret;
838 }
839
840
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
21 if (element->audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL) {
841 15 ret = scalable_channel_layout_config(audio_element, dyn_bc);
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
843 return ret;
844 } else {
845 6 ret = ambisonics_config(audio_element, dyn_bc);
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
847 return ret;
848 }
849
850 21 init_put_bits(&pbc, header, sizeof(header));
851 21 put_bits(&pbc, 5, IAMF_OBU_IA_AUDIO_ELEMENT);
852 21 put_bits(&pbc, 3, 0);
853 21 flush_put_bits(&pbc);
854
855 21 dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
856 21 avio_write(pb, header, put_bytes_count(&pbc, 1));
857 21 ffio_write_leb(pb, dyn_size);
858 21 avio_write(pb, dyn_buf, dyn_size);
859 21 ffio_free_dyn_buf(&dyn_bc);
860
861 21 return 0;
862 }
863
864 19 static int iamf_write_mixing_presentation(const IAMFContext *iamf,
865 const IAMFMixPresentation *mix_presentation,
866 AVIOContext *pb, void *log_ctx)
867 {
868 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
869 19 const AVIAMFMixPresentation *mix = mix_presentation->cmix;
870 19 const AVDictionaryEntry *tag = NULL;
871 PutBitContext pbc;
872 AVIOContext *dyn_bc;
873 19 uint8_t *dyn_buf = NULL;
874 int dyn_size;
875
876 19 int ret = avio_open_dyn_buf(&dyn_bc);
877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
878 return ret;
879
880 19 ffio_write_leb(dyn_bc, mix_presentation->mix_presentation_id); // mix_presentation_id
881 19 ffio_write_leb(dyn_bc, av_dict_count(mix->annotations)); // count_label
882
883
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 19 times.
38 while ((tag = av_dict_iterate(mix->annotations, tag)))
884 19 avio_put_str(dyn_bc, tag->key);
885
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 19 times.
38 while ((tag = av_dict_iterate(mix->annotations, tag)))
886 19 avio_put_str(dyn_bc, tag->value);
887
888 19 ffio_write_leb(dyn_bc, mix->nb_submixes);
889
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 19 times.
41 for (int i = 0; i < mix->nb_submixes; i++) {
890 22 const AVIAMFSubmix *sub_mix = mix->submixes[i];
891 const IAMFParamDefinition *param_def;
892
893 22 ffio_write_leb(dyn_bc, sub_mix->nb_elements);
894
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 22 times.
46 for (int j = 0; j < sub_mix->nb_elements; j++) {
895 24 const IAMFAudioElement *audio_element = NULL;
896 24 const AVIAMFSubmixElement *submix_element = sub_mix->elements[j];
897
898
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 for (int k = 0; k < iamf->nb_audio_elements; k++)
899
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2 times.
26 if (iamf->audio_elements[k]->audio_element_id == submix_element->audio_element_id) {
900 24 audio_element = iamf->audio_elements[k];
901 24 break;
902 }
903
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 av_assert0(audio_element);
905 24 ffio_write_leb(dyn_bc, submix_element->audio_element_id);
906
907
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 if (av_dict_count(submix_element->annotations) != av_dict_count(mix->annotations)) {
908 av_log(log_ctx, AV_LOG_ERROR, "Inconsistent amount of labels in submix %d from Mix Presentation id #%u\n",
909 j, audio_element->audio_element_id);
910 return AVERROR(EINVAL);
911 }
912
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 24 times.
48 while ((tag = av_dict_iterate(submix_element->annotations, tag)))
913 24 avio_put_str(dyn_bc, tag->value);
914
915 24 init_put_bits(&pbc, header, sizeof(header));
916 24 put_bits(&pbc, 2, submix_element->headphones_rendering_mode);
917 24 put_bits(&pbc, 6, 0); // reserved
918 24 flush_put_bits(&pbc);
919 24 avio_write(dyn_bc, header, put_bytes_count(&pbc, 1));
920 24 ffio_write_leb(dyn_bc, 0); // rendering_config_extension_size
921
922 24 param_def = ff_iamf_get_param_definition(iamf, submix_element->element_mix_config->parameter_id);
923 24 ret = param_definition(iamf, param_def, dyn_bc, log_ctx);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
925 return ret;
926
927 24 avio_wb16(dyn_bc, rescale_rational(submix_element->default_mix_gain, 1 << 8));
928 }
929
930 22 param_def = ff_iamf_get_param_definition(iamf, sub_mix->output_mix_config->parameter_id);
931 22 ret = param_definition(iamf, param_def, dyn_bc, log_ctx);
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
933 return ret;
934 22 avio_wb16(dyn_bc, rescale_rational(sub_mix->default_mix_gain, 1 << 8));
935
936 22 ffio_write_leb(dyn_bc, sub_mix->nb_layouts); // nb_layouts
937
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 22 times.
68 for (int i = 0; i < sub_mix->nb_layouts; i++) {
938 46 const AVIAMFSubmixLayout *submix_layout = sub_mix->layouts[i];
939 int layout, info_type;
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 int dialogue = submix_layout->dialogue_anchored_loudness.num &&
941 submix_layout->dialogue_anchored_loudness.den;
942
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 int album = submix_layout->album_anchored_loudness.num &&
943 submix_layout->album_anchored_loudness.den;
944
945
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 3 times.
46 if (submix_layout->layout_type == AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS) {
946
1/2
✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
208 for (layout = 0; layout < FF_ARRAY_ELEMS(ff_iamf_sound_system_map); layout++) {
947
2/2
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 165 times.
208 if (!av_channel_layout_compare(&submix_layout->sound_system, &ff_iamf_sound_system_map[layout].layout))
948 43 break;
949 }
950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (layout == FF_ARRAY_ELEMS(ff_iamf_sound_system_map)) {
951 av_log(log_ctx, AV_LOG_ERROR, "Invalid Sound System value in a submix\n");
952 return AVERROR(EINVAL);
953 }
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (submix_layout->layout_type != AV_IAMF_SUBMIX_LAYOUT_TYPE_BINAURAL) {
955 av_log(log_ctx, AV_LOG_ERROR, "Unsupported Layout Type value in a submix\n");
956 return AVERROR(EINVAL);
957 }
958 46 init_put_bits(&pbc, header, sizeof(header));
959 46 put_bits(&pbc, 2, submix_layout->layout_type); // layout_type
960
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 3 times.
46 if (submix_layout->layout_type == AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS) {
961 43 put_bits(&pbc, 4, ff_iamf_sound_system_map[layout].id); // sound_system
962 43 put_bits(&pbc, 2, 0); // reserved
963 } else
964 3 put_bits(&pbc, 6, 0); // reserved
965 46 flush_put_bits(&pbc);
966 46 avio_write(dyn_bc, header, put_bytes_count(&pbc, 1));
967
968
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
46 info_type = (submix_layout->true_peak.num && submix_layout->true_peak.den);
969
2/4
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
46 info_type |= (dialogue || album) << 1;
970 46 avio_w8(dyn_bc, info_type);
971 46 avio_wb16(dyn_bc, rescale_rational(submix_layout->integrated_loudness, 1 << 8));
972 46 avio_wb16(dyn_bc, rescale_rational(submix_layout->digital_peak, 1 << 8));
973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (info_type & 1)
974 avio_wb16(dyn_bc, rescale_rational(submix_layout->true_peak, 1 << 8));
975
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (info_type & 2) {
976 avio_w8(dyn_bc, dialogue + album); // num_anchored_loudness
977 if (dialogue) {
978 avio_w8(dyn_bc, IAMF_ANCHOR_ELEMENT_DIALOGUE);
979 avio_wb16(dyn_bc, rescale_rational(submix_layout->dialogue_anchored_loudness, 1 << 8));
980 }
981 if (album) {
982 avio_w8(dyn_bc, IAMF_ANCHOR_ELEMENT_ALBUM);
983 avio_wb16(dyn_bc, rescale_rational(submix_layout->album_anchored_loudness, 1 << 8));
984 }
985 }
986 }
987 }
988
989 19 init_put_bits(&pbc, header, sizeof(header));
990 19 put_bits(&pbc, 5, IAMF_OBU_IA_MIX_PRESENTATION);
991 19 put_bits(&pbc, 3, 0);
992 19 flush_put_bits(&pbc);
993
994 19 dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
995 19 avio_write(pb, header, put_bytes_count(&pbc, 1));
996 19 ffio_write_leb(pb, dyn_size);
997 19 avio_write(pb, dyn_buf, dyn_size);
998 19 ffio_free_dyn_buf(&dyn_bc);
999
1000 19 return 0;
1001 }
1002
1003 19 int ff_iamf_write_descriptors(const IAMFContext *iamf, AVIOContext *pb, void *log_ctx)
1004 {
1005 int ret;
1006
1007 // Sequence Header
1008 19 avio_w8(pb, IAMF_OBU_IA_SEQUENCE_HEADER << 3);
1009
1010 19 ffio_write_leb(pb, 6);
1011 19 avio_wb32(pb, MKBETAG('i','a','m','f'));
1012 19 avio_w8(pb, iamf->nb_audio_elements > 1); // primary_profile
1013 19 avio_w8(pb, iamf->nb_audio_elements > 1); // additional_profile
1014
1015
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
38 for (int i = 0; i < iamf->nb_codec_configs; i++) {
1016 19 ret = iamf_write_codec_config(iamf, iamf->codec_configs[i], pb);
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1018 return ret;
1019 }
1020
1021
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
40 for (int i = 0; i < iamf->nb_audio_elements; i++) {
1022 21 ret = iamf_write_audio_element(iamf, iamf->audio_elements[i], pb, log_ctx);
1023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
1024 return ret;
1025 }
1026
1027
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
38 for (int i = 0; i < iamf->nb_mix_presentations; i++) {
1028 19 ret = iamf_write_mixing_presentation(iamf, iamf->mix_presentations[i], pb, log_ctx);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1030 return ret;
1031 }
1032
1033 19 return 0;
1034 }
1035
1036 52 static int write_parameter_block(const IAMFContext *iamf, AVIOContext *pb,
1037 const AVIAMFParamDefinition *param, void *log_ctx)
1038 {
1039 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
1040 52 const IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
1041 PutBitContext pbc;
1042 AVIOContext *dyn_bc;
1043 52 uint8_t *dyn_buf = NULL;
1044 int dyn_size, ret;
1045
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (param->type > AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
1047 av_log(log_ctx, AV_LOG_DEBUG, "Ignoring side data with unknown type %u\n",
1048 param->type);
1049 return 0;
1050 }
1051
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (!param_definition) {
1053 av_log(log_ctx, AV_LOG_ERROR, "Non-existent Parameter Definition with ID %u referenced by a packet\n",
1054 param->parameter_id);
1055 return AVERROR(EINVAL);
1056 }
1057
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (param->type != param_definition->param->type) {
1059 av_log(log_ctx, AV_LOG_ERROR, "Inconsistent values for Parameter Definition "
1060 "with ID %u in a packet\n",
1061 param->parameter_id);
1062 return AVERROR(EINVAL);
1063 }
1064
1065 52 ret = avio_open_dyn_buf(&dyn_bc);
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (ret < 0)
1067 return ret;
1068
1069 // Sequence Header
1070 52 init_put_bits(&pbc, header, sizeof(header));
1071 52 put_bits(&pbc, 5, IAMF_OBU_IA_PARAMETER_BLOCK);
1072 52 put_bits(&pbc, 3, 0);
1073 52 flush_put_bits(&pbc);
1074 52 avio_write(pb, header, put_bytes_count(&pbc, 1));
1075
1076 52 ffio_write_leb(dyn_bc, param->parameter_id);
1077
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if (!param_definition->mode) {
1078 26 ffio_write_leb(dyn_bc, param->duration);
1079 26 ffio_write_leb(dyn_bc, param->constant_subblock_duration);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (param->constant_subblock_duration == 0)
1081 ffio_write_leb(dyn_bc, param->nb_subblocks);
1082 }
1083
1084
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 52 times.
104 for (int i = 0; i < param->nb_subblocks; i++) {
1085 52 const void *subblock = av_iamf_param_definition_get_subblock(param, i);
1086
1087
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
52 switch (param->type) {
1088 26 case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
1089 26 const AVIAMFMixGain *mix = subblock;
1090
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
26 if (!param_definition->mode && param->constant_subblock_duration == 0)
1091 ffio_write_leb(dyn_bc, mix->subblock_duration);
1092
1093 26 ffio_write_leb(dyn_bc, mix->animation_type);
1094
1095 26 avio_wb16(dyn_bc, rescale_rational(mix->start_point_value, 1 << 8));
1096
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (mix->animation_type >= AV_IAMF_ANIMATION_TYPE_LINEAR)
1097 avio_wb16(dyn_bc, rescale_rational(mix->end_point_value, 1 << 8));
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (mix->animation_type == AV_IAMF_ANIMATION_TYPE_BEZIER) {
1099 avio_wb16(dyn_bc, rescale_rational(mix->control_point_value, 1 << 8));
1100 avio_w8(dyn_bc, av_clip_uint8(av_rescale(mix->control_point_relative_time.num, 1 << 8,
1101 mix->control_point_relative_time.den)));
1102 }
1103 26 break;
1104 }
1105 case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
1106 const AVIAMFDemixingInfo *demix = subblock;
1107 if (!param_definition->mode && param->constant_subblock_duration == 0)
1108 ffio_write_leb(dyn_bc, demix->subblock_duration);
1109
1110 avio_w8(dyn_bc, demix->dmixp_mode << 5);
1111 break;
1112 }
1113 26 case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
1114 26 const AVIAMFReconGain *recon = subblock;
1115 26 const AVIAMFAudioElement *audio_element = param_definition->audio_element->celement;
1116
1117
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 if (!param_definition->mode && param->constant_subblock_duration == 0)
1118 ffio_write_leb(dyn_bc, recon->subblock_duration);
1119
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!audio_element) {
1121 av_log(log_ctx, AV_LOG_ERROR, "Invalid Parameter Definition with ID %u referenced by a packet\n", param->parameter_id);
1122 return AVERROR(EINVAL);
1123 }
1124
1125
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
78 for (int j = 0; j < audio_element->nb_layers; j++) {
1126 52 const AVIAMFLayer *layer = audio_element->layers[j];
1127
1128
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if (layer->flags & AV_IAMF_LAYER_FLAG_RECON_GAIN) {
1129 26 unsigned int recon_gain_flags = 0;
1130 26 int k = 0;
1131
1132
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 26 times.
208 for (; k < 7; k++)
1133 182 recon_gain_flags |= (1 << k) * !!recon->recon_gain[j][k];
1134
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
156 for (; k < 12; k++)
1135 130 recon_gain_flags |= (2 << k) * !!recon->recon_gain[j][k];
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (recon_gain_flags >> 8)
1137 recon_gain_flags |= (1 << k);
1138
1139 26 ffio_write_leb(dyn_bc, recon_gain_flags);
1140
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 26 times.
338 for (k = 0; k < 12; k++) {
1141
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 208 times.
312 if (recon->recon_gain[j][k])
1142 104 avio_w8(dyn_bc, recon->recon_gain[j][k]);
1143 }
1144 }
1145 }
1146 26 break;
1147 }
1148 default:
1149 av_unreachable("param_definition_type should have been checked above");
1150 }
1151 }
1152
1153 52 dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
1154 52 ffio_write_leb(pb, dyn_size);
1155 52 avio_write(pb, dyn_buf, dyn_size);
1156 52 ffio_free_dyn_buf(&dyn_bc);
1157
1158 52 return 0;
1159 }
1160
1161 398 int ff_iamf_write_parameter_blocks(const IAMFContext *iamf, AVIOContext *pb,
1162 const AVPacket *pkt, void *log_ctx)
1163 {
1164 AVIAMFParamDefinition *mix =
1165 398 (AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
1166 AV_PKT_DATA_IAMF_MIX_GAIN_PARAM,
1167 NULL);
1168 AVIAMFParamDefinition *demix =
1169 398 (AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
1170 AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM,
1171 NULL);
1172 AVIAMFParamDefinition *recon =
1173 398 (AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
1174 AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM,
1175 NULL);
1176
1177
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 372 times.
398 if (mix) {
1178 26 int ret = write_parameter_block(iamf, pb, mix, log_ctx);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1180 return ret;
1181 }
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 398 times.
398 if (demix) {
1183 int ret = write_parameter_block(iamf, pb, demix, log_ctx);
1184 if (ret < 0)
1185 return ret;
1186 }
1187
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 372 times.
398 if (recon) {
1188 26 int ret = write_parameter_block(iamf, pb, recon, log_ctx);
1189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
1190 return ret;
1191 }
1192
1193 398 return 0;
1194 }
1195
1196 1735 static IAMFAudioElement *get_audio_element(const IAMFContext *c,
1197 unsigned int audio_substream_id)
1198 {
1199
1/2
✓ Branch 0 taken 1746 times.
✗ Branch 1 not taken.
1746 for (int i = 0; i < c->nb_audio_elements; i++) {
1200 1746 IAMFAudioElement *audio_element = c->audio_elements[i];
1201
2/2
✓ Branch 0 taken 5113 times.
✓ Branch 1 taken 11 times.
5124 for (int j = 0; j < audio_element->nb_substreams; j++) {
1202 5113 IAMFSubStream *substream = &audio_element->substreams[j];
1203
2/2
✓ Branch 0 taken 1735 times.
✓ Branch 1 taken 3378 times.
5113 if (substream->audio_substream_id == audio_substream_id)
1204 1735 return audio_element;
1205 }
1206 }
1207
1208 return NULL;
1209 }
1210
1211 1735 int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
1212 unsigned audio_substream_id, const AVPacket *pkt)
1213 {
1214 uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
1215 PutBitContext pbc;
1216 const IAMFAudioElement *audio_element;
1217 IAMFCodecConfig *codec_config;
1218 AVIOContext *dyn_bc;
1219 const uint8_t *side_data;
1220 1735 uint8_t *dyn_buf = NULL;
1221 1735 unsigned int skip_samples = 0, discard_padding = 0;
1222 size_t side_data_size;
1223 1735 int dyn_size, type = audio_substream_id <= 17 ?
1224
1/2
✓ Branch 0 taken 1735 times.
✗ Branch 1 not taken.
1735 audio_substream_id + IAMF_OBU_IA_AUDIO_FRAME_ID0 : IAMF_OBU_IA_AUDIO_FRAME;
1225 int ret;
1226
1227 1735 audio_element = get_audio_element(iamf, audio_substream_id);
1228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1735 times.
1735 if (!audio_element)
1229 return AVERROR(EINVAL);
1230 1735 codec_config = ff_iamf_get_codec_config(iamf, audio_element->codec_config_id);
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1735 times.
1735 if (!codec_config)
1232 return AVERROR(EINVAL);
1233
1234
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 1678 times.
1735 if (!pkt->size) {
1235 size_t new_extradata_size;
1236 57 const uint8_t *new_extradata = av_packet_get_side_data(pkt,
1237 AV_PKT_DATA_NEW_EXTRADATA,
1238 &new_extradata_size);
1239
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (!new_extradata)
1241 return AVERROR_INVALIDDATA;
1242
1243 57 av_free(codec_config->extradata);
1244 57 codec_config->extradata = av_memdup(new_extradata, new_extradata_size);
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (!codec_config->extradata) {
1246 codec_config->extradata_size = 0;
1247 return AVERROR(ENOMEM);
1248 }
1249 57 codec_config->extradata_size = new_extradata_size;
1250
1251 57 return update_extradata(codec_config);
1252 }
1253
1254 1678 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES,
1255 &side_data_size);
1256
1257
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1662 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
1678 if (side_data && side_data_size >= 10) {
1258 16 skip_samples = AV_RL32(side_data);
1259 16 discard_padding = AV_RL32(side_data + 4);
1260 }
1261
1262
2/2
✓ Branch 0 taken 1108 times.
✓ Branch 1 taken 570 times.
1678 if (codec_config->codec_id == AV_CODEC_ID_OPUS) {
1263 // IAMF's num_samples_to_trim_at_start is the same as Opus's pre-skip.
1264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
1108 skip_samples = pkt->dts < 0
1265 ? av_rescale(-pkt->dts, 48000, pkt->time_base.den)
1266 : 0;
1267 1108 discard_padding = av_rescale(discard_padding, 48000, pkt->time_base.den);
1268 }
1269
1270 1678 ret = avio_open_dyn_buf(&dyn_bc);
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1678 times.
1678 if (ret < 0)
1272 return ret;
1273
1274 1678 init_put_bits(&pbc, header, sizeof(header));
1275 1678 put_bits(&pbc, 5, type);
1276 1678 put_bits(&pbc, 1, 0); // obu_redundant_copy
1277
3/4
✓ Branch 0 taken 1678 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1670 times.
1678 put_bits(&pbc, 1, skip_samples || discard_padding);
1278 1678 put_bits(&pbc, 1, 0); // obu_extension_flag
1279 1678 flush_put_bits(&pbc);
1280 1678 avio_write(pb, header, put_bytes_count(&pbc, 1));
1281
1282
3/4
✓ Branch 0 taken 1678 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1670 times.
1678 if (skip_samples || discard_padding) {
1283 8 ffio_write_leb(dyn_bc, discard_padding);
1284 8 ffio_write_leb(dyn_bc, skip_samples);
1285 }
1286
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1678 times.
1678 if (audio_substream_id > 17)
1288 ffio_write_leb(dyn_bc, audio_substream_id);
1289
1290 1678 dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
1291 1678 ffio_write_leb(pb, dyn_size + pkt->size);
1292 1678 avio_write(pb, dyn_buf, dyn_size);
1293 1678 ffio_free_dyn_buf(&dyn_bc);
1294 1678 avio_write(pb, pkt->data, pkt->size);
1295
1296 1678 return 0;
1297 }
1298