]> git.sesse.net Git - ffmpeg/blob - libavformat/options.c
examples/decode_video: switch to the new decoding API
[ffmpeg] / libavformat / options.c
1 /*
2  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 "internal.h"
23 #include "url.h"
24
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27
28 /**
29  * @file
30  * Options definition for AVFormatContext.
31  */
32
33 FF_DISABLE_DEPRECATION_WARNINGS
34 #include "options_table.h"
35 FF_ENABLE_DEPRECATION_WARNINGS
36
37 static const char* format_to_name(void* ptr)
38 {
39     AVFormatContext* fc = (AVFormatContext*) ptr;
40     if(fc->iformat) return fc->iformat->name;
41     else if(fc->oformat) return fc->oformat->name;
42     else return "NULL";
43 }
44
45 static void *format_child_next(void *obj, void *prev)
46 {
47     AVFormatContext *s = obj;
48     if (!prev && s->priv_data &&
49         ((s->iformat && s->iformat->priv_class) ||
50           s->oformat && s->oformat->priv_class))
51         return s->priv_data;
52     if (s->pb && s->pb->av_class && prev != s->pb)
53         return s->pb;
54     return NULL;
55 }
56
57 static const AVClass *format_child_class_next(const AVClass *prev)
58 {
59     AVInputFormat  *ifmt = NULL;
60     AVOutputFormat *ofmt = NULL;
61
62     if (!prev)
63         return &ff_avio_class;
64
65     while ((ifmt = av_iformat_next(ifmt)))
66         if (ifmt->priv_class == prev)
67             break;
68
69     if (!ifmt)
70         while ((ofmt = av_oformat_next(ofmt)))
71             if (ofmt->priv_class == prev)
72                 break;
73     if (!ofmt)
74         while (ifmt = av_iformat_next(ifmt))
75             if (ifmt->priv_class)
76                 return ifmt->priv_class;
77
78     while (ofmt = av_oformat_next(ofmt))
79         if (ofmt->priv_class)
80             return ofmt->priv_class;
81
82     return NULL;
83 }
84
85 static const AVClass av_format_context_class = {
86     .class_name     = "AVFormatContext",
87     .item_name      = format_to_name,
88     .option         = avformat_options,
89     .version        = LIBAVUTIL_VERSION_INT,
90     .child_next     = format_child_next,
91     .child_class_next = format_child_class_next,
92 };
93
94 static int io_open_default(AVFormatContext *s, AVIOContext **pb,
95                            const char *url, int flags, AVDictionary **options)
96 {
97     AVDictionary *opts_local = NULL;
98     int ret;
99
100     if (!options)
101         options = &opts_local;
102
103     if (s->protocol_whitelist) {
104         ret = av_dict_set(options, "protocol_whitelist", s->protocol_whitelist, 0);
105         if (ret < 0)
106             goto finish;
107     }
108     if (s->protocol_blacklist) {
109         ret = av_dict_set(options, "protocol_blacklist", s->protocol_blacklist, 0);
110         if (ret < 0)
111             goto finish;
112     }
113     ret = avio_open2(pb, url, flags, &s->interrupt_callback, options);
114 finish:
115     av_dict_free(&opts_local);
116     return ret;
117 }
118
119 static void io_close_default(AVFormatContext *s, AVIOContext *pb)
120 {
121     avio_close(pb);
122 }
123
124 static void avformat_get_context_defaults(AVFormatContext *s)
125 {
126     memset(s, 0, sizeof(AVFormatContext));
127
128     s->av_class = &av_format_context_class;
129
130     s->io_open  = io_open_default;
131     s->io_close = io_close_default;
132
133     av_opt_set_defaults(s);
134 }
135
136 AVFormatContext *avformat_alloc_context(void)
137 {
138     AVFormatContext *ic;
139     ic = av_malloc(sizeof(AVFormatContext));
140     if (!ic) return ic;
141     avformat_get_context_defaults(ic);
142
143     ic->internal = av_mallocz(sizeof(*ic->internal));
144     if (!ic->internal) {
145         avformat_free_context(ic);
146         return NULL;
147     }
148     ic->internal->offset = AV_NOPTS_VALUE;
149
150     return ic;
151 }
152
153 const AVClass *avformat_get_class(void)
154 {
155     return &av_format_context_class;
156 }