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