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
22 #include "libavutil/pixdesc.h"
25 #include "libavutil/pixdesc.h"
27 #define Y4M_MAGIC "YUV4MPEG2"
28 #define Y4M_FRAME_MAGIC "FRAME"
29 #define Y4M_LINE_MAX 256
31 struct frame_attributes {
36 #if CONFIG_YUV4MPEGPIPE_MUXER
37 static int yuv4_generate_header(AVFormatContext *s, char* buf)
41 int raten, rated, aspectn, aspectd, n;
43 const char *colorspace = "";
46 width = st->codec->width;
47 height = st->codec->height;
49 av_reduce(&raten, &rated, st->codec->time_base.den,
50 st->codec->time_base.num, (1UL << 31) - 1);
52 aspectn = st->sample_aspect_ratio.num;
53 aspectd = st->sample_aspect_ratio.den;
55 if (aspectn == 0 && aspectd == 1)
56 aspectd = 0; // 0:0 means unknown
58 inter = 'p'; /* progressive is the default */
59 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
60 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
62 switch (st->codec->pix_fmt) {
63 case AV_PIX_FMT_GRAY8:
64 colorspace = " Cmono";
66 case AV_PIX_FMT_GRAY16:
67 colorspace = " Cmono16";
69 case AV_PIX_FMT_YUV411P:
70 colorspace = " C411 XYSCSS=411";
72 case AV_PIX_FMT_YUV420P:
73 switch (st->codec->chroma_sample_location) {
74 case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
75 case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
76 default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
79 case AV_PIX_FMT_YUV422P:
80 colorspace = " C422 XYSCSS=422";
82 case AV_PIX_FMT_YUV444P:
83 colorspace = " C444 XYSCSS=444";
85 case AV_PIX_FMT_YUV420P9:
86 colorspace = " C420p9 XYSCSS=420P9";
88 case AV_PIX_FMT_YUV422P9:
89 colorspace = " C422p9 XYSCSS=422P9";
91 case AV_PIX_FMT_YUV444P9:
92 colorspace = " C444p9 XYSCSS=444P9";
94 case AV_PIX_FMT_YUV420P10:
95 colorspace = " C420p10 XYSCSS=420P10";
97 case AV_PIX_FMT_YUV422P10:
98 colorspace = " C422p10 XYSCSS=422P10";
100 case AV_PIX_FMT_YUV444P10:
101 colorspace = " C444p10 XYSCSS=444P10";
103 case AV_PIX_FMT_YUV420P12:
104 colorspace = " C420p12 XYSCSS=420P12";
106 case AV_PIX_FMT_YUV422P12:
107 colorspace = " C422p12 XYSCSS=422P12";
109 case AV_PIX_FMT_YUV444P12:
110 colorspace = " C444p12 XYSCSS=444P12";
112 case AV_PIX_FMT_YUV420P14:
113 colorspace = " C420p14 XYSCSS=420P14";
115 case AV_PIX_FMT_YUV422P14:
116 colorspace = " C422p14 XYSCSS=422P14";
118 case AV_PIX_FMT_YUV444P14:
119 colorspace = " C444p14 XYSCSS=444P14";
121 case AV_PIX_FMT_YUV420P16:
122 colorspace = " C420p16 XYSCSS=420P16";
124 case AV_PIX_FMT_YUV422P16:
125 colorspace = " C422p16 XYSCSS=422P16";
127 case AV_PIX_FMT_YUV444P16:
128 colorspace = " C444p16 XYSCSS=444P16";
132 /* construct stream header, if this is the first frame */
133 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
134 Y4M_MAGIC, width, height, raten, rated, inter,
135 aspectn, aspectd, colorspace);
140 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
142 AVStream *st = s->streams[pkt->stream_index];
143 AVIOContext *pb = s->pb;
144 AVPicture *picture, picture_tmp;
145 int* first_pkt = s->priv_data;
146 int width, height, h_chroma_shift, v_chroma_shift;
148 char buf2[Y4M_LINE_MAX + 1];
150 uint8_t *ptr, *ptr1, *ptr2;
152 memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
153 picture = &picture_tmp;
155 /* for the first packet we have to output the header as well */
158 if (yuv4_generate_header(s, buf2) < 0) {
159 av_log(s, AV_LOG_ERROR,
160 "Error. YUV4MPEG stream header write failed.\n");
163 avio_write(pb, buf2, strlen(buf2));
167 /* construct frame header */
169 snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
170 avio_write(pb, buf1, strlen(buf1));
172 width = st->codec->width;
173 height = st->codec->height;
175 ptr = picture->data[0];
177 switch (st->codec->pix_fmt) {
178 case AV_PIX_FMT_GRAY8:
179 case AV_PIX_FMT_YUV411P:
180 case AV_PIX_FMT_YUV420P:
181 case AV_PIX_FMT_YUV422P:
182 case AV_PIX_FMT_YUV444P:
184 case AV_PIX_FMT_GRAY16:
185 case AV_PIX_FMT_YUV420P9:
186 case AV_PIX_FMT_YUV422P9:
187 case AV_PIX_FMT_YUV444P9:
188 case AV_PIX_FMT_YUV420P10:
189 case AV_PIX_FMT_YUV422P10:
190 case AV_PIX_FMT_YUV444P10:
191 case AV_PIX_FMT_YUV420P12:
192 case AV_PIX_FMT_YUV422P12:
193 case AV_PIX_FMT_YUV444P12:
194 case AV_PIX_FMT_YUV420P14:
195 case AV_PIX_FMT_YUV422P14:
196 case AV_PIX_FMT_YUV444P14:
197 case AV_PIX_FMT_YUV420P16:
198 case AV_PIX_FMT_YUV422P16:
199 case AV_PIX_FMT_YUV444P16:
203 av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
204 av_get_pix_fmt_name(st->codec->pix_fmt));
205 return AVERROR(EINVAL);
208 for (i = 0; i < height; i++) {
209 avio_write(pb, ptr, width);
210 ptr += picture->linesize[0];
213 if (st->codec->pix_fmt != AV_PIX_FMT_GRAY8 &&
214 st->codec->pix_fmt != AV_PIX_FMT_GRAY16) {
215 // Adjust for smaller Cb and Cr planes
216 av_pix_fmt_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
218 width >>= h_chroma_shift;
219 height >>= v_chroma_shift;
221 ptr1 = picture->data[1];
222 ptr2 = picture->data[2];
223 for (i = 0; i < height; i++) { /* Cb */
224 avio_write(pb, ptr1, width);
225 ptr1 += picture->linesize[1];
227 for (i = 0; i < height; i++) { /* Cr */
228 avio_write(pb, ptr2, width);
229 ptr2 += picture->linesize[2];
237 static int yuv4_write_header(AVFormatContext *s)
239 int *first_pkt = s->priv_data;
241 if (s->nb_streams != 1)
244 if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
245 av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
246 return AVERROR_INVALIDDATA;
249 switch (s->streams[0]->codec->pix_fmt) {
250 case AV_PIX_FMT_YUV411P:
251 av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
252 "stream, some mjpegtools might not work.\n");
254 case AV_PIX_FMT_GRAY8:
255 case AV_PIX_FMT_GRAY16:
256 case AV_PIX_FMT_YUV420P:
257 case AV_PIX_FMT_YUV422P:
258 case AV_PIX_FMT_YUV444P:
260 case AV_PIX_FMT_YUV420P9:
261 case AV_PIX_FMT_YUV422P9:
262 case AV_PIX_FMT_YUV444P9:
263 case AV_PIX_FMT_YUV420P10:
264 case AV_PIX_FMT_YUV422P10:
265 case AV_PIX_FMT_YUV444P10:
266 case AV_PIX_FMT_YUV420P12:
267 case AV_PIX_FMT_YUV422P12:
268 case AV_PIX_FMT_YUV444P12:
269 case AV_PIX_FMT_YUV420P14:
270 case AV_PIX_FMT_YUV422P14:
271 case AV_PIX_FMT_YUV444P14:
272 case AV_PIX_FMT_YUV420P16:
273 case AV_PIX_FMT_YUV422P16:
274 case AV_PIX_FMT_YUV444P16:
275 if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
276 av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
277 "Use '-strict -1' to encode to this pixel format.\n",
278 av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
279 return AVERROR(EINVAL);
281 av_log(s, AV_LOG_WARNING, "Warning: generating non standard YUV stream. "
282 "Mjpegtools will not work.\n");
285 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
286 "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
287 "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
288 "yuv444p10, yuv422p10, yuv420p10, "
289 "yuv444p12, yuv422p12, yuv420p12, "
290 "yuv444p14, yuv422p14, yuv420p14, "
291 "yuv444p16, yuv422p16, yuv420p16 "
292 "and gray16 pixel formats. "
293 "Use -pix_fmt to select one.\n");
301 AVOutputFormat ff_yuv4mpegpipe_muxer = {
302 .name = "yuv4mpegpipe",
303 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
305 .priv_data_size = sizeof(int),
306 .audio_codec = AV_CODEC_ID_NONE,
307 .video_codec = AV_CODEC_ID_RAWVIDEO,
308 .write_header = yuv4_write_header,
309 .write_packet = yuv4_write_packet,
310 .flags = AVFMT_RAWPICTURE,
314 /* Header size increased to allow room for optional flags */
315 #define MAX_YUV4_HEADER 80
316 #define MAX_FRAME_HEADER 80
318 static int yuv4_read_header(AVFormatContext *s)
320 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
321 // the longest option
322 char *tokstart, *tokend, *header_end;
324 AVIOContext *pb = s->pb;
325 int width = -1, height = -1, raten = 0,
326 rated = 0, aspectn = 0, aspectd = 0;
327 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
328 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
330 struct frame_attributes *s1 = s->priv_data;
332 for (i = 0; i < MAX_YUV4_HEADER; i++) {
333 header[i] = avio_r8(pb);
334 if (header[i] == '\n') {
335 header[i + 1] = 0x20; // Add a space after last option.
336 // Makes parsing "444" vs "444alpha" easier.
341 if (i == MAX_YUV4_HEADER)
343 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
346 s1->interlaced_frame = 0;
347 s1->top_field_first = 0;
348 header_end = &header[i + 1]; // Include space
349 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
350 tokstart < header_end; tokstart++) {
351 if (*tokstart == 0x20)
353 switch (*tokstart++) {
354 case 'W': // Width. Required.
355 width = strtol(tokstart, &tokend, 10);
358 case 'H': // Height. Required.
359 height = strtol(tokstart, &tokend, 10);
362 case 'C': // Color space
363 if (strncmp("420jpeg", tokstart, 7) == 0) {
364 pix_fmt = AV_PIX_FMT_YUV420P;
365 chroma_sample_location = AVCHROMA_LOC_CENTER;
366 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
367 pix_fmt = AV_PIX_FMT_YUV420P;
368 chroma_sample_location = AVCHROMA_LOC_LEFT;
369 } else if (strncmp("420paldv", tokstart, 8) == 0) {
370 pix_fmt = AV_PIX_FMT_YUV420P;
371 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
372 } else if (strncmp("420p16", tokstart, 6) == 0) {
373 pix_fmt = AV_PIX_FMT_YUV420P16;
374 } else if (strncmp("422p16", tokstart, 6) == 0) {
375 pix_fmt = AV_PIX_FMT_YUV422P16;
376 } else if (strncmp("444p16", tokstart, 6) == 0) {
377 pix_fmt = AV_PIX_FMT_YUV444P16;
378 } else if (strncmp("420p14", tokstart, 6) == 0) {
379 pix_fmt = AV_PIX_FMT_YUV420P14;
380 } else if (strncmp("422p14", tokstart, 6) == 0) {
381 pix_fmt = AV_PIX_FMT_YUV422P14;
382 } else if (strncmp("444p14", tokstart, 6) == 0) {
383 pix_fmt = AV_PIX_FMT_YUV444P14;
384 } else if (strncmp("420p12", tokstart, 6) == 0) {
385 pix_fmt = AV_PIX_FMT_YUV420P12;
386 } else if (strncmp("422p12", tokstart, 6) == 0) {
387 pix_fmt = AV_PIX_FMT_YUV422P12;
388 } else if (strncmp("444p12", tokstart, 6) == 0) {
389 pix_fmt = AV_PIX_FMT_YUV444P12;
390 } else if (strncmp("420p10", tokstart, 6) == 0) {
391 pix_fmt = AV_PIX_FMT_YUV420P10;
392 } else if (strncmp("422p10", tokstart, 6) == 0) {
393 pix_fmt = AV_PIX_FMT_YUV422P10;
394 } else if (strncmp("444p10", tokstart, 6) == 0) {
395 pix_fmt = AV_PIX_FMT_YUV444P10;
396 } else if (strncmp("420p9", tokstart, 5) == 0) {
397 pix_fmt = AV_PIX_FMT_YUV420P9;
398 } else if (strncmp("422p9", tokstart, 5) == 0) {
399 pix_fmt = AV_PIX_FMT_YUV422P9;
400 } else if (strncmp("444p9", tokstart, 5) == 0) {
401 pix_fmt = AV_PIX_FMT_YUV444P9;
402 } else if (strncmp("420", tokstart, 3) == 0) {
403 pix_fmt = AV_PIX_FMT_YUV420P;
404 chroma_sample_location = AVCHROMA_LOC_CENTER;
405 } else if (strncmp("411", tokstart, 3) == 0) {
406 pix_fmt = AV_PIX_FMT_YUV411P;
407 } else if (strncmp("422", tokstart, 3) == 0) {
408 pix_fmt = AV_PIX_FMT_YUV422P;
409 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
410 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
411 "YUV4MPEG stream.\n");
413 } else if (strncmp("444", tokstart, 3) == 0) {
414 pix_fmt = AV_PIX_FMT_YUV444P;
415 } else if (strncmp("mono16", tokstart, 6) == 0) {
416 pix_fmt = AV_PIX_FMT_GRAY16;
417 } else if (strncmp("mono", tokstart, 4) == 0) {
418 pix_fmt = AV_PIX_FMT_GRAY8;
420 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
424 while (tokstart < header_end && *tokstart != 0x20)
427 case 'I': // Interlace type
428 switch (*tokstart++){
432 s1->interlaced_frame = 0;
435 s1->interlaced_frame = 1;
436 s1->top_field_first = 1;
439 s1->interlaced_frame = 1;
440 s1->top_field_first = 0;
443 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
444 "interlaced and non-interlaced frames.\n");
447 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
451 case 'F': // Frame rate
452 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
453 while (tokstart < header_end && *tokstart != 0x20)
456 case 'A': // Pixel aspect
457 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
458 while (tokstart < header_end && *tokstart != 0x20)
461 case 'X': // Vendor extensions
462 if (strncmp("YSCSS=", tokstart, 6) == 0) {
463 // Older nonstandard pixel format representation
465 if (strncmp("420JPEG", tokstart, 7) == 0)
466 alt_pix_fmt = AV_PIX_FMT_YUV420P;
467 else if (strncmp("420MPEG2", tokstart, 8) == 0)
468 alt_pix_fmt = AV_PIX_FMT_YUV420P;
469 else if (strncmp("420PALDV", tokstart, 8) == 0)
470 alt_pix_fmt = AV_PIX_FMT_YUV420P;
471 else if (strncmp("420P9", tokstart, 5) == 0)
472 alt_pix_fmt = AV_PIX_FMT_YUV420P9;
473 else if (strncmp("422P9", tokstart, 5) == 0)
474 alt_pix_fmt = AV_PIX_FMT_YUV422P9;
475 else if (strncmp("444P9", tokstart, 5) == 0)
476 alt_pix_fmt = AV_PIX_FMT_YUV444P9;
477 else if (strncmp("420P10", tokstart, 6) == 0)
478 alt_pix_fmt = AV_PIX_FMT_YUV420P10;
479 else if (strncmp("422P10", tokstart, 6) == 0)
480 alt_pix_fmt = AV_PIX_FMT_YUV422P10;
481 else if (strncmp("444P10", tokstart, 6) == 0)
482 alt_pix_fmt = AV_PIX_FMT_YUV444P10;
483 else if (strncmp("420P12", tokstart, 6) == 0)
484 alt_pix_fmt = AV_PIX_FMT_YUV420P12;
485 else if (strncmp("422P12", tokstart, 6) == 0)
486 alt_pix_fmt = AV_PIX_FMT_YUV422P12;
487 else if (strncmp("444P12", tokstart, 6) == 0)
488 alt_pix_fmt = AV_PIX_FMT_YUV444P12;
489 else if (strncmp("420P14", tokstart, 6) == 0)
490 alt_pix_fmt = AV_PIX_FMT_YUV420P14;
491 else if (strncmp("422P14", tokstart, 6) == 0)
492 alt_pix_fmt = AV_PIX_FMT_YUV422P14;
493 else if (strncmp("444P14", tokstart, 6) == 0)
494 alt_pix_fmt = AV_PIX_FMT_YUV444P14;
495 else if (strncmp("420P16", tokstart, 6) == 0)
496 alt_pix_fmt = AV_PIX_FMT_YUV420P16;
497 else if (strncmp("422P16", tokstart, 6) == 0)
498 alt_pix_fmt = AV_PIX_FMT_YUV422P16;
499 else if (strncmp("444P16", tokstart, 6) == 0)
500 alt_pix_fmt = AV_PIX_FMT_YUV444P16;
501 else if (strncmp("411", tokstart, 3) == 0)
502 alt_pix_fmt = AV_PIX_FMT_YUV411P;
503 else if (strncmp("422", tokstart, 3) == 0)
504 alt_pix_fmt = AV_PIX_FMT_YUV422P;
505 else if (strncmp("444", tokstart, 3) == 0)
506 alt_pix_fmt = AV_PIX_FMT_YUV444P;
508 while (tokstart < header_end && *tokstart != 0x20)
514 if (width == -1 || height == -1) {
515 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
519 if (pix_fmt == AV_PIX_FMT_NONE) {
520 if (alt_pix_fmt == AV_PIX_FMT_NONE)
521 pix_fmt = AV_PIX_FMT_YUV420P;
523 pix_fmt = alt_pix_fmt;
526 if (raten <= 0 || rated <= 0) {
527 // Frame rate unknown
532 if (aspectn == 0 && aspectd == 0) {
533 // Pixel aspect unknown
537 st = avformat_new_stream(s, NULL);
539 return AVERROR(ENOMEM);
540 st->codec->width = width;
541 st->codec->height = height;
542 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
543 avpriv_set_pts_info(st, 64, rated, raten);
544 st->codec->pix_fmt = pix_fmt;
545 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
546 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
547 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
548 st->codec->chroma_sample_location = chroma_sample_location;
553 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
556 char header[MAX_FRAME_HEADER+1];
557 int packet_size, width, height, ret;
558 AVStream *st = s->streams[0];
559 struct frame_attributes *s1 = s->priv_data;
561 for (i = 0; i < MAX_FRAME_HEADER; i++) {
562 header[i] = avio_r8(s->pb);
563 if (header[i] == '\n') {
570 else if (s->pb->eof_reached)
572 else if (i == MAX_FRAME_HEADER)
573 return AVERROR_INVALIDDATA;
575 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
576 return AVERROR_INVALIDDATA;
578 width = st->codec->width;
579 height = st->codec->height;
581 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
585 ret = av_get_packet(s->pb, pkt, packet_size);
588 else if (ret != packet_size)
589 return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
591 if (st->codec->coded_frame) {
592 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
593 st->codec->coded_frame->top_field_first = s1->top_field_first;
596 pkt->stream_index = 0;
600 static int yuv4_probe(AVProbeData *pd)
602 /* check file header */
603 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
604 return AVPROBE_SCORE_MAX;
609 #if CONFIG_YUV4MPEGPIPE_DEMUXER
610 AVInputFormat ff_yuv4mpegpipe_demuxer = {
611 .name = "yuv4mpegpipe",
612 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
613 .priv_data_size = sizeof(struct frame_attributes),
614 .read_probe = yuv4_probe,
615 .read_header = yuv4_read_header,
616 .read_packet = yuv4_read_packet,