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.

468 lines
15KB

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