Coverage Report

Created: 2026-07-15 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/options.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
#include "avformat.h"
21
#include "avformat_internal.h"
22
#include "avio_internal.h"
23
#include "demux.h"
24
#include "internal.h"
25
26
#include "libavcodec/avcodec.h"
27
#include "libavcodec/codec_par.h"
28
29
#include "libavutil/attributes.h"
30
#include "libavutil/avassert.h"
31
#include "libavutil/iamf.h"
32
#include "libavutil/internal.h"
33
#include "libavutil/intmath.h"
34
#include "libavutil/mem.h"
35
#include "libavutil/opt.h"
36
37
/**
38
 * @file
39
 * Options definition for AVFormatContext.
40
 */
41
42
FF_DISABLE_DEPRECATION_WARNINGS
43
#include "options_table.h"
44
FF_ENABLE_DEPRECATION_WARNINGS
45
46
static const char* format_to_name(void* ptr)
47
0
{
48
0
    AVFormatContext* fc = (AVFormatContext*) ptr;
49
0
    if (fc->name) return fc->name;
50
0
    else if(fc->iformat) return fc->iformat->name;
51
0
    else if(fc->oformat) return fc->oformat->name;
52
0
    else return fc->av_class->class_name;
53
0
}
54
55
static void *format_child_next(void *obj, void *prev)
56
0
{
57
0
    AVFormatContext *s = obj;
58
0
    if (!prev && s->priv_data &&
59
0
        ((s->iformat && s->iformat->priv_class) ||
60
0
          s->oformat && s->oformat->priv_class))
61
0
        return s->priv_data;
62
0
    if (s->pb && s->pb->av_class && prev != s->pb)
63
0
        return s->pb;
64
0
    return NULL;
65
0
}
66
67
enum {
68
    CHILD_CLASS_ITER_AVIO = 0,
69
    CHILD_CLASS_ITER_MUX,
70
    CHILD_CLASS_ITER_DEMUX,
71
    CHILD_CLASS_ITER_DONE,
72
73
};
74
75
0
#define ITER_STATE_SHIFT 16
76
77
static const AVClass *format_child_class_iterate(void **iter)
78
0
{
79
    // we use the low 16 bits of iter as the value to be passed to
80
    // av_(de)muxer_iterate()
81
0
    void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
82
0
    unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
83
0
    const AVClass *ret = NULL;
84
85
0
    if (state == CHILD_CLASS_ITER_AVIO) {
86
0
        ret = &ff_avio_class;
87
0
        state++;
88
0
        goto finish;
89
0
    }
90
91
0
    if (state == CHILD_CLASS_ITER_MUX) {
92
0
        const AVOutputFormat *ofmt;
93
94
0
        while ((ofmt = av_muxer_iterate(&val))) {
95
0
            ret = ofmt->priv_class;
96
0
            if (ret)
97
0
                goto finish;
98
0
        }
99
100
0
        val = NULL;
101
0
        state++;
102
0
    }
103
104
0
    if (state == CHILD_CLASS_ITER_DEMUX) {
105
0
        const AVInputFormat *ifmt;
106
107
0
        while ((ifmt = av_demuxer_iterate(&val))) {
108
0
            ret = ifmt->priv_class;
109
0
            if (ret)
110
0
                goto finish;
111
0
        }
112
0
        val = NULL;
113
0
        state++;
114
0
    }
115
116
0
finish:
117
    // make sure none av_(de)muxer_iterate does not set the high bits of val
118
0
    av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
119
0
    *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
120
0
    return ret;
121
0
}
122
123
static AVClassCategory get_category(void *ptr)
124
0
{
125
0
    AVFormatContext* s = ptr;
126
0
    if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
127
0
    else           return AV_CLASS_CATEGORY_MUXER;
128
0
}
129
130
static const AVClass av_format_context_class = {
131
    .class_name     = "AVFormatContext",
132
    .item_name      = format_to_name,
133
    .option         = avformat_options,
134
    .version        = LIBAVUTIL_VERSION_INT,
135
    .child_next     = format_child_next,
136
    .child_class_iterate = format_child_class_iterate,
137
    .category       = AV_CLASS_CATEGORY_MUXER,
138
    .get_category   = get_category,
139
};
140
141
static int io_open_default(AVFormatContext *s, AVIOContext **pb,
142
                           const char *url, int flags, AVDictionary **options)
143
1.57k
{
144
1.57k
    int loglevel;
145
146
1.57k
    if (!strcmp(url, s->url) ||
147
0
        s->iformat && !strcmp(s->iformat->name, "image2") ||
148
0
        s->oformat && !strcmp(s->oformat->name, "image2")
149
1.57k
    ) {
150
1.57k
        loglevel = AV_LOG_DEBUG;
151
1.57k
    } else
152
0
        loglevel = AV_LOG_INFO;
153
154
1.57k
    av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
155
156
1.57k
    return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
157
1.57k
}
158
159
static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
160
0
{
161
0
    return avio_close(pb);
162
0
}
163
164
AVFormatContext *avformat_alloc_context(void)
165
1.57k
{
166
1.57k
    FormatContextInternal *fci;
167
1.57k
    FFFormatContext *si;
168
1.57k
    AVFormatContext *s;
169
170
1.57k
    fci = av_mallocz(sizeof(*fci));
171
1.57k
    if (!fci)
172
0
        return NULL;
173
174
1.57k
    si = &fci->fc;
175
1.57k
    s = &si->pub;
176
1.57k
    s->av_class = &av_format_context_class;
177
1.57k
    s->io_open  = io_open_default;
178
1.57k
    s->io_close2= io_close2_default;
179
180
1.57k
    av_opt_set_defaults(s);
181
182
1.57k
    si->pkt = av_packet_alloc();
183
1.57k
    si->parse_pkt = av_packet_alloc();
184
1.57k
    if (!si->pkt || !si->parse_pkt) {
185
0
        avformat_free_context(s);
186
0
        return NULL;
187
0
    }
188
189
1.57k
    return s;
190
1.57k
}
191
192
const AVClass *avformat_get_class(void)
193
0
{
194
0
    return &av_format_context_class;
195
0
}
196
197
#define DISPOSITION_OPT(ctx)                                                                                                        \
198
    { "disposition", NULL, offsetof(ctx, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },                                             \
199
        .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },                                                               \
200
        { "default",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT           },    .unit = "disposition" }, \
201
        { "dub",                .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB               },    .unit = "disposition" }, \
202
        { "original",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL          },    .unit = "disposition" }, \
203
        { "comment",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT           },    .unit = "disposition" }, \
204
        { "lyrics",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS            },    .unit = "disposition" }, \
205
        { "karaoke",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE           },    .unit = "disposition" }, \
206
        { "forced",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED            },    .unit = "disposition" }, \
207
        { "hearing_impaired",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED  },    .unit = "disposition" }, \
208
        { "visual_impaired",    .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED   },    .unit = "disposition" }, \
209
        { "clean_effects",      .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS     },    .unit = "disposition" }, \
210
        { "attached_pic",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC      },    .unit = "disposition" }, \
211
        { "timed_thumbnails",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS  },    .unit = "disposition" }, \
212
        { "non_diegetic",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_NON_DIEGETIC      },    .unit = "disposition" }, \
213
        { "captions",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS          },    .unit = "disposition" }, \
214
        { "descriptions",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS      },    .unit = "disposition" }, \
215
        { "metadata",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA          },    .unit = "disposition" }, \
216
        { "dependent",          .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT         },    .unit = "disposition" }, \
217
        { "still_image",        .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE       },    .unit = "disposition" }, \
218
        { "multilayer",         .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_MULTILAYER        },    .unit = "disposition" }
219
220
static const AVOption stream_options[] = {
221
    DISPOSITION_OPT(AVStream),
222
    { "discard", NULL, offsetof(AVStream, discard), AV_OPT_TYPE_INT, { .i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX,
223
        .flags = AV_OPT_FLAG_DECODING_PARAM, .unit = "avdiscard" },
224
        { "none",               .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONE     }, .unit = "avdiscard" },
225
        { "default",            .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_DEFAULT  }, .unit = "avdiscard" },
226
        { "noref",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONREF   }, .unit = "avdiscard" },
227
        { "bidir",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_BIDIR    }, .unit = "avdiscard" },
228
        { "nointra",            .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONINTRA }, .unit = "avdiscard" },
229
        { "nokey",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONKEY   }, .unit = "avdiscard" },
230
        { "all",                .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_ALL      }, .unit = "avdiscard" },
231
    { NULL }
232
};
233
234
static const AVClass stream_class = {
235
    .class_name     = "AVStream",
236
    .item_name      = av_default_item_name,
237
    .version        = LIBAVUTIL_VERSION_INT,
238
    .option         = stream_options,
239
};
240
241
const AVClass *av_stream_get_class(void)
242
0
{
243
0
    return &stream_class;
244
0
}
245
246
AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
247
0
{
248
0
    FFStream *sti;
249
0
    AVStream *st;
250
0
    AVStream **streams;
251
252
0
    if (s->nb_streams >= s->max_streams) {
253
0
        av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
254
0
               " (%d), see the documentation if you wish to increase it\n",
255
0
               s->max_streams);
256
0
        return NULL;
257
0
    }
258
0
    streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
259
0
    if (!streams)
260
0
        return NULL;
261
0
    s->streams = streams;
262
263
0
    sti = av_mallocz(sizeof(*sti));
264
0
    if (!sti)
265
0
        return NULL;
266
0
    st = &sti->pub;
267
268
0
    st->av_class = &stream_class;
269
0
    st->codecpar = avcodec_parameters_alloc();
270
0
    if (!st->codecpar)
271
0
        goto fail;
272
273
0
    sti->fmtctx = s;
274
275
0
    sti->parse_pkt = av_packet_alloc();
276
0
    if (!sti->parse_pkt)
277
0
        goto fail;
278
279
0
    if (s->iformat) {
280
0
        sti->avctx = avcodec_alloc_context3(NULL);
281
0
        if (!sti->avctx)
282
0
            goto fail;
283
284
0
        sti->info = av_mallocz(sizeof(*sti->info));
285
0
        if (!sti->info)
286
0
            goto fail;
287
288
0
#if FF_API_R_FRAME_RATE
289
0
        sti->info->last_dts      = AV_NOPTS_VALUE;
290
0
#endif
291
0
        sti->info->fps_first_dts = AV_NOPTS_VALUE;
292
0
        sti->info->fps_last_dts  = AV_NOPTS_VALUE;
293
294
        /* default pts setting is MPEG-like */
295
0
        avpriv_set_pts_info(st, 33, 1, 90000);
296
        /* we set the current DTS to 0 so that formats without any timestamps
297
         * but durations get some timestamps, formats with some unknown
298
         * timestamps have their first few packets buffered and the
299
         * timestamps corrected before they are returned to the user */
300
0
        sti->cur_dts = RELATIVE_TS_BASE;
301
0
    } else {
302
0
        sti->cur_dts = AV_NOPTS_VALUE;
303
0
    }
304
305
0
    st->index      = s->nb_streams;
306
0
    st->start_time = AV_NOPTS_VALUE;
307
0
    st->duration   = AV_NOPTS_VALUE;
308
0
    sti->first_dts     = AV_NOPTS_VALUE;
309
0
    sti->probe_packets = s->max_probe_packets;
310
0
    sti->pts_wrap_reference = AV_NOPTS_VALUE;
311
0
    sti->pts_wrap_behavior  = AV_PTS_WRAP_IGNORE;
312
313
0
    sti->last_IP_pts = AV_NOPTS_VALUE;
314
0
    sti->last_dts_for_order_check = AV_NOPTS_VALUE;
315
0
    for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
316
0
        sti->pts_buffer[i] = AV_NOPTS_VALUE;
317
318
0
    st->sample_aspect_ratio = (AVRational) { 0, 1 };
319
320
0
    sti->need_context_update = 1;
321
322
0
    s->streams[s->nb_streams++] = st;
323
0
    return st;
324
0
fail:
325
0
    ff_free_stream(&st);
326
0
    return NULL;
327
0
}
328
329
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
330
#define OFFSET(x) offsetof(AVStreamGroupTileGrid, x)
331
static const AVOption tile_grid_options[] = {
332
    { "grid_size", "size of the output canvas", OFFSET(coded_width),
333
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
334
    { "output_size", "size of valid pixels in output image meant for presentation", OFFSET(width),
335
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
336
    { "background_color", "set a background color for unused pixels",
337
        OFFSET(background), AV_OPT_TYPE_COLOR, { .str = "black"}, 0, 0, FLAGS },
338
    { "horizontal_offset", NULL, OFFSET(horizontal_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
339
    { "vertical_offset",   NULL, OFFSET(vertical_offset),   AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
340
    { NULL },
341
};
342
#undef OFFSET
343
344
static const AVClass tile_grid_class = {
345
    .class_name = "AVStreamGroupTileGrid",
346
    .version    = LIBAVUTIL_VERSION_INT,
347
    .option     = tile_grid_options,
348
};
349
350
#define OFFSET(x) offsetof(AVStreamGroupTREF, x)
351
static const AVOption tref_options[] = {
352
    { "metadata_index", "Index of the data stream within the group", OFFSET(metadata_index),
353
        AV_OPT_TYPE_UINT, { .i64 = 0 }, 0, UINT_MAX, FLAGS },
354
    { NULL },
355
};
356
#undef OFFSET
357
358
static const AVClass tref_class = {
359
    .class_name = "AVStreamGroupTREF",
360
    .version    = LIBAVUTIL_VERSION_INT,
361
    .option     = tref_options,
362
};
363
364
#if FF_API_LCEVC_STRUCT
365
FF_DISABLE_DEPRECATION_WARNINGS
366
#endif
367
#define OFFSET(x) offsetof(AVStreamGroupLayeredVideo, x)
368
static const AVOption layered_video_options[] = {
369
#if FF_API_LCEVC_STRUCT
370
    { "lcevc_index", "Index of the LCEVC stream within the group", OFFSET(lcevc_index),
371
        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS | AV_OPT_FLAG_DEPRECATED },
372
#endif
373
    { "el_index", "Index of the enhancement layer stream within the group", OFFSET(el_index),
374
        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
375
    { "video_size", "size of the final layered video presentation", OFFSET(width),
376
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
377
    { NULL },
378
};
379
#if FF_API_LCEVC_STRUCT
380
FF_ENABLE_DEPRECATION_WARNINGS
381
#endif
382
#undef OFFSET
383
384
static const AVClass layered_video_class = {
385
    .class_name = "AVStreamGroupLayeredVideo",
386
    .version    = LIBAVUTIL_VERSION_INT,
387
    .option     = layered_video_options,
388
};
389
390
static void *stream_group_child_next(void *obj, void *prev)
391
0
{
392
0
    AVStreamGroup *stg = obj;
393
0
    if (!prev) {
394
0
        switch(stg->type) {
395
0
        case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
396
0
            return stg->params.iamf_audio_element;
397
0
        case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
398
0
            return stg->params.iamf_mix_presentation;
399
0
        case AV_STREAM_GROUP_PARAMS_TILE_GRID:
400
0
            return stg->params.tile_grid;
401
0
        case AV_STREAM_GROUP_PARAMS_TREF:
402
0
            return stg->params.tref;
403
0
        case AV_STREAM_GROUP_PARAMS_LCEVC:
404
0
        case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
405
0
            return stg->params.layered_video;
406
0
        default:
407
0
            break;
408
0
        }
409
0
    }
410
0
    return NULL;
411
0
}
412
413
#undef FLAGS
414
415
static const AVClass *stream_group_child_iterate(void **opaque)
416
0
{
417
0
    uintptr_t i = (uintptr_t)*opaque;
418
0
    const AVClass *ret = NULL;
419
420
0
    switch(i) {
421
0
    case AV_STREAM_GROUP_PARAMS_NONE:
422
0
        i++;
423
0
        av_fallthrough;
424
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
425
0
        ret = av_iamf_audio_element_get_class();
426
0
        break;
427
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
428
0
        ret = av_iamf_mix_presentation_get_class();
429
0
        break;
430
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
431
0
        ret = &tile_grid_class;
432
0
        break;
433
0
    case AV_STREAM_GROUP_PARAMS_TREF:
434
0
        ret = &tref_class;
435
0
        break;
436
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
437
0
    case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
438
0
        ret = &layered_video_class;
439
0
        break;
440
0
    default:
441
0
        break;
442
0
    }
443
444
0
    if (ret)
445
0
        *opaque = (void*)(i + 1);
446
0
    return ret;
447
0
}
448
449
static const AVOption stream_group_options[] = {
450
    DISPOSITION_OPT(AVStreamGroup),
451
    {"id", "Set group id", offsetof(AVStreamGroup, id), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
452
    { NULL }
453
};
454
455
static const AVClass stream_group_class = {
456
    .class_name     = "AVStreamGroup",
457
    .item_name      = av_default_item_name,
458
    .version        = LIBAVUTIL_VERSION_INT,
459
    .option         = stream_group_options,
460
    .child_next     = stream_group_child_next,
461
    .child_class_iterate = stream_group_child_iterate,
462
};
463
464
const AVClass *av_stream_group_get_class(void)
465
0
{
466
0
    return &stream_group_class;
467
0
}
468
469
AVStreamGroup *avformat_stream_group_create(AVFormatContext *s,
470
                                            enum AVStreamGroupParamsType type,
471
                                            AVDictionary **options)
472
0
{
473
0
    AVStreamGroup **stream_groups;
474
0
    AVStreamGroup *stg;
475
0
    FFStreamGroup *stgi;
476
477
0
    stream_groups = av_realloc_array(s->stream_groups, s->nb_stream_groups + 1,
478
0
                                     sizeof(*stream_groups));
479
0
    if (!stream_groups)
480
0
        return NULL;
481
0
    s->stream_groups = stream_groups;
482
483
0
    stgi = av_mallocz(sizeof(*stgi));
484
0
    if (!stgi)
485
0
        return NULL;
486
0
    stg = &stgi->pub;
487
488
0
    stg->av_class = &stream_group_class;
489
0
    av_opt_set_defaults(stg);
490
0
    stg->type = type;
491
0
    switch (type) {
492
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
493
0
        stg->params.iamf_audio_element = av_iamf_audio_element_alloc();
494
0
        if (!stg->params.iamf_audio_element)
495
0
            goto fail;
496
0
        break;
497
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
498
0
        stg->params.iamf_mix_presentation = av_iamf_mix_presentation_alloc();
499
0
        if (!stg->params.iamf_mix_presentation)
500
0
            goto fail;
501
0
        break;
502
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
503
0
        stg->params.tile_grid = av_mallocz(sizeof(*stg->params.tile_grid));
504
0
        if (!stg->params.tile_grid)
505
0
            goto fail;
506
0
        stg->params.tile_grid->av_class = &tile_grid_class;
507
0
        av_opt_set_defaults(stg->params.tile_grid);
508
0
        break;
509
0
    case AV_STREAM_GROUP_PARAMS_TREF:
510
0
        stg->params.tref = av_mallocz(sizeof(*stg->params.tref));
511
0
        if (!stg->params.tref)
512
0
            goto fail;
513
0
        stg->params.tref->av_class = &tref_class;
514
0
        av_opt_set_defaults(stg->params.tref);
515
0
        break;
516
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
517
0
    case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
518
0
        stg->params.layered_video = av_mallocz(sizeof(*stg->params.layered_video));
519
0
        if (!stg->params.layered_video)
520
0
            goto fail;
521
0
        stg->params.layered_video->av_class = &layered_video_class;
522
0
        av_opt_set_defaults(stg->params.layered_video);
523
0
        break;
524
0
    default:
525
0
        goto fail;
526
0
    }
527
528
0
    if (options) {
529
0
        if (av_opt_set_dict2(stg, options, AV_OPT_SEARCH_CHILDREN))
530
0
            goto fail;
531
0
    }
532
533
0
    stgi->fmtctx = s;
534
0
    stg->index   = s->nb_stream_groups;
535
536
0
    s->stream_groups[s->nb_stream_groups++] = stg;
537
538
0
    return stg;
539
0
fail:
540
0
    ff_free_stream_group(&stg);
541
0
    return NULL;
542
0
}
543
544
static int stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
545
0
{
546
0
    AVStream **streams = av_realloc_array(stg->streams, stg->nb_streams + 1,
547
0
                                          sizeof(*stg->streams));
548
0
    if (!streams)
549
0
        return AVERROR(ENOMEM);
550
551
0
    stg->streams = streams;
552
0
    stg->streams[stg->nb_streams++] = st;
553
554
0
    return 0;
555
0
}
556
557
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
558
0
{
559
0
    const FFStreamGroup *stgi = cffstreamgroup(stg);
560
0
    const FFStream *sti = cffstream(st);
561
562
0
    if (stgi->fmtctx != sti->fmtctx)
563
0
        return AVERROR(EINVAL);
564
565
0
    for (int i = 0; i < stg->nb_streams; i++)
566
0
        if (stg->streams[i]->index == st->index)
567
0
            return AVERROR(EEXIST);
568
569
0
    return stream_group_add_stream(stg, st);
570
0
}
571
572
static int option_is_disposition(const AVOption *opt)
573
0
{
574
0
    return opt->type == AV_OPT_TYPE_CONST &&
575
0
           opt->unit && !strcmp(opt->unit, "disposition");
576
0
}
577
578
int av_disposition_from_string(const char *disp)
579
0
{
580
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
581
0
        if (option_is_disposition(opt) && !strcmp(disp, opt->name))
582
0
            return opt->default_val.i64;
583
0
    return AVERROR(EINVAL);
584
0
}
585
586
const char *av_disposition_to_string(int disposition)
587
0
{
588
0
    int val;
589
590
0
    if (disposition <= 0)
591
0
        return NULL;
592
593
0
    val = 1 << ff_ctz(disposition);
594
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
595
0
        if (option_is_disposition(opt) && opt->default_val.i64 == val)
596
0
            return opt->name;
597
598
0
    return NULL;
599
0
}