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.

336 lines
10KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. typedef struct VideoDemuxData {
  30. const AVClass *class; /**< Class for private options. */
  31. int img_first;
  32. int img_last;
  33. int img_number;
  34. int img_count;
  35. int is_pipe;
  36. char path[1024];
  37. char *pixel_format; /**< Set by a private option. */
  38. char *video_size; /**< Set by a private option. */
  39. char *framerate; /**< Set by a private option. */
  40. int loop;
  41. int start_number;
  42. } VideoDemuxData;
  43. static const int sizes[][2] = {
  44. { 640, 480 },
  45. { 720, 480 },
  46. { 720, 576 },
  47. { 352, 288 },
  48. { 352, 240 },
  49. { 160, 128 },
  50. { 512, 384 },
  51. { 640, 352 },
  52. { 640, 240 },
  53. };
  54. static int infer_size(int *width_ptr, int *height_ptr, int size)
  55. {
  56. int i;
  57. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  58. if ((sizes[i][0] * sizes[i][1]) == size) {
  59. *width_ptr = sizes[i][0];
  60. *height_ptr = sizes[i][1];
  61. return 0;
  62. }
  63. }
  64. return -1;
  65. }
  66. /* return -1 if no image found */
  67. static int find_image_range(int *pfirst_index, int *plast_index,
  68. const char *path, int max_start)
  69. {
  70. char buf[1024];
  71. int range, last_index, range1, first_index;
  72. /* find the first image */
  73. for (first_index = 0; first_index < max_start; first_index++) {
  74. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  75. *pfirst_index =
  76. *plast_index = 1;
  77. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  78. return 0;
  79. return -1;
  80. }
  81. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  82. break;
  83. }
  84. if (first_index == 5)
  85. goto fail;
  86. /* find the last image */
  87. last_index = first_index;
  88. for (;;) {
  89. range = 0;
  90. for (;;) {
  91. if (!range)
  92. range1 = 1;
  93. else
  94. range1 = 2 * range;
  95. if (av_get_frame_filename(buf, sizeof(buf), path,
  96. last_index + range1) < 0)
  97. goto fail;
  98. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  99. break;
  100. range = range1;
  101. /* just in case... */
  102. if (range >= (1 << 30))
  103. goto fail;
  104. }
  105. /* we are sure than image last_index + range exists */
  106. if (!range)
  107. break;
  108. last_index += range;
  109. }
  110. *pfirst_index = first_index;
  111. *plast_index = last_index;
  112. return 0;
  113. fail:
  114. return -1;
  115. }
  116. static int img_read_probe(AVProbeData *p)
  117. {
  118. if (p->filename && ff_guess_image2_codec(p->filename)) {
  119. if (av_filename_number_test(p->filename))
  120. return AVPROBE_SCORE_MAX;
  121. else
  122. return AVPROBE_SCORE_EXTENSION;
  123. }
  124. return 0;
  125. }
  126. static int img_read_header(AVFormatContext *s1)
  127. {
  128. VideoDemuxData *s = s1->priv_data;
  129. int first_index, last_index, ret = 0;
  130. int width = 0, height = 0;
  131. AVStream *st;
  132. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  133. AVRational framerate;
  134. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  135. st = avformat_new_stream(s1, NULL);
  136. if (!st) {
  137. return AVERROR(ENOMEM);
  138. }
  139. if (s->pixel_format &&
  140. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  141. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  142. s->pixel_format);
  143. return AVERROR(EINVAL);
  144. }
  145. if (s->video_size &&
  146. (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  147. av_log(s, AV_LOG_ERROR,
  148. "Could not parse video size: %s.\n", s->video_size);
  149. return ret;
  150. }
  151. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  152. av_log(s, AV_LOG_ERROR,
  153. "Could not parse framerate: %s.\n", s->framerate);
  154. return ret;
  155. }
  156. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  157. s->img_number = 0;
  158. s->img_count = 0;
  159. /* find format */
  160. if (s1->iformat->flags & AVFMT_NOFILE)
  161. s->is_pipe = 0;
  162. else {
  163. s->is_pipe = 1;
  164. st->need_parsing = AVSTREAM_PARSE_FULL;
  165. }
  166. avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
  167. if (width && height) {
  168. st->codecpar->width = width;
  169. st->codecpar->height = height;
  170. }
  171. if (!s->is_pipe) {
  172. if (find_image_range(&first_index, &last_index, s->path,
  173. FFMAX(s->start_number, 5)) < 0)
  174. return AVERROR(ENOENT);
  175. s->img_first = first_index;
  176. s->img_last = last_index;
  177. s->img_number = s->start_number != 1 ? s->start_number : first_index;
  178. /* compute duration */
  179. st->start_time = 0;
  180. st->duration = last_index - first_index + 1;
  181. }
  182. if (s1->video_codec_id) {
  183. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  184. st->codecpar->codec_id = s1->video_codec_id;
  185. } else if (s1->audio_codec_id) {
  186. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  187. st->codecpar->codec_id = s1->audio_codec_id;
  188. } else {
  189. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  190. st->codecpar->codec_id = ff_guess_image2_codec(s->path);
  191. }
  192. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  193. pix_fmt != AV_PIX_FMT_NONE)
  194. st->codecpar->format = pix_fmt;
  195. return 0;
  196. }
  197. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  198. {
  199. VideoDemuxData *s = s1->priv_data;
  200. char filename[1024];
  201. int i, res;
  202. int size[3] = { 0 }, ret[3] = { 0 };
  203. AVIOContext *f[3] = { NULL };
  204. AVCodecParameters *par = s1->streams[0]->codecpar;
  205. if (!s->is_pipe) {
  206. /* loop over input */
  207. if (s->loop && s->img_number > s->img_last) {
  208. s->img_number = s->img_first;
  209. }
  210. if (s->img_number > s->img_last)
  211. return AVERROR_EOF;
  212. if (av_get_frame_filename(filename, sizeof(filename),
  213. s->path,
  214. s->img_number) < 0 && s->img_number > 1)
  215. return AVERROR(EIO);
  216. for (i = 0; i < 3; i++) {
  217. if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
  218. if (i >= 1)
  219. break;
  220. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  221. filename);
  222. return AVERROR(EIO);
  223. }
  224. size[i] = avio_size(f[i]);
  225. if (par->codec_id != AV_CODEC_ID_RAWVIDEO)
  226. break;
  227. filename[strlen(filename) - 1] = 'U' + i;
  228. }
  229. if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
  230. infer_size(&par->width, &par->height, size[0]);
  231. } else {
  232. f[0] = s1->pb;
  233. if (f[0]->eof_reached)
  234. return AVERROR(EIO);
  235. size[0] = 4096;
  236. }
  237. res = av_new_packet(pkt, size[0] + size[1] + size[2]);
  238. if (res < 0)
  239. return res;
  240. pkt->stream_index = 0;
  241. pkt->flags |= AV_PKT_FLAG_KEY;
  242. pkt->size = 0;
  243. for (i = 0; i < 3; i++) {
  244. if (f[i]) {
  245. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  246. if (!s->is_pipe)
  247. ff_format_io_close(s1, &f[i]);
  248. if (ret[i] > 0)
  249. pkt->size += ret[i];
  250. }
  251. }
  252. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  253. av_packet_unref(pkt);
  254. return AVERROR(EIO); /* signal EOF */
  255. } else {
  256. s->img_count++;
  257. s->img_number++;
  258. return 0;
  259. }
  260. }
  261. #define OFFSET(x) offsetof(VideoDemuxData, x)
  262. #define DEC AV_OPT_FLAG_DECODING_PARAM
  263. static const AVOption options[] = {
  264. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
  265. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
  266. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, 0, 0, DEC },
  267. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
  268. { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, DEC },
  269. { NULL },
  270. };
  271. #if CONFIG_IMAGE2_DEMUXER
  272. static const AVClass img2_class = {
  273. .class_name = "image2 demuxer",
  274. .item_name = av_default_item_name,
  275. .option = options,
  276. .version = LIBAVUTIL_VERSION_INT,
  277. };
  278. AVInputFormat ff_image2_demuxer = {
  279. .name = "image2",
  280. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  281. .priv_data_size = sizeof(VideoDemuxData),
  282. .read_probe = img_read_probe,
  283. .read_header = img_read_header,
  284. .read_packet = img_read_packet,
  285. .flags = AVFMT_NOFILE,
  286. .priv_class = &img2_class,
  287. };
  288. #endif
  289. #if CONFIG_IMAGE2PIPE_DEMUXER
  290. static const AVClass img2pipe_class = {
  291. .class_name = "image2pipe demuxer",
  292. .item_name = av_default_item_name,
  293. .option = options,
  294. .version = LIBAVUTIL_VERSION_INT,
  295. };
  296. AVInputFormat ff_image2pipe_demuxer = {
  297. .name = "image2pipe",
  298. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  299. .priv_data_size = sizeof(VideoDemuxData),
  300. .read_header = img_read_header,
  301. .read_packet = img_read_packet,
  302. .priv_class = &img2pipe_class,
  303. };
  304. #endif