dev-raw-vbi.rst 12 KB

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