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_YUV420P12:
102 colorspace = " C420p12 XYSCSS=420P12";
104 case AV_PIX_FMT_YUV422P12:
105 colorspace = " C422p12 XYSCSS=422P12";
107 case AV_PIX_FMT_YUV444P12:
108 colorspace = " C444p12 XYSCSS=444P12";
110 case AV_PIX_FMT_YUV420P14:
111 colorspace = " C420p14 XYSCSS=420P14";
113 case AV_PIX_FMT_YUV422P14:
114 colorspace = " C422p14 XYSCSS=422P14";
116 case AV_PIX_FMT_YUV444P14:
117 colorspace = " C444p14 XYSCSS=444P14";
119 case AV_PIX_FMT_YUV420P16:
120 colorspace = " C420p16 XYSCSS=420P16";
122 case AV_PIX_FMT_YUV422P16:
123 colorspace = " C422p16 XYSCSS=422P16";
125 case AV_PIX_FMT_YUV444P16:
126 colorspace = " C444p16 XYSCSS=444P16";
130 /* construct stream header, if this is the first frame */
131 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
132 Y4M_MAGIC, width, height, raten, rated, inter,
133 aspectn, aspectd, colorspace);
138 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
140 AVStream *st = s->streams[pkt->stream_index];
141 AVIOContext *pb = s->pb;
142 AVPicture *picture, picture_tmp;
143 int* first_pkt = s->priv_data;
144 int width, height, h_chroma_shift, v_chroma_shift;
146 char buf2[Y4M_LINE_MAX + 1];
148 uint8_t *ptr, *ptr1, *ptr2;
150 memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
151 picture = &picture_tmp;
153 /* for the first packet we have to output the header as well */
156 if (yuv4_generate_header(s, buf2) < 0) {
157 av_log(s, AV_LOG_ERROR,
158 "Error. YUV4MPEG stream header write failed.\n");
161 avio_write(pb, buf2, strlen(buf2));
165 /* construct frame header */
167 snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
168 avio_write(pb, buf1, strlen(buf1));
170 width = st->codec->width;
171 height = st->codec->height;
173 ptr = picture->data[0];
175 switch (st->codec->pix_fmt) {
176 case AV_PIX_FMT_GRAY8:
177 case AV_PIX_FMT_YUV411P:
178 case AV_PIX_FMT_YUV420P:
179 case AV_PIX_FMT_YUV422P:
180 case AV_PIX_FMT_YUV444P:
182 case AV_PIX_FMT_GRAY16:
183 case AV_PIX_FMT_YUV420P9:
184 case AV_PIX_FMT_YUV422P9:
185 case AV_PIX_FMT_YUV444P9:
186 case AV_PIX_FMT_YUV420P10:
187 case AV_PIX_FMT_YUV422P10:
188 case AV_PIX_FMT_YUV444P10:
189 case AV_PIX_FMT_YUV420P12:
190 case AV_PIX_FMT_YUV422P12:
191 case AV_PIX_FMT_YUV444P12:
192 case AV_PIX_FMT_YUV420P14:
193 case AV_PIX_FMT_YUV422P14:
194 case AV_PIX_FMT_YUV444P14:
195 case AV_PIX_FMT_YUV420P16:
196 case AV_PIX_FMT_YUV422P16:
197 case AV_PIX_FMT_YUV444P16:
201 av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
202 av_get_pix_fmt_name(st->codec->pix_fmt));
203 return AVERROR(EINVAL);
206 for (i = 0; i < height; i++) {
207 avio_write(pb, ptr, width);
208 ptr += picture->linesize[0];
211 if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
212 st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
213 // Adjust for smaller Cb and Cr planes
214 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
216 width >>= h_chroma_shift;
217 height >>= v_chroma_shift;
219 ptr1 = picture->data[1];
220 ptr2 = picture->data[2];
221 for (i = 0; i < height; i++) { /* Cb */
222 avio_write(pb, ptr1, width);
223 ptr1 += picture->linesize[1];
225 for (i = 0; i < height; i++) { /* Cr */
226 avio_write(pb, ptr2, width);
227 ptr2 += picture->linesize[2];
235 static int yuv4_write_header(AVFormatContext *s)
237 int *first_pkt = s->priv_data;
239 if (s->nb_streams != 1)
242 if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
243 av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
244 return AVERROR_INVALIDDATA;
247 switch (s->streams[0]->codec->pix_fmt) {
248 case AV_PIX_FMT_YUV411P:
249 av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
250 "stream, some mjpegtools might not work.\n");
252 case AV_PIX_FMT_GRAY8:
253 case AV_PIX_FMT_GRAY16:
254 case AV_PIX_FMT_YUV420P:
255 case AV_PIX_FMT_YUV422P:
256 case AV_PIX_FMT_YUV444P:
258 case AV_PIX_FMT_YUV420P9:
259 case AV_PIX_FMT_YUV422P9:
260 case AV_PIX_FMT_YUV444P9:
261 case AV_PIX_FMT_YUV420P10:
262 case AV_PIX_FMT_YUV422P10:
263 case AV_PIX_FMT_YUV444P10:
264 case AV_PIX_FMT_YUV420P12:
265 case AV_PIX_FMT_YUV422P12:
266 case AV_PIX_FMT_YUV444P12:
267 case AV_PIX_FMT_YUV420P14:
268 case AV_PIX_FMT_YUV422P14:
269 case AV_PIX_FMT_YUV444P14:
270 case AV_PIX_FMT_YUV420P16:
271 case AV_PIX_FMT_YUV422P16:
272 case AV_PIX_FMT_YUV444P16:
273 if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
274 av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
275 "Use '-strict -1' to encode to this pixel format.\n",
276 av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
277 return AVERROR(EINVAL);
279 av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
280 "Mjpegtools will not work.\n");
283 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
284 "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
285 "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
286 "yuv444p10, yuv422p10, yuv420p10, "
287 "yuv444p12, yuv422p12, yuv420p12, "
288 "yuv444p14, yuv422p14, yuv420p14, "
289 "yuv444p16, yuv422p16, yuv420p16 "
290 "and gray16 pixel formats. "
291 "Use -pix_fmt to select one.\n");
299 AVOutputFormat ff_yuv4mpegpipe_muxer = {
300 .name = "yuv4mpegpipe",
301 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
303 .priv_data_size = sizeof(int),
304 .audio_codec = AV_CODEC_ID_NONE,
305 .video_codec = AV_CODEC_ID_RAWVIDEO,
306 .write_header = yuv4_write_header,
307 .write_packet = yuv4_write_packet,
308 .flags = AVFMT_RAWPICTURE,
312 /* Header size increased to allow room for optional flags */
313 #define MAX_YUV4_HEADER 80
314 #define MAX_FRAME_HEADER 80
316 static int yuv4_read_header(AVFormatContext *s)
318 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
319 // the longest option
320 char *tokstart, *tokend, *header_end;
322 AVIOContext *pb = s->pb;
323 int width = -1, height = -1, raten = 0,
324 rated = 0, aspectn = 0, aspectd = 0;
325 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
326 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
328 struct frame_attributes *s1 = s->priv_data;
330 for (i = 0; i < MAX_YUV4_HEADER; i++) {
331 header[i] = avio_r8(pb);
332 if (header[i] == '\n') {
333 header[i + 1] = 0x20; // Add a space after last option.
334 // Makes parsing "444" vs "444alpha" easier.
339 if (i == MAX_YUV4_HEADER)
341 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
344 s1->interlaced_frame = 0;
345 s1->top_field_first = 0;
346 header_end = &header[i + 1]; // Include space
347 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
348 tokstart < header_end; tokstart++) {
349 if (*tokstart == 0x20)
351 switch (*tokstart++) {
352 case 'W': // Width. Required.
353 width = strtol(tokstart, &tokend, 10);
356 case 'H': // Height. Required.
357 height = strtol(tokstart, &tokend, 10);
360 case 'C': // Color space
361 if (strncmp("420jpeg", tokstart, 7) == 0) {
362 pix_fmt = AV_PIX_FMT_YUV420P;
363 chroma_sample_location = AVCHROMA_LOC_CENTER;
364 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
365 pix_fmt = AV_PIX_FMT_YUV420P;
366 chroma_sample_location = AVCHROMA_LOC_LEFT;
367 } else if (strncmp("420paldv", tokstart, 8) == 0) {
368 pix_fmt = AV_PIX_FMT_YUV420P;
369 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
370 } else if (strncmp("420p16", tokstart, 6) == 0) {
371 pix_fmt = AV_PIX_FMT_YUV420P16;
372 } else if (strncmp("422p16", tokstart, 6) == 0) {
373 pix_fmt = AV_PIX_FMT_YUV422P16;
374 } else if (strncmp("444p16", tokstart, 6) == 0) {
375 pix_fmt = AV_PIX_FMT_YUV444P16;
376 } else if (strncmp("420p14", tokstart, 6) == 0) {
377 pix_fmt = AV_PIX_FMT_YUV420P14;
378 } else if (strncmp("422p14", tokstart, 6) == 0) {
379 pix_fmt = AV_PIX_FMT_YUV422P14;
380 } else if (strncmp("444p14", tokstart, 6) == 0) {
381 pix_fmt = AV_PIX_FMT_YUV444P14;
382 } else if (strncmp("420p12", tokstart, 6) == 0) {
383 pix_fmt = AV_PIX_FMT_YUV420P12;
384 } else if (strncmp("422p12", tokstart, 6) == 0) {
385 pix_fmt = AV_PIX_FMT_YUV422P12;
386 } else if (strncmp("444p12", tokstart, 6) == 0) {
387 pix_fmt = AV_PIX_FMT_YUV444P12;
388 } else if (strncmp("420p10", tokstart, 6) == 0) {
389 pix_fmt = AV_PIX_FMT_YUV420P10;
390 } else if (strncmp("422p10", tokstart, 6) == 0) {
391 pix_fmt = AV_PIX_FMT_YUV422P10;
392 } else if (strncmp("444p10", tokstart, 6) == 0) {
393 pix_fmt = AV_PIX_FMT_YUV444P10;
394 } else if (strncmp("420p9", tokstart, 5) == 0) {
395 pix_fmt = AV_PIX_FMT_YUV420P9;
396 } else if (strncmp("422p9", tokstart, 5) == 0) {
397 pix_fmt = AV_PIX_FMT_YUV422P9;
398 } else if (strncmp("444p9", tokstart, 5) == 0) {
399 pix_fmt = AV_PIX_FMT_YUV444P9;
400 } else if (strncmp("420", tokstart, 3) == 0) {
401 pix_fmt = AV_PIX_FMT_YUV420P;
402 chroma_sample_location = AVCHROMA_LOC_CENTER;
403 } else if (strncmp("411", tokstart, 3) == 0) {
404 pix_fmt = AV_PIX_FMT_YUV411P;
405 } else if (strncmp("422", tokstart, 3) == 0) {
406 pix_fmt = AV_PIX_FMT_YUV422P;
407 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
408 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
409 "YUV4MPEG stream.\n");
411 } else if (strncmp("444", tokstart, 3) == 0) {
412 pix_fmt = AV_PIX_FMT_YUV444P;
413 } else if (strncmp("mono16", tokstart, 6) == 0) {
414 pix_fmt = AV_PIX_FMT_GRAY16;
415 } else if (strncmp("mono", tokstart, 4) == 0) {
416 pix_fmt = AV_PIX_FMT_GRAY8;
418 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
422 while (tokstart < header_end && *tokstart != 0x20)
425 case 'I': // Interlace type
426 switch (*tokstart++){
430 s1->interlaced_frame = 0;
433 s1->interlaced_frame = 1;
434 s1->top_field_first = 1;
437 s1->interlaced_frame = 1;
438 s1->top_field_first = 0;
441 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
442 "interlaced and non-interlaced frames.\n");
445 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
449 case 'F': // Frame rate
450 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
451 while (tokstart < header_end && *tokstart != 0x20)
454 case 'A': // Pixel aspect
455 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
456 while (tokstart < header_end && *tokstart != 0x20)
459 case 'X': // Vendor extensions
460 if (strncmp("YSCSS=", tokstart, 6) == 0) {
461 // Older nonstandard pixel format representation
463 if (strncmp("420JPEG", tokstart, 7) == 0)
464 alt_pix_fmt = AV_PIX_FMT_YUV420P;
465 else if (strncmp("420MPEG2", tokstart, 8) == 0)
466 alt_pix_fmt = AV_PIX_FMT_YUV420P;
467 else if (strncmp("420PALDV", tokstart, 8) == 0)
468 alt_pix_fmt = AV_PIX_FMT_YUV420P;
469 else if (strncmp("420P9", tokstart, 5) == 0)
470 alt_pix_fmt = AV_PIX_FMT_YUV420P9;
471 else if (strncmp("422P9", tokstart, 5) == 0)
472 alt_pix_fmt = AV_PIX_FMT_YUV422P9;
473 else if (strncmp("444P9", tokstart, 5) == 0)
474 alt_pix_fmt = AV_PIX_FMT_YUV444P9;
475 else if (strncmp("420P10", tokstart, 6) == 0)
476 alt_pix_fmt = AV_PIX_FMT_YUV420P10;
477 else if (strncmp("422P10", tokstart, 6) == 0)
478 alt_pix_fmt = AV_PIX_FMT_YUV422P10;
479 else if (strncmp("444P10", tokstart, 6) == 0)
480 alt_pix_fmt = AV_PIX_FMT_YUV444P10;
481 else if (strncmp("420P12", tokstart, 6) == 0)
482 alt_pix_fmt = AV_PIX_FMT_YUV420P12;
483 else if (strncmp("422P12", tokstart, 6) == 0)
484 alt_pix_fmt = AV_PIX_FMT_YUV422P12;
485 else if (strncmp("444P12", tokstart, 6) == 0)
486 alt_pix_fmt = AV_PIX_FMT_YUV444P12;
487 else if (strncmp("420P14", tokstart, 6) == 0)
488 alt_pix_fmt = AV_PIX_FMT_YUV420P14;
489 else if (strncmp("422P14", tokstart, 6) == 0)
490 alt_pix_fmt = AV_PIX_FMT_YUV422P14;
491 else if (strncmp("444P14", tokstart, 6) == 0)
492 alt_pix_fmt = AV_PIX_FMT_YUV444P14;
493 else if (strncmp("420P16", tokstart, 6) == 0)
494 alt_pix_fmt = AV_PIX_FMT_YUV420P16;
495 else if (strncmp("422P16", tokstart, 6) == 0)
496 alt_pix_fmt = AV_PIX_FMT_YUV422P16;
497 else if (strncmp("444P16", tokstart, 6) == 0)
498 alt_pix_fmt = AV_PIX_FMT_YUV444P16;
499 else if (strncmp("411", tokstart, 3) == 0)
500 alt_pix_fmt = AV_PIX_FMT_YUV411P;
501 else if (strncmp("422", tokstart, 3) == 0)
502 alt_pix_fmt = AV_PIX_FMT_YUV422P;
503 else if (strncmp("444", tokstart, 3) == 0)
504 alt_pix_fmt = AV_PIX_FMT_YUV444P;
506 while (tokstart < header_end && *tokstart != 0x20)
512 if (width == -1 || height == -1) {
513 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
517 if (pix_fmt == AV_PIX_FMT_NONE) {
518 if (alt_pix_fmt == AV_PIX_FMT_NONE)
519 pix_fmt = AV_PIX_FMT_YUV420P;
521 pix_fmt = alt_pix_fmt;
524 if (raten <= 0 || rated <= 0) {
525 // Frame rate unknown
530 if (aspectn == 0 && aspectd == 0) {
531 // Pixel aspect unknown
535 st = avformat_new_stream(s, NULL);
537 return AVERROR(ENOMEM);
538 st->codec->width = width;
539 st->codec->height = height;
540 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
541 avpriv_set_pts_info(st, 64, rated, raten);
542 st->codec->pix_fmt = pix_fmt;
543 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
544 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
545 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
546 st->codec->chroma_sample_location = chroma_sample_location;
551 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
554 char header[MAX_FRAME_HEADER+1];
555 int packet_size, width, height, ret;
556 AVStream *st = s->streams[0];
557 struct frame_attributes *s1 = s->priv_data;
559 for (i = 0; i < MAX_FRAME_HEADER; i++) {
560 header[i] = avio_r8(s->pb);
561 if (header[i] == '\n') {
568 else if (s->pb->eof_reached)
570 else if (i == MAX_FRAME_HEADER)
571 return AVERROR_INVALIDDATA;
573 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
574 return AVERROR_INVALIDDATA;
576 width = st->codec->width;
577 height = st->codec->height;
579 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
583 ret = av_get_packet(s->pb, pkt, packet_size);
586 else if (ret != packet_size)
587 return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
589 if (st->codec->coded_frame) {
590 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
591 st->codec->coded_frame->top_field_first = s1->top_field_first;
594 pkt->stream_index = 0;
598 static int yuv4_probe(AVProbeData *pd)
600 /* check file header */
601 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
602 return AVPROBE_SCORE_MAX;
607 #if CONFIG_YUV4MPEGPIPE_DEMUXER
608 AVInputFormat ff_yuv4mpegpipe_demuxer = {
609 .name = "yuv4mpegpipe",
610 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
611 .priv_data_size = sizeof(struct frame_attributes),
612 .read_probe = yuv4_probe,
613 .read_header = yuv4_read_header,
614 .read_packet = yuv4_read_packet,