3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
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.
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.
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
24 # define usleep(t) Sleep((t) / 1000)
29 # define usleep(t) snooze((bigtime_t)(t))
32 #if defined(CONFIG_OS2)
34 # define usleep(t) _sleep2((t) / 1000)
42 AVImageFormat *img_fmt;
50 int emulate_frame_rate;
52 static int image_probe(AVProbeData *p)
54 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
55 return AVPROBE_SCORE_MAX;
60 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
62 VideoData *s = opaque;
64 s->width = info->width;
65 s->height = info->height;
66 s->pix_fmt = info->pix_fmt;
67 /* stop image reading but no error */
71 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
73 VideoData *s = s1->priv_data;
76 ByteIOContext pb1, *f = &pb1;
79 st = av_new_stream(s1, 0);
85 if (ap && ap->image_format)
86 s->img_fmt = ap->image_format;
88 strcpy(s->path, s1->filename);
92 if (s1->iformat->flags & AVFMT_NOFILE)
98 /* try to find the first image */
100 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
102 if (url_fopen(f, buf, URL_RDONLY) >= 0)
112 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
119 url_fseek(f, 0, SEEK_SET);
122 st->codec.codec_type = CODEC_TYPE_VIDEO;
123 st->codec.codec_id = CODEC_ID_RAWVIDEO;
124 st->codec.width = s->width;
125 st->codec.height = s->height;
126 st->codec.pix_fmt = s->pix_fmt;
127 s->img_size = avpicture_get_size(s->pix_fmt, s->width, s->height);
129 if (!ap || !ap->frame_rate)
130 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
132 st->codec.frame_rate = ap->frame_rate;
143 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
145 VideoData *s = opaque;
147 if (info->width != s->width ||
148 info->height != s->height)
150 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
154 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
156 VideoData *s = s1->priv_data;
159 ByteIOContext f1, *f;
160 static INT64 first_frame;
162 if (emulate_frame_rate) {
164 first_frame = av_gettime();
169 nowus = av_gettime() - first_frame;
171 pts = ((INT64)s->img_number * FRAME_RATE_BASE * 1000000) / (s1->streams[0]->codec.frame_rate);
179 if (get_frame_filename(filename, sizeof(filename),
180 s->path, s->img_number) < 0)
183 if (url_fopen(f, filename, URL_RDONLY) < 0)
191 av_new_packet(pkt, s->img_size);
192 pkt->stream_index = 0;
195 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
202 return -EIO; /* signal EOF */
204 pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
210 static int img_read_close(AVFormatContext *s1)
215 /******************************************************/
218 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
220 VideoData *img = s->priv_data;
222 AVImageFormat *img_fmt;
225 /* find output image format */
226 if (ap && ap->image_format) {
227 img_fmt = ap->image_format;
229 img_fmt = guess_image_format(s->filename);
234 if (s->nb_streams != 1)
238 /* we select the first matching format */
239 for(i=0;i<PIX_FMT_NB;i++) {
240 if (img_fmt->supported_pixel_formats & (1 << i))
245 img->img_fmt = img_fmt;
247 st->codec.pix_fmt = img->pix_fmt;
251 static int img_write_header(AVFormatContext *s)
253 VideoData *img = s->priv_data;
256 strcpy(img->path, s->filename);
259 if (s->oformat->flags & AVFMT_NOFILE)
267 static int img_write_packet(AVFormatContext *s, int stream_index,
268 UINT8 *buf, int size, int force_pts)
270 VideoData *img = s->priv_data;
271 AVStream *st = s->streams[stream_index];
272 ByteIOContext pb1, *pb;
274 int width, height, ret;
278 width = st->codec.width;
279 height = st->codec.height;
281 picture = (AVPicture *)buf;
284 if (get_frame_filename(filename, sizeof(filename),
285 img->path, img->img_number) < 0)
288 if (url_fopen(pb, filename, URL_WRONLY) < 0)
294 info.height = height;
295 info.pix_fmt = st->codec.pix_fmt;
296 info.pict = *picture;
297 ret = av_write_image(pb, img->img_fmt, &info);
306 static int img_write_trailer(AVFormatContext *s)
313 static AVInputFormat image_iformat = {
322 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
325 static AVInputFormat imagepipe_iformat = {
327 "piped image sequence",
339 static AVOutputFormat image_oformat = {
350 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
354 static AVOutputFormat imagepipe_oformat = {
356 "piped image sequence",
371 av_register_input_format(&image_iformat);
372 av_register_output_format(&image_oformat);
374 av_register_input_format(&imagepipe_iformat);
375 av_register_output_format(&imagepipe_oformat);