dev-overlay.rst 13 KB

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