2 * Format register and lookup
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
25 #include "avio_internal.h"
32 * Format register and lookup
34 /** head of registered input format linked list */
35 static AVInputFormat *first_iformat = NULL;
36 /** head of registered output format linked list */
37 static AVOutputFormat *first_oformat = NULL;
39 AVInputFormat *av_iformat_next(const AVInputFormat *f)
47 AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
55 void av_register_input_format(AVInputFormat *format)
57 AVInputFormat **p = &first_iformat;
66 void av_register_output_format(AVOutputFormat *format)
68 AVOutputFormat **p = &first_oformat;
77 int av_match_ext(const char *filename, const char *extensions)
85 ext = strrchr(filename, '.');
91 while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
94 if (!av_strcasecmp(ext1, ext))
104 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
105 const char *mime_type)
107 AVOutputFormat *fmt = NULL, *fmt_found;
108 int score_max, score;
110 /* specific test for image sequences */
111 #if CONFIG_IMAGE2_MUXER
112 if (!short_name && filename &&
113 av_filename_number_test(filename) &&
114 ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
115 return av_guess_format("image2", NULL, NULL);
118 /* Find the proper file type. */
121 while ((fmt = av_oformat_next(fmt))) {
123 if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
125 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
127 if (filename && fmt->extensions &&
128 av_match_ext(filename, fmt->extensions)) {
131 if (score > score_max) {
139 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
140 const char *filename, const char *mime_type,
141 enum AVMediaType type)
143 if (type == AVMEDIA_TYPE_VIDEO) {
144 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
146 #if CONFIG_IMAGE2_MUXER
147 if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
148 codec_id = ff_guess_image2_codec(filename);
151 if (codec_id == AV_CODEC_ID_NONE)
152 codec_id = fmt->video_codec;
154 } else if (type == AVMEDIA_TYPE_AUDIO)
155 return fmt->audio_codec;
156 else if (type == AVMEDIA_TYPE_SUBTITLE)
157 return fmt->subtitle_codec;
159 return AV_CODEC_ID_NONE;
162 AVInputFormat *av_find_input_format(const char *short_name)
164 AVInputFormat *fmt = NULL;
165 while ((fmt = av_iformat_next(fmt)))
166 if (av_match_name(short_name, fmt->name))
171 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
174 AVProbeData lpd = *pd;
175 AVInputFormat *fmt1 = NULL, *fmt;
178 if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
179 int id3len = ff_id3v2_tag_len(lpd.buf);
180 if (lpd.buf_size > id3len + 16) {
182 lpd.buf_size -= id3len;
188 while ((fmt1 = av_iformat_next(fmt1))) {
189 if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
192 if (fmt1->read_probe) {
193 score = fmt1->read_probe(&lpd);
194 } else if (fmt1->extensions) {
195 if (av_match_ext(lpd.filename, fmt1->extensions))
196 score = AVPROBE_SCORE_EXTENSION;
198 if (av_match_name(lpd.mime_type, fmt1->mime_type))
199 score = FFMAX(score, AVPROBE_SCORE_MIME);
200 if (score > *score_max) {
203 } else if (score == *score_max)
207 // A hack for files with huge id3v2 tags -- try to guess by file extension.
208 if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
209 while ((fmt = av_iformat_next(fmt)))
210 if (fmt->extensions &&
211 av_match_ext(lpd.filename, fmt->extensions)) {
212 *score_max = AVPROBE_SCORE_EXTENSION / 2;
217 if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
218 while ((fmt = av_iformat_next(fmt)))
219 if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
220 *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
228 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
231 return av_probe_input_format2(pd, is_opened, &score);
234 /* size of probe buffer, for guessing file type from file contents */
235 #define PROBE_BUF_MIN 2048
236 #define PROBE_BUF_MAX (1 << 20)
238 int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
239 const char *filename, void *logctx,
240 unsigned int offset, unsigned int max_probe_size)
242 AVProbeData pd = { filename ? filename : "" };
244 int ret = 0, probe_size;
245 uint8_t *mime_type_opt = NULL;
248 max_probe_size = PROBE_BUF_MAX;
249 else if (max_probe_size > PROBE_BUF_MAX)
250 max_probe_size = PROBE_BUF_MAX;
251 else if (max_probe_size < PROBE_BUF_MIN)
252 return AVERROR(EINVAL);
254 if (offset >= max_probe_size)
255 return AVERROR(EINVAL);
256 avio_skip(pb, offset);
257 max_probe_size -= offset;
259 av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
260 pd.mime_type = (const char *)mime_type_opt;
261 mime_type_opt = NULL;
263 for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
264 probe_size = FFMIN(probe_size << 1,
265 FFMAX(max_probe_size, probe_size + 1))) {
266 int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
268 /* Read probe data. */
269 if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
271 if ((ret = avio_read(pb, buf + pd.buf_size,
272 probe_size - pd.buf_size)) < 0) {
273 /* Fail if error was not end of file, otherwise, lower score. */
274 if (ret != AVERROR_EOF)
278 ret = 0; /* error was end of file, nothing read */
283 memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
285 /* Guess file format. */
286 *fmt = av_probe_input_format2(&pd, 1, &score);
288 /* This can only be true in the last iteration. */
289 if (score <= AVPROBE_SCORE_MAX / 4) {
290 av_log(logctx, AV_LOG_WARNING,
291 "Format detected only with low score of %d, "
292 "misdetection possible!\n", score);
294 av_log(logctx, AV_LOG_DEBUG,
295 "Probed with size=%d and score=%d\n", probe_size, score);
300 ret = AVERROR_INVALIDDATA;
303 /* Rewind. Reuse probe buffer to avoid seeking. */
305 (ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0) {
308 av_freep(&pd.mime_type);