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.

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