mmap.rst 11 KB

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