dev-overlay.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: V4L
  3. .. _overlay:
  4. ***********************
  5. Video Overlay Interface
  6. ***********************
  7. **Also known as Framebuffer Overlay or Previewing.**
  8. Video overlay devices have the ability to genlock (TV-)video into the
  9. (VGA-)video signal of a graphics card, or to store captured images
  10. directly in video memory of a graphics card, typically with clipping.
  11. This can be considerable more efficient than capturing images and
  12. displaying them by other means. In the old days when only nuclear power
  13. plants needed cooling towers this used to be the only way to put live
  14. video into a window.
  15. Video overlay devices are accessed through the same character special
  16. files as :ref:`video capture <capture>` devices.
  17. .. note::
  18. The default function of a ``/dev/video`` device is video
  19. capturing. The overlay function is only available after calling
  20. the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl.
  21. The driver may support simultaneous overlay and capturing using the
  22. read/write and streaming I/O methods. If so, operation at the nominal
  23. frame rate of the video standard is not guaranteed. Frames may be
  24. directed away from overlay to capture, or one field may be used for
  25. overlay and the other for capture if the capture parameters permit this.
  26. Applications should use different file descriptors for capturing and
  27. overlay. This must be supported by all drivers capable of simultaneous
  28. capturing and overlay. Optionally these drivers may also permit
  29. capturing and overlay with a single file descriptor for compatibility
  30. with V4L and earlier versions of V4L2. [#f1]_
  31. A common application of two file descriptors is the X11
  32. :ref:`Xv/V4L <xvideo>` interface driver and a V4L2 application.
  33. While the X server controls video overlay, the application can take
  34. advantage of memory mapping and DMA.
  35. Querying Capabilities
  36. =====================
  37. Devices supporting the video overlay interface set the
  38. ``V4L2_CAP_VIDEO_OVERLAY`` flag in the ``capabilities`` field of struct
  39. :c:type:`v4l2_capability` returned by the
  40. :ref:`VIDIOC_QUERYCAP` ioctl. The overlay I/O
  41. method specified below must be supported. Tuners and audio inputs are
  42. optional.
  43. Supplemental Functions
  44. ======================
  45. Video overlay devices shall support :ref:`audio input <audio>`,
  46. :ref:`tuner`, :ref:`controls <control>`,
  47. :ref:`cropping and scaling <crop>` and
  48. :ref:`streaming parameter <streaming-par>` ioctls as needed. The
  49. :ref:`video input <video>` and :ref:`video standard <standard>`
  50. ioctls must be supported by all video overlay devices.
  51. Setup
  52. =====
  53. *Note: support for this has been removed.*
  54. Before overlay can commence applications must program the driver with
  55. frame buffer parameters, namely the address and size of the frame buffer
  56. and the image format, for example RGB 5:6:5. The
  57. :ref:`VIDIOC_G_FBUF <VIDIOC_G_FBUF>` and
  58. :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>` ioctls are available to get and
  59. set these parameters, respectively. The :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>` ioctl is
  60. privileged because it allows to set up DMA into physical memory,
  61. bypassing the memory protection mechanisms of the kernel. Only the
  62. superuser can change the frame buffer address and size. Users are not
  63. supposed to run TV applications as root or with SUID bit set. A small
  64. helper application with suitable privileges should query the graphics
  65. system and program the V4L2 driver at the appropriate time.
  66. Some devices add the video overlay to the output signal of the graphics
  67. card. In this case the frame buffer is not modified by the video device,
  68. and the frame buffer address and pixel format are not needed by the
  69. driver. The :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>` ioctl is not privileged. An application
  70. can check for this type of device by calling the :ref:`VIDIOC_G_FBUF <VIDIOC_G_FBUF>`
  71. ioctl.
  72. A driver may support any (or none) of five clipping/blending methods:
  73. 1. Chroma-keying displays the overlaid image only where pixels in the
  74. primary graphics surface assume a certain color.
  75. 2. *Note: support for this has been removed.*
  76. A bitmap can be specified where each bit corresponds to a pixel in
  77. the overlaid image. When the bit is set, the corresponding video
  78. pixel is displayed, otherwise a pixel of the graphics surface.
  79. 3. *Note: support for this has been removed.*
  80. A list of clipping rectangles can be specified. In these regions *no*
  81. video is displayed, so the graphics surface can be seen here.
  82. 4. The framebuffer has an alpha channel that can be used to clip or
  83. blend the framebuffer with the video.
  84. 5. A global alpha value can be specified to blend the framebuffer
  85. contents with video images.
  86. When simultaneous capturing and overlay is supported and the hardware
  87. prohibits different image and frame buffer formats, the format requested
  88. first takes precedence. The attempt to capture
  89. (:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`) or overlay
  90. (:ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>`) may fail with an ``EBUSY`` error
  91. code or return accordingly modified parameters..
  92. Overlay Window
  93. ==============
  94. The overlaid image is determined by cropping and overlay window
  95. parameters. The former select an area of the video picture to capture,
  96. the latter how images are overlaid and clipped. Cropping initialization
  97. at minimum requires to reset the parameters to defaults. An example is
  98. given in :ref:`crop`.
  99. The overlay window is described by a struct
  100. :c:type:`v4l2_window`. It defines the size of the image,
  101. its position over the graphics surface and the clipping to be applied.
  102. To get the current parameters applications set the ``type`` field of a
  103. struct :c:type:`v4l2_format` to
  104. ``V4L2_BUF_TYPE_VIDEO_OVERLAY`` and call the
  105. :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` ioctl. The driver fills the
  106. struct :c:type:`v4l2_window` substructure named ``win``. It is not
  107. possible to retrieve a previously programmed clipping list or bitmap.
  108. To program the overlay window applications set the ``type`` field of a
  109. struct :c:type:`v4l2_format` to
  110. ``V4L2_BUF_TYPE_VIDEO_OVERLAY``, initialize the ``win`` substructure and
  111. call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl. The driver
  112. adjusts the parameters against hardware limits and returns the actual
  113. parameters as :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` does. Like :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`, the
  114. :ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` ioctl can be used to learn
  115. about driver capabilities without actually changing driver state. Unlike
  116. :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` this also works after the overlay has been enabled.
  117. The scaling factor of the overlaid image is implied by the width and
  118. height given in struct :c:type:`v4l2_window` and the size
  119. of the cropping rectangle. For more information see :ref:`crop`.
  120. When simultaneous capturing and overlay is supported and the hardware
  121. prohibits different image and window sizes, the size requested first
  122. takes precedence. The attempt to capture or overlay as well
  123. (:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`) may fail with an ``EBUSY`` error
  124. code or return accordingly modified parameters.
  125. .. c:type:: v4l2_window
  126. struct v4l2_window
  127. ------------------
  128. ``struct v4l2_rect w``
  129. Size and position of the window relative to the top, left corner of
  130. the frame buffer defined with
  131. :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>`. The window can extend the
  132. frame buffer width and height, the ``x`` and ``y`` coordinates can
  133. be negative, and it can lie completely outside the frame buffer. The
  134. driver clips the window accordingly, or if that is not possible,
  135. modifies its size and/or position.
  136. ``enum v4l2_field field``
  137. Applications set this field to determine which video field shall be
  138. overlaid, typically one of ``V4L2_FIELD_ANY`` (0),
  139. ``V4L2_FIELD_TOP``, ``V4L2_FIELD_BOTTOM`` or
  140. ``V4L2_FIELD_INTERLACED``. Drivers may have to choose a different
  141. field order and return the actual setting here.
  142. ``__u32 chromakey``
  143. When chroma-keying has been negotiated with
  144. :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>` applications set this field
  145. to the desired pixel value for the chroma key. The format is the
  146. same as the pixel format of the framebuffer (struct
  147. :c:type:`v4l2_framebuffer` ``fmt.pixelformat``
  148. field), with bytes in host order. E. g. for
  149. :ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR32>` the value should
  150. be 0xRRGGBB on a little endian, 0xBBGGRR on a big endian host.
  151. ``struct v4l2_clip * clips``
  152. *Note: support for this has been removed.*
  153. When chroma-keying has *not* been negotiated and
  154. :ref:`VIDIOC_G_FBUF <VIDIOC_G_FBUF>` indicated this capability,
  155. applications can set this field to point to an array of clipping
  156. rectangles.
  157. Like the window coordinates w, clipping rectangles are defined
  158. relative to the top, left corner of the frame buffer. However
  159. clipping rectangles must not extend the frame buffer width and
  160. height, and they must not overlap. If possible applications
  161. should merge adjacent rectangles. Whether this must create
  162. x-y or y-x bands, or the order of rectangles, is not defined. When
  163. clip lists are not supported the driver ignores this field. Its
  164. contents after calling :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
  165. are undefined.
  166. ``__u32 clipcount``
  167. *Note: support for this has been removed.*
  168. When the application set the ``clips`` field, this field must
  169. contain the number of clipping rectangles in the list. When clip
  170. lists are not supported the driver ignores this field, its contents
  171. after calling :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` are undefined. When clip lists are
  172. supported but no clipping is desired this field must be set to zero.
  173. ``void * bitmap``
  174. *Note: support for this has been removed.*
  175. When chroma-keying has *not* been negotiated and
  176. :ref:`VIDIOC_G_FBUF <VIDIOC_G_FBUF>` indicated this capability,
  177. applications can set this field to point to a clipping bit mask.
  178. It must be of the same size as the window, ``w.width`` and ``w.height``.
  179. Each bit corresponds to a pixel in the overlaid image, which is
  180. displayed only when the bit is *set*. Pixel coordinates translate to
  181. bits like:
  182. .. code-block:: c
  183. ((__u8 *) bitmap)[w.width * y + x / 8] & (1 << (x & 7))
  184. where ``0`` ≤ x < ``w.width`` and ``0`` ≤ y <``w.height``. [#f2]_
  185. When a clipping bit mask is not supported the driver ignores this field,
  186. its contents after calling :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` are
  187. undefined. When a bit mask is supported but no clipping is desired this
  188. field must be set to ``NULL``.
  189. Applications need not create a clip list or bit mask. When they pass
  190. both, or despite negotiating chroma-keying, the results are undefined.
  191. Regardless of the chosen method, the clipping abilities of the hardware
  192. may be limited in quantity or quality. The results when these limits are
  193. exceeded are undefined. [#f3]_
  194. ``__u8 global_alpha``
  195. The global alpha value used to blend the framebuffer with video
  196. images, if global alpha blending has been negotiated
  197. (``V4L2_FBUF_FLAG_GLOBAL_ALPHA``, see
  198. :ref:`VIDIOC_S_FBUF <VIDIOC_G_FBUF>`,
  199. :ref:`framebuffer-flags`).
  200. .. note::
  201. This field was added in Linux 2.6.23, extending the
  202. structure. However the :ref:`VIDIOC_[G|S|TRY]_FMT <VIDIOC_G_FMT>`
  203. ioctls, which take a pointer to a :c:type:`v4l2_format`
  204. parent structure with padding bytes at the end, are not affected.
  205. .. c:type:: v4l2_clip
  206. struct v4l2_clip [#f4]_
  207. -----------------------
  208. ``struct v4l2_rect c``
  209. Coordinates of the clipping rectangle, relative to the top, left
  210. corner of the frame buffer. Only window pixels *outside* all
  211. clipping rectangles are displayed.
  212. ``struct v4l2_clip * next``
  213. Pointer to the next clipping rectangle, ``NULL`` when this is the last
  214. rectangle. Drivers ignore this field, it cannot be used to pass a
  215. linked list of clipping rectangles.
  216. .. c:type:: v4l2_rect
  217. struct v4l2_rect
  218. ----------------
  219. ``__s32 left``
  220. Horizontal offset of the top, left corner of the rectangle, in
  221. pixels.
  222. ``__s32 top``
  223. Vertical offset of the top, left corner of the rectangle, in pixels.
  224. Offsets increase to the right and down.
  225. ``__u32 width``
  226. Width of the rectangle, in pixels.
  227. ``__u32 height``
  228. Height of the rectangle, in pixels.
  229. Enabling Overlay
  230. ================
  231. To start or stop the frame buffer overlay applications call the
  232. :ref:`VIDIOC_OVERLAY` ioctl.
  233. .. [#f1]
  234. In the opinion of the designers of this API, no driver writer taking
  235. the efforts to support simultaneous capturing and overlay will
  236. restrict this ability by requiring a single file descriptor, as in
  237. V4L and earlier versions of V4L2. Making this optional means
  238. applications depending on two file descriptors need backup routines
  239. to be compatible with all drivers, which is considerable more work
  240. than using two fds in applications which do not. Also two fd's fit
  241. the general concept of one file descriptor for each logical stream.
  242. Hence as a complexity trade-off drivers *must* support two file
  243. descriptors and *may* support single fd operation.
  244. .. [#f2]
  245. Should we require ``w.width`` to be a multiple of eight?
  246. .. [#f3]
  247. When the image is written into frame buffer memory it will be
  248. undesirable if the driver clips out less pixels than expected,
  249. because the application and graphics system are not aware these
  250. regions need to be refreshed. The driver should clip out more pixels
  251. or not write the image at all.
  252. .. [#f4]
  253. The X Window system defines "regions" which are vectors of ``struct
  254. BoxRec { short x1, y1, x2, y2; }`` with ``width = x2 - x1`` and
  255. ``height = y2 - y1``, so one cannot pass X11 clip lists directly.