18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci.. _subdev: 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci******************** 68c2ecf20Sopenharmony_ciSub-device Interface 78c2ecf20Sopenharmony_ci******************** 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ciThe complex nature of V4L2 devices, where hardware is often made of 108c2ecf20Sopenharmony_ciseveral integrated circuits that need to interact with each other in a 118c2ecf20Sopenharmony_cicontrolled way, leads to complex V4L2 drivers. The drivers usually 128c2ecf20Sopenharmony_cireflect the hardware model in software, and model the different hardware 138c2ecf20Sopenharmony_cicomponents as software blocks called sub-devices. 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ciV4L2 sub-devices are usually kernel-only objects. If the V4L2 driver 168c2ecf20Sopenharmony_ciimplements the media device API, they will automatically inherit from 178c2ecf20Sopenharmony_cimedia entities. Applications will be able to enumerate the sub-devices 188c2ecf20Sopenharmony_ciand discover the hardware topology using the media entities, pads and 198c2ecf20Sopenharmony_cilinks enumeration API. 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciIn addition to make sub-devices discoverable, drivers can also choose to 228c2ecf20Sopenharmony_cimake them directly configurable by applications. When both the 238c2ecf20Sopenharmony_cisub-device driver and the V4L2 device driver support this, sub-devices 248c2ecf20Sopenharmony_ciwill feature a character device node on which ioctls can be called to 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci- query, read and write sub-devices controls 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci- subscribe and unsubscribe to events and retrieve them 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci- negotiate image formats on individual pads 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciSub-device character device nodes, conventionally named 338c2ecf20Sopenharmony_ci``/dev/v4l-subdev*``, use major number 81. 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ciDrivers may opt to limit the sub-device character devices to only expose 368c2ecf20Sopenharmony_cioperations that do not modify the device state. In such a case the sub-devices 378c2ecf20Sopenharmony_ciare referred to as ``read-only`` in the rest of this documentation, and the 388c2ecf20Sopenharmony_cirelated restrictions are documented in individual ioctls. 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciControls 428c2ecf20Sopenharmony_ci======== 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ciMost V4L2 controls are implemented by sub-device hardware. Drivers 458c2ecf20Sopenharmony_ciusually merge all controls and expose them through video device nodes. 468c2ecf20Sopenharmony_ciApplications can control all sub-devices through a single interface. 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ciComplex devices sometimes implement the same control in different pieces 498c2ecf20Sopenharmony_ciof hardware. This situation is common in embedded platforms, where both 508c2ecf20Sopenharmony_cisensors and image processing hardware implement identical functions, 518c2ecf20Sopenharmony_cisuch as contrast adjustment, white balance or faulty pixels correction. 528c2ecf20Sopenharmony_ciAs the V4L2 controls API doesn't support several identical controls in a 538c2ecf20Sopenharmony_cisingle device, all but one of the identical controls are hidden. 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ciApplications can access those hidden controls through the sub-device 568c2ecf20Sopenharmony_cinode with the V4L2 control API described in :ref:`control`. The ioctls 578c2ecf20Sopenharmony_cibehave identically as when issued on V4L2 device nodes, with the 588c2ecf20Sopenharmony_ciexception that they deal only with controls implemented in the 598c2ecf20Sopenharmony_cisub-device. 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ciDepending on the driver, those controls might also be exposed through 628c2ecf20Sopenharmony_cione (or several) V4L2 device nodes. 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ciEvents 668c2ecf20Sopenharmony_ci====== 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciV4L2 sub-devices can notify applications of events as described in 698c2ecf20Sopenharmony_ci:ref:`event`. The API behaves identically as when used on V4L2 device 708c2ecf20Sopenharmony_cinodes, with the exception that it only deals with events generated by 718c2ecf20Sopenharmony_cithe sub-device. Depending on the driver, those events might also be 728c2ecf20Sopenharmony_cireported on one (or several) V4L2 device nodes. 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci.. _pad-level-formats: 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciPad-level Formats 788c2ecf20Sopenharmony_ci================= 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci.. warning:: 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci Pad-level formats are only applicable to very complex devices that 838c2ecf20Sopenharmony_ci need to expose low-level format configuration to user space. Generic 848c2ecf20Sopenharmony_ci V4L2 applications do *not* need to use the API described in this 858c2ecf20Sopenharmony_ci section. 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci.. note:: 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci For the purpose of this section, the term *format* means the 908c2ecf20Sopenharmony_ci combination of media bus data format, frame width and frame height. 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ciImage formats are typically negotiated on video capture and output 938c2ecf20Sopenharmony_cidevices using the format and 948c2ecf20Sopenharmony_ci:ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is 958c2ecf20Sopenharmony_ciresponsible for configuring every block in the video pipeline according 968c2ecf20Sopenharmony_cito the requested format at the pipeline input and/or output. 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciFor complex devices, such as often found in embedded systems, identical 998c2ecf20Sopenharmony_ciimage sizes at the output of a pipeline can be achieved using different 1008c2ecf20Sopenharmony_cihardware configurations. One such example is shown on 1018c2ecf20Sopenharmony_ci:ref:`pipeline-scaling`, where image scaling can be performed on both 1028c2ecf20Sopenharmony_cithe video sensor and the host image processing hardware. 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci.. _pipeline-scaling: 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci.. kernel-figure:: pipeline.dot 1088c2ecf20Sopenharmony_ci :alt: pipeline.dot 1098c2ecf20Sopenharmony_ci :align: center 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci Image Format Negotiation on Pipelines 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci High quality and high speed pipeline configuration 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ciThe sensor scaler is usually of less quality than the host scaler, but 1188c2ecf20Sopenharmony_ciscaling on the sensor is required to achieve higher frame rates. 1198c2ecf20Sopenharmony_ciDepending on the use case (quality vs. speed), the pipeline must be 1208c2ecf20Sopenharmony_ciconfigured differently. Applications need to configure the formats at 1218c2ecf20Sopenharmony_cievery point in the pipeline explicitly. 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ciDrivers that implement the :ref:`media API <media-controller-intro>` 1248c2ecf20Sopenharmony_cican expose pad-level image format configuration to applications. When 1258c2ecf20Sopenharmony_cithey do, applications can use the 1268c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 1278c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls. to 1288c2ecf20Sopenharmony_cinegotiate formats on a per-pad basis. 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ciApplications are responsible for configuring coherent parameters on the 1318c2ecf20Sopenharmony_ciwhole pipeline and making sure that connected pads have compatible 1328c2ecf20Sopenharmony_ciformats. The pipeline is checked for formats mismatch at 1338c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error 1348c2ecf20Sopenharmony_cicode is then returned if the configuration is invalid. 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ciPad-level image format configuration support can be tested by calling 1378c2ecf20Sopenharmony_cithe :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad 1388c2ecf20Sopenharmony_ci0. If the driver returns an ``EINVAL`` error code pad-level format 1398c2ecf20Sopenharmony_ciconfiguration is not supported by the sub-device. 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ciFormat Negotiation 1438c2ecf20Sopenharmony_ci------------------ 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciAcceptable formats on pads can (and usually do) depend on a number of 1468c2ecf20Sopenharmony_ciexternal parameters, such as formats on other pads, active links, or 1478c2ecf20Sopenharmony_cieven controls. Finding a combination of formats on all pads in a video 1488c2ecf20Sopenharmony_cipipeline, acceptable to both application and driver, can't rely on 1498c2ecf20Sopenharmony_ciformats enumeration only. A format negotiation mechanism is required. 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ciCentral to the format negotiation mechanism are the get/set format 1528c2ecf20Sopenharmony_cioperations. When called with the ``which`` argument set to 1538c2ecf20Sopenharmony_ci:ref:`V4L2_SUBDEV_FORMAT_TRY <VIDIOC_SUBDEV_G_FMT>`, the 1548c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 1558c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls operate on 1568c2ecf20Sopenharmony_cia set of formats parameters that are not connected to the hardware 1578c2ecf20Sopenharmony_ciconfiguration. Modifying those 'try' formats leaves the device state 1588c2ecf20Sopenharmony_ciuntouched (this applies to both the software state stored in the driver 1598c2ecf20Sopenharmony_ciand the hardware state stored in the device itself). 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ciWhile not kept as part of the device state, try formats are stored in 1628c2ecf20Sopenharmony_cithe sub-device file handles. A 1638c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return 1648c2ecf20Sopenharmony_cithe last try format set *on the same sub-device file handle*. Several 1658c2ecf20Sopenharmony_ciapplications querying the same sub-device at the same time will thus not 1668c2ecf20Sopenharmony_ciinteract with each other. 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ciTo find out whether a particular format is supported by the device, 1698c2ecf20Sopenharmony_ciapplications use the 1708c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers 1718c2ecf20Sopenharmony_civerify and, if needed, change the requested ``format`` based on device 1728c2ecf20Sopenharmony_cirequirements and return the possibly modified value. Applications can 1738c2ecf20Sopenharmony_cithen choose to try a different format or accept the returned value and 1748c2ecf20Sopenharmony_cicontinue. 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ciFormats returned by the driver during a negotiation iteration are 1778c2ecf20Sopenharmony_ciguaranteed to be supported by the device. In particular, drivers 1788c2ecf20Sopenharmony_ciguarantee that a returned format will not be further changed if passed 1798c2ecf20Sopenharmony_cito an :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` call as-is 1808c2ecf20Sopenharmony_ci(as long as external parameters, such as formats on other pads or links' 1818c2ecf20Sopenharmony_ciconfiguration are not changed). 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ciDrivers automatically propagate formats inside sub-devices. When a try 1848c2ecf20Sopenharmony_cior active format is set on a pad, corresponding formats on other pads of 1858c2ecf20Sopenharmony_cithe same sub-device can be modified by the driver. Drivers are free to 1868c2ecf20Sopenharmony_cimodify formats as required by the device. However, they should comply 1878c2ecf20Sopenharmony_ciwith the following rules when possible: 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci- Formats should be propagated from sink pads to source pads. Modifying 1908c2ecf20Sopenharmony_ci a format on a source pad should not modify the format on any sink 1918c2ecf20Sopenharmony_ci pad. 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci- Sub-devices that scale frames using variable scaling factors should 1948c2ecf20Sopenharmony_ci reset the scale factors to default values when sink pads formats are 1958c2ecf20Sopenharmony_ci modified. If the 1:1 scaling ratio is supported, this means that 1968c2ecf20Sopenharmony_ci source pads formats should be reset to the sink pads formats. 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ciFormats are not propagated across links, as that would involve 1998c2ecf20Sopenharmony_cipropagating them from one sub-device file handle to another. 2008c2ecf20Sopenharmony_ciApplications must then take care to configure both ends of every link 2018c2ecf20Sopenharmony_ciexplicitly with compatible formats. Identical formats on the two ends of 2028c2ecf20Sopenharmony_cia link are guaranteed to be compatible. Drivers are free to accept 2038c2ecf20Sopenharmony_cidifferent formats matching device requirements as being compatible. 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci:ref:`sample-pipeline-config` shows a sample configuration sequence 2068c2ecf20Sopenharmony_cifor the pipeline described in :ref:`pipeline-scaling` (table columns 2078c2ecf20Sopenharmony_cilist entity names and pad numbers). 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci.. raw:: latex 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci \scriptsize 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci.. tabularcolumns:: |p{2.0cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}| 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci.. _sample-pipeline-config: 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci.. flat-table:: Sample Pipeline Configuration 2198c2ecf20Sopenharmony_ci :header-rows: 1 2208c2ecf20Sopenharmony_ci :stub-columns: 0 2218c2ecf20Sopenharmony_ci :widths: 5 5 5 5 5 5 5 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci * - 2248c2ecf20Sopenharmony_ci - Sensor/0 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci format 2278c2ecf20Sopenharmony_ci - Frontend/0 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci format 2308c2ecf20Sopenharmony_ci - Frontend/1 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci format 2338c2ecf20Sopenharmony_ci - Scaler/0 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci format 2368c2ecf20Sopenharmony_ci - Scaler/0 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci compose selection rectangle 2398c2ecf20Sopenharmony_ci - Scaler/1 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci format 2428c2ecf20Sopenharmony_ci * - Initial state 2438c2ecf20Sopenharmony_ci - 2048x1536 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci SGRBG8_1X8 2468c2ecf20Sopenharmony_ci - (default) 2478c2ecf20Sopenharmony_ci - (default) 2488c2ecf20Sopenharmony_ci - (default) 2498c2ecf20Sopenharmony_ci - (default) 2508c2ecf20Sopenharmony_ci - (default) 2518c2ecf20Sopenharmony_ci * - Configure frontend sink format 2528c2ecf20Sopenharmony_ci - 2048x1536 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci SGRBG8_1X8 2558c2ecf20Sopenharmony_ci - *2048x1536* 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci *SGRBG8_1X8* 2588c2ecf20Sopenharmony_ci - *2046x1534* 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci *SGRBG8_1X8* 2618c2ecf20Sopenharmony_ci - (default) 2628c2ecf20Sopenharmony_ci - (default) 2638c2ecf20Sopenharmony_ci - (default) 2648c2ecf20Sopenharmony_ci * - Configure scaler sink format 2658c2ecf20Sopenharmony_ci - 2048x1536 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci SGRBG8_1X8 2688c2ecf20Sopenharmony_ci - 2048x1536 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci SGRBG8_1X8 2718c2ecf20Sopenharmony_ci - 2046x1534 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci SGRBG8_1X8 2748c2ecf20Sopenharmony_ci - *2046x1534* 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci *SGRBG8_1X8* 2778c2ecf20Sopenharmony_ci - *0,0/2046x1534* 2788c2ecf20Sopenharmony_ci - *2046x1534* 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci *SGRBG8_1X8* 2818c2ecf20Sopenharmony_ci * - Configure scaler sink compose selection 2828c2ecf20Sopenharmony_ci - 2048x1536 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci SGRBG8_1X8 2858c2ecf20Sopenharmony_ci - 2048x1536 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci SGRBG8_1X8 2888c2ecf20Sopenharmony_ci - 2046x1534 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci SGRBG8_1X8 2918c2ecf20Sopenharmony_ci - 2046x1534 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci SGRBG8_1X8 2948c2ecf20Sopenharmony_ci - *0,0/1280x960* 2958c2ecf20Sopenharmony_ci - *1280x960* 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci *SGRBG8_1X8* 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci.. raw:: latex 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci \normalsize 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci1. Initial state. The sensor source pad format is set to its native 3MP 3048c2ecf20Sopenharmony_ci size and V4L2_MBUS_FMT_SGRBG8_1X8 media bus code. Formats on the 3058c2ecf20Sopenharmony_ci host frontend and scaler sink and source pads have the default 3068c2ecf20Sopenharmony_ci values, as well as the compose rectangle on the scaler's sink pad. 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci2. The application configures the frontend sink pad format's size to 3098c2ecf20Sopenharmony_ci 2048x1536 and its media bus code to V4L2_MBUS_FMT_SGRBG_1X8. The 3108c2ecf20Sopenharmony_ci driver propagates the format to the frontend source pad. 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci3. The application configures the scaler sink pad format's size to 3138c2ecf20Sopenharmony_ci 2046x1534 and the media bus code to V4L2_MBUS_FMT_SGRBG_1X8 to 3148c2ecf20Sopenharmony_ci match the frontend source size and media bus code. The media bus code 3158c2ecf20Sopenharmony_ci on the sink pad is set to V4L2_MBUS_FMT_SGRBG_1X8. The driver 3168c2ecf20Sopenharmony_ci propagates the size to the compose selection rectangle on the 3178c2ecf20Sopenharmony_ci scaler's sink pad, and the format to the scaler source pad. 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci4. The application configures the size of the compose selection 3208c2ecf20Sopenharmony_ci rectangle of the scaler's sink pad 1280x960. The driver propagates 3218c2ecf20Sopenharmony_ci the size to the scaler's source pad format. 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ciWhen satisfied with the try results, applications can set the active 3248c2ecf20Sopenharmony_ciformats by setting the ``which`` argument to 3258c2ecf20Sopenharmony_ci``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try 3268c2ecf20Sopenharmony_ciformats by drivers. To avoid modifying the hardware state during format 3278c2ecf20Sopenharmony_cinegotiation, applications should negotiate try formats first and then 3288c2ecf20Sopenharmony_cimodify the active settings using the try formats returned during the 3298c2ecf20Sopenharmony_cilast negotiation iteration. This guarantees that the active format will 3308c2ecf20Sopenharmony_cibe applied as-is by the driver without being modified. 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci.. _v4l2-subdev-selections: 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ciSelections: cropping, scaling and composition 3368c2ecf20Sopenharmony_ci--------------------------------------------- 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ciMany sub-devices support cropping frames on their input or output pads 3398c2ecf20Sopenharmony_ci(or possible even on both). Cropping is used to select the area of 3408c2ecf20Sopenharmony_ciinterest in an image, typically on an image sensor or a video decoder. 3418c2ecf20Sopenharmony_ciIt can also be used as part of digital zoom implementations to select 3428c2ecf20Sopenharmony_cithe area of the image that will be scaled up. 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ciCrop settings are defined by a crop rectangle and represented in a 3458c2ecf20Sopenharmony_cistruct :c:type:`v4l2_rect` by the coordinates of the top 3468c2ecf20Sopenharmony_cileft corner and the rectangle size. Both the coordinates and sizes are 3478c2ecf20Sopenharmony_ciexpressed in pixels. 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ciAs for pad formats, drivers store try and active rectangles for the 3508c2ecf20Sopenharmony_ciselection targets :ref:`v4l2-selections-common`. 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ciOn sink pads, cropping is applied relative to the current pad format. 3538c2ecf20Sopenharmony_ciThe pad format represents the image size as received by the sub-device 3548c2ecf20Sopenharmony_cifrom the previous block in the pipeline, and the crop rectangle 3558c2ecf20Sopenharmony_cirepresents the sub-image that will be transmitted further inside the 3568c2ecf20Sopenharmony_cisub-device for processing. 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ciThe scaling operation changes the size of the image by scaling it to new 3598c2ecf20Sopenharmony_cidimensions. The scaling ratio isn't specified explicitly, but is implied 3608c2ecf20Sopenharmony_cifrom the original and scaled image sizes. Both sizes are represented by 3618c2ecf20Sopenharmony_cistruct :c:type:`v4l2_rect`. 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ciScaling support is optional. When supported by a subdev, the crop 3648c2ecf20Sopenharmony_cirectangle on the subdev's sink pad is scaled to the size configured 3658c2ecf20Sopenharmony_ciusing the 3668c2ecf20Sopenharmony_ci:ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL 3678c2ecf20Sopenharmony_ciusing ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the 3688c2ecf20Sopenharmony_cisubdev supports scaling but not composing, the top and left values are 3698c2ecf20Sopenharmony_cinot used and must always be set to zero. 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ciOn source pads, cropping is similar to sink pads, with the exception 3728c2ecf20Sopenharmony_cithat the source size from which the cropping is performed, is the 3738c2ecf20Sopenharmony_ciCOMPOSE rectangle on the sink pad. In both sink and source pads, the 3748c2ecf20Sopenharmony_cicrop rectangle must be entirely contained inside the source image size 3758c2ecf20Sopenharmony_cifor the crop operation. 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ciThe drivers should always use the closest possible rectangle the user 3788c2ecf20Sopenharmony_cirequests on all selection targets, unless specifically told otherwise. 3798c2ecf20Sopenharmony_ci``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round 3808c2ecf20Sopenharmony_cithe image size either up or down. :ref:`v4l2-selection-flags` 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ciTypes of selection targets 3848c2ecf20Sopenharmony_ci-------------------------- 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ciActual targets 3888c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^ 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ciActual targets (without a postfix) reflect the actual hardware 3918c2ecf20Sopenharmony_ciconfiguration at any point of time. There is a BOUNDS target 3928c2ecf20Sopenharmony_cicorresponding to every actual target. 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ciBOUNDS targets 3968c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^ 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ciBOUNDS targets is the smallest rectangle that contains all valid actual 3998c2ecf20Sopenharmony_cirectangles. It may not be possible to set the actual rectangle as large 4008c2ecf20Sopenharmony_cias the BOUNDS rectangle, however. This may be because e.g. a sensor's 4018c2ecf20Sopenharmony_cipixel array is not rectangular but cross-shaped or round. The maximum 4028c2ecf20Sopenharmony_cisize may also be smaller than the BOUNDS rectangle. 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ciOrder of configuration and format propagation 4068c2ecf20Sopenharmony_ci--------------------------------------------- 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ciInside subdevs, the order of image processing steps will always be from 4098c2ecf20Sopenharmony_cithe sink pad towards the source pad. This is also reflected in the order 4108c2ecf20Sopenharmony_ciin which the configuration must be performed by the user: the changes 4118c2ecf20Sopenharmony_cimade will be propagated to any subsequent stages. If this behaviour is 4128c2ecf20Sopenharmony_cinot desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This 4138c2ecf20Sopenharmony_ciflag causes no propagation of the changes are allowed in any 4148c2ecf20Sopenharmony_cicircumstances. This may also cause the accessed rectangle to be adjusted 4158c2ecf20Sopenharmony_ciby the driver, depending on the properties of the underlying hardware. 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ciThe coordinates to a step always refer to the actual size of the 4188c2ecf20Sopenharmony_ciprevious step. The exception to this rule is the sink compose 4198c2ecf20Sopenharmony_cirectangle, which refers to the sink compose bounds rectangle --- if it 4208c2ecf20Sopenharmony_ciis supported by the hardware. 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci1. Sink pad format. The user configures the sink pad format. This format 4238c2ecf20Sopenharmony_ci defines the parameters of the image the entity receives through the 4248c2ecf20Sopenharmony_ci pad for further processing. 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci2. Sink pad actual crop selection. The sink pad crop defines the crop 4278c2ecf20Sopenharmony_ci performed to the sink pad format. 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci3. Sink pad actual compose selection. The size of the sink pad compose 4308c2ecf20Sopenharmony_ci rectangle defines the scaling ratio compared to the size of the sink 4318c2ecf20Sopenharmony_ci pad crop rectangle. The location of the compose rectangle specifies 4328c2ecf20Sopenharmony_ci the location of the actual sink compose rectangle in the sink compose 4338c2ecf20Sopenharmony_ci bounds rectangle. 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci4. Source pad actual crop selection. Crop on the source pad defines crop 4368c2ecf20Sopenharmony_ci performed to the image in the sink compose bounds rectangle. 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci5. Source pad format. The source pad format defines the output pixel 4398c2ecf20Sopenharmony_ci format of the subdev, as well as the other parameters with the 4408c2ecf20Sopenharmony_ci exception of the image width and height. Width and height are defined 4418c2ecf20Sopenharmony_ci by the size of the source pad actual crop selection. 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ciAccessing any of the above rectangles not supported by the subdev will 4448c2ecf20Sopenharmony_cireturn ``EINVAL``. Any rectangle referring to a previous unsupported 4458c2ecf20Sopenharmony_cirectangle coordinates will instead refer to the previous supported 4468c2ecf20Sopenharmony_cirectangle. For example, if sink crop is not supported, the compose 4478c2ecf20Sopenharmony_ciselection will refer to the sink pad format dimensions instead. 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci.. _subdev-image-processing-crop: 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci.. kernel-figure:: subdev-image-processing-crop.svg 4538c2ecf20Sopenharmony_ci :alt: subdev-image-processing-crop.svg 4548c2ecf20Sopenharmony_ci :align: center 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci **Figure 4.5. Image processing in subdevs: simple crop example** 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ciIn the above example, the subdev supports cropping on its sink pad. To 4598c2ecf20Sopenharmony_ciconfigure it, the user sets the media bus format on the subdev's sink 4608c2ecf20Sopenharmony_cipad. Now the actual crop rectangle can be set on the sink pad --- the 4618c2ecf20Sopenharmony_cilocation and size of this rectangle reflect the location and size of a 4628c2ecf20Sopenharmony_cirectangle to be cropped from the sink format. The size of the sink crop 4638c2ecf20Sopenharmony_cirectangle will also be the size of the format of the subdev's source 4648c2ecf20Sopenharmony_cipad. 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci.. _subdev-image-processing-scaling-multi-source: 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci.. kernel-figure:: subdev-image-processing-scaling-multi-source.svg 4708c2ecf20Sopenharmony_ci :alt: subdev-image-processing-scaling-multi-source.svg 4718c2ecf20Sopenharmony_ci :align: center 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci **Figure 4.6. Image processing in subdevs: scaling with multiple sources** 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ciIn this example, the subdev is capable of first cropping, then scaling 4768c2ecf20Sopenharmony_ciand finally cropping for two source pads individually from the resulting 4778c2ecf20Sopenharmony_ciscaled image. The location of the scaled image in the cropped image is 4788c2ecf20Sopenharmony_ciignored in sink compose target. Both of the locations of the source crop 4798c2ecf20Sopenharmony_cirectangles refer to the sink scaling rectangle, independently cropping 4808c2ecf20Sopenharmony_cian area at location specified by the source crop rectangle from it. 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci.. _subdev-image-processing-full: 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci.. kernel-figure:: subdev-image-processing-full.svg 4868c2ecf20Sopenharmony_ci :alt: subdev-image-processing-full.svg 4878c2ecf20Sopenharmony_ci :align: center 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci **Figure 4.7. Image processing in subdevs: scaling and composition with multiple sinks and sources** 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ciThe subdev driver supports two sink pads and two source pads. The images 4928c2ecf20Sopenharmony_cifrom both of the sink pads are individually cropped, then scaled and 4938c2ecf20Sopenharmony_cifurther composed on the composition bounds rectangle. From that, two 4948c2ecf20Sopenharmony_ciindependent streams are cropped and sent out of the subdev from the 4958c2ecf20Sopenharmony_cisource pads. 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci.. toctree:: 4998c2ecf20Sopenharmony_ci :maxdepth: 1 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci subdev-formats 502