3 * Copyright (c) 2000, 2001 Gerard Lantau.
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.
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.
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.
30 #define IMGFMT_PGMYUV 2
44 static inline int pnm_space(int c)
46 return (c==' ' || c=='\n' || c=='\r' || c=='\t');
49 static void pnm_get(ByteIOContext *f, char *str, int buf_size)
62 } while (pnm_space(c));
68 if ((s - str) < buf_size - 1)
71 } while (!pnm_space(c));
75 static int pgm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size, int is_yuv)
84 pnm_get(f, buf1, sizeof(buf1));
85 if (strcmp(buf1, "P5")) {
88 pnm_get(f, buf1, sizeof(buf1));
89 pnm_get(f, buf1, sizeof(buf1));
90 pnm_get(f, buf1, sizeof(buf1));
93 picture[1] = buf + width * height;
94 picture[2] = buf + width * height + (width * height / 4);
95 get_buffer(f, picture[0], width * height);
100 for(i=0;i<height;i++) {
101 get_buffer(f, picture[1] + i * width, width);
102 get_buffer(f, picture[2] + i * width, width);
105 for(i=0;i<height;i++) {
106 memset(picture[1] + i * width, 128, width);
107 memset(picture[2] + i * width, 128, width);
113 static int ppm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size)
122 pnm_get(f, buf1, sizeof(buf1));
123 if (strcmp(buf1, "P6")) {
127 pnm_get(f, buf1, sizeof(buf1));
128 pnm_get(f, buf1, sizeof(buf1));
129 pnm_get(f, buf1, sizeof(buf1));
132 get_buffer(f, picture[0], width * height*3);
138 static int yuv_read(VideoData *s, const char *filename, UINT8 *buf, int size1)
140 ByteIOContext pb1, *pb = &pb1;
141 char fname[1024], *p;
144 size = s->width * s->height;
146 strcpy(fname, filename);
147 p = strrchr(fname, '.');
148 if (!p || p[1] != 'Y')
151 if (url_fopen(pb, fname, URL_RDONLY) < 0)
154 get_buffer(pb, buf, size);
158 if (url_fopen(pb, fname, URL_RDONLY) < 0)
161 get_buffer(pb, buf + size, size / 4);
165 if (url_fopen(pb, fname, URL_RDONLY) < 0)
168 get_buffer(pb, buf + size + (size / 4), size / 4);
173 int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
175 VideoData *s = s1->priv_data;
178 ByteIOContext f1, *f;
180 snprintf(filename, sizeof(filename), s->path, s->img_number);
184 if (url_fopen(f, filename, URL_RDONLY) < 0)
192 av_new_packet(pkt, s->img_size);
193 pkt->stream_index = 0;
197 ret = pgm_read(s, f, pkt->data, pkt->size, 1);
200 ret = pgm_read(s, f, pkt->data, pkt->size, 0);
203 ret = yuv_read(s, filename, pkt->data, pkt->size);
206 ret = ppm_read(s, f, pkt->data, pkt->size);
218 return -EIO; /* signal EOF */
225 static int sizes[][2] = {
236 static int infer_size(int *width_ptr, int *height_ptr, int size)
240 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
241 if ((sizes[i][0] * sizes[i][1]) == size) {
242 *width_ptr = sizes[i][0];
243 *height_ptr = sizes[i][1];
250 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
256 ByteIOContext pb1, *f = &pb1;
259 s = malloc(sizeof(VideoData));
266 st = av_mallocz(sizeof(AVStream));
273 strcpy(s->path, s1->filename);
277 if (s1->format->flags & AVFMT_NOFILE)
282 if (s1->format == &pgmyuvpipe_format ||
283 s1->format == &pgmyuv_format)
284 s->img_fmt = IMGFMT_PGMYUV;
285 else if (s1->format == &pgmpipe_format ||
286 s1->format == &pgm_format)
287 s->img_fmt = IMGFMT_PGM;
288 else if (s1->format == &imgyuv_format)
289 s->img_fmt = IMGFMT_YUV;
290 else if (s1->format == &ppmpipe_format ||
291 s1->format == &ppm_format)
292 s->img_fmt = IMGFMT_PPM;
297 /* try to find the first image */
299 snprintf(buf, sizeof(buf), s->path, s->img_number);
300 if (url_fopen(f, buf, URL_RDONLY) >= 0)
310 /* find the image size */
311 /* XXX: use generic file format guessing, as mpeg */
316 pnm_get(f, buf1, sizeof(buf1));
317 pnm_get(f, buf1, sizeof(buf1));
318 s->width = atoi(buf1);
319 pnm_get(f, buf1, sizeof(buf1));
321 if (s->img_fmt == IMGFMT_PGMYUV)
326 (s->width % 2) != 0 ||
327 (s->height % 2) != 0) {
332 /* infer size by using the file size. */
339 img_size = lseek((int)h->priv_data, 0, SEEK_END);
340 if (infer_size(&s->width, &s->height, img_size) < 0) {
350 url_fseek(f, 0, SEEK_SET);
354 st->codec.codec_type = CODEC_TYPE_VIDEO;
355 st->codec.codec_id = CODEC_ID_RAWVIDEO;
356 st->codec.width = s->width;
357 st->codec.height = s->height;
358 if (s->img_fmt == IMGFMT_PPM) {
359 st->codec.pix_fmt = PIX_FMT_RGB24;
360 s->img_size = (s->width * s->height * 3);
362 st->codec.pix_fmt = PIX_FMT_YUV420P;
363 s->img_size = (s->width * s->height * 3) / 2;
365 if (!ap || !ap->frame_rate)
366 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
368 st->codec.frame_rate = ap->frame_rate;
379 static int img_read_close(AVFormatContext *s1)
381 VideoData *s = s1->priv_data;
386 /******************************************************/
389 static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv)
393 UINT8 *ptr, *ptr1, *ptr2;
397 h = (height * 3) / 2;
398 snprintf(buf, sizeof(buf),
401 put_buffer(pb, buf, strlen(buf));
403 ptr = picture->data[0];
404 for(i=0;i<height;i++) {
405 put_buffer(pb, ptr, width);
406 ptr += picture->linesize[0];
412 ptr1 = picture->data[1];
413 ptr2 = picture->data[2];
414 for(i=0;i<height;i++) {
415 put_buffer(pb, ptr1, width);
416 put_buffer(pb, ptr2, width);
417 ptr1 += picture->linesize[1];
418 ptr2 += picture->linesize[2];
421 put_flush_packet(pb);
425 static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb)
431 snprintf(buf, sizeof(buf),
434 put_buffer(pb, buf, strlen(buf));
436 ptr = picture->data[0];
437 for(i=0;i<height;i++) {
438 put_buffer(pb, ptr, width * 3);
439 ptr += picture->linesize[0];
442 put_flush_packet(pb);
446 static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
448 ByteIOContext pb1, *pb = &pb1;
449 char fname[1024], *p;
452 static char *ext = "YUV";
454 strcpy(fname, filename);
455 p = strrchr(fname, '.');
456 if (!p || p[1] != 'Y')
466 if (url_fopen(pb, fname, URL_WRONLY) < 0)
469 ptr = picture->data[i];
470 for(j=0;j<height;j++) {
471 put_buffer(pb, ptr, width);
472 ptr += picture->linesize[i];
474 put_flush_packet(pb);
480 static int img_write_header(AVFormatContext *s)
484 img = av_mallocz(sizeof(VideoData));
489 strcpy(img->path, s->filename);
492 if (s->format->flags & AVFMT_NOFILE)
497 if (s->format == &pgmyuvpipe_format ||
498 s->format == &pgmyuv_format) {
499 img->img_fmt = IMGFMT_PGMYUV;
500 } else if (s->format == &pgmpipe_format ||
501 s->format == &pgm_format) {
502 img->img_fmt = IMGFMT_PGM;
503 } else if (s->format == &imgyuv_format) {
504 img->img_fmt = IMGFMT_YUV;
505 } else if (s->format == &ppmpipe_format ||
506 s->format == &ppm_format) {
507 img->img_fmt = IMGFMT_PPM;
517 static int img_write_packet(AVFormatContext *s, int stream_index,
518 UINT8 *buf, int size)
520 VideoData *img = s->priv_data;
521 AVStream *st = s->streams[stream_index];
522 ByteIOContext pb1, *pb;
524 int width, height, ret, size1;
527 width = st->codec.width;
528 height = st->codec.height;
530 switch(st->codec.pix_fmt) {
531 case PIX_FMT_YUV420P:
532 size1 = (width * height * 3) / 2;
536 picture.data[0] = buf;
537 picture.data[1] = picture.data[0] + width * height;
538 picture.data[2] = picture.data[1] + (width * height) / 4;
539 picture.linesize[0] = width;
540 picture.linesize[1] = width >> 1;
541 picture.linesize[2] = width >> 1;
544 size1 = (width * height * 3);
547 picture.data[0] = buf;
548 picture.linesize[0] = width * 3;
554 snprintf(filename, sizeof(filename), img->path, img->img_number);
558 if (url_fopen(pb, filename, URL_WRONLY) < 0)
563 switch(img->img_fmt) {
565 ret = pgm_save(&picture, width, height, pb, 1);
568 ret = pgm_save(&picture, width, height, pb, 0);
571 ret = yuv_save(&picture, width, height, filename);
574 ret = ppm_save(&picture, width, height, pb);
585 static int img_write_trailer(AVFormatContext *s)
587 VideoData *img = s->priv_data;
592 AVFormat pgm_format = {
610 AVFormat pgmyuv_format = {
612 "pgm with YUV content image format",
628 AVFormat ppm_format = {
646 AVFormat imgyuv_format = {
664 AVFormat pgmpipe_format = {
681 AVFormat pgmyuvpipe_format = {
683 "PGM YUV pipe format",
698 AVFormat ppmpipe_format = {