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.

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