xref: /third_party/ffmpeg/libavformat/options.c (revision cabdff1a)
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 "avio_internal.h"
22#include "demux.h"
23#include "internal.h"
24
25#include "libavcodec/avcodec.h"
26#include "libavcodec/codec_par.h"
27
28#include "libavutil/avassert.h"
29#include "libavutil/internal.h"
30#include "libavutil/intmath.h"
31#include "libavutil/opt.h"
32
33/**
34 * @file
35 * Options definition for AVFormatContext.
36 */
37
38FF_DISABLE_DEPRECATION_WARNINGS
39#include "options_table.h"
40FF_ENABLE_DEPRECATION_WARNINGS
41
42static const char* format_to_name(void* ptr)
43{
44    AVFormatContext* fc = (AVFormatContext*) ptr;
45    if(fc->iformat) return fc->iformat->name;
46    else if(fc->oformat) return fc->oformat->name;
47    else return "NULL";
48}
49
50static void *format_child_next(void *obj, void *prev)
51{
52    AVFormatContext *s = obj;
53    if (!prev && s->priv_data &&
54        ((s->iformat && s->iformat->priv_class) ||
55          s->oformat && s->oformat->priv_class))
56        return s->priv_data;
57    if (s->pb && s->pb->av_class && prev != s->pb)
58        return s->pb;
59    return NULL;
60}
61
62enum {
63    CHILD_CLASS_ITER_AVIO = 0,
64    CHILD_CLASS_ITER_MUX,
65    CHILD_CLASS_ITER_DEMUX,
66    CHILD_CLASS_ITER_DONE,
67
68};
69
70#define ITER_STATE_SHIFT 16
71
72static const AVClass *format_child_class_iterate(void **iter)
73{
74    // we use the low 16 bits of iter as the value to be passed to
75    // av_(de)muxer_iterate()
76    void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
77    unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
78    const AVClass *ret = NULL;
79
80    if (state == CHILD_CLASS_ITER_AVIO) {
81        ret = &ff_avio_class;
82        state++;
83        goto finish;
84    }
85
86    if (state == CHILD_CLASS_ITER_MUX) {
87        const AVOutputFormat *ofmt;
88
89        while ((ofmt = av_muxer_iterate(&val))) {
90            ret = ofmt->priv_class;
91            if (ret)
92                goto finish;
93        }
94
95        val = NULL;
96        state++;
97    }
98
99    if (state == CHILD_CLASS_ITER_DEMUX) {
100        const AVInputFormat *ifmt;
101
102        while ((ifmt = av_demuxer_iterate(&val))) {
103            ret = ifmt->priv_class;
104            if (ret)
105                goto finish;
106        }
107        val = NULL;
108        state++;
109    }
110
111finish:
112    // make sure none av_(de)muxer_iterate does not set the high bits of val
113    av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
114    *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
115    return ret;
116}
117
118static AVClassCategory get_category(void *ptr)
119{
120    AVFormatContext* s = ptr;
121    if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
122    else           return AV_CLASS_CATEGORY_MUXER;
123}
124
125static const AVClass av_format_context_class = {
126    .class_name     = "AVFormatContext",
127    .item_name      = format_to_name,
128    .option         = avformat_options,
129    .version        = LIBAVUTIL_VERSION_INT,
130    .child_next     = format_child_next,
131    .child_class_iterate = format_child_class_iterate,
132    .category       = AV_CLASS_CATEGORY_MUXER,
133    .get_category   = get_category,
134};
135
136static int io_open_default(AVFormatContext *s, AVIOContext **pb,
137                           const char *url, int flags, AVDictionary **options)
138{
139    int loglevel;
140
141    if (!strcmp(url, s->url) ||
142        s->iformat && !strcmp(s->iformat->name, "image2") ||
143        s->oformat && !strcmp(s->oformat->name, "image2")
144    ) {
145        loglevel = AV_LOG_DEBUG;
146    } else
147        loglevel = AV_LOG_INFO;
148
149    av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
150
151    return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
152}
153
154void ff_format_io_close_default(AVFormatContext *s, AVIOContext *pb)
155{
156    avio_close(pb);
157}
158
159static int io_close2_default(AVFormatContext *s, AVIOContext *pb)
160{
161    return avio_close(pb);
162}
163
164AVFormatContext *avformat_alloc_context(void)
165{
166    FFFormatContext *const si = av_mallocz(sizeof(*si));
167    AVFormatContext *s;
168
169    if (!si)
170        return NULL;
171
172    s = &si->pub;
173    s->av_class = &av_format_context_class;
174    s->io_open  = io_open_default;
175    s->io_close = ff_format_io_close_default;
176    s->io_close2= io_close2_default;
177
178    av_opt_set_defaults(s);
179
180    si->pkt = av_packet_alloc();
181    si->parse_pkt = av_packet_alloc();
182    if (!si->pkt || !si->parse_pkt) {
183        avformat_free_context(s);
184        return NULL;
185    }
186
187    si->shortest_end = AV_NOPTS_VALUE;
188
189    return s;
190}
191
192enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
193{
194    return ctx->duration_estimation_method;
195}
196
197const AVClass *avformat_get_class(void)
198{
199    return &av_format_context_class;
200}
201
202static const AVOption stream_options[] = {
203    { "disposition", NULL, offsetof(AVStream, disposition), AV_OPT_TYPE_FLAGS, { .i64 = 0 },
204        .flags = AV_OPT_FLAG_ENCODING_PARAM, .unit = "disposition" },
205        { "default",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEFAULT           },    .unit = "disposition" },
206        { "dub",                .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DUB               },    .unit = "disposition" },
207        { "original",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ORIGINAL          },    .unit = "disposition" },
208        { "comment",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_COMMENT           },    .unit = "disposition" },
209        { "lyrics",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_LYRICS            },    .unit = "disposition" },
210        { "karaoke",            .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_KARAOKE           },    .unit = "disposition" },
211        { "forced",             .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_FORCED            },    .unit = "disposition" },
212        { "hearing_impaired",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_HEARING_IMPAIRED  },    .unit = "disposition" },
213        { "visual_impaired",    .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_VISUAL_IMPAIRED   },    .unit = "disposition" },
214        { "clean_effects",      .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CLEAN_EFFECTS     },    .unit = "disposition" },
215        { "attached_pic",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_ATTACHED_PIC      },    .unit = "disposition" },
216        { "timed_thumbnails",   .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_TIMED_THUMBNAILS  },    .unit = "disposition" },
217        { "captions",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_CAPTIONS          },    .unit = "disposition" },
218        { "descriptions",       .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DESCRIPTIONS      },    .unit = "disposition" },
219        { "metadata",           .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_METADATA          },    .unit = "disposition" },
220        { "dependent",          .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_DEPENDENT         },    .unit = "disposition" },
221        { "still_image",        .type = AV_OPT_TYPE_CONST, { .i64 = AV_DISPOSITION_STILL_IMAGE       },    .unit = "disposition" },
222    { NULL }
223};
224
225static const AVClass stream_class = {
226    .class_name     = "AVStream",
227    .item_name      = av_default_item_name,
228    .version        = LIBAVUTIL_VERSION_INT,
229    .option         = stream_options,
230};
231
232const AVClass *av_stream_get_class(void)
233{
234    return &stream_class;
235}
236
237AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
238{
239    FFFormatContext *const si = ffformatcontext(s);
240    FFStream *sti;
241    AVStream *st;
242    AVStream **streams;
243
244    if (s->nb_streams >= s->max_streams) {
245        av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter"
246               " (%d), see the documentation if you wish to increase it\n",
247               s->max_streams);
248        return NULL;
249    }
250    streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
251    if (!streams)
252        return NULL;
253    s->streams = streams;
254
255    sti = av_mallocz(sizeof(*sti));
256    if (!sti)
257        return NULL;
258    st = &sti->pub;
259
260#if FF_API_AVSTREAM_CLASS
261    st->av_class = &stream_class;
262#endif
263
264    st->codecpar = avcodec_parameters_alloc();
265    if (!st->codecpar)
266        goto fail;
267
268    sti->avctx = avcodec_alloc_context3(NULL);
269    if (!sti->avctx)
270        goto fail;
271
272    if (s->iformat) {
273        sti->info = av_mallocz(sizeof(*sti->info));
274        if (!sti->info)
275            goto fail;
276
277#if FF_API_R_FRAME_RATE
278        sti->info->last_dts      = AV_NOPTS_VALUE;
279#endif
280        sti->info->fps_first_dts = AV_NOPTS_VALUE;
281        sti->info->fps_last_dts  = AV_NOPTS_VALUE;
282
283        /* default pts setting is MPEG-like */
284        avpriv_set_pts_info(st, 33, 1, 90000);
285        /* we set the current DTS to 0 so that formats without any timestamps
286         * but durations get some timestamps, formats with some unknown
287         * timestamps have their first few packets buffered and the
288         * timestamps corrected before they are returned to the user */
289        sti->cur_dts = RELATIVE_TS_BASE;
290    } else {
291        sti->cur_dts = AV_NOPTS_VALUE;
292    }
293
294    st->index      = s->nb_streams;
295    st->start_time = AV_NOPTS_VALUE;
296    st->duration   = AV_NOPTS_VALUE;
297    sti->first_dts     = AV_NOPTS_VALUE;
298
299#ifdef OHOS_EXPAND_MP4_INFO
300    st->stts_count = 0;
301    st->stts_data = NULL;
302    st->ctts_count = 0;
303    st->ctts_data = NULL;
304    st->time_scale = 0;
305#endif
306
307    sti->probe_packets = s->max_probe_packets;
308    sti->pts_wrap_reference = AV_NOPTS_VALUE;
309    sti->pts_wrap_behavior  = AV_PTS_WRAP_IGNORE;
310
311    sti->last_IP_pts = AV_NOPTS_VALUE;
312    sti->last_dts_for_order_check = AV_NOPTS_VALUE;
313    for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
314        sti->pts_buffer[i] = AV_NOPTS_VALUE;
315
316    st->sample_aspect_ratio = (AVRational) { 0, 1 };
317
318    sti->inject_global_side_data = si->inject_global_side_data;
319
320    sti->need_context_update = 1;
321
322    s->streams[s->nb_streams++] = st;
323    return st;
324fail:
325    ff_free_stream(&st);
326    return NULL;
327}
328
329static int option_is_disposition(const AVOption *opt)
330{
331    return opt->type == AV_OPT_TYPE_CONST &&
332           opt->unit && !strcmp(opt->unit, "disposition");
333}
334
335int av_disposition_from_string(const char *disp)
336{
337    for (const AVOption *opt = stream_options; opt->name; opt++)
338        if (option_is_disposition(opt) && !strcmp(disp, opt->name))
339            return opt->default_val.i64;
340    return AVERROR(EINVAL);
341}
342
343const char *av_disposition_to_string(int disposition)
344{
345    int val;
346
347    if (disposition <= 0)
348        return NULL;
349
350    val = 1 << ff_ctz(disposition);
351    for (const AVOption *opt = stream_options; opt->name; opt++)
352        if (option_is_disposition(opt) && opt->default_val.i64 == val)
353            return opt->name;
354
355    return NULL;
356}
357