1*4882a593Smuzhiyun.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 2*4882a593Smuzhiyun.. c:namespace:: V4L 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun.. _raw-vbi: 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun********************** 7*4882a593SmuzhiyunRaw VBI Data Interface 8*4882a593Smuzhiyun********************** 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunVBI is an abbreviation of Vertical Blanking Interval, a gap in the 11*4882a593Smuzhiyunsequence of lines of an analog video signal. During VBI no picture 12*4882a593Smuzhiyuninformation is transmitted, allowing some time while the electron beam 13*4882a593Smuzhiyunof a cathode ray tube TV returns to the top of the screen. Using an 14*4882a593Smuzhiyunoscilloscope you will find here the vertical synchronization pulses and 15*4882a593Smuzhiyunshort data packages ASK modulated [#f1]_ onto the video signal. These are 16*4882a593Smuzhiyuntransmissions of services such as Teletext or Closed Caption. 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunSubject of this interface type is raw VBI data, as sampled off a video 19*4882a593Smuzhiyunsignal, or to be added to a signal for output. The data format is 20*4882a593Smuzhiyunsimilar to uncompressed video images, a number of lines times a number 21*4882a593Smuzhiyunof samples per line, we call this a VBI image. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunConventionally V4L2 VBI devices are accessed through character device 24*4882a593Smuzhiyunspecial files named ``/dev/vbi`` and ``/dev/vbi0`` to ``/dev/vbi31`` 25*4882a593Smuzhiyunwith major number 81 and minor numbers 224 to 255. ``/dev/vbi`` is 26*4882a593Smuzhiyuntypically a symbolic link to the preferred VBI device. This convention 27*4882a593Smuzhiyunapplies to both input and output devices. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunTo address the problems of finding related video and VBI devices VBI 30*4882a593Smuzhiyuncapturing and output is also available as device function under 31*4882a593Smuzhiyun``/dev/video``. To capture or output raw VBI data with these devices 32*4882a593Smuzhiyunapplications must call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl. 33*4882a593SmuzhiyunAccessed as ``/dev/vbi``, raw VBI capturing or output is the default 34*4882a593Smuzhiyundevice function. 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunQuerying Capabilities 37*4882a593Smuzhiyun===================== 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunDevices supporting the raw VBI capturing or output API set the 40*4882a593Smuzhiyun``V4L2_CAP_VBI_CAPTURE`` or ``V4L2_CAP_VBI_OUTPUT`` flags, respectively, 41*4882a593Smuzhiyunin the ``capabilities`` field of struct 42*4882a593Smuzhiyun:c:type:`v4l2_capability` returned by the 43*4882a593Smuzhiyun:ref:`VIDIOC_QUERYCAP` ioctl. At least one of the 44*4882a593Smuzhiyunread/write, streaming or asynchronous I/O methods must be supported. VBI 45*4882a593Smuzhiyundevices may or may not have a tuner or modulator. 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunSupplemental Functions 48*4882a593Smuzhiyun====================== 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunVBI devices shall support :ref:`video input or output <video>`, 51*4882a593Smuzhiyun:ref:`tuner or modulator <tuner>`, and :ref:`controls <control>` 52*4882a593Smuzhiyunioctls as needed. The :ref:`video standard <standard>` ioctls provide 53*4882a593Smuzhiyuninformation vital to program a VBI device, therefore must be supported. 54*4882a593Smuzhiyun 55*4882a593SmuzhiyunRaw VBI Format Negotiation 56*4882a593Smuzhiyun========================== 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunRaw VBI sampling abilities can vary, in particular the sampling 59*4882a593Smuzhiyunfrequency. To properly interpret the data V4L2 specifies an ioctl to 60*4882a593Smuzhiyunquery the sampling parameters. Moreover, to allow for some flexibility 61*4882a593Smuzhiyunapplications can also suggest different parameters. 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunAs usual these parameters are *not* reset at :c:func:`open()` 64*4882a593Smuzhiyuntime to permit Unix tool chains, programming a device and then reading 65*4882a593Smuzhiyunfrom it as if it was a plain file. Well written V4L2 applications should 66*4882a593Smuzhiyunalways ensure they really get what they want, requesting reasonable 67*4882a593Smuzhiyunparameters and then checking if the actual parameters are suitable. 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunTo query the current raw VBI capture parameters applications set the 70*4882a593Smuzhiyun``type`` field of a struct :c:type:`v4l2_format` to 71*4882a593Smuzhiyun``V4L2_BUF_TYPE_VBI_CAPTURE`` or ``V4L2_BUF_TYPE_VBI_OUTPUT``, and call 72*4882a593Smuzhiyunthe :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` ioctl with a pointer to this 73*4882a593Smuzhiyunstructure. Drivers fill the struct 74*4882a593Smuzhiyun:c:type:`v4l2_vbi_format` ``vbi`` member of the 75*4882a593Smuzhiyun``fmt`` union. 76*4882a593Smuzhiyun 77*4882a593SmuzhiyunTo request different parameters applications set the ``type`` field of a 78*4882a593Smuzhiyunstruct :c:type:`v4l2_format` as above and initialize all 79*4882a593Smuzhiyunfields of the struct :c:type:`v4l2_vbi_format` 80*4882a593Smuzhiyun``vbi`` member of the ``fmt`` union, or better just modify the results 81*4882a593Smuzhiyunof :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`, and call the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` 82*4882a593Smuzhiyunioctl with a pointer to this structure. Drivers return an ``EINVAL`` error 83*4882a593Smuzhiyuncode only when the given parameters are ambiguous, otherwise they modify 84*4882a593Smuzhiyunthe parameters according to the hardware capabilities and return the 85*4882a593Smuzhiyunactual parameters. When the driver allocates resources at this point, it 86*4882a593Smuzhiyunmay return an ``EBUSY`` error code to indicate the returned parameters are 87*4882a593Smuzhiyunvalid but the required resources are currently not available. That may 88*4882a593Smuzhiyunhappen for instance when the video and VBI areas to capture would 89*4882a593Smuzhiyunoverlap, or when the driver supports multiple opens and another process 90*4882a593Smuzhiyunalready requested VBI capturing or output. Anyway, applications must 91*4882a593Smuzhiyunexpect other resource allocation points which may return ``EBUSY``, at the 92*4882a593Smuzhiyun:ref:`VIDIOC_STREAMON` ioctl and the first :c:func:`read()` 93*4882a593Smuzhiyun, :c:func:`write()` and :c:func:`select()` calls. 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunVBI devices must implement both the :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` and 96*4882a593Smuzhiyun:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl, even if :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ignores all requests 97*4882a593Smuzhiyunand always returns default parameters as :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>` does. 98*4882a593Smuzhiyun:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>` is optional. 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun.. tabularcolumns:: |p{1.6cm}|p{4.2cm}|p{11.7cm}| 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun.. c:type:: v4l2_vbi_format 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun.. cssclass:: longtable 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun.. flat-table:: struct v4l2_vbi_format 107*4882a593Smuzhiyun :header-rows: 0 108*4882a593Smuzhiyun :stub-columns: 0 109*4882a593Smuzhiyun :widths: 1 1 2 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun * - __u32 112*4882a593Smuzhiyun - ``sampling_rate`` 113*4882a593Smuzhiyun - Samples per second, i. e. unit 1 Hz. 114*4882a593Smuzhiyun * - __u32 115*4882a593Smuzhiyun - ``offset`` 116*4882a593Smuzhiyun - Horizontal offset of the VBI image, relative to the leading edge 117*4882a593Smuzhiyun of the line synchronization pulse and counted in samples: The 118*4882a593Smuzhiyun first sample in the VBI image will be located ``offset`` / 119*4882a593Smuzhiyun ``sampling_rate`` seconds following the leading edge. See also 120*4882a593Smuzhiyun :ref:`vbi-hsync`. 121*4882a593Smuzhiyun * - __u32 122*4882a593Smuzhiyun - ``samples_per_line`` 123*4882a593Smuzhiyun - 124*4882a593Smuzhiyun * - __u32 125*4882a593Smuzhiyun - ``sample_format`` 126*4882a593Smuzhiyun - Defines the sample format as in :ref:`pixfmt`, a 127*4882a593Smuzhiyun four-character-code. [#f2]_ Usually this is ``V4L2_PIX_FMT_GREY``, 128*4882a593Smuzhiyun i. e. each sample consists of 8 bits with lower values oriented 129*4882a593Smuzhiyun towards the black level. Do not assume any other correlation of 130*4882a593Smuzhiyun values with the signal level. For example, the MSB does not 131*4882a593Smuzhiyun necessarily indicate if the signal is 'high' or 'low' because 128 132*4882a593Smuzhiyun may not be the mean value of the signal. Drivers shall not convert 133*4882a593Smuzhiyun the sample format by software. 134*4882a593Smuzhiyun * - __u32 135*4882a593Smuzhiyun - ``start``\ [#f2]_ 136*4882a593Smuzhiyun - This is the scanning system line number associated with the first 137*4882a593Smuzhiyun line of the VBI image, of the first and the second field 138*4882a593Smuzhiyun respectively. See :ref:`vbi-525` and :ref:`vbi-625` for valid 139*4882a593Smuzhiyun values. The ``V4L2_VBI_ITU_525_F1_START``, 140*4882a593Smuzhiyun ``V4L2_VBI_ITU_525_F2_START``, ``V4L2_VBI_ITU_625_F1_START`` and 141*4882a593Smuzhiyun ``V4L2_VBI_ITU_625_F2_START`` defines give the start line numbers 142*4882a593Smuzhiyun for each field for each 525 or 625 line format as a convenience. 143*4882a593Smuzhiyun Don't forget that ITU line numbering starts at 1, not 0. VBI input 144*4882a593Smuzhiyun drivers can return start values 0 if the hardware cannot reliable 145*4882a593Smuzhiyun identify scanning lines, VBI acquisition may not require this 146*4882a593Smuzhiyun information. 147*4882a593Smuzhiyun * - __u32 148*4882a593Smuzhiyun - ``count``\ [#f2]_ 149*4882a593Smuzhiyun - The number of lines in the first and second field image, 150*4882a593Smuzhiyun respectively. 151*4882a593Smuzhiyun * - :cspan:`2` 152*4882a593Smuzhiyun 153*4882a593Smuzhiyun Drivers should be as flexibility as possible. For example, it may 154*4882a593Smuzhiyun be possible to extend or move the VBI capture window down to the 155*4882a593Smuzhiyun picture area, implementing a 'full field mode' to capture data 156*4882a593Smuzhiyun service transmissions embedded in the picture. 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun An application can set the first or second ``count`` value to zero 159*4882a593Smuzhiyun if no data is required from the respective field; ``count``\ [1] 160*4882a593Smuzhiyun if the scanning system is progressive, i. e. not interlaced. The 161*4882a593Smuzhiyun corresponding start value shall be ignored by the application and 162*4882a593Smuzhiyun driver. Anyway, drivers may not support single field capturing and 163*4882a593Smuzhiyun return both count values non-zero. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun Both ``count`` values set to zero, or line numbers are outside the 166*4882a593Smuzhiyun bounds depicted\ [#f4]_, or a field image covering lines of two 167*4882a593Smuzhiyun fields, are invalid and shall not be returned by the driver. 168*4882a593Smuzhiyun 169*4882a593Smuzhiyun To initialize the ``start`` and ``count`` fields, applications 170*4882a593Smuzhiyun must first determine the current video standard selection. The 171*4882a593Smuzhiyun :ref:`v4l2_std_id <v4l2-std-id>` or the ``framelines`` field 172*4882a593Smuzhiyun of struct :c:type:`v4l2_standard` can be evaluated 173*4882a593Smuzhiyun for this purpose. 174*4882a593Smuzhiyun * - __u32 175*4882a593Smuzhiyun - ``flags`` 176*4882a593Smuzhiyun - See :ref:`vbifmt-flags` below. Currently only drivers set flags, 177*4882a593Smuzhiyun applications must set this field to zero. 178*4882a593Smuzhiyun * - __u32 179*4882a593Smuzhiyun - ``reserved``\ [#f2]_ 180*4882a593Smuzhiyun - This array is reserved for future extensions. Drivers and 181*4882a593Smuzhiyun applications must set it to zero. 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun.. tabularcolumns:: |p{4.4cm}|p{1.5cm}|p{11.6cm}| 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun.. _vbifmt-flags: 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun.. flat-table:: Raw VBI Format Flags 188*4882a593Smuzhiyun :header-rows: 0 189*4882a593Smuzhiyun :stub-columns: 0 190*4882a593Smuzhiyun :widths: 3 1 4 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun * - ``V4L2_VBI_UNSYNC`` 193*4882a593Smuzhiyun - 0x0001 194*4882a593Smuzhiyun - This flag indicates hardware which does not properly distinguish 195*4882a593Smuzhiyun between fields. Normally the VBI image stores the first field 196*4882a593Smuzhiyun (lower scanning line numbers) first in memory. This may be a top 197*4882a593Smuzhiyun or bottom field depending on the video standard. When this flag is 198*4882a593Smuzhiyun set the first or second field may be stored first, however the 199*4882a593Smuzhiyun fields are still in correct temporal order with the older field 200*4882a593Smuzhiyun first in memory. [#f3]_ 201*4882a593Smuzhiyun * - ``V4L2_VBI_INTERLACED`` 202*4882a593Smuzhiyun - 0x0002 203*4882a593Smuzhiyun - By default the two field images will be passed sequentially; all 204*4882a593Smuzhiyun lines of the first field followed by all lines of the second field 205*4882a593Smuzhiyun (compare :ref:`field-order` ``V4L2_FIELD_SEQ_TB`` and 206*4882a593Smuzhiyun ``V4L2_FIELD_SEQ_BT``, whether the top or bottom field is first in 207*4882a593Smuzhiyun memory depends on the video standard). When this flag is set, the 208*4882a593Smuzhiyun two fields are interlaced (cf. ``V4L2_FIELD_INTERLACED``). The 209*4882a593Smuzhiyun first line of the first field followed by the first line of the 210*4882a593Smuzhiyun second field, then the two second lines, and so on. Such a layout 211*4882a593Smuzhiyun may be necessary when the hardware has been programmed to capture 212*4882a593Smuzhiyun or output interlaced video images and is unable to separate the 213*4882a593Smuzhiyun fields for VBI capturing at the same time. For simplicity setting 214*4882a593Smuzhiyun this flag implies that both ``count`` values are equal and 215*4882a593Smuzhiyun non-zero. 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun.. _vbi-hsync: 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun.. kernel-figure:: vbi_hsync.svg 221*4882a593Smuzhiyun :alt: vbi_hsync.svg 222*4882a593Smuzhiyun :align: center 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun **Figure 4.1. Line synchronization** 225*4882a593Smuzhiyun 226*4882a593Smuzhiyun.. _vbi-525: 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun.. kernel-figure:: vbi_525.svg 229*4882a593Smuzhiyun :alt: vbi_525.svg 230*4882a593Smuzhiyun :align: center 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun **Figure 4.2. ITU-R 525 line numbering (M/NTSC and M/PAL)** 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun.. _vbi-625: 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun.. kernel-figure:: vbi_625.svg 237*4882a593Smuzhiyun :alt: vbi_625.svg 238*4882a593Smuzhiyun :align: center 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun **Figure 4.3. ITU-R 625 line numbering** 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunRemember the VBI image format depends on the selected video standard, 243*4882a593Smuzhiyuntherefore the application must choose a new standard or query the 244*4882a593Smuzhiyuncurrent standard first. Attempts to read or write data ahead of format 245*4882a593Smuzhiyunnegotiation, or after switching the video standard which may invalidate 246*4882a593Smuzhiyunthe negotiated VBI parameters, should be refused by the driver. A format 247*4882a593Smuzhiyunchange during active I/O is not permitted. 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunReading and writing VBI images 250*4882a593Smuzhiyun============================== 251*4882a593Smuzhiyun 252*4882a593SmuzhiyunTo assure synchronization with the field number and easier 253*4882a593Smuzhiyunimplementation, the smallest unit of data passed at a time is one frame, 254*4882a593Smuzhiyunconsisting of two fields of VBI images immediately following in memory. 255*4882a593Smuzhiyun 256*4882a593SmuzhiyunThe total size of a frame computes as follows: 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun.. code-block:: c 259*4882a593Smuzhiyun 260*4882a593Smuzhiyun (count[0] + count[1]) * samples_per_line * sample size in bytes 261*4882a593Smuzhiyun 262*4882a593SmuzhiyunThe sample size is most likely always one byte, applications must check 263*4882a593Smuzhiyunthe ``sample_format`` field though, to function properly with other 264*4882a593Smuzhiyundrivers. 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunA VBI device may support :ref:`read/write <rw>` and/or streaming 267*4882a593Smuzhiyun(:ref:`memory mapping <mmap>` or :ref:`user pointer <userp>`) I/O. 268*4882a593SmuzhiyunThe latter bears the possibility of synchronizing video and VBI data by 269*4882a593Smuzhiyunusing buffer timestamps. 270*4882a593Smuzhiyun 271*4882a593SmuzhiyunRemember the :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` ioctl and the 272*4882a593Smuzhiyunfirst :c:func:`read()`, :c:func:`write()` and 273*4882a593Smuzhiyun:c:func:`select()` call can be resource allocation 274*4882a593Smuzhiyunpoints returning an ``EBUSY`` error code if the required hardware resources 275*4882a593Smuzhiyunare temporarily unavailable, for example the device is already in use by 276*4882a593Smuzhiyunanother process. 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun.. [#f1] 279*4882a593Smuzhiyun ASK: Amplitude-Shift Keying. A high signal level represents a '1' 280*4882a593Smuzhiyun bit, a low level a '0' bit. 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun.. [#f2] 283*4882a593Smuzhiyun A few devices may be unable to sample VBI data at all but can extend 284*4882a593Smuzhiyun the video capture window to the VBI region. 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun.. [#f3] 287*4882a593Smuzhiyun Most VBI services transmit on both fields, but some have different 288*4882a593Smuzhiyun semantics depending on the field number. These cannot be reliable 289*4882a593Smuzhiyun decoded or encoded when ``V4L2_VBI_UNSYNC`` is set. 290*4882a593Smuzhiyun 291*4882a593Smuzhiyun.. [#f4] 292*4882a593Smuzhiyun The valid values ar shown at :ref:`vbi-525` and :ref:`vbi-625`. 293