aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/DocBook/v4l/io.xml
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/DocBook/v4l/io.xml')
-rw-r--r--Documentation/DocBook/v4l/io.xml1073
1 files changed, 1073 insertions, 0 deletions
diff --git a/Documentation/DocBook/v4l/io.xml b/Documentation/DocBook/v4l/io.xml
new file mode 100644
index 000000000000..f92f24323b2a
--- /dev/null
+++ b/Documentation/DocBook/v4l/io.xml
@@ -0,0 +1,1073 @@
1 <title>Input/Output</title>
2
3 <para>The V4L2 API defines several different methods to read from or
4write to a device. All drivers exchanging data with applications must
5support at least one of them.</para>
6
7 <para>The classic I/O method using the <function>read()</function>
8and <function>write()</function> function is automatically selected
9after opening a V4L2 device. When the driver does not support this
10method attempts to read or write will fail at any time.</para>
11
12 <para>Other methods must be negotiated. To select the streaming I/O
13method with memory mapped or user buffers applications call the
14&VIDIOC-REQBUFS; ioctl. The asynchronous I/O method is not defined
15yet.</para>
16
17 <para>Video overlay can be considered another I/O method, although
18the application does not directly receive the image data. It is
19selected by initiating video overlay with the &VIDIOC-S-FMT; ioctl.
20For more information see <xref linkend="overlay" />.</para>
21
22 <para>Generally exactly one I/O method, including overlay, is
23associated with each file descriptor. The only exceptions are
24applications not exchanging data with a driver ("panel applications",
25see <xref linkend="open" />) and drivers permitting simultaneous video capturing
26and overlay using the same file descriptor, for compatibility with V4L
27and earlier versions of V4L2.</para>
28
29 <para><constant>VIDIOC_S_FMT</constant> and
30<constant>VIDIOC_REQBUFS</constant> would permit this to some degree,
31but for simplicity drivers need not support switching the I/O method
32(after first switching away from read/write) other than by closing
33and reopening the device.</para>
34
35 <para>The following sections describe the various I/O methods in
36more detail.</para>
37
38 <section id="rw">
39 <title>Read/Write</title>
40
41 <para>Input and output devices support the
42<function>read()</function> and <function>write()</function> function,
43respectively, when the <constant>V4L2_CAP_READWRITE</constant> flag in
44the <structfield>capabilities</structfield> field of &v4l2-capability;
45returned by the &VIDIOC-QUERYCAP; ioctl is set.</para>
46
47 <para>Drivers may need the CPU to copy the data, but they may also
48support DMA to or from user memory, so this I/O method is not
49necessarily less efficient than other methods merely exchanging buffer
50pointers. It is considered inferior though because no meta-information
51like frame counters or timestamps are passed. This information is
52necessary to recognize frame dropping and to synchronize with other
53data streams. However this is also the simplest I/O method, requiring
54little or no setup to exchange data. It permits command line stunts
55like this (the <application>vidctrl</application> tool is
56fictitious):</para>
57
58 <informalexample>
59 <screen>
60&gt; vidctrl /dev/video --input=0 --format=YUYV --size=352x288
61&gt; dd if=/dev/video of=myimage.422 bs=202752 count=1
62</screen>
63 </informalexample>
64
65 <para>To read from the device applications use the
66&func-read; function, to write the &func-write; function.
67Drivers must implement one I/O method if they
68exchange data with applications, but it need not be this.<footnote>
69 <para>It would be desirable if applications could depend on
70drivers supporting all I/O interfaces, but as much as the complex
71memory mapping I/O can be inadequate for some devices we have no
72reason to require this interface, which is most useful for simple
73applications capturing still images.</para>
74 </footnote> When reading or writing is supported, the driver
75must also support the &func-select; and &func-poll;
76function.<footnote>
77 <para>At the driver level <function>select()</function> and
78<function>poll()</function> are the same, and
79<function>select()</function> is too important to be optional.</para>
80 </footnote></para>
81 </section>
82
83 <section id="mmap">
84 <title>Streaming I/O (Memory Mapping)</title>
85
86 <para>Input and output devices support this I/O method when the
87<constant>V4L2_CAP_STREAMING</constant> flag in the
88<structfield>capabilities</structfield> field of &v4l2-capability;
89returned by the &VIDIOC-QUERYCAP; ioctl is set. There are two
90streaming methods, to determine if the memory mapping flavor is
91supported applications must call the &VIDIOC-REQBUFS; ioctl.</para>
92
93 <para>Streaming is an I/O method where only pointers to buffers
94are exchanged between application and driver, the data itself is not
95copied. Memory mapping is primarily intended to map buffers in device
96memory into the application's address space. Device memory can be for
97example the video memory on a graphics card with a video capture
98add-on. However, being the most efficient I/O method available for a
99long time, many other drivers support streaming as well, allocating
100buffers in DMA-able main memory.</para>
101
102 <para>A driver can support many sets of buffers. Each set is
103identified by a unique buffer type value. The sets are independent and
104each set can hold a different type of data. To access different sets
105at the same time different file descriptors must be used.<footnote>
106 <para>One could use one file descriptor and set the buffer
107type field accordingly when calling &VIDIOC-QBUF; etc., but it makes
108the <function>select()</function> function ambiguous. We also like the
109clean approach of one file descriptor per logical stream. Video
110overlay for example is also a logical stream, although the CPU is not
111needed for continuous operation.</para>
112 </footnote></para>
113
114 <para>To allocate device buffers applications call the
115&VIDIOC-REQBUFS; ioctl with the desired number of buffers and buffer
116type, for example <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant>.
117This ioctl can also be used to change the number of buffers or to free
118the allocated memory, provided none of the buffers are still
119mapped.</para>
120
121 <para>Before applications can access the buffers they must map
122them into their address space with the &func-mmap; function. The
123location of the buffers in device memory can be determined with the
124&VIDIOC-QUERYBUF; ioctl. The <structfield>m.offset</structfield> and
125<structfield>length</structfield> returned in a &v4l2-buffer; are
126passed as sixth and second parameter to the
127<function>mmap()</function> function. The offset and length values
128must not be modified. Remember the buffers are allocated in physical
129memory, as opposed to virtual memory which can be swapped out to disk.
130Applications should free the buffers as soon as possible with the
131&func-munmap; function.</para>
132
133 <example>
134 <title>Mapping buffers</title>
135
136 <programlisting>
137&v4l2-requestbuffers; reqbuf;
138struct {
139 void *start;
140 size_t length;
141} *buffers;
142unsigned int i;
143
144memset (&amp;reqbuf, 0, sizeof (reqbuf));
145reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
146reqbuf.memory = V4L2_MEMORY_MMAP;
147reqbuf.count = 20;
148
149if (-1 == ioctl (fd, &VIDIOC-REQBUFS;, &amp;reqbuf)) {
150 if (errno == EINVAL)
151 printf ("Video capturing or mmap-streaming is not supported\n");
152 else
153 perror ("VIDIOC_REQBUFS");
154
155 exit (EXIT_FAILURE);
156}
157
158/* We want at least five buffers. */
159
160if (reqbuf.count &lt; 5) {
161 /* You may need to free the buffers here. */
162 printf ("Not enough buffer memory\n");
163 exit (EXIT_FAILURE);
164}
165
166buffers = calloc (reqbuf.count, sizeof (*buffers));
167assert (buffers != NULL);
168
169for (i = 0; i &lt; reqbuf.count; i++) {
170 &v4l2-buffer; buffer;
171
172 memset (&amp;buffer, 0, sizeof (buffer));
173 buffer.type = reqbuf.type;
174 buffer.memory = V4L2_MEMORY_MMAP;
175 buffer.index = i;
176
177 if (-1 == ioctl (fd, &VIDIOC-QUERYBUF;, &amp;buffer)) {
178 perror ("VIDIOC_QUERYBUF");
179 exit (EXIT_FAILURE);
180 }
181
182 buffers[i].length = buffer.length; /* remember for munmap() */
183
184 buffers[i].start = mmap (NULL, buffer.length,
185 PROT_READ | PROT_WRITE, /* recommended */
186 MAP_SHARED, /* recommended */
187 fd, buffer.m.offset);
188
189 if (MAP_FAILED == buffers[i].start) {
190 /* If you do not exit here you should unmap() and free()
191 the buffers mapped so far. */
192 perror ("mmap");
193 exit (EXIT_FAILURE);
194 }
195}
196
197/* Cleanup. */
198
199for (i = 0; i &lt; reqbuf.count; i++)
200 munmap (buffers[i].start, buffers[i].length);
201 </programlisting>
202 </example>
203
204 <para>Conceptually streaming drivers maintain two buffer queues, an incoming
205and an outgoing queue. They separate the synchronous capture or output
206operation locked to a video clock from the application which is
207subject to random disk or network delays and preemption by
208other processes, thereby reducing the probability of data loss.
209The queues are organized as FIFOs, buffers will be
210output in the order enqueued in the incoming FIFO, and were
211captured in the order dequeued from the outgoing FIFO.</para>
212
213 <para>The driver may require a minimum number of buffers enqueued
214at all times to function, apart of this no limit exists on the number
215of buffers applications can enqueue in advance, or dequeue and
216process. They can also enqueue in a different order than buffers have
217been dequeued, and the driver can <emphasis>fill</emphasis> enqueued
218<emphasis>empty</emphasis> buffers in any order. <footnote>
219 <para>Random enqueue order permits applications processing
220images out of order (such as video codecs) to return buffers earlier,
221reducing the probability of data loss. Random fill order allows
222drivers to reuse buffers on a LIFO-basis, taking advantage of caches
223holding scatter-gather lists and the like.</para>
224 </footnote> The index number of a buffer (&v4l2-buffer;
225<structfield>index</structfield>) plays no role here, it only
226identifies the buffer.</para>
227
228 <para>Initially all mapped buffers are in dequeued state,
229inaccessible by the driver. For capturing applications it is customary
230to first enqueue all mapped buffers, then to start capturing and enter
231the read loop. Here the application waits until a filled buffer can be
232dequeued, and re-enqueues the buffer when the data is no longer
233needed. Output applications fill and enqueue buffers, when enough
234buffers are stacked up the output is started with
235<constant>VIDIOC_STREAMON</constant>. In the write loop, when
236the application runs out of free buffers, it must wait until an empty
237buffer can be dequeued and reused.</para>
238
239 <para>To enqueue and dequeue a buffer applications use the
240&VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl. The status of a buffer being
241mapped, enqueued, full or empty can be determined at any time using the
242&VIDIOC-QUERYBUF; ioctl. Two methods exist to suspend execution of the
243application until one or more buffers can be dequeued. By default
244<constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the
245outgoing queue. When the <constant>O_NONBLOCK</constant> flag was
246given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>
247returns immediately with an &EAGAIN; when no buffer is available. The
248&func-select; or &func-poll; function are always available.</para>
249
250 <para>To start and stop capturing or output applications call the
251&VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note
252<constant>VIDIOC_STREAMOFF</constant> removes all buffers from both
253queues as a side effect. Since there is no notion of doing anything
254"now" on a multitasking system, if an application needs to synchronize
255with another event it should examine the &v4l2-buffer;
256<structfield>timestamp</structfield> of captured buffers, or set the
257field before enqueuing buffers for output.</para>
258
259 <para>Drivers implementing memory mapping I/O must
260support the <constant>VIDIOC_REQBUFS</constant>,
261<constant>VIDIOC_QUERYBUF</constant>,
262<constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,
263<constant>VIDIOC_STREAMON</constant> and
264<constant>VIDIOC_STREAMOFF</constant> ioctl, the
265<function>mmap()</function>, <function>munmap()</function>,
266<function>select()</function> and <function>poll()</function>
267function.<footnote>
268 <para>At the driver level <function>select()</function> and
269<function>poll()</function> are the same, and
270<function>select()</function> is too important to be optional. The
271rest should be evident.</para>
272 </footnote></para>
273
274 <para>[capture example]</para>
275
276 </section>
277
278 <section id="userp">
279 <title>Streaming I/O (User Pointers)</title>
280
281 <para>Input and output devices support this I/O method when the
282<constant>V4L2_CAP_STREAMING</constant> flag in the
283<structfield>capabilities</structfield> field of &v4l2-capability;
284returned by the &VIDIOC-QUERYCAP; ioctl is set. If the particular user
285pointer method (not only memory mapping) is supported must be
286determined by calling the &VIDIOC-REQBUFS; ioctl.</para>
287
288 <para>This I/O method combines advantages of the read/write and
289memory mapping methods. Buffers are allocated by the application
290itself, and can reside for example in virtual or shared memory. Only
291pointers to data are exchanged, these pointers and meta-information
292are passed in &v4l2-buffer;. The driver must be switched
293into user pointer I/O mode by calling the &VIDIOC-REQBUFS; with the
294desired buffer type. No buffers are allocated beforehands,
295consequently they are not indexed and cannot be queried like mapped
296buffers with the <constant>VIDIOC_QUERYBUF</constant> ioctl.</para>
297
298 <example>
299 <title>Initiating streaming I/O with user pointers</title>
300
301 <programlisting>
302&v4l2-requestbuffers; reqbuf;
303
304memset (&amp;reqbuf, 0, sizeof (reqbuf));
305reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
306reqbuf.memory = V4L2_MEMORY_USERPTR;
307
308if (ioctl (fd, &VIDIOC-REQBUFS;, &amp;reqbuf) == -1) {
309 if (errno == EINVAL)
310 printf ("Video capturing or user pointer streaming is not supported\n");
311 else
312 perror ("VIDIOC_REQBUFS");
313
314 exit (EXIT_FAILURE);
315}
316 </programlisting>
317 </example>
318
319 <para>Buffer addresses and sizes are passed on the fly with the
320&VIDIOC-QBUF; ioctl. Although buffers are commonly cycled,
321applications can pass different addresses and sizes at each
322<constant>VIDIOC_QBUF</constant> call. If required by the hardware the
323driver swaps memory pages within physical memory to create a
324continuous area of memory. This happens transparently to the
325application in the virtual memory subsystem of the kernel. When buffer
326pages have been swapped out to disk they are brought back and finally
327locked in physical memory for DMA.<footnote>
328 <para>We expect that frequently used buffers are typically not
329swapped out. Anyway, the process of swapping, locking or generating
330scatter-gather lists may be time consuming. The delay can be masked by
331the depth of the incoming buffer queue, and perhaps by maintaining
332caches assuming a buffer will be soon enqueued again. On the other
333hand, to optimize memory usage drivers can limit the number of buffers
334locked in advance and recycle the most recently used buffers first. Of
335course, the pages of empty buffers in the incoming queue need not be
336saved to disk. Output buffers must be saved on the incoming and
337outgoing queue because an application may share them with other
338processes.</para>
339 </footnote></para>
340
341 <para>Filled or displayed buffers are dequeued with the
342&VIDIOC-DQBUF; ioctl. The driver can unlock the memory pages at any
343time between the completion of the DMA and this ioctl. The memory is
344also unlocked when &VIDIOC-STREAMOFF; is called, &VIDIOC-REQBUFS;, or
345when the device is closed. Applications must take care not to free
346buffers without dequeuing. For once, the buffers remain locked until
347further, wasting physical memory. Second the driver will not be
348notified when the memory is returned to the application's free list
349and subsequently reused for other purposes, possibly completing the
350requested DMA and overwriting valuable data.</para>
351
352 <para>For capturing applications it is customary to enqueue a
353number of empty buffers, to start capturing and enter the read loop.
354Here the application waits until a filled buffer can be dequeued, and
355re-enqueues the buffer when the data is no longer needed. Output
356applications fill and enqueue buffers, when enough buffers are stacked
357up output is started. In the write loop, when the application
358runs out of free buffers it must wait until an empty buffer can be
359dequeued and reused. Two methods exist to suspend execution of the
360application until one or more buffers can be dequeued. By default
361<constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the
362outgoing queue. When the <constant>O_NONBLOCK</constant> flag was
363given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>
364returns immediately with an &EAGAIN; when no buffer is available. The
365&func-select; or &func-poll; function are always available.</para>
366
367 <para>To start and stop capturing or output applications call the
368&VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note
369<constant>VIDIOC_STREAMOFF</constant> removes all buffers from both
370queues and unlocks all buffers as a side effect. Since there is no
371notion of doing anything "now" on a multitasking system, if an
372application needs to synchronize with another event it should examine
373the &v4l2-buffer; <structfield>timestamp</structfield> of captured
374buffers, or set the field before enqueuing buffers for output.</para>
375
376 <para>Drivers implementing user pointer I/O must
377support the <constant>VIDIOC_REQBUFS</constant>,
378<constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,
379<constant>VIDIOC_STREAMON</constant> and
380<constant>VIDIOC_STREAMOFF</constant> ioctl, the
381<function>select()</function> and <function>poll()</function> function.<footnote>
382 <para>At the driver level <function>select()</function> and
383<function>poll()</function> are the same, and
384<function>select()</function> is too important to be optional. The
385rest should be evident.</para>
386 </footnote></para>
387 </section>
388
389 <section id="async">
390 <title>Asynchronous I/O</title>
391
392 <para>This method is not defined yet.</para>
393 </section>
394
395 <section id="buffer">
396 <title>Buffers</title>
397
398 <para>A buffer contains data exchanged by application and
399driver using one of the Streaming I/O methods. Only pointers to
400buffers are exchanged, the data itself is not copied. These pointers,
401together with meta-information like timestamps or field parity, are
402stored in a struct <structname>v4l2_buffer</structname>, argument to
403the &VIDIOC-QUERYBUF;, &VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl.</para>
404
405 <para>Nominally timestamps refer to the first data byte transmitted.
406In practice however the wide range of hardware covered by the V4L2 API
407limits timestamp accuracy. Often an interrupt routine will
408sample the system clock shortly after the field or frame was stored
409completely in memory. So applications must expect a constant
410difference up to one field or frame period plus a small (few scan
411lines) random error. The delay and error can be much
412larger due to compression or transmission over an external bus when
413the frames are not properly stamped by the sender. This is frequently
414the case with USB cameras. Here timestamps refer to the instant the
415field or frame was received by the driver, not the capture time. These
416devices identify by not enumerating any video standards, see <xref
417linkend="standard" />.</para>
418
419 <para>Similar limitations apply to output timestamps. Typically
420the video hardware locks to a clock controlling the video timing, the
421horizontal and vertical synchronization pulses. At some point in the
422line sequence, possibly the vertical blanking, an interrupt routine
423samples the system clock, compares against the timestamp and programs
424the hardware to repeat the previous field or frame, or to display the
425buffer contents.</para>
426
427 <para>Apart of limitations of the video device and natural
428inaccuracies of all clocks, it should be noted system time itself is
429not perfectly stable. It can be affected by power saving cycles,
430warped to insert leap seconds, or even turned back or forth by the
431system administrator affecting long term measurements. <footnote>
432 <para>Since no other Linux multimedia
433API supports unadjusted time it would be foolish to introduce here. We
434must use a universally supported clock to synchronize different media,
435hence time of day.</para>
436 </footnote></para>
437
438 <table frame="none" pgwide="1" id="v4l2-buffer">
439 <title>struct <structname>v4l2_buffer</structname></title>
440 <tgroup cols="4">
441 &cs-ustr;
442 <tbody valign="top">
443 <row>
444 <entry>__u32</entry>
445 <entry><structfield>index</structfield></entry>
446 <entry></entry>
447 <entry>Number of the buffer, set by the application. This
448field is only used for <link linkend="mmap">memory mapping</link> I/O
449and can range from zero to the number of buffers allocated
450with the &VIDIOC-REQBUFS; ioctl (&v4l2-requestbuffers; <structfield>count</structfield>) minus one.</entry>
451 </row>
452 <row>
453 <entry>&v4l2-buf-type;</entry>
454 <entry><structfield>type</structfield></entry>
455 <entry></entry>
456 <entry>Type of the buffer, same as &v4l2-format;
457<structfield>type</structfield> or &v4l2-requestbuffers;
458<structfield>type</structfield>, set by the application.</entry>
459 </row>
460 <row>
461 <entry>__u32</entry>
462 <entry><structfield>bytesused</structfield></entry>
463 <entry></entry>
464 <entry>The number of bytes occupied by the data in the
465buffer. It depends on the negotiated data format and may change with
466each buffer for compressed variable size data like JPEG images.
467Drivers must set this field when <structfield>type</structfield>
468refers to an input stream, applications when an output stream.</entry>
469 </row>
470 <row>
471 <entry>__u32</entry>
472 <entry><structfield>flags</structfield></entry>
473 <entry></entry>
474 <entry>Flags set by the application or driver, see <xref
475linkend="buffer-flags" />.</entry>
476 </row>
477 <row>
478 <entry>&v4l2-field;</entry>
479 <entry><structfield>field</structfield></entry>
480 <entry></entry>
481 <entry>Indicates the field order of the image in the
482buffer, see <xref linkend="v4l2-field" />. This field is not used when
483the buffer contains VBI data. Drivers must set it when
484<structfield>type</structfield> refers to an input stream,
485applications when an output stream.</entry>
486 </row>
487 <row>
488 <entry>struct timeval</entry>
489 <entry><structfield>timestamp</structfield></entry>
490 <entry></entry>
491 <entry><para>For input streams this is the
492system time (as returned by the <function>gettimeofday()</function>
493function) when the first data byte was captured. For output streams
494the data will not be displayed before this time, secondary to the
495nominal frame rate determined by the current video standard in
496enqueued order. Applications can for example zero this field to
497display frames as soon as possible. The driver stores the time at
498which the first data byte was actually sent out in the
499<structfield>timestamp</structfield> field. This permits
500applications to monitor the drift between the video and system
501clock.</para></entry>
502 </row>
503 <row>
504 <entry>&v4l2-timecode;</entry>
505 <entry><structfield>timecode</structfield></entry>
506 <entry></entry>
507 <entry>When <structfield>type</structfield> is
508<constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> and the
509<constant>V4L2_BUF_FLAG_TIMECODE</constant> flag is set in
510<structfield>flags</structfield>, this structure contains a frame
511timecode. In <link linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link>
512mode the top and bottom field contain the same timecode.
513Timecodes are intended to help video editing and are typically recorded on
514video tapes, but also embedded in compressed formats like MPEG. This
515field is independent of the <structfield>timestamp</structfield> and
516<structfield>sequence</structfield> fields.</entry>
517 </row>
518 <row>
519 <entry>__u32</entry>
520 <entry><structfield>sequence</structfield></entry>
521 <entry></entry>
522 <entry>Set by the driver, counting the frames in the
523sequence.</entry>
524 </row>
525 <row>
526 <entry spanname="hspan"><para>In <link
527linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link> mode the top and
528bottom field have the same sequence number. The count starts at zero
529and includes dropped or repeated frames. A dropped frame was received
530by an input device but could not be stored due to lack of free buffer
531space. A repeated frame was displayed again by an output device
532because the application did not pass new data in
533time.</para><para>Note this may count the frames received
534e.g. over USB, without taking into account the frames dropped by the
535remote hardware due to limited compression throughput or bus
536bandwidth. These devices identify by not enumerating any video
537standards, see <xref linkend="standard" />.</para></entry>
538 </row>
539 <row>
540 <entry>&v4l2-memory;</entry>
541 <entry><structfield>memory</structfield></entry>
542 <entry></entry>
543 <entry>This field must be set by applications and/or drivers
544in accordance with the selected I/O method.</entry>
545 </row>
546 <row>
547 <entry>union</entry>
548 <entry><structfield>m</structfield></entry>
549 </row>
550 <row>
551 <entry></entry>
552 <entry>__u32</entry>
553 <entry><structfield>offset</structfield></entry>
554 <entry>When <structfield>memory</structfield> is
555<constant>V4L2_MEMORY_MMAP</constant> this is the offset of the buffer
556from the start of the device memory. The value is returned by the
557driver and apart of serving as parameter to the &func-mmap; function
558not useful for applications. See <xref linkend="mmap" /> for details.</entry>
559 </row>
560 <row>
561 <entry></entry>
562 <entry>unsigned long</entry>
563 <entry><structfield>userptr</structfield></entry>
564 <entry>When <structfield>memory</structfield> is
565<constant>V4L2_MEMORY_USERPTR</constant> this is a pointer to the
566buffer (casted to unsigned long type) in virtual memory, set by the
567application. See <xref linkend="userp" /> for details.</entry>
568 </row>
569 <row>
570 <entry>__u32</entry>
571 <entry><structfield>length</structfield></entry>
572 <entry></entry>
573 <entry>Size of the buffer (not the payload) in bytes.</entry>
574 </row>
575 <row>
576 <entry>__u32</entry>
577 <entry><structfield>input</structfield></entry>
578 <entry></entry>
579 <entry>Some video capture drivers support rapid and
580synchronous video input changes, a function useful for example in
581video surveillance applications. For this purpose applications set the
582<constant>V4L2_BUF_FLAG_INPUT</constant> flag, and this field to the
583number of a video input as in &v4l2-input; field
584<structfield>index</structfield>.</entry>
585 </row>
586 <row>
587 <entry>__u32</entry>
588 <entry><structfield>reserved</structfield></entry>
589 <entry></entry>
590 <entry>A place holder for future extensions and custom
591(driver defined) buffer types
592<constant>V4L2_BUF_TYPE_PRIVATE</constant> and higher.</entry>
593 </row>
594 </tbody>
595 </tgroup>
596 </table>
597
598 <table frame="none" pgwide="1" id="v4l2-buf-type">
599 <title>enum v4l2_buf_type</title>
600 <tgroup cols="3">
601 &cs-def;
602 <tbody valign="top">
603 <row>
604 <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant></entry>
605 <entry>1</entry>
606 <entry>Buffer of a video capture stream, see <xref
607 linkend="capture" />.</entry>
608 </row>
609 <row>
610 <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT</constant></entry>
611 <entry>2</entry>
612 <entry>Buffer of a video output stream, see <xref
613 linkend="output" />.</entry>
614 </row>
615 <row>
616 <entry><constant>V4L2_BUF_TYPE_VIDEO_OVERLAY</constant></entry>
617 <entry>3</entry>
618 <entry>Buffer for video overlay, see <xref linkend="overlay" />.</entry>
619 </row>
620 <row>
621 <entry><constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant></entry>
622 <entry>4</entry>
623 <entry>Buffer of a raw VBI capture stream, see <xref
624 linkend="raw-vbi" />.</entry>
625 </row>
626 <row>
627 <entry><constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant></entry>
628 <entry>5</entry>
629 <entry>Buffer of a raw VBI output stream, see <xref
630 linkend="raw-vbi" />.</entry>
631 </row>
632 <row>
633 <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_CAPTURE</constant></entry>
634 <entry>6</entry>
635 <entry>Buffer of a sliced VBI capture stream, see <xref
636 linkend="sliced" />.</entry>
637 </row>
638 <row>
639 <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_OUTPUT</constant></entry>
640 <entry>7</entry>
641 <entry>Buffer of a sliced VBI output stream, see <xref
642 linkend="sliced" />.</entry>
643 </row>
644 <row>
645 <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY</constant></entry>
646 <entry>8</entry>
647 <entry>Buffer for video output overlay (OSD), see <xref
648 linkend="osd" />. Status: <link
649linkend="experimental">Experimental</link>.</entry>
650 </row>
651 <row>
652 <entry><constant>V4L2_BUF_TYPE_PRIVATE</constant></entry>
653 <entry>0x80</entry>
654 <entry>This and higher values are reserved for custom
655(driver defined) buffer types.</entry>
656 </row>
657 </tbody>
658 </tgroup>
659 </table>
660
661 <table frame="none" pgwide="1" id="buffer-flags">
662 <title>Buffer Flags</title>
663 <tgroup cols="3">
664 &cs-def;
665 <tbody valign="top">
666 <row>
667 <entry><constant>V4L2_BUF_FLAG_MAPPED</constant></entry>
668 <entry>0x0001</entry>
669 <entry>The buffer resides in device memory and has been mapped
670into the application's address space, see <xref linkend="mmap" /> for details.
671Drivers set or clear this flag when the
672<link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
673 linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
674 linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called. Set by the driver.</entry>
675 </row>
676 <row>
677 <entry><constant>V4L2_BUF_FLAG_QUEUED</constant></entry>
678 <entry>0x0002</entry>
679 <entry>Internally drivers maintain two buffer queues, an
680incoming and outgoing queue. When this flag is set, the buffer is
681currently on the incoming queue. It automatically moves to the
682outgoing queue after the buffer has been filled (capture devices) or
683displayed (output devices). Drivers set or clear this flag when the
684<constant>VIDIOC_QUERYBUF</constant> ioctl is called. After
685(successful) calling the <constant>VIDIOC_QBUF </constant>ioctl it is
686always set and after <constant>VIDIOC_DQBUF</constant> always
687cleared.</entry>
688 </row>
689 <row>
690 <entry><constant>V4L2_BUF_FLAG_DONE</constant></entry>
691 <entry>0x0004</entry>
692 <entry>When this flag is set, the buffer is currently on
693the outgoing queue, ready to be dequeued from the driver. Drivers set
694or clear this flag when the <constant>VIDIOC_QUERYBUF</constant> ioctl
695is called. After calling the <constant>VIDIOC_QBUF</constant> or
696<constant>VIDIOC_DQBUF</constant> it is always cleared. Of course a
697buffer cannot be on both queues at the same time, the
698<constant>V4L2_BUF_FLAG_QUEUED</constant> and
699<constant>V4L2_BUF_FLAG_DONE</constant> flag are mutually exclusive.
700They can be both cleared however, then the buffer is in "dequeued"
701state, in the application domain to say so.</entry>
702 </row>
703 <row>
704 <entry><constant>V4L2_BUF_FLAG_KEYFRAME</constant></entry>
705 <entry>0x0008</entry>
706 <entry>Drivers set or clear this flag when calling the
707<constant>VIDIOC_DQBUF</constant> ioctl. It may be set by video
708capture devices when the buffer contains a compressed image which is a
709key frame (or field), &ie; can be decompressed on its own.</entry>
710 </row>
711 <row>
712 <entry><constant>V4L2_BUF_FLAG_PFRAME</constant></entry>
713 <entry>0x0010</entry>
714 <entry>Similar to <constant>V4L2_BUF_FLAG_KEYFRAME</constant>
715this flags predicted frames or fields which contain only differences to a
716previous key frame.</entry>
717 </row>
718 <row>
719 <entry><constant>V4L2_BUF_FLAG_BFRAME</constant></entry>
720 <entry>0x0020</entry>
721 <entry>Similar to <constant>V4L2_BUF_FLAG_PFRAME</constant>
722 this is a bidirectional predicted frame or field. [ooc tbd]</entry>
723 </row>
724 <row>
725 <entry><constant>V4L2_BUF_FLAG_TIMECODE</constant></entry>
726 <entry>0x0100</entry>
727 <entry>The <structfield>timecode</structfield> field is valid.
728Drivers set or clear this flag when the <constant>VIDIOC_DQBUF</constant>
729ioctl is called.</entry>
730 </row>
731 <row>
732 <entry><constant>V4L2_BUF_FLAG_INPUT</constant></entry>
733 <entry>0x0200</entry>
734 <entry>The <structfield>input</structfield> field is valid.
735Applications set or clear this flag before calling the
736<constant>VIDIOC_QBUF</constant> ioctl.</entry>
737 </row>
738 </tbody>
739 </tgroup>
740 </table>
741
742 <table pgwide="1" frame="none" id="v4l2-memory">
743 <title>enum v4l2_memory</title>
744 <tgroup cols="3">
745 &cs-def;
746 <tbody valign="top">
747 <row>
748 <entry><constant>V4L2_MEMORY_MMAP</constant></entry>
749 <entry>1</entry>
750 <entry>The buffer is used for <link linkend="mmap">memory
751mapping</link> I/O.</entry>
752 </row>
753 <row>
754 <entry><constant>V4L2_MEMORY_USERPTR</constant></entry>
755 <entry>2</entry>
756 <entry>The buffer is used for <link linkend="userp">user
757pointer</link> I/O.</entry>
758 </row>
759 <row>
760 <entry><constant>V4L2_MEMORY_OVERLAY</constant></entry>
761 <entry>3</entry>
762 <entry>[to do]</entry>
763 </row>
764 </tbody>
765 </tgroup>
766 </table>
767
768 <section>
769 <title>Timecodes</title>
770
771 <para>The <structname>v4l2_timecode</structname> structure is
772designed to hold a <xref linkend="smpte12m" /> or similar timecode.
773(struct <structname>timeval</structname> timestamps are stored in
774&v4l2-buffer; field <structfield>timestamp</structfield>.)</para>
775
776 <table frame="none" pgwide="1" id="v4l2-timecode">
777 <title>struct <structname>v4l2_timecode</structname></title>
778 <tgroup cols="3">
779 &cs-str;
780 <tbody valign="top">
781 <row>
782 <entry>__u32</entry>
783 <entry><structfield>type</structfield></entry>
784 <entry>Frame rate the timecodes are based on, see <xref
785 linkend="timecode-type" />.</entry>
786 </row>
787 <row>
788 <entry>__u32</entry>
789 <entry><structfield>flags</structfield></entry>
790 <entry>Timecode flags, see <xref linkend="timecode-flags" />.</entry>
791 </row>
792 <row>
793 <entry>__u8</entry>
794 <entry><structfield>frames</structfield></entry>
795 <entry>Frame count, 0 ... 23/24/29/49/59, depending on the
796 type of timecode.</entry>
797 </row>
798 <row>
799 <entry>__u8</entry>
800 <entry><structfield>seconds</structfield></entry>
801 <entry>Seconds count, 0 ... 59. This is a binary, not BCD number.</entry>
802 </row>
803 <row>
804 <entry>__u8</entry>
805 <entry><structfield>minutes</structfield></entry>
806 <entry>Minutes count, 0 ... 59. This is a binary, not BCD number.</entry>
807 </row>
808 <row>
809 <entry>__u8</entry>
810 <entry><structfield>hours</structfield></entry>
811 <entry>Hours count, 0 ... 29. This is a binary, not BCD number.</entry>
812 </row>
813 <row>
814 <entry>__u8</entry>
815 <entry><structfield>userbits</structfield>[4]</entry>
816 <entry>The "user group" bits from the timecode.</entry>
817 </row>
818 </tbody>
819 </tgroup>
820 </table>
821
822 <table frame="none" pgwide="1" id="timecode-type">
823 <title>Timecode Types</title>
824 <tgroup cols="3">
825 &cs-def;
826 <tbody valign="top">
827 <row>
828 <entry><constant>V4L2_TC_TYPE_24FPS</constant></entry>
829 <entry>1</entry>
830 <entry>24 frames per second, i.&nbsp;e. film.</entry>
831 </row>
832 <row>
833 <entry><constant>V4L2_TC_TYPE_25FPS</constant></entry>
834 <entry>2</entry>
835 <entry>25 frames per second, &ie; PAL or SECAM video.</entry>
836 </row>
837 <row>
838 <entry><constant>V4L2_TC_TYPE_30FPS</constant></entry>
839 <entry>3</entry>
840 <entry>30 frames per second, &ie; NTSC video.</entry>
841 </row>
842 <row>
843 <entry><constant>V4L2_TC_TYPE_50FPS</constant></entry>
844 <entry>4</entry>
845 <entry></entry>
846 </row>
847 <row>
848 <entry><constant>V4L2_TC_TYPE_60FPS</constant></entry>
849 <entry>5</entry>
850 <entry></entry>
851 </row>
852 </tbody>
853 </tgroup>
854 </table>
855
856 <table frame="none" pgwide="1" id="timecode-flags">
857 <title>Timecode Flags</title>
858 <tgroup cols="3">
859 &cs-def;
860 <tbody valign="top">
861 <row>
862 <entry><constant>V4L2_TC_FLAG_DROPFRAME</constant></entry>
863 <entry>0x0001</entry>
864 <entry>Indicates "drop frame" semantics for counting frames
865in 29.97 fps material. When set, frame numbers 0 and 1 at the start of
866each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the
867count.</entry>
868 </row>
869 <row>
870 <entry><constant>V4L2_TC_FLAG_COLORFRAME</constant></entry>
871 <entry>0x0002</entry>
872 <entry>The "color frame" flag.</entry>
873 </row>
874 <row>
875 <entry><constant>V4L2_TC_USERBITS_field</constant></entry>
876 <entry>0x000C</entry>
877 <entry>Field mask for the "binary group flags".</entry>
878 </row>
879 <row>
880 <entry><constant>V4L2_TC_USERBITS_USERDEFINED</constant></entry>
881 <entry>0x0000</entry>
882 <entry>Unspecified format.</entry>
883 </row>
884 <row>
885 <entry><constant>V4L2_TC_USERBITS_8BITCHARS</constant></entry>
886 <entry>0x0008</entry>
887 <entry>8-bit ISO characters.</entry>
888 </row>
889 </tbody>
890 </tgroup>
891 </table>
892 </section>
893 </section>
894
895 <section id="field-order">
896 <title>Field Order</title>
897
898 <para>We have to distinguish between progressive and interlaced
899video. Progressive video transmits all lines of a video image
900sequentially. Interlaced video divides an image into two fields,
901containing only the odd and even lines of the image, respectively.
902Alternating the so called odd and even field are transmitted, and due
903to a small delay between fields a cathode ray TV displays the lines
904interleaved, yielding the original frame. This curious technique was
905invented because at refresh rates similar to film the image would
906fade out too quickly. Transmitting fields reduces the flicker without
907the necessity of doubling the frame rate and with it the bandwidth
908required for each channel.</para>
909
910 <para>It is important to understand a video camera does not expose
911one frame at a time, merely transmitting the frames separated into
912fields. The fields are in fact captured at two different instances in
913time. An object on screen may well move between one field and the
914next. For applications analysing motion it is of paramount importance
915to recognize which field of a frame is older, the <emphasis>temporal
916order</emphasis>.</para>
917
918 <para>When the driver provides or accepts images field by field
919rather than interleaved, it is also important applications understand
920how the fields combine to frames. We distinguish between top and
921bottom fields, the <emphasis>spatial order</emphasis>: The first line
922of the top field is the first line of an interlaced frame, the first
923line of the bottom field is the second line of that frame.</para>
924
925 <para>However because fields were captured one after the other,
926arguing whether a frame commences with the top or bottom field is
927pointless. Any two successive top and bottom, or bottom and top fields
928yield a valid frame. Only when the source was progressive to begin
929with, &eg; when transferring film to video, two fields may come from
930the same frame, creating a natural order.</para>
931
932 <para>Counter to intuition the top field is not necessarily the
933older field. Whether the older field contains the top or bottom lines
934is a convention determined by the video standard. Hence the
935distinction between temporal and spatial order of fields. The diagrams
936below should make this clearer.</para>
937
938 <para>All video capture and output devices must report the current
939field order. Some drivers may permit the selection of a different
940order, to this end applications initialize the
941<structfield>field</structfield> field of &v4l2-pix-format; before
942calling the &VIDIOC-S-FMT; ioctl. If this is not desired it should
943have the value <constant>V4L2_FIELD_ANY</constant> (0).</para>
944
945 <table frame="none" pgwide="1" id="v4l2-field">
946 <title>enum v4l2_field</title>
947 <tgroup cols="3">
948 &cs-def;
949 <tbody valign="top">
950 <row>
951 <entry><constant>V4L2_FIELD_ANY</constant></entry>
952 <entry>0</entry>
953 <entry>Applications request this field order when any
954one of the <constant>V4L2_FIELD_NONE</constant>,
955<constant>V4L2_FIELD_TOP</constant>,
956<constant>V4L2_FIELD_BOTTOM</constant>, or
957<constant>V4L2_FIELD_INTERLACED</constant> formats is acceptable.
958Drivers choose depending on hardware capabilities or e.&nbsp;g. the
959requested image size, and return the actual field order. &v4l2-buffer;
960<structfield>field</structfield> can never be
961<constant>V4L2_FIELD_ANY</constant>.</entry>
962 </row>
963 <row>
964 <entry><constant>V4L2_FIELD_NONE</constant></entry>
965 <entry>1</entry>
966 <entry>Images are in progressive format, not interlaced.
967The driver may also indicate this order when it cannot distinguish
968between <constant>V4L2_FIELD_TOP</constant> and
969<constant>V4L2_FIELD_BOTTOM</constant>.</entry>
970 </row>
971 <row>
972 <entry><constant>V4L2_FIELD_TOP</constant></entry>
973 <entry>2</entry>
974 <entry>Images consist of the top field only.</entry>
975 </row>
976 <row>
977 <entry><constant>V4L2_FIELD_BOTTOM</constant></entry>
978 <entry>3</entry>
979 <entry>Images consist of the bottom field only.
980Applications may wish to prevent a device from capturing interlaced
981images because they will have "comb" or "feathering" artefacts around
982moving objects.</entry>
983 </row>
984 <row>
985 <entry><constant>V4L2_FIELD_INTERLACED</constant></entry>
986 <entry>4</entry>
987 <entry>Images contain both fields, interleaved line by
988line. The temporal order of the fields (whether the top or bottom
989field is first transmitted) depends on the current video standard.
990M/NTSC transmits the bottom field first, all other standards the top
991field first.</entry>
992 </row>
993 <row>
994 <entry><constant>V4L2_FIELD_SEQ_TB</constant></entry>
995 <entry>5</entry>
996 <entry>Images contain both fields, the top field lines
997are stored first in memory, immediately followed by the bottom field
998lines. Fields are always stored in temporal order, the older one first
999in memory. Image sizes refer to the frame, not fields.</entry>
1000 </row>
1001 <row>
1002 <entry><constant>V4L2_FIELD_SEQ_BT</constant></entry>
1003 <entry>6</entry>
1004 <entry>Images contain both fields, the bottom field
1005lines are stored first in memory, immediately followed by the top
1006field lines. Fields are always stored in temporal order, the older one
1007first in memory. Image sizes refer to the frame, not fields.</entry>
1008 </row>
1009 <row>
1010 <entry><constant>V4L2_FIELD_ALTERNATE</constant></entry>
1011 <entry>7</entry>
1012 <entry>The two fields of a frame are passed in separate
1013buffers, in temporal order, &ie; the older one first. To indicate the field
1014parity (whether the current field is a top or bottom field) the driver
1015or application, depending on data direction, must set &v4l2-buffer;
1016<structfield>field</structfield> to
1017<constant>V4L2_FIELD_TOP</constant> or
1018<constant>V4L2_FIELD_BOTTOM</constant>. Any two successive fields pair
1019to build a frame. If fields are successive, without any dropped fields
1020between them (fields can drop individually), can be determined from
1021the &v4l2-buffer; <structfield>sequence</structfield> field. Image
1022sizes refer to the frame, not fields. This format cannot be selected
1023when using the read/write I/O method.<!-- Where it's indistinguishable
1024from V4L2_FIELD_SEQ_*. --></entry>
1025 </row>
1026 <row>
1027 <entry><constant>V4L2_FIELD_INTERLACED_TB</constant></entry>
1028 <entry>8</entry>
1029 <entry>Images contain both fields, interleaved line by
1030line, top field first. The top field is transmitted first.</entry>
1031 </row>
1032 <row>
1033 <entry><constant>V4L2_FIELD_INTERLACED_BT</constant></entry>
1034 <entry>9</entry>
1035 <entry>Images contain both fields, interleaved line by
1036line, top field first. The bottom field is transmitted first.</entry>
1037 </row>
1038 </tbody>
1039 </tgroup>
1040 </table>
1041
1042 <figure id="fieldseq-tb">
1043 <title>Field Order, Top Field First Transmitted</title>
1044 <mediaobject>
1045 <imageobject>
1046 <imagedata fileref="fieldseq_tb.pdf" format="PS" />
1047 </imageobject>
1048 <imageobject>
1049 <imagedata fileref="fieldseq_tb.gif" format="GIF" />
1050 </imageobject>
1051 </mediaobject>
1052 </figure>
1053
1054 <figure id="fieldseq-bt">
1055 <title>Field Order, Bottom Field First Transmitted</title>
1056 <mediaobject>
1057 <imageobject>
1058 <imagedata fileref="fieldseq_bt.pdf" format="PS" />
1059 </imageobject>
1060 <imageobject>
1061 <imagedata fileref="fieldseq_bt.gif" format="GIF" />
1062 </imageobject>
1063 </mediaobject>
1064 </figure>
1065 </section>
1066
1067 <!--
1068Local Variables:
1069mode: sgml
1070sgml-parent-document: "v4l2.sgml"
1071indent-tabs-mode: nil
1072End:
1073 -->