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.

311 lines
9.1KB

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