18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
28c2ecf20Sopenharmony_ci.. c:namespace:: V4L
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci.. _raw-vbi:
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci**********************
78c2ecf20Sopenharmony_ciRaw VBI Data Interface
88c2ecf20Sopenharmony_ci**********************
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciVBI is an abbreviation of Vertical Blanking Interval, a gap in the
118c2ecf20Sopenharmony_cisequence of lines of an analog video signal. During VBI no picture
128c2ecf20Sopenharmony_ciinformation is transmitted, allowing some time while the electron beam
138c2ecf20Sopenharmony_ciof a cathode ray tube TV returns to the top of the screen. Using an
148c2ecf20Sopenharmony_cioscilloscope you will find here the vertical synchronization pulses and
158c2ecf20Sopenharmony_cishort data packages ASK modulated [#f1]_ onto the video signal. These are
168c2ecf20Sopenharmony_citransmissions of services such as Teletext or Closed Caption.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciSubject of this interface type is raw VBI data, as sampled off a video
198c2ecf20Sopenharmony_cisignal, or to be added to a signal for output. The data format is
208c2ecf20Sopenharmony_cisimilar to uncompressed video images, a number of lines times a number
218c2ecf20Sopenharmony_ciof samples per line, we call this a VBI image.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciConventionally V4L2 VBI devices are accessed through character device
248c2ecf20Sopenharmony_cispecial files named ``/dev/vbi`` and ``/dev/vbi0`` to ``/dev/vbi31``
258c2ecf20Sopenharmony_ciwith major number 81 and minor numbers 224 to 255. ``/dev/vbi`` is
268c2ecf20Sopenharmony_citypically a symbolic link to the preferred VBI device. This convention
278c2ecf20Sopenharmony_ciapplies to both input and output devices.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciTo address the problems of finding related video and VBI devices VBI
308c2ecf20Sopenharmony_cicapturing and output is also available as device function under
318c2ecf20Sopenharmony_ci``/dev/video``. To capture or output raw VBI data with these devices
328c2ecf20Sopenharmony_ciapplications must call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl.
338c2ecf20Sopenharmony_ciAccessed as ``/dev/vbi``, raw VBI capturing or output is the default
348c2ecf20Sopenharmony_cidevice function.
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciQuerying Capabilities
378c2ecf20Sopenharmony_ci=====================
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciDevices supporting the raw VBI capturing or output API set the
408c2ecf20Sopenharmony_ci``V4L2_CAP_VBI_CAPTURE`` or ``V4L2_CAP_VBI_OUTPUT`` flags, respectively,
418c2ecf20Sopenharmony_ciin the ``capabilities`` field of struct
428c2ecf20Sopenharmony_ci:c:type:`v4l2_capability` returned by the
438c2ecf20Sopenharmony_ci:ref:`VIDIOC_QUERYCAP` ioctl. At least one of the
448c2ecf20Sopenharmony_ciread/write, streaming or asynchronous I/O methods must be supported. VBI
458c2ecf20Sopenharmony_cidevices may or may not have a tuner or modulator.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciSupplemental Functions
488c2ecf20Sopenharmony_ci======================
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciVBI devices shall support :ref:`video input or output <video>`,
518c2ecf20Sopenharmony_ci:ref:`tuner or modulator <tuner>`, and :ref:`controls <control>`
528c2ecf20Sopenharmony_ciioctls as needed. The :ref:`video standard <standard>` ioctls provide
538c2ecf20Sopenharmony_ciinformation vital to program a VBI device, therefore must be supported.
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciRaw VBI Format Negotiation
568c2ecf20Sopenharmony_ci==========================
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ciRaw VBI sampling abilities can vary, in particular the sampling
598c2ecf20Sopenharmony_cifrequency. To properly interpret the data V4L2 specifies an ioctl to
608c2ecf20Sopenharmony_ciquery the sampling parameters. Moreover, to allow for some flexibility
618c2ecf20Sopenharmony_ciapplications can also suggest different parameters.
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciAs usual these parameters are *not* reset at :c:func:`open()`
648c2ecf20Sopenharmony_citime to permit Unix tool chains, programming a device and then reading
658c2ecf20Sopenharmony_cifrom it as if it was a plain file. Well written V4L2 applications should
668c2ecf20Sopenharmony_cialways ensure they really get what they want, requesting reasonable
678c2ecf20Sopenharmony_ciparameters and then checking if the actual parameters are suitable.
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ciTo query the current raw VBI capture parameters applications set the
708c2ecf20Sopenharmony_ci``type`` field of a struct :c:type:`v4l2_format` to
718c2ecf20Sopenharmony_ci``V4L2_BUF_TYPE_VBI_CAPTURE`` or ``V4L2_BUF_TYPE_VBI_OUTPUT``, and call
728c2ecf20Sopenharmony_cithe :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` ioctl with a pointer to this
738c2ecf20Sopenharmony_cistructure. Drivers fill the struct
748c2ecf20Sopenharmony_ci:c:type:`v4l2_vbi_format` ``vbi`` member of the
758c2ecf20Sopenharmony_ci``fmt`` union.
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciTo request different parameters applications set the ``type`` field of a
788c2ecf20Sopenharmony_cistruct :c:type:`v4l2_format` as above and initialize all
798c2ecf20Sopenharmony_cifields of the struct :c:type:`v4l2_vbi_format`
808c2ecf20Sopenharmony_ci``vbi`` member of the ``fmt`` union, or better just modify the results
818c2ecf20Sopenharmony_ciof :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`, and call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
828c2ecf20Sopenharmony_ciioctl with a pointer to this structure. Drivers return an ``EINVAL`` error
838c2ecf20Sopenharmony_cicode only when the given parameters are ambiguous, otherwise they modify
848c2ecf20Sopenharmony_cithe parameters according to the hardware capabilities and return the
858c2ecf20Sopenharmony_ciactual parameters. When the driver allocates resources at this point, it
868c2ecf20Sopenharmony_cimay return an ``EBUSY`` error code to indicate the returned parameters are
878c2ecf20Sopenharmony_civalid but the required resources are currently not available. That may
888c2ecf20Sopenharmony_cihappen for instance when the video and VBI areas to capture would
898c2ecf20Sopenharmony_cioverlap, or when the driver supports multiple opens and another process
908c2ecf20Sopenharmony_cialready requested VBI capturing or output. Anyway, applications must
918c2ecf20Sopenharmony_ciexpect other resource allocation points which may return ``EBUSY``, at the
928c2ecf20Sopenharmony_ci:ref:`VIDIOC_STREAMON` ioctl and the first :c:func:`read()`
938c2ecf20Sopenharmony_ci, :c:func:`write()` and :c:func:`select()` calls.
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciVBI devices must implement both the :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and
968c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl, even if :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ignores all requests
978c2ecf20Sopenharmony_ciand always returns default parameters as :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` does.
988c2ecf20Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` is optional.
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci.. tabularcolumns:: |p{1.6cm}|p{4.2cm}|p{11.7cm}|
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci.. c:type:: v4l2_vbi_format
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci.. cssclass:: longtable
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci.. flat-table:: struct v4l2_vbi_format
1078c2ecf20Sopenharmony_ci    :header-rows:  0
1088c2ecf20Sopenharmony_ci    :stub-columns: 0
1098c2ecf20Sopenharmony_ci    :widths:       1 1 2
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci    * - __u32
1128c2ecf20Sopenharmony_ci      - ``sampling_rate``
1138c2ecf20Sopenharmony_ci      - Samples per second, i. e. unit 1 Hz.
1148c2ecf20Sopenharmony_ci    * - __u32
1158c2ecf20Sopenharmony_ci      - ``offset``
1168c2ecf20Sopenharmony_ci      - Horizontal offset of the VBI image, relative to the leading edge
1178c2ecf20Sopenharmony_ci	of the line synchronization pulse and counted in samples: The
1188c2ecf20Sopenharmony_ci	first sample in the VBI image will be located ``offset`` /
1198c2ecf20Sopenharmony_ci	``sampling_rate`` seconds following the leading edge. See also
1208c2ecf20Sopenharmony_ci	:ref:`vbi-hsync`.
1218c2ecf20Sopenharmony_ci    * - __u32
1228c2ecf20Sopenharmony_ci      - ``samples_per_line``
1238c2ecf20Sopenharmony_ci      -
1248c2ecf20Sopenharmony_ci    * - __u32
1258c2ecf20Sopenharmony_ci      - ``sample_format``
1268c2ecf20Sopenharmony_ci      - Defines the sample format as in :ref:`pixfmt`, a
1278c2ecf20Sopenharmony_ci	four-character-code. [#f2]_ Usually this is ``V4L2_PIX_FMT_GREY``,
1288c2ecf20Sopenharmony_ci	i. e. each sample consists of 8 bits with lower values oriented
1298c2ecf20Sopenharmony_ci	towards the black level. Do not assume any other correlation of
1308c2ecf20Sopenharmony_ci	values with the signal level. For example, the MSB does not
1318c2ecf20Sopenharmony_ci	necessarily indicate if the signal is 'high' or 'low' because 128
1328c2ecf20Sopenharmony_ci	may not be the mean value of the signal. Drivers shall not convert
1338c2ecf20Sopenharmony_ci	the sample format by software.
1348c2ecf20Sopenharmony_ci    * - __u32
1358c2ecf20Sopenharmony_ci      - ``start``\ [#f2]_
1368c2ecf20Sopenharmony_ci      - This is the scanning system line number associated with the first
1378c2ecf20Sopenharmony_ci	line of the VBI image, of the first and the second field
1388c2ecf20Sopenharmony_ci	respectively. See :ref:`vbi-525` and :ref:`vbi-625` for valid
1398c2ecf20Sopenharmony_ci	values. The ``V4L2_VBI_ITU_525_F1_START``,
1408c2ecf20Sopenharmony_ci	``V4L2_VBI_ITU_525_F2_START``, ``V4L2_VBI_ITU_625_F1_START`` and
1418c2ecf20Sopenharmony_ci	``V4L2_VBI_ITU_625_F2_START`` defines give the start line numbers
1428c2ecf20Sopenharmony_ci	for each field for each 525 or 625 line format as a convenience.
1438c2ecf20Sopenharmony_ci	Don't forget that ITU line numbering starts at 1, not 0. VBI input
1448c2ecf20Sopenharmony_ci	drivers can return start values 0 if the hardware cannot reliable
1458c2ecf20Sopenharmony_ci	identify scanning lines, VBI acquisition may not require this
1468c2ecf20Sopenharmony_ci	information.
1478c2ecf20Sopenharmony_ci    * - __u32
1488c2ecf20Sopenharmony_ci      - ``count``\ [#f2]_
1498c2ecf20Sopenharmony_ci      - The number of lines in the first and second field image,
1508c2ecf20Sopenharmony_ci	respectively.
1518c2ecf20Sopenharmony_ci    * - :cspan:`2`
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	Drivers should be as flexibility as possible. For example, it may
1548c2ecf20Sopenharmony_ci	be possible to extend or move the VBI capture window down to the
1558c2ecf20Sopenharmony_ci	picture area, implementing a 'full field mode' to capture data
1568c2ecf20Sopenharmony_ci	service transmissions embedded in the picture.
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	An application can set the first or second ``count`` value to zero
1598c2ecf20Sopenharmony_ci	if no data is required from the respective field; ``count``\ [1]
1608c2ecf20Sopenharmony_ci	if the scanning system is progressive, i. e. not interlaced. The
1618c2ecf20Sopenharmony_ci	corresponding start value shall be ignored by the application and
1628c2ecf20Sopenharmony_ci	driver. Anyway, drivers may not support single field capturing and
1638c2ecf20Sopenharmony_ci	return both count values non-zero.
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	Both ``count`` values set to zero, or line numbers are outside the
1668c2ecf20Sopenharmony_ci	bounds depicted\ [#f4]_, or a field image covering lines of two
1678c2ecf20Sopenharmony_ci	fields, are invalid and shall not be returned by the driver.
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	To initialize the ``start`` and ``count`` fields, applications
1708c2ecf20Sopenharmony_ci	must first determine the current video standard selection. The
1718c2ecf20Sopenharmony_ci	:ref:`v4l2_std_id <v4l2-std-id>` or the ``framelines`` field
1728c2ecf20Sopenharmony_ci	of struct :c:type:`v4l2_standard` can be evaluated
1738c2ecf20Sopenharmony_ci	for this purpose.
1748c2ecf20Sopenharmony_ci    * - __u32
1758c2ecf20Sopenharmony_ci      - ``flags``
1768c2ecf20Sopenharmony_ci      - See :ref:`vbifmt-flags` below. Currently only drivers set flags,
1778c2ecf20Sopenharmony_ci	applications must set this field to zero.
1788c2ecf20Sopenharmony_ci    * - __u32
1798c2ecf20Sopenharmony_ci      - ``reserved``\ [#f2]_
1808c2ecf20Sopenharmony_ci      - This array is reserved for future extensions. Drivers and
1818c2ecf20Sopenharmony_ci	applications must set it to zero.
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci.. tabularcolumns:: |p{4.4cm}|p{1.5cm}|p{11.6cm}|
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci.. _vbifmt-flags:
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci.. flat-table:: Raw VBI Format Flags
1888c2ecf20Sopenharmony_ci    :header-rows:  0
1898c2ecf20Sopenharmony_ci    :stub-columns: 0
1908c2ecf20Sopenharmony_ci    :widths:       3 1 4
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci    * - ``V4L2_VBI_UNSYNC``
1938c2ecf20Sopenharmony_ci      - 0x0001
1948c2ecf20Sopenharmony_ci      - This flag indicates hardware which does not properly distinguish
1958c2ecf20Sopenharmony_ci	between fields. Normally the VBI image stores the first field
1968c2ecf20Sopenharmony_ci	(lower scanning line numbers) first in memory. This may be a top
1978c2ecf20Sopenharmony_ci	or bottom field depending on the video standard. When this flag is
1988c2ecf20Sopenharmony_ci	set the first or second field may be stored first, however the
1998c2ecf20Sopenharmony_ci	fields are still in correct temporal order with the older field
2008c2ecf20Sopenharmony_ci	first in memory. [#f3]_
2018c2ecf20Sopenharmony_ci    * - ``V4L2_VBI_INTERLACED``
2028c2ecf20Sopenharmony_ci      - 0x0002
2038c2ecf20Sopenharmony_ci      - By default the two field images will be passed sequentially; all
2048c2ecf20Sopenharmony_ci	lines of the first field followed by all lines of the second field
2058c2ecf20Sopenharmony_ci	(compare :ref:`field-order` ``V4L2_FIELD_SEQ_TB`` and
2068c2ecf20Sopenharmony_ci	``V4L2_FIELD_SEQ_BT``, whether the top or bottom field is first in
2078c2ecf20Sopenharmony_ci	memory depends on the video standard). When this flag is set, the
2088c2ecf20Sopenharmony_ci	two fields are interlaced (cf. ``V4L2_FIELD_INTERLACED``). The
2098c2ecf20Sopenharmony_ci	first line of the first field followed by the first line of the
2108c2ecf20Sopenharmony_ci	second field, then the two second lines, and so on. Such a layout
2118c2ecf20Sopenharmony_ci	may be necessary when the hardware has been programmed to capture
2128c2ecf20Sopenharmony_ci	or output interlaced video images and is unable to separate the
2138c2ecf20Sopenharmony_ci	fields for VBI capturing at the same time. For simplicity setting
2148c2ecf20Sopenharmony_ci	this flag implies that both ``count`` values are equal and
2158c2ecf20Sopenharmony_ci	non-zero.
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci.. _vbi-hsync:
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci.. kernel-figure:: vbi_hsync.svg
2218c2ecf20Sopenharmony_ci    :alt:   vbi_hsync.svg
2228c2ecf20Sopenharmony_ci    :align: center
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci    **Figure 4.1. Line synchronization**
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci.. _vbi-525:
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci.. kernel-figure:: vbi_525.svg
2298c2ecf20Sopenharmony_ci    :alt:   vbi_525.svg
2308c2ecf20Sopenharmony_ci    :align: center
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci    **Figure 4.2. ITU-R 525 line numbering (M/NTSC and M/PAL)**
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci.. _vbi-625:
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci.. kernel-figure:: vbi_625.svg
2378c2ecf20Sopenharmony_ci    :alt:   vbi_625.svg
2388c2ecf20Sopenharmony_ci    :align: center
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci    **Figure 4.3. ITU-R 625 line numbering**
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ciRemember the VBI image format depends on the selected video standard,
2438c2ecf20Sopenharmony_citherefore the application must choose a new standard or query the
2448c2ecf20Sopenharmony_cicurrent standard first. Attempts to read or write data ahead of format
2458c2ecf20Sopenharmony_cinegotiation, or after switching the video standard which may invalidate
2468c2ecf20Sopenharmony_cithe negotiated VBI parameters, should be refused by the driver. A format
2478c2ecf20Sopenharmony_cichange during active I/O is not permitted.
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciReading and writing VBI images
2508c2ecf20Sopenharmony_ci==============================
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciTo assure synchronization with the field number and easier
2538c2ecf20Sopenharmony_ciimplementation, the smallest unit of data passed at a time is one frame,
2548c2ecf20Sopenharmony_ciconsisting of two fields of VBI images immediately following in memory.
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ciThe total size of a frame computes as follows:
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci.. code-block:: c
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci    (count[0] + count[1]) * samples_per_line * sample size in bytes
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciThe sample size is most likely always one byte, applications must check
2638c2ecf20Sopenharmony_cithe ``sample_format`` field though, to function properly with other
2648c2ecf20Sopenharmony_cidrivers.
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciA VBI device may support :ref:`read/write <rw>` and/or streaming
2678c2ecf20Sopenharmony_ci(:ref:`memory mapping <mmap>` or :ref:`user pointer <userp>`) I/O.
2688c2ecf20Sopenharmony_ciThe latter bears the possibility of synchronizing video and VBI data by
2698c2ecf20Sopenharmony_ciusing buffer timestamps.
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ciRemember the :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` ioctl and the
2728c2ecf20Sopenharmony_cifirst :c:func:`read()`, :c:func:`write()` and
2738c2ecf20Sopenharmony_ci:c:func:`select()` call can be resource allocation
2748c2ecf20Sopenharmony_cipoints returning an ``EBUSY`` error code if the required hardware resources
2758c2ecf20Sopenharmony_ciare temporarily unavailable, for example the device is already in use by
2768c2ecf20Sopenharmony_cianother process.
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci.. [#f1]
2798c2ecf20Sopenharmony_ci   ASK: Amplitude-Shift Keying. A high signal level represents a '1'
2808c2ecf20Sopenharmony_ci   bit, a low level a '0' bit.
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci.. [#f2]
2838c2ecf20Sopenharmony_ci   A few devices may be unable to sample VBI data at all but can extend
2848c2ecf20Sopenharmony_ci   the video capture window to the VBI region.
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci.. [#f3]
2878c2ecf20Sopenharmony_ci   Most VBI services transmit on both fields, but some have different
2888c2ecf20Sopenharmony_ci   semantics depending on the field number. These cannot be reliable
2898c2ecf20Sopenharmony_ci   decoded or encoded when ``V4L2_VBI_UNSYNC`` is set.
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci.. [#f4]
2928c2ecf20Sopenharmony_ci   The valid values ar shown at :ref:`vbi-525` and :ref:`vbi-625`.
293