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.

783 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->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
  152. return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
  153. else if (p->buf_size == 0)
  154. return 0;
  155. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  156. return 5;
  157. else
  158. return AVPROBE_SCORE_EXTENSION;
  159. }
  160. return 0;
  161. }
  162. int ff_img_read_header(AVFormatContext *s1)
  163. {
  164. VideoDemuxData *s = s1->priv_data;
  165. int first_index, last_index;
  166. AVStream *st;
  167. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  168. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  169. st = avformat_new_stream(s1, NULL);
  170. if (!st) {
  171. return AVERROR(ENOMEM);
  172. }
  173. if (s->pixel_format &&
  174. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  175. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  176. s->pixel_format);
  177. return AVERROR(EINVAL);
  178. }
  179. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  180. s->img_number = 0;
  181. s->img_count = 0;
  182. /* find format */
  183. if (s1->iformat->flags & AVFMT_NOFILE)
  184. s->is_pipe = 0;
  185. else {
  186. s->is_pipe = 1;
  187. st->need_parsing = AVSTREAM_PARSE_FULL;
  188. }
  189. if (s->ts_from_file == 2) {
  190. #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  191. av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
  192. return AVERROR(ENOSYS);
  193. #endif
  194. avpriv_set_pts_info(st, 64, 1, 1000000000);
  195. } else if (s->ts_from_file)
  196. avpriv_set_pts_info(st, 64, 1, 1);
  197. else
  198. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  199. if (s->width && s->height) {
  200. st->codec->width = s->width;
  201. st->codec->height = s->height;
  202. }
  203. if (!s->is_pipe) {
  204. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  205. s->use_glob = is_glob(s->path);
  206. if (s->use_glob) {
  207. char *p = s->path, *q, *dup;
  208. int gerr;
  209. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  210. "use pattern_type 'glob' instead\n");
  211. #if HAVE_GLOB
  212. dup = q = av_strdup(p);
  213. while (*q) {
  214. /* Do we have room for the next char and a \ insertion? */
  215. if ((p - s->path) >= (sizeof(s->path) - 2))
  216. break;
  217. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  218. ++q;
  219. else if (strspn(q, "\\*?[]{}"))
  220. *p++ = '\\';
  221. *p++ = *q++;
  222. }
  223. *p = 0;
  224. av_free(dup);
  225. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  226. if (gerr != 0) {
  227. return AVERROR(ENOENT);
  228. }
  229. first_index = 0;
  230. last_index = s->globstate.gl_pathc - 1;
  231. #endif
  232. }
  233. }
  234. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  235. if (find_image_range(&first_index, &last_index, s->path,
  236. s->start_number, s->start_number_range) < 0) {
  237. av_log(s1, AV_LOG_ERROR,
  238. "Could find no file with path '%s' and index in the range %d-%d\n",
  239. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  240. return AVERROR(ENOENT);
  241. }
  242. } else if (s->pattern_type == PT_GLOB) {
  243. #if HAVE_GLOB
  244. int gerr;
  245. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  246. if (gerr != 0) {
  247. return AVERROR(ENOENT);
  248. }
  249. first_index = 0;
  250. last_index = s->globstate.gl_pathc - 1;
  251. s->use_glob = 1;
  252. #else
  253. av_log(s1, AV_LOG_ERROR,
  254. "Pattern type 'glob' was selected but globbing "
  255. "is not supported by this libavformat build\n");
  256. return AVERROR(ENOSYS);
  257. #endif
  258. } else if (s->pattern_type != PT_GLOB_SEQUENCE) {
  259. av_log(s1, AV_LOG_ERROR,
  260. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  261. return AVERROR(EINVAL);
  262. }
  263. s->img_first = first_index;
  264. s->img_last = last_index;
  265. s->img_number = first_index;
  266. /* compute duration */
  267. if (!s->ts_from_file) {
  268. st->start_time = 0;
  269. st->duration = last_index - first_index + 1;
  270. }
  271. }
  272. if (s1->video_codec_id) {
  273. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  274. st->codec->codec_id = s1->video_codec_id;
  275. } else if (s1->audio_codec_id) {
  276. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  277. st->codec->codec_id = s1->audio_codec_id;
  278. } else if (s1->iformat->raw_codec_id) {
  279. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  280. st->codec->codec_id = s1->iformat->raw_codec_id;
  281. } else {
  282. const char *str = strrchr(s->path, '.');
  283. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  284. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  285. if (s1->pb) {
  286. int probe_buffer_size = 2048;
  287. uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
  288. AVInputFormat *fmt = NULL;
  289. AVProbeData pd = { 0 };
  290. if (!probe_buffer)
  291. return AVERROR(ENOMEM);
  292. probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
  293. if (probe_buffer_size < 0) {
  294. av_free(probe_buffer);
  295. return probe_buffer_size;
  296. }
  297. memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
  298. pd.buf = probe_buffer;
  299. pd.buf_size = probe_buffer_size;
  300. pd.filename = s1->filename;
  301. while ((fmt = av_iformat_next(fmt))) {
  302. if (fmt->read_header != ff_img_read_header ||
  303. !fmt->read_probe ||
  304. (fmt->flags & AVFMT_NOFILE) ||
  305. !fmt->raw_codec_id)
  306. continue;
  307. if (fmt->read_probe(&pd) > 0) {
  308. st->codec->codec_id = fmt->raw_codec_id;
  309. break;
  310. }
  311. }
  312. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  313. }
  314. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  315. st->codec->codec_id = ff_guess_image2_codec(s->path);
  316. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  317. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  318. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  319. st->codec->codec_id = AV_CODEC_ID_NONE;
  320. }
  321. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  322. pix_fmt != AV_PIX_FMT_NONE)
  323. st->codec->pix_fmt = pix_fmt;
  324. return 0;
  325. }
  326. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  327. {
  328. VideoDemuxData *s = s1->priv_data;
  329. char filename_bytes[1024];
  330. char *filename = filename_bytes;
  331. int i;
  332. int size[3] = { 0 }, ret[3] = { 0 };
  333. AVIOContext *f[3] = { NULL };
  334. AVCodecContext *codec = s1->streams[0]->codec;
  335. if (!s->is_pipe) {
  336. /* loop over input */
  337. if (s->loop && s->img_number > s->img_last) {
  338. s->img_number = s->img_first;
  339. }
  340. if (s->img_number > s->img_last)
  341. return AVERROR_EOF;
  342. if (s->use_glob) {
  343. #if HAVE_GLOB
  344. filename = s->globstate.gl_pathv[s->img_number];
  345. #endif
  346. } else {
  347. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  348. s->path,
  349. s->img_number) < 0 && s->img_number > 1)
  350. return AVERROR(EIO);
  351. }
  352. for (i = 0; i < 3; i++) {
  353. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  354. &s1->interrupt_callback, NULL) < 0) {
  355. if (i >= 1)
  356. break;
  357. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  358. filename);
  359. return AVERROR(EIO);
  360. }
  361. size[i] = avio_size(f[i]);
  362. if (!s->split_planes)
  363. break;
  364. filename[strlen(filename) - 1] = 'U' + i;
  365. }
  366. if (codec->codec_id == AV_CODEC_ID_NONE) {
  367. AVProbeData pd = { 0 };
  368. AVInputFormat *ifmt;
  369. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  370. int ret;
  371. int score = 0;
  372. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  373. if (ret < 0)
  374. return ret;
  375. memset(header + ret, 0, sizeof(header) - ret);
  376. avio_skip(f[0], -ret);
  377. pd.buf = header;
  378. pd.buf_size = ret;
  379. pd.filename = filename;
  380. ifmt = av_probe_input_format3(&pd, 1, &score);
  381. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  382. codec->codec_id = ifmt->raw_codec_id;
  383. }
  384. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  385. infer_size(&codec->width, &codec->height, size[0]);
  386. } else {
  387. f[0] = s1->pb;
  388. if (avio_feof(f[0]) && s->loop && s->is_pipe)
  389. avio_seek(f[0], 0, SEEK_SET);
  390. if (avio_feof(f[0]))
  391. return AVERROR_EOF;
  392. if (s->frame_size > 0) {
  393. size[0] = s->frame_size;
  394. } else if (!s1->streams[0]->parser) {
  395. size[0] = avio_size(s1->pb);
  396. } else {
  397. size[0] = 4096;
  398. }
  399. }
  400. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  401. return AVERROR(ENOMEM);
  402. pkt->stream_index = 0;
  403. pkt->flags |= AV_PKT_FLAG_KEY;
  404. if (s->ts_from_file) {
  405. struct stat img_stat;
  406. if (stat(filename, &img_stat))
  407. return AVERROR(EIO);
  408. pkt->pts = (int64_t)img_stat.st_mtime;
  409. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  410. if (s->ts_from_file == 2)
  411. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  412. #endif
  413. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  414. } else if (!s->is_pipe) {
  415. pkt->pts = s->pts;
  416. }
  417. if (s->is_pipe)
  418. pkt->pos = avio_tell(f[0]);
  419. pkt->size = 0;
  420. for (i = 0; i < 3; i++) {
  421. if (f[i]) {
  422. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  423. if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
  424. if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
  425. pkt->pos = 0;
  426. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  427. }
  428. }
  429. if (!s->is_pipe)
  430. avio_close(f[i]);
  431. if (ret[i] > 0)
  432. pkt->size += ret[i];
  433. }
  434. }
  435. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  436. av_free_packet(pkt);
  437. if (ret[0] < 0) {
  438. return ret[0];
  439. } else if (ret[1] < 0) {
  440. return ret[1];
  441. } else if (ret[2] < 0)
  442. return ret[2];
  443. return AVERROR_EOF;
  444. } else {
  445. s->img_count++;
  446. s->img_number++;
  447. s->pts++;
  448. return 0;
  449. }
  450. }
  451. static int img_read_close(struct AVFormatContext* s1)
  452. {
  453. VideoDemuxData *s = s1->priv_data;
  454. #if HAVE_GLOB
  455. if (s->use_glob) {
  456. globfree(&s->globstate);
  457. }
  458. #endif
  459. return 0;
  460. }
  461. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  462. {
  463. VideoDemuxData *s1 = s->priv_data;
  464. AVStream *st = s->streams[0];
  465. if (s1->ts_from_file) {
  466. int index = av_index_search_timestamp(st, timestamp, flags);
  467. if(index < 0)
  468. return -1;
  469. s1->img_number = st->index_entries[index].pos;
  470. return 0;
  471. }
  472. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  473. return -1;
  474. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  475. s1->pts = timestamp;
  476. return 0;
  477. }
  478. #define OFFSET(x) offsetof(VideoDemuxData, x)
  479. #define DEC AV_OPT_FLAG_DECODING_PARAM
  480. const AVOption ff_img_options[] = {
  481. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  482. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  483. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  484. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  485. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  486. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  487. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  488. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  489. { "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 },
  490. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  491. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  492. { "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" },
  493. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  494. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  495. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  496. { NULL },
  497. };
  498. #if CONFIG_IMAGE2_DEMUXER
  499. static const AVClass img2_class = {
  500. .class_name = "image2 demuxer",
  501. .item_name = av_default_item_name,
  502. .option = ff_img_options,
  503. .version = LIBAVUTIL_VERSION_INT,
  504. };
  505. AVInputFormat ff_image2_demuxer = {
  506. .name = "image2",
  507. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  508. .priv_data_size = sizeof(VideoDemuxData),
  509. .read_probe = img_read_probe,
  510. .read_header = ff_img_read_header,
  511. .read_packet = ff_img_read_packet,
  512. .read_close = img_read_close,
  513. .read_seek = img_read_seek,
  514. .flags = AVFMT_NOFILE,
  515. .priv_class = &img2_class,
  516. };
  517. #endif
  518. #if CONFIG_IMAGE2PIPE_DEMUXER
  519. static const AVClass img2pipe_class = {
  520. .class_name = "image2pipe demuxer",
  521. .item_name = av_default_item_name,
  522. .option = ff_img_options,
  523. .version = LIBAVUTIL_VERSION_INT,
  524. };
  525. AVInputFormat ff_image2pipe_demuxer = {
  526. .name = "image2pipe",
  527. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  528. .priv_data_size = sizeof(VideoDemuxData),
  529. .read_header = ff_img_read_header,
  530. .read_packet = ff_img_read_packet,
  531. .priv_class = &img2pipe_class,
  532. };
  533. #endif
  534. static int bmp_probe(AVProbeData *p)
  535. {
  536. const uint8_t *b = p->buf;
  537. int ihsize;
  538. if (AV_RB16(b) != 0x424d)
  539. return 0;
  540. ihsize = AV_RL32(b+14);
  541. if (ihsize < 12 || ihsize > 255)
  542. return 0;
  543. if (!AV_RN32(b + 6)) {
  544. return AVPROBE_SCORE_EXTENSION + 1;
  545. } else {
  546. return AVPROBE_SCORE_EXTENSION / 4;
  547. }
  548. return 0;
  549. }
  550. static int dpx_probe(AVProbeData *p)
  551. {
  552. const uint8_t *b = p->buf;
  553. if (AV_RN32(b) == AV_RN32("SDPX") || AV_RN32(b) == AV_RN32("XPDS"))
  554. return AVPROBE_SCORE_EXTENSION + 1;
  555. return 0;
  556. }
  557. static int exr_probe(AVProbeData *p)
  558. {
  559. const uint8_t *b = p->buf;
  560. if (AV_RL32(b) == 20000630)
  561. return AVPROBE_SCORE_EXTENSION + 1;
  562. return 0;
  563. }
  564. static int j2k_probe(AVProbeData *p)
  565. {
  566. const uint8_t *b = p->buf;
  567. if (AV_RB64(b) == 0x0000000c6a502020 ||
  568. AV_RB32(b) == 0xff4fff51)
  569. return AVPROBE_SCORE_EXTENSION + 1;
  570. return 0;
  571. }
  572. static int jpeg_probe(AVProbeData *p)
  573. {
  574. const uint8_t *b = p->buf;
  575. int i, state = 0xD8;
  576. if (AV_RB16(b) != 0xFFD8 ||
  577. AV_RB32(b) == 0xFFD8FFF7)
  578. return 0;
  579. b += 2;
  580. for (i = 0; i < p->buf_size - 2; i++) {
  581. int c;
  582. if (b[i] != 0xFF)
  583. continue;
  584. c = b[i + 1];
  585. switch (c) {
  586. case 0xD8:
  587. return 0;
  588. case 0xC0:
  589. case 0xC1:
  590. case 0xC2:
  591. case 0xC3:
  592. case 0xC5:
  593. case 0xC6:
  594. case 0xC7:
  595. if (state != 0xD8)
  596. return 0;
  597. state = 0xC0;
  598. break;
  599. case 0xDA:
  600. if (state != 0xC0)
  601. return 0;
  602. state = 0xDA;
  603. break;
  604. case 0xD9:
  605. if (state != 0xDA)
  606. return 0;
  607. state = 0xD9;
  608. break;
  609. default:
  610. if ( (c >= 0x02 && c <= 0xBF)
  611. || c == 0xC8)
  612. return 0;
  613. }
  614. }
  615. if (state == 0xD9)
  616. return AVPROBE_SCORE_EXTENSION + 1;
  617. return AVPROBE_SCORE_EXTENSION / 8;
  618. }
  619. static int jpegls_probe(AVProbeData *p)
  620. {
  621. const uint8_t *b = p->buf;
  622. if (AV_RB32(b) == 0xffd8fff7)
  623. return AVPROBE_SCORE_EXTENSION + 1;
  624. return 0;
  625. }
  626. static int pictor_probe(AVProbeData *p)
  627. {
  628. const uint8_t *b = p->buf;
  629. if (AV_RL16(b) == 0x1234)
  630. return AVPROBE_SCORE_EXTENSION / 4;
  631. return 0;
  632. }
  633. static int png_probe(AVProbeData *p)
  634. {
  635. const uint8_t *b = p->buf;
  636. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  637. return AVPROBE_SCORE_MAX - 1;
  638. return 0;
  639. }
  640. static int sgi_probe(AVProbeData *p)
  641. {
  642. const uint8_t *b = p->buf;
  643. if (AV_RB16(b) == 474 &&
  644. (b[2] & ~1) == 0 &&
  645. (b[3] & ~3) == 0 && b[3] &&
  646. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  647. return AVPROBE_SCORE_EXTENSION + 1;
  648. return 0;
  649. }
  650. static int sunrast_probe(AVProbeData *p)
  651. {
  652. const uint8_t *b = p->buf;
  653. if (AV_RB32(b) == 0x59a66a95)
  654. return AVPROBE_SCORE_EXTENSION + 1;
  655. return 0;
  656. }
  657. static int tiff_probe(AVProbeData *p)
  658. {
  659. const uint8_t *b = p->buf;
  660. if (AV_RB32(b) == 0x49492a00 ||
  661. AV_RB32(b) == 0x4D4D002a)
  662. return AVPROBE_SCORE_EXTENSION + 1;
  663. return 0;
  664. }
  665. static int webp_probe(AVProbeData *p)
  666. {
  667. const uint8_t *b = p->buf;
  668. if (AV_RB32(b) == 0x52494646 &&
  669. AV_RB32(b + 8) == 0x57454250)
  670. return AVPROBE_SCORE_MAX - 1;
  671. return 0;
  672. }
  673. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  674. static const AVClass imgname ## _class = {\
  675. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  676. .item_name = av_default_item_name,\
  677. .option = ff_img_options,\
  678. .version = LIBAVUTIL_VERSION_INT,\
  679. };\
  680. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  681. .name = AV_STRINGIFY(imgname) "_pipe",\
  682. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  683. .priv_data_size = sizeof(VideoDemuxData),\
  684. .read_probe = imgname ## _probe,\
  685. .read_header = ff_img_read_header,\
  686. .read_packet = ff_img_read_packet,\
  687. .priv_class = & imgname ## _class,\
  688. .flags = AVFMT_GENERIC_INDEX, \
  689. .raw_codec_id = codecid,\
  690. };
  691. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  692. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  693. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  694. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  695. IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
  696. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  697. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  698. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  699. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  700. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  701. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  702. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)