img2dec.c 21 KB

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