1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2
3.. _control:
4
5*************
6User Controls
7*************
8
9Devices typically have a number of user-settable controls such as
10brightness, saturation and so on, which would be presented to the user
11on a graphical user interface. But, different devices will have
12different controls available, and furthermore, the range of possible
13values, and the default value will vary from device to device. The
14control ioctls provide the information and a mechanism to create a nice
15user interface for these controls that will work correctly with any
16device.
17
18All controls are accessed using an ID value. V4L2 defines several IDs
19for specific purposes. Drivers can also implement their own custom
20controls using ``V4L2_CID_PRIVATE_BASE``  [#f1]_ and higher values. The
21pre-defined control IDs have the prefix ``V4L2_CID_``, and are listed in
22:ref:`control-id`. The ID is used when querying the attributes of a
23control, and when getting or setting the current value.
24
25Generally applications should present controls to the user without
26assumptions about their purpose. Each control comes with a name string
27the user is supposed to understand. When the purpose is non-intuitive
28the driver writer should provide a user manual, a user interface plug-in
29or a driver specific panel application. Predefined IDs were introduced
30to change a few controls programmatically, for example to mute a device
31during a channel switch.
32
33Drivers may enumerate different controls after switching the current
34video input or output, tuner or modulator, or audio input or output.
35Different in the sense of other bounds, another default and current
36value, step size or other menu items. A control with a certain *custom*
37ID can also change name and type.
38
39If a control is not applicable to the current configuration of the
40device (for example, it doesn't apply to the current video input)
41drivers set the ``V4L2_CTRL_FLAG_INACTIVE`` flag.
42
43Control values are stored globally, they do not change when switching
44except to stay within the reported bounds. They also do not change e. g.
45when the device is opened or closed, when the tuner radio frequency is
46changed or generally never without application request.
47
48V4L2 specifies an event mechanism to notify applications when controls
49change value (see
50:ref:`VIDIOC_SUBSCRIBE_EVENT`, event
51``V4L2_EVENT_CTRL``), panel applications might want to make use of that
52in order to always reflect the correct control value.
53
54All controls use machine endianness.
55
56
57.. _control-id:
58
59Control IDs
60===========
61
62``V4L2_CID_BASE``
63    First predefined ID, equal to ``V4L2_CID_BRIGHTNESS``.
64
65``V4L2_CID_USER_BASE``
66    Synonym of ``V4L2_CID_BASE``.
67
68``V4L2_CID_BRIGHTNESS`` ``(integer)``
69    Picture brightness, or more precisely, the black level.
70
71``V4L2_CID_CONTRAST`` ``(integer)``
72    Picture contrast or luma gain.
73
74``V4L2_CID_SATURATION`` ``(integer)``
75    Picture color saturation or chroma gain.
76
77``V4L2_CID_HUE`` ``(integer)``
78    Hue or color balance.
79
80``V4L2_CID_AUDIO_VOLUME`` ``(integer)``
81    Overall audio volume. Note some drivers also provide an OSS or ALSA
82    mixer interface.
83
84``V4L2_CID_AUDIO_BALANCE`` ``(integer)``
85    Audio stereo balance. Minimum corresponds to all the way left,
86    maximum to right.
87
88``V4L2_CID_AUDIO_BASS`` ``(integer)``
89    Audio bass adjustment.
90
91``V4L2_CID_AUDIO_TREBLE`` ``(integer)``
92    Audio treble adjustment.
93
94``V4L2_CID_AUDIO_MUTE`` ``(boolean)``
95    Mute audio, i. e. set the volume to zero, however without affecting
96    ``V4L2_CID_AUDIO_VOLUME``. Like ALSA drivers, V4L2 drivers must mute
97    at load time to avoid excessive noise. Actually the entire device
98    should be reset to a low power consumption state.
99
100``V4L2_CID_AUDIO_LOUDNESS`` ``(boolean)``
101    Loudness mode (bass boost).
102
103``V4L2_CID_BLACK_LEVEL`` ``(integer)``
104    Another name for brightness (not a synonym of
105    ``V4L2_CID_BRIGHTNESS``). This control is deprecated and should not
106    be used in new drivers and applications.
107
108``V4L2_CID_AUTO_WHITE_BALANCE`` ``(boolean)``
109    Automatic white balance (cameras).
110
111``V4L2_CID_DO_WHITE_BALANCE`` ``(button)``
112    This is an action control. When set (the value is ignored), the
113    device will do a white balance and then hold the current setting.
114    Contrast this with the boolean ``V4L2_CID_AUTO_WHITE_BALANCE``,
115    which, when activated, keeps adjusting the white balance.
116
117``V4L2_CID_RED_BALANCE`` ``(integer)``
118    Red chroma balance.
119
120``V4L2_CID_BLUE_BALANCE`` ``(integer)``
121    Blue chroma balance.
122
123``V4L2_CID_GAMMA`` ``(integer)``
124    Gamma adjust.
125
126``V4L2_CID_WHITENESS`` ``(integer)``
127    Whiteness for grey-scale devices. This is a synonym for
128    ``V4L2_CID_GAMMA``. This control is deprecated and should not be
129    used in new drivers and applications.
130
131``V4L2_CID_EXPOSURE`` ``(integer)``
132    Exposure (cameras). [Unit?]
133
134``V4L2_CID_AUTOGAIN`` ``(boolean)``
135    Automatic gain/exposure control.
136
137``V4L2_CID_GAIN`` ``(integer)``
138    Gain control.
139
140    Primarily used to control gain on e.g. TV tuners but also on
141    webcams. Most devices control only digital gain with this control
142    but on some this could include analogue gain as well. Devices that
143    recognise the difference between digital and analogue gain use
144    controls ``V4L2_CID_DIGITAL_GAIN`` and ``V4L2_CID_ANALOGUE_GAIN``.
145
146``V4L2_CID_HFLIP`` ``(boolean)``
147    Mirror the picture horizontally.
148
149``V4L2_CID_VFLIP`` ``(boolean)``
150    Mirror the picture vertically.
151
152.. _v4l2-power-line-frequency:
153
154``V4L2_CID_POWER_LINE_FREQUENCY`` ``(enum)``
155    Enables a power line frequency filter to avoid flicker. Possible
156    values for ``enum v4l2_power_line_frequency`` are:
157
158    ==========================================  ==
159    ``V4L2_CID_POWER_LINE_FREQUENCY_DISABLED``	 0
160    ``V4L2_CID_POWER_LINE_FREQUENCY_50HZ``	 1
161    ``V4L2_CID_POWER_LINE_FREQUENCY_60HZ``	 2
162    ``V4L2_CID_POWER_LINE_FREQUENCY_AUTO``	 3
163    ==========================================  ==
164
165``V4L2_CID_HUE_AUTO`` ``(boolean)``
166    Enables automatic hue control by the device. The effect of setting
167    ``V4L2_CID_HUE`` while automatic hue control is enabled is
168    undefined, drivers should ignore such request.
169
170``V4L2_CID_WHITE_BALANCE_TEMPERATURE`` ``(integer)``
171    This control specifies the white balance settings as a color
172    temperature in Kelvin. A driver should have a minimum of 2800
173    (incandescent) to 6500 (daylight). For more information about color
174    temperature see
175    `Wikipedia <http://en.wikipedia.org/wiki/Color_temperature>`__.
176
177``V4L2_CID_SHARPNESS`` ``(integer)``
178    Adjusts the sharpness filters in a camera. The minimum value
179    disables the filters, higher values give a sharper picture.
180
181``V4L2_CID_BACKLIGHT_COMPENSATION`` ``(integer)``
182    Adjusts the backlight compensation in a camera. The minimum value
183    disables backlight compensation.
184
185``V4L2_CID_CHROMA_AGC`` ``(boolean)``
186    Chroma automatic gain control.
187
188``V4L2_CID_CHROMA_GAIN`` ``(integer)``
189    Adjusts the Chroma gain control (for use when chroma AGC is
190    disabled).
191
192``V4L2_CID_COLOR_KILLER`` ``(boolean)``
193    Enable the color killer (i. e. force a black & white image in case
194    of a weak video signal).
195
196.. _v4l2-colorfx:
197
198``V4L2_CID_COLORFX`` ``(enum)``
199    Selects a color effect. The following values are defined:
200
201
202
203.. tabularcolumns:: |p{5.7cm}|p{11.8cm}|
204
205.. flat-table::
206    :header-rows:  0
207    :stub-columns: 0
208    :widths: 11 24
209
210    * - ``V4L2_COLORFX_NONE``
211      - Color effect is disabled.
212    * - ``V4L2_COLORFX_ANTIQUE``
213      - An aging (old photo) effect.
214    * - ``V4L2_COLORFX_ART_FREEZE``
215      - Frost color effect.
216    * - ``V4L2_COLORFX_AQUA``
217      - Water color, cool tone.
218    * - ``V4L2_COLORFX_BW``
219      - Black and white.
220    * - ``V4L2_COLORFX_EMBOSS``
221      - Emboss, the highlights and shadows replace light/dark boundaries
222	and low contrast areas are set to a gray background.
223    * - ``V4L2_COLORFX_GRASS_GREEN``
224      - Grass green.
225    * - ``V4L2_COLORFX_NEGATIVE``
226      - Negative.
227    * - ``V4L2_COLORFX_SEPIA``
228      - Sepia tone.
229    * - ``V4L2_COLORFX_SKETCH``
230      - Sketch.
231    * - ``V4L2_COLORFX_SKIN_WHITEN``
232      - Skin whiten.
233    * - ``V4L2_COLORFX_SKY_BLUE``
234      - Sky blue.
235    * - ``V4L2_COLORFX_SOLARIZATION``
236      - Solarization, the image is partially reversed in tone, only color
237	values above or below a certain threshold are inverted.
238    * - ``V4L2_COLORFX_SILHOUETTE``
239      - Silhouette (outline).
240    * - ``V4L2_COLORFX_VIVID``
241      - Vivid colors.
242    * - ``V4L2_COLORFX_SET_CBCR``
243      - The Cb and Cr chroma components are replaced by fixed coefficients
244	determined by ``V4L2_CID_COLORFX_CBCR`` control.
245
246
247
248``V4L2_CID_COLORFX_CBCR`` ``(integer)``
249    Determines the Cb and Cr coefficients for ``V4L2_COLORFX_SET_CBCR``
250    color effect. Bits [7:0] of the supplied 32 bit value are
251    interpreted as Cr component, bits [15:8] as Cb component and bits
252    [31:16] must be zero.
253
254``V4L2_CID_AUTOBRIGHTNESS`` ``(boolean)``
255    Enable Automatic Brightness.
256
257``V4L2_CID_ROTATE`` ``(integer)``
258    Rotates the image by specified angle. Common angles are 90, 270 and
259    180. Rotating the image to 90 and 270 will reverse the height and
260    width of the display window. It is necessary to set the new height
261    and width of the picture using the
262    :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl according to the
263    rotation angle selected.
264
265``V4L2_CID_BG_COLOR`` ``(integer)``
266    Sets the background color on the current output device. Background
267    color needs to be specified in the RGB24 format. The supplied 32 bit
268    value is interpreted as bits 0-7 Red color information, bits 8-15
269    Green color information, bits 16-23 Blue color information and bits
270    24-31 must be zero.
271
272``V4L2_CID_ILLUMINATORS_1 V4L2_CID_ILLUMINATORS_2`` ``(boolean)``
273    Switch on or off the illuminator 1 or 2 of the device (usually a
274    microscope).
275
276``V4L2_CID_MIN_BUFFERS_FOR_CAPTURE`` ``(integer)``
277    This is a read-only control that can be read by the application and
278    used as a hint to determine the number of CAPTURE buffers to pass to
279    REQBUFS. The value is the minimum number of CAPTURE buffers that is
280    necessary for hardware to work.
281
282``V4L2_CID_MIN_BUFFERS_FOR_OUTPUT`` ``(integer)``
283    This is a read-only control that can be read by the application and
284    used as a hint to determine the number of OUTPUT buffers to pass to
285    REQBUFS. The value is the minimum number of OUTPUT buffers that is
286    necessary for hardware to work.
287
288.. _v4l2-alpha-component:
289
290``V4L2_CID_ALPHA_COMPONENT`` ``(integer)``
291    Sets the alpha color component. When a capture device (or capture
292    queue of a mem-to-mem device) produces a frame format that includes
293    an alpha component (e.g.
294    :ref:`packed RGB image formats <pixfmt-rgb>`) and the alpha value
295    is not defined by the device or the mem-to-mem input data this
296    control lets you select the alpha component value of all pixels.
297    When an output device (or output queue of a mem-to-mem device)
298    consumes a frame format that doesn't include an alpha component and
299    the device supports alpha channel processing this control lets you
300    set the alpha component value of all pixels for further processing
301    in the device.
302
303``V4L2_CID_LASTP1``
304    End of the predefined control IDs (currently
305    ``V4L2_CID_ALPHA_COMPONENT`` + 1).
306
307``V4L2_CID_PRIVATE_BASE``
308    ID of the first custom (driver specific) control. Applications
309    depending on particular custom controls should check the driver name
310    and version, see :ref:`querycap`.
311
312Applications can enumerate the available controls with the
313:ref:`VIDIOC_QUERYCTRL` and
314:ref:`VIDIOC_QUERYMENU <VIDIOC_QUERYCTRL>` ioctls, get and set a
315control value with the :ref:`VIDIOC_G_CTRL <VIDIOC_G_CTRL>` and
316:ref:`VIDIOC_S_CTRL <VIDIOC_G_CTRL>` ioctls. Drivers must implement
317``VIDIOC_QUERYCTRL``, ``VIDIOC_G_CTRL`` and ``VIDIOC_S_CTRL`` when the
318device has one or more controls, ``VIDIOC_QUERYMENU`` when it has one or
319more menu type controls.
320
321
322.. _enum_all_controls:
323
324Example: Enumerating all controls
325=================================
326
327.. code-block:: c
328
329    struct v4l2_queryctrl queryctrl;
330    struct v4l2_querymenu querymenu;
331
332    static void enumerate_menu(__u32 id)
333    {
334	printf("  Menu items:\\n");
335
336	memset(&querymenu, 0, sizeof(querymenu));
337	querymenu.id = id;
338
339	for (querymenu.index = queryctrl.minimum;
340	     querymenu.index <= queryctrl.maximum;
341	     querymenu.index++) {
342	    if (0 == ioctl(fd, VIDIOC_QUERYMENU, &querymenu)) {
343		printf("  %s\\n", querymenu.name);
344	    }
345	}
346    }
347
348    memset(&queryctrl, 0, sizeof(queryctrl));
349
350    queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
351    while (0 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
352	if (!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)) {
353	    printf("Control %s\\n", queryctrl.name);
354
355	    if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
356	        enumerate_menu(queryctrl.id);
357        }
358
359	queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
360    }
361    if (errno != EINVAL) {
362	perror("VIDIOC_QUERYCTRL");
363	exit(EXIT_FAILURE);
364    }
365
366Example: Enumerating all controls including compound controls
367=============================================================
368
369.. code-block:: c
370
371    struct v4l2_query_ext_ctrl query_ext_ctrl;
372
373    memset(&query_ext_ctrl, 0, sizeof(query_ext_ctrl));
374
375    query_ext_ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
376    while (0 == ioctl(fd, VIDIOC_QUERY_EXT_CTRL, &query_ext_ctrl)) {
377	if (!(query_ext_ctrl.flags & V4L2_CTRL_FLAG_DISABLED)) {
378	    printf("Control %s\\n", query_ext_ctrl.name);
379
380	    if (query_ext_ctrl.type == V4L2_CTRL_TYPE_MENU)
381	        enumerate_menu(query_ext_ctrl.id);
382        }
383
384	query_ext_ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
385    }
386    if (errno != EINVAL) {
387	perror("VIDIOC_QUERY_EXT_CTRL");
388	exit(EXIT_FAILURE);
389    }
390
391Example: Enumerating all user controls (old style)
392==================================================
393
394.. code-block:: c
395
396
397    memset(&queryctrl, 0, sizeof(queryctrl));
398
399    for (queryctrl.id = V4L2_CID_BASE;
400	 queryctrl.id < V4L2_CID_LASTP1;
401	 queryctrl.id++) {
402	if (0 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
403	    if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
404		continue;
405
406	    printf("Control %s\\n", queryctrl.name);
407
408	    if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
409		enumerate_menu(queryctrl.id);
410	} else {
411	    if (errno == EINVAL)
412		continue;
413
414	    perror("VIDIOC_QUERYCTRL");
415	    exit(EXIT_FAILURE);
416	}
417    }
418
419    for (queryctrl.id = V4L2_CID_PRIVATE_BASE;;
420	 queryctrl.id++) {
421	if (0 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
422	    if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
423		continue;
424
425	    printf("Control %s\\n", queryctrl.name);
426
427	    if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
428		enumerate_menu(queryctrl.id);
429	} else {
430	    if (errno == EINVAL)
431		break;
432
433	    perror("VIDIOC_QUERYCTRL");
434	    exit(EXIT_FAILURE);
435	}
436    }
437
438
439Example: Changing controls
440==========================
441
442.. code-block:: c
443
444    struct v4l2_queryctrl queryctrl;
445    struct v4l2_control control;
446
447    memset(&queryctrl, 0, sizeof(queryctrl));
448    queryctrl.id = V4L2_CID_BRIGHTNESS;
449
450    if (-1 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
451	if (errno != EINVAL) {
452	    perror("VIDIOC_QUERYCTRL");
453	    exit(EXIT_FAILURE);
454	} else {
455	    printf("V4L2_CID_BRIGHTNESS is not supportedn");
456	}
457    } else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
458	printf("V4L2_CID_BRIGHTNESS is not supportedn");
459    } else {
460	memset(&control, 0, sizeof (control));
461	control.id = V4L2_CID_BRIGHTNESS;
462	control.value = queryctrl.default_value;
463
464	if (-1 == ioctl(fd, VIDIOC_S_CTRL, &control)) {
465	    perror("VIDIOC_S_CTRL");
466	    exit(EXIT_FAILURE);
467	}
468    }
469
470    memset(&control, 0, sizeof(control));
471    control.id = V4L2_CID_CONTRAST;
472
473    if (0 == ioctl(fd, VIDIOC_G_CTRL, &control)) {
474	control.value += 1;
475
476	/* The driver may clamp the value or return ERANGE, ignored here */
477
478	if (-1 == ioctl(fd, VIDIOC_S_CTRL, &control)
479	    && errno != ERANGE) {
480	    perror("VIDIOC_S_CTRL");
481	    exit(EXIT_FAILURE);
482	}
483    /* Ignore if V4L2_CID_CONTRAST is unsupported */
484    } else if (errno != EINVAL) {
485	perror("VIDIOC_G_CTRL");
486	exit(EXIT_FAILURE);
487    }
488
489    control.id = V4L2_CID_AUDIO_MUTE;
490    control.value = 1; /* silence */
491
492    /* Errors ignored */
493    ioctl(fd, VIDIOC_S_CTRL, &control);
494
495.. [#f1]
496   The use of ``V4L2_CID_PRIVATE_BASE`` is problematic because different
497   drivers may use the same ``V4L2_CID_PRIVATE_BASE`` ID for different
498   controls. This makes it hard to programmatically set such controls
499   since the meaning of the control with that ID is driver dependent. In
500   order to resolve this drivers use unique IDs and the
501   ``V4L2_CID_PRIVATE_BASE`` IDs are mapped to those unique IDs by the
502   kernel. Consider these ``V4L2_CID_PRIVATE_BASE`` IDs as aliases to
503   the real IDs.
504
505   Many applications today still use the ``V4L2_CID_PRIVATE_BASE`` IDs
506   instead of using :ref:`VIDIOC_QUERYCTRL` with
507   the ``V4L2_CTRL_FLAG_NEXT_CTRL`` flag to enumerate all IDs, so
508   support for ``V4L2_CID_PRIVATE_BASE`` is still around.
509