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.

711 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]))
  387. return AVERROR(EIO);
  388. if (s->frame_size > 0) {
  389. size[0] = s->frame_size;
  390. } else if (!s1->streams[0]->parser) {
  391. size[0] = avio_size(s1->pb);
  392. } else {
  393. size[0] = 4096;
  394. }
  395. }
  396. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  397. return AVERROR(ENOMEM);
  398. pkt->stream_index = 0;
  399. pkt->flags |= AV_PKT_FLAG_KEY;
  400. if (s->ts_from_file) {
  401. struct stat img_stat;
  402. if (stat(filename, &img_stat))
  403. return AVERROR(EIO);
  404. pkt->pts = (int64_t)img_stat.st_mtime;
  405. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  406. if (s->ts_from_file == 2)
  407. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  408. #endif
  409. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  410. } else if (!s->is_pipe) {
  411. pkt->pts = s->pts;
  412. }
  413. pkt->size = 0;
  414. for (i = 0; i < 3; i++) {
  415. if (f[i]) {
  416. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  417. if (!s->is_pipe)
  418. avio_close(f[i]);
  419. if (ret[i] > 0)
  420. pkt->size += ret[i];
  421. }
  422. }
  423. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  424. av_free_packet(pkt);
  425. return AVERROR(EIO); /* signal EOF */
  426. } else {
  427. s->img_count++;
  428. s->img_number++;
  429. s->pts++;
  430. return 0;
  431. }
  432. }
  433. static int img_read_close(struct AVFormatContext* s1)
  434. {
  435. VideoDemuxData *s = s1->priv_data;
  436. #if HAVE_GLOB
  437. if (s->use_glob) {
  438. globfree(&s->globstate);
  439. }
  440. #endif
  441. return 0;
  442. }
  443. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  444. {
  445. VideoDemuxData *s1 = s->priv_data;
  446. AVStream *st = s->streams[0];
  447. if (s1->ts_from_file) {
  448. int index = av_index_search_timestamp(st, timestamp, flags);
  449. if(index < 0)
  450. return -1;
  451. s1->img_number = st->index_entries[index].pos;
  452. return 0;
  453. }
  454. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  455. return -1;
  456. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  457. s1->pts = timestamp;
  458. return 0;
  459. }
  460. #define OFFSET(x) offsetof(VideoDemuxData, x)
  461. #define DEC AV_OPT_FLAG_DECODING_PARAM
  462. const AVOption ff_img_options[] = {
  463. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  464. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  465. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  466. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  467. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  468. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  469. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  470. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  471. { "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 },
  472. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  473. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  474. { "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" },
  475. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  476. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  477. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  478. { NULL },
  479. };
  480. #if CONFIG_IMAGE2_DEMUXER
  481. static const AVClass img2_class = {
  482. .class_name = "image2 demuxer",
  483. .item_name = av_default_item_name,
  484. .option = ff_img_options,
  485. .version = LIBAVUTIL_VERSION_INT,
  486. };
  487. AVInputFormat ff_image2_demuxer = {
  488. .name = "image2",
  489. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  490. .priv_data_size = sizeof(VideoDemuxData),
  491. .read_probe = img_read_probe,
  492. .read_header = ff_img_read_header,
  493. .read_packet = ff_img_read_packet,
  494. .read_close = img_read_close,
  495. .read_seek = img_read_seek,
  496. .flags = AVFMT_NOFILE,
  497. .priv_class = &img2_class,
  498. };
  499. #endif
  500. #if CONFIG_IMAGE2PIPE_DEMUXER
  501. static const AVClass img2pipe_class = {
  502. .class_name = "image2pipe demuxer",
  503. .item_name = av_default_item_name,
  504. .option = ff_img_options,
  505. .version = LIBAVUTIL_VERSION_INT,
  506. };
  507. AVInputFormat ff_image2pipe_demuxer = {
  508. .name = "image2pipe",
  509. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  510. .priv_data_size = sizeof(VideoDemuxData),
  511. .read_header = ff_img_read_header,
  512. .read_packet = ff_img_read_packet,
  513. .priv_class = &img2pipe_class,
  514. };
  515. #endif
  516. static int bmp_probe(AVProbeData *p)
  517. {
  518. const uint8_t *b = p->buf;
  519. int ihsize;
  520. if (AV_RB16(b) != 0x424d)
  521. return 0;
  522. ihsize = AV_RL32(b+14);
  523. if (ihsize < 12 || ihsize > 255)
  524. return 0;
  525. if (!AV_RN32(b + 6)) {
  526. return AVPROBE_SCORE_EXTENSION - 1; // lower than extension as bmp pipe has bugs
  527. } else {
  528. return AVPROBE_SCORE_EXTENSION / 4;
  529. }
  530. return 0;
  531. }
  532. static int dpx_probe(AVProbeData *p)
  533. {
  534. const uint8_t *b = p->buf;
  535. if (AV_RN32(b) == AV_RN32("SDPX") || AV_RN32(b) == AV_RN32("XPDS"))
  536. return AVPROBE_SCORE_EXTENSION + 1;
  537. return 0;
  538. }
  539. static int exr_probe(AVProbeData *p)
  540. {
  541. const uint8_t *b = p->buf;
  542. if (AV_RL32(b) == 20000630)
  543. return AVPROBE_SCORE_EXTENSION + 1;
  544. return 0;
  545. }
  546. static int j2k_probe(AVProbeData *p)
  547. {
  548. const uint8_t *b = p->buf;
  549. if (AV_RB64(b) == 0x0000000c6a502020 ||
  550. AV_RB32(b) == 0xff4fff51)
  551. return AVPROBE_SCORE_EXTENSION + 1;
  552. return 0;
  553. }
  554. static int jpegls_probe(AVProbeData *p)
  555. {
  556. const uint8_t *b = p->buf;
  557. if (AV_RB32(b) == 0xffd8fff7)
  558. return AVPROBE_SCORE_EXTENSION + 1;
  559. return 0;
  560. }
  561. static int pictor_probe(AVProbeData *p)
  562. {
  563. const uint8_t *b = p->buf;
  564. if (AV_RL16(b) == 0x1234)
  565. return AVPROBE_SCORE_EXTENSION / 4;
  566. return 0;
  567. }
  568. static int png_probe(AVProbeData *p)
  569. {
  570. const uint8_t *b = p->buf;
  571. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  572. return AVPROBE_SCORE_MAX - 1;
  573. return 0;
  574. }
  575. static int sgi_probe(AVProbeData *p)
  576. {
  577. const uint8_t *b = p->buf;
  578. if (AV_RB16(b) == 474 &&
  579. (b[2] & ~1) == 0 &&
  580. (b[3] & ~3) == 0 && b[3] &&
  581. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  582. return AVPROBE_SCORE_EXTENSION + 1;
  583. return 0;
  584. }
  585. static int sunrast_probe(AVProbeData *p)
  586. {
  587. const uint8_t *b = p->buf;
  588. if (AV_RB32(b) == 0x59a66a95)
  589. return AVPROBE_SCORE_EXTENSION + 1;
  590. return 0;
  591. }
  592. static int tiff_probe(AVProbeData *p)
  593. {
  594. const uint8_t *b = p->buf;
  595. if (AV_RB32(b) == 0x49492a00 ||
  596. AV_RB32(b) == 0x4D4D002a)
  597. return AVPROBE_SCORE_EXTENSION + 1;
  598. return 0;
  599. }
  600. static int webp_probe(AVProbeData *p)
  601. {
  602. const uint8_t *b = p->buf;
  603. if (AV_RB32(b) == 0x52494646 &&
  604. AV_RB32(b + 8) == 0x57454250)
  605. return AVPROBE_SCORE_MAX - 1;
  606. return 0;
  607. }
  608. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  609. static const AVClass imgname ## _class = {\
  610. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  611. .item_name = av_default_item_name,\
  612. .option = ff_img_options,\
  613. .version = LIBAVUTIL_VERSION_INT,\
  614. };\
  615. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  616. .name = AV_STRINGIFY(imgname) "_pipe",\
  617. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  618. .priv_data_size = sizeof(VideoDemuxData),\
  619. .read_probe = imgname ## _probe,\
  620. .read_header = ff_img_read_header,\
  621. .read_packet = ff_img_read_packet,\
  622. .priv_class = & imgname ## _class,\
  623. .raw_codec_id = codecid,\
  624. };
  625. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  626. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  627. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  628. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  629. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  630. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  631. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  632. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  633. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  634. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  635. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)