img2dec.c 9.5 KB

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