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.

729 lines
23KB

  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2004 Michael Niedermayer
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #define _BSD_SOURCE
  23. #include <sys/stat.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/log.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "internal.h"
  33. #include "img2.h"
  34. #if HAVE_GLOB
  35. /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
  36. are non-posix glibc/bsd extensions. */
  37. #ifndef GLOB_NOMAGIC
  38. #define GLOB_NOMAGIC 0
  39. #endif
  40. #ifndef GLOB_BRACE
  41. #define GLOB_BRACE 0
  42. #endif
  43. #endif /* HAVE_GLOB */
  44. static const int sizes[][2] = {
  45. { 640, 480 },
  46. { 720, 480 },
  47. { 720, 576 },
  48. { 352, 288 },
  49. { 352, 240 },
  50. { 160, 128 },
  51. { 512, 384 },
  52. { 640, 352 },
  53. { 640, 240 },
  54. };
  55. static int infer_size(int *width_ptr, int *height_ptr, int size)
  56. {
  57. int i;
  58. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  59. if ((sizes[i][0] * sizes[i][1]) == size) {
  60. *width_ptr = sizes[i][0];
  61. *height_ptr = sizes[i][1];
  62. return 0;
  63. }
  64. }
  65. return -1;
  66. }
  67. static int is_glob(const char *path)
  68. {
  69. #if HAVE_GLOB
  70. size_t span = 0;
  71. const char *p = path;
  72. while (p = strchr(p, '%')) {
  73. if (*(++p) == '%') {
  74. ++p;
  75. continue;
  76. }
  77. if (span = strspn(p, "*?[]{}"))
  78. break;
  79. }
  80. /* Did we hit a glob char or get to the end? */
  81. return span != 0;
  82. #else
  83. return 0;
  84. #endif
  85. }
  86. /**
  87. * Get index range of image files matched by path.
  88. *
  89. * @param pfirst_index pointer to index updated with the first number in the range
  90. * @param plast_index pointer to index updated with the last number in the range
  91. * @param path path which has to be matched by the image files in the range
  92. * @param start_index minimum accepted value for the first index in the range
  93. * @return -1 if no image file could be found
  94. */
  95. static int find_image_range(int *pfirst_index, int *plast_index,
  96. const char *path, int start_index, int start_index_range)
  97. {
  98. char buf[1024];
  99. int range, last_index, range1, first_index;
  100. /* find the first image */
  101. for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
  102. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  103. *pfirst_index =
  104. *plast_index = 1;
  105. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  106. return 0;
  107. return -1;
  108. }
  109. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  110. break;
  111. }
  112. if (first_index == start_index + start_index_range)
  113. goto fail;
  114. /* find the last image */
  115. last_index = first_index;
  116. for (;;) {
  117. range = 0;
  118. for (;;) {
  119. if (!range)
  120. range1 = 1;
  121. else
  122. range1 = 2 * range;
  123. if (av_get_frame_filename(buf, sizeof(buf), path,
  124. last_index + range1) < 0)
  125. goto fail;
  126. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  127. break;
  128. range = range1;
  129. /* just in case... */
  130. if (range >= (1 << 30))
  131. goto fail;
  132. }
  133. /* we are sure than image last_index + range exists */
  134. if (!range)
  135. break;
  136. last_index += range;
  137. }
  138. *pfirst_index = first_index;
  139. *plast_index = last_index;
  140. return 0;
  141. fail:
  142. return -1;
  143. }
  144. static int img_read_probe(AVProbeData *p)
  145. {
  146. if (p->filename && ff_guess_image2_codec(p->filename)) {
  147. if (av_filename_number_test(p->filename))
  148. return AVPROBE_SCORE_MAX;
  149. else if (is_glob(p->filename))
  150. return AVPROBE_SCORE_MAX;
  151. else if (p->buf_size == 0)
  152. return 0;
  153. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  154. return 5;
  155. else
  156. return AVPROBE_SCORE_EXTENSION;
  157. }
  158. return 0;
  159. }
  160. int ff_img_read_header(AVFormatContext *s1)
  161. {
  162. VideoDemuxData *s = s1->priv_data;
  163. int first_index, last_index;
  164. AVStream *st;
  165. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  166. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  167. st = avformat_new_stream(s1, NULL);
  168. if (!st) {
  169. return AVERROR(ENOMEM);
  170. }
  171. if (s->pixel_format &&
  172. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  173. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  174. s->pixel_format);
  175. return AVERROR(EINVAL);
  176. }
  177. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  178. s->img_number = 0;
  179. s->img_count = 0;
  180. /* find format */
  181. if (s1->iformat->flags & AVFMT_NOFILE)
  182. s->is_pipe = 0;
  183. else {
  184. s->is_pipe = 1;
  185. st->need_parsing = AVSTREAM_PARSE_FULL;
  186. }
  187. if (s->ts_from_file == 2) {
  188. #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  189. av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
  190. return AVERROR(ENOSYS);
  191. #endif
  192. avpriv_set_pts_info(st, 64, 1, 1000000000);
  193. } else if (s->ts_from_file)
  194. avpriv_set_pts_info(st, 64, 1, 1);
  195. else
  196. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  197. if (s->width && s->height) {
  198. st->codec->width = s->width;
  199. st->codec->height = s->height;
  200. }
  201. if (!s->is_pipe) {
  202. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  203. s->use_glob = is_glob(s->path);
  204. if (s->use_glob) {
  205. char *p = s->path, *q, *dup;
  206. int gerr;
  207. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  208. "use pattern_type 'glob' instead\n");
  209. #if HAVE_GLOB
  210. dup = q = av_strdup(p);
  211. while (*q) {
  212. /* Do we have room for the next char and a \ insertion? */
  213. if ((p - s->path) >= (sizeof(s->path) - 2))
  214. break;
  215. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  216. ++q;
  217. else if (strspn(q, "\\*?[]{}"))
  218. *p++ = '\\';
  219. *p++ = *q++;
  220. }
  221. *p = 0;
  222. av_free(dup);
  223. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  224. if (gerr != 0) {
  225. return AVERROR(ENOENT);
  226. }
  227. first_index = 0;
  228. last_index = s->globstate.gl_pathc - 1;
  229. #endif
  230. }
  231. }
  232. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  233. if (find_image_range(&first_index, &last_index, s->path,
  234. s->start_number, s->start_number_range) < 0) {
  235. av_log(s1, AV_LOG_ERROR,
  236. "Could find no file with path '%s' and index in the range %d-%d\n",
  237. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  238. return AVERROR(ENOENT);
  239. }
  240. } else if (s->pattern_type == PT_GLOB) {
  241. #if HAVE_GLOB
  242. int gerr;
  243. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  244. if (gerr != 0) {
  245. return AVERROR(ENOENT);
  246. }
  247. first_index = 0;
  248. last_index = s->globstate.gl_pathc - 1;
  249. s->use_glob = 1;
  250. #else
  251. av_log(s1, AV_LOG_ERROR,
  252. "Pattern type 'glob' was selected but globbing "
  253. "is not supported by this libavformat build\n");
  254. return AVERROR(ENOSYS);
  255. #endif
  256. } else if (s->pattern_type != PT_GLOB_SEQUENCE) {
  257. av_log(s1, AV_LOG_ERROR,
  258. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  259. return AVERROR(EINVAL);
  260. }
  261. s->img_first = first_index;
  262. s->img_last = last_index;
  263. s->img_number = first_index;
  264. /* compute duration */
  265. if (!s->ts_from_file) {
  266. st->start_time = 0;
  267. st->duration = last_index - first_index + 1;
  268. }
  269. }
  270. if (s1->video_codec_id) {
  271. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  272. st->codec->codec_id = s1->video_codec_id;
  273. } else if (s1->audio_codec_id) {
  274. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  275. st->codec->codec_id = s1->audio_codec_id;
  276. } else if (s1->iformat->raw_codec_id) {
  277. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  278. st->codec->codec_id = s1->iformat->raw_codec_id;
  279. } else {
  280. const char *str = strrchr(s->path, '.');
  281. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  282. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  283. if (s1->pb) {
  284. int probe_buffer_size = 2048;
  285. uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
  286. AVInputFormat *fmt = NULL;
  287. AVProbeData pd = { 0 };
  288. if (!probe_buffer)
  289. return AVERROR(ENOMEM);
  290. probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
  291. if (probe_buffer_size < 0) {
  292. av_free(probe_buffer);
  293. return probe_buffer_size;
  294. }
  295. memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
  296. pd.buf = probe_buffer;
  297. pd.buf_size = probe_buffer_size;
  298. pd.filename = s1->filename;
  299. while ((fmt = av_iformat_next(fmt))) {
  300. if (fmt->read_header != ff_img_read_header ||
  301. !fmt->read_probe ||
  302. (fmt->flags & AVFMT_NOFILE) ||
  303. !fmt->raw_codec_id)
  304. continue;
  305. if (fmt->read_probe(&pd) > 0) {
  306. st->codec->codec_id = fmt->raw_codec_id;
  307. break;
  308. }
  309. }
  310. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  311. }
  312. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  313. st->codec->codec_id = ff_guess_image2_codec(s->path);
  314. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  315. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  316. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  317. st->codec->codec_id = AV_CODEC_ID_NONE;
  318. }
  319. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  320. pix_fmt != AV_PIX_FMT_NONE)
  321. st->codec->pix_fmt = pix_fmt;
  322. return 0;
  323. }
  324. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  325. {
  326. VideoDemuxData *s = s1->priv_data;
  327. char filename_bytes[1024];
  328. char *filename = filename_bytes;
  329. int i;
  330. int size[3] = { 0 }, ret[3] = { 0 };
  331. AVIOContext *f[3] = { NULL };
  332. AVCodecContext *codec = s1->streams[0]->codec;
  333. if (!s->is_pipe) {
  334. /* loop over input */
  335. if (s->loop && s->img_number > s->img_last) {
  336. s->img_number = s->img_first;
  337. }
  338. if (s->img_number > s->img_last)
  339. return AVERROR_EOF;
  340. if (s->use_glob) {
  341. #if HAVE_GLOB
  342. filename = s->globstate.gl_pathv[s->img_number];
  343. #endif
  344. } else {
  345. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  346. s->path,
  347. s->img_number) < 0 && s->img_number > 1)
  348. return AVERROR(EIO);
  349. }
  350. for (i = 0; i < 3; i++) {
  351. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  352. &s1->interrupt_callback, NULL) < 0) {
  353. if (i >= 1)
  354. break;
  355. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  356. filename);
  357. return AVERROR(EIO);
  358. }
  359. size[i] = avio_size(f[i]);
  360. if (!s->split_planes)
  361. break;
  362. filename[strlen(filename) - 1] = 'U' + i;
  363. }
  364. if (codec->codec_id == AV_CODEC_ID_NONE) {
  365. AVProbeData pd = { 0 };
  366. AVInputFormat *ifmt;
  367. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  368. int ret;
  369. int score = 0;
  370. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  371. if (ret < 0)
  372. return ret;
  373. memset(header + ret, 0, sizeof(header) - ret);
  374. avio_skip(f[0], -ret);
  375. pd.buf = header;
  376. pd.buf_size = ret;
  377. pd.filename = filename;
  378. ifmt = av_probe_input_format3(&pd, 1, &score);
  379. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  380. codec->codec_id = ifmt->raw_codec_id;
  381. }
  382. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  383. infer_size(&codec->width, &codec->height, size[0]);
  384. } else {
  385. f[0] = s1->pb;
  386. if (avio_feof(f[0]) && s->loop && s->is_pipe)
  387. avio_seek(f[0], 0, SEEK_SET);
  388. if (avio_feof(f[0]))
  389. return AVERROR_EOF;
  390. if (s->frame_size > 0) {
  391. size[0] = s->frame_size;
  392. } else if (!s1->streams[0]->parser) {
  393. size[0] = avio_size(s1->pb);
  394. } else {
  395. size[0] = 4096;
  396. }
  397. }
  398. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  399. return AVERROR(ENOMEM);
  400. pkt->stream_index = 0;
  401. pkt->flags |= AV_PKT_FLAG_KEY;
  402. if (s->ts_from_file) {
  403. struct stat img_stat;
  404. if (stat(filename, &img_stat))
  405. return AVERROR(EIO);
  406. pkt->pts = (int64_t)img_stat.st_mtime;
  407. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  408. if (s->ts_from_file == 2)
  409. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  410. #endif
  411. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  412. } else if (!s->is_pipe) {
  413. pkt->pts = s->pts;
  414. }
  415. if (s->is_pipe)
  416. pkt->pos = avio_tell(f[0]);
  417. pkt->size = 0;
  418. for (i = 0; i < 3; i++) {
  419. if (f[i]) {
  420. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  421. if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
  422. if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
  423. pkt->pos = 0;
  424. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  425. }
  426. }
  427. if (!s->is_pipe)
  428. avio_close(f[i]);
  429. if (ret[i] > 0)
  430. pkt->size += ret[i];
  431. }
  432. }
  433. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  434. av_free_packet(pkt);
  435. if (ret[0] < 0) {
  436. return ret[0];
  437. } else if (ret[1] < 0) {
  438. return ret[1];
  439. } else if (ret[2] < 0)
  440. return ret[2];
  441. return AVERROR_EOF;
  442. } else {
  443. s->img_count++;
  444. s->img_number++;
  445. s->pts++;
  446. return 0;
  447. }
  448. }
  449. static int img_read_close(struct AVFormatContext* s1)
  450. {
  451. VideoDemuxData *s = s1->priv_data;
  452. #if HAVE_GLOB
  453. if (s->use_glob) {
  454. globfree(&s->globstate);
  455. }
  456. #endif
  457. return 0;
  458. }
  459. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  460. {
  461. VideoDemuxData *s1 = s->priv_data;
  462. AVStream *st = s->streams[0];
  463. if (s1->ts_from_file) {
  464. int index = av_index_search_timestamp(st, timestamp, flags);
  465. if(index < 0)
  466. return -1;
  467. s1->img_number = st->index_entries[index].pos;
  468. return 0;
  469. }
  470. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  471. return -1;
  472. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  473. s1->pts = timestamp;
  474. return 0;
  475. }
  476. #define OFFSET(x) offsetof(VideoDemuxData, x)
  477. #define DEC AV_OPT_FLAG_DECODING_PARAM
  478. const AVOption ff_img_options[] = {
  479. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  480. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  481. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  482. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  483. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  484. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  485. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  486. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  487. { "start_number_range", "set range for looking at the first sequence number", OFFSET(start_number_range), AV_OPT_TYPE_INT, {.i64 = 5}, 1, INT_MAX, DEC },
  488. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  489. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  490. { "ts_from_file", "set frame timestamp from file's one", OFFSET(ts_from_file), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  491. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  492. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  493. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  494. { NULL },
  495. };
  496. #if CONFIG_IMAGE2_DEMUXER
  497. static const AVClass img2_class = {
  498. .class_name = "image2 demuxer",
  499. .item_name = av_default_item_name,
  500. .option = ff_img_options,
  501. .version = LIBAVUTIL_VERSION_INT,
  502. };
  503. AVInputFormat ff_image2_demuxer = {
  504. .name = "image2",
  505. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  506. .priv_data_size = sizeof(VideoDemuxData),
  507. .read_probe = img_read_probe,
  508. .read_header = ff_img_read_header,
  509. .read_packet = ff_img_read_packet,
  510. .read_close = img_read_close,
  511. .read_seek = img_read_seek,
  512. .flags = AVFMT_NOFILE,
  513. .priv_class = &img2_class,
  514. };
  515. #endif
  516. #if CONFIG_IMAGE2PIPE_DEMUXER
  517. static const AVClass img2pipe_class = {
  518. .class_name = "image2pipe demuxer",
  519. .item_name = av_default_item_name,
  520. .option = ff_img_options,
  521. .version = LIBAVUTIL_VERSION_INT,
  522. };
  523. AVInputFormat ff_image2pipe_demuxer = {
  524. .name = "image2pipe",
  525. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  526. .priv_data_size = sizeof(VideoDemuxData),
  527. .read_header = ff_img_read_header,
  528. .read_packet = ff_img_read_packet,
  529. .priv_class = &img2pipe_class,
  530. };
  531. #endif
  532. static int bmp_probe(AVProbeData *p)
  533. {
  534. const uint8_t *b = p->buf;
  535. int ihsize;
  536. if (AV_RB16(b) != 0x424d)
  537. return 0;
  538. ihsize = AV_RL32(b+14);
  539. if (ihsize < 12 || ihsize > 255)
  540. return 0;
  541. if (!AV_RN32(b + 6)) {
  542. return AVPROBE_SCORE_EXTENSION - 1; // lower than extension as bmp pipe has bugs
  543. } else {
  544. return AVPROBE_SCORE_EXTENSION / 4;
  545. }
  546. return 0;
  547. }
  548. static int dpx_probe(AVProbeData *p)
  549. {
  550. const uint8_t *b = p->buf;
  551. if (AV_RN32(b) == AV_RN32("SDPX") || AV_RN32(b) == AV_RN32("XPDS"))
  552. return AVPROBE_SCORE_EXTENSION + 1;
  553. return 0;
  554. }
  555. static int exr_probe(AVProbeData *p)
  556. {
  557. const uint8_t *b = p->buf;
  558. if (AV_RL32(b) == 20000630)
  559. return AVPROBE_SCORE_EXTENSION + 1;
  560. return 0;
  561. }
  562. static int j2k_probe(AVProbeData *p)
  563. {
  564. const uint8_t *b = p->buf;
  565. if (AV_RB64(b) == 0x0000000c6a502020 ||
  566. AV_RB32(b) == 0xff4fff51)
  567. return AVPROBE_SCORE_EXTENSION + 1;
  568. return 0;
  569. }
  570. static int jpegls_probe(AVProbeData *p)
  571. {
  572. const uint8_t *b = p->buf;
  573. if (AV_RB32(b) == 0xffd8fff7)
  574. return AVPROBE_SCORE_EXTENSION + 1;
  575. return 0;
  576. }
  577. static int pictor_probe(AVProbeData *p)
  578. {
  579. const uint8_t *b = p->buf;
  580. if (AV_RL16(b) == 0x1234)
  581. return AVPROBE_SCORE_EXTENSION / 4;
  582. return 0;
  583. }
  584. static int png_probe(AVProbeData *p)
  585. {
  586. const uint8_t *b = p->buf;
  587. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  588. return AVPROBE_SCORE_MAX - 1;
  589. return 0;
  590. }
  591. static int sgi_probe(AVProbeData *p)
  592. {
  593. const uint8_t *b = p->buf;
  594. if (AV_RB16(b) == 474 &&
  595. (b[2] & ~1) == 0 &&
  596. (b[3] & ~3) == 0 && b[3] &&
  597. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  598. return AVPROBE_SCORE_EXTENSION + 1;
  599. return 0;
  600. }
  601. static int sunrast_probe(AVProbeData *p)
  602. {
  603. const uint8_t *b = p->buf;
  604. if (AV_RB32(b) == 0x59a66a95)
  605. return AVPROBE_SCORE_EXTENSION + 1;
  606. return 0;
  607. }
  608. static int tiff_probe(AVProbeData *p)
  609. {
  610. const uint8_t *b = p->buf;
  611. if (AV_RB32(b) == 0x49492a00 ||
  612. AV_RB32(b) == 0x4D4D002a)
  613. return AVPROBE_SCORE_EXTENSION + 1;
  614. return 0;
  615. }
  616. static int webp_probe(AVProbeData *p)
  617. {
  618. const uint8_t *b = p->buf;
  619. if (AV_RB32(b) == 0x52494646 &&
  620. AV_RB32(b + 8) == 0x57454250)
  621. return AVPROBE_SCORE_MAX - 1;
  622. return 0;
  623. }
  624. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  625. static const AVClass imgname ## _class = {\
  626. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  627. .item_name = av_default_item_name,\
  628. .option = ff_img_options,\
  629. .version = LIBAVUTIL_VERSION_INT,\
  630. };\
  631. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  632. .name = AV_STRINGIFY(imgname) "_pipe",\
  633. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  634. .priv_data_size = sizeof(VideoDemuxData),\
  635. .read_probe = imgname ## _probe,\
  636. .read_header = ff_img_read_header,\
  637. .read_packet = ff_img_read_packet,\
  638. .priv_class = & imgname ## _class,\
  639. .flags = AVFMT_GENERIC_INDEX, \
  640. .raw_codec_id = codecid,\
  641. };
  642. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  643. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  644. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  645. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  646. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  647. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  648. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  649. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  650. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  651. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  652. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)