1.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2
3.. _buffer:
4
5*******
6Buffers
7*******
8
9A buffer contains data exchanged by application and driver using one of
10the Streaming I/O methods. In the multi-planar API, the data is held in
11planes, while the buffer structure acts as a container for the planes.
12Only pointers to buffers (planes) are exchanged, the data itself is not
13copied. These pointers, together with meta-information like timestamps
14or field parity, are stored in a struct :c:type:`v4l2_buffer`,
15argument to the :ref:`VIDIOC_QUERYBUF`,
16:ref:`VIDIOC_QBUF <VIDIOC_QBUF>` and
17:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. In the multi-planar API,
18some plane-specific members of struct :c:type:`v4l2_buffer`,
19such as pointers and sizes for each plane, are stored in
20struct :c:type:`v4l2_plane` instead. In that case,
21struct :c:type:`v4l2_buffer` contains an array of plane structures.
22
23Dequeued video buffers come with timestamps. The driver decides at which
24part of the frame and with which clock the timestamp is taken. Please
25see flags in the masks ``V4L2_BUF_FLAG_TIMESTAMP_MASK`` and
26``V4L2_BUF_FLAG_TSTAMP_SRC_MASK`` in :ref:`buffer-flags`. These flags
27are always valid and constant across all buffers during the whole video
28stream. Changes in these flags may take place as a side effect of
29:ref:`VIDIOC_S_INPUT <VIDIOC_G_INPUT>` or
30:ref:`VIDIOC_S_OUTPUT <VIDIOC_G_OUTPUT>` however. The
31``V4L2_BUF_FLAG_TIMESTAMP_COPY`` timestamp type which is used by e.g. on
32mem-to-mem devices is an exception to the rule: the timestamp source
33flags are copied from the OUTPUT video buffer to the CAPTURE video
34buffer.
35
36
37Interactions between formats, controls and buffers
38==================================================
39
40V4L2 exposes parameters that influence the buffer size, or the way data is
41laid out in the buffer. Those parameters are exposed through both formats and
42controls. One example of such a control is the ``V4L2_CID_ROTATE`` control
43that modifies the direction in which pixels are stored in the buffer, as well
44as the buffer size when the selected format includes padding at the end of
45lines.
46
47The set of information needed to interpret the content of a buffer (e.g. the
48pixel format, the line stride, the tiling orientation or the rotation) is
49collectively referred to in the rest of this section as the buffer layout.
50
51Controls that can modify the buffer layout shall set the
52``V4L2_CTRL_FLAG_MODIFY_LAYOUT`` flag.
53
54Modifying formats or controls that influence the buffer size or layout require
55the stream to be stopped. Any attempt at such a modification while the stream
56is active shall cause the ioctl setting the format or the control to return
57the ``EBUSY`` error code. In that case drivers shall also set the
58``V4L2_CTRL_FLAG_GRABBED`` flag when calling
59:c:func:`VIDIOC_QUERYCTRL` or :c:func:`VIDIOC_QUERY_EXT_CTRL` for such a
60control while the stream is active.
61
62.. note::
63
64   The :c:func:`VIDIOC_S_SELECTION` ioctl can, depending on the hardware (for
65   instance if the device doesn't include a scaler), modify the format in
66   addition to the selection rectangle. Similarly, the
67   :c:func:`VIDIOC_S_INPUT`, :c:func:`VIDIOC_S_OUTPUT`, :c:func:`VIDIOC_S_STD`
68   and :c:func:`VIDIOC_S_DV_TIMINGS` ioctls can also modify the format and
69   selection rectangles. When those ioctls result in a buffer size or layout
70   change, drivers shall handle that condition as they would handle it in the
71   :c:func:`VIDIOC_S_FMT` ioctl in all cases described in this section.
72
73Controls that only influence the buffer layout can be modified at any time
74when the stream is stopped. As they don't influence the buffer size, no
75special handling is needed to synchronize those controls with buffer
76allocation and the ``V4L2_CTRL_FLAG_GRABBED`` flag is cleared once the
77stream is stopped.
78
79Formats and controls that influence the buffer size interact with buffer
80allocation. The simplest way to handle this is for drivers to always require
81buffers to be reallocated in order to change those formats or controls. In
82that case, to perform such changes, userspace applications shall first stop
83the video stream with the :c:func:`VIDIOC_STREAMOFF` ioctl if it is running
84and free all buffers with the :c:func:`VIDIOC_REQBUFS` ioctl if they are
85allocated. After freeing all buffers the ``V4L2_CTRL_FLAG_GRABBED`` flag
86for controls is cleared. The format or controls can then be modified, and
87buffers shall then be reallocated and the stream restarted. A typical ioctl
88sequence is
89
90 #. VIDIOC_STREAMOFF
91 #. VIDIOC_REQBUFS(0)
92 #. VIDIOC_S_EXT_CTRLS
93 #. VIDIOC_S_FMT
94 #. VIDIOC_REQBUFS(n)
95 #. VIDIOC_QBUF
96 #. VIDIOC_STREAMON
97
98The second :c:func:`VIDIOC_REQBUFS` call will take the new format and control
99value into account to compute the buffer size to allocate. Applications can
100also retrieve the size by calling the :c:func:`VIDIOC_G_FMT` ioctl if needed.
101
102.. note::
103
104   The API doesn't mandate the above order for control (3.) and format (4.)
105   changes. Format and controls can be set in a different order, or even
106   interleaved, depending on the device and use case. For instance some
107   controls might behave differently for different pixel formats, in which
108   case the format might need to be set first.
109
110When reallocation is required, any attempt to modify format or controls that
111influences the buffer size while buffers are allocated shall cause the format
112or control set ioctl to return the ``EBUSY`` error. Any attempt to queue a
113buffer too small for the current format or controls shall cause the
114:c:func:`VIDIOC_QBUF` ioctl to return a ``EINVAL`` error.
115
116Buffer reallocation is an expensive operation. To avoid that cost, drivers can
117(and are encouraged to) allow format or controls that influence the buffer
118size to be changed with buffers allocated. In that case, a typical ioctl
119sequence to modify format and controls is
120
121 #. VIDIOC_STREAMOFF
122 #. VIDIOC_S_EXT_CTRLS
123 #. VIDIOC_S_FMT
124 #. VIDIOC_QBUF
125 #. VIDIOC_STREAMON
126
127For this sequence to operate correctly, queued buffers need to be large enough
128for the new format or controls. Drivers shall return a ``ENOSPC`` error in
129response to format change (:c:func:`VIDIOC_S_FMT`) or control changes
130(:c:func:`VIDIOC_S_CTRL` or :c:func:`VIDIOC_S_EXT_CTRLS`) if buffers too small
131for the new format are currently queued. As a simplification, drivers are
132allowed to return a ``EBUSY`` error from these ioctls if any buffer is
133currently queued, without checking the queued buffers sizes.
134
135Additionally, drivers shall return a ``EINVAL`` error from the
136:c:func:`VIDIOC_QBUF` ioctl if the buffer being queued is too small for the
137current format or controls. Together, these requirements ensure that queued
138buffers will always be large enough for the configured format and controls.
139
140Userspace applications can query the buffer size required for a given format
141and controls by first setting the desired control values and then trying the
142desired format. The :c:func:`VIDIOC_TRY_FMT` ioctl will return the required
143buffer size.
144
145 #. VIDIOC_S_EXT_CTRLS(x)
146 #. VIDIOC_TRY_FMT()
147 #. VIDIOC_S_EXT_CTRLS(y)
148 #. VIDIOC_TRY_FMT()
149
150The :c:func:`VIDIOC_CREATE_BUFS` ioctl can then be used to allocate buffers
151based on the queried sizes (for instance by allocating a set of buffers large
152enough for all the desired formats and controls, or by allocating separate set
153of appropriately sized buffers for each use case).
154
155
156.. c:type:: v4l2_buffer
157
158struct v4l2_buffer
159==================
160
161.. tabularcolumns:: |p{2.8cm}|p{2.5cm}|p{1.6cm}|p{10.2cm}|
162
163.. cssclass:: longtable
164
165.. flat-table:: struct v4l2_buffer
166    :header-rows:  0
167    :stub-columns: 0
168    :widths:       1 2 10
169
170    * - __u32
171      - ``index``
172      - Number of the buffer, set by the application except when calling
173	:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`, then it is set by the
174	driver. This field can range from zero to the number of buffers
175	allocated with the :ref:`VIDIOC_REQBUFS` ioctl
176	(struct :c:type:`v4l2_requestbuffers`
177	``count``), plus any buffers allocated with
178	:ref:`VIDIOC_CREATE_BUFS` minus one.
179    * - __u32
180      - ``type``
181      - Type of the buffer, same as struct
182	:c:type:`v4l2_format` ``type`` or struct
183	:c:type:`v4l2_requestbuffers` ``type``, set
184	by the application. See :c:type:`v4l2_buf_type`
185    * - __u32
186      - ``bytesused``
187      - The number of bytes occupied by the data in the buffer. It depends
188	on the negotiated data format and may change with each buffer for
189	compressed variable size data like JPEG images. Drivers must set
190	this field when ``type`` refers to a capture stream, applications
191	when it refers to an output stream. If the application sets this
192	to 0 for an output stream, then ``bytesused`` will be set to the
193	size of the buffer (see the ``length`` field of this struct) by
194	the driver. For multiplanar formats this field is ignored and the
195	``planes`` pointer is used instead.
196    * - __u32
197      - ``flags``
198      - Flags set by the application or driver, see :ref:`buffer-flags`.
199    * - __u32
200      - ``field``
201      - Indicates the field order of the image in the buffer, see
202	:c:type:`v4l2_field`. This field is not used when the buffer
203	contains VBI data. Drivers must set it when ``type`` refers to a
204	capture stream, applications when it refers to an output stream.
205    * - struct timeval
206      - ``timestamp``
207      - For capture streams this is time when the first data byte was
208	captured, as returned by the :c:func:`clock_gettime()` function
209	for the relevant clock id; see ``V4L2_BUF_FLAG_TIMESTAMP_*`` in
210	:ref:`buffer-flags`. For output streams the driver stores the
211	time at which the last data byte was actually sent out in the
212	``timestamp`` field. This permits applications to monitor the
213	drift between the video and system clock. For output streams that
214	use ``V4L2_BUF_FLAG_TIMESTAMP_COPY`` the application has to fill
215	in the timestamp which will be copied by the driver to the capture
216	stream.
217    * - struct :c:type:`v4l2_timecode`
218      - ``timecode``
219      - When the ``V4L2_BUF_FLAG_TIMECODE`` flag is set in ``flags``, this
220	structure contains a frame timecode. In
221	:c:type:`V4L2_FIELD_ALTERNATE <v4l2_field>` mode the top and
222	bottom field contain the same timecode. Timecodes are intended to
223	help video editing and are typically recorded on video tapes, but
224	also embedded in compressed formats like MPEG. This field is
225	independent of the ``timestamp`` and ``sequence`` fields.
226    * - __u32
227      - ``sequence``
228      - Set by the driver, counting the frames (not fields!) in sequence.
229	This field is set for both input and output devices.
230    * - :cspan:`2`
231
232	In :c:type:`V4L2_FIELD_ALTERNATE <v4l2_field>` mode the top and
233	bottom field have the same sequence number. The count starts at
234	zero and includes dropped or repeated frames. A dropped frame was
235	received by an input device but could not be stored due to lack of
236	free buffer space. A repeated frame was displayed again by an
237	output device because the application did not pass new data in
238	time.
239
240	.. note::
241
242	   This may count the frames received e.g. over USB, without
243	   taking into account the frames dropped by the remote hardware due
244	   to limited compression throughput or bus bandwidth. These devices
245	   identify by not enumerating any video standards, see
246	   :ref:`standard`.
247
248    * - __u32
249      - ``memory``
250      - This field must be set by applications and/or drivers in
251	accordance with the selected I/O method. See :c:type:`v4l2_memory`
252    * - union {
253      - ``m``
254    * - __u32
255      - ``offset``
256      - For the single-planar API and when ``memory`` is
257	``V4L2_MEMORY_MMAP`` this is the offset of the buffer from the
258	start of the device memory. The value is returned by the driver
259	and apart of serving as parameter to the
260	:ref:`mmap() <func-mmap>` function not useful for applications.
261	See :ref:`mmap` for details
262    * - unsigned long
263      - ``userptr``
264      - For the single-planar API and when ``memory`` is
265	``V4L2_MEMORY_USERPTR`` this is a pointer to the buffer (casted to
266	unsigned long type) in virtual memory, set by the application. See
267	:ref:`userp` for details.
268    * - struct v4l2_plane
269      - ``*planes``
270      - When using the multi-planar API, contains a userspace pointer to
271	an array of struct :c:type:`v4l2_plane`. The size of
272	the array should be put in the ``length`` field of this
273	struct :c:type:`v4l2_buffer` structure.
274    * - int
275      - ``fd``
276      - For the single-plane API and when ``memory`` is
277	``V4L2_MEMORY_DMABUF`` this is the file descriptor associated with
278	a DMABUF buffer.
279    * - }
280      -
281    * - __u32
282      - ``length``
283      - Size of the buffer (not the payload) in bytes for the
284	single-planar API. This is set by the driver based on the calls to
285	:ref:`VIDIOC_REQBUFS` and/or
286	:ref:`VIDIOC_CREATE_BUFS`. For the
287	multi-planar API the application sets this to the number of
288	elements in the ``planes`` array. The driver will fill in the
289	actual number of valid elements in that array.
290    * - __u32
291      - ``reserved2``
292      - A place holder for future extensions. Drivers and applications
293	must set this to 0.
294    * - __u32
295      - ``request_fd``
296      - The file descriptor of the request to queue the buffer to. If the flag
297        ``V4L2_BUF_FLAG_REQUEST_FD`` is set, then the buffer will be
298	queued to this request. If the flag is not set, then this field will
299	be ignored.
300
301	The ``V4L2_BUF_FLAG_REQUEST_FD`` flag and this field are only used by
302	:ref:`ioctl VIDIOC_QBUF <VIDIOC_QBUF>` and ignored by other ioctls that
303	take a :c:type:`v4l2_buffer` as argument.
304
305	Applications should not set ``V4L2_BUF_FLAG_REQUEST_FD`` for any ioctls
306	other than :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`.
307
308	If the device does not support requests, then ``EBADR`` will be returned.
309	If requests are supported but an invalid request file descriptor is
310	given, then ``EINVAL`` will be returned.
311
312
313
314.. c:type:: v4l2_plane
315
316struct v4l2_plane
317=================
318
319.. tabularcolumns:: |p{3.5cm}|p{3.5cm}|p{3.5cm}|p{7.0cm}|
320
321.. cssclass:: longtable
322
323.. flat-table::
324    :header-rows:  0
325    :stub-columns: 0
326    :widths:       1 1 2
327
328    * - __u32
329      - ``bytesused``
330      - The number of bytes occupied by data in the plane (its payload).
331	Drivers must set this field when ``type`` refers to a capture
332	stream, applications when it refers to an output stream. If the
333	application sets this to 0 for an output stream, then
334	``bytesused`` will be set to the size of the plane (see the
335	``length`` field of this struct) by the driver.
336
337	.. note::
338
339	   Note that the actual image data starts at ``data_offset``
340	   which may not be 0.
341    * - __u32
342      - ``length``
343      - Size in bytes of the plane (not its payload). This is set by the
344	driver based on the calls to
345	:ref:`VIDIOC_REQBUFS` and/or
346	:ref:`VIDIOC_CREATE_BUFS`.
347    * - union {
348      - ``m``
349    * - __u32
350      - ``mem_offset``
351      - When the memory type in the containing struct
352	:c:type:`v4l2_buffer` is ``V4L2_MEMORY_MMAP``, this
353	is the value that should be passed to :ref:`mmap() <func-mmap>`,
354	similar to the ``offset`` field in struct
355	:c:type:`v4l2_buffer`.
356    * - unsigned long
357      - ``userptr``
358      - When the memory type in the containing struct
359	:c:type:`v4l2_buffer` is ``V4L2_MEMORY_USERPTR``,
360	this is a userspace pointer to the memory allocated for this plane
361	by an application.
362    * - int
363      - ``fd``
364      - When the memory type in the containing struct
365	:c:type:`v4l2_buffer` is ``V4L2_MEMORY_DMABUF``,
366	this is a file descriptor associated with a DMABUF buffer, similar
367	to the ``fd`` field in struct :c:type:`v4l2_buffer`.
368    * - }
369      -
370    * - __u32
371      - ``data_offset``
372      - Offset in bytes to video data in the plane. Drivers must set this
373	field when ``type`` refers to a capture stream, applications when
374	it refers to an output stream.
375
376	.. note::
377
378	   That data_offset is included  in ``bytesused``. So the
379	   size of the image in the plane is ``bytesused``-``data_offset``
380	   at offset ``data_offset`` from the start of the plane.
381    * - __u32
382      - ``reserved[11]``
383      - Reserved for future use. Should be zeroed by drivers and
384	applications.
385
386
387
388.. c:type:: v4l2_buf_type
389
390enum v4l2_buf_type
391==================
392
393.. cssclass:: longtable
394
395.. tabularcolumns:: |p{7.8cm}|p{0.6cm}|p{9.1cm}|
396
397.. flat-table::
398    :header-rows:  0
399    :stub-columns: 0
400    :widths:       4 1 9
401
402    * - ``V4L2_BUF_TYPE_VIDEO_CAPTURE``
403      - 1
404      - Buffer of a single-planar video capture stream, see
405	:ref:`capture`.
406    * - ``V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE``
407      - 9
408      - Buffer of a multi-planar video capture stream, see
409	:ref:`capture`.
410    * - ``V4L2_BUF_TYPE_VIDEO_OUTPUT``
411      - 2
412      - Buffer of a single-planar video output stream, see
413	:ref:`output`.
414    * - ``V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE``
415      - 10
416      - Buffer of a multi-planar video output stream, see :ref:`output`.
417    * - ``V4L2_BUF_TYPE_VIDEO_OVERLAY``
418      - 3
419      - Buffer for video overlay, see :ref:`overlay`.
420    * - ``V4L2_BUF_TYPE_VBI_CAPTURE``
421      - 4
422      - Buffer of a raw VBI capture stream, see :ref:`raw-vbi`.
423    * - ``V4L2_BUF_TYPE_VBI_OUTPUT``
424      - 5
425      - Buffer of a raw VBI output stream, see :ref:`raw-vbi`.
426    * - ``V4L2_BUF_TYPE_SLICED_VBI_CAPTURE``
427      - 6
428      - Buffer of a sliced VBI capture stream, see :ref:`sliced`.
429    * - ``V4L2_BUF_TYPE_SLICED_VBI_OUTPUT``
430      - 7
431      - Buffer of a sliced VBI output stream, see :ref:`sliced`.
432    * - ``V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY``
433      - 8
434      - Buffer for video output overlay (OSD), see :ref:`osd`.
435    * - ``V4L2_BUF_TYPE_SDR_CAPTURE``
436      - 11
437      - Buffer for Software Defined Radio (SDR) capture stream, see
438	:ref:`sdr`.
439    * - ``V4L2_BUF_TYPE_SDR_OUTPUT``
440      - 12
441      - Buffer for Software Defined Radio (SDR) output stream, see
442	:ref:`sdr`.
443    * - ``V4L2_BUF_TYPE_META_CAPTURE``
444      - 13
445      - Buffer for metadata capture, see :ref:`metadata`.
446    * - ``V4L2_BUF_TYPE_META_OUTPUT``
447      - 14
448      - Buffer for metadata output, see :ref:`metadata`.
449
450
451
452.. _buffer-flags:
453
454Buffer Flags
455============
456
457.. raw:: latex
458
459    \small
460
461.. tabularcolumns:: |p{7.0cm}|p{2.1cm}|p{8.4cm}|
462
463.. cssclass:: longtable
464
465.. flat-table::
466    :header-rows:  0
467    :stub-columns: 0
468    :widths:       3 1 4
469
470    * .. _`V4L2-BUF-FLAG-MAPPED`:
471
472      - ``V4L2_BUF_FLAG_MAPPED``
473      - 0x00000001
474      - The buffer resides in device memory and has been mapped into the
475	application's address space, see :ref:`mmap` for details.
476	Drivers set or clear this flag when the
477	:ref:`VIDIOC_QUERYBUF`,
478	:ref:`VIDIOC_QBUF` or
479	:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl is called. Set by the
480	driver.
481    * .. _`V4L2-BUF-FLAG-QUEUED`:
482
483      - ``V4L2_BUF_FLAG_QUEUED``
484      - 0x00000002
485      - Internally drivers maintain two buffer queues, an incoming and
486	outgoing queue. When this flag is set, the buffer is currently on
487	the incoming queue. It automatically moves to the outgoing queue
488	after the buffer has been filled (capture devices) or displayed
489	(output devices). Drivers set or clear this flag when the
490	``VIDIOC_QUERYBUF`` ioctl is called. After (successful) calling
491	the ``VIDIOC_QBUF``\ ioctl it is always set and after
492	``VIDIOC_DQBUF`` always cleared.
493    * .. _`V4L2-BUF-FLAG-DONE`:
494
495      - ``V4L2_BUF_FLAG_DONE``
496      - 0x00000004
497      - When this flag is set, the buffer is currently on the outgoing
498	queue, ready to be dequeued from the driver. Drivers set or clear
499	this flag when the ``VIDIOC_QUERYBUF`` ioctl is called. After
500	calling the ``VIDIOC_QBUF`` or ``VIDIOC_DQBUF`` it is always
501	cleared. Of course a buffer cannot be on both queues at the same
502	time, the ``V4L2_BUF_FLAG_QUEUED`` and ``V4L2_BUF_FLAG_DONE`` flag
503	are mutually exclusive. They can be both cleared however, then the
504	buffer is in "dequeued" state, in the application domain so to
505	say.
506    * .. _`V4L2-BUF-FLAG-ERROR`:
507
508      - ``V4L2_BUF_FLAG_ERROR``
509      - 0x00000040
510      - When this flag is set, the buffer has been dequeued successfully,
511	although the data might have been corrupted. This is recoverable,
512	streaming may continue as normal and the buffer may be reused
513	normally. Drivers set this flag when the ``VIDIOC_DQBUF`` ioctl is
514	called.
515    * .. _`V4L2-BUF-FLAG-IN-REQUEST`:
516
517      - ``V4L2_BUF_FLAG_IN_REQUEST``
518      - 0x00000080
519      - This buffer is part of a request that hasn't been queued yet.
520    * .. _`V4L2-BUF-FLAG-KEYFRAME`:
521
522      - ``V4L2_BUF_FLAG_KEYFRAME``
523      - 0x00000008
524      - Drivers set or clear this flag when calling the ``VIDIOC_DQBUF``
525	ioctl. It may be set by video capture devices when the buffer
526	contains a compressed image which is a key frame (or field), i. e.
527	can be decompressed on its own. Also known as an I-frame.
528	Applications can set this bit when ``type`` refers to an output
529	stream.
530    * .. _`V4L2-BUF-FLAG-PFRAME`:
531
532      - ``V4L2_BUF_FLAG_PFRAME``
533      - 0x00000010
534      - Similar to ``V4L2_BUF_FLAG_KEYFRAME`` this flags predicted frames
535	or fields which contain only differences to a previous key frame.
536	Applications can set this bit when ``type`` refers to an output
537	stream.
538    * .. _`V4L2-BUF-FLAG-BFRAME`:
539
540      - ``V4L2_BUF_FLAG_BFRAME``
541      - 0x00000020
542      - Similar to ``V4L2_BUF_FLAG_KEYFRAME`` this flags a bi-directional
543	predicted frame or field which contains only the differences
544	between the current frame and both the preceding and following key
545	frames to specify its content. Applications can set this bit when
546	``type`` refers to an output stream.
547    * .. _`V4L2-BUF-FLAG-TIMECODE`:
548
549      - ``V4L2_BUF_FLAG_TIMECODE``
550      - 0x00000100
551      - The ``timecode`` field is valid. Drivers set or clear this flag
552	when the ``VIDIOC_DQBUF`` ioctl is called. Applications can set
553	this bit and the corresponding ``timecode`` structure when
554	``type`` refers to an output stream.
555    * .. _`V4L2-BUF-FLAG-PREPARED`:
556
557      - ``V4L2_BUF_FLAG_PREPARED``
558      - 0x00000400
559      - The buffer has been prepared for I/O and can be queued by the
560	application. Drivers set or clear this flag when the
561	:ref:`VIDIOC_QUERYBUF`,
562	:ref:`VIDIOC_PREPARE_BUF <VIDIOC_QBUF>`,
563	:ref:`VIDIOC_QBUF` or
564	:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl is called.
565    * .. _`V4L2-BUF-FLAG-NO-CACHE-INVALIDATE`:
566
567      - ``V4L2_BUF_FLAG_NO_CACHE_INVALIDATE``
568      - 0x00000800
569      - Caches do not have to be invalidated for this buffer. Typically
570	applications shall use this flag if the data captured in the
571	buffer is not going to be touched by the CPU, instead the buffer
572	will, probably, be passed on to a DMA-capable hardware unit for
573	further processing or output. This flag is ignored unless the
574	queue is used for :ref:`memory mapping <mmap>` streaming I/O and
575	reports :ref:`V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS
576	<V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS>` capability.
577    * .. _`V4L2-BUF-FLAG-NO-CACHE-CLEAN`:
578
579      - ``V4L2_BUF_FLAG_NO_CACHE_CLEAN``
580      - 0x00001000
581      - Caches do not have to be cleaned for this buffer. Typically
582	applications shall use this flag for output buffers if the data in
583	this buffer has not been created by the CPU but by some
584	DMA-capable unit, in which case caches have not been used. This flag
585	is ignored unless the queue is used for :ref:`memory mapping <mmap>`
586	streaming I/O and reports :ref:`V4L2_BUF_CAP_SUPPORTS_MMAP_CACHE_HINTS
587	<V4L2-BUF-CAP-SUPPORTS-MMAP-CACHE-HINTS>` capability.
588    * .. _`V4L2-BUF-FLAG-M2M-HOLD-CAPTURE-BUF`:
589
590      - ``V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF``
591      - 0x00000200
592      - Only valid if ``V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF`` is
593	set. It is typically used with stateless decoders where multiple
594	output buffers each decode to a slice of the decoded frame.
595	Applications can set this flag when queueing the output buffer
596	to prevent the driver from dequeueing the capture buffer after
597	the output buffer has been decoded (i.e. the capture buffer is
598	'held'). If the timestamp of this output buffer differs from that
599	of the previous output buffer, then that indicates the start of a
600	new frame and the previously held capture buffer is dequeued.
601    * .. _`V4L2-BUF-FLAG-LAST`:
602
603      - ``V4L2_BUF_FLAG_LAST``
604      - 0x00100000
605      - Last buffer produced by the hardware. mem2mem codec drivers set
606	this flag on the capture queue for the last buffer when the
607	:ref:`VIDIOC_QUERYBUF` or
608	:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl is called. Due to
609	hardware limitations, the last buffer may be empty. In this case
610	the driver will set the ``bytesused`` field to 0, regardless of
611	the format. Any Any subsequent call to the
612	:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl will not block anymore,
613	but return an ``EPIPE`` error code.
614    * .. _`V4L2-BUF-FLAG-REQUEST-FD`:
615
616      - ``V4L2_BUF_FLAG_REQUEST_FD``
617      - 0x00800000
618      - The ``request_fd`` field contains a valid file descriptor.
619    * .. _`V4L2-BUF-FLAG-TIMESTAMP-MASK`:
620
621      - ``V4L2_BUF_FLAG_TIMESTAMP_MASK``
622      - 0x0000e000
623      - Mask for timestamp types below. To test the timestamp type, mask
624	out bits not belonging to timestamp type by performing a logical
625	and operation with buffer flags and timestamp mask.
626    * .. _`V4L2-BUF-FLAG-TIMESTAMP-UNKNOWN`:
627
628      - ``V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN``
629      - 0x00000000
630      - Unknown timestamp type. This type is used by drivers before Linux
631	3.9 and may be either monotonic (see below) or realtime (wall
632	clock). Monotonic clock has been favoured in embedded systems
633	whereas most of the drivers use the realtime clock. Either kinds
634	of timestamps are available in user space via
635	:c:func:`clock_gettime` using clock IDs ``CLOCK_MONOTONIC``
636	and ``CLOCK_REALTIME``, respectively.
637    * .. _`V4L2-BUF-FLAG-TIMESTAMP-MONOTONIC`:
638
639      - ``V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC``
640      - 0x00002000
641      - The buffer timestamp has been taken from the ``CLOCK_MONOTONIC``
642	clock. To access the same clock outside V4L2, use
643	:c:func:`clock_gettime`.
644    * .. _`V4L2-BUF-FLAG-TIMESTAMP-COPY`:
645
646      - ``V4L2_BUF_FLAG_TIMESTAMP_COPY``
647      - 0x00004000
648      - The CAPTURE buffer timestamp has been taken from the corresponding
649	OUTPUT buffer. This flag applies only to mem2mem devices.
650    * .. _`V4L2-BUF-FLAG-TSTAMP-SRC-MASK`:
651
652      - ``V4L2_BUF_FLAG_TSTAMP_SRC_MASK``
653      - 0x00070000
654      - Mask for timestamp sources below. The timestamp source defines the
655	point of time the timestamp is taken in relation to the frame.
656	Logical 'and' operation between the ``flags`` field and
657	``V4L2_BUF_FLAG_TSTAMP_SRC_MASK`` produces the value of the
658	timestamp source. Applications must set the timestamp source when
659	``type`` refers to an output stream and
660	``V4L2_BUF_FLAG_TIMESTAMP_COPY`` is set.
661    * .. _`V4L2-BUF-FLAG-TSTAMP-SRC-EOF`:
662
663      - ``V4L2_BUF_FLAG_TSTAMP_SRC_EOF``
664      - 0x00000000
665      - End Of Frame. The buffer timestamp has been taken when the last
666	pixel of the frame has been received or the last pixel of the
667	frame has been transmitted. In practice, software generated
668	timestamps will typically be read from the clock a small amount of
669	time after the last pixel has been received or transmitten,
670	depending on the system and other activity in it.
671    * .. _`V4L2-BUF-FLAG-TSTAMP-SRC-SOE`:
672
673      - ``V4L2_BUF_FLAG_TSTAMP_SRC_SOE``
674      - 0x00010000
675      - Start Of Exposure. The buffer timestamp has been taken when the
676	exposure of the frame has begun. This is only valid for the
677	``V4L2_BUF_TYPE_VIDEO_CAPTURE`` buffer type.
678
679.. raw:: latex
680
681    \normalsize
682
683.. _memory-flags:
684
685Memory Consistency Flags
686========================
687
688.. tabularcolumns:: |p{7.0cm}|p{2.2cm}|p{8.3cm}|
689
690.. cssclass:: longtable
691
692.. flat-table::
693    :header-rows:  0
694    :stub-columns: 0
695    :widths:       3 1 4
696
697.. c:type:: v4l2_memory
698
699enum v4l2_memory
700================
701
702.. tabularcolumns:: |p{5.0cm}|p{0.8cm}|p{11.7cm}|
703
704.. flat-table::
705    :header-rows:  0
706    :stub-columns: 0
707    :widths:       3 1 4
708
709    * - ``V4L2_MEMORY_MMAP``
710      - 1
711      - The buffer is used for :ref:`memory mapping <mmap>` I/O.
712    * - ``V4L2_MEMORY_USERPTR``
713      - 2
714      - The buffer is used for :ref:`user pointer <userp>` I/O.
715    * - ``V4L2_MEMORY_OVERLAY``
716      - 3
717      - [to do]
718    * - ``V4L2_MEMORY_DMABUF``
719      - 4
720      - The buffer is used for :ref:`DMA shared buffer <dmabuf>` I/O.
721
722
723
724Timecodes
725=========
726
727The :c:type:`v4l2_buffer_timecode` structure is designed to hold a
728:ref:`smpte12m` or similar timecode.
729(struct :c:type:`timeval` timestamps are stored in the struct
730:c:type:`v4l2_buffer` ``timestamp`` field.)
731
732
733.. c:type:: v4l2_timecode
734
735struct v4l2_timecode
736--------------------
737
738.. tabularcolumns:: |p{1.4cm}|p{2.8cm}|p{12.3cm}|
739
740.. flat-table::
741    :header-rows:  0
742    :stub-columns: 0
743    :widths:       1 1 2
744
745    * - __u32
746      - ``type``
747      - Frame rate the timecodes are based on, see :ref:`timecode-type`.
748    * - __u32
749      - ``flags``
750      - Timecode flags, see :ref:`timecode-flags`.
751    * - __u8
752      - ``frames``
753      - Frame count, 0 ... 23/24/29/49/59, depending on the type of
754	timecode.
755    * - __u8
756      - ``seconds``
757      - Seconds count, 0 ... 59. This is a binary, not BCD number.
758    * - __u8
759      - ``minutes``
760      - Minutes count, 0 ... 59. This is a binary, not BCD number.
761    * - __u8
762      - ``hours``
763      - Hours count, 0 ... 29. This is a binary, not BCD number.
764    * - __u8
765      - ``userbits``\ [4]
766      - The "user group" bits from the timecode.
767
768
769
770.. _timecode-type:
771
772Timecode Types
773--------------
774
775.. tabularcolumns:: |p{5.6cm}|p{0.8cm}|p{11.1cm}|
776
777.. flat-table::
778    :header-rows:  0
779    :stub-columns: 0
780    :widths:       3 1 4
781
782    * - ``V4L2_TC_TYPE_24FPS``
783      - 1
784      - 24 frames per second, i. e. film.
785    * - ``V4L2_TC_TYPE_25FPS``
786      - 2
787      - 25 frames per second, i. e. PAL or SECAM video.
788    * - ``V4L2_TC_TYPE_30FPS``
789      - 3
790      - 30 frames per second, i. e. NTSC video.
791    * - ``V4L2_TC_TYPE_50FPS``
792      - 4
793      -
794    * - ``V4L2_TC_TYPE_60FPS``
795      - 5
796      -
797
798
799
800.. _timecode-flags:
801
802Timecode Flags
803--------------
804
805.. tabularcolumns:: |p{6.6cm}|p{1.4cm}|p{9.5cm}|
806
807.. flat-table::
808    :header-rows:  0
809    :stub-columns: 0
810    :widths:       3 1 4
811
812    * - ``V4L2_TC_FLAG_DROPFRAME``
813      - 0x0001
814      - Indicates "drop frame" semantics for counting frames in 29.97 fps
815	material. When set, frame numbers 0 and 1 at the start of each
816	minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the
817	count.
818    * - ``V4L2_TC_FLAG_COLORFRAME``
819      - 0x0002
820      - The "color frame" flag.
821    * - ``V4L2_TC_USERBITS_field``
822      - 0x000C
823      - Field mask for the "binary group flags".
824    * - ``V4L2_TC_USERBITS_USERDEFINED``
825      - 0x0000
826      - Unspecified format.
827    * - ``V4L2_TC_USERBITS_8BITCHARS``
828      - 0x0008
829      - 8-bit ISO characters.
830