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