]> git.sesse.net Git - ffmpeg/blob - libavformat/yuv4mpeg.c
do not call (av_)abort()
[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
27 static int yuv4_generate_header(AVFormatContext *s, char* buf)
28 {
29     AVStream *st;
30     int width, height;
31     int raten, rated, aspectn, aspectd, n;
32     char inter;
33
34     st = s->streams[0];
35     width = st->codec.width;
36     height = st->codec.height;
37
38     av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
39     
40     aspectn = st->codec.sample_aspect_ratio.num;
41     aspectd = st->codec.sample_aspect_ratio.den;
42     
43     inter = 'p'; /* progressive is the default */
44     if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
45         inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
46     }
47
48     /* construct stream header, if this is the first frame */
49     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
50                  Y4M_MAGIC,
51                  width,
52                  height,
53                  raten, rated,
54                  inter,
55                  aspectn, aspectd,
56                  (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " C411 XYSCSS=411" : " C420mpeg2 XYSCSS=420MPEG2");
57                  
58     return n;
59 }
60
61 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
62 {
63     AVStream *st = s->streams[pkt->stream_index];
64     ByteIOContext *pb = &s->pb;
65     AVPicture *picture;
66     int* first_pkt = s->priv_data;
67     int width, height;
68     int i, m;
69     char buf2[Y4M_LINE_MAX+1];
70     char buf1[20];
71     uint8_t *ptr, *ptr1, *ptr2;
72
73     picture = (AVPicture *)pkt->data;
74
75     /* for the first packet we have to output the header as well */
76     if (*first_pkt) {
77         *first_pkt = 0;
78         if (yuv4_generate_header(s, buf2) < 0) {
79             av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
80             return AVERROR_IO;
81         } else {
82             put_buffer(pb, buf2, strlen(buf2)); 
83         }
84     }
85
86     /* construct frame header */
87     
88     m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
89     put_buffer(pb, buf1, strlen(buf1));
90
91     width = st->codec.width;
92     height = st->codec.height;
93     
94     ptr = picture->data[0];
95     for(i=0;i<height;i++) {
96         put_buffer(pb, ptr, width);
97         ptr += picture->linesize[0];
98     }
99
100     height >>= 1;
101     width >>= 1;
102     ptr1 = picture->data[1];
103     ptr2 = picture->data[2];
104     for(i=0;i<height;i++) {             /* Cb */
105         put_buffer(pb, ptr1, width);
106         ptr1 += picture->linesize[1];
107     }
108     for(i=0;i<height;i++) {     /* Cr */
109         put_buffer(pb, ptr2, width);
110             ptr2 += picture->linesize[2];
111     }
112     put_flush_packet(pb);
113     return 0;
114 }
115
116 static int yuv4_write_header(AVFormatContext *s)
117 {
118     int* first_pkt = s->priv_data;
119     
120     if (s->nb_streams != 1)
121         return AVERROR_IO;
122     
123     if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
124         av_log(s, AV_LOG_ERROR, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
125     } 
126     else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
127         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
128         return AVERROR_IO;
129     }
130     
131     *first_pkt = 1;
132     return 0;
133 }
134
135 static int yuv4_write_trailer(AVFormatContext *s)
136 {
137     return 0;
138 }
139
140 AVOutputFormat yuv4mpegpipe_oformat = {
141     "yuv4mpegpipe",
142     "YUV4MPEG pipe format",
143     "",
144     "yuv4mpeg",
145     sizeof(int),
146     CODEC_ID_NONE,
147     CODEC_ID_RAWVIDEO,
148     yuv4_write_header,
149     yuv4_write_packet,
150     yuv4_write_trailer,
151     .flags = AVFMT_RAWPICTURE,
152 };
153 #endif //CONFIG_ENCODERS
154
155 /* Header size increased to allow room for optional flags */
156 #define MAX_YUV4_HEADER 80
157 #define MAX_FRAME_HEADER 10
158
159 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
160 {
161     char header[MAX_YUV4_HEADER+1];
162     int i;
163     ByteIOContext *pb = &s->pb;
164     int width, height, raten, rated, aspectn, aspectd;
165     char lacing;
166     AVStream *st;
167     
168     for (i=0; i<MAX_YUV4_HEADER; i++) {
169         header[i] = get_byte(pb);
170         if (header[i] == '\n') {
171             header[i+1] = 0;
172             break;
173         }
174     }
175     if (i == MAX_YUV4_HEADER) return -1;
176     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
177     sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
178            &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
179     
180     st = av_new_stream(s, 0);
181     st = s->streams[0];
182     st->codec.width = width;
183     st->codec.height = height;
184     av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
185     st->codec.frame_rate = raten;
186     st->codec.frame_rate_base = rated;
187     st->codec.pix_fmt = PIX_FMT_YUV420P;
188     st->codec.codec_type = CODEC_TYPE_VIDEO;
189     st->codec.codec_id = CODEC_ID_RAWVIDEO;
190     st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
191
192     return 0;
193 }
194
195 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
196 {
197     int i;
198     char header[MAX_FRAME_HEADER+1];
199     int packet_size, ret, width, height;
200     AVStream *st = s->streams[0];
201
202     for (i=0; i<MAX_FRAME_HEADER; i++) {
203         header[i] = get_byte(&s->pb);
204         if (header[i] == '\n') {
205             header[i+1] = 0;
206             break;
207         }
208     }
209     if (i == MAX_FRAME_HEADER) return -1;
210     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
211     
212     width = st->codec.width;
213     height = st->codec.height;
214
215     packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
216     if (packet_size < 0)
217         return -1;
218
219     if (av_new_packet(pkt, packet_size) < 0)
220         return AVERROR_IO;
221
222     pkt->stream_index = 0;
223     ret = get_buffer(&s->pb, pkt->data, pkt->size);
224     if (ret != pkt->size) {
225         av_free_packet(pkt);
226         return AVERROR_IO;
227     } else {
228         return 0;
229     }
230 }
231
232 static int yuv4_read_close(AVFormatContext *s)
233 {
234     return 0;
235 }
236
237 AVInputFormat yuv4mpegpipe_iformat = {
238     "yuv4mpegpipe",
239     "YUV4MPEG pipe format",
240     0,
241     NULL,
242     yuv4_read_header,
243     yuv4_read_packet,
244     yuv4_read_close,
245     .extensions = "yuv4mpeg"
246 };
247
248 int yuv4mpeg_init(void)
249 {
250     av_register_input_format(&yuv4mpegpipe_iformat);
251 #ifdef CONFIG_ENCODERS
252     av_register_output_format(&yuv4mpegpipe_oformat);
253 #endif //CONFIG_ENCODERS
254     return 0;
255 }
256