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
25
26 /* Header size increased to allow room for optional flags */
27 #define MAX_YUV4_HEADER 80
28 #define MAX_FRAME_HEADER 80
29
31 {
33 // the longest option
34 char *tokstart, *tokend, *header_end;
35 int i;
38 rated = 0, aspectn = 0, aspectd = 0;
43
46 if (header[i] == '\n') {
47 header[i + 1] = 0x20; // Add a space after last option.
48 // Makes parsing "444" vs "444alpha" easier.
49 header[i + 2] = 0;
50 break;
51 }
52 }
53 if (i == MAX_YUV4_HEADER)
54 return -1;
56 return -1;
57
58 header_end = &header[i + 1]; // Include space
59 for (tokstart = &header[strlen(
Y4M_MAGIC) + 1];
60 tokstart < header_end; tokstart++) {
61 if (*tokstart == 0x20)
62 continue;
63 switch (*tokstart++) {
64 case 'W': // Width. Required.
65 width = strtol(tokstart, &tokend, 10);
66 tokstart = tokend;
67 break;
68 case 'H': // Height. Required.
69 height = strtol(tokstart, &tokend, 10);
70 tokstart = tokend;
71 break;
72 case 'C': // Color space
73 if (strncmp("420jpeg", tokstart, 7) == 0) {
76 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
79 } else if (strncmp("420paldv", tokstart, 8) == 0) {
82 } else if (strncmp("420p16", tokstart, 6) == 0) {
84 } else if (strncmp("422p16", tokstart, 6) == 0) {
86 } else if (strncmp("444p16", tokstart, 6) == 0) {
88 } else if (strncmp("420p14", tokstart, 6) == 0) {
90 } else if (strncmp("422p14", tokstart, 6) == 0) {
92 } else if (strncmp("444p14", tokstart, 6) == 0) {
94 } else if (strncmp("420p12", tokstart, 6) == 0) {
96 } else if (strncmp("422p12", tokstart, 6) == 0) {
98 } else if (strncmp("444p12", tokstart, 6) == 0) {
100 } else if (strncmp("420p10", tokstart, 6) == 0) {
102 } else if (strncmp("422p10", tokstart, 6) == 0) {
104 } else if (strncmp("444p10", tokstart, 6) == 0) {
106 } else if (strncmp("420p9", tokstart, 5) == 0) {
108 } else if (strncmp("422p9", tokstart, 5) == 0) {
110 } else if (strncmp("444p9", tokstart, 5) == 0) {
112 } else if (strncmp("420", tokstart, 3) == 0) {
115 } else if (strncmp("411", tokstart, 3) == 0) {
117 } else if (strncmp("422", tokstart, 3) == 0) {
119 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
121 "YUV4MPEG stream.\n");
122 return -1;
123 } else if (strncmp("444", tokstart, 3) == 0) {
125 } else if (strncmp("mono16", tokstart, 6) == 0) {
127 } else if (strncmp("mono", tokstart, 4) == 0) {
129 } else {
131 "pixel format.\n");
132 return -1;
133 }
134 while (tokstart < header_end && *tokstart != 0x20)
135 tokstart++;
136 break;
137 case 'I': // Interlace type
138 switch (*tokstart++){
139 case '?':
141 break;
142 case 'p':
144 break;
145 case 't':
147 break;
148 case 'b':
150 break;
151 case 'm':
153 "interlaced and non-interlaced frames.\n");
154 default:
157 }
158 break;
159 case 'F': // Frame rate
160 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
161 while (tokstart < header_end && *tokstart != 0x20)
162 tokstart++;
163 break;
164 case 'A': // Pixel aspect
165 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
166 while (tokstart < header_end && *tokstart != 0x20)
167 tokstart++;
168 break;
169 case 'X': // Vendor extensions
170 if (strncmp("YSCSS=", tokstart, 6) == 0) {
171 // Older nonstandard pixel format representation
172 tokstart += 6;
173 if (strncmp("420JPEG", tokstart, 7) == 0)
175 else if (strncmp("420MPEG2", tokstart, 8) == 0)
177 else if (strncmp("420PALDV", tokstart, 8) == 0)
179 else if (strncmp("420P9", tokstart, 5) == 0)
181 else if (strncmp("422P9", tokstart, 5) == 0)
183 else if (strncmp("444P9", tokstart, 5) == 0)
185 else if (strncmp("420P10", tokstart, 6) == 0)
187 else if (strncmp("422P10", tokstart, 6) == 0)
189 else if (strncmp("444P10", tokstart, 6) == 0)
191 else if (strncmp("420P12", tokstart, 6) == 0)
193 else if (strncmp("422P12", tokstart, 6) == 0)
195 else if (strncmp("444P12", tokstart, 6) == 0)
197 else if (strncmp("420P14", tokstart, 6) == 0)
199 else if (strncmp("422P14", tokstart, 6) == 0)
201 else if (strncmp("444P14", tokstart, 6) == 0)
203 else if (strncmp("420P16", tokstart, 6) == 0)
205 else if (strncmp("422P16", tokstart, 6) == 0)
207 else if (strncmp("444P16", tokstart, 6) == 0)
209 else if (strncmp("411", tokstart, 3) == 0)
211 else if (strncmp("422", tokstart, 3) == 0)
213 else if (strncmp("444", tokstart, 3) == 0)
215 }
216 while (tokstart < header_end && *tokstart != 0x20)
217 tokstart++;
218 break;
219 }
220 }
221
222 if (width == -1 ||
height == -1) {
224 return -1;
225 }
226
230 else
231 pix_fmt = alt_pix_fmt;
232 }
233
234 if (raten <= 0 || rated <= 0) {
235 // Frame rate unknown
236 raten = 25;
237 rated = 1;
238 }
239
240 if (aspectn == 0 && aspectd == 0) {
241 // Pixel aspect unknown
242 aspectd = 1;
243 }
244
246 if (!st)
250 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
259
260 return 0;
261 }
262
264 {
265 int i;
269
272 if (header[i] == '\n') {
273 header[i + 1] = 0;
274 break;
275 }
276 }
281 else if (i == MAX_FRAME_HEADER)
283
286
289
291 if (packet_size < 0)
292 return packet_size;
293
295 if (ret < 0)
297 else if (ret != packet_size)
299
301 return 0;
302 }
303
305 {
306 /* check file header */
309 else
310 return 0;
311 }
312
314 .
name =
"yuv4mpegpipe",
319 .extensions = "y4m",
320 };