Path: blob/master/Documentation/DocBook/v4l/io.xml
10823 views
<title>Input/Output</title>12<para>The V4L2 API defines several different methods to read from or3write to a device. All drivers exchanging data with applications must4support at least one of them.</para>56<para>The classic I/O method using the <function>read()</function>7and <function>write()</function> function is automatically selected8after opening a V4L2 device. When the driver does not support this9method attempts to read or write will fail at any time.</para>1011<para>Other methods must be negotiated. To select the streaming I/O12method with memory mapped or user buffers applications call the13&VIDIOC-REQBUFS; ioctl. The asynchronous I/O method is not defined14yet.</para>1516<para>Video overlay can be considered another I/O method, although17the application does not directly receive the image data. It is18selected by initiating video overlay with the &VIDIOC-S-FMT; ioctl.19For more information see <xref linkend="overlay" />.</para>2021<para>Generally exactly one I/O method, including overlay, is22associated with each file descriptor. The only exceptions are23applications not exchanging data with a driver ("panel applications",24see <xref linkend="open" />) and drivers permitting simultaneous video capturing25and overlay using the same file descriptor, for compatibility with V4L26and earlier versions of V4L2.</para>2728<para><constant>VIDIOC_S_FMT</constant> and29<constant>VIDIOC_REQBUFS</constant> would permit this to some degree,30but for simplicity drivers need not support switching the I/O method31(after first switching away from read/write) other than by closing32and reopening the device.</para>3334<para>The following sections describe the various I/O methods in35more detail.</para>3637<section id="rw">38<title>Read/Write</title>3940<para>Input and output devices support the41<function>read()</function> and <function>write()</function> function,42respectively, when the <constant>V4L2_CAP_READWRITE</constant> flag in43the <structfield>capabilities</structfield> field of &v4l2-capability;44returned by the &VIDIOC-QUERYCAP; ioctl is set.</para>4546<para>Drivers may need the CPU to copy the data, but they may also47support DMA to or from user memory, so this I/O method is not48necessarily less efficient than other methods merely exchanging buffer49pointers. It is considered inferior though because no meta-information50like frame counters or timestamps are passed. This information is51necessary to recognize frame dropping and to synchronize with other52data streams. However this is also the simplest I/O method, requiring53little or no setup to exchange data. It permits command line stunts54like this (the <application>vidctrl</application> tool is55fictitious):</para>5657<informalexample>58<screen>59> vidctrl /dev/video --input=0 --format=YUYV --size=352x28860> dd if=/dev/video of=myimage.422 bs=202752 count=161</screen>62</informalexample>6364<para>To read from the device applications use the65&func-read; function, to write the &func-write; function.66Drivers must implement one I/O method if they67exchange data with applications, but it need not be this.<footnote>68<para>It would be desirable if applications could depend on69drivers supporting all I/O interfaces, but as much as the complex70memory mapping I/O can be inadequate for some devices we have no71reason to require this interface, which is most useful for simple72applications capturing still images.</para>73</footnote> When reading or writing is supported, the driver74must also support the &func-select; and &func-poll;75function.<footnote>76<para>At the driver level <function>select()</function> and77<function>poll()</function> are the same, and78<function>select()</function> is too important to be optional.</para>79</footnote></para>80</section>8182<section id="mmap">83<title>Streaming I/O (Memory Mapping)</title>8485<para>Input and output devices support this I/O method when the86<constant>V4L2_CAP_STREAMING</constant> flag in the87<structfield>capabilities</structfield> field of &v4l2-capability;88returned by the &VIDIOC-QUERYCAP; ioctl is set. There are two89streaming methods, to determine if the memory mapping flavor is90supported applications must call the &VIDIOC-REQBUFS; ioctl.</para>9192<para>Streaming is an I/O method where only pointers to buffers93are exchanged between application and driver, the data itself is not94copied. Memory mapping is primarily intended to map buffers in device95memory into the application's address space. Device memory can be for96example the video memory on a graphics card with a video capture97add-on. However, being the most efficient I/O method available for a98long time, many other drivers support streaming as well, allocating99buffers in DMA-able main memory.</para>100101<para>A driver can support many sets of buffers. Each set is102identified by a unique buffer type value. The sets are independent and103each set can hold a different type of data. To access different sets104at the same time different file descriptors must be used.<footnote>105<para>One could use one file descriptor and set the buffer106type field accordingly when calling &VIDIOC-QBUF; etc., but it makes107the <function>select()</function> function ambiguous. We also like the108clean approach of one file descriptor per logical stream. Video109overlay for example is also a logical stream, although the CPU is not110needed for continuous operation.</para>111</footnote></para>112113<para>To allocate device buffers applications call the114&VIDIOC-REQBUFS; ioctl with the desired number of buffers and buffer115type, for example <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant>.116This ioctl can also be used to change the number of buffers or to free117the allocated memory, provided none of the buffers are still118mapped.</para>119120<para>Before applications can access the buffers they must map121them into their address space with the &func-mmap; function. The122location of the buffers in device memory can be determined with the123&VIDIOC-QUERYBUF; ioctl. In the single-planar API case, the124<structfield>m.offset</structfield> and <structfield>length</structfield>125returned in a &v4l2-buffer; are passed as sixth and second parameter to the126<function>mmap()</function> function. When using the multi-planar API,127struct &v4l2-buffer; contains an array of &v4l2-plane; structures, each128containing its own <structfield>m.offset</structfield> and129<structfield>length</structfield>. When using the multi-planar API, every130plane of every buffer has to be mapped separately, so the number of131calls to &func-mmap; should be equal to number of buffers times number of132planes in each buffer. The offset and length values must not be modified.133Remember, the buffers are allocated in physical memory, as opposed to virtual134memory, which can be swapped out to disk. Applications should free the buffers135as soon as possible with the &func-munmap; function.</para>136137<example>138<title>Mapping buffers in the single-planar API</title>139<programlisting>140&v4l2-requestbuffers; reqbuf;141struct {142void *start;143size_t length;144} *buffers;145unsigned int i;146147memset(&reqbuf, 0, sizeof(reqbuf));148reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;149reqbuf.memory = V4L2_MEMORY_MMAP;150reqbuf.count = 20;151152if (-1 == ioctl (fd, &VIDIOC-REQBUFS;, &reqbuf)) {153if (errno == EINVAL)154printf("Video capturing or mmap-streaming is not supported\n");155else156perror("VIDIOC_REQBUFS");157158exit(EXIT_FAILURE);159}160161/* We want at least five buffers. */162163if (reqbuf.count < 5) {164/* You may need to free the buffers here. */165printf("Not enough buffer memory\n");166exit(EXIT_FAILURE);167}168169buffers = calloc(reqbuf.count, sizeof(*buffers));170assert(buffers != NULL);171172for (i = 0; i < reqbuf.count; i++) {173&v4l2-buffer; buffer;174175memset(&buffer, 0, sizeof(buffer));176buffer.type = reqbuf.type;177buffer.memory = V4L2_MEMORY_MMAP;178buffer.index = i;179180if (-1 == ioctl (fd, &VIDIOC-QUERYBUF;, &buffer)) {181perror("VIDIOC_QUERYBUF");182exit(EXIT_FAILURE);183}184185buffers[i].length = buffer.length; /* remember for munmap() */186187buffers[i].start = mmap(NULL, buffer.length,188PROT_READ | PROT_WRITE, /* recommended */189MAP_SHARED, /* recommended */190fd, buffer.m.offset);191192if (MAP_FAILED == buffers[i].start) {193/* If you do not exit here you should unmap() and free()194the buffers mapped so far. */195perror("mmap");196exit(EXIT_FAILURE);197}198}199200/* Cleanup. */201202for (i = 0; i < reqbuf.count; i++)203munmap(buffers[i].start, buffers[i].length);204</programlisting>205</example>206207<example>208<title>Mapping buffers in the multi-planar API</title>209<programlisting>210&v4l2-requestbuffers; reqbuf;211/* Our current format uses 3 planes per buffer */212#define FMT_NUM_PLANES = 3;213214struct {215void *start[FMT_NUM_PLANES];216size_t length[FMT_NUM_PLANES];217} *buffers;218unsigned int i, j;219220memset(&reqbuf, 0, sizeof(reqbuf));221reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;222reqbuf.memory = V4L2_MEMORY_MMAP;223reqbuf.count = 20;224225if (ioctl(fd, &VIDIOC-REQBUFS;, &reqbuf) < 0) {226if (errno == EINVAL)227printf("Video capturing or mmap-streaming is not supported\n");228else229perror("VIDIOC_REQBUFS");230231exit(EXIT_FAILURE);232}233234/* We want at least five buffers. */235236if (reqbuf.count < 5) {237/* You may need to free the buffers here. */238printf("Not enough buffer memory\n");239exit(EXIT_FAILURE);240}241242buffers = calloc(reqbuf.count, sizeof(*buffers));243assert(buffers != NULL);244245for (i = 0; i < reqbuf.count; i++) {246&v4l2-buffer; buffer;247&v4l2-plane; planes[FMT_NUM_PLANES];248249memset(&buffer, 0, sizeof(buffer));250buffer.type = reqbuf.type;251buffer.memory = V4L2_MEMORY_MMAP;252buffer.index = i;253/* length in struct v4l2_buffer in multi-planar API stores the size254* of planes array. */255buffer.length = FMT_NUM_PLANES;256buffer.m.planes = planes;257258if (ioctl(fd, &VIDIOC-QUERYBUF;, &buffer) < 0) {259perror("VIDIOC_QUERYBUF");260exit(EXIT_FAILURE);261}262263/* Every plane has to be mapped separately */264for (j = 0; j < FMT_NUM_PLANES; j++) {265buffers[i].length[j] = buffer.m.planes[j].length; /* remember for munmap() */266267buffers[i].start[j] = mmap(NULL, buffer.m.planes[j].length,268PROT_READ | PROT_WRITE, /* recommended */269MAP_SHARED, /* recommended */270fd, buffer.m.planes[j].m.offset);271272if (MAP_FAILED == buffers[i].start[j]) {273/* If you do not exit here you should unmap() and free()274the buffers and planes mapped so far. */275perror("mmap");276exit(EXIT_FAILURE);277}278}279}280281/* Cleanup. */282283for (i = 0; i < reqbuf.count; i++)284for (j = 0; j < FMT_NUM_PLANES; j++)285munmap(buffers[i].start[j], buffers[i].length[j]);286</programlisting>287</example>288289<para>Conceptually streaming drivers maintain two buffer queues, an incoming290and an outgoing queue. They separate the synchronous capture or output291operation locked to a video clock from the application which is292subject to random disk or network delays and preemption by293other processes, thereby reducing the probability of data loss.294The queues are organized as FIFOs, buffers will be295output in the order enqueued in the incoming FIFO, and were296captured in the order dequeued from the outgoing FIFO.</para>297298<para>The driver may require a minimum number of buffers enqueued299at all times to function, apart of this no limit exists on the number300of buffers applications can enqueue in advance, or dequeue and301process. They can also enqueue in a different order than buffers have302been dequeued, and the driver can <emphasis>fill</emphasis> enqueued303<emphasis>empty</emphasis> buffers in any order. <footnote>304<para>Random enqueue order permits applications processing305images out of order (such as video codecs) to return buffers earlier,306reducing the probability of data loss. Random fill order allows307drivers to reuse buffers on a LIFO-basis, taking advantage of caches308holding scatter-gather lists and the like.</para>309</footnote> The index number of a buffer (&v4l2-buffer;310<structfield>index</structfield>) plays no role here, it only311identifies the buffer.</para>312313<para>Initially all mapped buffers are in dequeued state,314inaccessible by the driver. For capturing applications it is customary315to first enqueue all mapped buffers, then to start capturing and enter316the read loop. Here the application waits until a filled buffer can be317dequeued, and re-enqueues the buffer when the data is no longer318needed. Output applications fill and enqueue buffers, when enough319buffers are stacked up the output is started with320<constant>VIDIOC_STREAMON</constant>. In the write loop, when321the application runs out of free buffers, it must wait until an empty322buffer can be dequeued and reused.</para>323324<para>To enqueue and dequeue a buffer applications use the325&VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl. The status of a buffer being326mapped, enqueued, full or empty can be determined at any time using the327&VIDIOC-QUERYBUF; ioctl. Two methods exist to suspend execution of the328application until one or more buffers can be dequeued. By default329<constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the330outgoing queue. When the <constant>O_NONBLOCK</constant> flag was331given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>332returns immediately with an &EAGAIN; when no buffer is available. The333&func-select; or &func-poll; function are always available.</para>334335<para>To start and stop capturing or output applications call the336&VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note337<constant>VIDIOC_STREAMOFF</constant> removes all buffers from both338queues as a side effect. Since there is no notion of doing anything339"now" on a multitasking system, if an application needs to synchronize340with another event it should examine the &v4l2-buffer;341<structfield>timestamp</structfield> of captured buffers, or set the342field before enqueuing buffers for output.</para>343344<para>Drivers implementing memory mapping I/O must345support the <constant>VIDIOC_REQBUFS</constant>,346<constant>VIDIOC_QUERYBUF</constant>,347<constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,348<constant>VIDIOC_STREAMON</constant> and349<constant>VIDIOC_STREAMOFF</constant> ioctl, the350<function>mmap()</function>, <function>munmap()</function>,351<function>select()</function> and <function>poll()</function>352function.<footnote>353<para>At the driver level <function>select()</function> and354<function>poll()</function> are the same, and355<function>select()</function> is too important to be optional. The356rest should be evident.</para>357</footnote></para>358359<para>[capture example]</para>360361</section>362363<section id="userp">364<title>Streaming I/O (User Pointers)</title>365366<para>Input and output devices support this I/O method when the367<constant>V4L2_CAP_STREAMING</constant> flag in the368<structfield>capabilities</structfield> field of &v4l2-capability;369returned by the &VIDIOC-QUERYCAP; ioctl is set. If the particular user370pointer method (not only memory mapping) is supported must be371determined by calling the &VIDIOC-REQBUFS; ioctl.</para>372373<para>This I/O method combines advantages of the read/write and374memory mapping methods. Buffers (planes) are allocated by the application375itself, and can reside for example in virtual or shared memory. Only376pointers to data are exchanged, these pointers and meta-information377are passed in &v4l2-buffer; (or in &v4l2-plane; in the multi-planar API case).378The driver must be switched into user pointer I/O mode by calling the379&VIDIOC-REQBUFS; with the desired buffer type. No buffers (planes) are allocated380beforehand, consequently they are not indexed and cannot be queried like mapped381buffers with the <constant>VIDIOC_QUERYBUF</constant> ioctl.</para>382383<example>384<title>Initiating streaming I/O with user pointers</title>385386<programlisting>387&v4l2-requestbuffers; reqbuf;388389memset (&reqbuf, 0, sizeof (reqbuf));390reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;391reqbuf.memory = V4L2_MEMORY_USERPTR;392393if (ioctl (fd, &VIDIOC-REQBUFS;, &reqbuf) == -1) {394if (errno == EINVAL)395printf ("Video capturing or user pointer streaming is not supported\n");396else397perror ("VIDIOC_REQBUFS");398399exit (EXIT_FAILURE);400}401</programlisting>402</example>403404<para>Buffer (plane) addresses and sizes are passed on the fly with the405&VIDIOC-QBUF; ioctl. Although buffers are commonly cycled,406applications can pass different addresses and sizes at each407<constant>VIDIOC_QBUF</constant> call. If required by the hardware the408driver swaps memory pages within physical memory to create a409continuous area of memory. This happens transparently to the410application in the virtual memory subsystem of the kernel. When buffer411pages have been swapped out to disk they are brought back and finally412locked in physical memory for DMA.<footnote>413<para>We expect that frequently used buffers are typically not414swapped out. Anyway, the process of swapping, locking or generating415scatter-gather lists may be time consuming. The delay can be masked by416the depth of the incoming buffer queue, and perhaps by maintaining417caches assuming a buffer will be soon enqueued again. On the other418hand, to optimize memory usage drivers can limit the number of buffers419locked in advance and recycle the most recently used buffers first. Of420course, the pages of empty buffers in the incoming queue need not be421saved to disk. Output buffers must be saved on the incoming and422outgoing queue because an application may share them with other423processes.</para>424</footnote></para>425426<para>Filled or displayed buffers are dequeued with the427&VIDIOC-DQBUF; ioctl. The driver can unlock the memory pages at any428time between the completion of the DMA and this ioctl. The memory is429also unlocked when &VIDIOC-STREAMOFF; is called, &VIDIOC-REQBUFS;, or430when the device is closed. Applications must take care not to free431buffers without dequeuing. For once, the buffers remain locked until432further, wasting physical memory. Second the driver will not be433notified when the memory is returned to the application's free list434and subsequently reused for other purposes, possibly completing the435requested DMA and overwriting valuable data.</para>436437<para>For capturing applications it is customary to enqueue a438number of empty buffers, to start capturing and enter the read loop.439Here the application waits until a filled buffer can be dequeued, and440re-enqueues the buffer when the data is no longer needed. Output441applications fill and enqueue buffers, when enough buffers are stacked442up output is started. In the write loop, when the application443runs out of free buffers it must wait until an empty buffer can be444dequeued and reused. Two methods exist to suspend execution of the445application until one or more buffers can be dequeued. By default446<constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the447outgoing queue. When the <constant>O_NONBLOCK</constant> flag was448given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>449returns immediately with an &EAGAIN; when no buffer is available. The450&func-select; or &func-poll; function are always available.</para>451452<para>To start and stop capturing or output applications call the453&VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note454<constant>VIDIOC_STREAMOFF</constant> removes all buffers from both455queues and unlocks all buffers as a side effect. Since there is no456notion of doing anything "now" on a multitasking system, if an457application needs to synchronize with another event it should examine458the &v4l2-buffer; <structfield>timestamp</structfield> of captured459buffers, or set the field before enqueuing buffers for output.</para>460461<para>Drivers implementing user pointer I/O must462support the <constant>VIDIOC_REQBUFS</constant>,463<constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,464<constant>VIDIOC_STREAMON</constant> and465<constant>VIDIOC_STREAMOFF</constant> ioctl, the466<function>select()</function> and <function>poll()</function> function.<footnote>467<para>At the driver level <function>select()</function> and468<function>poll()</function> are the same, and469<function>select()</function> is too important to be optional. The470rest should be evident.</para>471</footnote></para>472</section>473474<section id="async">475<title>Asynchronous I/O</title>476477<para>This method is not defined yet.</para>478</section>479480<section id="buffer">481<title>Buffers</title>482483<para>A buffer contains data exchanged by application and484driver using one of the Streaming I/O methods. In the multi-planar API, the485data is held in planes, while the buffer structure acts as a container486for the planes. Only pointers to buffers (planes) are exchanged, the data487itself is not copied. These pointers, together with meta-information like488timestamps or field parity, are stored in a struct489<structname>v4l2_buffer</structname>, argument to490the &VIDIOC-QUERYBUF;, &VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl.491In the multi-planar API, some plane-specific members of struct492<structname>v4l2_buffer</structname>, such as pointers and sizes for each493plane, are stored in struct <structname>v4l2_plane</structname> instead.494In that case, struct <structname>v4l2_buffer</structname> contains an array of495plane structures.</para>496497<para>Nominally timestamps refer to the first data byte transmitted.498In practice however the wide range of hardware covered by the V4L2 API499limits timestamp accuracy. Often an interrupt routine will500sample the system clock shortly after the field or frame was stored501completely in memory. So applications must expect a constant502difference up to one field or frame period plus a small (few scan503lines) random error. The delay and error can be much504larger due to compression or transmission over an external bus when505the frames are not properly stamped by the sender. This is frequently506the case with USB cameras. Here timestamps refer to the instant the507field or frame was received by the driver, not the capture time. These508devices identify by not enumerating any video standards, see <xref509linkend="standard" />.</para>510511<para>Similar limitations apply to output timestamps. Typically512the video hardware locks to a clock controlling the video timing, the513horizontal and vertical synchronization pulses. At some point in the514line sequence, possibly the vertical blanking, an interrupt routine515samples the system clock, compares against the timestamp and programs516the hardware to repeat the previous field or frame, or to display the517buffer contents.</para>518519<para>Apart of limitations of the video device and natural520inaccuracies of all clocks, it should be noted system time itself is521not perfectly stable. It can be affected by power saving cycles,522warped to insert leap seconds, or even turned back or forth by the523system administrator affecting long term measurements. <footnote>524<para>Since no other Linux multimedia525API supports unadjusted time it would be foolish to introduce here. We526must use a universally supported clock to synchronize different media,527hence time of day.</para>528</footnote></para>529530<table frame="none" pgwide="1" id="v4l2-buffer">531<title>struct <structname>v4l2_buffer</structname></title>532<tgroup cols="4">533&cs-ustr;534<tbody valign="top">535<row>536<entry>__u32</entry>537<entry><structfield>index</structfield></entry>538<entry></entry>539<entry>Number of the buffer, set by the application. This540field is only used for <link linkend="mmap">memory mapping</link> I/O541and can range from zero to the number of buffers allocated542with the &VIDIOC-REQBUFS; ioctl (&v4l2-requestbuffers; <structfield>count</structfield>) minus one.</entry>543</row>544<row>545<entry>&v4l2-buf-type;</entry>546<entry><structfield>type</structfield></entry>547<entry></entry>548<entry>Type of the buffer, same as &v4l2-format;549<structfield>type</structfield> or &v4l2-requestbuffers;550<structfield>type</structfield>, set by the application.</entry>551</row>552<row>553<entry>__u32</entry>554<entry><structfield>bytesused</structfield></entry>555<entry></entry>556<entry>The number of bytes occupied by the data in the557buffer. It depends on the negotiated data format and may change with558each buffer for compressed variable size data like JPEG images.559Drivers must set this field when <structfield>type</structfield>560refers to an input stream, applications when an output stream.</entry>561</row>562<row>563<entry>__u32</entry>564<entry><structfield>flags</structfield></entry>565<entry></entry>566<entry>Flags set by the application or driver, see <xref567linkend="buffer-flags" />.</entry>568</row>569<row>570<entry>&v4l2-field;</entry>571<entry><structfield>field</structfield></entry>572<entry></entry>573<entry>Indicates the field order of the image in the574buffer, see <xref linkend="v4l2-field" />. This field is not used when575the buffer contains VBI data. Drivers must set it when576<structfield>type</structfield> refers to an input stream,577applications when an output stream.</entry>578</row>579<row>580<entry>struct timeval</entry>581<entry><structfield>timestamp</structfield></entry>582<entry></entry>583<entry><para>For input streams this is the584system time (as returned by the <function>gettimeofday()</function>585function) when the first data byte was captured. For output streams586the data will not be displayed before this time, secondary to the587nominal frame rate determined by the current video standard in588enqueued order. Applications can for example zero this field to589display frames as soon as possible. The driver stores the time at590which the first data byte was actually sent out in the591<structfield>timestamp</structfield> field. This permits592applications to monitor the drift between the video and system593clock.</para></entry>594</row>595<row>596<entry>&v4l2-timecode;</entry>597<entry><structfield>timecode</structfield></entry>598<entry></entry>599<entry>When <structfield>type</structfield> is600<constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> and the601<constant>V4L2_BUF_FLAG_TIMECODE</constant> flag is set in602<structfield>flags</structfield>, this structure contains a frame603timecode. In <link linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link>604mode the top and bottom field contain the same timecode.605Timecodes are intended to help video editing and are typically recorded on606video tapes, but also embedded in compressed formats like MPEG. This607field is independent of the <structfield>timestamp</structfield> and608<structfield>sequence</structfield> fields.</entry>609</row>610<row>611<entry>__u32</entry>612<entry><structfield>sequence</structfield></entry>613<entry></entry>614<entry>Set by the driver, counting the frames in the615sequence.</entry>616</row>617<row>618<entry spanname="hspan"><para>In <link619linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link> mode the top and620bottom field have the same sequence number. The count starts at zero621and includes dropped or repeated frames. A dropped frame was received622by an input device but could not be stored due to lack of free buffer623space. A repeated frame was displayed again by an output device624because the application did not pass new data in625time.</para><para>Note this may count the frames received626e.g. over USB, without taking into account the frames dropped by the627remote hardware due to limited compression throughput or bus628bandwidth. These devices identify by not enumerating any video629standards, see <xref linkend="standard" />.</para></entry>630</row>631<row>632<entry>&v4l2-memory;</entry>633<entry><structfield>memory</structfield></entry>634<entry></entry>635<entry>This field must be set by applications and/or drivers636in accordance with the selected I/O method.</entry>637</row>638<row>639<entry>union</entry>640<entry><structfield>m</structfield></entry>641</row>642<row>643<entry></entry>644<entry>__u32</entry>645<entry><structfield>offset</structfield></entry>646<entry>For the single-planar API and when647<structfield>memory</structfield> is <constant>V4L2_MEMORY_MMAP</constant> this648is the offset of the buffer from the start of the device memory. The value is649returned by the driver and apart of serving as parameter to the &func-mmap;650function not useful for applications. See <xref linkend="mmap" /> for details651</entry>652</row>653<row>654<entry></entry>655<entry>unsigned long</entry>656<entry><structfield>userptr</structfield></entry>657<entry>For the single-planar API and when658<structfield>memory</structfield> is <constant>V4L2_MEMORY_USERPTR</constant>659this is a pointer to the buffer (casted to unsigned long type) in virtual660memory, set by the application. See <xref linkend="userp" /> for details.661</entry>662</row>663<row>664<entry></entry>665<entry>struct v4l2_plane</entry>666<entry><structfield>*planes</structfield></entry>667<entry>When using the multi-planar API, contains a userspace pointer668to an array of &v4l2-plane;. The size of the array should be put669in the <structfield>length</structfield> field of this670<structname>v4l2_buffer</structname> structure.</entry>671</row>672<row>673<entry>__u32</entry>674<entry><structfield>length</structfield></entry>675<entry></entry>676<entry>Size of the buffer (not the payload) in bytes for the677single-planar API. For the multi-planar API should contain the678number of elements in the <structfield>planes</structfield> array.679</entry>680</row>681<row>682<entry>__u32</entry>683<entry><structfield>input</structfield></entry>684<entry></entry>685<entry>Some video capture drivers support rapid and686synchronous video input changes, a function useful for example in687video surveillance applications. For this purpose applications set the688<constant>V4L2_BUF_FLAG_INPUT</constant> flag, and this field to the689number of a video input as in &v4l2-input; field690<structfield>index</structfield>.</entry>691</row>692<row>693<entry>__u32</entry>694<entry><structfield>reserved</structfield></entry>695<entry></entry>696<entry>A place holder for future extensions and custom697(driver defined) buffer types698<constant>V4L2_BUF_TYPE_PRIVATE</constant> and higher. Applications699should set this to 0.</entry>700</row>701</tbody>702</tgroup>703</table>704705<table frame="none" pgwide="1" id="v4l2-plane">706<title>struct <structname>v4l2_plane</structname></title>707<tgroup cols="4">708&cs-ustr;709<tbody valign="top">710<row>711<entry>__u32</entry>712<entry><structfield>bytesused</structfield></entry>713<entry></entry>714<entry>The number of bytes occupied by data in the plane715(its payload).</entry>716</row>717<row>718<entry>__u32</entry>719<entry><structfield>length</structfield></entry>720<entry></entry>721<entry>Size in bytes of the plane (not its payload).</entry>722</row>723<row>724<entry>union</entry>725<entry><structfield>m</structfield></entry>726<entry></entry>727<entry></entry>728</row>729<row>730<entry></entry>731<entry>__u32</entry>732<entry><structfield>mem_offset</structfield></entry>733<entry>When the memory type in the containing &v4l2-buffer; is734<constant>V4L2_MEMORY_MMAP</constant>, this is the value that735should be passed to &func-mmap;, similar to the736<structfield>offset</structfield> field in &v4l2-buffer;.</entry>737</row>738<row>739<entry></entry>740<entry>__unsigned long</entry>741<entry><structfield>userptr</structfield></entry>742<entry>When the memory type in the containing &v4l2-buffer; is743<constant>V4L2_MEMORY_USERPTR</constant>, this is a userspace744pointer to the memory allocated for this plane by an application.745</entry>746</row>747<row>748<entry>__u32</entry>749<entry><structfield>data_offset</structfield></entry>750<entry></entry>751<entry>Offset in bytes to video data in the plane, if applicable.752</entry>753</row>754<row>755<entry>__u32</entry>756<entry><structfield>reserved[11]</structfield></entry>757<entry></entry>758<entry>Reserved for future use. Should be zeroed by an759application.</entry>760</row>761</tbody>762</tgroup>763</table>764765<table frame="none" pgwide="1" id="v4l2-buf-type">766<title>enum v4l2_buf_type</title>767<tgroup cols="3">768&cs-def;769<tbody valign="top">770<row>771<entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant></entry>772<entry>1</entry>773<entry>Buffer of a single-planar video capture stream, see <xref774linkend="capture" />.</entry>775</row>776<row>777<entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE</constant>778</entry>779<entry>9</entry>780<entry>Buffer of a multi-planar video capture stream, see <xref781linkend="capture" />.</entry>782</row>783<row>784<entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT</constant></entry>785<entry>2</entry>786<entry>Buffer of a single-planar video output stream, see <xref787linkend="output" />.</entry>788</row>789<row>790<entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE</constant>791</entry>792<entry>10</entry>793<entry>Buffer of a multi-planar video output stream, see <xref794linkend="output" />.</entry>795</row>796<row>797<entry><constant>V4L2_BUF_TYPE_VIDEO_OVERLAY</constant></entry>798<entry>3</entry>799<entry>Buffer for video overlay, see <xref linkend="overlay" />.</entry>800</row>801<row>802<entry><constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant></entry>803<entry>4</entry>804<entry>Buffer of a raw VBI capture stream, see <xref805linkend="raw-vbi" />.</entry>806</row>807<row>808<entry><constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant></entry>809<entry>5</entry>810<entry>Buffer of a raw VBI output stream, see <xref811linkend="raw-vbi" />.</entry>812</row>813<row>814<entry><constant>V4L2_BUF_TYPE_SLICED_VBI_CAPTURE</constant></entry>815<entry>6</entry>816<entry>Buffer of a sliced VBI capture stream, see <xref817linkend="sliced" />.</entry>818</row>819<row>820<entry><constant>V4L2_BUF_TYPE_SLICED_VBI_OUTPUT</constant></entry>821<entry>7</entry>822<entry>Buffer of a sliced VBI output stream, see <xref823linkend="sliced" />.</entry>824</row>825<row>826<entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY</constant></entry>827<entry>8</entry>828<entry>Buffer for video output overlay (OSD), see <xref829linkend="osd" />. Status: <link830linkend="experimental">Experimental</link>.</entry>831</row>832<row>833<entry><constant>V4L2_BUF_TYPE_PRIVATE</constant></entry>834<entry>0x80</entry>835<entry>This and higher values are reserved for custom836(driver defined) buffer types.</entry>837</row>838</tbody>839</tgroup>840</table>841842<table frame="none" pgwide="1" id="buffer-flags">843<title>Buffer Flags</title>844<tgroup cols="3">845&cs-def;846<tbody valign="top">847<row>848<entry><constant>V4L2_BUF_FLAG_MAPPED</constant></entry>849<entry>0x0001</entry>850<entry>The buffer resides in device memory and has been mapped851into the application's address space, see <xref linkend="mmap" /> for details.852Drivers set or clear this flag when the853<link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link854linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link855linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called. Set by the driver.</entry>856</row>857<row>858<entry><constant>V4L2_BUF_FLAG_QUEUED</constant></entry>859<entry>0x0002</entry>860<entry>Internally drivers maintain two buffer queues, an861incoming and outgoing queue. When this flag is set, the buffer is862currently on the incoming queue. It automatically moves to the863outgoing queue after the buffer has been filled (capture devices) or864displayed (output devices). Drivers set or clear this flag when the865<constant>VIDIOC_QUERYBUF</constant> ioctl is called. After866(successful) calling the <constant>VIDIOC_QBUF </constant>ioctl it is867always set and after <constant>VIDIOC_DQBUF</constant> always868cleared.</entry>869</row>870<row>871<entry><constant>V4L2_BUF_FLAG_DONE</constant></entry>872<entry>0x0004</entry>873<entry>When this flag is set, the buffer is currently on874the outgoing queue, ready to be dequeued from the driver. Drivers set875or clear this flag when the <constant>VIDIOC_QUERYBUF</constant> ioctl876is called. After calling the <constant>VIDIOC_QBUF</constant> or877<constant>VIDIOC_DQBUF</constant> it is always cleared. Of course a878buffer cannot be on both queues at the same time, the879<constant>V4L2_BUF_FLAG_QUEUED</constant> and880<constant>V4L2_BUF_FLAG_DONE</constant> flag are mutually exclusive.881They can be both cleared however, then the buffer is in "dequeued"882state, in the application domain to say so.</entry>883</row>884<row>885<entry><constant>V4L2_BUF_FLAG_ERROR</constant></entry>886<entry>0x0040</entry>887<entry>When this flag is set, the buffer has been dequeued888successfully, although the data might have been corrupted.889This is recoverable, streaming may continue as normal and890the buffer may be reused normally.891Drivers set this flag when the <constant>VIDIOC_DQBUF</constant>892ioctl is called.</entry>893</row>894<row>895<entry><constant>V4L2_BUF_FLAG_KEYFRAME</constant></entry>896<entry>0x0008</entry>897<entry>Drivers set or clear this flag when calling the898<constant>VIDIOC_DQBUF</constant> ioctl. It may be set by video899capture devices when the buffer contains a compressed image which is a900key frame (or field), &ie; can be decompressed on its own.</entry>901</row>902<row>903<entry><constant>V4L2_BUF_FLAG_PFRAME</constant></entry>904<entry>0x0010</entry>905<entry>Similar to <constant>V4L2_BUF_FLAG_KEYFRAME</constant>906this flags predicted frames or fields which contain only differences to a907previous key frame.</entry>908</row>909<row>910<entry><constant>V4L2_BUF_FLAG_BFRAME</constant></entry>911<entry>0x0020</entry>912<entry>Similar to <constant>V4L2_BUF_FLAG_PFRAME</constant>913this is a bidirectional predicted frame or field. [ooc tbd]</entry>914</row>915<row>916<entry><constant>V4L2_BUF_FLAG_TIMECODE</constant></entry>917<entry>0x0100</entry>918<entry>The <structfield>timecode</structfield> field is valid.919Drivers set or clear this flag when the <constant>VIDIOC_DQBUF</constant>920ioctl is called.</entry>921</row>922<row>923<entry><constant>V4L2_BUF_FLAG_INPUT</constant></entry>924<entry>0x0200</entry>925<entry>The <structfield>input</structfield> field is valid.926Applications set or clear this flag before calling the927<constant>VIDIOC_QBUF</constant> ioctl.</entry>928</row>929</tbody>930</tgroup>931</table>932933<table pgwide="1" frame="none" id="v4l2-memory">934<title>enum v4l2_memory</title>935<tgroup cols="3">936&cs-def;937<tbody valign="top">938<row>939<entry><constant>V4L2_MEMORY_MMAP</constant></entry>940<entry>1</entry>941<entry>The buffer is used for <link linkend="mmap">memory942mapping</link> I/O.</entry>943</row>944<row>945<entry><constant>V4L2_MEMORY_USERPTR</constant></entry>946<entry>2</entry>947<entry>The buffer is used for <link linkend="userp">user948pointer</link> I/O.</entry>949</row>950<row>951<entry><constant>V4L2_MEMORY_OVERLAY</constant></entry>952<entry>3</entry>953<entry>[to do]</entry>954</row>955</tbody>956</tgroup>957</table>958959<section>960<title>Timecodes</title>961962<para>The <structname>v4l2_timecode</structname> structure is963designed to hold a <xref linkend="smpte12m" /> or similar timecode.964(struct <structname>timeval</structname> timestamps are stored in965&v4l2-buffer; field <structfield>timestamp</structfield>.)</para>966967<table frame="none" pgwide="1" id="v4l2-timecode">968<title>struct <structname>v4l2_timecode</structname></title>969<tgroup cols="3">970&cs-str;971<tbody valign="top">972<row>973<entry>__u32</entry>974<entry><structfield>type</structfield></entry>975<entry>Frame rate the timecodes are based on, see <xref976linkend="timecode-type" />.</entry>977</row>978<row>979<entry>__u32</entry>980<entry><structfield>flags</structfield></entry>981<entry>Timecode flags, see <xref linkend="timecode-flags" />.</entry>982</row>983<row>984<entry>__u8</entry>985<entry><structfield>frames</structfield></entry>986<entry>Frame count, 0 ... 23/24/29/49/59, depending on the987type of timecode.</entry>988</row>989<row>990<entry>__u8</entry>991<entry><structfield>seconds</structfield></entry>992<entry>Seconds count, 0 ... 59. This is a binary, not BCD number.</entry>993</row>994<row>995<entry>__u8</entry>996<entry><structfield>minutes</structfield></entry>997<entry>Minutes count, 0 ... 59. This is a binary, not BCD number.</entry>998</row>999<row>1000<entry>__u8</entry>1001<entry><structfield>hours</structfield></entry>1002<entry>Hours count, 0 ... 29. This is a binary, not BCD number.</entry>1003</row>1004<row>1005<entry>__u8</entry>1006<entry><structfield>userbits</structfield>[4]</entry>1007<entry>The "user group" bits from the timecode.</entry>1008</row>1009</tbody>1010</tgroup>1011</table>10121013<table frame="none" pgwide="1" id="timecode-type">1014<title>Timecode Types</title>1015<tgroup cols="3">1016&cs-def;1017<tbody valign="top">1018<row>1019<entry><constant>V4L2_TC_TYPE_24FPS</constant></entry>1020<entry>1</entry>1021<entry>24 frames per second, i. e. film.</entry>1022</row>1023<row>1024<entry><constant>V4L2_TC_TYPE_25FPS</constant></entry>1025<entry>2</entry>1026<entry>25 frames per second, &ie; PAL or SECAM video.</entry>1027</row>1028<row>1029<entry><constant>V4L2_TC_TYPE_30FPS</constant></entry>1030<entry>3</entry>1031<entry>30 frames per second, &ie; NTSC video.</entry>1032</row>1033<row>1034<entry><constant>V4L2_TC_TYPE_50FPS</constant></entry>1035<entry>4</entry>1036<entry></entry>1037</row>1038<row>1039<entry><constant>V4L2_TC_TYPE_60FPS</constant></entry>1040<entry>5</entry>1041<entry></entry>1042</row>1043</tbody>1044</tgroup>1045</table>10461047<table frame="none" pgwide="1" id="timecode-flags">1048<title>Timecode Flags</title>1049<tgroup cols="3">1050&cs-def;1051<tbody valign="top">1052<row>1053<entry><constant>V4L2_TC_FLAG_DROPFRAME</constant></entry>1054<entry>0x0001</entry>1055<entry>Indicates "drop frame" semantics for counting frames1056in 29.97 fps material. When set, frame numbers 0 and 1 at the start of1057each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the1058count.</entry>1059</row>1060<row>1061<entry><constant>V4L2_TC_FLAG_COLORFRAME</constant></entry>1062<entry>0x0002</entry>1063<entry>The "color frame" flag.</entry>1064</row>1065<row>1066<entry><constant>V4L2_TC_USERBITS_field</constant></entry>1067<entry>0x000C</entry>1068<entry>Field mask for the "binary group flags".</entry>1069</row>1070<row>1071<entry><constant>V4L2_TC_USERBITS_USERDEFINED</constant></entry>1072<entry>0x0000</entry>1073<entry>Unspecified format.</entry>1074</row>1075<row>1076<entry><constant>V4L2_TC_USERBITS_8BITCHARS</constant></entry>1077<entry>0x0008</entry>1078<entry>8-bit ISO characters.</entry>1079</row>1080</tbody>1081</tgroup>1082</table>1083</section>1084</section>10851086<section id="field-order">1087<title>Field Order</title>10881089<para>We have to distinguish between progressive and interlaced1090video. Progressive video transmits all lines of a video image1091sequentially. Interlaced video divides an image into two fields,1092containing only the odd and even lines of the image, respectively.1093Alternating the so called odd and even field are transmitted, and due1094to a small delay between fields a cathode ray TV displays the lines1095interleaved, yielding the original frame. This curious technique was1096invented because at refresh rates similar to film the image would1097fade out too quickly. Transmitting fields reduces the flicker without1098the necessity of doubling the frame rate and with it the bandwidth1099required for each channel.</para>11001101<para>It is important to understand a video camera does not expose1102one frame at a time, merely transmitting the frames separated into1103fields. The fields are in fact captured at two different instances in1104time. An object on screen may well move between one field and the1105next. For applications analysing motion it is of paramount importance1106to recognize which field of a frame is older, the <emphasis>temporal1107order</emphasis>.</para>11081109<para>When the driver provides or accepts images field by field1110rather than interleaved, it is also important applications understand1111how the fields combine to frames. We distinguish between top (aka odd) and1112bottom (aka even) fields, the <emphasis>spatial order</emphasis>: The first line1113of the top field is the first line of an interlaced frame, the first1114line of the bottom field is the second line of that frame.</para>11151116<para>However because fields were captured one after the other,1117arguing whether a frame commences with the top or bottom field is1118pointless. Any two successive top and bottom, or bottom and top fields1119yield a valid frame. Only when the source was progressive to begin1120with, ⪚ when transferring film to video, two fields may come from1121the same frame, creating a natural order.</para>11221123<para>Counter to intuition the top field is not necessarily the1124older field. Whether the older field contains the top or bottom lines1125is a convention determined by the video standard. Hence the1126distinction between temporal and spatial order of fields. The diagrams1127below should make this clearer.</para>11281129<para>All video capture and output devices must report the current1130field order. Some drivers may permit the selection of a different1131order, to this end applications initialize the1132<structfield>field</structfield> field of &v4l2-pix-format; before1133calling the &VIDIOC-S-FMT; ioctl. If this is not desired it should1134have the value <constant>V4L2_FIELD_ANY</constant> (0).</para>11351136<table frame="none" pgwide="1" id="v4l2-field">1137<title>enum v4l2_field</title>1138<tgroup cols="3">1139&cs-def;1140<tbody valign="top">1141<row>1142<entry><constant>V4L2_FIELD_ANY</constant></entry>1143<entry>0</entry>1144<entry>Applications request this field order when any1145one of the <constant>V4L2_FIELD_NONE</constant>,1146<constant>V4L2_FIELD_TOP</constant>,1147<constant>V4L2_FIELD_BOTTOM</constant>, or1148<constant>V4L2_FIELD_INTERLACED</constant> formats is acceptable.1149Drivers choose depending on hardware capabilities or e. g. the1150requested image size, and return the actual field order. &v4l2-buffer;1151<structfield>field</structfield> can never be1152<constant>V4L2_FIELD_ANY</constant>.</entry>1153</row>1154<row>1155<entry><constant>V4L2_FIELD_NONE</constant></entry>1156<entry>1</entry>1157<entry>Images are in progressive format, not interlaced.1158The driver may also indicate this order when it cannot distinguish1159between <constant>V4L2_FIELD_TOP</constant> and1160<constant>V4L2_FIELD_BOTTOM</constant>.</entry>1161</row>1162<row>1163<entry><constant>V4L2_FIELD_TOP</constant></entry>1164<entry>2</entry>1165<entry>Images consist of the top (aka odd) field only.</entry>1166</row>1167<row>1168<entry><constant>V4L2_FIELD_BOTTOM</constant></entry>1169<entry>3</entry>1170<entry>Images consist of the bottom (aka even) field only.1171Applications may wish to prevent a device from capturing interlaced1172images because they will have "comb" or "feathering" artefacts around1173moving objects.</entry>1174</row>1175<row>1176<entry><constant>V4L2_FIELD_INTERLACED</constant></entry>1177<entry>4</entry>1178<entry>Images contain both fields, interleaved line by1179line. The temporal order of the fields (whether the top or bottom1180field is first transmitted) depends on the current video standard.1181M/NTSC transmits the bottom field first, all other standards the top1182field first.</entry>1183</row>1184<row>1185<entry><constant>V4L2_FIELD_SEQ_TB</constant></entry>1186<entry>5</entry>1187<entry>Images contain both fields, the top field lines1188are stored first in memory, immediately followed by the bottom field1189lines. Fields are always stored in temporal order, the older one first1190in memory. Image sizes refer to the frame, not fields.</entry>1191</row>1192<row>1193<entry><constant>V4L2_FIELD_SEQ_BT</constant></entry>1194<entry>6</entry>1195<entry>Images contain both fields, the bottom field1196lines are stored first in memory, immediately followed by the top1197field lines. Fields are always stored in temporal order, the older one1198first in memory. Image sizes refer to the frame, not fields.</entry>1199</row>1200<row>1201<entry><constant>V4L2_FIELD_ALTERNATE</constant></entry>1202<entry>7</entry>1203<entry>The two fields of a frame are passed in separate1204buffers, in temporal order, &ie; the older one first. To indicate the field1205parity (whether the current field is a top or bottom field) the driver1206or application, depending on data direction, must set &v4l2-buffer;1207<structfield>field</structfield> to1208<constant>V4L2_FIELD_TOP</constant> or1209<constant>V4L2_FIELD_BOTTOM</constant>. Any two successive fields pair1210to build a frame. If fields are successive, without any dropped fields1211between them (fields can drop individually), can be determined from1212the &v4l2-buffer; <structfield>sequence</structfield> field. Image1213sizes refer to the frame, not fields. This format cannot be selected1214when using the read/write I/O method.<!-- Where it's indistinguishable1215from V4L2_FIELD_SEQ_*. --></entry>1216</row>1217<row>1218<entry><constant>V4L2_FIELD_INTERLACED_TB</constant></entry>1219<entry>8</entry>1220<entry>Images contain both fields, interleaved line by1221line, top field first. The top field is transmitted first.</entry>1222</row>1223<row>1224<entry><constant>V4L2_FIELD_INTERLACED_BT</constant></entry>1225<entry>9</entry>1226<entry>Images contain both fields, interleaved line by1227line, top field first. The bottom field is transmitted first.</entry>1228</row>1229</tbody>1230</tgroup>1231</table>12321233<figure id="fieldseq-tb">1234<title>Field Order, Top Field First Transmitted</title>1235<mediaobject>1236<imageobject>1237<imagedata fileref="fieldseq_tb.pdf" format="PS" />1238</imageobject>1239<imageobject>1240<imagedata fileref="fieldseq_tb.gif" format="GIF" />1241</imageobject>1242</mediaobject>1243</figure>12441245<figure id="fieldseq-bt">1246<title>Field Order, Bottom Field First Transmitted</title>1247<mediaobject>1248<imageobject>1249<imagedata fileref="fieldseq_bt.pdf" format="PS" />1250</imageobject>1251<imageobject>1252<imagedata fileref="fieldseq_bt.gif" format="GIF" />1253</imageobject>1254</mediaobject>1255</figure>1256</section>12571258<!--1259Local Variables:1260mode: sgml1261sgml-parent-document: "v4l2.sgml"1262indent-tabs-mode: nil1263End:1264-->126512661267