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.

371 lines
12KB

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