Debian Codegraph @

utils/v4l2-ctl/v4l2-ctl.cpp

raw
1 /*
2 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo dot com>
3
4 Cleanup and VBI and audio in/out options, introduction in v4l-dvb,
5 support for most new APIs since 2006.
6 Copyright (C) 2004, 2006, 2007 Hans Verkuil <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
21 */
22
23 #include <cctype>
24 #include <vector>
25
26 #include <dirent.h>
27 #include <getopt.h>
28 #include <sys/epoll.h>
29
30 #include <linux/media.h>
31
32 #include "v4l2-ctl.h"
33
34 #include <media-info.h>
35
36 #ifdef HAVE_SYS_KLOG_H
37 #include <sys/klog.h>
38 #endif
39
40 char options[OptLast];
41
42 static int app_result;
43 int verbose;
44
45 unsigned capabilities;
46 unsigned out_capabilities;
47 unsigned priv_magic;
48 unsigned out_priv_magic;
49 bool is_multiplanar;
50 __u32 vidcap_buftype;
51 __u32 vidout_buftype;
52
53 static struct option long_options[] = {
54 {"list-audio-inputs", no_argument, nullptr, OptListAudioInputs},
55 {"list-audio-outputs", no_argument, nullptr, OptListAudioOutputs},
56 {"all", no_argument, nullptr, OptAll},
57 {"device", required_argument, nullptr, OptSetDevice},
58 {"out-device", required_argument, nullptr, OptSetOutDevice},
59 {"export-device", required_argument, nullptr, OptSetExportDevice},
60 {"media-bus-info", required_argument, nullptr, OptMediaBusInfo},
61 {"get-fmt-video", no_argument, nullptr, OptGetVideoFormat},
62 {"set-fmt-video", required_argument, nullptr, OptSetVideoFormat},
63 {"try-fmt-video", required_argument, nullptr, OptTryVideoFormat},
64 {"get-fmt-video-out", no_argument, nullptr, OptGetVideoOutFormat},
65 {"set-fmt-video-out", required_argument, nullptr, OptSetVideoOutFormat},
66 {"try-fmt-video-out", required_argument, nullptr, OptTryVideoOutFormat},
67 {"get-routing", no_argument, 0, OptGetRouting},
68 {"set-routing", required_argument, 0, OptSetRouting},
69 {"try-routing", required_argument, 0, OptTryRouting},
70 {"help", no_argument, nullptr, OptHelp},
71 {"help-tuner", no_argument, nullptr, OptHelpTuner},
72 {"help-io", no_argument, nullptr, OptHelpIO},
73 {"help-stds", no_argument, nullptr, OptHelpStds},
74 {"help-vidcap", no_argument, nullptr, OptHelpVidCap},
75 {"help-vidout", no_argument, nullptr, OptHelpVidOut},
76 {"help-overlay", no_argument, nullptr, OptHelpOverlay},
77 {"help-vbi", no_argument, nullptr, OptHelpVbi},
78 {"help-sdr", no_argument, nullptr, OptHelpSdr},
79 {"help-meta", no_argument, nullptr, OptHelpMeta},
80 {"help-subdev", no_argument, nullptr, OptHelpSubDev},
81 {"help-selection", no_argument, nullptr, OptHelpSelection},
82 {"help-misc", no_argument, nullptr, OptHelpMisc},
83 {"help-streaming", no_argument, nullptr, OptHelpStreaming},
84 {"help-edid", no_argument, nullptr, OptHelpEdid},
85 {"help-all", no_argument, nullptr, OptHelpAll},
86 #ifndef NO_LIBV4L2
87 {"wrapper", no_argument, nullptr, OptUseWrapper},
88 #endif
89 {"concise", no_argument, nullptr, OptConcise},
90 {"get-output", no_argument, nullptr, OptGetOutput},
91 {"set-output", required_argument, nullptr, OptSetOutput},
92 {"list-outputs", no_argument, nullptr, OptListOutputs},
93 {"list-inputs", no_argument, nullptr, OptListInputs},
94 {"get-input", no_argument, nullptr, OptGetInput},
95 {"set-input", required_argument, nullptr, OptSetInput},
96 {"get-audio-input", no_argument, nullptr, OptGetAudioInput},
97 {"set-audio-input", required_argument, nullptr, OptSetAudioInput},
98 {"get-audio-output", no_argument, nullptr, OptGetAudioOutput},
99 {"set-audio-output", required_argument, nullptr, OptSetAudioOutput},
100 {"get-freq", no_argument, nullptr, OptGetFreq},
101 {"set-freq", required_argument, nullptr, OptSetFreq},
102 {"list-standards", no_argument, nullptr, OptListStandards},
103 {"list-formats", optional_argument, nullptr, OptListFormats},
104 {"list-formats-ext", optional_argument, nullptr, OptListFormatsExt},
105 {"list-fields", no_argument, nullptr, OptListFields},
106 {"list-framesizes", required_argument, nullptr, OptListFrameSizes},
107 {"list-frameintervals", required_argument, nullptr, OptListFrameIntervals},
108 {"list-formats-overlay", no_argument, nullptr, OptListOverlayFormats},
109 {"list-formats-sdr", no_argument, nullptr, OptListSdrFormats},
110 {"list-formats-sdr-out", no_argument, nullptr, OptListSdrOutFormats},
111 {"list-formats-out", optional_argument, nullptr, OptListOutFormats},
112 {"list-formats-out-ext", optional_argument, nullptr, OptListOutFormatsExt},
113 {"list-formats-meta", optional_argument, nullptr, OptListMetaFormats},
114 {"list-formats-meta-out", optional_argument, nullptr, OptListMetaOutFormats},
115 {"list-subdev-mbus-codes", optional_argument, nullptr, OptListSubDevMBusCodes},
116 {"list-subdev-framesizes", required_argument, nullptr, OptListSubDevFrameSizes},
117 {"list-subdev-frameintervals", required_argument, nullptr, OptListSubDevFrameIntervals},
118 {"list-fields-out", no_argument, nullptr, OptListOutFields},
119 {"clear-clips", no_argument, nullptr, OptClearClips},
120 {"clear-bitmap", no_argument, nullptr, OptClearBitmap},
121 {"add-clip", required_argument, nullptr, OptAddClip},
122 {"add-bitmap", required_argument, nullptr, OptAddBitmap},
123 {"find-fb", no_argument, nullptr, OptFindFb},
124 {"subset", required_argument, nullptr, OptSubset},
125 {"get-standard", no_argument, nullptr, OptGetStandard},
126 {"set-standard", required_argument, nullptr, OptSetStandard},
127 {"get-detected-standard", no_argument, nullptr, OptQueryStandard},
128 {"get-parm", no_argument, nullptr, OptGetParm},
129 {"set-parm", required_argument, nullptr, OptSetParm},
130 {"get-output-parm", no_argument, nullptr, OptGetOutputParm},
131 {"set-output-parm", required_argument, nullptr, OptSetOutputParm},
132 {"info", no_argument, nullptr, OptGetDriverInfo},
133 {"list-ctrls", no_argument, nullptr, OptListCtrls},
134 {"list-ctrls-menus", no_argument, nullptr, OptListCtrlsMenus},
135 {"set-ctrl", required_argument, nullptr, OptSetCtrl},
136 {"get-ctrl", required_argument, nullptr, OptGetCtrl},
137 {"get-tuner", no_argument, nullptr, OptGetTuner},
138 {"set-tuner", required_argument, nullptr, OptSetTuner},
139 {"list-freq-bands", no_argument, nullptr, OptListFreqBands},
140 {"silent", no_argument, nullptr, OptSilent},
141 {"verbose", no_argument, nullptr, OptVerbose},
142 {"log-status", no_argument, nullptr, OptLogStatus},
143 {"get-fmt-overlay", no_argument, nullptr, OptGetOverlayFormat},
144 {"set-fmt-overlay", optional_argument, nullptr, OptSetOverlayFormat},
145 {"try-fmt-overlay", optional_argument, nullptr, OptTryOverlayFormat},
146 {"get-fmt-sliced-vbi", no_argument, nullptr, OptGetSlicedVbiFormat},
147 {"set-fmt-sliced-vbi", required_argument, nullptr, OptSetSlicedVbiFormat},
148 {"try-fmt-sliced-vbi", required_argument, nullptr, OptTrySlicedVbiFormat},
149 {"get-fmt-sliced-vbi-out", no_argument, nullptr, OptGetSlicedVbiOutFormat},
150 {"set-fmt-sliced-vbi-out", required_argument, nullptr, OptSetSlicedVbiOutFormat},
151 {"try-fmt-sliced-vbi-out", required_argument, nullptr, OptTrySlicedVbiOutFormat},
152 {"get-fmt-vbi", no_argument, nullptr, OptGetVbiFormat},
153 {"set-fmt-vbi", required_argument, nullptr, OptSetVbiFormat},
154 {"try-fmt-vbi", required_argument, nullptr, OptTryVbiFormat},
155 {"get-fmt-vbi-out", no_argument, nullptr, OptGetVbiOutFormat},
156 {"set-fmt-vbi-out", required_argument, nullptr, OptSetVbiOutFormat},
157 {"try-fmt-vbi-out", required_argument, nullptr, OptTryVbiOutFormat},
158 {"get-fmt-sdr", no_argument, nullptr, OptGetSdrFormat},
159 {"set-fmt-sdr", required_argument, nullptr, OptSetSdrFormat},
160 {"try-fmt-sdr", required_argument, nullptr, OptTrySdrFormat},
161 {"get-fmt-sdr-out", no_argument, nullptr, OptGetSdrOutFormat},
162 {"set-fmt-sdr-out", required_argument, nullptr, OptSetSdrOutFormat},
163 {"try-fmt-sdr-out", required_argument, nullptr, OptTrySdrOutFormat},
164 {"get-fmt-meta", no_argument, nullptr, OptGetMetaFormat},
165 {"set-fmt-meta", required_argument, nullptr, OptSetMetaFormat},
166 {"try-fmt-meta", required_argument, nullptr, OptTryMetaFormat},
167 {"get-fmt-meta-out", no_argument, nullptr, OptGetMetaOutFormat},
168 {"set-fmt-meta-out", required_argument, nullptr, OptSetMetaOutFormat},
169 {"try-fmt-meta-out", required_argument, nullptr, OptTryMetaOutFormat},
170 {"get-subdev-fmt", optional_argument, nullptr, OptGetSubDevFormat},
171 {"set-subdev-fmt", required_argument, nullptr, OptSetSubDevFormat},
172 {"try-subdev-fmt", required_argument, nullptr, OptTrySubDevFormat},
173 {"get-sliced-vbi-cap", no_argument, nullptr, OptGetSlicedVbiCap},
174 {"get-sliced-vbi-out-cap", no_argument, nullptr, OptGetSlicedVbiOutCap},
175 {"get-fbuf", no_argument, nullptr, OptGetFBuf},
176 {"set-fbuf", required_argument, nullptr, OptSetFBuf},
177 {"get-cropcap", no_argument, nullptr, OptGetCropCap},
178 {"get-crop", no_argument, nullptr, OptGetCrop},
179 {"set-crop", required_argument, nullptr, OptSetCrop},
180 {"get-cropcap-output", no_argument, nullptr, OptGetOutputCropCap},
181 {"get-crop-output", no_argument, nullptr, OptGetOutputCrop},
182 {"set-crop-output", required_argument, nullptr, OptSetOutputCrop},
183 {"get-cropcap-overlay", no_argument, nullptr, OptGetOverlayCropCap},
184 {"get-crop-overlay", no_argument, nullptr, OptGetOverlayCrop},
185 {"set-crop-overlay", required_argument, nullptr, OptSetOverlayCrop},
186 {"get-cropcap-output-overlay", no_argument, nullptr, OptGetOutputOverlayCropCap},
187 {"get-crop-output-overlay", no_argument, nullptr, OptGetOutputOverlayCrop},
188 {"set-crop-output-overlay", required_argument, nullptr, OptSetOutputOverlayCrop},
189 {"get-selection", required_argument, nullptr, OptGetSelection},
190 {"set-selection", required_argument, nullptr, OptSetSelection},
191 {"get-selection-output", required_argument, nullptr, OptGetOutputSelection},
192 {"set-selection-output", required_argument, nullptr, OptSetOutputSelection},
193 {"get-subdev-selection", required_argument, nullptr, OptGetSubDevSelection},
194 {"set-subdev-selection", required_argument, nullptr, OptSetSubDevSelection},
195 {"try-subdev-selection", required_argument, nullptr, OptTrySubDevSelection},
196 {"get-subdev-fps", optional_argument, nullptr, OptGetSubDevFPS},
197 {"set-subdev-fps", required_argument, nullptr, OptSetSubDevFPS},
198 {"get-jpeg-comp", no_argument, nullptr, OptGetJpegComp},
199 {"set-jpeg-comp", required_argument, nullptr, OptSetJpegComp},
200 {"get-modulator", no_argument, nullptr, OptGetModulator},
201 {"set-modulator", required_argument, nullptr, OptSetModulator},
202 {"get-priority", no_argument, nullptr, OptGetPriority},
203 {"set-priority", required_argument, nullptr, OptSetPriority},
204 {"wait-for-event", required_argument, nullptr, OptWaitForEvent},
205 {"poll-for-event", required_argument, nullptr, OptPollForEvent},
206 {"epoll-for-event", required_argument, nullptr, OptEPollForEvent},
207 {"overlay", required_argument, nullptr, OptOverlay},
208 {"sleep", required_argument, nullptr, OptSleep},
209 {"list-devices", no_argument, nullptr, OptListDevices},
210 {"list-devices-input", required_argument, nullptr, OptListDevicesInput},
211 {"list-devices-output", required_argument, nullptr, OptListDevicesOutput},
212 {"list-dv-timings", optional_argument, nullptr, OptListDvTimings},
213 {"query-dv-timings", no_argument, nullptr, OptQueryDvTimings},
214 {"get-dv-timings", no_argument, nullptr, OptGetDvTimings},
215 {"set-dv-bt-timings", required_argument, nullptr, OptSetDvBtTimings},
216 {"get-dv-timings-cap", optional_argument, nullptr, OptGetDvTimingsCap},
217 {"freq-seek", required_argument, nullptr, OptFreqSeek},
218 {"encoder-cmd", required_argument, nullptr, OptEncoderCmd},
219 {"try-encoder-cmd", required_argument, nullptr, OptTryEncoderCmd},
220 {"decoder-cmd", required_argument, nullptr, OptDecoderCmd},
221 {"try-decoder-cmd", required_argument, nullptr, OptTryDecoderCmd},
222 {"set-edid", required_argument, nullptr, OptSetEdid},
223 {"clear-edid", optional_argument, nullptr, OptClearEdid},
224 {"get-edid", optional_argument, nullptr, OptGetEdid},
225 {"info-edid", optional_argument, nullptr, OptInfoEdid},
226 {"show-edid", required_argument, nullptr, OptShowEdid},
227 {"keep-edid-checksums", no_argument, nullptr, OptKeepEdidChecksums},
228 {"tuner-index", required_argument, nullptr, OptTunerIndex},
229 {"list-buffers", no_argument, nullptr, OptListBuffers},
230 {"list-buffers-out", no_argument, nullptr, OptListBuffersOut},
231 {"list-buffers-vbi", no_argument, nullptr, OptListBuffersVbi},
232 {"list-buffers-sliced-vbi", no_argument, nullptr, OptListBuffersSlicedVbi},
233 {"list-buffers-vbi-out", no_argument, nullptr, OptListBuffersVbiOut},
234 {"list-buffers-sliced-vbi-out", no_argument, nullptr, OptListBuffersSlicedVbiOut},
235 {"list-buffers-sdr", no_argument, nullptr, OptListBuffersSdr},
236 {"list-buffers-sdr-out", no_argument, nullptr, OptListBuffersSdrOut},
237 {"list-buffers-meta", no_argument, nullptr, OptListBuffersMeta},
238 {"list-buffers-meta-out", no_argument, nullptr, OptListBuffersMetaOut},
239 {"stream-count", required_argument, nullptr, OptStreamCount},
240 {"stream-skip", required_argument, nullptr, OptStreamSkip},
241 {"stream-loop", no_argument, nullptr, OptStreamLoop},
242 {"stream-sleep", required_argument, nullptr, OptStreamSleep},
243 {"stream-poll", no_argument, nullptr, OptStreamPoll},
244 {"stream-no-query", no_argument, nullptr, OptStreamNoQuery},
245 #ifndef NO_STREAM_TO
246 {"stream-to", required_argument, nullptr, OptStreamTo},
247 {"stream-to-hdr", required_argument, nullptr, OptStreamToHdr},
248 {"stream-lossless", no_argument, nullptr, OptStreamLossless},
249 {"stream-to-host", required_argument, nullptr, OptStreamToHost},
250 #endif
251 {"stream-buf-caps", no_argument, nullptr, OptStreamBufCaps},
252 {"stream-show-delta-now", no_argument, nullptr, OptStreamShowDeltaNow},
253 {"stream-mmap", optional_argument, nullptr, OptStreamMmap},
254 {"stream-user", optional_argument, nullptr, OptStreamUser},
255 {"stream-dmabuf", no_argument, nullptr, OptStreamDmaBuf},
256 {"stream-from", required_argument, nullptr, OptStreamFrom},
257 {"stream-from-hdr", required_argument, nullptr, OptStreamFromHdr},
258 {"stream-from-host", required_argument, nullptr, OptStreamFromHost},
259 {"stream-out-pattern", required_argument, nullptr, OptStreamOutPattern},
260 {"stream-out-square", no_argument, nullptr, OptStreamOutSquare},
261 {"stream-out-border", no_argument, nullptr, OptStreamOutBorder},
262 {"stream-out-sav", no_argument, nullptr, OptStreamOutInsertSAV},
263 {"stream-out-eav", no_argument, nullptr, OptStreamOutInsertEAV},
264 {"stream-out-pixel-aspect", required_argument, nullptr, OptStreamOutPixelAspect},
265 {"stream-out-video-aspect", required_argument, nullptr, OptStreamOutVideoAspect},
266 {"stream-out-alpha", required_argument, nullptr, OptStreamOutAlphaComponent},
267 {"stream-out-alpha-red-only", no_argument, nullptr, OptStreamOutAlphaRedOnly},
268 {"stream-out-rgb-lim-range", required_argument, nullptr, OptStreamOutRGBLimitedRange},
269 {"stream-out-hor-speed", required_argument, nullptr, OptStreamOutHorSpeed},
270 {"stream-out-vert-speed", required_argument, nullptr, OptStreamOutVertSpeed},
271 {"stream-out-perc-fill", required_argument, nullptr, OptStreamOutPercFill},
272 {"stream-out-buf-caps", no_argument, nullptr, OptStreamOutBufCaps},
273 {"stream-out-mmap", optional_argument, nullptr, OptStreamOutMmap},
274 {"stream-out-user", optional_argument, nullptr, OptStreamOutUser},
275 {"stream-out-dmabuf", no_argument, nullptr, OptStreamOutDmaBuf},
276 {"list-patterns", no_argument, nullptr, OptListPatterns},
277 {"version", no_argument, nullptr, OptVersion},
278 {nullptr, 0, nullptr, 0}
279 };
280
281 static void usage_all()
282 {
283 common_usage();
284 tuner_usage();
285 io_usage();
286 stds_usage();
287 vidcap_usage();
288 vidout_usage();
289 overlay_usage();
290 vbi_usage();
291 sdr_usage();
292 meta_usage();
293 subdev_usage();
294 selection_usage();
295 misc_usage();
296 streaming_usage();
297 edid_usage();
298 }
299
300 static void print_version()
301 {
302 #define STR(x) #x
303 #define STRING(x) STR(x)
304 printf("v4l2-ctl %s%s\n", PACKAGE_VERSION, STRING(GIT_COMMIT_CNT));
305 if (strlen(STRING(GIT_SHA)))
306 printf("v4l2-ctl SHA: %s %s\n",
307 STRING(GIT_SHA), STRING(GIT_COMMIT_DATE));
308 }
309
310 int test_ioctl(int fd, unsigned long cmd, void *arg)
311 {
312 return options[OptUseWrapper] ? v4l2_ioctl(fd, cmd, arg) : ioctl(fd, cmd, arg);
313 }
314
315 int doioctl_name(int fd, unsigned long int request, void *parm, const char *name)
316 {
317 int retval = test_ioctl(fd, request, parm);
318
319 if (retval < 0)
320 app_result = -1;
321 if (options[OptSilent]) return retval;
322 if (retval < 0)
323 printf("%s: failed: %s\n", name, strerror(errno));
324 else if (verbose)
325 printf("%s: ok\n", name);
326
327 return retval;
328 }
329
330 /*
331 * Any pixelformat that is not a YUV format is assumed to be
332 * RGB or HSV.
333 */
334 static bool is_rgb_or_hsv(__u32 pixelformat)
335 {
336 switch (pixelformat) {
337 case V4L2_PIX_FMT_UV8:
338 case V4L2_PIX_FMT_YVU410:
339 case V4L2_PIX_FMT_YVU420:
340 case V4L2_PIX_FMT_YUYV:
341 case V4L2_PIX_FMT_YYUV:
342 case V4L2_PIX_FMT_YVYU:
343 case V4L2_PIX_FMT_UYVY:
344 case V4L2_PIX_FMT_VYUY:
345 case V4L2_PIX_FMT_YUV422P:
346 case V4L2_PIX_FMT_YUV411P:
347 case V4L2_PIX_FMT_Y41P:
348 case V4L2_PIX_FMT_YUV444:
349 case V4L2_PIX_FMT_YUV555:
350 case V4L2_PIX_FMT_YUV565:
351 case V4L2_PIX_FMT_YUV32:
352 case V4L2_PIX_FMT_AYUV32:
353 case V4L2_PIX_FMT_XYUV32:
354 case V4L2_PIX_FMT_VUYA32:
355 case V4L2_PIX_FMT_VUYX32:
356 case V4L2_PIX_FMT_YUVA32:
357 case V4L2_PIX_FMT_YUVX32:
358 case V4L2_PIX_FMT_YUV410:
359 case V4L2_PIX_FMT_YUV420:
360 case V4L2_PIX_FMT_HI240:
361 case V4L2_PIX_FMT_NV12_16L16:
362 case V4L2_PIX_FMT_M420:
363 case V4L2_PIX_FMT_NV12:
364 case V4L2_PIX_FMT_NV21:
365 case V4L2_PIX_FMT_NV16:
366 case V4L2_PIX_FMT_NV61:
367 case V4L2_PIX_FMT_NV24:
368 case V4L2_PIX_FMT_NV42:
369 case V4L2_PIX_FMT_NV12M:
370 case V4L2_PIX_FMT_NV21M:
371 case V4L2_PIX_FMT_NV16M:
372 case V4L2_PIX_FMT_NV61M:
373 case V4L2_PIX_FMT_NV12MT:
374 case V4L2_PIX_FMT_NV12MT_16X16:
375 case V4L2_PIX_FMT_YUV420M:
376 case V4L2_PIX_FMT_YVU420M:
377 case V4L2_PIX_FMT_YUV422M:
378 case V4L2_PIX_FMT_YVU422M:
379 case V4L2_PIX_FMT_YUV444M:
380 case V4L2_PIX_FMT_YVU444M:
381 case V4L2_PIX_FMT_SN9C20X_I420:
382 case V4L2_PIX_FMT_SPCA501:
383 case V4L2_PIX_FMT_SPCA505:
384 case V4L2_PIX_FMT_SPCA508:
385 case V4L2_PIX_FMT_CIT_YYVYUY:
386 case V4L2_PIX_FMT_KONICA420:
387 return false;
388 default:
389 return true;
390 }
391 }
392
393 static std::string printfmtname(int fd, __u32 type, __u32 pixfmt)
394 {
395 struct v4l2_fmtdesc fmt = {};
396 std::string s(" (");
397
398 fmt.index = 0;
399 fmt.type = type;
400 while (test_ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
401 if (fmt.pixelformat == pixfmt)
402 return s + reinterpret_cast<const char *>(fmt.description) + ")";
403 fmt.index++;
404 }
405 return "";
406 }
407
408 void printfmt(int fd, const struct v4l2_format &vfmt)
409 {
410 __u32 colsp = vfmt.fmt.pix.colorspace;
411 __u32 ycbcr_enc = vfmt.fmt.pix.ycbcr_enc;
412
413 printf("Format %s:\n", buftype2s(vfmt.type).c_str());
414
415 switch (vfmt.type) {
416 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
417 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
418 printf("\tWidth/Height : %u/%u\n", vfmt.fmt.pix.width, vfmt.fmt.pix.height);
419 printf("\tPixel Format : '%s'%s\n", fcc2s(vfmt.fmt.pix.pixelformat).c_str(),
420 printfmtname(fd, vfmt.type, vfmt.fmt.pix.pixelformat).c_str());
421 printf("\tField : %s\n", field2s(vfmt.fmt.pix.field).c_str());
422 printf("\tBytes per Line : %u\n", vfmt.fmt.pix.bytesperline);
423 printf("\tSize Image : %u\n", vfmt.fmt.pix.sizeimage);
424 printf("\tColorspace : %s\n", colorspace2s(colsp).c_str());
425 printf("\tTransfer Function : %s", xfer_func2s(vfmt.fmt.pix.xfer_func).c_str());
426 if (vfmt.fmt.pix.xfer_func == V4L2_XFER_FUNC_DEFAULT)
427 printf(" (maps to %s)",
428 xfer_func2s(V4L2_MAP_XFER_FUNC_DEFAULT(colsp)).c_str());
429 printf("\n");
430 printf("\tYCbCr/HSV Encoding: %s", ycbcr_enc2s(ycbcr_enc).c_str());
431 if (ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
432 ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(colsp);
433 printf(" (maps to %s)", ycbcr_enc2s(ycbcr_enc).c_str());
434 }
435 printf("\n");
436 printf("\tQuantization : %s", quantization2s(vfmt.fmt.pix.quantization).c_str());
437 if (vfmt.fmt.pix.quantization == V4L2_QUANTIZATION_DEFAULT)
438 printf(" (maps to %s)",
439 quantization2s(V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb_or_hsv(vfmt.fmt.pix.pixelformat),
440 colsp, ycbcr_enc)).c_str());
441 printf("\n");
442 if (vfmt.fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
443 printf("\tFlags : %s\n", pixflags2s(vfmt.fmt.pix.flags).c_str());
444 break;
445 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
446 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
447 printf("\tWidth/Height : %u/%u\n", vfmt.fmt.pix_mp.width, vfmt.fmt.pix_mp.height);
448 printf("\tPixel Format : '%s'%s\n", fcc2s(vfmt.fmt.pix_mp.pixelformat).c_str(),
449 printfmtname(fd, vfmt.type, vfmt.fmt.pix_mp.pixelformat).c_str());
450 printf("\tField : %s\n", field2s(vfmt.fmt.pix_mp.field).c_str());
451 printf("\tNumber of planes : %u\n", vfmt.fmt.pix_mp.num_planes);
452 printf("\tFlags : %s\n", pixflags2s(vfmt.fmt.pix_mp.flags).c_str());
453 printf("\tColorspace : %s\n", colorspace2s(vfmt.fmt.pix_mp.colorspace).c_str());
454 printf("\tTransfer Function : %s\n", xfer_func2s(vfmt.fmt.pix_mp.xfer_func).c_str());
455 printf("\tYCbCr/HSV Encoding: %s\n", ycbcr_enc2s(vfmt.fmt.pix_mp.ycbcr_enc).c_str());
456 printf("\tQuantization : %s\n", quantization2s(vfmt.fmt.pix_mp.quantization).c_str());
457 for (int i = 0; i < vfmt.fmt.pix_mp.num_planes && i < VIDEO_MAX_PLANES; i++) {
458 printf("\tPlane %d :\n", i);
459 printf("\t Bytes per Line : %u\n", vfmt.fmt.pix_mp.plane_fmt[i].bytesperline);
460 printf("\t Size Image : %u\n", vfmt.fmt.pix_mp.plane_fmt[i].sizeimage);
461 }
462 break;
463 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
464 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
465 printf("\tLeft/Top : %d/%d\n",
466 vfmt.fmt.win.w.left, vfmt.fmt.win.w.top);
467 printf("\tWidth/Height: %d/%d\n",
468 vfmt.fmt.win.w.width, vfmt.fmt.win.w.height);
469 printf("\tField : %s\n", field2s(vfmt.fmt.win.field).c_str());
470 printf("\tChroma Key : 0x%08x\n", vfmt.fmt.win.chromakey);
471 printf("\tGlobal Alpha: 0x%02x\n", vfmt.fmt.win.global_alpha);
472 printf("\tClip Count : %u\n", vfmt.fmt.win.clipcount);
473 if (vfmt.fmt.win.clips)
474 for (unsigned i = 0; i < vfmt.fmt.win.clipcount; i++) {
475 struct v4l2_rect &r = vfmt.fmt.win.clips[i].c;
476
477 printf("\t\tClip %2d: (%d,%d)/%ux%u\n", i,
478 r.left, r.top, r.width, r.height);
479 }
480 printf("\tClip Bitmap : %s", vfmt.fmt.win.bitmap ? "Yes, " : "No\n");
481 if (vfmt.fmt.win.bitmap) {
482 auto bitmap = static_cast<unsigned char *>(vfmt.fmt.win.bitmap);
483 unsigned stride = (vfmt.fmt.win.w.width + 7) / 8;
484 unsigned cnt = 0;
485
486 for (unsigned y = 0; y < vfmt.fmt.win.w.height; y++)
487 for (unsigned x = 0; x < vfmt.fmt.win.w.width; x++)
488 if (bitmap[y * stride + x / 8] & (1 << (x & 7)))
489 cnt++;
490 printf("%u bits of %u are set\n", cnt,
491 vfmt.fmt.win.w.width * vfmt.fmt.win.w.height);
492 }
493 break;
494 case V4L2_BUF_TYPE_VBI_CAPTURE:
495 case V4L2_BUF_TYPE_VBI_OUTPUT:
496 printf("\tSampling Rate : %u Hz\n", vfmt.fmt.vbi.sampling_rate);
497 printf("\tOffset : %u samples (%g secs after leading edge)\n",
498 vfmt.fmt.vbi.offset,
499 static_cast<double>(vfmt.fmt.vbi.offset) / static_cast<double>(vfmt.fmt.vbi.sampling_rate));
500 printf("\tSamples per Line: %u\n", vfmt.fmt.vbi.samples_per_line);
501 printf("\tSample Format : '%s'\n", fcc2s(vfmt.fmt.vbi.sample_format).c_str());
502 printf("\tStart 1st Field : %u\n", vfmt.fmt.vbi.start[0]);
503 printf("\tCount 1st Field : %u\n", vfmt.fmt.vbi.count[0]);
504 printf("\tStart 2nd Field : %u\n", vfmt.fmt.vbi.start[1]);
505 printf("\tCount 2nd Field : %u\n", vfmt.fmt.vbi.count[1]);
506 if (vfmt.fmt.vbi.flags)
507 printf("\tFlags : %s\n", vbiflags2s(vfmt.fmt.vbi.flags).c_str());
508 break;
509 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
510 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
511 printf("\tService Set : %s\n",
512 service2s(vfmt.fmt.sliced.service_set).c_str());
513 for (int i = 0; i < 24; i++) {
514 printf("\tService Line %2d: %8s / %-8s\n", i,
515 service2s(vfmt.fmt.sliced.service_lines[0][i]).c_str(),
516 service2s(vfmt.fmt.sliced.service_lines[1][i]).c_str());
517 }
518 printf("\tI/O Size : %u\n", vfmt.fmt.sliced.io_size);
519 break;
520 case V4L2_BUF_TYPE_SDR_CAPTURE:
521 case V4L2_BUF_TYPE_SDR_OUTPUT:
522 printf("\tSample Format : '%s'%s\n", fcc2s(vfmt.fmt.sdr.pixelformat).c_str(),
523 printfmtname(fd, vfmt.type, vfmt.fmt.sdr.pixelformat).c_str());
524 printf("\tBuffer Size : %u\n", vfmt.fmt.sdr.buffersize);
525 break;
526 case V4L2_BUF_TYPE_META_CAPTURE:
527 case V4L2_BUF_TYPE_META_OUTPUT:
528 printf("\tSample Format : '%s'%s\n", fcc2s(vfmt.fmt.meta.dataformat).c_str(),
529 printfmtname(fd, vfmt.type, vfmt.fmt.meta.dataformat).c_str());
530 printf("\tBuffer Size : %u\n", vfmt.fmt.meta.buffersize);
531 break;
532 }
533 }
534
535 static std::string frmtype2s(unsigned type)
536 {
537 static constexpr const char *types[] = {
538 "Unknown",
539 "Discrete",
540 "Continuous",
541 "Stepwise"
542 };
543
544 if (type > 3)
545 type = 0;
546 return types[type];
547 }
548
549 static std::string fract2sec(const struct v4l2_fract &f)
550 {
551 char buf[100];
552
553 sprintf(buf, "%.3f", (1.0 * f.numerator) / f.denominator);
554 return buf;
555 }
556
557 static std::string fract2fps(const struct v4l2_fract &f)
558 {
559 char buf[100];
560
561 sprintf(buf, "%.3f", (1.0 * f.denominator) / f.numerator);
562 return buf;
563 }
564
565 void print_frmsize(const struct v4l2_frmsizeenum &frmsize, const char *prefix)
566 {
567 printf("%s\tSize: %s ", prefix, frmtype2s(frmsize.type).c_str());
568 if (frmsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
569 printf("%ux%u", frmsize.discrete.width, frmsize.discrete.height);
570 } else if (frmsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
571 printf("%ux%u - %ux%u",
572 frmsize.stepwise.min_width,
573 frmsize.stepwise.min_height,
574 frmsize.stepwise.max_width,
575 frmsize.stepwise.max_height);
576 } else if (frmsize.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
577 printf("%ux%u - %ux%u with step %u/%u",
578 frmsize.stepwise.min_width,
579 frmsize.stepwise.min_height,
580 frmsize.stepwise.max_width,
581 frmsize.stepwise.max_height,
582 frmsize.stepwise.step_width,
583 frmsize.stepwise.step_height);
584 }
585 printf("\n");
586 }
587
588 void print_frmival(const struct v4l2_frmivalenum &frmival, const char *prefix)
589 {
590 printf("%s\tInterval: %s ", prefix, frmtype2s(frmival.type).c_str());
591 if (frmival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
592 printf("%ss (%s fps)\n", fract2sec(frmival.discrete).c_str(),
593 fract2fps(frmival.discrete).c_str());
594 } else if (frmival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
595 printf("%ss - %ss (%s-%s fps)\n",
596 fract2sec(frmival.stepwise.min).c_str(),
597 fract2sec(frmival.stepwise.max).c_str(),
598 fract2fps(frmival.stepwise.max).c_str(),
599 fract2fps(frmival.stepwise.min).c_str());
600 } else if (frmival.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
601 printf("%ss - %ss with step %ss (%s-%s fps)\n",
602 fract2sec(frmival.stepwise.min).c_str(),
603 fract2sec(frmival.stepwise.max).c_str(),
604 fract2sec(frmival.stepwise.step).c_str(),
605 fract2fps(frmival.stepwise.max).c_str(),
606 fract2fps(frmival.stepwise.min).c_str());
607 }
608 }
609
610 void print_video_formats(cv4l_fd &fd, __u32 type, unsigned int mbus_code, bool enum_all)
611 {
612 cv4l_disable_trace dt(fd);
613 struct v4l2_fmtdesc fmt = {};
614
615 if (mbus_code && !(capabilities & V4L2_CAP_IO_MC))
616 mbus_code = 0;
617
618 printf("\tType: %s\n\n", buftype2s(type).c_str());
619 if (fd.enum_fmt(fmt, true, 0, type, mbus_code, enum_all))
620 return;
621 do {
622 printf("\t[%d]: '%s' (%s", fmt.index, fcc2s(fmt.pixelformat).c_str(),
623 fmt.description);
624 if (fmt.flags) {
625 bool is_hsv = fmt.pixelformat == V4L2_PIX_FMT_HSV24 ||
626 fmt.pixelformat == V4L2_PIX_FMT_HSV32;
627
628 printf(", %s", fmtdesc2s(fmt.flags, is_hsv).c_str());
629 }
630 printf(")\n");
631 } while (!fd.enum_fmt(fmt, false, 0, type, mbus_code, enum_all));
632 }
633
634 void print_video_formats_ext(cv4l_fd &fd, __u32 type, unsigned int mbus_code, bool enum_all)
635 {
636 cv4l_disable_trace dt(fd);
637 struct v4l2_fmtdesc fmt = {};
638 struct v4l2_frmsizeenum frmsize;
639 struct v4l2_frmivalenum frmival;
640
641 if (mbus_code && !(capabilities & V4L2_CAP_IO_MC))
642 mbus_code = 0;
643
644 printf("\tType: %s\n\n", buftype2s(type).c_str());
645 if (fd.enum_fmt(fmt, true, 0, type, mbus_code, enum_all))
646 return;
647 do {
648 printf("\t[%d]: '%s' (%s", fmt.index, fcc2s(fmt.pixelformat).c_str(),
649 fmt.description);
650 if (fmt.flags) {
651 bool is_hsv = fmt.pixelformat == V4L2_PIX_FMT_HSV24 ||
652 fmt.pixelformat == V4L2_PIX_FMT_HSV32;
653
654 printf(", %s", fmtdesc2s(fmt.flags, is_hsv).c_str());
655 }
656 printf(")\n");
657
658 if (enum_all)
659 continue;
660
661 if (fd.enum_framesizes(frmsize, fmt.pixelformat))
662 continue;
663 do {
664 print_frmsize(frmsize, "\t");
665 if (frmsize.type != V4L2_FRMSIZE_TYPE_DISCRETE)
666 continue;
667
668 if (fd.enum_frameintervals(frmival, fmt.pixelformat,
669 frmsize.discrete.width,
670 frmsize.discrete.height))
671 continue;
672 do {
673 print_frmival(frmival, "\t\t");
674 } while (!fd.enum_frameintervals(frmival));
675 } while (!fd.enum_framesizes(frmsize));
676 } while (!fd.enum_fmt(fmt,false, 0, type, mbus_code, enum_all));
677 }
678
679 int parse_subopt(char **subs, const char * const *subopts, char **value)
680 {
681 int opt = v4l_getsubopt(subs, const_cast<char * const *>(subopts), value);
682
683 if (opt == -1) {
684 fprintf(stderr, "Invalid suboptions specified\n");
685 return -1;
686 }
687 if (*value == nullptr) {
688 fprintf(stderr, "No value given to suboption <%s>\n",
689 subopts[opt]);
690 return -1;
691 }
692 return opt;
693 }
694
695 __u32 parse_field(const char *s)
696 {
697 if (!strcmp(s, "any")) return V4L2_FIELD_ANY;
698 if (!strcmp(s, "none")) return V4L2_FIELD_NONE;
699 if (!strcmp(s, "top")) return V4L2_FIELD_TOP;
700 if (!strcmp(s, "bottom")) return V4L2_FIELD_BOTTOM;
701 if (!strcmp(s, "interlaced")) return V4L2_FIELD_INTERLACED;
702 if (!strcmp(s, "seq_tb")) return V4L2_FIELD_SEQ_TB;
703 if (!strcmp(s, "seq_bt")) return V4L2_FIELD_SEQ_BT;
704 if (!strcmp(s, "alternate")) return V4L2_FIELD_ALTERNATE;
705 if (!strcmp(s, "interlaced_tb")) return V4L2_FIELD_INTERLACED_TB;
706 if (!strcmp(s, "interlaced_bt")) return V4L2_FIELD_INTERLACED_BT;
707 return V4L2_FIELD_ANY;
708 }
709
710 __u32 parse_colorspace(const char *s)
711 {
712 if (!strcmp(s, "smpte170m")) return V4L2_COLORSPACE_SMPTE170M;
713 if (!strcmp(s, "smpte240m")) return V4L2_COLORSPACE_SMPTE240M;
714 if (!strcmp(s, "rec709")) return V4L2_COLORSPACE_REC709;
715 if (!strcmp(s, "470m")) return V4L2_COLORSPACE_470_SYSTEM_M;
716 if (!strcmp(s, "470bg")) return V4L2_COLORSPACE_470_SYSTEM_BG;
717 if (!strcmp(s, "jpeg")) return V4L2_COLORSPACE_JPEG;
718 if (!strcmp(s, "srgb")) return V4L2_COLORSPACE_SRGB;
719 if (!strcmp(s, "oprgb")) return V4L2_COLORSPACE_OPRGB;
720 if (!strcmp(s, "bt2020")) return V4L2_COLORSPACE_BT2020;
721 if (!strcmp(s, "dcip3")) return V4L2_COLORSPACE_DCI_P3;
722 return 0;
723 }
724
725 __u32 parse_xfer_func(const char *s)
726 {
727 if (!strcmp(s, "default")) return V4L2_XFER_FUNC_DEFAULT;
728 if (!strcmp(s, "smpte240m")) return V4L2_XFER_FUNC_SMPTE240M;
729 if (!strcmp(s, "rec709")) return V4L2_XFER_FUNC_709;
730 if (!strcmp(s, "srgb")) return V4L2_XFER_FUNC_SRGB;
731 if (!strcmp(s, "oprgb")) return V4L2_XFER_FUNC_OPRGB;
732 if (!strcmp(s, "dcip3")) return V4L2_XFER_FUNC_DCI_P3;
733 if (!strcmp(s, "smpte2084")) return V4L2_XFER_FUNC_SMPTE2084;
734 if (!strcmp(s, "none")) return V4L2_XFER_FUNC_NONE;
735 return 0;
736 }
737
738 __u32 parse_ycbcr(const char *s)
739 {
740 if (!strcmp(s, "default")) return V4L2_YCBCR_ENC_DEFAULT;
741 if (!strcmp(s, "601")) return V4L2_YCBCR_ENC_601;
742 if (!strcmp(s, "709")) return V4L2_YCBCR_ENC_709;
743 if (!strcmp(s, "xv601")) return V4L2_YCBCR_ENC_XV601;
744 if (!strcmp(s, "xv709")) return V4L2_YCBCR_ENC_XV709;
745 if (!strcmp(s, "bt2020")) return V4L2_YCBCR_ENC_BT2020;
746 if (!strcmp(s, "bt2020c")) return V4L2_YCBCR_ENC_BT2020_CONST_LUM;
747 if (!strcmp(s, "smpte240m")) return V4L2_YCBCR_ENC_SMPTE240M;
748 return V4L2_YCBCR_ENC_DEFAULT;
749 }
750
751 __u32 parse_hsv(const char *s)
752 {
753 if (!strcmp(s, "default")) return V4L2_YCBCR_ENC_DEFAULT;
754 if (!strcmp(s, "180")) return V4L2_HSV_ENC_180;
755 if (!strcmp(s, "256")) return V4L2_HSV_ENC_256;
756 return V4L2_YCBCR_ENC_DEFAULT;
757 }
758
759 __u32 parse_quantization(const char *s)
760 {
761 if (!strcmp(s, "default")) return V4L2_QUANTIZATION_DEFAULT;
762 if (!strcmp(s, "full-range")) return V4L2_QUANTIZATION_FULL_RANGE;
763 if (!strcmp(s, "lim-range")) return V4L2_QUANTIZATION_LIM_RANGE;
764 return V4L2_QUANTIZATION_DEFAULT;
765 }
766
767 int parse_fmt(char *optarg, __u32 &width, __u32 &height, __u32 &pixelformat,
768 __u32 &field, __u32 &colorspace, __u32 &xfer_func, __u32 &ycbcr,
769 __u32 &quantization, __u32 &flags, __u32 *bytesperline,
770 __u32 *sizeimage)
771 {
772 char *value, *subs;
773 int fmts = 0;
774 unsigned bpl_index = 0;
775 unsigned sizeimage_index = 0;
776 bool be_pixfmt;
777
778 field = V4L2_FIELD_ANY;
779 flags = 0;
780 subs = optarg;
781 while (*subs != '\0') {
782 static constexpr const char *subopts[] = {
783 "width",
784 "height",
785 "pixelformat",
786 "field",
787 "colorspace",
788 "ycbcr",
789 "hsv",
790 "bytesperline",
791 "premul-alpha",
792 "quantization",
793 "xfer",
794 "sizeimage",
795 nullptr
796 };
797
798 switch (parse_subopt(&subs, subopts, &value)) {
799 case 0:
800 width = strtoul(value, nullptr, 0);
801 fmts |= FmtWidth;
802 break;
803 case 1:
804 height = strtoul(value, nullptr, 0);
805 fmts |= FmtHeight;
806 break;
807 case 2:
808 be_pixfmt = strlen(value) == 7 && !memcmp(value + 4, "-BE", 3);
809 if (be_pixfmt || strlen(value) == 4) {
810 pixelformat =
811 v4l2_fourcc(value[0], value[1],
812 value[2], value[3]);
813 if (be_pixfmt)
814 pixelformat |= 1U << 31;
815 } else if (isdigit(value[0])) {
816 pixelformat = strtoul(value, nullptr, 0);
817 } else {
818 fprintf(stderr, "The pixelformat '%s' is invalid\n", value);
819 std::exit(EXIT_FAILURE);
820 }
821 fmts |= FmtPixelFormat;
822 break;
823 case 3:
824 field = parse_field(value);
825 fmts |= FmtField;
826 break;
827 case 4:
828 colorspace = parse_colorspace(value);
829 if (colorspace)
830 fmts |= FmtColorspace;
831 else
832 fprintf(stderr, "unknown colorspace %s\n", value);
833 break;
834 case 5:
835 ycbcr = parse_ycbcr(value);
836 fmts |= FmtYCbCr;
837 break;
838 case 6:
839 ycbcr = parse_hsv(value);
840 fmts |= FmtYCbCr;
841 break;
842 case 7:
843 bytesperline[bpl_index] = strtoul(value, nullptr, 0);
844 if (bytesperline[bpl_index] > 0xffff) {
845 fprintf(stderr, "bytesperline can't be more than 65535\n");
846 bytesperline[bpl_index] = 0;
847 }
848 bpl_index++;
849 fmts |= FmtBytesPerLine;
850 break;
851 case 8:
852 if (strtoul(value, nullptr, 0))
853 flags |= V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
854 else
855 flags &= ~V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
856 fmts |= FmtFlags;
857 break;
858 case 9:
859 quantization = parse_quantization(value);
860 fmts |= FmtQuantization;
861 break;
862 case 10:
863 xfer_func = parse_xfer_func(value);
864 fmts |= FmtXferFunc;
865 break;
866 case 11:
867 sizeimage[sizeimage_index] = strtoul(value, nullptr, 0);
868 sizeimage_index++;
869 fmts |= FmtSizeImage;
870 break;
871 default:
872 return 0;
873 }
874 }
875 return fmts;
876 }
877
878 int parse_selection_flags(const char *s)
879 {
880 if (!strcmp(s, "le")) return V4L2_SEL_FLAG_LE;
881 if (!strcmp(s, "ge")) return V4L2_SEL_FLAG_GE;
882 if (!strcmp(s, "keep-config")) return V4L2_SEL_FLAG_KEEP_CONFIG;
883 return 0;
884 }
885
886 void print_selection(const struct v4l2_selection &sel)
887 {
888 printf("Selection %s: %s, Left %d, Top %d, Width %d, Height %d, Flags: %s\n",
889 buftype2s(sel.type).c_str(), seltarget2s(sel.target).c_str(),
890 sel.r.left, sel.r.top, sel.r.width, sel.r.height,
891 selflags2s(sel.flags).c_str());
892 }
893
894 int parse_selection_target(const char *s, unsigned int &target)
895 {
896 if (!strcmp(s, "crop")) target = V4L2_SEL_TGT_CROP_ACTIVE;
897 else if (!strcmp(s, "crop_default")) target = V4L2_SEL_TGT_CROP_DEFAULT;
898 else if (!strcmp(s, "crop_bounds")) target = V4L2_SEL_TGT_CROP_BOUNDS;
899 else if (!strcmp(s, "compose")) target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
900 else if (!strcmp(s, "compose_default")) target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
901 else if (!strcmp(s, "compose_bounds")) target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
902 else if (!strcmp(s, "compose_padded")) target = V4L2_SEL_TGT_COMPOSE_PADDED;
903 else if (!strcmp(s, "native_size")) target = V4L2_SEL_TGT_NATIVE_SIZE;
904 else return -EINVAL;
905
906 return 0;
907 }
908
909
910 static void print_event(int fd, const struct v4l2_event *ev)
911 {
912 printf("%lld.%06ld: event %u, pending %u: ",
913 static_cast<__u64>(ev->timestamp.tv_sec), ev->timestamp.tv_nsec / 1000,
914 ev->sequence, ev->pending);
915 switch (ev->type) {
916 case V4L2_EVENT_VSYNC:
917 printf("vsync %s\n", field2s(ev->u.vsync.field).c_str());
918 break;
919 case V4L2_EVENT_EOS:
920 printf("eos\n");
921 break;
922 case V4L2_EVENT_CTRL:
923 common_control_event(fd, ev);
924 break;
925 case V4L2_EVENT_FRAME_SYNC:
926 printf("frame_sync %d\n", ev->u.frame_sync.frame_sequence);
927 break;
928 case V4L2_EVENT_SOURCE_CHANGE:
929 printf("source_change: pad/input=%d changes: %x\n", ev->id, ev->u.src_change.changes);
930 break;
931 case V4L2_EVENT_MOTION_DET:
932 if (ev->u.motion_det.flags & V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ)
933 printf("motion_det frame %d, regions 0x%x\n",
934 ev->u.motion_det.frame_sequence,
935 ev->u.motion_det.region_mask);
936 else
937 printf("motion_det regions 0x%x\n", ev->u.motion_det.region_mask);
938 break;
939 default:
940 if (ev->type >= V4L2_EVENT_PRIVATE_START)
941 printf("unknown private event (%08x)\n", ev->type);
942 else
943 printf("unknown event (%08x)\n", ev->type);
944 break;
945 }
946 }
947
948 static __u32 parse_event(const char *e, const char **name)
949 {
950 __u32 event = 0;
951
952 *name = "0";
953 if (isdigit(e[0])) {
954 event = strtoul(e, nullptr, 0);
955 if (event == V4L2_EVENT_CTRL) {
956 fprintf(stderr, "Missing control name for ctrl event, use ctrl=<name>\n");
957 misc_usage();
958 std::exit(EXIT_FAILURE);
959 }
960 } else if (!strcmp(e, "eos")) {
961 event = V4L2_EVENT_EOS;
962 } else if (!strcmp(e, "vsync")) {
963 event = V4L2_EVENT_VSYNC;
964 } else if (!strcmp(e, "frame_sync")) {
965 event = V4L2_EVENT_FRAME_SYNC;
966 } else if (!strcmp(e, "motion_det")) {
967 event = V4L2_EVENT_MOTION_DET;
968 } else if (!strncmp(e, "ctrl=", 5)) {
969 event = V4L2_EVENT_CTRL;
970 *name = e + 5;
971 } else if (!strncmp(e, "source_change=", 14)) {
972 event = V4L2_EVENT_SOURCE_CHANGE;
973 *name = e + 14;
974 } else if (!strcmp(e, "source_change")) {
975 event = V4L2_EVENT_SOURCE_CHANGE;
976 }
977
978 if (event == 0) {
979 fprintf(stderr, "Unknown event\n");
980 misc_usage();
981 std::exit(EXIT_FAILURE);
982 }
983 return event;
984 }
985
986 bool valid_pixel_format(int fd, __u32 pixelformat, bool output, bool mplane)
987 {
988 struct v4l2_fmtdesc fmt = {};
989
990 if (output)
991 fmt.type = mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
992 V4L2_BUF_TYPE_VIDEO_OUTPUT;
993 else
994 fmt.type = mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
995 V4L2_BUF_TYPE_VIDEO_CAPTURE;
996
997 while (!ioctl(fd, VIDIOC_ENUM_FMT, &fmt)) {
998 if (fmt.pixelformat == pixelformat)
999 return true;
1000 fmt.index++;
1001 }
1002 return false;
1003 }
1004
1005 __u32 find_pixel_format(int fd, unsigned index, bool output, bool mplane)
1006 {
1007 struct v4l2_fmtdesc fmt = {};
1008
1009 fmt.index = index;
1010 if (output)
1011 fmt.type = mplane ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1012 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1013 else
1014 fmt.type = mplane ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1015 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1016
1017 if (ioctl(fd, VIDIOC_ENUM_FMT, &fmt))
1018 return 0;
1019 return fmt.pixelformat;
1020 }
1021
1022 static int open_media_bus_info(const std::string &bus_info)
1023 {
1024 DIR *dp;
1025 struct dirent *ep;
1026
1027 dp = opendir("/dev");
1028 if (dp == nullptr)
1029 return -1;
1030
1031 while ((ep = readdir(dp))) {
1032 const char *name = ep->d_name;
1033
1034 if (!memcmp(name, "media", 5) && isdigit(name[5])) {
1035 struct media_device_info mdi;
1036 std::string devname = std::string("/dev/") + name;
1037
1038 int fd = open(devname.c_str(), O_RDWR);
1039 if (fd < 0)
1040 continue;
1041 if (!ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi) &&
1042 bus_info == mdi.bus_info) {
1043 closedir(dp);
1044 return fd;
1045 }
1046 close(fd);
1047 }
1048 }
1049 closedir(dp);
1050 return -1;
1051 }
1052
1053 static const char *make_devname(const char *device, const char *devname,
1054 const std::string &media_bus_info)
1055 {
1056 if (device[0] >= '0' && device[0] <= '9' && strlen(device) <= 3) {
1057 static char newdev[32];
1058
1059 sprintf(newdev, "/dev/%s%s", devname, device);
1060 return newdev;
1061 }
1062 if (media_bus_info.empty())
1063 return device;
1064 int media_fd = open_media_bus_info(media_bus_info);
1065 if (media_fd < 0)
1066 return device;
1067
1068 media_v2_topology topology;
1069 memset(&topology, 0, sizeof(topology));
1070 if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology)) {
1071 close(media_fd);
1072 return device;
1073 }
1074
1075 auto ents = new media_v2_entity[topology.num_entities];
1076 topology.ptr_entities = (uintptr_t)ents;
1077 auto links = new media_v2_link[topology.num_links];
1078 topology.ptr_links = (uintptr_t)links;
1079 auto ifaces = new media_v2_interface[topology.num_interfaces];
1080 topology.ptr_interfaces = (uintptr_t)ifaces;
1081
1082 unsigned i, ent_id, iface_id = 0;
1083
1084 if (ioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology))
1085 goto err;
1086
1087 if (device[0] == '0' && device[1] == 'x')
1088 iface_id = strtoul(device, nullptr, 16);
1089
1090 if (!iface_id) {
1091 for (i = 0; i < topology.num_entities; i++)
1092 if (!strcmp(ents[i].name, device))
1093 break;
1094 if (i >= topology.num_entities)
1095 goto err;
1096 ent_id = ents[i].id;
1097 for (i = 0; i < topology.num_links; i++)
1098 if (links[i].sink_id == ent_id &&
1099 (links[i].flags & MEDIA_LNK_FL_LINK_TYPE) ==
1100 MEDIA_LNK_FL_INTERFACE_LINK)
1101 break;
1102 if (i >= topology.num_links)
1103 goto err;
1104 iface_id = links[i].source_id;
1105 }
1106 for (i = 0; i < topology.num_interfaces; i++)
1107 if (ifaces[i].id == iface_id)
1108 break;
1109 if (i >= topology.num_interfaces)
1110 goto err;
1111
1112 static char newdev[32];
1113 sprintf(newdev, "/dev/char/%d:%d",
1114 ifaces[i].devnode.major, ifaces[i].devnode.minor);
1115 device = newdev;
1116
1117 err:
1118 delete [] ents;
1119 delete [] links;
1120 delete [] ifaces;
1121 close(media_fd);
1122 return device;
1123 }
1124
1125 int main(int argc, char **argv)
1126 {
1127 struct event {
1128 int type;
1129 __u32 ev;
1130 __u32 id;
1131 std::string name;
1132 };
1133 int i;
1134 cv4l_fd c_fd;
1135 cv4l_fd c_out_fd;
1136 cv4l_fd c_exp_fd;
1137 int fd = -1;
1138 int out_fd = -1;
1139 int exp_fd = -1;
1140 int media_fd = -1;
1141 bool is_subdev = false;
1142 std::string media_bus_info;
1143
1144 /* command args */
1145 int ch;
1146 const char *device = "/dev/video0"; /* -d device */
1147 const char *out_device = nullptr;
1148 const char *export_device = nullptr;
1149 struct v4l2_capability vcap = {};
1150 struct v4l2_subdev_capability subdevcap = {};
1151 struct v4l2_subdev_client_capability subdevclientcap = {};
1152 std::vector<event> events;
1153 unsigned secs = 0;
1154 char short_options[26 * 2 * 3 + 1];
1155 int idx = 0;
1156
1157 if (argc == 1) {
1158 common_usage();
1159 return 0;
1160 }
1161 for (i = 0; long_options[i].name; i++) {
1162 if (!isalpha(long_options[i].val))
1163 continue;
1164 short_options[idx++] = long_options[i].val;
1165 if (long_options[i].has_arg == required_argument) {
1166 short_options[idx++] = ':';
1167 } else if (long_options[i].has_arg == optional_argument) {
1168 short_options[idx++] = ':';
1169 short_options[idx++] = ':';
1170 }
1171 }
1172 while (true) {
1173 int option_index = 0;
1174 const char *name;
1175 event new_ev;
1176
1177 short_options[idx] = 0;
1178 ch = getopt_long(argc, argv, short_options,
1179 long_options, &option_index);
1180 if (ch == -1)
1181 break;
1182
1183 options[ch] = 1;
1184 if (!option_index) {
1185 for (i = 0; long_options[i].val; i++) {
1186 if (long_options[i].val == ch) {
1187 option_index = i;
1188 break;
1189 }
1190 }
1191 }
1192 if (long_options[option_index].has_arg == optional_argument &&
1193 !optarg && argv[optind] && argv[optind][0] != '-')
1194 optarg = argv[optind++];
1195
1196 switch (ch) {
1197 case OptHelp:
1198 common_usage();
1199 return 0;
1200 case OptHelpTuner:
1201 tuner_usage();
1202 return 0;
1203 case OptHelpIO:
1204 io_usage();
1205 return 0;
1206 case OptHelpStds:
1207 stds_usage();
1208 return 0;
1209 case OptHelpVidCap:
1210 vidcap_usage();
1211 return 0;
1212 case OptHelpVidOut:
1213 vidout_usage();
1214 return 0;
1215 case OptHelpOverlay:
1216 overlay_usage();
1217 return 0;
1218 case OptHelpVbi:
1219 vbi_usage();
1220 return 0;
1221 case OptHelpSdr:
1222 sdr_usage();
1223 return 0;
1224 case OptHelpMeta:
1225 meta_usage();
1226 return 0;
1227 case OptHelpSubDev:
1228 subdev_usage();
1229 return 0;
1230 case OptHelpSelection:
1231 selection_usage();
1232 return 0;
1233 case OptHelpMisc:
1234 misc_usage();
1235 return 0;
1236 case OptHelpStreaming:
1237 streaming_usage();
1238 return 0;
1239 case OptHelpEdid:
1240 edid_usage();
1241 return 0;
1242 case OptHelpAll:
1243 usage_all();
1244 return 0;
1245 case OptSetDevice:
1246 device = make_devname(optarg, "video", media_bus_info);
1247 break;
1248 case OptSetOutDevice:
1249 out_device = make_devname(optarg, "video", media_bus_info);
1250 break;
1251 case OptSetExportDevice:
1252 export_device = make_devname(optarg, "video", media_bus_info);
1253 break;
1254 case OptMediaBusInfo:
1255 media_bus_info = optarg;
1256 break;
1257 case OptWaitForEvent:
1258 case OptPollForEvent:
1259 case OptEPollForEvent:
1260 new_ev.type = ch;
1261 new_ev.ev = parse_event(optarg, &name);
1262 new_ev.id = 0;
1263 if (new_ev.ev == 0)
1264 return 1;
1265 if (new_ev.ev == V4L2_EVENT_SOURCE_CHANGE)
1266 new_ev.id = strtoul(name, nullptr, 0);
1267 else if (new_ev.ev == V4L2_EVENT_CTRL)
1268 new_ev.name = name;
1269 events.push_back(new_ev);
1270 break;
1271 case OptSleep:
1272 secs = strtoul(optarg, nullptr, 0);
1273 break;
1274 case OptVersion:
1275 print_version();
1276 return 0;
1277 case ':':
1278 fprintf(stderr, "Option '%s' requires a value\n",
1279 argv[optind]);
1280 common_usage();
1281 return 1;
1282 case '?':
1283 if (argv[optind])
1284 fprintf(stderr, "Unknown argument '%s'\n", argv[optind]);
1285 common_usage();
1286 return 1;
1287 default:
1288 common_cmd(ch, optarg);
1289 tuner_cmd(ch, optarg);
1290 io_cmd(ch, optarg);
1291 stds_cmd(ch, optarg);
1292 vidcap_cmd(ch, optarg);
1293 vidout_cmd(ch, optarg);
1294 overlay_cmd(ch, optarg);
1295 vbi_cmd(ch, optarg);
1296 sdr_cmd(ch, optarg);
1297 meta_cmd(ch, optarg);
1298 subdev_cmd(ch, optarg);
1299 selection_cmd(ch, optarg);
1300 misc_cmd(ch, optarg);
1301 streaming_cmd(ch, optarg);
1302 edid_cmd(ch, optarg);
1303 break;
1304 }
1305 }
1306 if (optind < argc) {
1307 printf("unknown arguments: ");
1308 while (optind < argc)
1309 printf("%s ", argv[optind++]);
1310 printf("\n");
1311 common_usage();
1312 return 1;
1313 }
1314
1315 verbose = options[OptVerbose];
1316
1317 if (common_list_devices(media_bus_info, c_fd))
1318 return 0;
1319
1320 media_type type = mi_media_detect_type(device);
1321 if (type == MEDIA_TYPE_CANT_STAT) {
1322 fprintf(stderr, "Cannot open device %s, exiting.\n",
1323 device);
1324 std::exit(EXIT_FAILURE);
1325 }
1326
1327 switch (type) {
1328 // For now we can only handle V4L2 devices
1329 case MEDIA_TYPE_VIDEO:
1330 case MEDIA_TYPE_VBI:
1331 case MEDIA_TYPE_RADIO:
1332 case MEDIA_TYPE_SDR:
1333 case MEDIA_TYPE_TOUCH:
1334 case MEDIA_TYPE_SUBDEV:
1335 break;
1336 default:
1337 type = MEDIA_TYPE_UNKNOWN;
1338 break;
1339 }
1340
1341 if (type == MEDIA_TYPE_UNKNOWN) {
1342 fprintf(stderr, "Unable to detect what device %s is, exiting.\n",
1343 device);
1344 std::exit(EXIT_FAILURE);
1345 }
1346 is_subdev = type == MEDIA_TYPE_SUBDEV;
1347 if (is_subdev)
1348 options[OptUseWrapper] = 0;
1349 c_fd.s_direct(!options[OptUseWrapper]);
1350 c_out_fd.s_direct(!options[OptUseWrapper]);
1351 c_exp_fd.s_direct(!options[OptUseWrapper]);
1352
1353 if (is_subdev)
1354 fd = c_fd.subdev_open(device);
1355 else
1356 fd = c_fd.open(device);
1357
1358 if (fd < 0) {
1359 fprintf(stderr, "Failed to open %s: %s\n", device,
1360 strerror(errno));
1361 std::exit(EXIT_FAILURE);
1362 }
1363
1364 c_fd.s_trace(options[OptSilent] ? 0 : (verbose ? 2 : 1));
1365
1366 if (!is_subdev && doioctl(fd, VIDIOC_QUERYCAP, &vcap)) {
1367 fprintf(stderr, "%s: not a v4l2 node\n", device);
1368 std::exit(EXIT_FAILURE);
1369 } else if (is_subdev) {
1370 // This ioctl was introduced in kernel 5.10, so don't
1371 // exit if this ioctl returns an error.
1372 doioctl(fd, VIDIOC_SUBDEV_QUERYCAP, &subdevcap);
1373 subdevclientcap.capabilities = ~0ULL;
1374 if (doioctl(fd, VIDIOC_SUBDEV_S_CLIENT_CAP, &subdevclientcap))
1375 subdevclientcap.capabilities = 0ULL;
1376 }
1377 if (!is_subdev) {
1378 capabilities = vcap.capabilities;
1379 if (capabilities & V4L2_CAP_DEVICE_CAPS)
1380 capabilities = vcap.device_caps;
1381 }
1382
1383 media_fd = mi_get_media_fd(fd, is_subdev ? 0 : (const char *)vcap.bus_info);
1384
1385 priv_magic = (capabilities & V4L2_CAP_EXT_PIX_FORMAT) ?
1386 V4L2_PIX_FMT_PRIV_MAGIC : 0;
1387 is_multiplanar = capabilities & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1388 V4L2_CAP_VIDEO_M2M_MPLANE |
1389 V4L2_CAP_VIDEO_OUTPUT_MPLANE);
1390
1391 vidcap_buftype = is_multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1392 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1393 vidout_buftype = is_multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1394 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1395
1396 if (out_device) {
1397 out_fd = c_out_fd.open(out_device);
1398 if (out_fd < 0) {
1399 fprintf(stderr, "Failed to open %s: %s\n", out_device,
1400 strerror(errno));
1401 std::exit(EXIT_FAILURE);
1402 }
1403 c_out_fd.s_trace(options[OptSilent] ? 0 : (verbose ? 2 : 1));
1404 if (doioctl(out_fd, VIDIOC_QUERYCAP, &vcap)) {
1405 fprintf(stderr, "%s: not a v4l2 node\n", out_device);
1406 std::exit(EXIT_FAILURE);
1407 }
1408 out_capabilities = vcap.capabilities;
1409 if (out_capabilities & V4L2_CAP_DEVICE_CAPS)
1410 out_capabilities = vcap.device_caps;
1411 out_priv_magic = (out_capabilities & V4L2_CAP_EXT_PIX_FORMAT) ?
1412 V4L2_PIX_FMT_PRIV_MAGIC : 0;
1413 }
1414
1415 if (export_device) {
1416 exp_fd = c_exp_fd.open(export_device);
1417 if (exp_fd < 0) {
1418 fprintf(stderr, "Failed to open %s: %s\n", export_device,
1419 strerror(errno));
1420 std::exit(EXIT_FAILURE);
1421 }
1422 c_exp_fd.s_trace(options[OptSilent] ? 0 : (verbose ? 2 : 1));
1423 if (doioctl(exp_fd, VIDIOC_QUERYCAP, &vcap)) {
1424 fprintf(stderr, "%s: not a v4l2 node\n", export_device);
1425 std::exit(EXIT_FAILURE);
1426 }
1427 }
1428
1429 common_process_controls(c_fd);
1430
1431 for (auto &e : events) {
1432 if (e.ev != V4L2_EVENT_CTRL)
1433 continue;
1434 e.id = common_find_ctrl_id(e.name.c_str());
1435 if (!e.id) {
1436 fprintf(stderr, "unknown control '%s'\n", e.name.c_str());
1437 std::exit(EXIT_FAILURE);
1438 }
1439 }
1440
1441 if (options[OptAll]) {
1442 options[OptGetVideoFormat] = 1;
1443 options[OptGetVideoOutFormat] = 1;
1444 options[OptGetDriverInfo] = 1;
1445 options[OptGetInput] = 1;
1446 options[OptGetOutput] = 1;
1447 options[OptGetAudioInput] = 1;
1448 options[OptGetAudioOutput] = 1;
1449 options[OptGetStandard] = 1;
1450 options[OptGetParm] = 1;
1451 options[OptGetOutputParm] = 1;
1452 options[OptGetFreq] = 1;
1453 options[OptGetTuner] = 1;
1454 options[OptGetModulator] = 1;
1455 options[OptGetOverlayFormat] = 1;
1456 options[OptGetVbiFormat] = 1;
1457 options[OptGetVbiOutFormat] = 1;
1458 options[OptGetSlicedVbiFormat] = 1;
1459 options[OptGetSlicedVbiOutFormat] = 1;
1460 options[OptGetSdrFormat] = 1;
1461 options[OptGetSdrOutFormat] = 1;
1462 options[OptGetMetaFormat] = 1;
1463 options[OptGetMetaOutFormat] = 1;
1464 options[OptGetFBuf] = 1;
1465 options[OptGetCropCap] = 1;
1466 options[OptGetOutputCropCap] = 1;
1467 options[OptGetJpegComp] = 1;
1468 options[OptGetDvTimings] = 1;
1469 options[OptGetDvTimingsCap] = 1;
1470 options[OptGetPriority] = 1;
1471 options[OptGetSelection] = 1;
1472 options[OptGetOutputSelection] = 1;
1473 options[OptListCtrlsMenus] = 1;
1474 options[OptSilent] = 1;
1475 }
1476
1477 /* Information Opts */
1478
1479 if (options[OptGetDriverInfo]) {
1480 printf("Driver Info%s:\n",
1481 options[OptUseWrapper] ? " (using libv4l2)" : "");
1482 if (is_subdev) {
1483 v4l2_info_subdev_capability(subdevcap, subdevclientcap);
1484 } else {
1485 v4l2_info_capability(vcap);
1486 }
1487 }
1488 if (options[OptGetDriverInfo] && media_fd >= 0)
1489 mi_media_info_for_fd(media_fd, fd);
1490
1491 /* Set options */
1492
1493 common_set(c_fd);
1494 tuner_set(c_fd);
1495 io_set(c_fd);
1496 stds_set(c_fd);
1497 vidcap_set(c_fd);
1498 vidout_set(out_device ? c_out_fd : c_fd);
1499 overlay_set(c_fd);
1500 vbi_set(c_fd);
1501 sdr_set(c_fd);
1502 meta_set(c_fd);
1503 subdev_set(c_fd);
1504 selection_set(c_fd);
1505 misc_set(c_fd);
1506 edid_set(c_fd);
1507
1508 /* Get options */
1509
1510 common_get(c_fd);
1511 tuner_get(c_fd);
1512 io_get(c_fd);
1513 stds_get(c_fd);
1514 vidcap_get(c_fd);
1515 vidout_get(out_device ? c_out_fd : c_fd);
1516 overlay_get(c_fd);
1517 vbi_get(c_fd);
1518 sdr_get(c_fd);
1519 meta_get(c_fd);
1520 subdev_get(c_fd);
1521 selection_get(c_fd);
1522 misc_get(c_fd);
1523 edid_get(c_fd);
1524
1525 /* List options */
1526
1527 common_list(c_fd);
1528 io_list(c_fd);
1529 stds_list(c_fd);
1530 vidcap_list(c_fd);
1531 vidout_list(out_device ? c_out_fd : c_fd);
1532 overlay_list(c_fd);
1533 vbi_list(c_fd);
1534 sdr_list(c_fd);
1535 meta_list(c_fd);
1536 subdev_list(c_fd);
1537 streaming_list(c_fd, c_out_fd);
1538
1539 /* Special case: handled last */
1540
1541 streaming_set(c_fd, c_out_fd, c_exp_fd);
1542
1543 for (const auto &e : events) {
1544 struct v4l2_event_subscription sub;
1545 struct v4l2_event ev;
1546
1547 if (e.type != OptWaitForEvent)
1548 continue;
1549 memset(&sub, 0, sizeof(sub));
1550 sub.type = e.ev;
1551 sub.id = e.id;
1552 if (!doioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub))
1553 if (!doioctl(fd, VIDIOC_DQEVENT, &ev))
1554 print_event(fd, &ev);
1555 doioctl(fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub);
1556 }
1557
1558 for (const auto &e : events) {
1559 struct v4l2_event_subscription sub;
1560
1561 if (e.type == OptWaitForEvent)
1562 continue;
1563 memset(&sub, 0, sizeof(sub));
1564 sub.flags = V4L2_EVENT_SUB_FL_SEND_INITIAL;
1565 sub.type = e.ev;
1566 sub.id = e.id;
1567 doioctl(fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
1568 }
1569
1570 if (options[OptPollForEvent]) {
1571 fd_set fds;
1572 __u32 seq = 0;
1573
1574 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
1575 while (true) {
1576 struct v4l2_event ev;
1577 int res;
1578
1579 FD_ZERO(&fds);
1580 FD_SET(fd, &fds);
1581 res = select(fd + 1, nullptr, nullptr, &fds, nullptr);
1582 if (res <= 0)
1583 break;
1584 if (doioctl(fd, VIDIOC_DQEVENT, &ev))
1585 break;
1586 print_event(fd, &ev);
1587 if (ev.sequence > seq)
1588 printf("\tMissed %d events\n", ev.sequence - seq);
1589 seq = ev.sequence + 1;
1590 }
1591 }
1592
1593 if (options[OptEPollForEvent]) {
1594 struct epoll_event epoll_ev;
1595 int epollfd = -1;
1596 __u32 seq = 0;
1597
1598 epollfd = epoll_create1(0);
1599 epoll_ev.events = EPOLLPRI;
1600 epoll_ev.data.fd = fd;
1601
1602 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
1603 epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &epoll_ev);
1604 while (true) {
1605 struct v4l2_event ev;
1606 int res;
1607
1608 res = epoll_wait(epollfd, &epoll_ev, 1, -1);
1609 if (res <= 0)
1610 break;
1611 if (doioctl(fd, VIDIOC_DQEVENT, &ev))
1612 break;
1613 print_event(fd, &ev);
1614 if (ev.sequence > seq)
1615 printf("\tMissed %d events\n", ev.sequence - seq);
1616 seq = ev.sequence + 1;
1617 }
1618 close(epollfd);
1619 }
1620
1621 if (options[OptSleep]) {
1622 sleep(secs);
1623 printf("Test VIDIOC_QUERYCAP:\n");
1624 if (c_fd.querycap(vcap, true) == 0)
1625 printf("\tDriver name : %s\n", vcap.driver);
1626 else
1627 perror("VIDIOC_QUERYCAP");
1628 }
1629
1630 c_fd.close();
1631 if (out_device)
1632 c_out_fd.close();
1633 if (export_device)
1634 c_exp_fd.close();
1635 if (media_fd >= 0)
1636 close(media_fd);
1637
1638 // --all sets --silent to avoid ioctl errors to be shown when an ioctl
1639 // is not implemented by the driver. Which is fine, but we shouldn't
1640 // return an application error in that specific case.
1641 std::exit(options[OptAll] ? 0 : app_result);
1642 }