162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
262306a36Sopenharmony_ci.. c:namespace:: V4L
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci.. _VIDIOC_QBUF:
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci*******************************
762306a36Sopenharmony_ciioctl VIDIOC_QBUF, VIDIOC_DQBUF
862306a36Sopenharmony_ci*******************************
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciName
1162306a36Sopenharmony_ci====
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ciVIDIOC_QBUF - VIDIOC_DQBUF - Exchange a buffer with the driver
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciSynopsis
1662306a36Sopenharmony_ci========
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci.. c:macro:: VIDIOC_QBUF
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci``int ioctl(int fd, VIDIOC_QBUF, struct v4l2_buffer *argp)``
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci.. c:macro:: VIDIOC_DQBUF
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci``int ioctl(int fd, VIDIOC_DQBUF, struct v4l2_buffer *argp)``
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciArguments
2762306a36Sopenharmony_ci=========
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci``fd``
3062306a36Sopenharmony_ci    File descriptor returned by :c:func:`open()`.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci``argp``
3362306a36Sopenharmony_ci    Pointer to struct :c:type:`v4l2_buffer`.
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciDescription
3662306a36Sopenharmony_ci===========
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciApplications call the ``VIDIOC_QBUF`` ioctl to enqueue an empty
3962306a36Sopenharmony_ci(capturing) or filled (output) buffer in the driver's incoming queue.
4062306a36Sopenharmony_ciThe semantics depend on the selected I/O method.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciTo enqueue a buffer applications set the ``type`` field of a struct
4362306a36Sopenharmony_ci:c:type:`v4l2_buffer` to the same buffer type as was
4462306a36Sopenharmony_cipreviously used with struct :c:type:`v4l2_format` ``type``
4562306a36Sopenharmony_ciand struct :c:type:`v4l2_requestbuffers` ``type``.
4662306a36Sopenharmony_ciApplications must also set the ``index`` field. Valid index numbers
4762306a36Sopenharmony_cirange from zero to the number of buffers allocated with
4862306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS` (struct
4962306a36Sopenharmony_ci:c:type:`v4l2_requestbuffers` ``count``) minus
5062306a36Sopenharmony_cione. The contents of the struct :c:type:`v4l2_buffer` returned
5162306a36Sopenharmony_ciby a :ref:`VIDIOC_QUERYBUF` ioctl will do as well.
5262306a36Sopenharmony_ciWhen the buffer is intended for output (``type`` is
5362306a36Sopenharmony_ci``V4L2_BUF_TYPE_VIDEO_OUTPUT``, ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``,
5462306a36Sopenharmony_cior ``V4L2_BUF_TYPE_VBI_OUTPUT``) applications must also initialize the
5562306a36Sopenharmony_ci``bytesused``, ``field`` and ``timestamp`` fields, see :ref:`buffer`
5662306a36Sopenharmony_cifor details. Applications must also set ``flags`` to 0. The
5762306a36Sopenharmony_ci``reserved2`` and ``reserved`` fields must be set to 0. When using the
5862306a36Sopenharmony_ci:ref:`multi-planar API <planar-apis>`, the ``m.planes`` field must
5962306a36Sopenharmony_cicontain a userspace pointer to a filled-in array of struct
6062306a36Sopenharmony_ci:c:type:`v4l2_plane` and the ``length`` field must be set
6162306a36Sopenharmony_cito the number of elements in that array.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciTo enqueue a :ref:`memory mapped <mmap>` buffer applications set the
6462306a36Sopenharmony_ci``memory`` field to ``V4L2_MEMORY_MMAP``. When ``VIDIOC_QBUF`` is called
6562306a36Sopenharmony_ciwith a pointer to this structure the driver sets the
6662306a36Sopenharmony_ci``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_QUEUED`` flags and clears
6762306a36Sopenharmony_cithe ``V4L2_BUF_FLAG_DONE`` flag in the ``flags`` field, or it returns an
6862306a36Sopenharmony_ci``EINVAL`` error code.
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciTo enqueue a :ref:`user pointer <userp>` buffer applications set the
7162306a36Sopenharmony_ci``memory`` field to ``V4L2_MEMORY_USERPTR``, the ``m.userptr`` field to
7262306a36Sopenharmony_cithe address of the buffer and ``length`` to its size. When the
7362306a36Sopenharmony_cimulti-planar API is used, ``m.userptr`` and ``length`` members of the
7462306a36Sopenharmony_cipassed array of struct :c:type:`v4l2_plane` have to be used
7562306a36Sopenharmony_ciinstead. When ``VIDIOC_QBUF`` is called with a pointer to this structure
7662306a36Sopenharmony_cithe driver sets the ``V4L2_BUF_FLAG_QUEUED`` flag and clears the
7762306a36Sopenharmony_ci``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_DONE`` flags in the
7862306a36Sopenharmony_ci``flags`` field, or it returns an error code. This ioctl locks the
7962306a36Sopenharmony_cimemory pages of the buffer in physical memory, they cannot be swapped
8062306a36Sopenharmony_ciout to disk. Buffers remain locked until dequeued, until the
8162306a36Sopenharmony_ci:ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` or
8262306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS` ioctl is called, or until the
8362306a36Sopenharmony_cidevice is closed.
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciTo enqueue a :ref:`DMABUF <dmabuf>` buffer applications set the
8662306a36Sopenharmony_ci``memory`` field to ``V4L2_MEMORY_DMABUF`` and the ``m.fd`` field to a
8762306a36Sopenharmony_cifile descriptor associated with a DMABUF buffer. When the multi-planar
8862306a36Sopenharmony_ciAPI is used the ``m.fd`` fields of the passed array of struct
8962306a36Sopenharmony_ci:c:type:`v4l2_plane` have to be used instead. When
9062306a36Sopenharmony_ci``VIDIOC_QBUF`` is called with a pointer to this structure the driver
9162306a36Sopenharmony_cisets the ``V4L2_BUF_FLAG_QUEUED`` flag and clears the
9262306a36Sopenharmony_ci``V4L2_BUF_FLAG_MAPPED`` and ``V4L2_BUF_FLAG_DONE`` flags in the
9362306a36Sopenharmony_ci``flags`` field, or it returns an error code. This ioctl locks the
9462306a36Sopenharmony_cibuffer. Locking a buffer means passing it to a driver for a hardware
9562306a36Sopenharmony_ciaccess (usually DMA). If an application accesses (reads/writes) a locked
9662306a36Sopenharmony_cibuffer then the result is undefined. Buffers remain locked until
9762306a36Sopenharmony_cidequeued, until the :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` or
9862306a36Sopenharmony_ci:ref:`VIDIOC_REQBUFS` ioctl is called, or until the
9962306a36Sopenharmony_cidevice is closed.
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciThe ``request_fd`` field can be used with the ``VIDIOC_QBUF`` ioctl to specify
10262306a36Sopenharmony_cithe file descriptor of a :ref:`request <media-request-api>`, if requests are
10362306a36Sopenharmony_ciin use. Setting it means that the buffer will not be passed to the driver
10462306a36Sopenharmony_ciuntil the request itself is queued. Also, the driver will apply any
10562306a36Sopenharmony_cisettings associated with the request for this buffer. This field will
10662306a36Sopenharmony_cibe ignored unless the ``V4L2_BUF_FLAG_REQUEST_FD`` flag is set.
10762306a36Sopenharmony_ciIf the device does not support requests, then ``EBADR`` will be returned.
10862306a36Sopenharmony_ciIf requests are supported but an invalid request file descriptor is given,
10962306a36Sopenharmony_cithen ``EINVAL`` will be returned.
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci.. caution::
11262306a36Sopenharmony_ci   It is not allowed to mix queuing requests with queuing buffers directly.
11362306a36Sopenharmony_ci   ``EBUSY`` will be returned if the first buffer was queued directly and
11462306a36Sopenharmony_ci   then the application tries to queue a request, or vice versa. After
11562306a36Sopenharmony_ci   closing the file descriptor, calling
11662306a36Sopenharmony_ci   :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` or calling :ref:`VIDIOC_REQBUFS`
11762306a36Sopenharmony_ci   the check for this will be reset.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci   For :ref:`memory-to-memory devices <mem2mem>` you can specify the
12062306a36Sopenharmony_ci   ``request_fd`` only for output buffers, not for capture buffers. Attempting
12162306a36Sopenharmony_ci   to specify this for a capture buffer will result in an ``EBADR`` error.
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciApplications call the ``VIDIOC_DQBUF`` ioctl to dequeue a filled
12462306a36Sopenharmony_ci(capturing) or displayed (output) buffer from the driver's outgoing
12562306a36Sopenharmony_ciqueue. They just set the ``type``, ``memory`` and ``reserved`` fields of
12662306a36Sopenharmony_cia struct :c:type:`v4l2_buffer` as above, when
12762306a36Sopenharmony_ci``VIDIOC_DQBUF`` is called with a pointer to this structure the driver
12862306a36Sopenharmony_cifills all remaining fields or returns an error code. The driver may also
12962306a36Sopenharmony_ciset ``V4L2_BUF_FLAG_ERROR`` in the ``flags`` field. It indicates a
13062306a36Sopenharmony_cinon-critical (recoverable) streaming error. In such case the application
13162306a36Sopenharmony_cimay continue as normal, but should be aware that data in the dequeued
13262306a36Sopenharmony_cibuffer might be corrupted. When using the multi-planar API, the planes
13362306a36Sopenharmony_ciarray must be passed in as well.
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciIf the application sets the ``memory`` field to ``V4L2_MEMORY_DMABUF`` to
13662306a36Sopenharmony_cidequeue a :ref:`DMABUF <dmabuf>` buffer, the driver fills the ``m.fd`` field
13762306a36Sopenharmony_ciwith a file descriptor numerically the same as the one given to ``VIDIOC_QBUF``
13862306a36Sopenharmony_ciwhen the buffer was enqueued. No new file descriptor is created at dequeue time
13962306a36Sopenharmony_ciand the value is only for the application convenience. When the multi-planar
14062306a36Sopenharmony_ciAPI is used the ``m.fd`` fields of the passed array of struct
14162306a36Sopenharmony_ci:c:type:`v4l2_plane` are filled instead.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciBy default ``VIDIOC_DQBUF`` blocks when no buffer is in the outgoing
14462306a36Sopenharmony_ciqueue. When the ``O_NONBLOCK`` flag was given to the
14562306a36Sopenharmony_ci:c:func:`open()` function, ``VIDIOC_DQBUF`` returns
14662306a36Sopenharmony_ciimmediately with an ``EAGAIN`` error code when no buffer is available.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciThe struct :c:type:`v4l2_buffer` structure is specified in
14962306a36Sopenharmony_ci:ref:`buffer`.
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciReturn Value
15262306a36Sopenharmony_ci============
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciOn success 0 is returned, on error -1 and the ``errno`` variable is set
15562306a36Sopenharmony_ciappropriately. The generic error codes are described at the
15662306a36Sopenharmony_ci:ref:`Generic Error Codes <gen-errors>` chapter.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciEAGAIN
15962306a36Sopenharmony_ci    Non-blocking I/O has been selected using ``O_NONBLOCK`` and no
16062306a36Sopenharmony_ci    buffer was in the outgoing queue.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciEINVAL
16362306a36Sopenharmony_ci    The buffer ``type`` is not supported, or the ``index`` is out of
16462306a36Sopenharmony_ci    bounds, or no buffers have been allocated yet, or the ``userptr`` or
16562306a36Sopenharmony_ci    ``length`` are invalid, or the ``V4L2_BUF_FLAG_REQUEST_FD`` flag was
16662306a36Sopenharmony_ci    set but the given ``request_fd`` was invalid, or ``m.fd`` was
16762306a36Sopenharmony_ci    an invalid DMABUF file descriptor.
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciEIO
17062306a36Sopenharmony_ci    ``VIDIOC_DQBUF`` failed due to an internal error. Can also indicate
17162306a36Sopenharmony_ci    temporary problems like signal loss.
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci    .. note::
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci       The driver might dequeue an (empty) buffer despite returning
17662306a36Sopenharmony_ci       an error, or even stop capturing. Reusing such buffer may be unsafe
17762306a36Sopenharmony_ci       though and its details (e.g. ``index``) may not be returned either.
17862306a36Sopenharmony_ci       It is recommended that drivers indicate recoverable errors by setting
17962306a36Sopenharmony_ci       the ``V4L2_BUF_FLAG_ERROR`` and returning 0 instead. In that case the
18062306a36Sopenharmony_ci       application should be able to safely reuse the buffer and continue
18162306a36Sopenharmony_ci       streaming.
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciEPIPE
18462306a36Sopenharmony_ci    ``VIDIOC_DQBUF`` returns this on an empty capture queue for mem2mem
18562306a36Sopenharmony_ci    codecs if a buffer with the ``V4L2_BUF_FLAG_LAST`` was already
18662306a36Sopenharmony_ci    dequeued and no new buffers are expected to become available.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciEBADR
18962306a36Sopenharmony_ci    The ``V4L2_BUF_FLAG_REQUEST_FD`` flag was set but the device does not
19062306a36Sopenharmony_ci    support requests for the given buffer type, or
19162306a36Sopenharmony_ci    the ``V4L2_BUF_FLAG_REQUEST_FD`` flag was not set but the device requires
19262306a36Sopenharmony_ci    that the buffer is part of a request.
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ciEBUSY
19562306a36Sopenharmony_ci    The first buffer was queued via a request, but the application now tries
19662306a36Sopenharmony_ci    to queue it directly, or vice versa (it is not permitted to mix the two
19762306a36Sopenharmony_ci    APIs).
198