]> git.sesse.net Git - ffmpeg/blob - libavformat/img2.c
1d50fbf567ca959cb1bb8be7a0ab6925a9ddfaf9
[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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/avstring.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "internal.h"
28 #include <strings.h>
29
30 typedef struct {
31     int img_first;
32     int img_last;
33     int img_number;
34     int img_count;
35     int is_pipe;
36     char path[1024];
37 } VideoData;
38
39 typedef struct {
40     enum CodecID id;
41     const char *str;
42 } IdStrMap;
43
44 static const IdStrMap img_tags[] = {
45     { CODEC_ID_MJPEG     , "jpeg"},
46     { CODEC_ID_MJPEG     , "jpg"},
47     { CODEC_ID_LJPEG     , "ljpg"},
48     { CODEC_ID_PNG       , "png"},
49     { CODEC_ID_PNG       , "mng"},
50     { CODEC_ID_PPM       , "ppm"},
51     { CODEC_ID_PPM       , "pnm"},
52     { CODEC_ID_PGM       , "pgm"},
53     { CODEC_ID_PGMYUV    , "pgmyuv"},
54     { CODEC_ID_PBM       , "pbm"},
55     { CODEC_ID_PAM       , "pam"},
56     { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
57     { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
58     { CODEC_ID_MPEG4     , "mpg4-img"},
59     { CODEC_ID_FFV1      , "ffv1-img"},
60     { CODEC_ID_RAWVIDEO  , "y"},
61     { CODEC_ID_BMP       , "bmp"},
62     { CODEC_ID_GIF       , "gif"},
63     { CODEC_ID_TARGA     , "tga"},
64     { CODEC_ID_TIFF      , "tiff"},
65     { CODEC_ID_TIFF      , "tif"},
66     { CODEC_ID_SGI       , "sgi"},
67     { CODEC_ID_PTX       , "ptx"},
68     { CODEC_ID_PCX       , "pcx"},
69     { CODEC_ID_SUNRAST   , "sun"},
70     { CODEC_ID_SUNRAST   , "ras"},
71     { CODEC_ID_SUNRAST   , "rs"},
72     { CODEC_ID_SUNRAST   , "im1"},
73     { CODEC_ID_SUNRAST   , "im8"},
74     { CODEC_ID_SUNRAST   , "im24"},
75     { CODEC_ID_SUNRAST   , "sunras"},
76     { CODEC_ID_JPEG2000  , "jp2"},
77     { CODEC_ID_DPX       , "dpx"},
78     { CODEC_ID_PICTOR    , "pic"},
79     { CODEC_ID_NONE      , NULL}
80 };
81
82 static const int sizes[][2] = {
83     { 640, 480 },
84     { 720, 480 },
85     { 720, 576 },
86     { 352, 288 },
87     { 352, 240 },
88     { 160, 128 },
89     { 512, 384 },
90     { 640, 352 },
91     { 640, 240 },
92 };
93
94 static int infer_size(int *width_ptr, int *height_ptr, int size)
95 {
96     int i;
97
98     for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
99         if ((sizes[i][0] * sizes[i][1]) == size) {
100             *width_ptr = sizes[i][0];
101             *height_ptr = sizes[i][1];
102             return 0;
103         }
104     }
105     return -1;
106 }
107 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
108 {
109     str= strrchr(str, '.');
110     if(!str) return CODEC_ID_NONE;
111     str++;
112
113     while (tags->id) {
114         if (!strcasecmp(str, tags->str))
115             return tags->id;
116
117         tags++;
118     }
119     return CODEC_ID_NONE;
120 }
121
122 /* return -1 if no image found */
123 static int find_image_range(int *pfirst_index, int *plast_index,
124                             const char *path)
125 {
126     char buf[1024];
127     int range, last_index, range1, first_index;
128
129     /* find the first image */
130     for(first_index = 0; first_index < 5; first_index++) {
131         if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
132             *pfirst_index =
133             *plast_index = 1;
134             if (avio_check(buf, AVIO_FLAG_READ) > 0)
135                 return 0;
136             return -1;
137         }
138         if (avio_check(buf, AVIO_FLAG_READ) > 0)
139             break;
140     }
141     if (first_index == 5)
142         goto fail;
143
144     /* find the last image */
145     last_index = first_index;
146     for(;;) {
147         range = 0;
148         for(;;) {
149             if (!range)
150                 range1 = 1;
151             else
152                 range1 = 2 * range;
153             if (av_get_frame_filename(buf, sizeof(buf), path,
154                                       last_index + range1) < 0)
155                 goto fail;
156             if (avio_check(buf, AVIO_FLAG_READ) <= 0)
157                 break;
158             range = range1;
159             /* just in case... */
160             if (range >= (1 << 30))
161                 goto fail;
162         }
163         /* we are sure than image last_index + range exists */
164         if (!range)
165             break;
166         last_index += range;
167     }
168     *pfirst_index = first_index;
169     *plast_index = last_index;
170     return 0;
171  fail:
172     return -1;
173 }
174
175
176 static int read_probe(AVProbeData *p)
177 {
178     if (p->filename && av_str2id(img_tags, p->filename)) {
179         if (av_filename_number_test(p->filename))
180             return AVPROBE_SCORE_MAX;
181         else
182             return AVPROBE_SCORE_MAX/2;
183     }
184     return 0;
185 }
186
187 enum CodecID ff_guess_image2_codec(const char *filename)
188 {
189     return av_str2id(img_tags, filename);
190 }
191
192 #if FF_API_GUESS_IMG2_CODEC
193 enum CodecID av_guess_image2_codec(const char *filename){
194     return av_str2id(img_tags, filename);
195 }
196 #endif
197
198 static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
199 {
200     VideoData *s = s1->priv_data;
201     int first_index, last_index;
202     AVStream *st;
203
204     s1->ctx_flags |= AVFMTCTX_NOHEADER;
205
206     st = av_new_stream(s1, 0);
207     if (!st) {
208         return AVERROR(ENOMEM);
209     }
210
211     av_strlcpy(s->path, s1->filename, sizeof(s->path));
212     s->img_number = 0;
213     s->img_count = 0;
214
215     /* find format */
216     if (s1->iformat->flags & AVFMT_NOFILE)
217         s->is_pipe = 0;
218     else{
219         s->is_pipe = 1;
220         st->need_parsing = AVSTREAM_PARSE_FULL;
221     }
222
223     if (!ap->time_base.num) {
224         av_set_pts_info(st, 60, 1, 25);
225     } else {
226         av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
227     }
228
229     if(ap->width && ap->height){
230         st->codec->width = ap->width;
231         st->codec->height= ap->height;
232     }
233
234     if (!s->is_pipe) {
235         if (find_image_range(&first_index, &last_index, s->path) < 0)
236             return AVERROR(ENOENT);
237         s->img_first = first_index;
238         s->img_last = last_index;
239         s->img_number = first_index;
240         /* compute duration */
241         st->start_time = 0;
242         st->duration = last_index - first_index + 1;
243     }
244
245     if(s1->video_codec_id){
246         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247         st->codec->codec_id = s1->video_codec_id;
248     }else if(s1->audio_codec_id){
249         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
250         st->codec->codec_id = s1->audio_codec_id;
251     }else{
252         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
253         st->codec->codec_id = av_str2id(img_tags, s->path);
254     }
255     if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
256         st->codec->pix_fmt = ap->pix_fmt;
257
258     return 0;
259 }
260
261 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
262 {
263     VideoData *s = s1->priv_data;
264     char filename[1024];
265     int i;
266     int size[3]={0}, ret[3]={0};
267     AVIOContext *f[3];
268     AVCodecContext *codec= s1->streams[0]->codec;
269
270     if (!s->is_pipe) {
271         /* loop over input */
272         if (s1->loop_input && s->img_number > s->img_last) {
273             s->img_number = s->img_first;
274         }
275         if (s->img_number > s->img_last)
276             return AVERROR_EOF;
277         if (av_get_frame_filename(filename, sizeof(filename),
278                                   s->path, s->img_number)<0 && s->img_number > 1)
279             return AVERROR(EIO);
280         for(i=0; i<3; i++){
281             if (avio_open(&f[i], filename, AVIO_FLAG_READ) < 0) {
282                 if(i==1)
283                     break;
284                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
285                 return AVERROR(EIO);
286             }
287             size[i]= avio_size(f[i]);
288
289             if(codec->codec_id != CODEC_ID_RAWVIDEO)
290                 break;
291             filename[ strlen(filename) - 1 ]= 'U' + i;
292         }
293
294         if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
295             infer_size(&codec->width, &codec->height, size[0]);
296     } else {
297         f[0] = s1->pb;
298         if (f[0]->eof_reached)
299             return AVERROR(EIO);
300         size[0]= 4096;
301     }
302
303     av_new_packet(pkt, size[0] + size[1] + size[2]);
304     pkt->stream_index = 0;
305     pkt->flags |= AV_PKT_FLAG_KEY;
306
307     pkt->size= 0;
308     for(i=0; i<3; i++){
309         if(size[i]){
310             ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
311             if (!s->is_pipe)
312                 avio_close(f[i]);
313             if(ret[i]>0)
314                 pkt->size += ret[i];
315         }
316     }
317
318     if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
319         av_free_packet(pkt);
320         return AVERROR(EIO); /* signal EOF */
321     } else {
322         s->img_count++;
323         s->img_number++;
324         return 0;
325     }
326 }
327
328 #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
329 /******************************************************/
330 /* image output */
331
332 static int write_header(AVFormatContext *s)
333 {
334     VideoData *img = s->priv_data;
335
336     img->img_number = 1;
337     av_strlcpy(img->path, s->filename, sizeof(img->path));
338
339     /* find format */
340     if (s->oformat->flags & AVFMT_NOFILE)
341         img->is_pipe = 0;
342     else
343         img->is_pipe = 1;
344
345     return 0;
346 }
347
348 static int write_packet(AVFormatContext *s, AVPacket *pkt)
349 {
350     VideoData *img = s->priv_data;
351     AVIOContext *pb[3];
352     char filename[1024];
353     AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
354     int i;
355
356     if (!img->is_pipe) {
357         if (av_get_frame_filename(filename, sizeof(filename),
358                                   img->path, img->img_number) < 0 && img->img_number>1) {
359             av_log(s, AV_LOG_ERROR,
360                    "Could not get frame filename number %d from pattern '%s'\n",
361                    img->img_number, img->path);
362             return AVERROR(EIO);
363         }
364         for(i=0; i<3; i++){
365             if (avio_open(&pb[i], filename, AVIO_FLAG_WRITE) < 0) {
366                 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
367                 return AVERROR(EIO);
368             }
369
370             if(codec->codec_id != CODEC_ID_RAWVIDEO)
371                 break;
372             filename[ strlen(filename) - 1 ]= 'U' + i;
373         }
374     } else {
375         pb[0] = s->pb;
376     }
377
378     if(codec->codec_id == CODEC_ID_RAWVIDEO){
379         int ysize = codec->width * codec->height;
380         avio_write(pb[0], pkt->data        , ysize);
381         avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
382         avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
383         avio_flush(pb[1]);
384         avio_flush(pb[2]);
385         avio_close(pb[1]);
386         avio_close(pb[2]);
387     }else{
388         if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
389             AVStream *st = s->streams[0];
390             if(st->codec->extradata_size > 8 &&
391                AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
392                 if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
393                     goto error;
394                 avio_wb32(pb[0], 12);
395                 ffio_wfourcc(pb[0], "jP  ");
396                 avio_wb32(pb[0], 0x0D0A870A); // signature
397                 avio_wb32(pb[0], 20);
398                 ffio_wfourcc(pb[0], "ftyp");
399                 ffio_wfourcc(pb[0], "jp2 ");
400                 avio_wb32(pb[0], 0);
401                 ffio_wfourcc(pb[0], "jp2 ");
402                 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
403             }else if(pkt->size < 8 ||
404                      (!st->codec->extradata_size &&
405                       AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
406             error:
407                 av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
408                 return -1;
409             }
410         }
411         avio_write(pb[0], pkt->data, pkt->size);
412     }
413     avio_flush(pb[0]);
414     if (!img->is_pipe) {
415         avio_close(pb[0]);
416     }
417
418     img->img_number++;
419     return 0;
420 }
421
422 #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
423
424 /* input */
425 #if CONFIG_IMAGE2_DEMUXER
426 AVInputFormat ff_image2_demuxer = {
427     .name           = "image2",
428     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
429     .priv_data_size = sizeof(VideoData),
430     .read_probe     = read_probe,
431     .read_header    = read_header,
432     .read_packet    = read_packet,
433     .flags          = AVFMT_NOFILE,
434 };
435 #endif
436 #if CONFIG_IMAGE2PIPE_DEMUXER
437 AVInputFormat ff_image2pipe_demuxer = {
438     .name           = "image2pipe",
439     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
440     .priv_data_size = sizeof(VideoData),
441     .read_header    = read_header,
442     .read_packet    = read_packet,
443 };
444 #endif
445
446 /* output */
447 #if CONFIG_IMAGE2_MUXER
448 AVOutputFormat ff_image2_muxer = {
449     .name           = "image2",
450     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
451     .extensions     = "bmp,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
452                       "ppm,sgi,tga,tif,tiff,jp2",
453     .priv_data_size = sizeof(VideoData),
454     .video_codec    = CODEC_ID_MJPEG,
455     .write_header   = write_header,
456     .write_packet   = write_packet,
457     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
458 };
459 #endif
460 #if CONFIG_IMAGE2PIPE_MUXER
461 AVOutputFormat ff_image2pipe_muxer = {
462     .name           = "image2pipe",
463     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
464     .priv_data_size = sizeof(VideoData),
465     .video_codec    = CODEC_ID_MJPEG,
466     .write_header   = write_header,
467     .write_packet   = write_packet,
468     .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
469 };
470 #endif