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
21 /* XXX: this is a hack */
32 AVImageFormat *img_fmt;
41 /* return -1 if no image found */
42 static int find_image_range(int *pfirst_index, int *plast_index,
46 int range, last_index, range1, first_index;
48 /* find the first image */
49 for(first_index = 0; first_index < 5; first_index++) {
50 if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
58 /* find the last image */
59 last_index = first_index;
67 if (get_frame_filename(buf, sizeof(buf), path,
68 last_index + range1) < 0)
74 if (range >= (1 << 30))
77 /* we are sure than image last_index + range exists */
82 *pfirst_index = first_index;
83 *plast_index = last_index;
90 static int image_probe(AVProbeData *p)
92 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
93 return AVPROBE_SCORE_MAX-1;
98 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
100 VideoData *s = opaque;
102 s->width = info->width;
103 s->height = info->height;
104 s->pix_fmt = info->pix_fmt;
105 /* stop image reading but no error */
109 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
111 VideoData *s = s1->priv_data;
112 int ret, first_index, last_index;
114 ByteIOContext pb1, *f = &pb1;
117 st = av_new_stream(s1, 0);
123 if (ap && ap->image_format)
124 s->img_fmt = ap->image_format;
126 pstrcpy(s->path, sizeof(s->path), s1->filename);
131 if (s1->iformat->flags & AVFMT_NOFILE)
136 if (!ap || !ap->frame_rate) {
137 st->codec.frame_rate = 25;
138 st->codec.frame_rate_base = 1;
140 st->codec.frame_rate = ap->frame_rate;
141 st->codec.frame_rate_base = ap->frame_rate_base;
145 if (find_image_range(&first_index, &last_index, s->path) < 0)
147 s->img_first = first_index;
148 s->img_last = last_index;
149 s->img_number = first_index;
150 /* compute duration */
152 st->duration = ((int64_t)AV_TIME_BASE *
153 (last_index - first_index + 1) *
154 st->codec.frame_rate_base) / st->codec.frame_rate;
155 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
157 if (url_fopen(f, buf, URL_RDONLY) < 0)
163 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
170 url_fseek(f, 0, SEEK_SET);
173 st->codec.codec_type = CODEC_TYPE_VIDEO;
174 st->codec.codec_id = CODEC_ID_RAWVIDEO;
175 st->codec.width = s->width;
176 st->codec.height = s->height;
177 st->codec.pix_fmt = s->pix_fmt;
178 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
189 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
191 VideoData *s = opaque;
193 if (info->width != s->width ||
194 info->height != s->height)
196 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
200 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
202 VideoData *s = s1->priv_data;
205 ByteIOContext f1, *f;
208 /* loop over input */
209 if (loop_input && s->img_number > s->img_last) {
210 s->img_number = s->img_first;
212 if (get_frame_filename(filename, sizeof(filename),
213 s->path, s->img_number) < 0)
216 if (url_fopen(f, filename, URL_RDONLY) < 0)
224 av_new_packet(pkt, s->img_size);
225 pkt->stream_index = 0;
228 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
235 return AVERROR_IO; /* signal EOF */
237 /* XXX: computing this pts is not necessary as it is done in
238 the generic code too */
239 pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec.frame_rate_base, s1->streams[0]->time_base.den, s1->streams[0]->codec.frame_rate) / s1->streams[0]->time_base.num;
246 static int img_read_close(AVFormatContext *s1)
251 /******************************************************/
254 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
256 VideoData *img = s->priv_data;
258 AVImageFormat *img_fmt;
261 /* find output image format */
262 if (ap && ap->image_format) {
263 img_fmt = ap->image_format;
265 img_fmt = guess_image_format(s->filename);
270 if (s->nb_streams != 1)
274 /* we select the first matching format */
275 for(i=0;i<PIX_FMT_NB;i++) {
276 if (img_fmt->supported_pixel_formats & (1 << i))
281 img->img_fmt = img_fmt;
283 st->codec.pix_fmt = img->pix_fmt;
287 static int img_write_header(AVFormatContext *s)
289 VideoData *img = s->priv_data;
292 pstrcpy(img->path, sizeof(img->path), s->filename);
295 if (s->oformat->flags & AVFMT_NOFILE)
303 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
305 VideoData *img = s->priv_data;
306 AVStream *st = s->streams[pkt->stream_index];
307 ByteIOContext pb1, *pb;
309 int width, height, ret;
313 width = st->codec.width;
314 height = st->codec.height;
316 picture = (AVPicture *)pkt->data;
319 if (get_frame_filename(filename, sizeof(filename),
320 img->path, img->img_number) < 0)
323 if (url_fopen(pb, filename, URL_WRONLY) < 0)
329 info.height = height;
330 info.pix_fmt = st->codec.pix_fmt;
331 info.interleaved = 0; /* FIXME: there should be a way to set it right */
332 info.pict = *picture;
333 ret = av_write_image(pb, img->img_fmt, &info);
342 static int img_write_trailer(AVFormatContext *s)
349 static AVInputFormat image_iformat = {
359 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
362 static AVInputFormat imagepipe_iformat = {
364 "piped image sequence",
376 static AVOutputFormat image_oformat = {
387 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
391 static AVOutputFormat imagepipe_oformat = {
393 "piped image sequence",
408 av_register_input_format(&image_iformat);
409 av_register_output_format(&image_oformat);
411 av_register_input_format(&imagepipe_iformat);
412 av_register_output_format(&imagepipe_oformat);