3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 /* Header size increased to allow room for optional flags */
27 #define MAX_YUV4_HEADER 80
28 #define MAX_FRAME_HEADER 80
30 struct frame_attributes {
35 static int yuv4_read_header(AVFormatContext *s)
37 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
39 char *tokstart, *tokend, *header_end;
41 AVIOContext *pb = s->pb;
42 int width = -1, height = -1, raten = 0,
43 rated = 0, aspectn = 0, aspectd = 0;
44 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
45 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
47 struct frame_attributes *s1 = s->priv_data;
49 for (i = 0; i < MAX_YUV4_HEADER; i++) {
50 header[i] = avio_r8(pb);
51 if (header[i] == '\n') {
52 header[i + 1] = 0x20; // Add a space after last option.
53 // Makes parsing "444" vs "444alpha" easier.
58 if (i == MAX_YUV4_HEADER)
60 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
63 s1->interlaced_frame = 0;
64 s1->top_field_first = 0;
65 header_end = &header[i + 1]; // Include space
66 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
67 tokstart < header_end; tokstart++) {
68 if (*tokstart == 0x20)
70 switch (*tokstart++) {
71 case 'W': // Width. Required.
72 width = strtol(tokstart, &tokend, 10);
75 case 'H': // Height. Required.
76 height = strtol(tokstart, &tokend, 10);
79 case 'C': // Color space
80 if (strncmp("420jpeg", tokstart, 7) == 0) {
81 pix_fmt = AV_PIX_FMT_YUV420P;
82 chroma_sample_location = AVCHROMA_LOC_CENTER;
83 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
84 pix_fmt = AV_PIX_FMT_YUV420P;
85 chroma_sample_location = AVCHROMA_LOC_LEFT;
86 } else if (strncmp("420paldv", tokstart, 8) == 0) {
87 pix_fmt = AV_PIX_FMT_YUV420P;
88 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
89 } else if (strncmp("420", tokstart, 3) == 0) {
90 pix_fmt = AV_PIX_FMT_YUV420P;
91 chroma_sample_location = AVCHROMA_LOC_CENTER;
92 } else if (strncmp("411", tokstart, 3) == 0)
93 pix_fmt = AV_PIX_FMT_YUV411P;
94 else if (strncmp("422", tokstart, 3) == 0)
95 pix_fmt = AV_PIX_FMT_YUV422P;
96 else if (strncmp("444alpha", tokstart, 8) == 0 ) {
97 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
98 "YUV4MPEG stream.\n");
100 } else if (strncmp("444", tokstart, 3) == 0)
101 pix_fmt = AV_PIX_FMT_YUV444P;
102 else if (strncmp("mono", tokstart, 4) == 0) {
103 pix_fmt = AV_PIX_FMT_GRAY8;
105 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
109 while (tokstart < header_end && *tokstart != 0x20)
112 case 'I': // Interlace type
113 switch (*tokstart++){
117 s1->interlaced_frame = 0;
120 s1->interlaced_frame = 1;
121 s1->top_field_first = 1;
124 s1->interlaced_frame = 1;
125 s1->top_field_first = 0;
128 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
129 "interlaced and non-interlaced frames.\n");
132 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
136 case 'F': // Frame rate
137 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
138 while (tokstart < header_end && *tokstart != 0x20)
141 case 'A': // Pixel aspect
142 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
143 while (tokstart < header_end && *tokstart != 0x20)
146 case 'X': // Vendor extensions
147 if (strncmp("YSCSS=", tokstart, 6) == 0) {
148 // Older nonstandard pixel format representation
150 if (strncmp("420JPEG", tokstart, 7) == 0)
151 alt_pix_fmt = AV_PIX_FMT_YUV420P;
152 else if (strncmp("420MPEG2", tokstart, 8) == 0)
153 alt_pix_fmt = AV_PIX_FMT_YUV420P;
154 else if (strncmp("420PALDV", tokstart, 8) == 0)
155 alt_pix_fmt = AV_PIX_FMT_YUV420P;
156 else if (strncmp("411", tokstart, 3) == 0)
157 alt_pix_fmt = AV_PIX_FMT_YUV411P;
158 else if (strncmp("422", tokstart, 3) == 0)
159 alt_pix_fmt = AV_PIX_FMT_YUV422P;
160 else if (strncmp("444", tokstart, 3) == 0)
161 alt_pix_fmt = AV_PIX_FMT_YUV444P;
163 while (tokstart < header_end && *tokstart != 0x20)
169 if (width == -1 || height == -1) {
170 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
174 if (pix_fmt == AV_PIX_FMT_NONE) {
175 if (alt_pix_fmt == AV_PIX_FMT_NONE)
176 pix_fmt = AV_PIX_FMT_YUV420P;
178 pix_fmt = alt_pix_fmt;
181 if (raten <= 0 || rated <= 0) {
182 // Frame rate unknown
187 if (aspectn == 0 && aspectd == 0) {
188 // Pixel aspect unknown
192 st = avformat_new_stream(s, NULL);
194 return AVERROR(ENOMEM);
195 st->codec->width = width;
196 st->codec->height = height;
197 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
198 avpriv_set_pts_info(st, 64, rated, raten);
199 st->avg_frame_rate = av_inv_q(st->time_base);
200 st->codec->pix_fmt = pix_fmt;
201 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
202 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
203 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
204 st->codec->chroma_sample_location = chroma_sample_location;
209 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
212 char header[MAX_FRAME_HEADER+1];
213 int packet_size, width, height, ret;
214 AVStream *st = s->streams[0];
215 struct frame_attributes *s1 = s->priv_data;
217 for (i = 0; i < MAX_FRAME_HEADER; i++) {
218 header[i] = avio_r8(s->pb);
219 if (header[i] == '\n') {
226 else if (s->pb->eof_reached)
228 else if (i == MAX_FRAME_HEADER)
229 return AVERROR_INVALIDDATA;
231 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
232 return AVERROR_INVALIDDATA;
234 width = st->codec->width;
235 height = st->codec->height;
237 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
241 ret = av_get_packet(s->pb, pkt, packet_size);
244 else if (ret != packet_size)
245 return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
247 if (st->codec->coded_frame) {
248 st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
249 st->codec->coded_frame->top_field_first = s1->top_field_first;
252 pkt->stream_index = 0;
256 static int yuv4_probe(AVProbeData *pd)
258 /* check file header */
259 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
260 return AVPROBE_SCORE_MAX;
265 AVInputFormat ff_yuv4mpegpipe_demuxer = {
266 .name = "yuv4mpegpipe",
267 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
268 .priv_data_size = sizeof(struct frame_attributes),
269 .read_probe = yuv4_probe,
270 .read_header = yuv4_read_header,
271 .read_packet = yuv4_read_packet,