Coverage Report

Created: 2026-01-25 07:18

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