mmap.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: V4L
  3. .. _mmap:
  4. ******************************
  5. Streaming I/O (Memory Mapping)
  6. ******************************
  7. Input and output devices support this I/O method when the
  8. ``V4L2_CAP_STREAMING`` flag in the ``capabilities`` field of struct
  9. :c:type:`v4l2_capability` returned by the
  10. :ref:`VIDIOC_QUERYCAP` ioctl is set. There are two
  11. streaming methods, to determine if the memory mapping flavor is
  12. supported applications must call the :ref:`VIDIOC_REQBUFS` ioctl
  13. with the memory type set to ``V4L2_MEMORY_MMAP``.
  14. Streaming is an I/O method where only pointers to buffers are exchanged
  15. between application and driver, the data itself is not copied. Memory
  16. mapping is primarily intended to map buffers in device memory into the
  17. application's address space. Device memory can be for example the video
  18. memory on a graphics card with a video capture add-on. However, being
  19. the most efficient I/O method available for a long time, many other
  20. drivers support streaming as well, allocating buffers in DMA-able main
  21. memory.
  22. A driver can support many sets of buffers. Each set is identified by a
  23. unique buffer type value. The sets are independent and each set can hold
  24. a different type of data. To access different sets at the same time
  25. different file descriptors must be used. [#f1]_
  26. To allocate device buffers applications call the
  27. :ref:`VIDIOC_REQBUFS` ioctl with the desired number
  28. of buffers and buffer type, for example ``V4L2_BUF_TYPE_VIDEO_CAPTURE``.
  29. This ioctl can also be used to change the number of buffers or to free
  30. the allocated memory, provided none of the buffers are still mapped.
  31. Before applications can access the buffers they must map them into their
  32. address space with the :c:func:`mmap()` function. The
  33. location of the buffers in device memory can be determined with the
  34. :ref:`VIDIOC_QUERYBUF` ioctl. In the single-planar
  35. API case, the ``m.offset`` and ``length`` returned in a struct
  36. :c:type:`v4l2_buffer` are passed as sixth and second
  37. parameter to the :c:func:`mmap()` function. When using the
  38. multi-planar API, struct :c:type:`v4l2_buffer` contains an
  39. array of struct :c:type:`v4l2_plane` structures, each
  40. containing its own ``m.offset`` and ``length``. When using the
  41. multi-planar API, every plane of every buffer has to be mapped
  42. separately, so the number of calls to :c:func:`mmap()` should
  43. be equal to number of buffers times number of planes in each buffer. The
  44. offset and length values must not be modified. Remember, the buffers are
  45. allocated in physical memory, as opposed to virtual memory, which can be
  46. swapped out to disk. Applications should free the buffers as soon as
  47. possible with the :c:func:`munmap()` function.
  48. Example: Mapping buffers in the single-planar API
  49. =================================================
  50. .. code-block:: c
  51. struct v4l2_requestbuffers reqbuf;
  52. struct {
  53. void *start;
  54. size_t length;
  55. } *buffers;
  56. unsigned int i;
  57. memset(&reqbuf, 0, sizeof(reqbuf));
  58. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  59. reqbuf.memory = V4L2_MEMORY_MMAP;
  60. reqbuf.count = 20;
  61. if (-1 == ioctl (fd, VIDIOC_REQBUFS, &reqbuf)) {
  62. if (errno == EINVAL)
  63. printf("Video capturing or mmap-streaming is not supported\\n");
  64. else
  65. perror("VIDIOC_REQBUFS");
  66. exit(EXIT_FAILURE);
  67. }
  68. /* We want at least five buffers. */
  69. if (reqbuf.count < 5) {
  70. /* You may need to free the buffers here. */
  71. printf("Not enough buffer memory\\n");
  72. exit(EXIT_FAILURE);
  73. }
  74. buffers = calloc(reqbuf.count, sizeof(*buffers));
  75. assert(buffers != NULL);
  76. for (i = 0; i < reqbuf.count; i++) {
  77. struct v4l2_buffer buffer;
  78. memset(&buffer, 0, sizeof(buffer));
  79. buffer.type = reqbuf.type;
  80. buffer.memory = V4L2_MEMORY_MMAP;
  81. buffer.index = i;
  82. if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buffer)) {
  83. perror("VIDIOC_QUERYBUF");
  84. exit(EXIT_FAILURE);
  85. }
  86. buffers[i].length = buffer.length; /* remember for munmap() */
  87. buffers[i].start = mmap(NULL, buffer.length,
  88. PROT_READ | PROT_WRITE, /* recommended */
  89. MAP_SHARED, /* recommended */
  90. fd, buffer.m.offset);
  91. if (MAP_FAILED == buffers[i].start) {
  92. /* If you do not exit here you should unmap() and free()
  93. the buffers mapped so far. */
  94. perror("mmap");
  95. exit(EXIT_FAILURE);
  96. }
  97. }
  98. /* Cleanup. */
  99. for (i = 0; i < reqbuf.count; i++)
  100. munmap(buffers[i].start, buffers[i].length);
  101. Example: Mapping buffers in the multi-planar API
  102. ================================================
  103. .. code-block:: c
  104. struct v4l2_requestbuffers reqbuf;
  105. /* Our current format uses 3 planes per buffer */
  106. #define FMT_NUM_PLANES = 3
  107. struct {
  108. void *start[FMT_NUM_PLANES];
  109. size_t length[FMT_NUM_PLANES];
  110. } *buffers;
  111. unsigned int i, j;
  112. memset(&reqbuf, 0, sizeof(reqbuf));
  113. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  114. reqbuf.memory = V4L2_MEMORY_MMAP;
  115. reqbuf.count = 20;
  116. if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
  117. if (errno == EINVAL)
  118. printf("Video capturing or mmap-streaming is not supported\\n");
  119. else
  120. perror("VIDIOC_REQBUFS");
  121. exit(EXIT_FAILURE);
  122. }
  123. /* We want at least five buffers. */
  124. if (reqbuf.count < 5) {
  125. /* You may need to free the buffers here. */
  126. printf("Not enough buffer memory\\n");
  127. exit(EXIT_FAILURE);
  128. }
  129. buffers = calloc(reqbuf.count, sizeof(*buffers));
  130. assert(buffers != NULL);
  131. for (i = 0; i < reqbuf.count; i++) {
  132. struct v4l2_buffer buffer;
  133. struct v4l2_plane planes[FMT_NUM_PLANES];
  134. memset(&buffer, 0, sizeof(buffer));
  135. buffer.type = reqbuf.type;
  136. buffer.memory = V4L2_MEMORY_MMAP;
  137. buffer.index = i;
  138. /* length in struct v4l2_buffer in multi-planar API stores the size
  139. * of planes array. */
  140. buffer.length = FMT_NUM_PLANES;
  141. buffer.m.planes = planes;
  142. if (ioctl(fd, VIDIOC_QUERYBUF, &buffer) < 0) {
  143. perror("VIDIOC_QUERYBUF");
  144. exit(EXIT_FAILURE);
  145. }
  146. /* Every plane has to be mapped separately */
  147. for (j = 0; j < FMT_NUM_PLANES; j++) {
  148. buffers[i].length[j] = buffer.m.planes[j].length; /* remember for munmap() */
  149. buffers[i].start[j] = mmap(NULL, buffer.m.planes[j].length,
  150. PROT_READ | PROT_WRITE, /* recommended */
  151. MAP_SHARED, /* recommended */
  152. fd, buffer.m.planes[j].m.mem_offset);
  153. if (MAP_FAILED == buffers[i].start[j]) {
  154. /* If you do not exit here you should unmap() and free()
  155. the buffers and planes mapped so far. */
  156. perror("mmap");
  157. exit(EXIT_FAILURE);
  158. }
  159. }
  160. }
  161. /* Cleanup. */
  162. for (i = 0; i < reqbuf.count; i++)
  163. for (j = 0; j < FMT_NUM_PLANES; j++)
  164. munmap(buffers[i].start[j], buffers[i].length[j]);
  165. Conceptually streaming drivers maintain two buffer queues, an incoming
  166. and an outgoing queue. They separate the synchronous capture or output
  167. operation locked to a video clock from the application which is subject
  168. to random disk or network delays and preemption by other processes,
  169. thereby reducing the probability of data loss. The queues are organized
  170. as FIFOs, buffers will be output in the order enqueued in the incoming
  171. FIFO, and were captured in the order dequeued from the outgoing FIFO.
  172. The driver may require a minimum number of buffers enqueued at all times
  173. to function, apart of this no limit exists on the number of buffers
  174. applications can enqueue in advance, or dequeue and process. They can
  175. also enqueue in a different order than buffers have been dequeued, and
  176. the driver can *fill* enqueued *empty* buffers in any order. [#f2]_ The
  177. index number of a buffer (struct :c:type:`v4l2_buffer`
  178. ``index``) plays no role here, it only identifies the buffer.
  179. Initially all mapped buffers are in dequeued state, inaccessible by the
  180. driver. For capturing applications it is customary to first enqueue all
  181. mapped buffers, then to start capturing and enter the read loop. Here
  182. the application waits until a filled buffer can be dequeued, and
  183. re-enqueues the buffer when the data is no longer needed. Output
  184. applications fill and enqueue buffers, when enough buffers are stacked
  185. up the output is started with :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>`.
  186. In the write loop, when the application runs out of free buffers, it
  187. must wait until an empty buffer can be dequeued and reused.
  188. To enqueue and dequeue a buffer applications use the
  189. :ref:`VIDIOC_QBUF <VIDIOC_QBUF>` and :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
  190. ioctl. The status of a buffer being mapped, enqueued, full or empty can
  191. be determined at any time using the :ref:`VIDIOC_QUERYBUF` ioctl. Two
  192. methods exist to suspend execution of the application until one or more
  193. buffers can be dequeued. By default :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
  194. blocks when no buffer is in the outgoing queue. When the ``O_NONBLOCK``
  195. flag was given to the :c:func:`open()` function,
  196. :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` returns immediately with an ``EAGAIN``
  197. error code when no buffer is available. The :c:func:`select()`
  198. or :c:func:`poll()` functions are always available.
  199. To start and stop capturing or output applications call the
  200. :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>` and :ref:`VIDIOC_STREAMOFF
  201. <VIDIOC_STREAMON>` ioctl.
  202. .. note:::ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>`
  203. removes all buffers from both queues as a side effect. Since there is
  204. no notion of doing anything "now" on a multitasking system, if an
  205. application needs to synchronize with another event it should examine
  206. the struct ::c:type:`v4l2_buffer` ``timestamp`` of captured
  207. or outputted buffers.
  208. Drivers implementing memory mapping I/O must support the
  209. :ref:`VIDIOC_REQBUFS <VIDIOC_REQBUFS>`, :ref:`VIDIOC_QUERYBUF
  210. <VIDIOC_QUERYBUF>`, :ref:`VIDIOC_QBUF <VIDIOC_QBUF>`, :ref:`VIDIOC_DQBUF
  211. <VIDIOC_QBUF>`, :ref:`VIDIOC_STREAMON <VIDIOC_STREAMON>`
  212. and :ref:`VIDIOC_STREAMOFF <VIDIOC_STREAMON>` ioctls, the :ref:`mmap()
  213. <func-mmap>`, :c:func:`munmap()`, :ref:`select()
  214. <func-select>` and :c:func:`poll()` function. [#f3]_
  215. [capture example]
  216. .. [#f1]
  217. One could use one file descriptor and set the buffer type field
  218. accordingly when calling :ref:`VIDIOC_QBUF` etc.,
  219. but it makes the :c:func:`select()` function ambiguous. We also
  220. like the clean approach of one file descriptor per logical stream.
  221. Video overlay for example is also a logical stream, although the CPU
  222. is not needed for continuous operation.
  223. .. [#f2]
  224. Random enqueue order permits applications processing images out of
  225. order (such as video codecs) to return buffers earlier, reducing the
  226. probability of data loss. Random fill order allows drivers to reuse
  227. buffers on a LIFO-basis, taking advantage of caches holding
  228. scatter-gather lists and the like.
  229. .. [#f3]
  230. At the driver level :c:func:`select()` and :c:func:`poll()` are
  231. the same, and :c:func:`select()` is too important to be optional.
  232. The rest should be evident.