18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _format:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci************
78c2ecf20Sopenharmony_ciData Formats
88c2ecf20Sopenharmony_ci************
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciData Format Negotiation
118c2ecf20Sopenharmony_ci=======================
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciDifferent devices exchange different kinds of data with applications,
148c2ecf20Sopenharmony_cifor example video images, raw or sliced VBI data, RDS datagrams. Even
158c2ecf20Sopenharmony_ciwithin one kind many different formats are possible, in particular there is an
168c2ecf20Sopenharmony_ciabundance of image formats. Although drivers must provide a default and
178c2ecf20Sopenharmony_cithe selection persists across closing and reopening a device,
188c2ecf20Sopenharmony_ciapplications should always negotiate a data format before engaging in
198c2ecf20Sopenharmony_cidata exchange. Negotiation means the application asks for a particular
208c2ecf20Sopenharmony_ciformat and the driver selects and reports the best the hardware can do
218c2ecf20Sopenharmony_cito satisfy the request. Of course applications can also just query the
228c2ecf20Sopenharmony_cicurrent selection.
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciA single mechanism exists to negotiate all data formats using the
258c2ecf20Sopenharmony_ciaggregate struct :c:type:`v4l2_format` and the
268c2ecf20Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and
278c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctls. Additionally the
288c2ecf20Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` ioctl can be used to examine
298c2ecf20Sopenharmony_ciwhat the hardware *could* do, without actually selecting a new data
308c2ecf20Sopenharmony_ciformat. The data formats supported by the V4L2 API are covered in the
318c2ecf20Sopenharmony_cirespective device section in :ref:`devices`. For a closer look at
328c2ecf20Sopenharmony_ciimage formats see :ref:`pixfmt`.
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciThe :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl is a major turning-point in the
358c2ecf20Sopenharmony_ciinitialization sequence. Prior to this point multiple panel applications
368c2ecf20Sopenharmony_cican access the same device concurrently to select the current input,
378c2ecf20Sopenharmony_cichange controls or modify other properties. The first :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
388c2ecf20Sopenharmony_ciassigns a logical stream (video data, VBI data etc.) exclusively to one
398c2ecf20Sopenharmony_cifile descriptor.
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciExclusive means no other application, more precisely no other file
428c2ecf20Sopenharmony_cidescriptor, can grab this stream or change device properties
438c2ecf20Sopenharmony_ciinconsistent with the negotiated parameters. A video standard change for
448c2ecf20Sopenharmony_ciexample, when the new standard uses a different number of scan lines,
458c2ecf20Sopenharmony_cican invalidate the selected image format. Therefore only the file
468c2ecf20Sopenharmony_cidescriptor owning the stream can make invalidating changes. Accordingly
478c2ecf20Sopenharmony_cimultiple file descriptors which grabbed different logical streams
488c2ecf20Sopenharmony_ciprevent each other from interfering with their settings. When for
498c2ecf20Sopenharmony_ciexample video overlay is about to start or already in progress,
508c2ecf20Sopenharmony_cisimultaneous video capturing may be restricted to the same cropping and
518c2ecf20Sopenharmony_ciimage size.
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciWhen applications omit the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl its locking side
548c2ecf20Sopenharmony_cieffects are implied by the next step, the selection of an I/O method
558c2ecf20Sopenharmony_ciwith the :ref:`VIDIOC_REQBUFS` ioctl or implicit
568c2ecf20Sopenharmony_ciwith the first :c:func:`read()` or
578c2ecf20Sopenharmony_ci:c:func:`write()` call.
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciGenerally only one logical stream can be assigned to a file descriptor,
608c2ecf20Sopenharmony_cithe exception being drivers permitting simultaneous video capturing and
618c2ecf20Sopenharmony_cioverlay using the same file descriptor for compatibility with V4L and
628c2ecf20Sopenharmony_ciearlier versions of V4L2. Switching the logical stream or returning into
638c2ecf20Sopenharmony_ci"panel mode" is possible by closing and reopening the device. Drivers
648c2ecf20Sopenharmony_ci*may* support a switch using :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`.
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciAll drivers exchanging data with applications must support the
678c2ecf20Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl. Implementation of the
688c2ecf20Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` is highly recommended but optional.
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciImage Format Enumeration
718c2ecf20Sopenharmony_ci========================
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciApart of the generic format negotiation functions a special ioctl to
748c2ecf20Sopenharmony_cienumerate all image formats supported by video capture, overlay or
758c2ecf20Sopenharmony_cioutput devices is available. [#f1]_
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciThe :ref:`VIDIOC_ENUM_FMT` ioctl must be supported
788c2ecf20Sopenharmony_ciby all drivers exchanging image data with applications.
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci.. important::
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci    Drivers are not supposed to convert image formats in kernel space.
838c2ecf20Sopenharmony_ci    They must enumerate only formats directly supported by the hardware.
848c2ecf20Sopenharmony_ci    If necessary driver writers should publish an example conversion
858c2ecf20Sopenharmony_ci    routine or library for integration into applications.
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci.. [#f1]
888c2ecf20Sopenharmony_ci   Enumerating formats an application has no a-priori knowledge of
898c2ecf20Sopenharmony_ci   (otherwise it could explicitly ask for them and need not enumerate)
908c2ecf20Sopenharmony_ci   seems useless, but there are applications serving as proxy between
918c2ecf20Sopenharmony_ci   drivers and the actual video applications for which this is useful.
92