dev-subdev.xml 22 KB

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