Coverage Report

Created: 2026-10-28 07:09

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.43k
{
144
1.43k
    int loglevel;
145
146
1.43k
    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.43k
    ) {
150
1.43k
        loglevel = AV_LOG_DEBUG;
151
1.43k
    } else
152
0
        loglevel = AV_LOG_INFO;
153
154
1.43k
    av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
155
156
1.43k
    return ffio_open_whitelist2(pb, url, flags, &s->interrupt_callback, options,
157
1.43k
                                s->protocol_whitelist, s->protocol_blacklist, s);
158
1.43k
}
159
160
static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
161
0
{
162
0
    return avio_close(pb);
163
0
}
164
165
AVFormatContext *avformat_alloc_context(void)
166
1.43k
{
167
1.43k
    FormatContextInternal *fci;
168
1.43k
    FFFormatContext *si;
169
1.43k
    AVFormatContext *s;
170
171
1.43k
    fci = av_mallocz(sizeof(*fci));
172
1.43k
    if (!fci)
173
0
        return NULL;
174
175
1.43k
    si = &fci->fc;
176
1.43k
    s = &si->pub;
177
1.43k
    s->av_class = &av_format_context_class;
178
1.43k
    s->io_open  = io_open_default;
179
1.43k
    s->io_close2= io_close2_default;
180
181
1.43k
    av_opt_set_defaults(s);
182
183
1.43k
    si->pkt = av_packet_alloc();
184
1.43k
    si->parse_pkt = av_packet_alloc();
185
1.43k
    if (!si->pkt || !si->parse_pkt) {
186
0
        avformat_free_context(s);
187
0
        return NULL;
188
0
    }
189
190
1.43k
    return s;
191
1.43k
}
192
193
const AVClass *avformat_get_class(void)
194
0
{
195
0
    return &av_format_context_class;
196
0
}
197
198
#define DISPOSITION_OPT(ctx)                                                                                                        \
199
    { "disposition", NULL, offsetof(ctx, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },                                             \
200
        .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },                                                               \
201
        { "default",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT           },    .unit = "disposition" }, \
202
        { "dub",                .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB               },    .unit = "disposition" }, \
203
        { "original",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL          },    .unit = "disposition" }, \
204
        { "comment",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT           },    .unit = "disposition" }, \
205
        { "lyrics",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS            },    .unit = "disposition" }, \
206
        { "karaoke",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE           },    .unit = "disposition" }, \
207
        { "forced",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED            },    .unit = "disposition" }, \
208
        { "hearing_impaired",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED  },    .unit = "disposition" }, \
209
        { "visual_impaired",    .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED   },    .unit = "disposition" }, \
210
        { "clean_effects",      .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS     },    .unit = "disposition" }, \
211
        { "attached_pic",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC      },    .unit = "disposition" }, \
212
        { "timed_thumbnails",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS  },    .unit = "disposition" }, \
213
        { "non_diegetic",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_NON_DIEGETIC      },    .unit = "disposition" }, \
214
        { "captions",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS          },    .unit = "disposition" }, \
215
        { "descriptions",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS      },    .unit = "disposition" }, \
216
        { "metadata",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA          },    .unit = "disposition" }, \
217
        { "dependent",          .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT         },    .unit = "disposition" }, \
218
        { "still_image",        .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE       },    .unit = "disposition" }, \
219
        { "multilayer",         .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_MULTILAYER        },    .unit = "disposition" }
220
221
static const AVOption stream_options[] = {
222
    DISPOSITION_OPT(AVStream),
223
    { "discard", NULL, offsetof(AVStream, discard), AV_OPT_TYPE_INT, { .i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX,
224
        .flags = AV_OPT_FLAG_DECODING_PARAM, .unit = "avdiscard" },
225
        { "none",               .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONE     }, .unit = "avdiscard" },
226
        { "default",            .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_DEFAULT  }, .unit = "avdiscard" },
227
        { "noref",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONREF   }, .unit = "avdiscard" },
228
        { "bidir",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_BIDIR    }, .unit = "avdiscard" },
229
        { "nointra",            .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONINTRA }, .unit = "avdiscard" },
230
        { "nokey",              .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONKEY   }, .unit = "avdiscard" },
231
        { "all",                .type = AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_ALL      }, .unit = "avdiscard" },
232
    { NULL }
233
};
234
235
static const AVClass stream_class = {
236
    .class_name     = "AVStream",
237
    .item_name      = av_default_item_name,
238
    .version        = LIBAVUTIL_VERSION_INT,
239
    .option         = stream_options,
240
};
241
242
const AVClass *av_stream_get_class(void)
243
0
{
244
0
    return &stream_class;
245
0
}
246
247
AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
248
0
{
249
0
    FFStream *sti;
250
0
    AVStream *st;
251
0
    AVStream **streams;
252
253
0
    if (s->nb_streams >= s->max_streams) {
254
0
        av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
255
0
               " (%d), see the documentation if you wish to increase it\n",
256
0
               s->max_streams);
257
0
        return NULL;
258
0
    }
259
0
    streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
260
0
    if (!streams)
261
0
        return NULL;
262
0
    s->streams = streams;
263
264
0
    sti = av_mallocz(sizeof(*sti));
265
0
    if (!sti)
266
0
        return NULL;
267
0
    st = &sti->pub;
268
269
0
    st->av_class = &stream_class;
270
0
    st->codecpar = avcodec_parameters_alloc();
271
0
    if (!st->codecpar)
272
0
        goto fail;
273
274
0
    sti->fmtctx = s;
275
276
0
    sti->parse_pkt = av_packet_alloc();
277
0
    if (!sti->parse_pkt)
278
0
        goto fail;
279
280
0
    if (s->iformat) {
281
0
        sti->avctx = avcodec_alloc_context3(NULL);
282
0
        if (!sti->avctx)
283
0
            goto fail;
284
285
0
        sti->info = av_mallocz(sizeof(*sti->info));
286
0
        if (!sti->info)
287
0
            goto fail;
288
289
0
#if FF_API_R_FRAME_RATE
290
0
        sti->info->last_dts      = AV_NOPTS_VALUE;
291
0
#endif
292
0
        sti->info->fps_first_dts = AV_NOPTS_VALUE;
293
0
        sti->info->fps_last_dts  = AV_NOPTS_VALUE;
294
295
        /* default pts setting is MPEG-like */
296
0
        avpriv_set_pts_info(st, 33, 1, 90000);
297
        /* we set the current DTS to 0 so that formats without any timestamps
298
         * but durations get some timestamps, formats with some unknown
299
         * timestamps have their first few packets buffered and the
300
         * timestamps corrected before they are returned to the user */
301
0
        sti->cur_dts = RELATIVE_TS_BASE;
302
0
    } else {
303
0
        sti->cur_dts = AV_NOPTS_VALUE;
304
0
    }
305
306
0
    st->index      = s->nb_streams;
307
0
    st->start_time = AV_NOPTS_VALUE;
308
0
    st->duration   = AV_NOPTS_VALUE;
309
0
    sti->first_dts     = AV_NOPTS_VALUE;
310
0
    sti->probe_packets = s->max_probe_packets;
311
0
    sti->pts_wrap_reference = AV_NOPTS_VALUE;
312
0
    sti->pts_wrap_behavior  = AV_PTS_WRAP_IGNORE;
313
314
0
    sti->last_IP_pts = AV_NOPTS_VALUE;
315
0
    sti->last_dts_for_order_check = AV_NOPTS_VALUE;
316
0
    for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
317
0
        sti->pts_buffer[i] = AV_NOPTS_VALUE;
318
319
0
    st->sample_aspect_ratio = (AVRational) { 0, 1 };
320
321
0
    sti->need_context_update = 1;
322
323
0
    s->streams[s->nb_streams++] = st;
324
0
    return st;
325
0
fail:
326
0
    ff_free_stream(&st);
327
0
    return NULL;
328
0
}
329
330
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
331
#define OFFSET(x) offsetof(AVStreamGroupTileGrid, x)
332
static const AVOption tile_grid_options[] = {
333
    { "grid_size", "size of the output canvas", OFFSET(coded_width),
334
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
335
    { "output_size", "size of valid pixels in output image meant for presentation", OFFSET(width),
336
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
337
    { "background_color", "set a background color for unused pixels",
338
        OFFSET(background), AV_OPT_TYPE_COLOR, { .str = "black"}, 0, 0, FLAGS },
339
    { "horizontal_offset", NULL, OFFSET(horizontal_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
340
    { "vertical_offset",   NULL, OFFSET(vertical_offset),   AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
341
    { NULL },
342
};
343
#undef OFFSET
344
345
static const AVClass tile_grid_class = {
346
    .class_name = "AVStreamGroupTileGrid",
347
    .version    = LIBAVUTIL_VERSION_INT,
348
    .option     = tile_grid_options,
349
};
350
351
#define OFFSET(x) offsetof(AVStreamGroupTREF, x)
352
static const AVOption tref_options[] = {
353
    { "metadata_index", "Index of the data stream within the group", OFFSET(metadata_index),
354
        AV_OPT_TYPE_UINT, { .i64 = 0 }, 0, UINT_MAX, FLAGS },
355
    { NULL },
356
};
357
#undef OFFSET
358
359
static const AVClass tref_class = {
360
    .class_name = "AVStreamGroupTREF",
361
    .version    = LIBAVUTIL_VERSION_INT,
362
    .option     = tref_options,
363
};
364
365
#if FF_API_LCEVC_STRUCT
366
FF_DISABLE_DEPRECATION_WARNINGS
367
#endif
368
#define OFFSET(x) offsetof(AVStreamGroupLayeredVideo, x)
369
static const AVOption layered_video_options[] = {
370
#if FF_API_LCEVC_STRUCT
371
    { "lcevc_index", "Index of the LCEVC stream within the group", OFFSET(lcevc_index),
372
        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS | AV_OPT_FLAG_DEPRECATED },
373
#endif
374
    { "el_index", "Index of the enhancement layer stream within the group", OFFSET(el_index),
375
        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
376
    { "video_size", "size of the final layered video presentation", OFFSET(width),
377
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
378
    { NULL },
379
};
380
#if FF_API_LCEVC_STRUCT
381
FF_ENABLE_DEPRECATION_WARNINGS
382
#endif
383
#undef OFFSET
384
385
static const AVClass layered_video_class = {
386
    .class_name = "AVStreamGroupLayeredVideo",
387
    .version    = LIBAVUTIL_VERSION_INT,
388
    .option     = layered_video_options,
389
};
390
391
static void *stream_group_child_next(void *obj, void *prev)
392
0
{
393
0
    AVStreamGroup *stg = obj;
394
0
    if (!prev) {
395
0
        switch(stg->type) {
396
0
        case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
397
0
            return stg->params.iamf_audio_element;
398
0
        case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
399
0
            return stg->params.iamf_mix_presentation;
400
0
        case AV_STREAM_GROUP_PARAMS_TILE_GRID:
401
0
            return stg->params.tile_grid;
402
0
        case AV_STREAM_GROUP_PARAMS_TREF:
403
0
            return stg->params.tref;
404
0
        case AV_STREAM_GROUP_PARAMS_LCEVC:
405
0
        case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
406
0
        case AV_STREAM_GROUP_PARAMS_GAIN_MAP:
407
0
        case AV_STREAM_GROUP_PARAMS_ALPHA:
408
0
            return stg->params.layered_video;
409
0
        default:
410
0
            break;
411
0
        }
412
0
    }
413
0
    return NULL;
414
0
}
415
416
#undef FLAGS
417
418
static const AVClass *stream_group_child_iterate(void **opaque)
419
0
{
420
0
    uintptr_t i = (uintptr_t)*opaque;
421
0
    const AVClass *ret = NULL;
422
423
0
    switch(i) {
424
0
    case AV_STREAM_GROUP_PARAMS_NONE:
425
0
        i++;
426
0
        av_fallthrough;
427
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
428
0
        ret = av_iamf_audio_element_get_class();
429
0
        break;
430
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
431
0
        ret = av_iamf_mix_presentation_get_class();
432
0
        break;
433
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
434
0
        ret = &tile_grid_class;
435
0
        break;
436
0
    case AV_STREAM_GROUP_PARAMS_TREF:
437
0
        ret = &tref_class;
438
0
        break;
439
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
440
0
    case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
441
0
    case AV_STREAM_GROUP_PARAMS_GAIN_MAP:
442
0
    case AV_STREAM_GROUP_PARAMS_ALPHA:
443
0
        ret = &layered_video_class;
444
0
        break;
445
0
    default:
446
0
        break;
447
0
    }
448
449
0
    if (ret)
450
0
        *opaque = (void*)(i + 1);
451
0
    return ret;
452
0
}
453
454
static const AVOption stream_group_options[] = {
455
    DISPOSITION_OPT(AVStreamGroup),
456
    {"id", "Set group id", offsetof(AVStreamGroup, id), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
457
    { NULL }
458
};
459
460
static const AVClass stream_group_class = {
461
    .class_name     = "AVStreamGroup",
462
    .item_name      = av_default_item_name,
463
    .version        = LIBAVUTIL_VERSION_INT,
464
    .option         = stream_group_options,
465
    .child_next     = stream_group_child_next,
466
    .child_class_iterate = stream_group_child_iterate,
467
};
468
469
const AVClass *av_stream_group_get_class(void)
470
0
{
471
0
    return &stream_group_class;
472
0
}
473
474
AVStreamGroup *avformat_stream_group_create(AVFormatContext *s,
475
                                            enum AVStreamGroupParamsType type,
476
                                            AVDictionary **options)
477
0
{
478
0
    AVStreamGroup **stream_groups;
479
0
    AVStreamGroup *stg;
480
0
    FFStreamGroup *stgi;
481
482
0
    stream_groups = av_realloc_array(s->stream_groups, s->nb_stream_groups + 1,
483
0
                                     sizeof(*stream_groups));
484
0
    if (!stream_groups)
485
0
        return NULL;
486
0
    s->stream_groups = stream_groups;
487
488
0
    stgi = av_mallocz(sizeof(*stgi));
489
0
    if (!stgi)
490
0
        return NULL;
491
0
    stg = &stgi->pub;
492
493
0
    stg->av_class = &stream_group_class;
494
0
    av_opt_set_defaults(stg);
495
0
    switch (type) {
496
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
497
0
        stg->params.iamf_audio_element = av_iamf_audio_element_alloc();
498
0
        if (!stg->params.iamf_audio_element)
499
0
            goto fail;
500
0
        break;
501
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
502
0
        stg->params.iamf_mix_presentation = av_iamf_mix_presentation_alloc();
503
0
        if (!stg->params.iamf_mix_presentation)
504
0
            goto fail;
505
0
        break;
506
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
507
0
        stg->params.tile_grid = av_mallocz(sizeof(*stg->params.tile_grid));
508
0
        if (!stg->params.tile_grid)
509
0
            goto fail;
510
0
        stg->params.tile_grid->av_class = &tile_grid_class;
511
0
        av_opt_set_defaults(stg->params.tile_grid);
512
0
        break;
513
0
    case AV_STREAM_GROUP_PARAMS_TREF:
514
0
        stg->params.tref = av_mallocz(sizeof(*stg->params.tref));
515
0
        if (!stg->params.tref)
516
0
            goto fail;
517
0
        stg->params.tref->av_class = &tref_class;
518
0
        av_opt_set_defaults(stg->params.tref);
519
0
        break;
520
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
521
0
    case AV_STREAM_GROUP_PARAMS_DOLBY_VISION:
522
0
    case AV_STREAM_GROUP_PARAMS_GAIN_MAP:
523
0
    case AV_STREAM_GROUP_PARAMS_ALPHA:
524
0
        stg->params.layered_video = av_mallocz(sizeof(*stg->params.layered_video));
525
0
        if (!stg->params.layered_video)
526
0
            goto fail;
527
0
        stg->params.layered_video->av_class = &layered_video_class;
528
0
        av_opt_set_defaults(stg->params.layered_video);
529
0
        break;
530
0
    default:
531
0
        goto fail;
532
0
    }
533
0
    stg->type = type;
534
535
0
    if (options) {
536
0
        if (av_opt_set_dict2(stg, options, AV_OPT_SEARCH_CHILDREN))
537
0
            goto fail;
538
0
    }
539
540
0
    stgi->fmtctx = s;
541
0
    stg->index   = s->nb_stream_groups;
542
543
0
    s->stream_groups[s->nb_stream_groups++] = stg;
544
545
0
    return stg;
546
0
fail:
547
0
    ff_free_stream_group(&stg);
548
0
    return NULL;
549
0
}
550
551
static int stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
552
0
{
553
0
#if FF_API_STREAM_GROUP_ENTRIES
554
0
FF_DISABLE_DEPRECATION_WARNINGS
555
0
    AVStream **streams = av_realloc_array(stg->streams, stg->nb_streams + 1,
556
0
                                          sizeof(*stg->streams));
557
0
    if (!streams)
558
0
        return AVERROR(ENOMEM);
559
560
0
    stg->streams = streams;
561
0
FF_ENABLE_DEPRECATION_WARNINGS
562
0
#endif
563
564
0
    AVStreamGroupEntry **entries = av_realloc_array(stg->entries, stg->nb_entries + 1,
565
0
                                                    sizeof(*stg->entries));
566
0
    if (!entries)
567
0
        return AVERROR(ENOMEM);
568
569
0
    stg->entries = entries;
570
0
    stg->entries[stg->nb_entries] = av_mallocz(sizeof(*stg->entries[stg->nb_entries]));
571
0
    if (!stg->entries[stg->nb_entries])
572
0
        return AVERROR(ENOMEM);
573
574
0
    stg->entries[stg->nb_entries]->type = AV_STREAM_GROUP_ENTRY_STREAM;
575
0
    stg->entries[stg->nb_entries++]->stream = st;
576
577
0
#if FF_API_STREAM_GROUP_ENTRIES
578
0
FF_DISABLE_DEPRECATION_WARNINGS
579
0
    if (stg->nb_streams == (stg->nb_entries - 1))
580
0
        stg->streams[stg->nb_streams++] = st;
581
0
    else
582
0
        stg->nb_streams = 0;
583
0
FF_ENABLE_DEPRECATION_WARNINGS
584
0
#endif
585
586
0
    return 0;
587
0
}
588
589
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
590
0
{
591
0
    const FFStreamGroup *stgi = cffstreamgroup(stg);
592
0
    const FFStream *sti = cffstream(st);
593
594
0
    if (stgi->fmtctx != sti->fmtctx)
595
0
        return AVERROR(EINVAL);
596
597
0
    for (int i = 0; i < stg->nb_entries; i++) {
598
0
        const AVStreamGroupEntry *entry = stg->entries[i];
599
0
        if (entry->type != AV_STREAM_GROUP_ENTRY_STREAM)
600
0
            continue;
601
0
        if (entry->stream->index == st->index)
602
0
            return AVERROR(EEXIST);
603
0
    }
604
605
0
    return stream_group_add_stream(stg, st);
606
0
}
607
608
static int stream_group_add_stream_group(AVStreamGroup *stg, AVStreamGroup *group)
609
0
{
610
0
    AVStreamGroupEntry **entries = av_realloc_array(stg->entries, stg->nb_entries + 1,
611
0
                                                    sizeof(*stg->entries));
612
0
    if (!entries)
613
0
        return AVERROR(ENOMEM);
614
615
0
    stg->entries = entries;
616
0
    stg->entries[stg->nb_entries] = av_mallocz(sizeof(*stg->entries[stg->nb_entries]));
617
0
    if (!stg->entries[stg->nb_entries])
618
0
        return AVERROR(ENOMEM);
619
620
0
    stg->entries[stg->nb_entries]->type = AV_STREAM_GROUP_ENTRY_STREAM_GROUP;
621
0
    stg->entries[stg->nb_entries++]->stream_group = group;
622
0
#if FF_API_STREAM_GROUP_ENTRIES
623
0
FF_DISABLE_DEPRECATION_WARNINGS
624
0
    stg->nb_streams = 0;
625
0
FF_ENABLE_DEPRECATION_WARNINGS
626
0
#endif
627
628
0
    return 0;
629
0
}
630
631
int avformat_stream_group_add_stream_group(AVStreamGroup *stg, AVStreamGroup *group)
632
0
{
633
0
    const FFStreamGroup *stgi = cffstreamgroup(stg);
634
0
    const FFStreamGroup *groupi = cffstreamgroup(group);
635
636
0
    if (stgi->fmtctx != groupi->fmtctx)
637
0
        return AVERROR(EINVAL);
638
639
0
    for (int i = 0; i < stg->nb_entries; i++) {
640
0
        const AVStreamGroupEntry *entry = stg->entries[i];
641
0
        if (entry->type != AV_STREAM_GROUP_ENTRY_STREAM_GROUP)
642
0
            continue;
643
0
        if (entry->stream_group->index == group->index)
644
0
            return AVERROR(EEXIST);
645
0
    }
646
647
0
    return stream_group_add_stream_group(stg, group);
648
0
}
649
650
static int option_is_disposition(const AVOption *opt)
651
0
{
652
0
    return opt->type == AV_OPT_TYPE_CONST &&
653
0
           opt->unit && !strcmp(opt->unit, "disposition");
654
0
}
655
656
int av_disposition_from_string(const char *disp)
657
0
{
658
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
659
0
        if (option_is_disposition(opt) && !strcmp(disp, opt->name))
660
0
            return opt->default_val.i64;
661
0
    return AVERROR(EINVAL);
662
0
}
663
664
const char *av_disposition_to_string(int disposition)
665
0
{
666
0
    int val;
667
668
0
    if (disposition <= 0)
669
0
        return NULL;
670
671
0
    val = 1 << ff_ctz(disposition);
672
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
673
0
        if (option_is_disposition(opt) && opt->default_val.i64 == val)
674
0
            return opt->name;
675
676
0
    return NULL;
677
0
}