dev-subdev.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 output
  66. devices using the <link linkend="crop">cropping and scaling</link> ioctls.
  67. The driver is responsible for configuring every block in the video pipeline
  68. according to the requested format at the pipeline input and/or
  69. output.</para>
  70. <para>For complex devices, such as often found in embedded systems,
  71. identical image sizes at the output of a pipeline can be achieved using
  72. different hardware configurations. One such example is shown on
  73. <xref linkend="pipeline-scaling" />, where
  74. image scaling can be performed on both the video sensor and the host image
  75. processing hardware.</para>
  76. <figure id="pipeline-scaling">
  77. <title>Image Format Negotiation on Pipelines</title>
  78. <mediaobject>
  79. <imageobject>
  80. <imagedata fileref="pipeline.pdf" format="PS" />
  81. </imageobject>
  82. <imageobject>
  83. <imagedata fileref="pipeline.png" format="PNG" />
  84. </imageobject>
  85. <textobject>
  86. <phrase>High quality and high speed pipeline configuration</phrase>
  87. </textobject>
  88. </mediaobject>
  89. </figure>
  90. <para>The sensor scaler is usually of less quality than the host scaler, but
  91. scaling on the sensor is required to achieve higher frame rates. Depending
  92. on the use case (quality vs. speed), the pipeline must be configured
  93. differently. Applications need to configure the formats at every point in
  94. the pipeline explicitly.</para>
  95. <para>Drivers that implement the <link linkend="media-controller-intro">media
  96. API</link> can expose pad-level image format configuration to applications.
  97. When they do, applications can use the &VIDIOC-SUBDEV-G-FMT; and
  98. &VIDIOC-SUBDEV-S-FMT; ioctls. to negotiate formats on a per-pad basis.</para>
  99. <para>Applications are responsible for configuring coherent parameters on
  100. the whole pipeline and making sure that connected pads have compatible
  101. formats. The pipeline is checked for formats mismatch at &VIDIOC-STREAMON;
  102. time, and an &EPIPE; is then returned if the configuration is
  103. invalid.</para>
  104. <para>Pad-level image format configuration support can be tested by calling
  105. the &VIDIOC-SUBDEV-G-FMT; ioctl on pad 0. If the driver returns an &EINVAL;
  106. pad-level format configuration is not supported by the sub-device.</para>
  107. <section>
  108. <title>Format Negotiation</title>
  109. <para>Acceptable formats on pads can (and usually do) depend on a number
  110. of external parameters, such as formats on other pads, active links, or
  111. even controls. Finding a combination of formats on all pads in a video
  112. pipeline, acceptable to both application and driver, can't rely on formats
  113. enumeration only. A format negotiation mechanism is required.</para>
  114. <para>Central to the format negotiation mechanism are the get/set format
  115. operations. When called with the <structfield>which</structfield> argument
  116. set to <constant>V4L2_SUBDEV_FORMAT_TRY</constant>, the
  117. &VIDIOC-SUBDEV-G-FMT; and &VIDIOC-SUBDEV-S-FMT; ioctls operate on a set of
  118. formats parameters that are not connected to the hardware configuration.
  119. Modifying those 'try' formats leaves the device state untouched (this
  120. applies to both the software state stored in the driver and the hardware
  121. state stored in the device itself).</para>
  122. <para>While not kept as part of the device state, try formats are stored
  123. in the sub-device file handles. A &VIDIOC-SUBDEV-G-FMT; call will return
  124. the last try format set <emphasis>on the same sub-device file
  125. handle</emphasis>. Several applications querying the same sub-device at
  126. the same time will thus not interact with each other.</para>
  127. <para>To find out whether a particular format is supported by the device,
  128. applications use the &VIDIOC-SUBDEV-S-FMT; ioctl. Drivers verify and, if
  129. needed, change the requested <structfield>format</structfield> based on
  130. device requirements and return the possibly modified value. Applications
  131. can then choose to try a different format or accept the returned value and
  132. continue.</para>
  133. <para>Formats returned by the driver during a negotiation iteration are
  134. guaranteed to be supported by the device. In particular, drivers guarantee
  135. that a returned format will not be further changed if passed to an
  136. &VIDIOC-SUBDEV-S-FMT; call as-is (as long as external parameters, such as
  137. formats on other pads or links' configuration are not changed).</para>
  138. <para>Drivers automatically propagate formats inside sub-devices. When a
  139. try or active format is set on a pad, corresponding formats on other pads
  140. of the same sub-device can be modified by the driver. Drivers are free to
  141. modify formats as required by the device. However, they should comply with
  142. the following rules when possible:
  143. <itemizedlist>
  144. <listitem><para>Formats should be propagated from sink pads to source pads.
  145. Modifying a format on a source pad should not modify the format on any
  146. sink pad.</para></listitem>
  147. <listitem><para>Sub-devices that scale frames using variable scaling factors
  148. should reset the scale factors to default values when sink pads formats
  149. are modified. If the 1:1 scaling ratio is supported, this means that
  150. source pads formats should be reset to the sink pads formats.</para></listitem>
  151. </itemizedlist>
  152. </para>
  153. <para>Formats are not propagated across links, as that would involve
  154. propagating them from one sub-device file handle to another. Applications
  155. must then take care to configure both ends of every link explicitly with
  156. compatible formats. Identical formats on the two ends of a link are
  157. guaranteed to be compatible. Drivers are free to accept different formats
  158. matching device requirements as being compatible.</para>
  159. <para><xref linkend="sample-pipeline-config" />
  160. shows a sample configuration sequence for the pipeline described in
  161. <xref linkend="pipeline-scaling" /> (table
  162. columns list entity names and pad numbers).</para>
  163. <table pgwide="0" frame="none" id="sample-pipeline-config">
  164. <title>Sample Pipeline Configuration</title>
  165. <tgroup cols="3">
  166. <colspec colname="what"/>
  167. <colspec colname="sensor-0" />
  168. <colspec colname="frontend-0" />
  169. <colspec colname="frontend-1" />
  170. <colspec colname="scaler-0" />
  171. <colspec colname="scaler-1" />
  172. <thead>
  173. <row>
  174. <entry></entry>
  175. <entry>Sensor/0</entry>
  176. <entry>Frontend/0</entry>
  177. <entry>Frontend/1</entry>
  178. <entry>Scaler/0</entry>
  179. <entry>Scaler/1</entry>
  180. </row>
  181. </thead>
  182. <tbody valign="top">
  183. <row>
  184. <entry>Initial state</entry>
  185. <entry>2048x1536</entry>
  186. <entry>-</entry>
  187. <entry>-</entry>
  188. <entry>-</entry>
  189. <entry>-</entry>
  190. </row>
  191. <row>
  192. <entry>Configure frontend input</entry>
  193. <entry>2048x1536</entry>
  194. <entry><emphasis>2048x1536</emphasis></entry>
  195. <entry><emphasis>2046x1534</emphasis></entry>
  196. <entry>-</entry>
  197. <entry>-</entry>
  198. </row>
  199. <row>
  200. <entry>Configure scaler input</entry>
  201. <entry>2048x1536</entry>
  202. <entry>2048x1536</entry>
  203. <entry>2046x1534</entry>
  204. <entry><emphasis>2046x1534</emphasis></entry>
  205. <entry><emphasis>2046x1534</emphasis></entry>
  206. </row>
  207. <row>
  208. <entry>Configure scaler output</entry>
  209. <entry>2048x1536</entry>
  210. <entry>2048x1536</entry>
  211. <entry>2046x1534</entry>
  212. <entry>2046x1534</entry>
  213. <entry><emphasis>1280x960</emphasis></entry>
  214. </row>
  215. </tbody>
  216. </tgroup>
  217. </table>
  218. <para>
  219. <orderedlist>
  220. <listitem><para>Initial state. The sensor output is set to its native 3MP
  221. resolution. Resolutions on the host frontend and scaler input and output
  222. pads are undefined.</para></listitem>
  223. <listitem><para>The application configures the frontend input pad resolution to
  224. 2048x1536. The driver propagates the format to the frontend output pad.
  225. Note that the propagated output format can be different, as in this case,
  226. than the input format, as the hardware might need to crop pixels (for
  227. instance when converting a Bayer filter pattern to RGB or YUV).</para></listitem>
  228. <listitem><para>The application configures the scaler input pad resolution to
  229. 2046x1534 to match the frontend output resolution. The driver propagates
  230. the format to the scaler output pad.</para></listitem>
  231. <listitem><para>The application configures the scaler output pad resolution to
  232. 1280x960.</para></listitem>
  233. </orderedlist>
  234. </para>
  235. <para>When satisfied with the try results, applications can set the active
  236. formats by setting the <structfield>which</structfield> argument to
  237. <constant>V4L2_SUBDEV_FORMAT_ACTIVE</constant>. Active formats are changed
  238. exactly as try formats by drivers. To avoid modifying the hardware state
  239. during format negotiation, applications should negotiate try formats first
  240. and then modify the active settings using the try formats returned during
  241. the last negotiation iteration. This guarantees that the active format
  242. will be applied as-is by the driver without being modified.
  243. </para>
  244. </section>
  245. <section>
  246. <title>Cropping and scaling</title>
  247. <para>Many sub-devices support cropping frames on their input or output
  248. pads (or possible even on both). Cropping is used to select the area of
  249. interest in an image, typically on a video sensor or video decoder. It can
  250. also be used as part of digital zoom implementations to select the area of
  251. the image that will be scaled up.</para>
  252. <para>Crop settings are defined by a crop rectangle and represented in a
  253. &v4l2-rect; by the coordinates of the top left corner and the rectangle
  254. size. Both the coordinates and sizes are expressed in pixels.</para>
  255. <para>The crop rectangle is retrieved and set using the
  256. &VIDIOC-SUBDEV-G-CROP; and &VIDIOC-SUBDEV-S-CROP; ioctls. Like for pad
  257. formats, drivers store try and active crop rectangles. The format
  258. negotiation mechanism applies to crop settings as well.</para>
  259. <para>On input pads, cropping is applied relatively to the current pad
  260. format. The pad format represents the image size as received by the
  261. sub-device from the previous block in the pipeline, and the crop rectangle
  262. represents the sub-image that will be transmitted further inside the
  263. sub-device for processing. The crop rectangle be entirely containted
  264. inside the input image size.</para>
  265. <para>Input crop rectangle are reset to their default value when the input
  266. image format is modified. Drivers should use the input image size as the
  267. crop rectangle default value, but hardware requirements may prevent this.
  268. </para>
  269. <para>Cropping behaviour on output pads is not defined.</para>
  270. </section>
  271. </section>
  272. &sub-subdev-formats;