About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Documentation / DocBook / media / v4l / dev-subdev.xml


Based on kernel version 4.7.2. Page generated on 2016-08-22 22:45 EST.

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


About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog