selection-api-examples.rst 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: V4L
  3. ********
  4. Examples
  5. ********
  6. (A video capture device is assumed; change
  7. ``V4L2_BUF_TYPE_VIDEO_CAPTURE`` for other devices; change target to
  8. ``V4L2_SEL_TGT_COMPOSE_*`` family to configure composing area)
  9. Example: Resetting the cropping parameters
  10. ==========================================
  11. .. code-block:: c
  12. struct v4l2_selection sel = {
  13. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  14. .target = V4L2_SEL_TGT_CROP_DEFAULT,
  15. };
  16. ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
  17. if (ret)
  18. exit(-1);
  19. sel.target = V4L2_SEL_TGT_CROP;
  20. ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
  21. if (ret)
  22. exit(-1);
  23. Setting a composing area on output of size of *at most* half of limit
  24. placed at a center of a display.
  25. Example: Simple downscaling
  26. ===========================
  27. .. code-block:: c
  28. struct v4l2_selection sel = {
  29. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  30. .target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
  31. };
  32. struct v4l2_rect r;
  33. ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
  34. if (ret)
  35. exit(-1);
  36. /* setting smaller compose rectangle */
  37. r.width = sel.r.width / 2;
  38. r.height = sel.r.height / 2;
  39. r.left = sel.r.width / 4;
  40. r.top = sel.r.height / 4;
  41. sel.r = r;
  42. sel.target = V4L2_SEL_TGT_COMPOSE;
  43. sel.flags = V4L2_SEL_FLAG_LE;
  44. ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
  45. if (ret)
  46. exit(-1);
  47. A video output device is assumed; change ``V4L2_BUF_TYPE_VIDEO_OUTPUT``
  48. for other devices
  49. Example: Querying for scaling factors
  50. =====================================
  51. .. code-block:: c
  52. struct v4l2_selection compose = {
  53. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  54. .target = V4L2_SEL_TGT_COMPOSE,
  55. };
  56. struct v4l2_selection crop = {
  57. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  58. .target = V4L2_SEL_TGT_CROP,
  59. };
  60. double hscale, vscale;
  61. ret = ioctl(fd, VIDIOC_G_SELECTION, &compose);
  62. if (ret)
  63. exit(-1);
  64. ret = ioctl(fd, VIDIOC_G_SELECTION, &crop);
  65. if (ret)
  66. exit(-1);
  67. /* computing scaling factors */
  68. hscale = (double)compose.r.width / crop.r.width;
  69. vscale = (double)compose.r.height / crop.r.height;