2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "avio_internal.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
30 * Options definition for AVFormatContext.
33 FF_DISABLE_DEPRECATION_WARNINGS
34 #include "options_table.h"
35 FF_ENABLE_DEPRECATION_WARNINGS
37 static const char* format_to_name(void* ptr)
39 AVFormatContext* fc = (AVFormatContext*) ptr;
40 if(fc->iformat) return fc->iformat->name;
41 else if(fc->oformat) return fc->oformat->name;
45 static void *format_child_next(void *obj, void *prev)
47 AVFormatContext *s = obj;
48 if (!prev && s->priv_data &&
49 ((s->iformat && s->iformat->priv_class) ||
50 s->oformat && s->oformat->priv_class))
52 if (s->pb && s->pb->av_class && prev != s->pb)
57 #if FF_API_CHILD_CLASS_NEXT
58 static const AVClass *format_child_class_next(const AVClass *prev)
60 const AVInputFormat *ifmt = NULL;
61 const AVOutputFormat *ofmt = NULL;
62 void *ifmt_iter = NULL, *ofmt_iter = NULL;
65 return &ff_avio_class;
67 while ((ifmt = av_demuxer_iterate(&ifmt_iter)))
68 if (ifmt->priv_class == prev)
73 while ((ofmt = av_muxer_iterate(&ofmt_iter)))
74 if (ofmt->priv_class == prev)
79 while ((ifmt = av_demuxer_iterate(&ifmt_iter)))
81 return ifmt->priv_class;
84 while ((ofmt = av_muxer_iterate(&ofmt_iter)))
86 return ofmt->priv_class;
93 CHILD_CLASS_ITER_AVIO = 0,
95 CHILD_CLASS_ITER_DEMUX,
96 CHILD_CLASS_ITER_DONE,
100 #define ITER_STATE_SHIFT 16
102 static const AVClass *format_child_class_iterate(void **iter)
104 // we use the low 16 bits of iter as the value to be passed to
105 // av_(de)muxer_iterate()
106 void *val = (void*)(((uintptr_t)*iter) & ((1 << ITER_STATE_SHIFT) - 1));
107 unsigned int state = ((uintptr_t)*iter) >> ITER_STATE_SHIFT;
108 const AVClass *ret = NULL;
110 if (state == CHILD_CLASS_ITER_AVIO) {
111 ret = &ff_avio_class;
116 if (state == CHILD_CLASS_ITER_MUX) {
117 const AVOutputFormat *ofmt;
119 while ((ofmt = av_muxer_iterate(&val))) {
120 ret = ofmt->priv_class;
129 if (state == CHILD_CLASS_ITER_DEMUX) {
130 const AVInputFormat *ifmt;
132 while ((ifmt = av_demuxer_iterate(&val))) {
133 ret = ifmt->priv_class;
142 // make sure none av_(de)muxer_iterate does not set the high bits of val
143 av_assert0(!((uintptr_t)val >> ITER_STATE_SHIFT));
144 *iter = (void*)((uintptr_t)val | (state << ITER_STATE_SHIFT));
148 static AVClassCategory get_category(void *ptr)
150 AVFormatContext* s = ptr;
151 if(s->iformat) return AV_CLASS_CATEGORY_DEMUXER;
152 else return AV_CLASS_CATEGORY_MUXER;
155 static const AVClass av_format_context_class = {
156 .class_name = "AVFormatContext",
157 .item_name = format_to_name,
158 .option = avformat_options,
159 .version = LIBAVUTIL_VERSION_INT,
160 .child_next = format_child_next,
161 #if FF_API_CHILD_CLASS_NEXT
162 .child_class_next = format_child_class_next,
164 .child_class_iterate = format_child_class_iterate,
165 .category = AV_CLASS_CATEGORY_MUXER,
166 .get_category = get_category,
169 static int io_open_default(AVFormatContext *s, AVIOContext **pb,
170 const char *url, int flags, AVDictionary **options)
174 if (!strcmp(url, s->url) ||
175 s->iformat && !strcmp(s->iformat->name, "image2") ||
176 s->oformat && !strcmp(s->oformat->name, "image2")
178 loglevel = AV_LOG_DEBUG;
180 loglevel = AV_LOG_INFO;
182 av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
184 return ffio_open_whitelist(pb, url, flags, &s->interrupt_callback, options, s->protocol_whitelist, s->protocol_blacklist);
187 static void io_close_default(AVFormatContext *s, AVIOContext *pb)
192 static void avformat_get_context_defaults(AVFormatContext *s)
194 memset(s, 0, sizeof(AVFormatContext));
196 s->av_class = &av_format_context_class;
198 s->io_open = io_open_default;
199 s->io_close = io_close_default;
201 av_opt_set_defaults(s);
204 AVFormatContext *avformat_alloc_context(void)
207 AVFormatInternal *internal;
208 ic = av_malloc(sizeof(AVFormatContext));
211 internal = av_mallocz(sizeof(*internal));
216 internal->pkt = av_packet_alloc();
217 internal->parse_pkt = av_packet_alloc();
218 if (!internal->pkt || !internal->parse_pkt) {
219 av_packet_free(&internal->pkt);
220 av_packet_free(&internal->parse_pkt);
225 avformat_get_context_defaults(ic);
226 ic->internal = internal;
227 ic->internal->offset = AV_NOPTS_VALUE;
228 ic->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
229 ic->internal->shortest_end = AV_NOPTS_VALUE;
234 enum AVDurationEstimationMethod av_fmt_ctx_get_duration_estimation_method(const AVFormatContext* ctx)
236 return ctx->duration_estimation_method;
239 const AVClass *avformat_get_class(void)
241 return &av_format_context_class;