| 1 |
#include <cctype> |
| 2 |
|
| 3 |
#include <endian.h> |
| 4 |
|
| 5 |
#include "v4l2-ctl.h" |
| 6 |
|
| 7 |
static struct v4l2_frmsizeenum frmsize; /* list frame sizes */ |
| 8 |
static struct v4l2_frmivalenum frmival; /* list frame intervals */ |
| 9 |
static unsigned set_fmts; |
| 10 |
static __u32 width, height, pixfmt, field, flags; |
| 11 |
static __u32 ycbcr, quantization, xfer_func, colorspace; |
| 12 |
static __u32 bytesperline[VIDEO_MAX_PLANES]; |
| 13 |
static __u32 sizeimage[VIDEO_MAX_PLANES]; |
| 14 |
static unsigned mbus_code; |
| 15 |
static bool enum_all; |
| 16 |
|
| 17 |
void vidcap_usage() |
| 18 |
{ |
| 19 |
printf("\nVideo Capture Formats options:\n" |
| 20 |
" --list-formats [<mbus_code>|all]\n" |
| 21 |
" display supported video formats. <mbus_code> is an optional\n" |
| 22 |
" media bus code, if the device has capability V4L2_CAP_IO_MC\n" |
| 23 |
" then only formats that support this media bus code are listed.\n" |
| 24 |
" When 'all' is specified it enumerates all pixel formats if\n" |
| 25 |
" V4L2_FMTDESC_FLAG_ENUM_ALL flag is supported by the driver.\n" |
| 26 |
" [VIDIOC_ENUM_FMT]\n" |
| 27 |
" --list-formats-ext [<mbus_code>|all]\n" |
| 28 |
" display supported video formats including frame sizes and intervals\n" |
| 29 |
" <mbus_code> is an optional media bus code, if the device has\n" |
| 30 |
" capability V4L2_CAP_IO_MC then only formats that support this\n" |
| 31 |
" media bus code are listed.\n" |
| 32 |
" When 'all' is specified it enumerates all pixel formats if\n" |
| 33 |
" V4L2_FMTDESC_FLAG_ENUM_ALL flag is supported by the driver.\n" |
| 34 |
" [VIDIOC_ENUM_FMT]\n" |
| 35 |
" --list-framesizes <f>\n" |
| 36 |
" list supported framesizes for pixelformat <f>\n" |
| 37 |
" [VIDIOC_ENUM_FRAMESIZES]\n" |
| 38 |
" pixelformat is the fourcc value as a string\n" |
| 39 |
" --list-frameintervals width=<w>,height=<h>,pixelformat=<f>\n" |
| 40 |
" list supported frame intervals for pixelformat <f> and\n" |
| 41 |
" the given width and height [VIDIOC_ENUM_FRAMEINTERVALS]\n" |
| 42 |
" pixelformat is the fourcc value as a string\n" |
| 43 |
" --list-fields list supported fields for the current format\n" |
| 44 |
" -V, --get-fmt-video\n" |
| 45 |
" query the video capture format [VIDIOC_G_FMT]\n" |
| 46 |
" -v, --set-fmt-video\n" |
| 47 |
" --try-fmt-video width=<w>,height=<h>,pixelformat=<pf>,field=<f>,colorspace=<c>,\n" |
| 48 |
" xfer=<xf>,ycbcr=<y>,hsv=<hsv>,quantization=<q>,\n" |
| 49 |
" premul-alpha=<0/1>,bytesperline=<bpl>,sizeimage=<sz>\n" |
| 50 |
" set/try the video capture format [VIDIOC_S/TRY_FMT]\n" |
| 51 |
" pixelformat is either the format index as reported by\n" |
| 52 |
" --list-formats, or the fourcc value as a string.\n" |
| 53 |
" The bytesperline and sizeimage options can be used multiple times,\n" |
| 54 |
" once for each plane.\n" |
| 55 |
" premul-alpha sets (1) or clears (0) V4L2_PIX_FMT_FLAG_PREMUL_ALPHA.\n" |
| 56 |
" <f> can be one of the following field layouts:\n" |
| 57 |
" any, none, top, bottom, interlaced, seq_tb, seq_bt,\n" |
| 58 |
" alternate, interlaced_tb, interlaced_bt\n" |
| 59 |
" <c> can be one of the following colorspaces:\n" |
| 60 |
" smpte170m, smpte240m, rec709, 470m, 470bg, jpeg, srgb,\n" |
| 61 |
" oprgb, bt2020, dcip3\n" |
| 62 |
" <xf> can be one of the following transfer functions:\n" |
| 63 |
" default, 709, srgb, oprgb, smpte240m, smpte2084, dcip3, none\n" |
| 64 |
" <y> can be one of the following Y'CbCr encodings:\n" |
| 65 |
" default, 601, 709, xv601, xv709, bt2020, bt2020c, smpte240m\n" |
| 66 |
" <hsv> can be one of the following HSV encodings:\n" |
| 67 |
" default, 180, 256\n" |
| 68 |
" <q> can be one of the following quantization methods:\n" |
| 69 |
" default, full-range, lim-range\n" |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
static void print_video_fields(int fd) |
| 74 |
{ |
| 75 |
struct v4l2_format fmt; |
| 76 |
struct v4l2_format tmp; |
| 77 |
|
| 78 |
memset(&fmt, 0, sizeof(fmt)); |
| 79 |
fmt.fmt.pix.priv = priv_magic; |
| 80 |
fmt.type = vidcap_buftype; |
| 81 |
if (test_ioctl(fd, VIDIOC_G_FMT, &fmt) < 0) |
| 82 |
return; |
| 83 |
|
| 84 |
printf("Supported Video Fields:\n"); |
| 85 |
for (__u32 f = V4L2_FIELD_NONE; f <= V4L2_FIELD_INTERLACED_BT; f++) { |
| 86 |
bool ok; |
| 87 |
|
| 88 |
tmp = fmt; |
| 89 |
if (is_multiplanar) |
| 90 |
tmp.fmt.pix_mp.field = f; |
| 91 |
else |
| 92 |
tmp.fmt.pix.field = f; |
| 93 |
if (test_ioctl(fd, VIDIOC_TRY_FMT, &tmp) < 0) |
| 94 |
continue; |
| 95 |
if (is_multiplanar) |
| 96 |
ok = tmp.fmt.pix_mp.field == f; |
| 97 |
else |
| 98 |
ok = tmp.fmt.pix.field == f; |
| 99 |
if (ok) |
| 100 |
printf("\t%s\n", field2s(f).c_str()); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void vidcap_cmd(int ch, char *optarg) |
| 105 |
{ |
| 106 |
char *value, *subs; |
| 107 |
bool be_pixfmt; |
| 108 |
|
| 109 |
switch (ch) { |
| 110 |
case OptSetVideoFormat: |
| 111 |
case OptTryVideoFormat: |
| 112 |
set_fmts = parse_fmt(optarg, width, height, pixfmt, field, colorspace, |
| 113 |
xfer_func, ycbcr, quantization, flags, bytesperline, |
| 114 |
sizeimage); |
| 115 |
if (!set_fmts) { |
| 116 |
vidcap_usage(); |
| 117 |
std::exit(EXIT_FAILURE); |
| 118 |
} |
| 119 |
break; |
| 120 |
case OptListFormats: |
| 121 |
case OptListFormatsExt: |
| 122 |
if (optarg) { |
| 123 |
if (strstr(optarg , "all")) |
| 124 |
enum_all = true; |
| 125 |
else |
| 126 |
mbus_code = strtoul(optarg, nullptr, 0); |
| 127 |
} |
| 128 |
break; |
| 129 |
case OptListFrameSizes: |
| 130 |
be_pixfmt = strlen(optarg) == 7 && !memcmp(optarg + 4, "-BE", 3); |
| 131 |
if (be_pixfmt || strlen(optarg) == 4) { |
| 132 |
frmsize.pixel_format = v4l2_fourcc(optarg[0], optarg[1], |
| 133 |
optarg[2], optarg[3]); |
| 134 |
if (be_pixfmt) |
| 135 |
frmsize.pixel_format |= 1U << 31; |
| 136 |
} else if (isdigit(optarg[0])) { |
| 137 |
frmsize.pixel_format = strtoul(optarg, nullptr, 0); |
| 138 |
} else { |
| 139 |
fprintf(stderr, "The pixelformat '%s' is invalid\n", optarg); |
| 140 |
std::exit(EXIT_FAILURE); |
| 141 |
} |
| 142 |
break; |
| 143 |
case OptListFrameIntervals: |
| 144 |
subs = optarg; |
| 145 |
while (*subs != '\0') { |
| 146 |
static constexpr const char *subopts[] = { |
| 147 |
"width", |
| 148 |
"height", |
| 149 |
"pixelformat", |
| 150 |
nullptr |
| 151 |
}; |
| 152 |
|
| 153 |
switch (parse_subopt(&subs, subopts, &value)) { |
| 154 |
case 0: |
| 155 |
frmival.width = strtoul(value, nullptr, 0); |
| 156 |
break; |
| 157 |
case 1: |
| 158 |
frmival.height = strtoul(value, nullptr, 0); |
| 159 |
break; |
| 160 |
case 2: |
| 161 |
be_pixfmt = strlen(value) == 7 && !memcmp(value + 4, "-BE", 3); |
| 162 |
if (be_pixfmt || strlen(value) == 4) { |
| 163 |
frmival.pixel_format = |
| 164 |
v4l2_fourcc(value[0], value[1], |
| 165 |
value[2], value[3]); |
| 166 |
if (be_pixfmt) |
| 167 |
frmival.pixel_format |= 1U << 31; |
| 168 |
} else if (isdigit(optarg[0])) { |
| 169 |
frmival.pixel_format = strtoul(value, nullptr, 0); |
| 170 |
} else { |
| 171 |
fprintf(stderr, "The pixelformat '%s' is invalid\n", optarg); |
| 172 |
std::exit(EXIT_FAILURE); |
| 173 |
} |
| 174 |
break; |
| 175 |
default: |
| 176 |
vidcap_usage(); |
| 177 |
std::exit(EXIT_FAILURE); |
| 178 |
} |
| 179 |
} |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
int vidcap_get_and_update_fmt(cv4l_fd &_fd, struct v4l2_format &vfmt) |
| 185 |
{ |
| 186 |
int fd = _fd.g_fd(); |
| 187 |
int ret; |
| 188 |
|
| 189 |
memset(&vfmt, 0, sizeof(vfmt)); |
| 190 |
vfmt.fmt.pix.priv = priv_magic; |
| 191 |
vfmt.type = vidcap_buftype; |
| 192 |
|
| 193 |
ret = doioctl(fd, VIDIOC_G_FMT, &vfmt); |
| 194 |
if (ret) |
| 195 |
return ret; |
| 196 |
|
| 197 |
if (is_multiplanar) { |
| 198 |
if (set_fmts & FmtWidth) |
| 199 |
vfmt.fmt.pix_mp.width = width; |
| 200 |
if (set_fmts & FmtHeight) |
| 201 |
vfmt.fmt.pix_mp.height = height; |
| 202 |
if (set_fmts & FmtPixelFormat) { |
| 203 |
vfmt.fmt.pix_mp.pixelformat = pixfmt; |
| 204 |
if (vfmt.fmt.pix_mp.pixelformat < 256) { |
| 205 |
vfmt.fmt.pix_mp.pixelformat = pixfmt = |
| 206 |
find_pixel_format(fd, vfmt.fmt.pix_mp.pixelformat, |
| 207 |
false, true); |
| 208 |
} |
| 209 |
} |
| 210 |
if (set_fmts & FmtField) |
| 211 |
vfmt.fmt.pix_mp.field = field; |
| 212 |
if (set_fmts & FmtFlags) |
| 213 |
vfmt.fmt.pix_mp.flags = flags; |
| 214 |
if (set_fmts & FmtBytesPerLine) { |
| 215 |
for (unsigned i = 0; i < VIDEO_MAX_PLANES; i++) |
| 216 |
vfmt.fmt.pix_mp.plane_fmt[i].bytesperline = |
| 217 |
bytesperline[i]; |
| 218 |
} else { |
| 219 |
/* |
| 220 |
* G_FMT might return bytesperline values > width, |
| 221 |
* reset them to 0 to force the driver to update them |
| 222 |
* to the closest value for the new width. |
| 223 |
*/ |
| 224 |
for (unsigned i = 0; i < vfmt.fmt.pix_mp.num_planes; i++) |
| 225 |
vfmt.fmt.pix_mp.plane_fmt[i].bytesperline = 0; |
| 226 |
} |
| 227 |
if (set_fmts & FmtSizeImage) { |
| 228 |
for (unsigned i = 0; i < VIDEO_MAX_PLANES; i++) |
| 229 |
vfmt.fmt.pix_mp.plane_fmt[i].sizeimage = |
| 230 |
sizeimage[i]; |
| 231 |
} |
| 232 |
|
| 233 |
if (set_fmts & FmtColorspace) { |
| 234 |
vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 235 |
vfmt.fmt.pix_mp.colorspace = colorspace; |
| 236 |
} |
| 237 |
if (set_fmts & FmtYCbCr) { |
| 238 |
vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 239 |
vfmt.fmt.pix_mp.ycbcr_enc = ycbcr; |
| 240 |
} |
| 241 |
if (set_fmts & FmtQuantization) { |
| 242 |
vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 243 |
vfmt.fmt.pix_mp.quantization = quantization; |
| 244 |
} |
| 245 |
if (set_fmts & FmtXferFunc) { |
| 246 |
vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 247 |
vfmt.fmt.pix_mp.xfer_func = xfer_func; |
| 248 |
} |
| 249 |
} else { |
| 250 |
if (set_fmts & FmtWidth) |
| 251 |
vfmt.fmt.pix.width = width; |
| 252 |
if (set_fmts & FmtHeight) |
| 253 |
vfmt.fmt.pix.height = height; |
| 254 |
if (set_fmts & FmtPixelFormat) { |
| 255 |
vfmt.fmt.pix.pixelformat = pixfmt; |
| 256 |
if (vfmt.fmt.pix.pixelformat < 256) { |
| 257 |
vfmt.fmt.pix.pixelformat = pixfmt = |
| 258 |
find_pixel_format(fd, vfmt.fmt.pix.pixelformat, |
| 259 |
false, false); |
| 260 |
} |
| 261 |
} |
| 262 |
if (set_fmts & FmtField) |
| 263 |
vfmt.fmt.pix.field = field; |
| 264 |
if (set_fmts & FmtFlags) |
| 265 |
vfmt.fmt.pix.flags = flags; |
| 266 |
if (set_fmts & FmtBytesPerLine) { |
| 267 |
vfmt.fmt.pix.bytesperline = bytesperline[0]; |
| 268 |
} else { |
| 269 |
/* |
| 270 |
* G_FMT might return a bytesperline value > width, |
| 271 |
* reset this to 0 to force the driver to update it |
| 272 |
* to the closest value for the new width. |
| 273 |
*/ |
| 274 |
vfmt.fmt.pix.bytesperline = 0; |
| 275 |
} |
| 276 |
if (set_fmts & FmtSizeImage) |
| 277 |
vfmt.fmt.pix.sizeimage = sizeimage[0]; |
| 278 |
if (set_fmts & FmtColorspace) { |
| 279 |
vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 280 |
vfmt.fmt.pix.colorspace = colorspace; |
| 281 |
} |
| 282 |
if (set_fmts & FmtYCbCr) { |
| 283 |
vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 284 |
vfmt.fmt.pix.ycbcr_enc = ycbcr; |
| 285 |
} |
| 286 |
if (set_fmts & FmtQuantization) { |
| 287 |
vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 288 |
vfmt.fmt.pix.quantization = quantization; |
| 289 |
} |
| 290 |
if (set_fmts & FmtXferFunc) { |
| 291 |
vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; |
| 292 |
vfmt.fmt.pix.xfer_func = xfer_func; |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
if ((set_fmts & FmtPixelFormat) && |
| 298 |
!valid_pixel_format(fd, pixfmt, false, is_multiplanar)) { |
| 299 |
if (pixfmt) |
| 300 |
fprintf(stderr, "The pixelformat '%s' is invalid\n", |
| 301 |
fcc2s(pixfmt).c_str()); |
| 302 |
else |
| 303 |
fprintf(stderr, "The pixelformat index was invalid\n"); |
| 304 |
return -EINVAL; |
| 305 |
} |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
void vidcap_set(cv4l_fd &_fd) |
| 310 |
{ |
| 311 |
if (options[OptSetVideoFormat] || options[OptTryVideoFormat]) { |
| 312 |
int fd = _fd.g_fd(); |
| 313 |
int ret; |
| 314 |
struct v4l2_format vfmt; |
| 315 |
|
| 316 |
if (vidcap_get_and_update_fmt(_fd, vfmt) == 0) { |
| 317 |
if (options[OptSetVideoFormat]) |
| 318 |
ret = doioctl(fd, VIDIOC_S_FMT, &vfmt); |
| 319 |
else |
| 320 |
ret = doioctl(fd, VIDIOC_TRY_FMT, &vfmt); |
| 321 |
if (ret == 0 && (verbose || options[OptTryVideoFormat])) |
| 322 |
printfmt(fd, vfmt); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void vidcap_get(cv4l_fd &fd) |
| 328 |
{ |
| 329 |
if (options[OptGetVideoFormat]) { |
| 330 |
struct v4l2_format vfmt; |
| 331 |
|
| 332 |
memset(&vfmt, 0, sizeof(vfmt)); |
| 333 |
vfmt.fmt.pix.priv = priv_magic; |
| 334 |
vfmt.type = vidcap_buftype; |
| 335 |
if (doioctl(fd.g_fd(), VIDIOC_G_FMT, &vfmt) == 0) |
| 336 |
printfmt(fd.g_fd(), vfmt); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void vidcap_list(cv4l_fd &fd) |
| 341 |
{ |
| 342 |
if (options[OptListFormats]) { |
| 343 |
printf("ioctl: VIDIOC_ENUM_FMT\n"); |
| 344 |
print_video_formats(fd, vidcap_buftype, mbus_code, enum_all); |
| 345 |
} |
| 346 |
|
| 347 |
if (options[OptListFormatsExt]) { |
| 348 |
printf("ioctl: VIDIOC_ENUM_FMT\n"); |
| 349 |
print_video_formats_ext(fd, vidcap_buftype, mbus_code, enum_all); |
| 350 |
} |
| 351 |
|
| 352 |
if (options[OptListFields]) { |
| 353 |
print_video_fields(fd.g_fd()); |
| 354 |
} |
| 355 |
|
| 356 |
if (options[OptListFrameSizes]) { |
| 357 |
frmsize.index = 0; |
| 358 |
if (frmsize.pixel_format < 256) { |
| 359 |
frmsize.pixel_format = |
| 360 |
find_pixel_format(fd.g_fd(), frmsize.pixel_format, |
| 361 |
false, is_multiplanar); |
| 362 |
if (!frmsize.pixel_format) { |
| 363 |
fprintf(stderr, "The pixelformat index was invalid\n"); |
| 364 |
std::exit(EXIT_FAILURE); |
| 365 |
} |
| 366 |
} |
| 367 |
if (!valid_pixel_format(fd.g_fd(), frmsize.pixel_format, false, is_multiplanar) && |
| 368 |
!valid_pixel_format(fd.g_fd(), frmsize.pixel_format, true, is_multiplanar)) { |
| 369 |
fprintf(stderr, "The pixelformat '%s' is invalid\n", |
| 370 |
fcc2s(frmsize.pixel_format).c_str()); |
| 371 |
std::exit(EXIT_FAILURE); |
| 372 |
} |
| 373 |
|
| 374 |
printf("ioctl: VIDIOC_ENUM_FRAMESIZES\n"); |
| 375 |
while (test_ioctl(fd.g_fd(), VIDIOC_ENUM_FRAMESIZES, &frmsize) >= 0) { |
| 376 |
print_frmsize(frmsize, ""); |
| 377 |
frmsize.index++; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if (options[OptListFrameIntervals]) { |
| 382 |
frmival.index = 0; |
| 383 |
if (frmival.pixel_format < 256) { |
| 384 |
frmival.pixel_format = |
| 385 |
find_pixel_format(fd.g_fd(), frmival.pixel_format, |
| 386 |
false, is_multiplanar); |
| 387 |
if (!frmival.pixel_format) { |
| 388 |
fprintf(stderr, "The pixelformat index was invalid\n"); |
| 389 |
std::exit(EXIT_FAILURE); |
| 390 |
} |
| 391 |
} |
| 392 |
if (!valid_pixel_format(fd.g_fd(), frmival.pixel_format, false, is_multiplanar)) { |
| 393 |
fprintf(stderr, "The pixelformat '%s' is invalid\n", |
| 394 |
fcc2s(frmival.pixel_format).c_str()); |
| 395 |
std::exit(EXIT_FAILURE); |
| 396 |
} |
| 397 |
|
| 398 |
printf("ioctl: VIDIOC_ENUM_FRAMEINTERVALS\n"); |
| 399 |
while (test_ioctl(fd.g_fd(), VIDIOC_ENUM_FRAMEINTERVALS, &frmival) >= 0) { |
| 400 |
print_frmival(frmival, ""); |
| 401 |
frmival.index++; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void print_touch_buffer(FILE *f, cv4l_buffer &buf, cv4l_fmt &fmt, cv4l_queue &q) |
| 407 |
{ |
| 408 |
static constexpr char img[16] = { |
| 409 |
'.', ',', ':', ';', '!', '|', 'i', 'c', |
| 410 |
'n', 'o', 'm', 'I', 'C', 'N', 'O', 'M', |
| 411 |
}; |
| 412 |
auto vbuf = static_cast<__s16 *>(q.g_dataptr(buf.g_index(), 0)); |
| 413 |
__u32 x, y; |
| 414 |
|
| 415 |
switch (fmt.g_pixelformat()) { |
| 416 |
case V4L2_TCH_FMT_DELTA_TD16: |
| 417 |
for (y = 0; y < fmt.g_height(); y++) { |
| 418 |
fprintf(f, "TD16: "); |
| 419 |
|
| 420 |
for (x = 0; x < fmt.g_width(); x++, vbuf++) { |
| 421 |
auto v = static_cast<__s16>(le16toh(*vbuf)); |
| 422 |
|
| 423 |
if (!options[OptConcise]) |
| 424 |
fprintf(f, "% 4d", v); |
| 425 |
else if (v > 255) |
| 426 |
fprintf(f, "*"); |
| 427 |
else if (v < -32) |
| 428 |
fprintf(f, "-"); |
| 429 |
else if (v < 0) |
| 430 |
fprintf(f, "%c", img[0]); |
| 431 |
else |
| 432 |
fprintf(f, "%c", img[v / 16]); |
| 433 |
} |
| 434 |
fprintf(f, "\n"); |
| 435 |
} |
| 436 |
break; |
| 437 |
} |
| 438 |
} |