About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Documentation / DocBook / media / v4l / selection-api.xml

Custom Search

Based on kernel version 3.16. Page generated on 2014-08-06 21:39 EST.

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	      <para>
44	      <figure id="sel-targets-capture">
45		<title>Cropping and composing targets</title>
46		<mediaobject>
47		  <imageobject>
48		    <imagedata fileref="selection.png" format="PNG" />
49		  </imageobject>
50		  <textobject>
51		    <phrase>Targets used by a cropping, composing and scaling
52	            process</phrase>
53		  </textobject>
54		</mediaobject>
55	      </figure>
56	      </para>
58	      <para>See <xref linkend="v4l2-selection-targets" /> for more
59	    information.</para>
60	    </section>
62	  <section>
64	  <title>Configuration</title>
66	<para>Applications can use the <link linkend="vidioc-g-selection">selection
67	API</link> to select an area in a video signal or a buffer, and to query for
68	default settings and hardware limits.</para>
70	<para>Video hardware can have various cropping, composing and scaling
71	limitations. It may only scale up or down, support only discrete scaling
72	factors, or have different scaling abilities in the horizontal and vertical
73	directions. Also it may not support scaling at all. At the same time the
74	cropping/composing rectangles may have to be aligned, and both the source and
75	the sink may have arbitrary upper and lower size limits. Therefore, as usual,
76	drivers are expected to adjust the requested parameters and return the actual
77	values selected. An application can control the rounding behaviour using <link
78	linkend="v4l2-selection-flags"> constraint flags </link>.</para>
80	   <section>
82	   <title>Configuration of video capture</title>
84	<para>See figure <xref linkend="sel-targets-capture" /> for examples of the
85	selection targets available for a video capture device.  It is recommended to
86	configure the cropping targets before to the composing targets.</para>
88	<para>The range of coordinates of the top left corner, width and height of
89	areas that can be sampled is given by the <constant> V4L2_SEL_TGT_CROP_BOUNDS
90	</constant> target. It is recommended for the driver developers to put the
91	top/left corner at position <constant> (0,0) </constant>.  The rectangle's
92	coordinates are expressed in pixels.</para>
94	<para>The top left corner, width and height of the source rectangle, that is
95	the area actually sampled, is given by the <constant> V4L2_SEL_TGT_CROP
96	</constant> target. It uses the same coordinate system as <constant>
97	V4L2_SEL_TGT_CROP_BOUNDS </constant>. The active cropping area must lie
98	completely inside the capture boundaries. The driver may further adjust the
99	requested size and/or position according to hardware limitations.</para>
101	<para>Each capture device has a default source rectangle, given by the
102	<constant> V4L2_SEL_TGT_CROP_DEFAULT </constant> target. This rectangle shall
103	over what the driver writer considers the complete picture.  Drivers shall set
104	the active crop rectangle to the default when the driver is first loaded, but
105	not later.</para>
107	<para>The composing targets refer to a memory buffer. The limits of composing
108	coordinates are obtained using <constant> V4L2_SEL_TGT_COMPOSE_BOUNDS
109	</constant>.  All coordinates are expressed in pixels. The rectangle's top/left
110	corner must be located at position <constant> (0,0) </constant>. The width and
111	height are equal to the image size set by <constant> VIDIOC_S_FMT </constant>.
112	</para>
114	<para>The part of a buffer into which the image is inserted by the hardware is
115	controlled by the <constant> V4L2_SEL_TGT_COMPOSE </constant> target.
116	The rectangle's coordinates are also expressed in the same coordinate system as
117	the bounds rectangle. The composing rectangle must lie completely inside bounds
118	rectangle. The driver must adjust the composing rectangle to fit to the
119	bounding limits. Moreover, the driver can perform other adjustments according
120	to hardware limitations. The application can control rounding behaviour using
121	<link linkend="v4l2-selection-flags"> constraint flags </link>.</para>
123	<para>For capture devices the default composing rectangle is queried using
124	<constant> V4L2_SEL_TGT_COMPOSE_DEFAULT </constant>. It is usually equal to the
125	bounding rectangle.</para>
127	<para>The part of a buffer that is modified by the hardware is given by
128	<constant> V4L2_SEL_TGT_COMPOSE_PADDED </constant>. It contains all pixels
129	defined using <constant> V4L2_SEL_TGT_COMPOSE </constant> plus all
130	padding data modified by hardware during insertion process. All pixels outside
131	this rectangle <emphasis>must not</emphasis> be changed by the hardware. The
132	content of pixels that lie inside the padded area but outside active area is
133	undefined. The application can use the padded and active rectangles to detect
134	where the rubbish pixels are located and remove them if needed.</para>
136	   </section>
138	   <section>
140	   <title>Configuration of video output</title>
142	<para>For output devices targets and ioctls are used similarly to the video
143	capture case. The <emphasis> composing </emphasis> rectangle refers to the
144	insertion of an image into a video signal. The cropping rectangles refer to a
145	memory buffer. It is recommended to configure the composing targets before to
146	the cropping targets.</para>
148	<para>The cropping targets refer to the memory buffer that contains an image to
149	be inserted into a video signal or graphical screen. The limits of cropping
150	coordinates are obtained using <constant> V4L2_SEL_TGT_CROP_BOUNDS </constant>.
151	All coordinates are expressed in pixels. The top/left corner is always point
152	<constant> (0,0) </constant>.  The width and height is equal to the image size
153	specified using <constant> VIDIOC_S_FMT </constant> ioctl.</para>
155	<para>The top left corner, width and height of the source rectangle, that is
156	the area from which image date are processed by the hardware, is given by the
157	<constant> V4L2_SEL_TGT_CROP </constant>. Its coordinates are expressed
158	in in the same coordinate system as the bounds rectangle. The active cropping
159	area must lie completely inside the crop boundaries and the driver may further
160	adjust the requested size and/or position according to hardware
161	limitations.</para>
163	<para>For output devices the default cropping rectangle is queried using
164	<constant> V4L2_SEL_TGT_CROP_DEFAULT </constant>. It is usually equal to the
165	bounding rectangle.</para>
167	<para>The part of a video signal or graphics display where the image is
168	inserted by the hardware is controlled by <constant>
169	V4L2_SEL_TGT_COMPOSE </constant> target.  The rectangle's coordinates
170	are expressed in pixels. The composing rectangle must lie completely inside the
171	bounds rectangle.  The driver must adjust the area to fit to the bounding
172	limits.  Moreover, the driver can perform other adjustments according to
173	hardware limitations. </para>
175	<para>The device has a default composing rectangle, given by the <constant>
176	V4L2_SEL_TGT_COMPOSE_DEFAULT </constant> target. This rectangle shall cover what
177	the driver writer considers the complete picture. It is recommended for the
178	driver developers to put the top/left corner at position <constant> (0,0)
179	</constant>. Drivers shall set the active composing rectangle to the default
180	one when the driver is first loaded.</para>
182	<para>The devices may introduce additional content to video signal other than
183	an image from memory buffers.  It includes borders around an image. However,
184	such a padded area is driver-dependent feature not covered by this document.
185	Driver developers are encouraged to keep padded rectangle equal to active one.
186	The padded target is accessed by the <constant> V4L2_SEL_TGT_COMPOSE_PADDED
187	</constant> identifier.  It must contain all pixels from the <constant>
188	V4L2_SEL_TGT_COMPOSE </constant> target.</para>
190	   </section>
192	   <section>
194	     <title>Scaling control</title>
196	<para>An application can detect if scaling is performed by comparing the width
197	and the height of rectangles obtained using <constant> V4L2_SEL_TGT_CROP
198	</constant> and <constant> V4L2_SEL_TGT_COMPOSE </constant> targets. If
199	these are not equal then the scaling is applied. The application can compute
200	the scaling ratios using these values.</para>
202	   </section>
204	  </section>
206	  <section>
208	    <title>Comparison with old cropping API</title>
210	<para>The selection API was introduced to cope with deficiencies of previous
211	<link linkend="crop"> API </link>, that was designed to control simple capture
212	devices. Later the cropping API was adopted by video output drivers. The ioctls
213	are used to select a part of the display were the video signal is inserted. It
214	should be considered as an API abuse because the described operation is
215	actually the composing.  The selection API makes a clear distinction between
216	composing and cropping operations by setting the appropriate targets.  The V4L2
217	API lacks any support for composing to and cropping from an image inside a
218	memory buffer.  The application could configure a capture device to fill only a
219	part of an image by abusing V4L2 API.  Cropping a smaller image from a larger
220	one is achieved by setting the field
221	&v4l2-pix-format;<structfield>::bytesperline</structfield>.  Introducing an image offsets
222	could be done by modifying field &v4l2-buffer;<structfield>::m_userptr</structfield>
223	before calling <constant> VIDIOC_QBUF </constant>. Those
224	operations should be avoided because they are not portable (endianness), and do
225	not work for macroblock and Bayer formats and mmap buffers.  The selection API
226	deals with configuration of buffer cropping/composing in a clear, intuitive and
227	portable way.  Next, with the selection API the concepts of the padded target
228	and constraints flags are introduced.  Finally, &v4l2-crop; and &v4l2-cropcap;
229	have no reserved fields. Therefore there is no way to extend their functionality.
230	The new &v4l2-selection; provides a lot of place for future
231	extensions.  Driver developers are encouraged to implement only selection API.
232	The former cropping API would be simulated using the new one. </para>
234	  </section>
236	   <section>
237	      <title>Examples</title>
238	      <example>
239		<title>Resetting the cropping parameters</title>
241		<para>(A video capture device is assumed; change <constant>
242	V4L2_BUF_TYPE_VIDEO_CAPTURE </constant> for other devices; change target to
243	<constant> V4L2_SEL_TGT_COMPOSE_* </constant> family to configure composing
244	area)</para>
246		<programlisting>
248		&v4l2-selection; sel = {
249			.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
250			.target = V4L2_SEL_TGT_CROP_DEFAULT,
251		};
252		ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;sel);
253		if (ret)
254			exit(-1);
255		sel.target = V4L2_SEL_TGT_CROP;
256		ret = ioctl(fd, &VIDIOC-S-SELECTION;, &amp;sel);
257		if (ret)
258			exit(-1);
260	        </programlisting>
261	      </example>
263	      <example>
264		<title>Simple downscaling</title>
265		<para>Setting a composing area on output of size of <emphasis> at most
266	</emphasis> half of limit placed at a center of a display.</para>
267		<programlisting>
269		&v4l2-selection; sel = {
270			.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
271			.target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
272		};
273		struct v4l2_rect r;
275		ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;sel);
276		if (ret)
277			exit(-1);
278		/* setting smaller compose rectangle */
279		r.width = sel.r.width / 2;
280		r.height = sel.r.height / 2;
281		r.left = sel.r.width / 4;
282		r.top = sel.r.height / 4;
283		sel.r = r;
284		sel.target = V4L2_SEL_TGT_COMPOSE;
285		sel.flags = V4L2_SEL_FLAG_LE;
286		ret = ioctl(fd, &VIDIOC-S-SELECTION;, &amp;sel);
287		if (ret)
288			exit(-1);
290	        </programlisting>
291	      </example>
293	      <example>
294		<title>Querying for scaling factors</title>
295		<para>A video output device is assumed; change <constant>
296	V4L2_BUF_TYPE_VIDEO_OUTPUT </constant> for other devices</para>
297		<programlisting>
299		&v4l2-selection; compose = {
300			.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
301			.target = V4L2_SEL_TGT_COMPOSE,
302		};
303		&v4l2-selection; crop = {
304			.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
305			.target = V4L2_SEL_TGT_CROP,
306		};
307		double hscale, vscale;
309		ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;compose);
310		if (ret)
311			exit(-1);
312		ret = ioctl(fd, &VIDIOC-G-SELECTION;, &amp;crop);
313		if (ret)
314			exit(-1);
316		/* computing scaling factors */
317		hscale = (double)compose.r.width / crop.r.width;
318		vscale = (double)compose.r.height / crop.r.height;
320		</programlisting>
321	      </example>
323	   </section>
325	</section>
Hide Line Numbers
About Kernel Documentation Linux Kernel Contact Linux Resources Linux Blog

Information is copyright its respective author. All material is available from the Linux Kernel Source distributed under a GPL License. This page is provided as a free service by mjmwired.net.