]> git.sesse.net Git - ffmpeg/blob - libavformat/options.c
avformat/argo_asf: fix enforcement of chunk count
[ffmpeg] / libavformat / options.c
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 "internal.h"
23
24 #include "libavutil/avassert.h"
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 #if FF_API_CHILD_CLASS_NEXT
58 FF_DISABLE_DEPRECATION_WARNINGS
59 static const AVClass *format_child_class_next(const AVClass *prev)
60 {
61     AVInputFormat  *ifmt = NULL;
62     AVOutputFormat *ofmt = NULL;
63
64     if (!prev)
65         return &ff_avio_class;
66
67     while ((ifmt = av_iformat_next(ifmt)))
68         if (ifmt->priv_class == prev)
69             break;
70
71     if (!ifmt)
72         while ((ofmt = av_oformat_next(ofmt)))
73             if (ofmt->priv_class == prev)
74                 break;
75     if (!ofmt)
76         while (ifmt = av_iformat_next(ifmt))
77             if (ifmt->priv_class)
78                 return ifmt->priv_class;
79
80     while (ofmt = av_oformat_next(ofmt))
81         if (ofmt->priv_class)
82             return ofmt->priv_class;
83
84     return NULL;
85 }
86 FF_ENABLE_DEPRECATION_WARNINGS
87 #endif
88
89 enum {
90     CHILD_CLASS_ITER_AVIO = 0,
91     CHILD_CLASS_ITER_MUX,
92     CHILD_CLASS_ITER_DEMUX,
93     CHILD_CLASS_ITER_DONE,
94
95 };
96
97 #define ITER_STATE_SHIFT 16
98
99 static const AVClass *format_child_class_iterate(void **iter)
100 {
101     // we use the low 16 bits of iter as the value to be passed to
102     // av_(de)muxer_iterate()
103     void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
104     unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
105     const AVClass *ret = NULL;
106
107     if (state == CHILD_CLASS_ITER_AVIO) {
108         ret = &ff_avio_class;
109         state++;
110         goto finish;
111     }
112
113     if (state == CHILD_CLASS_ITER_MUX) {
114         const AVOutputFormat *ofmt;
115
116         while ((ofmt = av_muxer_iterate(&val))) {
117             ret = ofmt->priv_class;
118             if (ret)
119                 goto finish;
120         }
121
122         val = NULL;
123         state++;
124     }
125
126     if (state == CHILD_CLASS_ITER_DEMUX) {
127         const AVInputFormat *ifmt;
128
129         while ((ifmt = av_demuxer_iterate(&val))) {
130             ret = ifmt->priv_class;
131             if (ret)
132                 goto finish;
133         }
134         val = NULL;
135         state++;
136     }
137
138 finish:
139     // make sure none av_(de)muxer_iterate does not set the high bits of val
140     av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
141     *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
142     return ret;
143 }
144
145 static AVClassCategory get_category(void *ptr)
146 {
147     AVFormatContext* s = ptr;
148     if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
149     else           return AV_CLASS_CATEGORY_MUXER;
150 }
151
152 static const AVClass av_format_context_class = {
153     .class_name     = "AVFormatContext",
154     .item_name      = format_to_name,
155     .option         = avformat_options,
156     .version        = LIBAVUTIL_VERSION_INT,
157     .child_next     = format_child_next,
158 #if FF_API_CHILD_CLASS_NEXT
159     .child_class_next = format_child_class_next,
160 #endif
161     .child_class_iterate = format_child_class_iterate,
162     .category       = AV_CLASS_CATEGORY_MUXER,
163     .get_category   = get_category,
164 };
165
166 static int io_open_default(AVFormatContext *s, AVIOContext **pb,
167                            const char *url, int flags, AVDictionary **options)
168 {
169     int loglevel;
170
171     if (!strcmp(url, s->url) ||
172         s->iformat && !strcmp(s->iformat->name, "image2") ||
173         s->oformat && !strcmp(s->oformat->name, "image2")
174     ) {
175         loglevel = AV_LOG_DEBUG;
176     } else
177         loglevel = AV_LOG_INFO;
178
179     av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
180
181 #if FF_API_OLD_OPEN_CALLBACKS
182 FF_DISABLE_DEPRECATION_WARNINGS
183     if (s->open_cb)
184         return s->open_cb(s, pb, url, flags, &s->interrupt_callback, options);
185 FF_ENABLE_DEPRECATION_WARNINGS
186 #endif
187
188     return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
189 }
190
191 static void io_close_default(AVFormatContext *s, AVIOContext *pb)
192 {
193     avio_close(pb);
194 }
195
196 static void avformat_get_context_defaults(AVFormatContext *s)
197 {
198     memset(s, 0, sizeof(AVFormatContext));
199
200     s->av_class = &av_format_context_class;
201
202     s->io_open  = io_open_default;
203     s->io_close = io_close_default;
204
205     av_opt_set_defaults(s);
206 }
207
208 AVFormatContext *avformat_alloc_context(void)
209 {
210     AVFormatContext *ic;
211     AVFormatInternal *internal;
212     ic = av_malloc(sizeof(AVFormatContext));
213     if (!ic) return ic;
214
215     internal = av_mallocz(sizeof(*internal));
216     if (!internal) {
217         av_free(ic);
218         return NULL;
219     }
220     avformat_get_context_defaults(ic);
221     ic->internal = internal;
222     ic->internal->offset = AV_NOPTS_VALUE;
223     ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
224     ic->internal->shortest_end = AV_NOPTS_VALUE;
225
226     return ic;
227 }
228
229 enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
230 {
231     return ctx->duration_estimation_method;
232 }
233
234 const AVClass *avformat_get_class(void)
235 {
236     return &av_format_context_class;
237 }