XWD encoder and decoder
[libav.git] / libavformat / img2.c
CommitLineData
03cfe134
MN
1/*
2 * Image format
406792e7 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
03cfe134
MN
4 * Copyright (c) 2004 Michael Niedermayer
5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
03cfe134
MN
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
03cfe134 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
03cfe134
MN
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
03cfe134 21 */
245976da 22
c91662ce 23#include "libavutil/intreadwrite.h"
245976da 24#include "libavutil/avstring.h"
f33e2a51
AK
25#include "libavutil/log.h"
26#include "libavutil/opt.h"
27#include "libavutil/pixdesc.h"
a915bf64 28#include "libavutil/parseutils.h"
03cfe134 29#include "avformat.h"
0abdb293 30#include "avio_internal.h"
a9bf9d8e 31#include "internal.h"
03cfe134
MN
32
33typedef struct {
f33e2a51 34 const AVClass *class; /**< Class for private options. */
03cfe134
MN
35 int img_first;
36 int img_last;
37 int img_number;
38 int img_count;
39 int is_pipe;
40 char path[1024];
f33e2a51 41 char *pixel_format; /**< Set by a private option. */
a915bf64 42 char *video_size; /**< Set by a private option. */
abcedfac 43 char *framerate; /**< Set by a private option. */
6002fdef 44 int loop;
03cfe134
MN
45} VideoData;
46
47typedef struct {
48 enum CodecID id;
49 const char *str;
50} IdStrMap;
51
52static const IdStrMap img_tags[] = {
53 { CODEC_ID_MJPEG , "jpeg"},
54 { CODEC_ID_MJPEG , "jpg"},
a481293f 55 { CODEC_ID_LJPEG , "ljpg"},
4eff7cf4 56 { CODEC_ID_PNG , "png"},
042e0add 57 { CODEC_ID_PNG , "mng"},
5b6d5596 58 { CODEC_ID_PPM , "ppm"},
485cfbe8 59 { CODEC_ID_PPM , "pnm"},
5b6d5596
MN
60 { CODEC_ID_PGM , "pgm"},
61 { CODEC_ID_PGMYUV , "pgmyuv"},
62 { CODEC_ID_PBM , "pbm"},
63 { CODEC_ID_PAM , "pam"},
03cfe134
MN
64 { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
65 { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
66 { CODEC_ID_MPEG4 , "mpg4-img"},
67 { CODEC_ID_FFV1 , "ffv1-img"},
55cf1959 68 { CODEC_ID_RAWVIDEO , "y"},
9fa62f2a 69 { CODEC_ID_BMP , "bmp"},
6f9e492a 70 { CODEC_ID_GIF , "gif"},
79f7c32f
KS
71 { CODEC_ID_TARGA , "tga"},
72 { CODEC_ID_TIFF , "tiff"},
dd0f776c 73 { CODEC_ID_TIFF , "tif"},
2d99eed1 74 { CODEC_ID_SGI , "sgi"},
8d2fb333 75 { CODEC_ID_PTX , "ptx"},
b4abe1d1 76 { CODEC_ID_PCX , "pcx"},
d43df959
IP
77 { CODEC_ID_SUNRAST , "sun"},
78 { CODEC_ID_SUNRAST , "ras"},
79 { CODEC_ID_SUNRAST , "rs"},
80 { CODEC_ID_SUNRAST , "im1"},
81 { CODEC_ID_SUNRAST , "im8"},
82 { CODEC_ID_SUNRAST , "im24"},
83 { CODEC_ID_SUNRAST , "sunras"},
8b2386dc 84 { CODEC_ID_JPEG2000 , "jp2"},
a304a833 85 { CODEC_ID_JPEG2000 , "jpc"},
94d3d6a4 86 { CODEC_ID_DPX , "dpx"},
971c3c98 87 { CODEC_ID_PICTOR , "pic"},
27ed027b 88 { CODEC_ID_XWD , "xwd"},
d9133126 89 { CODEC_ID_NONE , NULL}
03cfe134
MN
90};
91
aecf157e 92static const int sizes[][2] = {
55cf1959
MN
93 { 640, 480 },
94 { 720, 480 },
95 { 720, 576 },
96 { 352, 288 },
97 { 352, 240 },
98 { 160, 128 },
99 { 512, 384 },
100 { 640, 352 },
101 { 640, 240 },
102};
103
104static int infer_size(int *width_ptr, int *height_ptr, int size)
105{
106 int i;
107
37d3e066 108 for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
55cf1959
MN
109 if ((sizes[i][0] * sizes[i][1]) == size) {
110 *width_ptr = sizes[i][0];
111 *height_ptr = sizes[i][1];
112 return 0;
113 }
114 }
115 return -1;
116}
03cfe134
MN
117static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
118{
3ed02129
MN
119 str= strrchr(str, '.');
120 if(!str) return CODEC_ID_NONE;
121 str++;
03cfe134
MN
122
123 while (tags->id) {
bb3244de 124 if (!av_strcasecmp(str, tags->str))
3375a6a5 125 return tags->id;
03cfe134
MN
126
127 tags++;
128 }
129 return CODEC_ID_NONE;
130}
131
03cfe134 132/* return -1 if no image found */
115329f1 133static int find_image_range(int *pfirst_index, int *plast_index,
03cfe134
MN
134 const char *path)
135{
136 char buf[1024];
137 int range, last_index, range1, first_index;
138
139 /* find the first image */
140 for(first_index = 0; first_index < 5; first_index++) {
5c07cf53 141 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
115329f1 142 *pfirst_index =
61fb3183 143 *plast_index = 1;
55815edc 144 if (avio_check(buf, AVIO_FLAG_READ) > 0)
c671ac40
SV
145 return 0;
146 return -1;
61fb3183 147 }
55815edc 148 if (avio_check(buf, AVIO_FLAG_READ) > 0)
03cfe134
MN
149 break;
150 }
151 if (first_index == 5)
152 goto fail;
115329f1 153
03cfe134
MN
154 /* find the last image */
155 last_index = first_index;
156 for(;;) {
157 range = 0;
158 for(;;) {
159 if (!range)
160 range1 = 1;
161 else
162 range1 = 2 * range;
5c07cf53
MB
163 if (av_get_frame_filename(buf, sizeof(buf), path,
164 last_index + range1) < 0)
03cfe134 165 goto fail;
55815edc 166 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
03cfe134
MN
167 break;
168 range = range1;
169 /* just in case... */
170 if (range >= (1 << 30))
171 goto fail;
172 }
173 /* we are sure than image last_index + range exists */
174 if (!range)
175 break;
176 last_index += range;
177 }
178 *pfirst_index = first_index;
179 *plast_index = last_index;
180 return 0;
181 fail:
182 return -1;
183}
184
185
ac6d020f 186static int read_probe(AVProbeData *p)
03cfe134 187{
e825b500 188 if (p->filename && av_str2id(img_tags, p->filename)) {
d1e8675c
BC
189 if (av_filename_number_test(p->filename))
190 return AVPROBE_SCORE_MAX;
191 else
192 return AVPROBE_SCORE_MAX/2;
193 }
194 return 0;
03cfe134
MN
195}
196
a9bf9d8e
AK
197enum CodecID ff_guess_image2_codec(const char *filename)
198{
199 return av_str2id(img_tags, filename);
200}
201
202#if FF_API_GUESS_IMG2_CODEC
5b6d5596
MN
203enum CodecID av_guess_image2_codec(const char *filename){
204 return av_str2id(img_tags, filename);
205}
a9bf9d8e 206#endif
5b6d5596 207
ac6d020f 208static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
03cfe134
MN
209{
210 VideoData *s = s1->priv_data;
a915bf64
AK
211 int first_index, last_index, ret = 0;
212 int width = 0, height = 0;
03cfe134 213 AVStream *st;
f33e2a51 214 enum PixelFormat pix_fmt = PIX_FMT_NONE;
abcedfac 215 AVRational framerate;
03cfe134
MN
216
217 s1->ctx_flags |= AVFMTCTX_NOHEADER;
218
3b3bbdd3 219 st = avformat_new_stream(s1, NULL);
03cfe134 220 if (!st) {
8fa36ae0 221 return AVERROR(ENOMEM);
03cfe134
MN
222 }
223
f33e2a51
AK
224 if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
225 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
226 return AVERROR(EINVAL);
227 }
a915bf64
AK
228 if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
229 av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
230 return ret;
231 }
abcedfac
AK
232 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
233 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
234 return ret;
235 }
f33e2a51 236
6002fdef
AK
237#if FF_API_LOOP_INPUT
238 if (s1->loop_input)
239 s->loop = s1->loop_input;
240#endif
241
75e61b0e 242 av_strlcpy(s->path, s1->filename, sizeof(s->path));
03cfe134
MN
243 s->img_number = 0;
244 s->img_count = 0;
115329f1 245
03cfe134
MN
246 /* find format */
247 if (s1->iformat->flags & AVFMT_NOFILE)
248 s->is_pipe = 0;
146210ca 249 else{
03cfe134 250 s->is_pipe = 1;
57004ff1 251 st->need_parsing = AVSTREAM_PARSE_FULL;
146210ca 252 }
115329f1 253
c3f9ebf7 254 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
115329f1 255
a915bf64
AK
256 if (width && height) {
257 st->codec->width = width;
258 st->codec->height = height;
55cf1959 259 }
115329f1 260
03cfe134
MN
261 if (!s->is_pipe) {
262 if (find_image_range(&first_index, &last_index, s->path) < 0)
c671ac40 263 return AVERROR(ENOENT);
03cfe134
MN
264 s->img_first = first_index;
265 s->img_last = last_index;
266 s->img_number = first_index;
267 /* compute duration */
268 st->start_time = 0;
c0df9d75 269 st->duration = last_index - first_index + 1;
03cfe134 270 }
115329f1 271
90d0379f 272 if(s1->video_codec_id){
72415b2a 273 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
90d0379f
JM
274 st->codec->codec_id = s1->video_codec_id;
275 }else if(s1->audio_codec_id){
72415b2a 276 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
90d0379f 277 st->codec->codec_id = s1->audio_codec_id;
5b6d5596 278 }else{
72415b2a 279 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 280 st->codec->codec_id = av_str2id(img_tags, s->path);
5b6d5596 281 }
f33e2a51
AK
282 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
283 st->codec->pix_fmt = pix_fmt;
03cfe134
MN
284
285 return 0;
03cfe134
MN
286}
287
ac6d020f 288static int read_packet(AVFormatContext *s1, AVPacket *pkt)
03cfe134
MN
289{
290 VideoData *s = s1->priv_data;
291 char filename[1024];
55cf1959
MN
292 int i;
293 int size[3]={0}, ret[3]={0};
ae628ec1 294 AVIOContext *f[3];
01f4895c 295 AVCodecContext *codec= s1->streams[0]->codec;
03cfe134
MN
296
297 if (!s->is_pipe) {
298 /* loop over input */
6002fdef 299 if (s->loop && s->img_number > s->img_last) {
03cfe134 300 s->img_number = s->img_first;
b339fb00 301 }
e4b8d05d
BC
302 if (s->img_number > s->img_last)
303 return AVERROR_EOF;
5c07cf53
MB
304 if (av_get_frame_filename(filename, sizeof(filename),
305 s->path, s->img_number)<0 && s->img_number > 1)
6f3e0b21 306 return AVERROR(EIO);
55cf1959 307 for(i=0; i<3; i++){
9d77a8fa
MS
308 if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
309 &s1->interrupt_callback, NULL) < 0) {
c5113129
MN
310 if(i==1)
311 break;
9ed3afcb 312 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
6f3e0b21 313 return AVERROR(EIO);
9ed3afcb 314 }
76aa876e 315 size[i]= avio_size(f[i]);
115329f1 316
55cf1959
MN
317 if(codec->codec_id != CODEC_ID_RAWVIDEO)
318 break;
319 filename[ strlen(filename) - 1 ]= 'U' + i;
320 }
115329f1 321
55cf1959
MN
322 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
323 infer_size(&codec->width, &codec->height, size[0]);
03cfe134 324 } else {
899681cd 325 f[0] = s1->pb;
66e5b1df 326 if (f[0]->eof_reached)
6f3e0b21 327 return AVERROR(EIO);
55cf1959 328 size[0]= 4096;
03cfe134
MN
329 }
330
55cf1959 331 av_new_packet(pkt, size[0] + size[1] + size[2]);
03cfe134 332 pkt->stream_index = 0;
cc947f04 333 pkt->flags |= AV_PKT_FLAG_KEY;
03cfe134 334
55cf1959
MN
335 pkt->size= 0;
336 for(i=0; i<3; i++){
337 if(size[i]){
b7effd4e 338 ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
55cf1959 339 if (!s->is_pipe)
22a3212e 340 avio_close(f[i]);
55cf1959
MN
341 if(ret[i]>0)
342 pkt->size += ret[i];
343 }
03cfe134
MN
344 }
345
55cf1959 346 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
03cfe134 347 av_free_packet(pkt);
6f3e0b21 348 return AVERROR(EIO); /* signal EOF */
03cfe134
MN
349 } else {
350 s->img_count++;
351 s->img_number++;
352 return 0;
353 }
354}
355
b250f9c6 356#if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
03cfe134
MN
357/******************************************************/
358/* image output */
359
ac6d020f 360static int write_header(AVFormatContext *s)
03cfe134
MN
361{
362 VideoData *img = s->priv_data;
363
364 img->img_number = 1;
75e61b0e 365 av_strlcpy(img->path, s->filename, sizeof(img->path));
03cfe134
MN
366
367 /* find format */
368 if (s->oformat->flags & AVFMT_NOFILE)
369 img->is_pipe = 0;
370 else
371 img->is_pipe = 1;
115329f1 372
03cfe134
MN
373 return 0;
374}
375
ac6d020f 376static int write_packet(AVFormatContext *s, AVPacket *pkt)
03cfe134
MN
377{
378 VideoData *img = s->priv_data;
ae628ec1 379 AVIOContext *pb[3];
03cfe134 380 char filename[1024];
01f4895c 381 AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
55cf1959 382 int i;
03cfe134
MN
383
384 if (!img->is_pipe) {
5c07cf53 385 if (av_get_frame_filename(filename, sizeof(filename),
77df894a 386 img->path, img->img_number) < 0 && img->img_number>1) {
4fc9ff0a
SS
387 av_log(s, AV_LOG_ERROR,
388 "Could not get frame filename number %d from pattern '%s'\n",
389 img->img_number, img->path);
6f3e0b21 390 return AVERROR(EIO);
77df894a 391 }
55cf1959 392 for(i=0; i<3; i++){
9d77a8fa
MS
393 if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
394 &s->interrupt_callback, NULL) < 0) {
9ed3afcb 395 av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
6f3e0b21 396 return AVERROR(EIO);
9ed3afcb 397 }
115329f1 398
55cf1959
MN
399 if(codec->codec_id != CODEC_ID_RAWVIDEO)
400 break;
401 filename[ strlen(filename) - 1 ]= 'U' + i;
402 }
03cfe134 403 } else {
899681cd 404 pb[0] = s->pb;
03cfe134 405 }
115329f1 406
55cf1959 407 if(codec->codec_id == CODEC_ID_RAWVIDEO){
2c34596f 408 int ysize = codec->width * codec->height;
77eb5504
AK
409 avio_write(pb[0], pkt->data , ysize);
410 avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
411 avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
b7f2fdde
AK
412 avio_flush(pb[1]);
413 avio_flush(pb[2]);
22a3212e
AK
414 avio_close(pb[1]);
415 avio_close(pb[2]);
55cf1959 416 }else{
b4097b13
BC
417 if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
418 AVStream *st = s->streams[0];
419 if(st->codec->extradata_size > 8 &&
420 AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
421 if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
422 goto error;
77eb5504 423 avio_wb32(pb[0], 12);
0abdb293 424 ffio_wfourcc(pb[0], "jP ");
77eb5504
AK
425 avio_wb32(pb[0], 0x0D0A870A); // signature
426 avio_wb32(pb[0], 20);
0abdb293
AK
427 ffio_wfourcc(pb[0], "ftyp");
428 ffio_wfourcc(pb[0], "jp2 ");
77eb5504 429 avio_wb32(pb[0], 0);
0abdb293 430 ffio_wfourcc(pb[0], "jp2 ");
77eb5504 431 avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
b4097b13
BC
432 }else if(pkt->size < 8 ||
433 (!st->codec->extradata_size &&
434 AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
435 error:
da9cea77 436 av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream\n");
b4097b13
BC
437 return -1;
438 }
439 }
77eb5504 440 avio_write(pb[0], pkt->data, pkt->size);
55cf1959 441 }
b7f2fdde 442 avio_flush(pb[0]);
03cfe134 443 if (!img->is_pipe) {
22a3212e 444 avio_close(pb[0]);
03cfe134
MN
445 }
446
447 img->img_number++;
448 return 0;
449}
450
b250f9c6 451#endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
8228bff5 452
f33e2a51
AK
453#define OFFSET(x) offsetof(VideoData, x)
454#define DEC AV_OPT_FLAG_DECODING_PARAM
455static const AVOption options[] = {
145f741e
AK
456 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
457 { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
458 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
459 { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
f33e2a51
AK
460 { NULL },
461};
462
a4ea00d0
AK
463/* input */
464#if CONFIG_IMAGE2_DEMUXER
f33e2a51
AK
465static const AVClass img2_class = {
466 .class_name = "image2 demuxer",
467 .item_name = av_default_item_name,
468 .option = options,
469 .version = LIBAVUTIL_VERSION_INT,
470};
c6610a21 471AVInputFormat ff_image2_demuxer = {
85f56757
PM
472 .name = "image2",
473 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
ac6d020f 474 .priv_data_size = sizeof(VideoData),
85f56757
PM
475 .read_probe = read_probe,
476 .read_header = read_header,
477 .read_packet = read_packet,
478 .flags = AVFMT_NOFILE,
f33e2a51 479 .priv_class = &img2_class,
03cfe134 480};
ff70e601 481#endif
b250f9c6 482#if CONFIG_IMAGE2PIPE_DEMUXER
a4ea00d0
AK
483static const AVClass img2pipe_class = {
484 .class_name = "image2pipe demuxer",
485 .item_name = av_default_item_name,
486 .option = options,
487 .version = LIBAVUTIL_VERSION_INT,
488};
c6610a21 489AVInputFormat ff_image2pipe_demuxer = {
85f56757
PM
490 .name = "image2pipe",
491 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
ac6d020f 492 .priv_data_size = sizeof(VideoData),
85f56757
PM
493 .read_header = read_header,
494 .read_packet = read_packet,
a4ea00d0 495 .priv_class = &img2pipe_class,
03cfe134 496};
ff70e601 497#endif
03cfe134 498
03cfe134 499/* output */
b250f9c6 500#if CONFIG_IMAGE2_MUXER
c6610a21 501AVOutputFormat ff_image2_muxer = {
85f56757
PM
502 .name = "image2",
503 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
a31d4b3a 504 .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
27ed027b 505 "ppm,sgi,tga,tif,tiff,jp2,xwd",
ac6d020f 506 .priv_data_size = sizeof(VideoData),
85f56757
PM
507 .video_codec = CODEC_ID_MJPEG,
508 .write_header = write_header,
509 .write_packet = write_packet,
510 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
03cfe134 511};
ff70e601 512#endif
b250f9c6 513#if CONFIG_IMAGE2PIPE_MUXER
c6610a21 514AVOutputFormat ff_image2pipe_muxer = {
85f56757
PM
515 .name = "image2pipe",
516 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
ac6d020f 517 .priv_data_size = sizeof(VideoData),
85f56757
PM
518 .video_codec = CODEC_ID_MJPEG,
519 .write_header = write_header,
520 .write_packet = write_packet,
521 .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
03cfe134 522};
8228bff5 523#endif