PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/mythtv/external/FFmpeg/libavformat/format.c

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