Coverage Report

Created: 2026-03-12 07:14

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