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 #ifdef CONFIG_ENCODERS
27 static int yuv4_generate_header(AVFormatContext *s, char* buf)
31 int raten, rated, aspectn, aspectd, n;
33 char *colorspace = "";
36 width = st->codec.width;
37 height = st->codec.height;
39 av_reduce(&raten, &rated, st->codec.time_base.den, st->codec.time_base.num, (1UL<<31)-1);
41 aspectn = st->codec.sample_aspect_ratio.num;
42 aspectd = st->codec.sample_aspect_ratio.den;
44 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
46 inter = 'p'; /* progressive is the default */
47 if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
48 inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
51 switch(st->codec.pix_fmt) {
53 colorspace = " Cmono";
56 colorspace = " C411 XYSCSS=411";
59 colorspace = (st->codec.codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
62 colorspace = " C422 XYSCSS=422";
65 colorspace = " C444 XYSCSS=444";
69 /* construct stream header, if this is the first frame */
70 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
82 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
84 AVStream *st = s->streams[pkt->stream_index];
85 ByteIOContext *pb = &s->pb;
87 int* first_pkt = s->priv_data;
88 int width, height, h_chroma_shift, v_chroma_shift;
90 char buf2[Y4M_LINE_MAX+1];
92 uint8_t *ptr, *ptr1, *ptr2;
94 picture = (AVPicture *)pkt->data;
96 /* for the first packet we have to output the header as well */
99 if (yuv4_generate_header(s, buf2) < 0) {
100 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
103 put_buffer(pb, buf2, strlen(buf2));
107 /* construct frame header */
109 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
110 put_buffer(pb, buf1, strlen(buf1));
112 width = st->codec.width;
113 height = st->codec.height;
115 ptr = picture->data[0];
116 for(i=0;i<height;i++) {
117 put_buffer(pb, ptr, width);
118 ptr += picture->linesize[0];
121 if (st->codec.pix_fmt != PIX_FMT_GRAY8){
122 // Adjust for smaller Cb and Cr planes
123 avcodec_get_chroma_sub_sample(st->codec.pix_fmt, &h_chroma_shift, &v_chroma_shift);
124 width >>= h_chroma_shift;
125 height >>= v_chroma_shift;
127 ptr1 = picture->data[1];
128 ptr2 = picture->data[2];
129 for(i=0;i<height;i++) { /* Cb */
130 put_buffer(pb, ptr1, width);
131 ptr1 += picture->linesize[1];
133 for(i=0;i<height;i++) { /* Cr */
134 put_buffer(pb, ptr2, width);
135 ptr2 += picture->linesize[2];
138 put_flush_packet(pb);
142 static int yuv4_write_header(AVFormatContext *s)
144 int* first_pkt = s->priv_data;
146 if (s->nb_streams != 1)
149 if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
150 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
152 else if ((s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) &&
153 (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV422P) &&
154 (s->streams[0]->codec.pix_fmt != PIX_FMT_GRAY8) &&
155 (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV444P)) {
156 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
164 static int yuv4_write_trailer(AVFormatContext *s)
169 AVOutputFormat yuv4mpegpipe_oformat = {
171 "YUV4MPEG pipe format",
180 .flags = AVFMT_RAWPICTURE,
182 #endif //CONFIG_ENCODERS
184 /* Header size increased to allow room for optional flags */
185 #define MAX_YUV4_HEADER 80
186 #define MAX_FRAME_HEADER 80
188 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
190 char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
191 char *tokstart,*tokend,*header_end;
193 ByteIOContext *pb = &s->pb;
194 int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0,interlaced_frame=0,top_field_first=0;
195 enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
198 for (i=0; i<MAX_YUV4_HEADER; i++) {
199 header[i] = get_byte(pb);
200 if (header[i] == '\n') {
201 header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
206 if (i == MAX_YUV4_HEADER) return -1;
207 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
209 header_end = &header[i+1]; // Include space
210 for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
211 if (*tokstart==0x20) continue;
212 switch (*tokstart++) {
213 case 'W': // Width. Required.
214 width = strtol(tokstart, &tokend, 10);
217 case 'H': // Height. Required.
218 height = strtol(tokstart, &tokend, 10);
221 case 'C': // Color space
222 if (strncmp("420jpeg",tokstart,7)==0)
223 pix_fmt = PIX_FMT_YUV420P;
224 else if (strncmp("420mpeg2",tokstart,8)==0)
225 pix_fmt = PIX_FMT_YUV420P;
226 else if (strncmp("420paldv", tokstart, 8)==0)
227 pix_fmt = PIX_FMT_YUV420P;
228 else if (strncmp("411", tokstart, 3)==0)
229 pix_fmt = PIX_FMT_YUV411P;
230 else if (strncmp("422", tokstart, 3)==0)
231 pix_fmt = PIX_FMT_YUV422P;
232 else if (strncmp("444alpha", tokstart, 8)==0) {
233 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
235 } else if (strncmp("444", tokstart, 3)==0)
236 pix_fmt = PIX_FMT_YUV444P;
237 else if (strncmp("mono",tokstart, 4)==0) {
238 pix_fmt = PIX_FMT_GRAY8;
240 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
243 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
245 case 'I': // Interlace type
246 switch (*tokstart++){
261 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
264 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
268 case 'F': // Frame rate
269 sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
270 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
272 case 'A': // Pixel aspect
273 sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
274 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
276 case 'X': // Vendor extensions
277 if (strncmp("YSCSS=",tokstart,6)==0) {
278 // Older nonstandard pixel format representation
280 if (strncmp("420JPEG",tokstart,7)==0)
281 alt_pix_fmt=PIX_FMT_YUV420P;
282 else if (strncmp("420MPEG2",tokstart,8)==0)
283 alt_pix_fmt=PIX_FMT_YUV420P;
284 else if (strncmp("420PALDV",tokstart,8)==0)
285 alt_pix_fmt=PIX_FMT_YUV420P;
286 else if (strncmp("411",tokstart,3)==0)
287 alt_pix_fmt=PIX_FMT_YUV411P;
288 else if (strncmp("422",tokstart,3)==0)
289 alt_pix_fmt=PIX_FMT_YUV422P;
290 else if (strncmp("444",tokstart,3)==0)
291 alt_pix_fmt=PIX_FMT_YUV444P;
293 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
298 if ((width == -1) || (height == -1)) {
299 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
303 if (pix_fmt == PIX_FMT_NONE) {
304 if (alt_pix_fmt == PIX_FMT_NONE)
305 pix_fmt = PIX_FMT_YUV420P;
307 pix_fmt = alt_pix_fmt;
310 if (raten == 0 && rated == 0) {
311 // Frame rate unknown
316 if (aspectn == 0 && aspectd == 0) {
317 // Pixel aspect unknown
321 st = av_new_stream(s, 0);
323 st->codec.width = width;
324 st->codec.height = height;
325 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
326 av_set_pts_info(st, 64, rated, raten);
327 st->codec.pix_fmt = pix_fmt;
328 st->codec.codec_type = CODEC_TYPE_VIDEO;
329 st->codec.codec_id = CODEC_ID_RAWVIDEO;
330 st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
335 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
338 char header[MAX_FRAME_HEADER+1];
339 int packet_size, ret, width, height;
340 AVStream *st = s->streams[0];
342 for (i=0; i<MAX_FRAME_HEADER; i++) {
343 header[i] = get_byte(&s->pb);
344 if (header[i] == '\n') {
349 if (i == MAX_FRAME_HEADER) return -1;
350 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
352 width = st->codec.width;
353 height = st->codec.height;
355 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
359 if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
362 pkt->stream_index = 0;
366 static int yuv4_read_close(AVFormatContext *s)
371 static int yuv4_probe(AVProbeData *pd)
373 /* check file header */
374 if (pd->buf_size <= sizeof(Y4M_MAGIC))
376 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
377 return AVPROBE_SCORE_MAX;
382 AVInputFormat yuv4mpegpipe_iformat = {
384 "YUV4MPEG pipe format",
393 int yuv4mpeg_init(void)
395 av_register_input_format(&yuv4mpegpipe_iformat);
396 #ifdef CONFIG_ENCODERS
397 av_register_output_format(&yuv4mpegpipe_oformat);
398 #endif //CONFIG_ENCODERS