Also known as Framebuffer Overlay or Previewing.
Video overlay devices have the ability to genlock (TV-)video into the (VGA-)video signal of a graphics card, or to store captured images directly in video memory of a graphics card, typically with clipping. This can be considerable more efficient than capturing images and displaying them by other means. In the old days when only nuclear power plants needed cooling towers this used to be the only way to put live video into a window.
Video overlay devices are accessed through the same character special files as video capture devices.
Note
The default function of a /dev/video device is video capturing. The overlay function is only available after calling the VIDIOC_S_FMT ioctl.
The driver may support simultaneous overlay and capturing using the read/write and streaming I/O methods. If so, operation at the nominal frame rate of the video standard is not guaranteed. Frames may be directed away from overlay to capture, or one field may be used for overlay and the other for capture if the capture parameters permit this.
Applications should use different file descriptors for capturing and overlay. This must be supported by all drivers capable of simultaneous capturing and overlay. Optionally these drivers may also permit capturing and overlay with a single file descriptor for compatibility with V4L and earlier versions of V4L2. [1]
Devices supporting the video overlay interface set the V4L2_CAP_VIDEO_OVERLAY flag in the capabilities field of struct v4l2_capability returned by the ioctl VIDIOC_QUERYCAP ioctl. The overlay I/O method specified below must be supported. Tuners and audio inputs are optional.
Video overlay devices shall support audio input, Tuners and Modulators, controls, cropping and scaling and streaming parameter ioctls as needed. The video input and video standard ioctls must be supported by all video overlay devices.
Before overlay can commence applications must program the driver with frame buffer parameters, namely the address and size of the frame buffer and the image format, for example RGB 5:6:5. The VIDIOC_G_FBUF and VIDIOC_S_FBUF ioctls are available to get and set these parameters, respectively. The VIDIOC_S_FBUF ioctl is privileged because it allows to set up DMA into physical memory, bypassing the memory protection mechanisms of the kernel. Only the superuser can change the frame buffer address and size. Users are not supposed to run TV applications as root or with SUID bit set. A small helper application with suitable privileges should query the graphics system and program the V4L2 driver at the appropriate time.
Some devices add the video overlay to the output signal of the graphics card. In this case the frame buffer is not modified by the video device, and the frame buffer address and pixel format are not needed by the driver. The VIDIOC_S_FBUF ioctl is not privileged. An application can check for this type of device by calling the VIDIOC_G_FBUF ioctl.
A driver may support any (or none) of five clipping/blending methods:
When simultaneous capturing and overlay is supported and the hardware prohibits different image and frame buffer formats, the format requested first takes precedence. The attempt to capture (VIDIOC_S_FMT) or overlay (VIDIOC_S_FBUF) may fail with an EBUSY error code or return accordingly modified parameters..
The overlaid image is determined by cropping and overlay window parameters. The former select an area of the video picture to capture, the latter how images are overlaid and clipped. Cropping initialization at minimum requires to reset the parameters to defaults. An example is given in Image Cropping, Insertion and Scaling.
The overlay window is described by a struct v4l2_window. It defines the size of the image, its position over the graphics surface and the clipping to be applied. To get the current parameters applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OVERLAY and call the VIDIOC_G_FMT ioctl. The driver fills the struct v4l2_window substructure named win. It is not possible to retrieve a previously programmed clipping list or bitmap.
To program the overlay window applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OVERLAY, initialize the win substructure and call the VIDIOC_S_FMT ioctl. The driver adjusts the parameters against hardware limits and returns the actual parameters as VIDIOC_G_FMT does. Like VIDIOC_S_FMT, the VIDIOC_TRY_FMT ioctl can be used to learn about driver capabilities without actually changing driver state. Unlike VIDIOC_S_FMT this also works after the overlay has been enabled.
The scaling factor of the overlaid image is implied by the width and height given in struct v4l2_window and the size of the cropping rectangle. For more information see Image Cropping, Insertion and Scaling.
When simultaneous capturing and overlay is supported and the hardware prohibits different image and window sizes, the size requested first takes precedence. The attempt to capture or overlay as well (VIDIOC_S_FMT) may fail with an EBUSY error code or return accordingly modified parameters.
When chroma-keying has not been negotiated and VIDIOC_G_FBUF indicated this capability, applications can set this field to point to an array of clipping rectangles.
Like the window coordinates w, clipping rectangles are defined relative to the top, left corner of the frame buffer. However clipping rectangles must not extend the frame buffer width and height, and they must not overlap. If possible applications should merge adjacent rectangles. Whether this must create x-y or y-x bands, or the order of rectangles, is not defined. When clip lists are not supported the driver ignores this field. Its contents after calling VIDIOC_S_FMT are undefined.
It must be of the same size as the window, w.width and w.height. Each bit corresponds to a pixel in the overlaid image, which is displayed only when the bit is set. Pixel coordinates translate to bits like:
((__u8 *) bitmap)[w.width * y + x / 8] & (1 << (x & 7))
where 0 ≤ x < w.width and 0 ≤ y <w.height. [2]
When a clipping bit mask is not supported the driver ignores this field, its contents after calling VIDIOC_S_FMT are undefined. When a bit mask is supported but no clipping is desired this field must be set to NULL.
Applications need not create a clip list or bit mask. When they pass both, or despite negotiating chroma-keying, the results are undefined. Regardless of the chosen method, the clipping abilities of the hardware may be limited in quantity or quality. The results when these limits are exceeded are undefined. [3]
Note
This field was added in Linux 2.6.23, extending the structure. However the VIDIOC_[G|S|TRY]_FMT ioctls, which take a pointer to a v4l2_format parent structure with padding bytes at the end, are not affected.
To start or stop the frame buffer overlay applications call the ioctl VIDIOC_OVERLAY ioctl.
[1] | A common application of two file descriptors is the XFree86 Xv/V4L interface driver and a V4L2 application. While the X server controls video overlay, the application can take advantage of memory mapping and DMA. In the opinion of the designers of this API, no driver writer taking the efforts to support simultaneous capturing and overlay will restrict this ability by requiring a single file descriptor, as in V4L and earlier versions of V4L2. Making this optional means applications depending on two file descriptors need backup routines to be compatible with all drivers, which is considerable more work than using two fds in applications which do not. Also two fd’s fit the general concept of one file descriptor for each logical stream. Hence as a complexity trade-off drivers must support two file descriptors and may support single fd operation. |
[2] | Should we require w.width to be a multiple of eight? |
[3] | When the image is written into frame buffer memory it will be undesirable if the driver clips out less pixels than expected, because the application and graphics system are not aware these regions need to be refreshed. The driver should clip out more pixels or not write the image at all. |
[4] | The X Window system defines “regions” which are vectors of struct BoxRec { short x1, y1, x2, y2; } with width = x2 - x1 and height = y2 - y1, so one cannot pass X11 clip lists directly. |