Libav
format.c
Go to the documentation of this file.
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 
38 
40 {
41  if (f)
42  return f->next;
43  else
44  return first_iformat;
45 }
46 
48 {
49  if (f)
50  return f->next;
51  else
52  return first_oformat;
53 }
54 
56 {
58 
59  while (*p)
60  p = &(*p)->next;
61 
62  *p = format;
63  format->next = NULL;
64 }
65 
67 {
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) &&
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) {
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 
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_EXTENSION);
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 
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 
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 
246  if (!max_probe_size)
247  max_probe_size = PROBE_BUF_MAX;
248  else if (max_probe_size > PROBE_BUF_MAX)
249  max_probe_size = PROBE_BUF_MAX;
250  else if (max_probe_size < PROBE_BUF_MIN)
251  return AVERROR(EINVAL);
252 
253  if (offset >= max_probe_size)
254  return AVERROR(EINVAL);
255  avio_skip(pb, offset);
256  max_probe_size -= offset;
257  if (pb->av_class)
258  av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &pd.mime_type);
259  for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
260  probe_size = FFMIN(probe_size << 1,
261  FFMAX(max_probe_size, probe_size + 1))) {
262  int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
263 
264  /* Read probe data. */
265  if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
266  goto fail;
267  if ((ret = avio_read(pb, buf + pd.buf_size,
268  probe_size - pd.buf_size)) < 0) {
269  /* Fail if error was not end of file, otherwise, lower score. */
270  if (ret != AVERROR_EOF)
271  goto fail;
272 
273  score = 0;
274  ret = 0; /* error was end of file, nothing read */
275  }
276  pd.buf_size += ret;
277  pd.buf = buf;
278 
279  memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
280 
281  /* Guess file format. */
282  *fmt = av_probe_input_format2(&pd, 1, &score);
283  if (*fmt) {
284  /* This can only be true in the last iteration. */
285  if (score <= AVPROBE_SCORE_MAX / 4) {
286  av_log(logctx, AV_LOG_WARNING,
287  "Format detected only with low score of %d, "
288  "misdetection possible!\n", score);
289  } else
290  av_log(logctx, AV_LOG_DEBUG,
291  "Probed with size=%d and score=%d\n", probe_size, score);
292  }
293  }
294 
295  if (!*fmt)
296  ret = AVERROR_INVALIDDATA;
297 
298 fail:
299  /* Rewind. Reuse probe buffer to avoid seeking. */
300  if (ret < 0 ||
301  (ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0) {
302  av_free(buf);
303  }
304  av_free(pd.mime_type);
305  return ret;
306 }
uint8_t * mime_type
mime_type, when known.
Definition: avformat.h:399
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
const char * filename
Definition: avformat.h:396
enum AVCodecID video_codec
default video codec
Definition: avformat.h:457
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:406
uint8_t
AVOptions.
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK.
Definition: avformat.h:539
const AVClass * av_class
A class for private options.
Definition: avio.h:81
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
#define AVERROR_EOF
End of file.
Definition: error.h:51
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:77
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int(* read_probe)(AVProbeData *)
Tell if a given file has a chance of being parsed as this format.
Definition: avformat.h:583
static AVOutputFormat * first_oformat
head of registered output format linked list
Definition: format.c:37
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition: avstring.c:237
enum AVCodecID codec_id
Definition: mov_chan.c:432
#define FFMAX(a, b)
Definition: common.h:55
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:171
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:398
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:162
#define FFMIN(a, b)
Definition: common.h:57
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:383
enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name, const char *filename, const char *mime_type, enum AVMediaType type)
Guess the codec ID based upon muxer and filename.
Definition: format.c:139
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size)
Probe a bytestream to determine the input format.
Definition: format.c:238
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
const char * name
Definition: avformat.h:446
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:104
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:739
void av_register_input_format(AVInputFormat *format)
Definition: format.c:55
AVOutputFormat * av_oformat_next(const AVOutputFormat *f)
If f is NULL, returns the first registered output format, if f is non-NULL, returns the next register...
Definition: format.c:47
void av_register_output_format(AVOutputFormat *format)
Definition: format.c:66
NULL
Definition: eval.c:55
struct AVOutputFormat * next
Definition: avformat.h:483
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:134
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:458
AVMediaType
Definition: avutil.h:185
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
#define PROBE_BUF_MAX
Definition: format.c:236
const char * mime_type
Comma-separated list of mime types.
Definition: avformat.h:557
#define PROBE_BUF_MIN
Definition: format.c:235
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
const char * extensions
If extensions are defined, then no probe is done.
Definition: avformat.h:546
Main libavformat public API header.
static AVInputFormat * first_iformat
head of registered input format linked list
Definition: format.c:35
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:125
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:97
int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val)
Definition: opt.c:330
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:456
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:138
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
struct AVInputFormat * next
Definition: avformat.h:566
const char * extensions
comma-separated filename extensions
Definition: avformat.h:454
AVInputFormat * av_probe_input_format(AVProbeData *pd, int is_opened)
Guess the file format.
Definition: format.c:228
const char * mime_type
Definition: avformat.h:453
AVInputFormat * av_iformat_next(const AVInputFormat *f)
If f is NULL, returns the first registered input format, if f is non-NULL, returns the next registere...
Definition: format.c:39