1 /*
2 * YUV4MPEG demuxer
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/avstring.h"
23 #include "libavutil/imgutils.h"
24
25 #include "avformat.h"
26 #include "internal.h"
27 #include "yuv4mpeg.h"
28
29 /* Header size increased to allow room for optional flags */
30 #define MAX_YUV4_HEADER 96
31 #define MAX_FRAME_HEADER 80
32
yuv4_read_header(AVFormatContext * s)33 static int yuv4_read_header(AVFormatContext *s)
34 {
35 char header[MAX_YUV4_HEADER + 10]; // Include headroom for
36 // the longest option
37 char *tokstart, *tokend, *header_end;
38 int i;
39 AVIOContext *pb = s->pb;
40 int width = -1, height = -1, raten = 0,
41 rated = 0, aspectn = 0, aspectd = 0;
42 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
43 enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
44 enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
45 enum AVColorRange color_range = AVCOL_RANGE_UNSPECIFIED;
46 AVStream *st;
47 int64_t data_offset;
48
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.
54 header[i + 2] = 0;
55 break;
56 }
57 }
58 if (i == MAX_YUV4_HEADER) {
59 av_log(s, AV_LOG_ERROR, "Header too large.\n");
60 return AVERROR(EINVAL);
61 }
62 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) {
63 av_log(s, AV_LOG_ERROR, "Invalid magic number for yuv4mpeg.\n");
64 return AVERROR(EINVAL);
65 }
66
67 header_end = &header[i + 1]; // Include space
68 for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
69 tokstart < header_end; tokstart++) {
70 if (*tokstart == 0x20)
71 continue;
72 switch (*tokstart++) {
73 case 'W': // Width. Required.
74 width = strtol(tokstart, &tokend, 10);
75 tokstart = tokend;
76 break;
77 case 'H': // Height. Required.
78 height = strtol(tokstart, &tokend, 10);
79 tokstart = tokend;
80 break;
81 case 'C': // Color space
82 {
83 static const struct {
84 #define MAX_PIX_FMT_LENGTH 8
85 char name[MAX_PIX_FMT_LENGTH + 1];
86 #undef MAX_PIX_FMT_LENGTH
87 enum AVPixelFormat pix_fmt;
88 enum AVChromaLocation chroma_loc;
89 } pix_fmt_array[] = {
90 { "420jpeg", AV_PIX_FMT_YUV420P, AVCHROMA_LOC_CENTER },
91 { "420mpeg2", AV_PIX_FMT_YUV420P, AVCHROMA_LOC_LEFT },
92 { "420paldv", AV_PIX_FMT_YUV420P, AVCHROMA_LOC_TOPLEFT },
93 { "420p16", AV_PIX_FMT_YUV420P16, AVCHROMA_LOC_UNSPECIFIED },
94 { "422p16", AV_PIX_FMT_YUV422P16, AVCHROMA_LOC_UNSPECIFIED },
95 { "444p16", AV_PIX_FMT_YUV444P16, AVCHROMA_LOC_UNSPECIFIED },
96 { "420p14", AV_PIX_FMT_YUV420P14, AVCHROMA_LOC_UNSPECIFIED },
97 { "422p14", AV_PIX_FMT_YUV422P14, AVCHROMA_LOC_UNSPECIFIED },
98 { "444p14", AV_PIX_FMT_YUV444P14, AVCHROMA_LOC_UNSPECIFIED },
99 { "420p12", AV_PIX_FMT_YUV420P12, AVCHROMA_LOC_UNSPECIFIED },
100 { "422p12", AV_PIX_FMT_YUV422P12, AVCHROMA_LOC_UNSPECIFIED },
101 { "444p12", AV_PIX_FMT_YUV444P12, AVCHROMA_LOC_UNSPECIFIED },
102 { "420p10", AV_PIX_FMT_YUV420P10, AVCHROMA_LOC_UNSPECIFIED },
103 { "422p10", AV_PIX_FMT_YUV422P10, AVCHROMA_LOC_UNSPECIFIED },
104 { "444p10", AV_PIX_FMT_YUV444P10, AVCHROMA_LOC_UNSPECIFIED },
105 { "420p9", AV_PIX_FMT_YUV420P9, AVCHROMA_LOC_UNSPECIFIED },
106 { "422p9", AV_PIX_FMT_YUV422P9, AVCHROMA_LOC_UNSPECIFIED },
107 { "444p9", AV_PIX_FMT_YUV444P9, AVCHROMA_LOC_UNSPECIFIED },
108 { "420", AV_PIX_FMT_YUV420P, AVCHROMA_LOC_CENTER },
109 { "411", AV_PIX_FMT_YUV411P, AVCHROMA_LOC_UNSPECIFIED },
110 { "422", AV_PIX_FMT_YUV422P, AVCHROMA_LOC_UNSPECIFIED },
111 { "444alpha", AV_PIX_FMT_YUVA444P, AVCHROMA_LOC_UNSPECIFIED },
112 { "444", AV_PIX_FMT_YUV444P, AVCHROMA_LOC_UNSPECIFIED },
113 { "mono16", AV_PIX_FMT_GRAY16, AVCHROMA_LOC_UNSPECIFIED },
114 { "mono12", AV_PIX_FMT_GRAY12, AVCHROMA_LOC_UNSPECIFIED },
115 { "mono10", AV_PIX_FMT_GRAY10, AVCHROMA_LOC_UNSPECIFIED },
116 { "mono9", AV_PIX_FMT_GRAY9, AVCHROMA_LOC_UNSPECIFIED },
117 { "mono", AV_PIX_FMT_GRAY8, AVCHROMA_LOC_UNSPECIFIED },
118 };
119 for (i = 0; i < FF_ARRAY_ELEMS(pix_fmt_array); i++) {
120 if (av_strstart(tokstart, pix_fmt_array[i].name, NULL)) {
121 pix_fmt = pix_fmt_array[i].pix_fmt;
122 if (pix_fmt_array[i].chroma_loc != AVCHROMA_LOC_UNSPECIFIED)
123 chroma_sample_location = pix_fmt_array[i].chroma_loc;
124 break;
125 }
126 }
127 if (i == FF_ARRAY_ELEMS(pix_fmt_array)) {
128 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
129 "pixel format.\n");
130 return AVERROR_INVALIDDATA;
131 }
132 while (tokstart < header_end && *tokstart != 0x20)
133 tokstart++;
134 break;
135 }
136 case 'I': // Interlace type
137 switch (*tokstart++){
138 case '?':
139 field_order = AV_FIELD_UNKNOWN;
140 break;
141 case 'p':
142 field_order = AV_FIELD_PROGRESSIVE;
143 break;
144 case 't':
145 field_order = AV_FIELD_TT;
146 break;
147 case 'b':
148 field_order = AV_FIELD_BB;
149 break;
150 case 'm':
151 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
152 "interlaced and non-interlaced frames.\n");
153 default:
154 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
155 return AVERROR(EINVAL);
156 }
157 break;
158 case 'F': // Frame rate
159 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
160 while (tokstart < header_end && *tokstart != 0x20)
161 tokstart++;
162 break;
163 case 'A': // Pixel aspect
164 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
165 while (tokstart < header_end && *tokstart != 0x20)
166 tokstart++;
167 break;
168 case 'X': // Vendor extensions
169 if (strncmp("YSCSS=", tokstart, 6) == 0) {
170 static const struct {
171 #define MAX_PIX_FMT_LENGTH 8
172 char name[MAX_PIX_FMT_LENGTH + 1];
173 #undef MAX_PIX_FMT_LENGTH
174 enum AVPixelFormat pix_fmt;
175 } pix_fmt_array[] = {
176 { "420JPEG", AV_PIX_FMT_YUV420P },
177 { "420MPEG2", AV_PIX_FMT_YUV420P },
178 { "420PALDV", AV_PIX_FMT_YUV420P },
179 { "420P9", AV_PIX_FMT_YUV420P9 },
180 { "422P9", AV_PIX_FMT_YUV422P9 },
181 { "444P9", AV_PIX_FMT_YUV444P9 },
182 { "420P10", AV_PIX_FMT_YUV420P10 },
183 { "444P10", AV_PIX_FMT_YUV444P10 },
184 { "420P12", AV_PIX_FMT_YUV420P12 },
185 { "422P12", AV_PIX_FMT_YUV422P12 },
186 { "444P12", AV_PIX_FMT_YUV444P12 },
187 { "420P14", AV_PIX_FMT_YUV420P14 },
188 { "422P14", AV_PIX_FMT_YUV422P14 },
189 { "444P14", AV_PIX_FMT_YUV444P14 },
190 { "420P16", AV_PIX_FMT_YUV420P16 },
191 { "422P16", AV_PIX_FMT_YUV422P16 },
192 { "444P16", AV_PIX_FMT_YUV444P16 },
193 { "411", AV_PIX_FMT_YUV411P },
194 { "422", AV_PIX_FMT_YUV422P },
195 { "444", AV_PIX_FMT_YUV444P },
196 };
197 // Older nonstandard pixel format representation
198 tokstart += 6;
199 for (size_t i = 0; i < FF_ARRAY_ELEMS(pix_fmt_array); i++)
200 if (av_strstart(tokstart, pix_fmt_array[i].name, NULL)) {
201 alt_pix_fmt = pix_fmt_array[i].pix_fmt;
202 break;
203 }
204 } else if (strncmp("COLORRANGE=", tokstart, 11) == 0) {
205 tokstart += 11;
206 if (strncmp("FULL",tokstart, 4) == 0)
207 color_range = AVCOL_RANGE_JPEG;
208 else if (strncmp("LIMITED", tokstart, 7) == 0)
209 color_range = AVCOL_RANGE_MPEG;
210 }
211 while (tokstart < header_end && *tokstart != 0x20)
212 tokstart++;
213 break;
214 }
215 }
216
217 if (width == -1 || height == -1) {
218 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
219 return AVERROR_INVALIDDATA;
220 }
221
222 if (pix_fmt == AV_PIX_FMT_NONE) {
223 if (alt_pix_fmt == AV_PIX_FMT_NONE)
224 pix_fmt = AV_PIX_FMT_YUV420P;
225 else
226 pix_fmt = alt_pix_fmt;
227 }
228
229 if (raten <= 0 || rated <= 0) {
230 // Frame rate unknown
231 raten = 25;
232 rated = 1;
233 }
234
235 if (aspectn == 0 && aspectd == 0) {
236 // Pixel aspect unknown
237 aspectd = 1;
238 }
239
240 st = avformat_new_stream(s, NULL);
241 if (!st)
242 return AVERROR(ENOMEM);
243 st->codecpar->width = width;
244 st->codecpar->height = height;
245 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
246 avpriv_set_pts_info(st, 64, rated, raten);
247 st->avg_frame_rate = av_inv_q(st->time_base);
248 st->codecpar->format = pix_fmt;
249 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
250 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
251 st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
252 st->codecpar->chroma_location = chroma_sample_location;
253 st->codecpar->color_range = color_range;
254 st->codecpar->field_order = field_order;
255 s->packet_size = av_image_get_buffer_size(st->codecpar->format, width, height, 1) + Y4M_FRAME_MAGIC_LEN;
256 if ((int) s->packet_size < 0)
257 return s->packet_size;
258 ffformatcontext(s)->data_offset = data_offset = avio_tell(pb);
259
260 st->duration = (avio_size(pb) - data_offset) / s->packet_size;
261
262 return 0;
263 }
264
yuv4_read_packet(AVFormatContext * s,AVPacket * pkt)265 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
266 {
267 int i;
268 char header[MAX_FRAME_HEADER+1];
269 int ret;
270 int64_t off = avio_tell(s->pb);
271
272 for (i = 0; i < MAX_FRAME_HEADER; i++) {
273 header[i] = avio_r8(s->pb);
274 if (header[i] == '\n') {
275 header[i + 1] = 0;
276 break;
277 }
278 }
279 if (s->pb->error)
280 return s->pb->error;
281 else if (s->pb->eof_reached)
282 return AVERROR_EOF;
283 else if (i == MAX_FRAME_HEADER)
284 return AVERROR_INVALIDDATA;
285
286 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
287 return AVERROR_INVALIDDATA;
288
289 ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
290 if (ret < 0)
291 return ret;
292 else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
293 return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
294 }
295 pkt->stream_index = 0;
296 pkt->pts = (off - ffformatcontext(s)->data_offset) / s->packet_size;
297 pkt->duration = 1;
298 return 0;
299 }
300
yuv4_read_seek(AVFormatContext * s,int stream_index,int64_t pts,int flags)301 static int yuv4_read_seek(AVFormatContext *s, int stream_index,
302 int64_t pts, int flags)
303 {
304 int64_t pos;
305
306 if (flags & AVSEEK_FLAG_BACKWARD)
307 pts = FFMAX(0, pts - 1);
308 if (pts < 0)
309 return -1;
310 pos = pts * s->packet_size;
311
312 if (avio_seek(s->pb, pos + ffformatcontext(s)->data_offset, SEEK_SET) < 0)
313 return -1;
314 return 0;
315 }
316
yuv4_probe(const AVProbeData * pd)317 static int yuv4_probe(const AVProbeData *pd)
318 {
319 /* check file header */
320 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
321 return AVPROBE_SCORE_MAX;
322 else
323 return 0;
324 }
325
326 const AVInputFormat ff_yuv4mpegpipe_demuxer = {
327 .name = "yuv4mpegpipe",
328 .long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
329 .read_probe = yuv4_probe,
330 .read_header = yuv4_read_header,
331 .read_packet = yuv4_read_packet,
332 .read_seek = yuv4_read_seek,
333 .extensions = "y4m",
334 };
335