dev-subdev.rst 18 KB

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