FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/yuv4mpegdec.c
Date: 2026-04-30 23:37:26
Exec Total Coverage
Lines: 127 164 77.4%
Functions: 4 4 100.0%
Branches: 70 106 66.0%

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