Coverage Report

Created: 2026-04-29 07:00

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.52k
{
144
1.52k
    int loglevel;
145
146
1.52k
    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.52k
    ) {
150
1.52k
        loglevel = AV_LOG_DEBUG;
151
1.52k
    } else
152
0
        loglevel = AV_LOG_INFO;
153
154
1.52k
    av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
155
156
1.52k
    return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
157
1.52k
}
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.52k
{
166
1.52k
    FormatContextInternal *fci;
167
1.52k
    FFFormatContext *si;
168
1.52k
    AVFormatContext *s;
169
170
1.52k
    fci = av_mallocz(sizeof(*fci));
171
1.52k
    if (!fci)
172
0
        return NULL;
173
174
1.52k
    si = &fci->fc;
175
1.52k
    s = &si->pub;
176
1.52k
    s->av_class = &av_format_context_class;
177
1.52k
    s->io_open  = io_open_default;
178
1.52k
    s->io_close2= io_close2_default;
179
180
1.52k
    av_opt_set_defaults(s);
181
182
1.52k
    si->pkt = av_packet_alloc();
183
1.52k
    si->parse_pkt = av_packet_alloc();
184
1.52k
    if (!si->pkt || !si->parse_pkt) {
185
0
        avformat_free_context(s);
186
0
        return NULL;
187
0
    }
188
189
1.52k
    return s;
190
1.52k
}
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
0
#if FF_API_INTERNAL_TIMING
320
0
    sti->transferred_mux_tb = (AVRational) { 0, 1 };;
321
0
#endif
322
323
0
    sti->need_context_update = 1;
324
325
0
    s->streams[s->nb_streams++] = st;
326
0
    return st;
327
0
fail:
328
0
    ff_free_stream(&st);
329
0
    return NULL;
330
0
}
331
332
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
333
#define OFFSET(x) offsetof(AVStreamGroupTileGrid, x)
334
static const AVOption tile_grid_options[] = {
335
    { "grid_size", "size of the output canvas", OFFSET(coded_width),
336
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
337
    { "output_size", "size of valid pixels in output image meant for presentation", OFFSET(width),
338
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
339
    { "background_color", "set a background color for unused pixels",
340
        OFFSET(background), AV_OPT_TYPE_COLOR, { .str = "black"}, 0, 0, FLAGS },
341
    { "horizontal_offset", NULL, OFFSET(horizontal_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
342
    { "vertical_offset",   NULL, OFFSET(vertical_offset),   AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
343
    { NULL },
344
};
345
#undef OFFSET
346
347
static const AVClass tile_grid_class = {
348
    .class_name = "AVStreamGroupTileGrid",
349
    .version    = LIBAVUTIL_VERSION_INT,
350
    .option     = tile_grid_options,
351
};
352
353
#define OFFSET(x) offsetof(AVStreamGroupLCEVC, x)
354
static const AVOption lcevc_options[] = {
355
    { "lcevc_index", "Index of the LCEVC stream within the group", OFFSET(lcevc_index),
356
        AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
357
    { "video_size", "size of video after LCEVC enhancement has been applied", OFFSET(width),
358
        AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, FLAGS },
359
    { NULL },
360
};
361
#undef OFFSET
362
363
static const AVClass lcevc_class = {
364
    .class_name = "AVStreamGroupLCEVC",
365
    .version    = LIBAVUTIL_VERSION_INT,
366
    .option     = lcevc_options,
367
};
368
369
static void *stream_group_child_next(void *obj, void *prev)
370
0
{
371
0
    AVStreamGroup *stg = obj;
372
0
    if (!prev) {
373
0
        switch(stg->type) {
374
0
        case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
375
0
            return stg->params.iamf_audio_element;
376
0
        case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
377
0
            return stg->params.iamf_mix_presentation;
378
0
        case AV_STREAM_GROUP_PARAMS_TILE_GRID:
379
0
            return stg->params.tile_grid;
380
0
        case AV_STREAM_GROUP_PARAMS_LCEVC:
381
0
            return stg->params.lcevc;
382
0
        default:
383
0
            break;
384
0
        }
385
0
    }
386
0
    return NULL;
387
0
}
388
389
#undef FLAGS
390
391
static const AVClass *stream_group_child_iterate(void **opaque)
392
0
{
393
0
    uintptr_t i = (uintptr_t)*opaque;
394
0
    const AVClass *ret = NULL;
395
396
0
    switch(i) {
397
0
    case AV_STREAM_GROUP_PARAMS_NONE:
398
0
        i++;
399
0
        av_fallthrough;
400
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
401
0
        ret = av_iamf_audio_element_get_class();
402
0
        break;
403
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
404
0
        ret = av_iamf_mix_presentation_get_class();
405
0
        break;
406
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
407
0
        ret = &tile_grid_class;
408
0
        break;
409
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
410
0
        ret = &lcevc_class;
411
0
        break;
412
0
    default:
413
0
        break;
414
0
    }
415
416
0
    if (ret)
417
0
        *opaque = (void*)(i + 1);
418
0
    return ret;
419
0
}
420
421
static const AVOption stream_group_options[] = {
422
    DISPOSITION_OPT(AVStreamGroup),
423
    {"id", "Set group id", offsetof(AVStreamGroup, id), AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM },
424
    { NULL }
425
};
426
427
static const AVClass stream_group_class = {
428
    .class_name     = "AVStreamGroup",
429
    .item_name      = av_default_item_name,
430
    .version        = LIBAVUTIL_VERSION_INT,
431
    .option         = stream_group_options,
432
    .child_next     = stream_group_child_next,
433
    .child_class_iterate = stream_group_child_iterate,
434
};
435
436
const AVClass *av_stream_group_get_class(void)
437
0
{
438
0
    return &stream_group_class;
439
0
}
440
441
AVStreamGroup *avformat_stream_group_create(AVFormatContext *s,
442
                                            enum AVStreamGroupParamsType type,
443
                                            AVDictionary **options)
444
0
{
445
0
    AVStreamGroup **stream_groups;
446
0
    AVStreamGroup *stg;
447
0
    FFStreamGroup *stgi;
448
449
0
    stream_groups = av_realloc_array(s->stream_groups, s->nb_stream_groups + 1,
450
0
                                     sizeof(*stream_groups));
451
0
    if (!stream_groups)
452
0
        return NULL;
453
0
    s->stream_groups = stream_groups;
454
455
0
    stgi = av_mallocz(sizeof(*stgi));
456
0
    if (!stgi)
457
0
        return NULL;
458
0
    stg = &stgi->pub;
459
460
0
    stg->av_class = &stream_group_class;
461
0
    av_opt_set_defaults(stg);
462
0
    stg->type = type;
463
0
    switch (type) {
464
0
    case AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT:
465
0
        stg->params.iamf_audio_element = av_iamf_audio_element_alloc();
466
0
        if (!stg->params.iamf_audio_element)
467
0
            goto fail;
468
0
        break;
469
0
    case AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION:
470
0
        stg->params.iamf_mix_presentation = av_iamf_mix_presentation_alloc();
471
0
        if (!stg->params.iamf_mix_presentation)
472
0
            goto fail;
473
0
        break;
474
0
    case AV_STREAM_GROUP_PARAMS_TILE_GRID:
475
0
        stg->params.tile_grid = av_mallocz(sizeof(*stg->params.tile_grid));
476
0
        if (!stg->params.tile_grid)
477
0
            goto fail;
478
0
        stg->params.tile_grid->av_class = &tile_grid_class;
479
0
        av_opt_set_defaults(stg->params.tile_grid);
480
0
        break;
481
0
    case AV_STREAM_GROUP_PARAMS_LCEVC:
482
0
        stg->params.lcevc = av_mallocz(sizeof(*stg->params.lcevc));
483
0
        if (!stg->params.lcevc)
484
0
            goto fail;
485
0
        stg->params.lcevc->av_class = &lcevc_class;
486
0
        av_opt_set_defaults(stg->params.lcevc);
487
0
        break;
488
0
    default:
489
0
        goto fail;
490
0
    }
491
492
0
    if (options) {
493
0
        if (av_opt_set_dict2(stg, options, AV_OPT_SEARCH_CHILDREN))
494
0
            goto fail;
495
0
    }
496
497
0
    stgi->fmtctx = s;
498
0
    stg->index   = s->nb_stream_groups;
499
500
0
    s->stream_groups[s->nb_stream_groups++] = stg;
501
502
0
    return stg;
503
0
fail:
504
0
    ff_free_stream_group(&stg);
505
0
    return NULL;
506
0
}
507
508
static int stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
509
0
{
510
0
    AVStream **streams = av_realloc_array(stg->streams, stg->nb_streams + 1,
511
0
                                          sizeof(*stg->streams));
512
0
    if (!streams)
513
0
        return AVERROR(ENOMEM);
514
515
0
    stg->streams = streams;
516
0
    stg->streams[stg->nb_streams++] = st;
517
518
0
    return 0;
519
0
}
520
521
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
522
0
{
523
0
    const FFStreamGroup *stgi = cffstreamgroup(stg);
524
0
    const FFStream *sti = cffstream(st);
525
526
0
    if (stgi->fmtctx != sti->fmtctx)
527
0
        return AVERROR(EINVAL);
528
529
0
    for (int i = 0; i < stg->nb_streams; i++)
530
0
        if (stg->streams[i]->index == st->index)
531
0
            return AVERROR(EEXIST);
532
533
0
    return stream_group_add_stream(stg, st);
534
0
}
535
536
static int option_is_disposition(const AVOption *opt)
537
0
{
538
0
    return opt->type == AV_OPT_TYPE_CONST &&
539
0
           opt->unit && !strcmp(opt->unit, "disposition");
540
0
}
541
542
int av_disposition_from_string(const char *disp)
543
0
{
544
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
545
0
        if (option_is_disposition(opt) && !strcmp(disp, opt->name))
546
0
            return opt->default_val.i64;
547
0
    return AVERROR(EINVAL);
548
0
}
549
550
const char *av_disposition_to_string(int disposition)
551
0
{
552
0
    int val;
553
554
0
    if (disposition <= 0)
555
0
        return NULL;
556
557
0
    val = 1 << ff_ctz(disposition);
558
0
    for (const AVOption *opt = stream_options; opt->name; opt++)
559
0
        if (option_is_disposition(opt) && opt->default_val.i64 == val)
560
0
            return opt->name;
561
562
0
    return NULL;
563
0
}