3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define Y4M_MAGIC "YUV4MPEG2"
24 #define Y4M_FRAME_MAGIC "FRAME"
25 #define Y4M_LINE_MAX 256
27 struct frame_attributes {
32 #if CONFIG_YUV4MPEGPIPE_MUXER
33 static int yuv4_generate_header(AVFormatContext *s, char* buf)
37 int raten, rated, aspectn, aspectd, n;
39 const char *colorspace = "";
42 width = st->codec->width;
43 height = st->codec->height;
45 av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
47 aspectn = st->sample_aspect_ratio.num;
48 aspectd = st->sample_aspect_ratio.den;
50 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
52 inter = 'p'; /* progressive is the default */
53 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
54 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
57 switch(st->codec->pix_fmt) {
59 colorspace = " Cmono";
62 colorspace = " C411 XYSCSS=411";
65 colorspace = (st->codec->chroma_sample_location == AVCHROMA_LOC_TOPLEFT)?" C420paldv XYSCSS=420PALDV":
66 (st->codec->chroma_sample_location == AVCHROMA_LOC_LEFT) ?" C420mpeg2 XYSCSS=420MPEG2":
67 " C420jpeg XYSCSS=420JPEG";
70 colorspace = " C422 XYSCSS=422";
73 colorspace = " C444 XYSCSS=444";
77 /* construct stream header, if this is the first frame */
78 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
90 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
92 AVStream *st = s->streams[pkt->stream_index];
93 AVIOContext *pb = s->pb;
95 int* first_pkt = s->priv_data;
96 int width, height, h_chroma_shift, v_chroma_shift;
98 char buf2[Y4M_LINE_MAX+1];
100 uint8_t *ptr, *ptr1, *ptr2;
102 picture = (AVPicture *)pkt->data;
104 /* for the first packet we have to output the header as well */
107 if (yuv4_generate_header(s, buf2) < 0) {
108 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
111 avio_write(pb, buf2, strlen(buf2));
115 /* construct frame header */
117 snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
118 avio_write(pb, buf1, strlen(buf1));
120 width = st->codec->width;
121 height = st->codec->height;
123 ptr = picture->data[0];
124 for(i=0;i<height;i++) {
125 avio_write(pb, ptr, width);
126 ptr += picture->linesize[0];
129 if (st->codec->pix_fmt != PIX_FMT_GRAY8){
130 // Adjust for smaller Cb and Cr planes
131 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
132 width >>= h_chroma_shift;
133 height >>= v_chroma_shift;
135 ptr1 = picture->data[1];
136 ptr2 = picture->data[2];
137 for(i=0;i<height;i++) { /* Cb */
138 avio_write(pb, ptr1, width);
139 ptr1 += picture->linesize[1];
141 for(i=0;i<height;i++) { /* Cr */
142 avio_write(pb, ptr2, width);
143 ptr2 += picture->linesize[2];
150 static int yuv4_write_header(AVFormatContext *s)
152 int* first_pkt = s->priv_data;
154 if (s->nb_streams != 1)
157 if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
158 av_log(s, AV_LOG_ERROR,
159 "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
160 return AVERROR(EINVAL);
163 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
164 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
166 else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
167 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
168 (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
169 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
170 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");
178 AVOutputFormat ff_yuv4mpegpipe_muxer = {
179 .name = "yuv4mpegpipe",
180 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
183 .priv_data_size = sizeof(int),
184 .audio_codec = CODEC_ID_NONE,
185 .video_codec = CODEC_ID_RAWVIDEO,
186 .write_header = yuv4_write_header,
187 .write_packet = yuv4_write_packet,
188 .flags = AVFMT_RAWPICTURE,
192 /* Header size increased to allow room for optional flags */
193 #define MAX_YUV4_HEADER 80
194 #define MAX_FRAME_HEADER 80
196 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
198 char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
199 char *tokstart,*tokend,*header_end;
201 AVIOContext *pb = s->pb;
202 int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
203 enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
204 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
206 struct frame_attributes *s1 = s->priv_data;
208 for (i=0; i<MAX_YUV4_HEADER; i++) {
209 header[i] = avio_r8(pb);
210 if (header[i] == '\n') {
211 header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
216 if (i == MAX_YUV4_HEADER) return -1;
217 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
219 s1->interlaced_frame = 0;
220 s1->top_field_first = 0;
221 header_end = &header[i+1]; // Include space
222 for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
223 if (*tokstart==0x20) continue;
224 switch (*tokstart++) {
225 case 'W': // Width. Required.
226 width = strtol(tokstart, &tokend, 10);
229 case 'H': // Height. Required.
230 height = strtol(tokstart, &tokend, 10);
233 case 'C': // Color space
234 if (strncmp("420jpeg",tokstart,7)==0) {
235 pix_fmt = PIX_FMT_YUV420P;
236 chroma_sample_location = AVCHROMA_LOC_CENTER;
237 } else if (strncmp("420mpeg2",tokstart,8)==0) {
238 pix_fmt = PIX_FMT_YUV420P;
239 chroma_sample_location = AVCHROMA_LOC_LEFT;
240 } else if (strncmp("420paldv", tokstart, 8)==0) {
241 pix_fmt = PIX_FMT_YUV420P;
242 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
243 } else if (strncmp("411", tokstart, 3)==0)
244 pix_fmt = PIX_FMT_YUV411P;
245 else if (strncmp("422", tokstart, 3)==0)
246 pix_fmt = PIX_FMT_YUV422P;
247 else if (strncmp("444alpha", tokstart, 8)==0) {
248 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
250 } else if (strncmp("444", tokstart, 3)==0)
251 pix_fmt = PIX_FMT_YUV444P;
252 else if (strncmp("mono",tokstart, 4)==0) {
253 pix_fmt = PIX_FMT_GRAY8;
255 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
258 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
260 case 'I': // Interlace type
261 switch (*tokstart++){
265 s1->interlaced_frame=0;
268 s1->interlaced_frame=1;
269 s1->top_field_first=1;
272 s1->interlaced_frame=1;
273 s1->top_field_first=0;
276 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
279 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
283 case 'F': // Frame rate
284 sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
285 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
287 case 'A': // Pixel aspect
288 sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
289 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
291 case 'X': // Vendor extensions
292 if (strncmp("YSCSS=",tokstart,6)==0) {
293 // Older nonstandard pixel format representation
295 if (strncmp("420JPEG",tokstart,7)==0)
296 alt_pix_fmt=PIX_FMT_YUV420P;
297 else if (strncmp("420MPEG2",tokstart,8)==0)
298 alt_pix_fmt=PIX_FMT_YUV420P;
299 else if (strncmp("420PALDV",tokstart,8)==0)
300 alt_pix_fmt=PIX_FMT_YUV420P;
301 else if (strncmp("411",tokstart,3)==0)
302 alt_pix_fmt=PIX_FMT_YUV411P;
303 else if (strncmp("422",tokstart,3)==0)
304 alt_pix_fmt=PIX_FMT_YUV422P;
305 else if (strncmp("444",tokstart,3)==0)
306 alt_pix_fmt=PIX_FMT_YUV444P;
308 while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
313 if ((width == -1) || (height == -1)) {
314 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
318 if (pix_fmt == PIX_FMT_NONE) {
319 if (alt_pix_fmt == PIX_FMT_NONE)
320 pix_fmt = PIX_FMT_YUV420P;
322 pix_fmt = alt_pix_fmt;
325 if (raten <= 0 || rated <= 0) {
326 // Frame rate unknown
331 if (aspectn == 0 && aspectd == 0) {
332 // Pixel aspect unknown
336 st = avformat_new_stream(s, NULL);
338 return AVERROR(ENOMEM);
339 st->codec->width = width;
340 st->codec->height = height;
341 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
342 av_set_pts_info(st, 64, rated, raten);
343 st->codec->pix_fmt = pix_fmt;
344 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
345 st->codec->codec_id = CODEC_ID_RAWVIDEO;
346 st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
347 st->codec->chroma_sample_location = chroma_sample_location;
352 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
355 char header[MAX_FRAME_HEADER+1];
356 int packet_size, width, height;
357 AVStream *st = s->streams[0];
358 struct frame_attributes *s1 = s->priv_data;
360 for (i=0; i<MAX_FRAME_HEADER; i++) {
361 header[i] = avio_r8(s->pb);
362 if (header[i] == '\n') {
367 if (i == MAX_FRAME_HEADER) return -1;
368 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
370 width = st->codec->width;
371 height = st->codec->height;
373 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
377 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
380 if (s->streams[0]->codec->coded_frame) {
381 s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
382 s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
385 pkt->stream_index = 0;
389 static int yuv4_probe(AVProbeData *pd)
391 /* check file header */
392 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
393 return AVPROBE_SCORE_MAX;
398 #if CONFIG_YUV4MPEGPIPE_DEMUXER
399 AVInputFormat ff_yuv4mpegpipe_demuxer = {
400 .name = "yuv4mpegpipe",
401 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
402 .priv_data_size = sizeof(struct frame_attributes),
403 .read_probe = yuv4_probe,
404 .read_header = yuv4_read_header,
405 .read_packet = yuv4_read_packet,