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.

474 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. 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 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"))
  170. return 5;
  171. else
  172. return AVPROBE_SCORE_MAX/2;
  173. }
  174. return 0;
  175. }
  176. static int 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 && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  190. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  191. return AVERROR(EINVAL);
  192. }
  193. if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  194. av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
  195. return ret;
  196. }
  197. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  198. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
  199. return ret;
  200. }
  201. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  202. s->img_number = 0;
  203. s->img_count = 0;
  204. /* find format */
  205. if (s1->iformat->flags & AVFMT_NOFILE)
  206. s->is_pipe = 0;
  207. else{
  208. s->is_pipe = 1;
  209. st->need_parsing = AVSTREAM_PARSE_FULL;
  210. }
  211. avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
  212. if (width && height) {
  213. st->codec->width = width;
  214. st->codec->height = height;
  215. }
  216. if (!s->is_pipe) {
  217. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  218. s->use_glob = is_glob(s->path);
  219. if (s->use_glob) {
  220. char *p = s->path, *q, *dup;
  221. int gerr;
  222. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  223. "use pattern_type 'glob' instead\n");
  224. #if HAVE_GLOB
  225. dup = q = av_strdup(p);
  226. while (*q) {
  227. /* Do we have room for the next char and a \ insertion? */
  228. if ((p - s->path) >= (sizeof(s->path) - 2))
  229. break;
  230. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  231. ++q;
  232. else if (strspn(q, "\\*?[]{}"))
  233. *p++ = '\\';
  234. *p++ = *q++;
  235. }
  236. *p = 0;
  237. av_free(dup);
  238. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  239. if (gerr != 0) {
  240. return AVERROR(ENOENT);
  241. }
  242. first_index = 0;
  243. last_index = s->globstate.gl_pathc - 1;
  244. #endif
  245. }
  246. }
  247. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  248. if (find_image_range(&first_index, &last_index, s->path,
  249. s->start_number, s->start_number_range) < 0) {
  250. av_log(s1, AV_LOG_ERROR,
  251. "Could find no file with with path '%s' and index in the range %d-%d\n",
  252. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  253. return AVERROR(ENOENT);
  254. }
  255. } else if (s->pattern_type == PT_GLOB) {
  256. #if HAVE_GLOB
  257. int gerr;
  258. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  259. if (gerr != 0) {
  260. return AVERROR(ENOENT);
  261. }
  262. first_index = 0;
  263. last_index = s->globstate.gl_pathc - 1;
  264. s->use_glob = 1;
  265. #else
  266. av_log(s1, AV_LOG_ERROR,
  267. "Pattern type 'glob' was selected but globbing "
  268. "is not supported by this libavformat build\n");
  269. return AVERROR(ENOSYS);
  270. #endif
  271. } else if (s->pattern_type != PT_GLOB_SEQUENCE) {
  272. av_log(s1, AV_LOG_ERROR,
  273. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  274. return AVERROR(EINVAL);
  275. }
  276. s->img_first = first_index;
  277. s->img_last = last_index;
  278. s->img_number = first_index;
  279. /* compute duration */
  280. st->start_time = 0;
  281. st->duration = last_index - first_index + 1;
  282. }
  283. if(s1->video_codec_id){
  284. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  285. st->codec->codec_id = s1->video_codec_id;
  286. }else if(s1->audio_codec_id){
  287. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  288. st->codec->codec_id = s1->audio_codec_id;
  289. }else{
  290. const char *str= strrchr(s->path, '.');
  291. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  292. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  293. st->codec->codec_id = ff_guess_image2_codec(s->path);
  294. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  295. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  296. }
  297. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != AV_PIX_FMT_NONE)
  298. st->codec->pix_fmt = pix_fmt;
  299. return 0;
  300. }
  301. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  302. {
  303. VideoDemuxData *s = s1->priv_data;
  304. char filename_bytes[1024];
  305. char *filename = filename_bytes;
  306. int i;
  307. int size[3]={0}, ret[3]={0};
  308. AVIOContext *f[3] = {NULL};
  309. AVCodecContext *codec= s1->streams[0]->codec;
  310. if (!s->is_pipe) {
  311. /* loop over input */
  312. if (s->loop && s->img_number > s->img_last) {
  313. s->img_number = s->img_first;
  314. }
  315. if (s->img_number > s->img_last)
  316. return AVERROR_EOF;
  317. if (s->use_glob) {
  318. #if HAVE_GLOB
  319. filename = s->globstate.gl_pathv[s->img_number];
  320. #endif
  321. } else {
  322. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  323. s->path, s->img_number)<0 && s->img_number > 1)
  324. return AVERROR(EIO);
  325. }
  326. for(i=0; i<3; i++){
  327. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  328. &s1->interrupt_callback, NULL) < 0) {
  329. if(i>=1)
  330. break;
  331. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  332. return AVERROR(EIO);
  333. }
  334. size[i]= avio_size(f[i]);
  335. if(!s->split_planes)
  336. break;
  337. filename[ strlen(filename) - 1 ]= 'U' + i;
  338. }
  339. if(codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  340. infer_size(&codec->width, &codec->height, size[0]);
  341. } else {
  342. f[0] = s1->pb;
  343. if (url_feof(f[0]))
  344. return AVERROR(EIO);
  345. if (s->frame_size > 0) {
  346. size[0] = s->frame_size;
  347. } else {
  348. size[0]= 4096;
  349. }
  350. }
  351. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  352. return AVERROR(ENOMEM);
  353. pkt->stream_index = 0;
  354. pkt->flags |= AV_PKT_FLAG_KEY;
  355. pkt->size= 0;
  356. for(i=0; i<3; i++){
  357. if(f[i]){
  358. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  359. if (!s->is_pipe)
  360. avio_close(f[i]);
  361. if(ret[i]>0)
  362. pkt->size += ret[i];
  363. }
  364. }
  365. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  366. av_free_packet(pkt);
  367. return AVERROR(EIO); /* signal EOF */
  368. } else {
  369. s->img_count++;
  370. s->img_number++;
  371. return 0;
  372. }
  373. }
  374. static int read_close(struct AVFormatContext* s1)
  375. {
  376. VideoDemuxData *s = s1->priv_data;
  377. #if HAVE_GLOB
  378. if (s->use_glob) {
  379. globfree(&s->globstate);
  380. }
  381. #endif
  382. return 0;
  383. }
  384. #define OFFSET(x) offsetof(VideoDemuxData, x)
  385. #define DEC AV_OPT_FLAG_DECODING_PARAM
  386. static const AVOption options[] = {
  387. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  388. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  389. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  390. { "glob_sequence","glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  391. { "glob", "glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  392. { "sequence", "glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  393. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  394. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  395. { "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 },
  396. { "video_size", "set video size", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  397. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, DEC },
  398. { NULL },
  399. };
  400. #if CONFIG_IMAGE2_DEMUXER
  401. static const AVClass img2_class = {
  402. .class_name = "image2 demuxer",
  403. .item_name = av_default_item_name,
  404. .option = options,
  405. .version = LIBAVUTIL_VERSION_INT,
  406. };
  407. AVInputFormat ff_image2_demuxer = {
  408. .name = "image2",
  409. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  410. .priv_data_size = sizeof(VideoDemuxData),
  411. .read_probe = read_probe,
  412. .read_header = read_header,
  413. .read_packet = read_packet,
  414. .read_close = read_close,
  415. .flags = AVFMT_NOFILE,
  416. .priv_class = &img2_class,
  417. };
  418. #endif
  419. #if CONFIG_IMAGE2PIPE_DEMUXER
  420. static const AVClass img2pipe_class = {
  421. .class_name = "image2pipe demuxer",
  422. .item_name = av_default_item_name,
  423. .option = options,
  424. .version = LIBAVUTIL_VERSION_INT,
  425. };
  426. AVInputFormat ff_image2pipe_demuxer = {
  427. .name = "image2pipe",
  428. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  429. .priv_data_size = sizeof(VideoDemuxData),
  430. .read_header = read_header,
  431. .read_packet = read_packet,
  432. .priv_class = &img2pipe_class,
  433. };
  434. #endif