dev-subdev.xml 21 KB

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