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.

326 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 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 {
  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 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,
  168. FFMAX(s->start_number, 5)) < 0)
  169. return AVERROR(ENOENT);
  170. s->img_first = first_index;
  171. s->img_last = last_index;
  172. s->img_number = first_index;
  173. /* compute duration */
  174. st->start_time = 0;
  175. st->duration = last_index - first_index + 1;
  176. }
  177. if(s1->video_codec_id){
  178. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  179. st->codec->codec_id = s1->video_codec_id;
  180. }else if(s1->audio_codec_id){
  181. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  182. st->codec->codec_id = s1->audio_codec_id;
  183. }else{
  184. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  185. st->codec->codec_id = ff_guess_image2_codec(s->path);
  186. }
  187. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  188. st->codec->pix_fmt = pix_fmt;
  189. return 0;
  190. }
  191. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  192. {
  193. VideoDemuxData *s = s1->priv_data;
  194. char filename[1024];
  195. int i;
  196. int size[3]={0}, ret[3]={0};
  197. AVIOContext *f[3];
  198. AVCodecContext *codec= s1->streams[0]->codec;
  199. if (!s->is_pipe) {
  200. /* loop over input */
  201. if (s->loop && s->img_number > s->img_last) {
  202. s->img_number = s->img_first;
  203. }
  204. if (s->img_number > s->img_last)
  205. return AVERROR_EOF;
  206. if (av_get_frame_filename(filename, sizeof(filename),
  207. s->path, s->img_number)<0 && s->img_number > 1)
  208. return AVERROR(EIO);
  209. for(i=0; i<3; i++){
  210. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  211. &s1->interrupt_callback, NULL) < 0) {
  212. if(i==1)
  213. break;
  214. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  215. return AVERROR(EIO);
  216. }
  217. size[i]= avio_size(f[i]);
  218. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  219. break;
  220. filename[ strlen(filename) - 1 ]= 'U' + i;
  221. }
  222. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  223. infer_size(&codec->width, &codec->height, size[0]);
  224. } else {
  225. f[0] = s1->pb;
  226. if (f[0]->eof_reached)
  227. return AVERROR(EIO);
  228. size[0]= 4096;
  229. }
  230. av_new_packet(pkt, size[0] + size[1] + size[2]);
  231. pkt->stream_index = 0;
  232. pkt->flags |= AV_PKT_FLAG_KEY;
  233. pkt->size= 0;
  234. for(i=0; i<3; i++){
  235. if(size[i]){
  236. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  237. if (!s->is_pipe)
  238. avio_close(f[i]);
  239. if(ret[i]>0)
  240. pkt->size += ret[i];
  241. }
  242. }
  243. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  244. av_free_packet(pkt);
  245. return AVERROR(EIO); /* signal EOF */
  246. } else {
  247. s->img_count++;
  248. s->img_number++;
  249. return 0;
  250. }
  251. }
  252. #define OFFSET(x) offsetof(VideoDemuxData, x)
  253. #define DEC AV_OPT_FLAG_DECODING_PARAM
  254. static const AVOption options[] = {
  255. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  256. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  257. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  258. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  259. { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.dbl = 1}, 1, INT_MAX, DEC },
  260. { NULL },
  261. };
  262. #if CONFIG_IMAGE2_DEMUXER
  263. static const AVClass img2_class = {
  264. .class_name = "image2 demuxer",
  265. .item_name = av_default_item_name,
  266. .option = options,
  267. .version = LIBAVUTIL_VERSION_INT,
  268. };
  269. AVInputFormat ff_image2_demuxer = {
  270. .name = "image2",
  271. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  272. .priv_data_size = sizeof(VideoDemuxData),
  273. .read_probe = read_probe,
  274. .read_header = read_header,
  275. .read_packet = read_packet,
  276. .flags = AVFMT_NOFILE,
  277. .priv_class = &img2_class,
  278. };
  279. #endif
  280. #if CONFIG_IMAGE2PIPE_DEMUXER
  281. static const AVClass img2pipe_class = {
  282. .class_name = "image2pipe demuxer",
  283. .item_name = av_default_item_name,
  284. .option = options,
  285. .version = LIBAVUTIL_VERSION_INT,
  286. };
  287. AVInputFormat ff_image2pipe_demuxer = {
  288. .name = "image2pipe",
  289. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  290. .priv_data_size = sizeof(VideoDemuxData),
  291. .read_header = read_header,
  292. .read_packet = read_packet,
  293. .priv_class = &img2pipe_class,
  294. };
  295. #endif