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.

433 lines
13KB

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