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.

511 lines
17KB

  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. #include <sys/stat.h>
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/parseutils.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. #include "img2.h"
  31. #if HAVE_GLOB
  32. /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
  33. are non-posix glibc/bsd extensions. */
  34. #ifndef GLOB_NOMAGIC
  35. #define GLOB_NOMAGIC 0
  36. #endif
  37. #ifndef GLOB_BRACE
  38. #define GLOB_BRACE 0
  39. #endif
  40. #endif /* HAVE_GLOB */
  41. static const int sizes[][2] = {
  42. { 640, 480 },
  43. { 720, 480 },
  44. { 720, 576 },
  45. { 352, 288 },
  46. { 352, 240 },
  47. { 160, 128 },
  48. { 512, 384 },
  49. { 640, 352 },
  50. { 640, 240 },
  51. };
  52. static int infer_size(int *width_ptr, int *height_ptr, int size)
  53. {
  54. int i;
  55. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  56. if ((sizes[i][0] * sizes[i][1]) == size) {
  57. *width_ptr = sizes[i][0];
  58. *height_ptr = sizes[i][1];
  59. return 0;
  60. }
  61. }
  62. return -1;
  63. }
  64. static int is_glob(const char *path)
  65. {
  66. #if HAVE_GLOB
  67. size_t span = 0;
  68. const char *p = path;
  69. while (p = strchr(p, '%')) {
  70. if (*(++p) == '%') {
  71. ++p;
  72. continue;
  73. }
  74. if (span = strspn(p, "*?[]{}"))
  75. break;
  76. }
  77. /* Did we hit a glob char or get to the end? */
  78. return span != 0;
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. /**
  84. * Get index range of image files matched by path.
  85. *
  86. * @param pfirst_index pointer to index updated with the first number in the range
  87. * @param plast_index pointer to index updated with the last number in the range
  88. * @param path path which has to be matched by the image files in the range
  89. * @param start_index minimum accepted value for the first index in the range
  90. * @return -1 if no image file could be found
  91. */
  92. static int find_image_range(int *pfirst_index, int *plast_index,
  93. const char *path, int start_index, int start_index_range)
  94. {
  95. char buf[1024];
  96. int range, last_index, range1, first_index;
  97. /* find the first image */
  98. for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
  99. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  100. *pfirst_index =
  101. *plast_index = 1;
  102. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  103. return 0;
  104. return -1;
  105. }
  106. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  107. break;
  108. }
  109. if (first_index == start_index + start_index_range)
  110. goto fail;
  111. /* find the last image */
  112. last_index = first_index;
  113. for (;;) {
  114. range = 0;
  115. for (;;) {
  116. if (!range)
  117. range1 = 1;
  118. else
  119. range1 = 2 * range;
  120. if (av_get_frame_filename(buf, sizeof(buf), path,
  121. last_index + range1) < 0)
  122. goto fail;
  123. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  124. break;
  125. range = range1;
  126. /* just in case... */
  127. if (range >= (1 << 30))
  128. goto fail;
  129. }
  130. /* we are sure than image last_index + range exists */
  131. if (!range)
  132. break;
  133. last_index += range;
  134. }
  135. *pfirst_index = first_index;
  136. *plast_index = last_index;
  137. return 0;
  138. fail:
  139. return -1;
  140. }
  141. static int img_read_probe(AVProbeData *p)
  142. {
  143. if (p->filename && ff_guess_image2_codec(p->filename)) {
  144. if (av_filename_number_test(p->filename))
  145. return AVPROBE_SCORE_MAX;
  146. else if (is_glob(p->filename))
  147. return AVPROBE_SCORE_MAX;
  148. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  149. return 5;
  150. else
  151. return AVPROBE_SCORE_EXTENSION;
  152. }
  153. return 0;
  154. }
  155. int ff_img_read_header(AVFormatContext *s1)
  156. {
  157. VideoDemuxData *s = s1->priv_data;
  158. int first_index, last_index;
  159. AVStream *st;
  160. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  161. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  162. st = avformat_new_stream(s1, NULL);
  163. if (!st) {
  164. return AVERROR(ENOMEM);
  165. }
  166. if (s->pixel_format &&
  167. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  168. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  169. s->pixel_format);
  170. return AVERROR(EINVAL);
  171. }
  172. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  173. s->img_number = 0;
  174. s->img_count = 0;
  175. /* find format */
  176. if (s1->iformat->flags & AVFMT_NOFILE)
  177. s->is_pipe = 0;
  178. else {
  179. s->is_pipe = 1;
  180. st->need_parsing = AVSTREAM_PARSE_FULL;
  181. }
  182. if (s->ts_from_file)
  183. avpriv_set_pts_info(st, 64, 1, 1);
  184. else
  185. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  186. if (s->width && s->height) {
  187. st->codec->width = s->width;
  188. st->codec->height = s->height;
  189. }
  190. if (!s->is_pipe) {
  191. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  192. s->use_glob = is_glob(s->path);
  193. if (s->use_glob) {
  194. char *p = s->path, *q, *dup;
  195. int gerr;
  196. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  197. "use pattern_type 'glob' instead\n");
  198. #if HAVE_GLOB
  199. dup = q = av_strdup(p);
  200. while (*q) {
  201. /* Do we have room for the next char and a \ insertion? */
  202. if ((p - s->path) >= (sizeof(s->path) - 2))
  203. break;
  204. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  205. ++q;
  206. else if (strspn(q, "\\*?[]{}"))
  207. *p++ = '\\';
  208. *p++ = *q++;
  209. }
  210. *p = 0;
  211. av_free(dup);
  212. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  213. if (gerr != 0) {
  214. return AVERROR(ENOENT);
  215. }
  216. first_index = 0;
  217. last_index = s->globstate.gl_pathc - 1;
  218. #endif
  219. }
  220. }
  221. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  222. if (find_image_range(&first_index, &last_index, s->path,
  223. s->start_number, s->start_number_range) < 0) {
  224. av_log(s1, AV_LOG_ERROR,
  225. "Could find no file with path '%s' and index in the range %d-%d\n",
  226. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  227. return AVERROR(ENOENT);
  228. }
  229. } else if (s->pattern_type == PT_GLOB) {
  230. #if HAVE_GLOB
  231. int gerr;
  232. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  233. if (gerr != 0) {
  234. return AVERROR(ENOENT);
  235. }
  236. first_index = 0;
  237. last_index = s->globstate.gl_pathc - 1;
  238. s->use_glob = 1;
  239. #else
  240. av_log(s1, AV_LOG_ERROR,
  241. "Pattern type 'glob' was selected but globbing "
  242. "is not supported by this libavformat build\n");
  243. return AVERROR(ENOSYS);
  244. #endif
  245. } else if (s->pattern_type != PT_GLOB_SEQUENCE) {
  246. av_log(s1, AV_LOG_ERROR,
  247. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  248. return AVERROR(EINVAL);
  249. }
  250. s->img_first = first_index;
  251. s->img_last = last_index;
  252. s->img_number = first_index;
  253. /* compute duration */
  254. if (!s->ts_from_file) {
  255. st->start_time = 0;
  256. st->duration = last_index - first_index + 1;
  257. }
  258. }
  259. if (s1->video_codec_id) {
  260. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  261. st->codec->codec_id = s1->video_codec_id;
  262. } else if (s1->audio_codec_id) {
  263. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  264. st->codec->codec_id = s1->audio_codec_id;
  265. } else if (s1->iformat->raw_codec_id) {
  266. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  267. st->codec->codec_id = s1->iformat->raw_codec_id;
  268. } else {
  269. const char *str = strrchr(s->path, '.');
  270. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  271. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  272. st->codec->codec_id = ff_guess_image2_codec(s->path);
  273. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  274. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  275. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  276. st->codec->codec_id = AV_CODEC_ID_NONE;
  277. }
  278. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  279. pix_fmt != AV_PIX_FMT_NONE)
  280. st->codec->pix_fmt = pix_fmt;
  281. return 0;
  282. }
  283. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  284. {
  285. VideoDemuxData *s = s1->priv_data;
  286. char filename_bytes[1024];
  287. char *filename = filename_bytes;
  288. int i;
  289. int size[3] = { 0 }, ret[3] = { 0 };
  290. AVIOContext *f[3] = { NULL };
  291. AVCodecContext *codec = s1->streams[0]->codec;
  292. if (!s->is_pipe) {
  293. /* loop over input */
  294. if (s->loop && s->img_number > s->img_last) {
  295. s->img_number = s->img_first;
  296. }
  297. if (s->img_number > s->img_last)
  298. return AVERROR_EOF;
  299. if (s->use_glob) {
  300. #if HAVE_GLOB
  301. filename = s->globstate.gl_pathv[s->img_number];
  302. #endif
  303. } else {
  304. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  305. s->path,
  306. s->img_number) < 0 && s->img_number > 1)
  307. return AVERROR(EIO);
  308. }
  309. for (i = 0; i < 3; i++) {
  310. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  311. &s1->interrupt_callback, NULL) < 0) {
  312. if (i >= 1)
  313. break;
  314. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  315. filename);
  316. return AVERROR(EIO);
  317. }
  318. size[i] = avio_size(f[i]);
  319. if (!s->split_planes)
  320. break;
  321. filename[strlen(filename) - 1] = 'U' + i;
  322. }
  323. if (codec->codec_id == AV_CODEC_ID_NONE) {
  324. AVProbeData pd;
  325. AVInputFormat *ifmt;
  326. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  327. int ret;
  328. int score = 0;
  329. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  330. if (ret < 0)
  331. return ret;
  332. memset(header + ret, 0, sizeof(header) - ret);
  333. avio_skip(f[0], -ret);
  334. pd.buf = header;
  335. pd.buf_size = ret;
  336. pd.filename = filename;
  337. ifmt = av_probe_input_format3(&pd, 1, &score);
  338. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  339. codec->codec_id = ifmt->raw_codec_id;
  340. }
  341. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  342. infer_size(&codec->width, &codec->height, size[0]);
  343. } else {
  344. f[0] = s1->pb;
  345. if (url_feof(f[0]))
  346. return AVERROR(EIO);
  347. if (s->frame_size > 0) {
  348. size[0] = s->frame_size;
  349. } else {
  350. size[0] = 4096;
  351. }
  352. }
  353. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  354. return AVERROR(ENOMEM);
  355. pkt->stream_index = 0;
  356. pkt->flags |= AV_PKT_FLAG_KEY;
  357. if (s->ts_from_file) {
  358. struct stat img_stat;
  359. if (stat(filename, &img_stat))
  360. return AVERROR(EIO);
  361. pkt->pts = (int64_t)img_stat.st_mtime;
  362. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  363. } else if (!s->is_pipe) {
  364. pkt->pts = s->pts;
  365. }
  366. pkt->size = 0;
  367. for (i = 0; i < 3; i++) {
  368. if (f[i]) {
  369. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  370. if (!s->is_pipe)
  371. avio_close(f[i]);
  372. if (ret[i] > 0)
  373. pkt->size += ret[i];
  374. }
  375. }
  376. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  377. av_free_packet(pkt);
  378. return AVERROR(EIO); /* signal EOF */
  379. } else {
  380. s->img_count++;
  381. s->img_number++;
  382. s->pts++;
  383. return 0;
  384. }
  385. }
  386. static int img_read_close(struct AVFormatContext* s1)
  387. {
  388. VideoDemuxData *s = s1->priv_data;
  389. #if HAVE_GLOB
  390. if (s->use_glob) {
  391. globfree(&s->globstate);
  392. }
  393. #endif
  394. return 0;
  395. }
  396. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  397. {
  398. VideoDemuxData *s1 = s->priv_data;
  399. AVStream *st = s->streams[0];
  400. if (s1->ts_from_file) {
  401. int index = av_index_search_timestamp(st, timestamp, flags);
  402. if(index < 0)
  403. return -1;
  404. s1->img_number = st->index_entries[index].pos;
  405. return 0;
  406. }
  407. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  408. return -1;
  409. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  410. s1->pts = timestamp;
  411. return 0;
  412. }
  413. #define OFFSET(x) offsetof(VideoDemuxData, x)
  414. #define DEC AV_OPT_FLAG_DECODING_PARAM
  415. static const AVOption options[] = {
  416. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  417. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  418. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  419. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  420. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  421. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  422. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  423. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  424. { "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 },
  425. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  426. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  427. { "ts_from_file", "set frame timestamp from file's one", OFFSET(ts_from_file), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  428. { NULL },
  429. };
  430. #if CONFIG_IMAGE2_DEMUXER
  431. static const AVClass img2_class = {
  432. .class_name = "image2 demuxer",
  433. .item_name = av_default_item_name,
  434. .option = options,
  435. .version = LIBAVUTIL_VERSION_INT,
  436. };
  437. AVInputFormat ff_image2_demuxer = {
  438. .name = "image2",
  439. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  440. .priv_data_size = sizeof(VideoDemuxData),
  441. .read_probe = img_read_probe,
  442. .read_header = ff_img_read_header,
  443. .read_packet = ff_img_read_packet,
  444. .read_close = img_read_close,
  445. .read_seek = img_read_seek,
  446. .flags = AVFMT_NOFILE,
  447. .priv_class = &img2_class,
  448. };
  449. #endif
  450. #if CONFIG_IMAGE2PIPE_DEMUXER
  451. static const AVClass img2pipe_class = {
  452. .class_name = "image2pipe demuxer",
  453. .item_name = av_default_item_name,
  454. .option = options,
  455. .version = LIBAVUTIL_VERSION_INT,
  456. };
  457. AVInputFormat ff_image2pipe_demuxer = {
  458. .name = "image2pipe",
  459. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  460. .priv_data_size = sizeof(VideoDemuxData),
  461. .read_header = ff_img_read_header,
  462. .read_packet = ff_img_read_packet,
  463. .priv_class = &img2pipe_class,
  464. };
  465. #endif