io.xml 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 linkend="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 time when the first data
  638. byte was captured, as returned by the
  639. <function>clock_gettime()</function> function for the relevant
  640. clock id; see <constant>V4L2_BUF_FLAG_TIMESTAMP_*</constant> in
  641. <xref linkend="buffer-flags" />. For output streams the data
  642. will not be displayed before this time, secondary to the nominal
  643. frame rate determined by the current video standard in enqueued
  644. order. Applications can for example zero this field to display
  645. frames as soon as possible. The driver stores the time at which
  646. the first data byte was actually sent out in the
  647. <structfield>timestamp</structfield> field. This permits
  648. applications to monitor the drift between the video and system
  649. clock.</para></entry>
  650. </row>
  651. <row>
  652. <entry>&v4l2-timecode;</entry>
  653. <entry><structfield>timecode</structfield></entry>
  654. <entry></entry>
  655. <entry>When <structfield>type</structfield> is
  656. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> and the
  657. <constant>V4L2_BUF_FLAG_TIMECODE</constant> flag is set in
  658. <structfield>flags</structfield>, this structure contains a frame
  659. timecode. In <link linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link>
  660. mode the top and bottom field contain the same timecode.
  661. Timecodes are intended to help video editing and are typically recorded on
  662. video tapes, but also embedded in compressed formats like MPEG. This
  663. field is independent of the <structfield>timestamp</structfield> and
  664. <structfield>sequence</structfield> fields.</entry>
  665. </row>
  666. <row>
  667. <entry>__u32</entry>
  668. <entry><structfield>sequence</structfield></entry>
  669. <entry></entry>
  670. <entry>Set by the driver, counting the frames (not fields!) in
  671. sequence. This field is set for both input and output devices.</entry>
  672. </row>
  673. <row>
  674. <entry spanname="hspan"><para>In <link
  675. linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link> mode the top and
  676. bottom field have the same sequence number. The count starts at zero
  677. and includes dropped or repeated frames. A dropped frame was received
  678. by an input device but could not be stored due to lack of free buffer
  679. space. A repeated frame was displayed again by an output device
  680. because the application did not pass new data in
  681. time.</para><para>Note this may count the frames received
  682. e.g. over USB, without taking into account the frames dropped by the
  683. remote hardware due to limited compression throughput or bus
  684. bandwidth. These devices identify by not enumerating any video
  685. standards, see <xref linkend="standard" />.</para></entry>
  686. </row>
  687. <row>
  688. <entry>__u32</entry>
  689. <entry><structfield>memory</structfield></entry>
  690. <entry></entry>
  691. <entry>This field must be set by applications and/or drivers
  692. in accordance with the selected I/O method. See <xref linkend="v4l2-memory"
  693. /></entry>
  694. </row>
  695. <row>
  696. <entry>union</entry>
  697. <entry><structfield>m</structfield></entry>
  698. </row>
  699. <row>
  700. <entry></entry>
  701. <entry>__u32</entry>
  702. <entry><structfield>offset</structfield></entry>
  703. <entry>For the single-planar API and when
  704. <structfield>memory</structfield> is <constant>V4L2_MEMORY_MMAP</constant> this
  705. is the offset of the buffer from the start of the device memory. The value is
  706. returned by the driver and apart of serving as parameter to the &func-mmap;
  707. function not useful for applications. See <xref linkend="mmap" /> for details
  708. </entry>
  709. </row>
  710. <row>
  711. <entry></entry>
  712. <entry>unsigned long</entry>
  713. <entry><structfield>userptr</structfield></entry>
  714. <entry>For the single-planar API and when
  715. <structfield>memory</structfield> is <constant>V4L2_MEMORY_USERPTR</constant>
  716. this is a pointer to the buffer (casted to unsigned long type) in virtual
  717. memory, set by the application. See <xref linkend="userp" /> for details.
  718. </entry>
  719. </row>
  720. <row>
  721. <entry></entry>
  722. <entry>struct v4l2_plane</entry>
  723. <entry><structfield>*planes</structfield></entry>
  724. <entry>When using the multi-planar API, contains a userspace pointer
  725. to an array of &v4l2-plane;. The size of the array should be put
  726. in the <structfield>length</structfield> field of this
  727. <structname>v4l2_buffer</structname> structure.</entry>
  728. </row>
  729. <row>
  730. <entry></entry>
  731. <entry>int</entry>
  732. <entry><structfield>fd</structfield></entry>
  733. <entry>For the single-plane API and when
  734. <structfield>memory</structfield> is <constant>V4L2_MEMORY_DMABUF</constant> this
  735. is the file descriptor associated with a DMABUF buffer.</entry>
  736. </row>
  737. <row>
  738. <entry>__u32</entry>
  739. <entry><structfield>length</structfield></entry>
  740. <entry></entry>
  741. <entry>Size of the buffer (not the payload) in bytes for the
  742. single-planar API. For the multi-planar API the application sets
  743. this to the number of elements in the <structfield>planes</structfield>
  744. array. The driver will fill in the actual number of valid elements in
  745. that array.
  746. </entry>
  747. </row>
  748. <row>
  749. <entry>__u32</entry>
  750. <entry><structfield>reserved2</structfield></entry>
  751. <entry></entry>
  752. <entry>A place holder for future extensions. Applications
  753. should set this to 0.</entry>
  754. </row>
  755. <row>
  756. <entry>__u32</entry>
  757. <entry><structfield>reserved</structfield></entry>
  758. <entry></entry>
  759. <entry>A place holder for future extensions. Applications
  760. should set this to 0.</entry>
  761. </row>
  762. </tbody>
  763. </tgroup>
  764. </table>
  765. <table frame="none" pgwide="1" id="v4l2-plane">
  766. <title>struct <structname>v4l2_plane</structname></title>
  767. <tgroup cols="4">
  768. &cs-ustr;
  769. <tbody valign="top">
  770. <row>
  771. <entry>__u32</entry>
  772. <entry><structfield>bytesused</structfield></entry>
  773. <entry></entry>
  774. <entry>The number of bytes occupied by data in the plane
  775. (its payload).</entry>
  776. </row>
  777. <row>
  778. <entry>__u32</entry>
  779. <entry><structfield>length</structfield></entry>
  780. <entry></entry>
  781. <entry>Size in bytes of the plane (not its payload).</entry>
  782. </row>
  783. <row>
  784. <entry>union</entry>
  785. <entry><structfield>m</structfield></entry>
  786. <entry></entry>
  787. <entry></entry>
  788. </row>
  789. <row>
  790. <entry></entry>
  791. <entry>__u32</entry>
  792. <entry><structfield>mem_offset</structfield></entry>
  793. <entry>When the memory type in the containing &v4l2-buffer; is
  794. <constant>V4L2_MEMORY_MMAP</constant>, this is the value that
  795. should be passed to &func-mmap;, similar to the
  796. <structfield>offset</structfield> field in &v4l2-buffer;.</entry>
  797. </row>
  798. <row>
  799. <entry></entry>
  800. <entry>unsigned long</entry>
  801. <entry><structfield>userptr</structfield></entry>
  802. <entry>When the memory type in the containing &v4l2-buffer; is
  803. <constant>V4L2_MEMORY_USERPTR</constant>, this is a userspace
  804. pointer to the memory allocated for this plane by an application.
  805. </entry>
  806. </row>
  807. <row>
  808. <entry></entry>
  809. <entry>int</entry>
  810. <entry><structfield>fd</structfield></entry>
  811. <entry>When the memory type in the containing &v4l2-buffer; is
  812. <constant>V4L2_MEMORY_DMABUF</constant>, this is a file
  813. descriptor associated with a DMABUF buffer, similar to the
  814. <structfield>fd</structfield> field in &v4l2-buffer;.</entry>
  815. </row>
  816. <row>
  817. <entry>__u32</entry>
  818. <entry><structfield>data_offset</structfield></entry>
  819. <entry></entry>
  820. <entry>Offset in bytes to video data in the plane, if applicable.
  821. </entry>
  822. </row>
  823. <row>
  824. <entry>__u32</entry>
  825. <entry><structfield>reserved[11]</structfield></entry>
  826. <entry></entry>
  827. <entry>Reserved for future use. Should be zeroed by an
  828. application.</entry>
  829. </row>
  830. </tbody>
  831. </tgroup>
  832. </table>
  833. <table frame="none" pgwide="1" id="v4l2-buf-type">
  834. <title>enum v4l2_buf_type</title>
  835. <tgroup cols="3">
  836. &cs-def;
  837. <tbody valign="top">
  838. <row>
  839. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant></entry>
  840. <entry>1</entry>
  841. <entry>Buffer of a single-planar video capture stream, see <xref
  842. linkend="capture" />.</entry>
  843. </row>
  844. <row>
  845. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE</constant>
  846. </entry>
  847. <entry>9</entry>
  848. <entry>Buffer of a multi-planar video capture stream, see <xref
  849. linkend="capture" />.</entry>
  850. </row>
  851. <row>
  852. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT</constant></entry>
  853. <entry>2</entry>
  854. <entry>Buffer of a single-planar video output stream, see <xref
  855. linkend="output" />.</entry>
  856. </row>
  857. <row>
  858. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE</constant>
  859. </entry>
  860. <entry>10</entry>
  861. <entry>Buffer of a multi-planar video output stream, see <xref
  862. linkend="output" />.</entry>
  863. </row>
  864. <row>
  865. <entry><constant>V4L2_BUF_TYPE_VIDEO_OVERLAY</constant></entry>
  866. <entry>3</entry>
  867. <entry>Buffer for video overlay, see <xref linkend="overlay" />.</entry>
  868. </row>
  869. <row>
  870. <entry><constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant></entry>
  871. <entry>4</entry>
  872. <entry>Buffer of a raw VBI capture stream, see <xref
  873. linkend="raw-vbi" />.</entry>
  874. </row>
  875. <row>
  876. <entry><constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant></entry>
  877. <entry>5</entry>
  878. <entry>Buffer of a raw VBI output stream, see <xref
  879. linkend="raw-vbi" />.</entry>
  880. </row>
  881. <row>
  882. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_CAPTURE</constant></entry>
  883. <entry>6</entry>
  884. <entry>Buffer of a sliced VBI capture stream, see <xref
  885. linkend="sliced" />.</entry>
  886. </row>
  887. <row>
  888. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_OUTPUT</constant></entry>
  889. <entry>7</entry>
  890. <entry>Buffer of a sliced VBI output stream, see <xref
  891. linkend="sliced" />.</entry>
  892. </row>
  893. <row>
  894. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY</constant></entry>
  895. <entry>8</entry>
  896. <entry>Buffer for video output overlay (OSD), see <xref
  897. linkend="osd" />.</entry>
  898. </row>
  899. </tbody>
  900. </tgroup>
  901. </table>
  902. <table frame="none" pgwide="1" id="buffer-flags">
  903. <title>Buffer Flags</title>
  904. <tgroup cols="3">
  905. &cs-def;
  906. <tbody valign="top">
  907. <row>
  908. <entry><constant>V4L2_BUF_FLAG_MAPPED</constant></entry>
  909. <entry>0x0001</entry>
  910. <entry>The buffer resides in device memory and has been mapped
  911. into the application's address space, see <xref linkend="mmap" /> for details.
  912. Drivers set or clear this flag when the
  913. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  914. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  915. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called. Set by the driver.</entry>
  916. </row>
  917. <row>
  918. <entry><constant>V4L2_BUF_FLAG_QUEUED</constant></entry>
  919. <entry>0x0002</entry>
  920. <entry>Internally drivers maintain two buffer queues, an
  921. incoming and outgoing queue. When this flag is set, the buffer is
  922. currently on the incoming queue. It automatically moves to the
  923. outgoing queue after the buffer has been filled (capture devices) or
  924. displayed (output devices). Drivers set or clear this flag when the
  925. <constant>VIDIOC_QUERYBUF</constant> ioctl is called. After
  926. (successful) calling the <constant>VIDIOC_QBUF </constant>ioctl it is
  927. always set and after <constant>VIDIOC_DQBUF</constant> always
  928. cleared.</entry>
  929. </row>
  930. <row>
  931. <entry><constant>V4L2_BUF_FLAG_DONE</constant></entry>
  932. <entry>0x0004</entry>
  933. <entry>When this flag is set, the buffer is currently on
  934. the outgoing queue, ready to be dequeued from the driver. Drivers set
  935. or clear this flag when the <constant>VIDIOC_QUERYBUF</constant> ioctl
  936. is called. After calling the <constant>VIDIOC_QBUF</constant> or
  937. <constant>VIDIOC_DQBUF</constant> it is always cleared. Of course a
  938. buffer cannot be on both queues at the same time, the
  939. <constant>V4L2_BUF_FLAG_QUEUED</constant> and
  940. <constant>V4L2_BUF_FLAG_DONE</constant> flag are mutually exclusive.
  941. They can be both cleared however, then the buffer is in "dequeued"
  942. state, in the application domain to say so.</entry>
  943. </row>
  944. <row>
  945. <entry><constant>V4L2_BUF_FLAG_ERROR</constant></entry>
  946. <entry>0x0040</entry>
  947. <entry>When this flag is set, the buffer has been dequeued
  948. successfully, although the data might have been corrupted.
  949. This is recoverable, streaming may continue as normal and
  950. the buffer may be reused normally.
  951. Drivers set this flag when the <constant>VIDIOC_DQBUF</constant>
  952. ioctl is called.</entry>
  953. </row>
  954. <row>
  955. <entry><constant>V4L2_BUF_FLAG_KEYFRAME</constant></entry>
  956. <entry>0x0008</entry>
  957. <entry>Drivers set or clear this flag when calling the
  958. <constant>VIDIOC_DQBUF</constant> ioctl. It may be set by video
  959. capture devices when the buffer contains a compressed image which is a
  960. key frame (or field), &ie; can be decompressed on its own.</entry>
  961. </row>
  962. <row>
  963. <entry><constant>V4L2_BUF_FLAG_PFRAME</constant></entry>
  964. <entry>0x0010</entry>
  965. <entry>Similar to <constant>V4L2_BUF_FLAG_KEYFRAME</constant>
  966. this flags predicted frames or fields which contain only differences to a
  967. previous key frame.</entry>
  968. </row>
  969. <row>
  970. <entry><constant>V4L2_BUF_FLAG_BFRAME</constant></entry>
  971. <entry>0x0020</entry>
  972. <entry>Similar to <constant>V4L2_BUF_FLAG_PFRAME</constant>
  973. this is a bidirectional predicted frame or field. [ooc tbd]</entry>
  974. </row>
  975. <row>
  976. <entry><constant>V4L2_BUF_FLAG_TIMECODE</constant></entry>
  977. <entry>0x0100</entry>
  978. <entry>The <structfield>timecode</structfield> field is valid.
  979. Drivers set or clear this flag when the <constant>VIDIOC_DQBUF</constant>
  980. ioctl is called.</entry>
  981. </row>
  982. <row>
  983. <entry><constant>V4L2_BUF_FLAG_PREPARED</constant></entry>
  984. <entry>0x0400</entry>
  985. <entry>The buffer has been prepared for I/O and can be queued by the
  986. application. Drivers set or clear this flag when the
  987. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  988. linkend="vidioc-qbuf">VIDIOC_PREPARE_BUF</link>, <link
  989. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  990. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called.</entry>
  991. </row>
  992. <row>
  993. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_INVALIDATE</constant></entry>
  994. <entry>0x0800</entry>
  995. <entry>Caches do not have to be invalidated for this buffer.
  996. Typically applications shall use this flag if the data captured in the buffer
  997. is not going to be touched by the CPU, instead the buffer will, probably, be
  998. passed on to a DMA-capable hardware unit for further processing or output.
  999. </entry>
  1000. </row>
  1001. <row>
  1002. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_CLEAN</constant></entry>
  1003. <entry>0x1000</entry>
  1004. <entry>Caches do not have to be cleaned for this buffer.
  1005. Typically applications shall use this flag for output buffers if the data
  1006. in this buffer has not been created by the CPU but by some DMA-capable unit,
  1007. in which case caches have not been used.</entry>
  1008. </row>
  1009. <row>
  1010. <entry><constant>V4L2_BUF_FLAG_TIMESTAMP_MASK</constant></entry>
  1011. <entry>0xe000</entry>
  1012. <entry>Mask for timestamp types below. To test the
  1013. timestamp type, mask out bits not belonging to timestamp
  1014. type by performing a logical and operation with buffer
  1015. flags and timestamp mask.</entry>
  1016. </row>
  1017. <row>
  1018. <entry><constant>V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN</constant></entry>
  1019. <entry>0x0000</entry>
  1020. <entry>Unknown timestamp type. This type is used by
  1021. drivers before Linux 3.9 and may be either monotonic (see
  1022. below) or realtime (wall clock). Monotonic clock has been
  1023. favoured in embedded systems whereas most of the drivers
  1024. use the realtime clock. Either kinds of timestamps are
  1025. available in user space via
  1026. <function>clock_gettime(2)</function> using clock IDs
  1027. <constant>CLOCK_MONOTONIC</constant> and
  1028. <constant>CLOCK_REALTIME</constant>, respectively.</entry>
  1029. </row>
  1030. <row>
  1031. <entry><constant>V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC</constant></entry>
  1032. <entry>0x2000</entry>
  1033. <entry>The buffer timestamp has been taken from the
  1034. <constant>CLOCK_MONOTONIC</constant> clock. To access the
  1035. same clock outside V4L2, use
  1036. <function>clock_gettime(2)</function> .</entry>
  1037. </row>
  1038. </tbody>
  1039. </tgroup>
  1040. </table>
  1041. <table pgwide="1" frame="none" id="v4l2-memory">
  1042. <title>enum v4l2_memory</title>
  1043. <tgroup cols="3">
  1044. &cs-def;
  1045. <tbody valign="top">
  1046. <row>
  1047. <entry><constant>V4L2_MEMORY_MMAP</constant></entry>
  1048. <entry>1</entry>
  1049. <entry>The buffer is used for <link linkend="mmap">memory
  1050. mapping</link> I/O.</entry>
  1051. </row>
  1052. <row>
  1053. <entry><constant>V4L2_MEMORY_USERPTR</constant></entry>
  1054. <entry>2</entry>
  1055. <entry>The buffer is used for <link linkend="userp">user
  1056. pointer</link> I/O.</entry>
  1057. </row>
  1058. <row>
  1059. <entry><constant>V4L2_MEMORY_OVERLAY</constant></entry>
  1060. <entry>3</entry>
  1061. <entry>[to do]</entry>
  1062. </row>
  1063. <row>
  1064. <entry><constant>V4L2_MEMORY_DMABUF</constant></entry>
  1065. <entry>4</entry>
  1066. <entry>The buffer is used for <link linkend="dmabuf">DMA shared
  1067. buffer</link> I/O.</entry>
  1068. </row>
  1069. </tbody>
  1070. </tgroup>
  1071. </table>
  1072. <section>
  1073. <title>Timecodes</title>
  1074. <para>The <structname>v4l2_timecode</structname> structure is
  1075. designed to hold a <xref linkend="smpte12m" /> or similar timecode.
  1076. (struct <structname>timeval</structname> timestamps are stored in
  1077. &v4l2-buffer; field <structfield>timestamp</structfield>.)</para>
  1078. <table frame="none" pgwide="1" id="v4l2-timecode">
  1079. <title>struct <structname>v4l2_timecode</structname></title>
  1080. <tgroup cols="3">
  1081. &cs-str;
  1082. <tbody valign="top">
  1083. <row>
  1084. <entry>__u32</entry>
  1085. <entry><structfield>type</structfield></entry>
  1086. <entry>Frame rate the timecodes are based on, see <xref
  1087. linkend="timecode-type" />.</entry>
  1088. </row>
  1089. <row>
  1090. <entry>__u32</entry>
  1091. <entry><structfield>flags</structfield></entry>
  1092. <entry>Timecode flags, see <xref linkend="timecode-flags" />.</entry>
  1093. </row>
  1094. <row>
  1095. <entry>__u8</entry>
  1096. <entry><structfield>frames</structfield></entry>
  1097. <entry>Frame count, 0 ... 23/24/29/49/59, depending on the
  1098. type of timecode.</entry>
  1099. </row>
  1100. <row>
  1101. <entry>__u8</entry>
  1102. <entry><structfield>seconds</structfield></entry>
  1103. <entry>Seconds count, 0 ... 59. This is a binary, not BCD number.</entry>
  1104. </row>
  1105. <row>
  1106. <entry>__u8</entry>
  1107. <entry><structfield>minutes</structfield></entry>
  1108. <entry>Minutes count, 0 ... 59. This is a binary, not BCD number.</entry>
  1109. </row>
  1110. <row>
  1111. <entry>__u8</entry>
  1112. <entry><structfield>hours</structfield></entry>
  1113. <entry>Hours count, 0 ... 29. This is a binary, not BCD number.</entry>
  1114. </row>
  1115. <row>
  1116. <entry>__u8</entry>
  1117. <entry><structfield>userbits</structfield>[4]</entry>
  1118. <entry>The "user group" bits from the timecode.</entry>
  1119. </row>
  1120. </tbody>
  1121. </tgroup>
  1122. </table>
  1123. <table frame="none" pgwide="1" id="timecode-type">
  1124. <title>Timecode Types</title>
  1125. <tgroup cols="3">
  1126. &cs-def;
  1127. <tbody valign="top">
  1128. <row>
  1129. <entry><constant>V4L2_TC_TYPE_24FPS</constant></entry>
  1130. <entry>1</entry>
  1131. <entry>24 frames per second, i.&nbsp;e. film.</entry>
  1132. </row>
  1133. <row>
  1134. <entry><constant>V4L2_TC_TYPE_25FPS</constant></entry>
  1135. <entry>2</entry>
  1136. <entry>25 frames per second, &ie; PAL or SECAM video.</entry>
  1137. </row>
  1138. <row>
  1139. <entry><constant>V4L2_TC_TYPE_30FPS</constant></entry>
  1140. <entry>3</entry>
  1141. <entry>30 frames per second, &ie; NTSC video.</entry>
  1142. </row>
  1143. <row>
  1144. <entry><constant>V4L2_TC_TYPE_50FPS</constant></entry>
  1145. <entry>4</entry>
  1146. <entry></entry>
  1147. </row>
  1148. <row>
  1149. <entry><constant>V4L2_TC_TYPE_60FPS</constant></entry>
  1150. <entry>5</entry>
  1151. <entry></entry>
  1152. </row>
  1153. </tbody>
  1154. </tgroup>
  1155. </table>
  1156. <table frame="none" pgwide="1" id="timecode-flags">
  1157. <title>Timecode Flags</title>
  1158. <tgroup cols="3">
  1159. &cs-def;
  1160. <tbody valign="top">
  1161. <row>
  1162. <entry><constant>V4L2_TC_FLAG_DROPFRAME</constant></entry>
  1163. <entry>0x0001</entry>
  1164. <entry>Indicates "drop frame" semantics for counting frames
  1165. in 29.97 fps material. When set, frame numbers 0 and 1 at the start of
  1166. each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the
  1167. count.</entry>
  1168. </row>
  1169. <row>
  1170. <entry><constant>V4L2_TC_FLAG_COLORFRAME</constant></entry>
  1171. <entry>0x0002</entry>
  1172. <entry>The "color frame" flag.</entry>
  1173. </row>
  1174. <row>
  1175. <entry><constant>V4L2_TC_USERBITS_field</constant></entry>
  1176. <entry>0x000C</entry>
  1177. <entry>Field mask for the "binary group flags".</entry>
  1178. </row>
  1179. <row>
  1180. <entry><constant>V4L2_TC_USERBITS_USERDEFINED</constant></entry>
  1181. <entry>0x0000</entry>
  1182. <entry>Unspecified format.</entry>
  1183. </row>
  1184. <row>
  1185. <entry><constant>V4L2_TC_USERBITS_8BITCHARS</constant></entry>
  1186. <entry>0x0008</entry>
  1187. <entry>8-bit ISO characters.</entry>
  1188. </row>
  1189. </tbody>
  1190. </tgroup>
  1191. </table>
  1192. </section>
  1193. </section>
  1194. <section id="field-order">
  1195. <title>Field Order</title>
  1196. <para>We have to distinguish between progressive and interlaced
  1197. video. Progressive video transmits all lines of a video image
  1198. sequentially. Interlaced video divides an image into two fields,
  1199. containing only the odd and even lines of the image, respectively.
  1200. Alternating the so called odd and even field are transmitted, and due
  1201. to a small delay between fields a cathode ray TV displays the lines
  1202. interleaved, yielding the original frame. This curious technique was
  1203. invented because at refresh rates similar to film the image would
  1204. fade out too quickly. Transmitting fields reduces the flicker without
  1205. the necessity of doubling the frame rate and with it the bandwidth
  1206. required for each channel.</para>
  1207. <para>It is important to understand a video camera does not expose
  1208. one frame at a time, merely transmitting the frames separated into
  1209. fields. The fields are in fact captured at two different instances in
  1210. time. An object on screen may well move between one field and the
  1211. next. For applications analysing motion it is of paramount importance
  1212. to recognize which field of a frame is older, the <emphasis>temporal
  1213. order</emphasis>.</para>
  1214. <para>When the driver provides or accepts images field by field
  1215. rather than interleaved, it is also important applications understand
  1216. how the fields combine to frames. We distinguish between top (aka odd) and
  1217. bottom (aka even) fields, the <emphasis>spatial order</emphasis>: The first line
  1218. of the top field is the first line of an interlaced frame, the first
  1219. line of the bottom field is the second line of that frame.</para>
  1220. <para>However because fields were captured one after the other,
  1221. arguing whether a frame commences with the top or bottom field is
  1222. pointless. Any two successive top and bottom, or bottom and top fields
  1223. yield a valid frame. Only when the source was progressive to begin
  1224. with, &eg; when transferring film to video, two fields may come from
  1225. the same frame, creating a natural order.</para>
  1226. <para>Counter to intuition the top field is not necessarily the
  1227. older field. Whether the older field contains the top or bottom lines
  1228. is a convention determined by the video standard. Hence the
  1229. distinction between temporal and spatial order of fields. The diagrams
  1230. below should make this clearer.</para>
  1231. <para>All video capture and output devices must report the current
  1232. field order. Some drivers may permit the selection of a different
  1233. order, to this end applications initialize the
  1234. <structfield>field</structfield> field of &v4l2-pix-format; before
  1235. calling the &VIDIOC-S-FMT; ioctl. If this is not desired it should
  1236. have the value <constant>V4L2_FIELD_ANY</constant> (0).</para>
  1237. <table frame="none" pgwide="1" id="v4l2-field">
  1238. <title>enum v4l2_field</title>
  1239. <tgroup cols="3">
  1240. &cs-def;
  1241. <tbody valign="top">
  1242. <row>
  1243. <entry><constant>V4L2_FIELD_ANY</constant></entry>
  1244. <entry>0</entry>
  1245. <entry>Applications request this field order when any
  1246. one of the <constant>V4L2_FIELD_NONE</constant>,
  1247. <constant>V4L2_FIELD_TOP</constant>,
  1248. <constant>V4L2_FIELD_BOTTOM</constant>, or
  1249. <constant>V4L2_FIELD_INTERLACED</constant> formats is acceptable.
  1250. Drivers choose depending on hardware capabilities or e.&nbsp;g. the
  1251. requested image size, and return the actual field order. &v4l2-buffer;
  1252. <structfield>field</structfield> can never be
  1253. <constant>V4L2_FIELD_ANY</constant>.</entry>
  1254. </row>
  1255. <row>
  1256. <entry><constant>V4L2_FIELD_NONE</constant></entry>
  1257. <entry>1</entry>
  1258. <entry>Images are in progressive format, not interlaced.
  1259. The driver may also indicate this order when it cannot distinguish
  1260. between <constant>V4L2_FIELD_TOP</constant> and
  1261. <constant>V4L2_FIELD_BOTTOM</constant>.</entry>
  1262. </row>
  1263. <row>
  1264. <entry><constant>V4L2_FIELD_TOP</constant></entry>
  1265. <entry>2</entry>
  1266. <entry>Images consist of the top (aka odd) field only.</entry>
  1267. </row>
  1268. <row>
  1269. <entry><constant>V4L2_FIELD_BOTTOM</constant></entry>
  1270. <entry>3</entry>
  1271. <entry>Images consist of the bottom (aka even) field only.
  1272. Applications may wish to prevent a device from capturing interlaced
  1273. images because they will have "comb" or "feathering" artefacts around
  1274. moving objects.</entry>
  1275. </row>
  1276. <row>
  1277. <entry><constant>V4L2_FIELD_INTERLACED</constant></entry>
  1278. <entry>4</entry>
  1279. <entry>Images contain both fields, interleaved line by
  1280. line. The temporal order of the fields (whether the top or bottom
  1281. field is first transmitted) depends on the current video standard.
  1282. M/NTSC transmits the bottom field first, all other standards the top
  1283. field first.</entry>
  1284. </row>
  1285. <row>
  1286. <entry><constant>V4L2_FIELD_SEQ_TB</constant></entry>
  1287. <entry>5</entry>
  1288. <entry>Images contain both fields, the top field lines
  1289. are stored first in memory, immediately followed by the bottom field
  1290. lines. Fields are always stored in temporal order, the older one first
  1291. in memory. Image sizes refer to the frame, not fields.</entry>
  1292. </row>
  1293. <row>
  1294. <entry><constant>V4L2_FIELD_SEQ_BT</constant></entry>
  1295. <entry>6</entry>
  1296. <entry>Images contain both fields, the bottom field
  1297. lines are stored first in memory, immediately followed by the top
  1298. field lines. Fields are always stored in temporal order, the older one
  1299. first in memory. Image sizes refer to the frame, not fields.</entry>
  1300. </row>
  1301. <row>
  1302. <entry><constant>V4L2_FIELD_ALTERNATE</constant></entry>
  1303. <entry>7</entry>
  1304. <entry>The two fields of a frame are passed in separate
  1305. buffers, in temporal order, &ie; the older one first. To indicate the field
  1306. parity (whether the current field is a top or bottom field) the driver
  1307. or application, depending on data direction, must set &v4l2-buffer;
  1308. <structfield>field</structfield> to
  1309. <constant>V4L2_FIELD_TOP</constant> or
  1310. <constant>V4L2_FIELD_BOTTOM</constant>. Any two successive fields pair
  1311. to build a frame. If fields are successive, without any dropped fields
  1312. between them (fields can drop individually), can be determined from
  1313. the &v4l2-buffer; <structfield>sequence</structfield> field. Image
  1314. sizes refer to the frame, not fields. This format cannot be selected
  1315. when using the read/write I/O method.<!-- Where it's indistinguishable
  1316. from V4L2_FIELD_SEQ_*. --></entry>
  1317. </row>
  1318. <row>
  1319. <entry><constant>V4L2_FIELD_INTERLACED_TB</constant></entry>
  1320. <entry>8</entry>
  1321. <entry>Images contain both fields, interleaved line by
  1322. line, top field first. The top field is transmitted first.</entry>
  1323. </row>
  1324. <row>
  1325. <entry><constant>V4L2_FIELD_INTERLACED_BT</constant></entry>
  1326. <entry>9</entry>
  1327. <entry>Images contain both fields, interleaved line by
  1328. line, top field first. The bottom field is transmitted first.</entry>
  1329. </row>
  1330. </tbody>
  1331. </tgroup>
  1332. </table>
  1333. <figure id="fieldseq-tb">
  1334. <title>Field Order, Top Field First Transmitted</title>
  1335. <mediaobject>
  1336. <imageobject>
  1337. <imagedata fileref="fieldseq_tb.pdf" format="PS" />
  1338. </imageobject>
  1339. <imageobject>
  1340. <imagedata fileref="fieldseq_tb.gif" format="GIF" />
  1341. </imageobject>
  1342. </mediaobject>
  1343. </figure>
  1344. <figure id="fieldseq-bt">
  1345. <title>Field Order, Bottom Field First Transmitted</title>
  1346. <mediaobject>
  1347. <imageobject>
  1348. <imagedata fileref="fieldseq_bt.pdf" format="PS" />
  1349. </imageobject>
  1350. <imageobject>
  1351. <imagedata fileref="fieldseq_bt.gif" format="GIF" />
  1352. </imageobject>
  1353. </mediaobject>
  1354. </figure>
  1355. </section>