0001 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
0002
0003 .. _extended-controls:
0004
0005 *********************
0006 Extended Controls API
0007 *********************
0008
0009
0010 Introduction
0011 ============
0012
0013 The control mechanism as originally designed was meant to be used for
0014 user settings (brightness, saturation, etc). However, it turned out to
0015 be a very useful model for implementing more complicated driver APIs
0016 where each driver implements only a subset of a larger API.
0017
0018 The MPEG encoding API was the driving force behind designing and
0019 implementing this extended control mechanism: the MPEG standard is quite
0020 large and the currently supported hardware MPEG encoders each only
0021 implement a subset of this standard. Further more, many parameters
0022 relating to how the video is encoded into an MPEG stream are specific to
0023 the MPEG encoding chip since the MPEG standard only defines the format
0024 of the resulting MPEG stream, not how the video is actually encoded into
0025 that format.
0026
0027 Unfortunately, the original control API lacked some features needed for
0028 these new uses and so it was extended into the (not terribly originally
0029 named) extended control API.
0030
0031 Even though the MPEG encoding API was the first effort to use the
0032 Extended Control API, nowadays there are also other classes of Extended
0033 Controls, such as Camera Controls and FM Transmitter Controls. The
0034 Extended Controls API as well as all Extended Controls classes are
0035 described in the following text.
0036
0037
0038 The Extended Control API
0039 ========================
0040
0041 Three new ioctls are available:
0042 :ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`,
0043 :ref:`VIDIOC_S_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>` and
0044 :ref:`VIDIOC_TRY_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`. These ioctls act
0045 on arrays of controls (as opposed to the
0046 :ref:`VIDIOC_G_CTRL <VIDIOC_G_CTRL>` and
0047 :ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>` ioctls that act on a single
0048 control). This is needed since it is often required to atomically change
0049 several controls at once.
0050
0051 Each of the new ioctls expects a pointer to a struct
0052 :c:type:`v4l2_ext_controls`. This structure
0053 contains a pointer to the control array, a count of the number of
0054 controls in that array and a control class. Control classes are used to
0055 group similar controls into a single class. For example, control class
0056 ``V4L2_CTRL_CLASS_USER`` contains all user controls (i. e. all controls
0057 that can also be set using the old :ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>`
0058 ioctl). Control class ``V4L2_CTRL_CLASS_CODEC`` contains controls
0059 relating to codecs.
0060
0061 All controls in the control array must belong to the specified control
0062 class. An error is returned if this is not the case.
0063
0064 It is also possible to use an empty control array (``count`` == 0) to check
0065 whether the specified control class is supported.
0066
0067 The control array is a struct
0068 :c:type:`v4l2_ext_control` array. The
0069 struct :c:type:`v4l2_ext_control` is very similar to
0070 struct :c:type:`v4l2_control`, except for the fact that
0071 it also allows for 64-bit values and pointers to be passed.
0072
0073 Since the struct :c:type:`v4l2_ext_control` supports
0074 pointers it is now also possible to have controls with compound types
0075 such as N-dimensional arrays and/or structures. You need to specify the
0076 ``V4L2_CTRL_FLAG_NEXT_COMPOUND`` when enumerating controls to actually
0077 be able to see such compound controls. In other words, these controls
0078 with compound types should only be used programmatically.
0079
0080 Since such compound controls need to expose more information about
0081 themselves than is possible with :ref:`VIDIOC_QUERYCTRL <VIDIOC_QUERYCTRL>`
0082 the :ref:`VIDIOC_QUERY_EXT_CTRL <VIDIOC_QUERYCTRL>` ioctl was added. In
0083 particular, this ioctl gives the dimensions of the N-dimensional array if
0084 this control consists of more than one element.
0085
0086 .. note::
0087
0088 #. It is important to realize that due to the flexibility of controls it is
0089 necessary to check whether the control you want to set actually is
0090 supported in the driver and what the valid range of values is. So use
0091 :ref:`VIDIOC_QUERYCTRL` to check this.
0092
0093 #. It is possible that some of the menu indices in a control of
0094 type ``V4L2_CTRL_TYPE_MENU`` may not be supported (``VIDIOC_QUERYMENU``
0095 will return an error). A good example is the list of supported MPEG
0096 audio bitrates. Some drivers only support one or two bitrates, others
0097 support a wider range.
0098
0099 All controls use machine endianness.
0100
0101
0102 Enumerating Extended Controls
0103 =============================
0104
0105 The recommended way to enumerate over the extended controls is by using
0106 :ref:`VIDIOC_QUERYCTRL` in combination with the
0107 ``V4L2_CTRL_FLAG_NEXT_CTRL`` flag:
0108
0109
0110 .. code-block:: c
0111
0112 struct v4l2_queryctrl qctrl;
0113
0114 qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
0115 while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl)) {
0116 /* ... */
0117 qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
0118 }
0119
0120 The initial control ID is set to 0 ORed with the
0121 ``V4L2_CTRL_FLAG_NEXT_CTRL`` flag. The ``VIDIOC_QUERYCTRL`` ioctl will
0122 return the first control with a higher ID than the specified one. When
0123 no such controls are found an error is returned.
0124
0125 If you want to get all controls within a specific control class, then
0126 you can set the initial ``qctrl.id`` value to the control class and add
0127 an extra check to break out of the loop when a control of another
0128 control class is found:
0129
0130
0131 .. code-block:: c
0132
0133 qctrl.id = V4L2_CTRL_CLASS_CODEC | V4L2_CTRL_FLAG_NEXT_CTRL;
0134 while (0 == ioctl(fd, VIDIOC_QUERYCTRL, &qctrl)) {
0135 if (V4L2_CTRL_ID2CLASS(qctrl.id) != V4L2_CTRL_CLASS_CODEC)
0136 break;
0137 /* ... */
0138 qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
0139 }
0140
0141 The 32-bit ``qctrl.id`` value is subdivided into three bit ranges: the
0142 top 4 bits are reserved for flags (e. g. ``V4L2_CTRL_FLAG_NEXT_CTRL``)
0143 and are not actually part of the ID. The remaining 28 bits form the
0144 control ID, of which the most significant 12 bits define the control
0145 class and the least significant 16 bits identify the control within the
0146 control class. It is guaranteed that these last 16 bits are always
0147 non-zero for controls. The range of 0x1000 and up are reserved for
0148 driver-specific controls. The macro ``V4L2_CTRL_ID2CLASS(id)`` returns
0149 the control class ID based on a control ID.
0150
0151 If the driver does not support extended controls, then
0152 ``VIDIOC_QUERYCTRL`` will fail when used in combination with
0153 ``V4L2_CTRL_FLAG_NEXT_CTRL``. In that case the old method of enumerating
0154 control should be used (see :ref:`enum_all_controls`). But if it is
0155 supported, then it is guaranteed to enumerate over all controls,
0156 including driver-private controls.
0157
0158
0159 Creating Control Panels
0160 =======================
0161
0162 It is possible to create control panels for a graphical user interface
0163 where the user can select the various controls. Basically you will have
0164 to iterate over all controls using the method described above. Each
0165 control class starts with a control of type
0166 ``V4L2_CTRL_TYPE_CTRL_CLASS``. ``VIDIOC_QUERYCTRL`` will return the name
0167 of this control class which can be used as the title of a tab page
0168 within a control panel.
0169
0170 The flags field of struct :ref:`v4l2_queryctrl <v4l2-queryctrl>` also
0171 contains hints on the behavior of the control. See the
0172 :ref:`VIDIOC_QUERYCTRL` documentation for more
0173 details.