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
24 #define Y4M_MAGIC "YUV4MPEG2"
25 #define Y4M_FRAME_MAGIC "FRAME"
26 #define Y4M_LINE_MAX 256
28 struct frame_attributes {
33 #if CONFIG_YUV4MPEGPIPE_MUXER
34 static int yuv4_generate_header(AVFormatContext *s, char* buf)
38 int raten, rated, aspectn, aspectd, n;
40 const char *colorspace = "";
43 width = st->codec->width;
44 height = st->codec->height;
46 av_reduce(&raten, &rated, st->codec->time_base.den,
47 st->codec->time_base.num, (1UL << 31) - 1);
49 aspectn = st->sample_aspect_ratio.num;
50 aspectd = st->sample_aspect_ratio.den;
52 if (aspectn == 0 && aspectd == 1)
53 aspectd = 0; // 0:0 means unknown
55 inter = 'p'; /* progressive is the default */
56 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
57 inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
59 switch (st->codec->pix_fmt) {
61 colorspace = " Cmono";
64 colorspace = " C411 XYSCSS=411";
67 switch (st->codec->chroma_sample_location) {
68 case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
69 case AVCHROMA_LOC_LEFT: colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
70 default: colorspace = " C420jpeg XYSCSS=420JPEG"; break;
74 colorspace = " C422 XYSCSS=422";
77 colorspace = " C444 XYSCSS=444";
81 /* construct stream header, if this is the first frame */
82 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
83 Y4M_MAGIC, width, height, raten, rated, inter,
84 aspectn, aspectd, colorspace);
89 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
91 AVStream *st = s->streams[pkt->stream_index];
92 AVIOContext *pb = s->pb;
94 int* first_pkt = s->priv_data;
95 int width, height, h_chroma_shift, v_chroma_shift;
97 char buf2[Y4M_LINE_MAX + 1];
99 uint8_t *ptr, *ptr1, *ptr2;
101 picture = (AVPicture *)pkt->data;
103 /* for the first packet we have to output the header as well */
106 if (yuv4_generate_header(s, buf2) < 0) {
107 av_log(s, AV_LOG_ERROR,
108 "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,
133 width >>= h_chroma_shift;
134 height >>= v_chroma_shift;
136 ptr1 = picture->data[1];
137 ptr2 = picture->data[2];
138 for (i = 0; i < height; i++) { /* Cb */
139 avio_write(pb, ptr1, width);
140 ptr1 += picture->linesize[1];
142 for (i = 0; i < height; i++) { /* Cr */
143 avio_write(pb, ptr2, width);
144 ptr2 += picture->linesize[2];
151 static int yuv4_write_header(AVFormatContext *s)
153 int *first_pkt = s->priv_data;
155 if (s->nb_streams != 1)
158 if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
159 av_log(s, AV_LOG_ERROR,
160 "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
161 return AVERROR(EINVAL);
164 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
165 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
166 "stream, some mjpegtools might not work.\n");
167 } else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
168 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
169 (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
170 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
171 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
172 "yuv422p, yuv420p, yuv411p and gray pixel formats. "
173 "Use -pix_fmt to select one.\n");
181 AVOutputFormat ff_yuv4mpegpipe_muxer = {
182 .name = "yuv4mpegpipe",
183 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
186 .priv_data_size = sizeof(int),
187 .audio_codec = CODEC_ID_NONE,
188 .video_codec = CODEC_ID_RAWVIDEO,
189 .write_header = yuv4_write_header,
190 .write_packet = yuv4_write_packet,
191 .flags = AVFMT_RAWPICTURE,
195 /* Header size increased to allow room for optional flags */
196 #define MAX_YUV4_HEADER 80
197 #define MAX_FRAME_HEADER 80
199 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
201 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
202 // the longest option
203 char *tokstart, *tokend, *header_end;
205 AVIOContext *pb = s->pb;
206 int width = -1, height = -1, raten = 0,
207 rated = 0, aspectn = 0, aspectd = 0;
208 enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
209 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
211 struct frame_attributes *s1 = s->priv_data;
213 for (i = 0; i < MAX_YUV4_HEADER; i++) {
214 header[i] = avio_r8(pb);
215 if (header[i] == '\n') {
216 header[i + 1] = 0x20; // Add a space after last option.
217 // Makes parsing "444" vs "444alpha" easier.
222 if (i == MAX_YUV4_HEADER)
224 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
227 s1->interlaced_frame = 0;
228 s1->top_field_first = 0;
229 header_end = &header[i + 1]; // Include space
230 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
231 tokstart < header_end; tokstart++) {
232 if (*tokstart == 0x20)
234 switch (*tokstart++) {
235 case 'W': // Width. Required.
236 width = strtol(tokstart, &tokend, 10);
239 case 'H': // Height. Required.
240 height = strtol(tokstart, &tokend, 10);
243 case 'C': // Color space
244 if (strncmp("420jpeg", tokstart, 7) == 0) {
245 pix_fmt = PIX_FMT_YUV420P;
246 chroma_sample_location = AVCHROMA_LOC_CENTER;
247 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
248 pix_fmt = PIX_FMT_YUV420P;
249 chroma_sample_location = AVCHROMA_LOC_LEFT;
250 } else if (strncmp("420paldv", tokstart, 8) == 0) {
251 pix_fmt = PIX_FMT_YUV420P;
252 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
253 } else if (strncmp("411", tokstart, 3) == 0)
254 pix_fmt = PIX_FMT_YUV411P;
255 else if (strncmp("422", tokstart, 3) == 0)
256 pix_fmt = PIX_FMT_YUV422P;
257 else if (strncmp("444alpha", tokstart, 8) == 0 ) {
258 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
259 "YUV4MPEG stream.\n");
261 } else if (strncmp("444", tokstart, 3) == 0)
262 pix_fmt = PIX_FMT_YUV444P;
263 else if (strncmp("mono", tokstart, 4) == 0) {
264 pix_fmt = PIX_FMT_GRAY8;
266 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
270 while (tokstart < header_end && *tokstart != 0x20)
273 case 'I': // Interlace type
274 switch (*tokstart++){
278 s1->interlaced_frame = 0;
281 s1->interlaced_frame = 1;
282 s1->top_field_first = 1;
285 s1->interlaced_frame = 1;
286 s1->top_field_first = 0;
289 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
290 "interlaced and non-interlaced frames.\n");
293 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
297 case 'F': // Frame rate
298 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
299 while (tokstart < header_end && *tokstart != 0x20)
302 case 'A': // Pixel aspect
303 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
304 while (tokstart < header_end && *tokstart != 0x20)
307 case 'X': // Vendor extensions
308 if (strncmp("YSCSS=", tokstart, 6) == 0) {
309 // Older nonstandard pixel format representation
311 if (strncmp("420JPEG", tokstart, 7) == 0)
312 alt_pix_fmt = PIX_FMT_YUV420P;
313 else if (strncmp("420MPEG2", tokstart, 8) == 0)
314 alt_pix_fmt = PIX_FMT_YUV420P;
315 else if (strncmp("420PALDV", tokstart, 8) == 0)
316 alt_pix_fmt = PIX_FMT_YUV420P;
317 else if (strncmp("411", tokstart, 3) == 0)
318 alt_pix_fmt = PIX_FMT_YUV411P;
319 else if (strncmp("422", tokstart, 3) == 0)
320 alt_pix_fmt = PIX_FMT_YUV422P;
321 else if (strncmp("444", tokstart, 3) == 0)
322 alt_pix_fmt = PIX_FMT_YUV444P;
324 while (tokstart < header_end && *tokstart != 0x20)
330 if (width == -1 || height == -1) {
331 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
335 if (pix_fmt == PIX_FMT_NONE) {
336 if (alt_pix_fmt == PIX_FMT_NONE)
337 pix_fmt = PIX_FMT_YUV420P;
339 pix_fmt = alt_pix_fmt;
342 if (raten <= 0 || rated <= 0) {
343 // Frame rate unknown
348 if (aspectn == 0 && aspectd == 0) {
349 // Pixel aspect unknown
353 st = avformat_new_stream(s, NULL);
355 return AVERROR(ENOMEM);
356 st->codec->width = width;
357 st->codec->height = height;
358 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
359 avpriv_set_pts_info(st, 64, rated, raten);
360 st->codec->pix_fmt = pix_fmt;
361 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
362 st->codec->codec_id = CODEC_ID_RAWVIDEO;
363 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
364 st->codec->chroma_sample_location = chroma_sample_location;
369 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
372 char header[MAX_FRAME_HEADER+1];
373 int packet_size, width, height;
374 AVStream *st = s->streams[0];
375 struct frame_attributes *s1 = s->priv_data;
377 for (i = 0; i < MAX_FRAME_HEADER; i++) {
378 header[i] = avio_r8(s->pb);
379 if (header[i] == '\n') {
384 if (i == MAX_FRAME_HEADER)
386 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
389 width = st->codec->width;
390 height = st->codec->height;
392 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
396 if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
399 if (st->codec->coded_frame) {
400 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
401 st->codec->coded_frame->top_field_first = s1->top_field_first;
404 pkt->stream_index = 0;
408 static int yuv4_probe(AVProbeData *pd)
410 /* check file header */
411 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
412 return AVPROBE_SCORE_MAX;
417 #if CONFIG_YUV4MPEGPIPE_DEMUXER
418 AVInputFormat ff_yuv4mpegpipe_demuxer = {
419 .name = "yuv4mpegpipe",
420 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
421 .priv_data_size = sizeof(struct frame_attributes),
422 .read_probe = yuv4_probe,
423 .read_header = yuv4_read_header,
424 .read_packet = yuv4_read_packet,