1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun.. _subdev: 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun******************** 6*4882a593SmuzhiyunSub-device Interface 7*4882a593Smuzhiyun******************** 8*4882a593Smuzhiyun 9*4882a593SmuzhiyunThe complex nature of V4L2 devices, where hardware is often made of 10*4882a593Smuzhiyunseveral integrated circuits that need to interact with each other in a 11*4882a593Smuzhiyuncontrolled way, leads to complex V4L2 drivers. The drivers usually 12*4882a593Smuzhiyunreflect the hardware model in software, and model the different hardware 13*4882a593Smuzhiyuncomponents as software blocks called sub-devices. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunV4L2 sub-devices are usually kernel-only objects. If the V4L2 driver 16*4882a593Smuzhiyunimplements the media device API, they will automatically inherit from 17*4882a593Smuzhiyunmedia entities. Applications will be able to enumerate the sub-devices 18*4882a593Smuzhiyunand discover the hardware topology using the media entities, pads and 19*4882a593Smuzhiyunlinks enumeration API. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunIn addition to make sub-devices discoverable, drivers can also choose to 22*4882a593Smuzhiyunmake them directly configurable by applications. When both the 23*4882a593Smuzhiyunsub-device driver and the V4L2 device driver support this, sub-devices 24*4882a593Smuzhiyunwill feature a character device node on which ioctls can be called to 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun- query, read and write sub-devices controls 27*4882a593Smuzhiyun 28*4882a593Smuzhiyun- subscribe and unsubscribe to events and retrieve them 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun- negotiate image formats on individual pads 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunSub-device character device nodes, conventionally named 33*4882a593Smuzhiyun``/dev/v4l-subdev*``, use major number 81. 34*4882a593Smuzhiyun 35*4882a593SmuzhiyunDrivers may opt to limit the sub-device character devices to only expose 36*4882a593Smuzhiyunoperations that do not modify the device state. In such a case the sub-devices 37*4882a593Smuzhiyunare referred to as ``read-only`` in the rest of this documentation, and the 38*4882a593Smuzhiyunrelated restrictions are documented in individual ioctls. 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunControls 42*4882a593Smuzhiyun======== 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunMost V4L2 controls are implemented by sub-device hardware. Drivers 45*4882a593Smuzhiyunusually merge all controls and expose them through video device nodes. 46*4882a593SmuzhiyunApplications can control all sub-devices through a single interface. 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunComplex devices sometimes implement the same control in different pieces 49*4882a593Smuzhiyunof hardware. This situation is common in embedded platforms, where both 50*4882a593Smuzhiyunsensors and image processing hardware implement identical functions, 51*4882a593Smuzhiyunsuch as contrast adjustment, white balance or faulty pixels correction. 52*4882a593SmuzhiyunAs the V4L2 controls API doesn't support several identical controls in a 53*4882a593Smuzhiyunsingle device, all but one of the identical controls are hidden. 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunApplications can access those hidden controls through the sub-device 56*4882a593Smuzhiyunnode with the V4L2 control API described in :ref:`control`. The ioctls 57*4882a593Smuzhiyunbehave identically as when issued on V4L2 device nodes, with the 58*4882a593Smuzhiyunexception that they deal only with controls implemented in the 59*4882a593Smuzhiyunsub-device. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunDepending on the driver, those controls might also be exposed through 62*4882a593Smuzhiyunone (or several) V4L2 device nodes. 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunEvents 66*4882a593Smuzhiyun====== 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunV4L2 sub-devices can notify applications of events as described in 69*4882a593Smuzhiyun:ref:`event`. The API behaves identically as when used on V4L2 device 70*4882a593Smuzhiyunnodes, with the exception that it only deals with events generated by 71*4882a593Smuzhiyunthe sub-device. Depending on the driver, those events might also be 72*4882a593Smuzhiyunreported on one (or several) V4L2 device nodes. 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun.. _pad-level-formats: 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunPad-level Formats 78*4882a593Smuzhiyun================= 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun.. warning:: 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun Pad-level formats are only applicable to very complex devices that 83*4882a593Smuzhiyun need to expose low-level format configuration to user space. Generic 84*4882a593Smuzhiyun V4L2 applications do *not* need to use the API described in this 85*4882a593Smuzhiyun section. 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun.. note:: 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun For the purpose of this section, the term *format* means the 90*4882a593Smuzhiyun combination of media bus data format, frame width and frame height. 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunImage formats are typically negotiated on video capture and output 93*4882a593Smuzhiyundevices using the format and 94*4882a593Smuzhiyun:ref:`selection <VIDIOC_SUBDEV_G_SELECTION>` ioctls. The driver is 95*4882a593Smuzhiyunresponsible for configuring every block in the video pipeline according 96*4882a593Smuzhiyunto the requested format at the pipeline input and/or output. 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunFor complex devices, such as often found in embedded systems, identical 99*4882a593Smuzhiyunimage sizes at the output of a pipeline can be achieved using different 100*4882a593Smuzhiyunhardware configurations. One such example is shown on 101*4882a593Smuzhiyun:ref:`pipeline-scaling`, where image scaling can be performed on both 102*4882a593Smuzhiyunthe video sensor and the host image processing hardware. 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun.. _pipeline-scaling: 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun.. kernel-figure:: pipeline.dot 108*4882a593Smuzhiyun :alt: pipeline.dot 109*4882a593Smuzhiyun :align: center 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun Image Format Negotiation on Pipelines 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun High quality and high speed pipeline configuration 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunThe sensor scaler is usually of less quality than the host scaler, but 118*4882a593Smuzhiyunscaling on the sensor is required to achieve higher frame rates. 119*4882a593SmuzhiyunDepending on the use case (quality vs. speed), the pipeline must be 120*4882a593Smuzhiyunconfigured differently. Applications need to configure the formats at 121*4882a593Smuzhiyunevery point in the pipeline explicitly. 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunDrivers that implement the :ref:`media API <media-controller-intro>` 124*4882a593Smuzhiyuncan expose pad-level image format configuration to applications. When 125*4882a593Smuzhiyunthey do, applications can use the 126*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 127*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls. to 128*4882a593Smuzhiyunnegotiate formats on a per-pad basis. 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunApplications are responsible for configuring coherent parameters on the 131*4882a593Smuzhiyunwhole pipeline and making sure that connected pads have compatible 132*4882a593Smuzhiyunformats. The pipeline is checked for formats mismatch at 133*4882a593Smuzhiyun:ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` time, and an ``EPIPE`` error 134*4882a593Smuzhiyuncode is then returned if the configuration is invalid. 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunPad-level image format configuration support can be tested by calling 137*4882a593Smuzhiyunthe :ref:`VIDIOC_SUBDEV_G_FMT` ioctl on pad 138*4882a593Smuzhiyun0. If the driver returns an ``EINVAL`` error code pad-level format 139*4882a593Smuzhiyunconfiguration is not supported by the sub-device. 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunFormat Negotiation 143*4882a593Smuzhiyun------------------ 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunAcceptable formats on pads can (and usually do) depend on a number of 146*4882a593Smuzhiyunexternal parameters, such as formats on other pads, active links, or 147*4882a593Smuzhiyuneven controls. Finding a combination of formats on all pads in a video 148*4882a593Smuzhiyunpipeline, acceptable to both application and driver, can't rely on 149*4882a593Smuzhiyunformats enumeration only. A format negotiation mechanism is required. 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunCentral to the format negotiation mechanism are the get/set format 152*4882a593Smuzhiyunoperations. When called with the ``which`` argument set to 153*4882a593Smuzhiyun:ref:`V4L2_SUBDEV_FORMAT_TRY <VIDIOC_SUBDEV_G_FMT>`, the 154*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` and 155*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctls operate on 156*4882a593Smuzhiyuna set of formats parameters that are not connected to the hardware 157*4882a593Smuzhiyunconfiguration. Modifying those 'try' formats leaves the device state 158*4882a593Smuzhiyununtouched (this applies to both the software state stored in the driver 159*4882a593Smuzhiyunand the hardware state stored in the device itself). 160*4882a593Smuzhiyun 161*4882a593SmuzhiyunWhile not kept as part of the device state, try formats are stored in 162*4882a593Smuzhiyunthe sub-device file handles. A 163*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_G_FMT <VIDIOC_SUBDEV_G_FMT>` call will return 164*4882a593Smuzhiyunthe last try format set *on the same sub-device file handle*. Several 165*4882a593Smuzhiyunapplications querying the same sub-device at the same time will thus not 166*4882a593Smuzhiyuninteract with each other. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunTo find out whether a particular format is supported by the device, 169*4882a593Smuzhiyunapplications use the 170*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` ioctl. Drivers 171*4882a593Smuzhiyunverify and, if needed, change the requested ``format`` based on device 172*4882a593Smuzhiyunrequirements and return the possibly modified value. Applications can 173*4882a593Smuzhiyunthen choose to try a different format or accept the returned value and 174*4882a593Smuzhiyuncontinue. 175*4882a593Smuzhiyun 176*4882a593SmuzhiyunFormats returned by the driver during a negotiation iteration are 177*4882a593Smuzhiyunguaranteed to be supported by the device. In particular, drivers 178*4882a593Smuzhiyunguarantee that a returned format will not be further changed if passed 179*4882a593Smuzhiyunto an :ref:`VIDIOC_SUBDEV_S_FMT <VIDIOC_SUBDEV_G_FMT>` call as-is 180*4882a593Smuzhiyun(as long as external parameters, such as formats on other pads or links' 181*4882a593Smuzhiyunconfiguration are not changed). 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunDrivers automatically propagate formats inside sub-devices. When a try 184*4882a593Smuzhiyunor active format is set on a pad, corresponding formats on other pads of 185*4882a593Smuzhiyunthe same sub-device can be modified by the driver. Drivers are free to 186*4882a593Smuzhiyunmodify formats as required by the device. However, they should comply 187*4882a593Smuzhiyunwith the following rules when possible: 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun- Formats should be propagated from sink pads to source pads. Modifying 190*4882a593Smuzhiyun a format on a source pad should not modify the format on any sink 191*4882a593Smuzhiyun pad. 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun- Sub-devices that scale frames using variable scaling factors should 194*4882a593Smuzhiyun reset the scale factors to default values when sink pads formats are 195*4882a593Smuzhiyun modified. If the 1:1 scaling ratio is supported, this means that 196*4882a593Smuzhiyun source pads formats should be reset to the sink pads formats. 197*4882a593Smuzhiyun 198*4882a593SmuzhiyunFormats are not propagated across links, as that would involve 199*4882a593Smuzhiyunpropagating them from one sub-device file handle to another. 200*4882a593SmuzhiyunApplications must then take care to configure both ends of every link 201*4882a593Smuzhiyunexplicitly with compatible formats. Identical formats on the two ends of 202*4882a593Smuzhiyuna link are guaranteed to be compatible. Drivers are free to accept 203*4882a593Smuzhiyundifferent formats matching device requirements as being compatible. 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun:ref:`sample-pipeline-config` shows a sample configuration sequence 206*4882a593Smuzhiyunfor the pipeline described in :ref:`pipeline-scaling` (table columns 207*4882a593Smuzhiyunlist entity names and pad numbers). 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun.. raw:: latex 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun \scriptsize 213*4882a593Smuzhiyun 214*4882a593Smuzhiyun.. tabularcolumns:: |p{2.0cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}|p{2.3cm}| 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun.. _sample-pipeline-config: 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun.. flat-table:: Sample Pipeline Configuration 219*4882a593Smuzhiyun :header-rows: 1 220*4882a593Smuzhiyun :stub-columns: 0 221*4882a593Smuzhiyun :widths: 5 5 5 5 5 5 5 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun * - 224*4882a593Smuzhiyun - Sensor/0 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun format 227*4882a593Smuzhiyun - Frontend/0 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun format 230*4882a593Smuzhiyun - Frontend/1 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun format 233*4882a593Smuzhiyun - Scaler/0 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun format 236*4882a593Smuzhiyun - Scaler/0 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun compose selection rectangle 239*4882a593Smuzhiyun - Scaler/1 240*4882a593Smuzhiyun 241*4882a593Smuzhiyun format 242*4882a593Smuzhiyun * - Initial state 243*4882a593Smuzhiyun - 2048x1536 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun SGRBG8_1X8 246*4882a593Smuzhiyun - (default) 247*4882a593Smuzhiyun - (default) 248*4882a593Smuzhiyun - (default) 249*4882a593Smuzhiyun - (default) 250*4882a593Smuzhiyun - (default) 251*4882a593Smuzhiyun * - Configure frontend sink format 252*4882a593Smuzhiyun - 2048x1536 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun SGRBG8_1X8 255*4882a593Smuzhiyun - *2048x1536* 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun *SGRBG8_1X8* 258*4882a593Smuzhiyun - *2046x1534* 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun *SGRBG8_1X8* 261*4882a593Smuzhiyun - (default) 262*4882a593Smuzhiyun - (default) 263*4882a593Smuzhiyun - (default) 264*4882a593Smuzhiyun * - Configure scaler sink format 265*4882a593Smuzhiyun - 2048x1536 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun SGRBG8_1X8 268*4882a593Smuzhiyun - 2048x1536 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun SGRBG8_1X8 271*4882a593Smuzhiyun - 2046x1534 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun SGRBG8_1X8 274*4882a593Smuzhiyun - *2046x1534* 275*4882a593Smuzhiyun 276*4882a593Smuzhiyun *SGRBG8_1X8* 277*4882a593Smuzhiyun - *0,0/2046x1534* 278*4882a593Smuzhiyun - *2046x1534* 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun *SGRBG8_1X8* 281*4882a593Smuzhiyun * - Configure scaler sink compose selection 282*4882a593Smuzhiyun - 2048x1536 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun SGRBG8_1X8 285*4882a593Smuzhiyun - 2048x1536 286*4882a593Smuzhiyun 287*4882a593Smuzhiyun SGRBG8_1X8 288*4882a593Smuzhiyun - 2046x1534 289*4882a593Smuzhiyun 290*4882a593Smuzhiyun SGRBG8_1X8 291*4882a593Smuzhiyun - 2046x1534 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun SGRBG8_1X8 294*4882a593Smuzhiyun - *0,0/1280x960* 295*4882a593Smuzhiyun - *1280x960* 296*4882a593Smuzhiyun 297*4882a593Smuzhiyun *SGRBG8_1X8* 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun.. raw:: latex 300*4882a593Smuzhiyun 301*4882a593Smuzhiyun \normalsize 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun1. Initial state. The sensor source pad format is set to its native 3MP 304*4882a593Smuzhiyun size and V4L2_MBUS_FMT_SGRBG8_1X8 media bus code. Formats on the 305*4882a593Smuzhiyun host frontend and scaler sink and source pads have the default 306*4882a593Smuzhiyun values, as well as the compose rectangle on the scaler's sink pad. 307*4882a593Smuzhiyun 308*4882a593Smuzhiyun2. The application configures the frontend sink pad format's size to 309*4882a593Smuzhiyun 2048x1536 and its media bus code to V4L2_MBUS_FMT_SGRBG_1X8. The 310*4882a593Smuzhiyun driver propagates the format to the frontend source pad. 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun3. The application configures the scaler sink pad format's size to 313*4882a593Smuzhiyun 2046x1534 and the media bus code to V4L2_MBUS_FMT_SGRBG_1X8 to 314*4882a593Smuzhiyun match the frontend source size and media bus code. The media bus code 315*4882a593Smuzhiyun on the sink pad is set to V4L2_MBUS_FMT_SGRBG_1X8. The driver 316*4882a593Smuzhiyun propagates the size to the compose selection rectangle on the 317*4882a593Smuzhiyun scaler's sink pad, and the format to the scaler source pad. 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun4. The application configures the size of the compose selection 320*4882a593Smuzhiyun rectangle of the scaler's sink pad 1280x960. The driver propagates 321*4882a593Smuzhiyun the size to the scaler's source pad format. 322*4882a593Smuzhiyun 323*4882a593SmuzhiyunWhen satisfied with the try results, applications can set the active 324*4882a593Smuzhiyunformats by setting the ``which`` argument to 325*4882a593Smuzhiyun``V4L2_SUBDEV_FORMAT_ACTIVE``. Active formats are changed exactly as try 326*4882a593Smuzhiyunformats by drivers. To avoid modifying the hardware state during format 327*4882a593Smuzhiyunnegotiation, applications should negotiate try formats first and then 328*4882a593Smuzhiyunmodify the active settings using the try formats returned during the 329*4882a593Smuzhiyunlast negotiation iteration. This guarantees that the active format will 330*4882a593Smuzhiyunbe applied as-is by the driver without being modified. 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun.. _v4l2-subdev-selections: 334*4882a593Smuzhiyun 335*4882a593SmuzhiyunSelections: cropping, scaling and composition 336*4882a593Smuzhiyun--------------------------------------------- 337*4882a593Smuzhiyun 338*4882a593SmuzhiyunMany sub-devices support cropping frames on their input or output pads 339*4882a593Smuzhiyun(or possible even on both). Cropping is used to select the area of 340*4882a593Smuzhiyuninterest in an image, typically on an image sensor or a video decoder. 341*4882a593SmuzhiyunIt can also be used as part of digital zoom implementations to select 342*4882a593Smuzhiyunthe area of the image that will be scaled up. 343*4882a593Smuzhiyun 344*4882a593SmuzhiyunCrop settings are defined by a crop rectangle and represented in a 345*4882a593Smuzhiyunstruct :c:type:`v4l2_rect` by the coordinates of the top 346*4882a593Smuzhiyunleft corner and the rectangle size. Both the coordinates and sizes are 347*4882a593Smuzhiyunexpressed in pixels. 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunAs for pad formats, drivers store try and active rectangles for the 350*4882a593Smuzhiyunselection targets :ref:`v4l2-selections-common`. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunOn sink pads, cropping is applied relative to the current pad format. 353*4882a593SmuzhiyunThe pad format represents the image size as received by the sub-device 354*4882a593Smuzhiyunfrom the previous block in the pipeline, and the crop rectangle 355*4882a593Smuzhiyunrepresents the sub-image that will be transmitted further inside the 356*4882a593Smuzhiyunsub-device for processing. 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunThe scaling operation changes the size of the image by scaling it to new 359*4882a593Smuzhiyundimensions. The scaling ratio isn't specified explicitly, but is implied 360*4882a593Smuzhiyunfrom the original and scaled image sizes. Both sizes are represented by 361*4882a593Smuzhiyunstruct :c:type:`v4l2_rect`. 362*4882a593Smuzhiyun 363*4882a593SmuzhiyunScaling support is optional. When supported by a subdev, the crop 364*4882a593Smuzhiyunrectangle on the subdev's sink pad is scaled to the size configured 365*4882a593Smuzhiyunusing the 366*4882a593Smuzhiyun:ref:`VIDIOC_SUBDEV_S_SELECTION <VIDIOC_SUBDEV_G_SELECTION>` IOCTL 367*4882a593Smuzhiyunusing ``V4L2_SEL_TGT_COMPOSE`` selection target on the same pad. If the 368*4882a593Smuzhiyunsubdev supports scaling but not composing, the top and left values are 369*4882a593Smuzhiyunnot used and must always be set to zero. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunOn source pads, cropping is similar to sink pads, with the exception 372*4882a593Smuzhiyunthat the source size from which the cropping is performed, is the 373*4882a593SmuzhiyunCOMPOSE rectangle on the sink pad. In both sink and source pads, the 374*4882a593Smuzhiyuncrop rectangle must be entirely contained inside the source image size 375*4882a593Smuzhiyunfor the crop operation. 376*4882a593Smuzhiyun 377*4882a593SmuzhiyunThe drivers should always use the closest possible rectangle the user 378*4882a593Smuzhiyunrequests on all selection targets, unless specifically told otherwise. 379*4882a593Smuzhiyun``V4L2_SEL_FLAG_GE`` and ``V4L2_SEL_FLAG_LE`` flags may be used to round 380*4882a593Smuzhiyunthe image size either up or down. :ref:`v4l2-selection-flags` 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun 383*4882a593SmuzhiyunTypes of selection targets 384*4882a593Smuzhiyun-------------------------- 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun 387*4882a593SmuzhiyunActual targets 388*4882a593Smuzhiyun^^^^^^^^^^^^^^ 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunActual targets (without a postfix) reflect the actual hardware 391*4882a593Smuzhiyunconfiguration at any point of time. There is a BOUNDS target 392*4882a593Smuzhiyuncorresponding to every actual target. 393*4882a593Smuzhiyun 394*4882a593Smuzhiyun 395*4882a593SmuzhiyunBOUNDS targets 396*4882a593Smuzhiyun^^^^^^^^^^^^^^ 397*4882a593Smuzhiyun 398*4882a593SmuzhiyunBOUNDS targets is the smallest rectangle that contains all valid actual 399*4882a593Smuzhiyunrectangles. It may not be possible to set the actual rectangle as large 400*4882a593Smuzhiyunas the BOUNDS rectangle, however. This may be because e.g. a sensor's 401*4882a593Smuzhiyunpixel array is not rectangular but cross-shaped or round. The maximum 402*4882a593Smuzhiyunsize may also be smaller than the BOUNDS rectangle. 403*4882a593Smuzhiyun 404*4882a593Smuzhiyun 405*4882a593SmuzhiyunOrder of configuration and format propagation 406*4882a593Smuzhiyun--------------------------------------------- 407*4882a593Smuzhiyun 408*4882a593SmuzhiyunInside subdevs, the order of image processing steps will always be from 409*4882a593Smuzhiyunthe sink pad towards the source pad. This is also reflected in the order 410*4882a593Smuzhiyunin which the configuration must be performed by the user: the changes 411*4882a593Smuzhiyunmade will be propagated to any subsequent stages. If this behaviour is 412*4882a593Smuzhiyunnot desired, the user must set ``V4L2_SEL_FLAG_KEEP_CONFIG`` flag. This 413*4882a593Smuzhiyunflag causes no propagation of the changes are allowed in any 414*4882a593Smuzhiyuncircumstances. This may also cause the accessed rectangle to be adjusted 415*4882a593Smuzhiyunby the driver, depending on the properties of the underlying hardware. 416*4882a593Smuzhiyun 417*4882a593SmuzhiyunThe coordinates to a step always refer to the actual size of the 418*4882a593Smuzhiyunprevious step. The exception to this rule is the sink compose 419*4882a593Smuzhiyunrectangle, which refers to the sink compose bounds rectangle --- if it 420*4882a593Smuzhiyunis supported by the hardware. 421*4882a593Smuzhiyun 422*4882a593Smuzhiyun1. Sink pad format. The user configures the sink pad format. This format 423*4882a593Smuzhiyun defines the parameters of the image the entity receives through the 424*4882a593Smuzhiyun pad for further processing. 425*4882a593Smuzhiyun 426*4882a593Smuzhiyun2. Sink pad actual crop selection. The sink pad crop defines the crop 427*4882a593Smuzhiyun performed to the sink pad format. 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun3. Sink pad actual compose selection. The size of the sink pad compose 430*4882a593Smuzhiyun rectangle defines the scaling ratio compared to the size of the sink 431*4882a593Smuzhiyun pad crop rectangle. The location of the compose rectangle specifies 432*4882a593Smuzhiyun the location of the actual sink compose rectangle in the sink compose 433*4882a593Smuzhiyun bounds rectangle. 434*4882a593Smuzhiyun 435*4882a593Smuzhiyun4. Source pad actual crop selection. Crop on the source pad defines crop 436*4882a593Smuzhiyun performed to the image in the sink compose bounds rectangle. 437*4882a593Smuzhiyun 438*4882a593Smuzhiyun5. Source pad format. The source pad format defines the output pixel 439*4882a593Smuzhiyun format of the subdev, as well as the other parameters with the 440*4882a593Smuzhiyun exception of the image width and height. Width and height are defined 441*4882a593Smuzhiyun by the size of the source pad actual crop selection. 442*4882a593Smuzhiyun 443*4882a593SmuzhiyunAccessing any of the above rectangles not supported by the subdev will 444*4882a593Smuzhiyunreturn ``EINVAL``. Any rectangle referring to a previous unsupported 445*4882a593Smuzhiyunrectangle coordinates will instead refer to the previous supported 446*4882a593Smuzhiyunrectangle. For example, if sink crop is not supported, the compose 447*4882a593Smuzhiyunselection will refer to the sink pad format dimensions instead. 448*4882a593Smuzhiyun 449*4882a593Smuzhiyun 450*4882a593Smuzhiyun.. _subdev-image-processing-crop: 451*4882a593Smuzhiyun 452*4882a593Smuzhiyun.. kernel-figure:: subdev-image-processing-crop.svg 453*4882a593Smuzhiyun :alt: subdev-image-processing-crop.svg 454*4882a593Smuzhiyun :align: center 455*4882a593Smuzhiyun 456*4882a593Smuzhiyun **Figure 4.5. Image processing in subdevs: simple crop example** 457*4882a593Smuzhiyun 458*4882a593SmuzhiyunIn the above example, the subdev supports cropping on its sink pad. To 459*4882a593Smuzhiyunconfigure it, the user sets the media bus format on the subdev's sink 460*4882a593Smuzhiyunpad. Now the actual crop rectangle can be set on the sink pad --- the 461*4882a593Smuzhiyunlocation and size of this rectangle reflect the location and size of a 462*4882a593Smuzhiyunrectangle to be cropped from the sink format. The size of the sink crop 463*4882a593Smuzhiyunrectangle will also be the size of the format of the subdev's source 464*4882a593Smuzhiyunpad. 465*4882a593Smuzhiyun 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun.. _subdev-image-processing-scaling-multi-source: 468*4882a593Smuzhiyun 469*4882a593Smuzhiyun.. kernel-figure:: subdev-image-processing-scaling-multi-source.svg 470*4882a593Smuzhiyun :alt: subdev-image-processing-scaling-multi-source.svg 471*4882a593Smuzhiyun :align: center 472*4882a593Smuzhiyun 473*4882a593Smuzhiyun **Figure 4.6. Image processing in subdevs: scaling with multiple sources** 474*4882a593Smuzhiyun 475*4882a593SmuzhiyunIn this example, the subdev is capable of first cropping, then scaling 476*4882a593Smuzhiyunand finally cropping for two source pads individually from the resulting 477*4882a593Smuzhiyunscaled image. The location of the scaled image in the cropped image is 478*4882a593Smuzhiyunignored in sink compose target. Both of the locations of the source crop 479*4882a593Smuzhiyunrectangles refer to the sink scaling rectangle, independently cropping 480*4882a593Smuzhiyunan area at location specified by the source crop rectangle from it. 481*4882a593Smuzhiyun 482*4882a593Smuzhiyun 483*4882a593Smuzhiyun.. _subdev-image-processing-full: 484*4882a593Smuzhiyun 485*4882a593Smuzhiyun.. kernel-figure:: subdev-image-processing-full.svg 486*4882a593Smuzhiyun :alt: subdev-image-processing-full.svg 487*4882a593Smuzhiyun :align: center 488*4882a593Smuzhiyun 489*4882a593Smuzhiyun **Figure 4.7. Image processing in subdevs: scaling and composition with multiple sinks and sources** 490*4882a593Smuzhiyun 491*4882a593SmuzhiyunThe subdev driver supports two sink pads and two source pads. The images 492*4882a593Smuzhiyunfrom both of the sink pads are individually cropped, then scaled and 493*4882a593Smuzhiyunfurther composed on the composition bounds rectangle. From that, two 494*4882a593Smuzhiyunindependent streams are cropped and sent out of the subdev from the 495*4882a593Smuzhiyunsource pads. 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun 498*4882a593Smuzhiyun.. toctree:: 499*4882a593Smuzhiyun :maxdepth: 1 500*4882a593Smuzhiyun 501*4882a593Smuzhiyun subdev-formats 502