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