io.xml 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. <title>Input/Output</title>
  2. <para>The V4L2 API defines several different methods to read from or
  3. write to a device. All drivers exchanging data with applications must
  4. support at least one of them.</para>
  5. <para>The classic I/O method using the <function>read()</function>
  6. and <function>write()</function> function is automatically selected
  7. after opening a V4L2 device. When the driver does not support this
  8. method attempts to read or write will fail at any time.</para>
  9. <para>Other methods must be negotiated. To select the streaming I/O
  10. method with memory mapped or user buffers applications call the
  11. &VIDIOC-REQBUFS; ioctl. The asynchronous I/O method is not defined
  12. yet.</para>
  13. <para>Video overlay can be considered another I/O method, although
  14. the application does not directly receive the image data. It is
  15. selected by initiating video overlay with the &VIDIOC-S-FMT; ioctl.
  16. For more information see <xref linkend="overlay" />.</para>
  17. <para>Generally exactly one I/O method, including overlay, is
  18. associated with each file descriptor. The only exceptions are
  19. applications not exchanging data with a driver ("panel applications",
  20. see <xref linkend="open" />) and drivers permitting simultaneous video capturing
  21. and overlay using the same file descriptor, for compatibility with V4L
  22. and earlier versions of V4L2.</para>
  23. <para><constant>VIDIOC_S_FMT</constant> and
  24. <constant>VIDIOC_REQBUFS</constant> would permit this to some degree,
  25. but for simplicity drivers need not support switching the I/O method
  26. (after first switching away from read/write) other than by closing
  27. and reopening the device.</para>
  28. <para>The following sections describe the various I/O methods in
  29. more detail.</para>
  30. <section id="rw">
  31. <title>Read/Write</title>
  32. <para>Input and output devices support the
  33. <function>read()</function> and <function>write()</function> function,
  34. respectively, when the <constant>V4L2_CAP_READWRITE</constant> flag in
  35. the <structfield>capabilities</structfield> field of &v4l2-capability;
  36. returned by the &VIDIOC-QUERYCAP; ioctl is set.</para>
  37. <para>Drivers may need the CPU to copy the data, but they may also
  38. support DMA to or from user memory, so this I/O method is not
  39. necessarily less efficient than other methods merely exchanging buffer
  40. pointers. It is considered inferior though because no meta-information
  41. like frame counters or timestamps are passed. This information is
  42. necessary to recognize frame dropping and to synchronize with other
  43. data streams. However this is also the simplest I/O method, requiring
  44. little or no setup to exchange data. It permits command line stunts
  45. like this (the <application>vidctrl</application> tool is
  46. fictitious):</para>
  47. <informalexample>
  48. <screen>
  49. &gt; vidctrl /dev/video --input=0 --format=YUYV --size=352x288
  50. &gt; dd if=/dev/video of=myimage.422 bs=202752 count=1
  51. </screen>
  52. </informalexample>
  53. <para>To read from the device applications use the
  54. &func-read; function, to write the &func-write; function.
  55. Drivers must implement one I/O method if they
  56. exchange data with applications, but it need not be this.<footnote>
  57. <para>It would be desirable if applications could depend on
  58. drivers supporting all I/O interfaces, but as much as the complex
  59. memory mapping I/O can be inadequate for some devices we have no
  60. reason to require this interface, which is most useful for simple
  61. applications capturing still images.</para>
  62. </footnote> When reading or writing is supported, the driver
  63. must also support the &func-select; and &func-poll;
  64. function.<footnote>
  65. <para>At the driver level <function>select()</function> and
  66. <function>poll()</function> are the same, and
  67. <function>select()</function> is too important to be optional.</para>
  68. </footnote></para>
  69. </section>
  70. <section id="mmap">
  71. <title>Streaming I/O (Memory Mapping)</title>
  72. <para>Input and output devices support this I/O method when the
  73. <constant>V4L2_CAP_STREAMING</constant> flag in the
  74. <structfield>capabilities</structfield> field of &v4l2-capability;
  75. returned by the &VIDIOC-QUERYCAP; ioctl is set. There are two
  76. streaming methods, to determine if the memory mapping flavor is
  77. supported applications must call the &VIDIOC-REQBUFS; ioctl.</para>
  78. <para>Streaming is an I/O method where only pointers to buffers
  79. are exchanged between application and driver, the data itself is not
  80. copied. Memory mapping is primarily intended to map buffers in device
  81. memory into the application's address space. Device memory can be for
  82. example the video memory on a graphics card with a video capture
  83. add-on. However, being the most efficient I/O method available for a
  84. long time, many other drivers support streaming as well, allocating
  85. buffers in DMA-able main memory.</para>
  86. <para>A driver can support many sets of buffers. Each set is
  87. identified by a unique buffer type value. The sets are independent and
  88. each set can hold a different type of data. To access different sets
  89. at the same time different file descriptors must be used.<footnote>
  90. <para>One could use one file descriptor and set the buffer
  91. type field accordingly when calling &VIDIOC-QBUF; etc., but it makes
  92. the <function>select()</function> function ambiguous. We also like the
  93. clean approach of one file descriptor per logical stream. Video
  94. overlay for example is also a logical stream, although the CPU is not
  95. needed for continuous operation.</para>
  96. </footnote></para>
  97. <para>To allocate device buffers applications call the
  98. &VIDIOC-REQBUFS; ioctl with the desired number of buffers and buffer
  99. type, for example <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant>.
  100. This ioctl can also be used to change the number of buffers or to free
  101. the allocated memory, provided none of the buffers are still
  102. mapped.</para>
  103. <para>Before applications can access the buffers they must map
  104. them into their address space with the &func-mmap; function. The
  105. location of the buffers in device memory can be determined with the
  106. &VIDIOC-QUERYBUF; ioctl. In the single-planar API case, the
  107. <structfield>m.offset</structfield> and <structfield>length</structfield>
  108. returned in a &v4l2-buffer; are passed as sixth and second parameter to the
  109. <function>mmap()</function> function. When using the multi-planar API,
  110. struct &v4l2-buffer; contains an array of &v4l2-plane; structures, each
  111. containing its own <structfield>m.offset</structfield> and
  112. <structfield>length</structfield>. When using the multi-planar API, every
  113. plane of every buffer has to be mapped separately, so the number of
  114. calls to &func-mmap; should be equal to number of buffers times number of
  115. planes in each buffer. The offset and length values must not be modified.
  116. Remember, the buffers are allocated in physical memory, as opposed to virtual
  117. memory, which can be swapped out to disk. Applications should free the buffers
  118. as soon as possible with the &func-munmap; function.</para>
  119. <example>
  120. <title>Mapping buffers in the single-planar API</title>
  121. <programlisting>
  122. &v4l2-requestbuffers; reqbuf;
  123. struct {
  124. void *start;
  125. size_t length;
  126. } *buffers;
  127. unsigned int i;
  128. memset(&amp;reqbuf, 0, sizeof(reqbuf));
  129. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  130. reqbuf.memory = V4L2_MEMORY_MMAP;
  131. reqbuf.count = 20;
  132. if (-1 == ioctl (fd, &VIDIOC-REQBUFS;, &amp;reqbuf)) {
  133. if (errno == EINVAL)
  134. printf("Video capturing or mmap-streaming is not supported\n");
  135. else
  136. perror("VIDIOC_REQBUFS");
  137. exit(EXIT_FAILURE);
  138. }
  139. /* We want at least five buffers. */
  140. if (reqbuf.count &lt; 5) {
  141. /* You may need to free the buffers here. */
  142. printf("Not enough buffer memory\n");
  143. exit(EXIT_FAILURE);
  144. }
  145. buffers = calloc(reqbuf.count, sizeof(*buffers));
  146. assert(buffers != NULL);
  147. for (i = 0; i &lt; reqbuf.count; i++) {
  148. &v4l2-buffer; buffer;
  149. memset(&amp;buffer, 0, sizeof(buffer));
  150. buffer.type = reqbuf.type;
  151. buffer.memory = V4L2_MEMORY_MMAP;
  152. buffer.index = i;
  153. if (-1 == ioctl (fd, &VIDIOC-QUERYBUF;, &amp;buffer)) {
  154. perror("VIDIOC_QUERYBUF");
  155. exit(EXIT_FAILURE);
  156. }
  157. buffers[i].length = buffer.length; /* remember for munmap() */
  158. buffers[i].start = mmap(NULL, buffer.length,
  159. PROT_READ | PROT_WRITE, /* recommended */
  160. MAP_SHARED, /* recommended */
  161. fd, buffer.m.offset);
  162. if (MAP_FAILED == buffers[i].start) {
  163. /* If you do not exit here you should unmap() and free()
  164. the buffers mapped so far. */
  165. perror("mmap");
  166. exit(EXIT_FAILURE);
  167. }
  168. }
  169. /* Cleanup. */
  170. for (i = 0; i &lt; reqbuf.count; i++)
  171. munmap(buffers[i].start, buffers[i].length);
  172. </programlisting>
  173. </example>
  174. <example>
  175. <title>Mapping buffers in the multi-planar API</title>
  176. <programlisting>
  177. &v4l2-requestbuffers; reqbuf;
  178. /* Our current format uses 3 planes per buffer */
  179. #define FMT_NUM_PLANES = 3
  180. struct {
  181. void *start[FMT_NUM_PLANES];
  182. size_t length[FMT_NUM_PLANES];
  183. } *buffers;
  184. unsigned int i, j;
  185. memset(&amp;reqbuf, 0, sizeof(reqbuf));
  186. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  187. reqbuf.memory = V4L2_MEMORY_MMAP;
  188. reqbuf.count = 20;
  189. if (ioctl(fd, &VIDIOC-REQBUFS;, &amp;reqbuf) &lt; 0) {
  190. if (errno == EINVAL)
  191. printf("Video capturing or mmap-streaming is not supported\n");
  192. else
  193. perror("VIDIOC_REQBUFS");
  194. exit(EXIT_FAILURE);
  195. }
  196. /* We want at least five buffers. */
  197. if (reqbuf.count &lt; 5) {
  198. /* You may need to free the buffers here. */
  199. printf("Not enough buffer memory\n");
  200. exit(EXIT_FAILURE);
  201. }
  202. buffers = calloc(reqbuf.count, sizeof(*buffers));
  203. assert(buffers != NULL);
  204. for (i = 0; i &lt; reqbuf.count; i++) {
  205. &v4l2-buffer; buffer;
  206. &v4l2-plane; planes[FMT_NUM_PLANES];
  207. memset(&amp;buffer, 0, sizeof(buffer));
  208. buffer.type = reqbuf.type;
  209. buffer.memory = V4L2_MEMORY_MMAP;
  210. buffer.index = i;
  211. /* length in struct v4l2_buffer in multi-planar API stores the size
  212. * of planes array. */
  213. buffer.length = FMT_NUM_PLANES;
  214. buffer.m.planes = planes;
  215. if (ioctl(fd, &VIDIOC-QUERYBUF;, &amp;buffer) &lt; 0) {
  216. perror("VIDIOC_QUERYBUF");
  217. exit(EXIT_FAILURE);
  218. }
  219. /* Every plane has to be mapped separately */
  220. for (j = 0; j &lt; FMT_NUM_PLANES; j++) {
  221. buffers[i].length[j] = buffer.m.planes[j].length; /* remember for munmap() */
  222. buffers[i].start[j] = mmap(NULL, buffer.m.planes[j].length,
  223. PROT_READ | PROT_WRITE, /* recommended */
  224. MAP_SHARED, /* recommended */
  225. fd, buffer.m.planes[j].m.offset);
  226. if (MAP_FAILED == buffers[i].start[j]) {
  227. /* If you do not exit here you should unmap() and free()
  228. the buffers and planes mapped so far. */
  229. perror("mmap");
  230. exit(EXIT_FAILURE);
  231. }
  232. }
  233. }
  234. /* Cleanup. */
  235. for (i = 0; i &lt; reqbuf.count; i++)
  236. for (j = 0; j &lt; FMT_NUM_PLANES; j++)
  237. munmap(buffers[i].start[j], buffers[i].length[j]);
  238. </programlisting>
  239. </example>
  240. <para>Conceptually streaming drivers maintain two buffer queues, an incoming
  241. and an outgoing queue. They separate the synchronous capture or output
  242. operation locked to a video clock from the application which is
  243. subject to random disk or network delays and preemption by
  244. other processes, thereby reducing the probability of data loss.
  245. The queues are organized as FIFOs, buffers will be
  246. output in the order enqueued in the incoming FIFO, and were
  247. captured in the order dequeued from the outgoing FIFO.</para>
  248. <para>The driver may require a minimum number of buffers enqueued
  249. at all times to function, apart of this no limit exists on the number
  250. of buffers applications can enqueue in advance, or dequeue and
  251. process. They can also enqueue in a different order than buffers have
  252. been dequeued, and the driver can <emphasis>fill</emphasis> enqueued
  253. <emphasis>empty</emphasis> buffers in any order. <footnote>
  254. <para>Random enqueue order permits applications processing
  255. images out of order (such as video codecs) to return buffers earlier,
  256. reducing the probability of data loss. Random fill order allows
  257. drivers to reuse buffers on a LIFO-basis, taking advantage of caches
  258. holding scatter-gather lists and the like.</para>
  259. </footnote> The index number of a buffer (&v4l2-buffer;
  260. <structfield>index</structfield>) plays no role here, it only
  261. identifies the buffer.</para>
  262. <para>Initially all mapped buffers are in dequeued state,
  263. inaccessible by the driver. For capturing applications it is customary
  264. to first enqueue all mapped buffers, then to start capturing and enter
  265. the read loop. Here the application waits until a filled buffer can be
  266. dequeued, and re-enqueues the buffer when the data is no longer
  267. needed. Output applications fill and enqueue buffers, when enough
  268. buffers are stacked up the output is started with
  269. <constant>VIDIOC_STREAMON</constant>. In the write loop, when
  270. the application runs out of free buffers, it must wait until an empty
  271. buffer can be dequeued and reused.</para>
  272. <para>To enqueue and dequeue a buffer applications use the
  273. &VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl. The status of a buffer being
  274. mapped, enqueued, full or empty can be determined at any time using the
  275. &VIDIOC-QUERYBUF; ioctl. Two methods exist to suspend execution of the
  276. application until one or more buffers can be dequeued. By default
  277. <constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the
  278. outgoing queue. When the <constant>O_NONBLOCK</constant> flag was
  279. given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>
  280. returns immediately with an &EAGAIN; when no buffer is available. The
  281. &func-select; or &func-poll; functions are always available.</para>
  282. <para>To start and stop capturing or output applications call the
  283. &VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note
  284. <constant>VIDIOC_STREAMOFF</constant> removes all buffers from both
  285. queues as a side effect. Since there is no notion of doing anything
  286. "now" on a multitasking system, if an application needs to synchronize
  287. with another event it should examine the &v4l2-buffer;
  288. <structfield>timestamp</structfield> of captured buffers, or set the
  289. field before enqueuing buffers for output.</para>
  290. <para>Drivers implementing memory mapping I/O must
  291. support the <constant>VIDIOC_REQBUFS</constant>,
  292. <constant>VIDIOC_QUERYBUF</constant>,
  293. <constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,
  294. <constant>VIDIOC_STREAMON</constant> and
  295. <constant>VIDIOC_STREAMOFF</constant> ioctl, the
  296. <function>mmap()</function>, <function>munmap()</function>,
  297. <function>select()</function> and <function>poll()</function>
  298. function.<footnote>
  299. <para>At the driver level <function>select()</function> and
  300. <function>poll()</function> are the same, and
  301. <function>select()</function> is too important to be optional. The
  302. rest should be evident.</para>
  303. </footnote></para>
  304. <para>[capture example]</para>
  305. </section>
  306. <section id="userp">
  307. <title>Streaming I/O (User Pointers)</title>
  308. <para>Input and output devices support this I/O method when the
  309. <constant>V4L2_CAP_STREAMING</constant> flag in the
  310. <structfield>capabilities</structfield> field of &v4l2-capability;
  311. returned by the &VIDIOC-QUERYCAP; ioctl is set. If the particular user
  312. pointer method (not only memory mapping) is supported must be
  313. determined by calling the &VIDIOC-REQBUFS; ioctl.</para>
  314. <para>This I/O method combines advantages of the read/write and
  315. memory mapping methods. Buffers (planes) are allocated by the application
  316. itself, and can reside for example in virtual or shared memory. Only
  317. pointers to data are exchanged, these pointers and meta-information
  318. are passed in &v4l2-buffer; (or in &v4l2-plane; in the multi-planar API case).
  319. The driver must be switched into user pointer I/O mode by calling the
  320. &VIDIOC-REQBUFS; with the desired buffer type. No buffers (planes) are allocated
  321. beforehand, consequently they are not indexed and cannot be queried like mapped
  322. buffers with the <constant>VIDIOC_QUERYBUF</constant> ioctl.</para>
  323. <example>
  324. <title>Initiating streaming I/O with user pointers</title>
  325. <programlisting>
  326. &v4l2-requestbuffers; reqbuf;
  327. memset (&amp;reqbuf, 0, sizeof (reqbuf));
  328. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  329. reqbuf.memory = V4L2_MEMORY_USERPTR;
  330. if (ioctl (fd, &VIDIOC-REQBUFS;, &amp;reqbuf) == -1) {
  331. if (errno == EINVAL)
  332. printf ("Video capturing or user pointer streaming is not supported\n");
  333. else
  334. perror ("VIDIOC_REQBUFS");
  335. exit (EXIT_FAILURE);
  336. }
  337. </programlisting>
  338. </example>
  339. <para>Buffer (plane) addresses and sizes are passed on the fly with the
  340. &VIDIOC-QBUF; ioctl. Although buffers are commonly cycled,
  341. applications can pass different addresses and sizes at each
  342. <constant>VIDIOC_QBUF</constant> call. If required by the hardware the
  343. driver swaps memory pages within physical memory to create a
  344. continuous area of memory. This happens transparently to the
  345. application in the virtual memory subsystem of the kernel. When buffer
  346. pages have been swapped out to disk they are brought back and finally
  347. locked in physical memory for DMA.<footnote>
  348. <para>We expect that frequently used buffers are typically not
  349. swapped out. Anyway, the process of swapping, locking or generating
  350. scatter-gather lists may be time consuming. The delay can be masked by
  351. the depth of the incoming buffer queue, and perhaps by maintaining
  352. caches assuming a buffer will be soon enqueued again. On the other
  353. hand, to optimize memory usage drivers can limit the number of buffers
  354. locked in advance and recycle the most recently used buffers first. Of
  355. course, the pages of empty buffers in the incoming queue need not be
  356. saved to disk. Output buffers must be saved on the incoming and
  357. outgoing queue because an application may share them with other
  358. processes.</para>
  359. </footnote></para>
  360. <para>Filled or displayed buffers are dequeued with the
  361. &VIDIOC-DQBUF; ioctl. The driver can unlock the memory pages at any
  362. time between the completion of the DMA and this ioctl. The memory is
  363. also unlocked when &VIDIOC-STREAMOFF; is called, &VIDIOC-REQBUFS;, or
  364. when the device is closed. Applications must take care not to free
  365. buffers without dequeuing. For once, the buffers remain locked until
  366. further, wasting physical memory. Second the driver will not be
  367. notified when the memory is returned to the application's free list
  368. and subsequently reused for other purposes, possibly completing the
  369. requested DMA and overwriting valuable data.</para>
  370. <para>For capturing applications it is customary to enqueue a
  371. number of empty buffers, to start capturing and enter the read loop.
  372. Here the application waits until a filled buffer can be dequeued, and
  373. re-enqueues the buffer when the data is no longer needed. Output
  374. applications fill and enqueue buffers, when enough buffers are stacked
  375. up output is started. In the write loop, when the application
  376. runs out of free buffers it must wait until an empty buffer can be
  377. dequeued and reused. Two methods exist to suspend execution of the
  378. application until one or more buffers can be dequeued. By default
  379. <constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the
  380. outgoing queue. When the <constant>O_NONBLOCK</constant> flag was
  381. given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>
  382. returns immediately with an &EAGAIN; when no buffer is available. The
  383. &func-select; or &func-poll; function are always available.</para>
  384. <para>To start and stop capturing or output applications call the
  385. &VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctl. Note
  386. <constant>VIDIOC_STREAMOFF</constant> removes all buffers from both
  387. queues and unlocks all buffers as a side effect. Since there is no
  388. notion of doing anything "now" on a multitasking system, if an
  389. application needs to synchronize with another event it should examine
  390. the &v4l2-buffer; <structfield>timestamp</structfield> of captured
  391. buffers, or set the field before enqueuing buffers for output.</para>
  392. <para>Drivers implementing user pointer I/O must
  393. support the <constant>VIDIOC_REQBUFS</constant>,
  394. <constant>VIDIOC_QBUF</constant>, <constant>VIDIOC_DQBUF</constant>,
  395. <constant>VIDIOC_STREAMON</constant> and
  396. <constant>VIDIOC_STREAMOFF</constant> ioctl, the
  397. <function>select()</function> and <function>poll()</function> function.<footnote>
  398. <para>At the driver level <function>select()</function> and
  399. <function>poll()</function> are the same, and
  400. <function>select()</function> is too important to be optional. The
  401. rest should be evident.</para>
  402. </footnote></para>
  403. </section>
  404. <section id="dmabuf">
  405. <title>Streaming I/O (DMA buffer importing)</title>
  406. <note>
  407. <title>Experimental</title>
  408. <para>This is an <link linkend="experimental"> experimental </link>
  409. interface and may change in the future.</para>
  410. </note>
  411. <para>The DMABUF framework provides a generic method for sharing buffers
  412. between multiple devices. Device drivers that support DMABUF can export a DMA
  413. buffer to userspace as a file descriptor (known as the exporter role), import a
  414. DMA buffer from userspace using a file descriptor previously exported for a
  415. different or the same device (known as the importer role), or both. This
  416. section describes the DMABUF importer role API in V4L2.</para>
  417. <para>Refer to <link linked="vidioc-expbuf"> DMABUF exporting </link> for
  418. details about exporting V4L2 buffers as DMABUF file descriptors.</para>
  419. <para>Input and output devices support the streaming I/O method when the
  420. <constant>V4L2_CAP_STREAMING</constant> flag in the
  421. <structfield>capabilities</structfield> field of &v4l2-capability; returned by
  422. the &VIDIOC-QUERYCAP; ioctl is set. Whether importing DMA buffers through
  423. DMABUF file descriptors is supported is determined by calling the
  424. &VIDIOC-REQBUFS; ioctl with the memory type set to
  425. <constant>V4L2_MEMORY_DMABUF</constant>.</para>
  426. <para>This I/O method is dedicated to sharing DMA buffers between different
  427. devices, which may be V4L devices or other video-related devices (e.g. DRM).
  428. Buffers (planes) are allocated by a driver on behalf of an application. Next,
  429. these buffers are exported to the application as file descriptors using an API
  430. which is specific for an allocator driver. Only such file descriptor are
  431. exchanged. The descriptors and meta-information are passed in &v4l2-buffer; (or
  432. in &v4l2-plane; in the multi-planar API case). The driver must be switched
  433. into DMABUF I/O mode by calling the &VIDIOC-REQBUFS; with the desired buffer
  434. type.</para>
  435. <example>
  436. <title>Initiating streaming I/O with DMABUF file descriptors</title>
  437. <programlisting>
  438. &v4l2-requestbuffers; reqbuf;
  439. memset(&amp;reqbuf, 0, sizeof (reqbuf));
  440. reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  441. reqbuf.memory = V4L2_MEMORY_DMABUF;
  442. reqbuf.count = 1;
  443. if (ioctl(fd, &VIDIOC-REQBUFS;, &amp;reqbuf) == -1) {
  444. if (errno == EINVAL)
  445. printf("Video capturing or DMABUF streaming is not supported\n");
  446. else
  447. perror("VIDIOC_REQBUFS");
  448. exit(EXIT_FAILURE);
  449. }
  450. </programlisting>
  451. </example>
  452. <para>The buffer (plane) file descriptor is passed on the fly with the
  453. &VIDIOC-QBUF; ioctl. In case of multiplanar buffers, every plane can be
  454. associated with a different DMABUF descriptor. Although buffers are commonly
  455. cycled, applications can pass a different DMABUF descriptor at each
  456. <constant>VIDIOC_QBUF</constant> call.</para>
  457. <example>
  458. <title>Queueing DMABUF using single plane API</title>
  459. <programlisting>
  460. int buffer_queue(int v4lfd, int index, int dmafd)
  461. {
  462. &v4l2-buffer; buf;
  463. memset(&amp;buf, 0, sizeof buf);
  464. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  465. buf.memory = V4L2_MEMORY_DMABUF;
  466. buf.index = index;
  467. buf.m.fd = dmafd;
  468. if (ioctl(v4lfd, &VIDIOC-QBUF;, &amp;buf) == -1) {
  469. perror("VIDIOC_QBUF");
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. </programlisting>
  475. </example>
  476. <example>
  477. <title>Queueing DMABUF using multi plane API</title>
  478. <programlisting>
  479. int buffer_queue_mp(int v4lfd, int index, int dmafd[], int n_planes)
  480. {
  481. &v4l2-buffer; buf;
  482. &v4l2-plane; planes[VIDEO_MAX_PLANES];
  483. int i;
  484. memset(&amp;buf, 0, sizeof buf);
  485. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  486. buf.memory = V4L2_MEMORY_DMABUF;
  487. buf.index = index;
  488. buf.m.planes = planes;
  489. buf.length = n_planes;
  490. memset(&amp;planes, 0, sizeof planes);
  491. for (i = 0; i &lt; n_planes; ++i)
  492. buf.m.planes[i].m.fd = dmafd[i];
  493. if (ioctl(v4lfd, &VIDIOC-QBUF;, &amp;buf) == -1) {
  494. perror("VIDIOC_QBUF");
  495. return -1;
  496. }
  497. return 0;
  498. }
  499. </programlisting>
  500. </example>
  501. <para>Captured or displayed buffers are dequeued with the
  502. &VIDIOC-DQBUF; ioctl. The driver can unlock the buffer at any
  503. time between the completion of the DMA and this ioctl. The memory is
  504. also unlocked when &VIDIOC-STREAMOFF; is called, &VIDIOC-REQBUFS;, or
  505. when the device is closed.</para>
  506. <para>For capturing applications it is customary to enqueue a
  507. number of empty buffers, to start capturing and enter the read loop.
  508. Here the application waits until a filled buffer can be dequeued, and
  509. re-enqueues the buffer when the data is no longer needed. Output
  510. applications fill and enqueue buffers, when enough buffers are stacked
  511. up output is started. In the write loop, when the application
  512. runs out of free buffers it must wait until an empty buffer can be
  513. dequeued and reused. Two methods exist to suspend execution of the
  514. application until one or more buffers can be dequeued. By default
  515. <constant>VIDIOC_DQBUF</constant> blocks when no buffer is in the
  516. outgoing queue. When the <constant>O_NONBLOCK</constant> flag was
  517. given to the &func-open; function, <constant>VIDIOC_DQBUF</constant>
  518. returns immediately with an &EAGAIN; when no buffer is available. The
  519. &func-select; and &func-poll; functions are always available.</para>
  520. <para>To start and stop capturing or displaying applications call the
  521. &VIDIOC-STREAMON; and &VIDIOC-STREAMOFF; ioctls. Note that
  522. <constant>VIDIOC_STREAMOFF</constant> removes all buffers from both queues and
  523. unlocks all buffers as a side effect. Since there is no notion of doing
  524. anything "now" on a multitasking system, if an application needs to synchronize
  525. with another event it should examine the &v4l2-buffer;
  526. <structfield>timestamp</structfield> of captured buffers, or set the field
  527. before enqueuing buffers for output.</para>
  528. <para>Drivers implementing DMABUF importing I/O must support the
  529. <constant>VIDIOC_REQBUFS</constant>, <constant>VIDIOC_QBUF</constant>,
  530. <constant>VIDIOC_DQBUF</constant>, <constant>VIDIOC_STREAMON</constant> and
  531. <constant>VIDIOC_STREAMOFF</constant> ioctls, and the
  532. <function>select()</function> and <function>poll()</function> functions.</para>
  533. </section>
  534. <section id="async">
  535. <title>Asynchronous I/O</title>
  536. <para>This method is not defined yet.</para>
  537. </section>
  538. <section id="buffer">
  539. <title>Buffers</title>
  540. <para>A buffer contains data exchanged by application and
  541. driver using one of the Streaming I/O methods. In the multi-planar API, the
  542. data is held in planes, while the buffer structure acts as a container
  543. for the planes. Only pointers to buffers (planes) are exchanged, the data
  544. itself is not copied. These pointers, together with meta-information like
  545. timestamps or field parity, are stored in a struct
  546. <structname>v4l2_buffer</structname>, argument to
  547. the &VIDIOC-QUERYBUF;, &VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl.
  548. In the multi-planar API, some plane-specific members of struct
  549. <structname>v4l2_buffer</structname>, such as pointers and sizes for each
  550. plane, are stored in struct <structname>v4l2_plane</structname> instead.
  551. In that case, struct <structname>v4l2_buffer</structname> contains an array of
  552. plane structures.</para>
  553. <para>Nominally timestamps refer to the first data byte transmitted.
  554. In practice however the wide range of hardware covered by the V4L2 API
  555. limits timestamp accuracy. Often an interrupt routine will
  556. sample the system clock shortly after the field or frame was stored
  557. completely in memory. So applications must expect a constant
  558. difference up to one field or frame period plus a small (few scan
  559. lines) random error. The delay and error can be much
  560. larger due to compression or transmission over an external bus when
  561. the frames are not properly stamped by the sender. This is frequently
  562. the case with USB cameras. Here timestamps refer to the instant the
  563. field or frame was received by the driver, not the capture time. These
  564. devices identify by not enumerating any video standards, see <xref
  565. linkend="standard" />.</para>
  566. <para>Similar limitations apply to output timestamps. Typically
  567. the video hardware locks to a clock controlling the video timing, the
  568. horizontal and vertical synchronization pulses. At some point in the
  569. line sequence, possibly the vertical blanking, an interrupt routine
  570. samples the system clock, compares against the timestamp and programs
  571. the hardware to repeat the previous field or frame, or to display the
  572. buffer contents.</para>
  573. <para>Apart of limitations of the video device and natural
  574. inaccuracies of all clocks, it should be noted system time itself is
  575. not perfectly stable. It can be affected by power saving cycles,
  576. warped to insert leap seconds, or even turned back or forth by the
  577. system administrator affecting long term measurements. <footnote>
  578. <para>Since no other Linux multimedia
  579. API supports unadjusted time it would be foolish to introduce here. We
  580. must use a universally supported clock to synchronize different media,
  581. hence time of day.</para>
  582. </footnote></para>
  583. <table frame="none" pgwide="1" id="v4l2-buffer">
  584. <title>struct <structname>v4l2_buffer</structname></title>
  585. <tgroup cols="4">
  586. &cs-ustr;
  587. <tbody valign="top">
  588. <row>
  589. <entry>__u32</entry>
  590. <entry><structfield>index</structfield></entry>
  591. <entry></entry>
  592. <entry>Number of the buffer, set by the application. This
  593. field is only used for <link linkend="mmap">memory mapping</link> I/O
  594. and can range from zero to the number of buffers allocated
  595. with the &VIDIOC-REQBUFS; ioctl (&v4l2-requestbuffers; <structfield>count</structfield>) minus one.</entry>
  596. </row>
  597. <row>
  598. <entry>__u32</entry>
  599. <entry><structfield>type</structfield></entry>
  600. <entry></entry>
  601. <entry>Type of the buffer, same as &v4l2-format;
  602. <structfield>type</structfield> or &v4l2-requestbuffers;
  603. <structfield>type</structfield>, set by the application. See <xref
  604. linkend="v4l2-buf-type" /></entry>
  605. </row>
  606. <row>
  607. <entry>__u32</entry>
  608. <entry><structfield>bytesused</structfield></entry>
  609. <entry></entry>
  610. <entry>The number of bytes occupied by the data in the
  611. buffer. It depends on the negotiated data format and may change with
  612. each buffer for compressed variable size data like JPEG images.
  613. Drivers must set this field when <structfield>type</structfield>
  614. refers to an input stream, applications when an output stream.</entry>
  615. </row>
  616. <row>
  617. <entry>__u32</entry>
  618. <entry><structfield>flags</structfield></entry>
  619. <entry></entry>
  620. <entry>Flags set by the application or driver, see <xref
  621. linkend="buffer-flags" />.</entry>
  622. </row>
  623. <row>
  624. <entry>__u32</entry>
  625. <entry><structfield>field</structfield></entry>
  626. <entry></entry>
  627. <entry>Indicates the field order of the image in the
  628. buffer, see <xref linkend="v4l2-field" />. This field is not used when
  629. the buffer contains VBI data. Drivers must set it when
  630. <structfield>type</structfield> refers to an input stream,
  631. applications when an output stream.</entry>
  632. </row>
  633. <row>
  634. <entry>struct timeval</entry>
  635. <entry><structfield>timestamp</structfield></entry>
  636. <entry></entry>
  637. <entry><para>For input streams this is the
  638. system time (as returned by the <function>gettimeofday()</function>
  639. function) when the first data byte was captured. For output streams
  640. the data will not be displayed before this time, secondary to the
  641. nominal frame rate determined by the current video standard in
  642. enqueued order. Applications can for example zero this field to
  643. display frames as soon as possible. The driver stores the time at
  644. which the first data byte was actually sent out in the
  645. <structfield>timestamp</structfield> field. This permits
  646. applications to monitor the drift between the video and system
  647. clock.</para></entry>
  648. </row>
  649. <row>
  650. <entry>&v4l2-timecode;</entry>
  651. <entry><structfield>timecode</structfield></entry>
  652. <entry></entry>
  653. <entry>When <structfield>type</structfield> is
  654. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> and the
  655. <constant>V4L2_BUF_FLAG_TIMECODE</constant> flag is set in
  656. <structfield>flags</structfield>, this structure contains a frame
  657. timecode. In <link linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link>
  658. mode the top and bottom field contain the same timecode.
  659. Timecodes are intended to help video editing and are typically recorded on
  660. video tapes, but also embedded in compressed formats like MPEG. This
  661. field is independent of the <structfield>timestamp</structfield> and
  662. <structfield>sequence</structfield> fields.</entry>
  663. </row>
  664. <row>
  665. <entry>__u32</entry>
  666. <entry><structfield>sequence</structfield></entry>
  667. <entry></entry>
  668. <entry>Set by the driver, counting the frames (not fields!) in
  669. sequence. This field is set for both input and output devices.</entry>
  670. </row>
  671. <row>
  672. <entry spanname="hspan"><para>In <link
  673. linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link> mode the top and
  674. bottom field have the same sequence number. The count starts at zero
  675. and includes dropped or repeated frames. A dropped frame was received
  676. by an input device but could not be stored due to lack of free buffer
  677. space. A repeated frame was displayed again by an output device
  678. because the application did not pass new data in
  679. time.</para><para>Note this may count the frames received
  680. e.g. over USB, without taking into account the frames dropped by the
  681. remote hardware due to limited compression throughput or bus
  682. bandwidth. These devices identify by not enumerating any video
  683. standards, see <xref linkend="standard" />.</para></entry>
  684. </row>
  685. <row>
  686. <entry>__u32</entry>
  687. <entry><structfield>memory</structfield></entry>
  688. <entry></entry>
  689. <entry>This field must be set by applications and/or drivers
  690. in accordance with the selected I/O method. See <xref linkend="v4l2-memory"
  691. /></entry>
  692. </row>
  693. <row>
  694. <entry>union</entry>
  695. <entry><structfield>m</structfield></entry>
  696. </row>
  697. <row>
  698. <entry></entry>
  699. <entry>__u32</entry>
  700. <entry><structfield>offset</structfield></entry>
  701. <entry>For the single-planar API and when
  702. <structfield>memory</structfield> is <constant>V4L2_MEMORY_MMAP</constant> this
  703. is the offset of the buffer from the start of the device memory. The value is
  704. returned by the driver and apart of serving as parameter to the &func-mmap;
  705. function not useful for applications. See <xref linkend="mmap" /> for details
  706. </entry>
  707. </row>
  708. <row>
  709. <entry></entry>
  710. <entry>unsigned long</entry>
  711. <entry><structfield>userptr</structfield></entry>
  712. <entry>For the single-planar API and when
  713. <structfield>memory</structfield> is <constant>V4L2_MEMORY_USERPTR</constant>
  714. this is a pointer to the buffer (casted to unsigned long type) in virtual
  715. memory, set by the application. See <xref linkend="userp" /> for details.
  716. </entry>
  717. </row>
  718. <row>
  719. <entry></entry>
  720. <entry>struct v4l2_plane</entry>
  721. <entry><structfield>*planes</structfield></entry>
  722. <entry>When using the multi-planar API, contains a userspace pointer
  723. to an array of &v4l2-plane;. The size of the array should be put
  724. in the <structfield>length</structfield> field of this
  725. <structname>v4l2_buffer</structname> structure.</entry>
  726. </row>
  727. <row>
  728. <entry></entry>
  729. <entry>int</entry>
  730. <entry><structfield>fd</structfield></entry>
  731. <entry>For the single-plane API and when
  732. <structfield>memory</structfield> is <constant>V4L2_MEMORY_DMABUF</constant> this
  733. is the file descriptor associated with a DMABUF buffer.</entry>
  734. </row>
  735. <row>
  736. <entry>__u32</entry>
  737. <entry><structfield>length</structfield></entry>
  738. <entry></entry>
  739. <entry>Size of the buffer (not the payload) in bytes for the
  740. single-planar API. For the multi-planar API the application sets
  741. this to the number of elements in the <structfield>planes</structfield>
  742. array. The driver will fill in the actual number of valid elements in
  743. that array.
  744. </entry>
  745. </row>
  746. <row>
  747. <entry>__u32</entry>
  748. <entry><structfield>reserved2</structfield></entry>
  749. <entry></entry>
  750. <entry>A place holder for future extensions. Applications
  751. should set this to 0.</entry>
  752. </row>
  753. <row>
  754. <entry>__u32</entry>
  755. <entry><structfield>reserved</structfield></entry>
  756. <entry></entry>
  757. <entry>A place holder for future extensions. Applications
  758. should set this to 0.</entry>
  759. </row>
  760. </tbody>
  761. </tgroup>
  762. </table>
  763. <table frame="none" pgwide="1" id="v4l2-plane">
  764. <title>struct <structname>v4l2_plane</structname></title>
  765. <tgroup cols="4">
  766. &cs-ustr;
  767. <tbody valign="top">
  768. <row>
  769. <entry>__u32</entry>
  770. <entry><structfield>bytesused</structfield></entry>
  771. <entry></entry>
  772. <entry>The number of bytes occupied by data in the plane
  773. (its payload).</entry>
  774. </row>
  775. <row>
  776. <entry>__u32</entry>
  777. <entry><structfield>length</structfield></entry>
  778. <entry></entry>
  779. <entry>Size in bytes of the plane (not its payload).</entry>
  780. </row>
  781. <row>
  782. <entry>union</entry>
  783. <entry><structfield>m</structfield></entry>
  784. <entry></entry>
  785. <entry></entry>
  786. </row>
  787. <row>
  788. <entry></entry>
  789. <entry>__u32</entry>
  790. <entry><structfield>mem_offset</structfield></entry>
  791. <entry>When the memory type in the containing &v4l2-buffer; is
  792. <constant>V4L2_MEMORY_MMAP</constant>, this is the value that
  793. should be passed to &func-mmap;, similar to the
  794. <structfield>offset</structfield> field in &v4l2-buffer;.</entry>
  795. </row>
  796. <row>
  797. <entry></entry>
  798. <entry>__unsigned long</entry>
  799. <entry><structfield>userptr</structfield></entry>
  800. <entry>When the memory type in the containing &v4l2-buffer; is
  801. <constant>V4L2_MEMORY_USERPTR</constant>, this is a userspace
  802. pointer to the memory allocated for this plane by an application.
  803. </entry>
  804. </row>
  805. <row>
  806. <entry></entry>
  807. <entry>int</entry>
  808. <entry><structfield>fd</structfield></entry>
  809. <entry>When the memory type in the containing &v4l2-buffer; is
  810. <constant>V4L2_MEMORY_DMABUF</constant>, this is a file
  811. descriptor associated with a DMABUF buffer, similar to the
  812. <structfield>fd</structfield> field in &v4l2-buffer;.</entry>
  813. </row>
  814. <row>
  815. <entry>__u32</entry>
  816. <entry><structfield>data_offset</structfield></entry>
  817. <entry></entry>
  818. <entry>Offset in bytes to video data in the plane, if applicable.
  819. </entry>
  820. </row>
  821. <row>
  822. <entry>__u32</entry>
  823. <entry><structfield>reserved[11]</structfield></entry>
  824. <entry></entry>
  825. <entry>Reserved for future use. Should be zeroed by an
  826. application.</entry>
  827. </row>
  828. </tbody>
  829. </tgroup>
  830. </table>
  831. <table frame="none" pgwide="1" id="v4l2-buf-type">
  832. <title>enum v4l2_buf_type</title>
  833. <tgroup cols="3">
  834. &cs-def;
  835. <tbody valign="top">
  836. <row>
  837. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant></entry>
  838. <entry>1</entry>
  839. <entry>Buffer of a single-planar video capture stream, see <xref
  840. linkend="capture" />.</entry>
  841. </row>
  842. <row>
  843. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE</constant>
  844. </entry>
  845. <entry>9</entry>
  846. <entry>Buffer of a multi-planar video capture stream, see <xref
  847. linkend="capture" />.</entry>
  848. </row>
  849. <row>
  850. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT</constant></entry>
  851. <entry>2</entry>
  852. <entry>Buffer of a single-planar video output stream, see <xref
  853. linkend="output" />.</entry>
  854. </row>
  855. <row>
  856. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE</constant>
  857. </entry>
  858. <entry>10</entry>
  859. <entry>Buffer of a multi-planar video output stream, see <xref
  860. linkend="output" />.</entry>
  861. </row>
  862. <row>
  863. <entry><constant>V4L2_BUF_TYPE_VIDEO_OVERLAY</constant></entry>
  864. <entry>3</entry>
  865. <entry>Buffer for video overlay, see <xref linkend="overlay" />.</entry>
  866. </row>
  867. <row>
  868. <entry><constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant></entry>
  869. <entry>4</entry>
  870. <entry>Buffer of a raw VBI capture stream, see <xref
  871. linkend="raw-vbi" />.</entry>
  872. </row>
  873. <row>
  874. <entry><constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant></entry>
  875. <entry>5</entry>
  876. <entry>Buffer of a raw VBI output stream, see <xref
  877. linkend="raw-vbi" />.</entry>
  878. </row>
  879. <row>
  880. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_CAPTURE</constant></entry>
  881. <entry>6</entry>
  882. <entry>Buffer of a sliced VBI capture stream, see <xref
  883. linkend="sliced" />.</entry>
  884. </row>
  885. <row>
  886. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_OUTPUT</constant></entry>
  887. <entry>7</entry>
  888. <entry>Buffer of a sliced VBI output stream, see <xref
  889. linkend="sliced" />.</entry>
  890. </row>
  891. <row>
  892. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY</constant></entry>
  893. <entry>8</entry>
  894. <entry>Buffer for video output overlay (OSD), see <xref
  895. linkend="osd" />.</entry>
  896. </row>
  897. </tbody>
  898. </tgroup>
  899. </table>
  900. <table frame="none" pgwide="1" id="buffer-flags">
  901. <title>Buffer Flags</title>
  902. <tgroup cols="3">
  903. &cs-def;
  904. <tbody valign="top">
  905. <row>
  906. <entry><constant>V4L2_BUF_FLAG_MAPPED</constant></entry>
  907. <entry>0x0001</entry>
  908. <entry>The buffer resides in device memory and has been mapped
  909. into the application's address space, see <xref linkend="mmap" /> for details.
  910. Drivers set or clear this flag when the
  911. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  912. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  913. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called. Set by the driver.</entry>
  914. </row>
  915. <row>
  916. <entry><constant>V4L2_BUF_FLAG_QUEUED</constant></entry>
  917. <entry>0x0002</entry>
  918. <entry>Internally drivers maintain two buffer queues, an
  919. incoming and outgoing queue. When this flag is set, the buffer is
  920. currently on the incoming queue. It automatically moves to the
  921. outgoing queue after the buffer has been filled (capture devices) or
  922. displayed (output devices). Drivers set or clear this flag when the
  923. <constant>VIDIOC_QUERYBUF</constant> ioctl is called. After
  924. (successful) calling the <constant>VIDIOC_QBUF </constant>ioctl it is
  925. always set and after <constant>VIDIOC_DQBUF</constant> always
  926. cleared.</entry>
  927. </row>
  928. <row>
  929. <entry><constant>V4L2_BUF_FLAG_DONE</constant></entry>
  930. <entry>0x0004</entry>
  931. <entry>When this flag is set, the buffer is currently on
  932. the outgoing queue, ready to be dequeued from the driver. Drivers set
  933. or clear this flag when the <constant>VIDIOC_QUERYBUF</constant> ioctl
  934. is called. After calling the <constant>VIDIOC_QBUF</constant> or
  935. <constant>VIDIOC_DQBUF</constant> it is always cleared. Of course a
  936. buffer cannot be on both queues at the same time, the
  937. <constant>V4L2_BUF_FLAG_QUEUED</constant> and
  938. <constant>V4L2_BUF_FLAG_DONE</constant> flag are mutually exclusive.
  939. They can be both cleared however, then the buffer is in "dequeued"
  940. state, in the application domain to say so.</entry>
  941. </row>
  942. <row>
  943. <entry><constant>V4L2_BUF_FLAG_ERROR</constant></entry>
  944. <entry>0x0040</entry>
  945. <entry>When this flag is set, the buffer has been dequeued
  946. successfully, although the data might have been corrupted.
  947. This is recoverable, streaming may continue as normal and
  948. the buffer may be reused normally.
  949. Drivers set this flag when the <constant>VIDIOC_DQBUF</constant>
  950. ioctl is called.</entry>
  951. </row>
  952. <row>
  953. <entry><constant>V4L2_BUF_FLAG_KEYFRAME</constant></entry>
  954. <entry>0x0008</entry>
  955. <entry>Drivers set or clear this flag when calling the
  956. <constant>VIDIOC_DQBUF</constant> ioctl. It may be set by video
  957. capture devices when the buffer contains a compressed image which is a
  958. key frame (or field), &ie; can be decompressed on its own.</entry>
  959. </row>
  960. <row>
  961. <entry><constant>V4L2_BUF_FLAG_PFRAME</constant></entry>
  962. <entry>0x0010</entry>
  963. <entry>Similar to <constant>V4L2_BUF_FLAG_KEYFRAME</constant>
  964. this flags predicted frames or fields which contain only differences to a
  965. previous key frame.</entry>
  966. </row>
  967. <row>
  968. <entry><constant>V4L2_BUF_FLAG_BFRAME</constant></entry>
  969. <entry>0x0020</entry>
  970. <entry>Similar to <constant>V4L2_BUF_FLAG_PFRAME</constant>
  971. this is a bidirectional predicted frame or field. [ooc tbd]</entry>
  972. </row>
  973. <row>
  974. <entry><constant>V4L2_BUF_FLAG_TIMECODE</constant></entry>
  975. <entry>0x0100</entry>
  976. <entry>The <structfield>timecode</structfield> field is valid.
  977. Drivers set or clear this flag when the <constant>VIDIOC_DQBUF</constant>
  978. ioctl is called.</entry>
  979. </row>
  980. <row>
  981. <entry><constant>V4L2_BUF_FLAG_PREPARED</constant></entry>
  982. <entry>0x0400</entry>
  983. <entry>The buffer has been prepared for I/O and can be queued by the
  984. application. Drivers set or clear this flag when the
  985. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  986. linkend="vidioc-qbuf">VIDIOC_PREPARE_BUF</link>, <link
  987. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  988. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called.</entry>
  989. </row>
  990. <row>
  991. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_INVALIDATE</constant></entry>
  992. <entry>0x0800</entry>
  993. <entry>Caches do not have to be invalidated for this buffer.
  994. Typically applications shall use this flag if the data captured in the buffer
  995. is not going to be touched by the CPU, instead the buffer will, probably, be
  996. passed on to a DMA-capable hardware unit for further processing or output.
  997. </entry>
  998. </row>
  999. <row>
  1000. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_CLEAN</constant></entry>
  1001. <entry>0x1000</entry>
  1002. <entry>Caches do not have to be cleaned for this buffer.
  1003. Typically applications shall use this flag for output buffers if the data
  1004. in this buffer has not been created by the CPU but by some DMA-capable unit,
  1005. in which case caches have not been used.</entry>
  1006. </row>
  1007. </tbody>
  1008. </tgroup>
  1009. </table>
  1010. <table pgwide="1" frame="none" id="v4l2-memory">
  1011. <title>enum v4l2_memory</title>
  1012. <tgroup cols="3">
  1013. &cs-def;
  1014. <tbody valign="top">
  1015. <row>
  1016. <entry><constant>V4L2_MEMORY_MMAP</constant></entry>
  1017. <entry>1</entry>
  1018. <entry>The buffer is used for <link linkend="mmap">memory
  1019. mapping</link> I/O.</entry>
  1020. </row>
  1021. <row>
  1022. <entry><constant>V4L2_MEMORY_USERPTR</constant></entry>
  1023. <entry>2</entry>
  1024. <entry>The buffer is used for <link linkend="userp">user
  1025. pointer</link> I/O.</entry>
  1026. </row>
  1027. <row>
  1028. <entry><constant>V4L2_MEMORY_OVERLAY</constant></entry>
  1029. <entry>3</entry>
  1030. <entry>[to do]</entry>
  1031. </row>
  1032. <row>
  1033. <entry><constant>V4L2_MEMORY_DMABUF</constant></entry>
  1034. <entry>4</entry>
  1035. <entry>The buffer is used for <link linkend="dmabuf">DMA shared
  1036. buffer</link> I/O.</entry>
  1037. </row>
  1038. </tbody>
  1039. </tgroup>
  1040. </table>
  1041. <section>
  1042. <title>Timecodes</title>
  1043. <para>The <structname>v4l2_timecode</structname> structure is
  1044. designed to hold a <xref linkend="smpte12m" /> or similar timecode.
  1045. (struct <structname>timeval</structname> timestamps are stored in
  1046. &v4l2-buffer; field <structfield>timestamp</structfield>.)</para>
  1047. <table frame="none" pgwide="1" id="v4l2-timecode">
  1048. <title>struct <structname>v4l2_timecode</structname></title>
  1049. <tgroup cols="3">
  1050. &cs-str;
  1051. <tbody valign="top">
  1052. <row>
  1053. <entry>__u32</entry>
  1054. <entry><structfield>type</structfield></entry>
  1055. <entry>Frame rate the timecodes are based on, see <xref
  1056. linkend="timecode-type" />.</entry>
  1057. </row>
  1058. <row>
  1059. <entry>__u32</entry>
  1060. <entry><structfield>flags</structfield></entry>
  1061. <entry>Timecode flags, see <xref linkend="timecode-flags" />.</entry>
  1062. </row>
  1063. <row>
  1064. <entry>__u8</entry>
  1065. <entry><structfield>frames</structfield></entry>
  1066. <entry>Frame count, 0 ... 23/24/29/49/59, depending on the
  1067. type of timecode.</entry>
  1068. </row>
  1069. <row>
  1070. <entry>__u8</entry>
  1071. <entry><structfield>seconds</structfield></entry>
  1072. <entry>Seconds count, 0 ... 59. This is a binary, not BCD number.</entry>
  1073. </row>
  1074. <row>
  1075. <entry>__u8</entry>
  1076. <entry><structfield>minutes</structfield></entry>
  1077. <entry>Minutes count, 0 ... 59. This is a binary, not BCD number.</entry>
  1078. </row>
  1079. <row>
  1080. <entry>__u8</entry>
  1081. <entry><structfield>hours</structfield></entry>
  1082. <entry>Hours count, 0 ... 29. This is a binary, not BCD number.</entry>
  1083. </row>
  1084. <row>
  1085. <entry>__u8</entry>
  1086. <entry><structfield>userbits</structfield>[4]</entry>
  1087. <entry>The "user group" bits from the timecode.</entry>
  1088. </row>
  1089. </tbody>
  1090. </tgroup>
  1091. </table>
  1092. <table frame="none" pgwide="1" id="timecode-type">
  1093. <title>Timecode Types</title>
  1094. <tgroup cols="3">
  1095. &cs-def;
  1096. <tbody valign="top">
  1097. <row>
  1098. <entry><constant>V4L2_TC_TYPE_24FPS</constant></entry>
  1099. <entry>1</entry>
  1100. <entry>24 frames per second, i.&nbsp;e. film.</entry>
  1101. </row>
  1102. <row>
  1103. <entry><constant>V4L2_TC_TYPE_25FPS</constant></entry>
  1104. <entry>2</entry>
  1105. <entry>25 frames per second, &ie; PAL or SECAM video.</entry>
  1106. </row>
  1107. <row>
  1108. <entry><constant>V4L2_TC_TYPE_30FPS</constant></entry>
  1109. <entry>3</entry>
  1110. <entry>30 frames per second, &ie; NTSC video.</entry>
  1111. </row>
  1112. <row>
  1113. <entry><constant>V4L2_TC_TYPE_50FPS</constant></entry>
  1114. <entry>4</entry>
  1115. <entry></entry>
  1116. </row>
  1117. <row>
  1118. <entry><constant>V4L2_TC_TYPE_60FPS</constant></entry>
  1119. <entry>5</entry>
  1120. <entry></entry>
  1121. </row>
  1122. </tbody>
  1123. </tgroup>
  1124. </table>
  1125. <table frame="none" pgwide="1" id="timecode-flags">
  1126. <title>Timecode Flags</title>
  1127. <tgroup cols="3">
  1128. &cs-def;
  1129. <tbody valign="top">
  1130. <row>
  1131. <entry><constant>V4L2_TC_FLAG_DROPFRAME</constant></entry>
  1132. <entry>0x0001</entry>
  1133. <entry>Indicates "drop frame" semantics for counting frames
  1134. in 29.97 fps material. When set, frame numbers 0 and 1 at the start of
  1135. each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the
  1136. count.</entry>
  1137. </row>
  1138. <row>
  1139. <entry><constant>V4L2_TC_FLAG_COLORFRAME</constant></entry>
  1140. <entry>0x0002</entry>
  1141. <entry>The "color frame" flag.</entry>
  1142. </row>
  1143. <row>
  1144. <entry><constant>V4L2_TC_USERBITS_field</constant></entry>
  1145. <entry>0x000C</entry>
  1146. <entry>Field mask for the "binary group flags".</entry>
  1147. </row>
  1148. <row>
  1149. <entry><constant>V4L2_TC_USERBITS_USERDEFINED</constant></entry>
  1150. <entry>0x0000</entry>
  1151. <entry>Unspecified format.</entry>
  1152. </row>
  1153. <row>
  1154. <entry><constant>V4L2_TC_USERBITS_8BITCHARS</constant></entry>
  1155. <entry>0x0008</entry>
  1156. <entry>8-bit ISO characters.</entry>
  1157. </row>
  1158. </tbody>
  1159. </tgroup>
  1160. </table>
  1161. </section>
  1162. </section>
  1163. <section id="field-order">
  1164. <title>Field Order</title>
  1165. <para>We have to distinguish between progressive and interlaced
  1166. video. Progressive video transmits all lines of a video image
  1167. sequentially. Interlaced video divides an image into two fields,
  1168. containing only the odd and even lines of the image, respectively.
  1169. Alternating the so called odd and even field are transmitted, and due
  1170. to a small delay between fields a cathode ray TV displays the lines
  1171. interleaved, yielding the original frame. This curious technique was
  1172. invented because at refresh rates similar to film the image would
  1173. fade out too quickly. Transmitting fields reduces the flicker without
  1174. the necessity of doubling the frame rate and with it the bandwidth
  1175. required for each channel.</para>
  1176. <para>It is important to understand a video camera does not expose
  1177. one frame at a time, merely transmitting the frames separated into
  1178. fields. The fields are in fact captured at two different instances in
  1179. time. An object on screen may well move between one field and the
  1180. next. For applications analysing motion it is of paramount importance
  1181. to recognize which field of a frame is older, the <emphasis>temporal
  1182. order</emphasis>.</para>
  1183. <para>When the driver provides or accepts images field by field
  1184. rather than interleaved, it is also important applications understand
  1185. how the fields combine to frames. We distinguish between top (aka odd) and
  1186. bottom (aka even) fields, the <emphasis>spatial order</emphasis>: The first line
  1187. of the top field is the first line of an interlaced frame, the first
  1188. line of the bottom field is the second line of that frame.</para>
  1189. <para>However because fields were captured one after the other,
  1190. arguing whether a frame commences with the top or bottom field is
  1191. pointless. Any two successive top and bottom, or bottom and top fields
  1192. yield a valid frame. Only when the source was progressive to begin
  1193. with, &eg; when transferring film to video, two fields may come from
  1194. the same frame, creating a natural order.</para>
  1195. <para>Counter to intuition the top field is not necessarily the
  1196. older field. Whether the older field contains the top or bottom lines
  1197. is a convention determined by the video standard. Hence the
  1198. distinction between temporal and spatial order of fields. The diagrams
  1199. below should make this clearer.</para>
  1200. <para>All video capture and output devices must report the current
  1201. field order. Some drivers may permit the selection of a different
  1202. order, to this end applications initialize the
  1203. <structfield>field</structfield> field of &v4l2-pix-format; before
  1204. calling the &VIDIOC-S-FMT; ioctl. If this is not desired it should
  1205. have the value <constant>V4L2_FIELD_ANY</constant> (0).</para>
  1206. <table frame="none" pgwide="1" id="v4l2-field">
  1207. <title>enum v4l2_field</title>
  1208. <tgroup cols="3">
  1209. &cs-def;
  1210. <tbody valign="top">
  1211. <row>
  1212. <entry><constant>V4L2_FIELD_ANY</constant></entry>
  1213. <entry>0</entry>
  1214. <entry>Applications request this field order when any
  1215. one of the <constant>V4L2_FIELD_NONE</constant>,
  1216. <constant>V4L2_FIELD_TOP</constant>,
  1217. <constant>V4L2_FIELD_BOTTOM</constant>, or
  1218. <constant>V4L2_FIELD_INTERLACED</constant> formats is acceptable.
  1219. Drivers choose depending on hardware capabilities or e.&nbsp;g. the
  1220. requested image size, and return the actual field order. &v4l2-buffer;
  1221. <structfield>field</structfield> can never be
  1222. <constant>V4L2_FIELD_ANY</constant>.</entry>
  1223. </row>
  1224. <row>
  1225. <entry><constant>V4L2_FIELD_NONE</constant></entry>
  1226. <entry>1</entry>
  1227. <entry>Images are in progressive format, not interlaced.
  1228. The driver may also indicate this order when it cannot distinguish
  1229. between <constant>V4L2_FIELD_TOP</constant> and
  1230. <constant>V4L2_FIELD_BOTTOM</constant>.</entry>
  1231. </row>
  1232. <row>
  1233. <entry><constant>V4L2_FIELD_TOP</constant></entry>
  1234. <entry>2</entry>
  1235. <entry>Images consist of the top (aka odd) field only.</entry>
  1236. </row>
  1237. <row>
  1238. <entry><constant>V4L2_FIELD_BOTTOM</constant></entry>
  1239. <entry>3</entry>
  1240. <entry>Images consist of the bottom (aka even) field only.
  1241. Applications may wish to prevent a device from capturing interlaced
  1242. images because they will have "comb" or "feathering" artefacts around
  1243. moving objects.</entry>
  1244. </row>
  1245. <row>
  1246. <entry><constant>V4L2_FIELD_INTERLACED</constant></entry>
  1247. <entry>4</entry>
  1248. <entry>Images contain both fields, interleaved line by
  1249. line. The temporal order of the fields (whether the top or bottom
  1250. field is first transmitted) depends on the current video standard.
  1251. M/NTSC transmits the bottom field first, all other standards the top
  1252. field first.</entry>
  1253. </row>
  1254. <row>
  1255. <entry><constant>V4L2_FIELD_SEQ_TB</constant></entry>
  1256. <entry>5</entry>
  1257. <entry>Images contain both fields, the top field lines
  1258. are stored first in memory, immediately followed by the bottom field
  1259. lines. Fields are always stored in temporal order, the older one first
  1260. in memory. Image sizes refer to the frame, not fields.</entry>
  1261. </row>
  1262. <row>
  1263. <entry><constant>V4L2_FIELD_SEQ_BT</constant></entry>
  1264. <entry>6</entry>
  1265. <entry>Images contain both fields, the bottom field
  1266. lines are stored first in memory, immediately followed by the top
  1267. field lines. Fields are always stored in temporal order, the older one
  1268. first in memory. Image sizes refer to the frame, not fields.</entry>
  1269. </row>
  1270. <row>
  1271. <entry><constant>V4L2_FIELD_ALTERNATE</constant></entry>
  1272. <entry>7</entry>
  1273. <entry>The two fields of a frame are passed in separate
  1274. buffers, in temporal order, &ie; the older one first. To indicate the field
  1275. parity (whether the current field is a top or bottom field) the driver
  1276. or application, depending on data direction, must set &v4l2-buffer;
  1277. <structfield>field</structfield> to
  1278. <constant>V4L2_FIELD_TOP</constant> or
  1279. <constant>V4L2_FIELD_BOTTOM</constant>. Any two successive fields pair
  1280. to build a frame. If fields are successive, without any dropped fields
  1281. between them (fields can drop individually), can be determined from
  1282. the &v4l2-buffer; <structfield>sequence</structfield> field. Image
  1283. sizes refer to the frame, not fields. This format cannot be selected
  1284. when using the read/write I/O method.<!-- Where it's indistinguishable
  1285. from V4L2_FIELD_SEQ_*. --></entry>
  1286. </row>
  1287. <row>
  1288. <entry><constant>V4L2_FIELD_INTERLACED_TB</constant></entry>
  1289. <entry>8</entry>
  1290. <entry>Images contain both fields, interleaved line by
  1291. line, top field first. The top field is transmitted first.</entry>
  1292. </row>
  1293. <row>
  1294. <entry><constant>V4L2_FIELD_INTERLACED_BT</constant></entry>
  1295. <entry>9</entry>
  1296. <entry>Images contain both fields, interleaved line by
  1297. line, top field first. The bottom field is transmitted first.</entry>
  1298. </row>
  1299. </tbody>
  1300. </tgroup>
  1301. </table>
  1302. <figure id="fieldseq-tb">
  1303. <title>Field Order, Top Field First Transmitted</title>
  1304. <mediaobject>
  1305. <imageobject>
  1306. <imagedata fileref="fieldseq_tb.pdf" format="PS" />
  1307. </imageobject>
  1308. <imageobject>
  1309. <imagedata fileref="fieldseq_tb.gif" format="GIF" />
  1310. </imageobject>
  1311. </mediaobject>
  1312. </figure>
  1313. <figure id="fieldseq-bt">
  1314. <title>Field Order, Bottom Field First Transmitted</title>
  1315. <mediaobject>
  1316. <imageobject>
  1317. <imagedata fileref="fieldseq_bt.pdf" format="PS" />
  1318. </imageobject>
  1319. <imageobject>
  1320. <imagedata fileref="fieldseq_bt.gif" format="GIF" />
  1321. </imageobject>
  1322. </mediaobject>
  1323. </figure>
  1324. </section>