1.. Permission is granted to copy, distribute and/or modify this
2.. document under the terms of the GNU Free Documentation License,
3.. Version 1.1 or any later version published by the Free Software
4.. Foundation, with no Invariant Sections, no Front-Cover Texts
5.. and no Back-Cover Texts. A copy of the license is included at
6.. Documentation/userspace-api/media/fdl-appendix.rst.
7..
8.. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections
9
10.. _extended-controls:
11
12*********************
13Extended Controls API
14*********************
15
16
17Introduction
18============
19
20The control mechanism as originally designed was meant to be used for
21user settings (brightness, saturation, etc). However, it turned out to
22be a very useful model for implementing more complicated driver APIs
23where each driver implements only a subset of a larger API.
24
25The MPEG encoding API was the driving force behind designing and
26implementing this extended control mechanism: the MPEG standard is quite
27large and the currently supported hardware MPEG encoders each only
28implement a subset of this standard. Further more, many parameters
29relating to how the video is encoded into an MPEG stream are specific to
30the MPEG encoding chip since the MPEG standard only defines the format
31of the resulting MPEG stream, not how the video is actually encoded into
32that format.
33
34Unfortunately, the original control API lacked some features needed for
35these new uses and so it was extended into the (not terribly originally
36named) extended control API.
37
38Even though the MPEG encoding API was the first effort to use the
39Extended Control API, nowadays there are also other classes of Extended
40Controls, such as Camera Controls and FM Transmitter Controls. The
41Extended Controls API as well as all Extended Controls classes are
42described in the following text.
43
44
45The Extended Control API
46========================
47
48Three new ioctls are available:
49:ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`,
50:ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` and
51:ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`. These ioctls act
52on arrays of controls (as opposed to the
53:ref:`VIDIOC_G_CTRL <VIDIOC_G_CTRL>` and
54:ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>` ioctls that act on a single
55control). This is needed since it is often required to atomically change
56several controls at once.
57
58Each of the new ioctls expects a pointer to a struct
59:c:type:`v4l2_ext_controls`. This structure
60contains a pointer to the control array, a count of the number of
61controls in that array and a control class. Control classes are used to
62group similar controls into a single class. For example, control class
63``V4L2_CTRL_CLASS_USER`` contains all user controls (i. e. all controls
64that can also be set using the old :ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>`
65ioctl). Control class ``V4L2_CTRL_CLASS_MPEG`` contains all controls
66relating to MPEG encoding, etc.
67
68All controls in the control array must belong to the specified control
69class. An error is returned if this is not the case.
70
71It is also possible to use an empty control array (``count`` == 0) to check
72whether the specified control class is supported.
73
74The control array is a struct
75:c:type:`v4l2_ext_control` array. The
76struct :c:type:`v4l2_ext_control` is very similar to
77struct :c:type:`v4l2_control`, except for the fact that
78it also allows for 64-bit values and pointers to be passed.
79
80Since the struct :c:type:`v4l2_ext_control` supports
81pointers it is now also possible to have controls with compound types
82such as N-dimensional arrays and/or structures. You need to specify the
83``V4L2_CTRL_FLAG_NEXT_COMPOUND`` when enumerating controls to actually
84be able to see such compound controls. In other words, these controls
85with compound types should only be used programmatically.
86
87Since such compound controls need to expose more information about
88themselves than is possible with :ref:`VIDIOC_QUERYCTRL <VIDIOC_QUERYCTRL>`
89the :ref:`VIDIOC_QUERY_EXT_CTRL <VIDIOC_QUERYCTRL>` ioctl was added. In
90particular, this ioctl gives the dimensions of the N-dimensional array if
91this control consists of more than one element.
92
93.. note::
94
95   #. It is important to realize that due to the flexibility of controls it is
96      necessary to check whether the control you want to set actually is
97      supported in the driver and what the valid range of values is. So use
98      :ref:`VIDIOC_QUERYCTRL` to check this.
99
100   #. It is possible that some of the menu indices in a control of
101      type ``V4L2_CTRL_TYPE_MENU`` may not be supported (``VIDIOC_QUERYMENU``
102      will return an error). A good example is the list of supported MPEG
103      audio bitrates. Some drivers only support one or two bitrates, others
104      support a wider range.
105
106All controls use machine endianness.
107
108
109Enumerating Extended Controls
110=============================
111
112The recommended way to enumerate over the extended controls is by using
113:ref:`VIDIOC_QUERYCTRL` in combination with the
114``V4L2_CTRL_FLAG_NEXT_CTRL`` flag:
115
116
117.. code-block:: c
118
119    struct v4l2_queryctrl qctrl;
120
121    qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
122    while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl)) {
123	/* ... */
124	qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
125    }
126
127The initial control ID is set to 0 ORed with the
128``V4L2_CTRL_FLAG_NEXT_CTRL`` flag. The ``VIDIOC_QUERYCTRL`` ioctl will
129return the first control with a higher ID than the specified one. When
130no such controls are found an error is returned.
131
132If you want to get all controls within a specific control class, then
133you can set the initial ``qctrl.id`` value to the control class and add
134an extra check to break out of the loop when a control of another
135control class is found:
136
137
138.. code-block:: c
139
140    qctrl.id = V4L2_CTRL_CLASS_MPEG | V4L2_CTRL_FLAG_NEXT_CTRL;
141    while (0 == ioctl(fd, VIDIOC_QUERYCTRL, &qctrl)) {
142	if (V4L2_CTRL_ID2CLASS(qctrl.id) != V4L2_CTRL_CLASS_MPEG)
143	    break;
144	/* ... */
145	qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
146    }
147
148The 32-bit ``qctrl.id`` value is subdivided into three bit ranges: the
149top 4 bits are reserved for flags (e. g. ``V4L2_CTRL_FLAG_NEXT_CTRL``)
150and are not actually part of the ID. The remaining 28 bits form the
151control ID, of which the most significant 12 bits define the control
152class and the least significant 16 bits identify the control within the
153control class. It is guaranteed that these last 16 bits are always
154non-zero for controls. The range of 0x1000 and up are reserved for
155driver-specific controls. The macro ``V4L2_CTRL_ID2CLASS(id)`` returns
156the control class ID based on a control ID.
157
158If the driver does not support extended controls, then
159``VIDIOC_QUERYCTRL`` will fail when used in combination with
160``V4L2_CTRL_FLAG_NEXT_CTRL``. In that case the old method of enumerating
161control should be used (see :ref:`enum_all_controls`). But if it is
162supported, then it is guaranteed to enumerate over all controls,
163including driver-private controls.
164
165
166Creating Control Panels
167=======================
168
169It is possible to create control panels for a graphical user interface
170where the user can select the various controls. Basically you will have
171to iterate over all controls using the method described above. Each
172control class starts with a control of type
173``V4L2_CTRL_TYPE_CTRL_CLASS``. ``VIDIOC_QUERYCTRL`` will return the name
174of this control class which can be used as the title of a tab page
175within a control panel.
176
177The flags field of struct :ref:`v4l2_queryctrl <v4l2-queryctrl>` also
178contains hints on the behavior of the control. See the
179:ref:`VIDIOC_QUERYCTRL` documentation for more
180details.
181