162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci.. _subdev: 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci******************** 662306a36Sopenharmony_ciSub-device Interface 762306a36Sopenharmony_ci******************** 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciThe complex nature of V4L2 devices, where hardware is often made of 1062306a36Sopenharmony_ciseveral integrated circuits that need to interact with each other in a 1162306a36Sopenharmony_cicontrolled way, leads to complex V4L2 drivers. The drivers usually 1262306a36Sopenharmony_cireflect the hardware model in software, and model the different hardware 1362306a36Sopenharmony_cicomponents as software blocks called sub-devices. 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciV4L2 sub-devices are usually kernel-only objects. If the V4L2 driver 1662306a36Sopenharmony_ciimplements the media device API, they will automatically inherit from 1762306a36Sopenharmony_cimedia entities. Applications will be able to enumerate the sub-devices 1862306a36Sopenharmony_ciand discover the hardware topology using the media entities, pads and 1962306a36Sopenharmony_cilinks enumeration API. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciIn addition to make sub-devices discoverable, drivers can also choose to 2262306a36Sopenharmony_cimake them directly configurable by applications. When both the 2362306a36Sopenharmony_cisub-device driver and the V4L2 device driver support this, sub-devices 2462306a36Sopenharmony_ciwill feature a character device node on which ioctls can be called to 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci- query, read and write sub-devices controls 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci- subscribe and unsubscribe to events and retrieve them 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci- negotiate image formats on individual pads 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci- inspect and modify internal data routing between pads of the same entity 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciSub-device character device nodes, conventionally named 3562306a36Sopenharmony_ci``/dev/v4l-subdev*``, use major number 81. 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ciDrivers may opt to limit the sub-device character devices to only expose 3862306a36Sopenharmony_cioperations that do not modify the device state. In such a case the sub-devices 3962306a36Sopenharmony_ciare referred to as ``read-only`` in the rest of this documentation, and the 4062306a36Sopenharmony_cirelated restrictions are documented in individual ioctls. 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ciControls 4462306a36Sopenharmony_ci======== 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciMost V4L2 controls are implemented by sub-device hardware. Drivers 4762306a36Sopenharmony_ciusually merge all controls and expose them through video device nodes. 4862306a36Sopenharmony_ciApplications can control all sub-devices through a single interface. 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ciComplex devices sometimes implement the same control in different pieces 5162306a36Sopenharmony_ciof hardware. This situation is common in embedded platforms, where both 5262306a36Sopenharmony_cisensors and image processing hardware implement identical functions, 5362306a36Sopenharmony_cisuch as contrast adjustment, white balance or faulty pixels correction. 5462306a36Sopenharmony_ciAs the V4L2 controls API doesn't support several identical controls in a 5562306a36Sopenharmony_cisingle device, all but one of the identical controls are hidden. 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciApplications can access those hidden controls through the sub-device 5862306a36Sopenharmony_cinode with the V4L2 control API described in :ref:`control`. The ioctls 5962306a36Sopenharmony_cibehave identically as when issued on V4L2 device nodes, with the 6062306a36Sopenharmony_ciexception that they deal only with controls implemented in the 6162306a36Sopenharmony_cisub-device. 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ciDepending on the driver, those controls might also be exposed through 6462306a36Sopenharmony_cione (or several) V4L2 device nodes. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciEvents 6862306a36Sopenharmony_ci====== 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciV4L2 sub-devices can notify applications of events as described in 7162306a36Sopenharmony_ci:ref:`event`. The API behaves identically as when used on V4L2 device 7262306a36Sopenharmony_cinodes, with the exception that it only deals with events generated by 7362306a36Sopenharmony_cithe sub-device. Depending on the driver, those events might also be 7462306a36Sopenharmony_cireported on one (or several) V4L2 device nodes. 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci.. _pad-level-formats: 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ciPad-level Formats 8062306a36Sopenharmony_ci================= 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci.. warning:: 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci Pad-level formats are only applicable to very complex devices that 8562306a36Sopenharmony_ci need to expose low-level format configuration to user space. Generic 8662306a36Sopenharmony_ci V4L2 applications do *not* need to use the API described in this 8762306a36Sopenharmony_ci section. 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci.. note:: 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci For the purpose of this section, the term *format* means the 9262306a36Sopenharmony_ci combination of media bus data format, frame width and frame height. 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ciImage formats are typically negotiated on video capture and output 9562306a36Sopenharmony_cidevices using the format and 9662306a36Sopenharmony_ci:ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is 9762306a36Sopenharmony_ciresponsible for configuring every block in the video pipeline according 9862306a36Sopenharmony_cito the requested format at the pipeline input and/or output. 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciFor complex devices, such as often found in embedded systems, identical 10162306a36Sopenharmony_ciimage sizes at the output of a pipeline can be achieved using different 10262306a36Sopenharmony_cihardware configurations. One such example is shown on 10362306a36Sopenharmony_ci:ref:`pipeline-scaling`, where image scaling can be performed on both 10462306a36Sopenharmony_cithe video sensor and the host image processing hardware. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci.. _pipeline-scaling: 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci.. kernel-figure:: pipeline.dot 11062306a36Sopenharmony_ci :alt: pipeline.dot 11162306a36Sopenharmony_ci :align: center 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci Image Format Negotiation on Pipelines 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci High quality and high speed pipeline configuration 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciThe sensor scaler is usually of less quality than the host scaler, but 12062306a36Sopenharmony_ciscaling on the sensor is required to achieve higher frame rates. 12162306a36Sopenharmony_ciDepending on the use case (quality vs. speed), the pipeline must be 12262306a36Sopenharmony_ciconfigured differently. Applications need to configure the formats at 12362306a36Sopenharmony_cievery point in the pipeline explicitly. 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ciDrivers that implement the :ref:`media API <media-controller-intro>` 12662306a36Sopenharmony_cican expose pad-level image format configuration to applications. When 12762306a36Sopenharmony_cithey do, applications can use the 12862306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 12962306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls. to 13062306a36Sopenharmony_cinegotiate formats on a per-pad basis. 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ciApplications are responsible for configuring coherent parameters on the 13362306a36Sopenharmony_ciwhole pipeline and making sure that connected pads have compatible 13462306a36Sopenharmony_ciformats. The pipeline is checked for formats mismatch at 13562306a36Sopenharmony_ci:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error 13662306a36Sopenharmony_cicode is then returned if the configuration is invalid. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciPad-level image format configuration support can be tested by calling 13962306a36Sopenharmony_cithe :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad 14062306a36Sopenharmony_ci0. If the driver returns an ``EINVAL`` error code pad-level format 14162306a36Sopenharmony_ciconfiguration is not supported by the sub-device. 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ciFormat Negotiation 14562306a36Sopenharmony_ci------------------ 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciAcceptable formats on pads can (and usually do) depend on a number of 14862306a36Sopenharmony_ciexternal parameters, such as formats on other pads, active links, or 14962306a36Sopenharmony_cieven controls. Finding a combination of formats on all pads in a video 15062306a36Sopenharmony_cipipeline, acceptable to both application and driver, can't rely on 15162306a36Sopenharmony_ciformats enumeration only. A format negotiation mechanism is required. 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ciCentral to the format negotiation mechanism are the get/set format 15462306a36Sopenharmony_cioperations. When called with the ``which`` argument set to 15562306a36Sopenharmony_ci:ref:`V4L2_SUBDEV_FORMAT_TRY <VIDIOC_SUBDEV_G_FMT>`, the 15662306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 15762306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls operate on 15862306a36Sopenharmony_cia set of formats parameters that are not connected to the hardware 15962306a36Sopenharmony_ciconfiguration. Modifying those 'try' formats leaves the device state 16062306a36Sopenharmony_ciuntouched (this applies to both the software state stored in the driver 16162306a36Sopenharmony_ciand the hardware state stored in the device itself). 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciWhile not kept as part of the device state, try formats are stored in 16462306a36Sopenharmony_cithe sub-device file handles. A 16562306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return 16662306a36Sopenharmony_cithe last try format set *on the same sub-device file handle*. Several 16762306a36Sopenharmony_ciapplications querying the same sub-device at the same time will thus not 16862306a36Sopenharmony_ciinteract with each other. 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ciTo find out whether a particular format is supported by the device, 17162306a36Sopenharmony_ciapplications use the 17262306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers 17362306a36Sopenharmony_civerify and, if needed, change the requested ``format`` based on device 17462306a36Sopenharmony_cirequirements and return the possibly modified value. Applications can 17562306a36Sopenharmony_cithen choose to try a different format or accept the returned value and 17662306a36Sopenharmony_cicontinue. 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ciFormats returned by the driver during a negotiation iteration are 17962306a36Sopenharmony_ciguaranteed to be supported by the device. In particular, drivers 18062306a36Sopenharmony_ciguarantee that a returned format will not be further changed if passed 18162306a36Sopenharmony_cito an :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` call as-is 18262306a36Sopenharmony_ci(as long as external parameters, such as formats on other pads or links' 18362306a36Sopenharmony_ciconfiguration are not changed). 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ciDrivers automatically propagate formats inside sub-devices. When a try 18662306a36Sopenharmony_cior active format is set on a pad, corresponding formats on other pads of 18762306a36Sopenharmony_cithe same sub-device can be modified by the driver. Drivers are free to 18862306a36Sopenharmony_cimodify formats as required by the device. However, they should comply 18962306a36Sopenharmony_ciwith the following rules when possible: 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci- Formats should be propagated from sink pads to source pads. Modifying 19262306a36Sopenharmony_ci a format on a source pad should not modify the format on any sink 19362306a36Sopenharmony_ci pad. 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci- Sub-devices that scale frames using variable scaling factors should 19662306a36Sopenharmony_ci reset the scale factors to default values when sink pads formats are 19762306a36Sopenharmony_ci modified. If the 1:1 scaling ratio is supported, this means that 19862306a36Sopenharmony_ci source pads formats should be reset to the sink pads formats. 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ciFormats are not propagated across links, as that would involve 20162306a36Sopenharmony_cipropagating them from one sub-device file handle to another. 20262306a36Sopenharmony_ciApplications must then take care to configure both ends of every link 20362306a36Sopenharmony_ciexplicitly with compatible formats. Identical formats on the two ends of 20462306a36Sopenharmony_cia link are guaranteed to be compatible. Drivers are free to accept 20562306a36Sopenharmony_cidifferent formats matching device requirements as being compatible. 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci:ref:`sample-pipeline-config` shows a sample configuration sequence 20862306a36Sopenharmony_cifor the pipeline described in :ref:`pipeline-scaling` (table columns 20962306a36Sopenharmony_cilist entity names and pad numbers). 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci.. raw:: latex 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci \begingroup 21562306a36Sopenharmony_ci \scriptsize 21662306a36Sopenharmony_ci \setlength{\tabcolsep}{2pt} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci.. tabularcolumns:: |p{2.0cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci.. _sample-pipeline-config: 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci.. flat-table:: Sample Pipeline Configuration 22362306a36Sopenharmony_ci :header-rows: 1 22462306a36Sopenharmony_ci :stub-columns: 0 22562306a36Sopenharmony_ci :widths: 5 5 5 5 5 5 5 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci * - 22862306a36Sopenharmony_ci - Sensor/0 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci format 23162306a36Sopenharmony_ci - Frontend/0 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci format 23462306a36Sopenharmony_ci - Frontend/1 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci format 23762306a36Sopenharmony_ci - Scaler/0 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci format 24062306a36Sopenharmony_ci - Scaler/0 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci compose selection rectangle 24362306a36Sopenharmony_ci - Scaler/1 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci format 24662306a36Sopenharmony_ci * - Initial state 24762306a36Sopenharmony_ci - 2048x1536 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci SGRBG8_1X8 25062306a36Sopenharmony_ci - (default) 25162306a36Sopenharmony_ci - (default) 25262306a36Sopenharmony_ci - (default) 25362306a36Sopenharmony_ci - (default) 25462306a36Sopenharmony_ci - (default) 25562306a36Sopenharmony_ci * - Configure frontend sink format 25662306a36Sopenharmony_ci - 2048x1536 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci SGRBG8_1X8 25962306a36Sopenharmony_ci - *2048x1536* 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ci *SGRBG8_1X8* 26262306a36Sopenharmony_ci - *2046x1534* 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci *SGRBG8_1X8* 26562306a36Sopenharmony_ci - (default) 26662306a36Sopenharmony_ci - (default) 26762306a36Sopenharmony_ci - (default) 26862306a36Sopenharmony_ci * - Configure scaler sink format 26962306a36Sopenharmony_ci - 2048x1536 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci SGRBG8_1X8 27262306a36Sopenharmony_ci - 2048x1536 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ci SGRBG8_1X8 27562306a36Sopenharmony_ci - 2046x1534 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci SGRBG8_1X8 27862306a36Sopenharmony_ci - *2046x1534* 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci *SGRBG8_1X8* 28162306a36Sopenharmony_ci - *0,0/2046x1534* 28262306a36Sopenharmony_ci - *2046x1534* 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci *SGRBG8_1X8* 28562306a36Sopenharmony_ci * - Configure scaler sink compose selection 28662306a36Sopenharmony_ci - 2048x1536 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci SGRBG8_1X8 28962306a36Sopenharmony_ci - 2048x1536 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci SGRBG8_1X8 29262306a36Sopenharmony_ci - 2046x1534 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci SGRBG8_1X8 29562306a36Sopenharmony_ci - 2046x1534 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci SGRBG8_1X8 29862306a36Sopenharmony_ci - *0,0/1280x960* 29962306a36Sopenharmony_ci - *1280x960* 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci *SGRBG8_1X8* 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci.. raw:: latex 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci \endgroup 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci1. Initial state. The sensor source pad format is set to its native 3MP 30862306a36Sopenharmony_ci size and V4L2_MBUS_FMT_SGRBG8_1X8 media bus code. Formats on the 30962306a36Sopenharmony_ci host frontend and scaler sink and source pads have the default 31062306a36Sopenharmony_ci values, as well as the compose rectangle on the scaler's sink pad. 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci2. The application configures the frontend sink pad format's size to 31362306a36Sopenharmony_ci 2048x1536 and its media bus code to V4L2_MBUS_FMT_SGRBG_1X8. The 31462306a36Sopenharmony_ci driver propagates the format to the frontend source pad. 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci3. The application configures the scaler sink pad format's size to 31762306a36Sopenharmony_ci 2046x1534 and the media bus code to V4L2_MBUS_FMT_SGRBG_1X8 to 31862306a36Sopenharmony_ci match the frontend source size and media bus code. The media bus code 31962306a36Sopenharmony_ci on the sink pad is set to V4L2_MBUS_FMT_SGRBG_1X8. The driver 32062306a36Sopenharmony_ci propagates the size to the compose selection rectangle on the 32162306a36Sopenharmony_ci scaler's sink pad, and the format to the scaler source pad. 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci4. The application configures the size of the compose selection 32462306a36Sopenharmony_ci rectangle of the scaler's sink pad 1280x960. The driver propagates 32562306a36Sopenharmony_ci the size to the scaler's source pad format. 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ciWhen satisfied with the try results, applications can set the active 32862306a36Sopenharmony_ciformats by setting the ``which`` argument to 32962306a36Sopenharmony_ci``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try 33062306a36Sopenharmony_ciformats by drivers. To avoid modifying the hardware state during format 33162306a36Sopenharmony_cinegotiation, applications should negotiate try formats first and then 33262306a36Sopenharmony_cimodify the active settings using the try formats returned during the 33362306a36Sopenharmony_cilast negotiation iteration. This guarantees that the active format will 33462306a36Sopenharmony_cibe applied as-is by the driver without being modified. 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci.. _v4l2-subdev-selections: 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ciSelections: cropping, scaling and composition 34062306a36Sopenharmony_ci--------------------------------------------- 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ciMany sub-devices support cropping frames on their input or output pads 34362306a36Sopenharmony_ci(or possible even on both). Cropping is used to select the area of 34462306a36Sopenharmony_ciinterest in an image, typically on an image sensor or a video decoder. 34562306a36Sopenharmony_ciIt can also be used as part of digital zoom implementations to select 34662306a36Sopenharmony_cithe area of the image that will be scaled up. 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ciCrop settings are defined by a crop rectangle and represented in a 34962306a36Sopenharmony_cistruct :c:type:`v4l2_rect` by the coordinates of the top 35062306a36Sopenharmony_cileft corner and the rectangle size. Both the coordinates and sizes are 35162306a36Sopenharmony_ciexpressed in pixels. 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ciAs for pad formats, drivers store try and active rectangles for the 35462306a36Sopenharmony_ciselection targets :ref:`v4l2-selections-common`. 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ciOn sink pads, cropping is applied relative to the current pad format. 35762306a36Sopenharmony_ciThe pad format represents the image size as received by the sub-device 35862306a36Sopenharmony_cifrom the previous block in the pipeline, and the crop rectangle 35962306a36Sopenharmony_cirepresents the sub-image that will be transmitted further inside the 36062306a36Sopenharmony_cisub-device for processing. 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ciThe scaling operation changes the size of the image by scaling it to new 36362306a36Sopenharmony_cidimensions. The scaling ratio isn't specified explicitly, but is implied 36462306a36Sopenharmony_cifrom the original and scaled image sizes. Both sizes are represented by 36562306a36Sopenharmony_cistruct :c:type:`v4l2_rect`. 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ciScaling support is optional. When supported by a subdev, the crop 36862306a36Sopenharmony_cirectangle on the subdev's sink pad is scaled to the size configured 36962306a36Sopenharmony_ciusing the 37062306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL 37162306a36Sopenharmony_ciusing ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the 37262306a36Sopenharmony_cisubdev supports scaling but not composing, the top and left values are 37362306a36Sopenharmony_cinot used and must always be set to zero. 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ciOn source pads, cropping is similar to sink pads, with the exception 37662306a36Sopenharmony_cithat the source size from which the cropping is performed, is the 37762306a36Sopenharmony_ciCOMPOSE rectangle on the sink pad. In both sink and source pads, the 37862306a36Sopenharmony_cicrop rectangle must be entirely contained inside the source image size 37962306a36Sopenharmony_cifor the crop operation. 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ciThe drivers should always use the closest possible rectangle the user 38262306a36Sopenharmony_cirequests on all selection targets, unless specifically told otherwise. 38362306a36Sopenharmony_ci``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round 38462306a36Sopenharmony_cithe image size either up or down. :ref:`v4l2-selection-flags` 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ciTypes of selection targets 38862306a36Sopenharmony_ci-------------------------- 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ciActual targets 39262306a36Sopenharmony_ci^^^^^^^^^^^^^^ 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ciActual targets (without a postfix) reflect the actual hardware 39562306a36Sopenharmony_ciconfiguration at any point of time. There is a BOUNDS target 39662306a36Sopenharmony_cicorresponding to every actual target. 39762306a36Sopenharmony_ci 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ciBOUNDS targets 40062306a36Sopenharmony_ci^^^^^^^^^^^^^^ 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ciBOUNDS targets is the smallest rectangle that contains all valid actual 40362306a36Sopenharmony_cirectangles. It may not be possible to set the actual rectangle as large 40462306a36Sopenharmony_cias the BOUNDS rectangle, however. This may be because e.g. a sensor's 40562306a36Sopenharmony_cipixel array is not rectangular but cross-shaped or round. The maximum 40662306a36Sopenharmony_cisize may also be smaller than the BOUNDS rectangle. 40762306a36Sopenharmony_ci 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci.. _format-propagation: 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ciOrder of configuration and format propagation 41262306a36Sopenharmony_ci--------------------------------------------- 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ciInside subdevs, the order of image processing steps will always be from 41562306a36Sopenharmony_cithe sink pad towards the source pad. This is also reflected in the order 41662306a36Sopenharmony_ciin which the configuration must be performed by the user: the changes 41762306a36Sopenharmony_cimade will be propagated to any subsequent stages. If this behaviour is 41862306a36Sopenharmony_cinot desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This 41962306a36Sopenharmony_ciflag causes no propagation of the changes are allowed in any 42062306a36Sopenharmony_cicircumstances. This may also cause the accessed rectangle to be adjusted 42162306a36Sopenharmony_ciby the driver, depending on the properties of the underlying hardware. 42262306a36Sopenharmony_ci 42362306a36Sopenharmony_ciThe coordinates to a step always refer to the actual size of the 42462306a36Sopenharmony_ciprevious step. The exception to this rule is the sink compose 42562306a36Sopenharmony_cirectangle, which refers to the sink compose bounds rectangle --- if it 42662306a36Sopenharmony_ciis supported by the hardware. 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci1. Sink pad format. The user configures the sink pad format. This format 42962306a36Sopenharmony_ci defines the parameters of the image the entity receives through the 43062306a36Sopenharmony_ci pad for further processing. 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci2. Sink pad actual crop selection. The sink pad crop defines the crop 43362306a36Sopenharmony_ci performed to the sink pad format. 43462306a36Sopenharmony_ci 43562306a36Sopenharmony_ci3. Sink pad actual compose selection. The size of the sink pad compose 43662306a36Sopenharmony_ci rectangle defines the scaling ratio compared to the size of the sink 43762306a36Sopenharmony_ci pad crop rectangle. The location of the compose rectangle specifies 43862306a36Sopenharmony_ci the location of the actual sink compose rectangle in the sink compose 43962306a36Sopenharmony_ci bounds rectangle. 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci4. Source pad actual crop selection. Crop on the source pad defines crop 44262306a36Sopenharmony_ci performed to the image in the sink compose bounds rectangle. 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci5. Source pad format. The source pad format defines the output pixel 44562306a36Sopenharmony_ci format of the subdev, as well as the other parameters with the 44662306a36Sopenharmony_ci exception of the image width and height. Width and height are defined 44762306a36Sopenharmony_ci by the size of the source pad actual crop selection. 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ciAccessing any of the above rectangles not supported by the subdev will 45062306a36Sopenharmony_cireturn ``EINVAL``. Any rectangle referring to a previous unsupported 45162306a36Sopenharmony_cirectangle coordinates will instead refer to the previous supported 45262306a36Sopenharmony_cirectangle. For example, if sink crop is not supported, the compose 45362306a36Sopenharmony_ciselection will refer to the sink pad format dimensions instead. 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci.. _subdev-image-processing-crop: 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci.. kernel-figure:: subdev-image-processing-crop.svg 45962306a36Sopenharmony_ci :alt: subdev-image-processing-crop.svg 46062306a36Sopenharmony_ci :align: center 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_ci **Figure 4.5. Image processing in subdevs: simple crop example** 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ciIn the above example, the subdev supports cropping on its sink pad. To 46562306a36Sopenharmony_ciconfigure it, the user sets the media bus format on the subdev's sink 46662306a36Sopenharmony_cipad. Now the actual crop rectangle can be set on the sink pad --- the 46762306a36Sopenharmony_cilocation and size of this rectangle reflect the location and size of a 46862306a36Sopenharmony_cirectangle to be cropped from the sink format. The size of the sink crop 46962306a36Sopenharmony_cirectangle will also be the size of the format of the subdev's source 47062306a36Sopenharmony_cipad. 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci.. _subdev-image-processing-scaling-multi-source: 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ci.. kernel-figure:: subdev-image-processing-scaling-multi-source.svg 47662306a36Sopenharmony_ci :alt: subdev-image-processing-scaling-multi-source.svg 47762306a36Sopenharmony_ci :align: center 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci **Figure 4.6. Image processing in subdevs: scaling with multiple sources** 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ciIn this example, the subdev is capable of first cropping, then scaling 48262306a36Sopenharmony_ciand finally cropping for two source pads individually from the resulting 48362306a36Sopenharmony_ciscaled image. The location of the scaled image in the cropped image is 48462306a36Sopenharmony_ciignored in sink compose target. Both of the locations of the source crop 48562306a36Sopenharmony_cirectangles refer to the sink scaling rectangle, independently cropping 48662306a36Sopenharmony_cian area at location specified by the source crop rectangle from it. 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci.. _subdev-image-processing-full: 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci.. kernel-figure:: subdev-image-processing-full.svg 49262306a36Sopenharmony_ci :alt: subdev-image-processing-full.svg 49362306a36Sopenharmony_ci :align: center 49462306a36Sopenharmony_ci 49562306a36Sopenharmony_ci **Figure 4.7. Image processing in subdevs: scaling and composition with multiple sinks and sources** 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ciThe subdev driver supports two sink pads and two source pads. The images 49862306a36Sopenharmony_cifrom both of the sink pads are individually cropped, then scaled and 49962306a36Sopenharmony_cifurther composed on the composition bounds rectangle. From that, two 50062306a36Sopenharmony_ciindependent streams are cropped and sent out of the subdev from the 50162306a36Sopenharmony_cisource pads. 50262306a36Sopenharmony_ci 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci.. toctree:: 50562306a36Sopenharmony_ci :maxdepth: 1 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci subdev-formats 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ciStreams, multiplexed media pads and internal routing 51062306a36Sopenharmony_ci---------------------------------------------------- 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ciSimple V4L2 sub-devices do not support multiple, unrelated video streams, 51362306a36Sopenharmony_ciand only a single stream can pass through a media link and a media pad. 51462306a36Sopenharmony_ciThus each pad contains a format and selection configuration for that 51562306a36Sopenharmony_cisingle stream. A subdev can do stream processing and split a stream into 51662306a36Sopenharmony_citwo or compose two streams into one, but the inputs and outputs for the 51762306a36Sopenharmony_cisubdev are still a single stream per pad. 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ciSome hardware, e.g. MIPI CSI-2, support multiplexed streams, that is, multiple 52062306a36Sopenharmony_cidata streams are transmitted on the same bus, which is represented by a media 52162306a36Sopenharmony_cilink connecting a transmitter source pad with a sink pad on the receiver. For 52262306a36Sopenharmony_ciexample, a camera sensor can produce two distinct streams, a pixel stream and a 52362306a36Sopenharmony_cimetadata stream, which are transmitted on the multiplexed data bus, represented 52462306a36Sopenharmony_ciby a media link which connects the single sensor's source pad with the receiver 52562306a36Sopenharmony_cisink pad. The stream-aware receiver will de-multiplex the streams received on 52662306a36Sopenharmony_cithe its sink pad and allows to route them individually to one of its source 52762306a36Sopenharmony_cipads. 52862306a36Sopenharmony_ci 52962306a36Sopenharmony_ciSubdevice drivers that support multiplexed streams are compatible with 53062306a36Sopenharmony_cinon-multiplexed subdev drivers, but, of course, require a routing configuration 53162306a36Sopenharmony_ciwhere the link between those two types of drivers contains only a single 53262306a36Sopenharmony_cistream. 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ciUnderstanding streams 53562306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^ 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ciA stream is a stream of content (e.g. pixel data or metadata) flowing through 53862306a36Sopenharmony_cithe media pipeline from a source (e.g. a sensor) towards the final sink (e.g. a 53962306a36Sopenharmony_cireceiver and demultiplexer in a SoC). Each media link carries all the enabled 54062306a36Sopenharmony_cistreams from one end of the link to the other, and sub-devices have routing 54162306a36Sopenharmony_citables which describe how the incoming streams from sink pads are routed to the 54262306a36Sopenharmony_cisource pads. 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ciA stream ID is a media pad-local identifier for a stream. Streams IDs of 54562306a36Sopenharmony_cithe same stream must be equal on both ends of a link. In other words, 54662306a36Sopenharmony_cia particular stream ID must exist on both sides of a media 54762306a36Sopenharmony_cilink, but another stream ID can be used for the same stream at the other side 54862306a36Sopenharmony_ciof the sub-device. 54962306a36Sopenharmony_ci 55062306a36Sopenharmony_ciA stream at a specific point in the media pipeline is identified by the 55162306a36Sopenharmony_cisub-device and a (pad, stream) pair. For sub-devices that do not support 55262306a36Sopenharmony_cimultiplexed streams the 'stream' field is always 0. 55362306a36Sopenharmony_ci 55462306a36Sopenharmony_ciInteraction between routes, streams, formats and selections 55562306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ciThe addition of streams to the V4L2 sub-device interface moves the sub-device 55862306a36Sopenharmony_ciformats and selections from pads to (pad, stream) pairs. Besides the 55962306a36Sopenharmony_ciusual pad, also the stream ID needs to be provided for setting formats and 56062306a36Sopenharmony_ciselections. The order of configuring formats and selections along a stream is 56162306a36Sopenharmony_cithe same as without streams (see :ref:`format-propagation`). 56262306a36Sopenharmony_ci 56362306a36Sopenharmony_ciInstead of the sub-device wide merging of streams from all sink pads 56462306a36Sopenharmony_citowards all source pads, data flows for each route are separate from each 56562306a36Sopenharmony_ciother. Any number of routes from streams on sink pads towards streams on 56662306a36Sopenharmony_cisource pads is allowed, to the extent supported by drivers. For every 56762306a36Sopenharmony_cistream on a source pad, however, only a single route is allowed. 56862306a36Sopenharmony_ci 56962306a36Sopenharmony_ciAny configurations of a stream within a pad, such as format or selections, 57062306a36Sopenharmony_ciare independent of similar configurations on other streams. This is 57162306a36Sopenharmony_cisubject to change in the future. 57262306a36Sopenharmony_ci 57362306a36Sopenharmony_ciConfiguring streams 57462306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^ 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ciThe configuration of the streams is done individually for each sub-device and 57762306a36Sopenharmony_cithe validity of the streams between sub-devices is validated when the pipeline 57862306a36Sopenharmony_ciis started. 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ciThere are three steps in configuring the streams: 58162306a36Sopenharmony_ci 58262306a36Sopenharmony_ci1) Set up links. Connect the pads between sub-devices using the :ref:`Media 58362306a36Sopenharmony_ciController API <media_controller>` 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci2) Streams. Streams are declared and their routing is configured by 58662306a36Sopenharmony_cisetting the routing table for the sub-device using 58762306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_ROUTING <VIDIOC_SUBDEV_G_ROUTING>` ioctl. Note that 58862306a36Sopenharmony_cisetting the routing table will reset formats and selections in the 58962306a36Sopenharmony_cisub-device to default values. 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci3) Configure formats and selections. Formats and selections of each stream 59262306a36Sopenharmony_ciare configured separately as documented for plain sub-devices in 59362306a36Sopenharmony_ci:ref:`format-propagation`. The stream ID is set to the same stream ID 59462306a36Sopenharmony_ciassociated with either sink or source pads of routes configured using the 59562306a36Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_ROUTING <VIDIOC_SUBDEV_G_ROUTING>` ioctl. 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ciMultiplexed streams setup example 59862306a36Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ciA simple example of a multiplexed stream setup might be as follows: 60162306a36Sopenharmony_ci 60262306a36Sopenharmony_ci- Two identical sensors (Sensor A and Sensor B). Each sensor has a single source 60362306a36Sopenharmony_ci pad (pad 0) which carries a pixel data stream. 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci- Multiplexer bridge (Bridge). The bridge has two sink pads, connected to the 60662306a36Sopenharmony_ci sensors (pads 0, 1), and one source pad (pad 2), which outputs two streams. 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci- Receiver in the SoC (Receiver). The receiver has a single sink pad (pad 0), 60962306a36Sopenharmony_ci connected to the bridge, and two source pads (pads 1-2), going to the DMA 61062306a36Sopenharmony_ci engine. The receiver demultiplexes the incoming streams to the source pads. 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_ci- DMA Engines in the SoC (DMA Engine), one for each stream. Each DMA engine is 61362306a36Sopenharmony_ci connected to a single source pad in the receiver. 61462306a36Sopenharmony_ci 61562306a36Sopenharmony_ciThe sensors, the bridge and the receiver are modeled as V4L2 sub-devices, 61662306a36Sopenharmony_ciexposed to userspace via /dev/v4l-subdevX device nodes. The DMA engines are 61762306a36Sopenharmony_cimodeled as V4L2 devices, exposed to userspace via /dev/videoX nodes. 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ciTo configure this pipeline, the userspace must take the following steps: 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci1) Set up media links between entities: connect the sensors to the bridge, 62262306a36Sopenharmony_cibridge to the receiver, and the receiver to the DMA engines. This step does 62362306a36Sopenharmony_cinot differ from normal non-multiplexed media controller setup. 62462306a36Sopenharmony_ci 62562306a36Sopenharmony_ci2) Configure routing 62662306a36Sopenharmony_ci 62762306a36Sopenharmony_ci.. flat-table:: Bridge routing table 62862306a36Sopenharmony_ci :header-rows: 1 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci * - Sink Pad/Stream 63162306a36Sopenharmony_ci - Source Pad/Stream 63262306a36Sopenharmony_ci - Routing Flags 63362306a36Sopenharmony_ci - Comments 63462306a36Sopenharmony_ci * - 0/0 63562306a36Sopenharmony_ci - 2/0 63662306a36Sopenharmony_ci - V4L2_SUBDEV_ROUTE_FL_ACTIVE 63762306a36Sopenharmony_ci - Pixel data stream from Sensor A 63862306a36Sopenharmony_ci * - 1/0 63962306a36Sopenharmony_ci - 2/1 64062306a36Sopenharmony_ci - V4L2_SUBDEV_ROUTE_FL_ACTIVE 64162306a36Sopenharmony_ci - Pixel data stream from Sensor B 64262306a36Sopenharmony_ci 64362306a36Sopenharmony_ci.. flat-table:: Receiver routing table 64462306a36Sopenharmony_ci :header-rows: 1 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci * - Sink Pad/Stream 64762306a36Sopenharmony_ci - Source Pad/Stream 64862306a36Sopenharmony_ci - Routing Flags 64962306a36Sopenharmony_ci - Comments 65062306a36Sopenharmony_ci * - 0/0 65162306a36Sopenharmony_ci - 1/0 65262306a36Sopenharmony_ci - V4L2_SUBDEV_ROUTE_FL_ACTIVE 65362306a36Sopenharmony_ci - Pixel data stream from Sensor A 65462306a36Sopenharmony_ci * - 0/1 65562306a36Sopenharmony_ci - 2/0 65662306a36Sopenharmony_ci - V4L2_SUBDEV_ROUTE_FL_ACTIVE 65762306a36Sopenharmony_ci - Pixel data stream from Sensor B 65862306a36Sopenharmony_ci 65962306a36Sopenharmony_ci3) Configure formats and selections 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ciAfter configuring routing, the next step is configuring the formats and 66262306a36Sopenharmony_ciselections for the streams. This is similar to performing this step without 66362306a36Sopenharmony_cistreams, with just one exception: the ``stream`` field needs to be assigned 66462306a36Sopenharmony_cito the value of the stream ID. 66562306a36Sopenharmony_ci 66662306a36Sopenharmony_ciA common way to accomplish this is to start from the sensors and propagate the 66762306a36Sopenharmony_ciconfigurations along the stream towards the receiver, 66862306a36Sopenharmony_ciusing :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls to configure each 66962306a36Sopenharmony_cistream endpoint in each sub-device. 670