Debian Codegraph @

utils/v4l2-ctl/v4l2-ctl-overlay.cpp

raw
1 #include <array>
2 #include <vector>
3
4 #include <linux/fb.h>
5
6 #include "v4l2-ctl.h"
7
8 static unsigned int set_fbuf;
9 static unsigned int set_overlay_fmt;
10 static struct v4l2_format overlay_fmt; /* set_format/get_format video overlay */
11 static struct v4l2_framebuffer fbuf; /* fbuf */
12 static int overlay; /* overlay */
13 static const char *fb_device;
14 static std::vector<struct v4l2_clip> clips;
15 static std::vector<struct v4l2_rect> bitmap_rects;
16
17 void overlay_usage()
18 {
19 printf("\nVideo Overlay options:\n"
20 " --list-formats-overlay\n"
21 " display supported overlay formats [VIDIOC_ENUM_FMT]\n"
22 " --find-fb find the fb device corresponding with the overlay\n"
23 " --overlay <on> turn overlay on (1) or off (0) (VIDIOC_OVERLAY)\n"
24 " --get-fmt-overlay query the video or video output overlay format [VIDIOC_G_FMT]\n"
25 " --set-fmt-overlay\n"
26 " --try-fmt-overlay chromakey=<key>,global_alpha=<alpha>,\n"
27 " top=<t>,left=<l>,width=<w>,height=<h>,field=<f>\n"
28 " set/try the video or video output overlay format [VIDIOC_TRY/S_FMT]\n"
29 " <f> can be one of:\n"
30 " any, none, top, bottom, interlaced, seq_tb, seq_bt,\n"
31 " alternate, interlaced_tb, interlaced_bt\n"
32 " If the width or height changed then the old clip list and bitmap will\n"
33 " be invalidated.\n"
34 " --clear-clips clear any old clips, to be used in combination with --try/set-fmt-overlay\n"
35 " --clear-bitmap clear any old bitmap, to be used in combination with --try/set-fmt-overlay\n"
36 " --add-clip top=<t>,left=<l>,width=<w>,height=<h>\n"
37 " Add an entry to the clip list. May be used multiple times.\n"
38 " This clip list will be passed to --try/set-fmt-overlay\n"
39 " --add-bitmap top=<t>,left=<l>,width=<w>,height=<h>\n"
40 " Set the bits in the given rectangle in the bitmap to 1. May be\n"
41 " used multiple times.\n"
42 " The bitmap will be passed to --try/set-fmt-overlay\n"
43 " --get-fbuf query the overlay framebuffer data [VIDIOC_G_FBUF]\n"
44 " --set-fbuf chromakey=<b>,src_chromakey=<b>,global_alpha=<b>,local_alpha=<b>,local_inv_alpha=<b>,fb=<fb>\n"
45 " set the overlay framebuffer [VIDIOC_S_FBUF]\n"
46 " <b> is 0 or 1\n"
47 " <fb> is the framebuffer device (/dev/fbX)\n"
48 " if <fb> starts with a digit, then /dev/fb<fb> is used\n"
49 );
50 }
51
52 static void printfbuf(const struct v4l2_framebuffer &fb)
53 {
54 int is_ext = fb.capability & V4L2_FBUF_CAP_EXTERNOVERLAY;
55
56 printf("Framebuffer Format:\n");
57 printf("\tCapability : %s", fbufcap2s(fb.capability).c_str() + 3);
58 printf("\tFlags : %s", fbufflags2s(fb.flags).c_str() + 3);
59 if (fb.base)
60 printf("\tBase : %p\n", fb.base);
61 printf("\tWidth : %d\n", fb.fmt.width);
62 printf("\tHeight : %d\n", fb.fmt.height);
63 printf("\tPixel Format : '%s'\n", fcc2s(fb.fmt.pixelformat).c_str());
64 if (!is_ext) {
65 printf("\tBytes per Line: %d\n", fb.fmt.bytesperline);
66 printf("\tSize image : %d\n", fb.fmt.sizeimage);
67 printf("\tColorspace : %s\n", colorspace2s(fb.fmt.colorspace).c_str());
68 if (fb.fmt.priv)
69 printf("\tCustom Info : %08x\n", fb.fmt.priv);
70 }
71 }
72
73 static void find_fb(int fd)
74 {
75 struct v4l2_framebuffer fbuf;
76 unsigned int i;
77 int fb_fd;
78
79 if (doioctl(fd, VIDIOC_G_FBUF, &fbuf))
80 return;
81 if (fbuf.base == nullptr) {
82 printf("No framebuffer base address was defined\n");
83 return;
84 }
85
86 for (i = 0; i < 30; i++) {
87 struct fb_fix_screeninfo si;
88 char dev_name[16];
89
90 snprintf(dev_name, sizeof(dev_name), "/dev/fb%u", i);
91
92 fb_fd = open(dev_name, O_RDWR);
93 if (fb_fd == -1) {
94 switch (errno) {
95 case ENOENT: /* no such file */
96 case ENXIO: /* no driver */
97 continue;
98
99 default:
100 return;
101 }
102 }
103
104 if (!ioctl(fb_fd, FBIOGET_FSCREENINFO, &si))
105 if (si.smem_start == (unsigned long)fbuf.base) {
106 printf("%s is the framebuffer associated with base address %p\n",
107 dev_name, fbuf.base);
108 close(fb_fd);
109 return;
110 }
111 close(fb_fd);
112 fb_fd = -1;
113 }
114 printf("No matching framebuffer found for base address %p\n", fbuf.base);
115 }
116
117 struct bitfield2fmt {
118 unsigned red_off, red_len;
119 unsigned green_off, green_len;
120 unsigned blue_off, blue_len;
121 unsigned transp_off, transp_len;
122 __u32 pixfmt;
123 };
124
125 static constexpr std::array<bitfield2fmt, 8> fb_formats{
126 bitfield2fmt{ 10, 5, 5, 5, 0, 5, 15, 1, V4L2_PIX_FMT_ARGB555 },
127 bitfield2fmt{ 11, 5, 5, 6, 0, 5, 0, 0, V4L2_PIX_FMT_RGB565 },
128 bitfield2fmt{ 1, 5, 6, 5, 11, 5, 0, 1, V4L2_PIX_FMT_RGB555X },
129 bitfield2fmt{ 0, 5, 5, 6, 11, 5, 0, 0, V4L2_PIX_FMT_RGB565X },
130 bitfield2fmt{ 16, 8, 8, 8, 0, 8, 0, 0, V4L2_PIX_FMT_BGR24 },
131 bitfield2fmt{ 0, 8, 8, 8, 16, 8, 0, 0, V4L2_PIX_FMT_RGB24 },
132 bitfield2fmt{ 16, 8, 8, 8, 0, 8, 24, 8, V4L2_PIX_FMT_ABGR32 },
133 bitfield2fmt{ 8, 8, 16, 8, 24, 8, 0, 8, V4L2_PIX_FMT_ARGB32 },
134 };
135
136 static bool match_bitfield(const struct fb_bitfield &bf, unsigned off, unsigned len)
137 {
138 if (bf.msb_right || bf.length != len)
139 return false;
140 return !len || bf.offset == off;
141 }
142
143 static int fbuf_fill_from_fb(struct v4l2_framebuffer &fb, const char *fb_device)
144 {
145 struct fb_fix_screeninfo si;
146 struct fb_var_screeninfo vi;
147 int fb_fd;
148
149 if (fb_device == nullptr)
150 return 0;
151 fb_fd = open(fb_device, O_RDWR);
152 if (fb_fd == -1) {
153 fprintf(stderr, "cannot open %s\n", fb_device);
154 return -1;
155 }
156 if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &si)) {
157 fprintf(stderr, "could not obtain fscreeninfo from %s\n", fb_device);
158 close(fb_fd);
159 return -1;
160 }
161 if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &vi)) {
162 fprintf(stderr, "could not obtain vscreeninfo from %s\n", fb_device);
163 close(fb_fd);
164 return -1;
165 }
166 fb.base = (void *)si.smem_start;
167 fb.fmt.sizeimage = si.smem_len;
168 fb.fmt.width = vi.xres;
169 fb.fmt.height = vi.yres;
170 fb.fmt.bytesperline = si.line_length;
171 if (fb.fmt.height * fb.fmt.bytesperline > fb.fmt.sizeimage) {
172 fprintf(stderr, "height * bytesperline > sizeimage?!\n");
173 close(fb_fd);
174 return -1;
175 }
176 fb.fmt.pixelformat = 0;
177 if ((si.capabilities & FB_CAP_FOURCC) && vi.grayscale > 1) {
178 fb.fmt.pixelformat = vi.grayscale;
179 fb.fmt.colorspace = vi.colorspace;
180 } else {
181 if (vi.grayscale == 1) {
182 if (vi.bits_per_pixel == 8)
183 fb.fmt.pixelformat = V4L2_PIX_FMT_GREY;
184 } else {
185 for (const auto &p : fb_formats) {
186 if (match_bitfield(vi.blue, p.blue_off, p.blue_len) &&
187 match_bitfield(vi.green, p.green_off, p.green_len) &&
188 match_bitfield(vi.red, p.red_off, p.red_len) &&
189 match_bitfield(vi.transp, p.transp_off, p.transp_len)) {
190 fb.fmt.pixelformat = p.pixfmt;
191 break;
192 }
193 }
194 }
195 fb.fmt.colorspace = V4L2_COLORSPACE_SRGB;
196 }
197 printfbuf(fb);
198 close(fb_fd);
199 return 0;
200 }
201
202 void overlay_cmd(int ch, char *optarg)
203 {
204 struct v4l2_rect r;
205 char *value, *subs;
206
207 switch (ch) {
208 case OptSetOverlayFormat:
209 case OptTryOverlayFormat:
210 subs = optarg;
211 while (subs && *subs != '\0') {
212 static constexpr const char *subopts[] = {
213 "chromakey",
214 "global_alpha",
215 "left",
216 "top",
217 "width",
218 "height",
219 "field",
220 nullptr
221 };
222
223 switch (parse_subopt(&subs, subopts, &value)) {
224 case 0:
225 overlay_fmt.fmt.win.chromakey = strtoul(value, nullptr, 0);
226 set_overlay_fmt |= FmtChromaKey;
227 break;
228 case 1:
229 overlay_fmt.fmt.win.global_alpha = strtoul(value, nullptr, 0);
230 set_overlay_fmt |= FmtGlobalAlpha;
231 break;
232 case 2:
233 overlay_fmt.fmt.win.w.left = strtol(value, nullptr, 0);
234 set_overlay_fmt |= FmtLeft;
235 break;
236 case 3:
237 overlay_fmt.fmt.win.w.top = strtol(value, nullptr, 0);
238 set_overlay_fmt |= FmtTop;
239 break;
240 case 4:
241 overlay_fmt.fmt.win.w.width = strtoul(value, nullptr, 0);
242 set_overlay_fmt |= FmtWidth;
243 break;
244 case 5:
245 overlay_fmt.fmt.win.w.height = strtoul(value, nullptr, 0);
246 set_overlay_fmt |= FmtHeight;
247 break;
248 case 6:
249 overlay_fmt.fmt.win.field = parse_field(value);
250 set_overlay_fmt |= FmtField;
251 break;
252 default:
253 overlay_usage();
254 break;
255 }
256 }
257 break;
258 case OptAddClip:
259 case OptAddBitmap:
260 subs = optarg;
261 memset(&r, 0, sizeof(r));
262 while (*subs != '\0') {
263 static constexpr const char *subopts[] = {
264 "left",
265 "top",
266 "width",
267 "height",
268 nullptr
269 };
270
271 switch (parse_subopt(&subs, subopts, &value)) {
272 case 0:
273 r.left = strtoul(value, nullptr, 0);
274 break;
275 case 1:
276 r.top = strtoul(value, nullptr, 0);
277 break;
278 case 2:
279 r.width = strtoul(value, nullptr, 0);
280 break;
281 case 3:
282 r.height = strtoul(value, nullptr, 0);
283 break;
284 default:
285 overlay_usage();
286 return;
287 }
288 }
289 if (r.width == 0 || r.height == 0) {
290 overlay_usage();
291 return;
292 }
293 if (ch == OptAddBitmap) {
294 bitmap_rects.push_back(r);
295 } else {
296 struct v4l2_clip c;
297
298 c.c = r;
299 c.next = nullptr;
300 clips.push_back(c);
301 }
302 break;
303 case OptSetFBuf:
304 subs = optarg;
305 while (*subs != '\0') {
306 constexpr unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
307 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
308 constexpr unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
309 V4L2_FBUF_FLAG_LOCAL_ALPHA |
310 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
311 static constexpr const char *subopts[] = {
312 "chromakey",
313 "src_chromakey",
314 "global_alpha",
315 "local_alpha",
316 "local_inv_alpha",
317 "fb",
318 nullptr
319 };
320
321 switch (parse_subopt(&subs, subopts, &value)) {
322 case 0:
323 fbuf.flags &= ~chroma_flags;
324 fbuf.flags |= strtoul(value, nullptr, 0) ? V4L2_FBUF_FLAG_CHROMAKEY : 0;
325 set_fbuf |= chroma_flags;
326 break;
327 case 1:
328 fbuf.flags &= ~chroma_flags;
329 fbuf.flags |= strtoul(value, nullptr, 0) ? V4L2_FBUF_FLAG_SRC_CHROMAKEY : 0;
330 set_fbuf |= chroma_flags;
331 break;
332 case 2:
333 fbuf.flags &= ~alpha_flags;
334 fbuf.flags |= strtoul(value, nullptr, 0) ? V4L2_FBUF_FLAG_GLOBAL_ALPHA : 0;
335 set_fbuf |= alpha_flags;
336 break;
337 case 3:
338 fbuf.flags &= ~alpha_flags;
339 fbuf.flags |= strtoul(value, nullptr, 0) ? V4L2_FBUF_FLAG_LOCAL_ALPHA : 0;
340 set_fbuf |= alpha_flags;
341 break;
342 case 4:
343 fbuf.flags &= ~alpha_flags;
344 fbuf.flags |= strtoul(value, nullptr, 0) ? V4L2_FBUF_FLAG_LOCAL_INV_ALPHA : 0;
345 set_fbuf |= alpha_flags;
346 break;
347 case 5:
348 fb_device = value;
349 if (fb_device[0] >= '0' && fb_device[0] <= '9' && strlen(fb_device) <= 3) {
350 static char newdev[20];
351
352 sprintf(newdev, "/dev/fb%s", fb_device);
353 fb_device = newdev;
354 }
355 break;
356 default:
357 overlay_usage();
358 break;
359 }
360 }
361 break;
362 case OptOverlay:
363 overlay = strtol(optarg, nullptr, 0);
364 break;
365 }
366 }
367
368 static void do_try_set_overlay(struct v4l2_format &fmt, int fd)
369 {
370 struct v4l2_window &win = fmt.fmt.win;
371 bool keep_old_clip = true;
372 bool keep_old_bitmap = true;
373 struct v4l2_clip *cliplist = nullptr;
374 unsigned stride = (win.w.width + 7) / 8;
375 unsigned char *bitmap = nullptr;
376 int ret;
377
378 if (((set_overlay_fmt & FmtWidth) && win.w.width != overlay_fmt.fmt.win.w.width) ||
379 ((set_overlay_fmt & FmtHeight) && win.w.height != overlay_fmt.fmt.win.w.height))
380 keep_old_bitmap = keep_old_clip = false;
381 if (options[OptClearBitmap] || !bitmap_rects.empty())
382 keep_old_bitmap = false;
383 if (options[OptClearClips] || !clips.empty())
384 keep_old_clip = false;
385
386 win.bitmap = nullptr;
387 if (keep_old_bitmap) {
388 bitmap = static_cast<unsigned char *>(calloc(1, stride * win.w.height));
389 win.bitmap = bitmap;
390 }
391 if (keep_old_clip) {
392 if (win.clipcount)
393 cliplist = static_cast<struct v4l2_clip *>(malloc(win.clipcount * sizeof(*cliplist)));
394 win.clips = cliplist;
395 }
396 if (keep_old_clip || keep_old_bitmap)
397 if (doioctl(fd, VIDIOC_G_FMT, &fmt))
398 goto free;
399
400 if (set_overlay_fmt & FmtChromaKey)
401 win.chromakey = overlay_fmt.fmt.win.chromakey;
402 if (set_overlay_fmt & FmtGlobalAlpha)
403 win.global_alpha = overlay_fmt.fmt.win.global_alpha;
404 if (set_overlay_fmt & FmtLeft)
405 win.w.left = overlay_fmt.fmt.win.w.left;
406 if (set_overlay_fmt & FmtTop)
407 win.w.top = overlay_fmt.fmt.win.w.top;
408 if (set_overlay_fmt & FmtWidth)
409 win.w.width = overlay_fmt.fmt.win.w.width;
410 if (set_overlay_fmt & FmtHeight)
411 win.w.height = overlay_fmt.fmt.win.w.height;
412 if (set_overlay_fmt & FmtField)
413 win.field = overlay_fmt.fmt.win.field;
414 if (!clips.empty()) {
415 win.clipcount = clips.size();
416 win.clips = &clips[0];
417 }
418 if (!bitmap_rects.empty()) {
419 free(bitmap);
420 stride = (win.w.width + 7) / 8;
421 bitmap = static_cast<unsigned char *>(calloc(1, stride * win.w.height));
422 win.bitmap = bitmap;
423 for (const auto &r : bitmap_rects) {
424 if (r.left + r.width > win.w.width ||
425 r.top + r.height > win.w.height) {
426 fprintf(stderr, "rectangle is out of range\n");
427 return;
428 }
429 for (unsigned y = r.top; y < r.top + r.height; y++)
430 for (unsigned x = r.left; x < r.left + r.width; x++)
431 bitmap[y * stride + x / 8] |= 1 << (x & 7);
432 }
433 win.bitmap = bitmap;
434 }
435 if (options[OptSetOverlayFormat])
436 ret = doioctl(fd, VIDIOC_S_FMT, &fmt);
437 else
438 ret = doioctl(fd, VIDIOC_TRY_FMT, &fmt);
439 if (ret == 0 && (verbose || options[OptTryOverlayFormat]))
440 printfmt(fd, fmt);
441
442 free:
443 if (bitmap)
444 free(bitmap);
445 if (cliplist)
446 free(cliplist);
447 }
448
449 void overlay_set(cv4l_fd &_fd)
450 {
451 int fd = _fd.g_fd();
452
453 if ((options[OptSetOverlayFormat] || options[OptTryOverlayFormat]) &&
454 (set_overlay_fmt || options[OptClearClips] || options[OptClearBitmap] ||
455 !bitmap_rects.empty() || !clips.empty())) {
456 struct v4l2_format fmt;
457
458 memset(&fmt, 0, sizeof(fmt));
459 // You can never have both VIDEO_OVERLAY and VIDEO_OUTPUT_OVERLAY
460 if (capabilities & V4L2_CAP_VIDEO_OVERLAY)
461 fmt.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
462 else
463 fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
464 if (doioctl(fd, VIDIOC_G_FMT, &fmt) == 0)
465 do_try_set_overlay(fmt, fd);
466 }
467
468 if (options[OptSetFBuf]) {
469 struct v4l2_framebuffer fb;
470
471 if (doioctl(fd, VIDIOC_G_FBUF, &fb) == 0) {
472 fb.flags &= ~set_fbuf;
473 fb.flags |= fbuf.flags;
474 if (!fbuf_fill_from_fb(fb, fb_device))
475 doioctl(fd, VIDIOC_S_FBUF, &fb);
476 }
477 }
478
479 if (options[OptOverlay]) {
480 doioctl(fd, VIDIOC_OVERLAY, &overlay);
481 }
482 }
483
484 void overlay_get(cv4l_fd &_fd)
485 {
486 int fd = _fd.g_fd();
487
488 if (options[OptGetOverlayFormat]) {
489 struct v4l2_format fmt;
490 unsigned char *bitmap = nullptr;
491
492 memset(&fmt, 0, sizeof(fmt));
493 // You can never have both VIDEO_OVERLAY and VIDEO_OUTPUT_OVERLAY
494 if (capabilities & V4L2_CAP_VIDEO_OVERLAY)
495 fmt.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
496 else
497 fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
498 if (doioctl(fd, VIDIOC_G_FMT, &fmt) == 0) {
499 unsigned stride = (fmt.fmt.win.w.width + 7) / 8;
500
501 if (fmt.fmt.win.clipcount)
502 fmt.fmt.win.clips = static_cast<struct v4l2_clip *>(malloc(fmt.fmt.win.clipcount * sizeof(clips[0])));
503 bitmap = static_cast<unsigned char *>(calloc(1, stride * fmt.fmt.win.w.height));
504 fmt.fmt.win.bitmap = bitmap;
505 doioctl(fd, VIDIOC_G_FMT, &fmt);
506 printfmt(fd, fmt);
507 if (fmt.fmt.win.clips)
508 free(fmt.fmt.win.clips);
509 if (bitmap)
510 free(bitmap);
511 }
512 }
513
514 if (options[OptGetFBuf]) {
515 struct v4l2_framebuffer fb;
516 if (doioctl(fd, VIDIOC_G_FBUF, &fb) == 0)
517 printfbuf(fb);
518 }
519 }
520
521 void overlay_list(cv4l_fd &fd)
522 {
523 if (options[OptListOverlayFormats]) {
524 printf("ioctl: VIDIOC_ENUM_FMT\n");
525 print_video_formats(fd, V4L2_BUF_TYPE_VIDEO_OVERLAY, 0, false);
526 }
527 if (options[OptFindFb])
528 find_fb(fd.g_fd());
529 }