]> git.sesse.net Git - ffmpeg/blob - libavformat/format.c
bethsoftvid: use the AVFrame API properly.
[ffmpeg] / libavformat / format.c
1 /*
2  * Format register and lookup
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/avstring.h"
25
26 /**
27  * @file
28  * Format register and lookup
29  */
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;
34
35 AVInputFormat *av_iformat_next(AVInputFormat *f)
36 {
37     if (f)
38         return f->next;
39     else
40         return first_iformat;
41 }
42
43 AVOutputFormat *av_oformat_next(AVOutputFormat *f)
44 {
45     if (f)
46         return f->next;
47     else
48         return first_oformat;
49 }
50
51 void av_register_input_format(AVInputFormat *format)
52 {
53     AVInputFormat **p = &first_iformat;
54
55     while (*p != NULL)
56         p = &(*p)->next;
57
58     *p = format;
59     format->next = NULL;
60 }
61
62 void av_register_output_format(AVOutputFormat *format)
63 {
64     AVOutputFormat **p = &first_oformat;
65
66     while (*p != NULL)
67         p = &(*p)->next;
68
69     *p = format;
70     format->next = NULL;
71 }
72
73 int av_match_ext(const char *filename, const char *extensions)
74 {
75     const char *ext, *p;
76     char ext1[32], *q;
77
78     if (!filename)
79         return 0;
80
81     ext = strrchr(filename, '.');
82     if (ext) {
83         ext++;
84         p = extensions;
85         for (;;) {
86             q = ext1;
87             while (*p != '\0' && *p != ','  && q - ext1 < sizeof(ext1) - 1)
88                 *q++ = *p++;
89             *q = '\0';
90             if (!av_strcasecmp(ext1, ext))
91                 return 1;
92             if (*p == '\0')
93                 break;
94             p++;
95         }
96     }
97     return 0;
98 }
99
100 static int match_format(const char *name, const char *names)
101 {
102     const char *p;
103     int len, namelen;
104
105     if (!name || !names)
106         return 0;
107
108     namelen = strlen(name);
109     while ((p = strchr(names, ','))) {
110         len = FFMAX(p - names, namelen);
111         if (!av_strncasecmp(name, names, len))
112             return 1;
113         names = p + 1;
114     }
115     return !av_strcasecmp(name, names);
116 }
117
118 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
119                                 const char *mime_type)
120 {
121     AVOutputFormat *fmt = NULL, *fmt_found;
122     int score_max, score;
123
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);
130     }
131 #endif
132     /* Find the proper file type. */
133     fmt_found = NULL;
134     score_max = 0;
135     while ((fmt = av_oformat_next(fmt))) {
136         score = 0;
137         if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
138             score += 100;
139         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
140             score += 10;
141         if (filename && fmt->extensions &&
142             av_match_ext(filename, fmt->extensions)) {
143             score += 5;
144         }
145         if (score > score_max) {
146             score_max = score;
147             fmt_found = fmt;
148         }
149     }
150     return fmt_found;
151 }
152
153 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
154                               const char *filename, const char *mime_type,
155                               enum AVMediaType type)
156 {
157     if (type == AVMEDIA_TYPE_VIDEO) {
158         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
159
160 #if CONFIG_IMAGE2_MUXER
161         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
162             codec_id = ff_guess_image2_codec(filename);
163         }
164 #endif
165         if (codec_id == AV_CODEC_ID_NONE)
166             codec_id = fmt->video_codec;
167         return codec_id;
168     } else if (type == AVMEDIA_TYPE_AUDIO)
169         return fmt->audio_codec;
170     else if (type == AVMEDIA_TYPE_SUBTITLE)
171         return fmt->subtitle_codec;
172     else
173         return AV_CODEC_ID_NONE;
174 }
175
176 AVInputFormat *av_find_input_format(const char *short_name)
177 {
178     AVInputFormat *fmt = NULL;
179     while ((fmt = av_iformat_next(fmt)))
180         if (match_format(short_name, fmt->name))
181             return fmt;
182     return NULL;
183 }