1  <title>Raw VBI Data Interface</title>
2
3  <para>VBI is an abbreviation of Vertical Blanking Interval, a gap
4in the sequence of lines of an analog video signal. During VBI
5no picture information is transmitted, allowing some time while the
6electron beam of a cathode ray tube TV returns to the top of the
7screen. Using an oscilloscope you will find here the vertical
8synchronization pulses and short data packages ASK
9modulated<footnote><para>ASK: Amplitude-Shift Keying. A high signal
10level represents a '1' bit, a low level a '0' bit.</para></footnote>
11onto the video signal. These are transmissions of services such as
12Teletext or Closed Caption.</para>
13
14  <para>Subject of this interface type is raw VBI data, as sampled off
15a video signal, or to be added to a signal for output.
16The data format is similar to uncompressed video images, a number of
17lines times a number of samples per line, we call this a VBI image.</para>
18
19  <para>Conventionally V4L2 VBI devices are accessed through character
20device special files named <filename>/dev/vbi</filename> and
21<filename>/dev/vbi0</filename> to <filename>/dev/vbi31</filename> with
22major number 81 and minor numbers 224 to 255.
23<filename>/dev/vbi</filename> is typically a symbolic link to the
24preferred VBI device. This convention applies to both input and output
25devices.</para>
26
27  <para>To address the problems of finding related video and VBI
28devices VBI capturing and output is also available as device function
29under <filename>/dev/video</filename>. To capture or output raw VBI
30data with these devices applications must call the &VIDIOC-S-FMT;
31ioctl. Accessed as <filename>/dev/vbi</filename>, raw VBI capturing
32or output is the default device function.</para>
33
34    <section>
35      <title>Querying Capabilities</title>
36
37      <para>Devices supporting the raw VBI capturing or output API set
38the <constant>V4L2_CAP_VBI_CAPTURE</constant> or
39<constant>V4L2_CAP_VBI_OUTPUT</constant> flags, respectively, in the
40<structfield>capabilities</structfield> field of &v4l2-capability;
41returned by the &VIDIOC-QUERYCAP; ioctl. At least one of the
42read/write, streaming or asynchronous I/O methods must be
43supported. VBI devices may or may not have a tuner or modulator.</para>
44    </section>
45
46    <section>
47      <title>Supplemental Functions</title>
48
49      <para>VBI devices shall support <link linkend="video">video
50input or output</link>, <link linkend="tuner">tuner or
51modulator</link>, and <link linkend="control">controls</link> ioctls
52as needed. The <link linkend="standard">video standard</link> ioctls provide
53information vital to program a VBI device, therefore must be
54supported.</para>
55    </section>
56
57    <section>
58      <title>Raw VBI Format Negotiation</title>
59
60      <para>Raw VBI sampling abilities can vary, in particular the
61sampling frequency. To properly interpret the data V4L2 specifies an
62ioctl to query the sampling parameters. Moreover, to allow for some
63flexibility applications can also suggest different parameters.</para>
64
65      <para>As usual these parameters are <emphasis>not</emphasis>
66reset at &func-open; time to permit Unix tool chains, programming a
67device and then reading from it as if it was a plain file. Well
68written V4L2 applications should always ensure they really get what
69they want, requesting reasonable parameters and then checking if the
70actual parameters are suitable.</para>
71
72      <para>To query the current raw VBI capture parameters
73applications set the <structfield>type</structfield> field of a
74&v4l2-format; to <constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant> or
75<constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant>, and call the
76&VIDIOC-G-FMT; ioctl with a pointer to this structure. Drivers fill
77the &v4l2-vbi-format; <structfield>vbi</structfield> member of the
78<structfield>fmt</structfield> union.</para>
79
80      <para>To request different parameters applications set the
81<structfield>type</structfield> field of a &v4l2-format; as above and
82initialize all fields of the &v4l2-vbi-format;
83<structfield>vbi</structfield> member of the
84<structfield>fmt</structfield> union, or better just modify the
85results of <constant>VIDIOC_G_FMT</constant>, and call the
86&VIDIOC-S-FMT; ioctl with a pointer to this structure. Drivers return
87an &EINVAL; only when the given parameters are ambiguous, otherwise
88they modify the parameters according to the hardware capabilites and
89return the actual parameters. When the driver allocates resources at
90this point, it may return an &EBUSY; to indicate the returned
91parameters are valid but the required resources are currently not
92available. That may happen for instance when the video and VBI areas
93to capture would overlap, or when the driver supports multiple opens
94and another process already requested VBI capturing or output. Anyway,
95applications must expect other resource allocation points which may
96return <errorcode>EBUSY</errorcode>, at the &VIDIOC-STREAMON; ioctl
97and the first read(), write() and select() call.</para>
98
99      <para>VBI devices must implement both the
100<constant>VIDIOC_G_FMT</constant> and
101<constant>VIDIOC_S_FMT</constant> ioctl, even if
102<constant>VIDIOC_S_FMT</constant> ignores all requests and always
103returns default parameters as <constant>VIDIOC_G_FMT</constant> does.
104<constant>VIDIOC_TRY_FMT</constant> is optional.</para>
105
106      <table pgwide="1" frame="none" id="v4l2-vbi-format">
107	<title>struct <structname>v4l2_vbi_format</structname></title>
108	<tgroup cols="3">
109	  &cs-str;
110	  <tbody valign="top">
111	    <row>
112	      <entry>__u32</entry>
113	      <entry><structfield>sampling_rate</structfield></entry>
114	      <entry>Samples per second, i.&nbsp;e. unit 1 Hz.</entry>
115	    </row>
116	    <row>
117	      <entry>__u32</entry>
118	      <entry><structfield>offset</structfield></entry>
119	      <entry><para>Horizontal offset of the VBI image,
120relative to the leading edge of the line synchronization pulse and
121counted in samples: The first sample in the VBI image will be located
122<structfield>offset</structfield> /
123<structfield>sampling_rate</structfield> seconds following the leading
124edge. See also <xref linkend="vbi-hsync" />.</para></entry>
125	    </row>
126	    <row>
127	      <entry>__u32</entry>
128	      <entry><structfield>samples_per_line</structfield></entry>
129	      <entry></entry>
130	    </row>
131	    <row>
132	      <entry>__u32</entry>
133	      <entry><structfield>sample_format</structfield></entry>
134	      <entry><para>Defines the sample format as in <xref
135linkend="pixfmt" />, a four-character-code.<footnote>
136		    <para>A few devices may be unable to
137sample VBI data at all but can extend the video capture window to the
138VBI region.</para>
139		  </footnote> Usually this is
140<constant>V4L2_PIX_FMT_GREY</constant>, i.&nbsp;e. each sample
141consists of 8 bits with lower values oriented towards the black level.
142Do not assume any other correlation of values with the signal level.
143For example, the MSB does not necessarily indicate if the signal is
144'high' or 'low' because 128 may not be the mean value of the
145signal. Drivers shall not convert the sample format by software.</para></entry>
146	    </row>
147	    <row>
148	      <entry>__u32</entry>
149	      <entry><structfield>start</structfield>[2]</entry>
150	      <entry>This is the scanning system line number
151associated with the first line of the VBI image, of the first and the
152second field respectively. See <xref linkend="vbi-525" /> and
153<xref linkend="vbi-625" /> for valid values.
154The <constant>V4L2_VBI_ITU_525_F1_START</constant>,
155<constant>V4L2_VBI_ITU_525_F2_START</constant>,
156<constant>V4L2_VBI_ITU_625_F1_START</constant> and
157<constant>V4L2_VBI_ITU_625_F2_START</constant> defines give the start line
158numbers for each field for each 525 or 625 line format as a convenience.
159Don't forget that ITU line numbering starts at 1, not 0.
160VBI input drivers can return start values 0 if the hardware cannot
161reliable identify scanning lines, VBI acquisition may not require this
162information.</entry>
163	    </row>
164	    <row>
165	      <entry>__u32</entry>
166	      <entry><structfield>count</structfield>[2]</entry>
167	      <entry>The number of lines in the first and second
168field image, respectively.</entry>
169	  </row>
170	  <row>
171	    <entry spanname="hspan"><para>Drivers should be as
172flexibility as possible. For example, it may be possible to extend or
173move the VBI capture window down to the picture area, implementing a
174'full field mode' to capture data service transmissions embedded in
175the picture.</para><para>An application can set the first or second
176<structfield>count</structfield> value to zero if no data is required
177from the respective field; <structfield>count</structfield>[1] if the
178scanning system is progressive, &ie; not interlaced. The
179corresponding start value shall be ignored by the application and
180driver. Anyway, drivers may not support single field capturing and
181return both count values non-zero.</para><para>Both
182<structfield>count</structfield> values set to zero, or line numbers
183outside the bounds depicted in <xref linkend="vbi-525" /> and <xref
184		    linkend="vbi-625" />, or a field image covering
185lines of two fields, are invalid and shall not be returned by the
186driver.</para><para>To initialize the <structfield>start</structfield>
187and <structfield>count</structfield> fields, applications must first
188determine the current video standard selection. The &v4l2-std-id; or
189the <structfield>framelines</structfield> field of &v4l2-standard; can
190be evaluated for this purpose.</para></entry>
191	    </row>
192	    <row>
193	      <entry>__u32</entry>
194	      <entry><structfield>flags</structfield></entry>
195	      <entry>See <xref linkend="vbifmt-flags" /> below. Currently
196only drivers set flags, applications must set this field to
197zero.</entry>
198	    </row>
199	    <row>
200	      <entry>__u32</entry>
201	      <entry><structfield>reserved</structfield>[2]</entry>
202	      <entry>This array is reserved for future extensions.
203Drivers and applications must set it to zero.</entry>
204	    </row>
205	  </tbody>
206	</tgroup>
207      </table>
208
209      <table pgwide="1" frame="none" id="vbifmt-flags">
210	<title>Raw VBI Format Flags</title>
211	<tgroup cols="3">
212	  &cs-def;
213	  <tbody valign="top">
214	    <row>
215	      <entry><constant>V4L2_VBI_UNSYNC</constant></entry>
216	      <entry>0x0001</entry>
217	      <entry><para>This flag indicates hardware which does not
218properly distinguish between fields. Normally the VBI image stores the
219first field (lower scanning line numbers) first in memory. This may be
220a top or bottom field depending on the video standard. When this flag
221is set the first or second field may be stored first, however the
222fields are still in correct temporal order with the older field first
223in memory.<footnote>
224		  <para>Most VBI services transmit on both fields, but
225some have different semantics depending on the field number. These
226cannot be reliable decoded or encoded when
227<constant>V4L2_VBI_UNSYNC</constant> is set.</para>
228		</footnote></para></entry>
229	    </row>
230	    <row>
231	      <entry><constant>V4L2_VBI_INTERLACED</constant></entry>
232	      <entry>0x0002</entry>
233	      <entry>By default the two field images will be passed
234sequentially; all lines of the first field followed by all lines of
235the second field (compare <xref linkend="field-order" />
236<constant>V4L2_FIELD_SEQ_TB</constant> and
237<constant>V4L2_FIELD_SEQ_BT</constant>, whether the top or bottom
238field is first in memory depends on the video standard). When this
239flag is set, the two fields are interlaced (cf.
240<constant>V4L2_FIELD_INTERLACED</constant>). The first line of the
241first field followed by the first line of the second field, then the
242two second lines, and so on. Such a layout may be necessary when the
243hardware has been programmed to capture or output interlaced video
244images and is unable to separate the fields for VBI capturing at
245the same time. For simplicity setting this flag implies that both
246<structfield>count</structfield> values are equal and non-zero.</entry>
247	    </row>
248	  </tbody>
249	</tgroup>
250      </table>
251
252      <figure id="vbi-hsync">
253	<title>Line synchronization</title>
254	<mediaobject>
255	  <imageobject>
256	    <imagedata fileref="vbi_hsync.pdf" format="PS" />
257	  </imageobject>
258	  <imageobject>
259	    <imagedata fileref="vbi_hsync.gif" format="GIF" />
260	  </imageobject>
261	  <textobject>
262	    <phrase>Line synchronization diagram</phrase>
263	  </textobject>
264	</mediaobject>
265      </figure>
266
267      <figure id="vbi-525">
268	<title>ITU-R 525 line numbering (M/NTSC and M/PAL)</title>
269	<mediaobject>
270	  <imageobject>
271	    <imagedata fileref="vbi_525.pdf" format="PS" />
272	  </imageobject>
273	  <imageobject>
274	    <imagedata fileref="vbi_525.gif" format="GIF" />
275	  </imageobject>
276	  <textobject>
277	    <phrase>NTSC field synchronization diagram</phrase>
278	  </textobject>
279	  <caption>
280	    <para>(1) For the purpose of this specification field 2
281starts in line 264 and not 263.5 because half line capturing is not
282supported.</para>
283	  </caption>
284	</mediaobject>
285      </figure>
286
287      <figure id="vbi-625">
288	<title>ITU-R 625 line numbering</title>
289	<mediaobject>
290	  <imageobject>
291	    <imagedata fileref="vbi_625.pdf" format="PS" />
292	  </imageobject>
293	  <imageobject>
294	    <imagedata fileref="vbi_625.gif" format="GIF" />
295	  </imageobject>
296	  <textobject>
297	    <phrase>PAL/SECAM field synchronization diagram</phrase>
298	  </textobject>
299	  <caption>
300	    <para>(1) For the purpose of this specification field 2
301starts in line 314 and not 313.5 because half line capturing is not
302supported.</para>
303	  </caption>
304	</mediaobject>
305      </figure>
306
307      <para>Remember the VBI image format depends on the selected
308video standard, therefore the application must choose a new standard or
309query the current standard first. Attempts to read or write data ahead
310of format negotiation, or after switching the video standard which may
311invalidate the negotiated VBI parameters, should be refused by the
312driver. A format change during active I/O is not permitted.</para>
313    </section>
314
315    <section>
316      <title>Reading and writing VBI images</title>
317
318      <para>To assure synchronization with the field number and easier
319implementation, the smallest unit of data passed at a time is one
320frame, consisting of two fields of VBI images immediately following in
321memory.</para>
322
323      <para>The total size of a frame computes as follows:</para>
324
325      <programlisting>
326(<structfield>count</structfield>[0] + <structfield>count</structfield>[1]) *
327<structfield>samples_per_line</structfield> * sample size in bytes</programlisting>
328
329      <para>The sample size is most likely always one byte,
330applications must check the <structfield>sample_format</structfield>
331field though, to function properly with other drivers.</para>
332
333      <para>A VBI device may support <link
334      linkend="rw">read/write</link> and/or streaming (<link
335      linkend="mmap">memory mapping</link> or <link
336      linkend="userp">user pointer</link>) I/O. The latter bears the
337possibility of synchronizing video and
338VBI data by using buffer timestamps.</para>
339
340      <para>Remember the &VIDIOC-STREAMON; ioctl and the first read(),
341write() and select() call can be resource allocation points returning
342an &EBUSY; if the required hardware resources are temporarily
343unavailable, for example the device is already in use by another
344process.</para>
345  </section>
346