User Controls

Video for Linux Two API Specification: Revision 0.24
Prev | Chapter 1. Common API Elements | Next

# 1.8. User Controls

Devices typically have a number of user-settable controls such as brightness, saturation and so on, which would be presented to the user on a graphical user interface. But, different devices will have different controls available, and furthermore, the range of possible values, and the default value will vary from device to device. The control ioctls provide the information and a mechanism to create a nice user interface for these controls that will work correctly with any device.

All controls are accessed using an ID value. V4L2 defines several IDs for specific purposes. Drivers can also implement their own custom controls using `V4L2_CID_PRIVATE_BASE` and higher values. The pre-defined control IDs have the prefix `V4L2_CID_`, and are listed in Table 1-1. The ID is used when querying the attributes of a control, and when getting or setting the current value.

Generally applications should present controls to the user without assumptions about their purpose. Each control comes with a name string the user is supposed to understand. When the purpose is non-intuitive the driver writer should provide a user manual, a user interface plug-in or a driver specific panel application. Predefined IDs were introduced to change a few controls programmatically, for example to mute a device during a channel switch.

Drivers may enumerate different controls after switching the current video input or output, tuner or modulator, or audio input or output. Different in the sense of other bounds, another default and current value, step size or other menu items. A control with a certain custom ID can also change name and type.[1] Control values are stored globally, they do not change when switching except to stay within the reported bounds. They also do not change e. g. when the device is opened or closed, when the tuner radio frequency is changed or generally never without application request. Since V4L2 specifies no event mechanism, panel applications intended to cooperate with other panel applications (be they built into a larger application, as a TV viewer) may need to regularly poll control values to update their user interface.[2]

Table 1-1. Control IDs

ID | Type | Description
--- | --- | ---
`V4L2_CID_BASE` |  | First predefined ID, equal to `V4L2_CID_BRIGHTNESS`.
`V4L2_CID_USER_BASE` |  | Synonym of `V4L2_CID_BASE`.
`V4L2_CID_BRIGHTNESS` | integer | Picture brightness, or more precisely, the black level.
`V4L2_CID_CONTRAST` | integer | Picture contrast or luma gain.
`V4L2_CID_SATURATION` | integer | Picture color saturation or chroma gain.
`V4L2_CID_HUE` | integer | Hue or color balance.
`V4L2_CID_AUDIO_VOLUME` | integer | Overall audio volume. Note some drivers also provide an OSS or ALSA mixer interface.
`V4L2_CID_AUDIO_BALANCE` | integer | Audio stereo balance. Minimum corresponds to all the way left, maximum to right.
`V4L2_CID_AUDIO_BASS` | integer | Audio bass adjustment.
`V4L2_CID_AUDIO_TREBLE` | integer | Audio treble adjustment.
`V4L2_CID_AUDIO_MUTE` | boolean | Mute audio, i. e. set the volume to zero, however without affecting `V4L2_CID_AUDIO_VOLUME`. Like ALSA drivers, V4L2 drivers must mute at load time to avoid excessive noise. Actually the entire device should be reset to a low power consumption state.
`V4L2_CID_AUDIO_LOUDNESS` | boolean | Loudness mode (bass boost).
`V4L2_CID_BLACK_LEVEL` | integer | Another name for brightness (not a synonym of `V4L2_CID_BRIGHTNESS`). This control is deprecated and should not be used in new drivers and applications.
`V4L2_CID_AUTO_WHITE_BALANCE` | boolean | Automatic white balance (cameras).
`V4L2_CID_DO_WHITE_BALANCE` | button | This is an action control. When set (the value is ignored), the device will do a white balance and then hold the current setting. Contrast this with the boolean `V4L2_CID_AUTO_WHITE_BALANCE`, which, when activated, keeps adjusting the white balance.
`V4L2_CID_RED_BALANCE` | integer | Red chroma balance.
`V4L2_CID_BLUE_BALANCE` | integer | Blue chroma balance.
`V4L2_CID_GAMMA` | integer | Gamma adjust.
`V4L2_CID_WHITENESS` | integer | Whiteness for grey-scale devices. This is a synonym for `V4L2_CID_GAMMA`. This control is deprecated and should not be used in new drivers and applications.
`V4L2_CID_EXPOSURE` | integer | Exposure (cameras). [Unit?]
`V4L2_CID_AUTOGAIN` | boolean | Automatic gain/exposure control.
`V4L2_CID_GAIN` | integer | Gain control.
`V4L2_CID_HFLIP` | boolean | Mirror the picture horizontally.
`V4L2_CID_VFLIP` | boolean | Mirror the picture vertically.
`V4L2_CID_HCENTER_DEPRECATED` (formerly `V4L2_CID_HCENTER`) | integer | Horizontal image centering. This control is deprecated. New drivers and applications should use the Camera class controls `V4L2_CID_PAN_ABSOLUTE`, `V4L2_CID_PAN_RELATIVE` and `V4L2_CID_PAN_RESET` instead.
`V4L2_CID_VCENTER_DEPRECATED` (formerly `V4L2_CID_VCENTER`) | integer | Vertical image centering. Centering is intended to physically adjust cameras. For image cropping see Section 1.11, for clipping Section 4.2. This control is deprecated. New drivers and applications should use the Camera class controls `V4L2_CID_TILT_ABSOLUTE`, `V4L2_CID_TILT_RELATIVE` and `V4L2_CID_TILT_RESET` instead.
`V4L2_CID_POWER_LINE_FREQUENCY` | integer | Enables a power line frequency filter to avoid flicker. Possible values are: `V4L2_CID_POWER_LINE_FREQUENCY_DISABLED` (0), `V4L2_CID_POWER_LINE_FREQUENCY_50HZ` (1) and `V4L2_CID_POWER_LINE_FREQUENCY_60HZ` (2).
`V4L2_CID_HUE_AUTO` | boolean | Enables automatic hue control by the device. The effect of setting `V4L2_CID_HUE` while automatic hue control is enabled is undefined, drivers should ignore such request.
`V4L2_CID_WHITE_BALANCE_TEMPERATURE` | integer | This control specifies the white balance settings as a color temperature in Kelvin. A driver should have a minimum of 2800 (incandescent) to 6500 (daylight). For more information about color temperature see Wikipedia.
`V4L2_CID_SHARPNESS` | integer | Adjusts the sharpness filters in a camera. The minimum value disables the filters, higher values give a sharper picture.
`V4L2_CID_BACKLIGHT_COMPENSATION` | integer | Adjusts the backlight compensation in a camera. The minimum value disables backlight compensation.
`V4L2_CID_LASTP1` |  | End of the predefined control IDs (currently `V4L2_CID_BACKLIGHT_COMPENSATION` + 1).
`V4L2_CID_PRIVATE_BASE` |  | ID of the first custom (driver specific) control. Applications depending on particular custom controls should check the driver name and version, see Section 1.2.

Applications can enumerate the available controls with the `VIDIOC_QUERYCTRL` and `VIDIOC_QUERYMENU` ioctls, get and set a control value with the `VIDIOC_G_CTRL` and `VIDIOC_S_CTRL` ioctls. Drivers must implement `VIDIOC_QUERYCTRL`, `VIDIOC_G_CTRL` and `VIDIOC_S_CTRL` when the device has one or more controls, `VIDIOC_QUERYMENU` when it has one or more menu type controls.

Example 1-8. Enumerating all controls

    struct v4l2_queryctrl queryctrl;
    struct v4l2_querymenu querymenu;

    static void
    enumerate_menu (void)
    {
            printf ("  Menu items:\n");

            memset (&querymenu, 0, sizeof (querymenu));
            querymenu.id = queryctrl.id;

            for (querymenu.index = queryctrl.minimum;
                 querymenu.index <= queryctrl.maximum;
                  querymenu.index++) {
                    if (0 == ioctl (fd, VIDIOC_QUERYMENU, &querymenu)) {
                            printf ("  %s\n", querymenu.name);
                    } else {
                            perror ("VIDIOC_QUERYMENU");
                            exit (EXIT_FAILURE);
                    }
            }
    }

    memset (&queryctrl, 0, sizeof (queryctrl));

    for (queryctrl.id = V4L2_CID_BASE;
         queryctrl.id < V4L2_CID_LASTP1;
         queryctrl.id++) {
            if (0 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
                    if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
                            continue;

                    printf ("Control %s\n", queryctrl.name);

                    if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
                            enumerate_menu ();
            } else {
                    if (errno == EINVAL)
                            continue;

                    perror ("VIDIOC_QUERYCTRL");
                    exit (EXIT_FAILURE);
            }
    }

    for (queryctrl.id = V4L2_CID_PRIVATE_BASE;;
         queryctrl.id++) {
            if (0 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
                    if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
                            continue;

                    printf ("Control %s\n", queryctrl.name);

                    if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
                            enumerate_menu ();
            } else {
                    if (errno == EINVAL)
                            break;

                    perror ("VIDIOC_QUERYCTRL");
                    exit (EXIT_FAILURE);
            }
    }

Example 1-9. Changing controls

    struct v4l2_queryctrl queryctrl;
    struct v4l2_control control;

    memset (&queryctrl, 0, sizeof (queryctrl));
    queryctrl.id = V4L2_CID_BRIGHTNESS;

    if (-1 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
            if (errno != EINVAL) {
                    perror ("VIDIOC_QUERYCTRL");
                    exit (EXIT_FAILURE);
            } else {
                    printf ("V4L2_CID_BRIGHTNESS is not supported\n");
            }
    } else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
            printf ("V4L2_CID_BRIGHTNESS is not supported\n");
    } else {
            memset (&control, 0, sizeof (control));
            control.id = V4L2_CID_BRIGHTNESS;
            control.value = queryctrl.default_value;

            if (-1 == ioctl (fd, VIDIOC_S_CTRL, &control)) {
                    perror ("VIDIOC_S_CTRL");
                    exit (EXIT_FAILURE);
            }
    }

    memset (&control, 0, sizeof (control));
    control.id = V4L2_CID_CONTRAST;

    if (0 == ioctl (fd, VIDIOC_G_CTRL, &control)) {
            control.value += 1;

            /* The driver may clamp the value or return ERANGE, ignored here */

            if (-1 == ioctl (fd, VIDIOC_S_CTRL, &control)
                && errno != ERANGE) {
                    perror ("VIDIOC_S_CTRL");
                    exit (EXIT_FAILURE);
            }
    /* Ignore if V4L2_CID_CONTRAST is unsupported */
    } else if (errno != EINVAL) {
            perror ("VIDIOC_G_CTRL");
            exit (EXIT_FAILURE);
    }

    control.id = V4L2_CID_AUDIO_MUTE;
    control.value = TRUE; /* silence */

    /* Errors ignored */
    ioctl (fd, VIDIOC_S_CTRL, &control);

### Notes

[1] | It will be more convenient for applications if drivers make use of the `V4L2_CTRL_FLAG_DISABLED` flag, but that was never required.
[2] | Applications could call an ioctl to request events. After another process called `VIDIOC_S_CTRL` or another ioctl changing shared properties the `select()` function would indicate readability until any ioctl (querying the properties) is called.

Prev | Home | Next
Video Standards | Up | Extended Controls