83b5ff2ab1f048eb7085109118bf12cdfe35f78d
[libav.git] / libavformat / yuv4mpeg.c
1 /*
2 * YUV4MPEG format
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19 #include "avformat.h"
20
21 #define Y4M_MAGIC "YUV4MPEG2"
22 #define Y4M_FRAME_MAGIC "FRAME"
23 #define Y4M_LINE_MAX 256
24
25 static int yuv4_write_header(AVFormatContext *s)
26 {
27 AVStream *st;
28 int width, height;
29 int raten, rated, aspectn, aspectd, n;
30 char buf[Y4M_LINE_MAX+1];
31
32 if (s->nb_streams != 1)
33 return -EIO;
34
35 st = s->streams[0];
36 width = st->codec.width;
37 height = st->codec.height;
38
39 #if 1
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);
42 #else
43 {
44 int gcd, fps, fps1;
45
46 fps = st->codec.frame_rate;
47 fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
48
49 /* Sorry about this messy code, but mpeg2enc is very picky about
50 * the framerates it accepts. */
51 switch(fps1) {
52 case 23976:
53 raten = 24000; /* turn the framerate into a ratio */
54 rated = 1001;
55 break;
56 case 29970:
57 raten = 30000;
58 rated = 1001;
59 break;
60 case 25000:
61 raten = 25;
62 rated = 1;
63 break;
64 case 30000:
65 raten = 30;
66 rated = 1;
67 break;
68 case 24000:
69 raten = 24;
70 rated = 1;
71 break;
72 case 50000:
73 raten = 50;
74 rated = 1;
75 break;
76 case 59940:
77 raten = 60000;
78 rated = 1001;
79 break;
80 case 60000:
81 raten = 60;
82 rated = 1;
83 break;
84 default:
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);
88 raten /= gcd;
89 rated /= gcd;
90 break;
91 }
92 }
93 #endif
94
95 aspectn = 1;
96 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
97
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",
100 Y4M_MAGIC,
101 width,
102 height,
103 raten, rated,
104 "p", /* ffmpeg seems to only output progressive video */
105 aspectn, aspectd);
106 if (n < 0) {
107 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
108 return -EIO;
109 } else {
110 put_buffer(&s->pb, buf, strlen(buf));
111 }
112 return 0;
113 }
114
115 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
116 uint8_t *buf, int size, int force_pts)
117 {
118 AVStream *st = s->streams[stream_index];
119 ByteIOContext *pb = &s->pb;
120 AVPicture *picture;
121 int width, height;
122 int i, m;
123 char buf1[20];
124 uint8_t *ptr, *ptr1, *ptr2;
125
126 picture = (AVPicture *)buf;
127
128 /* construct frame header */
129 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
130 put_buffer(pb, buf1, strlen(buf1));
131
132 width = st->codec.width;
133 height = st->codec.height;
134
135 ptr = picture->data[0];
136 for(i=0;i<height;i++) {
137 put_buffer(pb, ptr, width);
138 ptr += picture->linesize[0];
139 }
140
141 height >>= 1;
142 width >>= 1;
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];
148 }
149 for(i=0;i<height;i++) { /* Cr */
150 put_buffer(pb, ptr2, width);
151 ptr2 += picture->linesize[2];
152 }
153 put_flush_packet(pb);
154 return 0;
155 }
156
157 static int yuv4_write_trailer(AVFormatContext *s)
158 {
159 return 0;
160 }
161
162 AVOutputFormat yuv4mpegpipe_oformat = {
163 "yuv4mpegpipe",
164 "YUV4MPEG pipe format",
165 "",
166 "yuv4mpeg",
167 0,
168 CODEC_ID_NONE,
169 CODEC_ID_RAWVIDEO,
170 yuv4_write_header,
171 yuv4_write_packet,
172 yuv4_write_trailer,
173 .flags = AVFMT_RAWPICTURE,
174 };
175
176 #define MAX_YUV4_HEADER 50
177 #define MAX_FRAME_HEADER 10
178
179 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
180 {
181 char header[MAX_YUV4_HEADER+1];
182 int i;
183 ByteIOContext *pb = &s->pb;
184 int width, height, raten, rated, aspectn, aspectd;
185 char lacing;
186 AVStream *st;
187
188 for (i=0; i<MAX_YUV4_HEADER; i++) {
189 header[i] = get_byte(pb);
190 if (header[i] == '\n') {
191 header[i+1] = 0;
192 break;
193 }
194 }
195 if (i == MAX_YUV4_HEADER) return -1;
196 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
197 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
198 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
199
200 st = av_new_stream(s, 0);
201 st = s->streams[0];
202 st->codec.width = width;
203 st->codec.height = height;
204 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
205 st->codec.frame_rate = raten;
206 st->codec.frame_rate_base = rated;
207 st->codec.pix_fmt = PIX_FMT_YUV420P;
208 st->codec.codec_type = CODEC_TYPE_VIDEO;
209 st->codec.codec_id = CODEC_ID_RAWVIDEO;
210
211 return 0;
212 }
213
214 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
215 {
216 int i;
217 char header[MAX_FRAME_HEADER+1];
218 int packet_size, ret, width, height;
219 AVStream *st = s->streams[0];
220
221 for (i=0; i<MAX_FRAME_HEADER; i++) {
222 header[i] = get_byte(&s->pb);
223 if (header[i] == '\n') {
224 header[i+1] = 0;
225 break;
226 }
227 }
228 if (i == MAX_FRAME_HEADER) return -1;
229 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
230
231 width = st->codec.width;
232 height = st->codec.height;
233
234 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
235 if (packet_size < 0)
236 av_abort();
237
238 if (av_new_packet(pkt, packet_size) < 0)
239 return -EIO;
240
241 pkt->stream_index = 0;
242 ret = get_buffer(&s->pb, pkt->data, pkt->size);
243 if (ret != pkt->size) {
244 av_free_packet(pkt);
245 return -EIO;
246 } else {
247 return 0;
248 }
249 }
250
251 static int yuv4_read_close(AVFormatContext *s)
252 {
253 return 0;
254 }
255
256 AVInputFormat yuv4mpegpipe_iformat = {
257 "yuv4mpegpipe",
258 "YUV4MPEG pipe format",
259 0,
260 NULL,
261 yuv4_read_header,
262 yuv4_read_packet,
263 yuv4_read_close,
264 .extensions = "yuv4mpeg"
265 };
266
267 int yuv4mpeg_init(void)
268 {
269 av_register_input_format(&yuv4mpegpipe_iformat);
270 av_register_output_format(&yuv4mpegpipe_oformat);
271 return 0;
272 }
273