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.

375 lines
8.6KB

  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <unistd.h>
  20. #include "avformat.h"
  21. #ifdef __MINGW32__
  22. # include <windows.h>
  23. # define usleep(t) Sleep((t) / 1000)
  24. #endif
  25. #ifdef __BEOS__
  26. # ifndef usleep
  27. # include <OS.h>
  28. # define usleep(t) snooze((bigtime_t)(t))
  29. # endif
  30. #endif
  31. typedef struct {
  32. int width;
  33. int height;
  34. int img_number;
  35. int img_size;
  36. AVImageFormat *img_fmt;
  37. int pix_fmt;
  38. int is_pipe;
  39. char path[1024];
  40. /* temporary usage */
  41. void *ptr;
  42. } VideoData;
  43. int emulate_frame_rate;
  44. static int image_probe(AVProbeData *p)
  45. {
  46. if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
  47. return AVPROBE_SCORE_MAX;
  48. else
  49. return 0;
  50. }
  51. static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
  52. {
  53. VideoData *s = opaque;
  54. s->width = info->width;
  55. s->height = info->height;
  56. s->pix_fmt = info->pix_fmt;
  57. /* stop image reading but no error */
  58. return 1;
  59. }
  60. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  61. {
  62. VideoData *s = s1->priv_data;
  63. int i, ret;
  64. char buf[1024];
  65. ByteIOContext pb1, *f = &pb1;
  66. AVStream *st;
  67. st = av_new_stream(s1, 0);
  68. if (!st) {
  69. av_free(s);
  70. return -ENOMEM;
  71. }
  72. if (ap && ap->image_format)
  73. s->img_fmt = ap->image_format;
  74. strcpy(s->path, s1->filename);
  75. s->img_number = 0;
  76. /* find format */
  77. if (s1->iformat->flags & AVFMT_NOFILE)
  78. s->is_pipe = 0;
  79. else
  80. s->is_pipe = 1;
  81. if (!s->is_pipe) {
  82. /* try to find the first image */
  83. for(i=0;i<5;i++) {
  84. if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
  85. goto fail;
  86. if (url_fopen(f, buf, URL_RDONLY) >= 0)
  87. break;
  88. s->img_number++;
  89. }
  90. if (i == 5)
  91. goto fail;
  92. } else {
  93. f = &s1->pb;
  94. }
  95. ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
  96. if (ret < 0)
  97. goto fail1;
  98. if (!s->is_pipe) {
  99. url_fclose(f);
  100. } else {
  101. url_fseek(f, 0, SEEK_SET);
  102. }
  103. st->codec.codec_type = CODEC_TYPE_VIDEO;
  104. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  105. st->codec.width = s->width;
  106. st->codec.height = s->height;
  107. st->codec.pix_fmt = s->pix_fmt;
  108. s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
  109. if (!ap || !ap->frame_rate)
  110. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  111. else
  112. st->codec.frame_rate = ap->frame_rate;
  113. return 0;
  114. fail1:
  115. if (!s->is_pipe)
  116. url_fclose(f);
  117. fail:
  118. av_free(s);
  119. return -EIO;
  120. }
  121. static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
  122. {
  123. VideoData *s = opaque;
  124. if (info->width != s->width ||
  125. info->height != s->height)
  126. return -1;
  127. avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
  128. return 0;
  129. }
  130. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  131. {
  132. VideoData *s = s1->priv_data;
  133. char filename[1024];
  134. int ret;
  135. ByteIOContext f1, *f;
  136. static INT64 first_frame;
  137. if (emulate_frame_rate) {
  138. if (!first_frame) {
  139. first_frame = av_gettime();
  140. } else {
  141. INT64 pts;
  142. INT64 nowus;
  143. nowus = av_gettime() - first_frame;
  144. pts = ((INT64)s->img_number * FRAME_RATE_BASE * 1000000) / (s1->streams[0]->codec.frame_rate);
  145. if (pts > nowus)
  146. usleep(pts - nowus);
  147. }
  148. }
  149. if (!s->is_pipe) {
  150. if (get_frame_filename(filename, sizeof(filename),
  151. s->path, s->img_number) < 0)
  152. return -EIO;
  153. f = &f1;
  154. if (url_fopen(f, filename, URL_RDONLY) < 0)
  155. return -EIO;
  156. } else {
  157. f = &s1->pb;
  158. if (url_feof(f))
  159. return -EIO;
  160. }
  161. av_new_packet(pkt, s->img_size);
  162. pkt->stream_index = 0;
  163. s->ptr = pkt->data;
  164. ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
  165. if (!s->is_pipe) {
  166. url_fclose(f);
  167. }
  168. if (ret < 0) {
  169. av_free_packet(pkt);
  170. return -EIO; /* signal EOF */
  171. } else {
  172. pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
  173. s->img_number++;
  174. return 0;
  175. }
  176. }
  177. static int img_read_close(AVFormatContext *s1)
  178. {
  179. return 0;
  180. }
  181. /******************************************************/
  182. /* image output */
  183. static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
  184. {
  185. VideoData *img = s->priv_data;
  186. AVStream *st;
  187. AVImageFormat *img_fmt;
  188. int i;
  189. /* find output image format */
  190. if (ap && ap->image_format) {
  191. img_fmt = ap->image_format;
  192. } else {
  193. img_fmt = guess_image_format(s->filename);
  194. }
  195. if (!img_fmt)
  196. return -1;
  197. if (s->nb_streams != 1)
  198. return -1;
  199. st = s->streams[0];
  200. /* we select the first matching format */
  201. for(i=0;i<PIX_FMT_NB;i++) {
  202. if (img_fmt->supported_pixel_formats & (1 << i))
  203. break;
  204. }
  205. if (i >= PIX_FMT_NB)
  206. return -1;
  207. img->img_fmt = img_fmt;
  208. img->pix_fmt = i;
  209. st->codec.pix_fmt = img->pix_fmt;
  210. return 0;
  211. }
  212. static int img_write_header(AVFormatContext *s)
  213. {
  214. VideoData *img = s->priv_data;
  215. img->img_number = 1;
  216. strcpy(img->path, s->filename);
  217. /* find format */
  218. if (s->oformat->flags & AVFMT_NOFILE)
  219. img->is_pipe = 0;
  220. else
  221. img->is_pipe = 1;
  222. return 0;
  223. }
  224. static int img_write_packet(AVFormatContext *s, int stream_index,
  225. UINT8 *buf, int size, int force_pts)
  226. {
  227. VideoData *img = s->priv_data;
  228. AVStream *st = s->streams[stream_index];
  229. ByteIOContext pb1, *pb;
  230. AVPicture *picture;
  231. int width, height, ret;
  232. char filename[1024];
  233. AVImageInfo info;
  234. width = st->codec.width;
  235. height = st->codec.height;
  236. picture = (AVPicture *)buf;
  237. if (!img->is_pipe) {
  238. if (get_frame_filename(filename, sizeof(filename),
  239. img->path, img->img_number) < 0)
  240. return -EIO;
  241. pb = &pb1;
  242. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  243. return -EIO;
  244. } else {
  245. pb = &s->pb;
  246. }
  247. info.width = width;
  248. info.height = height;
  249. info.pix_fmt = st->codec.pix_fmt;
  250. info.pict = *picture;
  251. ret = av_write_image(pb, img->img_fmt, &info);
  252. if (!img->is_pipe) {
  253. url_fclose(pb);
  254. }
  255. img->img_number++;
  256. return 0;
  257. }
  258. static int img_write_trailer(AVFormatContext *s)
  259. {
  260. return 0;
  261. }
  262. /* input */
  263. static AVInputFormat image_iformat = {
  264. "image",
  265. "image sequence",
  266. sizeof(VideoData),
  267. image_probe,
  268. img_read_header,
  269. img_read_packet,
  270. img_read_close,
  271. NULL,
  272. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  273. };
  274. static AVInputFormat imagepipe_iformat = {
  275. "imagepipe",
  276. "piped image sequence",
  277. sizeof(VideoData),
  278. NULL, /* no probe */
  279. img_read_header,
  280. img_read_packet,
  281. img_read_close,
  282. NULL,
  283. };
  284. /* output */
  285. static AVOutputFormat image_oformat = {
  286. "image",
  287. "image sequence",
  288. "",
  289. "",
  290. sizeof(VideoData),
  291. CODEC_ID_NONE,
  292. CODEC_ID_RAWVIDEO,
  293. img_write_header,
  294. img_write_packet,
  295. img_write_trailer,
  296. AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
  297. img_set_parameters,
  298. };
  299. static AVOutputFormat imagepipe_oformat = {
  300. "imagepipe",
  301. "piped image sequence",
  302. "",
  303. "",
  304. sizeof(VideoData),
  305. CODEC_ID_NONE,
  306. CODEC_ID_RAWVIDEO,
  307. img_write_header,
  308. img_write_packet,
  309. img_write_trailer,
  310. AVFMT_RAWPICTURE,
  311. img_set_parameters,
  312. };
  313. int img_init(void)
  314. {
  315. av_register_input_format(&image_iformat);
  316. av_register_output_format(&image_oformat);
  317. av_register_input_format(&imagepipe_iformat);
  318. av_register_output_format(&imagepipe_oformat);
  319. return 0;
  320. }