-
Notifications
You must be signed in to change notification settings - Fork 4
/
pivid_inspect_v4l2.cpp
341 lines (314 loc) · 11 KB
/
pivid_inspect_v4l2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Simple command line tool to list V4L devices.
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <vector>
#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <fmt/core.h>
// Print driver name and capability bits from VIDIOC_QUERYCAP results.
std::string describe_driver(v4l2_capability const& cap) {
uint32_t const v = cap.version;
std::string out = fmt::format(
"{} v{}.{}.{}:",
(char const*) cap.driver, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF
);
auto const caps = (cap.capabilities & V4L2_CAP_DEVICE_CAPS)
? cap.device_caps : cap.capabilities;
for (uint32_t bit = 1; bit > 0; bit <<= 1) {
if (!(caps & bit)) continue;
switch (bit) {
#define C(X) case V4L2_CAP_##X: out += fmt::format(" {}", #X); break
C(VIDEO_CAPTURE);
C(VIDEO_CAPTURE_MPLANE);
C(VIDEO_OUTPUT);
C(VIDEO_OUTPUT_MPLANE);
C(VIDEO_M2M);
C(VIDEO_M2M_MPLANE);
C(VIDEO_OVERLAY);
C(VBI_CAPTURE);
C(VBI_OUTPUT);
C(SLICED_VBI_CAPTURE);
C(SLICED_VBI_OUTPUT);
C(RDS_CAPTURE);
C(VIDEO_OUTPUT_OVERLAY);
C(HW_FREQ_SEEK);
C(RDS_OUTPUT);
C(TUNER);
C(AUDIO);
C(RADIO);
C(MODULATOR);
C(SDR_CAPTURE);
C(EXT_PIX_FORMAT);
C(SDR_OUTPUT);
C(META_CAPTURE);
C(READWRITE);
C(ASYNCIO);
C(STREAMING);
C(META_OUTPUT);
C(TOUCH);
C(IO_MC);
#undef C
default: out += fmt::format(" ?0x{:x}?", bit); break;
}
}
return out;
}
// Scan all V4L2 video devices and print a line for each.
void scan_videodevs() {
fmt::print("=== Scanning V4L video I/O devices ===\n");
std::filesystem::path const dev_dir = "/dev";
std::vector<std::string> dev_files;
for (auto const& entry : std::filesystem::directory_iterator(dev_dir)) {
std::string const filename = entry.path().filename();
if (filename.substr(0, 5) == "video" && isdigit(filename[5]))
dev_files.push_back(entry.path().native());
}
std::sort(dev_files.begin(), dev_files.end());
for (auto const &path : dev_files) {
int const fd = open(path.c_str(), O_RDWR);
if (fd < 0) {
fmt::print("*** {}: {}\n", path, strerror(errno));
continue;
}
v4l2_capability cap = {};
if (!ioctl(fd, VIDIOC_QUERYCAP, &cap)) {
fmt::print(
"{}: {} ({})\n",
path, (char*) cap.bus_info, (char*) cap.card
);
fmt::print(" {}\n", describe_driver(cap));
}
close(fd);
}
if (dev_files.empty()) {
fmt::print("*** No V4L devices found\n");
} else {
fmt::print(
"--- {} V4L device(s); inspect with --dev=<dev>\n",
dev_files.size()
);
}
}
// Print information about a V4L2 video device.
void inspect_videodev(std::string const& path) {
fmt::print("=== {} ===\n", path);
int const fd = open(path.c_str(), O_RDWR);
if (fd < 0) {
fmt::print("*** {}: {}\n", path, strerror(errno));
exit(1);
}
v4l2_capability cap = {};
if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) {
fmt::print("*** Querying: {}\n", strerror(errno));
exit(1);
}
fmt::print("Driver: {}\n", describe_driver(cap));
fmt::print("Device: {} ({})\n", (char*) cap.bus_info, (char*) cap.card);
fmt::print("\n");
for (int type = 0; type < V4L2_BUF_TYPE_PRIVATE; ++type) {
v4l2_fmtdesc format = {};
for (
format.type = type;
ioctl(fd, VIDIOC_ENUM_FMT, &format) >= 0;
++format.index
) {
if (format.index == 0) {
switch (format.type) {
#define T(X) case V4L2_BUF_TYPE_##X: fmt::print("{}", #X); break
T(VIDEO_CAPTURE);
T(VIDEO_CAPTURE_MPLANE);
T(VIDEO_OUTPUT);
T(VIDEO_OUTPUT_MPLANE);
T(VIDEO_OVERLAY);
T(SDR_CAPTURE);
T(SDR_OUTPUT);
T(META_CAPTURE);
T(META_OUTPUT);
#undef T
default: fmt::print("?{}?", format.type); break;
}
fmt::print(":");
v4l2_requestbuffers buffers = {};
buffers.count = 0; // Query capbilities only
buffers.type = format.type;
buffers.memory = V4L2_MEMORY_MMAP;
if (!ioctl(fd, VIDIOC_REQBUFS, &buffers)) {
for (uint32_t bit = 1; bit > 0; bit <<= 1) {
if (!(buffers.capabilities & bit)) continue;
switch (bit) {
#define C(X) case V4L2_BUF_CAP_SUPPORTS_##X: fmt::print(" {}", #X); break
C(MMAP);
C(USERPTR);
C(DMABUF);
C(REQUESTS);
C(ORPHANED_BUFS);
C(M2M_HOLD_CAPTURE_BUF);
C(MMAP_CACHE_HINTS);
#undef C
default: fmt::print(" ?0x{:x}?", bit); break;
}
}
}
fmt::print("\n");
}
std::string const fourcc((char const*) &format.pixelformat, 4);
std::string const desc((char const*) format.description);
fmt::print(" {}", fourcc);
for (uint32_t bit = 1; bit > 0; bit <<= 1) {
if (!(format.flags & bit)) continue;
switch (bit) {
#define F(X) case V4L2_FMT_FLAG_##X: fmt::print(" {}", #X); break
F(COMPRESSED);
F(EMULATED);
F(CONTINUOUS_BYTESTREAM);
F(DYN_RESOLUTION);
F(ENC_CAP_FRAME_INTERVAL);
F(CSC_COLORSPACE);
F(CSC_XFER_FUNC);
F(CSC_YCBCR_ENC);
F(CSC_QUANTIZATION);
#undef F
default: fmt::print(" ?0x{:x}?", bit); break;
}
}
if (desc != fourcc) fmt::print(" ({})", desc);
v4l2_frmsizeenum size = {};
size.pixel_format = format.pixelformat;
while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0) {
if (size.index % 6 == 0) fmt::print("\n ");
if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
auto const& dim = size.discrete;
fmt::print(" {}x{}", dim.width, dim.height);
} else {
auto const& dim = size.stepwise;
fmt::print(
" {}x{} - {}x{}",
dim.min_width, dim.min_height,
dim.max_width, dim.max_height
);
if (dim.step_width != 1 || dim.step_height != 1) {
fmt::print(
" ±{}x{}", dim.step_width, dim.step_height
);
}
}
++size.index;
}
fmt::print("\n");
}
if (format.index > 0) fmt::print("\n");
}
v4l2_input input = {};
for (; !ioctl(fd, VIDIOC_ENUMINPUT, &input); ++input.index) {
if (input.index == 0) fmt::print("Inputs:\n");
fmt::print(" Inp #{}", input.index);
switch (input.type) {
#define I(X, y) case V4L2_INPUT_TYPE_##X: fmt::print(" {}{}", #X, y); break
I(TUNER, "");
I(CAMERA, "/video");
I(TOUCH, "");
#undef I
default: fmt::print(" ?{}?", input.type); break;
}
fmt::print(" ({})\n", (char const*) input.name);
}
if (input.index > 0) fmt::print("\n");
v4l2_output output = {};
for (; !ioctl(fd, VIDIOC_ENUMOUTPUT, &output); ++output.index) {
if (output.index == 0) fmt::print("Outputs:\n");
fmt::print(" Out #{}", output.index);
switch (output.type) {
#define O(X, y) case V4L2_OUTPUT_TYPE_##X: fmt::print(" {}{}", #X, y); break
O(MODULATOR, "");
O(ANALOG, "/video");
O(ANALOGVGAOVERLAY, "/overlay");
#undef O
default: fmt::print(" ?{}?", output.type); break;
}
fmt::print(" ({})\n", (char const*) output.name);
}
if (output.index > 0) fmt::print("\n");
v4l2_query_ext_ctrl ctrl = {};
ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
int found = 0;
for (
;
ioctl(fd, VIDIOC_QUERY_EXT_CTRL, &ctrl) >= 0;
ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND
) {
if (!found++) fmt::print("Controls:\n");
fmt::print(" Ctrl 0x{:x}", ctrl.id);
switch (ctrl.type) {
#define T(X) case V4L2_CTRL_TYPE_##X: fmt::print(" {:<7}", #X); break
T(INTEGER);
T(BOOLEAN);
T(MENU);
T(BUTTON);
T(INTEGER64);
T(CTRL_CLASS);
T(STRING);
T(BITMASK);
T(INTEGER_MENU);
T(U8);
T(U16);
T(U32);
T(AREA);
#undef T
default: fmt::print(" ?{}?", ctrl.type); break;
}
if (ctrl.minimum || ctrl.maximum) {
fmt::print(" {:>4}-{:<4}", ctrl.minimum, ctrl.maximum);
if (ctrl.step > 1) fmt::print(" ±{}", ctrl.step);
}
for (uint32_t bit = 1; bit > 0; bit <<= 1) {
if (!(ctrl.flags & bit)) continue;
switch (bit) {
#define F(X) case V4L2_CTRL_FLAG_##X: fmt::print(" {}", #X); break
F(DISABLED);
F(GRABBED);
F(READ_ONLY);
F(UPDATE);
F(INACTIVE);
F(SLIDER);
F(WRITE_ONLY);
F(VOLATILE);
F(HAS_PAYLOAD);
F(EXECUTE_ON_WRITE);
F(MODIFY_LAYOUT);
#undef F
}
}
fmt::print(" ({})\n", ctrl.name);
if (ctrl.type == V4L2_CTRL_TYPE_MENU) {
v4l2_querymenu item = {};
item.id = ctrl.id;
for (; ioctl(fd, VIDIOC_QUERYMENU, &item) >= 0; ++item.index) {
fmt::print(
" {}: {}\n",
int(item.index), (char const*) item.name
);
}
}
}
if (found > 0) fmt::print("\n");
close(fd);
}
int main(int argc, char** argv) {
std::string dev;
CLI::App app("Inspect kernel video (V4L2) devices");
app.add_option("--dev", dev, "Video device (in /dev/v4l) to inspect");
CLI11_PARSE(app, argc, argv);
if (!dev.empty()) {
inspect_videodev(dev);
} else {
scan_videodevs();
}
return 0;
}