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.

418 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, s->start_number - 1) < 0)
  231. return AVERROR(ENOENT);
  232. }
  233. s->img_first = first_index;
  234. s->img_last = last_index;
  235. s->img_number = first_index;
  236. /* compute duration */
  237. st->start_time = 0;
  238. st->duration = last_index - first_index + 1;
  239. }
  240. if(s1->video_codec_id){
  241. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  242. st->codec->codec_id = s1->video_codec_id;
  243. }else if(s1->audio_codec_id){
  244. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  245. st->codec->codec_id = s1->audio_codec_id;
  246. }else{
  247. const char *str= strrchr(s->path, '.');
  248. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  249. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  250. st->codec->codec_id = ff_guess_image2_codec(s->path);
  251. if (st->codec->codec_id == CODEC_ID_LJPEG)
  252. st->codec->codec_id = CODEC_ID_MJPEG;
  253. }
  254. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  255. st->codec->pix_fmt = pix_fmt;
  256. return 0;
  257. }
  258. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  259. {
  260. VideoDemuxData *s = s1->priv_data;
  261. char filename_bytes[1024];
  262. char *filename = filename_bytes;
  263. int i;
  264. int size[3]={0}, ret[3]={0};
  265. AVIOContext *f[3];
  266. AVCodecContext *codec= s1->streams[0]->codec;
  267. if (!s->is_pipe) {
  268. /* loop over input */
  269. if (s->loop && s->img_number > s->img_last) {
  270. s->img_number = s->img_first;
  271. }
  272. if (s->img_number > s->img_last)
  273. return AVERROR_EOF;
  274. if (s->use_glob) {
  275. #if HAVE_GLOB
  276. filename = s->globstate.gl_pathv[s->img_number];
  277. #endif
  278. } else {
  279. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  280. s->path, s->img_number)<0 && s->img_number > 1)
  281. return AVERROR(EIO);
  282. }
  283. for(i=0; i<3; i++){
  284. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  285. &s1->interrupt_callback, NULL) < 0) {
  286. if(i==1)
  287. break;
  288. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  289. return AVERROR(EIO);
  290. }
  291. size[i]= avio_size(f[i]);
  292. if(!s->split_planes)
  293. break;
  294. filename[ strlen(filename) - 1 ]= 'U' + i;
  295. }
  296. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  297. infer_size(&codec->width, &codec->height, size[0]);
  298. } else {
  299. f[0] = s1->pb;
  300. if (url_feof(f[0]))
  301. return AVERROR(EIO);
  302. size[0]= 4096;
  303. }
  304. av_new_packet(pkt, size[0] + size[1] + size[2]);
  305. pkt->stream_index = 0;
  306. pkt->flags |= AV_PKT_FLAG_KEY;
  307. pkt->size= 0;
  308. for(i=0; i<3; i++){
  309. if(size[i]){
  310. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  311. if (!s->is_pipe)
  312. avio_close(f[i]);
  313. if(ret[i]>0)
  314. pkt->size += ret[i];
  315. }
  316. }
  317. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  318. av_free_packet(pkt);
  319. return AVERROR(EIO); /* signal EOF */
  320. } else {
  321. s->img_count++;
  322. s->img_number++;
  323. return 0;
  324. }
  325. }
  326. static int read_close(struct AVFormatContext* s1)
  327. {
  328. VideoDemuxData *s = s1->priv_data;
  329. #if HAVE_GLOB
  330. if (s->use_glob) {
  331. globfree(&s->globstate);
  332. }
  333. #endif
  334. return 0;
  335. }
  336. #define OFFSET(x) offsetof(VideoDemuxData, x)
  337. #define DEC AV_OPT_FLAG_DECODING_PARAM
  338. static const AVOption options[] = {
  339. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  340. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  341. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  342. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  343. { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.dbl = 1}, 1, INT_MAX, DEC },
  344. { NULL },
  345. };
  346. #if CONFIG_IMAGE2_DEMUXER
  347. static const AVClass img2_class = {
  348. .class_name = "image2 demuxer",
  349. .item_name = av_default_item_name,
  350. .option = options,
  351. .version = LIBAVUTIL_VERSION_INT,
  352. };
  353. AVInputFormat ff_image2_demuxer = {
  354. .name = "image2",
  355. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  356. .priv_data_size = sizeof(VideoDemuxData),
  357. .read_probe = read_probe,
  358. .read_header = read_header,
  359. .read_packet = read_packet,
  360. .read_close = read_close,
  361. .flags = AVFMT_NOFILE,
  362. .priv_class = &img2_class,
  363. };
  364. #endif
  365. #if CONFIG_IMAGE2PIPE_DEMUXER
  366. static const AVClass img2pipe_class = {
  367. .class_name = "image2pipe demuxer",
  368. .item_name = av_default_item_name,
  369. .option = options,
  370. .version = LIBAVUTIL_VERSION_INT,
  371. };
  372. AVInputFormat ff_image2pipe_demuxer = {
  373. .name = "image2pipe",
  374. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  375. .priv_data_size = sizeof(VideoDemuxData),
  376. .read_header = read_header,
  377. .read_packet = read_packet,
  378. .priv_class = &img2pipe_class,
  379. };
  380. #endif