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