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.

360 lines
8.2KB

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