dev-subdev.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 format" />
  169. <colspec colname="frontend-0 format" />
  170. <colspec colname="frontend-1 format" />
  171. <colspec colname="scaler-0 format" />
  172. <colspec colname="scaler-0 compose" />
  173. <colspec colname="scaler-1 format" />
  174. <thead>
  175. <row>
  176. <entry></entry>
  177. <entry>Sensor/0 format</entry>
  178. <entry>Frontend/0 format</entry>
  179. <entry>Frontend/1 format</entry>
  180. <entry>Scaler/0 format</entry>
  181. <entry>Scaler/0 compose selection rectangle</entry>
  182. <entry>Scaler/1 format</entry>
  183. </row>
  184. </thead>
  185. <tbody valign="top">
  186. <row>
  187. <entry>Initial state</entry>
  188. <entry>2048x1536/SGRBG8_1X8</entry>
  189. <entry>(default)</entry>
  190. <entry>(default)</entry>
  191. <entry>(default)</entry>
  192. <entry>(default)</entry>
  193. <entry>(default)</entry>
  194. </row>
  195. <row>
  196. <entry>Configure frontend sink format</entry>
  197. <entry>2048x1536/SGRBG8_1X8</entry>
  198. <entry><emphasis>2048x1536/SGRBG8_1X8</emphasis></entry>
  199. <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
  200. <entry>(default)</entry>
  201. <entry>(default)</entry>
  202. <entry>(default)</entry>
  203. </row>
  204. <row>
  205. <entry>Configure scaler sink format</entry>
  206. <entry>2048x1536/SGRBG8_1X8</entry>
  207. <entry>2048x1536/SGRBG8_1X8</entry>
  208. <entry>2046x1534/SGRBG8_1X8</entry>
  209. <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
  210. <entry><emphasis>0,0/2046x1534</emphasis></entry>
  211. <entry><emphasis>2046x1534/SGRBG8_1X8</emphasis></entry>
  212. </row>
  213. <row>
  214. <entry>Configure scaler sink compose selection</entry>
  215. <entry>2048x1536/SGRBG8_1X8</entry>
  216. <entry>2048x1536/SGRBG8_1X8</entry>
  217. <entry>2046x1534/SGRBG8_1X8</entry>
  218. <entry>2046x1534/SGRBG8_1X8</entry>
  219. <entry><emphasis>0,0/1280x960</emphasis></entry>
  220. <entry><emphasis>1280x960/SGRBG8_1X8</emphasis></entry>
  221. </row>
  222. </tbody>
  223. </tgroup>
  224. </table>
  225. <para>
  226. <orderedlist>
  227. <listitem><para>Initial state. The sensor source pad format is
  228. set to its native 3MP size and V4L2_MBUS_FMT_SGRBG8_1X8
  229. media bus code. Formats on the host frontend and scaler sink
  230. and source pads have the default values, as well as the
  231. compose rectangle on the scaler's sink pad.</para></listitem>
  232. <listitem><para>The application configures the frontend sink
  233. pad format's size to 2048x1536 and its media bus code to
  234. V4L2_MBUS_FMT_SGRBG_1X8. The driver propagates the format to
  235. the frontend source pad.</para></listitem>
  236. <listitem><para>The application configures the scaler sink pad
  237. format's size to 2046x1534 and the media bus code to
  238. V4L2_MBUS_FMT_SGRBG_1X8 to match the frontend source size and
  239. media bus code. The media bus code on the sink pad is set to
  240. V4L2_MBUS_FMT_SGRBG_1X8. The driver propagates the size to the
  241. compose selection rectangle on the scaler's sink pad, and the
  242. format to the scaler source pad.</para></listitem>
  243. <listitem><para>The application configures the size of the compose
  244. selection rectangle of the scaler's sink pad 1280x960. The driver
  245. propagates the size to the scaler's source pad
  246. format.</para></listitem>
  247. </orderedlist>
  248. </para>
  249. <para>When satisfied with the try results, applications can set the active
  250. formats by setting the <structfield>which</structfield> argument to
  251. <constant>V4L2_SUBDEV_FORMAT_ACTIVE</constant>. Active formats are changed
  252. exactly as try formats by drivers. To avoid modifying the hardware state
  253. during format negotiation, applications should negotiate try formats first
  254. and then modify the active settings using the try formats returned during
  255. the last negotiation iteration. This guarantees that the active format
  256. will be applied as-is by the driver without being modified.
  257. </para>
  258. </section>
  259. <section id="v4l2-subdev-selections">
  260. <title>Selections: cropping, scaling and composition</title>
  261. <para>Many sub-devices support cropping frames on their input or output
  262. pads (or possible even on both). Cropping is used to select the area of
  263. interest in an image, typically on an image sensor or a video decoder. It can
  264. also be used as part of digital zoom implementations to select the area of
  265. the image that will be scaled up.</para>
  266. <para>Crop settings are defined by a crop rectangle and represented in a
  267. &v4l2-rect; by the coordinates of the top left corner and the rectangle
  268. size. Both the coordinates and sizes are expressed in pixels.</para>
  269. <para>As for pad formats, drivers store try and active
  270. rectangles for the selection targets <xref
  271. linkend="v4l2-selections-common" />.</para>
  272. <para>On sink pads, cropping is applied relative to the
  273. current pad format. The pad format represents the image size as
  274. received by the sub-device from the previous block in the
  275. pipeline, and the crop rectangle represents the sub-image that
  276. will be transmitted further inside the sub-device for
  277. processing.</para>
  278. <para>The scaling operation changes the size of the image by
  279. scaling it to new dimensions. The scaling ratio isn't specified
  280. explicitly, but is implied from the original and scaled image
  281. sizes. Both sizes are represented by &v4l2-rect;.</para>
  282. <para>Scaling support is optional. When supported by a subdev,
  283. the crop rectangle on the subdev's sink pad is scaled to the
  284. size configured using the &VIDIOC-SUBDEV-S-SELECTION; IOCTL
  285. using <constant>V4L2_SEL_TGT_COMPOSE</constant>
  286. selection target on the same pad. If the subdev supports scaling
  287. but not composing, the top and left values are not used and must
  288. always be set to zero.</para>
  289. <para>On source pads, cropping is similar to sink pads, with the
  290. exception that the source size from which the cropping is
  291. performed, is the COMPOSE rectangle on the sink pad. In both
  292. sink and source pads, the crop rectangle must be entirely
  293. contained inside the source image size for the crop
  294. operation.</para>
  295. <para>The drivers should always use the closest possible
  296. rectangle the user requests on all selection targets, unless
  297. specifically told otherwise.
  298. <constant>V4L2_SEL_FLAG_GE</constant> and
  299. <constant>V4L2_SEL_FLAG_LE</constant> flags may be
  300. used to round the image size either up or down. <xref
  301. linkend="v4l2-selection-flags" /></para>
  302. </section>
  303. <section>
  304. <title>Types of selection targets</title>
  305. <section>
  306. <title>Actual targets</title>
  307. <para>Actual targets (without a postfix) reflect the actual
  308. hardware configuration at any point of time. There is a BOUNDS
  309. target corresponding to every actual target.</para>
  310. </section>
  311. <section>
  312. <title>BOUNDS targets</title>
  313. <para>BOUNDS targets is the smallest rectangle that contains all
  314. valid actual rectangles. It may not be possible to set the actual
  315. rectangle as large as the BOUNDS rectangle, however. This may be
  316. because e.g. a sensor's pixel array is not rectangular but
  317. cross-shaped or round. The maximum size may also be smaller than the
  318. BOUNDS rectangle.</para>
  319. </section>
  320. </section>
  321. <section>
  322. <title>Order of configuration and format propagation</title>
  323. <para>Inside subdevs, the order of image processing steps will
  324. always be from the sink pad towards the source pad. This is also
  325. reflected in the order in which the configuration must be
  326. performed by the user: the changes made will be propagated to
  327. any subsequent stages. If this behaviour is not desired, the
  328. user must set
  329. <constant>V4L2_SEL_FLAG_KEEP_CONFIG</constant> flag. This
  330. flag causes no propagation of the changes are allowed in any
  331. circumstances. This may also cause the accessed rectangle to be
  332. adjusted by the driver, depending on the properties of the
  333. underlying hardware.</para>
  334. <para>The coordinates to a step always refer to the actual size
  335. of the previous step. The exception to this rule is the source
  336. compose rectangle, which refers to the sink compose bounds
  337. rectangle --- if it is supported by the hardware.</para>
  338. <orderedlist>
  339. <listitem><para>Sink pad format. The user configures the sink pad
  340. format. This format defines the parameters of the image the
  341. entity receives through the pad for further processing.</para></listitem>
  342. <listitem><para>Sink pad actual crop selection. The sink pad crop
  343. defines the crop performed to the sink pad format.</para></listitem>
  344. <listitem><para>Sink pad actual compose selection. The size of the
  345. sink pad compose rectangle defines the scaling ratio compared
  346. to the size of the sink pad crop rectangle. The location of
  347. the compose rectangle specifies the location of the actual
  348. sink compose rectangle in the sink compose bounds
  349. rectangle.</para></listitem>
  350. <listitem><para>Source pad actual crop selection. Crop on the source
  351. pad defines crop performed to the image in the sink compose
  352. bounds rectangle.</para></listitem>
  353. <listitem><para>Source pad format. The source pad format defines the
  354. output pixel format of the subdev, as well as the other
  355. parameters with the exception of the image width and height.
  356. Width and height are defined by the size of the source pad
  357. actual crop selection.</para></listitem>
  358. </orderedlist>
  359. <para>Accessing any of the above rectangles not supported by the
  360. subdev will return <constant>EINVAL</constant>. Any rectangle
  361. referring to a previous unsupported rectangle coordinates will
  362. instead refer to the previous supported rectangle. For example,
  363. if sink crop is not supported, the compose selection will refer
  364. to the sink pad format dimensions instead.</para>
  365. <figure id="subdev-image-processing-crop">
  366. <title>Image processing in subdevs: simple crop example</title>
  367. <mediaobject>
  368. <imageobject>
  369. <imagedata fileref="subdev-image-processing-crop.svg"
  370. format="SVG" scale="200" />
  371. </imageobject>
  372. </mediaobject>
  373. </figure>
  374. <para>In the above example, the subdev supports cropping on its
  375. sink pad. To configure it, the user sets the media bus format on
  376. the subdev's sink pad. Now the actual crop rectangle can be set
  377. on the sink pad --- the location and size of this rectangle
  378. reflect the location and size of a rectangle to be cropped from
  379. the sink format. The size of the sink crop rectangle will also
  380. be the size of the format of the subdev's source pad.</para>
  381. <figure id="subdev-image-processing-scaling-multi-source">
  382. <title>Image processing in subdevs: scaling with multiple sources</title>
  383. <mediaobject>
  384. <imageobject>
  385. <imagedata fileref="subdev-image-processing-scaling-multi-source.svg"
  386. format="SVG" scale="200" />
  387. </imageobject>
  388. </mediaobject>
  389. </figure>
  390. <para>In this example, the subdev is capable of first cropping,
  391. then scaling and finally cropping for two source pads
  392. individually from the resulting scaled image. The location of
  393. the scaled image in the cropped image is ignored in sink compose
  394. target. Both of the locations of the source crop rectangles
  395. refer to the sink scaling rectangle, independently cropping an
  396. area at location specified by the source crop rectangle from
  397. it.</para>
  398. <figure id="subdev-image-processing-full">
  399. <title>Image processing in subdevs: scaling and composition
  400. with multiple sinks and sources</title>
  401. <mediaobject>
  402. <imageobject>
  403. <imagedata fileref="subdev-image-processing-full.svg"
  404. format="SVG" scale="200" />
  405. </imageobject>
  406. </mediaobject>
  407. </figure>
  408. <para>The subdev driver supports two sink pads and two source
  409. pads. The images from both of the sink pads are individually
  410. cropped, then scaled and further composed on the composition
  411. bounds rectangle. From that, two independent streams are cropped
  412. and sent out of the subdev from the source pads.</para>
  413. </section>
  414. </section>
  415. &sub-subdev-formats;