162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _raw-vbi:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci**********************
762306a36Sopenharmony_ciRaw VBI Data Interface
862306a36Sopenharmony_ci**********************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciVBI is an abbreviation of Vertical Blanking Interval, a gap in the
1162306a36Sopenharmony_cisequence of lines of an analog video signal. During VBI no picture
1262306a36Sopenharmony_ciinformation is transmitted, allowing some time while the electron beam
1362306a36Sopenharmony_ciof a cathode ray tube TV returns to the top of the screen. Using an
1462306a36Sopenharmony_cioscilloscope you will find here the vertical synchronization pulses and
1562306a36Sopenharmony_cishort data packages ASK modulated [#f1]_ onto the video signal. These are
1662306a36Sopenharmony_citransmissions of services such as Teletext or Closed Caption.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciSubject of this interface type is raw VBI data, as sampled off a video
1962306a36Sopenharmony_cisignal, or to be added to a signal for output. The data format is
2062306a36Sopenharmony_cisimilar to uncompressed video images, a number of lines times a number
2162306a36Sopenharmony_ciof samples per line, we call this a VBI image.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciConventionally V4L2 VBI devices are accessed through character device
2462306a36Sopenharmony_cispecial files named ``/dev/vbi`` and ``/dev/vbi0`` to ``/dev/vbi31``
2562306a36Sopenharmony_ciwith major number 81 and minor numbers 224 to 255. ``/dev/vbi`` is
2662306a36Sopenharmony_citypically a symbolic link to the preferred VBI device. This convention
2762306a36Sopenharmony_ciapplies to both input and output devices.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciTo address the problems of finding related video and VBI devices VBI
3062306a36Sopenharmony_cicapturing and output is also available as device function under
3162306a36Sopenharmony_ci``/dev/video``. To capture or output raw VBI data with these devices
3262306a36Sopenharmony_ciapplications must call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl.
3362306a36Sopenharmony_ciAccessed as ``/dev/vbi``, raw VBI capturing or output is the default
3462306a36Sopenharmony_cidevice function.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciQuerying Capabilities
3762306a36Sopenharmony_ci=====================
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciDevices supporting the raw VBI capturing or output API set the
4062306a36Sopenharmony_ci``V4L2_CAP_VBI_CAPTURE`` or ``V4L2_CAP_VBI_OUTPUT`` flags, respectively,
4162306a36Sopenharmony_ciin the ``capabilities`` field of struct
4262306a36Sopenharmony_ci:c:type:`v4l2_capability` returned by the
4362306a36Sopenharmony_ci:ref:`VIDIOC_QUERYCAP` ioctl. At least one of the
4462306a36Sopenharmony_ciread/write or streaming I/O methods must be supported. VBI
4562306a36Sopenharmony_cidevices may or may not have a tuner or modulator.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciSupplemental Functions
4862306a36Sopenharmony_ci======================
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciVBI devices shall support :ref:`video input or output <video>`,
5162306a36Sopenharmony_ci:ref:`tuner or modulator <tuner>`, and :ref:`controls <control>`
5262306a36Sopenharmony_ciioctls as needed. The :ref:`video standard <standard>` ioctls provide
5362306a36Sopenharmony_ciinformation vital to program a VBI device, therefore must be supported.
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciRaw VBI Format Negotiation
5662306a36Sopenharmony_ci==========================
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciRaw VBI sampling abilities can vary, in particular the sampling
5962306a36Sopenharmony_cifrequency. To properly interpret the data V4L2 specifies an ioctl to
6062306a36Sopenharmony_ciquery the sampling parameters. Moreover, to allow for some flexibility
6162306a36Sopenharmony_ciapplications can also suggest different parameters.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciAs usual these parameters are *not* reset at :c:func:`open()`
6462306a36Sopenharmony_citime to permit Unix tool chains, programming a device and then reading
6562306a36Sopenharmony_cifrom it as if it was a plain file. Well written V4L2 applications should
6662306a36Sopenharmony_cialways ensure they really get what they want, requesting reasonable
6762306a36Sopenharmony_ciparameters and then checking if the actual parameters are suitable.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciTo query the current raw VBI capture parameters applications set the
7062306a36Sopenharmony_ci``type`` field of a struct :c:type:`v4l2_format` to
7162306a36Sopenharmony_ci``V4L2_BUF_TYPE_VBI_CAPTURE`` or ``V4L2_BUF_TYPE_VBI_OUTPUT``, and call
7262306a36Sopenharmony_cithe :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` ioctl with a pointer to this
7362306a36Sopenharmony_cistructure. Drivers fill the struct
7462306a36Sopenharmony_ci:c:type:`v4l2_vbi_format` ``vbi`` member of the
7562306a36Sopenharmony_ci``fmt`` union.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciTo request different parameters applications set the ``type`` field of a
7862306a36Sopenharmony_cistruct :c:type:`v4l2_format` as above and initialize all
7962306a36Sopenharmony_cifields of the struct :c:type:`v4l2_vbi_format`
8062306a36Sopenharmony_ci``vbi`` member of the ``fmt`` union, or better just modify the results
8162306a36Sopenharmony_ciof :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`, and call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`
8262306a36Sopenharmony_ciioctl with a pointer to this structure. Drivers return an ``EINVAL`` error
8362306a36Sopenharmony_cicode only when the given parameters are ambiguous, otherwise they modify
8462306a36Sopenharmony_cithe parameters according to the hardware capabilities and return the
8562306a36Sopenharmony_ciactual parameters. When the driver allocates resources at this point, it
8662306a36Sopenharmony_cimay return an ``EBUSY`` error code to indicate the returned parameters are
8762306a36Sopenharmony_civalid but the required resources are currently not available. That may
8862306a36Sopenharmony_cihappen for instance when the video and VBI areas to capture would
8962306a36Sopenharmony_cioverlap, or when the driver supports multiple opens and another process
9062306a36Sopenharmony_cialready requested VBI capturing or output. Anyway, applications must
9162306a36Sopenharmony_ciexpect other resource allocation points which may return ``EBUSY``, at the
9262306a36Sopenharmony_ci:ref:`VIDIOC_STREAMON` ioctl and the first :c:func:`read()`
9362306a36Sopenharmony_ci, :c:func:`write()` and :c:func:`select()` calls.
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciVBI devices must implement both the :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and
9662306a36Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl, even if :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ignores all requests
9762306a36Sopenharmony_ciand always returns default parameters as :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` does.
9862306a36Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` is optional.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci.. tabularcolumns:: |p{1.6cm}|p{4.2cm}|p{11.5cm}|
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci.. c:type:: v4l2_vbi_format
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci.. cssclass:: longtable
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci.. flat-table:: struct v4l2_vbi_format
10762306a36Sopenharmony_ci    :header-rows:  0
10862306a36Sopenharmony_ci    :stub-columns: 0
10962306a36Sopenharmony_ci    :widths:       1 1 2
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci    * - __u32
11262306a36Sopenharmony_ci      - ``sampling_rate``
11362306a36Sopenharmony_ci      - Samples per second, i. e. unit 1 Hz.
11462306a36Sopenharmony_ci    * - __u32
11562306a36Sopenharmony_ci      - ``offset``
11662306a36Sopenharmony_ci      - Horizontal offset of the VBI image, relative to the leading edge
11762306a36Sopenharmony_ci	of the line synchronization pulse and counted in samples: The
11862306a36Sopenharmony_ci	first sample in the VBI image will be located ``offset`` /
11962306a36Sopenharmony_ci	``sampling_rate`` seconds following the leading edge. See also
12062306a36Sopenharmony_ci	:ref:`vbi-hsync`.
12162306a36Sopenharmony_ci    * - __u32
12262306a36Sopenharmony_ci      - ``samples_per_line``
12362306a36Sopenharmony_ci      -
12462306a36Sopenharmony_ci    * - __u32
12562306a36Sopenharmony_ci      - ``sample_format``
12662306a36Sopenharmony_ci      - Defines the sample format as in :ref:`pixfmt`, a
12762306a36Sopenharmony_ci	four-character-code. [#f2]_ Usually this is ``V4L2_PIX_FMT_GREY``,
12862306a36Sopenharmony_ci	i. e. each sample consists of 8 bits with lower values oriented
12962306a36Sopenharmony_ci	towards the black level. Do not assume any other correlation of
13062306a36Sopenharmony_ci	values with the signal level. For example, the MSB does not
13162306a36Sopenharmony_ci	necessarily indicate if the signal is 'high' or 'low' because 128
13262306a36Sopenharmony_ci	may not be the mean value of the signal. Drivers shall not convert
13362306a36Sopenharmony_ci	the sample format by software.
13462306a36Sopenharmony_ci    * - __u32
13562306a36Sopenharmony_ci      - ``start``\ [#f2]_
13662306a36Sopenharmony_ci      - This is the scanning system line number associated with the first
13762306a36Sopenharmony_ci	line of the VBI image, of the first and the second field
13862306a36Sopenharmony_ci	respectively. See :ref:`vbi-525` and :ref:`vbi-625` for valid
13962306a36Sopenharmony_ci	values. The ``V4L2_VBI_ITU_525_F1_START``,
14062306a36Sopenharmony_ci	``V4L2_VBI_ITU_525_F2_START``, ``V4L2_VBI_ITU_625_F1_START`` and
14162306a36Sopenharmony_ci	``V4L2_VBI_ITU_625_F2_START`` defines give the start line numbers
14262306a36Sopenharmony_ci	for each field for each 525 or 625 line format as a convenience.
14362306a36Sopenharmony_ci	Don't forget that ITU line numbering starts at 1, not 0. VBI input
14462306a36Sopenharmony_ci	drivers can return start values 0 if the hardware cannot reliable
14562306a36Sopenharmony_ci	identify scanning lines, VBI acquisition may not require this
14662306a36Sopenharmony_ci	information.
14762306a36Sopenharmony_ci    * - __u32
14862306a36Sopenharmony_ci      - ``count``\ [#f2]_
14962306a36Sopenharmony_ci      - The number of lines in the first and second field image,
15062306a36Sopenharmony_ci	respectively.
15162306a36Sopenharmony_ci    * - :cspan:`2`
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	Drivers should be as flexibility as possible. For example, it may
15462306a36Sopenharmony_ci	be possible to extend or move the VBI capture window down to the
15562306a36Sopenharmony_ci	picture area, implementing a 'full field mode' to capture data
15662306a36Sopenharmony_ci	service transmissions embedded in the picture.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	An application can set the first or second ``count`` value to zero
15962306a36Sopenharmony_ci	if no data is required from the respective field; ``count``\ [1]
16062306a36Sopenharmony_ci	if the scanning system is progressive, i. e. not interlaced. The
16162306a36Sopenharmony_ci	corresponding start value shall be ignored by the application and
16262306a36Sopenharmony_ci	driver. Anyway, drivers may not support single field capturing and
16362306a36Sopenharmony_ci	return both count values non-zero.
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	Both ``count`` values set to zero, or line numbers are outside the
16662306a36Sopenharmony_ci	bounds depicted\ [#f4]_, or a field image covering lines of two
16762306a36Sopenharmony_ci	fields, are invalid and shall not be returned by the driver.
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	To initialize the ``start`` and ``count`` fields, applications
17062306a36Sopenharmony_ci	must first determine the current video standard selection. The
17162306a36Sopenharmony_ci	:ref:`v4l2_std_id <v4l2-std-id>` or the ``framelines`` field
17262306a36Sopenharmony_ci	of struct :c:type:`v4l2_standard` can be evaluated
17362306a36Sopenharmony_ci	for this purpose.
17462306a36Sopenharmony_ci    * - __u32
17562306a36Sopenharmony_ci      - ``flags``
17662306a36Sopenharmony_ci      - See :ref:`vbifmt-flags` below. Currently only drivers set flags,
17762306a36Sopenharmony_ci	applications must set this field to zero.
17862306a36Sopenharmony_ci    * - __u32
17962306a36Sopenharmony_ci      - ``reserved``\ [#f2]_
18062306a36Sopenharmony_ci      - This array is reserved for future extensions. Drivers and
18162306a36Sopenharmony_ci	applications must set it to zero.
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci.. tabularcolumns:: |p{4.4cm}|p{1.5cm}|p{11.4cm}|
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci.. _vbifmt-flags:
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci.. flat-table:: Raw VBI Format Flags
18862306a36Sopenharmony_ci    :header-rows:  0
18962306a36Sopenharmony_ci    :stub-columns: 0
19062306a36Sopenharmony_ci    :widths:       3 1 4
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci    * - ``V4L2_VBI_UNSYNC``
19362306a36Sopenharmony_ci      - 0x0001
19462306a36Sopenharmony_ci      - This flag indicates hardware which does not properly distinguish
19562306a36Sopenharmony_ci	between fields. Normally the VBI image stores the first field
19662306a36Sopenharmony_ci	(lower scanning line numbers) first in memory. This may be a top
19762306a36Sopenharmony_ci	or bottom field depending on the video standard. When this flag is
19862306a36Sopenharmony_ci	set the first or second field may be stored first, however the
19962306a36Sopenharmony_ci	fields are still in correct temporal order with the older field
20062306a36Sopenharmony_ci	first in memory. [#f3]_
20162306a36Sopenharmony_ci    * - ``V4L2_VBI_INTERLACED``
20262306a36Sopenharmony_ci      - 0x0002
20362306a36Sopenharmony_ci      - By default the two field images will be passed sequentially; all
20462306a36Sopenharmony_ci	lines of the first field followed by all lines of the second field
20562306a36Sopenharmony_ci	(compare :ref:`field-order` ``V4L2_FIELD_SEQ_TB`` and
20662306a36Sopenharmony_ci	``V4L2_FIELD_SEQ_BT``, whether the top or bottom field is first in
20762306a36Sopenharmony_ci	memory depends on the video standard). When this flag is set, the
20862306a36Sopenharmony_ci	two fields are interlaced (cf. ``V4L2_FIELD_INTERLACED``). The
20962306a36Sopenharmony_ci	first line of the first field followed by the first line of the
21062306a36Sopenharmony_ci	second field, then the two second lines, and so on. Such a layout
21162306a36Sopenharmony_ci	may be necessary when the hardware has been programmed to capture
21262306a36Sopenharmony_ci	or output interlaced video images and is unable to separate the
21362306a36Sopenharmony_ci	fields for VBI capturing at the same time. For simplicity setting
21462306a36Sopenharmony_ci	this flag implies that both ``count`` values are equal and
21562306a36Sopenharmony_ci	non-zero.
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci.. _vbi-hsync:
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci.. kernel-figure:: vbi_hsync.svg
22162306a36Sopenharmony_ci    :alt:   vbi_hsync.svg
22262306a36Sopenharmony_ci    :align: center
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci    **Figure 4.1. Line synchronization**
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci.. _vbi-525:
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci.. kernel-figure:: vbi_525.svg
22962306a36Sopenharmony_ci    :alt:   vbi_525.svg
23062306a36Sopenharmony_ci    :align: center
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci    **Figure 4.2. ITU-R 525 line numbering (M/NTSC and M/PAL)**
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci.. _vbi-625:
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci.. kernel-figure:: vbi_625.svg
23762306a36Sopenharmony_ci    :alt:   vbi_625.svg
23862306a36Sopenharmony_ci    :align: center
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci    **Figure 4.3. ITU-R 625 line numbering**
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciRemember the VBI image format depends on the selected video standard,
24362306a36Sopenharmony_citherefore the application must choose a new standard or query the
24462306a36Sopenharmony_cicurrent standard first. Attempts to read or write data ahead of format
24562306a36Sopenharmony_cinegotiation, or after switching the video standard which may invalidate
24662306a36Sopenharmony_cithe negotiated VBI parameters, should be refused by the driver. A format
24762306a36Sopenharmony_cichange during active I/O is not permitted.
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ciReading and writing VBI images
25062306a36Sopenharmony_ci==============================
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ciTo assure synchronization with the field number and easier
25362306a36Sopenharmony_ciimplementation, the smallest unit of data passed at a time is one frame,
25462306a36Sopenharmony_ciconsisting of two fields of VBI images immediately following in memory.
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ciThe total size of a frame computes as follows:
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci.. code-block:: c
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci    (count[0] + count[1]) * samples_per_line * sample size in bytes
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciThe sample size is most likely always one byte, applications must check
26362306a36Sopenharmony_cithe ``sample_format`` field though, to function properly with other
26462306a36Sopenharmony_cidrivers.
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ciA VBI device may support :ref:`read/write <rw>` and/or streaming
26762306a36Sopenharmony_ci(:ref:`memory mapping <mmap>` or :ref:`user pointer <userp>`) I/O.
26862306a36Sopenharmony_ciThe latter bears the possibility of synchronizing video and VBI data by
26962306a36Sopenharmony_ciusing buffer timestamps.
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ciRemember the :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` ioctl and the
27262306a36Sopenharmony_cifirst :c:func:`read()`, :c:func:`write()` and
27362306a36Sopenharmony_ci:c:func:`select()` call can be resource allocation
27462306a36Sopenharmony_cipoints returning an ``EBUSY`` error code if the required hardware resources
27562306a36Sopenharmony_ciare temporarily unavailable, for example the device is already in use by
27662306a36Sopenharmony_cianother process.
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci.. [#f1]
27962306a36Sopenharmony_ci   ASK: Amplitude-Shift Keying. A high signal level represents a '1'
28062306a36Sopenharmony_ci   bit, a low level a '0' bit.
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci.. [#f2]
28362306a36Sopenharmony_ci   A few devices may be unable to sample VBI data at all but can extend
28462306a36Sopenharmony_ci   the video capture window to the VBI region.
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci.. [#f3]
28762306a36Sopenharmony_ci   Most VBI services transmit on both fields, but some have different
28862306a36Sopenharmony_ci   semantics depending on the field number. These cannot be reliable
28962306a36Sopenharmony_ci   decoded or encoded when ``V4L2_VBI_UNSYNC`` is set.
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci.. [#f4]
29262306a36Sopenharmony_ci   The valid values ar shown at :ref:`vbi-525` and :ref:`vbi-625`.
293