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 #include "libavutil/pixdesc.h"
25 #define Y4M_MAGIC "YUV4MPEG2"
26 #define Y4M_FRAME_MAGIC "FRAME"
27 #define Y4M_LINE_MAX 256
29 struct frame_attributes {
34 #if CONFIG_YUV4MPEGPIPE_MUXER
35 static int yuv4_generate_header(AVFormatContext *s, char* buf)
39 int raten, rated, aspectn, aspectd, n;
41 const char *colorspace = "";
44 width = st->codec->width;
45 height = st->codec->height;
47 av_reduce(&raten, &rated, st->codec->time_base.den,
48 st->codec->time_base.num, (1UL << 31) - 1);
50 aspectn = st->sample_aspect_ratio.num;
51 aspectd = st->sample_aspect_ratio.den;
53 if (aspectn == 0 && aspectd == 1)
54 aspectd = 0; // 0:0 means unknown
56 inter = 'p'; /* progressive is the default */
57 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
58 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
60 switch (st->codec->pix_fmt) {
61 case AV_PIX_FMT_GRAY8:
62 colorspace = " Cmono";
64 case AV_PIX_FMT_GRAY16:
65 colorspace = " Cmono16";
67 case AV_PIX_FMT_YUV411P:
68 colorspace = " C411 XYSCSS=411";
70 case AV_PIX_FMT_YUV420P:
71 switch (st->codec->chroma_sample_location) {
72 case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
73 case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
74 default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
77 case AV_PIX_FMT_YUV422P:
78 colorspace = " C422 XYSCSS=422";
80 case AV_PIX_FMT_YUV444P:
81 colorspace = " C444 XYSCSS=444";
83 case AV_PIX_FMT_YUV420P9:
84 colorspace = " C420p9 XYSCSS=420P9";
86 case AV_PIX_FMT_YUV422P9:
87 colorspace = " C422p9 XYSCSS=422P9";
89 case AV_PIX_FMT_YUV444P9:
90 colorspace = " C444p9 XYSCSS=444P9";
92 case AV_PIX_FMT_YUV420P10:
93 colorspace = " C420p10 XYSCSS=420P10";
95 case AV_PIX_FMT_YUV422P10:
96 colorspace = " C422p10 XYSCSS=422P10";
98 case AV_PIX_FMT_YUV444P10:
99 colorspace = " C444p10 XYSCSS=444P10";
101 case AV_PIX_FMT_YUV420P16:
102 colorspace = " C420p16 XYSCSS=420P16";
104 case AV_PIX_FMT_YUV422P16:
105 colorspace = " C422p16 XYSCSS=422P16";
107 case AV_PIX_FMT_YUV444P16:
108 colorspace = " C444p16 XYSCSS=444P16";
112 /* construct stream header, if this is the first frame */
113 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
114 Y4M_MAGIC, width, height, raten, rated, inter,
115 aspectn, aspectd, colorspace);
120 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
122 AVStream *st = s->streams[pkt->stream_index];
123 AVIOContext *pb = s->pb;
124 AVPicture *picture, picture_tmp;
125 int* first_pkt = s->priv_data;
126 int width, height, h_chroma_shift, v_chroma_shift;
128 char buf2[Y4M_LINE_MAX + 1];
130 uint8_t *ptr, *ptr1, *ptr2;
132 memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
133 picture = &picture_tmp;
135 /* for the first packet we have to output the header as well */
138 if (yuv4_generate_header(s, buf2) < 0) {
139 av_log(s, AV_LOG_ERROR,
140 "Error. YUV4MPEG stream header write failed.\n");
143 avio_write(pb, buf2, strlen(buf2));
147 /* construct frame header */
149 snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
150 avio_write(pb, buf1, strlen(buf1));
152 width = st->codec->width;
153 height = st->codec->height;
155 ptr = picture->data[0];
157 switch (st->codec->pix_fmt) {
158 case AV_PIX_FMT_GRAY8:
159 case AV_PIX_FMT_YUV411P:
160 case AV_PIX_FMT_YUV420P:
161 case AV_PIX_FMT_YUV422P:
162 case AV_PIX_FMT_YUV444P:
164 case AV_PIX_FMT_GRAY16:
165 case AV_PIX_FMT_YUV420P9:
166 case AV_PIX_FMT_YUV422P9:
167 case AV_PIX_FMT_YUV444P9:
168 case AV_PIX_FMT_YUV420P10:
169 case AV_PIX_FMT_YUV422P10:
170 case AV_PIX_FMT_YUV444P10:
171 case AV_PIX_FMT_YUV420P16:
172 case AV_PIX_FMT_YUV422P16:
173 case AV_PIX_FMT_YUV444P16:
177 av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
178 av_get_pix_fmt_name(st->codec->pix_fmt));
179 return AVERROR(EINVAL);
182 for (i = 0; i < height; i++) {
183 avio_write(pb, ptr, width);
184 ptr += picture->linesize[0];
187 if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
188 st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
189 // Adjust for smaller Cb and Cr planes
190 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
192 width >>= h_chroma_shift;
193 height >>= v_chroma_shift;
195 ptr1 = picture->data[1];
196 ptr2 = picture->data[2];
197 for (i = 0; i < height; i++) { /* Cb */
198 avio_write(pb, ptr1, width);
199 ptr1 += picture->linesize[1];
201 for (i = 0; i < height; i++) { /* Cr */
202 avio_write(pb, ptr2, width);
203 ptr2 += picture->linesize[2];
211 static int yuv4_write_header(AVFormatContext *s)
213 int *first_pkt = s->priv_data;
215 if (s->nb_streams != 1)
218 if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
219 av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
220 return AVERROR_INVALIDDATA;
223 switch (s->streams[0]->codec->pix_fmt) {
224 case AV_PIX_FMT_YUV411P:
225 av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
226 "stream, some mjpegtools might not work.\n");
228 case AV_PIX_FMT_GRAY8:
229 case AV_PIX_FMT_GRAY16:
230 case AV_PIX_FMT_YUV420P:
231 case AV_PIX_FMT_YUV422P:
232 case AV_PIX_FMT_YUV444P:
234 case AV_PIX_FMT_YUV420P9:
235 case AV_PIX_FMT_YUV422P9:
236 case AV_PIX_FMT_YUV444P9:
237 case AV_PIX_FMT_YUV420P10:
238 case AV_PIX_FMT_YUV422P10:
239 case AV_PIX_FMT_YUV444P10:
240 case AV_PIX_FMT_YUV420P16:
241 case AV_PIX_FMT_YUV422P16:
242 case AV_PIX_FMT_YUV444P16:
243 if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
244 av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
245 "Use '-strict -1' to encode to this pixel format.\n",
246 av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
247 return AVERROR(EINVAL);
249 av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
250 "Mjpegtools will not work.\n");
253 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
254 "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
255 "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
256 "yuv444p10, yuv422p10, yuv420p10, "
257 "yuv444p16, yuv422p16, yuv420p16 "
258 "and gray16 pixel formats. "
259 "Use -pix_fmt to select one.\n");
267 AVOutputFormat ff_yuv4mpegpipe_muxer = {
268 .name = "yuv4mpegpipe",
269 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
271 .priv_data_size = sizeof(int),
272 .audio_codec = AV_CODEC_ID_NONE,
273 .video_codec = AV_CODEC_ID_RAWVIDEO,
274 .write_header = yuv4_write_header,
275 .write_packet = yuv4_write_packet,
276 .flags = AVFMT_RAWPICTURE,
280 /* Header size increased to allow room for optional flags */
281 #define MAX_YUV4_HEADER 80
282 #define MAX_FRAME_HEADER 80
284 static int yuv4_read_header(AVFormatContext *s)
286 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
287 // the longest option
288 char *tokstart, *tokend, *header_end;
290 AVIOContext *pb = s->pb;
291 int width = -1, height = -1, raten = 0,
292 rated = 0, aspectn = 0, aspectd = 0;
293 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
294 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
296 struct frame_attributes *s1 = s->priv_data;
298 for (i = 0; i < MAX_YUV4_HEADER; i++) {
299 header[i] = avio_r8(pb);
300 if (header[i] == '\n') {
301 header[i + 1] = 0x20; // Add a space after last option.
302 // Makes parsing "444" vs "444alpha" easier.
307 if (i == MAX_YUV4_HEADER)
309 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
312 s1->interlaced_frame = 0;
313 s1->top_field_first = 0;
314 header_end = &header[i + 1]; // Include space
315 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
316 tokstart < header_end; tokstart++) {
317 if (*tokstart == 0x20)
319 switch (*tokstart++) {
320 case 'W': // Width. Required.
321 width = strtol(tokstart, &tokend, 10);
324 case 'H': // Height. Required.
325 height = strtol(tokstart, &tokend, 10);
328 case 'C': // Color space
329 if (strncmp("420jpeg", tokstart, 7) == 0) {
330 pix_fmt = AV_PIX_FMT_YUV420P;
331 chroma_sample_location = AVCHROMA_LOC_CENTER;
332 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
333 pix_fmt = AV_PIX_FMT_YUV420P;
334 chroma_sample_location = AVCHROMA_LOC_LEFT;
335 } else if (strncmp("420paldv", tokstart, 8) == 0) {
336 pix_fmt = AV_PIX_FMT_YUV420P;
337 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
338 } else if (strncmp("420p16", tokstart, 6) == 0) {
339 pix_fmt = AV_PIX_FMT_YUV420P16;
340 } else if (strncmp("422p16", tokstart, 6) == 0) {
341 pix_fmt = AV_PIX_FMT_YUV422P16;
342 } else if (strncmp("444p16", tokstart, 6) == 0) {
343 pix_fmt = AV_PIX_FMT_YUV444P16;
344 } else if (strncmp("420p10", tokstart, 6) == 0) {
345 pix_fmt = AV_PIX_FMT_YUV420P10;
346 } else if (strncmp("422p10", tokstart, 6) == 0) {
347 pix_fmt = AV_PIX_FMT_YUV422P10;
348 } else if (strncmp("444p10", tokstart, 6) == 0) {
349 pix_fmt = AV_PIX_FMT_YUV444P10;
350 } else if (strncmp("420p9", tokstart, 5) == 0) {
351 pix_fmt = AV_PIX_FMT_YUV420P9;
352 } else if (strncmp("422p9", tokstart, 5) == 0) {
353 pix_fmt = AV_PIX_FMT_YUV422P9;
354 } else if (strncmp("444p9", tokstart, 5) == 0) {
355 pix_fmt = AV_PIX_FMT_YUV444P9;
356 } else if (strncmp("420", tokstart, 3) == 0) {
357 pix_fmt = AV_PIX_FMT_YUV420P;
358 chroma_sample_location = AVCHROMA_LOC_CENTER;
359 } else if (strncmp("411", tokstart, 3) == 0) {
360 pix_fmt = AV_PIX_FMT_YUV411P;
361 } else if (strncmp("422", tokstart, 3) == 0) {
362 pix_fmt = AV_PIX_FMT_YUV422P;
363 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
364 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
365 "YUV4MPEG stream.\n");
367 } else if (strncmp("444", tokstart, 3) == 0) {
368 pix_fmt = AV_PIX_FMT_YUV444P;
369 } else if (strncmp("mono16", tokstart, 6) == 0) {
370 pix_fmt = AV_PIX_FMT_GRAY16;
371 } else if (strncmp("mono", tokstart, 4) == 0) {
372 pix_fmt = AV_PIX_FMT_GRAY8;
374 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
378 while (tokstart < header_end && *tokstart != 0x20)
381 case 'I': // Interlace type
382 switch (*tokstart++){
386 s1->interlaced_frame = 0;
389 s1->interlaced_frame = 1;
390 s1->top_field_first = 1;
393 s1->interlaced_frame = 1;
394 s1->top_field_first = 0;
397 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
398 "interlaced and non-interlaced frames.\n");
401 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
405 case 'F': // Frame rate
406 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
407 while (tokstart < header_end && *tokstart != 0x20)
410 case 'A': // Pixel aspect
411 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
412 while (tokstart < header_end && *tokstart != 0x20)
415 case 'X': // Vendor extensions
416 if (strncmp("YSCSS=", tokstart, 6) == 0) {
417 // Older nonstandard pixel format representation
419 if (strncmp("420JPEG", tokstart, 7) == 0)
420 alt_pix_fmt = AV_PIX_FMT_YUV420P;
421 else if (strncmp("420MPEG2", tokstart, 8) == 0)
422 alt_pix_fmt = AV_PIX_FMT_YUV420P;
423 else if (strncmp("420PALDV", tokstart, 8) == 0)
424 alt_pix_fmt = AV_PIX_FMT_YUV420P;
425 else if (strncmp("420P9", tokstart, 5) == 0)
426 alt_pix_fmt = AV_PIX_FMT_YUV420P9;
427 else if (strncmp("422P9", tokstart, 5) == 0)
428 alt_pix_fmt = AV_PIX_FMT_YUV422P9;
429 else if (strncmp("444P9", tokstart, 5) == 0)
430 alt_pix_fmt = AV_PIX_FMT_YUV444P9;
431 else if (strncmp("420P10", tokstart, 6) == 0)
432 alt_pix_fmt = AV_PIX_FMT_YUV420P10;
433 else if (strncmp("422P10", tokstart, 6) == 0)
434 alt_pix_fmt = AV_PIX_FMT_YUV422P10;
435 else if (strncmp("444P10", tokstart, 6) == 0)
436 alt_pix_fmt = AV_PIX_FMT_YUV444P10;
437 else if (strncmp("420P16", tokstart, 6) == 0)
438 alt_pix_fmt = AV_PIX_FMT_YUV420P16;
439 else if (strncmp("422P16", tokstart, 6) == 0)
440 alt_pix_fmt = AV_PIX_FMT_YUV422P16;
441 else if (strncmp("444P16", tokstart, 6) == 0)
442 alt_pix_fmt = AV_PIX_FMT_YUV444P16;
443 else if (strncmp("411", tokstart, 3) == 0)
444 alt_pix_fmt = AV_PIX_FMT_YUV411P;
445 else if (strncmp("422", tokstart, 3) == 0)
446 alt_pix_fmt = AV_PIX_FMT_YUV422P;
447 else if (strncmp("444", tokstart, 3) == 0)
448 alt_pix_fmt = AV_PIX_FMT_YUV444P;
450 while (tokstart < header_end && *tokstart != 0x20)
456 if (width == -1 || height == -1) {
457 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
461 if (pix_fmt == AV_PIX_FMT_NONE) {
462 if (alt_pix_fmt == AV_PIX_FMT_NONE)
463 pix_fmt = AV_PIX_FMT_YUV420P;
465 pix_fmt = alt_pix_fmt;
468 if (raten <= 0 || rated <= 0) {
469 // Frame rate unknown
474 if (aspectn == 0 && aspectd == 0) {
475 // Pixel aspect unknown
479 st = avformat_new_stream(s, NULL);
481 return AVERROR(ENOMEM);
482 st->codec->width = width;
483 st->codec->height = height;
484 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
485 avpriv_set_pts_info(st, 64, rated, raten);
486 st->codec->pix_fmt = pix_fmt;
487 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
488 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
489 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
490 st->codec->chroma_sample_location = chroma_sample_location;
495 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
498 char header[MAX_FRAME_HEADER+1];
499 int packet_size, width, height, ret;
500 AVStream *st = s->streams[0];
501 struct frame_attributes *s1 = s->priv_data;
503 for (i = 0; i < MAX_FRAME_HEADER; i++) {
504 header[i] = avio_r8(s->pb);
505 if (header[i] == '\n') {
512 else if (s->pb->eof_reached)
514 else if (i == MAX_FRAME_HEADER)
515 return AVERROR_INVALIDDATA;
517 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
518 return AVERROR_INVALIDDATA;
520 width = st->codec->width;
521 height = st->codec->height;
523 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
527 ret = av_get_packet(s->pb, pkt, packet_size);
530 else if (ret != packet_size)
531 return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
533 if (st->codec->coded_frame) {
534 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
535 st->codec->coded_frame->top_field_first = s1->top_field_first;
538 pkt->stream_index = 0;
542 static int yuv4_probe(AVProbeData *pd)
544 /* check file header */
545 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
546 return AVPROBE_SCORE_MAX;
551 #if CONFIG_YUV4MPEGPIPE_DEMUXER
552 AVInputFormat ff_yuv4mpegpipe_demuxer = {
553 .name = "yuv4mpegpipe",
554 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
555 .priv_data_size = sizeof(struct frame_attributes),
556 .read_probe = yuv4_probe,
557 .read_header = yuv4_read_header,
558 .read_packet = yuv4_read_packet,