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.

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