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
24 #include "libavutil/avstring.h"
28 * Format register and lookup
30 /** head of registered input format linked list */
31 static AVInputFormat *first_iformat = NULL;
32 /** head of registered output format linked list */
33 static AVOutputFormat *first_oformat = NULL;
35 AVInputFormat *av_iformat_next(const AVInputFormat *f)
43 AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
51 void av_register_input_format(AVInputFormat *format)
53 AVInputFormat **p = &first_iformat;
62 void av_register_output_format(AVOutputFormat *format)
64 AVOutputFormat **p = &first_oformat;
73 int av_match_ext(const char *filename, const char *extensions)
81 ext = strrchr(filename, '.');
87 while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
90 if (!av_strcasecmp(ext1, ext))
100 static int match_format(const char *name, const char *names)
108 namelen = strlen(name);
109 while ((p = strchr(names, ','))) {
110 len = FFMAX(p - names, namelen);
111 if (!av_strncasecmp(name, names, len))
115 return !av_strcasecmp(name, names);
118 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
119 const char *mime_type)
121 AVOutputFormat *fmt = NULL, *fmt_found;
122 int score_max, score;
124 /* specific test for image sequences */
125 #if CONFIG_IMAGE2_MUXER
126 if (!short_name && filename &&
127 av_filename_number_test(filename) &&
128 ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
129 return av_guess_format("image2", NULL, NULL);
132 /* Find the proper file type. */
135 while ((fmt = av_oformat_next(fmt))) {
137 if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
139 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
141 if (filename && fmt->extensions &&
142 av_match_ext(filename, fmt->extensions)) {
145 if (score > score_max) {
153 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
154 const char *filename, const char *mime_type,
155 enum AVMediaType type)
157 if (type == AVMEDIA_TYPE_VIDEO) {
158 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
160 #if CONFIG_IMAGE2_MUXER
161 if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
162 codec_id = ff_guess_image2_codec(filename);
165 if (codec_id == AV_CODEC_ID_NONE)
166 codec_id = fmt->video_codec;
168 } else if (type == AVMEDIA_TYPE_AUDIO)
169 return fmt->audio_codec;
170 else if (type == AVMEDIA_TYPE_SUBTITLE)
171 return fmt->subtitle_codec;
173 return AV_CODEC_ID_NONE;
176 AVInputFormat *av_find_input_format(const char *short_name)
178 AVInputFormat *fmt = NULL;
179 while ((fmt = av_iformat_next(fmt)))
180 if (match_format(short_name, fmt->name))