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;
30 char buf[Y4M_LINE_MAX+1];
32 if (s->nb_streams != 1)
36 width = st->codec.width;
37 height = st->codec.height;
39 fps = st->codec.frame_rate;
40 fps1 = (((float)fps / FRAME_RATE_BASE) * 1000);
42 /* Sorry about this messy code, but mpeg2enc is very picky about
43 * the framerates it accepts. */
46 raten = 24000; /* turn the framerate into a ratio */
78 raten = fps1; /* this setting should work, but often doesn't */
84 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */
86 /* construct stream header, if this is the first frame */
87 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
92 "p", /* ffmpeg seems to only output progressive video */
95 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
98 put_buffer(&s->pb, buf, strlen(buf));
103 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
104 uint8_t *buf, int size, int force_pts)
106 AVStream *st = s->streams[stream_index];
107 ByteIOContext *pb = &s->pb;
112 uint8_t *ptr, *ptr1, *ptr2;
114 picture = (AVPicture *)buf;
116 /* construct frame header */
117 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
118 put_buffer(pb, buf1, strlen(buf1));
120 width = st->codec.width;
121 height = st->codec.height;
123 ptr = picture->data[0];
124 for(i=0;i<height;i++) {
125 put_buffer(pb, ptr, width);
126 ptr += picture->linesize[0];
131 ptr1 = picture->data[1];
132 ptr2 = picture->data[2];
133 for(i=0;i<height;i++) { /* Cb */
134 put_buffer(pb, ptr1, width);
135 ptr1 += picture->linesize[1];
137 for(i=0;i<height;i++) { /* Cr */
138 put_buffer(pb, ptr2, width);
139 ptr2 += picture->linesize[2];
141 put_flush_packet(pb);
145 static int yuv4_write_trailer(AVFormatContext *s)
150 AVOutputFormat yuv4mpegpipe_oformat = {
152 "YUV4MPEG pipe format",
161 .flags = AVFMT_RAWPICTURE,