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.

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