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, n;
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);
46 fps = st->codec.frame_rate;
47 fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
49 /* Sorry about this messy code, but mpeg2enc is very picky about
50 * the framerates it accepts. */
53 raten = 24000; /* turn the framerate into a ratio */
85 raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
86 rated = st->codec.frame_rate_base;
87 gcd= av_gcd(raten, rated);
96 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
98 /* construct stream header, if this is the first frame */
99 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
104 "p", /* ffmpeg seems to only output progressive video */
107 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
110 put_buffer(&s->pb, buf, strlen(buf));
115 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
116 uint8_t *buf, int size, int force_pts)
118 AVStream *st = s->streams[stream_index];
119 ByteIOContext *pb = &s->pb;
124 uint8_t *ptr, *ptr1, *ptr2;
126 picture = (AVPicture *)buf;
128 /* construct frame header */
129 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
130 put_buffer(pb, buf1, strlen(buf1));
132 width = st->codec.width;
133 height = st->codec.height;
135 ptr = picture->data[0];
136 for(i=0;i<height;i++) {
137 put_buffer(pb, ptr, width);
138 ptr += picture->linesize[0];
143 ptr1 = picture->data[1];
144 ptr2 = picture->data[2];
145 for(i=0;i<height;i++) { /* Cb */
146 put_buffer(pb, ptr1, width);
147 ptr1 += picture->linesize[1];
149 for(i=0;i<height;i++) { /* Cr */
150 put_buffer(pb, ptr2, width);
151 ptr2 += picture->linesize[2];
153 put_flush_packet(pb);
157 static int yuv4_write_trailer(AVFormatContext *s)
162 AVOutputFormat yuv4mpegpipe_oformat = {
164 "YUV4MPEG pipe format",
173 .flags = AVFMT_RAWPICTURE,
176 /* Header size increased to allow room for optional flags */
177 #define MAX_YUV4_HEADER 80
178 #define MAX_FRAME_HEADER 10
180 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
182 char header[MAX_YUV4_HEADER+1];
184 ByteIOContext *pb = &s->pb;
185 int width, height, raten, rated, aspectn, aspectd;
189 for (i=0; i<MAX_YUV4_HEADER; i++) {
190 header[i] = get_byte(pb);
191 if (header[i] == '\n') {
196 if (i == MAX_YUV4_HEADER) return -1;
197 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
198 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
199 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
201 st = av_new_stream(s, 0);
203 st->codec.width = width;
204 st->codec.height = height;
205 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
206 st->codec.frame_rate = raten;
207 st->codec.frame_rate_base = rated;
208 st->codec.pix_fmt = PIX_FMT_YUV420P;
209 st->codec.codec_type = CODEC_TYPE_VIDEO;
210 st->codec.codec_id = CODEC_ID_RAWVIDEO;
215 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
218 char header[MAX_FRAME_HEADER+1];
219 int packet_size, ret, width, height;
220 AVStream *st = s->streams[0];
222 for (i=0; i<MAX_FRAME_HEADER; i++) {
223 header[i] = get_byte(&s->pb);
224 if (header[i] == '\n') {
229 if (i == MAX_FRAME_HEADER) return -1;
230 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
232 width = st->codec.width;
233 height = st->codec.height;
235 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
239 if (av_new_packet(pkt, packet_size) < 0)
242 pkt->stream_index = 0;
243 ret = get_buffer(&s->pb, pkt->data, pkt->size);
244 if (ret != pkt->size) {
252 static int yuv4_read_close(AVFormatContext *s)
257 AVInputFormat yuv4mpegpipe_iformat = {
259 "YUV4MPEG pipe format",
265 .extensions = "yuv4mpeg"
268 int yuv4mpeg_init(void)
270 av_register_input_format(&yuv4mpegpipe_iformat);
271 av_register_output_format(&yuv4mpegpipe_oformat);