blob: 81b7cf384a398ac3fd44bea1a769162e9aa51a09 [file] [log] [blame]
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -03001 <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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300103the <filename>/dev</filename> directory. The names recommended for the
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300104different 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
Mauro Carvalho Chehabc20eb182011-06-25 14:11:52 -0300239methods</link> supported by the device.</para>
240
241 <para>Starting with kernel version 3.1, VIDIOC-QUERYCAP will return the
242V4L2 API version used by the driver, with generally matches the Kernel version.
243There's no need of using &VIDIOC-QUERYCAP; to check if an specific ioctl is
244supported, the V4L2 core now returns ENOIOCTLCMD if a driver doesn't provide
245support for an ioctl.</para>
246
247 <para>Other features can be queried
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300248by calling the respective ioctl, for example &VIDIOC-ENUMINPUT;
249to learn about the number, types and names of video connectors on the
250device. Although abstraction is a major objective of this API, the
251ioctl also allows driver specific applications to reliable identify
252the driver.</para>
253
254 <para>All V4L2 drivers must support
255<constant>VIDIOC_QUERYCAP</constant>. Applications should always call
256this ioctl after opening the device.</para>
257 </section>
258
259 <section id="app-pri">
260 <title>Application Priority</title>
261
262 <para>When multiple applications share a device it may be
263desirable to assign them different priorities. Contrary to the
264traditional "rm -rf /" school of thought a video recording application
265could for example block other applications from changing video
266controls or switching the current TV channel. Another objective is to
267permit low priority applications working in background, which can be
268preempted by user controlled applications and automatically regain
269control of the device at a later time.</para>
270
271 <para>Since these features cannot be implemented entirely in user
272space V4L2 defines the &VIDIOC-G-PRIORITY; and &VIDIOC-S-PRIORITY;
273ioctls to request and query the access priority associate with a file
274descriptor. Opening a device assigns a medium priority, compatible
275with earlier versions of V4L2 and drivers not supporting these ioctls.
276Applications requiring a different priority will usually call
277<constant>VIDIOC_S_PRIORITY</constant> after verifying the device with
278the &VIDIOC-QUERYCAP; ioctl.</para>
279
280 <para>Ioctls changing driver properties, such as &VIDIOC-S-INPUT;,
281return an &EBUSY; after another application obtained higher priority.
282An event mechanism to notify applications about asynchronous property
283changes has been proposed but not added yet.</para>
284 </section>
285
286 <section id="video">
287 <title>Video Inputs and Outputs</title>
288
289 <para>Video inputs and outputs are physical connectors of a
290device. These can be for example RF connectors (antenna/cable), CVBS
291a.k.a. Composite Video, S-Video or RGB connectors. Only video and VBI
292capture devices have inputs, output devices have outputs, at least one
293each. Radio devices have no video inputs or outputs.</para>
294
295 <para>To learn about the number and attributes of the
296available inputs and outputs applications can enumerate them with the
297&VIDIOC-ENUMINPUT; and &VIDIOC-ENUMOUTPUT; ioctl, respectively. The
298&v4l2-input; returned by the <constant>VIDIOC_ENUMINPUT</constant>
299ioctl also contains signal status information applicable when the
300current video input is queried.</para>
301
302 <para>The &VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; ioctl return the
303index of the current video input or output. To select a different
304input or output applications call the &VIDIOC-S-INPUT; and
305&VIDIOC-S-OUTPUT; ioctl. Drivers must implement all the input ioctls
306when the device has one or more inputs, all the output ioctls when the
307device has one or more outputs.</para>
308
309 <!--
310 <figure id=io-tree>
311 <title>Input and output enumeration is the root of most device properties.</title>
312 <mediaobject>
313 <imageobject>
314 <imagedata fileref="links.pdf" format="ps" />
315 </imageobject>
316 <imageobject>
317 <imagedata fileref="links.gif" format="gif" />
318 </imageobject>
319 <textobject>
320 <phrase>Links between various device property structures.</phrase>
321 </textobject>
322 </mediaobject>
323 </figure>
324 -->
325
326 <example>
327 <title>Information about the current video input</title>
328
329 <programlisting>
330&v4l2-input; input;
331int index;
332
333if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;index)) {
334 perror ("VIDIOC_G_INPUT");
335 exit (EXIT_FAILURE);
336}
337
338memset (&amp;input, 0, sizeof (input));
339input.index = index;
340
341if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
342 perror ("VIDIOC_ENUMINPUT");
343 exit (EXIT_FAILURE);
344}
345
346printf ("Current input: %s\n", input.name);
347 </programlisting>
348 </example>
349
350 <example>
351 <title>Switching to the first video input</title>
352
353 <programlisting>
354int index;
355
356index = 0;
357
358if (-1 == ioctl (fd, &VIDIOC-S-INPUT;, &amp;index)) {
359 perror ("VIDIOC_S_INPUT");
360 exit (EXIT_FAILURE);
361}
362 </programlisting>
363 </example>
364 </section>
365
366 <section id="audio">
367 <title>Audio Inputs and Outputs</title>
368
369 <para>Audio inputs and outputs are physical connectors of a
370device. Video capture devices have inputs, output devices have
371outputs, zero or more each. Radio devices have no audio inputs or
372outputs. They have exactly one tuner which in fact
373<emphasis>is</emphasis> an audio source, but this API associates
374tuners with video inputs or outputs only, and radio devices have
375none of these.<footnote>
376 <para>Actually &v4l2-audio; ought to have a
377<structfield>tuner</structfield> field like &v4l2-input;, not only
378making the API more consistent but also permitting radio devices with
379multiple tuners.</para>
380 </footnote> A connector on a TV card to loop back the received
381audio signal to a sound card is not considered an audio output.</para>
382
383 <para>Audio and video inputs and outputs are associated. Selecting
384a video source also selects an audio source. This is most evident when
385the video and audio source is a tuner. Further audio connectors can
386combine with more than one video input or output. Assumed two
387composite video inputs and two audio inputs exist, there may be up to
388four valid combinations. The relation of video and audio connectors
389is defined in the <structfield>audioset</structfield> field of the
390respective &v4l2-input; or &v4l2-output;, where each bit represents
391the index number, starting at zero, of one audio input or output.</para>
392
393 <para>To learn about the number and attributes of the
394available inputs and outputs applications can enumerate them with the
395&VIDIOC-ENUMAUDIO; and &VIDIOC-ENUMAUDOUT; ioctl, respectively. The
396&v4l2-audio; returned by the <constant>VIDIOC_ENUMAUDIO</constant> ioctl
397also contains signal status information applicable when the current
398audio input is queried.</para>
399
400 <para>The &VIDIOC-G-AUDIO; and &VIDIOC-G-AUDOUT; ioctl report
401the current audio input and output, respectively. Note that, unlike
402&VIDIOC-G-INPUT; and &VIDIOC-G-OUTPUT; these ioctls return a structure
403as <constant>VIDIOC_ENUMAUDIO</constant> and
404<constant>VIDIOC_ENUMAUDOUT</constant> do, not just an index.</para>
405
406 <para>To select an audio input and change its properties
407applications call the &VIDIOC-S-AUDIO; ioctl. To select an audio
408output (which presently has no changeable properties) applications
409call the &VIDIOC-S-AUDOUT; ioctl.</para>
410
411 <para>Drivers must implement all input ioctls when the device
412has one or more inputs, all output ioctls when the device has one
413or more outputs. When the device has any audio inputs or outputs the
414driver must set the <constant>V4L2_CAP_AUDIO</constant> flag in the
415&v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl.</para>
416
417 <example>
418 <title>Information about the current audio input</title>
419
420 <programlisting>
421&v4l2-audio; audio;
422
423memset (&amp;audio, 0, sizeof (audio));
424
425if (-1 == ioctl (fd, &VIDIOC-G-AUDIO;, &amp;audio)) {
426 perror ("VIDIOC_G_AUDIO");
427 exit (EXIT_FAILURE);
428}
429
430printf ("Current input: %s\n", audio.name);
431 </programlisting>
432 </example>
433
434 <example>
435 <title>Switching to the first audio input</title>
436
437 <programlisting>
438&v4l2-audio; audio;
439
440memset (&amp;audio, 0, sizeof (audio)); /* clear audio.mode, audio.reserved */
441
442audio.index = 0;
443
444if (-1 == ioctl (fd, &VIDIOC-S-AUDIO;, &amp;audio)) {
445 perror ("VIDIOC_S_AUDIO");
446 exit (EXIT_FAILURE);
447}
448 </programlisting>
449 </example>
450 </section>
451
452 <section id="tuner">
453 <title>Tuners and Modulators</title>
454
455 <section>
456 <title>Tuners</title>
457
458 <para>Video input devices can have one or more tuners
459demodulating a RF signal. Each tuner is associated with one or more
460video inputs, depending on the number of RF connectors on the tuner.
461The <structfield>type</structfield> field of the respective
462&v4l2-input; returned by the &VIDIOC-ENUMINPUT; ioctl is set to
463<constant>V4L2_INPUT_TYPE_TUNER</constant> and its
464<structfield>tuner</structfield> field contains the index number of
465the tuner.</para>
466
467 <para>Radio devices have exactly one tuner with index zero, no
468video inputs.</para>
469
470 <para>To query and change tuner properties applications use the
471&VIDIOC-G-TUNER; and &VIDIOC-S-TUNER; ioctl, respectively. The
472&v4l2-tuner; returned by <constant>VIDIOC_G_TUNER</constant> also
473contains signal status information applicable when the tuner of the
474current video input, or a radio tuner is queried. Note that
475<constant>VIDIOC_S_TUNER</constant> does not switch the current tuner,
476when there is more than one at all. The tuner is solely determined by
477the current video input. Drivers must support both ioctls and set the
478<constant>V4L2_CAP_TUNER</constant> flag in the &v4l2-capability;
479returned by the &VIDIOC-QUERYCAP; ioctl when the device has one or
480more tuners.</para>
481 </section>
482
483 <section>
484 <title>Modulators</title>
485
486 <para>Video output devices can have one or more modulators, uh,
487modulating a video signal for radiation or connection to the antenna
488input of a TV set or video recorder. Each modulator is associated with
489one or more video outputs, depending on the number of RF connectors on
490the modulator. The <structfield>type</structfield> field of the
491respective &v4l2-output; returned by the &VIDIOC-ENUMOUTPUT; ioctl is
492set to <constant>V4L2_OUTPUT_TYPE_MODULATOR</constant> and its
493<structfield>modulator</structfield> field contains the index number
494of the modulator. This specification does not define radio output
495devices.</para>
496
497 <para>To query and change modulator properties applications use
498the &VIDIOC-G-MODULATOR; and &VIDIOC-S-MODULATOR; ioctl. Note that
499<constant>VIDIOC_S_MODULATOR</constant> does not switch the current
500modulator, when there is more than one at all. The modulator is solely
501determined by the current video output. Drivers must support both
502ioctls and set the <constant>V4L2_CAP_MODULATOR</constant> flag in
503the &v4l2-capability; returned by the &VIDIOC-QUERYCAP; ioctl when the
504device has one or more modulators.</para>
505 </section>
506
507 <section>
508 <title>Radio Frequency</title>
509
510 <para>To get and set the tuner or modulator radio frequency
511applications use the &VIDIOC-G-FREQUENCY; and &VIDIOC-S-FREQUENCY;
512ioctl which both take a pointer to a &v4l2-frequency;. These ioctls
513are used for TV and radio devices alike. Drivers must support both
514ioctls when the tuner or modulator ioctls are supported, or
515when the device is a radio device.</para>
516 </section>
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300517 </section>
518
519 <section id="standard">
520 <title>Video Standards</title>
521
522 <para>Video devices typically support one or more different video
523standards or variations of standards. Each video input and output may
524support another set of standards. This set is reported by the
525<structfield>std</structfield> field of &v4l2-input; and
526&v4l2-output; returned by the &VIDIOC-ENUMINPUT; and
527&VIDIOC-ENUMOUTPUT; ioctl, respectively.</para>
528
529 <para>V4L2 defines one bit for each analog video standard
530currently in use worldwide, and sets aside bits for driver defined
531standards, &eg; hybrid standards to watch NTSC video tapes on PAL TVs
532and vice versa. Applications can use the predefined bits to select a
533particular standard, although presenting the user a menu of supported
534standards is preferred. To enumerate and query the attributes of the
535supported standards applications use the &VIDIOC-ENUMSTD; ioctl.</para>
536
537 <para>Many of the defined standards are actually just variations
538of a few major standards. The hardware may in fact not distinguish
539between them, or do so internal and switch automatically. Therefore
540enumerated standards also contain sets of one or more standard
541bits.</para>
542
543 <para>Assume a hypothetic tuner capable of demodulating B/PAL,
544G/PAL and I/PAL signals. The first enumerated standard is a set of B
545and G/PAL, switched automatically depending on the selected radio
546frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I"
547choice. Similar a Composite input may collapse standards, enumerating
548"PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".<footnote>
549 <para>Some users are already confused by technical terms PAL,
550NTSC and SECAM. There is no point asking them to distinguish between
551B, G, D, or K when the software or hardware can do that
552automatically.</para>
553 </footnote></para>
554
555 <para>To query and select the standard used by the current video
556input or output applications call the &VIDIOC-G-STD; and
557&VIDIOC-S-STD; ioctl, respectively. The <emphasis>received</emphasis>
558standard 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>
559 <para>An alternative to the current scheme is to use pointers
560to indices as arguments of <constant>VIDIOC_G_STD</constant> and
561<constant>VIDIOC_S_STD</constant>, the &v4l2-input; and
562&v4l2-output; <structfield>std</structfield> field would be a set of
563indices like <structfield>audioset</structfield>.</para>
564 <para>Indices are consistent with the rest of the API
565and identify the standard unambiguously. In the present scheme of
566things an enumerated standard is looked up by &v4l2-std-id;. Now the
567standards supported by the inputs of a device can overlap. Just
568assume the tuner and composite input in the example above both
569exist on a device. An enumeration of "PAL-B/G", "PAL-H/I" suggests
570a choice which does not exist. We cannot merge or omit sets, because
571applications would be unable to find the standards reported by
572<constant>VIDIOC_G_STD</constant>. That leaves separate enumerations
573for each input. Also selecting a standard by &v4l2-std-id; can be
574ambiguous. Advantage of this method is that applications need not
575identify the standard indirectly, after enumerating.</para><para>So in
576summary, the lookup itself is unavoidable. The difference is only
577whether the lookup is necessary to find an enumerated standard or to
578switch to a standard by &v4l2-std-id;.</para>
579 </footnote> Drivers must implement all video standard ioctls
580when the device has one or more video inputs or outputs.</para>
581
582 <para>Special rules apply to USB cameras where the notion of video
583standards makes little sense. More generally any capture device,
584output devices accordingly, which is <itemizedlist>
585 <listitem>
586 <para>incapable of capturing fields or frames at the nominal
587rate of the video standard, or</para>
588 </listitem>
589 <listitem>
590 <para>where <link linkend="buffer">timestamps</link> refer
591to the instant the field or frame was received by the driver, not the
592capture time, or</para>
593 </listitem>
594 <listitem>
595 <para>where <link linkend="buffer">sequence numbers</link>
596refer to the frames received by the driver, not the captured
597frames.</para>
598 </listitem>
599 </itemizedlist> Here the driver shall set the
600<structfield>std</structfield> field of &v4l2-input; and &v4l2-output;
601to zero, the <constant>VIDIOC_G_STD</constant>,
602<constant>VIDIOC_S_STD</constant>,
603<constant>VIDIOC_QUERYSTD</constant> and
604<constant>VIDIOC_ENUMSTD</constant> ioctls shall return the
605&EINVAL;.<footnote>
606 <para>See <xref linkend="buffer" /> for a rationale. Probably
607even USB cameras follow some well known video standard. It might have
608been better to explicitly indicate elsewhere if a device cannot live
609up to normal expectations, instead of this exception.</para>
610 </footnote></para>
611
612 <example>
613 <title>Information about the current video standard</title>
614
615 <programlisting>
616&v4l2-std-id; std_id;
617&v4l2-standard; standard;
618
619if (-1 == ioctl (fd, &VIDIOC-G-STD;, &amp;std_id)) {
620 /* Note when VIDIOC_ENUMSTD always returns EINVAL this
621 is no video device or it falls under the USB exception,
622 and VIDIOC_G_STD returning EINVAL is no error. */
623
624 perror ("VIDIOC_G_STD");
625 exit (EXIT_FAILURE);
626}
627
628memset (&amp;standard, 0, sizeof (standard));
629standard.index = 0;
630
631while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
632 if (standard.id &amp; std_id) {
633 printf ("Current video standard: %s\n", standard.name);
634 exit (EXIT_SUCCESS);
635 }
636
637 standard.index++;
638}
639
640/* EINVAL indicates the end of the enumeration, which cannot be
641 empty unless this device falls under the USB exception. */
642
643if (errno == EINVAL || standard.index == 0) {
644 perror ("VIDIOC_ENUMSTD");
645 exit (EXIT_FAILURE);
646}
647 </programlisting>
648 </example>
649
650 <example>
651 <title>Listing the video standards supported by the current
652input</title>
653
654 <programlisting>
655&v4l2-input; input;
656&v4l2-standard; standard;
657
658memset (&amp;input, 0, sizeof (input));
659
660if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
661 perror ("VIDIOC_G_INPUT");
662 exit (EXIT_FAILURE);
663}
664
665if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
666 perror ("VIDIOC_ENUM_INPUT");
667 exit (EXIT_FAILURE);
668}
669
670printf ("Current input %s supports:\n", input.name);
671
672memset (&amp;standard, 0, sizeof (standard));
673standard.index = 0;
674
675while (0 == ioctl (fd, &VIDIOC-ENUMSTD;, &amp;standard)) {
676 if (standard.id &amp; input.std)
677 printf ("%s\n", standard.name);
678
679 standard.index++;
680}
681
682/* EINVAL indicates the end of the enumeration, which cannot be
683 empty unless this device falls under the USB exception. */
684
685if (errno != EINVAL || standard.index == 0) {
686 perror ("VIDIOC_ENUMSTD");
687 exit (EXIT_FAILURE);
688}
689 </programlisting>
690 </example>
691
692 <example>
693 <title>Selecting a new video standard</title>
694
695 <programlisting>
696&v4l2-input; input;
697&v4l2-std-id; std_id;
698
699memset (&amp;input, 0, sizeof (input));
700
701if (-1 == ioctl (fd, &VIDIOC-G-INPUT;, &amp;input.index)) {
702 perror ("VIDIOC_G_INPUT");
703 exit (EXIT_FAILURE);
704}
705
706if (-1 == ioctl (fd, &VIDIOC-ENUMINPUT;, &amp;input)) {
707 perror ("VIDIOC_ENUM_INPUT");
708 exit (EXIT_FAILURE);
709}
710
711if (0 == (input.std &amp; V4L2_STD_PAL_BG)) {
712 fprintf (stderr, "Oops. B/G PAL is not supported.\n");
713 exit (EXIT_FAILURE);
714}
715
716/* Note this is also supposed to work when only B
717 <emphasis>or</emphasis> G/PAL is supported. */
718
719std_id = V4L2_STD_PAL_BG;
720
721if (-1 == ioctl (fd, &VIDIOC-S-STD;, &amp;std_id)) {
722 perror ("VIDIOC_S_STD");
723 exit (EXIT_FAILURE);
724}
725 </programlisting>
726 </example>
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300727 </section>
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300728 <section id="dv-timings">
729 <title>Digital Video (DV) Timings</title>
730 <para>
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300731 The video standards discussed so far have been dealing with Analog TV and the
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300732corresponding video timings. Today there are many more different hardware interfaces
733such as High Definition TV interfaces (HDMI), VGA, DVI connectors etc., that carry
734video signals and there is a need to extend the API to select the video timings
735for these interfaces. Since it is not possible to extend the &v4l2-std-id; due to
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300736the limited bits available, a new set of IOCTLs was added to set/get video timings at
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300737the input and output: </para><itemizedlist>
738 <listitem>
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300739 <para>DV Timings: This will allow applications to define detailed
740video timings for the interface. This includes parameters such as width, height,
741polarities, frontporch, backporch etc. The <filename>linux/v4l2-dv-timings.h</filename>
742header can be used to get the timings of the formats in the <xref linkend="cea861" /> and
743<xref linkend="vesadmt" /> standards.
744 </para>
745 </listitem>
746 <listitem>
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300747 <para>DV Presets: Digital Video (DV) presets. These are IDs representing a
748video timing at the input/output. Presets are pre-defined timings implemented
749by the hardware according to video standards. A __u32 data type is used to represent
750a preset unlike the bit mask that is used in &v4l2-std-id; allowing future extensions
751to support as many different presets as needed.</para>
752 </listitem>
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300753 </itemizedlist>
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300754 <para>To enumerate and query the attributes of the DV timings supported by a device,
755 applications use the &VIDIOC-ENUM-DV-TIMINGS; and &VIDIOC-DV-TIMINGS-CAP; ioctls.
756 To set DV timings for the device, applications use the
757&VIDIOC-S-DV-TIMINGS; ioctl and to get current DV timings they use the
758&VIDIOC-G-DV-TIMINGS; ioctl. To detect the DV timings as seen by the video receiver applications
759use the &VIDIOC-QUERY-DV-TIMINGS; ioctl.</para>
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300760 <para>To enumerate and query the attributes of DV presets supported by a device,
761applications use the &VIDIOC-ENUM-DV-PRESETS; ioctl. To get the current DV preset,
762applications use the &VIDIOC-G-DV-PRESET; ioctl and to set a preset they use the
Hans Verkuil7dcc6062012-05-15 08:04:28 -0300763&VIDIOC-S-DV-PRESET; ioctl. To detect the preset as seen by the video receiver applications
764use the &VIDIOC-QUERY-DV-PRESET; ioctl.</para>
Muralidharan Karicheri007701e2009-12-03 01:13:17 -0300765 <para>Applications can make use of the <xref linkend="input-capabilities" /> and
766<xref linkend="output-capabilities"/> flags to decide what ioctls are available to set the
767video timings for the device.</para>
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300768 </section>
769
770 &sub-controls;
771
772 <section id="format">
773 <title>Data Formats</title>
774
775 <section>
776 <title>Data Format Negotiation</title>
777
778 <para>Different devices exchange different kinds of data with
779applications, for example video images, raw or sliced VBI data, RDS
780datagrams. Even within one kind many different formats are possible,
781in particular an abundance of image formats. Although drivers must
782provide a default and the selection persists across closing and
783reopening a device, applications should always negotiate a data format
784before engaging in data exchange. Negotiation means the application
785asks for a particular format and the driver selects and reports the
786best the hardware can do to satisfy the request. Of course
787applications can also just query the current selection.</para>
788
789 <para>A single mechanism exists to negotiate all data formats
790using the aggregate &v4l2-format; and the &VIDIOC-G-FMT; and
791&VIDIOC-S-FMT; ioctls. Additionally the &VIDIOC-TRY-FMT; ioctl can be
792used to examine what the hardware <emphasis>could</emphasis> do,
793without actually selecting a new data format. The data formats
794supported by the V4L2 API are covered in the respective device section
795in <xref linkend="devices" />. For a closer look at image formats see
796<xref linkend="pixfmt" />.</para>
797
798 <para>The <constant>VIDIOC_S_FMT</constant> ioctl is a major
799turning-point in the initialization sequence. Prior to this point
800multiple panel applications can access the same device concurrently to
801select the current input, change controls or modify other properties.
802The first <constant>VIDIOC_S_FMT</constant> assigns a logical stream
803(video data, VBI data etc.) exclusively to one file descriptor.</para>
804
805 <para>Exclusive means no other application, more precisely no
806other file descriptor, can grab this stream or change device
807properties inconsistent with the negotiated parameters. A video
808standard change for example, when the new standard uses a different
809number of scan lines, can invalidate the selected image format.
810Therefore only the file descriptor owning the stream can make
811invalidating changes. Accordingly multiple file descriptors which
812grabbed different logical streams prevent each other from interfering
813with their settings. When for example video overlay is about to start
814or already in progress, simultaneous video capturing may be restricted
815to the same cropping and image size.</para>
816
817 <para>When applications omit the
818<constant>VIDIOC_S_FMT</constant> ioctl its locking side effects are
819implied by the next step, the selection of an I/O method with the
820&VIDIOC-REQBUFS; ioctl or implicit with the first &func-read; or
821&func-write; call.</para>
822
823 <para>Generally only one logical stream can be assigned to a
824file descriptor, the exception being drivers permitting simultaneous
825video capturing and overlay using the same file descriptor for
826compatibility with V4L and earlier versions of V4L2. Switching the
827logical stream or returning into "panel mode" is possible by closing
828and reopening the device. Drivers <emphasis>may</emphasis> support a
829switch using <constant>VIDIOC_S_FMT</constant>.</para>
830
831 <para>All drivers exchanging data with
832applications must support the <constant>VIDIOC_G_FMT</constant> and
833<constant>VIDIOC_S_FMT</constant> ioctl. Implementation of the
834<constant>VIDIOC_TRY_FMT</constant> is highly recommended but
835optional.</para>
836 </section>
837
838 <section>
839 <title>Image Format Enumeration</title>
840
841 <para>Apart of the generic format negotiation functions
842a special ioctl to enumerate all image formats supported by video
843capture, overlay or output devices is available.<footnote>
844 <para>Enumerating formats an application has no a-priori
Mauro Carvalho Chehab9aa08852009-09-15 20:27:18 -0300845knowledge of (otherwise it could explicitly ask for them and need not
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300846enumerate) seems useless, but there are applications serving as proxy
847between drivers and the actual video applications for which this is
848useful.</para>
849 </footnote></para>
850
851 <para>The &VIDIOC-ENUM-FMT; ioctl must be supported
852by all drivers exchanging image data with applications.</para>
853
854 <important>
855 <para>Drivers are not supposed to convert image formats in
856kernel space. They must enumerate only formats directly supported by
857the hardware. If necessary driver writers should publish an example
858conversion routine or library for integration into applications.</para>
859 </important>
860 </section>
861 </section>
862
Pawel Osciak53b5d572011-01-07 01:41:33 -0300863 &sub-planar-apis;
864
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -0300865 <section id="crop">
866 <title>Image Cropping, Insertion and Scaling</title>
867
868 <para>Some video capture devices can sample a subsection of the
869picture and shrink or enlarge it to an image of arbitrary size. We
870call these abilities cropping and scaling. Some video output devices
871can scale an image up or down and insert it at an arbitrary scan line
872and horizontal offset into a video signal.</para>
873
874 <para>Applications can use the following API to select an area in
875the video signal, query the default area and the hardware limits.
876<emphasis>Despite their name, the &VIDIOC-CROPCAP;, &VIDIOC-G-CROP;
877and &VIDIOC-S-CROP; ioctls apply to input as well as output
878devices.</emphasis></para>
879
880 <para>Scaling requires a source and a target. On a video capture
881or overlay device the source is the video signal, and the cropping
882ioctls determine the area actually sampled. The target are images
883read by the application or overlaid onto the graphics screen. Their
884size (and position for an overlay) is negotiated with the
885&VIDIOC-G-FMT; and &VIDIOC-S-FMT; ioctls.</para>
886
887 <para>On a video output device the source are the images passed in
888by the application, and their size is again negotiated with the
889<constant>VIDIOC_G/S_FMT</constant> ioctls, or may be encoded in a
890compressed video stream. The target is the video signal, and the
891cropping ioctls determine the area where the images are
892inserted.</para>
893
894 <para>Source and target rectangles are defined even if the device
895does not support scaling or the <constant>VIDIOC_G/S_CROP</constant>
896ioctls. Their size (and position where applicable) will be fixed in
897this case. <emphasis>All capture and output device must support the
898<constant>VIDIOC_CROPCAP</constant> ioctl such that applications can
899determine if scaling takes place.</emphasis></para>
900
901 <section>
902 <title>Cropping Structures</title>
903
904 <figure id="crop-scale">
905 <title>Image Cropping, Insertion and Scaling</title>
906 <mediaobject>
907 <imageobject>
908 <imagedata fileref="crop.pdf" format="PS" />
909 </imageobject>
910 <imageobject>
911 <imagedata fileref="crop.gif" format="GIF" />
912 </imageobject>
913 <textobject>
914 <phrase>The cropping, insertion and scaling process</phrase>
915 </textobject>
916 </mediaobject>
917 </figure>
918
919 <para>For capture devices the coordinates of the top left
920corner, width and height of the area which can be sampled is given by
921the <structfield>bounds</structfield> substructure of the
922&v4l2-cropcap; returned by the <constant>VIDIOC_CROPCAP</constant>
923ioctl. To support a wide range of hardware this specification does not
924define an origin or units. However by convention drivers should
925horizontally count unscaled samples relative to 0H (the leading edge
926of the horizontal sync pulse, see <xref linkend="vbi-hsync" />).
927Vertically ITU-R line
928numbers of the first field (<xref linkend="vbi-525" />, <xref
929linkend="vbi-625" />), multiplied by two if the driver can capture both
930fields.</para>
931
932 <para>The top left corner, width and height of the source
933rectangle, that is the area actually sampled, is given by &v4l2-crop;
934using the same coordinate system as &v4l2-cropcap;. Applications can
935use the <constant>VIDIOC_G_CROP</constant> and
936<constant>VIDIOC_S_CROP</constant> ioctls to get and set this
937rectangle. It must lie completely within the capture boundaries and
938the driver may further adjust the requested size and/or position
939according to hardware limitations.</para>
940
941 <para>Each capture device has a default source rectangle, given
942by the <structfield>defrect</structfield> substructure of
943&v4l2-cropcap;. The center of this rectangle shall align with the
944center of the active picture area of the video signal, and cover what
945the driver writer considers the complete picture. Drivers shall reset
946the source rectangle to the default when the driver is first loaded,
947but not later.</para>
948
949 <para>For output devices these structures and ioctls are used
950accordingly, defining the <emphasis>target</emphasis> rectangle where
951the images will be inserted into the video signal.</para>
952
953 </section>
954
955 <section>
956 <title>Scaling Adjustments</title>
957
958 <para>Video hardware can have various cropping, insertion and
959scaling limitations. It may only scale up or down, support only
960discrete scaling factors, or have different scaling abilities in
961horizontal and vertical direction. Also it may not support scaling at
962all. At the same time the &v4l2-crop; rectangle may have to be
963aligned, and both the source and target rectangles may have arbitrary
964upper and lower size limits. In particular the maximum
965<structfield>width</structfield> and <structfield>height</structfield>
966in &v4l2-crop; may be smaller than the
967&v4l2-cropcap;.<structfield>bounds</structfield> area. Therefore, as
968usual, drivers are expected to adjust the requested parameters and
969return the actual values selected.</para>
970
971 <para>Applications can change the source or the target rectangle
972first, as they may prefer a particular image size or a certain area in
973the video signal. If the driver has to adjust both to satisfy hardware
974limitations, the last requested rectangle shall take priority, and the
975driver should preferably adjust the opposite one. The &VIDIOC-TRY-FMT;
976ioctl however shall not change the driver state and therefore only
977adjust the requested rectangle.</para>
978
979 <para>Suppose scaling on a video capture device is restricted to
980a factor 1:1 or 2:1 in either direction and the target image size must
981be a multiple of 16&nbsp;&times;&nbsp;16 pixels. The source cropping
982rectangle is set to defaults, which are also the upper limit in this
983example, of 640&nbsp;&times;&nbsp;400 pixels at offset 0,&nbsp;0. An
984application requests an image size of 300&nbsp;&times;&nbsp;225
985pixels, assuming video will be scaled down from the "full picture"
986accordingly. The driver sets the image size to the closest possible
987values 304&nbsp;&times;&nbsp;224, then chooses the cropping rectangle
988closest to the requested size, that is 608&nbsp;&times;&nbsp;224
989(224&nbsp;&times;&nbsp;2:1 would exceed the limit 400). The offset
9900,&nbsp;0 is still valid, thus unmodified. Given the default cropping
991rectangle reported by <constant>VIDIOC_CROPCAP</constant> the
992application can easily propose another offset to center the cropping
993rectangle.</para>
994
995 <para>Now the application may insist on covering an area using a
996picture aspect ratio closer to the original request, so it asks for a
997cropping rectangle of 608&nbsp;&times;&nbsp;456 pixels. The present
998scaling factors limit cropping to 640&nbsp;&times;&nbsp;384, so the
999driver returns the cropping size 608&nbsp;&times;&nbsp;384 and adjusts
1000the image size to closest possible 304&nbsp;&times;&nbsp;192.</para>
1001
1002 </section>
1003
1004 <section>
1005 <title>Examples</title>
1006
1007 <para>Source and target rectangles shall remain unchanged across
1008closing and reopening a device, such that piping data into or out of a
1009device will work without special preparations. More advanced
1010applications should ensure the parameters are suitable before starting
1011I/O.</para>
1012
1013 <example>
1014 <title>Resetting the cropping parameters</title>
1015
1016 <para>(A video capture device is assumed; change
1017<constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> for other
1018devices.)</para>
1019
1020 <programlisting>
1021&v4l2-cropcap; cropcap;
1022&v4l2-crop; crop;
1023
1024memset (&amp;cropcap, 0, sizeof (cropcap));
1025cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1026
1027if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
1028 perror ("VIDIOC_CROPCAP");
1029 exit (EXIT_FAILURE);
1030}
1031
1032memset (&amp;crop, 0, sizeof (crop));
1033crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1034crop.c = cropcap.defrect;
1035
1036/* Ignore if cropping is not supported (EINVAL). */
1037
1038if (-1 == ioctl (fd, &VIDIOC-S-CROP;, &amp;crop)
1039 &amp;&amp; errno != EINVAL) {
1040 perror ("VIDIOC_S_CROP");
1041 exit (EXIT_FAILURE);
1042}
1043 </programlisting>
1044 </example>
1045
1046 <example>
1047 <title>Simple downscaling</title>
1048
1049 <para>(A video capture device is assumed.)</para>
1050
1051 <programlisting>
1052&v4l2-cropcap; cropcap;
1053&v4l2-format; format;
1054
1055reset_cropping_parameters ();
1056
1057/* Scale down to 1/4 size of full picture. */
1058
1059memset (&amp;format, 0, sizeof (format)); /* defaults */
1060
1061format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1062
1063format.fmt.pix.width = cropcap.defrect.width &gt;&gt; 1;
1064format.fmt.pix.height = cropcap.defrect.height &gt;&gt; 1;
1065format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1066
1067if (-1 == ioctl (fd, &VIDIOC-S-FMT;, &amp;format)) {
1068 perror ("VIDIOC_S_FORMAT");
1069 exit (EXIT_FAILURE);
1070}
1071
1072/* We could check the actual image size now, the actual scaling factor
1073 or if the driver can scale at all. */
1074 </programlisting>
1075 </example>
1076
1077 <example>
1078 <title>Selecting an output area</title>
1079
1080 <programlisting>
1081&v4l2-cropcap; cropcap;
1082&v4l2-crop; crop;
1083
1084memset (&amp;cropcap, 0, sizeof (cropcap));
1085cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1086
1087if (-1 == ioctl (fd, VIDIOC_CROPCAP;, &amp;cropcap)) {
1088 perror ("VIDIOC_CROPCAP");
1089 exit (EXIT_FAILURE);
1090}
1091
1092memset (&amp;crop, 0, sizeof (crop));
1093
1094crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1095crop.c = cropcap.defrect;
1096
1097/* Scale the width and height to 50 % of their original size
1098 and center the output. */
1099
1100crop.c.width /= 2;
1101crop.c.height /= 2;
1102crop.c.left += crop.c.width / 2;
1103crop.c.top += crop.c.height / 2;
1104
1105/* Ignore if cropping is not supported (EINVAL). */
1106
1107if (-1 == ioctl (fd, VIDIOC_S_CROP, &amp;crop)
1108 &amp;&amp; errno != EINVAL) {
1109 perror ("VIDIOC_S_CROP");
1110 exit (EXIT_FAILURE);
1111}
1112</programlisting>
1113 </example>
1114
1115 <example>
1116 <title>Current scaling factor and pixel aspect</title>
1117
1118 <para>(A video capture device is assumed.)</para>
1119
1120 <programlisting>
1121&v4l2-cropcap; cropcap;
1122&v4l2-crop; crop;
1123&v4l2-format; format;
1124double hscale, vscale;
1125double aspect;
1126int dwidth, dheight;
1127
1128memset (&amp;cropcap, 0, sizeof (cropcap));
1129cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1130
1131if (-1 == ioctl (fd, &VIDIOC-CROPCAP;, &amp;cropcap)) {
1132 perror ("VIDIOC_CROPCAP");
1133 exit (EXIT_FAILURE);
1134}
1135
1136memset (&amp;crop, 0, sizeof (crop));
1137crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1138
1139if (-1 == ioctl (fd, &VIDIOC-G-CROP;, &amp;crop)) {
1140 if (errno != EINVAL) {
1141 perror ("VIDIOC_G_CROP");
1142 exit (EXIT_FAILURE);
1143 }
1144
1145 /* Cropping not supported. */
1146 crop.c = cropcap.defrect;
1147}
1148
1149memset (&amp;format, 0, sizeof (format));
1150format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1151
1152if (-1 == ioctl (fd, &VIDIOC-G-FMT;, &amp;format)) {
1153 perror ("VIDIOC_G_FMT");
1154 exit (EXIT_FAILURE);
1155}
1156
1157/* The scaling applied by the driver. */
1158
1159hscale = format.fmt.pix.width / (double) crop.c.width;
1160vscale = format.fmt.pix.height / (double) crop.c.height;
1161
1162aspect = cropcap.pixelaspect.numerator /
1163 (double) cropcap.pixelaspect.denominator;
1164aspect = aspect * hscale / vscale;
1165
1166/* Devices following ITU-R BT.601 do not capture
1167 square pixels. For playback on a computer monitor
1168 we should scale the images to this size. */
1169
1170dwidth = format.fmt.pix.width / aspect;
1171dheight = format.fmt.pix.height;
1172 </programlisting>
1173 </example>
1174 </section>
1175 </section>
1176
Tomasz Stanislawski8af49222011-08-19 07:00:04 -03001177 &sub-selection-api;
1178
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -03001179 <section id="streaming-par">
1180 <title>Streaming Parameters</title>
1181
1182 <para>Streaming parameters are intended to optimize the video
1183capture process as well as I/O. Presently applications can request a
1184high quality capture mode with the &VIDIOC-S-PARM; ioctl.</para>
1185
1186 <para>The current video standard determines a nominal number of
1187frames per second. If less than this number of frames is to be
1188captured or output, applications can request frame skipping or
1189duplicating on the driver side. This is especially useful when using
1190the &func-read; or &func-write;, which are not augmented by timestamps
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001191or sequence counters, and to avoid unnecessary data copying.</para>
Mauro Carvalho Chehab8e080c22009-09-13 22:16:04 -03001192
1193 <para>Finally these ioctls can be used to determine the number of
1194buffers used internally by a driver in read/write mode. For
1195implications see the section discussing the &func-read;
1196function.</para>
1197
1198 <para>To get and set the streaming parameters applications call
1199the &VIDIOC-G-PARM; and &VIDIOC-S-PARM; ioctl, respectively. They take
1200a pointer to a &v4l2-streamparm;, which contains a union holding
1201separate parameters for input and output devices.</para>
1202
1203 <para>These ioctls are optional, drivers need not implement
1204them. If so, they return the &EINVAL;.</para>
1205 </section>