]> git.sesse.net Git - ffmpeg/blob - libavformat/img2.c
Make PNG produce correct 8-bit pictures
[ffmpeg] / libavformat / img2.c
1 /*
2  * Image format
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4  * Copyright (c) 2004 Michael Niedermayer
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #include "avformat.h"
23
24 typedef struct {
25     int img_first;
26     int img_last;
27     int img_number;
28     int img_count;
29     int is_pipe;
30     char path[1024];
31 } VideoData;
32
33 typedef struct {
34     enum CodecID id;
35     const char *str;
36 } IdStrMap;
37
38 static const IdStrMap img_tags[] = {
39     { CODEC_ID_MJPEG     , "jpeg"},
40     { CODEC_ID_MJPEG     , "jpg"},
41     { CODEC_ID_LJPEG     , "ljpg"},
42     { CODEC_ID_PNG       , "png"},
43     { CODEC_ID_PPM       , "ppm"},
44     { CODEC_ID_PGM       , "pgm"},
45     { CODEC_ID_PGMYUV    , "pgmyuv"},
46     { CODEC_ID_PBM       , "pbm"},
47     { CODEC_ID_PAM       , "pam"},
48     { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
49     { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
50     { CODEC_ID_MPEG4     , "mpg4-img"},
51     { CODEC_ID_FFV1      , "ffv1-img"},
52     { CODEC_ID_RAWVIDEO  , "y"},
53     { CODEC_ID_BMP       , "bmp"},
54     {0, NULL}
55 };
56
57 static int sizes[][2] = {
58     { 640, 480 },
59     { 720, 480 },
60     { 720, 576 },
61     { 352, 288 },
62     { 352, 240 },
63     { 160, 128 },
64     { 512, 384 },
65     { 640, 352 },
66     { 640, 240 },
67 };
68
69 static int infer_size(int *width_ptr, int *height_ptr, int size)
70 {
71     int i;
72
73     for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
74         if ((sizes[i][0] * sizes[i][1]) == size) {
75             *width_ptr = sizes[i][0];
76             *height_ptr = sizes[i][1];
77             return 0;
78         }
79     }
80     return -1;
81 }
82 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
83 {
84     str= strrchr(str, '.');
85     if(!str) return CODEC_ID_NONE;
86     str++;
87
88     while (tags->id) {
89         int i;
90         for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
91             if(tags->str[i]==0 && str[i]==0)
92                 return tags->id;
93         }
94
95         tags++;
96     }
97     return CODEC_ID_NONE;
98 }
99
100 /* return -1 if no image found */
101 static int find_image_range(int *pfirst_index, int *plast_index,
102                             const char *path)
103 {
104     char buf[1024];
105     int range, last_index, range1, first_index;
106
107     /* find the first image */
108     for(first_index = 0; first_index < 5; first_index++) {
109         if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
110             *pfirst_index =
111             *plast_index = 1;
112             return 0;
113         }
114         if (url_exist(buf))
115             break;
116     }
117     if (first_index == 5)
118         goto fail;
119
120     /* find the last image */
121     last_index = first_index;
122     for(;;) {
123         range = 0;
124         for(;;) {
125             if (!range)
126                 range1 = 1;
127             else
128                 range1 = 2 * range;
129             if (av_get_frame_filename(buf, sizeof(buf), path,
130                                       last_index + range1) < 0)
131                 goto fail;
132             if (!url_exist(buf))
133                 break;
134             range = range1;
135             /* just in case... */
136             if (range >= (1 << 30))
137                 goto fail;
138         }
139         /* we are sure than image last_index + range exists */
140         if (!range)
141             break;
142         last_index += range;
143     }
144     *pfirst_index = first_index;
145     *plast_index = last_index;
146     return 0;
147  fail:
148     return -1;
149 }
150
151
152 static int image_probe(AVProbeData *p)
153 {
154     if (av_filename_number_test(p->filename) && av_str2id(img_tags, p->filename))
155         return AVPROBE_SCORE_MAX;
156     else
157         return 0;
158 }
159
160 enum CodecID av_guess_image2_codec(const char *filename){
161     return av_str2id(img_tags, filename);
162 }
163
164 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
165 {
166     VideoData *s = s1->priv_data;
167     int first_index, last_index;
168     AVStream *st;
169
170     s1->ctx_flags |= AVFMTCTX_NOHEADER;
171
172     st = av_new_stream(s1, 0);
173     if (!st) {
174         return -ENOMEM;
175     }
176
177     pstrcpy(s->path, sizeof(s->path), s1->filename);
178     s->img_number = 0;
179     s->img_count = 0;
180
181     /* find format */
182     if (s1->iformat->flags & AVFMT_NOFILE)
183         s->is_pipe = 0;
184     else{
185         s->is_pipe = 1;
186         st->need_parsing= 1;
187     }
188
189     if (!ap->time_base.num) {
190         av_set_pts_info(st, 60, 1, 25);
191     } else {
192         av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
193     }
194
195     if(ap->width && ap->height){
196         st->codec->width = ap->width;
197         st->codec->height= ap->height;
198     }
199
200     if (!s->is_pipe) {
201         if (find_image_range(&first_index, &last_index, s->path) < 0)
202             return AVERROR_IO;
203         s->img_first = first_index;
204         s->img_last = last_index;
205         s->img_number = first_index;
206         /* compute duration */
207         st->start_time = 0;
208         st->duration = last_index - first_index + 1;
209     }
210
211     if(ap->video_codec_id){
212         st->codec->codec_type = CODEC_TYPE_VIDEO;
213         st->codec->codec_id = ap->video_codec_id;
214     }else if(ap->audio_codec_id){
215         st->codec->codec_type = CODEC_TYPE_AUDIO;
216         st->codec->codec_id = ap->audio_codec_id;
217     }else{
218         st->codec->codec_type = CODEC_TYPE_VIDEO;
219         st->codec->codec_id = av_str2id(img_tags, s->path);
220     }
221     if(st->codec->codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
222         st->codec->pix_fmt = ap->pix_fmt;
223
224     return 0;
225 }
226
227 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
228 {
229     VideoData *s = s1->priv_data;
230     char filename[1024];
231     int i;
232     int size[3]={0}, ret[3]={0};
233     ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
234     AVCodecContext *codec= s1->streams[0]->codec;
235
236     if (!s->is_pipe) {
237         /* loop over input */
238         if (s1->loop_input && s->img_number > s->img_last) {
239             s->img_number = s->img_first;
240         }
241         if (av_get_frame_filename(filename, sizeof(filename),
242                                   s->path, s->img_number)<0 && s->img_number > 1)
243             return AVERROR_IO;
244         for(i=0; i<3; i++){
245             if (url_fopen(f[i], filename, URL_RDONLY) < 0)
246                 return AVERROR_IO;
247             size[i]= url_fsize(f[i]);
248
249             if(codec->codec_id != CODEC_ID_RAWVIDEO)
250                 break;
251             filename[ strlen(filename) - 1 ]= 'U' + i;
252         }
253
254         if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
255             infer_size(&codec->width, &codec->height, size[0]);
256     } else {
257         f[0] = &s1->pb;
258         if (url_feof(f[0]))
259             return AVERROR_IO;
260         size[0]= 4096;
261     }
262
263     av_new_packet(pkt, size[0] + size[1] + size[2]);
264     pkt->stream_index = 0;
265     pkt->flags |= PKT_FLAG_KEY;
266
267     pkt->size= 0;
268     for(i=0; i<3; i++){
269         if(size[i]){
270             ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
271             if (!s->is_pipe)
272                 url_fclose(f[i]);
273             if(ret[i]>0)
274                 pkt->size += ret[i];
275         }
276     }
277
278     if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
279         av_free_packet(pkt);
280         return AVERROR_IO; /* signal EOF */
281     } else {
282         s->img_count++;
283         s->img_number++;
284         return 0;
285     }
286 }
287
288 static int img_read_close(AVFormatContext *s1)
289 {
290     return 0;
291 }
292
293 #ifdef CONFIG_MUXERS
294 /******************************************************/
295 /* image output */
296
297 static int img_write_header(AVFormatContext *s)
298 {
299     VideoData *img = s->priv_data;
300
301     img->img_number = 1;
302     pstrcpy(img->path, sizeof(img->path), s->filename);
303
304     /* find format */
305     if (s->oformat->flags & AVFMT_NOFILE)
306         img->is_pipe = 0;
307     else
308         img->is_pipe = 1;
309
310     return 0;
311 }
312
313 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
314 {
315     VideoData *img = s->priv_data;
316     ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
317     char filename[1024];
318     AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
319     int i;
320
321     if (!img->is_pipe) {
322         if (av_get_frame_filename(filename, sizeof(filename),
323                                   img->path, img->img_number) < 0 && img->img_number>1)
324             return AVERROR_IO;
325         for(i=0; i<3; i++){
326             if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
327                 return AVERROR_IO;
328
329             if(codec->codec_id != CODEC_ID_RAWVIDEO)
330                 break;
331             filename[ strlen(filename) - 1 ]= 'U' + i;
332         }
333     } else {
334         pb[0] = &s->pb;
335     }
336
337     if(codec->codec_id == CODEC_ID_RAWVIDEO){
338         int ysize = codec->width * codec->height;
339         put_buffer(pb[0], pkt->data        , ysize);
340         put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
341         put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
342         put_flush_packet(pb[1]);
343         put_flush_packet(pb[2]);
344         url_fclose(pb[1]);
345         url_fclose(pb[2]);
346     }else{
347         put_buffer(pb[0], pkt->data, pkt->size);
348     }
349     put_flush_packet(pb[0]);
350     if (!img->is_pipe) {
351         url_fclose(pb[0]);
352     }
353
354     img->img_number++;
355     return 0;
356 }
357
358 static int img_write_trailer(AVFormatContext *s)
359 {
360     return 0;
361 }
362
363 #endif /* CONFIG_MUXERS */
364
365 /* input */
366 #ifdef CONFIG_IMAGE2_DEMUXER
367 AVInputFormat image2_demuxer = {
368     "image2",
369     "image2 sequence",
370     sizeof(VideoData),
371     image_probe,
372     img_read_header,
373     img_read_packet,
374     img_read_close,
375     NULL,
376     NULL,
377     AVFMT_NOFILE,
378 };
379 #endif
380 #ifdef CONFIG_IMAGE2PIPE_DEMUXER
381 AVInputFormat image2pipe_demuxer = {
382     "image2pipe",
383     "piped image2 sequence",
384     sizeof(VideoData),
385     NULL, /* no probe */
386     img_read_header,
387     img_read_packet,
388     img_read_close,
389     NULL,
390 };
391 #endif
392
393 /* output */
394 #ifdef CONFIG_IMAGE2_MUXER
395 AVOutputFormat image2_muxer = {
396     "image2",
397     "image2 sequence",
398     "",
399     "",
400     sizeof(VideoData),
401     CODEC_ID_NONE,
402     CODEC_ID_MJPEG,
403     img_write_header,
404     img_write_packet,
405     img_write_trailer,
406     AVFMT_NOFILE,
407 };
408 #endif
409 #ifdef CONFIG_IMAGE2PIPE_MUXER
410 AVOutputFormat image2pipe_muxer = {
411     "image2pipe",
412     "piped image2 sequence",
413     "",
414     "",
415     sizeof(VideoData),
416     CODEC_ID_NONE,
417     CODEC_ID_MJPEG,
418     img_write_header,
419     img_write_packet,
420     img_write_trailer,
421 };
422 #endif