162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _format:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci************
762306a36Sopenharmony_ciData Formats
862306a36Sopenharmony_ci************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciData Format Negotiation
1162306a36Sopenharmony_ci=======================
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciDifferent devices exchange different kinds of data with applications,
1462306a36Sopenharmony_cifor example video images, raw or sliced VBI data, RDS datagrams. Even
1562306a36Sopenharmony_ciwithin one kind many different formats are possible, in particular there is an
1662306a36Sopenharmony_ciabundance of image formats. Although drivers must provide a default and
1762306a36Sopenharmony_cithe selection persists across closing and reopening a device,
1862306a36Sopenharmony_ciapplications should always negotiate a data format before engaging in
1962306a36Sopenharmony_cidata exchange. Negotiation means the application asks for a particular
2062306a36Sopenharmony_ciformat and the driver selects and reports the best the hardware can do
2162306a36Sopenharmony_cito satisfy the request. Of course applications can also just query the
2262306a36Sopenharmony_cicurrent selection.
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciA single mechanism exists to negotiate all data formats using the
2562306a36Sopenharmony_ciaggregate struct :c:type:`v4l2_format` and the
2662306a36Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and
2762306a36Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctls. Additionally the
2862306a36Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` ioctl can be used to examine
2962306a36Sopenharmony_ciwhat the hardware *could* do, without actually selecting a new data
3062306a36Sopenharmony_ciformat. The data formats supported by the V4L2 API are covered in the
3162306a36Sopenharmony_cirespective device section in :ref:`devices`. For a closer look at
3262306a36Sopenharmony_ciimage formats see :ref:`pixfmt`.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciThe :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl is a major turning-point in the
3562306a36Sopenharmony_ciinitialization sequence. Prior to this point multiple panel applications
3662306a36Sopenharmony_cican access the same device concurrently to select the current input,
3762306a36Sopenharmony_cichange controls or modify other properties. The first :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
3862306a36Sopenharmony_ciassigns a logical stream (video data, VBI data etc.) exclusively to one
3962306a36Sopenharmony_cifile descriptor.
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciExclusive means no other application, more precisely no other file
4262306a36Sopenharmony_cidescriptor, can grab this stream or change device properties
4362306a36Sopenharmony_ciinconsistent with the negotiated parameters. A video standard change for
4462306a36Sopenharmony_ciexample, when the new standard uses a different number of scan lines,
4562306a36Sopenharmony_cican invalidate the selected image format. Therefore only the file
4662306a36Sopenharmony_cidescriptor owning the stream can make invalidating changes. Accordingly
4762306a36Sopenharmony_cimultiple file descriptors which grabbed different logical streams
4862306a36Sopenharmony_ciprevent each other from interfering with their settings. When for
4962306a36Sopenharmony_ciexample video overlay is about to start or already in progress,
5062306a36Sopenharmony_cisimultaneous video capturing may be restricted to the same cropping and
5162306a36Sopenharmony_ciimage size.
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciWhen applications omit the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl its locking side
5462306a36Sopenharmony_cieffects are implied by the next step, the selection of an I/O method
5562306a36Sopenharmony_ciwith the :ref:`VIDIOC_REQBUFS` ioctl or implicit
5662306a36Sopenharmony_ciwith the first :c:func:`read()` or
5762306a36Sopenharmony_ci:c:func:`write()` call.
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciGenerally only one logical stream can be assigned to a file descriptor,
6062306a36Sopenharmony_cithe exception being drivers permitting simultaneous video capturing and
6162306a36Sopenharmony_cioverlay using the same file descriptor for compatibility with V4L and
6262306a36Sopenharmony_ciearlier versions of V4L2. Switching the logical stream or returning into
6362306a36Sopenharmony_ci"panel mode" is possible by closing and reopening the device. Drivers
6462306a36Sopenharmony_ci*may* support a switch using :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciAll drivers exchanging data with applications must support the
6762306a36Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl. Implementation of the
6862306a36Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` is highly recommended but optional.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciImage Format Enumeration
7162306a36Sopenharmony_ci========================
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciApart of the generic format negotiation functions a special ioctl to
7462306a36Sopenharmony_cienumerate all image formats supported by video capture, overlay or
7562306a36Sopenharmony_cioutput devices is available. [#f1]_
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciThe :ref:`VIDIOC_ENUM_FMT` ioctl must be supported
7862306a36Sopenharmony_ciby all drivers exchanging image data with applications.
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci.. important::
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci    Drivers are not supposed to convert image formats in kernel space.
8362306a36Sopenharmony_ci    They must enumerate only formats directly supported by the hardware.
8462306a36Sopenharmony_ci    If necessary driver writers should publish an example conversion
8562306a36Sopenharmony_ci    routine or library for integration into applications.
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci.. [#f1]
8862306a36Sopenharmony_ci   Enumerating formats an application has no a-priori knowledge of
8962306a36Sopenharmony_ci   (otherwise it could explicitly ask for them and need not enumerate)
9062306a36Sopenharmony_ci   seems useless, but there are applications serving as proxy between
9162306a36Sopenharmony_ci   drivers and the actual video applications for which this is useful.
92