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.

303 lines
8.8KB

  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. AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  89. const char *mime_type)
  90. {
  91. AVOutputFormat *fmt = NULL, *fmt_found;
  92. int score_max, score;
  93. /* specific test for image sequences */
  94. #if CONFIG_IMAGE2_MUXER
  95. if (!short_name && filename &&
  96. av_filename_number_test(filename) &&
  97. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  98. return av_guess_format("image2", NULL, NULL);
  99. }
  100. #endif
  101. /* Find the proper file type. */
  102. fmt_found = NULL;
  103. score_max = 0;
  104. while ((fmt = av_oformat_next(fmt))) {
  105. score = 0;
  106. if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
  107. score += 100;
  108. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  109. score += 10;
  110. if (filename && fmt->extensions &&
  111. av_match_ext(filename, fmt->extensions)) {
  112. score += 5;
  113. }
  114. if (score > score_max) {
  115. score_max = score;
  116. fmt_found = fmt;
  117. }
  118. }
  119. return fmt_found;
  120. }
  121. enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
  122. const char *filename, const char *mime_type,
  123. enum AVMediaType type)
  124. {
  125. if (type == AVMEDIA_TYPE_VIDEO) {
  126. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  127. #if CONFIG_IMAGE2_MUXER
  128. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  129. codec_id = ff_guess_image2_codec(filename);
  130. }
  131. #endif
  132. if (codec_id == AV_CODEC_ID_NONE)
  133. codec_id = fmt->video_codec;
  134. return codec_id;
  135. } else if (type == AVMEDIA_TYPE_AUDIO)
  136. return fmt->audio_codec;
  137. else if (type == AVMEDIA_TYPE_SUBTITLE)
  138. return fmt->subtitle_codec;
  139. else
  140. return AV_CODEC_ID_NONE;
  141. }
  142. AVInputFormat *av_find_input_format(const char *short_name)
  143. {
  144. AVInputFormat *fmt = NULL;
  145. while ((fmt = av_iformat_next(fmt)))
  146. if (av_match_name(short_name, fmt->name))
  147. return fmt;
  148. return NULL;
  149. }
  150. AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
  151. int *score_max)
  152. {
  153. AVProbeData lpd = *pd;
  154. AVInputFormat *fmt1 = NULL, *fmt;
  155. int score, id3 = 0;
  156. if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
  157. int id3len = ff_id3v2_tag_len(lpd.buf);
  158. if (lpd.buf_size > id3len + 16) {
  159. lpd.buf += id3len;
  160. lpd.buf_size -= id3len;
  161. }
  162. id3 = 1;
  163. }
  164. fmt = NULL;
  165. while ((fmt1 = av_iformat_next(fmt1))) {
  166. if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
  167. continue;
  168. score = 0;
  169. if (fmt1->read_probe) {
  170. score = fmt1->read_probe(&lpd);
  171. } else if (fmt1->extensions) {
  172. if (av_match_ext(lpd.filename, fmt1->extensions))
  173. score = AVPROBE_SCORE_EXTENSION;
  174. }
  175. if (score > *score_max) {
  176. *score_max = score;
  177. fmt = fmt1;
  178. } else if (score == *score_max)
  179. fmt = NULL;
  180. }
  181. // A hack for files with huge id3v2 tags -- try to guess by file extension.
  182. if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
  183. while ((fmt = av_iformat_next(fmt)))
  184. if (fmt->extensions &&
  185. av_match_ext(lpd.filename, fmt->extensions)) {
  186. *score_max = AVPROBE_SCORE_EXTENSION / 2;
  187. break;
  188. }
  189. }
  190. if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
  191. while ((fmt = av_iformat_next(fmt)))
  192. if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
  193. *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
  194. break;
  195. }
  196. }
  197. return fmt;
  198. }
  199. AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
  200. {
  201. int score = 0;
  202. return av_probe_input_format2(pd, is_opened, &score);
  203. }
  204. /* size of probe buffer, for guessing file type from file contents */
  205. #define PROBE_BUF_MIN 2048
  206. #define PROBE_BUF_MAX (1 << 20)
  207. int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
  208. const char *filename, void *logctx,
  209. unsigned int offset, unsigned int max_probe_size)
  210. {
  211. AVProbeData pd = { filename ? filename : "" };
  212. uint8_t *buf = NULL;
  213. int ret = 0, probe_size;
  214. if (!max_probe_size)
  215. max_probe_size = PROBE_BUF_MAX;
  216. else if (max_probe_size > PROBE_BUF_MAX)
  217. max_probe_size = PROBE_BUF_MAX;
  218. else if (max_probe_size < PROBE_BUF_MIN)
  219. return AVERROR(EINVAL);
  220. if (offset >= max_probe_size)
  221. return AVERROR(EINVAL);
  222. avio_skip(pb, offset);
  223. max_probe_size -= offset;
  224. for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
  225. probe_size = FFMIN(probe_size << 1,
  226. FFMAX(max_probe_size, probe_size + 1))) {
  227. int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
  228. /* Read probe data. */
  229. if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
  230. return ret;
  231. if ((ret = avio_read(pb, buf + pd.buf_size,
  232. probe_size - pd.buf_size)) < 0) {
  233. /* Fail if error was not end of file, otherwise, lower score. */
  234. if (ret != AVERROR_EOF) {
  235. av_free(buf);
  236. return ret;
  237. }
  238. score = 0;
  239. ret = 0; /* error was end of file, nothing read */
  240. }
  241. pd.buf_size += ret;
  242. pd.buf = buf;
  243. memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
  244. /* Guess file format. */
  245. *fmt = av_probe_input_format2(&pd, 1, &score);
  246. if (*fmt) {
  247. /* This can only be true in the last iteration. */
  248. if (score <= AVPROBE_SCORE_MAX / 4) {
  249. av_log(logctx, AV_LOG_WARNING,
  250. "Format detected only with low score of %d, "
  251. "misdetection possible!\n", score);
  252. } else
  253. av_log(logctx, AV_LOG_DEBUG,
  254. "Probed with size=%d and score=%d\n", probe_size, score);
  255. }
  256. }
  257. if (!*fmt) {
  258. av_free(buf);
  259. return AVERROR_INVALIDDATA;
  260. }
  261. /* Rewind. Reuse probe buffer to avoid seeking. */
  262. if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
  263. av_free(buf);
  264. return ret;
  265. }