vidioc-qbuf.rst 7.8 KB
Newer Older
1 2
.. -*- coding: utf-8; mode: rst -*-

3
.. _VIDIOC_QBUF:
4 5 6 7 8

*******************************
ioctl VIDIOC_QBUF, VIDIOC_DQBUF
*******************************

9
Name
10
====
11

12
VIDIOC_QBUF - VIDIOC_DQBUF - Exchange a buffer with the driver
13

14 15

Synopsis
16 17
========

18 19 20 21 22
.. c:function:: int ioctl( int fd, VIDIOC_QBUF, struct v4l2_buffer *argp )
    :name: VIDIOC_QBUF

.. c:function:: int ioctl( int fd, VIDIOC_DQBUF, struct v4l2_buffer *argp )
    :name: VIDIOC_DQBUF
23

24

25
Arguments
26 27 28 29 30 31
=========

``fd``
    File descriptor returned by :ref:`open() <func-open>`.

``argp``
32
    Pointer to struct :c:type:`v4l2_buffer`.
33 34


35
Description
36 37 38 39 40 41 42
===========

Applications call the ``VIDIOC_QBUF`` ioctl to enqueue an empty
(capturing) or filled (output) buffer in the driver's incoming queue.
The semantics depend on the selected I/O method.

To enqueue a buffer applications set the ``type`` field of a struct
43 44 45
:c:type:`v4l2_buffer` to the same buffer type as was
previously used with struct :c:type:`v4l2_format` ``type``
and struct :c:type:`v4l2_requestbuffers` ``type``.
46 47
Applications must also set the ``index`` field. Valid index numbers
range from zero to the number of buffers allocated with
48
:ref:`VIDIOC_REQBUFS` (struct
49
:c:type:`v4l2_requestbuffers` ``count``) minus
50
one. The contents of the struct :c:type:`v4l2_buffer` returned
51
by a :ref:`VIDIOC_QUERYBUF` ioctl will do as well.
52 53 54 55 56 57 58 59
When the buffer is intended for output (``type`` is
``V4L2_BUF_TYPE_VIDEO_OUTPUT``, ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``,
or ``V4L2_BUF_TYPE_VBI_OUTPUT``) applications must also initialize the
``bytesused``, ``field`` and ``timestamp`` fields, see :ref:`buffer`
for details. Applications must also set ``flags`` to 0. The
``reserved2`` and ``reserved`` fields must be set to 0. When using the
:ref:`multi-planar API <planar-apis>`, the ``m.planes`` field must
contain a userspace pointer to a filled-in array of struct
60
:c:type:`v4l2_plane` and the ``length`` field must be set
61 62 63 64 65 66 67
to the number of elements in that array.

To enqueue a :ref:`memory mapped <mmap>` buffer applications set the
``memory`` field to ``V4L2_MEMORY_MMAP``. When ``VIDIOC_QBUF`` is called
with a pointer to this structure the driver sets the
``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_QUEUED`` flags and clears
the ``V4L2_BUF_FLAG_DONE`` flag in the ``flags`` field, or it returns an
68
``EINVAL`` error code.
69 70 71 72 73

To enqueue a :ref:`user pointer <userp>` buffer applications set the
``memory`` field to ``V4L2_MEMORY_USERPTR``, the ``m.userptr`` field to
the address of the buffer and ``length`` to its size. When the
multi-planar API is used, ``m.userptr`` and ``length`` members of the
74
passed array of struct :c:type:`v4l2_plane` have to be used
75 76 77 78 79 80
instead. When ``VIDIOC_QBUF`` is called with a pointer to this structure
the driver sets the ``V4L2_BUF_FLAG_QUEUED`` flag and clears the
``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_DONE`` flags in the
``flags`` field, or it returns an error code. This ioctl locks the
memory pages of the buffer in physical memory, they cannot be swapped
out to disk. Buffers remain locked until dequeued, until the
81
:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` or
82
:ref:`VIDIOC_REQBUFS` ioctl is called, or until the
83 84 85 86 87 88
device is closed.

To enqueue a :ref:`DMABUF <dmabuf>` buffer applications set the
``memory`` field to ``V4L2_MEMORY_DMABUF`` and the ``m.fd`` field to a
file descriptor associated with a DMABUF buffer. When the multi-planar
API is used the ``m.fd`` fields of the passed array of struct
89
:c:type:`v4l2_plane` have to be used instead. When
90 91 92 93 94 95 96
``VIDIOC_QBUF`` is called with a pointer to this structure the driver
sets the ``V4L2_BUF_FLAG_QUEUED`` flag and clears the
``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_DONE`` flags in the
``flags`` field, or it returns an error code. This ioctl locks the
buffer. Locking a buffer means passing it to a driver for a hardware
access (usually DMA). If an application accesses (reads/writes) a locked
buffer then the result is undefined. Buffers remain locked until
97
dequeued, until the :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` or
98
:ref:`VIDIOC_REQBUFS` ioctl is called, or until the
99 100
device is closed.

101 102 103 104 105 106
The ``request_fd`` field can be used with the ``VIDIOC_QBUF`` ioctl to specify
the file descriptor of a :ref:`request <media-request-api>`, if requests are
in use. Setting it means that the buffer will not be passed to the driver
until the request itself is queued. Also, the driver will apply any
settings associated with the request for this buffer. This field will
be ignored unless the ``V4L2_BUF_FLAG_REQUEST_FD`` flag is set.
107
If the device does not support requests, then ``EACCES`` will be returned.
108 109
If requests are supported but an invalid request file descriptor is given,
then ``EINVAL`` will be returned.
110 111 112

.. caution::
   It is not allowed to mix queuing requests with queuing buffers directly.
113
   ``EBUSY`` will be returned if the first buffer was queued directly and
114 115 116 117
   then the application tries to queue a request, or vice versa.

   For :ref:`memory-to-memory devices <codec>` you can specify the
   ``request_fd`` only for output buffers, not for capture buffers. Attempting
118
   to specify this for a capture buffer will result in an ``EACCES`` error.
119

120 121 122
Applications call the ``VIDIOC_DQBUF`` ioctl to dequeue a filled
(capturing) or displayed (output) buffer from the driver's outgoing
queue. They just set the ``type``, ``memory`` and ``reserved`` fields of
123
a struct :c:type:`v4l2_buffer` as above, when
124 125 126 127 128 129 130 131 132 133 134
``VIDIOC_DQBUF`` is called with a pointer to this structure the driver
fills the remaining fields or returns an error code. The driver may also
set ``V4L2_BUF_FLAG_ERROR`` in the ``flags`` field. It indicates a
non-critical (recoverable) streaming error. In such case the application
may continue as normal, but should be aware that data in the dequeued
buffer might be corrupted. When using the multi-planar API, the planes
array must be passed in as well.

By default ``VIDIOC_DQBUF`` blocks when no buffer is in the outgoing
queue. When the ``O_NONBLOCK`` flag was given to the
:ref:`open() <func-open>` function, ``VIDIOC_DQBUF`` returns
135
immediately with an ``EAGAIN`` error code when no buffer is available.
136

137
The struct :c:type:`v4l2_buffer` structure is specified in
138 139 140
:ref:`buffer`.


141
Return Value
142 143 144 145 146 147 148 149 150 151 152 153 154
============

On success 0 is returned, on error -1 and the ``errno`` variable is set
appropriately. The generic error codes are described at the
:ref:`Generic Error Codes <gen-errors>` chapter.

EAGAIN
    Non-blocking I/O has been selected using ``O_NONBLOCK`` and no
    buffer was in the outgoing queue.

EINVAL
    The buffer ``type`` is not supported, or the ``index`` is out of
    bounds, or no buffers have been allocated yet, or the ``userptr`` or
155 156 157
    ``length`` are invalid, or the ``V4L2_BUF_FLAG_REQUEST_FD`` flag was
    set but the the given ``request_fd`` was invalid, or ``m.fd`` was
    an invalid DMABUF file descriptor.
158 159 160

EIO
    ``VIDIOC_DQBUF`` failed due to an internal error. Can also indicate
161 162
    temporary problems like signal loss.

163 164 165
    .. note::

       The driver might dequeue an (empty) buffer despite returning
166 167 168 169 170 171
       an error, or even stop capturing. Reusing such buffer may be unsafe
       though and its details (e.g. ``index``) may not be returned either.
       It is recommended that drivers indicate recoverable errors by setting
       the ``V4L2_BUF_FLAG_ERROR`` and returning 0 instead. In that case the
       application should be able to safely reuse the buffer and continue
       streaming.
172 173 174 175 176

EPIPE
    ``VIDIOC_DQBUF`` returns this on an empty capture queue for mem2mem
    codecs if a buffer with the ``V4L2_BUF_FLAG_LAST`` was already
    dequeued and no new buffers are expected to become available.
177

178
EACCES
179
    The ``V4L2_BUF_FLAG_REQUEST_FD`` flag was set but the device does not
180 181 182 183 184 185
    support requests for the given buffer type.

EBUSY
    The first buffer was queued via a request, but the application now tries
    to queue it directly, or vice versa (it is not permitted to mix the two
    APIs).