[media] doc: v4l: add documentation for selection API
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / DocBook / media / v4l / selection-api.xml
blob46cb47ab8e3b231624324fa625eb77ac85f9ea4b
1 <section id="selection-api">
3   <title>Experimental API for cropping, composing and scaling</title>
5       <note>
6         <title>Experimental</title>
8         <para>This is an <link linkend="experimental">experimental</link>
9 interface and may change in the future.</para>
10       </note>
12   <section>
13     <title>Introduction</title>
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>
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>
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>
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>
38   </section>
40     <section>
41       <title>Selection targets</title>
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>
57   <section>
59   <title>Configuration</title>
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>
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>
75    <section>
77    <title>Configuration of video capture</title>
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>
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
87 coordinates are expressed in driver dependant units, although the coordinate
88 system guarantees that if sizes of the active cropping and the active composing
89 rectangles are equal then no scaling is performed.  </para>
91 <para>The top left corner, width and height of the source rectangle, that is
92 the area actually sampled, is given by the <constant> V4L2_SEL_TGT_CROP_ACTIVE
93 </constant> target. It uses the same coordinate system as <constant>
94 V4L2_SEL_TGT_CROP_BOUNDS </constant>. The active cropping area must lie
95 completely inside the capture boundaries. The driver may further adjust the
96 requested size and/or position according to hardware limitations.</para>
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
100 over what the driver writer considers the complete picture.  Drivers shall set
101 the active crop rectangle to the default when the driver is first loaded, but
102 not later.</para>
104 <para>The composing targets refer to a memory buffer. The limits of composing
105 coordinates are obtained using <constant> V4L2_SEL_TGT_COMPOSE_BOUNDS
106 </constant>.  All coordinates are expressed in natural unit for given formats.
107 Pixels are highly recommended.  The rectangle's top/left corner must be located
108 at position <constant> (0,0) </constant>. The width and height are equal to the
109 image size set by <constant> VIDIOC_S_FMT </constant>.</para>
111 <para>The part of a buffer into which the image is inserted by the hardware is
112 controlled by the <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> target.
113 The rectangle's coordinates are also expressed in the same coordinate system as
114 the bounds rectangle. The composing rectangle must lie completely inside bounds
115 rectangle. The driver must adjust the composing rectangle to fit to the
116 bounding limits. Moreover, the driver can perform other adjustments according
117 to hardware limitations. The application can control rounding behaviour using
118 <link linkend="v4l2-sel-flags"> constraint flags </link>.</para>
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
122 bounding rectangle.</para>
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
126 defined using <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> plus all
127 padding data modified by hardware during insertion process. All pixels outside
128 this rectangle <emphasis>must not</emphasis> be changed by the hardware. The
129 content of pixels that lie inside the padded area but outside active area is
130 undefined. The application can use the padded and active rectangles to detect
131 where the rubbish pixels are located and remove them if needed.</para>
133    </section>
135    <section>
137    <title>Configuration of video output</title>
139 <para>For output devices targets and ioctls are used similarly to the video
140 capture case. The <emphasis> composing </emphasis> rectangle refers to the
141 insertion of an image into a video signal. The cropping rectangles refer to a
142 memory buffer. It is recommended to configure the composing targets before to
143 the cropping targets.</para>
145 <para>The cropping targets refer to the memory buffer that contains an image to
146 be inserted into a video signal or graphical screen. The limits of cropping
147 coordinates are obtained using <constant> V4L2_SEL_TGT_CROP_BOUNDS </constant>.
148 All coordinates are expressed in natural units for a given format. Pixels are
149 highly 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>
153 <para>The top left corner, width and height of the source rectangle, that is
154 the 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
156 in in the same coordinate system as the bounds rectangle. The active cropping
157 area must lie completely inside the crop boundaries and the driver may further
158 adjust the requested size and/or position according to hardware
159 limitations.</para>
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
163 bounding rectangle.</para>
165 <para>The part of a video signal or graphics display where the image is
166 inserted by the hardware is controlled by <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE
167 </constant> target.  The rectangle's coordinates are expressed in driver
168 dependant units. The only exception are digital outputs where the units are
169 pixels. For other types of devices, the coordinate system guarantees that if
170 sizes of the active cropping and the active composing rectangles are equal then
171 no scaling is performed.  The composing rectangle must lie completely inside
172 the bounds rectangle.  The driver must adjust the area to fit to the bounding
173 limits.  Moreover, the driver can perform other adjustments according to
174 hardware limitations. </para>
176 <para>The device has a default composing rectangle, given by the <constant>
177 V4L2_SEL_TGT_COMPOSE_DEFAULT </constant> target. This rectangle shall cover what
178 the driver writer considers the complete picture. It is recommended for the
179 driver developers to put the top/left corner at position <constant> (0,0)
180 </constant>. Drivers shall set the active composing rectangle to the default
181 one when the driver is first loaded.</para>
183 <para>The devices may introduce additional content to video signal other than
184 an image from memory buffers.  It includes borders around an image. However,
185 such a padded area is driver-dependent feature not covered by this document.
186 Driver developers are encouraged to keep padded rectangle equal to active one.
187 The padded target is accessed by the <constant> V4L2_SEL_TGT_COMPOSE_PADDED
188 </constant> identifier.  It must contain all pixels from the <constant>
189 V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> target.</para>
191    </section>
193    <section>
195      <title>Scaling control.</title>
197 <para>An application can detect if scaling is performed by comparing the width
198 and the height of rectangles obtained using <constant> V4L2_SEL_TGT_CROP_ACTIVE
199 </constant> and <constant> V4L2_SEL_TGT_COMPOSE_ACTIVE </constant> targets. If
200 these are not equal then the scaling is applied. The application can compute
201 the scaling ratios using these values.</para>
203    </section>
205   </section>
207   <section>
209     <title>Comparison with old cropping API.</title>
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
213 devices. Later the cropping API was adopted by video output drivers. The ioctls
214 are used to select a part of the display were the video signal is inserted. It
215 should be considered as an API abuse because the described operation is
216 actually the composing.  The selection API makes a clear distinction between
217 composing and cropping operations by setting the appropriate targets.  The V4L2
218 API lacks any support for composing to and cropping from an image inside a
219 memory buffer.  The application could configure a capture device to fill only a
220 part of an image by abusing V4L2 API.  Cropping a smaller image from a larger
221 one is achieved by setting the field <structfield>
222 &v4l2-pix-format;::bytesperline </structfield>.  Introducing an image offsets
223 could be done by modifying field <structfield> &v4l2-buffer;::m:userptr
224 </structfield> before calling <constant> VIDIOC_QBUF </constant>. Those
225 operations should be avoided because they are not portable (endianness), and do
226 not work for macroblock and Bayer formats and mmap buffers.  The selection API
227 deals with configuration of buffer cropping/composing in a clear, intuitive and
228 portable way.  Next, with the selection API the concepts of the padded target
229 and constraints flags are introduced.  Finally, <structname> &v4l2-crop;
230 </structname> and <structname> &v4l2-cropcap; </structname> have no reserved
231 fields. Therefore there is no way to extend their functionality.  The new
232 <structname> &v4l2-selection; </structname> provides a lot of place for future
233 extensions.  Driver developers are encouraged to implement only selection API.
234 The former cropping API would be simulated using the new one. </para>
236   </section>
238    <section>
239       <title>Examples</title>
240       <example>
241         <title>Resetting the cropping parameters</title>
243         <para>(A video capture device is assumed; change <constant>
244 V4L2_BUF_TYPE_VIDEO_CAPTURE </constant> for other devices; change target to
245 <constant> V4L2_SEL_TGT_COMPOSE_* </constant> family to configure composing
246 area)</para>
248         <programlisting>
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);
262         </programlisting>
263       </example>
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>
271         &v4l2-selection; sel = {
272                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
273                 .target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
274         };
275         struct v4l2_rect r;
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);
292         </programlisting>
293       </example>
295       <example>
296         <title>Querying for scaling factors</title>
297         <para>A video output device is assumed; change <constant>
298 V4L2_BUF_TYPE_VIDEO_OUTPUT </constant> for other devices</para>
299         <programlisting>
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;
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);
318         /* computing scaling factors */
319         hscale = (double)compose.r.width / crop.r.width;
320         vscale = (double)compose.r.height / crop.r.height;
322         </programlisting>
323       </example>
325    </section>
327 </section>