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.

328 lines
9.7KB

  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. typedef struct {
  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. int split_planes; /**< use independent file for each Y, U, V plane */
  37. char path[1024];
  38. char *pixel_format; /**< Set by a private option. */
  39. char *video_size; /**< Set by a private option. */
  40. char *framerate; /**< Set by a private option. */
  41. int loop;
  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)
  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 < 5; 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 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_MAX/2;
  123. }
  124. return 0;
  125. }
  126. static int 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 PixelFormat pix_fmt = 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 && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
  140. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  141. return AVERROR(EINVAL);
  142. }
  143. if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  144. av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
  145. return ret;
  146. }
  147. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  148. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
  149. return ret;
  150. }
  151. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  152. s->img_number = 0;
  153. s->img_count = 0;
  154. /* find format */
  155. if (s1->iformat->flags & AVFMT_NOFILE)
  156. s->is_pipe = 0;
  157. else{
  158. s->is_pipe = 1;
  159. st->need_parsing = AVSTREAM_PARSE_FULL;
  160. }
  161. avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
  162. if (width && height) {
  163. st->codec->width = width;
  164. st->codec->height = height;
  165. }
  166. if (!s->is_pipe) {
  167. if (find_image_range(&first_index, &last_index, s->path) < 0)
  168. return AVERROR(ENOENT);
  169. s->img_first = first_index;
  170. s->img_last = last_index;
  171. s->img_number = first_index;
  172. /* compute duration */
  173. st->start_time = 0;
  174. st->duration = last_index - first_index + 1;
  175. }
  176. if(s1->video_codec_id){
  177. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  178. st->codec->codec_id = s1->video_codec_id;
  179. }else if(s1->audio_codec_id){
  180. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  181. st->codec->codec_id = s1->audio_codec_id;
  182. }else{
  183. const char *str= strrchr(s->path, '.');
  184. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  185. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  186. st->codec->codec_id = ff_guess_image2_codec(s->path);
  187. if (st->codec->codec_id == CODEC_ID_LJPEG)
  188. st->codec->codec_id = CODEC_ID_MJPEG;
  189. }
  190. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  191. st->codec->pix_fmt = pix_fmt;
  192. return 0;
  193. }
  194. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  195. {
  196. VideoDemuxData *s = s1->priv_data;
  197. char filename[1024];
  198. int i;
  199. int size[3]={0}, ret[3]={0};
  200. AVIOContext *f[3];
  201. AVCodecContext *codec= s1->streams[0]->codec;
  202. if (!s->is_pipe) {
  203. /* loop over input */
  204. if (s->loop && s->img_number > s->img_last) {
  205. s->img_number = s->img_first;
  206. }
  207. if (s->img_number > s->img_last)
  208. return AVERROR_EOF;
  209. if (av_get_frame_filename(filename, sizeof(filename),
  210. s->path, s->img_number)<0 && s->img_number > 1)
  211. return AVERROR(EIO);
  212. for(i=0; i<3; i++){
  213. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  214. &s1->interrupt_callback, NULL) < 0) {
  215. if(i==1)
  216. break;
  217. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  218. return AVERROR(EIO);
  219. }
  220. size[i]= avio_size(f[i]);
  221. if(!s->split_planes)
  222. break;
  223. filename[ strlen(filename) - 1 ]= 'U' + i;
  224. }
  225. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  226. infer_size(&codec->width, &codec->height, size[0]);
  227. } else {
  228. f[0] = s1->pb;
  229. if (url_feof(f[0]))
  230. return AVERROR(EIO);
  231. size[0]= 4096;
  232. }
  233. av_new_packet(pkt, size[0] + size[1] + size[2]);
  234. pkt->stream_index = 0;
  235. pkt->flags |= AV_PKT_FLAG_KEY;
  236. pkt->size= 0;
  237. for(i=0; i<3; i++){
  238. if(size[i]){
  239. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  240. if (!s->is_pipe)
  241. avio_close(f[i]);
  242. if(ret[i]>0)
  243. pkt->size += ret[i];
  244. }
  245. }
  246. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  247. av_free_packet(pkt);
  248. return AVERROR(EIO); /* signal EOF */
  249. } else {
  250. s->img_count++;
  251. s->img_number++;
  252. return 0;
  253. }
  254. }
  255. #define OFFSET(x) offsetof(VideoDemuxData, x)
  256. #define DEC AV_OPT_FLAG_DECODING_PARAM
  257. static const AVOption options[] = {
  258. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  259. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  260. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  261. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  262. { NULL },
  263. };
  264. #if CONFIG_IMAGE2_DEMUXER
  265. static const AVClass img2_class = {
  266. .class_name = "image2 demuxer",
  267. .item_name = av_default_item_name,
  268. .option = options,
  269. .version = LIBAVUTIL_VERSION_INT,
  270. };
  271. AVInputFormat ff_image2_demuxer = {
  272. .name = "image2",
  273. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  274. .priv_data_size = sizeof(VideoDemuxData),
  275. .read_probe = read_probe,
  276. .read_header = read_header,
  277. .read_packet = read_packet,
  278. .flags = AVFMT_NOFILE,
  279. .priv_class = &img2_class,
  280. };
  281. #endif
  282. #if CONFIG_IMAGE2PIPE_DEMUXER
  283. static const AVClass img2pipe_class = {
  284. .class_name = "image2pipe demuxer",
  285. .item_name = av_default_item_name,
  286. .option = options,
  287. .version = LIBAVUTIL_VERSION_INT,
  288. };
  289. AVInputFormat ff_image2pipe_demuxer = {
  290. .name = "image2pipe",
  291. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  292. .priv_data_size = sizeof(VideoDemuxData),
  293. .read_header = read_header,
  294. .read_packet = read_packet,
  295. .priv_class = &img2pipe_class,
  296. };
  297. #endif