Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavformat / format.c
1 /*
2  * Format register and lookup
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config_components.h"
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/thread.h"
28
29 #include "avio_internal.h"
30 #include "avformat.h"
31 #include "id3v2.h"
32 #include "internal.h"
33 #include "url.h"
34
35
36 /**
37  * @file
38  * Format register and lookup
39  */
40
41 int av_match_ext(const char *filename, const char *extensions)
42 {
43     const char *ext;
44
45     if (!filename)
46         return 0;
47
48     ext = strrchr(filename, '.');
49     if (ext)
50         return av_match_name(ext + 1, extensions);
51     return 0;
52 }
53
54 int ff_match_url_ext(const char *url, const char *extensions)
55 {
56     const char *ext;
57     URLComponents uc;
58     int ret;
59     char scratchpad[128];
60
61     if (!url)
62         return 0;
63
64     ret = ff_url_decompose(&uc, url, NULL);
65     if (ret < 0 || !URL_COMPONENT_HAVE(uc, scheme))
66         return ret;
67     for (ext = uc.query; *ext != '.' && ext > uc.path; ext--)
68         ;
69
70     if (*ext != '.')
71         return 0;
72     if (uc.query - ext > sizeof(scratchpad))
73         return AVERROR(ENOMEM); //not enough memory in our scratchpad
74     av_strlcpy(scratchpad, ext + 1, uc.query - ext);
75
76     return av_match_name(scratchpad, extensions);
77 }
78
79 const AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
80                                       const char *mime_type)
81 {
82     const AVOutputFormat *fmt = NULL;
83     const AVOutputFormat *fmt_found = NULL;
84     void *i = 0;
85     int score_max, score;
86
87     /* specific test for image sequences */
88 #if CONFIG_IMAGE2_MUXER
89     if (!short_name && filename &&
90         av_filename_number_test(filename) &&
91         ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
92         return av_guess_format("image2", NULL, NULL);
93     }
94 #endif
95     /* Find the proper file type. */
96     score_max = 0;
97     while ((fmt = av_muxer_iterate(&i))) {
98         score = 0;
99         if (fmt->name && short_name && av_match_name(short_name, fmt->name))
100             score += 100;
101         if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
102             score += 10;
103         if (filename && fmt->extensions &&
104             av_match_ext(filename, fmt->extensions)) {
105             score += 5;
106         }
107         if (score > score_max) {
108             score_max = score;
109             fmt_found = fmt;
110         }
111     }
112     return fmt_found;
113 }
114
115 enum AVCodecID av_guess_codec(const AVOutputFormat *fmt, const char *short_name,
116                               const char *filename, const char *mime_type,
117                               enum AVMediaType type)
118 {
119     if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
120         const AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
121         if (fmt2)
122             fmt = fmt2;
123     }
124
125     if (type == AVMEDIA_TYPE_VIDEO) {
126         enum AVCodecID codec_id = AV_CODEC_ID_NONE;
127
128 #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
129         if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
130             codec_id = ff_guess_image2_codec(filename);
131         }
132 #endif
133         if (codec_id == AV_CODEC_ID_NONE)
134             codec_id = fmt->video_codec;
135         return codec_id;
136     } else if (type == AVMEDIA_TYPE_AUDIO)
137         return fmt->audio_codec;
138     else if (type == AVMEDIA_TYPE_SUBTITLE)
139         return fmt->subtitle_codec;
140     else
141         return AV_CODEC_ID_NONE;
142 }
143
144 const AVInputFormat *av_find_input_format(const char *short_name)
145 {
146     const AVInputFormat *fmt = NULL;
147     void *i = 0;
148     while ((fmt = av_demuxer_iterate(&i)))
149         if (av_match_name(short_name, fmt->name))
150             return fmt;
151     return NULL;
152 }
153
154 const AVInputFormat *av_probe_input_format3(const AVProbeData *pd,
155                                             int is_opened, int *score_ret)
156 {
157     AVProbeData lpd = *pd;
158     const AVInputFormat *fmt1 = NULL;
159     const AVInputFormat *fmt = NULL;
160     int score, score_max = 0;
161     void *i = 0;
162     const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
163     enum nodat {
164         NO_ID3,
165         ID3_ALMOST_GREATER_PROBE,
166         ID3_GREATER_PROBE,
167         ID3_GREATER_MAX_PROBE,
168     } nodat = NO_ID3;
169
170     if (!lpd.buf)
171         lpd.buf = (unsigned char *) zerobuffer;
172
173     if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
174         int id3len = ff_id3v2_tag_len(lpd.buf);
175         if (lpd.buf_size > id3len + 16) {
176             if (lpd.buf_size < 2LL*id3len + 16)
177                 nodat = ID3_ALMOST_GREATER_PROBE;
178             lpd.buf      += id3len;
179             lpd.buf_size -= id3len;
180         } else if (id3len >= PROBE_BUF_MAX) {
181             nodat = ID3_GREATER_MAX_PROBE;
182         } else
183             nodat = ID3_GREATER_PROBE;
184     }
185
186     while ((fmt1 = av_demuxer_iterate(&i))) {
187         if (fmt1->flags & AVFMT_EXPERIMENTAL)
188             continue;
189         if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
190             continue;
191         score = 0;
192         if (fmt1->read_probe) {
193             score = fmt1->read_probe(&lpd);
194             if (score)
195                 av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
196             if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
197                 switch (nodat) {
198                 case NO_ID3:
199                     score = FFMAX(score, 1);
200                     break;
201                 case ID3_GREATER_PROBE:
202                 case ID3_ALMOST_GREATER_PROBE:
203                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION / 2 - 1);
204                     break;
205                 case ID3_GREATER_MAX_PROBE:
206                     score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
207                     break;
208                 }
209             }
210         } else if (fmt1->extensions) {
211             if (av_match_ext(lpd.filename, fmt1->extensions))
212                 score = AVPROBE_SCORE_EXTENSION;
213         }
214         if (av_match_name(lpd.mime_type, fmt1->mime_type)) {
215             if (AVPROBE_SCORE_MIME > score) {
216                 av_log(NULL, AV_LOG_DEBUG, "Probing %s score:%d increased to %d due to MIME type\n", fmt1->name, score, AVPROBE_SCORE_MIME);
217                 score = AVPROBE_SCORE_MIME;
218             }
219         }
220         if (score > score_max) {
221             score_max = score;
222             fmt       = fmt1;
223         } else if (score == score_max)
224             fmt = NULL;
225     }
226     if (nodat == ID3_GREATER_PROBE)
227         score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
228     *score_ret = score_max;
229
230     return fmt;
231 }
232
233 const AVInputFormat *av_probe_input_format2(const AVProbeData *pd,
234                                             int is_opened, int *score_max)
235 {
236     int score_ret;
237     const AVInputFormat *fmt = av_probe_input_format3(pd, is_opened, &score_ret);
238     if (score_ret > *score_max) {
239         *score_max = score_ret;
240         return fmt;
241     } else
242         return NULL;
243 }
244
245 const AVInputFormat *av_probe_input_format(const AVProbeData *pd, int is_opened)
246 {
247     int score = 0;
248     return av_probe_input_format2(pd, is_opened, &score);
249 }
250
251 int av_probe_input_buffer2(AVIOContext *pb, const AVInputFormat **fmt,
252                            const char *filename, void *logctx,
253                            unsigned int offset, unsigned int max_probe_size)
254 {
255     AVProbeData pd = { filename ? filename : "" };
256     uint8_t *buf = NULL;
257     int ret = 0, probe_size, buf_offset = 0;
258     int score = 0;
259     int ret2;
260     int eof = 0;
261
262     if (!max_probe_size)
263         max_probe_size = PROBE_BUF_MAX;
264     else if (max_probe_size < PROBE_BUF_MIN) {
265         av_log(logctx, AV_LOG_ERROR,
266                "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN);
267         return AVERROR(EINVAL);
268     }
269
270     if (offset >= max_probe_size)
271         return AVERROR(EINVAL);
272
273     if (pb->av_class) {
274         uint8_t *mime_type_opt = NULL;
275         char *semi;
276         av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
277         pd.mime_type = (const char *)mime_type_opt;
278         semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
279         if (semi) {
280             *semi = '\0';
281         }
282     }
283
284     for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt && !eof;
285          probe_size = FFMIN(probe_size << 1,
286                             FFMAX(max_probe_size, probe_size + 1))) {
287         score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
288
289         /* Read probe data. */
290         if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
291             goto fail;
292         if ((ret = avio_read(pb, buf + buf_offset,
293                              probe_size - buf_offset)) < 0) {
294             /* Fail if error was not end of file, otherwise, lower score. */
295             if (ret != AVERROR_EOF)
296                 goto fail;
297
298             score = 0;
299             ret   = 0;          /* error was end of file, nothing read */
300             eof   = 1;
301         }
302         buf_offset += ret;
303         if (buf_offset < offset)
304             continue;
305         pd.buf_size = buf_offset - offset;
306         pd.buf = &buf[offset];
307
308         memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
309
310         /* Guess file format. */
311         *fmt = av_probe_input_format2(&pd, 1, &score);
312         if (*fmt) {
313             /* This can only be true in the last iteration. */
314             if (score <= AVPROBE_SCORE_RETRY) {
315                 av_log(logctx, AV_LOG_WARNING,
316                        "Format %s detected only with low score of %d, "
317                        "misdetection possible!\n", (*fmt)->name, score);
318             } else
319                 av_log(logctx, AV_LOG_DEBUG,
320                        "Format %s probed with size=%d and score=%d\n",
321                        (*fmt)->name, probe_size, score);
322 #if 0
323             FILE *f = fopen("probestat.tmp", "ab");
324             fprintf(f, "probe_size:%d format:%s score:%d filename:%s\n", probe_size, (*fmt)->name, score, filename);
325             fclose(f);
326 #endif
327         }
328     }
329
330     if (!*fmt)
331         ret = AVERROR_INVALIDDATA;
332
333 fail:
334     /* Rewind. Reuse probe buffer to avoid seeking. */
335     ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
336     if (ret >= 0)
337         ret = ret2;
338
339     av_freep(&pd.mime_type);
340     return ret < 0 ? ret : score;
341 }
342
343 int av_probe_input_buffer(AVIOContext *pb, const AVInputFormat **fmt,
344                           const char *filename, void *logctx,
345                           unsigned int offset, unsigned int max_probe_size)
346 {
347     int ret = av_probe_input_buffer2(pb, fmt, filename, logctx, offset, max_probe_size);
348     return ret < 0 ? ret : 0;
349 }