FFmpeg coverage


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