3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of Libav.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/avstring.h"
26 #include "avio_internal.h"
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}
82 static const int sizes[][2] = {
94 static int infer_size(int *width_ptr, int *height_ptr, int size)
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];
107 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
109 str= strrchr(str, '.');
110 if(!str) return CODEC_ID_NONE;
114 if (!strcasecmp(str, tags->str))
119 return CODEC_ID_NONE;
122 /* return -1 if no image found */
123 static int find_image_range(int *pfirst_index, int *plast_index,
127 int range, last_index, range1, first_index;
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){
141 if (first_index == 5)
144 /* find the last image */
145 last_index = first_index;
153 if (av_get_frame_filename(buf, sizeof(buf), path,
154 last_index + range1) < 0)
159 /* just in case... */
160 if (range >= (1 << 30))
163 /* we are sure than image last_index + range exists */
168 *pfirst_index = first_index;
169 *plast_index = last_index;
176 static int read_probe(AVProbeData *p)
178 if (p->filename && av_str2id(img_tags, p->filename)) {
179 if (av_filename_number_test(p->filename))
180 return AVPROBE_SCORE_MAX;
182 return AVPROBE_SCORE_MAX/2;
187 enum CodecID ff_guess_image2_codec(const char *filename)
189 return av_str2id(img_tags, filename);
192 #if FF_API_GUESS_IMG2_CODEC
193 enum CodecID av_guess_image2_codec(const char *filename){
194 return av_str2id(img_tags, filename);
198 static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
200 VideoData *s = s1->priv_data;
201 int first_index, last_index;
204 s1->ctx_flags |= AVFMTCTX_NOHEADER;
206 st = av_new_stream(s1, 0);
208 return AVERROR(ENOMEM);
211 av_strlcpy(s->path, s1->filename, sizeof(s->path));
216 if (s1->iformat->flags & AVFMT_NOFILE)
220 st->need_parsing = AVSTREAM_PARSE_FULL;
223 if (!ap->time_base.num) {
224 av_set_pts_info(st, 60, 1, 25);
226 av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
229 if(ap->width && ap->height){
230 st->codec->width = ap->width;
231 st->codec->height= ap->height;
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 */
242 st->duration = last_index - first_index + 1;
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;
252 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
253 st->codec->codec_id = av_str2id(img_tags, s->path);
255 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
256 st->codec->pix_fmt = ap->pix_fmt;
261 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
263 VideoData *s = s1->priv_data;
266 int size[3]={0}, ret[3]={0};
268 AVCodecContext *codec= s1->streams[0]->codec;
271 /* loop over input */
272 if (s1->loop_input && s->img_number > s->img_last) {
273 s->img_number = s->img_first;
275 if (s->img_number > s->img_last)
277 if (av_get_frame_filename(filename, sizeof(filename),
278 s->path, s->img_number)<0 && s->img_number > 1)
281 if (avio_open(&f[i], filename, AVIO_FLAG_READ) < 0) {
284 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
287 size[i]= avio_size(f[i]);
289 if(codec->codec_id != CODEC_ID_RAWVIDEO)
291 filename[ strlen(filename) - 1 ]= 'U' + i;
294 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
295 infer_size(&codec->width, &codec->height, size[0]);
298 if (f[0]->eof_reached)
303 av_new_packet(pkt, size[0] + size[1] + size[2]);
304 pkt->stream_index = 0;
305 pkt->flags |= AV_PKT_FLAG_KEY;
310 ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
318 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
320 return AVERROR(EIO); /* signal EOF */
328 #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
329 /******************************************************/
332 static int write_header(AVFormatContext *s)
334 VideoData *img = s->priv_data;
337 av_strlcpy(img->path, s->filename, sizeof(img->path));
340 if (s->oformat->flags & AVFMT_NOFILE)
348 static int write_packet(AVFormatContext *s, AVPacket *pkt)
350 VideoData *img = s->priv_data;
353 AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
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);
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);
370 if(codec->codec_id != CODEC_ID_RAWVIDEO)
372 filename[ strlen(filename) - 1 ]= 'U' + i;
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);
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'))
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 ");
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
407 av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
411 avio_write(pb[0], pkt->data, pkt->size);
422 #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
425 #if CONFIG_IMAGE2_DEMUXER
426 AVInputFormat ff_image2_demuxer = {
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,
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,
447 #if CONFIG_IMAGE2_MUXER
448 AVOutputFormat ff_image2_muxer = {
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
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