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