control.rst 17 KB

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