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.

846 lines
26KB

  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 = 1, last_index = 1;
  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 && s->pattern_type != PT_NONE) {
  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. if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
  315. avio_seek(s1->pb, 0, SEEK_SET);
  316. } else
  317. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  318. }
  319. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  320. st->codec->codec_id = ff_guess_image2_codec(s->path);
  321. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  322. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  323. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  324. st->codec->codec_id = AV_CODEC_ID_NONE;
  325. }
  326. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  327. pix_fmt != AV_PIX_FMT_NONE)
  328. st->codec->pix_fmt = pix_fmt;
  329. return 0;
  330. }
  331. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  332. {
  333. VideoDemuxData *s = s1->priv_data;
  334. char filename_bytes[1024];
  335. char *filename = filename_bytes;
  336. int i, res;
  337. int size[3] = { 0 }, ret[3] = { 0 };
  338. AVIOContext *f[3] = { NULL };
  339. AVCodecContext *codec = s1->streams[0]->codec;
  340. if (!s->is_pipe) {
  341. /* loop over input */
  342. if (s->loop && s->img_number > s->img_last) {
  343. s->img_number = s->img_first;
  344. }
  345. if (s->img_number > s->img_last)
  346. return AVERROR_EOF;
  347. if (s->pattern_type == PT_NONE) {
  348. av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
  349. } else if (s->use_glob) {
  350. #if HAVE_GLOB
  351. filename = s->globstate.gl_pathv[s->img_number];
  352. #endif
  353. } else {
  354. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  355. s->path,
  356. s->img_number) < 0 && s->img_number > 1)
  357. return AVERROR(EIO);
  358. }
  359. for (i = 0; i < 3; i++) {
  360. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  361. &s1->interrupt_callback, NULL) < 0) {
  362. if (i >= 1)
  363. break;
  364. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  365. filename);
  366. return AVERROR(EIO);
  367. }
  368. size[i] = avio_size(f[i]);
  369. if (!s->split_planes)
  370. break;
  371. filename[strlen(filename) - 1] = 'U' + i;
  372. }
  373. if (codec->codec_id == AV_CODEC_ID_NONE) {
  374. AVProbeData pd = { 0 };
  375. AVInputFormat *ifmt;
  376. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  377. int ret;
  378. int score = 0;
  379. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  380. if (ret < 0)
  381. return ret;
  382. memset(header + ret, 0, sizeof(header) - ret);
  383. avio_skip(f[0], -ret);
  384. pd.buf = header;
  385. pd.buf_size = ret;
  386. pd.filename = filename;
  387. ifmt = av_probe_input_format3(&pd, 1, &score);
  388. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  389. codec->codec_id = ifmt->raw_codec_id;
  390. }
  391. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  392. infer_size(&codec->width, &codec->height, size[0]);
  393. } else {
  394. f[0] = s1->pb;
  395. if (avio_feof(f[0]) && s->loop && s->is_pipe)
  396. avio_seek(f[0], 0, SEEK_SET);
  397. if (avio_feof(f[0]))
  398. return AVERROR_EOF;
  399. if (s->frame_size > 0) {
  400. size[0] = s->frame_size;
  401. } else if (!s1->streams[0]->parser) {
  402. size[0] = avio_size(s1->pb);
  403. } else {
  404. size[0] = 4096;
  405. }
  406. }
  407. res = av_new_packet(pkt, size[0] + size[1] + size[2]);
  408. if (res < 0)
  409. return res;
  410. pkt->stream_index = 0;
  411. pkt->flags |= AV_PKT_FLAG_KEY;
  412. if (s->ts_from_file) {
  413. struct stat img_stat;
  414. if (stat(filename, &img_stat))
  415. return AVERROR(EIO);
  416. pkt->pts = (int64_t)img_stat.st_mtime;
  417. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  418. if (s->ts_from_file == 2)
  419. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  420. #endif
  421. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  422. } else if (!s->is_pipe) {
  423. pkt->pts = s->pts;
  424. }
  425. if (s->is_pipe)
  426. pkt->pos = avio_tell(f[0]);
  427. pkt->size = 0;
  428. for (i = 0; i < 3; i++) {
  429. if (f[i]) {
  430. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  431. if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
  432. if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
  433. pkt->pos = 0;
  434. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  435. }
  436. }
  437. if (!s->is_pipe)
  438. avio_closep(&f[i]);
  439. if (ret[i] > 0)
  440. pkt->size += ret[i];
  441. }
  442. }
  443. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  444. av_free_packet(pkt);
  445. if (ret[0] < 0) {
  446. return ret[0];
  447. } else if (ret[1] < 0) {
  448. return ret[1];
  449. } else if (ret[2] < 0)
  450. return ret[2];
  451. return AVERROR_EOF;
  452. } else {
  453. s->img_count++;
  454. s->img_number++;
  455. s->pts++;
  456. return 0;
  457. }
  458. }
  459. static int img_read_close(struct AVFormatContext* s1)
  460. {
  461. #if HAVE_GLOB
  462. VideoDemuxData *s = s1->priv_data;
  463. if (s->use_glob) {
  464. globfree(&s->globstate);
  465. }
  466. #endif
  467. return 0;
  468. }
  469. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  470. {
  471. VideoDemuxData *s1 = s->priv_data;
  472. AVStream *st = s->streams[0];
  473. if (s1->ts_from_file) {
  474. int index = av_index_search_timestamp(st, timestamp, flags);
  475. if(index < 0)
  476. return -1;
  477. s1->img_number = st->index_entries[index].pos;
  478. return 0;
  479. }
  480. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  481. return -1;
  482. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  483. s1->pts = timestamp;
  484. return 0;
  485. }
  486. #define OFFSET(x) offsetof(VideoDemuxData, x)
  487. #define DEC AV_OPT_FLAG_DECODING_PARAM
  488. const AVOption ff_img_options[] = {
  489. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  490. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  491. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  492. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  493. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  494. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  495. { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  496. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  497. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
  498. { "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 },
  499. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  500. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  501. { "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" },
  502. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  503. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  504. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  505. { NULL },
  506. };
  507. #if CONFIG_IMAGE2_DEMUXER
  508. static const AVClass img2_class = {
  509. .class_name = "image2 demuxer",
  510. .item_name = av_default_item_name,
  511. .option = ff_img_options,
  512. .version = LIBAVUTIL_VERSION_INT,
  513. };
  514. AVInputFormat ff_image2_demuxer = {
  515. .name = "image2",
  516. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  517. .priv_data_size = sizeof(VideoDemuxData),
  518. .read_probe = img_read_probe,
  519. .read_header = ff_img_read_header,
  520. .read_packet = ff_img_read_packet,
  521. .read_close = img_read_close,
  522. .read_seek = img_read_seek,
  523. .flags = AVFMT_NOFILE,
  524. .priv_class = &img2_class,
  525. };
  526. #endif
  527. #if CONFIG_IMAGE2PIPE_DEMUXER
  528. static const AVClass img2pipe_class = {
  529. .class_name = "image2pipe demuxer",
  530. .item_name = av_default_item_name,
  531. .option = ff_img_options,
  532. .version = LIBAVUTIL_VERSION_INT,
  533. };
  534. AVInputFormat ff_image2pipe_demuxer = {
  535. .name = "image2pipe",
  536. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  537. .priv_data_size = sizeof(VideoDemuxData),
  538. .read_header = ff_img_read_header,
  539. .read_packet = ff_img_read_packet,
  540. .priv_class = &img2pipe_class,
  541. };
  542. #endif
  543. static int bmp_probe(AVProbeData *p)
  544. {
  545. const uint8_t *b = p->buf;
  546. int ihsize;
  547. if (AV_RB16(b) != 0x424d)
  548. return 0;
  549. ihsize = AV_RL32(b+14);
  550. if (ihsize < 12 || ihsize > 255)
  551. return 0;
  552. if (!AV_RN32(b + 6)) {
  553. return AVPROBE_SCORE_EXTENSION + 1;
  554. }
  555. return AVPROBE_SCORE_EXTENSION / 4;
  556. }
  557. static int dds_probe(AVProbeData *p)
  558. {
  559. const uint8_t *b = p->buf;
  560. if ( AV_RB64(b) == 0x444453207c000000
  561. && AV_RL32(b + 8)
  562. && AV_RL32(b + 12))
  563. return AVPROBE_SCORE_MAX - 1;
  564. return 0;
  565. }
  566. static int dpx_probe(AVProbeData *p)
  567. {
  568. const uint8_t *b = p->buf;
  569. int w, h;
  570. int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
  571. if (p->buf_size < 0x304+8)
  572. return 0;
  573. w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
  574. h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
  575. if (w <= 0 || h <= 0)
  576. return 0;
  577. if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
  578. return AVPROBE_SCORE_EXTENSION + 1;
  579. return 0;
  580. }
  581. static int exr_probe(AVProbeData *p)
  582. {
  583. const uint8_t *b = p->buf;
  584. if (AV_RL32(b) == 20000630)
  585. return AVPROBE_SCORE_EXTENSION + 1;
  586. return 0;
  587. }
  588. static int j2k_probe(AVProbeData *p)
  589. {
  590. const uint8_t *b = p->buf;
  591. if (AV_RB64(b) == 0x0000000c6a502020 ||
  592. AV_RB32(b) == 0xff4fff51)
  593. return AVPROBE_SCORE_EXTENSION + 1;
  594. return 0;
  595. }
  596. static int jpeg_probe(AVProbeData *p)
  597. {
  598. const uint8_t *b = p->buf;
  599. int i, state = 0xD8;
  600. if (AV_RB16(b) != 0xFFD8 ||
  601. AV_RB32(b) == 0xFFD8FFF7)
  602. return 0;
  603. b += 2;
  604. for (i = 0; i < p->buf_size - 3; i++) {
  605. int c;
  606. if (b[i] != 0xFF)
  607. continue;
  608. c = b[i + 1];
  609. switch (c) {
  610. case 0xD8:
  611. return 0;
  612. case 0xC0:
  613. case 0xC1:
  614. case 0xC2:
  615. case 0xC3:
  616. case 0xC5:
  617. case 0xC6:
  618. case 0xC7:
  619. if (state != 0xD8)
  620. return 0;
  621. state = 0xC0;
  622. break;
  623. case 0xDA:
  624. if (state != 0xC0)
  625. return 0;
  626. state = 0xDA;
  627. break;
  628. case 0xD9:
  629. if (state != 0xDA)
  630. return 0;
  631. state = 0xD9;
  632. break;
  633. case 0xE0:
  634. case 0xE1:
  635. case 0xE2:
  636. case 0xE3:
  637. case 0xE4:
  638. case 0xE5:
  639. case 0xE6:
  640. case 0xE7:
  641. case 0xE8:
  642. case 0xE9:
  643. case 0xEA:
  644. case 0xEB:
  645. case 0xEC:
  646. case 0xED:
  647. case 0xEE:
  648. case 0xEF:
  649. i += AV_RB16(&b[i + 2]) + 1;
  650. break;
  651. default:
  652. if ( (c >= 0x02 && c <= 0xBF)
  653. || c == 0xC8)
  654. return 0;
  655. }
  656. }
  657. if (state == 0xD9)
  658. return AVPROBE_SCORE_EXTENSION + 1;
  659. return AVPROBE_SCORE_EXTENSION / 8;
  660. }
  661. static int jpegls_probe(AVProbeData *p)
  662. {
  663. const uint8_t *b = p->buf;
  664. if (AV_RB32(b) == 0xffd8fff7)
  665. return AVPROBE_SCORE_EXTENSION + 1;
  666. return 0;
  667. }
  668. static int qdraw_probe(AVProbeData *p)
  669. {
  670. const uint8_t *b = p->buf;
  671. if ( p->buf_size >= 528
  672. && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  673. && AV_RB16(b + 520)
  674. && AV_RB16(b + 518))
  675. return AVPROBE_SCORE_MAX * 3 / 4;
  676. if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  677. && AV_RB16(b + 8)
  678. && AV_RB16(b + 6))
  679. return AVPROBE_SCORE_EXTENSION / 4;
  680. return 0;
  681. }
  682. static int pictor_probe(AVProbeData *p)
  683. {
  684. const uint8_t *b = p->buf;
  685. if (AV_RL16(b) == 0x1234)
  686. return AVPROBE_SCORE_EXTENSION / 4;
  687. return 0;
  688. }
  689. static int png_probe(AVProbeData *p)
  690. {
  691. const uint8_t *b = p->buf;
  692. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  693. return AVPROBE_SCORE_MAX - 1;
  694. return 0;
  695. }
  696. static int sgi_probe(AVProbeData *p)
  697. {
  698. const uint8_t *b = p->buf;
  699. if (AV_RB16(b) == 474 &&
  700. (b[2] & ~1) == 0 &&
  701. (b[3] & ~3) == 0 && b[3] &&
  702. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  703. return AVPROBE_SCORE_EXTENSION + 1;
  704. return 0;
  705. }
  706. static int sunrast_probe(AVProbeData *p)
  707. {
  708. const uint8_t *b = p->buf;
  709. if (AV_RB32(b) == 0x59a66a95)
  710. return AVPROBE_SCORE_EXTENSION + 1;
  711. return 0;
  712. }
  713. static int tiff_probe(AVProbeData *p)
  714. {
  715. const uint8_t *b = p->buf;
  716. if (AV_RB32(b) == 0x49492a00 ||
  717. AV_RB32(b) == 0x4D4D002a)
  718. return AVPROBE_SCORE_EXTENSION + 1;
  719. return 0;
  720. }
  721. static int webp_probe(AVProbeData *p)
  722. {
  723. const uint8_t *b = p->buf;
  724. if (AV_RB32(b) == 0x52494646 &&
  725. AV_RB32(b + 8) == 0x57454250)
  726. return AVPROBE_SCORE_MAX - 1;
  727. return 0;
  728. }
  729. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  730. static const AVClass imgname ## _class = {\
  731. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  732. .item_name = av_default_item_name,\
  733. .option = ff_img_options,\
  734. .version = LIBAVUTIL_VERSION_INT,\
  735. };\
  736. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  737. .name = AV_STRINGIFY(imgname) "_pipe",\
  738. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  739. .priv_data_size = sizeof(VideoDemuxData),\
  740. .read_probe = imgname ## _probe,\
  741. .read_header = ff_img_read_header,\
  742. .read_packet = ff_img_read_packet,\
  743. .priv_class = & imgname ## _class,\
  744. .flags = AVFMT_GENERIC_INDEX, \
  745. .raw_codec_id = codecid,\
  746. };
  747. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  748. IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
  749. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  750. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  751. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  752. IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
  753. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  754. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  755. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  756. IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
  757. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  758. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  759. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  760. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)