1 <title>Sub-device Interface</title>
4 <title>Experimental</title>
5 <para>This is an <link linkend="experimental">experimental</link>
6 interface and may change in the future.</para>
9 <para>The complex nature of V4L2 devices, where hardware is often made of
10 several integrated circuits that need to interact with each other in a
11 controlled way, leads to complex V4L2 drivers. The drivers usually reflect
12 the hardware model in software, and model the different hardware components
13 as software blocks called sub-devices.</para>
15 <para>V4L2 sub-devices are usually kernel-only objects. If the V4L2 driver
16 implements the media device API, they will automatically inherit from media
17 entities. Applications will be able to enumerate the sub-devices and discover
18 the hardware topology using the media entities, pads and links enumeration
21 <para>In addition to make sub-devices discoverable, drivers can also choose
22 to make them directly configurable by applications. When both the sub-device
23 driver and the V4L2 device driver support this, sub-devices will feature a
24 character device node on which ioctls can be called to
26 <listitem><para>query, read and write sub-devices controls</para></listitem>
27 <listitem><para>subscribe and unsubscribe to events and retrieve them</para></listitem>
28 <listitem><para>negotiate image formats on individual pads</para></listitem>
32 <para>Sub-device character device nodes, conventionally named
33 <filename>/dev/v4l-subdev*</filename>, use major number 81.</para>
36 <title>Controls</title>
37 <para>Most V4L2 controls are implemented by sub-device hardware. Drivers
38 usually merge all controls and expose them through video device nodes.
39 Applications can control all sub-devices through a single interface.</para>
41 <para>Complex devices sometimes implement the same control in different
42 pieces of hardware. This situation is common in embedded platforms, where
43 both sensors and image processing hardware implement identical functions,
44 such as contrast adjustment, white balance or faulty pixels correction. As
45 the V4L2 controls API doesn't support several identical controls in a single
46 device, all but one of the identical controls are hidden.</para>
48 <para>Applications can access those hidden controls through the sub-device
49 node with the V4L2 control API described in <xref linkend="control" />. The
50 ioctls behave identically as when issued on V4L2 device nodes, with the
51 exception that they deal only with controls implemented in the sub-device.
54 <para>Depending on the driver, those controls might also be exposed through
55 one (or several) V4L2 device nodes.</para>
60 <para>V4L2 sub-devices can notify applications of events as described in
61 <xref linkend="event" />. The API behaves identically as when used on V4L2
62 device nodes, with the exception that it only deals with events generated by
63 the sub-device. Depending on the driver, those events might also be reported
64 on one (or several) V4L2 device nodes.</para>
67 <section id="pad-level-formats">
68 <title>Pad-level Formats</title>
70 <warning><para>Pad-level formats are only applicable to very complex device that
71 need to expose low-level format configuration to user space. Generic V4L2
72 applications do <emphasis>not</emphasis> need to use the API described in
73 this section.</para></warning>
75 <note><para>For the purpose of this section, the term
76 <wordasword>format</wordasword> means the combination of media bus data
77 format, frame width and frame height.</para></note>
79 <para>Image formats are typically negotiated on video capture and
80 output devices using the format and <link
81 linkend="vidioc-subdev-g-selection">selection</link> ioctls. The
82 driver is responsible for configuring every block in the video
83 pipeline according to the requested format at the pipeline input
86 <para>For complex devices, such as often found in embedded systems,
87 identical image sizes at the output of a pipeline can be achieved using
88 different hardware configurations. One such example is shown on
89 <xref linkend="pipeline-scaling" />, where
90 image scaling can be performed on both the video sensor and the host image
91 processing hardware.</para>
93 <figure id="pipeline-scaling">
94 <title>Image Format Negotiation on Pipelines</title>
97 <imagedata fileref="pipeline.pdf" format="PS" />
100 <imagedata fileref="pipeline.png" format="PNG" />
103 <phrase>High quality and high speed pipeline configuration</phrase>
108 <para>The sensor scaler is usually of less quality than the host scaler, but
109 scaling on the sensor is required to achieve higher frame rates. Depending
110 on the use case (quality vs. speed), the pipeline must be configured
111 differently. Applications need to configure the formats at every point in
112 the pipeline explicitly.</para>
114 <para>Drivers that implement the <link linkend="media-controller-intro">media
115 API</link> can expose pad-level image format configuration to applications.
116 When they do, applications can use the &VIDIOC-SUBDEV-G-FMT; and
117 &VIDIOC-SUBDEV-S-FMT; ioctls. to negotiate formats on a per-pad basis.</para>
119 <para>Applications are responsible for configuring coherent parameters on
120 the whole pipeline and making sure that connected pads have compatible
121 formats. The pipeline is checked for formats mismatch at &VIDIOC-STREAMON;
122 time, and an &EPIPE; is then returned if the configuration is
125 <para>Pad-level image format configuration support can be tested by calling
126 the &VIDIOC-SUBDEV-G-FMT; ioctl on pad 0. If the driver returns an &EINVAL;
127 pad-level format configuration is not supported by the sub-device.</para>
130 <title>Format Negotiation</title>
132 <para>Acceptable formats on pads can (and usually do) depend on a number
133 of external parameters, such as formats on other pads, active links, or
134 even controls. Finding a combination of formats on all pads in a video
135 pipeline, acceptable to both application and driver, can't rely on formats
136 enumeration only. A format negotiation mechanism is required.</para>
138 <para>Central to the format negotiation mechanism are the get/set format
139 operations. When called with the <structfield>which</structfield> argument
140 set to <constant>V4L2_SUBDEV_FORMAT_TRY</constant>, the
141 &VIDIOC-SUBDEV-G-FMT; and &VIDIOC-SUBDEV-S-FMT; ioctls operate on a set of
142 formats parameters that are not connected to the hardware configuration.
143 Modifying those 'try' formats leaves the device state untouched (this
144 applies to both the software state stored in the driver and the hardware
145 state stored in the device itself).</para>
147 <para>While not kept as part of the device state, try formats are stored
148 in the sub-device file handles. A &VIDIOC-SUBDEV-G-FMT; call will return
149 the last try format set <emphasis>on the same sub-device file
150 handle</emphasis>. Several applications querying the same sub-device at
151 the same time will thus not interact with each other.</para>
153 <para>To find out whether a particular format is supported by the device,
154 applications use the &VIDIOC-SUBDEV-S-FMT; ioctl. Drivers verify and, if
155 needed, change the requested <structfield>format</structfield> based on
156 device requirements and return the possibly modified value. Applications
157 can then choose to try a different format or accept the returned value and
160 <para>Formats returned by the driver during a negotiation iteration are
161 guaranteed to be supported by the device. In particular, drivers guarantee
162 that a returned format will not be further changed if passed to an
163 &VIDIOC-SUBDEV-S-FMT; call as-is (as long as external parameters, such as
164 formats on other pads or links' configuration are not changed).</para>
166 <para>Drivers automatically propagate formats inside sub-devices. When a
167 try or active format is set on a pad, corresponding formats on other pads
168 of the same sub-device can be modified by the driver. Drivers are free to
169 modify formats as required by the device. However, they should comply with
170 the following rules when possible:
172 <listitem><para>Formats should be propagated from sink pads to source pads.
173 Modifying a format on a source pad should not modify the format on any
174 sink pad.</para></listitem>
175 <listitem><para>Sub-devices that scale frames using variable scaling factors
176 should reset the scale factors to default values when sink pads formats
177 are modified. If the 1:1 scaling ratio is supported, this means that
178 source pads formats should be reset to the sink pads formats.</para></listitem>
182 <para>Formats are not propagated across links, as that would involve
183 propagating them from one sub-device file handle to another. Applications
184 must then take care to configure both ends of every link explicitly with
185 compatible formats. Identical formats on the two ends of a link are
186 guaranteed to be compatible. Drivers are free to accept different formats
187 matching device requirements as being compatible.</para>
189 <para><xref linkend="sample-pipeline-config" />
190 shows a sample configuration sequence for the pipeline described in
191 <xref linkend="pipeline-scaling" /> (table
192 columns list entity names and pad numbers).</para>
194 <table pgwide="0" frame="none" id="sample-pipeline-config">
195 <title>Sample Pipeline Configuration</title>
197 <colspec colname="what"/>
198 <colspec colname="sensor-0 format" />
199 <colspec colname="frontend-0 format" />
200 <colspec colname="frontend-1 format" />
201 <colspec colname="scaler-0 format" />
202 <colspec colname="scaler-0 compose" />
203 <colspec colname="scaler-1 format" />
207 <entry>Sensor/0 format</entry>
208 <entry>Frontend/0 format</entry>
209 <entry>Frontend/1 format</entry>
210 <entry>Scaler/0 format</entry>
211 <entry>Scaler/0 compose selection rectangle</entry>
212 <entry>Scaler/1 format</entry>
217 <entry>Initial state</entry>
218 <entry>2048x1536/SGRBG8_1X8</entry>
219 <entry>(default)</entry>
220 <entry>(default)</entry>
221 <entry>(default)</entry>
222 <entry>(default)</entry>
223 <entry>(default)</entry>
226 <entry>Configure frontend sink format</entry>
227 <entry>2048x1536/SGRBG8_1X8</entry>
228 <entry><emphasis>2048x1536/SGRBG8_1X8</emphasis></entry>
229 <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
230 <entry>(default)</entry>
231 <entry>(default)</entry>
232 <entry>(default)</entry>
235 <entry>Configure scaler sink format</entry>
236 <entry>2048x1536/SGRBG8_1X8</entry>
237 <entry>2048x1536/SGRBG8_1X8</entry>
238 <entry>2046x1534/SGRBG8_1X8</entry>
239 <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
240 <entry><emphasis>0,0/2046x1534</emphasis></entry>
241 <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
244 <entry>Configure scaler sink compose selection</entry>
245 <entry>2048x1536/SGRBG8_1X8</entry>
246 <entry>2048x1536/SGRBG8_1X8</entry>
247 <entry>2046x1534/SGRBG8_1X8</entry>
248 <entry>2046x1534/SGRBG8_1X8</entry>
249 <entry><emphasis>0,0/1280x960</emphasis></entry>
250 <entry><emphasis>1280x960/SGRBG8_1X8</emphasis></entry>
258 <listitem><para>Initial state. The sensor source pad format is
259 set to its native 3MP size and V4L2_MBUS_FMT_SGRBG8_1X8
260 media bus code. Formats on the host frontend and scaler sink
261 and source pads have the default values, as well as the
262 compose rectangle on the scaler's sink pad.</para></listitem>
264 <listitem><para>The application configures the frontend sink
265 pad format's size to 2048x1536 and its media bus code to
266 V4L2_MBUS_FMT_SGRBG_1X8. The driver propagates the format to
267 the frontend source pad.</para></listitem>
269 <listitem><para>The application configures the scaler sink pad
270 format's size to 2046x1534 and the media bus code to
271 V4L2_MBUS_FMT_SGRBG_1X8 to match the frontend source size and
272 media bus code. The media bus code on the sink pad is set to
273 V4L2_MBUS_FMT_SGRBG_1X8. The driver propagates the size to the
274 compose selection rectangle on the scaler's sink pad, and the
275 format to the scaler source pad.</para></listitem>
277 <listitem><para>The application configures the size of the compose
278 selection rectangle of the scaler's sink pad 1280x960. The driver
279 propagates the size to the scaler's source pad
280 format.</para></listitem>
285 <para>When satisfied with the try results, applications can set the active
286 formats by setting the <structfield>which</structfield> argument to
287 <constant>V4L2_SUBDEV_FORMAT_ACTIVE</constant>. Active formats are changed
288 exactly as try formats by drivers. To avoid modifying the hardware state
289 during format negotiation, applications should negotiate try formats first
290 and then modify the active settings using the try formats returned during
291 the last negotiation iteration. This guarantees that the active format
292 will be applied as-is by the driver without being modified.
296 <section id="v4l2-subdev-selections">
297 <title>Selections: cropping, scaling and composition</title>
299 <para>Many sub-devices support cropping frames on their input or output
300 pads (or possible even on both). Cropping is used to select the area of
301 interest in an image, typically on an image sensor or a video decoder. It can
302 also be used as part of digital zoom implementations to select the area of
303 the image that will be scaled up.</para>
305 <para>Crop settings are defined by a crop rectangle and represented in a
306 &v4l2-rect; by the coordinates of the top left corner and the rectangle
307 size. Both the coordinates and sizes are expressed in pixels.</para>
309 <para>As for pad formats, drivers store try and active
310 rectangles for the selection targets <xref
311 linkend="v4l2-selections-common" />.</para>
313 <para>On sink pads, cropping is applied relative to the
314 current pad format. The pad format represents the image size as
315 received by the sub-device from the previous block in the
316 pipeline, and the crop rectangle represents the sub-image that
317 will be transmitted further inside the sub-device for
320 <para>The scaling operation changes the size of the image by
321 scaling it to new dimensions. The scaling ratio isn't specified
322 explicitly, but is implied from the original and scaled image
323 sizes. Both sizes are represented by &v4l2-rect;.</para>
325 <para>Scaling support is optional. When supported by a subdev,
326 the crop rectangle on the subdev's sink pad is scaled to the
327 size configured using the &VIDIOC-SUBDEV-S-SELECTION; IOCTL
328 using <constant>V4L2_SEL_TGT_COMPOSE</constant>
329 selection target on the same pad. If the subdev supports scaling
330 but not composing, the top and left values are not used and must
331 always be set to zero.</para>
333 <para>On source pads, cropping is similar to sink pads, with the
334 exception that the source size from which the cropping is
335 performed, is the COMPOSE rectangle on the sink pad. In both
336 sink and source pads, the crop rectangle must be entirely
337 contained inside the source image size for the crop
340 <para>The drivers should always use the closest possible
341 rectangle the user requests on all selection targets, unless
342 specifically told otherwise.
343 <constant>V4L2_SEL_FLAG_GE</constant> and
344 <constant>V4L2_SEL_FLAG_LE</constant> flags may be
345 used to round the image size either up or down. <xref
346 linkend="v4l2-selection-flags" /></para>
350 <title>Types of selection targets</title>
353 <title>Actual targets</title>
355 <para>Actual targets (without a postfix) reflect the actual
356 hardware configuration at any point of time. There is a BOUNDS
357 target corresponding to every actual target.</para>
361 <title>BOUNDS targets</title>
363 <para>BOUNDS targets is the smallest rectangle that contains all
364 valid actual rectangles. It may not be possible to set the actual
365 rectangle as large as the BOUNDS rectangle, however. This may be
366 because e.g. a sensor's pixel array is not rectangular but
367 cross-shaped or round. The maximum size may also be smaller than the
368 BOUNDS rectangle.</para>
374 <title>Order of configuration and format propagation</title>
376 <para>Inside subdevs, the order of image processing steps will
377 always be from the sink pad towards the source pad. This is also
378 reflected in the order in which the configuration must be
379 performed by the user: the changes made will be propagated to
380 any subsequent stages. If this behaviour is not desired, the
382 <constant>V4L2_SEL_FLAG_KEEP_CONFIG</constant> flag. This
383 flag causes no propagation of the changes are allowed in any
384 circumstances. This may also cause the accessed rectangle to be
385 adjusted by the driver, depending on the properties of the
386 underlying hardware.</para>
388 <para>The coordinates to a step always refer to the actual size
389 of the previous step. The exception to this rule is the source
390 compose rectangle, which refers to the sink compose bounds
391 rectangle --- if it is supported by the hardware.</para>
394 <listitem><para>Sink pad format. The user configures the sink pad
395 format. This format defines the parameters of the image the
396 entity receives through the pad for further processing.</para></listitem>
398 <listitem><para>Sink pad actual crop selection. The sink pad crop
399 defines the crop performed to the sink pad format.</para></listitem>
401 <listitem><para>Sink pad actual compose selection. The size of the
402 sink pad compose rectangle defines the scaling ratio compared
403 to the size of the sink pad crop rectangle. The location of
404 the compose rectangle specifies the location of the actual
405 sink compose rectangle in the sink compose bounds
406 rectangle.</para></listitem>
408 <listitem><para>Source pad actual crop selection. Crop on the source
409 pad defines crop performed to the image in the sink compose
410 bounds rectangle.</para></listitem>
412 <listitem><para>Source pad format. The source pad format defines the
413 output pixel format of the subdev, as well as the other
414 parameters with the exception of the image width and height.
415 Width and height are defined by the size of the source pad
416 actual crop selection.</para></listitem>
419 <para>Accessing any of the above rectangles not supported by the
420 subdev will return <constant>EINVAL</constant>. Any rectangle
421 referring to a previous unsupported rectangle coordinates will
422 instead refer to the previous supported rectangle. For example,
423 if sink crop is not supported, the compose selection will refer
424 to the sink pad format dimensions instead.</para>
426 <figure id="subdev-image-processing-crop">
427 <title>Image processing in subdevs: simple crop example</title>
430 <imagedata fileref="subdev-image-processing-crop.svg"
431 format="SVG" scale="200" />
436 <para>In the above example, the subdev supports cropping on its
437 sink pad. To configure it, the user sets the media bus format on
438 the subdev's sink pad. Now the actual crop rectangle can be set
439 on the sink pad --- the location and size of this rectangle
440 reflect the location and size of a rectangle to be cropped from
441 the sink format. The size of the sink crop rectangle will also
442 be the size of the format of the subdev's source pad.</para>
444 <figure id="subdev-image-processing-scaling-multi-source">
445 <title>Image processing in subdevs: scaling with multiple sources</title>
448 <imagedata fileref="subdev-image-processing-scaling-multi-source.svg"
449 format="SVG" scale="200" />
454 <para>In this example, the subdev is capable of first cropping,
455 then scaling and finally cropping for two source pads
456 individually from the resulting scaled image. The location of
457 the scaled image in the cropped image is ignored in sink compose
458 target. Both of the locations of the source crop rectangles
459 refer to the sink scaling rectangle, independently cropping an
460 area at location specified by the source crop rectangle from
463 <figure id="subdev-image-processing-full">
464 <title>Image processing in subdevs: scaling and composition
465 with multiple sinks and sources</title>
468 <imagedata fileref="subdev-image-processing-full.svg"
469 format="SVG" scale="200" />
474 <para>The subdev driver supports two sink pads and two source
475 pads. The images from both of the sink pads are individually
476 cropped, then scaled and further composed on the composition
477 bounds rectangle. From that, two independent streams are cropped
478 and sent out of the subdev from the source pads.</para>