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