2 * Format register and lookup
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/atomic.h"
25 #include "libavutil/avstring.h"
29 * Format register and lookup
31 /** head of registered input format linked list */
32 static AVInputFormat *first_iformat = NULL;
33 /** head of registered output format linked list */
34 static AVOutputFormat *first_oformat = NULL;
36 static AVInputFormat **last_iformat = &first_iformat;
37 static AVOutputFormat **last_oformat = &first_oformat;
39 AVInputFormat *av_iformat_next(AVInputFormat *f)
47 AVOutputFormat *av_oformat_next(AVOutputFormat *f)
55 void av_register_input_format(AVInputFormat *format)
57 AVInputFormat **p = last_iformat;
60 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
62 last_iformat = &format->next;
65 void av_register_output_format(AVOutputFormat *format)
67 AVOutputFormat **p = last_oformat;
70 while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
72 last_oformat = &format->next;
75 int av_match_ext(const char *filename, const char *extensions)
83 ext = strrchr(filename, '.');
89 while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
92 if (!av_strcasecmp(ext1, ext))
102 static int match_format(const char *name, const char *names)
110 namelen = strlen(name);
111 while ((p = strchr(names, ','))) {
112 len = FFMAX(p - names, namelen);
113 if (!av_strncasecmp(name, names, len))
117 return !av_strcasecmp(name, names);
120 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
121 const char *mime_type)
123 AVOutputFormat *fmt = NULL, *fmt_found;
124 int score_max, score;
126 /* specific test for image sequences */
127 #if CONFIG_IMAGE2_MUXER
128 if (!short_name && filename &&
129 av_filename_number_test(filename) &&
130 ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
131 return av_guess_format("image2", NULL, NULL);
134 /* Find the proper file type. */
137 while ((fmt = av_oformat_next(fmt))) {
139 if (fmt->name && short_name && match_format(short_name, fmt->name))
141 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
143 if (filename && fmt->extensions &&
144 av_match_ext(filename, fmt->extensions)) {
147 if (score > score_max) {
155 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
156 const char *filename, const char *mime_type,
157 enum AVMediaType type)
159 if (!strcmp(fmt->name, "segment") || !strcmp(fmt->name, "ssegment")) {
160 fmt = av_guess_format(NULL, filename, NULL);
163 if (type == AVMEDIA_TYPE_VIDEO) {
164 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
166 #if CONFIG_IMAGE2_MUXER
167 if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
168 codec_id = ff_guess_image2_codec(filename);
171 if (codec_id == AV_CODEC_ID_NONE)
172 codec_id = fmt->video_codec;
174 } else if (type == AVMEDIA_TYPE_AUDIO)
175 return fmt->audio_codec;
176 else if (type == AVMEDIA_TYPE_SUBTITLE)
177 return fmt->subtitle_codec;
179 return AV_CODEC_ID_NONE;
182 AVInputFormat *av_find_input_format(const char *short_name)
184 AVInputFormat *fmt = NULL;
185 while ((fmt = av_iformat_next(fmt)))
186 if (match_format(short_name, fmt->name))