This is an experimental interface and may change in the future.
The complex nature of V4L2 devices, where hardware is often made of several integrated circuits that need to interact with each other in a controlled way, leads to complex V4L2 drivers. The drivers usually reflect the hardware model in software, and model the different hardware components as software blocks called sub-devices.
V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver implements the media device API, they will automatically inherit from media entities. Applications will be able to enumerate the sub-devices and discover the hardware topology using the media entities, pads and links enumeration API.
In addition to make sub-devices discoverable, drivers can also choose to make them directly configurable by applications. When both the sub-device driver and the V4L2 device driver support this, sub-devices will feature a character device node on which ioctls can be called to
query, read and write sub-devices controls
subscribe and unsubscribe to events and retrieve them
negotiate image formats on individual pads
Sub-device character device nodes, conventionally named
/dev/v4l-subdev*
, use major number 81.
Most V4L2 controls are implemented by sub-device hardware. Drivers usually merge all controls and expose them through video device nodes. Applications can control all sub-devices through a single interface.
Complex devices sometimes implement the same control in different pieces of hardware. This situation is common in embedded platforms, where both sensors and image processing hardware implement identical functions, such as contrast adjustment, white balance or faulty pixels correction. As the V4L2 controls API doesn't support several identical controls in a single device, all but one of the identical controls are hidden.
Applications can access those hidden controls through the sub-device node with the V4L2 control API described in the section called “User Controls”. The ioctls behave identically as when issued on V4L2 device nodes, with the exception that they deal only with controls implemented in the sub-device.
Depending on the driver, those controls might also be exposed through one (or several) V4L2 device nodes.
V4L2 sub-devices can notify applications of events as described in the section called “Event Interface”. The API behaves identically as when used on V4L2 device nodes, with the exception that it only deals with events generated by the sub-device. Depending on the driver, those events might also be reported on one (or several) V4L2 device nodes.
Pad-level formats are only applicable to very complex device that need to expose low-level format configuration to user space. Generic V4L2 applications do not need to use the API described in this section.
For the purpose of this section, the term format means the combination of media bus data format, frame width and frame height.
Image formats are typically negotiated on video capture and output devices using the cropping and scaling ioctls. The driver is responsible for configuring every block in the video pipeline according to the requested format at the pipeline input and/or output.
For complex devices, such as often found in embedded systems, identical image sizes at the output of a pipeline can be achieved using different hardware configurations. One such example is shown on Figure 4.4, “Image Format Negotiation on Pipelines”, where image scaling can be performed on both the video sensor and the host image processing hardware.
The sensor scaler is usually of less quality than the host scaler, but scaling on the sensor is required to achieve higher frame rates. Depending on the use case (quality vs. speed), the pipeline must be configured differently. Applications need to configure the formats at every point in the pipeline explicitly.
Drivers that implement the media
API can expose pad-level image format configuration to applications.
When they do, applications can use the VIDIOC_SUBDEV_G_FMT
and
VIDIOC_SUBDEV_S_FMT
ioctls. to negotiate formats on a per-pad basis.
Applications are responsible for configuring coherent parameters on
the whole pipeline and making sure that connected pads have compatible
formats. The pipeline is checked for formats mismatch at VIDIOC_STREAMON
time, and an EPIPE error code is then returned if the configuration is
invalid.
Pad-level image format configuration support can be tested by calling
the VIDIOC_SUBDEV_G_FMT
ioctl on pad 0. If the driver returns an EINVAL error code
pad-level format configuration is not supported by the sub-device.
Acceptable formats on pads can (and usually do) depend on a number of external parameters, such as formats on other pads, active links, or even controls. Finding a combination of formats on all pads in a video pipeline, acceptable to both application and driver, can't rely on formats enumeration only. A format negotiation mechanism is required.
Central to the format negotiation mechanism are the get/set format
operations. When called with the which
argument
set to V4L2_SUBDEV_FORMAT_TRY
, the
VIDIOC_SUBDEV_G_FMT
and VIDIOC_SUBDEV_S_FMT
ioctls operate on a set of
formats parameters that are not connected to the hardware configuration.
Modifying those 'try' formats leaves the device state untouched (this
applies to both the software state stored in the driver and the hardware
state stored in the device itself).
While not kept as part of the device state, try formats are stored
in the sub-device file handles. A VIDIOC_SUBDEV_G_FMT
call will return
the last try format set on the same sub-device file
handle. Several applications querying the same sub-device at
the same time will thus not interact with each other.
To find out whether a particular format is supported by the device,
applications use the VIDIOC_SUBDEV_S_FMT
ioctl. Drivers verify and, if
needed, change the requested format
based on
device requirements and return the possibly modified value. Applications
can then choose to try a different format or accept the returned value and
continue.
Formats returned by the driver during a negotiation iteration are
guaranteed to be supported by the device. In particular, drivers guarantee
that a returned format will not be further changed if passed to an
VIDIOC_SUBDEV_S_FMT
call as-is (as long as external parameters, such as
formats on other pads or links' configuration are not changed).
Drivers automatically propagate formats inside sub-devices. When a try or active format is set on a pad, corresponding formats on other pads of the same sub-device can be modified by the driver. Drivers are free to modify formats as required by the device. However, they should comply with the following rules when possible:
Formats should be propagated from sink pads to source pads. Modifying a format on a source pad should not modify the format on any sink pad.
Sub-devices that scale frames using variable scaling factors should reset the scale factors to default values when sink pads formats are modified. If the 1:1 scaling ratio is supported, this means that source pads formats should be reset to the sink pads formats.
Formats are not propagated across links, as that would involve propagating them from one sub-device file handle to another. Applications must then take care to configure both ends of every link explicitly with compatible formats. Identical formats on the two ends of a link are guaranteed to be compatible. Drivers are free to accept different formats matching device requirements as being compatible.
Table 4.18, “Sample Pipeline Configuration” shows a sample configuration sequence for the pipeline described in Figure 4.4, “Image Format Negotiation on Pipelines” (table columns list entity names and pad numbers).
Table 4.18. Sample Pipeline Configuration
Sensor/0 | Frontend/0 | Frontend/1 | Scaler/0 | Scaler/1 | |
---|---|---|---|---|---|
Initial state | 2048x1536 | - | - | - | - |
Configure frontend input | 2048x1536 | 2048x1536 | 2046x1534 | - | - |
Configure scaler input | 2048x1536 | 2048x1536 | 2046x1534 | 2046x1534 | 2046x1534 |
Configure scaler output | 2048x1536 | 2048x1536 | 2046x1534 | 2046x1534 | 1280x960 |
Initial state. The sensor output is set to its native 3MP resolution. Resolutions on the host frontend and scaler input and output pads are undefined.
The application configures the frontend input pad resolution to 2048x1536. The driver propagates the format to the frontend output pad. Note that the propagated output format can be different, as in this case, than the input format, as the hardware might need to crop pixels (for instance when converting a Bayer filter pattern to RGB or YUV).
The application configures the scaler input pad resolution to 2046x1534 to match the frontend output resolution. The driver propagates the format to the scaler output pad.
The application configures the scaler output pad resolution to 1280x960.
When satisfied with the try results, applications can set the active
formats by setting the which
argument to
V4L2_SUBDEV_FORMAT_TRY
. Active formats are changed
exactly as try formats by drivers. To avoid modifying the hardware state
during format negotiation, applications should negotiate try formats first
and then modify the active settings using the try formats returned during
the last negotiation iteration. This guarantees that the active format
will be applied as-is by the driver without being modified.
Many sub-devices support cropping frames on their input or output pads (or possible even on both). Cropping is used to select the area of interest in an image, typically on a video sensor or video decoder. It can also be used as part of digital zoom implementations to select the area of the image that will be scaled up.
Crop settings are defined by a crop rectangle and represented in a struct v4l2_rect by the coordinates of the top left corner and the rectangle size. Both the coordinates and sizes are expressed in pixels.
The crop rectangle is retrieved and set using the
VIDIOC_SUBDEV_G_CROP
and VIDIOC_SUBDEV_S_CROP
ioctls. Like for pad
formats, drivers store try and active crop rectangles. The format
negotiation mechanism applies to crop settings as well.
On input pads, cropping is applied relatively to the current pad format. The pad format represents the image size as received by the sub-device from the previous block in the pipeline, and the crop rectangle represents the sub-image that will be transmitted further inside the sub-device for processing. The crop rectangle be entirely containted inside the input image size.
Input crop rectangle are reset to their default value when the input image format is modified. Drivers should use the input image size as the crop rectangle default value, but hardware requirements may prevent this.
Cropping behaviour on output pads is not defined.
Table 4.19. struct v4l2_mbus_framefmt
__u32 | width | Image width, in pixels. |
__u32 | height | Image height, in pixels. |
__u32 | code | Format code, from enum v4l2_mbus_pixelcode. |
__u32 | field | Field order, from enum v4l2_field. See the section called “Field Order” for details. |
__u32 | colorspace | Image colorspace, from enum v4l2_colorspace. See the section called “Colorspaces” for details. |
__u32 | reserved [7] | Reserved for future extensions. Applications and drivers must set the array to zero. |
The media bus pixel codes describe image formats as flowing over physical busses (both between separate physical components and inside SoC devices). This should not be confused with the V4L2 pixel formats that describe, using four character codes, image formats as stored in memory.
While there is a relationship between image formats on busses and image formats in memory (a raw Bayer image won't be magically converted to JPEG just by storing it to memory), there is no one-to-one correspondance between them.
Those formats transfer pixel data as red, green and blue components. The format code is made of the following information.
The red, green and blue components order code, as encoded in a pixel sample. Possible values are RGB and BGR.
The number of bits per component, for each component. The values can be different for all components. Common values are 555 and 565.
The number of bus samples per pixel. Pixels that are wider than the bus width must be transferred in multiple samples. Common values are 1 and 2.
The bus width.
For formats where the total number of bits per pixel is smaller than the number of bus samples per pixel times the bus width, a padding value stating if the bytes are padded in their most high order bits (PADHI) or low order bits (PADLO).
For formats where the number of bus samples per pixel is larger than 1, an endianness value stating if the pixel is transferred MSB first (BE) or LSB first (LE).
For instance, a format where pixels are encoded as 5-bits red, 5-bits
green and 5-bit blue values padded on the high bit, transferred as 2 8-bit
samples per pixel with the most significant bits (padding, red and half of
the green value) transferred first will be named
V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE
.
The following tables list existing packet RGB formats.
Table 4.20. RGB formats
Those formats transfer pixel data as red, green and blue components. The format code is made of the following information.
The red, green and blue components order code, as encoded in a pixel sample. The possible values are shown in Figure 4.5, “Bayer Patterns”.
The number of bits per pixel component. All components are transferred on the same number of bits. Common values are 8, 10 and 12.
If the pixel components are DPCM-compressed, a mention of the DPCM compression and the number of bits per compressed pixel component.
The number of bus samples per pixel. Pixels that are wider than the bus width must be transferred in multiple samples. Common values are 1 and 2.
The bus width.
For formats where the total number of bits per pixel is smaller than the number of bus samples per pixel times the bus width, a padding value stating if the bytes are padded in their most high order bits (PADHI) or low order bits (PADLO).
For formats where the number of bus samples per pixel is larger than 1, an endianness value stating if the pixel is transferred MSB first (BE) or LSB first (LE).
For instance, a format with uncompressed 10-bit Bayer components
arranged in a red, green, green, blue pattern transferred as 2 8-bit
samples per pixel with the least significant bits transferred first will
be named V4L2_MBUS_FMT_SRGGB10_2X8_PADHI_LE
.
The following table lists existing packet Bayer formats. The data organization is given as an example for the first pixel only.
Table 4.21. Bayer Formats
Those data formats transfer pixel data as (possibly downsampled) Y, U and V components. The format code is made of the following information.
The Y, U and V components order code, as transferred on the bus. Possible values are YUYV, UYVY, YVYU and VYUY.
The number of bits per pixel component. All components are transferred on the same number of bits. Common values are 8, 10 and 12.
The number of bus samples per pixel. Pixels that are wider than the bus width must be transferred in multiple samples. Common values are 1, 1.5 (encoded as 1_5) and 2.
The bus width. When the bus width is larger than the number of bits per pixel component, several components are packed in a single bus sample. The components are ordered as specified by the order code, with components on the left of the code transferred in the high order bits. Common values are 8 and 16.
For instance, a format where pixels are encoded as 8-bit YUV values
downsampled to 4:2:2 and transferred as 2 8-bit bus samples per pixel in the
U, Y, V, Y order will be named V4L2_MBUS_FMT_UYVY8_2X8
.
The following table lisst existing packet YUV formats.
Table 4.22. YUV Formats
Those data formats consist of an ordered sequence of 8-bit bytes
obtained from JPEG compression process. Additionally to the
_JPEG
prefix the format code is made of
the following information.
The number of bus samples per entropy encoded byte.
The bus width.
For instance, for a JPEG baseline process and an 8-bit bus width
the format will be named V4L2_MBUS_FMT_JPEG_1X8
.
The following table lists existing JPEG compressed formats.