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
23
27
28 /* Header size increased to allow room for optional flags */
29 #define MAX_YUV4_HEADER 80
30 #define MAX_FRAME_HEADER 80
31
33 {
35 // the longest option
36 char *tokstart, *tokend, *header_end;
37 int i;
40 rated = 0, aspectn = 0, aspectd = 0;
45
48 if (header[i] == '\n') {
49 header[i + 1] = 0x20; // Add a space after last option.
50 // Makes parsing "444" vs "444alpha" easier.
51 header[i + 2] = 0;
52 break;
53 }
54 }
55 if (i == MAX_YUV4_HEADER)
56 return -1;
58 return -1;
59
60 header_end = &header[i + 1]; // Include space
61 for (tokstart = &header[strlen(
Y4M_MAGIC) + 1];
62 tokstart < header_end; tokstart++) {
63 if (*tokstart == 0x20)
64 continue;
65 switch (*tokstart++) {
66 case 'W': // Width. Required.
67 width = strtol(tokstart, &tokend, 10);
68 tokstart = tokend;
69 break;
70 case 'H': // Height. Required.
71 height = strtol(tokstart, &tokend, 10);
72 tokstart = tokend;
73 break;
74 case 'C': // Color space
75 if (strncmp("420jpeg", tokstart, 7) == 0) {
78 } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
81 } else if (strncmp("420paldv", tokstart, 8) == 0) {
84 } else if (strncmp("420p16", tokstart, 6) == 0) {
86 } else if (strncmp("422p16", tokstart, 6) == 0) {
88 } else if (strncmp("444p16", tokstart, 6) == 0) {
90 } else if (strncmp("420p14", tokstart, 6) == 0) {
92 } else if (strncmp("422p14", tokstart, 6) == 0) {
94 } else if (strncmp("444p14", tokstart, 6) == 0) {
96 } else if (strncmp("420p12", tokstart, 6) == 0) {
98 } else if (strncmp("422p12", tokstart, 6) == 0) {
100 } else if (strncmp("444p12", tokstart, 6) == 0) {
102 } else if (strncmp("420p10", tokstart, 6) == 0) {
104 } else if (strncmp("422p10", tokstart, 6) == 0) {
106 } else if (strncmp("444p10", tokstart, 6) == 0) {
108 } else if (strncmp("420p9", tokstart, 5) == 0) {
110 } else if (strncmp("422p9", tokstart, 5) == 0) {
112 } else if (strncmp("444p9", tokstart, 5) == 0) {
114 } else if (strncmp("420", tokstart, 3) == 0) {
117 } else if (strncmp("411", tokstart, 3) == 0) {
119 } else if (strncmp("422", tokstart, 3) == 0) {
121 } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
123 "YUV4MPEG stream.\n");
124 return -1;
125 } else if (strncmp("444", tokstart, 3) == 0) {
127 } else if (strncmp("mono16", tokstart, 6) == 0) {
129 } else if (strncmp("mono12", tokstart, 6) == 0) {
131 } else if (strncmp("mono10", tokstart, 6) == 0) {
133 } else if (strncmp("mono9", tokstart, 5) == 0) {
135 } else if (strncmp("mono", tokstart, 4) == 0) {
137 } else {
139 "pixel format.\n");
140 return -1;
141 }
142 while (tokstart < header_end && *tokstart != 0x20)
143 tokstart++;
144 break;
145 case 'I': // Interlace type
146 switch (*tokstart++){
147 case '?':
149 break;
150 case 'p':
152 break;
153 case 't':
155 break;
156 case 'b':
158 break;
159 case 'm':
161 "interlaced and non-interlaced frames.\n");
162 default:
165 }
166 break;
167 case 'F': // Frame rate
168 sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
169 while (tokstart < header_end && *tokstart != 0x20)
170 tokstart++;
171 break;
172 case 'A': // Pixel aspect
173 sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
174 while (tokstart < header_end && *tokstart != 0x20)
175 tokstart++;
176 break;
177 case 'X': // Vendor extensions
178 if (strncmp("YSCSS=", tokstart, 6) == 0) {
179 // Older nonstandard pixel format representation
180 tokstart += 6;
181 if (strncmp("420JPEG", tokstart, 7) == 0)
183 else if (strncmp("420MPEG2", tokstart, 8) == 0)
185 else if (strncmp("420PALDV", tokstart, 8) == 0)
187 else if (strncmp("420P9", tokstart, 5) == 0)
189 else if (strncmp("422P9", tokstart, 5) == 0)
191 else if (strncmp("444P9", tokstart, 5) == 0)
193 else if (strncmp("420P10", tokstart, 6) == 0)
195 else if (strncmp("422P10", tokstart, 6) == 0)
197 else if (strncmp("444P10", tokstart, 6) == 0)
199 else if (strncmp("420P12", tokstart, 6) == 0)
201 else if (strncmp("422P12", tokstart, 6) == 0)
203 else if (strncmp("444P12", tokstart, 6) == 0)
205 else if (strncmp("420P14", tokstart, 6) == 0)
207 else if (strncmp("422P14", tokstart, 6) == 0)
209 else if (strncmp("444P14", tokstart, 6) == 0)
211 else if (strncmp("420P16", tokstart, 6) == 0)
213 else if (strncmp("422P16", tokstart, 6) == 0)
215 else if (strncmp("444P16", tokstart, 6) == 0)
217 else if (strncmp("411", tokstart, 3) == 0)
219 else if (strncmp("422", tokstart, 3) == 0)
221 else if (strncmp("444", tokstart, 3) == 0)
223 }
224 while (tokstart < header_end && *tokstart != 0x20)
225 tokstart++;
226 break;
227 }
228 }
229
230 if (width == -1 ||
height == -1) {
232 return -1;
233 }
234
238 else
239 pix_fmt = alt_pix_fmt;
240 }
241
242 if (raten <= 0 || rated <= 0) {
243 // Frame rate unknown
244 raten = 25;
245 rated = 1;
246 }
247
248 if (aspectn == 0 && aspectd == 0) {
249 // Pixel aspect unknown
250 aspectd = 1;
251 }
252
254 if (!st)
258 av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
271
273
274 return 0;
275 }
276
278 {
279 int i;
281 int ret;
283
286 if (header[i] == '\n') {
287 header[i + 1] = 0;
288 break;
289 }
290 }
295 else if (i == MAX_FRAME_HEADER)
297
300
302 if (ret < 0)
303 return ret;
307 }
311 return 0;
312 }
313
316 {
318 return -1;
319 return 0;
320 }
321
323 {
324 /* check file header */
327 else
328 return 0;
329 }
330
332 .
name =
"yuv4mpegpipe",
338 .extensions = "y4m",
339 };
enum AVChromaLocation chroma_location
enum AVFieldOrder field_order
Video only.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static enum AVPixelFormat pix_fmt
int64_t avio_size(AVIOContext *s)
Get the filesize.
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
#define AV_PIX_FMT_YUV444P14
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
AVFormatInternal * internal
An opaque field for libavformat internal usage.
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY12
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
#define AVERROR_EOF
End of file.
#define AV_PIX_FMT_YUV444P16
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static const uint8_t header[24]
#define AV_PIX_FMT_YUV422P12
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
enum AVMediaType codec_type
General type of the encoded data.
#define AV_PIX_FMT_YUV444P10
AVRational avg_frame_rate
Average framerate.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int avio_r8(AVIOContext *s)
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
#define AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_GRAY16
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
static int yuv4_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P14
static int yuv4_read_header(AVFormatContext *s)
AVIOContext * pb
I/O context.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
#define AV_PIX_FMT_YUV420P10
Rational number (pair of numerator and denominator).
#define AV_PIX_FMT_YUV420P9
int error
contains the error code or 0 if no error happened
This structure contains the data a format has to probe a file.
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV444P12
int64_t duration
Decoding: duration of the stream, in stream time base.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
int eof_reached
true if eof reached
AVInputFormat ff_yuv4mpegpipe_demuxer
static int yuv4_probe(AVProbeData *pd)
#define Y4M_FRAME_MAGIC_LEN
AVChromaLocation
Location of chroma samples.
MPEG-1 4:2:0, JPEG 4:2:0, H.263 4:2:0.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define AV_PIX_FMT_YUV422P16
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...