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.

379 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. #if defined(CONFIG_OS2)
  32. # include <stdlib.h>
  33. # define usleep(t) _sleep2((t) / 1000)
  34. #endif
  35. typedef struct {
  36. int width;
  37. int height;
  38. int img_number;
  39. int img_size;
  40. AVImageFormat *img_fmt;
  41. int pix_fmt;
  42. int is_pipe;
  43. char path[1024];
  44. /* temporary usage */
  45. void *ptr;
  46. } VideoData;
  47. int emulate_frame_rate;
  48. static int image_probe(AVProbeData *p)
  49. {
  50. if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
  51. return AVPROBE_SCORE_MAX;
  52. else
  53. return 0;
  54. }
  55. static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
  56. {
  57. VideoData *s = opaque;
  58. s->width = info->width;
  59. s->height = info->height;
  60. s->pix_fmt = info->pix_fmt;
  61. /* stop image reading but no error */
  62. return 1;
  63. }
  64. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  65. {
  66. VideoData *s = s1->priv_data;
  67. int i, ret;
  68. char buf[1024];
  69. ByteIOContext pb1, *f = &pb1;
  70. AVStream *st;
  71. st = av_new_stream(s1, 0);
  72. if (!st) {
  73. av_free(s);
  74. return -ENOMEM;
  75. }
  76. if (ap && ap->image_format)
  77. s->img_fmt = ap->image_format;
  78. strcpy(s->path, s1->filename);
  79. s->img_number = 0;
  80. /* find format */
  81. if (s1->iformat->flags & AVFMT_NOFILE)
  82. s->is_pipe = 0;
  83. else
  84. s->is_pipe = 1;
  85. if (!s->is_pipe) {
  86. /* try to find the first image */
  87. for(i=0;i<5;i++) {
  88. if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
  89. goto fail;
  90. if (url_fopen(f, buf, URL_RDONLY) >= 0)
  91. break;
  92. s->img_number++;
  93. }
  94. if (i == 5)
  95. goto fail;
  96. } else {
  97. f = &s1->pb;
  98. }
  99. ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
  100. if (ret < 0)
  101. goto fail1;
  102. if (!s->is_pipe) {
  103. url_fclose(f);
  104. } else {
  105. url_fseek(f, 0, SEEK_SET);
  106. }
  107. st->codec.codec_type = CODEC_TYPE_VIDEO;
  108. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  109. st->codec.width = s->width;
  110. st->codec.height = s->height;
  111. st->codec.pix_fmt = s->pix_fmt;
  112. s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
  113. if (!ap || !ap->frame_rate)
  114. st->codec.frame_rate = 25 * FRAME_RATE_BASE;
  115. else
  116. st->codec.frame_rate = ap->frame_rate;
  117. return 0;
  118. fail1:
  119. if (!s->is_pipe)
  120. url_fclose(f);
  121. fail:
  122. av_free(s);
  123. return -EIO;
  124. }
  125. static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
  126. {
  127. VideoData *s = opaque;
  128. if (info->width != s->width ||
  129. info->height != s->height)
  130. return -1;
  131. avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
  132. return 0;
  133. }
  134. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  135. {
  136. VideoData *s = s1->priv_data;
  137. char filename[1024];
  138. int ret;
  139. ByteIOContext f1, *f;
  140. static INT64 first_frame;
  141. if (emulate_frame_rate) {
  142. if (!first_frame) {
  143. first_frame = av_gettime();
  144. } else {
  145. INT64 pts;
  146. INT64 nowus;
  147. nowus = av_gettime() - first_frame;
  148. pts = ((INT64)s->img_number * FRAME_RATE_BASE * 1000000) / (s1->streams[0]->codec.frame_rate);
  149. if (pts > nowus)
  150. usleep(pts - nowus);
  151. }
  152. }
  153. if (!s->is_pipe) {
  154. if (get_frame_filename(filename, sizeof(filename),
  155. s->path, s->img_number) < 0)
  156. return -EIO;
  157. f = &f1;
  158. if (url_fopen(f, filename, URL_RDONLY) < 0)
  159. return -EIO;
  160. } else {
  161. f = &s1->pb;
  162. if (url_feof(f))
  163. return -EIO;
  164. }
  165. av_new_packet(pkt, s->img_size);
  166. pkt->stream_index = 0;
  167. s->ptr = pkt->data;
  168. ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
  169. if (!s->is_pipe) {
  170. url_fclose(f);
  171. }
  172. if (ret < 0) {
  173. av_free_packet(pkt);
  174. return -EIO; /* signal EOF */
  175. } else {
  176. pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
  177. s->img_number++;
  178. return 0;
  179. }
  180. }
  181. static int img_read_close(AVFormatContext *s1)
  182. {
  183. return 0;
  184. }
  185. /******************************************************/
  186. /* image output */
  187. static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
  188. {
  189. VideoData *img = s->priv_data;
  190. AVStream *st;
  191. AVImageFormat *img_fmt;
  192. int i;
  193. /* find output image format */
  194. if (ap && ap->image_format) {
  195. img_fmt = ap->image_format;
  196. } else {
  197. img_fmt = guess_image_format(s->filename);
  198. }
  199. if (!img_fmt)
  200. return -1;
  201. if (s->nb_streams != 1)
  202. return -1;
  203. st = s->streams[0];
  204. /* we select the first matching format */
  205. for(i=0;i<PIX_FMT_NB;i++) {
  206. if (img_fmt->supported_pixel_formats & (1 << i))
  207. break;
  208. }
  209. if (i >= PIX_FMT_NB)
  210. return -1;
  211. img->img_fmt = img_fmt;
  212. img->pix_fmt = i;
  213. st->codec.pix_fmt = img->pix_fmt;
  214. return 0;
  215. }
  216. static int img_write_header(AVFormatContext *s)
  217. {
  218. VideoData *img = s->priv_data;
  219. img->img_number = 1;
  220. strcpy(img->path, s->filename);
  221. /* find format */
  222. if (s->oformat->flags & AVFMT_NOFILE)
  223. img->is_pipe = 0;
  224. else
  225. img->is_pipe = 1;
  226. return 0;
  227. }
  228. static int img_write_packet(AVFormatContext *s, int stream_index,
  229. UINT8 *buf, int size, int force_pts)
  230. {
  231. VideoData *img = s->priv_data;
  232. AVStream *st = s->streams[stream_index];
  233. ByteIOContext pb1, *pb;
  234. AVPicture *picture;
  235. int width, height, ret;
  236. char filename[1024];
  237. AVImageInfo info;
  238. width = st->codec.width;
  239. height = st->codec.height;
  240. picture = (AVPicture *)buf;
  241. if (!img->is_pipe) {
  242. if (get_frame_filename(filename, sizeof(filename),
  243. img->path, img->img_number) < 0)
  244. return -EIO;
  245. pb = &pb1;
  246. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  247. return -EIO;
  248. } else {
  249. pb = &s->pb;
  250. }
  251. info.width = width;
  252. info.height = height;
  253. info.pix_fmt = st->codec.pix_fmt;
  254. info.pict = *picture;
  255. ret = av_write_image(pb, img->img_fmt, &info);
  256. if (!img->is_pipe) {
  257. url_fclose(pb);
  258. }
  259. img->img_number++;
  260. return 0;
  261. }
  262. static int img_write_trailer(AVFormatContext *s)
  263. {
  264. return 0;
  265. }
  266. /* input */
  267. static AVInputFormat image_iformat = {
  268. "image",
  269. "image sequence",
  270. sizeof(VideoData),
  271. image_probe,
  272. img_read_header,
  273. img_read_packet,
  274. img_read_close,
  275. NULL,
  276. AVFMT_NOFILE | AVFMT_NEEDNUMBER,
  277. };
  278. static AVInputFormat imagepipe_iformat = {
  279. "imagepipe",
  280. "piped image sequence",
  281. sizeof(VideoData),
  282. NULL, /* no probe */
  283. img_read_header,
  284. img_read_packet,
  285. img_read_close,
  286. NULL,
  287. };
  288. /* output */
  289. static AVOutputFormat image_oformat = {
  290. "image",
  291. "image sequence",
  292. "",
  293. "",
  294. sizeof(VideoData),
  295. CODEC_ID_NONE,
  296. CODEC_ID_RAWVIDEO,
  297. img_write_header,
  298. img_write_packet,
  299. img_write_trailer,
  300. AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
  301. img_set_parameters,
  302. };
  303. static AVOutputFormat imagepipe_oformat = {
  304. "imagepipe",
  305. "piped image sequence",
  306. "",
  307. "",
  308. sizeof(VideoData),
  309. CODEC_ID_NONE,
  310. CODEC_ID_RAWVIDEO,
  311. img_write_header,
  312. img_write_packet,
  313. img_write_trailer,
  314. AVFMT_RAWPICTURE,
  315. img_set_parameters,
  316. };
  317. int img_init(void)
  318. {
  319. av_register_input_format(&image_iformat);
  320. av_register_output_format(&image_oformat);
  321. av_register_input_format(&imagepipe_iformat);
  322. av_register_output_format(&imagepipe_oformat);
  323. return 0;
  324. }