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) {
62 colorspace = " Cmono";
65 colorspace = " Cmono16";
68 colorspace = " C411 XYSCSS=411";
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;
78 colorspace = " C422 XYSCSS=422";
81 colorspace = " C444 XYSCSS=444";
83 case PIX_FMT_YUV420P9:
84 colorspace = " C420p9 XYSCSS=420P9";
86 case PIX_FMT_YUV422P9:
87 colorspace = " C422p9 XYSCSS=422P9";
89 case PIX_FMT_YUV444P9:
90 colorspace = " C444p9 XYSCSS=444P9";
92 case PIX_FMT_YUV420P10:
93 colorspace = " C420p10 XYSCSS=420P10";
95 case PIX_FMT_YUV422P10:
96 colorspace = " C422p10 XYSCSS=422P10";
98 case PIX_FMT_YUV444P10:
99 colorspace = " C444p10 XYSCSS=444P10";
101 case PIX_FMT_YUV420P16:
102 colorspace = " C420p16 XYSCSS=420P16";
104 case PIX_FMT_YUV422P16:
105 colorspace = " C422p16 XYSCSS=422P16";
107 case 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) {
159 case PIX_FMT_YUV411P:
160 case PIX_FMT_YUV420P:
161 case PIX_FMT_YUV422P:
162 case PIX_FMT_YUV444P:
165 case PIX_FMT_YUV420P9:
166 case PIX_FMT_YUV422P9:
167 case PIX_FMT_YUV444P9:
168 case PIX_FMT_YUV420P10:
169 case PIX_FMT_YUV422P10:
170 case PIX_FMT_YUV444P10:
171 case PIX_FMT_YUV420P16:
172 case PIX_FMT_YUV422P16:
173 case 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 != PIX_FMT_GRAY8 &&
188 st->codec->pix_fmt != 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,
220 "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
221 return AVERROR(EINVAL);
224 switch (s->streams[0]->codec->pix_fmt) {
225 case PIX_FMT_YUV411P:
226 av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
227 "stream, some mjpegtools might not work.\n");
231 case PIX_FMT_YUV420P:
232 case PIX_FMT_YUV422P:
233 case PIX_FMT_YUV444P:
235 case PIX_FMT_YUV420P9:
236 case PIX_FMT_YUV422P9:
237 case PIX_FMT_YUV444P9:
238 case PIX_FMT_YUV420P10:
239 case PIX_FMT_YUV422P10:
240 case PIX_FMT_YUV444P10:
241 case PIX_FMT_YUV420P16:
242 case PIX_FMT_YUV422P16:
243 case PIX_FMT_YUV444P16:
244 if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
245 av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
246 "Use '-strict -1' to encode to this pixel format.\n",
247 av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
248 return AVERROR(EINVAL);
250 av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
251 "Mjpegtools will not work.\n");
254 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
255 "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
256 "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
257 "yuv444p10, yuv422p10, yuv420p10, "
258 "yuv444p16, yuv422p16, yuv420p16 "
259 "and gray16 pixel formats. "
260 "Use -pix_fmt to select one.\n");
268 AVOutputFormat ff_yuv4mpegpipe_muxer = {
269 .name = "yuv4mpegpipe",
270 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
272 .priv_data_size = sizeof(int),
273 .audio_codec = AV_CODEC_ID_NONE,
274 .video_codec = AV_CODEC_ID_RAWVIDEO,
275 .write_header = yuv4_write_header,
276 .write_packet = yuv4_write_packet,
277 .flags = AVFMT_RAWPICTURE,
281 /* Header size increased to allow room for optional flags */
282 #define MAX_YUV4_HEADER 80
283 #define MAX_FRAME_HEADER 80
285 static int yuv4_read_header(AVFormatContext *s)
287 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
288 // the longest option
289 char *tokstart, *tokend, *header_end;
291 AVIOContext *pb = s->pb;
292 int width = -1, height = -1, raten = 0,
293 rated = 0, aspectn = 0, aspectd = 0;
294 enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
295 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
297 struct frame_attributes *s1 = s->priv_data;
299 for (i = 0; i < MAX_YUV4_HEADER; i++) {
300 header[i] = avio_r8(pb);
301 if (header[i] == '\n') {
302 header[i + 1] = 0x20; // Add a space after last option.
303 // Makes parsing "444" vs "444alpha" easier.
308 if (i == MAX_YUV4_HEADER)
310 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
313 s1->interlaced_frame = 0;
314 s1->top_field_first = 0;
315 header_end = &header[i + 1]; // Include space
316 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
317 tokstart < header_end; tokstart++) {
318 if (*tokstart == 0x20)
320 switch (*tokstart++) {
321 case 'W': // Width. Required.
322 width = strtol(tokstart, &tokend, 10);
325 case 'H': // Height. Required.
326 height = strtol(tokstart, &tokend, 10);
329 case 'C': // Color space
330 if (strncmp("420jpeg", tokstart, 7) == 0) {
331 pix_fmt = PIX_FMT_YUV420P;
332 chroma_sample_location = AVCHROMA_LOC_CENTER;
333 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
334 pix_fmt = PIX_FMT_YUV420P;
335 chroma_sample_location = AVCHROMA_LOC_LEFT;
336 } else if (strncmp("420paldv", tokstart, 8) == 0) {
337 pix_fmt = PIX_FMT_YUV420P;
338 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
339 } else if (strncmp("420p16", tokstart, 6) == 0) {
340 pix_fmt = PIX_FMT_YUV420P16;
341 } else if (strncmp("422p16", tokstart, 6) == 0) {
342 pix_fmt = PIX_FMT_YUV422P16;
343 } else if (strncmp("444p16", tokstart, 6) == 0) {
344 pix_fmt = PIX_FMT_YUV444P16;
345 } else if (strncmp("420p10", tokstart, 6) == 0) {
346 pix_fmt = PIX_FMT_YUV420P10;
347 } else if (strncmp("422p10", tokstart, 6) == 0) {
348 pix_fmt = PIX_FMT_YUV422P10;
349 } else if (strncmp("444p10", tokstart, 6) == 0) {
350 pix_fmt = PIX_FMT_YUV444P10;
351 } else if (strncmp("420p9", tokstart, 5) == 0) {
352 pix_fmt = PIX_FMT_YUV420P9;
353 } else if (strncmp("422p9", tokstart, 5) == 0) {
354 pix_fmt = PIX_FMT_YUV422P9;
355 } else if (strncmp("444p9", tokstart, 5) == 0) {
356 pix_fmt = PIX_FMT_YUV444P9;
357 } else if (strncmp("420", tokstart, 3) == 0) {
358 pix_fmt = PIX_FMT_YUV420P;
359 chroma_sample_location = AVCHROMA_LOC_CENTER;
360 } else if (strncmp("411", tokstart, 3) == 0) {
361 pix_fmt = PIX_FMT_YUV411P;
362 } else if (strncmp("422", tokstart, 3) == 0) {
363 pix_fmt = PIX_FMT_YUV422P;
364 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
365 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
366 "YUV4MPEG stream.\n");
368 } else if (strncmp("444", tokstart, 3) == 0) {
369 pix_fmt = PIX_FMT_YUV444P;
370 } else if (strncmp("mono16", tokstart, 6) == 0) {
371 pix_fmt = PIX_FMT_GRAY16;
372 } else if (strncmp("mono", tokstart, 4) == 0) {
373 pix_fmt = PIX_FMT_GRAY8;
375 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
379 while (tokstart < header_end && *tokstart != 0x20)
382 case 'I': // Interlace type
383 switch (*tokstart++){
387 s1->interlaced_frame = 0;
390 s1->interlaced_frame = 1;
391 s1->top_field_first = 1;
394 s1->interlaced_frame = 1;
395 s1->top_field_first = 0;
398 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
399 "interlaced and non-interlaced frames.\n");
402 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
406 case 'F': // Frame rate
407 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
408 while (tokstart < header_end && *tokstart != 0x20)
411 case 'A': // Pixel aspect
412 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
413 while (tokstart < header_end && *tokstart != 0x20)
416 case 'X': // Vendor extensions
417 if (strncmp("YSCSS=", tokstart, 6) == 0) {
418 // Older nonstandard pixel format representation
420 if (strncmp("420JPEG", tokstart, 7) == 0)
421 alt_pix_fmt = PIX_FMT_YUV420P;
422 else if (strncmp("420MPEG2", tokstart, 8) == 0)
423 alt_pix_fmt = PIX_FMT_YUV420P;
424 else if (strncmp("420PALDV", tokstart, 8) == 0)
425 alt_pix_fmt = PIX_FMT_YUV420P;
426 else if (strncmp("420P9", tokstart, 5) == 0)
427 alt_pix_fmt = PIX_FMT_YUV420P9;
428 else if (strncmp("422P9", tokstart, 5) == 0)
429 alt_pix_fmt = PIX_FMT_YUV422P9;
430 else if (strncmp("444P9", tokstart, 5) == 0)
431 alt_pix_fmt = PIX_FMT_YUV444P9;
432 else if (strncmp("420P10", tokstart, 6) == 0)
433 alt_pix_fmt = PIX_FMT_YUV420P10;
434 else if (strncmp("422P10", tokstart, 6) == 0)
435 alt_pix_fmt = PIX_FMT_YUV422P10;
436 else if (strncmp("444P10", tokstart, 6) == 0)
437 alt_pix_fmt = PIX_FMT_YUV444P10;
438 else if (strncmp("420P16", tokstart, 6) == 0)
439 alt_pix_fmt = PIX_FMT_YUV420P16;
440 else if (strncmp("422P16", tokstart, 6) == 0)
441 alt_pix_fmt = PIX_FMT_YUV422P16;
442 else if (strncmp("444P16", tokstart, 6) == 0)
443 alt_pix_fmt = PIX_FMT_YUV444P16;
444 else if (strncmp("411", tokstart, 3) == 0)
445 alt_pix_fmt = PIX_FMT_YUV411P;
446 else if (strncmp("422", tokstart, 3) == 0)
447 alt_pix_fmt = PIX_FMT_YUV422P;
448 else if (strncmp("444", tokstart, 3) == 0)
449 alt_pix_fmt = PIX_FMT_YUV444P;
451 while (tokstart < header_end && *tokstart != 0x20)
457 if (width == -1 || height == -1) {
458 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
462 if (pix_fmt == PIX_FMT_NONE) {
463 if (alt_pix_fmt == PIX_FMT_NONE)
464 pix_fmt = PIX_FMT_YUV420P;
466 pix_fmt = alt_pix_fmt;
469 if (raten <= 0 || rated <= 0) {
470 // Frame rate unknown
475 if (aspectn == 0 && aspectd == 0) {
476 // Pixel aspect unknown
480 st = avformat_new_stream(s, NULL);
482 return AVERROR(ENOMEM);
483 st->codec->width = width;
484 st->codec->height = height;
485 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
486 avpriv_set_pts_info(st, 64, rated, raten);
487 st->codec->pix_fmt = pix_fmt;
488 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
489 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
490 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
491 st->codec->chroma_sample_location = chroma_sample_location;
496 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
499 char header[MAX_FRAME_HEADER+1];
500 int packet_size, width, height;
501 AVStream *st = s->streams[0];
502 struct frame_attributes *s1 = s->priv_data;
504 for (i = 0; i < MAX_FRAME_HEADER; i++) {
505 header[i] = avio_r8(s->pb);
506 if (header[i] == '\n') {
511 if (i == MAX_FRAME_HEADER)
513 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
516 width = st->codec->width;
517 height = st->codec->height;
519 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
523 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
526 if (st->codec->coded_frame) {
527 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
528 st->codec->coded_frame->top_field_first = s1->top_field_first;
531 pkt->stream_index = 0;
535 static int yuv4_probe(AVProbeData *pd)
537 /* check file header */
538 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
539 return AVPROBE_SCORE_MAX;
544 #if CONFIG_YUV4MPEGPIPE_DEMUXER
545 AVInputFormat ff_yuv4mpegpipe_demuxer = {
546 .name = "yuv4mpegpipe",
547 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
548 .priv_data_size = sizeof(struct frame_attributes),
549 .read_probe = yuv4_probe,
550 .read_header = yuv4_read_header,
551 .read_packet = yuv4_read_packet,