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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 AVImageFormat *img_fmt;
38 /* return -1 if no image found */
39 static int find_image_range(int *pfirst_index, int *plast_index,
43 int range, last_index, range1, first_index;
45 /* find the first image */
46 for(first_index = 0; first_index < 5; first_index++) {
47 if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0)
55 /* find the last image */
56 last_index = first_index;
64 if (get_frame_filename(buf, sizeof(buf), path,
65 last_index + range1) < 0)
71 if (range >= (1 << 30))
74 /* we are sure than image last_index + range exists */
79 *pfirst_index = first_index;
80 *plast_index = last_index;
87 static int image_probe(AVProbeData *p)
89 if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
90 return AVPROBE_SCORE_MAX-1;
95 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
97 VideoData *s = opaque;
99 s->width = info->width;
100 s->height = info->height;
101 s->pix_fmt = info->pix_fmt;
102 /* stop image reading but no error */
106 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
108 VideoData *s = s1->priv_data;
109 int ret, first_index, last_index;
111 ByteIOContext pb1, *f = &pb1;
114 st = av_new_stream(s1, 0);
119 if (ap->image_format)
120 s->img_fmt = ap->image_format;
122 pstrcpy(s->path, sizeof(s->path), s1->filename);
127 if (s1->iformat->flags & AVFMT_NOFILE)
132 if (!ap->time_base.num) {
133 st->codec->time_base= (AVRational){1,25};
135 st->codec->time_base= ap->time_base;
139 if (find_image_range(&first_index, &last_index, s->path) < 0)
141 s->img_first = first_index;
142 s->img_last = last_index;
143 s->img_number = first_index;
144 /* compute duration */
146 st->duration = last_index - first_index + 1;
147 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
149 if (url_fopen(f, buf, URL_RDONLY) < 0)
155 ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
162 url_fseek(f, 0, SEEK_SET);
165 st->codec->codec_type = CODEC_TYPE_VIDEO;
166 st->codec->codec_id = CODEC_ID_RAWVIDEO;
167 st->codec->width = s->width;
168 st->codec->height = s->height;
169 st->codec->pix_fmt = s->pix_fmt;
170 s->img_size = avpicture_get_size(s->pix_fmt, (s->width+15)&(~15), (s->height+15)&(~15));
180 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
182 VideoData *s = opaque;
184 if (info->width != s->width ||
185 info->height != s->height)
187 avpicture_fill(&info->pict, s->ptr, info->pix_fmt, (info->width+15)&(~15), (info->height+15)&(~15));
191 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
193 VideoData *s = s1->priv_data;
196 ByteIOContext f1, *f;
199 /* loop over input */
200 if (s1->loop_input && s->img_number > s->img_last) {
201 s->img_number = s->img_first;
203 if (get_frame_filename(filename, sizeof(filename),
204 s->path, s->img_number) < 0)
207 if (url_fopen(f, filename, URL_RDONLY) < 0)
215 av_new_packet(pkt, s->img_size);
216 pkt->stream_index = 0;
219 ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
226 return AVERROR_IO; /* signal EOF */
228 /* XXX: computing this pts is not necessary as it is done in
229 the generic code too */
230 pkt->pts = av_rescale((int64_t)s->img_count * s1->streams[0]->codec->time_base.num, s1->streams[0]->time_base.den, s1->streams[0]->codec->time_base.den) / s1->streams[0]->time_base.num;
237 static int img_read_close(AVFormatContext *s1)
242 /******************************************************/
245 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
247 VideoData *img = s->priv_data;
249 AVImageFormat *img_fmt;
252 /* find output image format */
253 if (ap->image_format) {
254 img_fmt = ap->image_format;
256 img_fmt = guess_image_format(s->filename);
261 if (s->nb_streams != 1)
265 /* we select the first matching format */
266 for(i=0;i<PIX_FMT_NB;i++) {
267 if (img_fmt->supported_pixel_formats & (1 << i))
272 img->img_fmt = img_fmt;
274 st->codec->pix_fmt = img->pix_fmt;
278 static int img_write_header(AVFormatContext *s)
280 VideoData *img = s->priv_data;
283 pstrcpy(img->path, sizeof(img->path), s->filename);
286 if (s->oformat->flags & AVFMT_NOFILE)
294 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
296 VideoData *img = s->priv_data;
297 AVStream *st = s->streams[pkt->stream_index];
298 ByteIOContext pb1, *pb;
300 int width, height, ret;
304 width = st->codec->width;
305 height = st->codec->height;
307 picture = (AVPicture *)pkt->data;
310 if (get_frame_filename(filename, sizeof(filename),
311 img->path, img->img_number) < 0)
314 if (url_fopen(pb, filename, URL_WRONLY) < 0)
320 info.height = height;
321 info.pix_fmt = st->codec->pix_fmt;
322 info.interleaved = 0; /* FIXME: there should be a way to set it right */
323 info.pict = *picture;
324 ret = av_write_image(pb, img->img_fmt, &info);
333 static int img_write_trailer(AVFormatContext *s)
339 #ifdef CONFIG_IMAGE_DEMUXER
340 AVInputFormat image_demuxer = {
350 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
353 #ifdef CONFIG_IMAGEPIPE_DEMUXER
354 AVInputFormat imagepipe_demuxer = {
356 "piped image sequence",
367 #ifdef CONFIG_IMAGE_MUXER
368 AVOutputFormat image_muxer = {
379 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
383 #ifdef CONFIG_IMAGEPIPE_MUXER
384 AVOutputFormat imagepipe_muxer = {
386 "piped image sequence",