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.

861 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 _DEFAULT_SOURCE
  23. #define _BSD_SOURCE
  24. #include <sys/stat.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "avformat.h"
  32. #include "avio_internal.h"
  33. #include "internal.h"
  34. #include "img2.h"
  35. #if HAVE_GLOB
  36. /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
  37. are non-posix glibc/bsd extensions. */
  38. #ifndef GLOB_NOMAGIC
  39. #define GLOB_NOMAGIC 0
  40. #endif
  41. #ifndef GLOB_BRACE
  42. #define GLOB_BRACE 0
  43. #endif
  44. #endif /* HAVE_GLOB */
  45. static const int sizes[][2] = {
  46. { 640, 480 },
  47. { 720, 480 },
  48. { 720, 576 },
  49. { 352, 288 },
  50. { 352, 240 },
  51. { 160, 128 },
  52. { 512, 384 },
  53. { 640, 352 },
  54. { 640, 240 },
  55. };
  56. static int infer_size(int *width_ptr, int *height_ptr, int size)
  57. {
  58. int i;
  59. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  60. if ((sizes[i][0] * sizes[i][1]) == size) {
  61. *width_ptr = sizes[i][0];
  62. *height_ptr = sizes[i][1];
  63. return 0;
  64. }
  65. }
  66. return -1;
  67. }
  68. static int is_glob(const char *path)
  69. {
  70. #if HAVE_GLOB
  71. size_t span = 0;
  72. const char *p = path;
  73. while (p = strchr(p, '%')) {
  74. if (*(++p) == '%') {
  75. ++p;
  76. continue;
  77. }
  78. if (span = strspn(p, "*?[]{}"))
  79. break;
  80. }
  81. /* Did we hit a glob char or get to the end? */
  82. return span != 0;
  83. #else
  84. return 0;
  85. #endif
  86. }
  87. /**
  88. * Get index range of image files matched by path.
  89. *
  90. * @param pfirst_index pointer to index updated with the first number in the range
  91. * @param plast_index pointer to index updated with the last number in the range
  92. * @param path path which has to be matched by the image files in the range
  93. * @param start_index minimum accepted value for the first index in the range
  94. * @return -1 if no image file could be found
  95. */
  96. static int find_image_range(int *pfirst_index, int *plast_index,
  97. const char *path, int start_index, int start_index_range)
  98. {
  99. char buf[1024];
  100. int range, last_index, range1, first_index;
  101. /* find the first image */
  102. for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
  103. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  104. *pfirst_index =
  105. *plast_index = 1;
  106. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  107. return 0;
  108. return -1;
  109. }
  110. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  111. break;
  112. }
  113. if (first_index == start_index + start_index_range)
  114. goto fail;
  115. /* find the last image */
  116. last_index = first_index;
  117. for (;;) {
  118. range = 0;
  119. for (;;) {
  120. if (!range)
  121. range1 = 1;
  122. else
  123. range1 = 2 * range;
  124. if (av_get_frame_filename(buf, sizeof(buf), path,
  125. last_index + range1) < 0)
  126. goto fail;
  127. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  128. break;
  129. range = range1;
  130. /* just in case... */
  131. if (range >= (1 << 30))
  132. goto fail;
  133. }
  134. /* we are sure than image last_index + range exists */
  135. if (!range)
  136. break;
  137. last_index += range;
  138. }
  139. *pfirst_index = first_index;
  140. *plast_index = last_index;
  141. return 0;
  142. fail:
  143. return -1;
  144. }
  145. static int img_read_probe(AVProbeData *p)
  146. {
  147. if (p->filename && ff_guess_image2_codec(p->filename)) {
  148. if (av_filename_number_test(p->filename))
  149. return AVPROBE_SCORE_MAX;
  150. else if (is_glob(p->filename))
  151. return AVPROBE_SCORE_MAX;
  152. else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
  153. return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
  154. else if (p->buf_size == 0)
  155. return 0;
  156. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  157. return 5;
  158. else
  159. return AVPROBE_SCORE_EXTENSION;
  160. }
  161. return 0;
  162. }
  163. int ff_img_read_header(AVFormatContext *s1)
  164. {
  165. VideoDemuxData *s = s1->priv_data;
  166. int first_index = 1, last_index = 1;
  167. AVStream *st;
  168. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  169. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  170. st = avformat_new_stream(s1, NULL);
  171. if (!st) {
  172. return AVERROR(ENOMEM);
  173. }
  174. if (s->pixel_format &&
  175. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  176. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  177. s->pixel_format);
  178. return AVERROR(EINVAL);
  179. }
  180. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  181. s->img_number = 0;
  182. s->img_count = 0;
  183. /* find format */
  184. if (s1->iformat->flags & AVFMT_NOFILE)
  185. s->is_pipe = 0;
  186. else {
  187. s->is_pipe = 1;
  188. st->need_parsing = AVSTREAM_PARSE_FULL;
  189. }
  190. if (s->ts_from_file == 2) {
  191. #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  192. av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
  193. return AVERROR(ENOSYS);
  194. #endif
  195. avpriv_set_pts_info(st, 64, 1, 1000000000);
  196. } else if (s->ts_from_file)
  197. avpriv_set_pts_info(st, 64, 1, 1);
  198. else
  199. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  200. if (s->width && s->height) {
  201. st->codec->width = s->width;
  202. st->codec->height = s->height;
  203. }
  204. if (!s->is_pipe) {
  205. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  206. s->use_glob = is_glob(s->path);
  207. if (s->use_glob) {
  208. #if HAVE_GLOB
  209. char *p = s->path, *q, *dup;
  210. int gerr;
  211. #endif
  212. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  213. "use pattern_type 'glob' instead\n");
  214. #if HAVE_GLOB
  215. dup = q = av_strdup(p);
  216. while (*q) {
  217. /* Do we have room for the next char and a \ insertion? */
  218. if ((p - s->path) >= (sizeof(s->path) - 2))
  219. break;
  220. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  221. ++q;
  222. else if (strspn(q, "\\*?[]{}"))
  223. *p++ = '\\';
  224. *p++ = *q++;
  225. }
  226. *p = 0;
  227. av_free(dup);
  228. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  229. if (gerr != 0) {
  230. return AVERROR(ENOENT);
  231. }
  232. first_index = 0;
  233. last_index = s->globstate.gl_pathc - 1;
  234. #endif
  235. }
  236. }
  237. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  238. if (find_image_range(&first_index, &last_index, s->path,
  239. s->start_number, s->start_number_range) < 0) {
  240. av_log(s1, AV_LOG_ERROR,
  241. "Could find no file with path '%s' and index in the range %d-%d\n",
  242. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  243. return AVERROR(ENOENT);
  244. }
  245. } else if (s->pattern_type == PT_GLOB) {
  246. #if HAVE_GLOB
  247. int gerr;
  248. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  249. if (gerr != 0) {
  250. return AVERROR(ENOENT);
  251. }
  252. first_index = 0;
  253. last_index = s->globstate.gl_pathc - 1;
  254. s->use_glob = 1;
  255. #else
  256. av_log(s1, AV_LOG_ERROR,
  257. "Pattern type 'glob' was selected but globbing "
  258. "is not supported by this libavformat build\n");
  259. return AVERROR(ENOSYS);
  260. #endif
  261. } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
  262. av_log(s1, AV_LOG_ERROR,
  263. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  264. return AVERROR(EINVAL);
  265. }
  266. s->img_first = first_index;
  267. s->img_last = last_index;
  268. s->img_number = first_index;
  269. /* compute duration */
  270. if (!s->ts_from_file) {
  271. st->start_time = 0;
  272. st->duration = last_index - first_index + 1;
  273. }
  274. }
  275. if (s1->video_codec_id) {
  276. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  277. st->codec->codec_id = s1->video_codec_id;
  278. } else if (s1->audio_codec_id) {
  279. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  280. st->codec->codec_id = s1->audio_codec_id;
  281. } else if (s1->iformat->raw_codec_id) {
  282. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  283. st->codec->codec_id = s1->iformat->raw_codec_id;
  284. } else {
  285. const char *str = strrchr(s->path, '.');
  286. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  287. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  288. if (s1->pb) {
  289. int probe_buffer_size = 2048;
  290. uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
  291. AVInputFormat *fmt = NULL;
  292. AVProbeData pd = { 0 };
  293. if (!probe_buffer)
  294. return AVERROR(ENOMEM);
  295. probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
  296. if (probe_buffer_size < 0) {
  297. av_free(probe_buffer);
  298. return probe_buffer_size;
  299. }
  300. memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
  301. pd.buf = probe_buffer;
  302. pd.buf_size = probe_buffer_size;
  303. pd.filename = s1->filename;
  304. while ((fmt = av_iformat_next(fmt))) {
  305. if (fmt->read_header != ff_img_read_header ||
  306. !fmt->read_probe ||
  307. (fmt->flags & AVFMT_NOFILE) ||
  308. !fmt->raw_codec_id)
  309. continue;
  310. if (fmt->read_probe(&pd) > 0) {
  311. st->codec->codec_id = fmt->raw_codec_id;
  312. break;
  313. }
  314. }
  315. if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
  316. avio_seek(s1->pb, 0, SEEK_SET);
  317. } else
  318. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  319. }
  320. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  321. st->codec->codec_id = ff_guess_image2_codec(s->path);
  322. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  323. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  324. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  325. st->codec->codec_id = AV_CODEC_ID_NONE;
  326. }
  327. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  328. pix_fmt != AV_PIX_FMT_NONE)
  329. st->codec->pix_fmt = pix_fmt;
  330. return 0;
  331. }
  332. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  333. {
  334. VideoDemuxData *s = s1->priv_data;
  335. char filename_bytes[1024];
  336. char *filename = filename_bytes;
  337. int i, res;
  338. int size[3] = { 0 }, ret[3] = { 0 };
  339. AVIOContext *f[3] = { NULL };
  340. AVCodecContext *codec = s1->streams[0]->codec;
  341. if (!s->is_pipe) {
  342. /* loop over input */
  343. if (s->loop && s->img_number > s->img_last) {
  344. s->img_number = s->img_first;
  345. }
  346. if (s->img_number > s->img_last)
  347. return AVERROR_EOF;
  348. if (s->pattern_type == PT_NONE) {
  349. av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
  350. } else if (s->use_glob) {
  351. #if HAVE_GLOB
  352. filename = s->globstate.gl_pathv[s->img_number];
  353. #endif
  354. } else {
  355. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  356. s->path,
  357. s->img_number) < 0 && s->img_number > 1)
  358. return AVERROR(EIO);
  359. }
  360. for (i = 0; i < 3; i++) {
  361. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  362. &s1->interrupt_callback, NULL) < 0) {
  363. if (i >= 1)
  364. break;
  365. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  366. filename);
  367. return AVERROR(EIO);
  368. }
  369. size[i] = avio_size(f[i]);
  370. if (!s->split_planes)
  371. break;
  372. filename[strlen(filename) - 1] = 'U' + i;
  373. }
  374. if (codec->codec_id == AV_CODEC_ID_NONE) {
  375. AVProbeData pd = { 0 };
  376. AVInputFormat *ifmt;
  377. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  378. int ret;
  379. int score = 0;
  380. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  381. if (ret < 0)
  382. return ret;
  383. memset(header + ret, 0, sizeof(header) - ret);
  384. avio_skip(f[0], -ret);
  385. pd.buf = header;
  386. pd.buf_size = ret;
  387. pd.filename = filename;
  388. ifmt = av_probe_input_format3(&pd, 1, &score);
  389. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  390. codec->codec_id = ifmt->raw_codec_id;
  391. }
  392. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  393. infer_size(&codec->width, &codec->height, size[0]);
  394. } else {
  395. f[0] = s1->pb;
  396. if (avio_feof(f[0]) && s->loop && s->is_pipe)
  397. avio_seek(f[0], 0, SEEK_SET);
  398. if (avio_feof(f[0]))
  399. return AVERROR_EOF;
  400. if (s->frame_size > 0) {
  401. size[0] = s->frame_size;
  402. } else if (!s1->streams[0]->parser) {
  403. size[0] = avio_size(s1->pb);
  404. } else {
  405. size[0] = 4096;
  406. }
  407. }
  408. res = av_new_packet(pkt, size[0] + size[1] + size[2]);
  409. if (res < 0) {
  410. goto fail;
  411. }
  412. pkt->stream_index = 0;
  413. pkt->flags |= AV_PKT_FLAG_KEY;
  414. if (s->ts_from_file) {
  415. struct stat img_stat;
  416. if (stat(filename, &img_stat)) {
  417. res = AVERROR(EIO);
  418. goto fail;
  419. }
  420. pkt->pts = (int64_t)img_stat.st_mtime;
  421. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  422. if (s->ts_from_file == 2)
  423. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  424. #endif
  425. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  426. } else if (!s->is_pipe) {
  427. pkt->pts = s->pts;
  428. }
  429. if (s->is_pipe)
  430. pkt->pos = avio_tell(f[0]);
  431. pkt->size = 0;
  432. for (i = 0; i < 3; i++) {
  433. if (f[i]) {
  434. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  435. if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
  436. if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
  437. pkt->pos = 0;
  438. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  439. }
  440. }
  441. if (!s->is_pipe)
  442. avio_closep(&f[i]);
  443. if (ret[i] > 0)
  444. pkt->size += ret[i];
  445. }
  446. }
  447. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  448. av_packet_unref(pkt);
  449. if (ret[0] < 0) {
  450. res = ret[0];
  451. } else if (ret[1] < 0) {
  452. res = ret[1];
  453. } else if (ret[2] < 0) {
  454. res = ret[2];
  455. } else {
  456. res = AVERROR_EOF;
  457. }
  458. goto fail;
  459. } else {
  460. s->img_count++;
  461. s->img_number++;
  462. s->pts++;
  463. return 0;
  464. }
  465. fail:
  466. if (!s->is_pipe) {
  467. for (i = 0; i < 3; i++) {
  468. avio_closep(&f[i]);
  469. }
  470. }
  471. return res;
  472. }
  473. static int img_read_close(struct AVFormatContext* s1)
  474. {
  475. #if HAVE_GLOB
  476. VideoDemuxData *s = s1->priv_data;
  477. if (s->use_glob) {
  478. globfree(&s->globstate);
  479. }
  480. #endif
  481. return 0;
  482. }
  483. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  484. {
  485. VideoDemuxData *s1 = s->priv_data;
  486. AVStream *st = s->streams[0];
  487. if (s1->ts_from_file) {
  488. int index = av_index_search_timestamp(st, timestamp, flags);
  489. if(index < 0)
  490. return -1;
  491. s1->img_number = st->index_entries[index].pos;
  492. return 0;
  493. }
  494. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  495. return -1;
  496. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  497. s1->pts = timestamp;
  498. return 0;
  499. }
  500. #define OFFSET(x) offsetof(VideoDemuxData, x)
  501. #define DEC AV_OPT_FLAG_DECODING_PARAM
  502. const AVOption ff_img_options[] = {
  503. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  504. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  505. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  506. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  507. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  508. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  509. { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  510. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  511. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
  512. { "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 },
  513. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  514. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  515. { "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" },
  516. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  517. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  518. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  519. { NULL },
  520. };
  521. #if CONFIG_IMAGE2_DEMUXER
  522. static const AVClass img2_class = {
  523. .class_name = "image2 demuxer",
  524. .item_name = av_default_item_name,
  525. .option = ff_img_options,
  526. .version = LIBAVUTIL_VERSION_INT,
  527. };
  528. AVInputFormat ff_image2_demuxer = {
  529. .name = "image2",
  530. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  531. .priv_data_size = sizeof(VideoDemuxData),
  532. .read_probe = img_read_probe,
  533. .read_header = ff_img_read_header,
  534. .read_packet = ff_img_read_packet,
  535. .read_close = img_read_close,
  536. .read_seek = img_read_seek,
  537. .flags = AVFMT_NOFILE,
  538. .priv_class = &img2_class,
  539. };
  540. #endif
  541. #if CONFIG_IMAGE2PIPE_DEMUXER
  542. static const AVClass img2pipe_class = {
  543. .class_name = "image2pipe demuxer",
  544. .item_name = av_default_item_name,
  545. .option = ff_img_options,
  546. .version = LIBAVUTIL_VERSION_INT,
  547. };
  548. AVInputFormat ff_image2pipe_demuxer = {
  549. .name = "image2pipe",
  550. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  551. .priv_data_size = sizeof(VideoDemuxData),
  552. .read_header = ff_img_read_header,
  553. .read_packet = ff_img_read_packet,
  554. .priv_class = &img2pipe_class,
  555. };
  556. #endif
  557. static int bmp_probe(AVProbeData *p)
  558. {
  559. const uint8_t *b = p->buf;
  560. int ihsize;
  561. if (AV_RB16(b) != 0x424d)
  562. return 0;
  563. ihsize = AV_RL32(b+14);
  564. if (ihsize < 12 || ihsize > 255)
  565. return 0;
  566. if (!AV_RN32(b + 6)) {
  567. return AVPROBE_SCORE_EXTENSION + 1;
  568. }
  569. return AVPROBE_SCORE_EXTENSION / 4;
  570. }
  571. static int dds_probe(AVProbeData *p)
  572. {
  573. const uint8_t *b = p->buf;
  574. if ( AV_RB64(b) == 0x444453207c000000
  575. && AV_RL32(b + 8)
  576. && AV_RL32(b + 12))
  577. return AVPROBE_SCORE_MAX - 1;
  578. return 0;
  579. }
  580. static int dpx_probe(AVProbeData *p)
  581. {
  582. const uint8_t *b = p->buf;
  583. int w, h;
  584. int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
  585. if (p->buf_size < 0x304+8)
  586. return 0;
  587. w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
  588. h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
  589. if (w <= 0 || h <= 0)
  590. return 0;
  591. if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
  592. return AVPROBE_SCORE_EXTENSION + 1;
  593. return 0;
  594. }
  595. static int exr_probe(AVProbeData *p)
  596. {
  597. const uint8_t *b = p->buf;
  598. if (AV_RL32(b) == 20000630)
  599. return AVPROBE_SCORE_EXTENSION + 1;
  600. return 0;
  601. }
  602. static int j2k_probe(AVProbeData *p)
  603. {
  604. const uint8_t *b = p->buf;
  605. if (AV_RB64(b) == 0x0000000c6a502020 ||
  606. AV_RB32(b) == 0xff4fff51)
  607. return AVPROBE_SCORE_EXTENSION + 1;
  608. return 0;
  609. }
  610. static int jpeg_probe(AVProbeData *p)
  611. {
  612. const uint8_t *b = p->buf;
  613. int i, state = 0xD8;
  614. if (AV_RB16(b) != 0xFFD8 ||
  615. AV_RB32(b) == 0xFFD8FFF7)
  616. return 0;
  617. b += 2;
  618. for (i = 0; i < p->buf_size - 3; i++) {
  619. int c;
  620. if (b[i] != 0xFF)
  621. continue;
  622. c = b[i + 1];
  623. switch (c) {
  624. case 0xD8:
  625. return 0;
  626. case 0xC0:
  627. case 0xC1:
  628. case 0xC2:
  629. case 0xC3:
  630. case 0xC5:
  631. case 0xC6:
  632. case 0xC7:
  633. if (state != 0xD8)
  634. return 0;
  635. state = 0xC0;
  636. break;
  637. case 0xDA:
  638. if (state != 0xC0)
  639. return 0;
  640. state = 0xDA;
  641. break;
  642. case 0xD9:
  643. if (state != 0xDA)
  644. return 0;
  645. state = 0xD9;
  646. break;
  647. case 0xE0:
  648. case 0xE1:
  649. case 0xE2:
  650. case 0xE3:
  651. case 0xE4:
  652. case 0xE5:
  653. case 0xE6:
  654. case 0xE7:
  655. case 0xE8:
  656. case 0xE9:
  657. case 0xEA:
  658. case 0xEB:
  659. case 0xEC:
  660. case 0xED:
  661. case 0xEE:
  662. case 0xEF:
  663. i += AV_RB16(&b[i + 2]) + 1;
  664. break;
  665. default:
  666. if ( (c >= 0x02 && c <= 0xBF)
  667. || c == 0xC8)
  668. return 0;
  669. }
  670. }
  671. if (state == 0xD9)
  672. return AVPROBE_SCORE_EXTENSION + 1;
  673. return AVPROBE_SCORE_EXTENSION / 8;
  674. }
  675. static int jpegls_probe(AVProbeData *p)
  676. {
  677. const uint8_t *b = p->buf;
  678. if (AV_RB32(b) == 0xffd8fff7)
  679. return AVPROBE_SCORE_EXTENSION + 1;
  680. return 0;
  681. }
  682. static int qdraw_probe(AVProbeData *p)
  683. {
  684. const uint8_t *b = p->buf;
  685. if ( p->buf_size >= 528
  686. && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  687. && AV_RB16(b + 520)
  688. && AV_RB16(b + 518))
  689. return AVPROBE_SCORE_MAX * 3 / 4;
  690. if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  691. && AV_RB16(b + 8)
  692. && AV_RB16(b + 6))
  693. return AVPROBE_SCORE_EXTENSION / 4;
  694. return 0;
  695. }
  696. static int pictor_probe(AVProbeData *p)
  697. {
  698. const uint8_t *b = p->buf;
  699. if (AV_RL16(b) == 0x1234)
  700. return AVPROBE_SCORE_EXTENSION / 4;
  701. return 0;
  702. }
  703. static int png_probe(AVProbeData *p)
  704. {
  705. const uint8_t *b = p->buf;
  706. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  707. return AVPROBE_SCORE_MAX - 1;
  708. return 0;
  709. }
  710. static int sgi_probe(AVProbeData *p)
  711. {
  712. const uint8_t *b = p->buf;
  713. if (AV_RB16(b) == 474 &&
  714. (b[2] & ~1) == 0 &&
  715. (b[3] & ~3) == 0 && b[3] &&
  716. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  717. return AVPROBE_SCORE_EXTENSION + 1;
  718. return 0;
  719. }
  720. static int sunrast_probe(AVProbeData *p)
  721. {
  722. const uint8_t *b = p->buf;
  723. if (AV_RB32(b) == 0x59a66a95)
  724. return AVPROBE_SCORE_EXTENSION + 1;
  725. return 0;
  726. }
  727. static int tiff_probe(AVProbeData *p)
  728. {
  729. const uint8_t *b = p->buf;
  730. if (AV_RB32(b) == 0x49492a00 ||
  731. AV_RB32(b) == 0x4D4D002a)
  732. return AVPROBE_SCORE_EXTENSION + 1;
  733. return 0;
  734. }
  735. static int webp_probe(AVProbeData *p)
  736. {
  737. const uint8_t *b = p->buf;
  738. if (AV_RB32(b) == 0x52494646 &&
  739. AV_RB32(b + 8) == 0x57454250)
  740. return AVPROBE_SCORE_MAX - 1;
  741. return 0;
  742. }
  743. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  744. static const AVClass imgname ## _class = {\
  745. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  746. .item_name = av_default_item_name,\
  747. .option = ff_img_options,\
  748. .version = LIBAVUTIL_VERSION_INT,\
  749. };\
  750. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  751. .name = AV_STRINGIFY(imgname) "_pipe",\
  752. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  753. .priv_data_size = sizeof(VideoDemuxData),\
  754. .read_probe = imgname ## _probe,\
  755. .read_header = ff_img_read_header,\
  756. .read_packet = ff_img_read_packet,\
  757. .priv_class = & imgname ## _class,\
  758. .flags = AVFMT_GENERIC_INDEX, \
  759. .raw_codec_id = codecid,\
  760. };
  761. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  762. IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
  763. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  764. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  765. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  766. IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
  767. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  768. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  769. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  770. IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
  771. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  772. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  773. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  774. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)