]> git.sesse.net Git - ffmpeg/blob - libavformat/format.c
avstring: Expose the simple name match function
[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 "libavutil/avstring.h"
23
24 #include "avio_internal.h"
25 #include "avformat.h"
26 #include "id3v2.h"
27 #include "internal.h"
28
29 /**
30  * @file
31  * Format register and lookup
32  */
33 /** head of registered input format linked list */
34 static AVInputFormat *first_iformat = NULL;
35 /** head of registered output format linked list */
36 static AVOutputFormat *first_oformat = NULL;
37
38 AVInputFormat *av_iformat_next(const AVInputFormat *f)
39 {
40     if (f)
41         return f->next;
42     else
43         return first_iformat;
44 }
45
46 AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
47 {
48     if (f)
49         return f->next;
50     else
51         return first_oformat;
52 }
53
54 void av_register_input_format(AVInputFormat *format)
55 {
56     AVInputFormat **p = &first_iformat;
57
58     while (*p != NULL)
59         p = &(*p)->next;
60
61     *p = format;
62     format->next = NULL;
63 }
64
65 void av_register_output_format(AVOutputFormat *format)
66 {
67     AVOutputFormat **p = &first_oformat;
68
69     while (*p != NULL)
70         p = &(*p)->next;
71
72     *p = format;
73     format->next = NULL;
74 }
75
76 int av_match_ext(const char *filename, const char *extensions)
77 {
78     const char *ext, *p;
79     char ext1[32], *q;
80
81     if (!filename)
82         return 0;
83
84     ext = strrchr(filename, '.');
85     if (ext) {
86         ext++;
87         p = extensions;
88         for (;;) {
89             q = ext1;
90             while (*p != '\0' && *p != ','  && q - ext1 < sizeof(ext1) - 1)
91                 *q++ = *p++;
92             *q = '\0';
93             if (!av_strcasecmp(ext1, ext))
94                 return 1;
95             if (*p == '\0')
96                 break;
97             p++;
98         }
99     }
100     return 0;
101 }
102
103 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
104                                 const char *mime_type)
105 {
106     AVOutputFormat *fmt = NULL, *fmt_found;
107     int score_max, score;
108
109     /* specific test for image sequences */
110 #if CONFIG_IMAGE2_MUXER
111     if (!short_name && filename &&
112         av_filename_number_test(filename) &&
113         ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
114         return av_guess_format("image2", NULL, NULL);
115     }
116 #endif
117     /* Find the proper file type. */
118     fmt_found = NULL;
119     score_max = 0;
120     while ((fmt = av_oformat_next(fmt))) {
121         score = 0;
122         if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
123             score += 100;
124         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
125             score += 10;
126         if (filename && fmt->extensions &&
127             av_match_ext(filename, fmt->extensions)) {
128             score += 5;
129         }
130         if (score > score_max) {
131             score_max = score;
132             fmt_found = fmt;
133         }
134     }
135     return fmt_found;
136 }
137
138 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
139                               const char *filename, const char *mime_type,
140                               enum AVMediaType type)
141 {
142     if (type == AVMEDIA_TYPE_VIDEO) {
143         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
144
145 #if CONFIG_IMAGE2_MUXER
146         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
147             codec_id = ff_guess_image2_codec(filename);
148         }
149 #endif
150         if (codec_id == AV_CODEC_ID_NONE)
151             codec_id = fmt->video_codec;
152         return codec_id;
153     } else if (type == AVMEDIA_TYPE_AUDIO)
154         return fmt->audio_codec;
155     else if (type == AVMEDIA_TYPE_SUBTITLE)
156         return fmt->subtitle_codec;
157     else
158         return AV_CODEC_ID_NONE;
159 }
160
161 AVInputFormat *av_find_input_format(const char *short_name)
162 {
163     AVInputFormat *fmt = NULL;
164     while ((fmt = av_iformat_next(fmt)))
165         if (av_match_name(short_name, fmt->name))
166             return fmt;
167     return NULL;
168 }
169
170 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
171                                       int *score_max)
172 {
173     AVProbeData lpd = *pd;
174     AVInputFormat *fmt1 = NULL, *fmt;
175     int score, id3 = 0;
176
177     if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
178         int id3len = ff_id3v2_tag_len(lpd.buf);
179         if (lpd.buf_size > id3len + 16) {
180             lpd.buf      += id3len;
181             lpd.buf_size -= id3len;
182         }
183         id3 = 1;
184     }
185
186     fmt = NULL;
187     while ((fmt1 = av_iformat_next(fmt1))) {
188         if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
189             continue;
190         score = 0;
191         if (fmt1->read_probe) {
192             score = fmt1->read_probe(&lpd);
193         } else if (fmt1->extensions) {
194             if (av_match_ext(lpd.filename, fmt1->extensions))
195                 score = AVPROBE_SCORE_EXTENSION;
196         }
197         if (score > *score_max) {
198             *score_max = score;
199             fmt        = fmt1;
200         } else if (score == *score_max)
201             fmt = NULL;
202     }
203
204     // A hack for files with huge id3v2 tags -- try to guess by file extension.
205     if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
206         while ((fmt = av_iformat_next(fmt)))
207             if (fmt->extensions &&
208                 av_match_ext(lpd.filename, fmt->extensions)) {
209                 *score_max = AVPROBE_SCORE_EXTENSION / 2;
210                 break;
211             }
212     }
213
214     if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
215         while ((fmt = av_iformat_next(fmt)))
216             if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
217                 *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
218                 break;
219             }
220     }
221
222     return fmt;
223 }
224
225 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
226 {
227     int score = 0;
228     return av_probe_input_format2(pd, is_opened, &score);
229 }
230
231 /* size of probe buffer, for guessing file type from file contents */
232 #define PROBE_BUF_MIN 2048
233 #define PROBE_BUF_MAX (1 << 20)
234
235 int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
236                           const char *filename, void *logctx,
237                           unsigned int offset, unsigned int max_probe_size)
238 {
239     AVProbeData pd = { filename ? filename : "" };
240     uint8_t *buf = NULL;
241     int ret = 0, probe_size;
242
243     if (!max_probe_size)
244         max_probe_size = PROBE_BUF_MAX;
245     else if (max_probe_size > PROBE_BUF_MAX)
246         max_probe_size = PROBE_BUF_MAX;
247     else if (max_probe_size < PROBE_BUF_MIN)
248         return AVERROR(EINVAL);
249
250     if (offset >= max_probe_size)
251         return AVERROR(EINVAL);
252     avio_skip(pb, offset);
253     max_probe_size -= offset;
254
255     for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
256          probe_size = FFMIN(probe_size << 1,
257                             FFMAX(max_probe_size, probe_size + 1))) {
258         int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
259
260         /* Read probe data. */
261         if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
262             return ret;
263         if ((ret = avio_read(pb, buf + pd.buf_size,
264                              probe_size - pd.buf_size)) < 0) {
265             /* Fail if error was not end of file, otherwise, lower score. */
266             if (ret != AVERROR_EOF) {
267                 av_free(buf);
268                 return ret;
269             }
270             score = 0;
271             ret   = 0;          /* error was end of file, nothing read */
272         }
273         pd.buf_size += ret;
274         pd.buf       = buf;
275
276         memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
277
278         /* Guess file format. */
279         *fmt = av_probe_input_format2(&pd, 1, &score);
280         if (*fmt) {
281             /* This can only be true in the last iteration. */
282             if (score <= AVPROBE_SCORE_MAX / 4) {
283                 av_log(logctx, AV_LOG_WARNING,
284                        "Format detected only with low score of %d, "
285                        "misdetection possible!\n", score);
286             } else
287                 av_log(logctx, AV_LOG_DEBUG,
288                        "Probed with size=%d and score=%d\n", probe_size, score);
289         }
290     }
291
292     if (!*fmt) {
293         av_free(buf);
294         return AVERROR_INVALIDDATA;
295     }
296
297     /* Rewind. Reuse probe buffer to avoid seeking. */
298     if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
299         av_free(buf);
300
301     return ret;
302 }