PageRenderTime 129ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libavformat/format.c

http://github.com/FFmpeg/FFmpeg
C | 318 lines | 254 code | 33 blank | 31 comment | 80 complexity | e3f5ca23383c0802a409ed7cd6892594 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0
  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. #include "libavutil/avstring.h"
  22. #include "libavutil/bprint.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/thread.h"
  25. #include "avio_internal.h"
  26. #include "avformat.h"
  27. #include "id3v2.h"
  28. #include "internal.h"
  29. /**
  30. * @file
  31. * Format register and lookup
  32. */
  33. int av_match_ext(const char *filename, const char *extensions)
  34. {
  35. const char *ext;
  36. if (!filename)
  37. return 0;
  38. ext = strrchr(filename, '.');
  39. if (ext)
  40. return av_match_name(ext + 1, extensions);
  41. return 0;
  42. }
  43. ff_const59 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
  44. const char *mime_type)
  45. {
  46. const AVOutputFormat *fmt = NULL;
  47. AVOutputFormat *fmt_found = NULL;
  48. void *i = 0;
  49. int score_max, score;
  50. /* specific test for image sequences */
  51. #if CONFIG_IMAGE2_MUXER
  52. if (!short_name && filename &&
  53. av_filename_number_test(filename) &&
  54. ff_guess_image2_codec(filename) != AV_CODEC_ID_NONE) {
  55. return av_guess_format("image2", NULL, NULL);
  56. }
  57. #endif
  58. /* Find the proper file type. */
  59. score_max = 0;
  60. while ((fmt = av_muxer_iterate(&i))) {
  61. score = 0;
  62. if (fmt->name && short_name && av_match_name(short_name, fmt->name))
  63. score += 100;
  64. if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
  65. score += 10;
  66. if (filename && fmt->extensions &&
  67. av_match_ext(filename, fmt->extensions)) {
  68. score += 5;
  69. }
  70. if (score > score_max) {
  71. score_max = score;
  72. fmt_found = (AVOutputFormat*)fmt;
  73. }
  74. }
  75. return fmt_found;
  76. }
  77. enum AVCodecID av_guess_codec(ff_const59 AVOutputFormat *fmt, const char *short_name,
  78. const char *filename, const char *mime_type,
  79. enum AVMediaType type)
  80. {
  81. if (av_match_name("segment", fmt->name) || av_match_name("ssegment", fmt->name)) {
  82. ff_const59 AVOutputFormat *fmt2 = av_guess_format(NULL, filename, NULL);
  83. if (fmt2)
  84. fmt = fmt2;
  85. }
  86. if (type == AVMEDIA_TYPE_VIDEO) {
  87. enum AVCodecID codec_id = AV_CODEC_ID_NONE;
  88. #if CONFIG_IMAGE2_MUXER
  89. if (!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")) {
  90. codec_id = ff_guess_image2_codec(filename);
  91. }
  92. #endif
  93. if (codec_id == AV_CODEC_ID_NONE)
  94. codec_id = fmt->video_codec;
  95. return codec_id;
  96. } else if (type == AVMEDIA_TYPE_AUDIO)
  97. return fmt->audio_codec;
  98. else if (type == AVMEDIA_TYPE_SUBTITLE)
  99. return fmt->subtitle_codec;
  100. else if (type == AVMEDIA_TYPE_DATA)
  101. return fmt->data_codec;
  102. else
  103. return AV_CODEC_ID_NONE;
  104. }
  105. ff_const59 AVInputFormat *av_find_input_format(const char *short_name)
  106. {
  107. const AVInputFormat *fmt = NULL;
  108. void *i = 0;
  109. while ((fmt = av_demuxer_iterate(&i)))
  110. if (av_match_name(short_name, fmt->name))
  111. return (AVInputFormat*)fmt;
  112. return NULL;
  113. }
  114. ff_const59 AVInputFormat *av_probe_input_format3(ff_const59 AVProbeData *pd, int is_opened,
  115. int *score_ret)
  116. {
  117. AVProbeData lpd = *pd;
  118. const AVInputFormat *fmt1 = NULL;
  119. ff_const59 AVInputFormat *fmt = NULL;
  120. int score, score_max = 0;
  121. void *i = 0;
  122. const static uint8_t zerobuffer[AVPROBE_PADDING_SIZE];
  123. enum nodat {
  124. NO_ID3,
  125. ID3_ALMOST_GREATER_PROBE,
  126. ID3_GREATER_PROBE,
  127. ID3_GREATER_MAX_PROBE,
  128. } nodat = NO_ID3;
  129. if (!lpd.buf)
  130. lpd.buf = (unsigned char *) zerobuffer;
  131. if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
  132. int id3len = ff_id3v2_tag_len(lpd.buf);
  133. if (lpd.buf_size > id3len + 16) {
  134. if (lpd.buf_size < 2LL*id3len + 16)
  135. nodat = ID3_ALMOST_GREATER_PROBE;
  136. lpd.buf += id3len;
  137. lpd.buf_size -= id3len;
  138. } else if (id3len >= PROBE_BUF_MAX) {
  139. nodat = ID3_GREATER_MAX_PROBE;
  140. } else
  141. nodat = ID3_GREATER_PROBE;
  142. }
  143. while ((fmt1 = av_demuxer_iterate(&i))) {
  144. if (!is_opened == !(fmt1->flags & AVFMT_NOFILE) && strcmp(fmt1->name, "image2"))
  145. continue;
  146. score = 0;
  147. if (fmt1->read_probe) {
  148. score = fmt1->read_probe(&lpd);
  149. if (score)
  150. av_log(NULL, AV_LOG_TRACE, "Probing %s score:%d size:%d\n", fmt1->name, score, lpd.buf_size);
  151. if (fmt1->extensions && av_match_ext(lpd.filename, fmt1->extensions)) {
  152. switch (nodat) {
  153. case NO_ID3:
  154. score = FFMAX(score, 1);
  155. break;
  156. case ID3_GREATER_PROBE:
  157. case ID3_ALMOST_GREATER_PROBE:
  158. score = FFMAX(score, AVPROBE_SCORE_EXTENSION / 2 - 1);
  159. break;
  160. case ID3_GREATER_MAX_PROBE:
  161. score = FFMAX(score, AVPROBE_SCORE_EXTENSION);
  162. break;
  163. }
  164. }
  165. } else if (fmt1->extensions) {
  166. if (av_match_ext(lpd.filename, fmt1->extensions))
  167. score = AVPROBE_SCORE_EXTENSION;
  168. }
  169. if (av_match_name(lpd.mime_type, fmt1->mime_type)) {
  170. if (AVPROBE_SCORE_MIME > score) {
  171. av_log(NULL, AV_LOG_DEBUG, "Probing %s score:%d increased to %d due to MIME type\n", fmt1->name, score, AVPROBE_SCORE_MIME);
  172. score = AVPROBE_SCORE_MIME;
  173. }
  174. }
  175. if (score > score_max) {
  176. score_max = score;
  177. fmt = (AVInputFormat*)fmt1;
  178. } else if (score == score_max)
  179. fmt = NULL;
  180. }
  181. if (nodat == ID3_GREATER_PROBE)
  182. score_max = FFMIN(AVPROBE_SCORE_EXTENSION / 2 - 1, score_max);
  183. *score_ret = score_max;
  184. return fmt;
  185. }
  186. ff_const59 AVInputFormat *av_probe_input_format2(ff_const59 AVProbeData *pd, int is_opened, int *score_max)
  187. {
  188. int score_ret;
  189. ff_const59 AVInputFormat *fmt = av_probe_input_format3(pd, is_opened, &score_ret);
  190. if (score_ret > *score_max) {
  191. *score_max = score_ret;
  192. return fmt;
  193. } else
  194. return NULL;
  195. }
  196. ff_const59 AVInputFormat *av_probe_input_format(ff_const59 AVProbeData *pd, int is_opened)
  197. {
  198. int score = 0;
  199. return av_probe_input_format2(pd, is_opened, &score);
  200. }
  201. int av_probe_input_buffer2(AVIOContext *pb, ff_const59 AVInputFormat **fmt,
  202. const char *filename, void *logctx,
  203. unsigned int offset, unsigned int max_probe_size)
  204. {
  205. AVProbeData pd = { filename ? filename : "" };
  206. uint8_t *buf = NULL;
  207. int ret = 0, probe_size, buf_offset = 0;
  208. int score = 0;
  209. int ret2;
  210. if (!max_probe_size)
  211. max_probe_size = PROBE_BUF_MAX;
  212. else if (max_probe_size < PROBE_BUF_MIN) {
  213. av_log(logctx, AV_LOG_ERROR,
  214. "Specified probe size value %u cannot be < %u\n", max_probe_size, PROBE_BUF_MIN);
  215. return AVERROR(EINVAL);
  216. }
  217. if (offset >= max_probe_size)
  218. return AVERROR(EINVAL);
  219. if (pb->av_class) {
  220. uint8_t *mime_type_opt = NULL;
  221. char *semi;
  222. av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
  223. pd.mime_type = (const char *)mime_type_opt;
  224. semi = pd.mime_type ? strchr(pd.mime_type, ';') : NULL;
  225. if (semi) {
  226. *semi = '\0';
  227. }
  228. }
  229. for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
  230. probe_size = FFMIN(probe_size << 1,
  231. FFMAX(max_probe_size, probe_size + 1))) {
  232. score = probe_size < max_probe_size ? AVPROBE_SCORE_RETRY : 0;
  233. /* Read probe data. */
  234. if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
  235. goto fail;
  236. if ((ret = avio_read(pb, buf + buf_offset,
  237. probe_size - buf_offset)) < 0) {
  238. /* Fail if error was not end of file, otherwise, lower score. */
  239. if (ret != AVERROR_EOF)
  240. goto fail;
  241. score = 0;
  242. ret = 0; /* error was end of file, nothing read */
  243. }
  244. buf_offset += ret;
  245. if (buf_offset < offset)
  246. continue;
  247. pd.buf_size = buf_offset - offset;
  248. pd.buf = &buf[offset];
  249. memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
  250. /* Guess file format. */
  251. *fmt = av_probe_input_format2(&pd, 1, &score);
  252. if (*fmt) {
  253. /* This can only be true in the last iteration. */
  254. if (score <= AVPROBE_SCORE_RETRY) {
  255. av_log(logctx, AV_LOG_WARNING,
  256. "Format %s detected only with low score of %d, "
  257. "misdetection possible!\n", (*fmt)->name, score);
  258. } else
  259. av_log(logctx, AV_LOG_DEBUG,
  260. "Format %s probed with size=%d and score=%d\n",
  261. (*fmt)->name, probe_size, score);
  262. #if 0
  263. FILE *f = fopen("probestat.tmp", "ab");
  264. fprintf(f, "probe_size:%d format:%s score:%d filename:%s\n", probe_size, (*fmt)->name, score, filename);
  265. fclose(f);
  266. #endif
  267. }
  268. }
  269. if (!*fmt)
  270. ret = AVERROR_INVALIDDATA;
  271. fail:
  272. /* Rewind. Reuse probe buffer to avoid seeking. */
  273. ret2 = ffio_rewind_with_probe_data(pb, &buf, buf_offset);
  274. if (ret >= 0)
  275. ret = ret2;
  276. av_freep(&pd.mime_type);
  277. return ret < 0 ? ret : score;
  278. }
  279. int av_probe_input_buffer(AVIOContext *pb, ff_const59 AVInputFormat **fmt,
  280. const char *filename, void *logctx,
  281. unsigned int offset, unsigned int max_probe_size)
  282. {
  283. int ret = av_probe_input_buffer2(pb, fmt, filename, logctx, offset, max_probe_size);
  284. return ret < 0 ? ret : 0;
  285. }