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.

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