3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 extern AVInputFormat pgm_iformat;
22 extern AVOutputFormat pgm_oformat;
23 extern AVInputFormat pgmyuv_iformat;
24 extern AVOutputFormat pgmyuv_oformat;
25 extern AVInputFormat ppm_iformat;
26 extern AVOutputFormat ppm_oformat;
27 extern AVInputFormat imgyuv_iformat;
28 extern AVOutputFormat imgyuv_oformat;
29 extern AVInputFormat pgmpipe_iformat;
30 extern AVOutputFormat pgmpipe_oformat;
31 extern AVInputFormat pgmyuvpipe_iformat;
32 extern AVOutputFormat pgmyuvpipe_oformat;
33 extern AVInputFormat ppmpipe_iformat;
34 extern AVOutputFormat ppmpipe_oformat;
35 extern AVOutputFormat yuv4mpegpipe_oformat;
38 #define IMGFMT_PGMYUV 2
41 #define IMGFMT_YUV4MPEG 5
43 #define Y4M_MAGIC "YUV4MPEG2"
44 #define Y4M_FRAME_MAGIC "FRAME"
45 #define Y4M_LINE_MAX 256
58 static inline int pnm_space(int c)
60 return (c==' ' || c=='\n' || c=='\r' || c=='\t');
63 static void pnm_get(ByteIOContext *f, char *str, int buf_size)
76 } while (pnm_space(c));
82 if ((s - str) < buf_size - 1)
85 } while (!pnm_space(c));
89 static int pgm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size, int is_yuv)
98 pnm_get(f, buf1, sizeof(buf1));
99 if (strcmp(buf1, "P5")) {
102 pnm_get(f, buf1, sizeof(buf1));
103 pnm_get(f, buf1, sizeof(buf1));
104 pnm_get(f, buf1, sizeof(buf1));
107 picture[1] = buf + width * height;
108 picture[2] = buf + width * height + (width * height / 4);
109 get_buffer(f, picture[0], width * height);
114 for(i=0;i<height;i++) {
115 get_buffer(f, picture[1] + i * width, width);
116 get_buffer(f, picture[2] + i * width, width);
119 for(i=0;i<height;i++) {
120 memset(picture[1] + i * width, 128, width);
121 memset(picture[2] + i * width, 128, width);
127 static int ppm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size)
136 pnm_get(f, buf1, sizeof(buf1));
137 if (strcmp(buf1, "P6")) {
141 pnm_get(f, buf1, sizeof(buf1));
142 pnm_get(f, buf1, sizeof(buf1));
143 pnm_get(f, buf1, sizeof(buf1));
146 get_buffer(f, picture[0], width * height*3);
152 static int yuv_read(VideoData *s, const char *filename, UINT8 *buf, int size1)
154 ByteIOContext pb1, *pb = &pb1;
155 char fname[1024], *p;
158 size = s->width * s->height;
160 strcpy(fname, filename);
161 p = strrchr(fname, '.');
162 if (!p || p[1] != 'Y')
165 if (url_fopen(pb, fname, URL_RDONLY) < 0)
168 get_buffer(pb, buf, size);
172 if (url_fopen(pb, fname, URL_RDONLY) < 0)
175 get_buffer(pb, buf + size, size / 4);
179 if (url_fopen(pb, fname, URL_RDONLY) < 0)
182 get_buffer(pb, buf + size + (size / 4), size / 4);
187 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
189 VideoData *s = s1->priv_data;
192 ByteIOContext f1, *f;
195 This if-statement destroys pipes - I do not see why it is necessary
196 if (get_frame_filename(filename, sizeof(filename),
197 s->path, s->img_number) < 0)
200 get_frame_filename(filename, sizeof(filename),
201 s->path, s->img_number);
204 if (url_fopen(f, filename, URL_RDONLY) < 0)
212 av_new_packet(pkt, s->img_size);
213 pkt->stream_index = 0;
217 ret = pgm_read(s, f, pkt->data, pkt->size, 1);
220 ret = pgm_read(s, f, pkt->data, pkt->size, 0);
223 ret = yuv_read(s, filename, pkt->data, pkt->size);
226 ret = ppm_read(s, f, pkt->data, pkt->size);
238 return -EIO; /* signal EOF */
240 pkt->pts = ((INT64)s->img_number * s1->pts_den * FRAME_RATE_BASE) / (s1->streams[0]->codec.frame_rate * s1->pts_num);
246 static int sizes[][2] = {
258 static int infer_size(int *width_ptr, int *height_ptr, int size)
262 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
263 if ((sizes[i][0] * sizes[i][1]) == size) {
264 *width_ptr = sizes[i][0];
265 *height_ptr = sizes[i][1];
272 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
274 VideoData *s = s1->priv_data;
278 ByteIOContext pb1, *f = &pb1;
281 st = av_new_stream(s1, 0);
287 strcpy(s->path, s1->filename);
291 if (s1->iformat->flags & AVFMT_NOFILE)
296 if (s1->iformat == &pgmyuvpipe_iformat ||
297 s1->iformat == &pgmyuv_iformat)
298 s->img_fmt = IMGFMT_PGMYUV;
299 else if (s1->iformat == &pgmpipe_iformat ||
300 s1->iformat == &pgm_iformat)
301 s->img_fmt = IMGFMT_PGM;
302 else if (s1->iformat == &imgyuv_iformat)
303 s->img_fmt = IMGFMT_YUV;
304 else if (s1->iformat == &ppmpipe_iformat ||
305 s1->iformat == &ppm_iformat)
306 s->img_fmt = IMGFMT_PPM;
311 /* try to find the first image */
313 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
315 if (url_fopen(f, buf, URL_RDONLY) >= 0)
325 /* find the image size */
326 /* XXX: use generic file format guessing, as mpeg */
331 pnm_get(f, buf1, sizeof(buf1));
332 pnm_get(f, buf1, sizeof(buf1));
333 s->width = atoi(buf1);
334 pnm_get(f, buf1, sizeof(buf1));
336 if (s->img_fmt == IMGFMT_PGMYUV)
341 (s->width % 2) != 0 ||
342 (s->height % 2) != 0) {
347 /* infer size by using the file size. */
354 img_size = url_seek(h, 0, SEEK_END);
355 if (infer_size(&s->width, &s->height, img_size) < 0) {
366 url_fseek(f, 0, SEEK_SET);
370 st->codec.codec_type = CODEC_TYPE_VIDEO;
371 st->codec.codec_id = CODEC_ID_RAWVIDEO;
372 st->codec.width = s->width;
373 st->codec.height = s->height;
374 if (s->img_fmt == IMGFMT_PPM) {
375 st->codec.pix_fmt = PIX_FMT_RGB24;
376 s->img_size = (s->width * s->height * 3);
378 st->codec.pix_fmt = PIX_FMT_YUV420P;
379 s->img_size = (s->width * s->height * 3) / 2;
381 if (!ap || !ap->frame_rate)
382 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
384 st->codec.frame_rate = ap->frame_rate;
395 static int img_read_close(AVFormatContext *s1)
400 /******************************************************/
403 static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv)
407 UINT8 *ptr, *ptr1, *ptr2;
411 h = (height * 3) / 2;
412 snprintf(buf, sizeof(buf),
415 put_buffer(pb, buf, strlen(buf));
417 ptr = picture->data[0];
418 for(i=0;i<height;i++) {
419 put_buffer(pb, ptr, width);
420 ptr += picture->linesize[0];
426 ptr1 = picture->data[1];
427 ptr2 = picture->data[2];
428 for(i=0;i<height;i++) {
429 put_buffer(pb, ptr1, width);
430 put_buffer(pb, ptr2, width);
431 ptr1 += picture->linesize[1];
432 ptr2 += picture->linesize[2];
435 put_flush_packet(pb);
439 static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb)
445 snprintf(buf, sizeof(buf),
448 put_buffer(pb, buf, strlen(buf));
450 ptr = picture->data[0];
451 for(i=0;i<height;i++) {
452 put_buffer(pb, ptr, width * 3);
453 ptr += picture->linesize[0];
456 put_flush_packet(pb);
460 static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
462 ByteIOContext pb1, *pb = &pb1;
463 char fname[1024], *p;
466 static char *ext = "YUV";
468 strcpy(fname, filename);
469 p = strrchr(fname, '.');
470 if (!p || p[1] != 'Y')
480 if (url_fopen(pb, fname, URL_WRONLY) < 0)
483 ptr = picture->data[i];
484 for(j=0;j<height;j++) {
485 put_buffer(pb, ptr, width);
486 ptr += picture->linesize[i];
488 put_flush_packet(pb);
494 static int yuv4mpeg_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int need_stream_header,
495 int is_yuv, int raten, int rated, int aspectn, int aspectd)
498 char buf[Y4M_LINE_MAX+1], buf1[20];
499 UINT8 *ptr, *ptr1, *ptr2;
501 /* construct stream header, if this is the first frame */
502 if(need_stream_header) {
503 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
508 "p", /* ffmpeg seems to only output progressive video */
511 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
513 fprintf(stderr, "YUV4MPEG stream header written. FPS is %d\n", raten);
514 put_buffer(pb, buf, strlen(buf));
518 /* construct frame header */
519 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
521 fprintf(stderr, "Error. YUV4MPEG frame header write failed.\n");
523 /* fprintf(stderr, "YUV4MPEG frame header written.\n"); */
524 put_buffer(pb, buf1, strlen(buf1));
527 ptr = picture->data[0];
528 for(i=0;i<height;i++) {
529 put_buffer(pb, ptr, width);
530 ptr += picture->linesize[0];
536 ptr1 = picture->data[1];
537 ptr2 = picture->data[2];
538 for(i=0;i<height;i++) { /* Cb */
539 put_buffer(pb, ptr1, width);
540 ptr1 += picture->linesize[1];
542 for(i=0;i<height;i++) { /* Cr */
543 put_buffer(pb, ptr2, width);
544 ptr2 += picture->linesize[2];
547 put_flush_packet(pb);
551 static int img_write_header(AVFormatContext *s)
553 VideoData *img = s->priv_data;
556 strcpy(img->path, s->filename);
559 if (s->oformat->flags & AVFMT_NOFILE)
564 if (s->oformat == &pgmyuvpipe_oformat ||
565 s->oformat == &pgmyuv_oformat) {
566 img->img_fmt = IMGFMT_PGMYUV;
567 } else if (s->oformat == &pgmpipe_oformat ||
568 s->oformat == &pgm_oformat) {
569 img->img_fmt = IMGFMT_PGM;
570 } else if (s->oformat == &imgyuv_oformat) {
571 img->img_fmt = IMGFMT_YUV;
572 } else if (s->oformat == &ppmpipe_oformat ||
573 s->oformat == &ppm_oformat) {
574 img->img_fmt = IMGFMT_PPM;
575 } else if (s->oformat == &yuv4mpegpipe_oformat) {
576 img->img_fmt = IMGFMT_YUV4MPEG;
577 img->header_written = 0;
587 static int img_write_packet(AVFormatContext *s, int stream_index,
588 UINT8 *buf, int size, int force_pts)
590 VideoData *img = s->priv_data;
591 AVStream *st = s->streams[stream_index];
592 ByteIOContext pb1, *pb;
594 int width, height, need_stream_header, ret, size1, raten, rated, aspectn, aspectd, fps, fps1;
597 width = st->codec.width;
598 height = st->codec.height;
600 if (img->img_number == 1) {
601 need_stream_header = 1;
603 need_stream_header = 0;
606 fps = st->codec.frame_rate;
607 fps1 = (((float)fps / FRAME_RATE_BASE) * 1000);
609 /* Sorry about this messy code, but mpeg2enc is very picky about
610 * the framerates it accepts. */
613 raten = 24000; /* turn the framerate into a ratio */
645 raten = fps1; /* this setting should work, but often doesn't */
651 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */
653 switch(st->codec.pix_fmt) {
654 case PIX_FMT_YUV420P:
655 size1 = (width * height * 3) / 2;
659 picture.data[0] = buf;
660 picture.data[1] = picture.data[0] + width * height;
661 picture.data[2] = picture.data[1] + (width * height) / 4;
662 picture.linesize[0] = width;
663 picture.linesize[1] = width >> 1;
664 picture.linesize[2] = width >> 1;
667 size1 = (width * height * 3);
670 picture.data[0] = buf;
671 picture.linesize[0] = width * 3;
678 This if-statement destroys pipes - I do not see why it is necessary
679 if (get_frame_filename(filename, sizeof(filename),
680 img->path, img->img_number) < 0)
683 get_frame_filename(filename, sizeof(filename),
684 img->path, img->img_number);
687 if (url_fopen(pb, filename, URL_WRONLY) < 0)
692 switch(img->img_fmt) {
694 ret = pgm_save(&picture, width, height, pb, 1);
697 ret = pgm_save(&picture, width, height, pb, 0);
700 ret = yuv_save(&picture, width, height, filename);
703 ret = ppm_save(&picture, width, height, pb);
705 case IMGFMT_YUV4MPEG:
706 ret = yuv4mpeg_save(&picture, width, height, pb,
707 need_stream_header, 1, raten, rated, aspectn, aspectd);
718 static int img_write_trailer(AVFormatContext *s)
723 static AVInputFormat pgm_iformat = {
732 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
736 static AVOutputFormat pgm_oformat = {
747 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
750 static AVInputFormat pgmyuv_iformat = {
752 "pgm with YUV content image format",
759 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
762 static AVOutputFormat pgmyuv_oformat = {
764 "pgm with YUV content image format",
773 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
776 static AVInputFormat ppm_iformat = {
785 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24,
789 static AVOutputFormat ppm_oformat = {
800 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24,
803 static AVInputFormat imgyuv_iformat = {
812 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
816 static AVOutputFormat imgyuv_oformat = {
827 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
830 static AVInputFormat pgmpipe_iformat = {
841 static AVOutputFormat pgmpipe_oformat = {
854 static AVInputFormat pgmyuvpipe_iformat = {
856 "PGM YUV pipe format",
865 static AVOutputFormat pgmyuvpipe_oformat = {
867 "PGM YUV pipe format",
878 static AVInputFormat ppmpipe_iformat = {
887 .flags = AVFMT_RGB24,
890 static AVOutputFormat ppmpipe_oformat = {
901 .flags = AVFMT_RGB24,
905 static AVOutputFormat yuv4mpegpipe_oformat = {
907 "YUV4MPEG pipe format",
921 av_register_input_format(&pgm_iformat);
922 av_register_output_format(&pgm_oformat);
924 av_register_input_format(&pgmyuv_iformat);
925 av_register_output_format(&pgmyuv_oformat);
927 av_register_input_format(&ppm_iformat);
928 av_register_output_format(&ppm_oformat);
930 av_register_input_format(&imgyuv_iformat);
931 av_register_output_format(&imgyuv_oformat);
933 av_register_input_format(&pgmpipe_iformat);
934 av_register_output_format(&pgmpipe_oformat);
936 av_register_input_format(&pgmyuvpipe_iformat);
937 av_register_output_format(&pgmyuvpipe_oformat);
939 av_register_input_format(&ppmpipe_iformat);
940 av_register_output_format(&ppmpipe_oformat);
942 av_register_output_format(&yuv4mpegpipe_oformat);