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.

419 lines
12KB

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