1  <title>Sub-device Interface</title>
2
3  <note>
4    <title>Experimental</title>
5    <para>This is an <link linkend="experimental">experimental</link>
6    interface and may change in the future.</para>
7  </note>
8
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>
14
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
19  API.</para>
20
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
25  <itemizedlist>
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>
29  </itemizedlist>
30  </para>
31
32  <para>Sub-device character device nodes, conventionally named
33  <filename>/dev/v4l-subdev*</filename>, use major number 81.</para>
34
35  <section>
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>
40
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>
47
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.
52    </para>
53
54    <para>Depending on the driver, those controls might also be exposed through
55    one (or several) V4L2 device nodes.</para>
56  </section>
57
58  <section>
59    <title>Events</title>
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>
65  </section>
66
67  <section id="pad-level-formats">
68    <title>Pad-level Formats</title>
69
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>
74
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>
78
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
84    and/or output.</para>
85
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>
92
93    <figure id="pipeline-scaling">
94      <title>Image Format Negotiation on Pipelines</title>
95      <mediaobject>
96	<imageobject>
97	  <imagedata fileref="pipeline.pdf" format="PS" />
98	</imageobject>
99	<imageobject>
100	  <imagedata fileref="pipeline.png" format="PNG" />
101	</imageobject>
102	<textobject>
103	  <phrase>High quality and high speed pipeline configuration</phrase>
104	</textobject>
105      </mediaobject>
106    </figure>
107
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>
113
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>
118
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
123    invalid.</para>
124
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>
128
129    <section>
130      <title>Format Negotiation</title>
131
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>
137
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>
146
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>
152
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
158      continue.</para>
159
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>
165
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:
171      <itemizedlist>
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>
179      </itemizedlist>
180      </para>
181
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>
188
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>
193
194      <table pgwide="0" frame="none" id="sample-pipeline-config">
195	<title>Sample Pipeline Configuration</title>
196	<tgroup cols="3">
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" />
204	  <thead>
205	    <row>
206	      <entry></entry>
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>
213	    </row>
214	  </thead>
215	  <tbody valign="top">
216	    <row>
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>
224	    </row>
225	    <row>
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>
233	    </row>
234	    <row>
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>
242	    </row>
243	    <row>
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>
251	    </row>
252	  </tbody>
253	</tgroup>
254      </table>
255
256      <para>
257      <orderedlist>
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>
263
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>
268
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>
276
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>
281
282      </orderedlist>
283      </para>
284
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.
293      </para>
294    </section>
295
296    <section id="v4l2-subdev-selections">
297      <title>Selections: cropping, scaling and composition</title>
298
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>
304
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>
308
309      <para>As for pad formats, drivers store try and active
310      rectangles for the selection targets <xref
311      linkend="v4l2-selections-common" />.</para>
312
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
318      processing.</para>
319
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>
324
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>
332
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
338      operation.</para>
339
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>
347    </section>
348
349    <section>
350      <title>Types of selection targets</title>
351
352      <section>
353	<title>Actual targets</title>
354
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>
358      </section>
359
360      <section>
361	<title>BOUNDS targets</title>
362
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>
369      </section>
370
371    </section>
372
373    <section>
374      <title>Order of configuration and format propagation</title>
375
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
381      user must set
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>
387
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>
392
393      <orderedlist>
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>
397
398	<listitem><para>Sink pad actual crop selection. The sink pad crop
399	defines the crop performed to the sink pad format.</para></listitem>
400
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>
407
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>
411
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>
417      </orderedlist>
418
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>
425
426      <figure id="subdev-image-processing-crop">
427	<title>Image processing in subdevs: simple crop example</title>
428	<mediaobject>
429	  <imageobject>
430	    <imagedata fileref="subdev-image-processing-crop.svg"
431	    format="SVG" scale="200" />
432	  </imageobject>
433	</mediaobject>
434      </figure>
435
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>
443
444      <figure id="subdev-image-processing-scaling-multi-source">
445	<title>Image processing in subdevs: scaling with multiple sources</title>
446	<mediaobject>
447	  <imageobject>
448	    <imagedata fileref="subdev-image-processing-scaling-multi-source.svg"
449	    format="SVG" scale="200" />
450	  </imageobject>
451	</mediaobject>
452      </figure>
453
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
461      it.</para>
462
463      <figure id="subdev-image-processing-full">
464	<title>Image processing in subdevs: scaling and composition
465	with multiple sinks and sources</title>
466	<mediaobject>
467	  <imageobject>
468	    <imagedata fileref="subdev-image-processing-full.svg"
469	    format="SVG" scale="200" />
470	  </imageobject>
471	</mediaobject>
472      </figure>
473
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>
479
480    </section>
481
482  </section>
483
484  &sub-subdev-formats;
485