David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 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/media/uapi/fdl-appendix.rst. |
| 7 | .. |
| 8 | .. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 9 | |
| 10 | .. _extended-controls: |
| 11 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 12 | ********************* |
| 13 | Extended Controls API |
| 14 | ********************* |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | Introduction |
| 18 | ============ |
| 19 | |
| 20 | The control mechanism as originally designed was meant to be used for |
| 21 | user settings (brightness, saturation, etc). However, it turned out to |
| 22 | be a very useful model for implementing more complicated driver APIs |
| 23 | where each driver implements only a subset of a larger API. |
| 24 | |
| 25 | The MPEG encoding API was the driving force behind designing and |
| 26 | implementing this extended control mechanism: the MPEG standard is quite |
| 27 | large and the currently supported hardware MPEG encoders each only |
| 28 | implement a subset of this standard. Further more, many parameters |
| 29 | relating to how the video is encoded into an MPEG stream are specific to |
| 30 | the MPEG encoding chip since the MPEG standard only defines the format |
| 31 | of the resulting MPEG stream, not how the video is actually encoded into |
| 32 | that format. |
| 33 | |
| 34 | Unfortunately, the original control API lacked some features needed for |
| 35 | these new uses and so it was extended into the (not terribly originally |
| 36 | named) extended control API. |
| 37 | |
| 38 | Even though the MPEG encoding API was the first effort to use the |
| 39 | Extended Control API, nowadays there are also other classes of Extended |
| 40 | Controls, such as Camera Controls and FM Transmitter Controls. The |
| 41 | Extended Controls API as well as all Extended Controls classes are |
| 42 | described in the following text. |
| 43 | |
| 44 | |
| 45 | The Extended Control API |
| 46 | ======================== |
| 47 | |
| 48 | Three 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 |
| 52 | on 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 |
| 55 | control). This is needed since it is often required to atomically change |
| 56 | several controls at once. |
| 57 | |
| 58 | Each of the new ioctls expects a pointer to a struct |
| 59 | :c:type:`v4l2_ext_controls`. This structure |
| 60 | contains a pointer to the control array, a count of the number of |
| 61 | controls in that array and a control class. Control classes are used to |
| 62 | group similar controls into a single class. For example, control class |
| 63 | ``V4L2_CTRL_CLASS_USER`` contains all user controls (i. e. all controls |
| 64 | that can also be set using the old :ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>` |
| 65 | ioctl). Control class ``V4L2_CTRL_CLASS_MPEG`` contains all controls |
| 66 | relating to MPEG encoding, etc. |
| 67 | |
| 68 | All controls in the control array must belong to the specified control |
| 69 | class. An error is returned if this is not the case. |
| 70 | |
| 71 | It is also possible to use an empty control array (``count`` == 0) to check |
| 72 | whether the specified control class is supported. |
| 73 | |
| 74 | The control array is a struct |
| 75 | :c:type:`v4l2_ext_control` array. The |
| 76 | struct :c:type:`v4l2_ext_control` is very similar to |
| 77 | struct :c:type:`v4l2_control`, except for the fact that |
| 78 | it also allows for 64-bit values and pointers to be passed. |
| 79 | |
| 80 | Since the struct :c:type:`v4l2_ext_control` supports |
| 81 | pointers it is now also possible to have controls with compound types |
| 82 | such as N-dimensional arrays and/or structures. You need to specify the |
| 83 | ``V4L2_CTRL_FLAG_NEXT_COMPOUND`` when enumerating controls to actually |
| 84 | be able to see such compound controls. In other words, these controls |
| 85 | with compound types should only be used programmatically. |
| 86 | |
| 87 | Since such compound controls need to expose more information about |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 88 | themselves than is possible with :ref:`VIDIOC_QUERYCTRL <VIDIOC_QUERYCTRL>` |
| 89 | the :ref:`VIDIOC_QUERY_EXT_CTRL <VIDIOC_QUERYCTRL>` ioctl was added. In |
| 90 | particular, this ioctl gives the dimensions of the N-dimensional array if |
| 91 | this control consists of more than one element. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 98 | :ref:`VIDIOC_QUERYCTRL` to check this. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
| 106 | All controls use machine endianness. |
| 107 | |
| 108 | |
| 109 | Enumerating Extended Controls |
| 110 | ============================= |
| 111 | |
| 112 | The 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 | |
| 127 | The initial control ID is set to 0 ORed with the |
| 128 | ``V4L2_CTRL_FLAG_NEXT_CTRL`` flag. The ``VIDIOC_QUERYCTRL`` ioctl will |
| 129 | return the first control with a higher ID than the specified one. When |
| 130 | no such controls are found an error is returned. |
| 131 | |
| 132 | If you want to get all controls within a specific control class, then |
| 133 | you can set the initial ``qctrl.id`` value to the control class and add |
| 134 | an extra check to break out of the loop when a control of another |
| 135 | control 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 144 | /* ... */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 145 | qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL; |
| 146 | } |
| 147 | |
| 148 | The 32-bit ``qctrl.id`` value is subdivided into three bit ranges: the |
| 149 | top 4 bits are reserved for flags (e. g. ``V4L2_CTRL_FLAG_NEXT_CTRL``) |
| 150 | and are not actually part of the ID. The remaining 28 bits form the |
| 151 | control ID, of which the most significant 12 bits define the control |
| 152 | class and the least significant 16 bits identify the control within the |
| 153 | control class. It is guaranteed that these last 16 bits are always |
| 154 | non-zero for controls. The range of 0x1000 and up are reserved for |
| 155 | driver-specific controls. The macro ``V4L2_CTRL_ID2CLASS(id)`` returns |
| 156 | the control class ID based on a control ID. |
| 157 | |
| 158 | If 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 |
| 161 | control should be used (see :ref:`enum_all_controls`). But if it is |
| 162 | supported, then it is guaranteed to enumerate over all controls, |
| 163 | including driver-private controls. |
| 164 | |
| 165 | |
| 166 | Creating Control Panels |
| 167 | ======================= |
| 168 | |
| 169 | It is possible to create control panels for a graphical user interface |
| 170 | where the user can select the various controls. Basically you will have |
| 171 | to iterate over all controls using the method described above. Each |
| 172 | control class starts with a control of type |
| 173 | ``V4L2_CTRL_TYPE_CTRL_CLASS``. ``VIDIOC_QUERYCTRL`` will return the name |
| 174 | of this control class which can be used as the title of a tab page |
| 175 | within a control panel. |
| 176 | |
| 177 | The flags field of struct :ref:`v4l2_queryctrl <v4l2-queryctrl>` also |
| 178 | contains hints on the behavior of the control. See the |
| 179 | :ref:`VIDIOC_QUERYCTRL` documentation for more |
| 180 | details. |