18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci.. _standard:
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci***************
68c2ecf20Sopenharmony_ciVideo Standards
78c2ecf20Sopenharmony_ci***************
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciVideo devices typically support one or more different video standards or
108c2ecf20Sopenharmony_civariations of standards. Each video input and output may support another
118c2ecf20Sopenharmony_ciset of standards. This set is reported by the ``std`` field of struct
128c2ecf20Sopenharmony_ci:c:type:`v4l2_input` and struct
138c2ecf20Sopenharmony_ci:c:type:`v4l2_output` returned by the
148c2ecf20Sopenharmony_ci:ref:`VIDIOC_ENUMINPUT` and
158c2ecf20Sopenharmony_ci:ref:`VIDIOC_ENUMOUTPUT` ioctls, respectively.
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciV4L2 defines one bit for each analog video standard currently in use
188c2ecf20Sopenharmony_ciworldwide, and sets aside bits for driver defined standards, e. g.
198c2ecf20Sopenharmony_cihybrid standards to watch NTSC video tapes on PAL TVs and vice versa.
208c2ecf20Sopenharmony_ciApplications can use the predefined bits to select a particular
218c2ecf20Sopenharmony_cistandard, although presenting the user a menu of supported standards is
228c2ecf20Sopenharmony_cipreferred. To enumerate and query the attributes of the supported
238c2ecf20Sopenharmony_cistandards applications use the :ref:`VIDIOC_ENUMSTD`
248c2ecf20Sopenharmony_ciioctl.
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciMany of the defined standards are actually just variations of a few
278c2ecf20Sopenharmony_cimajor standards. The hardware may in fact not distinguish between them,
288c2ecf20Sopenharmony_cior do so internal and switch automatically. Therefore enumerated
298c2ecf20Sopenharmony_cistandards also contain sets of one or more standard bits.
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ciAssume a hypothetic tuner capable of demodulating B/PAL, G/PAL and I/PAL
328c2ecf20Sopenharmony_cisignals. The first enumerated standard is a set of B and G/PAL, switched
338c2ecf20Sopenharmony_ciautomatically depending on the selected radio frequency in UHF or VHF
348c2ecf20Sopenharmony_ciband. Enumeration gives a "PAL-B/G" or "PAL-I" choice. Similar a
358c2ecf20Sopenharmony_ciComposite input may collapse standards, enumerating "PAL-B/G/H/I",
368c2ecf20Sopenharmony_ci"NTSC-M" and "SECAM-D/K". [#f1]_
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciTo query and select the standard used by the current video input or
398c2ecf20Sopenharmony_cioutput applications call the :ref:`VIDIOC_G_STD <VIDIOC_G_STD>` and
408c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_STD <VIDIOC_G_STD>` ioctl, respectively. The
418c2ecf20Sopenharmony_ci*received* standard can be sensed with the
428c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYSTD` ioctl.
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci.. note::
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci   The parameter of all these ioctls is a pointer to a
478c2ecf20Sopenharmony_ci   :ref:`v4l2_std_id <v4l2-std-id>` type (a standard set), *not* an
488c2ecf20Sopenharmony_ci   index into the standard enumeration. Drivers must implement all video
498c2ecf20Sopenharmony_ci   standard ioctls when the device has one or more video inputs or outputs.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciSpecial rules apply to devices such as USB cameras where the notion of
528c2ecf20Sopenharmony_civideo standards makes little sense. More generally for any capture or
538c2ecf20Sopenharmony_cioutput device which is:
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci-  incapable of capturing fields or frames at the nominal rate of the
568c2ecf20Sopenharmony_ci   video standard, or
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci-  that does not support the video standard formats at all.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciHere the driver shall set the ``std`` field of struct
618c2ecf20Sopenharmony_ci:c:type:`v4l2_input` and struct
628c2ecf20Sopenharmony_ci:c:type:`v4l2_output` to zero and the :ref:`VIDIOC_G_STD <VIDIOC_G_STD>`,
638c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_STD <VIDIOC_G_STD>`, :ref:`VIDIOC_QUERYSTD` and :ref:`VIDIOC_ENUMSTD` ioctls
648c2ecf20Sopenharmony_cishall return the ``ENOTTY`` error code or the ``EINVAL`` error code.
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciApplications can make use of the :ref:`input-capabilities` and
678c2ecf20Sopenharmony_ci:ref:`output-capabilities` flags to determine whether the video
688c2ecf20Sopenharmony_cistandard ioctls can be used with the given input or output.
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciExample: Information about the current video standard
718c2ecf20Sopenharmony_ci=====================================================
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci.. code-block:: c
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci    v4l2_std_id std_id;
768c2ecf20Sopenharmony_ci    struct v4l2_standard standard;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_STD, &std_id)) {
798c2ecf20Sopenharmony_ci	/* Note when VIDIOC_ENUMSTD always returns ENOTTY this
808c2ecf20Sopenharmony_ci	   is no video device or it falls under the USB exception,
818c2ecf20Sopenharmony_ci	   and VIDIOC_G_STD returning ENOTTY is no error. */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	perror("VIDIOC_G_STD");
848c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
858c2ecf20Sopenharmony_ci    }
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci    memset(&standard, 0, sizeof(standard));
888c2ecf20Sopenharmony_ci    standard.index = 0;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci    while (0 == ioctl(fd, VIDIOC_ENUMSTD, &standard)) {
918c2ecf20Sopenharmony_ci	if (standard.id & std_id) {
928c2ecf20Sopenharmony_ci	       printf("Current video standard: %s\\n", standard.name);
938c2ecf20Sopenharmony_ci	       exit(EXIT_SUCCESS);
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	standard.index++;
978c2ecf20Sopenharmony_ci    }
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci    /* EINVAL indicates the end of the enumeration, which cannot be
1008c2ecf20Sopenharmony_ci       empty unless this device falls under the USB exception. */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci    if (errno == EINVAL || standard.index == 0) {
1038c2ecf20Sopenharmony_ci	perror("VIDIOC_ENUMSTD");
1048c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1058c2ecf20Sopenharmony_ci    }
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciExample: Listing the video standards supported by the current input
1088c2ecf20Sopenharmony_ci===================================================================
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci.. code-block:: c
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci    struct v4l2_input input;
1138c2ecf20Sopenharmony_ci    struct v4l2_standard standard;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci    memset(&input, 0, sizeof(input));
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_INPUT, &input.index)) {
1188c2ecf20Sopenharmony_ci	perror("VIDIOC_G_INPUT");
1198c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1208c2ecf20Sopenharmony_ci    }
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_ENUMINPUT, &input)) {
1238c2ecf20Sopenharmony_ci	perror("VIDIOC_ENUM_INPUT");
1248c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1258c2ecf20Sopenharmony_ci    }
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci    printf("Current input %s supports:\\n", input.name);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci    memset(&standard, 0, sizeof(standard));
1308c2ecf20Sopenharmony_ci    standard.index = 0;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci    while (0 == ioctl(fd, VIDIOC_ENUMSTD, &standard)) {
1338c2ecf20Sopenharmony_ci	if (standard.id & input.std)
1348c2ecf20Sopenharmony_ci	    printf("%s\\n", standard.name);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	standard.index++;
1378c2ecf20Sopenharmony_ci    }
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci    /* EINVAL indicates the end of the enumeration, which cannot be
1408c2ecf20Sopenharmony_ci       empty unless this device falls under the USB exception. */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci    if (errno != EINVAL || standard.index == 0) {
1438c2ecf20Sopenharmony_ci	perror("VIDIOC_ENUMSTD");
1448c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1458c2ecf20Sopenharmony_ci    }
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ciExample: Selecting a new video standard
1488c2ecf20Sopenharmony_ci=======================================
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci.. code-block:: c
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci    struct v4l2_input input;
1538c2ecf20Sopenharmony_ci    v4l2_std_id std_id;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci    memset(&input, 0, sizeof(input));
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_INPUT, &input.index)) {
1588c2ecf20Sopenharmony_ci	perror("VIDIOC_G_INPUT");
1598c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1608c2ecf20Sopenharmony_ci    }
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_ENUMINPUT, &input)) {
1638c2ecf20Sopenharmony_ci	perror("VIDIOC_ENUM_INPUT");
1648c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1658c2ecf20Sopenharmony_ci    }
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci    if (0 == (input.std & V4L2_STD_PAL_BG)) {
1688c2ecf20Sopenharmony_ci	fprintf(stderr, "Oops. B/G PAL is not supported.\\n");
1698c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1708c2ecf20Sopenharmony_ci    }
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci    /* Note this is also supposed to work when only B
1738c2ecf20Sopenharmony_ci       or G/PAL is supported. */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci    std_id = V4L2_STD_PAL_BG;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_S_STD, &std_id)) {
1788c2ecf20Sopenharmony_ci	perror("VIDIOC_S_STD");
1798c2ecf20Sopenharmony_ci	exit(EXIT_FAILURE);
1808c2ecf20Sopenharmony_ci    }
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci.. [#f1]
1838c2ecf20Sopenharmony_ci   Some users are already confused by technical terms PAL, NTSC and
1848c2ecf20Sopenharmony_ci   SECAM. There is no point asking them to distinguish between B, G, D,
1858c2ecf20Sopenharmony_ci   or K when the software or hardware can do that automatically.
186