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.

401 lines
9.7KB

  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. typedef struct {
  23. int width;
  24. int height;
  25. int img_first;
  26. int img_last;
  27. int img_number;
  28. int img_count;
  29. int img_size;
  30. AVImageFormat *img_fmt;
  31. int pix_fmt;
  32. int is_pipe;
  33. char path[1024];
  34. /* temporary usage */
  35. void *ptr;
  36. } VideoData;
  37. /* return -1 if no image found */
  38. static int find_image_range(int *pfirst_index, int *plast_index,
  39. const char *path)
  40. {
  41. char buf[1024];
  42. int range, last_index, range1, first_index;
  43. /* find the first image */
  44. for(first_index = 0; first_index < 5; first_index++) {
  45. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
  46. goto fail;
  47. if (url_exist(buf))
  48. break;
  49. }
  50. if (first_index == 5)
  51. goto fail;
  52. /* find the last image */
  53. last_index = first_index;
  54. for(;;) {
  55. range = 0;
  56. for(;;) {
  57. if (!range)
  58. range1 = 1;
  59. else
  60. range1 = 2 * range;
  61. if (av_get_frame_filename(buf, sizeof(buf), path,
  62. last_index + range1) < 0)
  63. goto fail;
  64. if (!url_exist(buf))
  65. break;
  66. range = range1;
  67. /* just in case... */
  68. if (range >= (1 << 30))
  69. goto fail;
  70. }
  71. /* we are sure than image last_index + range exists */
  72. if (!range)
  73. break;
  74. last_index += range;
  75. }
  76. *pfirst_index = first_index;
  77. *plast_index = last_index;
  78. return 0;
  79. fail:
  80. return -1;
  81. }
  82. static int image_probe(AVProbeData *p)
  83. {
  84. if (av_filename_number_test(p->filename) && guess_image_format(p->filename))
  85. return AVPROBE_SCORE_MAX-1;
  86. else
  87. return 0;
  88. }
  89. static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
  90. {
  91. VideoData *s = opaque;
  92. s->width = info->width;
  93. s->height = info->height;
  94. s->pix_fmt = info->pix_fmt;
  95. /* stop image reading but no error */
  96. return 1;
  97. }
  98. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  99. {
  100. VideoData *s = s1->priv_data;
  101. int ret, first_index, last_index;
  102. char buf[1024];
  103. ByteIOContext pb1, *f = &pb1;
  104. AVStream *st;
  105. st = av_new_stream(s1, 0);
  106. if (!st) {
  107. return -ENOMEM;
  108. }
  109. if (ap->image_format)
  110. s->img_fmt = ap->image_format;
  111. pstrcpy(s->path, sizeof(s->path), s1->filename);
  112. s->img_number = 0;
  113. s->img_count = 0;
  114. /* find format */
  115. if (s1->iformat->flags & AVFMT_NOFILE)
  116. s->is_pipe = 0;
  117. else
  118. s->is_pipe = 1;
  119. if (!ap->time_base.num) {
  120. st->codec->time_base= (AVRational){1,25};
  121. } else {
  122. st->codec->time_base= ap->time_base;
  123. }
  124. if (!s->is_pipe) {
  125. if (find_image_range(&first_index, &last_index, s->path) < 0)
  126. goto fail;
  127. s->img_first = first_index;
  128. s->img_last = last_index;
  129. s->img_number = first_index;
  130. /* compute duration */
  131. st->start_time = 0;
  132. st->duration = last_index - first_index + 1;
  133. if (av_get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
  134. goto fail;
  135. if (url_fopen(f, buf, URL_RDONLY) < 0)
  136. goto fail;
  137. } else {
  138. f = &s1->pb;
  139. }
  140. ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
  141. if (ret < 0)
  142. goto fail1;
  143. if (!s->is_pipe) {
  144. url_fclose(f);
  145. } else {
  146. url_fseek(f, 0, SEEK_SET);
  147. }
  148. st->codec->codec_type = CODEC_TYPE_VIDEO;
  149. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  150. st->codec->width = s->width;
  151. st->codec->height = s->height;
  152. st->codec->pix_fmt = s->pix_fmt;
  153. s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
  154. return 0;
  155. fail1:
  156. if (!s->is_pipe)
  157. url_fclose(f);
  158. fail:
  159. return AVERROR_IO;
  160. }
  161. static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
  162. {
  163. VideoData *s = opaque;
  164. if (info->width != s->width ||
  165. info->height != s->height)
  166. return -1;
  167. avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
  168. return 0;
  169. }
  170. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  171. {
  172. VideoData *s = s1->priv_data;
  173. char filename[1024];
  174. int ret;
  175. ByteIOContext f1, *f;
  176. if (!s->is_pipe) {
  177. /* loop over input */
  178. if (s1->loop_input && s->img_number > s->img_last) {
  179. s->img_number = s->img_first;
  180. }
  181. if (av_get_frame_filename(filename, sizeof(filename),
  182. s->path, s->img_number) < 0)
  183. return AVERROR_IO;
  184. f = &f1;
  185. if (url_fopen(f, filename, URL_RDONLY) < 0)
  186. return AVERROR_IO;
  187. } else {
  188. f = &s1->pb;
  189. if (url_feof(f))
  190. return AVERROR_IO;
  191. }
  192. av_new_packet(pkt, s->img_size);
  193. pkt->stream_index = 0;
  194. s->ptr = pkt->data;
  195. ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
  196. if (!s->is_pipe) {
  197. url_fclose(f);
  198. }
  199. if (ret < 0) {
  200. av_free_packet(pkt);
  201. return AVERROR_IO; /* signal EOF */
  202. } else {
  203. /* XXX: computing this pts is not necessary as it is done in
  204. the generic code too */
  205. pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec->time_base.num, s1->streams[0]->time_base.den, s1->streams[0]->codec->time_base.den) / s1->streams[0]->time_base.num;
  206. s->img_count++;
  207. s->img_number++;
  208. return 0;
  209. }
  210. }
  211. static int img_read_close(AVFormatContext *s1)
  212. {
  213. return 0;
  214. }
  215. /******************************************************/
  216. /* image output */
  217. static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
  218. {
  219. VideoData *img = s->priv_data;
  220. AVStream *st;
  221. AVImageFormat *img_fmt;
  222. int i;
  223. /* find output image format */
  224. if (ap->image_format) {
  225. img_fmt = ap->image_format;
  226. } else {
  227. img_fmt = guess_image_format(s->filename);
  228. }
  229. if (!img_fmt)
  230. return -1;
  231. if (s->nb_streams != 1)
  232. return -1;
  233. st = s->streams[0];
  234. /* we select the first matching format */
  235. for(i=0;i<PIX_FMT_NB;i++) {
  236. if (img_fmt->supported_pixel_formats & (1 << i))
  237. break;
  238. }
  239. if (i >= PIX_FMT_NB)
  240. return -1;
  241. img->img_fmt = img_fmt;
  242. img->pix_fmt = i;
  243. st->codec->pix_fmt = img->pix_fmt;
  244. return 0;
  245. }
  246. static int img_write_header(AVFormatContext *s)
  247. {
  248. VideoData *img = s->priv_data;
  249. img->img_number = 1;
  250. pstrcpy(img->path, sizeof(img->path), s->filename);
  251. /* find format */
  252. if (s->oformat->flags & AVFMT_NOFILE)
  253. img->is_pipe = 0;
  254. else
  255. img->is_pipe = 1;
  256. return 0;
  257. }
  258. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  259. {
  260. VideoData *img = s->priv_data;
  261. AVStream *st = s->streams[pkt->stream_index];
  262. ByteIOContext pb1, *pb;
  263. AVPicture *picture;
  264. int width, height, ret;
  265. char filename[1024];
  266. AVImageInfo info;
  267. width = st->codec->width;
  268. height = st->codec->height;
  269. picture = (AVPicture *)pkt->data;
  270. if (!img->is_pipe) {
  271. if (av_get_frame_filename(filename, sizeof(filename),
  272. img->path, img->img_number) < 0)
  273. return AVERROR_IO;
  274. pb = &pb1;
  275. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  276. return AVERROR_IO;
  277. } else {
  278. pb = &s->pb;
  279. }
  280. info.width = width;
  281. info.height = height;
  282. info.pix_fmt = st->codec->pix_fmt;
  283. info.interleaved = 0; /* FIXME: there should be a way to set it right */
  284. info.pict = *picture;
  285. ret = av_write_image(pb, img->img_fmt, &info);
  286. if (!img->is_pipe) {
  287. url_fclose(pb);
  288. }
  289. img->img_number++;
  290. return 0;
  291. }
  292. static int img_write_trailer(AVFormatContext *s)
  293. {
  294. return 0;
  295. }
  296. /* input */
  297. #ifdef CONFIG_IMAGE_DEMUXER
  298. AVInputFormat image_demuxer = {
  299. "image",
  300. "image sequence",
  301. sizeof(VideoData),
  302. image_probe,
  303. img_read_header,
  304. img_read_packet,
  305. img_read_close,
  306. NULL,
  307. NULL,
  308. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  309. };
  310. #endif
  311. #ifdef CONFIG_IMAGEPIPE_DEMUXER
  312. AVInputFormat imagepipe_demuxer = {
  313. "imagepipe",
  314. "piped image sequence",
  315. sizeof(VideoData),
  316. NULL, /* no probe */
  317. img_read_header,
  318. img_read_packet,
  319. img_read_close,
  320. NULL,
  321. };
  322. #endif
  323. /* output */
  324. #ifdef CONFIG_IMAGE_MUXER
  325. AVOutputFormat image_muxer = {
  326. "image",
  327. "image sequence",
  328. "",
  329. "",
  330. sizeof(VideoData),
  331. CODEC_ID_NONE,
  332. CODEC_ID_RAWVIDEO,
  333. img_write_header,
  334. img_write_packet,
  335. img_write_trailer,
  336. AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
  337. img_set_parameters,
  338. };
  339. #endif
  340. #ifdef CONFIG_IMAGEPIPE_MUXER
  341. AVOutputFormat imagepipe_muxer = {
  342. "imagepipe",
  343. "piped image sequence",
  344. "",
  345. "",
  346. sizeof(VideoData),
  347. CODEC_ID_NONE,
  348. CODEC_ID_RAWVIDEO,
  349. img_write_header,
  350. img_write_packet,
  351. img_write_trailer,
  352. AVFMT_RAWPICTURE,
  353. img_set_parameters,
  354. };
  355. #endif