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