162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci.. _standard:
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci***************
662306a36Sopenharmony_ciVideo Standards
762306a36Sopenharmony_ci***************
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciVideo devices typically support one or more different video standards or
1062306a36Sopenharmony_civariations of standards. Each video input and output may support another
1162306a36Sopenharmony_ciset of standards. This set is reported by the ``std`` field of struct
1262306a36Sopenharmony_ci:c:type:`v4l2_input` and struct
1362306a36Sopenharmony_ci:c:type:`v4l2_output` returned by the
1462306a36Sopenharmony_ci:ref:`VIDIOC_ENUMINPUT` and
1562306a36Sopenharmony_ci:ref:`VIDIOC_ENUMOUTPUT` ioctls, respectively.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciV4L2 defines one bit for each analog video standard currently in use
1862306a36Sopenharmony_ciworldwide, and sets aside bits for driver defined standards, e. g.
1962306a36Sopenharmony_cihybrid standards to watch NTSC video tapes on PAL TVs and vice versa.
2062306a36Sopenharmony_ciApplications can use the predefined bits to select a particular
2162306a36Sopenharmony_cistandard, although presenting the user a menu of supported standards is
2262306a36Sopenharmony_cipreferred. To enumerate and query the attributes of the supported
2362306a36Sopenharmony_cistandards applications use the :ref:`VIDIOC_ENUMSTD`
2462306a36Sopenharmony_ciioctl.
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciMany of the defined standards are actually just variations of a few
2762306a36Sopenharmony_cimajor standards. The hardware may in fact not distinguish between them,
2862306a36Sopenharmony_cior do so internal and switch automatically. Therefore enumerated
2962306a36Sopenharmony_cistandards also contain sets of one or more standard bits.
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciAssume a hypothetic tuner capable of demodulating B/PAL, G/PAL and I/PAL
3262306a36Sopenharmony_cisignals. The first enumerated standard is a set of B and G/PAL, switched
3362306a36Sopenharmony_ciautomatically depending on the selected radio frequency in UHF or VHF
3462306a36Sopenharmony_ciband. Enumeration gives a "PAL-B/G" or "PAL-I" choice. Similar a
3562306a36Sopenharmony_ciComposite input may collapse standards, enumerating "PAL-B/G/H/I",
3662306a36Sopenharmony_ci"NTSC-M" and "SECAM-D/K". [#f1]_
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciTo query and select the standard used by the current video input or
3962306a36Sopenharmony_cioutput applications call the :ref:`VIDIOC_G_STD <VIDIOC_G_STD>` and
4062306a36Sopenharmony_ci:ref:`VIDIOC_S_STD <VIDIOC_G_STD>` ioctl, respectively. The
4162306a36Sopenharmony_ci*received* standard can be sensed with the
4262306a36Sopenharmony_ci:ref:`VIDIOC_QUERYSTD` ioctl.
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci.. note::
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci   The parameter of all these ioctls is a pointer to a
4762306a36Sopenharmony_ci   :ref:`v4l2_std_id <v4l2-std-id>` type (a standard set), *not* an
4862306a36Sopenharmony_ci   index into the standard enumeration. Drivers must implement all video
4962306a36Sopenharmony_ci   standard ioctls when the device has one or more video inputs or outputs.
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciSpecial rules apply to devices such as USB cameras where the notion of
5262306a36Sopenharmony_civideo standards makes little sense. More generally for any capture or
5362306a36Sopenharmony_cioutput device which is:
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci-  incapable of capturing fields or frames at the nominal rate of the
5662306a36Sopenharmony_ci   video standard, or
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci-  that does not support the video standard formats at all.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciHere the driver shall set the ``std`` field of struct
6162306a36Sopenharmony_ci:c:type:`v4l2_input` and struct
6262306a36Sopenharmony_ci:c:type:`v4l2_output` to zero and the :ref:`VIDIOC_G_STD <VIDIOC_G_STD>`,
6362306a36Sopenharmony_ci:ref:`VIDIOC_S_STD <VIDIOC_G_STD>`, :ref:`VIDIOC_QUERYSTD` and :ref:`VIDIOC_ENUMSTD` ioctls
6462306a36Sopenharmony_cishall return the ``ENOTTY`` error code or the ``EINVAL`` error code.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciApplications can make use of the :ref:`input-capabilities` and
6762306a36Sopenharmony_ci:ref:`output-capabilities` flags to determine whether the video
6862306a36Sopenharmony_cistandard ioctls can be used with the given input or output.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciExample: Information about the current video standard
7162306a36Sopenharmony_ci=====================================================
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci.. code-block:: c
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci    v4l2_std_id std_id;
7662306a36Sopenharmony_ci    struct v4l2_standard standard;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_STD, &std_id)) {
7962306a36Sopenharmony_ci	/* Note when VIDIOC_ENUMSTD always returns ENOTTY this
8062306a36Sopenharmony_ci	   is no video device or it falls under the USB exception,
8162306a36Sopenharmony_ci	   and VIDIOC_G_STD returning ENOTTY is no error. */
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	perror("VIDIOC_G_STD");
8462306a36Sopenharmony_ci	exit(EXIT_FAILURE);
8562306a36Sopenharmony_ci    }
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci    memset(&standard, 0, sizeof(standard));
8862306a36Sopenharmony_ci    standard.index = 0;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci    while (0 == ioctl(fd, VIDIOC_ENUMSTD, &standard)) {
9162306a36Sopenharmony_ci	if (standard.id & std_id) {
9262306a36Sopenharmony_ci	       printf("Current video standard: %s\\n", standard.name);
9362306a36Sopenharmony_ci	       exit(EXIT_SUCCESS);
9462306a36Sopenharmony_ci	}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	standard.index++;
9762306a36Sopenharmony_ci    }
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci    /* EINVAL indicates the end of the enumeration, which cannot be
10062306a36Sopenharmony_ci       empty unless this device falls under the USB exception. */
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci    if (errno == EINVAL || standard.index == 0) {
10362306a36Sopenharmony_ci	perror("VIDIOC_ENUMSTD");
10462306a36Sopenharmony_ci	exit(EXIT_FAILURE);
10562306a36Sopenharmony_ci    }
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciExample: Listing the video standards supported by the current input
10862306a36Sopenharmony_ci===================================================================
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci.. code-block:: c
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci    struct v4l2_input input;
11362306a36Sopenharmony_ci    struct v4l2_standard standard;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci    memset(&input, 0, sizeof(input));
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_INPUT, &input.index)) {
11862306a36Sopenharmony_ci	perror("VIDIOC_G_INPUT");
11962306a36Sopenharmony_ci	exit(EXIT_FAILURE);
12062306a36Sopenharmony_ci    }
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_ENUMINPUT, &input)) {
12362306a36Sopenharmony_ci	perror("VIDIOC_ENUM_INPUT");
12462306a36Sopenharmony_ci	exit(EXIT_FAILURE);
12562306a36Sopenharmony_ci    }
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci    printf("Current input %s supports:\\n", input.name);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci    memset(&standard, 0, sizeof(standard));
13062306a36Sopenharmony_ci    standard.index = 0;
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci    while (0 == ioctl(fd, VIDIOC_ENUMSTD, &standard)) {
13362306a36Sopenharmony_ci	if (standard.id & input.std)
13462306a36Sopenharmony_ci	    printf("%s\\n", standard.name);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	standard.index++;
13762306a36Sopenharmony_ci    }
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci    /* EINVAL indicates the end of the enumeration, which cannot be
14062306a36Sopenharmony_ci       empty unless this device falls under the USB exception. */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci    if (errno != EINVAL || standard.index == 0) {
14362306a36Sopenharmony_ci	perror("VIDIOC_ENUMSTD");
14462306a36Sopenharmony_ci	exit(EXIT_FAILURE);
14562306a36Sopenharmony_ci    }
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ciExample: Selecting a new video standard
14862306a36Sopenharmony_ci=======================================
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci.. code-block:: c
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci    struct v4l2_input input;
15362306a36Sopenharmony_ci    v4l2_std_id std_id;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci    memset(&input, 0, sizeof(input));
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_G_INPUT, &input.index)) {
15862306a36Sopenharmony_ci	perror("VIDIOC_G_INPUT");
15962306a36Sopenharmony_ci	exit(EXIT_FAILURE);
16062306a36Sopenharmony_ci    }
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_ENUMINPUT, &input)) {
16362306a36Sopenharmony_ci	perror("VIDIOC_ENUM_INPUT");
16462306a36Sopenharmony_ci	exit(EXIT_FAILURE);
16562306a36Sopenharmony_ci    }
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci    if (0 == (input.std & V4L2_STD_PAL_BG)) {
16862306a36Sopenharmony_ci	fprintf(stderr, "Oops. B/G PAL is not supported.\\n");
16962306a36Sopenharmony_ci	exit(EXIT_FAILURE);
17062306a36Sopenharmony_ci    }
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci    /* Note this is also supposed to work when only B
17362306a36Sopenharmony_ci       or G/PAL is supported. */
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci    std_id = V4L2_STD_PAL_BG;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci    if (-1 == ioctl(fd, VIDIOC_S_STD, &std_id)) {
17862306a36Sopenharmony_ci	perror("VIDIOC_S_STD");
17962306a36Sopenharmony_ci	exit(EXIT_FAILURE);
18062306a36Sopenharmony_ci    }
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci.. [#f1]
18362306a36Sopenharmony_ci   Some users are already confused by technical terms PAL, NTSC and
18462306a36Sopenharmony_ci   SECAM. There is no point asking them to distinguish between B, G, D,
18562306a36Sopenharmony_ci   or K when the software or hardware can do that automatically.
186