3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 * Copyright (c) 2004 Michael Niedermayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 static const IdStrMap img_tags[] = {
37 { CODEC_ID_MJPEG , "jpeg"},
38 { CODEC_ID_MJPEG , "jpg"},
39 { CODEC_ID_LJPEG , "ljpg"},
40 { CODEC_ID_PNG , "png"},
41 { CODEC_ID_PPM , "ppm"},
42 { CODEC_ID_PGM , "pgm"},
43 { CODEC_ID_PGMYUV , "pgmyuv"},
44 { CODEC_ID_PBM , "pbm"},
45 { CODEC_ID_PAM , "pam"},
46 { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
47 { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
48 { CODEC_ID_MPEG4 , "mpg4-img"},
49 { CODEC_ID_FFV1 , "ffv1-img"},
53 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
55 str= strrchr(str, '.');
56 if(!str) return CODEC_ID_NONE;
61 for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
62 if(tags->str[i]==0 && str[i]==0)
71 static const char *av_id2str(const IdStrMap *tags, enum CodecID id)
81 /* return -1 if no image found */
82 static int find_image_range(int *pfirst_index, int *plast_index,
86 int range, last_index, range1, first_index;
88 /* find the first image */
89 for(first_index = 0; first_index < 5; first_index++) {
90 if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
101 /* find the last image */
102 last_index = first_index;
110 if (get_frame_filename(buf, sizeof(buf), path,
111 last_index + range1) < 0)
116 /* just in case... */
117 if (range >= (1 << 30))
120 /* we are sure than image last_index + range exists */
125 *pfirst_index = first_index;
126 *plast_index = last_index;
133 static int image_probe(AVProbeData *p)
135 if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
136 return AVPROBE_SCORE_MAX;
141 enum CodecID av_guess_image2_codec(const char *filename){
142 return av_str2id(img_tags, filename);
145 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
147 VideoData *s = s1->priv_data;
148 int first_index, last_index;
151 s1->ctx_flags |= AVFMTCTX_NOHEADER;
153 st = av_new_stream(s1, 0);
159 strcpy(s->path, s1->filename);
164 if (s1->iformat->flags & AVFMT_NOFILE)
171 if (!ap || !ap->frame_rate) {
172 st->codec.frame_rate = 25;
173 st->codec.frame_rate_base = 1;
175 st->codec.frame_rate = ap->frame_rate;
176 st->codec.frame_rate_base = ap->frame_rate_base;
180 if (find_image_range(&first_index, &last_index, s->path) < 0)
182 s->img_first = first_index;
183 s->img_last = last_index;
184 s->img_number = first_index;
185 /* compute duration */
187 st->duration = ((int64_t)AV_TIME_BASE *
188 (last_index - first_index + 1) *
189 st->codec.frame_rate_base) / st->codec.frame_rate;
192 if(ap->video_codec_id){
193 st->codec.codec_type = CODEC_TYPE_VIDEO;
194 st->codec.codec_id = ap->video_codec_id;
195 }else if(ap->audio_codec_id){
196 st->codec.codec_type = CODEC_TYPE_AUDIO;
197 st->codec.codec_id = ap->audio_codec_id;
199 st->codec.codec_type = CODEC_TYPE_VIDEO;
200 st->codec.codec_id = av_str2id(img_tags, s->path);
210 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
212 VideoData *s = s1->priv_data;
215 ByteIOContext f1, *f;
218 /* loop over input */
219 /* if (loop_input && s->img_number > s->img_last) {
220 s->img_number = s->img_first;
222 if (get_frame_filename(filename, sizeof(filename),
223 s->path, s->img_number)<0 && s->img_number > 1)
226 if (url_fopen(f, filename, URL_RDONLY) < 0)
235 av_new_packet(pkt, 4096);
237 av_new_packet(pkt, url_filesize(url_fileno(f)));
239 pkt->stream_index = 0;
240 pkt->flags |= PKT_FLAG_KEY;
242 ret = get_buffer(f, pkt->data, pkt->size);
249 return AVERROR_IO; /* signal EOF */
257 static int img_read_close(AVFormatContext *s1)
262 /******************************************************/
265 static int img_write_header(AVFormatContext *s)
267 VideoData *img = s->priv_data;
270 strcpy(img->path, s->filename);
273 if (s->oformat->flags & AVFMT_NOFILE)
281 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
283 VideoData *img = s->priv_data;
284 ByteIOContext pb1, *pb;
288 if (get_frame_filename(filename, sizeof(filename),
289 img->path, img->img_number) < 0 && img->img_number>1)
292 if (url_fopen(pb, filename, URL_WRONLY) < 0)
298 put_buffer(pb, pkt->data, pkt->size);
299 put_flush_packet(pb);
308 static int img_write_trailer(AVFormatContext *s)
315 static AVInputFormat image2_iformat = {
328 static AVInputFormat image2pipe_iformat = {
330 "piped image2 sequence",
342 static AVOutputFormat image2_oformat = {
356 static AVOutputFormat image2pipe_oformat = {
358 "piped image2 sequence",
371 av_register_input_format(&image2_iformat);
372 av_register_output_format(&image2_oformat);
374 av_register_input_format(&image2pipe_iformat);
375 av_register_output_format(&image2pipe_oformat);