Debian Codegraph @

utils/v4l2-compliance/v4l2-test-formats.cpp

raw
1 /*
2 V4L2 API format ioctl tests.
3
4 Copyright (C) 2011 Hans Verkuil <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
19 */
20
21 #include <cassert>
22 #include <map>
23 #include <set>
24
25 #include <sys/types.h>
26
27 #include "compiler.h"
28 #include "v4l2-compliance.h"
29
30 static constexpr __u32 buftype2cap[] = {
31 0,
32 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_M2M,
33 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_M2M,
34 V4L2_CAP_VIDEO_OVERLAY,
35 V4L2_CAP_VBI_CAPTURE,
36 V4L2_CAP_VBI_OUTPUT,
37 V4L2_CAP_SLICED_VBI_CAPTURE,
38 V4L2_CAP_SLICED_VBI_OUTPUT,
39 V4L2_CAP_VIDEO_OUTPUT_OVERLAY,
40 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE,
41 V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_VIDEO_M2M_MPLANE,
42 V4L2_CAP_SDR_CAPTURE,
43 V4L2_CAP_SDR_OUTPUT,
44 V4L2_CAP_META_CAPTURE,
45 V4L2_CAP_META_OUTPUT,
46 };
47
48 static int testEnumFrameIntervals(struct node *node, __u32 pixfmt,
49 __u32 w, __u32 h, __u32 type)
50 {
51 struct v4l2_frmivalenum frmival;
52 const struct v4l2_frmival_stepwise *sw = &frmival.stepwise;
53 bool found_stepwise = false;
54 unsigned f = 0;
55 int ret;
56
57 for (;;) {
58 memset(&frmival, 0xff, sizeof(frmival));
59 frmival.index = f;
60 frmival.pixel_format = pixfmt;
61 frmival.width = w;
62 frmival.height = h;
63
64 ret = doioctl(node, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
65 if (ret == ENOTTY)
66 return ret;
67 // M2M devices don't support this, except for stateful encoders
68 fail_on_test(node->is_m2m && !(node->codec_mask & STATEFUL_ENCODER));
69 if (f == 0 && ret == EINVAL) {
70 if (type == V4L2_FRMSIZE_TYPE_DISCRETE)
71 warn("found framesize %ux%u, but no frame intervals\n", w, h);
72 return ENOTTY;
73 }
74 if (ret == EINVAL)
75 break;
76 if (ret)
77 return fail("expected EINVAL, but got %d when enumerating frameinterval %d\n", ret, f);
78 ret = check_0(frmival.reserved, sizeof(frmival.reserved));
79 if (ret)
80 return fail("frmival.reserved not zeroed\n");
81 if (frmival.pixel_format != pixfmt || frmival.index != f ||
82 frmival.width != w || frmival.height != h)
83 return fail("frmival.pixel_format, index, width or height changed\n");
84 switch (frmival.type) {
85 case V4L2_FRMIVAL_TYPE_DISCRETE:
86 ret = check_fract(&frmival.discrete);
87 if (ret)
88 return fail("invalid frameinterval %d (%d/%d)\n", f,
89 frmival.discrete.numerator,
90 frmival.discrete.denominator);
91 if (found_stepwise)
92 return fail("mixing discrete and stepwise is not allowed\n");
93 break;
94 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
95 if (sw->step.numerator != 1 || sw->step.denominator != 1)
96 return fail("invalid step for continuous frameinterval\n");
97 fallthrough;
98 case V4L2_FRMIVAL_TYPE_STEPWISE:
99 if (frmival.index)
100 return fail("index must be 0 for stepwise/continuous frameintervals\n");
101 found_stepwise = true;
102 ret = check_fract(&sw->min);
103 if (ret == 0)
104 ret = check_fract(&sw->max);
105 if (ret == 0)
106 ret = check_fract(&sw->step);
107 if (ret)
108 return fail("invalid min, max or step for frameinterval %d\n", f);
109 if (fract2f(&sw->min) > fract2f(&sw->max))
110 return fail("min > max\n");
111 if (frmival.type == V4L2_FRMIVAL_TYPE_STEPWISE &&
112 fract2f(&sw->step) > fract2f(&sw->max) - fract2f(&sw->min))
113 return fail("step > (max - min)\n");
114 break;
115 default:
116 return fail("frmival.type is invalid\n");
117 }
118
119 f++;
120 node->has_frmintervals = true;
121 }
122 if (type == 0)
123 return fail("found frame intervals for invalid size %ux%u\n", w, h);
124 info("found %d frameintervals for pixel format %08x (%s) and size %ux%u\n",
125 f, pixfmt, fcc2s(pixfmt).c_str(), w, h);
126 return 0;
127 }
128
129 static int testEnumFrameSizes(struct node *node, __u32 pixfmt)
130 {
131 struct v4l2_frmsizeenum frmsize;
132 const struct v4l2_frmsize_stepwise *sw = &frmsize.stepwise;
133 bool found_stepwise = false;
134 __u64 cookie;
135 unsigned f = 0;
136 unsigned count = 0;
137 int ret;
138
139 for (;;) {
140 memset(&frmsize, 0xff, sizeof(frmsize));
141 frmsize.index = f;
142 frmsize.pixel_format = pixfmt;
143
144 ret = doioctl(node, VIDIOC_ENUM_FRAMESIZES, &frmsize);
145 if (ret == ENOTTY)
146 return ret;
147 if (f == 0 && ret == EINVAL)
148 return ENOTTY;
149 if (ret == EINVAL)
150 break;
151 if (ret)
152 return fail("expected EINVAL, but got %d when enumerating framesize %d\n", ret, f);
153 ret = check_0(frmsize.reserved, sizeof(frmsize.reserved));
154 if (ret)
155 return fail("frmsize.reserved not zeroed\n");
156 if (frmsize.pixel_format != pixfmt || frmsize.index != f)
157 return fail("frmsize.pixel_format or index changed\n");
158 switch (frmsize.type) {
159 case V4L2_FRMSIZE_TYPE_DISCRETE:
160 if (frmsize.discrete.width == 0 || frmsize.discrete.height == 0)
161 return fail("invalid width/height for discrete framesize\n");
162 if (found_stepwise)
163 return fail("mixing discrete and stepwise is not allowed\n");
164 ret = testEnumFrameIntervals(node, pixfmt,
165 frmsize.discrete.width, frmsize.discrete.height, frmsize.type);
166 if (ret && ret != ENOTTY)
167 return ret;
168 ret = testEnumFrameIntervals(node, pixfmt,
169 frmsize.discrete.width + 1, frmsize.discrete.height, 0);
170 if (ret && ret != ENOTTY)
171 return ret;
172 if (ret == 0 && !(node->g_caps() & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
173 return fail("found discrete framesizes when no video capture is supported\n");
174 cookie = (static_cast<__u64>(pixfmt) << 32) |
175 (frmsize.discrete.width << 16) |
176 frmsize.discrete.height;
177 node->frmsizes.insert(cookie);
178 count++;
179 break;
180 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
181 if (frmsize.stepwise.step_width != 1 || frmsize.stepwise.step_height != 1)
182 return fail("invalid step_width/height for continuous framesize\n");
183 fallthrough;
184 case V4L2_FRMSIZE_TYPE_STEPWISE:
185 if (frmsize.index)
186 return fail("index must be 0 for stepwise/continuous framesizes\n");
187 found_stepwise = true;
188 if (!sw->min_width || !sw->min_height || !sw->step_width || !sw->step_height)
189 return fail("0 for min_width/height or step_width/height\n");
190 if (sw->min_width > sw->max_width || sw->min_height > sw->max_height)
191 return fail("min_width/height > max_width/height\n");
192 if (sw->step_width > sw->max_width - sw->min_width ||
193 sw->step_height > sw->max_height - sw->min_height)
194 return fail("step > max - min for width or height\n");
195 ret = testEnumFrameIntervals(node, pixfmt,
196 sw->min_width, sw->min_height, frmsize.type);
197 if (ret && ret != ENOTTY)
198 return ret;
199 ret = testEnumFrameIntervals(node, pixfmt,
200 sw->max_width, sw->max_height, frmsize.type);
201 if (ret && ret != ENOTTY)
202 return ret;
203 ret = testEnumFrameIntervals(node, pixfmt,
204 sw->min_width - 1, sw->min_height, 0);
205 if (ret && ret != ENOTTY)
206 return ret;
207 ret = testEnumFrameIntervals(node, pixfmt,
208 sw->max_width, sw->max_height + 1, 0);
209 if (ret && ret != ENOTTY)
210 return ret;
211 break;
212 default:
213 return fail("frmsize.type is invalid\n");
214 }
215
216 f++;
217 }
218 node->frmsizes_count[pixfmt] = count;
219 info("found %d framesizes for pixel format %08x (%s)\n",
220 f, pixfmt, fcc2s(pixfmt).c_str());
221 return 0;
222 }
223
224 static int testEnumFormatsType(struct node *node, unsigned type)
225 {
226 pixfmt_map &map = node->buftype_pixfmts[type];
227 pixfmt_map enum_all;
228 struct v4l2_fmtdesc fmtdesc;
229 unsigned f = 0;
230 int ret;
231
232 for (;;) {
233 memset(&fmtdesc, 0xff, sizeof(fmtdesc));
234 fmtdesc.type = type;
235 fmtdesc.index = f;
236 fmtdesc.mbus_code = 0;
237
238 ret = doioctl(node, VIDIOC_ENUM_FMT, &fmtdesc);
239 if (ret == ENOTTY)
240 return ret;
241 if (f == 0 && ret == EINVAL)
242 return ENOTTY;
243 if (ret == EINVAL)
244 break;
245 if (ret)
246 return fail("expected EINVAL, but got %d when enumerating buftype %d\n", ret, type);
247 ret = check_0(fmtdesc.reserved, sizeof(fmtdesc.reserved));
248 if (ret)
249 return fail("fmtdesc.reserved not zeroed\n");
250 if (fmtdesc.index != f)
251 return fail("fmtdesc.index was modified\n");
252 if (fmtdesc.type != type)
253 return fail("fmtdesc.type was modified\n");
254 ret = check_ustring(fmtdesc.description, sizeof(fmtdesc.description));
255 if (ret)
256 return fail("fmtdesc.description not set\n");
257 if (!fmtdesc.pixelformat)
258 return fail("fmtdesc.pixelformat not set\n");
259
260 // Check that the driver does not overwrites the kernel pixelformat description
261 std::string descr = pixfmt2s(fmtdesc.pixelformat);
262 // In v6.2 the Y/CbCr and Y/CrCb strings were replaced by
263 // Y/UV and Y/VU. Accept both variants for now.
264 std::string descr_alt = descr;
265 size_t idx = descr_alt.find("Y/UV", 0);
266 if (idx != std::string::npos)
267 descr_alt.replace(idx, 4, "Y/CbCr");
268 idx = descr_alt.find("Y/VU", 0);
269 if (idx != std::string::npos)
270 descr_alt.replace(idx, 4, "Y/CrCb");
271 if (strcmp(descr.c_str(), (const char *)fmtdesc.description) &&
272 strcmp(descr_alt.c_str(), (const char *)fmtdesc.description) &&
273 memcmp(descr.c_str(), "Unknown", 7))
274 return fail("fmtdesc.description mismatch: was '%s', expected '%s'\n",
275 fmtdesc.description, descr.c_str());
276
277 if (node->g_direct() && (fmtdesc.flags & V4L2_FMT_FLAG_EMULATED))
278 return fail("drivers must never set the emulated flag\n");
279 if (fmtdesc.flags & ~(V4L2_FMT_FLAG_COMPRESSED | V4L2_FMT_FLAG_EMULATED |
280 V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM |
281 V4L2_FMT_FLAG_DYN_RESOLUTION |
282 V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL |
283 V4L2_FMT_FLAG_CSC_COLORSPACE |
284 V4L2_FMT_FLAG_CSC_YCBCR_ENC | V4L2_FMT_FLAG_CSC_HSV_ENC |
285 V4L2_FMT_FLAG_CSC_QUANTIZATION | V4L2_FMT_FLAG_CSC_XFER_FUNC |
286 V4L2_FMT_FLAG_META_LINE_BASED))
287 return fail("unknown flag %08x returned\n", fmtdesc.flags);
288 if (!(fmtdesc.flags & V4L2_FMT_FLAG_COMPRESSED))
289 fail_on_test(fmtdesc.flags & (V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM |
290 V4L2_FMT_FLAG_DYN_RESOLUTION |
291 V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL));
292
293 // Checks for metadata formats.
294 // The META_LINE_BASED flag can be set for metadata formats only.
295 if (type == V4L2_BUF_TYPE_META_OUTPUT || type == V4L2_BUF_TYPE_META_CAPTURE)
296 fail_on_test(fmtdesc.flags & ~V4L2_FMT_FLAG_META_LINE_BASED);
297 // Only the META_LINE_BASED flag is valid for metadata formats.
298 if (fmtdesc.flags & V4L2_FMT_FLAG_META_LINE_BASED)
299 fail_on_test(type != V4L2_BUF_TYPE_META_OUTPUT &&
300 type != V4L2_BUF_TYPE_META_CAPTURE);
301
302 ret = testEnumFrameSizes(node, fmtdesc.pixelformat);
303 if (ret)
304 fail_on_test(node->codec_mask & STATEFUL_ENCODER);
305 if (ret && ret != ENOTTY)
306 return ret;
307 if (fmtdesc.flags & V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL) {
308 fail_on_test(!(node->codec_mask & STATEFUL_ENCODER));
309 node->has_enc_cap_frame_interval = true;
310 }
311 f++;
312 if (type == V4L2_BUF_TYPE_PRIVATE)
313 continue;
314 // Update define in v4l2-compliance.h if new buffer types are added
315 assert(type <= V4L2_BUF_TYPE_LAST);
316 if (map.find(fmtdesc.pixelformat) != map.end())
317 return fail("duplicate format %08x (%s)\n",
318 fmtdesc.pixelformat, fcc2s(fmtdesc.pixelformat).c_str());
319 map[fmtdesc.pixelformat] = fmtdesc.flags;
320 }
321 info("found %d formats for buftype %d\n", f, type);
322
323 /* Test V4L2_FMTDESC_FLAG_ENUM_ALL if supported */
324 f = 0;
325 for (;;) {
326 memset(&fmtdesc, 0xff, sizeof(fmtdesc));
327 fmtdesc.type = type;
328 fmtdesc.index = f | V4L2_FMTDESC_FLAG_ENUM_ALL;
329 fmtdesc.mbus_code = 0;
330
331 ret = doioctl(node, VIDIOC_ENUM_FMT, &fmtdesc);
332 if (f == 0 && ret == EINVAL)
333 return 0;
334 if (ret == EINVAL)
335 break;
336 if (ret)
337 return fail("expected EINVAL, but got %d when enumerating buftype %d\n", ret, type);
338 if (fmtdesc.index != f)
339 return fail("V4L2_FMTDESC_FLAG_ENUM_ALL hasn't been cleared from fmtdesc.index 0x%x f 0x%x\n", fmtdesc.index, f);
340 f++;
341 if (type == V4L2_BUF_TYPE_PRIVATE)
342 continue;
343 assert(type <= V4L2_BUF_TYPE_LAST);
344 if (enum_all.find(fmtdesc.pixelformat) != enum_all.end())
345 return fail("duplicate format %08x (%s)\n",
346 fmtdesc.pixelformat, fcc2s(fmtdesc.pixelformat).c_str());
347 enum_all[fmtdesc.pixelformat] = fmtdesc.flags;
348 }
349 info("found %d formats for buftype %d (with V4L2_FMTDESC_FLAG_ENUM_ALL)\n", f, type);
350
351 /* if V4L2_FMTDESC_FLAG_ENUM_ALL is supported, verify that the list is a subset of VIDIOC_ENUM_FMT list */
352 for (auto it = map.begin(); it != map.end(); it++)
353 if (enum_all.find(it->first) == enum_all.end())
354 return fail("V4L2_FMTDESC_FLAG_ENUM_ALL failed to enumerate format %08x (%s)\n", it->first, fcc2s(it->first).c_str());
355
356 return 0;
357 }
358
359 int testEnumFormats(struct node *node)
360 {
361 bool supported = false;
362 unsigned type;
363 int ret;
364
365 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
366 ret = testEnumFormatsType(node, type);
367 if (ret && ret != ENOTTY)
368 return ret;
369 if (!ret)
370 supported = true;
371 switch (type) {
372 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
373 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
374 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
375 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
376 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
377 case V4L2_BUF_TYPE_SDR_CAPTURE:
378 case V4L2_BUF_TYPE_SDR_OUTPUT:
379 if (ret && (node->g_caps() & buftype2cap[type]))
380 return fail("%s cap set, but no %s formats defined\n",
381 buftype2s(type).c_str(), buftype2s(type).c_str());
382 if (!ret && !(node->g_caps() & buftype2cap[type]))
383 return fail("%s cap not set, but %s formats defined\n",
384 buftype2s(type).c_str(), buftype2s(type).c_str());
385 break;
386 case V4L2_BUF_TYPE_META_CAPTURE:
387 case V4L2_BUF_TYPE_META_OUTPUT:
388 /* Metadata formats need not be present for the current input/output */
389 break;
390 default:
391 if (!ret)
392 return fail("Buffer type %s not allowed!\n", buftype2s(type).c_str());
393 break;
394 }
395 }
396
397 ret = testEnumFormatsType(node, V4L2_BUF_TYPE_PRIVATE);
398 if (ret != ENOTTY && ret != EINVAL)
399 return fail("Buffer type PRIVATE allowed!\n");
400
401 ret = testEnumFrameSizes(node, 0x20202020);
402 if (ret != ENOTTY)
403 return fail("Accepted framesize for invalid format\n");
404 ret = testEnumFrameIntervals(node, 0x20202020, 640, 480, 0);
405 if (ret != ENOTTY)
406 return fail("Accepted frameinterval for invalid format\n");
407 return supported ? 0 : ENOTTY;
408 }
409
410 static int testColorspace(bool non_zero_colorspace,
411 __u32 pixelformat, __u32 colorspace, __u32 ycbcr_enc, __u32 quantization)
412 {
413 if (non_zero_colorspace)
414 fail_on_test(!colorspace);
415 fail_on_test(colorspace == V4L2_COLORSPACE_BT878);
416 fail_on_test(pixelformat != V4L2_PIX_FMT_JPEG &&
417 pixelformat != V4L2_PIX_FMT_MJPEG &&
418 colorspace == V4L2_COLORSPACE_JPEG);
419 fail_on_test(colorspace >= 0xff);
420 fail_on_test(ycbcr_enc >= 0xff);
421 fail_on_test(quantization >= 0xff);
422 return 0;
423 }
424
425 int testFBuf(struct node *node)
426 {
427 struct v4l2_framebuffer fbuf;
428 __u32 caps;
429 __u32 flags;
430 int ret;
431
432 memset(&fbuf, 0xff, sizeof(fbuf));
433 fbuf.fmt.priv = 0;
434 ret = doioctl(node, VIDIOC_G_FBUF, &fbuf);
435 fail_on_test(ret == 0 && !(node->g_caps() & (V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_VIDEO_OUTPUT_OVERLAY)));
436 fail_on_test(ret == ENOTTY && (node->g_caps() & (V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_VIDEO_OUTPUT_OVERLAY)));
437 if (ret == ENOTTY)
438 return ret;
439 if (ret && ret != EINVAL)
440 return fail("expected EINVAL, but got %d when getting framebuffer format\n", ret);
441 node->fbuf_caps = caps = fbuf.capability;
442 flags = fbuf.flags;
443 if (node->g_caps() & V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
444 fail_on_test(!fbuf.base);
445 if (flags & V4L2_FBUF_FLAG_CHROMAKEY)
446 fail_on_test(!(caps & V4L2_FBUF_CAP_CHROMAKEY));
447 if (flags & V4L2_FBUF_FLAG_LOCAL_ALPHA)
448 fail_on_test(!(caps & V4L2_FBUF_CAP_LOCAL_ALPHA));
449 if (flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA)
450 fail_on_test(!(caps & V4L2_FBUF_CAP_GLOBAL_ALPHA));
451 if (flags & V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)
452 fail_on_test(!(caps & V4L2_FBUF_CAP_LOCAL_INV_ALPHA));
453 if (flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)
454 fail_on_test(!(caps & V4L2_FBUF_CAP_SRC_CHROMAKEY));
455 fail_on_test(!fbuf.fmt.width || !fbuf.fmt.height);
456 if (fbuf.fmt.priv)
457 warn("fbuf.fmt.priv is non-zero\n");
458 /* Not yet: unclear what EXTERNOVERLAY means in a output overlay context
459 if (caps & V4L2_FBUF_CAP_EXTERNOVERLAY) {
460 fail_on_test(fbuf.fmt.bytesperline);
461 fail_on_test(fbuf.fmt.sizeimage);
462 fail_on_test(fbuf.base);
463 }*/
464 fail_on_test(fbuf.fmt.bytesperline && fbuf.fmt.bytesperline < fbuf.fmt.width);
465 fail_on_test(fbuf.fmt.sizeimage && fbuf.fmt.sizeimage < fbuf.fmt.bytesperline * fbuf.fmt.height);
466 fail_on_test(testColorspace(true, fbuf.fmt.pixelformat, fbuf.fmt.colorspace, 0, 0));
467 return 0;
468 }
469
470 static void createInvalidFmt(struct v4l2_format &fmt, struct v4l2_clip &clip, unsigned type)
471 {
472 memset(&fmt, 0xff, sizeof(fmt));
473 fmt.type = type;
474 fmt.fmt.pix.field = V4L2_FIELD_ANY;
475 if (type == V4L2_BUF_TYPE_VIDEO_OVERLAY ||
476 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
477 memset(&clip, 0xff, sizeof(clip));
478 clip.next = (struct v4l2_clip *)0x0eadbeef;
479 fmt.fmt.win.clipcount = 1;
480 fmt.fmt.win.clips = &clip;
481 fmt.fmt.win.bitmap = nullptr;
482 }
483 }
484
485 static int testFormatsType(struct node *node, int ret, unsigned type, struct v4l2_format &fmt, bool have_clip = false)
486 {
487 const pixfmt_map &map = node->buftype_pixfmts[type];
488 const pixfmt_map *map_splane;
489 const struct v4l2_pix_format &pix = fmt.fmt.pix;
490 const struct v4l2_pix_format_mplane &pix_mp = fmt.fmt.pix_mp;
491 const struct v4l2_window &win = fmt.fmt.win;
492 const struct v4l2_vbi_format &vbi = fmt.fmt.vbi;
493 const struct v4l2_sliced_vbi_format &sliced = fmt.fmt.sliced;
494 const struct v4l2_sdr_format &sdr = fmt.fmt.sdr;
495 const struct v4l2_meta_format &meta = fmt.fmt.meta;
496 unsigned min_data_samples;
497 unsigned min_sampling_rate;
498 v4l2_std_id std;
499 __u32 service_set = 0;
500 unsigned tot_bytesperline = 0;
501 unsigned cnt = 0;
502
503 if (ret == ENOTTY)
504 return ret;
505 if (ret == EINVAL)
506 return ENOTTY;
507 if (ret)
508 return fail("expected EINVAL, but got %d when getting format for buftype %d\n", ret, type);
509 fail_on_test(fmt.type != type);
510
511 switch (type) {
512 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
513 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
514 fail_on_test(!pix.width || !pix.height);
515 if (map.find(pix.pixelformat) == map.end())
516 return fail("pixelformat %08x (%s) for buftype %d not reported by ENUM_FMT\n",
517 pix.pixelformat, fcc2s(pix.pixelformat).c_str(), type);
518 fail_on_test(pix.bytesperline && pix.bytesperline < pix.width);
519 fail_on_test(!pix.sizeimage);
520 if (!node->is_m2m)
521 fail_on_test(testColorspace(!node->is_io_mc,
522 pix.pixelformat, pix.colorspace,
523 pix.ycbcr_enc, pix.quantization));
524 fail_on_test(pix.field == V4L2_FIELD_ANY);
525 if (pix.priv && pix.priv != V4L2_PIX_FMT_PRIV_MAGIC)
526 return fail("priv is non-zero and non-magic!\n");
527 break;
528 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
529 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
530 fail_on_test(!pix_mp.width || !pix_mp.height);
531 map_splane = &node->buftype_pixfmts[type - 8];
532 if (map.find(pix_mp.pixelformat) == map.end() &&
533 map_splane->find(pix_mp.pixelformat) == map_splane->end())
534 return fail("pixelformat %08x (%s) for buftype %d not reported by ENUM_FMT\n",
535 pix_mp.pixelformat, fcc2s(pix_mp.pixelformat).c_str(), type);
536 if (!node->is_m2m)
537 fail_on_test(testColorspace(!node->is_io_mc,
538 pix_mp.pixelformat, pix_mp.colorspace,
539 pix_mp.ycbcr_enc, pix_mp.quantization));
540 fail_on_test(pix_mp.field == V4L2_FIELD_ANY);
541 ret = check_0(pix_mp.reserved, sizeof(pix_mp.reserved));
542 if (ret)
543 return fail("pix_mp.reserved not zeroed\n");
544 fail_on_test(pix_mp.num_planes == 0 || pix_mp.num_planes >= VIDEO_MAX_PLANES);
545 for (int i = 0; i < pix_mp.num_planes; i++) {
546 const struct v4l2_plane_pix_format &pfmt = pix_mp.plane_fmt[i];
547
548 ret = check_0(pfmt.reserved, sizeof(pfmt.reserved));
549 if (ret)
550 return fail("pix_mp.plane_fmt[%d].reserved not zeroed\n", i);
551 fail_on_test(!pfmt.sizeimage);
552 tot_bytesperline += pfmt.bytesperline;
553 }
554 fail_on_test(tot_bytesperline && tot_bytesperline < pix_mp.width);
555 break;
556 case V4L2_BUF_TYPE_VBI_CAPTURE:
557 case V4L2_BUF_TYPE_VBI_OUTPUT:
558 // Currently VBI assumes that you have G_STD as well.
559 fail_on_test(doioctl(node, VIDIOC_G_STD, &std));
560 if (std & V4L2_STD_625_50) {
561 min_sampling_rate = 6937500;
562 // the number of databits for PAL teletext is 18 (clock run in) +
563 // 6 (framing code) + 42 * 8 (data).
564 min_data_samples = (vbi.sampling_rate * (18 + 6 + 42 * 8)) / min_sampling_rate;
565 } else {
566 min_sampling_rate = 5727272;
567 // the number of databits for NTSC teletext is 18 (clock run in) +
568 // 6 (framing code) + 34 * 8 (data).
569 min_data_samples = (vbi.sampling_rate * (18 + 6 + 34 * 8)) / min_sampling_rate;
570 }
571 fail_on_test(vbi.sampling_rate < min_sampling_rate);
572 fail_on_test(!vbi.samples_per_line);
573 fail_on_test(vbi.sample_format != V4L2_PIX_FMT_GREY);
574 fail_on_test(vbi.offset > vbi.samples_per_line);
575 ret = check_0(vbi.reserved, sizeof(vbi.reserved));
576 if (ret)
577 return fail("vbi.reserved not zeroed\n");
578 // Check that offset leaves enough room for the maximum required
579 // amount of data.
580 fail_on_test(min_data_samples > vbi.samples_per_line - vbi.offset);
581 fail_on_test(!vbi.count[0] || !vbi.count[1]);
582 fail_on_test(vbi.flags & ~(V4L2_VBI_UNSYNC | V4L2_VBI_INTERLACED));
583 if (vbi.flags & V4L2_VBI_INTERLACED)
584 fail_on_test(vbi.count[0] != vbi.count[1]);
585 break;
586 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
587 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
588 ret = check_0(sliced.reserved, sizeof(sliced.reserved));
589 if (ret)
590 return fail("sliced.reserved not zeroed\n");
591 fail_on_test(sliced.service_lines[0][0] || sliced.service_lines[1][0]);
592 for (const auto &service_line : sliced.service_lines) {
593 for (unsigned short i : service_line) {
594 if (i)
595 cnt++;
596 service_set |= i;
597 }
598 }
599 fail_on_test(sliced.io_size < sizeof(struct v4l2_sliced_vbi_data) * cnt);
600 fail_on_test(sliced.service_set != service_set);
601 break;
602 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
603 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
604 fail_on_test(win.clipcount && !(node->fbuf_caps & V4L2_FBUF_CAP_LIST_CLIPPING));
605 if (have_clip)
606 fail_on_test(!win.clipcount && (node->fbuf_caps & V4L2_FBUF_CAP_LIST_CLIPPING));
607 if (win.clipcount) {
608 const struct v4l2_rect *r = &win.clips->c;
609 struct v4l2_framebuffer fb;
610
611 fail_on_test(doioctl(node, VIDIOC_G_FBUF, &fb));
612 fail_on_test(!win.clips);
613 fail_on_test(win.clips->next != (void *)0x0eadbeef);
614 fail_on_test(win.clipcount != 1);
615 fail_on_test(r->left < 0 || r->top < 0);
616 fail_on_test((unsigned)r->left >= fb.fmt.width || (unsigned)r->top >= fb.fmt.height);
617 fail_on_test(r->width == 0 || r->height == 0);
618 fail_on_test(r->left + r->width > fb.fmt.width || r->top + r->height > fb.fmt.height);
619 }
620 fail_on_test(win.chromakey && !(node->fbuf_caps & (V4L2_FBUF_CAP_CHROMAKEY | V4L2_FBUF_CAP_SRC_CHROMAKEY)));
621 if (!(node->fbuf_caps & V4L2_FBUF_CAP_BITMAP_CLIPPING))
622 fail_on_test(win.bitmap);
623 fail_on_test(win.global_alpha && !(node->fbuf_caps & V4L2_FBUF_CAP_GLOBAL_ALPHA));
624 break;
625 case V4L2_BUF_TYPE_SDR_CAPTURE:
626 case V4L2_BUF_TYPE_SDR_OUTPUT:
627 if (map.find(sdr.pixelformat) == map.end())
628 return fail("pixelformat %08x (%s) for buftype %d not reported by ENUM_FMT\n",
629 sdr.pixelformat, fcc2s(sdr.pixelformat).c_str(), type);
630 fail_on_test(sdr.buffersize == 0);
631 fail_on_test(check_0(sdr.reserved, sizeof(sdr.reserved)));
632 break;
633 case V4L2_BUF_TYPE_META_CAPTURE:
634 case V4L2_BUF_TYPE_META_OUTPUT:
635 if (map.find(meta.dataformat) == map.end())
636 return fail("dataformat %08x (%s) for buftype %d not reported by ENUM_FMT\n",
637 meta.dataformat, fcc2s(meta.dataformat).c_str(), type);
638 fail_on_test(meta.buffersize == 0);
639 if (map.at(meta.dataformat) & V4L2_FMT_FLAG_META_LINE_BASED) {
640 fail_on_test(!meta.width || !meta.height);
641 fail_on_test(!meta.bytesperline);
642 }
643 break;
644 case V4L2_BUF_TYPE_PRIVATE:
645 break;
646 }
647 return 0;
648 }
649
650 int testGetFormats(struct node *node)
651 {
652 struct v4l2_clip clip;
653 struct v4l2_format fmt;
654 bool supported = false;
655 int type;
656 int ret;
657
658 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
659 createInvalidFmt(fmt, clip, type);
660 ret = doioctl(node, VIDIOC_G_FMT, &fmt);
661 if (!ret)
662 node->valid_buftypes |= 1 << type;
663
664 ret = testFormatsType(node, ret, type, fmt);
665
666 if (ret && ret != ENOTTY)
667 return ret;
668 if (!ret)
669 supported = true;
670
671 switch (type) {
672 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
673 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
674 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
675 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
676 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
677 case V4L2_BUF_TYPE_SDR_CAPTURE:
678 case V4L2_BUF_TYPE_SDR_OUTPUT:
679 if (ret && (node->g_caps() & buftype2cap[type]))
680 return fail("%s cap set, but no %s formats defined\n",
681 buftype2s(type).c_str(), buftype2s(type).c_str());
682 if (!ret && !(node->g_caps() & buftype2cap[type]))
683 return fail("%s cap not set, but %s formats defined\n",
684 buftype2s(type).c_str(), buftype2s(type).c_str());
685 break;
686 case V4L2_BUF_TYPE_META_CAPTURE:
687 case V4L2_BUF_TYPE_META_OUTPUT:
688 if (ret && !node->buftype_pixfmts[type].empty())
689 return fail("%s G_FMT failed, but %s formats defined\n",
690 buftype2s(type).c_str(), buftype2s(type).c_str());
691 if (!ret && node->buftype_pixfmts[type].empty())
692 return fail("%s G_FMT success, but no %s formats defined\n",
693 buftype2s(type).c_str(), buftype2s(type).c_str());
694 break;
695 default:
696 /* ENUMFMT doesn't support other buftypes */
697 break;
698 }
699 }
700
701 memset(&fmt, 0, sizeof(fmt));
702 fmt.type = V4L2_BUF_TYPE_PRIVATE;
703 ret = doioctl(node, VIDIOC_G_FMT, &fmt);
704 if (ret != ENOTTY && ret != EINVAL)
705 return fail("Buffer type PRIVATE allowed!\n");
706 return supported ? 0 : ENOTTY;
707 }
708
709 static bool matchFormats(const struct v4l2_format &f1, const struct v4l2_format &f2)
710 {
711 const struct v4l2_pix_format &pix1 = f1.fmt.pix;
712 const struct v4l2_pix_format &pix2 = f2.fmt.pix;
713 const struct v4l2_pix_format_mplane &pix_mp1 = f1.fmt.pix_mp;
714 const struct v4l2_pix_format_mplane &pix_mp2 = f2.fmt.pix_mp;
715 const struct v4l2_window &win1 = f1.fmt.win;
716 const struct v4l2_window &win2 = f2.fmt.win;
717
718 if (f1.type != f2.type)
719 return false;
720 switch (f1.type) {
721 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
722 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
723 if (!memcmp(&f1.fmt.pix, &f2.fmt.pix, sizeof(f1.fmt.pix)))
724 return true;
725 printf("\t\tG_FMT: %ux%u, %s, %u, %u, %u, %u, %u, %u, %x\n",
726 pix1.width, pix1.height, fcc2s(pix1.pixelformat).c_str(), pix1.field, pix1.bytesperline,
727 pix1.sizeimage, pix1.colorspace, pix1.ycbcr_enc, pix1.quantization, pix1.priv);
728 printf("\t\tTRY/S_FMT: %ux%u, %s, %u, %u, %u, %u, %u, %u, %x\n",
729 pix2.width, pix2.height, fcc2s(pix2.pixelformat).c_str(), pix2.field, pix2.bytesperline,
730 pix2.sizeimage, pix2.colorspace, pix2.ycbcr_enc, pix2.quantization, pix2.priv);
731 return false;
732 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
733 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
734 if (!memcmp(&f1.fmt.win, &f2.fmt.win, sizeof(f1.fmt.win)))
735 return true;
736 printf("\t\tG_FMT: (%d,%d)/%ux%u, %u, %x, %p, %u, %p, %x\n",
737 win1.w.left, win1.w.top, win1.w.width, win1.w.height, win1.field,
738 win1.chromakey, (void *)win1.clips, win1.clipcount, win1.bitmap, win1.global_alpha);
739 printf("\t\tTRY/S_FMT: (%d,%d)/%ux%u, %u, %x, %p, %u, %p, %x\n",
740 win2.w.left, win2.w.top, win2.w.width, win2.w.height, win2.field,
741 win2.chromakey, (void *)win2.clips, win2.clipcount, win2.bitmap, win2.global_alpha);
742 return false;
743 case V4L2_BUF_TYPE_VBI_CAPTURE:
744 case V4L2_BUF_TYPE_VBI_OUTPUT:
745 return !memcmp(&f1.fmt.vbi, &f2.fmt.vbi, sizeof(f1.fmt.vbi));
746 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
747 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
748 return !memcmp(&f1.fmt.sliced, &f2.fmt.sliced, sizeof(f1.fmt.sliced));
749 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
750 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
751 if (!memcmp(&f1.fmt.pix_mp, &f2.fmt.pix_mp, sizeof(f1.fmt.pix_mp)))
752 return true;
753 printf("\t\tG_FMT: %ux%u, %s, %u, %u, %u, %u, %u\n",
754 pix_mp1.width, pix_mp1.height, fcc2s(pix_mp1.pixelformat).c_str(), pix_mp1.num_planes,
755 pix_mp1.field, pix_mp1.colorspace, pix_mp1.ycbcr_enc, pix_mp1.quantization);
756 for (unsigned p = 0; p < pix_mp1.num_planes; p++)
757 printf("\t\t\t%d: %d, %d\n", p, pix_mp1.plane_fmt[p].sizeimage, pix_mp1.plane_fmt[p].bytesperline);
758 printf("\t\tTRY/S_FMT: %ux%u, %s, %u, %u, %u, %u, %u\n",
759 pix_mp2.width, pix_mp2.height, fcc2s(pix_mp2.pixelformat).c_str(), pix_mp2.num_planes,
760 pix_mp2.field, pix_mp2.colorspace, pix_mp2.ycbcr_enc, pix_mp2.quantization);
761 for (unsigned p = 0; p < pix_mp2.num_planes; p++)
762 printf("\t\t\t%u: %u, %u\n", p, pix_mp2.plane_fmt[p].sizeimage, pix_mp2.plane_fmt[p].bytesperline);
763 return false;
764 case V4L2_BUF_TYPE_SDR_CAPTURE:
765 case V4L2_BUF_TYPE_SDR_OUTPUT:
766 return !memcmp(&f1.fmt.sdr, &f2.fmt.sdr, sizeof(f1.fmt.sdr));
767 case V4L2_BUF_TYPE_META_CAPTURE:
768 case V4L2_BUF_TYPE_META_OUTPUT:
769 return !memcmp(&f1.fmt.meta, &f2.fmt.meta, sizeof(f1.fmt.meta));
770
771 }
772 return false;
773 }
774
775 int testTryFormats(struct node *node)
776 {
777 struct v4l2_clip clip;
778 struct v4l2_format fmt, fmt_try;
779 int result = 0;
780 int type;
781 int ret;
782
783 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
784 if (!(node->valid_buftypes & (1 << type)))
785 continue;
786
787 switch (type) {
788 case V4L2_BUF_TYPE_VBI_CAPTURE:
789 case V4L2_BUF_TYPE_VBI_OUTPUT:
790 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
791 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
792 if (!(node->cur_io_caps & V4L2_IN_CAP_STD))
793 continue;
794 break;
795 }
796
797 createInvalidFmt(fmt, clip, type);
798 doioctl(node, VIDIOC_G_FMT, &fmt);
799 fmt_try = fmt;
800 ret = doioctl(node, VIDIOC_TRY_FMT, &fmt_try);
801 if (ret)
802 return fail("%s is valid, but no TRY_FMT was implemented\n",
803 buftype2s(type).c_str());
804 ret = testFormatsType(node, ret, type, fmt_try);
805 if (ret)
806 return ret;
807 if (!matchFormats(fmt, fmt_try))
808 result = fail("%s: TRY_FMT(G_FMT) != G_FMT\n",
809 buftype2s(type).c_str());
810 }
811
812 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
813 if (!(node->valid_buftypes & (1 << type)))
814 continue;
815
816 switch (type) {
817 case V4L2_BUF_TYPE_VBI_CAPTURE:
818 case V4L2_BUF_TYPE_VBI_OUTPUT:
819 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
820 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
821 if (!(node->cur_io_caps & V4L2_IN_CAP_STD))
822 continue;
823 break;
824 }
825
826 createInvalidFmt(fmt, clip, type);
827 ret = doioctl(node, VIDIOC_TRY_FMT, &fmt);
828 if (ret == EINVAL) {
829 __u32 pixelformat;
830 bool is_mplane = false;
831
832 /* In case of failure obtain a valid pixelformat and insert
833 * that in the next attempt to call TRY_FMT. */
834 doioctl(node, VIDIOC_G_FMT, &fmt);
835
836 switch (type) {
837 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
838 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
839 pixelformat = fmt.fmt.pix.pixelformat;
840 break;
841 case V4L2_BUF_TYPE_SDR_CAPTURE:
842 case V4L2_BUF_TYPE_SDR_OUTPUT:
843 pixelformat = fmt.fmt.sdr.pixelformat;
844 break;
845 case V4L2_BUF_TYPE_META_CAPTURE:
846 case V4L2_BUF_TYPE_META_OUTPUT:
847 pixelformat = fmt.fmt.meta.dataformat;
848 break;
849 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
850 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
851 pixelformat = fmt.fmt.pix_mp.pixelformat;
852 is_mplane = true;
853 break;
854 default:
855 /* for other formats returning EINVAL is certainly wrong */
856 return fail("TRY_FMT cannot handle an invalid format\n");
857 }
858 warn_once("TRY_FMT cannot handle an invalid pixelformat.\n");
859 warn_once("This may or may not be a problem. For more information see:\n");
860 warn_once("http://www.mail-archive.com/[email protected]/msg56550.html\n");
861
862 /* Now try again, but pass a valid pixelformat. */
863 createInvalidFmt(fmt, clip, type);
864 if (node->is_sdr)
865 fmt.fmt.sdr.pixelformat = pixelformat;
866 else if (node->is_meta)
867 fmt.fmt.meta.dataformat = pixelformat;
868 else if (is_mplane)
869 fmt.fmt.pix_mp.pixelformat = pixelformat;
870 else
871 fmt.fmt.pix.pixelformat = pixelformat;
872 ret = doioctl(node, VIDIOC_TRY_FMT, &fmt);
873 if (ret == EINVAL)
874 return fail("TRY_FMT cannot handle an invalid format\n");
875 }
876 ret = testFormatsType(node, ret, type, fmt, true);
877 if (ret)
878 return ret;
879 }
880
881 memset(&fmt, 0, sizeof(fmt));
882 fmt.type = V4L2_BUF_TYPE_PRIVATE;
883 ret = doioctl(node, VIDIOC_TRY_FMT, &fmt);
884 if (ret != ENOTTY && ret != EINVAL)
885 return fail("Buffer type PRIVATE allowed!\n");
886 return node->valid_buftypes ? result : ENOTTY;
887 }
888
889 static int testJPEGColorspace(const cv4l_fmt &fmt_raw, const cv4l_fmt &fmt_jpeg,
890 bool is_decoder)
891 {
892 fail_on_test(fmt_raw.g_colorspace() != V4L2_COLORSPACE_SRGB);
893 fail_on_test(fmt_raw.g_xfer_func() &&
894 fmt_raw.g_xfer_func() != V4L2_XFER_FUNC_SRGB);
895 fail_on_test(fmt_raw.g_ycbcr_enc() && is_decoder &&
896 fmt_raw.g_ycbcr_enc() != V4L2_YCBCR_ENC_601);
897 if (fmt_jpeg.g_colorspace() == V4L2_COLORSPACE_JPEG) {
898 /*
899 * If the V4L2_COLORSPACE_JPEG shorthand is used, then the
900 * other values shall be 0.
901 */
902 fail_on_test(fmt_jpeg.g_xfer_func() ||
903 fmt_jpeg.g_ycbcr_enc() ||
904 fmt_jpeg.g_quantization());
905 return 0;
906 }
907 /* V4L2_COLORSPACE_JPEG is shorthand for these values: */
908 fail_on_test(fmt_jpeg.g_colorspace() != V4L2_COLORSPACE_SRGB);
909 fail_on_test(fmt_jpeg.g_xfer_func() != V4L2_XFER_FUNC_SRGB);
910 fail_on_test(fmt_jpeg.g_ycbcr_enc() != V4L2_YCBCR_ENC_601);
911 fail_on_test(fmt_jpeg.g_quantization() != V4L2_QUANTIZATION_FULL_RANGE);
912 return 0;
913 }
914
915 static int testM2MFormats(struct node *node)
916 {
917 cv4l_fmt fmt_out;
918 cv4l_fmt fmt;
919 cv4l_fmt fmt_cap;
920 __u32 cap_type = node->g_type();
921 __u32 out_type = v4l_type_invert(cap_type);
922 __u32 col, ycbcr_enc, quant, xfer_func;
923
924 fail_on_test(node->g_fmt(fmt_out, out_type));
925 fail_on_test(node->g_fmt(fmt_cap, cap_type));
926
927 /*
928 * JPEG codec have fixed colorspace, so these tests
929 * are different compared to other m2m devices.
930 */
931 if (node->codec_mask & JPEG_DECODER)
932 return testJPEGColorspace(fmt_cap, fmt_out, true);
933 if (node->codec_mask & JPEG_ENCODER)
934 return testJPEGColorspace(fmt_out, fmt_cap, false);
935
936 fail_on_test(fmt_cap.g_colorspace() != fmt_out.g_colorspace());
937 fail_on_test(fmt_cap.g_ycbcr_enc() != fmt_out.g_ycbcr_enc());
938 fail_on_test(fmt_cap.g_quantization() != fmt_out.g_quantization());
939 fail_on_test(fmt_cap.g_xfer_func() != fmt_out.g_xfer_func());
940 col = fmt_out.g_colorspace() == V4L2_COLORSPACE_SMPTE170M ?
941 V4L2_COLORSPACE_REC709 : V4L2_COLORSPACE_SMPTE170M;
942 ycbcr_enc = fmt_out.g_ycbcr_enc() == V4L2_YCBCR_ENC_601 ?
943 V4L2_YCBCR_ENC_709 : V4L2_YCBCR_ENC_601;
944 quant = fmt_out.g_quantization() == V4L2_QUANTIZATION_LIM_RANGE ?
945 V4L2_QUANTIZATION_FULL_RANGE : V4L2_QUANTIZATION_LIM_RANGE;
946 xfer_func = fmt_out.g_xfer_func() == V4L2_XFER_FUNC_SRGB ?
947 V4L2_XFER_FUNC_709 : V4L2_XFER_FUNC_SRGB;
948 fmt_out.s_colorspace(col);
949 fmt_out.s_xfer_func(xfer_func);
950 fmt_out.s_ycbcr_enc(ycbcr_enc);
951 fmt_out.s_quantization(quant);
952 node->s_fmt(fmt_out);
953 fail_on_test(fmt_out.g_colorspace() != col);
954 fail_on_test(fmt_out.g_xfer_func() != xfer_func);
955 fail_on_test(fmt_out.g_ycbcr_enc() != ycbcr_enc);
956 fail_on_test(fmt_out.g_quantization() != quant);
957 node->g_fmt(fmt_cap);
958 fail_on_test(fmt_cap.g_colorspace() != col);
959 fail_on_test(fmt_cap.g_xfer_func() != xfer_func);
960 fail_on_test(fmt_cap.g_ycbcr_enc() != ycbcr_enc);
961 fail_on_test(fmt_cap.g_quantization() != quant);
962
963 if (!(node->codec_mask & STATEFUL_ENCODER))
964 return 0;
965
966 fmt = fmt_out;
967 unsigned w = (fmt.g_width() & ~7) - 4;
968 unsigned h = (fmt.g_height() & ~7) - 4;
969 fmt.s_width(w);
970 fmt.s_height(h);
971 node->s_fmt(fmt);
972 fail_on_test(fmt.g_width() < w || fmt.g_width() > fmt_out.g_width());
973 fail_on_test(fmt.g_height() < h || fmt.g_height() > fmt_out.g_height());
974 node->g_fmt(fmt_cap);
975 fail_on_test(fmt_cap.g_width() < fmt.g_width());
976 fail_on_test(fmt_cap.g_height() < fmt.g_height());
977
978 v4l2_selection sel = {
979 .type = fmt.g_type(),
980 .target = V4L2_SEL_TGT_CROP,
981 };
982 if (node->g_selection(sel) == ENOTTY) {
983 fail_on_test(fmt_cap.g_width() != fmt.g_width());
984 fail_on_test(fmt_cap.g_height() != fmt.g_height());
985 return 0;
986 }
987 fail_on_test(sel.r.top || sel.r.left);
988 fail_on_test(sel.r.width != fmt.g_width());
989 fail_on_test(sel.r.height != fmt.g_height());
990 sel.r.width = w;
991 sel.r.height = h;
992 node->s_selection(sel);
993 node->g_selection(sel);
994 fail_on_test(sel.r.top || sel.r.left);
995 fail_on_test(sel.r.width != w);
996 fail_on_test(sel.r.height != h);
997 sel.target = V4L2_SEL_TGT_CROP_BOUNDS;
998 node->g_selection(sel);
999 fail_on_test(sel.r.top || sel.r.left);
1000 fail_on_test(sel.r.width != fmt.g_width());
1001 fail_on_test(sel.r.height != fmt.g_height());
1002 sel.target = V4L2_SEL_TGT_CROP_DEFAULT;
1003 node->g_selection(sel);
1004 fail_on_test(sel.r.top || sel.r.left);
1005 fail_on_test(sel.r.width != fmt.g_width());
1006 fail_on_test(sel.r.height != fmt.g_height());
1007
1008 // It is unlikely that an encoder supports composition,
1009 // so warn if this is detected. Should we get encoders
1010 // that can actually do this, then this test needs to
1011 // be refined.
1012 sel.target = V4L2_SEL_TGT_COMPOSE;
1013 if (!node->g_selection(sel))
1014 warn("The stateful encoder unexpectedly supports composition\n");
1015
1016 sel.type = fmt_cap.g_type();
1017 fail_on_test(!node->g_selection(sel));
1018 sel.target = V4L2_SEL_TGT_CROP;
1019 fail_on_test(!node->g_selection(sel));
1020
1021 node->s_fmt(fmt_out);
1022
1023 return 0;
1024 }
1025
1026 static int testGlobalFormat(struct node *node, int type)
1027 {
1028 struct v4l2_fmtdesc fdesc;
1029 struct v4l2_frmsizeenum fsize;
1030 struct v4l2_format fmt1, fmt2;
1031 struct v4l2_pix_format *p1 = &fmt1.fmt.pix;
1032 struct v4l2_pix_format *p2 = &fmt2.fmt.pix;
1033 struct v4l2_pix_format_mplane *mp1 = &fmt1.fmt.pix_mp;
1034 struct v4l2_pix_format_mplane *mp2 = &fmt2.fmt.pix_mp;
1035 struct v4l2_sdr_format *sdr1 = &fmt1.fmt.sdr;
1036 struct v4l2_sdr_format *sdr2 = &fmt2.fmt.sdr;
1037 __u32 pixfmt1, pixfmt2;
1038 __u32 w1 = 0, w2 = 0, h1 = 0, h2 = 0;
1039
1040 memset(&fmt1, 0, sizeof(fmt1));
1041 memset(&fmt2, 0, sizeof(fmt2));
1042 fmt1.type = fmt2.type = type;
1043 fdesc.index = 1;
1044 fdesc.type = type;
1045 memset(&fsize, 0, sizeof(fsize));
1046
1047 if (!doioctl(node, VIDIOC_ENUM_FMT, &fdesc)) {
1048 // We found at least two different formats.
1049 pixfmt2 = fdesc.pixelformat;
1050 fdesc.index = 0;
1051 doioctl(node, VIDIOC_ENUM_FMT, &fdesc);
1052 pixfmt1 = fdesc.pixelformat;
1053 } else {
1054 fdesc.index = 0;
1055 doioctl(node, VIDIOC_ENUM_FMT, &fdesc);
1056 fsize.pixel_format = fdesc.pixelformat;
1057 if (doioctl(node, VIDIOC_ENUM_FRAMESIZES, &fsize))
1058 return 0;
1059 pixfmt1 = pixfmt2 = fdesc.pixelformat;
1060 switch (fsize.type) {
1061 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1062 case V4L2_FRMSIZE_TYPE_STEPWISE:
1063 w1 = fsize.stepwise.min_width;
1064 w2 = fsize.stepwise.max_width;
1065 h1 = fsize.stepwise.min_height;
1066 h2 = fsize.stepwise.max_height;
1067 break;
1068 case V4L2_FRMSIZE_TYPE_DISCRETE:
1069 w1 = fsize.discrete.width;
1070 h1 = fsize.discrete.height;
1071 fsize.index = 1;
1072 doioctl(node, VIDIOC_ENUM_FRAMESIZES, &fsize);
1073 w2 = fsize.discrete.width;
1074 h2 = fsize.discrete.height;
1075 break;
1076 }
1077 }
1078 // Check if we have found different formats, otherwise this
1079 // test is pointless.
1080 // This test will also never succeed if we are using the libv4l2
1081 // wrapper.
1082 if (!node->g_direct() || (pixfmt1 == pixfmt2 && w1 == w2 && h1 == h2))
1083 return 0;
1084
1085 if (type == V4L2_BUF_TYPE_SDR_CAPTURE ||
1086 type == V4L2_BUF_TYPE_SDR_OUTPUT) {
1087 sdr1->pixelformat = pixfmt1;
1088 sdr2->pixelformat = pixfmt2;
1089 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1090 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1091 mp1->pixelformat = pixfmt1;
1092 mp1->width = w1;
1093 mp1->height = h1;
1094 mp2->pixelformat = pixfmt2;
1095 mp2->width = w2;
1096 mp2->height = h2;
1097 } else {
1098 p1->pixelformat = pixfmt1;
1099 p1->width = w1;
1100 p1->height = h1;
1101 p2->pixelformat = pixfmt2;
1102 p2->width = w2;
1103 p2->height = h2;
1104 }
1105 if (doioctl(node, VIDIOC_S_FMT, &fmt1)) {
1106 warn("Could not set fmt1\n");
1107 return 0;
1108 }
1109 if (doioctl(node->node2, VIDIOC_S_FMT, &fmt2)) {
1110 warn("Could not set fmt2\n");
1111 return 0;
1112 }
1113 if (type == V4L2_BUF_TYPE_SDR_CAPTURE ||
1114 type == V4L2_BUF_TYPE_SDR_OUTPUT) {
1115 if (sdr1->pixelformat == sdr2->pixelformat) {
1116 // This compliance test only succeeds if the two formats
1117 // are really different after S_FMT
1118 info("Could not perform global format test\n");
1119 return 0;
1120 }
1121 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1122 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1123 if (mp1->pixelformat == mp2->pixelformat &&
1124 mp1->width == mp2->width && mp1->height == mp2->height) {
1125 // This compliance test only succeeds if the two formats
1126 // are really different after S_FMT
1127 info("Could not perform global format test\n");
1128 return 0;
1129 }
1130 } else {
1131 if (p1->pixelformat == p2->pixelformat &&
1132 p1->width == p2->width && p1->height == p2->height) {
1133 // This compliance test only succeeds if the two formats
1134 // are really different after S_FMT
1135 info("Could not perform global format test\n");
1136 return 0;
1137 }
1138 }
1139 doioctl(node, VIDIOC_G_FMT, &fmt1);
1140 if (type == V4L2_BUF_TYPE_SDR_CAPTURE ||
1141 type == V4L2_BUF_TYPE_SDR_OUTPUT) {
1142 pixfmt1 = sdr1->pixelformat;
1143 pixfmt2 = sdr2->pixelformat;
1144 } else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1145 type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1146 pixfmt1 = mp1->pixelformat;
1147 w1 = mp1->width;
1148 h1 = mp1->height;
1149 pixfmt2 = mp2->pixelformat;
1150 w2 = mp2->width;
1151 h2 = mp2->height;
1152 } else {
1153 pixfmt1 = p1->pixelformat;
1154 w1 = p1->width;
1155 h1 = p1->height;
1156 pixfmt2 = p2->pixelformat;
1157 w2 = p2->width;
1158 h2 = p2->height;
1159 }
1160 if (pixfmt1 != pixfmt2 || w1 != w2 || h1 != h2)
1161 return fail("Global format mismatch: %08x(%s)/%ux%u vs %08x(%s)/%ux%u\n",
1162 pixfmt1, fcc2s(pixfmt1).c_str(), w1, h1,
1163 pixfmt2, fcc2s(pixfmt2).c_str(), w2, h2);
1164 info("Global format check succeeded for type %d\n", type);
1165 return 0;
1166 }
1167
1168 int testSetFormats(struct node *node)
1169 {
1170 struct v4l2_clip clip, clip_set;
1171 struct v4l2_format fmt, fmt_set;
1172 struct v4l2_format initial_fmts[V4L2_BUF_TYPE_LAST + 1];
1173 int type;
1174 int ret;
1175
1176 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1177 if (!(node->valid_buftypes & (1 << type)))
1178 continue;
1179
1180 createInvalidFmt(fmt, clip, type);
1181 doioctl(node, VIDIOC_G_FMT, &fmt);
1182
1183 initial_fmts[type] = fmt;
1184 createInvalidFmt(fmt_set, clip_set, type);
1185 ret = doioctl(node, VIDIOC_S_FMT, &fmt_set);
1186 if (ret == EINVAL) {
1187 __u32 pixelformat;
1188 bool is_mplane = false;
1189
1190 /* In case of failure obtain a valid pixelformat and insert
1191 * that in the next attempt to call TRY_FMT. */
1192 doioctl(node, VIDIOC_G_FMT, &fmt_set);
1193
1194 switch (type) {
1195 case V4L2_BUF_TYPE_META_CAPTURE:
1196 case V4L2_BUF_TYPE_META_OUTPUT:
1197 pixelformat = fmt_set.fmt.meta.dataformat;
1198 break;
1199 case V4L2_BUF_TYPE_SDR_CAPTURE:
1200 case V4L2_BUF_TYPE_SDR_OUTPUT:
1201 pixelformat = fmt_set.fmt.sdr.pixelformat;
1202 break;
1203 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1204 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1205 pixelformat = fmt_set.fmt.pix.pixelformat;
1206 break;
1207 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1209 pixelformat = fmt_set.fmt.pix_mp.pixelformat;
1210 is_mplane = true;
1211 break;
1212 case V4L2_BUF_TYPE_VBI_CAPTURE:
1213 case V4L2_BUF_TYPE_VBI_OUTPUT:
1214 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1215 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1216 continue;
1217 default:
1218 /* for other formats returning EINVAL is certainly wrong */
1219 return fail("TRY_FMT cannot handle an invalid format\n");
1220 }
1221 warn_once("S_FMT cannot handle an invalid pixelformat.\n");
1222 warn_once("This may or may not be a problem. For more information see:\n");
1223 warn_once("http://www.mail-archive.com/[email protected]/msg56550.html\n");
1224
1225 /* Now try again, but pass a valid pixelformat. */
1226 createInvalidFmt(fmt_set, clip_set, type);
1227 if (node->is_sdr)
1228 fmt_set.fmt.sdr.pixelformat = pixelformat;
1229 else if (node->is_meta)
1230 fmt_set.fmt.meta.dataformat = pixelformat;
1231 else if (is_mplane)
1232 fmt_set.fmt.pix_mp.pixelformat = pixelformat;
1233 else
1234 fmt_set.fmt.pix.pixelformat = pixelformat;
1235 ret = doioctl(node, VIDIOC_S_FMT, &fmt_set);
1236 if (ret == EINVAL)
1237 return fail("S_FMT cannot handle an invalid format\n");
1238 }
1239 ret = testFormatsType(node, ret, type, fmt_set, true);
1240 if (ret)
1241 return ret;
1242
1243 fmt_set = fmt;
1244 ret = doioctl(node, VIDIOC_S_FMT, &fmt_set);
1245 ret = testFormatsType(node, ret, type, fmt_set);
1246 if (ret)
1247 return ret;
1248 if (!matchFormats(fmt, fmt_set))
1249 return fail("%s: S_FMT(G_FMT) != G_FMT\n",
1250 buftype2s(type).c_str());
1251 }
1252 memset(&fmt, 0, sizeof(fmt));
1253 fmt.type = V4L2_BUF_TYPE_PRIVATE;
1254 ret = doioctl(node, VIDIOC_S_FMT, &fmt);
1255 if (ret != ENOTTY && ret != EINVAL)
1256 return fail("Buffer type PRIVATE allowed!\n");
1257 if (!node->valid_buftypes)
1258 return ENOTTY;
1259
1260 // Test if setting a format on one fh will set the format for all
1261 // filehandles.
1262 if (node->node2 == nullptr)
1263 return 0;
1264
1265 // m2m devices are special in that the format is often per-filehandle.
1266 // But colorspace information should be passed from output to capture,
1267 // so test that.
1268 if (node->is_m2m)
1269 return testM2MFormats(node);
1270
1271 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1272 switch (type) {
1273 case V4L2_BUF_TYPE_SDR_CAPTURE:
1274 case V4L2_BUF_TYPE_SDR_OUTPUT:
1275 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1276 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1277 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1278 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1279 if (!(node->valid_buftypes & (1 << type)))
1280 continue;
1281
1282 ret = testGlobalFormat(node, type);
1283 if (ret)
1284 return ret;
1285 break;
1286
1287 default:
1288 break;
1289 }
1290 }
1291
1292 /* Restore initial format */
1293 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1294 if (!(node->valid_buftypes & (1 << type)))
1295 continue;
1296
1297 doioctl(node, VIDIOC_S_FMT, &initial_fmts[type]);
1298 }
1299 return 0;
1300 }
1301
1302 static int testSlicedVBICapType(struct node *node, unsigned type)
1303 {
1304 struct v4l2_sliced_vbi_cap cap;
1305 bool sliced_type = (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
1306 type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT);
1307 __u32 service_set = 0;
1308 int ret;
1309
1310 memset(&cap, 0xff, sizeof(cap));
1311 memset(&cap.reserved, 0, sizeof(cap.reserved));
1312 cap.type = type;
1313 ret = doioctl(node, VIDIOC_G_SLICED_VBI_CAP, &cap);
1314 if (ret == ENOTTY || ret == EINVAL) {
1315 if (node->cur_io_caps & V4L2_IN_CAP_STD)
1316 fail_on_test(sliced_type && (node->g_caps() & buftype2cap[type]));
1317 return ret == ENOTTY ? ret : 0;
1318 }
1319 fail_on_test(ret);
1320 fail_on_test(check_0(cap.reserved, sizeof(cap.reserved)));
1321 fail_on_test(cap.type != type);
1322 fail_on_test(!sliced_type || !(node->g_caps() & buftype2cap[type]));
1323
1324 for (const auto &service_line : cap.service_lines)
1325 for (unsigned short i : service_line)
1326 service_set |= i;
1327 fail_on_test(cap.service_set != service_set);
1328 fail_on_test(cap.service_lines[0][0] || cap.service_lines[1][0]);
1329 return 0;
1330 }
1331
1332 int testSlicedVBICap(struct node *node)
1333 {
1334 int ret;
1335
1336 ret = testSlicedVBICapType(node, V4L2_BUF_TYPE_SLICED_VBI_CAPTURE);
1337 if (ret)
1338 return ret;
1339 ret = testSlicedVBICapType(node, V4L2_BUF_TYPE_SLICED_VBI_OUTPUT);
1340 if (ret)
1341 return ret;
1342 return testSlicedVBICapType(node, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1343 }
1344
1345 static int testParmStruct(struct node *node, struct v4l2_streamparm &parm)
1346 {
1347 bool is_stateful_enc = node->codec_mask & STATEFUL_ENCODER;
1348 const struct v4l2_captureparm *cap = &parm.parm.capture;
1349 const struct v4l2_outputparm *out = &parm.parm.output;
1350 int ret;
1351
1352 switch (parm.type) {
1353 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1354 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1355 ret = check_0(cap->reserved, sizeof(cap->reserved));
1356 if (ret)
1357 return fail("reserved not zeroed\n");
1358 fail_on_test(cap->readbuffers > VIDEO_MAX_FRAME);
1359 if (!(node->g_caps() & V4L2_CAP_READWRITE))
1360 fail_on_test(cap->readbuffers);
1361 else if (node->g_caps() & V4L2_CAP_STREAMING)
1362 fail_on_test(!cap->readbuffers);
1363 fail_on_test(cap->capability & ~V4L2_CAP_TIMEPERFRAME);
1364 fail_on_test(node->has_frmintervals && !cap->capability);
1365 fail_on_test(is_stateful_enc && !cap->capability);
1366 fail_on_test(cap->capturemode & ~V4L2_MODE_HIGHQUALITY);
1367 if (cap->capturemode & V4L2_MODE_HIGHQUALITY)
1368 warn("V4L2_MODE_HIGHQUALITY is poorly defined\n");
1369 fail_on_test(cap->extendedmode);
1370 if (cap->capability & V4L2_CAP_TIMEPERFRAME)
1371 fail_on_test(cap->timeperframe.numerator == 0 || cap->timeperframe.denominator == 0);
1372 break;
1373 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1374 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1375 ret = check_0(out->reserved, sizeof(out->reserved));
1376 if (ret)
1377 return fail("reserved not zeroed\n");
1378 fail_on_test(out->writebuffers > VIDEO_MAX_FRAME);
1379 if (!(node->g_caps() & V4L2_CAP_READWRITE))
1380 fail_on_test(out->writebuffers);
1381 else if (node->g_caps() & V4L2_CAP_STREAMING)
1382 fail_on_test(!out->writebuffers);
1383 fail_on_test(out->capability & ~V4L2_CAP_TIMEPERFRAME);
1384 fail_on_test(is_stateful_enc && !out->capability);
1385 fail_on_test(out->outputmode);
1386 fail_on_test(out->extendedmode);
1387 if (out->capability & V4L2_CAP_TIMEPERFRAME)
1388 fail_on_test(out->timeperframe.numerator == 0 || out->timeperframe.denominator == 0);
1389 break;
1390 default:
1391 break;
1392 }
1393 return 0;
1394 }
1395
1396 static int testParmType(struct node *node, unsigned type)
1397 {
1398 struct v4l2_streamparm parm;
1399 bool is_stateful_enc = node->codec_mask & STATEFUL_ENCODER;
1400 int ret;
1401
1402 memset(&parm, 0, sizeof(parm));
1403 parm.type = type;
1404 if (V4L2_TYPE_IS_OUTPUT(type))
1405 memset(parm.parm.output.reserved, 0xff,
1406 sizeof(parm.parm.output.reserved));
1407 else
1408 memset(parm.parm.capture.reserved, 0xff,
1409 sizeof(parm.parm.capture.reserved));
1410
1411 ret = doioctl(node, VIDIOC_G_PARM, &parm);
1412 switch (type) {
1413 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1414 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1415 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1416 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1417 if (node->g_caps() & buftype2cap[type]) {
1418 if (is_stateful_enc) {
1419 if (V4L2_TYPE_IS_OUTPUT(type))
1420 fail_on_test(ret && node->has_frmintervals);
1421 else if (node->has_enc_cap_frame_interval)
1422 fail_on_test(ret);
1423 else
1424 fail_on_test(!ret);
1425 } else {
1426 fail_on_test(ret && node->has_frmintervals);
1427 }
1428 }
1429 break;
1430 default:
1431 fail_on_test(ret == 0);
1432 memset(&parm, 0, sizeof(parm));
1433 parm.type = type;
1434 fail_on_test(!doioctl(node, VIDIOC_S_PARM, &parm));
1435 break;
1436 }
1437 if (ret == ENOTTY) {
1438 memset(&parm, 0, sizeof(parm));
1439 parm.type = type;
1440 fail_on_test(doioctl(node, VIDIOC_S_PARM, &parm) != ENOTTY);
1441 }
1442 if (ret == ENOTTY)
1443 return ret;
1444 // M2M devices don't support this, except for stateful encoders
1445 fail_on_test(node->is_m2m && !is_stateful_enc);
1446 if (ret == EINVAL)
1447 return ENOTTY;
1448 if (ret)
1449 return fail("expected EINVAL, but got %d when getting parms for buftype %d\n", ret, type);
1450 fail_on_test(parm.type != type);
1451 ret = testParmStruct(node, parm);
1452 if (ret)
1453 return ret;
1454
1455 memset(&parm, 0, sizeof(parm));
1456 parm.type = type;
1457 if (V4L2_TYPE_IS_OUTPUT(type))
1458 memset(parm.parm.output.reserved, 0xff,
1459 sizeof(parm.parm.output.reserved));
1460 else
1461 memset(parm.parm.capture.reserved, 0xff,
1462 sizeof(parm.parm.capture.reserved));
1463 ret = doioctl(node, VIDIOC_S_PARM, &parm);
1464
1465 __u32 cap;
1466
1467 if (V4L2_TYPE_IS_OUTPUT(type))
1468 cap = parm.parm.output.capability;
1469 else
1470 cap = parm.parm.capture.capability;
1471
1472 if (is_stateful_enc) {
1473 fail_on_test(ret && node->has_enc_cap_frame_interval);
1474 if (V4L2_TYPE_IS_OUTPUT(type))
1475 fail_on_test(ret);
1476 } else {
1477 fail_on_test(ret && node->has_frmintervals);
1478 }
1479
1480 /*
1481 * Stateful encoders can support S_PARM without ENUM_FRAMEINTERVALS.
1482 * being present. In that case the limits of the chosen codec apply.
1483 */
1484 if (!ret && (cap & V4L2_CAP_TIMEPERFRAME) && !node->has_frmintervals && !is_stateful_enc)
1485 warn("S_PARM is supported for buftype %d, but not for ENUM_FRAMEINTERVALS\n", type);
1486 if (ret == ENOTTY)
1487 return 0;
1488 /*
1489 * S_PARM(CAPTURE) is optional for stateful encoders, so EINVAL is a
1490 * valid error code in that case.
1491 */
1492 if (is_stateful_enc && V4L2_TYPE_IS_CAPTURE(type) && ret == -EINVAL)
1493 return 0;
1494 if (ret)
1495 return fail("got error %d when setting parms for buftype %d\n", ret, type);
1496 fail_on_test(parm.type != type);
1497 if (!(parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME))
1498 warn("S_PARM is supported but doesn't report V4L2_CAP_TIMEPERFRAME\n");
1499 ret = testParmStruct(node, parm);
1500 if (ret)
1501 return ret;
1502 if (V4L2_TYPE_IS_OUTPUT(type)) {
1503 parm.parm.output.timeperframe.numerator = 0;
1504 parm.parm.output.timeperframe.denominator = 1;
1505 } else {
1506 parm.parm.capture.timeperframe.numerator = 0;
1507 parm.parm.capture.timeperframe.denominator = 1;
1508 }
1509 fail_on_test(doioctl(node, VIDIOC_S_PARM, &parm));
1510 ret = testParmStruct(node, parm);
1511 if (ret)
1512 return ret;
1513 if (V4L2_TYPE_IS_OUTPUT(type)) {
1514 parm.parm.output.timeperframe.numerator = 1;
1515 parm.parm.output.timeperframe.denominator = 0;
1516 } else {
1517 parm.parm.capture.timeperframe.numerator = 1;
1518 parm.parm.capture.timeperframe.denominator = 0;
1519 }
1520 fail_on_test(doioctl(node, VIDIOC_S_PARM, &parm));
1521 return testParmStruct(node, parm);
1522 }
1523
1524 int testParm(struct node *node)
1525 {
1526 bool supported = false;
1527 int type;
1528 int ret;
1529
1530 for (type = 0; type <= V4L2_BUF_TYPE_LAST; type++) {
1531 ret = testParmType(node, type);
1532
1533 if (ret && ret != ENOTTY)
1534 return ret;
1535 if (!ret) {
1536 supported = true;
1537 if (V4L2_TYPE_IS_OUTPUT(type)) {
1538 if (!node->has_vid_out())
1539 return fail("video output caps not set, but G/S_PARM worked\n");
1540 } else if (!node->has_vid_cap()) {
1541 return fail("video capture caps not set, but G/S_PARM worked\n");
1542 }
1543 }
1544 }
1545
1546 ret = testParmType(node, V4L2_BUF_TYPE_PRIVATE);
1547 if (ret != ENOTTY && ret != EINVAL)
1548 return fail("Buffer type PRIVATE allowed!\n");
1549 return supported ? 0 : ENOTTY;
1550 }
1551
1552 static bool rect_is_inside(const struct v4l2_rect *r1, const struct v4l2_rect *r2)
1553 {
1554 return r1->left >= r2->left && r1->top >= r2->top &&
1555 r1->left + r1->width <= r2->left + r2->width &&
1556 r1->top + r1->height <= r2->top + r2->height;
1557 }
1558
1559 static int testBasicSelection(struct node *node, unsigned type, unsigned target)
1560 {
1561 struct v4l2_selection sel = {
1562 type,
1563 target,
1564 };
1565 int ret;
1566 v4l2_format fmt;
1567
1568 memset(sel.reserved, 0xff, sizeof(sel.reserved));
1569 ret = doioctl(node, VIDIOC_G_SELECTION, &sel);
1570 if (ret == ENOTTY || ret == EINVAL || ret == ENODATA) {
1571 fail_on_test(!doioctl(node, VIDIOC_S_SELECTION, &sel));
1572 return ENOTTY;
1573 }
1574 fail_on_test(ret);
1575 fail_on_test(check_0(sel.reserved, sizeof(sel.reserved)));
1576
1577 // set selection is not supported (for now) if there is more than one
1578 // discrete frame size.
1579 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1580 v4l_format_init(&fmt, node->is_planar ?
1581 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1582 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1583 else
1584 v4l_format_init(&fmt, node->is_planar ?
1585 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1586 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1587 fail_on_test(doioctl(node, VIDIOC_G_FMT, &fmt));
1588
1589 sel.type = 0xff;
1590 bool have_s_sel = doioctl(node, VIDIOC_S_SELECTION, &sel) != ENOTTY;
1591
1592 __u32 pixfmt = v4l_format_g_pixelformat(&fmt);
1593 if (node->frmsizes_count.find(pixfmt) != node->frmsizes_count.end() &&
1594 have_s_sel)
1595 fail_on_test(node->frmsizes_count[pixfmt] > 1);
1596
1597 // Check handling of invalid type.
1598 sel.type = 0xff;
1599 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel) != EINVAL);
1600 // Check handling of invalid target.
1601 sel.type = type;
1602 sel.target = 0xffff;
1603 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel) != EINVAL);
1604 return 0;
1605 }
1606
1607 static int testBasicCrop(struct node *node, unsigned type)
1608 {
1609 struct v4l2_selection sel_crop = {
1610 type,
1611 V4L2_SEL_TGT_CROP,
1612 };
1613 struct v4l2_selection sel_def;
1614 struct v4l2_selection sel_bounds;
1615
1616 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_crop));
1617 fail_on_test(!sel_crop.r.width || !sel_crop.r.height);
1618 sel_def = sel_crop;
1619 sel_def.target = V4L2_SEL_TGT_CROP_DEFAULT;
1620 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_def));
1621 fail_on_test(!sel_def.r.width || !sel_def.r.height);
1622 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1623 fail_on_test(sel_def.r.left || sel_def.r.top);
1624 sel_bounds = sel_crop;
1625 sel_bounds.target = V4L2_SEL_TGT_CROP_BOUNDS;
1626 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_bounds));
1627 fail_on_test(!sel_bounds.r.width || !sel_bounds.r.height);
1628 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1629 fail_on_test(sel_bounds.r.left || sel_bounds.r.top);
1630 fail_on_test(!rect_is_inside(&sel_crop.r, &sel_bounds.r));
1631 fail_on_test(!rect_is_inside(&sel_def.r, &sel_bounds.r));
1632
1633 sel_crop.type = type;
1634 sel_crop.target = V4L2_SEL_TGT_CROP;
1635 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_crop));
1636
1637 // Check handling of invalid type.
1638 sel_crop.type = 0xff;
1639 int s_sel_ret = doioctl(node, VIDIOC_S_SELECTION, &sel_crop);
1640 fail_on_test(s_sel_ret != EINVAL && s_sel_ret != ENOTTY);
1641 // Check handling of invalid target.
1642 sel_crop.type = type;
1643 sel_crop.target = 0xffff;
1644 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_crop) != s_sel_ret);
1645 // Check handling of read-only targets.
1646 sel_crop.target = V4L2_SEL_TGT_CROP_DEFAULT;
1647 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_crop) != s_sel_ret);
1648 sel_crop.target = V4L2_SEL_TGT_CROP_BOUNDS;
1649 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_crop) != s_sel_ret);
1650 return 0;
1651 }
1652
1653 static int testLegacyCrop(struct node *node)
1654 {
1655 struct v4l2_cropcap cap = {
1656 node->g_selection_type()
1657 };
1658 struct v4l2_crop crop = {
1659 node->g_selection_type()
1660 };
1661 struct v4l2_selection sel = {
1662 node->g_selection_type()
1663 };
1664
1665 sel.target = node->can_capture ? V4L2_SEL_TGT_CROP_DEFAULT :
1666 V4L2_SEL_TGT_COMPOSE_DEFAULT;
1667 /*
1668 * If either CROPCAP or G_CROP works, then G_SELECTION should
1669 * work as well. This was typically the case for drivers that
1670 * only implemented G_CROP and not G_SELECTION. However,
1671 * support for G_CROP was removed from the kernel and all drivers
1672 * now support G_SELECTION, so this should no longer happen.
1673 *
1674 * If neither CROPCAP nor G_CROP work, then G_SELECTION shouldn't
1675 * work either. If this fails, then this is almost certainly because
1676 * G_SELECTION doesn't support V4L2_SEL_TGT_CROP_DEFAULT and/or
1677 * V4L2_SEL_TGT_CROP_BOUNDS. CROPCAP requires both to be present.
1678 * For output devices this is of course V4L2_SEL_TGT_COMPOSE_DEFAULT
1679 * and/or V4L2_SEL_TGT_COMPOSE_BOUNDS.
1680 */
1681 if (!doioctl(node, VIDIOC_CROPCAP, &cap)) {
1682 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel));
1683
1684 // Checks for mplane types
1685 switch (cap.type) {
1686 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1687 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1688 break;
1689 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1690 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1691 break;
1692 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1693 cap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1694 break;
1695 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1696 cap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1697 break;
1698 }
1699 // Replace with fail_on_test once kernel is fixed for this.
1700 warn_on_test(doioctl(node, VIDIOC_CROPCAP, &cap));
1701 cap.type = 0xff;
1702 fail_on_test(doioctl(node, VIDIOC_CROPCAP, &cap) != EINVAL);
1703 } else {
1704 fail_on_test(!doioctl(node, VIDIOC_G_SELECTION, &sel));
1705 }
1706 sel.target = node->can_capture ? V4L2_SEL_TGT_CROP :
1707 V4L2_SEL_TGT_COMPOSE;
1708 if (!doioctl(node, VIDIOC_G_CROP, &crop))
1709 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel));
1710 else
1711 fail_on_test(!doioctl(node, VIDIOC_G_SELECTION, &sel));
1712 return 0;
1713 }
1714
1715 int testCropping(struct node *node)
1716 {
1717 int ret_cap, ret_out;
1718
1719 ret_cap = ENOTTY;
1720 ret_out = ENOTTY;
1721
1722 fail_on_test(testLegacyCrop(node));
1723 if (node->can_capture && node->is_video)
1724 ret_cap = testBasicSelection(node, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_SEL_TGT_CROP);
1725 if (ret_cap && ret_cap != ENOTTY)
1726 return ret_cap;
1727 if (node->can_output && node->is_video)
1728 ret_out = testBasicSelection(node, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_SEL_TGT_CROP);
1729 if (ret_out && ret_out != ENOTTY)
1730 return ret_out;
1731 if ((!node->can_capture && !node->can_output) || !node->is_video) {
1732 struct v4l2_selection sel = {
1733 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1734 V4L2_SEL_TGT_CROP
1735 };
1736
1737 if (node->can_output)
1738 sel.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1739 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel) != ENOTTY);
1740 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel) != ENOTTY);
1741 }
1742 if (ret_cap && ret_out)
1743 return ret_cap;
1744
1745 if (!ret_cap) {
1746 fail_on_test(IS_ENCODER(node));
1747 fail_on_test(testBasicCrop(node, V4L2_BUF_TYPE_VIDEO_CAPTURE));
1748 }
1749 if (!ret_out) {
1750 fail_on_test(IS_DECODER(node));
1751 fail_on_test(testBasicCrop(node, V4L2_BUF_TYPE_VIDEO_OUTPUT));
1752 }
1753
1754 return 0;
1755 }
1756
1757 static int testBasicCompose(struct node *node, unsigned type)
1758 {
1759 struct v4l2_selection sel_compose = {
1760 type,
1761 V4L2_SEL_TGT_COMPOSE,
1762 };
1763 struct v4l2_selection sel_def;
1764 struct v4l2_selection sel_bounds;
1765 struct v4l2_selection sel_padded;
1766 int ret;
1767
1768 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_compose));
1769 fail_on_test(!sel_compose.r.width || !sel_compose.r.height);
1770 sel_def = sel_compose;
1771 sel_def.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1772 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_def));
1773 fail_on_test(!sel_def.r.width || !sel_def.r.height);
1774 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1775 fail_on_test(sel_def.r.left || sel_def.r.top);
1776 sel_bounds = sel_compose;
1777 sel_bounds.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1778 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_bounds));
1779 fail_on_test(!sel_bounds.r.width || !sel_bounds.r.height);
1780 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1781 fail_on_test(sel_bounds.r.left || sel_bounds.r.top);
1782 fail_on_test(!rect_is_inside(&sel_compose.r, &sel_bounds.r));
1783 fail_on_test(!rect_is_inside(&sel_def.r, &sel_bounds.r));
1784 sel_padded = sel_compose;
1785 sel_padded.target = V4L2_SEL_TGT_COMPOSE_PADDED;
1786 ret = doioctl(node, VIDIOC_G_SELECTION, &sel_padded);
1787 fail_on_test(ret && ret != EINVAL);
1788 if (!ret) {
1789 fail_on_test(!rect_is_inside(&sel_padded.r, &sel_bounds.r));
1790 fail_on_test(!sel_padded.r.width || !sel_padded.r.height);
1791 }
1792
1793 sel_compose.type = type;
1794 sel_compose.target = V4L2_SEL_TGT_COMPOSE;
1795 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel_compose));
1796
1797 // Check handling of invalid type.
1798 sel_compose.type = 0xff;
1799 int s_sel_ret = doioctl(node, VIDIOC_S_SELECTION, &sel_compose);
1800 fail_on_test(s_sel_ret != EINVAL && s_sel_ret != ENOTTY);
1801 // Check handling of invalid target.
1802 sel_compose.type = type;
1803 sel_compose.target = 0xffff;
1804 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_compose) != s_sel_ret);
1805 // Check handling of read-only targets.
1806 sel_compose.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1807 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_compose) != s_sel_ret);
1808 sel_compose.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1809 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_compose) != s_sel_ret);
1810 sel_compose.target = V4L2_SEL_TGT_COMPOSE_PADDED;
1811 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel_compose) != s_sel_ret);
1812 return 0;
1813 }
1814
1815 int testComposing(struct node *node)
1816 {
1817 int ret_cap, ret_out;
1818
1819 ret_cap = ENOTTY;
1820 ret_out = ENOTTY;
1821
1822 if (node->can_capture && node->is_video)
1823 ret_cap = testBasicSelection(node, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_SEL_TGT_COMPOSE);
1824 if (node->can_output && node->is_video)
1825 ret_out = testBasicSelection(node, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_SEL_TGT_COMPOSE);
1826 if ((!node->can_capture && !node->can_output) || !node->is_video) {
1827 struct v4l2_selection sel = {
1828 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1829 V4L2_SEL_TGT_COMPOSE
1830 };
1831
1832 if (node->can_output)
1833 sel.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1834 fail_on_test(doioctl(node, VIDIOC_G_SELECTION, &sel) != ENOTTY);
1835 fail_on_test(doioctl(node, VIDIOC_S_SELECTION, &sel) != ENOTTY);
1836 }
1837 if (ret_cap && ret_out)
1838 return ret_cap;
1839
1840 if (!ret_cap) {
1841 fail_on_test(IS_ENCODER(node));
1842 fail_on_test(testBasicCompose(node, V4L2_BUF_TYPE_VIDEO_CAPTURE));
1843 }
1844 if (!ret_out) {
1845 fail_on_test(IS_DECODER(node));
1846 fail_on_test(testBasicCompose(node, V4L2_BUF_TYPE_VIDEO_OUTPUT));
1847 }
1848
1849 return 0;
1850 }
1851
1852 static int testBasicScaling(struct node *node, const struct v4l2_format &cur)
1853 {
1854 struct v4l2_selection sel_crop = {
1855 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1856 V4L2_SEL_TGT_CROP,
1857 0,
1858 { 1, 1, 0, 0 }
1859 };
1860 struct v4l2_selection sel_compose = {
1861 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1862 V4L2_SEL_TGT_COMPOSE,
1863 0,
1864 { 1, 1, 0, 0 }
1865 };
1866 unsigned compose_w = 0, compose_h = 0;
1867 unsigned crop_w = 0, crop_h = 0;
1868 struct v4l2_format fmt = cur;
1869 bool have_crop = false;
1870 bool crop_is_fmt = false;
1871 bool crop_is_const = false;
1872 bool have_compose = false;
1873 bool compose_is_fmt = false;
1874 bool compose_is_const = false;
1875 bool compose_is_crop = false;
1876 __u64 cookie;
1877 int ret;
1878
1879 if (node->can_output) {
1880 sel_crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1881 sel_compose.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1882 }
1883 doioctl(node, VIDIOC_S_SELECTION, &sel_crop);
1884 doioctl(node, VIDIOC_S_SELECTION, &sel_compose);
1885 v4l_format_s_width(&fmt, 1);
1886 v4l_format_s_height(&fmt, 1);
1887 v4l_format_s_field(&fmt, V4L2_FIELD_ANY);
1888 fail_on_test(doioctl(node, VIDIOC_S_FMT, &fmt));
1889 ret = doioctl(node, VIDIOC_G_SELECTION, &sel_crop);
1890 have_crop = ret == 0;
1891 if (ret == 0) {
1892 crop_is_fmt = sel_crop.r.width == v4l_format_g_width(&fmt) &&
1893 sel_crop.r.height == v4l_format_g_height(&fmt);
1894 crop_w = sel_crop.r.width;
1895 crop_h = sel_crop.r.height;
1896 }
1897 ret = doioctl(node, VIDIOC_G_SELECTION, &sel_compose);
1898 have_compose = ret == 0;
1899 if (ret == 0) {
1900 compose_is_fmt = sel_compose.r.width == v4l_format_g_width(&fmt) &&
1901 sel_compose.r.height == v4l_format_g_height(&fmt);
1902 compose_w = sel_compose.r.width;
1903 compose_h = sel_compose.r.height;
1904 }
1905 if (have_crop && have_compose)
1906 compose_is_crop = compose_w == crop_w &&
1907 compose_h == crop_h;
1908
1909 cookie = (static_cast<__u64>(v4l_format_g_pixelformat(&fmt)) << 32) |
1910 (v4l_format_g_width(&fmt) << 16) |
1911 v4l_format_g_height(&fmt);
1912 if (node->can_output) {
1913 if (node->frmsizes.find(cookie) == node->frmsizes.end() && !compose_is_fmt &&
1914 (v4l_format_g_width(&fmt) != v4l_format_g_width(&cur) ||
1915 v4l_format_g_height(&fmt) != v4l_format_g_height(&cur)))
1916 node->can_scale = true;
1917 } else {
1918 if (node->frmsizes.find(cookie) == node->frmsizes.end() && !crop_is_fmt &&
1919 (v4l_format_g_width(&fmt) != v4l_format_g_width(&cur) ||
1920 v4l_format_g_height(&fmt) != v4l_format_g_height(&cur)))
1921 node->can_scale = true;
1922 }
1923 sel_crop.r.width = sel_compose.r.width = 0x4000;
1924 sel_crop.r.height = sel_compose.r.height = 0x4000;
1925 v4l_format_s_width(&fmt, 0x4000);
1926 v4l_format_s_height(&fmt, 0x4000);
1927 v4l_format_s_field(&fmt, V4L2_FIELD_ANY);
1928 fail_on_test(doioctl(node, VIDIOC_S_FMT, &fmt));
1929 doioctl(node, VIDIOC_S_SELECTION, &sel_crop);
1930 doioctl(node, VIDIOC_S_SELECTION, &sel_compose);
1931 crop_is_fmt = false;
1932 ret = doioctl(node, VIDIOC_G_SELECTION, &sel_crop);
1933 if (ret == 0) {
1934 crop_is_fmt = sel_crop.r.width == v4l_format_g_width(&fmt) &&
1935 sel_crop.r.height == v4l_format_g_height(&fmt);
1936 crop_is_const = sel_crop.r.width == crop_w &&
1937 sel_crop.r.height == crop_h;
1938 }
1939 ret = doioctl(node, VIDIOC_G_SELECTION, &sel_compose);
1940 if (ret == 0) {
1941 compose_is_fmt = sel_compose.r.width == v4l_format_g_width(&fmt) &&
1942 sel_compose.r.height == v4l_format_g_height(&fmt);
1943 compose_is_const = sel_compose.r.width == compose_w &&
1944 sel_compose.r.height == compose_h;
1945 }
1946 if (compose_is_crop)
1947 compose_is_crop = sel_compose.r.width == sel_crop.r.width &&
1948 sel_compose.r.height == sel_crop.r.height;
1949 cookie = (static_cast<__u64>(v4l_format_g_pixelformat(&fmt)) << 32) |
1950 (v4l_format_g_width(&fmt) << 16) |
1951 v4l_format_g_height(&fmt);
1952 if (node->can_output) {
1953 if (node->frmsizes.find(cookie) == node->frmsizes.end() && !compose_is_fmt &&
1954 (v4l_format_g_width(&fmt) != v4l_format_g_width(&cur) ||
1955 v4l_format_g_height(&fmt) != v4l_format_g_height(&cur)))
1956 node->can_scale = true;
1957 if (crop_is_const || compose_is_crop)
1958 node->can_scale = false;
1959 } else {
1960 if (node->frmsizes.find(cookie) == node->frmsizes.end() && !crop_is_fmt &&
1961 (v4l_format_g_width(&fmt) != v4l_format_g_width(&cur) ||
1962 v4l_format_g_height(&fmt) != v4l_format_g_height(&cur)))
1963 node->can_scale = true;
1964 if (compose_is_const || compose_is_crop)
1965 node->can_scale = false;
1966 }
1967 fail_on_test(node->can_scale &&
1968 node->frmsizes_count[v4l_format_g_pixelformat(&cur)]);
1969 return 0;
1970 }
1971
1972 static int testM2MScaling(struct node *node)
1973 {
1974 struct v4l2_selection sel_compose = {
1975 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1976 V4L2_SEL_TGT_COMPOSE,
1977 0,
1978 { 1, 1, 0, 0 }
1979 };
1980 __u32 cap_type = node->g_type();
1981 __u32 out_type = v4l_type_invert(cap_type);
1982 cv4l_fmt out_fmt, cap_fmt, fmt;
1983
1984 fail_on_test(node->g_fmt(out_fmt, out_type));
1985 out_fmt.s_width(1);
1986 out_fmt.s_height(1);
1987 fail_on_test(node->s_fmt(out_fmt, out_type));
1988
1989 fail_on_test(node->g_fmt(cap_fmt, cap_type));
1990 cap_fmt.s_width(out_fmt.g_width());
1991 cap_fmt.s_height(out_fmt.g_height());
1992 fail_on_test(node->s_fmt(cap_fmt, cap_type));
1993 fmt = cap_fmt;
1994 fmt.s_width(0x4000);
1995 fmt.s_height(0x4000);
1996 fail_on_test(node->s_fmt(fmt, cap_type));
1997 if (!doioctl(node, VIDIOC_G_SELECTION, &sel_compose)) {
1998 sel_compose.r.width = fmt.g_width();
1999 sel_compose.r.height = fmt.g_height();
2000 doioctl(node, VIDIOC_S_SELECTION, &sel_compose);
2001 doioctl(node, VIDIOC_G_SELECTION, &sel_compose);
2002 if (sel_compose.r.width > cap_fmt.g_width() ||
2003 sel_compose.r.height > cap_fmt.g_height())
2004 node->can_scale = true;
2005 } else {
2006 if (fmt.g_width() > cap_fmt.g_width() ||
2007 fmt.g_height() > cap_fmt.g_height())
2008 node->can_scale = true;
2009 }
2010 return node->can_scale ? 0 : ENOTTY;
2011 }
2012
2013 int testScaling(struct node *node)
2014 {
2015 struct v4l2_format fmt;
2016
2017 if (!node->is_video)
2018 return ENOTTY;
2019 node->can_scale = false;
2020
2021 /*
2022 * Encoders do not have a scaler. Decoders might, but it is
2023 * very hard to detect this for any decoder but JPEG.
2024 */
2025 if (node->codec_mask && node->codec_mask != JPEG_DECODER)
2026 return ENOTTY;
2027
2028 if (node->is_m2m)
2029 return testM2MScaling(node);
2030
2031 if (node->can_capture) {
2032 v4l_format_init(&fmt, node->is_planar ?
2033 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
2034 V4L2_BUF_TYPE_VIDEO_CAPTURE);
2035 fail_on_test(doioctl(node, VIDIOC_G_FMT, &fmt));
2036 fail_on_test(testBasicScaling(node, fmt));
2037 fail_on_test(doioctl(node, VIDIOC_S_FMT, &fmt));
2038 }
2039 if (node->can_output) {
2040 v4l_format_init(&fmt, node->is_planar ?
2041 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
2042 V4L2_BUF_TYPE_VIDEO_OUTPUT);
2043 fail_on_test(doioctl(node, VIDIOC_G_FMT, &fmt));
2044 fail_on_test(testBasicScaling(node, fmt));
2045 fail_on_test(doioctl(node, VIDIOC_S_FMT, &fmt));
2046 }
2047 return node->can_scale ? 0 : ENOTTY;
2048 }