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.

496 lines
16KB

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