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.

773 lines
24KB

  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_EOF;
  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. if (s->is_pipe)
  414. pkt->pos = avio_tell(f[0]);
  415. pkt->size = 0;
  416. for (i = 0; i < 3; i++) {
  417. if (f[i]) {
  418. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  419. if (!s->is_pipe)
  420. avio_close(f[i]);
  421. if (ret[i] > 0)
  422. pkt->size += ret[i];
  423. }
  424. }
  425. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  426. av_free_packet(pkt);
  427. if (ret[0] < 0) {
  428. return ret[0];
  429. } else if (ret[1] < 0) {
  430. return ret[1];
  431. } else if (ret[2] < 0)
  432. return ret[2];
  433. return AVERROR_EOF;
  434. } else {
  435. s->img_count++;
  436. s->img_number++;
  437. s->pts++;
  438. return 0;
  439. }
  440. }
  441. static int img_read_close(struct AVFormatContext* s1)
  442. {
  443. VideoDemuxData *s = s1->priv_data;
  444. #if HAVE_GLOB
  445. if (s->use_glob) {
  446. globfree(&s->globstate);
  447. }
  448. #endif
  449. return 0;
  450. }
  451. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  452. {
  453. VideoDemuxData *s1 = s->priv_data;
  454. AVStream *st = s->streams[0];
  455. if (s1->ts_from_file) {
  456. int index = av_index_search_timestamp(st, timestamp, flags);
  457. if(index < 0)
  458. return -1;
  459. s1->img_number = st->index_entries[index].pos;
  460. return 0;
  461. }
  462. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  463. return -1;
  464. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  465. s1->pts = timestamp;
  466. return 0;
  467. }
  468. #define OFFSET(x) offsetof(VideoDemuxData, x)
  469. #define DEC AV_OPT_FLAG_DECODING_PARAM
  470. const AVOption ff_img_options[] = {
  471. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  472. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  473. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  474. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  475. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  476. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  477. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  478. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  479. { "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 },
  480. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  481. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  482. { "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" },
  483. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  484. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  485. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  486. { NULL },
  487. };
  488. #if CONFIG_IMAGE2_DEMUXER
  489. static const AVClass img2_class = {
  490. .class_name = "image2 demuxer",
  491. .item_name = av_default_item_name,
  492. .option = ff_img_options,
  493. .version = LIBAVUTIL_VERSION_INT,
  494. };
  495. AVInputFormat ff_image2_demuxer = {
  496. .name = "image2",
  497. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  498. .priv_data_size = sizeof(VideoDemuxData),
  499. .read_probe = img_read_probe,
  500. .read_header = ff_img_read_header,
  501. .read_packet = ff_img_read_packet,
  502. .read_close = img_read_close,
  503. .read_seek = img_read_seek,
  504. .flags = AVFMT_NOFILE,
  505. .priv_class = &img2_class,
  506. };
  507. #endif
  508. #if CONFIG_IMAGE2PIPE_DEMUXER
  509. static const AVClass img2pipe_class = {
  510. .class_name = "image2pipe demuxer",
  511. .item_name = av_default_item_name,
  512. .option = ff_img_options,
  513. .version = LIBAVUTIL_VERSION_INT,
  514. };
  515. AVInputFormat ff_image2pipe_demuxer = {
  516. .name = "image2pipe",
  517. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  518. .priv_data_size = sizeof(VideoDemuxData),
  519. .read_header = ff_img_read_header,
  520. .read_packet = ff_img_read_packet,
  521. .priv_class = &img2pipe_class,
  522. };
  523. #endif
  524. static int bmp_probe(AVProbeData *p)
  525. {
  526. const uint8_t *b = p->buf;
  527. int ihsize;
  528. if (AV_RB16(b) != 0x424d)
  529. return 0;
  530. ihsize = AV_RL32(b+14);
  531. if (ihsize < 12 || ihsize > 255)
  532. return 0;
  533. if (!AV_RN32(b + 6)) {
  534. return AVPROBE_SCORE_EXTENSION - 1; // lower than extension as bmp pipe has bugs
  535. } else {
  536. return AVPROBE_SCORE_EXTENSION / 4;
  537. }
  538. return 0;
  539. }
  540. static int dpx_probe(AVProbeData *p)
  541. {
  542. const uint8_t *b = p->buf;
  543. if (AV_RN32(b) == AV_RN32("SDPX") || AV_RN32(b) == AV_RN32("XPDS"))
  544. return AVPROBE_SCORE_EXTENSION + 1;
  545. return 0;
  546. }
  547. static int exr_probe(AVProbeData *p)
  548. {
  549. const uint8_t *b = p->buf;
  550. if (AV_RL32(b) == 20000630)
  551. return AVPROBE_SCORE_EXTENSION + 1;
  552. return 0;
  553. }
  554. static int j2k_probe(AVProbeData *p)
  555. {
  556. const uint8_t *b = p->buf;
  557. if (AV_RB64(b) == 0x0000000c6a502020 ||
  558. AV_RB32(b) == 0xff4fff51)
  559. return AVPROBE_SCORE_EXTENSION + 1;
  560. return 0;
  561. }
  562. static int jpeg_probe(AVProbeData *p)
  563. {
  564. const uint8_t *b = p->buf;
  565. int i, state = 0xD8;
  566. if (AV_RB16(b) != 0xFFD8 ||
  567. AV_RB32(b) == 0xFFD8FFF7)
  568. return 0;
  569. b += 2;
  570. for (i = 0; i < p->buf_size - 2; i++) {
  571. int c;
  572. if (b[i] != 0xFF)
  573. continue;
  574. c = b[i + 1];
  575. switch (c) {
  576. case 0xD8:
  577. return 0;
  578. case 0xC0:
  579. case 0xC1:
  580. case 0xC2:
  581. case 0xC3:
  582. case 0xC5:
  583. case 0xC6:
  584. case 0xC7:
  585. if (state != 0xD8)
  586. return 0;
  587. state = 0xC0;
  588. break;
  589. case 0xDA:
  590. if (state != 0xC0)
  591. return 0;
  592. state = 0xDA;
  593. break;
  594. case 0xD9:
  595. if (state != 0xDA)
  596. return 0;
  597. state = 0xD9;
  598. break;
  599. default:
  600. if ( (c >= 0x02 && c <= 0xBF)
  601. || c == 0xC8)
  602. return 0;
  603. }
  604. }
  605. if (state == 0xD9)
  606. return AVPROBE_SCORE_EXTENSION + 1;
  607. return AVPROBE_SCORE_EXTENSION / 8;
  608. }
  609. static int jpegls_probe(AVProbeData *p)
  610. {
  611. const uint8_t *b = p->buf;
  612. if (AV_RB32(b) == 0xffd8fff7)
  613. return AVPROBE_SCORE_EXTENSION + 1;
  614. return 0;
  615. }
  616. static int pictor_probe(AVProbeData *p)
  617. {
  618. const uint8_t *b = p->buf;
  619. if (AV_RL16(b) == 0x1234)
  620. return AVPROBE_SCORE_EXTENSION / 4;
  621. return 0;
  622. }
  623. static int png_probe(AVProbeData *p)
  624. {
  625. const uint8_t *b = p->buf;
  626. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  627. return AVPROBE_SCORE_MAX - 1;
  628. return 0;
  629. }
  630. static int sgi_probe(AVProbeData *p)
  631. {
  632. const uint8_t *b = p->buf;
  633. if (AV_RB16(b) == 474 &&
  634. (b[2] & ~1) == 0 &&
  635. (b[3] & ~3) == 0 && b[3] &&
  636. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  637. return AVPROBE_SCORE_EXTENSION + 1;
  638. return 0;
  639. }
  640. static int sunrast_probe(AVProbeData *p)
  641. {
  642. const uint8_t *b = p->buf;
  643. if (AV_RB32(b) == 0x59a66a95)
  644. return AVPROBE_SCORE_EXTENSION + 1;
  645. return 0;
  646. }
  647. static int tiff_probe(AVProbeData *p)
  648. {
  649. const uint8_t *b = p->buf;
  650. if (AV_RB32(b) == 0x49492a00 ||
  651. AV_RB32(b) == 0x4D4D002a)
  652. return AVPROBE_SCORE_EXTENSION + 1;
  653. return 0;
  654. }
  655. static int webp_probe(AVProbeData *p)
  656. {
  657. const uint8_t *b = p->buf;
  658. if (AV_RB32(b) == 0x52494646 &&
  659. AV_RB32(b + 8) == 0x57454250)
  660. return AVPROBE_SCORE_MAX - 1;
  661. return 0;
  662. }
  663. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  664. static const AVClass imgname ## _class = {\
  665. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  666. .item_name = av_default_item_name,\
  667. .option = ff_img_options,\
  668. .version = LIBAVUTIL_VERSION_INT,\
  669. };\
  670. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  671. .name = AV_STRINGIFY(imgname) "_pipe",\
  672. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  673. .priv_data_size = sizeof(VideoDemuxData),\
  674. .read_probe = imgname ## _probe,\
  675. .read_header = ff_img_read_header,\
  676. .read_packet = ff_img_read_packet,\
  677. .priv_class = & imgname ## _class,\
  678. .flags = AVFMT_GENERIC_INDEX, \
  679. .raw_codec_id = codecid,\
  680. };
  681. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  682. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  683. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  684. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  685. IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
  686. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  687. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  688. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  689. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  690. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  691. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  692. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)