selection-api.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <section id="selection-api">
  2. <title>Experimental API for cropping, composing and scaling</title>
  3. <note>
  4. <title>Experimental</title>
  5. <para>This is an <link linkend="experimental">experimental</link>
  6. interface and may change in the future.</para>
  7. </note>
  8. <section>
  9. <title>Introduction</title>
  10. <para>Some video capture devices can sample a subsection of a picture and
  11. shrink or enlarge it to an image of arbitrary size. Next, the devices can
  12. insert the image into larger one. Some video output devices can crop part of an
  13. input image, scale it up or down and insert it at an arbitrary scan line and
  14. horizontal offset into a video signal. We call these abilities cropping,
  15. scaling and composing.</para>
  16. <para>On a video <emphasis>capture</emphasis> device the source is a video
  17. signal, and the cropping target determine the area actually sampled. The sink
  18. is an image stored in a memory buffer. The composing area specifies which part
  19. of the buffer is actually written to by the hardware. </para>
  20. <para>On a video <emphasis>output</emphasis> device the source is an image in a
  21. memory buffer, and the cropping target is a part of an image to be shown on a
  22. display. The sink is the display or the graphics screen. The application may
  23. select the part of display where the image should be displayed. The size and
  24. position of such a window is controlled by the compose target.</para>
  25. <para>Rectangles for all cropping and composing targets are defined even if the
  26. device does supports neither cropping nor composing. Their size and position
  27. will be fixed in such a case. If the device does not support scaling then the
  28. cropping and composing rectangles have the same size.</para>
  29. </section>
  30. <section>
  31. <title>Selection targets</title>
  32. <figure id="sel-targets-capture">
  33. <title>Cropping and composing targets</title>
  34. <mediaobject>
  35. <imageobject>
  36. <imagedata fileref="selection.png" format="PNG" />
  37. </imageobject>
  38. <textobject>
  39. <phrase>Targets used by a cropping, composing and scaling
  40. process</phrase>
  41. </textobject>
  42. </mediaobject>
  43. </figure>
  44. For complete list of the available selection targets see table <xref
  45. linkend="v4l2-sel-target"/>
  46. </section>
  47. <section>
  48. <title>Configuration</title>
  49. <para>Applications can use the <link linkend="vidioc-g-selection">selection
  50. API</link> to select an area in a video signal or a buffer, and to query for
  51. default settings and hardware limits.</para>
  52. <para>Video hardware can have various cropping, composing and scaling
  53. limitations. It may only scale up or down, support only discrete scaling
  54. factors, or have different scaling abilities in the horizontal and vertical
  55. directions. Also it may not support scaling at all. At the same time the
  56. cropping/composing rectangles may have to be aligned, and both the source and
  57. the sink may have arbitrary upper and lower size limits. Therefore, as usual,
  58. drivers are expected to adjust the requested parameters and return the actual
  59. values selected. An application can control the rounding behaviour using <link
  60. linkend="v4l2-sel-flags"> constraint flags </link>.</para>
  61. <section>
  62. <title>Configuration of video capture</title>
  63. <para>See figure <xref linkend="sel-targets-capture" /> for examples of the
  64. selection targets available for a video capture device. It is recommended to
  65. configure the cropping targets before to the composing targets.</para>
  66. <para>The range of coordinates of the top left corner, width and height of
  67. areas that can be sampled is given by the <constant> V4L2_SEL_TGT_CROP_BOUNDS
  68. </constant> target. It is recommended for the driver developers to put the
  69. top/left corner at position <constant> (0,0) </constant>. The rectangle's
  70. coordinates are expressed in pixels.</para>
  71. <para>The top left corner, width and height of the source rectangle, that is
  72. the area actually sampled, is given by the <constant> V4L2_SEL_TGT_CROP_ACTIVE
  73. </constant> target. It uses the same coordinate system as <constant>
  74. V4L2_SEL_TGT_CROP_BOUNDS </constant>. The active cropping area must lie
  75. completely inside the capture boundaries. The driver may further adjust the
  76. requested size and/or position according to hardware limitations.</para>
  77. <para>Each capture device has a default source rectangle, given by the
  78. <constant> V4L2_SEL_TGT_CROP_DEFAULT </constant> target. This rectangle shall
  79. over what the driver writer considers the complete picture. Drivers shall set
  80. the active crop rectangle to the default when the driver is first loaded, but
  81. not later.</para>
  82. <para>The composing targets refer to a memory buffer. The limits of composing
  83. coordinates are obtained using <constant> V4L2_SEL_TGT_COMPOSE_BOUNDS
  84. </constant>. All coordinates are expressed in pixels. The rectangle's top/left
  85. corner must be located at position <constant> (0,0) </constant>. The width and
  86. height are equal to the image size set by <constant> VIDIOC_S_FMT </constant>.
  87. </para>
  88. <para>The part of a buffer into which the image is inserted by the hardware is
  89. controlled by the <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> target.
  90. The rectangle's coordinates are also expressed in the same coordinate system as
  91. the bounds rectangle. The composing rectangle must lie completely inside bounds
  92. rectangle. The driver must adjust the composing rectangle to fit to the
  93. bounding limits. Moreover, the driver can perform other adjustments according
  94. to hardware limitations. The application can control rounding behaviour using
  95. <link linkend="v4l2-sel-flags"> constraint flags </link>.</para>
  96. <para>For capture devices the default composing rectangle is queried using
  97. <constant> V4L2_SEL_TGT_COMPOSE_DEFAULT </constant>. It is usually equal to the
  98. bounding rectangle.</para>
  99. <para>The part of a buffer that is modified by the hardware is given by
  100. <constant> V4L2_SEL_TGT_COMPOSE_PADDED </constant>. It contains all pixels
  101. defined using <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> plus all
  102. padding data modified by hardware during insertion process. All pixels outside
  103. this rectangle <emphasis>must not</emphasis> be changed by the hardware. The
  104. content of pixels that lie inside the padded area but outside active area is
  105. undefined. The application can use the padded and active rectangles to detect
  106. where the rubbish pixels are located and remove them if needed.</para>
  107. </section>
  108. <section>
  109. <title>Configuration of video output</title>
  110. <para>For output devices targets and ioctls are used similarly to the video
  111. capture case. The <emphasis> composing </emphasis> rectangle refers to the
  112. insertion of an image into a video signal. The cropping rectangles refer to a
  113. memory buffer. It is recommended to configure the composing targets before to
  114. the cropping targets.</para>
  115. <para>The cropping targets refer to the memory buffer that contains an image to
  116. be inserted into a video signal or graphical screen. The limits of cropping
  117. coordinates are obtained using <constant> V4L2_SEL_TGT_CROP_BOUNDS </constant>.
  118. All coordinates are expressed in pixels. The top/left corner is always point
  119. <constant> (0,0) </constant>. The width and height is equal to the image size
  120. specified using <constant> VIDIOC_S_FMT </constant> ioctl.</para>
  121. <para>The top left corner, width and height of the source rectangle, that is
  122. the area from which image date are processed by the hardware, is given by the
  123. <constant> V4L2_SEL_TGT_CROP_ACTIVE </constant>. Its coordinates are expressed
  124. in in the same coordinate system as the bounds rectangle. The active cropping
  125. area must lie completely inside the crop boundaries and the driver may further
  126. adjust the requested size and/or position according to hardware
  127. limitations.</para>
  128. <para>For output devices the default cropping rectangle is queried using
  129. <constant> V4L2_SEL_TGT_CROP_DEFAULT </constant>. It is usually equal to the
  130. bounding rectangle.</para>
  131. <para>The part of a video signal or graphics display where the image is
  132. inserted by the hardware is controlled by <constant>
  133. V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> target. The rectangle's coordinates
  134. are expressed in pixels. The composing rectangle must lie completely inside the
  135. bounds rectangle. The driver must adjust the area to fit to the bounding
  136. limits. Moreover, the driver can perform other adjustments according to
  137. hardware limitations. </para>
  138. <para>The device has a default composing rectangle, given by the <constant>
  139. V4L2_SEL_TGT_COMPOSE_DEFAULT </constant> target. This rectangle shall cover what
  140. the driver writer considers the complete picture. It is recommended for the
  141. driver developers to put the top/left corner at position <constant> (0,0)
  142. </constant>. Drivers shall set the active composing rectangle to the default
  143. one when the driver is first loaded.</para>
  144. <para>The devices may introduce additional content to video signal other than
  145. an image from memory buffers. It includes borders around an image. However,
  146. such a padded area is driver-dependent feature not covered by this document.
  147. Driver developers are encouraged to keep padded rectangle equal to active one.
  148. The padded target is accessed by the <constant> V4L2_SEL_TGT_COMPOSE_PADDED
  149. </constant> identifier. It must contain all pixels from the <constant>
  150. V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> target.</para>
  151. </section>
  152. <section>
  153. <title>Scaling control</title>
  154. <para>An application can detect if scaling is performed by comparing the width
  155. and the height of rectangles obtained using <constant> V4L2_SEL_TGT_CROP_ACTIVE
  156. </constant> and <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> targets. If
  157. these are not equal then the scaling is applied. The application can compute
  158. the scaling ratios using these values.</para>
  159. </section>
  160. </section>
  161. <section>
  162. <title>Comparison with old cropping API</title>
  163. <para>The selection API was introduced to cope with deficiencies of previous
  164. <link linkend="crop"> API </link>, that was designed to control simple capture
  165. devices. Later the cropping API was adopted by video output drivers. The ioctls
  166. are used to select a part of the display were the video signal is inserted. It
  167. should be considered as an API abuse because the described operation is
  168. actually the composing. The selection API makes a clear distinction between
  169. composing and cropping operations by setting the appropriate targets. The V4L2
  170. API lacks any support for composing to and cropping from an image inside a
  171. memory buffer. The application could configure a capture device to fill only a
  172. part of an image by abusing V4L2 API. Cropping a smaller image from a larger
  173. one is achieved by setting the field <structfield>
  174. &v4l2-pix-format;::bytesperline </structfield>. Introducing an image offsets
  175. could be done by modifying field <structfield> &v4l2-buffer;::m:userptr
  176. </structfield> before calling <constant> VIDIOC_QBUF </constant>. Those
  177. operations should be avoided because they are not portable (endianness), and do
  178. not work for macroblock and Bayer formats and mmap buffers. The selection API
  179. deals with configuration of buffer cropping/composing in a clear, intuitive and
  180. portable way. Next, with the selection API the concepts of the padded target
  181. and constraints flags are introduced. Finally, <structname> &v4l2-crop;
  182. </structname> and <structname> &v4l2-cropcap; </structname> have no reserved
  183. fields. Therefore there is no way to extend their functionality. The new
  184. <structname> &v4l2-selection; </structname> provides a lot of place for future
  185. extensions. Driver developers are encouraged to implement only selection API.
  186. The former cropping API would be simulated using the new one. </para>
  187. </section>
  188. <section>
  189. <title>Examples</title>
  190. <example>
  191. <title>Resetting the cropping parameters</title>
  192. <para>(A video capture device is assumed; change <constant>
  193. V4L2_BUF_TYPE_VIDEO_CAPTURE </constant> for other devices; change target to
  194. <constant> V4L2_SEL_TGT_COMPOSE_* </constant> family to configure composing
  195. area)</para>
  196. <programlisting>
  197. &v4l2-selection; sel = {
  198. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  199. .target = V4L2_SEL_TGT_CROP_DEFAULT,
  200. };
  201. ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;sel);
  202. if (ret)
  203. exit(-1);
  204. sel.target = V4L2_SEL_TGT_CROP_ACTIVE;
  205. ret = ioctl(fd, &VIDIOC-S-SELECTION;, &amp;sel);
  206. if (ret)
  207. exit(-1);
  208. </programlisting>
  209. </example>
  210. <example>
  211. <title>Simple downscaling</title>
  212. <para>Setting a composing area on output of size of <emphasis> at most
  213. </emphasis> half of limit placed at a center of a display.</para>
  214. <programlisting>
  215. &v4l2-selection; sel = {
  216. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  217. .target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
  218. };
  219. struct v4l2_rect r;
  220. ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;sel);
  221. if (ret)
  222. exit(-1);
  223. /* setting smaller compose rectangle */
  224. r.width = sel.r.width / 2;
  225. r.height = sel.r.height / 2;
  226. r.left = sel.r.width / 4;
  227. r.top = sel.r.height / 4;
  228. sel.r = r;
  229. sel.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
  230. sel.flags = V4L2_SEL_FLAG_LE;
  231. ret = ioctl(fd, &VIDIOC-S-SELECTION;, &amp;sel);
  232. if (ret)
  233. exit(-1);
  234. </programlisting>
  235. </example>
  236. <example>
  237. <title>Querying for scaling factors</title>
  238. <para>A video output device is assumed; change <constant>
  239. V4L2_BUF_TYPE_VIDEO_OUTPUT </constant> for other devices</para>
  240. <programlisting>
  241. &v4l2-selection; compose = {
  242. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  243. .target = V4L2_SEL_TGT_COMPOSE_ACTIVE,
  244. };
  245. &v4l2-selection; crop = {
  246. .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
  247. .target = V4L2_SEL_TGT_CROP_ACTIVE,
  248. };
  249. double hscale, vscale;
  250. ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;compose);
  251. if (ret)
  252. exit(-1);
  253. ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;crop);
  254. if (ret)
  255. exit(-1);
  256. /* computing scaling factors */
  257. hscale = (double)compose.r.width / crop.r.width;
  258. vscale = (double)compose.r.height / crop.r.height;
  259. </programlisting>
  260. </example>
  261. </section>
  262. </section>