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.

793 lines
25KB

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