| 1 |
#include <endian.h> |
| 2 |
|
| 3 |
#include "v4l2-ctl.h" |
| 4 |
|
| 5 |
static struct v4l2_format vfmt; /* set_format/get_format */ |
| 6 |
static unsigned mbus_code; |
| 7 |
static unsigned mbus_code_out; |
| 8 |
static bool enum_all; |
| 9 |
static bool enum_all_out; |
| 10 |
|
| 11 |
void meta_usage() |
| 12 |
{ |
| 13 |
printf("\nMetadata Formats options:\n" |
| 14 |
" --list-formats-meta [<mbus_code>|all] display supported metadata capture formats.\n" |
| 15 |
" <mbus_code> is an optional media bus code, if the device has\n" |
| 16 |
" capability V4L2_CAP_IO_MC then only formats that support this\n" |
| 17 |
" media bus code are listed.\n" |
| 18 |
" When 'all' is specified it enumerates all pixel formats if\n" |
| 19 |
" V4L2_FMTDESC_FLAG_ENUM_ALL flag is supported by the driver.\n" |
| 20 |
" [VIDIOC_ENUM_FMT]\n" |
| 21 |
" --get-fmt-meta query the metadata capture format [VIDIOC_G_FMT]\n" |
| 22 |
" --set-fmt-meta <f> set the metadata capture format [VIDIOC_S_FMT]\n" |
| 23 |
" parameter is either the format index as reported by\n" |
| 24 |
" --list-formats-meta, or the fourcc value as a string\n" |
| 25 |
" --try-fmt-meta <f> try the metadata capture format [VIDIOC_TRY_FMT]\n" |
| 26 |
" parameter is either the format index as reported by\n" |
| 27 |
" --list-formats-meta, or the fourcc value as a string\n" |
| 28 |
" --list-formats-meta-out [<mbus_code>] display supported metadata output formats.\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 |
" --get-fmt-meta-out query the metadata output format [VIDIOC_G_FMT]\n" |
| 36 |
" --set-fmt-meta-out <f> set the metadata output format [VIDIOC_S_FMT]\n" |
| 37 |
" parameter is either the format index as reported by\n" |
| 38 |
" --list-formats-meta-out, or the fourcc value as a string\n" |
| 39 |
" --try-fmt-meta-out <f> try the metadata output format [VIDIOC_TRY_FMT]\n" |
| 40 |
" parameter is either the format index as reported by\n" |
| 41 |
" --list-formats-meta-out, or the fourcc value as a string\n" |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
void meta_cmd(int ch, char *optarg) |
| 46 |
{ |
| 47 |
switch (ch) { |
| 48 |
case OptSetMetaFormat: |
| 49 |
case OptTryMetaFormat: |
| 50 |
case OptSetMetaOutFormat: |
| 51 |
case OptTryMetaOutFormat: |
| 52 |
if (strlen(optarg) == 0) { |
| 53 |
meta_usage(); |
| 54 |
std::exit(EXIT_FAILURE); |
| 55 |
} else if (strlen(optarg) == 4) { |
| 56 |
vfmt.fmt.meta.dataformat = v4l2_fourcc(optarg[0], |
| 57 |
optarg[1], optarg[2], optarg[3]); |
| 58 |
} else { |
| 59 |
vfmt.fmt.meta.dataformat = strtoul(optarg, nullptr, 0); |
| 60 |
} |
| 61 |
break; |
| 62 |
case OptListMetaFormats: |
| 63 |
if (optarg) { |
| 64 |
if (strstr(optarg , "all")) |
| 65 |
enum_all = true; |
| 66 |
else |
| 67 |
mbus_code = strtoul(optarg, nullptr, 0); |
| 68 |
} |
| 69 |
break; |
| 70 |
case OptListMetaOutFormats: |
| 71 |
if (optarg) { |
| 72 |
if (strstr(optarg , "all")) |
| 73 |
enum_all_out = true; |
| 74 |
else |
| 75 |
mbus_code_out = strtoul(optarg, nullptr, 0); |
| 76 |
} |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static void __meta_set(cv4l_fd &_fd, bool set, bool _try, __u32 type) |
| 82 |
{ |
| 83 |
struct v4l2_format in_vfmt; |
| 84 |
int fd = _fd.g_fd(); |
| 85 |
int ret; |
| 86 |
|
| 87 |
if (!set && !_try) |
| 88 |
return; |
| 89 |
|
| 90 |
in_vfmt.type = type; |
| 91 |
in_vfmt.fmt.meta.dataformat = vfmt.fmt.meta.dataformat; |
| 92 |
|
| 93 |
if (in_vfmt.fmt.meta.dataformat < 256) { |
| 94 |
struct v4l2_fmtdesc fmt = {}; |
| 95 |
|
| 96 |
fmt.index = in_vfmt.fmt.meta.dataformat; |
| 97 |
fmt.type = in_vfmt.type; |
| 98 |
|
| 99 |
if (doioctl(fd, VIDIOC_ENUM_FMT, &fmt)) |
| 100 |
fmt.pixelformat = 0; |
| 101 |
|
| 102 |
in_vfmt.fmt.meta.dataformat = fmt.pixelformat; |
| 103 |
} |
| 104 |
|
| 105 |
if (set) |
| 106 |
ret = doioctl(fd, VIDIOC_S_FMT, &in_vfmt); |
| 107 |
else |
| 108 |
ret = doioctl(fd, VIDIOC_TRY_FMT, &in_vfmt); |
| 109 |
if (ret == 0 && (verbose || _try)) |
| 110 |
printfmt(fd, in_vfmt); |
| 111 |
} |
| 112 |
|
| 113 |
void meta_set(cv4l_fd &_fd) |
| 114 |
{ |
| 115 |
__meta_set(_fd, options[OptSetMetaFormat], options[OptTryMetaFormat], |
| 116 |
V4L2_BUF_TYPE_META_CAPTURE); |
| 117 |
__meta_set(_fd, options[OptSetMetaOutFormat], |
| 118 |
options[OptTryMetaOutFormat], V4L2_BUF_TYPE_META_OUTPUT); |
| 119 |
} |
| 120 |
|
| 121 |
static void __meta_get(cv4l_fd &fd, __u32 type) |
| 122 |
{ |
| 123 |
vfmt.type = type; |
| 124 |
if (doioctl(fd.g_fd(), VIDIOC_G_FMT, &vfmt) == 0) |
| 125 |
printfmt(fd.g_fd(), vfmt); |
| 126 |
} |
| 127 |
|
| 128 |
void meta_get(cv4l_fd &fd) |
| 129 |
{ |
| 130 |
if (options[OptGetMetaFormat]) |
| 131 |
__meta_get(fd, V4L2_BUF_TYPE_META_CAPTURE); |
| 132 |
if (options[OptGetMetaOutFormat]) |
| 133 |
__meta_get(fd, V4L2_BUF_TYPE_META_OUTPUT); |
| 134 |
} |
| 135 |
|
| 136 |
void meta_list(cv4l_fd &fd) |
| 137 |
{ |
| 138 |
if (options[OptListMetaFormats]) { |
| 139 |
printf("ioctl: VIDIOC_ENUM_FMT\n"); |
| 140 |
print_video_formats(fd, V4L2_BUF_TYPE_META_CAPTURE, mbus_code, enum_all); |
| 141 |
} |
| 142 |
|
| 143 |
if (options[OptListMetaOutFormats]) { |
| 144 |
printf("ioctl: VIDIOC_ENUM_FMT\n"); |
| 145 |
print_video_formats(fd, V4L2_BUF_TYPE_META_OUTPUT, mbus_code_out, enum_all_out); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
struct uvc_meta_buf { |
| 150 |
__u64 ns; |
| 151 |
__u16 sof; |
| 152 |
__u8 length; |
| 153 |
__u8 flags; |
| 154 |
__u8 buf[10]; |
| 155 |
}; |
| 156 |
|
| 157 |
#define UVC_STREAM_SCR (1 << 3) |
| 158 |
#define UVC_STREAM_PTS (1 << 2) |
| 159 |
|
| 160 |
struct vivid_meta_out_buf { |
| 161 |
__u16 brightness; |
| 162 |
__u16 contrast; |
| 163 |
__u16 saturation; |
| 164 |
__s16 hue; |
| 165 |
}; |
| 166 |
|
| 167 |
void print_meta_buffer(FILE *f, cv4l_buffer &buf, cv4l_fmt &fmt, cv4l_queue &q) |
| 168 |
{ |
| 169 |
struct uvc_meta_buf *vbuf; |
| 170 |
int buf_off = 0; |
| 171 |
struct vivid_meta_out_buf *vbuf_out; |
| 172 |
|
| 173 |
switch (fmt.g_pixelformat()) { |
| 174 |
case V4L2_META_FMT_UVC: |
| 175 |
fprintf(f, "UVC: "); |
| 176 |
vbuf = static_cast<uvc_meta_buf *>(q.g_dataptr(buf.g_index(), 0)); |
| 177 |
|
| 178 |
fprintf(f, "%.6fs sof: %4d len: %u flags: 0x%02x", |
| 179 |
static_cast<double>(vbuf->ns) / 1000000000.0, |
| 180 |
vbuf->sof, |
| 181 |
vbuf->length, |
| 182 |
vbuf->flags); |
| 183 |
if (vbuf->flags & UVC_STREAM_PTS) { |
| 184 |
fprintf(f, " PTS: %u", le32toh(*(__u32*)(vbuf->buf))); |
| 185 |
buf_off = 4; |
| 186 |
} |
| 187 |
if (vbuf->flags & UVC_STREAM_SCR) |
| 188 |
fprintf(f, " STC: %u SOF counter: %u", |
| 189 |
le32toh(*(__u32*)(vbuf->buf + buf_off)), |
| 190 |
le16toh(*(__u16*)(vbuf->buf + buf_off + 4))); |
| 191 |
fprintf(f, "\n"); |
| 192 |
break; |
| 193 |
case V4L2_META_FMT_VIVID: |
| 194 |
fprintf(f, "VIVID:"); |
| 195 |
vbuf_out = static_cast<vivid_meta_out_buf *>(q.g_dataptr(buf.g_index(), 0)); |
| 196 |
|
| 197 |
fprintf(f, " brightness: %u contrast: %u saturation: %u hue: %d\n", |
| 198 |
vbuf_out->brightness, vbuf_out->contrast, |
| 199 |
vbuf_out->saturation, vbuf_out->hue); |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
void meta_fillbuffer(cv4l_buffer &buf, cv4l_fmt &fmt, cv4l_queue &q) |
| 205 |
{ |
| 206 |
struct vivid_meta_out_buf *vbuf; |
| 207 |
|
| 208 |
switch (fmt.g_pixelformat()) { |
| 209 |
case V4L2_META_FMT_VIVID: |
| 210 |
vbuf = static_cast<vivid_meta_out_buf *>(q.g_dataptr(buf.g_index(), 0)); |
| 211 |
vbuf->brightness = buf.g_sequence() % 192 + 64; |
| 212 |
vbuf->contrast = (buf.g_sequence() + 10) % 192 + 64; |
| 213 |
vbuf->saturation = (buf.g_sequence() + 20) % 256; |
| 214 |
vbuf->hue = buf.g_sequence() % 257 - 128; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |