3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/log.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/parseutils.h"
30 #include "avio_internal.h"
34 const AVClass *class; /**< Class for private options. */
40 int split_planes; /**< use independent file for each Y, U, V plane */
42 char *pixel_format; /**< Set by a private option. */
43 char *video_size; /**< Set by a private option. */
44 char *framerate; /**< Set by a private option. */
54 static const IdStrMap img_tags[] = {
55 { CODEC_ID_MJPEG , "jpeg"},
56 { CODEC_ID_MJPEG , "jpg"},
57 { CODEC_ID_MJPEG , "jps"},
58 { CODEC_ID_LJPEG , "ljpg"},
59 { CODEC_ID_JPEGLS , "jls"},
60 { CODEC_ID_PNG , "png"},
61 { CODEC_ID_PNG , "pns"},
62 { CODEC_ID_PNG , "mng"},
63 { CODEC_ID_PPM , "ppm"},
64 { CODEC_ID_PPM , "pnm"},
65 { CODEC_ID_PGM , "pgm"},
66 { CODEC_ID_PGMYUV , "pgmyuv"},
67 { CODEC_ID_PBM , "pbm"},
68 { CODEC_ID_PAM , "pam"},
69 { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
70 { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
71 { CODEC_ID_MPEG4 , "mpg4-img"},
72 { CODEC_ID_FFV1 , "ffv1-img"},
73 { CODEC_ID_RAWVIDEO , "y"},
74 { CODEC_ID_RAWVIDEO , "raw"},
75 { CODEC_ID_BMP , "bmp"},
76 { CODEC_ID_GIF , "gif"},
77 { CODEC_ID_TARGA , "tga"},
78 { CODEC_ID_TIFF , "tiff"},
79 { CODEC_ID_TIFF , "tif"},
80 { CODEC_ID_SGI , "sgi"},
81 { CODEC_ID_PTX , "ptx"},
82 { CODEC_ID_PCX , "pcx"},
83 { CODEC_ID_SUNRAST , "sun"},
84 { CODEC_ID_SUNRAST , "ras"},
85 { CODEC_ID_SUNRAST , "rs"},
86 { CODEC_ID_SUNRAST , "im1"},
87 { CODEC_ID_SUNRAST , "im8"},
88 { CODEC_ID_SUNRAST , "im24"},
89 { CODEC_ID_SUNRAST , "im32"},
90 { CODEC_ID_SUNRAST , "sunras"},
91 { CODEC_ID_JPEG2000 , "j2c"},
92 { CODEC_ID_JPEG2000 , "j2k"},
93 { CODEC_ID_JPEG2000 , "jp2"},
94 { CODEC_ID_JPEG2000 , "jpc"},
95 { CODEC_ID_DPX , "dpx"},
96 { CODEC_ID_PICTOR , "pic"},
97 { CODEC_ID_XWD , "xwd"},
98 { CODEC_ID_NONE , NULL}
101 static const int sizes[][2] = {
113 static int infer_size(int *width_ptr, int *height_ptr, int size)
117 for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
118 if ((sizes[i][0] * sizes[i][1]) == size) {
119 *width_ptr = sizes[i][0];
120 *height_ptr = sizes[i][1];
126 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
128 str= strrchr(str, '.');
129 if(!str) return CODEC_ID_NONE;
133 if (!av_strcasecmp(str, tags->str))
138 return CODEC_ID_NONE;
141 /* return -1 if no image found */
142 static int find_image_range(int *pfirst_index, int *plast_index,
146 int range, last_index, range1, first_index;
148 /* find the first image */
149 for(first_index = 0; first_index < 5; first_index++) {
150 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
153 if (avio_check(buf, AVIO_FLAG_READ) > 0)
157 if (avio_check(buf, AVIO_FLAG_READ) > 0)
160 if (first_index == 5)
163 /* find the last image */
164 last_index = first_index;
172 if (av_get_frame_filename(buf, sizeof(buf), path,
173 last_index + range1) < 0)
175 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
178 /* just in case... */
179 if (range >= (1 << 30))
182 /* we are sure than image last_index + range exists */
187 *pfirst_index = first_index;
188 *plast_index = last_index;
195 static int read_probe(AVProbeData *p)
197 if (p->filename && av_str2id(img_tags, p->filename)) {
198 if (av_filename_number_test(p->filename))
199 return AVPROBE_SCORE_MAX;
201 return AVPROBE_SCORE_MAX/2;
206 enum CodecID ff_guess_image2_codec(const char *filename)
208 return av_str2id(img_tags, filename);
211 static int read_header(AVFormatContext *s1)
213 VideoData *s = s1->priv_data;
214 int first_index, last_index, ret = 0;
215 int width = 0, height = 0;
217 enum PixelFormat pix_fmt = PIX_FMT_NONE;
218 AVRational framerate;
220 s1->ctx_flags |= AVFMTCTX_NOHEADER;
222 st = avformat_new_stream(s1, NULL);
224 return AVERROR(ENOMEM);
227 if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
228 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
229 return AVERROR(EINVAL);
231 if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
232 av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
235 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
236 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
240 av_strlcpy(s->path, s1->filename, sizeof(s->path));
245 if (s1->iformat->flags & AVFMT_NOFILE)
249 st->need_parsing = AVSTREAM_PARSE_FULL;
252 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
254 if (width && height) {
255 st->codec->width = width;
256 st->codec->height = height;
260 if (find_image_range(&first_index, &last_index, s->path) < 0)
261 return AVERROR(ENOENT);
262 s->img_first = first_index;
263 s->img_last = last_index;
264 s->img_number = first_index;
265 /* compute duration */
267 st->duration = last_index - first_index + 1;
270 if(s1->video_codec_id){
271 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
272 st->codec->codec_id = s1->video_codec_id;
273 }else if(s1->audio_codec_id){
274 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
275 st->codec->codec_id = s1->audio_codec_id;
277 const char *str= strrchr(s->path, '.');
278 s->split_planes = str && !av_strcasecmp(str + 1, "y");
279 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
280 st->codec->codec_id = av_str2id(img_tags, s->path);
281 if (st->codec->codec_id == CODEC_ID_LJPEG)
282 st->codec->codec_id = CODEC_ID_MJPEG;
284 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
285 st->codec->pix_fmt = pix_fmt;
290 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
292 VideoData *s = s1->priv_data;
295 int size[3]={0}, ret[3]={0};
297 AVCodecContext *codec= s1->streams[0]->codec;
300 /* loop over input */
301 if (s->loop && s->img_number > s->img_last) {
302 s->img_number = s->img_first;
304 if (s->img_number > s->img_last)
306 if (av_get_frame_filename(filename, sizeof(filename),
307 s->path, s->img_number)<0 && s->img_number > 1)
310 if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
311 &s1->interrupt_callback, NULL) < 0) {
314 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
317 size[i]= avio_size(f[i]);
321 filename[ strlen(filename) - 1 ]= 'U' + i;
324 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
325 infer_size(&codec->width, &codec->height, size[0]);
333 av_new_packet(pkt, size[0] + size[1] + size[2]);
334 pkt->stream_index = 0;
335 pkt->flags |= AV_PKT_FLAG_KEY;
340 ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
348 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
350 return AVERROR(EIO); /* signal EOF */
358 #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
359 /******************************************************/
362 static int write_header(AVFormatContext *s)
364 VideoData *img = s->priv_data;
368 av_strlcpy(img->path, s->filename, sizeof(img->path));
371 if (s->oformat->flags & AVFMT_NOFILE)
376 str = strrchr(img->path, '.');
377 img->split_planes = str && !av_strcasecmp(str + 1, "y");
381 static int write_packet(AVFormatContext *s, AVPacket *pkt)
383 VideoData *img = s->priv_data;
386 AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
390 if (av_get_frame_filename(filename, sizeof(filename),
391 img->path, img->img_number) < 0 && img->img_number>1 && !img->updatefirst) {
392 av_log(s, AV_LOG_ERROR,
393 "Could not get frame filename number %d from pattern '%s'\n",
394 img->img_number, img->path);
395 return AVERROR(EINVAL);
398 if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
399 &s->interrupt_callback, NULL) < 0) {
400 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
404 if(!img->split_planes)
406 filename[ strlen(filename) - 1 ]= 'U' + i;
412 if(img->split_planes){
413 int ysize = codec->width * codec->height;
414 avio_write(pb[0], pkt->data , ysize);
415 avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
416 avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
422 if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
423 AVStream *st = s->streams[0];
424 if(st->codec->extradata_size > 8 &&
425 AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
426 if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
428 avio_wb32(pb[0], 12);
429 ffio_wfourcc(pb[0], "jP ");
430 avio_wb32(pb[0], 0x0D0A870A); // signature
431 avio_wb32(pb[0], 20);
432 ffio_wfourcc(pb[0], "ftyp");
433 ffio_wfourcc(pb[0], "jp2 ");
435 ffio_wfourcc(pb[0], "jp2 ");
436 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
437 }else if(pkt->size >= 8 && AV_RB32(pkt->data) == 0xFF4FFF51){
438 //jpeg2000 codestream
439 }else if(pkt->size < 8 ||
440 (!st->codec->extradata_size &&
441 AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
443 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream %X\n", AV_RB32(pkt->data));
447 avio_write(pb[0], pkt->data, pkt->size);
458 #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
460 #define OFFSET(x) offsetof(VideoData, x)
461 #define DEC AV_OPT_FLAG_DECODING_PARAM
462 #define ENC AV_OPT_FLAG_ENCODING_PARAM
463 static const AVOption options[] = {
464 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
465 { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
466 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
467 { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
471 static const AVOption muxoptions[] = {
472 { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, ENC },
477 #if CONFIG_IMAGE2_DEMUXER
478 static const AVClass img2_class = {
479 .class_name = "image2 demuxer",
480 .item_name = av_default_item_name,
482 .version = LIBAVUTIL_VERSION_INT,
484 AVInputFormat ff_image2_demuxer = {
486 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
487 .priv_data_size = sizeof(VideoData),
488 .read_probe = read_probe,
489 .read_header = read_header,
490 .read_packet = read_packet,
491 .flags = AVFMT_NOFILE,
492 .priv_class = &img2_class,
495 #if CONFIG_IMAGE2PIPE_DEMUXER
496 static const AVClass img2pipe_class = {
497 .class_name = "image2pipe demuxer",
498 .item_name = av_default_item_name,
500 .version = LIBAVUTIL_VERSION_INT,
502 AVInputFormat ff_image2pipe_demuxer = {
503 .name = "image2pipe",
504 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
505 .priv_data_size = sizeof(VideoData),
506 .read_header = read_header,
507 .read_packet = read_packet,
508 .priv_class = &img2pipe_class,
513 #if CONFIG_IMAGE2_MUXER
514 static const AVClass img2mux_class = {
515 .class_name = "image2 muxer",
516 .item_name = av_default_item_name,
517 .option = muxoptions,
518 .version = LIBAVUTIL_VERSION_INT,
520 AVOutputFormat ff_image2_muxer = {
522 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
523 .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
524 "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd,sun,ras,rs,im1,im8,im24,"
526 .priv_data_size = sizeof(VideoData),
527 .video_codec = CODEC_ID_MJPEG,
528 .write_header = write_header,
529 .write_packet = write_packet,
530 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
531 .priv_class = &img2mux_class,
534 #if CONFIG_IMAGE2PIPE_MUXER
535 AVOutputFormat ff_image2pipe_muxer = {
536 .name = "image2pipe",
537 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
538 .priv_data_size = sizeof(VideoData),
539 .video_codec = CODEC_ID_MJPEG,
540 .write_header = write_header,
541 .write_packet = write_packet,
542 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS