win32 fixes
[libav.git] / libav / img.c
1 /*
2 * Image format
3 * Copyright (c) 2000, 2001 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "avformat.h"
20
21 #define IMGFMT_YUV 1
22 #define IMGFMT_PGMYUV 2
23 #define IMGFMT_PGM 3
24 #define IMGFMT_PPM 4
25
26 typedef struct {
27 int width;
28 int height;
29 int img_number;
30 int img_size;
31 int img_fmt;
32 int is_pipe;
33 char path[1024];
34 } VideoData;
35
36 static inline int pnm_space(int c)
37 {
38 return (c==' ' || c=='\n' || c=='\r' || c=='\t');
39 }
40
41 static void pnm_get(ByteIOContext *f, char *str, int buf_size)
42 {
43 char *s;
44 int c;
45
46 do {
47 c=get_byte(f);
48 if (c=='#') {
49 do {
50 c=get_byte(f);
51 } while (c!='\n');
52 c=get_byte(f);
53 }
54 } while (pnm_space(c));
55
56 s=str;
57 do {
58 if (url_feof(f))
59 break;
60 if ((s - str) < buf_size - 1)
61 *s++=c;
62 c=get_byte(f);
63 } while (!pnm_space(c));
64 *s = '\0';
65 }
66
67 static int pgm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size, int is_yuv)
68 {
69 int width, height, i;
70 char buf1[32];
71 UINT8 *picture[3];
72
73 width = s->width;
74 height = s->height;
75
76 pnm_get(f, buf1, sizeof(buf1));
77 if (strcmp(buf1, "P5")) {
78 return -EIO;
79 }
80 pnm_get(f, buf1, sizeof(buf1));
81 pnm_get(f, buf1, sizeof(buf1));
82 pnm_get(f, buf1, sizeof(buf1));
83
84 picture[0] = buf;
85 picture[1] = buf + width * height;
86 picture[2] = buf + width * height + (width * height / 4);
87 get_buffer(f, picture[0], width * height);
88
89 height>>=1;
90 width>>=1;
91 if (is_yuv) {
92 for(i=0;i<height;i++) {
93 get_buffer(f, picture[1] + i * width, width);
94 get_buffer(f, picture[2] + i * width, width);
95 }
96 } else {
97 for(i=0;i<height;i++) {
98 memset(picture[1] + i * width, 128, width);
99 memset(picture[2] + i * width, 128, width);
100 }
101 }
102 return 0;
103 }
104
105 static int ppm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size)
106 {
107 int width, height;
108 char buf1[32];
109 UINT8 *picture[3];
110
111 width = s->width;
112 height = s->height;
113
114 pnm_get(f, buf1, sizeof(buf1));
115 if (strcmp(buf1, "P6")) {
116 return -EIO;
117 }
118
119 pnm_get(f, buf1, sizeof(buf1));
120 pnm_get(f, buf1, sizeof(buf1));
121 pnm_get(f, buf1, sizeof(buf1));
122
123 picture[0] = buf;
124 get_buffer(f, picture[0], width * height*3);
125
126 return 0;
127
128 }
129
130 static int yuv_read(VideoData *s, const char *filename, UINT8 *buf, int size1)
131 {
132 ByteIOContext pb1, *pb = &pb1;
133 char fname[1024], *p;
134 int size;
135
136 size = s->width * s->height;
137
138 strcpy(fname, filename);
139 p = strrchr(fname, '.');
140 if (!p || p[1] != 'Y')
141 return -EIO;
142
143 if (url_fopen(pb, fname, URL_RDONLY) < 0)
144 return -EIO;
145
146 get_buffer(pb, buf, size);
147 url_fclose(pb);
148
149 p[1] = 'U';
150 if (url_fopen(pb, fname, URL_RDONLY) < 0)
151 return -EIO;
152
153 get_buffer(pb, buf + size, size / 4);
154 url_fclose(pb);
155
156 p[1] = 'V';
157 if (url_fopen(pb, fname, URL_RDONLY) < 0)
158 return -EIO;
159
160 get_buffer(pb, buf + size + (size / 4), size / 4);
161 url_fclose(pb);
162 return 0;
163 }
164
165 int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
166 {
167 VideoData *s = s1->priv_data;
168 char filename[1024];
169 int ret;
170 ByteIOContext f1, *f;
171
172 snprintf(filename, sizeof(filename), s->path, s->img_number);
173
174 if (!s->is_pipe) {
175 f = &f1;
176 if (url_fopen(f, filename, URL_RDONLY) < 0)
177 return -EIO;
178 } else {
179 f = &s1->pb;
180 if (url_feof(f))
181 return -EIO;
182 }
183
184 av_new_packet(pkt, s->img_size);
185 pkt->stream_index = 0;
186
187 switch(s->img_fmt) {
188 case IMGFMT_PGMYUV:
189 ret = pgm_read(s, f, pkt->data, pkt->size, 1);
190 break;
191 case IMGFMT_PGM:
192 ret = pgm_read(s, f, pkt->data, pkt->size, 0);
193 break;
194 case IMGFMT_YUV:
195 ret = yuv_read(s, filename, pkt->data, pkt->size);
196 break;
197 case IMGFMT_PPM:
198 ret = ppm_read(s, f, pkt->data, pkt->size);
199 break;
200 default:
201 return -EIO;
202 }
203
204 if (!s->is_pipe) {
205 url_fclose(f);
206 }
207
208 if (ret < 0) {
209 av_free_packet(pkt);
210 return -EIO; /* signal EOF */
211 } else {
212 s->img_number++;
213 return 0;
214 }
215 }
216
217 static int sizes[][2] = {
218 { 640, 480 },
219 { 720, 480 },
220 { 720, 576 },
221 { 352, 288 },
222 { 352, 240 },
223 { 160, 128 },
224 { 512, 384 },
225 { 640, 352 },
226 };
227
228 static int infer_size(int *width_ptr, int *height_ptr, int size)
229 {
230 int i;
231
232 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
233 if ((sizes[i][0] * sizes[i][1]) == size) {
234 *width_ptr = sizes[i][0];
235 *height_ptr = sizes[i][1];
236 return 0;
237 }
238 }
239 return -1;
240 }
241
242 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
243 {
244 VideoData *s;
245 int i, h;
246 char buf[1024];
247 char buf1[32];
248 ByteIOContext pb1, *f = &pb1;
249 AVStream *st;
250
251 s = malloc(sizeof(VideoData));
252 if (!s)
253 return -ENOMEM;
254
255 s1->priv_data = s;
256
257 s1->nb_streams = 1;
258 st = av_mallocz(sizeof(AVStream));
259 if (!st) {
260 free(s);
261 return -ENOMEM;
262 }
263 s1->streams[0] = st;
264
265 strcpy(s->path, s1->filename);
266 s->img_number = 0;
267
268 /* find format */
269 if (s1->format->flags & AVFMT_NOFILE)
270 s->is_pipe = 0;
271 else
272 s->is_pipe = 1;
273
274 if (s1->format == &pgmyuvpipe_format ||
275 s1->format == &pgmyuv_format)
276 s->img_fmt = IMGFMT_PGMYUV;
277 else if (s1->format == &pgmpipe_format ||
278 s1->format == &pgm_format)
279 s->img_fmt = IMGFMT_PGM;
280 else if (s1->format == &imgyuv_format)
281 s->img_fmt = IMGFMT_YUV;
282 else if (s1->format == &ppmpipe_format ||
283 s1->format == &ppm_format)
284 s->img_fmt = IMGFMT_PPM;
285 else
286 goto fail;
287
288 if (!s->is_pipe) {
289 /* try to find the first image */
290 for(i=0;i<5;i++) {
291 snprintf(buf, sizeof(buf), s->path, s->img_number);
292 if (url_fopen(f, buf, URL_RDONLY) >= 0)
293 break;
294 s->img_number++;
295 }
296 if (i == 5)
297 goto fail;
298 } else {
299 f = &s1->pb;
300 }
301
302 /* find the image size */
303 /* XXX: use generic file format guessing, as mpeg */
304 switch(s->img_fmt) {
305 case IMGFMT_PGM:
306 case IMGFMT_PGMYUV:
307 case IMGFMT_PPM:
308 pnm_get(f, buf1, sizeof(buf1));
309 pnm_get(f, buf1, sizeof(buf1));
310 s->width = atoi(buf1);
311 pnm_get(f, buf1, sizeof(buf1));
312 h = atoi(buf1);
313 if (s->img_fmt == IMGFMT_PGMYUV)
314 h = (h * 2) / 3;
315 s->height = h;
316 if (s->width <= 0 ||
317 s->height <= 0 ||
318 (s->width % 2) != 0 ||
319 (s->height % 2) != 0) {
320 goto fail1;
321 }
322 break;
323 case IMGFMT_YUV:
324 /* infer size by using the file size. */
325 {
326 int img_size;
327 URLContext *h;
328
329 /* XXX: hack hack */
330 h = url_fileno(f);
331 img_size = url_seek(h, 0, SEEK_END);
332 if (infer_size(&s->width, &s->height, img_size) < 0) {
333 goto fail1;
334 }
335 }
336 break;
337 }
338
339 if (!s->is_pipe) {
340 url_fclose(f);
341 } else {
342 url_fseek(f, 0, SEEK_SET);
343 }
344
345
346 st->codec.codec_type = CODEC_TYPE_VIDEO;
347 st->codec.codec_id = CODEC_ID_RAWVIDEO;
348 st->codec.width = s->width;
349 st->codec.height = s->height;
350 if (s->img_fmt == IMGFMT_PPM) {
351 st->codec.pix_fmt = PIX_FMT_RGB24;
352 s->img_size = (s->width * s->height * 3);
353 } else {
354 st->codec.pix_fmt = PIX_FMT_YUV420P;
355 s->img_size = (s->width * s->height * 3) / 2;
356 }
357 if (!ap || !ap->frame_rate)
358 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
359 else
360 st->codec.frame_rate = ap->frame_rate;
361
362 return 0;
363 fail1:
364 if (!s->is_pipe)
365 url_fclose(f);
366 fail:
367 free(s);
368 return -EIO;
369 }
370
371 static int img_read_close(AVFormatContext *s1)
372 {
373 VideoData *s = s1->priv_data;
374 free(s);
375 return 0;
376 }
377
378 /******************************************************/
379 /* image output */
380
381 static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv)
382 {
383 int i, h;
384 char buf[100];
385 UINT8 *ptr, *ptr1, *ptr2;
386
387 h = height;
388 if (is_yuv)
389 h = (height * 3) / 2;
390 snprintf(buf, sizeof(buf),
391 "P5\n%d %d\n%d\n",
392 width, h, 255);
393 put_buffer(pb, buf, strlen(buf));
394
395 ptr = picture->data[0];
396 for(i=0;i<height;i++) {
397 put_buffer(pb, ptr, width);
398 ptr += picture->linesize[0];
399 }
400
401 if (is_yuv) {
402 height >>= 1;
403 width >>= 1;
404 ptr1 = picture->data[1];
405 ptr2 = picture->data[2];
406 for(i=0;i<height;i++) {
407 put_buffer(pb, ptr1, width);
408 put_buffer(pb, ptr2, width);
409 ptr1 += picture->linesize[1];
410 ptr2 += picture->linesize[2];
411 }
412 }
413 put_flush_packet(pb);
414 return 0;
415 }
416
417 static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb)
418 {
419 int i;
420 char buf[100];
421 UINT8 *ptr;
422
423 snprintf(buf, sizeof(buf),
424 "P6\n%d %d\n%d\n",
425 width, height, 255);
426 put_buffer(pb, buf, strlen(buf));
427
428 ptr = picture->data[0];
429 for(i=0;i<height;i++) {
430 put_buffer(pb, ptr, width * 3);
431 ptr += picture->linesize[0];
432 }
433
434 put_flush_packet(pb);
435 return 0;
436 }
437
438 static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
439 {
440 ByteIOContext pb1, *pb = &pb1;
441 char fname[1024], *p;
442 int i, j;
443 UINT8 *ptr;
444 static char *ext = "YUV";
445
446 strcpy(fname, filename);
447 p = strrchr(fname, '.');
448 if (!p || p[1] != 'Y')
449 return -EIO;
450
451 for(i=0;i<3;i++) {
452 if (i == 1) {
453 width >>= 1;
454 height >>= 1;
455 }
456
457 p[1] = ext[i];
458 if (url_fopen(pb, fname, URL_WRONLY) < 0)
459 return -EIO;
460
461 ptr = picture->data[i];
462 for(j=0;j<height;j++) {
463 put_buffer(pb, ptr, width);
464 ptr += picture->linesize[i];
465 }
466 put_flush_packet(pb);
467 url_fclose(pb);
468 }
469 return 0;
470 }
471
472 static int img_write_header(AVFormatContext *s)
473 {
474 VideoData *img;
475
476 img = av_mallocz(sizeof(VideoData));
477 if (!img)
478 return -1;
479 s->priv_data = img;
480 img->img_number = 1;
481 strcpy(img->path, s->filename);
482
483 /* find format */
484 if (s->format->flags & AVFMT_NOFILE)
485 img->is_pipe = 0;
486 else
487 img->is_pipe = 1;
488
489 if (s->format == &pgmyuvpipe_format ||
490 s->format == &pgmyuv_format) {
491 img->img_fmt = IMGFMT_PGMYUV;
492 } else if (s->format == &pgmpipe_format ||
493 s->format == &pgm_format) {
494 img->img_fmt = IMGFMT_PGM;
495 } else if (s->format == &imgyuv_format) {
496 img->img_fmt = IMGFMT_YUV;
497 } else if (s->format == &ppmpipe_format ||
498 s->format == &ppm_format) {
499 img->img_fmt = IMGFMT_PPM;
500 } else {
501 goto fail;
502 }
503 return 0;
504 fail:
505 free(img);
506 return -EIO;
507 }
508
509 static int img_write_packet(AVFormatContext *s, int stream_index,
510 UINT8 *buf, int size)
511 {
512 VideoData *img = s->priv_data;
513 AVStream *st = s->streams[stream_index];
514 ByteIOContext pb1, *pb;
515 AVPicture picture;
516 int width, height, ret, size1;
517 char filename[1024];
518
519 width = st->codec.width;
520 height = st->codec.height;
521
522 switch(st->codec.pix_fmt) {
523 case PIX_FMT_YUV420P:
524 size1 = (width * height * 3) / 2;
525 if (size != size1)
526 return -EIO;
527
528 picture.data[0] = buf;
529 picture.data[1] = picture.data[0] + width * height;
530 picture.data[2] = picture.data[1] + (width * height) / 4;
531 picture.linesize[0] = width;
532 picture.linesize[1] = width >> 1;
533 picture.linesize[2] = width >> 1;
534 break;
535 case PIX_FMT_RGB24:
536 size1 = (width * height * 3);
537 if (size != size1)
538 return -EIO;
539 picture.data[0] = buf;
540 picture.linesize[0] = width * 3;
541 break;
542 default:
543 return -EIO;
544 }
545
546 snprintf(filename, sizeof(filename), img->path, img->img_number);
547
548 if (!img->is_pipe) {
549 pb = &pb1;
550 if (url_fopen(pb, filename, URL_WRONLY) < 0)
551 return -EIO;
552 } else {
553 pb = &s->pb;
554 }
555 switch(img->img_fmt) {
556 case IMGFMT_PGMYUV:
557 ret = pgm_save(&picture, width, height, pb, 1);
558 break;
559 case IMGFMT_PGM:
560 ret = pgm_save(&picture, width, height, pb, 0);
561 break;
562 case IMGFMT_YUV:
563 ret = yuv_save(&picture, width, height, filename);
564 break;
565 case IMGFMT_PPM:
566 ret = ppm_save(&picture, width, height, pb);
567 break;
568 }
569 if (!img->is_pipe) {
570 url_fclose(pb);
571 }
572
573 img->img_number++;
574 return 0;
575 }
576
577 static int img_write_trailer(AVFormatContext *s)
578 {
579 VideoData *img = s->priv_data;
580 free(img);
581 return 0;
582 }
583
584 AVFormat pgm_format = {
585 "pgm",
586 "pgm image format",
587 "",
588 "pgm",
589 CODEC_ID_NONE,
590 CODEC_ID_RAWVIDEO,
591 img_write_header,
592 img_write_packet,
593 img_write_trailer,
594
595 img_read_header,
596 img_read_packet,
597 img_read_close,
598 NULL,
599 AVFMT_NOFILE,
600 };
601
602 AVFormat pgmyuv_format = {
603 "pgmyuv",
604 "pgm with YUV content image format",
605 "",
606 "pgm",
607 CODEC_ID_NONE,
608 CODEC_ID_RAWVIDEO,
609 img_write_header,
610 img_write_packet,
611 img_write_trailer,
612
613 img_read_header,
614 img_read_packet,
615 img_read_close,
616 NULL,
617 AVFMT_NOFILE,
618 };
619
620 AVFormat ppm_format = {
621 "ppm",
622 "ppm image format",
623 "",
624 "ppm",
625 CODEC_ID_NONE,
626 CODEC_ID_RAWVIDEO,
627 img_write_header,
628 img_write_packet,
629 img_write_trailer,
630
631 img_read_header,
632 img_read_packet,
633 img_read_close,
634 NULL,
635 AVFMT_NOFILE,
636 };
637
638 AVFormat imgyuv_format = {
639 ".Y.U.V",
640 ".Y.U.V format",
641 "",
642 "Y",
643 CODEC_ID_NONE,
644 CODEC_ID_RAWVIDEO,
645 img_write_header,
646 img_write_packet,
647 img_write_trailer,
648
649 img_read_header,
650 img_read_packet,
651 img_read_close,
652 NULL,
653 AVFMT_NOFILE,
654 };
655
656 AVFormat pgmpipe_format = {
657 "pgmpipe",
658 "PGM pipe format",
659 "",
660 "pgm",
661 CODEC_ID_NONE,
662 CODEC_ID_RAWVIDEO,
663 img_write_header,
664 img_write_packet,
665 img_write_trailer,
666
667 img_read_header,
668 img_read_packet,
669 img_read_close,
670 NULL,
671 };
672
673 AVFormat pgmyuvpipe_format = {
674 "pgmyuvpipe",
675 "PGM YUV pipe format",
676 "",
677 "pgm",
678 CODEC_ID_NONE,
679 CODEC_ID_RAWVIDEO,
680 img_write_header,
681 img_write_packet,
682 img_write_trailer,
683
684 img_read_header,
685 img_read_packet,
686 img_read_close,
687 NULL,
688 };
689
690 AVFormat ppmpipe_format = {
691 "ppmpipe",
692 "PPM pipe format",
693 "",
694 "ppm",
695 CODEC_ID_NONE,
696 CODEC_ID_RAWVIDEO,
697 img_write_header,
698 img_write_packet,
699 img_write_trailer,
700
701 img_read_header,
702 img_read_packet,
703 img_read_close,
704 NULL,
705 };