3 * Copyright (c) 2001, 2002, 2003 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 #define Y4M_MAGIC "YUV4MPEG2"
22 #define Y4M_FRAME_MAGIC "FRAME"
23 #define Y4M_LINE_MAX 256
25 static int yuv4_write_header(AVFormatContext *s)
29 int raten, rated, aspectn, aspectd, fps, fps1, n, gcd;
30 char buf[Y4M_LINE_MAX+1];
32 if (s->nb_streams != 1)
36 width = st->codec.width;
37 height = st->codec.height;
40 //this is identical to the code below for exact fps
41 av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
43 fps = st->codec.frame_rate;
44 fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
46 /* Sorry about this messy code, but mpeg2enc is very picky about
47 * the framerates it accepts. */
50 raten = 24000; /* turn the framerate into a ratio */
82 raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
83 rated = st->codec.frame_rate_base;
84 gcd= av_gcd(raten, rated);
92 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
94 /* construct stream header, if this is the first frame */
95 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
100 "p", /* ffmpeg seems to only output progressive video */
103 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
106 put_buffer(&s->pb, buf, strlen(buf));
111 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
112 uint8_t *buf, int size, int force_pts)
114 AVStream *st = s->streams[stream_index];
115 ByteIOContext *pb = &s->pb;
120 uint8_t *ptr, *ptr1, *ptr2;
122 picture = (AVPicture *)buf;
124 /* construct frame header */
125 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
126 put_buffer(pb, buf1, strlen(buf1));
128 width = st->codec.width;
129 height = st->codec.height;
131 ptr = picture->data[0];
132 for(i=0;i<height;i++) {
133 put_buffer(pb, ptr, width);
134 ptr += picture->linesize[0];
139 ptr1 = picture->data[1];
140 ptr2 = picture->data[2];
141 for(i=0;i<height;i++) { /* Cb */
142 put_buffer(pb, ptr1, width);
143 ptr1 += picture->linesize[1];
145 for(i=0;i<height;i++) { /* Cr */
146 put_buffer(pb, ptr2, width);
147 ptr2 += picture->linesize[2];
149 put_flush_packet(pb);
153 static int yuv4_write_trailer(AVFormatContext *s)
158 AVOutputFormat yuv4mpegpipe_oformat = {
160 "YUV4MPEG pipe format",
169 .flags = AVFMT_RAWPICTURE,