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