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.

338 lines
11KB

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