img2dec.c 15 KB

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