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 "avformat.h"
23 #include "internal.h"
24 #include "libavutil/avstring.h"
25 
34 
36 {
37  if (f)
38  return f->next;
39  else
40  return first_iformat;
41 }
42 
44 {
45  if (f)
46  return f->next;
47  else
48  return first_oformat;
49 }
50 
52 {
54 
55  while (*p != NULL)
56  p = &(*p)->next;
57 
58  *p = format;
59  format->next = NULL;
60 }
61 
63 {
65 
66  while (*p != NULL)
67  p = &(*p)->next;
68 
69  *p = format;
70  format->next = NULL;
71 }
72 
73 int av_match_ext(const char *filename, const char *extensions)
74 {
75  const char *ext, *p;
76  char ext1[32], *q;
77 
78  if (!filename)
79  return 0;
80 
81  ext = strrchr(filename, '.');
82  if (ext) {
83  ext++;
84  p = extensions;
85  for (;;) {
86  q = ext1;
87  while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
88  *q++ = *p++;
89  *q = '\0';
90  if (!av_strcasecmp(ext1, ext))
91  return 1;
92  if (*p == '\0')
93  break;
94  p++;
95  }
96  }
97  return 0;
98 }
99 
100 static int match_format(const char *name, const char *names)
101 {
102  const char *p;
103  int len, namelen;
104 
105  if (!name || !names)
106  return 0;
107 
108  namelen = strlen(name);
109  while ((p = strchr(names, ','))) {
110  len = FFMAX(p - names, namelen);
111  if (!av_strncasecmp(name, names, len))
112  return 1;
113  names = p + 1;
114  }
115  return !av_strcasecmp(name, names);
116 }
117 
118 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
119  const char *mime_type)
120 {
121  AVOutputFormat *fmt = NULL, *fmt_found;
122  int score_max, score;
123 
124  /* specific test for image sequences */
125 #if CONFIG_IMAGE2_MUXER
126  if (!short_name && filename &&
127  av_filename_number_test(filename) &&
129  return av_guess_format("image2", NULL, NULL);
130  }
131 #endif
132  /* Find the proper file type. */
133  fmt_found = NULL;
134  score_max = 0;
135  while ((fmt = av_oformat_next(fmt))) {
136  score = 0;
137  if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
138  score += 100;
139  if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
140  score += 10;
141  if (filename && fmt->extensions &&
142  av_match_ext(filename, fmt->extensions)) {
143  score += 5;
144  }
145  if (score > score_max) {
146  score_max = score;
147  fmt_found = fmt;
148  }
149  }
150  return fmt_found;
151 }
152 
153 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
154  const char *filename, const char *mime_type,
155  enum AVMediaType type)
156 {
157  if (type == AVMEDIA_TYPE_VIDEO) {
159 
160 #if CONFIG_IMAGE2_MUXER
161  if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
162  codec_id = ff_guess_image2_codec(filename);
163  }
164 #endif
165  if (codec_id == AV_CODEC_ID_NONE)
166  codec_id = fmt->video_codec;
167  return codec_id;
168  } else if (type == AVMEDIA_TYPE_AUDIO)
169  return fmt->audio_codec;
170  else if (type == AVMEDIA_TYPE_SUBTITLE)
171  return fmt->subtitle_codec;
172  else
173  return AV_CODEC_ID_NONE;
174 }
175 
176 AVInputFormat *av_find_input_format(const char *short_name)
177 {
178  AVInputFormat *fmt = NULL;
179  while ((fmt = av_iformat_next(fmt)))
180  if (match_format(short_name, fmt->name))
181  return fmt;
182  return NULL;
183 }
enum AVCodecID video_codec
default video codec
Definition: avformat.h:448
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:166
const char * name
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:73
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
static AVOutputFormat * first_oformat
head of registered output format linked list
Definition: format.c:33
enum AVCodecID codec_id
Definition: mov_chan.c:432
#define FFMAX(a, b)
Definition: common.h:55
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:176
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
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:153
const char * name
Definition: avformat.h:437
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:118
void av_register_input_format(AVInputFormat *format)
Definition: format.c:51
AVOutputFormat * av_oformat_next(AVOutputFormat *f)
If f is NULL, returns the first registered output format, if f is non-NULL, returns the next register...
Definition: format.c:43
void av_register_output_format(AVOutputFormat *format)
Definition: format.c:62
NULL
Definition: eval.c:55
struct AVOutputFormat * next
Definition: avformat.h:474
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:135
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:449
static int match_format(const char *name, const char *names)
Definition: format.c:100
AVMediaType
Definition: avutil.h:185
Main libavformat public API header.
static AVInputFormat * first_iformat
head of registered input format linked list
Definition: format.c:31
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:93
AVInputFormat * av_iformat_next(AVInputFormat *f)
If f is NULL, returns the first registered input format, if f is non-NULL, returns the next registere...
Definition: format.c:35
int len
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:447
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
struct AVInputFormat * next
Definition: avformat.h:550
const char * extensions
comma-separated filename extensions
Definition: avformat.h:445
const char * mime_type
Definition: avformat.h:444