Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_libavcodec / libavformat / img.c
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 "avformat.h"
20
21 /* XXX: this is a hack */
22 int loop_input = 0;
23
24 typedef struct {
25     int width;
26     int height;
27     int img_first;
28     int img_last;
29     int img_number;
30     int img_count;
31     int img_size;
32     AVImageFormat *img_fmt;
33     int pix_fmt;
34     int is_pipe;
35     char path[1024];
36     /* temporary usage */
37     void *ptr;
38 } VideoData;
39
40
41 /* return -1 if no image found */
42 static int find_image_range(int *pfirst_index, int *plast_index, 
43                             const char *path)
44 {
45     char buf[1024];
46     int range, last_index, range1, first_index;
47
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)
51             goto fail;
52         if (url_exist(buf))
53             break;
54     }
55     if (first_index == 5)
56         goto fail;
57     
58     /* find the last image */
59     last_index = first_index;
60     for(;;) {
61         range = 0;
62         for(;;) {
63             if (!range)
64                 range1 = 1;
65             else
66                 range1 = 2 * range;
67             if (get_frame_filename(buf, sizeof(buf), path, 
68                                    last_index + range1) < 0)
69                 goto fail;
70             if (!url_exist(buf))
71                 break;
72             range = range1;
73             /* just in case... */
74             if (range >= (1 << 30))
75                 goto fail;
76         }
77         /* we are sure than image last_index + range exists */
78         if (!range)
79             break;
80         last_index += range;
81     }
82     *pfirst_index = first_index;
83     *plast_index = last_index;
84     return 0;
85  fail:
86     return -1;
87 }
88
89
90 static int image_probe(AVProbeData *p)
91 {
92     if (filename_number_test(p->filename) >= 0 && guess_image_format(p->filename))
93         return AVPROBE_SCORE_MAX;
94     else
95         return 0;
96 }
97
98 static int read_header_alloc_cb(void *opaque, AVImageInfo *info)
99 {
100     VideoData *s = opaque;
101
102     s->width = info->width;
103     s->height = info->height;
104     s->pix_fmt = info->pix_fmt;
105     /* stop image reading but no error */
106     return 1;
107 }
108
109 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
110 {
111     VideoData *s = s1->priv_data;
112     int ret, first_index, last_index;
113     char buf[1024];
114     ByteIOContext pb1, *f = &pb1;
115     AVStream *st;
116
117     st = av_new_stream(s1, 0);
118     if (!st) {
119         av_free(s);
120         return -ENOMEM;
121     }
122
123     if (ap && ap->image_format)
124         s->img_fmt = ap->image_format;
125
126     strcpy(s->path, s1->filename);
127     s->img_number = 0;
128     s->img_count = 0;
129     
130     /* find format */
131     if (s1->iformat->flags & AVFMT_NOFILE)
132         s->is_pipe = 0;
133     else
134         s->is_pipe = 1;
135         
136     if (!ap || !ap->frame_rate) {
137         st->codec.frame_rate      = 25;
138         st->codec.frame_rate_base = 1;
139     } else {
140         st->codec.frame_rate      = ap->frame_rate;
141         st->codec.frame_rate_base = ap->frame_rate_base;
142     }
143     
144     if (!s->is_pipe) {
145         if (find_image_range(&first_index, &last_index, s->path) < 0)
146             goto fail;
147         s->img_first = first_index;
148         s->img_last = last_index;
149         s->img_number = first_index;
150         /* compute duration */
151         st->start_time = 0;
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)
156             goto fail;
157         if (url_fopen(f, buf, URL_RDONLY) < 0)
158             goto fail;
159     } else {
160         f = &s1->pb;
161     }
162     
163     ret = av_read_image(f, s1->filename, s->img_fmt, read_header_alloc_cb, s);
164     if (ret < 0)
165         goto fail1;
166
167     if (!s->is_pipe) {
168         url_fclose(f);
169     } else {
170         url_fseek(f, 0, SEEK_SET);
171     }
172     
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, s->height);
179
180     return 0;
181  fail1:
182     if (!s->is_pipe)
183         url_fclose(f);
184  fail:
185     av_free(s);
186     return -EIO;
187 }
188
189 static int read_packet_alloc_cb(void *opaque, AVImageInfo *info)
190 {
191     VideoData *s = opaque;
192
193     if (info->width != s->width ||
194         info->height != s->height)
195         return -1;
196     avpicture_fill(&info->pict, s->ptr, info->pix_fmt, info->width, info->height);
197     return 0;
198 }
199
200 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
201 {
202     VideoData *s = s1->priv_data;
203     char filename[1024];
204     int ret;
205     ByteIOContext f1, *f;
206
207     if (!s->is_pipe) {
208         /* loop over input */
209         if (loop_input && s->img_number > s->img_last) {
210             s->img_number = s->img_first;
211         }
212         if (get_frame_filename(filename, sizeof(filename),
213                                s->path, s->img_number) < 0)
214             return -EIO;
215         f = &f1;
216         if (url_fopen(f, filename, URL_RDONLY) < 0)
217             return -EIO;
218     } else {
219         f = &s1->pb;
220         if (url_feof(f))
221             return -EIO;
222     }
223
224     av_new_packet(pkt, s->img_size);
225     pkt->stream_index = 0;
226
227     s->ptr = pkt->data;
228     ret = av_read_image(f, filename, s->img_fmt, read_packet_alloc_cb, s);
229     if (!s->is_pipe) {
230         url_fclose(f);
231     }
232
233     if (ret < 0) {
234         av_free_packet(pkt);
235         return -EIO; /* signal EOF */
236     } else {
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->pts_den, s1->streams[0]->codec.frame_rate) / s1->pts_num;
240         s->img_count++;
241         s->img_number++;
242         return 0;
243     }
244 }
245
246 static int img_read_close(AVFormatContext *s1)
247 {
248     return 0;
249 }
250
251 /******************************************************/
252 /* image output */
253
254 static int img_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
255 {
256     VideoData *img = s->priv_data;
257     AVStream *st;
258     AVImageFormat *img_fmt;
259     int i;
260
261     /* find output image format */
262     if (ap && ap->image_format) {
263         img_fmt = ap->image_format;
264     } else {
265         img_fmt = guess_image_format(s->filename);
266     }
267     if (!img_fmt)
268         return -1;
269
270     if (s->nb_streams != 1)
271         return -1;
272     
273     st = s->streams[0];
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))
277             break;
278     }
279     if (i >= PIX_FMT_NB)
280         return -1;
281     img->img_fmt = img_fmt;
282     img->pix_fmt = i;
283     st->codec.pix_fmt = img->pix_fmt;
284     return 0;
285 }
286
287 static int img_write_header(AVFormatContext *s)
288 {
289     VideoData *img = s->priv_data;
290
291     img->img_number = 1;
292     strcpy(img->path, s->filename);
293
294     /* find format */
295     if (s->oformat->flags & AVFMT_NOFILE)
296         img->is_pipe = 0;
297     else
298         img->is_pipe = 1;
299         
300     return 0;
301 }
302
303 static int img_write_packet(AVFormatContext *s, int stream_index,
304                             const uint8_t *buf, int size, int64_t pts)
305 {
306     VideoData *img = s->priv_data;
307     AVStream *st = s->streams[stream_index];
308     ByteIOContext pb1, *pb;
309     AVPicture *picture;
310     int width, height, ret;
311     char filename[1024];
312     AVImageInfo info;
313
314     width = st->codec.width;
315     height = st->codec.height;
316     
317     picture = (AVPicture *)buf;
318
319     if (!img->is_pipe) {
320         if (get_frame_filename(filename, sizeof(filename), 
321                                img->path, img->img_number) < 0)
322             return -EIO;
323         pb = &pb1;
324         if (url_fopen(pb, filename, URL_WRONLY) < 0)
325             return -EIO;
326     } else {
327         pb = &s->pb;
328     }
329     info.width = width;
330     info.height = height;
331     info.pix_fmt = st->codec.pix_fmt;
332     info.pict = *picture;
333     ret = av_write_image(pb, img->img_fmt, &info);
334     if (!img->is_pipe) {
335         url_fclose(pb);
336     }
337
338     img->img_number++;
339     return 0;
340 }
341
342 static int img_write_trailer(AVFormatContext *s)
343 {
344     return 0;
345 }
346
347 /* input */
348
349 static AVInputFormat image_iformat = {
350     "image",
351     "image sequence",
352     sizeof(VideoData),
353     image_probe,
354     img_read_header,
355     img_read_packet,
356     img_read_close,
357     NULL,
358     AVFMT_NOFILE | AVFMT_NEEDNUMBER,
359 };
360
361 static AVInputFormat imagepipe_iformat = {
362     "imagepipe",
363     "piped image sequence",
364     sizeof(VideoData),
365     NULL, /* no probe */
366     img_read_header,
367     img_read_packet,
368     img_read_close,
369     NULL,
370 };
371
372
373 /* output */
374
375 static AVOutputFormat image_oformat = {
376     "image",
377     "image sequence",
378     "",
379     "",
380     sizeof(VideoData),
381     CODEC_ID_NONE,
382     CODEC_ID_RAWVIDEO,
383     img_write_header,
384     img_write_packet,
385     img_write_trailer,
386     AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RAWPICTURE,
387     img_set_parameters,
388 };
389
390 static AVOutputFormat imagepipe_oformat = {
391     "imagepipe",
392     "piped image sequence",
393     "",
394     "",
395     sizeof(VideoData),
396     CODEC_ID_NONE,
397     CODEC_ID_RAWVIDEO,
398     img_write_header,
399     img_write_packet,
400     img_write_trailer,
401     AVFMT_RAWPICTURE,
402     img_set_parameters,
403 };
404
405 int img_init(void)
406 {
407     av_register_input_format(&image_iformat);
408     av_register_output_format(&image_oformat);
409
410     av_register_input_format(&imagepipe_iformat);
411     av_register_output_format(&imagepipe_oformat);
412     
413     return 0;
414 }