xref: /linux/Documentation/userspace-api/media/v4l/selection-api-examples.rst (revision 778b8ebe5192e7a7f00563a7456517dfa63e1d90)
1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2.. c:namespace:: V4L
3
4********
5Examples
6********
7
8(A video capture device is assumed; change
9``V4L2_BUF_TYPE_VIDEO_CAPTURE`` for other devices; change target to
10``V4L2_SEL_TGT_COMPOSE_*`` family to configure composing area)
11
12Example: Resetting the cropping parameters
13==========================================
14
15.. code-block:: c
16
17	struct v4l2_selection sel = {
18	    .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
19	    .target = V4L2_SEL_TGT_CROP_DEFAULT,
20	};
21	ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
22	if (ret)
23	    exit(-1);
24	sel.target = V4L2_SEL_TGT_CROP;
25	ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
26	if (ret)
27	    exit(-1);
28
29Setting a composing area on output of size of *at most* half of limit
30placed at a center of a display.
31
32Example: Simple downscaling
33===========================
34
35.. code-block:: c
36
37	struct v4l2_selection sel = {
38	    .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
39	    .target = V4L2_SEL_TGT_COMPOSE_BOUNDS,
40	};
41	struct v4l2_rect r;
42
43	ret = ioctl(fd, VIDIOC_G_SELECTION, &sel);
44	if (ret)
45	    exit(-1);
46	/* setting smaller compose rectangle */
47	r.width = sel.r.width / 2;
48	r.height = sel.r.height / 2;
49	r.left = sel.r.width / 4;
50	r.top = sel.r.height / 4;
51	sel.r = r;
52	sel.target = V4L2_SEL_TGT_COMPOSE;
53	sel.flags = V4L2_SEL_FLAG_LE;
54	ret = ioctl(fd, VIDIOC_S_SELECTION, &sel);
55	if (ret)
56	    exit(-1);
57
58A video output device is assumed; change ``V4L2_BUF_TYPE_VIDEO_OUTPUT``
59for other devices
60
61Example: Querying for scaling factors
62=====================================
63
64.. code-block:: c
65
66	struct v4l2_selection compose = {
67	    .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
68	    .target = V4L2_SEL_TGT_COMPOSE,
69	};
70	struct v4l2_selection crop = {
71	    .type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
72	    .target = V4L2_SEL_TGT_CROP,
73	};
74	double hscale, vscale;
75
76	ret = ioctl(fd, VIDIOC_G_SELECTION, &compose);
77	if (ret)
78	    exit(-1);
79	ret = ioctl(fd, VIDIOC_G_SELECTION, &crop);
80	if (ret)
81	    exit(-1);
82
83	/* computing scaling factors */
84	hscale = (double)compose.r.width / crop.r.width;
85	vscale = (double)compose.r.height / crop.r.height;
86