]> git.sesse.net Git - ffmpeg/blob - libavformat/format.c
crypto: consistently use size_t as type for length parameters
[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 #include "libavutil/opt.h"
24
25 #include "avio_internal.h"
26 #include "avformat.h"
27 #include "id3v2.h"
28 #include "internal.h"
29
30 /**
31  * @file
32  * Format register and lookup
33  */
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;
38
39 AVInputFormat *av_iformat_next(const AVInputFormat *f)
40 {
41     if (f)
42         return f->next;
43     else
44         return first_iformat;
45 }
46
47 AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
48 {
49     if (f)
50         return f->next;
51     else
52         return first_oformat;
53 }
54
55 void av_register_input_format(AVInputFormat *format)
56 {
57     AVInputFormat **p = &first_iformat;
58
59     while (*p)
60         p = &(*p)->next;
61
62     *p = format;
63     format->next = NULL;
64 }
65
66 void av_register_output_format(AVOutputFormat *format)
67 {
68     AVOutputFormat **p = &first_oformat;
69
70     while (*p)
71         p = &(*p)->next;
72
73     *p = format;
74     format->next = NULL;
75 }
76
77 int av_match_ext(const char *filename, const char *extensions)
78 {
79     const char *ext, *p;
80     char ext1[32], *q;
81
82     if (!filename)
83         return 0;
84
85     ext = strrchr(filename, '.');
86     if (ext) {
87         ext++;
88         p = extensions;
89         for (;;) {
90             q = ext1;
91             while (*p != '\0' && *p != ','  && q - ext1 < sizeof(ext1) - 1)
92                 *q++ = *p++;
93             *q = '\0';
94             if (!av_strcasecmp(ext1, ext))
95                 return 1;
96             if (*p == '\0')
97                 break;
98             p++;
99         }
100     }
101     return 0;
102 }
103
104 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
105                                 const char *mime_type)
106 {
107     AVOutputFormat *fmt = NULL, *fmt_found;
108     int score_max, score;
109
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);
116     }
117 #endif
118     /* Find the proper file type. */
119     fmt_found = NULL;
120     score_max = 0;
121     while ((fmt = av_oformat_next(fmt))) {
122         score = 0;
123         if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
124             score += 100;
125         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
126             score += 10;
127         if (filename && fmt->extensions &&
128             av_match_ext(filename, fmt->extensions)) {
129             score += 5;
130         }
131         if (score > score_max) {
132             score_max = score;
133             fmt_found = fmt;
134         }
135     }
136     return fmt_found;
137 }
138
139 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
140                               const char *filename, const char *mime_type,
141                               enum AVMediaType type)
142 {
143     if (type == AVMEDIA_TYPE_VIDEO) {
144         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
145
146 #if CONFIG_IMAGE2_MUXER
147         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
148             codec_id = ff_guess_image2_codec(filename);
149         }
150 #endif
151         if (codec_id == AV_CODEC_ID_NONE)
152             codec_id = fmt->video_codec;
153         return codec_id;
154     } else if (type == AVMEDIA_TYPE_AUDIO)
155         return fmt->audio_codec;
156     else if (type == AVMEDIA_TYPE_SUBTITLE)
157         return fmt->subtitle_codec;
158     else
159         return AV_CODEC_ID_NONE;
160 }
161
162 AVInputFormat *av_find_input_format(const char *short_name)
163 {
164     AVInputFormat *fmt = NULL;
165     while ((fmt = av_iformat_next(fmt)))
166         if (av_match_name(short_name, fmt->name))
167             return fmt;
168     return NULL;
169 }
170
171 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
172                                       int *score_max)
173 {
174     AVProbeData lpd = *pd;
175     AVInputFormat *fmt1 = NULL, *fmt;
176     int score, id3 = 0;
177
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) {
181             lpd.buf      += id3len;
182             lpd.buf_size -= id3len;
183         }
184         id3 = 1;
185     }
186
187     fmt = NULL;
188     while ((fmt1 = av_iformat_next(fmt1))) {
189         if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
190             continue;
191         score = 0;
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;
197         }
198         if (av_match_name(lpd.mime_type, fmt1->mime_type))
199             score = FFMAX(score, AVPROBE_SCORE_MIME);
200         if (score > *score_max) {
201             *score_max = score;
202             fmt        = fmt1;
203         } else if (score == *score_max)
204             fmt = NULL;
205     }
206
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;
213                 break;
214             }
215     }
216
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;
221                 break;
222             }
223     }
224
225     return fmt;
226 }
227
228 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
229 {
230     int score = 0;
231     return av_probe_input_format2(pd, is_opened, &score);
232 }
233
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)
237
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)
241 {
242     AVProbeData pd = { filename ? filename : "" };
243     uint8_t *buf = NULL;
244     int ret = 0, probe_size;
245     uint8_t *mime_type_opt = NULL;
246
247     if (!max_probe_size)
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);
253
254     if (offset >= max_probe_size)
255         return AVERROR(EINVAL);
256     avio_skip(pb, offset);
257     max_probe_size -= offset;
258     if (pb->av_class) {
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;
262     }
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;
267
268         /* Read probe data. */
269         if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
270             goto fail;
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)
275                 goto fail;
276
277             score = 0;
278             ret   = 0;          /* error was end of file, nothing read */
279         }
280         pd.buf_size += ret;
281         pd.buf       = buf;
282
283         memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
284
285         /* Guess file format. */
286         *fmt = av_probe_input_format2(&pd, 1, &score);
287         if (*fmt) {
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);
293             } else
294                 av_log(logctx, AV_LOG_DEBUG,
295                        "Probed with size=%d and score=%d\n", probe_size, score);
296         }
297     }
298
299     if (!*fmt)
300         ret = AVERROR_INVALIDDATA;
301
302 fail:
303     /* Rewind. Reuse probe buffer to avoid seeking. */
304     if (ret < 0 ||
305         (ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0) {
306         av_free(buf);
307     }
308     av_freep(&pd.mime_type);
309     return ret;
310 }