aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/DocBook/v4l/common.xml
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2009-09-13 21:16:04 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2009-09-18 22:47:55 -0400
commit8e080c2e6cadada82a6b520e0c23a1cb974822d5 (patch)
tree991450ff1abba98e5313906478c33816a202ccab /Documentation/DocBook/v4l/common.xml
parentf4e96deb4513d044653027d4921fd7592195503a (diff)
V4L/DVB (12761): DocBook: add media API specs
The V4L and DVB API's are there for a long time. however, up to now, no efforts were done to merge them to kernel DocBook. This patch adds the current versions of the specs as an unique compendium. Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'Documentation/DocBook/v4l/common.xml')
-rw-r--r--Documentation/DocBook/v4l/common.xml1169
1 files changed, 1169 insertions, 0 deletions
diff --git a/Documentation/DocBook/v4l/common.xml b/Documentation/DocBook/v4l/common.xml
new file mode 100644
index 000000000000..dd598ac9a450
--- /dev/null
+++ b/Documentation/DocBook/v4l/common.xml
@@ -0,0 +1,1169 @@
1 <title>Common API Elements</title>
2
3 <para>Programming a V4L2 device consists of these
4steps:</para>
5
6 <itemizedlist>
7 <listitem>
8 <para>Opening the device</para>
9 </listitem>
10 <listitem>
11 <para>Changing device properties, selecting a video and audio
12input, video standard, picture brightness a.&nbsp;o.</para>
13 </listitem>
14 <listitem>
15 <para>Negotiating a data format</para>
16 </listitem>
17 <listitem>
18 <para>Negotiating an input/output method</para>
19 </listitem>
20 <listitem>
21 <para>The actual input/output loop</para>
22 </listitem>
23 <listitem>
24 <para>Closing the device</para>
25 </listitem>
26 </itemizedlist>
27
28 <para>In practice most steps are optional and can be executed out of
29order. It depends on the V4L2 device type, you can read about the
30details in <xref linkend="devices" />. In this chapter we will discuss
31the basic concepts applicable to all devices.</para>
32
33 <section id="open">
34 <title>Opening and Closing Devices</title>
35
36 <section>
37 <title>Device Naming</title>
38
39 <para>V4L2 drivers are implemented as kernel modules, loaded
40manually by the system administrator or automatically when a device is
41first opened. The driver modules plug into the "videodev" kernel
42module. It provides helper functions and a common application
43interface specified in this document.</para>
44
45 <para>Each driver thus loaded registers one or more device nodes
46with major number 81 and a minor number between 0 and 255. Assigning
47minor numbers to V4L2 devices is entirely up to the system administrator,
48this is primarily intended to solve conflicts between devices.<footnote>
49 <para>Access permissions are associated with character
50device special files, hence we must ensure device numbers cannot
51change with the module load order. To this end minor numbers are no
52longer automatically assigned by the "videodev" module as in V4L but
53requested by the driver. The defaults will suffice for most people
54unless two drivers compete for the same minor numbers.</para>
55 </footnote> The module options to select minor numbers are named
56after the device special file with a "_nr" suffix. For example "video_nr"
57for <filename>/dev/video</filename> video capture devices. The number is
58an offset to the base minor number associated with the device type.
59<footnote>
60 <para>In earlier versions of the V4L2 API the module options
61where named after the device special file with a "unit_" prefix, expressing
62the minor number itself, not an offset. Rationale for this change is unknown.
63Lastly the naming and semantics are just a convention among driver writers,
64the point to note is that minor numbers are not supposed to be hardcoded
65into drivers.</para>
66 </footnote> When the driver supports multiple devices of the same
67type more than one minor number can be assigned, separated by commas:
68<informalexample>
69 <screen>
70&gt; insmod mydriver.o video_nr=0,1 radio_nr=0,1</screen>
71 </informalexample></para>
72
73 <para>In <filename>/etc/modules.conf</filename> this may be
74written as: <informalexample>
75 <screen>
76alias char-major-81-0 mydriver
77alias char-major-81-1 mydriver
78alias char-major-81-64 mydriver <co id="alias" />
79options mydriver video_nr=0,1 radio_nr=0,1 <co id="options" />
80 </screen>
81 <calloutlist>
82 <callout arearefs="alias">
83 <para>When an application attempts to open a device
84special file with major number 81 and minor number 0, 1, or 64, load
85"mydriver" (and the "videodev" module it depends upon).</para>
86 </callout>
87 <callout arearefs="options">
88 <para>Register the first two video capture devices with
89minor number 0 and 1 (base number is 0), the first two radio device
90with minor number 64 and 65 (base 64).</para>
91 </callout>
92 </calloutlist>
93 </informalexample> When no minor number is given as module
94option the driver supplies a default. <xref linkend="devices" />
95recommends the base minor numbers to be used for the various device
96types. Obviously minor numbers must be unique. When the number is
97already in use the <emphasis>offending device</emphasis> will not be
98registered. <!-- Blessed by Linus Torvalds on
99linux-kernel@vger.kernel.org, 2002-11-20. --></para>
100
101 <para>By convention system administrators create various
102character device special files with these major and minor numbers in
103the <filename>/dev</filename> directory. The names recomended for the
104different V4L2 device types are listed in <xref linkend="devices" />.
105</para>
106
107 <para>The creation of character special files (with
108<application>mknod</application>) is a privileged operation and
109devices cannot be opened by major and minor number. That means
110applications cannot <emphasis>reliable</emphasis> scan for loaded or
111installed drivers. The user must enter a device name, or the
112application can try the conventional device names.</para>
113
114 <para>Under the device filesystem (devfs) the minor number
115options are ignored. V4L2 drivers (or by proxy the "videodev" module)
116automatically create the required device files in the
117<filename>/dev/v4l</filename> directory using the conventional device
118names above.</para>
119 </section>
120
121 <section id="related">
122 <title>Related Devices</title>
123
124 <para>Devices can support several related functions. For example
125video capturing, video overlay and VBI capturing are related because
126these functions share, amongst other, the same video input and tuner
127frequency. V4L and earlier versions of V4L2 used the same device name
128and minor number for video capturing and overlay, but different ones
129for VBI. Experience showed this approach has several problems<footnote>
130 <para>Given a device file name one cannot reliable find
131related devices. For once names are arbitrary and in a system with
132multiple devices, where only some support VBI capturing, a
133<filename>/dev/video2</filename> is not necessarily related to
134<filename>/dev/vbi2</filename>. The V4L
135<constant>VIDIOCGUNIT</constant> ioctl would require a search for a
136device file with a particular major and minor number.</para>
137 </footnote>, and to make things worse the V4L videodev module
138used to prohibit multiple opens of a device.</para>
139
140 <para>As a remedy the present version of the V4L2 API relaxed the
141concept of device types with specific names and minor numbers. For
142compatibility with old applications drivers must still register different
143minor numbers to assign a default function to the device. But if related
144functions are supported by the driver they must be available under all
145registered minor numbers. The desired function can be selected after
146opening the device as described in <xref linkend="devices" />.</para>
147
148 <para>Imagine a driver supporting video capturing, video
149overlay, raw VBI capturing, and FM radio reception. It registers three
150devices with minor number 0, 64 and 224 (this numbering scheme is
151inherited from the V4L API). Regardless if
152<filename>/dev/video</filename> (81, 0) or
153<filename>/dev/vbi</filename> (81, 224) is opened the application can
154select any one of the video capturing, overlay or VBI capturing
155functions. Without programming (e.&nbsp;g. reading from the device
156with <application>dd</application> or <application>cat</application>)
157<filename>/dev/video</filename> captures video images, while
158<filename>/dev/vbi</filename> captures raw VBI data.
159<filename>/dev/radio</filename> (81, 64) is invariable a radio device,
160unrelated to the video functions. Being unrelated does not imply the
161devices can be used at the same time, however. The &func-open;
162function may very well return an &EBUSY;.</para>
163
164 <para>Besides video input or output the hardware may also
165support audio sampling or playback. If so, these functions are
166implemented as OSS or ALSA PCM devices and eventually OSS or ALSA
167audio mixer. The V4L2 API makes no provisions yet to find these
168related devices. If you have an idea please write to the linux-media
169mailing list: &v4l-ml;.</para>
170 </section>
171
172 <section>
173 <title>Multiple Opens</title>
174
175 <para>In general, V4L2 devices can be opened more than once.
176When this is supported by the driver, users can for example start a
177"panel" application to change controls like brightness or audio
178volume, while another application captures video and audio. In other words, panel
179applications are comparable to an OSS or ALSA audio mixer application.
180When a device supports multiple functions like capturing and overlay
181<emphasis>simultaneously</emphasis>, multiple opens allow concurrent
182use of the device by forked processes or specialized applications.</para>
183
184 <para>Multiple opens are optional, although drivers should
185permit at least concurrent accesses without data exchange, &ie; panel
186applications. This implies &func-open; can return an &EBUSY; when the
187device is already in use, as well as &func-ioctl; functions initiating
188data exchange (namely the &VIDIOC-S-FMT; ioctl), and the &func-read;
189and &func-write; functions.</para>
190
191 <para>Mere opening a V4L2 device does not grant exclusive
192access.<footnote>
193 <para>Drivers could recognize the
194<constant>O_EXCL</constant> open flag. Presently this is not required,
195so applications cannot know if it really works.</para>
196 </footnote> Initiating data exchange however assigns the right
197to read or write the requested type of data, and to change related
198properties, to this file descriptor. Applications can request
199additional access privileges using the priority mechanism described in
200<xref linkend="app-pri" />.</para>
201 </section>
202
203 <section>
204 <title>Shared Data Streams</title>
205
206 <para>V4L2 drivers should not support multiple applications
207reading or writing the same data stream on a device by copying
208buffers, time multiplexing or similar means. This is better handled by
209a proxy application in user space. When the driver supports stream
210sharing anyway it must be implemented transparently. The V4L2 API does
211not specify how conflicts are solved. <!-- For example O_EXCL when the
212application does not want to be preempted, PROT_READ mmapped buffers
213which can be mapped twice, what happens when image formats do not
214match etc.--></para>
215 </section>
216
217 <section>
218 <title>Functions</title>
219
220 <para>To open and close V4L2 devices applications use the
221&func-open; and &func-close; function, respectively. Devices are
222programmed using the &func-ioctl; function as explained in the
223following sections.</para>
224 </section>
225 </section>
226
227 <section id="querycap">
228 <title>Querying Capabilities</title>
229
230 <para>Because V4L2 covers a wide variety of devices not all
231aspects of the API are equally applicable to all types of devices.
232Furthermore devices of the same type have different capabilities and
233this specification permits the omission of a few complicated and less
234important parts of the API.</para>
235
236 <para>The &VIDIOC-QUERYCAP; ioctl is available to check if the kernel
237device is compatible with this specification, and to query the <link
238linkend="devices">functions</link> and <link linkend="io">I/O
239methods</link> supported by the device. Other features can be queried
240by calling the respective ioctl, for example &VIDIOC-ENUMINPUT;
241to learn about the number, types and names of video connectors on the
242device. Although abstraction is a major objective of this API, the
243ioctl also allows driver specific applications to reliable identify
244the driver.</para>
245
246 <para>All V4L2 drivers must support
247<constant>VIDIOC_QUERYCAP</constant>. Applications should always call
248this ioctl after opening the device.</para>
249 </section>
250
251 <section id="app-pri">
252 <title>Application Priority</title>
253
254 <para>When multiple applications share a device it may be
255desirable to assign them different priorities. Contrary to the
256traditional "rm -rf /" school of thought a video recording application
257could for example block other applications from changing video
258controls or switching the current TV channel. Another objective is to
259permit low priority applications working in background, which can be
260preempted by user controlled applications and automatically regain
261control of the device at a later time.</para>
262
263 <para>Since these features cannot be implemented entirely in user
264space V4L2 defines the &VIDIOC-G-PRIORITY; and &VIDIOC-S-PRIORITY;
265ioctls to request and query the access priority associate with a file
266descriptor. Opening a device assigns a medium priority, compatible
267with earlier versions of V4L2 and drivers not supporting these ioctls.
268Applications requiring a different priority will usually call
269<constant>VIDIOC_S_PRIORITY</constant> after verifying the device with
270the &VIDIOC-QUERYCAP; ioctl.</para>
271
272 <para>Ioctls changing driver properties, such as &VIDIOC-S-INPUT;,
273return an &EBUSY; after another application obtained higher priority.
274An event mechanism to notify applications about asynchronous property
275changes has been proposed but not added yet.</para>
276 </section>
277
278 <section id="video">
279 <title>Video Inputs and Outputs</title>
280
281 <para>Video inputs and outputs are physical connectors of a
282device. These can be for example RF connectors (antenna/cable), CVBS
283a.k.a. Composite Video, S-Video or RGB connectors. Only video and VBI
284capture devices have inputs, output devices have outputs, at least one
285each. Radio devices have no video inputs or outputs.</para>
286
287 <para>To learn about the number and attributes of the
288available inputs and outputs applications can enumerate them with the
289&VIDIOC-ENUMINPUT; and &VIDIOC-ENUMOUTPUT; ioctl, respectively. The
290&v4l2-input; returned by the <constant>VIDIOC_ENUMINPUT</constant>
291ioctl also contains signal status information applicable when the
292current video input is queried.</para>
293
294 <para>The &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; ioctl return the
295index of the current video input or output. To select a different
296input or output applications call the &VIDIOC-S-INPUT; and
297&VIDIOC-S-OUTPUT; ioctl. Drivers must implement all the input ioctls
298when the device has one or more inputs, all the output ioctls when the
299device has one or more outputs.</para>
300
301 <!--
302 <figure id=io-tree>
303 <title>Input and output enumeration is the root of most device properties.</title>
304 <mediaobject>
305 <imageobject>
306 <imagedata fileref="links.pdf" format="ps" />
307 </imageobject>
308 <imageobject>
309 <imagedata fileref="links.gif" format="gif" />
310 </imageobject>
311 <textobject>
312 <phrase>Links between various device property structures.</phrase>
313 </textobject>
314 </mediaobject>
315 </figure>
316 -->
317
318 <example>
319 <title>Information about the current video input</title>
320
321 <programlisting>
322&v4l2-input; input;
323int index;
324
325if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;index)) {
326 perror ("VIDIOC_G_INPUT");
327 exit (EXIT_FAILURE);
328}
329
330memset (&amp;input, 0, sizeof (input));
331input.index = index;
332
333if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
334 perror ("VIDIOC_ENUMINPUT");
335 exit (EXIT_FAILURE);
336}
337
338printf ("Current input: %s\n", input.name);
339 </programlisting>
340 </example>
341
342 <example>
343 <title>Switching to the first video input</title>
344
345 <programlisting>
346int index;
347
348index = 0;
349
350if (-1 == ioctl (fd, &VIDIOC-S-INPUT;, &amp;index)) {
351 perror ("VIDIOC_S_INPUT");
352 exit (EXIT_FAILURE);
353}
354 </programlisting>
355 </example>
356 </section>
357
358 <section id="audio">
359 <title>Audio Inputs and Outputs</title>
360
361 <para>Audio inputs and outputs are physical connectors of a
362device. Video capture devices have inputs, output devices have
363outputs, zero or more each. Radio devices have no audio inputs or
364outputs. They have exactly one tuner which in fact
365<emphasis>is</emphasis> an audio source, but this API associates
366tuners with video inputs or outputs only, and radio devices have
367none of these.<footnote>
368 <para>Actually &v4l2-audio; ought to have a
369<structfield>tuner</structfield> field like &v4l2-input;, not only
370making the API more consistent but also permitting radio devices with
371multiple tuners.</para>
372 </footnote> A connector on a TV card to loop back the received
373audio signal to a sound card is not considered an audio output.</para>
374
375 <para>Audio and video inputs and outputs are associated. Selecting
376a video source also selects an audio source. This is most evident when
377the video and audio source is a tuner. Further audio connectors can
378combine with more than one video input or output. Assumed two
379composite video inputs and two audio inputs exist, there may be up to
380four valid combinations. The relation of video and audio connectors
381is defined in the <structfield>audioset</structfield> field of the
382respective &v4l2-input; or &v4l2-output;, where each bit represents
383the index number, starting at zero, of one audio input or output.</para>
384
385 <para>To learn about the number and attributes of the
386available inputs and outputs applications can enumerate them with the
387&VIDIOC-ENUMAUDIO; and &VIDIOC-ENUMAUDOUT; ioctl, respectively. The
388&v4l2-audio; returned by the <constant>VIDIOC_ENUMAUDIO</constant> ioctl
389also contains signal status information applicable when the current
390audio input is queried.</para>
391
392 <para>The &VIDIOC-G-AUDIO; and &VIDIOC-G-AUDOUT; ioctl report
393the current audio input and output, respectively. Note that, unlike
394&VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; these ioctls return a structure
395as <constant>VIDIOC_ENUMAUDIO</constant> and
396<constant>VIDIOC_ENUMAUDOUT</constant> do, not just an index.</para>
397
398 <para>To select an audio input and change its properties
399applications call the &VIDIOC-S-AUDIO; ioctl. To select an audio
400output (which presently has no changeable properties) applications
401call the &VIDIOC-S-AUDOUT; ioctl.</para>
402
403 <para>Drivers must implement all input ioctls when the device
404has one or more inputs, all output ioctls when the device has one
405or more outputs. When the device has any audio inputs or outputs the
406driver must set the <constant>V4L2_CAP_AUDIO</constant> flag in the
407&v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl.</para>
408
409 <example>
410 <title>Information about the current audio input</title>
411
412 <programlisting>
413&v4l2-audio; audio;
414
415memset (&amp;audio, 0, sizeof (audio));
416
417if (-1 == ioctl (fd, &VIDIOC-G-AUDIO;, &amp;audio)) {
418 perror ("VIDIOC_G_AUDIO");
419 exit (EXIT_FAILURE);
420}
421
422printf ("Current input: %s\n", audio.name);
423 </programlisting>
424 </example>
425
426 <example>
427 <title>Switching to the first audio input</title>
428
429 <programlisting>
430&v4l2-audio; audio;
431
432memset (&amp;audio, 0, sizeof (audio)); /* clear audio.mode, audio.reserved */
433
434audio.index = 0;
435
436if (-1 == ioctl (fd, &VIDIOC-S-AUDIO;, &amp;audio)) {
437 perror ("VIDIOC_S_AUDIO");
438 exit (EXIT_FAILURE);
439}
440 </programlisting>
441 </example>
442 </section>
443
444 <section id="tuner">
445 <title>Tuners and Modulators</title>
446
447 <section>
448 <title>Tuners</title>
449
450 <para>Video input devices can have one or more tuners
451demodulating a RF signal. Each tuner is associated with one or more
452video inputs, depending on the number of RF connectors on the tuner.
453The <structfield>type</structfield> field of the respective
454&v4l2-input; returned by the &VIDIOC-ENUMINPUT; ioctl is set to
455<constant>V4L2_INPUT_TYPE_TUNER</constant> and its
456<structfield>tuner</structfield> field contains the index number of
457the tuner.</para>
458
459 <para>Radio devices have exactly one tuner with index zero, no
460video inputs.</para>
461
462 <para>To query and change tuner properties applications use the
463&VIDIOC-G-TUNER; and &VIDIOC-S-TUNER; ioctl, respectively. The
464&v4l2-tuner; returned by <constant>VIDIOC_G_TUNER</constant> also
465contains signal status information applicable when the tuner of the
466current video input, or a radio tuner is queried. Note that
467<constant>VIDIOC_S_TUNER</constant> does not switch the current tuner,
468when there is more than one at all. The tuner is solely determined by
469the current video input. Drivers must support both ioctls and set the
470<constant>V4L2_CAP_TUNER</constant> flag in the &v4l2-capability;
471returned by the &VIDIOC-QUERYCAP; ioctl when the device has one or
472more tuners.</para>
473 </section>
474
475 <section>
476 <title>Modulators</title>
477
478 <para>Video output devices can have one or more modulators, uh,
479modulating a video signal for radiation or connection to the antenna
480input of a TV set or video recorder. Each modulator is associated with
481one or more video outputs, depending on the number of RF connectors on
482the modulator. The <structfield>type</structfield> field of the
483respective &v4l2-output; returned by the &VIDIOC-ENUMOUTPUT; ioctl is
484set to <constant>V4L2_OUTPUT_TYPE_MODULATOR</constant> and its
485<structfield>modulator</structfield> field contains the index number
486of the modulator. This specification does not define radio output
487devices.</para>
488
489 <para>To query and change modulator properties applications use
490the &VIDIOC-G-MODULATOR; and &VIDIOC-S-MODULATOR; ioctl. Note that
491<constant>VIDIOC_S_MODULATOR</constant> does not switch the current
492modulator, when there is more than one at all. The modulator is solely
493determined by the current video output. Drivers must support both
494ioctls and set the <constant>V4L2_CAP_MODULATOR</constant> flag in
495the &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl when the
496device has one or more modulators.</para>
497 </section>
498
499 <section>
500 <title>Radio Frequency</title>
501
502 <para>To get and set the tuner or modulator radio frequency
503applications use the &VIDIOC-G-FREQUENCY; and &VIDIOC-S-FREQUENCY;
504ioctl which both take a pointer to a &v4l2-frequency;. These ioctls
505are used for TV and radio devices alike. Drivers must support both
506ioctls when the tuner or modulator ioctls are supported, or
507when the device is a radio device.</para>
508 </section>
509
510 <section>
511 <title>Satellite Receivers</title>
512
513 <para>To be discussed. See also <!-- private discussions of
514Peter Schlaf, BD & MHS on 23 Jun 1999 ff., subject "v4l2 and satellit" and -->
515proposals by Peter Schlaf, video4linux-list@redhat.com on 23 Oct 2002,
516subject: "Re: [V4L] Re: v4l2 api".</para>
517 </section>
518 </section>
519
520 <section id="standard">
521 <title>Video Standards</title>
522
523 <para>Video devices typically support one or more different video
524standards or variations of standards. Each video input and output may
525support another set of standards. This set is reported by the
526<structfield>std</structfield> field of &v4l2-input; and
527&v4l2-output; returned by the &VIDIOC-ENUMINPUT; and
528&VIDIOC-ENUMOUTPUT; ioctl, respectively.</para>
529
530 <para>V4L2 defines one bit for each analog video standard
531currently in use worldwide, and sets aside bits for driver defined
532standards, &eg; hybrid standards to watch NTSC video tapes on PAL TVs
533and vice versa. Applications can use the predefined bits to select a
534particular standard, although presenting the user a menu of supported
535standards is preferred. To enumerate and query the attributes of the
536supported standards applications use the &VIDIOC-ENUMSTD; ioctl.</para>
537
538 <para>Many of the defined standards are actually just variations
539of a few major standards. The hardware may in fact not distinguish
540between them, or do so internal and switch automatically. Therefore
541enumerated standards also contain sets of one or more standard
542bits.</para>
543
544 <para>Assume a hypothetic tuner capable of demodulating B/PAL,
545G/PAL and I/PAL signals. The first enumerated standard is a set of B
546and G/PAL, switched automatically depending on the selected radio
547frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I"
548choice. Similar a Composite input may collapse standards, enumerating
549"PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".<footnote>
550 <para>Some users are already confused by technical terms PAL,
551NTSC and SECAM. There is no point asking them to distinguish between
552B, G, D, or K when the software or hardware can do that
553automatically.</para>
554 </footnote></para>
555
556 <para>To query and select the standard used by the current video
557input or output applications call the &VIDIOC-G-STD; and
558&VIDIOC-S-STD; ioctl, respectively. The <emphasis>received</emphasis>
559standard can be sensed with the &VIDIOC-QUERYSTD; ioctl. Note parameter of all these ioctls is a pointer to a &v4l2-std-id; type (a standard set), <emphasis>not</emphasis> an index into the standard enumeration.<footnote>
560 <para>An alternative to the current scheme is to use pointers
561to indices as arguments of <constant>VIDIOC_G_STD</constant> and
562<constant>VIDIOC_S_STD</constant>, the &v4l2-input; and
563&v4l2-output; <structfield>std</structfield> field would be a set of
564indices like <structfield>audioset</structfield>.</para>
565 <para>Indices are consistent with the rest of the API
566and identify the standard unambiguously. In the present scheme of
567things an enumerated standard is looked up by &v4l2-std-id;. Now the
568standards supported by the inputs of a device can overlap. Just
569assume the tuner and composite input in the example above both
570exist on a device. An enumeration of "PAL-B/G", "PAL-H/I" suggests
571a choice which does not exist. We cannot merge or omit sets, because
572applications would be unable to find the standards reported by
573<constant>VIDIOC_G_STD</constant>. That leaves separate enumerations
574for each input. Also selecting a standard by &v4l2-std-id; can be
575ambiguous. Advantage of this method is that applications need not
576identify the standard indirectly, after enumerating.</para><para>So in
577summary, the lookup itself is unavoidable. The difference is only
578whether the lookup is necessary to find an enumerated standard or to
579switch to a standard by &v4l2-std-id;.</para>
580 </footnote> Drivers must implement all video standard ioctls
581when the device has one or more video inputs or outputs.</para>
582
583 <para>Special rules apply to USB cameras where the notion of video
584standards makes little sense. More generally any capture device,
585output devices accordingly, which is <itemizedlist>
586 <listitem>
587 <para>incapable of capturing fields or frames at the nominal
588rate of the video standard, or</para>
589 </listitem>
590 <listitem>
591 <para>where <link linkend="buffer">timestamps</link> refer
592to the instant the field or frame was received by the driver, not the
593capture time, or</para>
594 </listitem>
595 <listitem>
596 <para>where <link linkend="buffer">sequence numbers</link>
597refer to the frames received by the driver, not the captured
598frames.</para>
599 </listitem>
600 </itemizedlist> Here the driver shall set the
601<structfield>std</structfield> field of &v4l2-input; and &v4l2-output;
602to zero, the <constant>VIDIOC_G_STD</constant>,
603<constant>VIDIOC_S_STD</constant>,
604<constant>VIDIOC_QUERYSTD</constant> and
605<constant>VIDIOC_ENUMSTD</constant> ioctls shall return the
606&EINVAL;.<footnote>
607 <para>See <xref linkend="buffer" /> for a rationale. Probably
608even USB cameras follow some well known video standard. It might have
609been better to explicitly indicate elsewhere if a device cannot live
610up to normal expectations, instead of this exception.</para>
611 </footnote></para>
612
613 <example>
614 <title>Information about the current video standard</title>
615
616 <programlisting>
617&v4l2-std-id; std_id;
618&v4l2-standard; standard;
619
620if (-1 == ioctl (fd, &VIDIOC-G-STD;, &amp;std_id)) {
621 /* Note when VIDIOC_ENUMSTD always returns EINVAL this
622 is no video device or it falls under the USB exception,
623 and VIDIOC_G_STD returning EINVAL is no error. */
624
625 perror ("VIDIOC_G_STD");
626 exit (EXIT_FAILURE);
627}
628
629memset (&amp;standard, 0, sizeof (standard));
630standard.index = 0;
631
632while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
633 if (standard.id &amp; std_id) {
634 printf ("Current video standard: %s\n", standard.name);
635 exit (EXIT_SUCCESS);
636 }
637
638 standard.index++;
639}
640
641/* EINVAL indicates the end of the enumeration, which cannot be
642 empty unless this device falls under the USB exception. */
643
644if (errno == EINVAL || standard.index == 0) {
645 perror ("VIDIOC_ENUMSTD");
646 exit (EXIT_FAILURE);
647}
648 </programlisting>
649 </example>
650
651 <example>
652 <title>Listing the video standards supported by the current
653input</title>
654
655 <programlisting>
656&v4l2-input; input;
657&v4l2-standard; standard;
658
659memset (&amp;input, 0, sizeof (input));
660
661if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
662 perror ("VIDIOC_G_INPUT");
663 exit (EXIT_FAILURE);
664}
665
666if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
667 perror ("VIDIOC_ENUM_INPUT");
668 exit (EXIT_FAILURE);
669}
670
671printf ("Current input %s supports:\n", input.name);
672
673memset (&amp;standard, 0, sizeof (standard));
674standard.index = 0;
675
676while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
677 if (standard.id &amp; input.std)
678 printf ("%s\n", standard.name);
679
680 standard.index++;
681}
682
683/* EINVAL indicates the end of the enumeration, which cannot be
684 empty unless this device falls under the USB exception. */
685
686if (errno != EINVAL || standard.index == 0) {
687 perror ("VIDIOC_ENUMSTD");
688 exit (EXIT_FAILURE);
689}
690 </programlisting>
691 </example>
692
693 <example>
694 <title>Selecting a new video standard</title>
695
696 <programlisting>
697&v4l2-input; input;
698&v4l2-std-id; std_id;
699
700memset (&amp;input, 0, sizeof (input));
701
702if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
703 perror ("VIDIOC_G_INPUT");
704 exit (EXIT_FAILURE);
705}
706
707if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
708 perror ("VIDIOC_ENUM_INPUT");
709 exit (EXIT_FAILURE);
710}
711
712if (0 == (input.std &amp; V4L2_STD_PAL_BG)) {
713 fprintf (stderr, "Oops. B/G PAL is not supported.\n");
714 exit (EXIT_FAILURE);
715}
716
717/* Note this is also supposed to work when only B
718 <emphasis>or</emphasis> G/PAL is supported. */
719
720std_id = V4L2_STD_PAL_BG;
721
722if (-1 == ioctl (fd, &VIDIOC-S-STD;, &amp;std_id)) {
723 perror ("VIDIOC_S_STD");
724 exit (EXIT_FAILURE);
725}
726 </programlisting>
727 </example>
728 </section>
729
730 &sub-controls;
731
732 <section id="format">
733 <title>Data Formats</title>
734
735 <section>
736 <title>Data Format Negotiation</title>
737
738 <para>Different devices exchange different kinds of data with
739applications, for example video images, raw or sliced VBI data, RDS
740datagrams. Even within one kind many different formats are possible,
741in particular an abundance of image formats. Although drivers must
742provide a default and the selection persists across closing and
743reopening a device, applications should always negotiate a data format
744before engaging in data exchange. Negotiation means the application
745asks for a particular format and the driver selects and reports the
746best the hardware can do to satisfy the request. Of course
747applications can also just query the current selection.</para>
748
749 <para>A single mechanism exists to negotiate all data formats
750using the aggregate &v4l2-format; and the &VIDIOC-G-FMT; and
751&VIDIOC-S-FMT; ioctls. Additionally the &VIDIOC-TRY-FMT; ioctl can be
752used to examine what the hardware <emphasis>could</emphasis> do,
753without actually selecting a new data format. The data formats
754supported by the V4L2 API are covered in the respective device section
755in <xref linkend="devices" />. For a closer look at image formats see
756<xref linkend="pixfmt" />.</para>
757
758 <para>The <constant>VIDIOC_S_FMT</constant> ioctl is a major
759turning-point in the initialization sequence. Prior to this point
760multiple panel applications can access the same device concurrently to
761select the current input, change controls or modify other properties.
762The first <constant>VIDIOC_S_FMT</constant> assigns a logical stream
763(video data, VBI data etc.) exclusively to one file descriptor.</para>
764
765 <para>Exclusive means no other application, more precisely no
766other file descriptor, can grab this stream or change device
767properties inconsistent with the negotiated parameters. A video
768standard change for example, when the new standard uses a different
769number of scan lines, can invalidate the selected image format.
770Therefore only the file descriptor owning the stream can make
771invalidating changes. Accordingly multiple file descriptors which
772grabbed different logical streams prevent each other from interfering
773with their settings. When for example video overlay is about to start
774or already in progress, simultaneous video capturing may be restricted
775to the same cropping and image size.</para>
776
777 <para>When applications omit the
778<constant>VIDIOC_S_FMT</constant> ioctl its locking side effects are
779implied by the next step, the selection of an I/O method with the
780&VIDIOC-REQBUFS; ioctl or implicit with the first &func-read; or
781&func-write; call.</para>
782
783 <para>Generally only one logical stream can be assigned to a
784file descriptor, the exception being drivers permitting simultaneous
785video capturing and overlay using the same file descriptor for
786compatibility with V4L and earlier versions of V4L2. Switching the
787logical stream or returning into "panel mode" is possible by closing
788and reopening the device. Drivers <emphasis>may</emphasis> support a
789switch using <constant>VIDIOC_S_FMT</constant>.</para>
790
791 <para>All drivers exchanging data with
792applications must support the <constant>VIDIOC_G_FMT</constant> and
793<constant>VIDIOC_S_FMT</constant> ioctl. Implementation of the
794<constant>VIDIOC_TRY_FMT</constant> is highly recommended but
795optional.</para>
796 </section>
797
798 <section>
799 <title>Image Format Enumeration</title>
800
801 <para>Apart of the generic format negotiation functions
802a special ioctl to enumerate all image formats supported by video
803capture, overlay or output devices is available.<footnote>
804 <para>Enumerating formats an application has no a-priori
805knowledge of (otherwise it could explicitely ask for them and need not
806enumerate) seems useless, but there are applications serving as proxy
807between drivers and the actual video applications for which this is
808useful.</para>
809 </footnote></para>
810
811 <para>The &VIDIOC-ENUM-FMT; ioctl must be supported
812by all drivers exchanging image data with applications.</para>
813
814 <important>
815 <para>Drivers are not supposed to convert image formats in
816kernel space. They must enumerate only formats directly supported by
817the hardware. If necessary driver writers should publish an example
818conversion routine or library for integration into applications.</para>
819 </important>
820 </section>
821 </section>
822
823 <section id="crop">
824 <title>Image Cropping, Insertion and Scaling</title>
825
826 <para>Some video capture devices can sample a subsection of the
827picture and shrink or enlarge it to an image of arbitrary size. We
828call these abilities cropping and scaling. Some video output devices
829can scale an image up or down and insert it at an arbitrary scan line
830and horizontal offset into a video signal.</para>
831
832 <para>Applications can use the following API to select an area in
833the video signal, query the default area and the hardware limits.
834<emphasis>Despite their name, the &VIDIOC-CROPCAP;, &VIDIOC-G-CROP;
835and &VIDIOC-S-CROP; ioctls apply to input as well as output
836devices.</emphasis></para>
837
838 <para>Scaling requires a source and a target. On a video capture
839or overlay device the source is the video signal, and the cropping
840ioctls determine the area actually sampled. The target are images
841read by the application or overlaid onto the graphics screen. Their
842size (and position for an overlay) is negotiated with the
843&VIDIOC-G-FMT; and &VIDIOC-S-FMT; ioctls.</para>
844
845 <para>On a video output device the source are the images passed in
846by the application, and their size is again negotiated with the
847<constant>VIDIOC_G/S_FMT</constant> ioctls, or may be encoded in a
848compressed video stream. The target is the video signal, and the
849cropping ioctls determine the area where the images are
850inserted.</para>
851
852 <para>Source and target rectangles are defined even if the device
853does not support scaling or the <constant>VIDIOC_G/S_CROP</constant>
854ioctls. Their size (and position where applicable) will be fixed in
855this case. <emphasis>All capture and output device must support the
856<constant>VIDIOC_CROPCAP</constant> ioctl such that applications can
857determine if scaling takes place.</emphasis></para>
858
859 <section>
860 <title>Cropping Structures</title>
861
862 <figure id="crop-scale">
863 <title>Image Cropping, Insertion and Scaling</title>
864 <mediaobject>
865 <imageobject>
866 <imagedata fileref="crop.pdf" format="PS" />
867 </imageobject>
868 <imageobject>
869 <imagedata fileref="crop.gif" format="GIF" />
870 </imageobject>
871 <textobject>
872 <phrase>The cropping, insertion and scaling process</phrase>
873 </textobject>
874 </mediaobject>
875 </figure>
876
877 <para>For capture devices the coordinates of the top left
878corner, width and height of the area which can be sampled is given by
879the <structfield>bounds</structfield> substructure of the
880&v4l2-cropcap; returned by the <constant>VIDIOC_CROPCAP</constant>
881ioctl. To support a wide range of hardware this specification does not
882define an origin or units. However by convention drivers should
883horizontally count unscaled samples relative to 0H (the leading edge
884of the horizontal sync pulse, see <xref linkend="vbi-hsync" />).
885Vertically ITU-R line
886numbers of the first field (<xref linkend="vbi-525" />, <xref
887linkend="vbi-625" />), multiplied by two if the driver can capture both
888fields.</para>
889
890 <para>The top left corner, width and height of the source
891rectangle, that is the area actually sampled, is given by &v4l2-crop;
892using the same coordinate system as &v4l2-cropcap;. Applications can
893use the <constant>VIDIOC_G_CROP</constant> and
894<constant>VIDIOC_S_CROP</constant> ioctls to get and set this
895rectangle. It must lie completely within the capture boundaries and
896the driver may further adjust the requested size and/or position
897according to hardware limitations.</para>
898
899 <para>Each capture device has a default source rectangle, given
900by the <structfield>defrect</structfield> substructure of
901&v4l2-cropcap;. The center of this rectangle shall align with the
902center of the active picture area of the video signal, and cover what
903the driver writer considers the complete picture. Drivers shall reset
904the source rectangle to the default when the driver is first loaded,
905but not later.</para>
906
907 <para>For output devices these structures and ioctls are used
908accordingly, defining the <emphasis>target</emphasis> rectangle where
909the images will be inserted into the video signal.</para>
910
911 </section>
912
913 <section>
914 <title>Scaling Adjustments</title>
915
916 <para>Video hardware can have various cropping, insertion and
917scaling limitations. It may only scale up or down, support only
918discrete scaling factors, or have different scaling abilities in
919horizontal and vertical direction. Also it may not support scaling at
920all. At the same time the &v4l2-crop; rectangle may have to be
921aligned, and both the source and target rectangles may have arbitrary
922upper and lower size limits. In particular the maximum
923<structfield>width</structfield> and <structfield>height</structfield>
924in &v4l2-crop; may be smaller than the
925&v4l2-cropcap;.<structfield>bounds</structfield> area. Therefore, as
926usual, drivers are expected to adjust the requested parameters and
927return the actual values selected.</para>
928
929 <para>Applications can change the source or the target rectangle
930first, as they may prefer a particular image size or a certain area in
931the video signal. If the driver has to adjust both to satisfy hardware
932limitations, the last requested rectangle shall take priority, and the
933driver should preferably adjust the opposite one. The &VIDIOC-TRY-FMT;
934ioctl however shall not change the driver state and therefore only
935adjust the requested rectangle.</para>
936
937 <para>Suppose scaling on a video capture device is restricted to
938a factor 1:1 or 2:1 in either direction and the target image size must
939be a multiple of 16&nbsp;&times;&nbsp;16 pixels. The source cropping
940rectangle is set to defaults, which are also the upper limit in this
941example, of 640&nbsp;&times;&nbsp;400 pixels at offset 0,&nbsp;0. An
942application requests an image size of 300&nbsp;&times;&nbsp;225
943pixels, assuming video will be scaled down from the "full picture"
944accordingly. The driver sets the image size to the closest possible
945values 304&nbsp;&times;&nbsp;224, then chooses the cropping rectangle
946closest to the requested size, that is 608&nbsp;&times;&nbsp;224
947(224&nbsp;&times;&nbsp;2:1 would exceed the limit 400). The offset
9480,&nbsp;0 is still valid, thus unmodified. Given the default cropping
949rectangle reported by <constant>VIDIOC_CROPCAP</constant> the
950application can easily propose another offset to center the cropping
951rectangle.</para>
952
953 <para>Now the application may insist on covering an area using a
954picture aspect ratio closer to the original request, so it asks for a
955cropping rectangle of 608&nbsp;&times;&nbsp;456 pixels. The present
956scaling factors limit cropping to 640&nbsp;&times;&nbsp;384, so the
957driver returns the cropping size 608&nbsp;&times;&nbsp;384 and adjusts
958the image size to closest possible 304&nbsp;&times;&nbsp;192.</para>
959
960 </section>
961
962 <section>
963 <title>Examples</title>
964
965 <para>Source and target rectangles shall remain unchanged across
966closing and reopening a device, such that piping data into or out of a
967device will work without special preparations. More advanced
968applications should ensure the parameters are suitable before starting
969I/O.</para>
970
971 <example>
972 <title>Resetting the cropping parameters</title>
973
974 <para>(A video capture device is assumed; change
975<constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> for other
976devices.)</para>
977
978 <programlisting>
979&v4l2-cropcap; cropcap;
980&v4l2-crop; crop;
981
982memset (&amp;cropcap, 0, sizeof (cropcap));
983cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
984
985if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
986 perror ("VIDIOC_CROPCAP");
987 exit (EXIT_FAILURE);
988}
989
990memset (&amp;crop, 0, sizeof (crop));
991crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
992crop.c = cropcap.defrect;
993
994/* Ignore if cropping is not supported (EINVAL). */
995
996if (-1 == ioctl (fd, &VIDIOC-S-CROP;, &amp;crop)
997 &amp;&amp; errno != EINVAL) {
998 perror ("VIDIOC_S_CROP");
999 exit (EXIT_FAILURE);
1000}
1001 </programlisting>
1002 </example>
1003
1004 <example>
1005 <title>Simple downscaling</title>
1006
1007 <para>(A video capture device is assumed.)</para>
1008
1009 <programlisting>
1010&v4l2-cropcap; cropcap;
1011&v4l2-format; format;
1012
1013reset_cropping_parameters ();
1014
1015/* Scale down to 1/4 size of full picture. */
1016
1017memset (&amp;format, 0, sizeof (format)); /* defaults */
1018
1019format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1020
1021format.fmt.pix.width = cropcap.defrect.width &gt;&gt; 1;
1022format.fmt.pix.height = cropcap.defrect.height &gt;&gt; 1;
1023format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1024
1025if (-1 == ioctl (fd, &VIDIOC-S-FMT;, &amp;format)) {
1026 perror ("VIDIOC_S_FORMAT");
1027 exit (EXIT_FAILURE);
1028}
1029
1030/* We could check the actual image size now, the actual scaling factor
1031 or if the driver can scale at all. */
1032 </programlisting>
1033 </example>
1034
1035 <example>
1036 <title>Selecting an output area</title>
1037
1038 <programlisting>
1039&v4l2-cropcap; cropcap;
1040&v4l2-crop; crop;
1041
1042memset (&amp;cropcap, 0, sizeof (cropcap));
1043cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1044
1045if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &amp;cropcap)) {
1046 perror ("VIDIOC_CROPCAP");
1047 exit (EXIT_FAILURE);
1048}
1049
1050memset (&amp;crop, 0, sizeof (crop));
1051
1052crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1053crop.c = cropcap.defrect;
1054
1055/* Scale the width and height to 50 % of their original size
1056 and center the output. */
1057
1058crop.c.width /= 2;
1059crop.c.height /= 2;
1060crop.c.left += crop.c.width / 2;
1061crop.c.top += crop.c.height / 2;
1062
1063/* Ignore if cropping is not supported (EINVAL). */
1064
1065if (-1 == ioctl (fd, VIDIOC_S_CROP, &amp;crop)
1066 &amp;&amp; errno != EINVAL) {
1067 perror ("VIDIOC_S_CROP");
1068 exit (EXIT_FAILURE);
1069}
1070</programlisting>
1071 </example>
1072
1073 <example>
1074 <title>Current scaling factor and pixel aspect</title>
1075
1076 <para>(A video capture device is assumed.)</para>
1077
1078 <programlisting>
1079&v4l2-cropcap; cropcap;
1080&v4l2-crop; crop;
1081&v4l2-format; format;
1082double hscale, vscale;
1083double aspect;
1084int dwidth, dheight;
1085
1086memset (&amp;cropcap, 0, sizeof (cropcap));
1087cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1088
1089if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
1090 perror ("VIDIOC_CROPCAP");
1091 exit (EXIT_FAILURE);
1092}
1093
1094memset (&amp;crop, 0, sizeof (crop));
1095crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1096
1097if (-1 == ioctl (fd, &VIDIOC-G-CROP;, &amp;crop)) {
1098 if (errno != EINVAL) {
1099 perror ("VIDIOC_G_CROP");
1100 exit (EXIT_FAILURE);
1101 }
1102
1103 /* Cropping not supported. */
1104 crop.c = cropcap.defrect;
1105}
1106
1107memset (&amp;format, 0, sizeof (format));
1108format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1109
1110if (-1 == ioctl (fd, &VIDIOC-G-FMT;, &amp;format)) {
1111 perror ("VIDIOC_G_FMT");
1112 exit (EXIT_FAILURE);
1113}
1114
1115/* The scaling applied by the driver. */
1116
1117hscale = format.fmt.pix.width / (double) crop.c.width;
1118vscale = format.fmt.pix.height / (double) crop.c.height;
1119
1120aspect = cropcap.pixelaspect.numerator /
1121 (double) cropcap.pixelaspect.denominator;
1122aspect = aspect * hscale / vscale;
1123
1124/* Devices following ITU-R BT.601 do not capture
1125 square pixels. For playback on a computer monitor
1126 we should scale the images to this size. */
1127
1128dwidth = format.fmt.pix.width / aspect;
1129dheight = format.fmt.pix.height;
1130 </programlisting>
1131 </example>
1132 </section>
1133 </section>
1134
1135 <section id="streaming-par">
1136 <title>Streaming Parameters</title>
1137
1138 <para>Streaming parameters are intended to optimize the video
1139capture process as well as I/O. Presently applications can request a
1140high quality capture mode with the &VIDIOC-S-PARM; ioctl.</para>
1141
1142 <para>The current video standard determines a nominal number of
1143frames per second. If less than this number of frames is to be
1144captured or output, applications can request frame skipping or
1145duplicating on the driver side. This is especially useful when using
1146the &func-read; or &func-write;, which are not augmented by timestamps
1147or sequence counters, and to avoid unneccessary data copying.</para>
1148
1149 <para>Finally these ioctls can be used to determine the number of
1150buffers used internally by a driver in read/write mode. For
1151implications see the section discussing the &func-read;
1152function.</para>
1153
1154 <para>To get and set the streaming parameters applications call
1155the &VIDIOC-G-PARM; and &VIDIOC-S-PARM; ioctl, respectively. They take
1156a pointer to a &v4l2-streamparm;, which contains a union holding
1157separate parameters for input and output devices.</para>
1158
1159 <para>These ioctls are optional, drivers need not implement
1160them. If so, they return the &EINVAL;.</para>
1161 </section>
1162
1163 <!--
1164Local Variables:
1165mode: sgml
1166sgml-parent-document: "v4l2.sgml"
1167indent-tabs-mode: nil
1168End:
1169 -->