FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
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  format->next = NULL;
60  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
61  p = &(*p)->next;
62  last_iformat = &format->next;
63 }
64 
66 {
68 
69  format->next = NULL;
70  while(*p || avpriv_atomic_ptr_cas((void * volatile *)p, NULL, format))
71  p = &(*p)->next;
72  last_oformat = &format->next;
73 }
74 
75 int av_match_ext(const char *filename, const char *extensions)
76 {
77  const char *ext, *p;
78  char ext1[32], *q;
79 
80  if (!filename)
81  return 0;
82 
83  ext = strrchr(filename, '.');
84  if (ext) {
85  ext++;
86  p = extensions;
87  for (;;) {
88  q = ext1;
89  while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
90  *q++ = *p++;
91  *q = '\0';
92  if (!av_strcasecmp(ext1, ext))
93  return 1;
94  if (*p == '\0')
95  break;
96  p++;
97  }
98  }
99  return 0;
100 }
101 
102 static int match_format(const char *name, const char *names)
103 {
104  const char *p;
105  int len, namelen;
106 
107  if (!name || !names)
108  return 0;
109 
110  namelen = strlen(name);
111  while ((p = strchr(names, ','))) {
112  len = FFMAX(p - names, namelen);
113  if (!av_strncasecmp(name, names, len))
114  return 1;
115  names = p + 1;
116  }
117  return !av_strcasecmp(name, names);
118 }
119 
120 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
121  const char *mime_type)
122 {
123  AVOutputFormat *fmt = NULL, *fmt_found;
124  int score_max, score;
125 
126  /* specific test for image sequences */
127 #if CONFIG_IMAGE2_MUXER
128  if (!short_name && filename &&
129  av_filename_number_test(filename) &&
131  return av_guess_format("image2", NULL, NULL);
132  }
133 #endif
134  /* Find the proper file type. */
135  fmt_found = NULL;
136  score_max = 0;
137  while ((fmt = av_oformat_next(fmt))) {
138  score = 0;
139  if (fmt->name && short_name && match_format(short_name, fmt->name))
140  score += 100;
141  if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
142  score += 10;
143  if (filename && fmt->extensions &&
144  av_match_ext(filename, fmt->extensions)) {
145  score += 5;
146  }
147  if (score > score_max) {
148  score_max = score;
149  fmt_found = fmt;
150  }
151  }
152  return fmt_found;
153 }
154 
155 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
156  const char *filename, const char *mime_type,
157  enum AVMediaType type)
158 {
159  if (!strcmp(fmt->name, "segment") || !strcmp(fmt->name, "ssegment")) {
160  fmt = av_guess_format(NULL, filename, NULL);
161  }
162 
163  if (type == AVMEDIA_TYPE_VIDEO) {
165 
166 #if CONFIG_IMAGE2_MUXER
167  if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
168  codec_id = ff_guess_image2_codec(filename);
169  }
170 #endif
171  if (codec_id == AV_CODEC_ID_NONE)
172  codec_id = fmt->video_codec;
173  return codec_id;
174  } else if (type == AVMEDIA_TYPE_AUDIO)
175  return fmt->audio_codec;
176  else if (type == AVMEDIA_TYPE_SUBTITLE)
177  return fmt->subtitle_codec;
178  else
179  return AV_CODEC_ID_NONE;
180 }
181 
182 AVInputFormat *av_find_input_format(const char *short_name)
183 {
184  AVInputFormat *fmt = NULL;
185  while ((fmt = av_iformat_next(fmt)))
186  if (match_format(short_name, fmt->name))
187  return fmt;
188  return NULL;
189 }