You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
9.2KB

  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. #include "libavutil/avstring.h"
  22. #include "avio_internal.h"
  23. #include "avformat.h"
  24. #include "id3v2.h"
  25. #include "internal.h"
  26. /**
  27. * @file
  28. * Format register and lookup
  29. */
  30. /** head of registered input format linked list */
  31. static AVInputFormat *first_iformat = NULL;
  32. /** head of registered output format linked list */
  33. static AVOutputFormat *first_oformat = NULL;
  34. AVInputFormat *av_iformat_next(const AVInputFormat *f)
  35. {
  36. if (f)
  37. return f->next;
  38. else
  39. return first_iformat;
  40. }
  41. AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
  42. {
  43. if (f)
  44. return f->next;
  45. else
  46. return first_oformat;
  47. }
  48. void av_register_input_format(AVInputFormat *format)
  49. {
  50. AVInputFormat **p = &first_iformat;
  51. while (*p != NULL)
  52. p = &(*p)->next;
  53. *p = format;
  54. format->next = NULL;
  55. }
  56. void av_register_output_format(AVOutputFormat *format)
  57. {
  58. AVOutputFormat **p = &first_oformat;
  59. while (*p != NULL)
  60. p = &(*p)->next;
  61. *p = format;
  62. format->next = NULL;
  63. }
  64. int av_match_ext(const char *filename, const char *extensions)
  65. {
  66. const char *ext, *p;
  67. char ext1[32], *q;
  68. if (!filename)
  69. return 0;
  70. ext = strrchr(filename, '.');
  71. if (ext) {
  72. ext++;
  73. p = extensions;
  74. for (;;) {
  75. q = ext1;
  76. while (*p != '\0' && *p != ',' && q - ext1 < sizeof(ext1) - 1)
  77. *q++ = *p++;
  78. *q = '\0';
  79. if (!av_strcasecmp(ext1, ext))
  80. return 1;
  81. if (*p == '\0')
  82. break;
  83. p++;
  84. }
  85. }
  86. return 0;
  87. }
  88. static int match_format(const char *name, const char *names)
  89. {
  90. const char *p;
  91. int len, namelen;
  92. if (!name || !names)
  93. return 0;
  94. namelen = strlen(name);
  95. while ((p = strchr(names, ','))) {
  96. len = FFMAX(p - names, namelen);
  97. if (!av_strncasecmp(name, names, len))
  98. return 1;
  99. names = p + 1;
  100. }
  101. return !av_strcasecmp(name, names);
  102. }
  103. AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  104. const char *mime_type)
  105. {
  106. AVOutputFormat *fmt = NULL, *fmt_found;
  107. int score_max, score;
  108. /* specific test for image sequences */
  109. #if CONFIG_IMAGE2_MUXER
  110. if (!short_name && filename &&
  111. av_filename_number_test(filename) &&
  112. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  113. return av_guess_format("image2", NULL, NULL);
  114. }
  115. #endif
  116. /* Find the proper file type. */
  117. fmt_found = NULL;
  118. score_max = 0;
  119. while ((fmt = av_oformat_next(fmt))) {
  120. score = 0;
  121. if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
  122. score += 100;
  123. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  124. score += 10;
  125. if (filename && fmt->extensions &&
  126. av_match_ext(filename, fmt->extensions)) {
  127. score += 5;
  128. }
  129. if (score > score_max) {
  130. score_max = score;
  131. fmt_found = fmt;
  132. }
  133. }
  134. return fmt_found;
  135. }
  136. enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  137. const char *filename, const char *mime_type,
  138. enum AVMediaType type)
  139. {
  140. if (type == AVMEDIA_TYPE_VIDEO) {
  141. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  142. #if CONFIG_IMAGE2_MUXER
  143. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  144. codec_id = ff_guess_image2_codec(filename);
  145. }
  146. #endif
  147. if (codec_id == AV_CODEC_ID_NONE)
  148. codec_id = fmt->video_codec;
  149. return codec_id;
  150. } else if (type == AVMEDIA_TYPE_AUDIO)
  151. return fmt->audio_codec;
  152. else if (type == AVMEDIA_TYPE_SUBTITLE)
  153. return fmt->subtitle_codec;
  154. else
  155. return AV_CODEC_ID_NONE;
  156. }
  157. AVInputFormat *av_find_input_format(const char *short_name)
  158. {
  159. AVInputFormat *fmt = NULL;
  160. while ((fmt = av_iformat_next(fmt)))
  161. if (match_format(short_name, fmt->name))
  162. return fmt;
  163. return NULL;
  164. }
  165. AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
  166. int *score_max)
  167. {
  168. AVProbeData lpd = *pd;
  169. AVInputFormat *fmt1 = NULL, *fmt;
  170. int score, id3 = 0;
  171. if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
  172. int id3len = ff_id3v2_tag_len(lpd.buf);
  173. if (lpd.buf_size > id3len + 16) {
  174. lpd.buf += id3len;
  175. lpd.buf_size -= id3len;
  176. }
  177. id3 = 1;
  178. }
  179. fmt = NULL;
  180. while ((fmt1 = av_iformat_next(fmt1))) {
  181. if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
  182. continue;
  183. score = 0;
  184. if (fmt1->read_probe) {
  185. score = fmt1->read_probe(&lpd);
  186. } else if (fmt1->extensions) {
  187. if (av_match_ext(lpd.filename, fmt1->extensions))
  188. score = AVPROBE_SCORE_EXTENSION;
  189. }
  190. if (score > *score_max) {
  191. *score_max = score;
  192. fmt = fmt1;
  193. } else if (score == *score_max)
  194. fmt = NULL;
  195. }
  196. // A hack for files with huge id3v2 tags -- try to guess by file extension.
  197. if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
  198. while ((fmt = av_iformat_next(fmt)))
  199. if (fmt->extensions &&
  200. av_match_ext(lpd.filename, fmt->extensions)) {
  201. *score_max = AVPROBE_SCORE_EXTENSION / 2;
  202. break;
  203. }
  204. }
  205. if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
  206. while ((fmt = av_iformat_next(fmt)))
  207. if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
  208. *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
  209. break;
  210. }
  211. }
  212. return fmt;
  213. }
  214. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
  215. {
  216. int score = 0;
  217. return av_probe_input_format2(pd, is_opened, &score);
  218. }
  219. /* size of probe buffer, for guessing file type from file contents */
  220. #define PROBE_BUF_MIN 2048
  221. #define PROBE_BUF_MAX (1 << 20)
  222. int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
  223. const char *filename, void *logctx,
  224. unsigned int offset, unsigned int max_probe_size)
  225. {
  226. AVProbeData pd = { filename ? filename : "" };
  227. uint8_t *buf = NULL;
  228. int ret = 0, probe_size;
  229. if (!max_probe_size)
  230. max_probe_size = PROBE_BUF_MAX;
  231. else if (max_probe_size > PROBE_BUF_MAX)
  232. max_probe_size = PROBE_BUF_MAX;
  233. else if (max_probe_size < PROBE_BUF_MIN)
  234. return AVERROR(EINVAL);
  235. if (offset >= max_probe_size)
  236. return AVERROR(EINVAL);
  237. avio_skip(pb, offset);
  238. max_probe_size -= offset;
  239. for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
  240. probe_size = FFMIN(probe_size << 1,
  241. FFMAX(max_probe_size, probe_size + 1))) {
  242. int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
  243. /* Read probe data. */
  244. if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
  245. return ret;
  246. if ((ret = avio_read(pb, buf + pd.buf_size,
  247. probe_size - pd.buf_size)) < 0) {
  248. /* Fail if error was not end of file, otherwise, lower score. */
  249. if (ret != AVERROR_EOF) {
  250. av_free(buf);
  251. return ret;
  252. }
  253. score = 0;
  254. ret = 0; /* error was end of file, nothing read */
  255. }
  256. pd.buf_size += ret;
  257. pd.buf = buf;
  258. memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
  259. /* Guess file format. */
  260. *fmt = av_probe_input_format2(&pd, 1, &score);
  261. if (*fmt) {
  262. /* This can only be true in the last iteration. */
  263. if (score <= AVPROBE_SCORE_MAX / 4) {
  264. av_log(logctx, AV_LOG_WARNING,
  265. "Format detected only with low score of %d, "
  266. "misdetection possible!\n", score);
  267. } else
  268. av_log(logctx, AV_LOG_DEBUG,
  269. "Probed with size=%d and score=%d\n", probe_size, score);
  270. }
  271. }
  272. if (!*fmt) {
  273. av_free(buf);
  274. return AVERROR_INVALIDDATA;
  275. }
  276. /* Rewind. Reuse probe buffer to avoid seeking. */
  277. if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
  278. av_free(buf);
  279. return ret;
  280. }