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 */
245 static int sizes[][2] = {
257 static int infer_size(int *width_ptr, int *height_ptr, int size)
261 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
262 if ((sizes[i][0] * sizes[i][1]) == size) {
263 *width_ptr = sizes[i][0];
264 *height_ptr = sizes[i][1];
271 static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
273 VideoData *s = s1->priv_data;
277 ByteIOContext pb1, *f = &pb1;
280 st = av_new_stream(s1, 0);
286 strcpy(s->path, s1->filename);
290 if (s1->iformat->flags & AVFMT_NOFILE)
295 if (s1->iformat == &pgmyuvpipe_iformat ||
296 s1->iformat == &pgmyuv_iformat)
297 s->img_fmt = IMGFMT_PGMYUV;
298 else if (s1->iformat == &pgmpipe_iformat ||
299 s1->iformat == &pgm_iformat)
300 s->img_fmt = IMGFMT_PGM;
301 else if (s1->iformat == &imgyuv_iformat)
302 s->img_fmt = IMGFMT_YUV;
303 else if (s1->iformat == &ppmpipe_iformat ||
304 s1->iformat == &ppm_iformat)
305 s->img_fmt = IMGFMT_PPM;
310 /* try to find the first image */
312 if (get_frame_filename(buf, sizeof(buf), s->path, s->img_number) < 0)
314 if (url_fopen(f, buf, URL_RDONLY) >= 0)
324 /* find the image size */
325 /* XXX: use generic file format guessing, as mpeg */
330 pnm_get(f, buf1, sizeof(buf1));
331 pnm_get(f, buf1, sizeof(buf1));
332 s->width = atoi(buf1);
333 pnm_get(f, buf1, sizeof(buf1));
335 if (s->img_fmt == IMGFMT_PGMYUV)
340 (s->width % 2) != 0 ||
341 (s->height % 2) != 0) {
346 /* infer size by using the file size. */
353 img_size = url_seek(h, 0, SEEK_END);
354 if (infer_size(&s->width, &s->height, img_size) < 0) {
365 url_fseek(f, 0, SEEK_SET);
369 st->codec.codec_type = CODEC_TYPE_VIDEO;
370 st->codec.codec_id = CODEC_ID_RAWVIDEO;
371 st->codec.width = s->width;
372 st->codec.height = s->height;
373 if (s->img_fmt == IMGFMT_PPM) {
374 st->codec.pix_fmt = PIX_FMT_RGB24;
375 s->img_size = (s->width * s->height * 3);
377 st->codec.pix_fmt = PIX_FMT_YUV420P;
378 s->img_size = (s->width * s->height * 3) / 2;
380 if (!ap || !ap->frame_rate)
381 st->codec.frame_rate = 25 * FRAME_RATE_BASE;
383 st->codec.frame_rate = ap->frame_rate;
394 static int img_read_close(AVFormatContext *s1)
399 /******************************************************/
402 static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv)
406 UINT8 *ptr, *ptr1, *ptr2;
410 h = (height * 3) / 2;
411 snprintf(buf, sizeof(buf),
414 put_buffer(pb, buf, strlen(buf));
416 ptr = picture->data[0];
417 for(i=0;i<height;i++) {
418 put_buffer(pb, ptr, width);
419 ptr += picture->linesize[0];
425 ptr1 = picture->data[1];
426 ptr2 = picture->data[2];
427 for(i=0;i<height;i++) {
428 put_buffer(pb, ptr1, width);
429 put_buffer(pb, ptr2, width);
430 ptr1 += picture->linesize[1];
431 ptr2 += picture->linesize[2];
434 put_flush_packet(pb);
438 static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb)
444 snprintf(buf, sizeof(buf),
447 put_buffer(pb, buf, strlen(buf));
449 ptr = picture->data[0];
450 for(i=0;i<height;i++) {
451 put_buffer(pb, ptr, width * 3);
452 ptr += picture->linesize[0];
455 put_flush_packet(pb);
459 static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
461 ByteIOContext pb1, *pb = &pb1;
462 char fname[1024], *p;
465 static char *ext = "YUV";
467 strcpy(fname, filename);
468 p = strrchr(fname, '.');
469 if (!p || p[1] != 'Y')
479 if (url_fopen(pb, fname, URL_WRONLY) < 0)
482 ptr = picture->data[i];
483 for(j=0;j<height;j++) {
484 put_buffer(pb, ptr, width);
485 ptr += picture->linesize[i];
487 put_flush_packet(pb);
493 static int yuv4mpeg_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int need_stream_header,
494 int is_yuv, int raten, int rated, int aspectn, int aspectd)
497 char buf[Y4M_LINE_MAX+1], buf1[20];
498 UINT8 *ptr, *ptr1, *ptr2;
500 /* construct stream header, if this is the first frame */
501 if(need_stream_header) {
502 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
507 "p", /* ffmpeg seems to only output progressive video */
510 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
512 fprintf(stderr, "YUV4MPEG stream header written. FPS is %d\n", raten);
513 put_buffer(pb, buf, strlen(buf));
517 /* construct frame header */
518 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
520 fprintf(stderr, "Error. YUV4MPEG frame header write failed.\n");
522 /* fprintf(stderr, "YUV4MPEG frame header written.\n"); */
523 put_buffer(pb, buf1, strlen(buf1));
526 ptr = picture->data[0];
527 for(i=0;i<height;i++) {
528 put_buffer(pb, ptr, width);
529 ptr += picture->linesize[0];
535 ptr1 = picture->data[1];
536 ptr2 = picture->data[2];
537 for(i=0;i<height;i++) { /* Cb */
538 put_buffer(pb, ptr1, width);
539 ptr1 += picture->linesize[1];
541 for(i=0;i<height;i++) { /* Cr */
542 put_buffer(pb, ptr2, width);
543 ptr2 += picture->linesize[2];
546 put_flush_packet(pb);
550 static int img_write_header(AVFormatContext *s)
552 VideoData *img = s->priv_data;
555 strcpy(img->path, s->filename);
558 if (s->oformat->flags & AVFMT_NOFILE)
563 if (s->oformat == &pgmyuvpipe_oformat ||
564 s->oformat == &pgmyuv_oformat) {
565 img->img_fmt = IMGFMT_PGMYUV;
566 } else if (s->oformat == &pgmpipe_oformat ||
567 s->oformat == &pgm_oformat) {
568 img->img_fmt = IMGFMT_PGM;
569 } else if (s->oformat == &imgyuv_oformat) {
570 img->img_fmt = IMGFMT_YUV;
571 } else if (s->oformat == &ppmpipe_oformat ||
572 s->oformat == &ppm_oformat) {
573 img->img_fmt = IMGFMT_PPM;
574 } else if (s->oformat == &yuv4mpegpipe_oformat) {
575 img->img_fmt = IMGFMT_YUV4MPEG;
576 img->header_written = 0;
586 static int img_write_packet(AVFormatContext *s, int stream_index,
587 UINT8 *buf, int size, int force_pts)
589 VideoData *img = s->priv_data;
590 AVStream *st = s->streams[stream_index];
591 ByteIOContext pb1, *pb;
593 int width, height, need_stream_header, ret, size1, raten, rated, aspectn, aspectd, fps, fps1;
596 width = st->codec.width;
597 height = st->codec.height;
599 if (img->img_number == 1) {
600 need_stream_header = 1;
602 need_stream_header = 0;
605 fps = st->codec.frame_rate;
606 fps1 = (((float)fps / FRAME_RATE_BASE) * 1000);
608 /* Sorry about this messy code, but mpeg2enc is very picky about
609 * the framerates it accepts. */
612 raten = 24000; /* turn the framerate into a ratio */
644 raten = fps1; /* this setting should work, but often doesn't */
650 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */
652 switch(st->codec.pix_fmt) {
653 case PIX_FMT_YUV420P:
654 size1 = (width * height * 3) / 2;
658 picture.data[0] = buf;
659 picture.data[1] = picture.data[0] + width * height;
660 picture.data[2] = picture.data[1] + (width * height) / 4;
661 picture.linesize[0] = width;
662 picture.linesize[1] = width >> 1;
663 picture.linesize[2] = width >> 1;
666 size1 = (width * height * 3);
669 picture.data[0] = buf;
670 picture.linesize[0] = width * 3;
677 This if-statement destroys pipes - I do not see why it is necessary
678 if (get_frame_filename(filename, sizeof(filename),
679 img->path, img->img_number) < 0)
682 get_frame_filename(filename, sizeof(filename),
683 img->path, img->img_number);
686 if (url_fopen(pb, filename, URL_WRONLY) < 0)
691 switch(img->img_fmt) {
693 ret = pgm_save(&picture, width, height, pb, 1);
696 ret = pgm_save(&picture, width, height, pb, 0);
699 ret = yuv_save(&picture, width, height, filename);
702 ret = ppm_save(&picture, width, height, pb);
704 case IMGFMT_YUV4MPEG:
705 ret = yuv4mpeg_save(&picture, width, height, pb,
706 need_stream_header, 1, raten, rated, aspectn, aspectd);
717 static int img_write_trailer(AVFormatContext *s)
722 AVInputFormat pgm_iformat = {
731 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
735 AVOutputFormat pgm_oformat = {
746 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
749 AVInputFormat pgmyuv_iformat = {
751 "pgm with YUV content image format",
758 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
761 AVOutputFormat pgmyuv_oformat = {
763 "pgm with YUV content image format",
772 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
775 AVInputFormat ppm_iformat = {
784 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24,
788 AVOutputFormat ppm_oformat = {
799 AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24,
802 AVInputFormat imgyuv_iformat = {
811 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
815 AVOutputFormat imgyuv_oformat = {
826 AVFMT_NOFILE | AVFMT_NEEDNUMBER,
829 AVInputFormat pgmpipe_iformat = {
840 AVOutputFormat pgmpipe_oformat = {
853 AVInputFormat pgmyuvpipe_iformat = {
855 "PGM YUV pipe format",
864 AVOutputFormat pgmyuvpipe_oformat = {
866 "PGM YUV pipe format",
877 AVInputFormat ppmpipe_iformat = {
886 .flags = AVFMT_RGB24,
889 AVOutputFormat ppmpipe_oformat = {
900 .flags = AVFMT_RGB24,
904 AVOutputFormat yuv4mpegpipe_oformat = {
906 "YUV4MPEG pipe format",
920 av_register_input_format(&pgm_iformat);
921 av_register_output_format(&pgm_oformat);
923 av_register_input_format(&pgmyuv_iformat);
924 av_register_output_format(&pgmyuv_oformat);
926 av_register_input_format(&ppm_iformat);
927 av_register_output_format(&ppm_oformat);
929 av_register_input_format(&imgyuv_iformat);
930 av_register_output_format(&imgyuv_oformat);
932 av_register_input_format(&pgmpipe_iformat);
933 av_register_output_format(&pgmpipe_oformat);
935 av_register_input_format(&pgmyuvpipe_iformat);
936 av_register_output_format(&pgmyuvpipe_oformat);
938 av_register_input_format(&ppmpipe_iformat);
939 av_register_output_format(&ppmpipe_oformat);
941 av_register_output_format(&yuv4mpegpipe_oformat);