dev-raw-vbi.rst 12 KB

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