io.xml 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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; function 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="async">
  405. <title>Asynchronous I/O</title>
  406. <para>This method is not defined yet.</para>
  407. </section>
  408. <section id="buffer">
  409. <title>Buffers</title>
  410. <para>A buffer contains data exchanged by application and
  411. driver using one of the Streaming I/O methods. In the multi-planar API, the
  412. data is held in planes, while the buffer structure acts as a container
  413. for the planes. Only pointers to buffers (planes) are exchanged, the data
  414. itself is not copied. These pointers, together with meta-information like
  415. timestamps or field parity, are stored in a struct
  416. <structname>v4l2_buffer</structname>, argument to
  417. the &VIDIOC-QUERYBUF;, &VIDIOC-QBUF; and &VIDIOC-DQBUF; ioctl.
  418. In the multi-planar API, some plane-specific members of struct
  419. <structname>v4l2_buffer</structname>, such as pointers and sizes for each
  420. plane, are stored in struct <structname>v4l2_plane</structname> instead.
  421. In that case, struct <structname>v4l2_buffer</structname> contains an array of
  422. plane structures.</para>
  423. <para>Nominally timestamps refer to the first data byte transmitted.
  424. In practice however the wide range of hardware covered by the V4L2 API
  425. limits timestamp accuracy. Often an interrupt routine will
  426. sample the system clock shortly after the field or frame was stored
  427. completely in memory. So applications must expect a constant
  428. difference up to one field or frame period plus a small (few scan
  429. lines) random error. The delay and error can be much
  430. larger due to compression or transmission over an external bus when
  431. the frames are not properly stamped by the sender. This is frequently
  432. the case with USB cameras. Here timestamps refer to the instant the
  433. field or frame was received by the driver, not the capture time. These
  434. devices identify by not enumerating any video standards, see <xref
  435. linkend="standard" />.</para>
  436. <para>Similar limitations apply to output timestamps. Typically
  437. the video hardware locks to a clock controlling the video timing, the
  438. horizontal and vertical synchronization pulses. At some point in the
  439. line sequence, possibly the vertical blanking, an interrupt routine
  440. samples the system clock, compares against the timestamp and programs
  441. the hardware to repeat the previous field or frame, or to display the
  442. buffer contents.</para>
  443. <para>Apart of limitations of the video device and natural
  444. inaccuracies of all clocks, it should be noted system time itself is
  445. not perfectly stable. It can be affected by power saving cycles,
  446. warped to insert leap seconds, or even turned back or forth by the
  447. system administrator affecting long term measurements. <footnote>
  448. <para>Since no other Linux multimedia
  449. API supports unadjusted time it would be foolish to introduce here. We
  450. must use a universally supported clock to synchronize different media,
  451. hence time of day.</para>
  452. </footnote></para>
  453. <table frame="none" pgwide="1" id="v4l2-buffer">
  454. <title>struct <structname>v4l2_buffer</structname></title>
  455. <tgroup cols="4">
  456. &cs-ustr;
  457. <tbody valign="top">
  458. <row>
  459. <entry>__u32</entry>
  460. <entry><structfield>index</structfield></entry>
  461. <entry></entry>
  462. <entry>Number of the buffer, set by the application. This
  463. field is only used for <link linkend="mmap">memory mapping</link> I/O
  464. and can range from zero to the number of buffers allocated
  465. with the &VIDIOC-REQBUFS; ioctl (&v4l2-requestbuffers; <structfield>count</structfield>) minus one.</entry>
  466. </row>
  467. <row>
  468. <entry>__u32</entry>
  469. <entry><structfield>type</structfield></entry>
  470. <entry></entry>
  471. <entry>Type of the buffer, same as &v4l2-format;
  472. <structfield>type</structfield> or &v4l2-requestbuffers;
  473. <structfield>type</structfield>, set by the application. See <xref
  474. linkend="v4l2-buf-type" /></entry>
  475. </row>
  476. <row>
  477. <entry>__u32</entry>
  478. <entry><structfield>bytesused</structfield></entry>
  479. <entry></entry>
  480. <entry>The number of bytes occupied by the data in the
  481. buffer. It depends on the negotiated data format and may change with
  482. each buffer for compressed variable size data like JPEG images.
  483. Drivers must set this field when <structfield>type</structfield>
  484. refers to an input stream, applications when an output stream.</entry>
  485. </row>
  486. <row>
  487. <entry>__u32</entry>
  488. <entry><structfield>flags</structfield></entry>
  489. <entry></entry>
  490. <entry>Flags set by the application or driver, see <xref
  491. linkend="buffer-flags" />.</entry>
  492. </row>
  493. <row>
  494. <entry>__u32</entry>
  495. <entry><structfield>field</structfield></entry>
  496. <entry></entry>
  497. <entry>Indicates the field order of the image in the
  498. buffer, see <xref linkend="v4l2-field" />. This field is not used when
  499. the buffer contains VBI data. Drivers must set it when
  500. <structfield>type</structfield> refers to an input stream,
  501. applications when an output stream.</entry>
  502. </row>
  503. <row>
  504. <entry>struct timeval</entry>
  505. <entry><structfield>timestamp</structfield></entry>
  506. <entry></entry>
  507. <entry><para>For input streams this is the
  508. system time (as returned by the <function>gettimeofday()</function>
  509. function) when the first data byte was captured. For output streams
  510. the data will not be displayed before this time, secondary to the
  511. nominal frame rate determined by the current video standard in
  512. enqueued order. Applications can for example zero this field to
  513. display frames as soon as possible. The driver stores the time at
  514. which the first data byte was actually sent out in the
  515. <structfield>timestamp</structfield> field. This permits
  516. applications to monitor the drift between the video and system
  517. clock.</para></entry>
  518. </row>
  519. <row>
  520. <entry>&v4l2-timecode;</entry>
  521. <entry><structfield>timecode</structfield></entry>
  522. <entry></entry>
  523. <entry>When <structfield>type</structfield> is
  524. <constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant> and the
  525. <constant>V4L2_BUF_FLAG_TIMECODE</constant> flag is set in
  526. <structfield>flags</structfield>, this structure contains a frame
  527. timecode. In <link linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link>
  528. mode the top and bottom field contain the same timecode.
  529. Timecodes are intended to help video editing and are typically recorded on
  530. video tapes, but also embedded in compressed formats like MPEG. This
  531. field is independent of the <structfield>timestamp</structfield> and
  532. <structfield>sequence</structfield> fields.</entry>
  533. </row>
  534. <row>
  535. <entry>__u32</entry>
  536. <entry><structfield>sequence</structfield></entry>
  537. <entry></entry>
  538. <entry>Set by the driver, counting the frames in the
  539. sequence.</entry>
  540. </row>
  541. <row>
  542. <entry spanname="hspan"><para>In <link
  543. linkend="v4l2-field">V4L2_FIELD_ALTERNATE</link> mode the top and
  544. bottom field have the same sequence number. The count starts at zero
  545. and includes dropped or repeated frames. A dropped frame was received
  546. by an input device but could not be stored due to lack of free buffer
  547. space. A repeated frame was displayed again by an output device
  548. because the application did not pass new data in
  549. time.</para><para>Note this may count the frames received
  550. e.g. over USB, without taking into account the frames dropped by the
  551. remote hardware due to limited compression throughput or bus
  552. bandwidth. These devices identify by not enumerating any video
  553. standards, see <xref linkend="standard" />.</para></entry>
  554. </row>
  555. <row>
  556. <entry>__u32</entry>
  557. <entry><structfield>memory</structfield></entry>
  558. <entry></entry>
  559. <entry>This field must be set by applications and/or drivers
  560. in accordance with the selected I/O method. See <xref linkend="v4l2-memory"
  561. /></entry>
  562. </row>
  563. <row>
  564. <entry>union</entry>
  565. <entry><structfield>m</structfield></entry>
  566. </row>
  567. <row>
  568. <entry></entry>
  569. <entry>__u32</entry>
  570. <entry><structfield>offset</structfield></entry>
  571. <entry>For the single-planar API and when
  572. <structfield>memory</structfield> is <constant>V4L2_MEMORY_MMAP</constant> this
  573. is the offset of the buffer from the start of the device memory. The value is
  574. returned by the driver and apart of serving as parameter to the &func-mmap;
  575. function not useful for applications. See <xref linkend="mmap" /> for details
  576. </entry>
  577. </row>
  578. <row>
  579. <entry></entry>
  580. <entry>unsigned long</entry>
  581. <entry><structfield>userptr</structfield></entry>
  582. <entry>For the single-planar API and when
  583. <structfield>memory</structfield> is <constant>V4L2_MEMORY_USERPTR</constant>
  584. this is a pointer to the buffer (casted to unsigned long type) in virtual
  585. memory, set by the application. See <xref linkend="userp" /> for details.
  586. </entry>
  587. </row>
  588. <row>
  589. <entry></entry>
  590. <entry>struct v4l2_plane</entry>
  591. <entry><structfield>*planes</structfield></entry>
  592. <entry>When using the multi-planar API, contains a userspace pointer
  593. to an array of &v4l2-plane;. The size of the array should be put
  594. in the <structfield>length</structfield> field of this
  595. <structname>v4l2_buffer</structname> structure.</entry>
  596. </row>
  597. <row>
  598. <entry>__u32</entry>
  599. <entry><structfield>length</structfield></entry>
  600. <entry></entry>
  601. <entry>Size of the buffer (not the payload) in bytes for the
  602. single-planar API. For the multi-planar API should contain the
  603. number of elements in the <structfield>planes</structfield> array.
  604. </entry>
  605. </row>
  606. <row>
  607. <entry>__u32</entry>
  608. <entry><structfield>input</structfield></entry>
  609. <entry></entry>
  610. <entry>Some video capture drivers support rapid and
  611. synchronous video input changes, a function useful for example in
  612. video surveillance applications. For this purpose applications set the
  613. <constant>V4L2_BUF_FLAG_INPUT</constant> flag, and this field to the
  614. number of a video input as in &v4l2-input; field
  615. <structfield>index</structfield>.</entry>
  616. </row>
  617. <row>
  618. <entry>__u32</entry>
  619. <entry><structfield>reserved</structfield></entry>
  620. <entry></entry>
  621. <entry>A place holder for future extensions and custom
  622. (driver defined) buffer types
  623. <constant>V4L2_BUF_TYPE_PRIVATE</constant> and higher. Applications
  624. should set this to 0.</entry>
  625. </row>
  626. </tbody>
  627. </tgroup>
  628. </table>
  629. <table frame="none" pgwide="1" id="v4l2-plane">
  630. <title>struct <structname>v4l2_plane</structname></title>
  631. <tgroup cols="4">
  632. &cs-ustr;
  633. <tbody valign="top">
  634. <row>
  635. <entry>__u32</entry>
  636. <entry><structfield>bytesused</structfield></entry>
  637. <entry></entry>
  638. <entry>The number of bytes occupied by data in the plane
  639. (its payload).</entry>
  640. </row>
  641. <row>
  642. <entry>__u32</entry>
  643. <entry><structfield>length</structfield></entry>
  644. <entry></entry>
  645. <entry>Size in bytes of the plane (not its payload).</entry>
  646. </row>
  647. <row>
  648. <entry>union</entry>
  649. <entry><structfield>m</structfield></entry>
  650. <entry></entry>
  651. <entry></entry>
  652. </row>
  653. <row>
  654. <entry></entry>
  655. <entry>__u32</entry>
  656. <entry><structfield>mem_offset</structfield></entry>
  657. <entry>When the memory type in the containing &v4l2-buffer; is
  658. <constant>V4L2_MEMORY_MMAP</constant>, this is the value that
  659. should be passed to &func-mmap;, similar to the
  660. <structfield>offset</structfield> field in &v4l2-buffer;.</entry>
  661. </row>
  662. <row>
  663. <entry></entry>
  664. <entry>__unsigned long</entry>
  665. <entry><structfield>userptr</structfield></entry>
  666. <entry>When the memory type in the containing &v4l2-buffer; is
  667. <constant>V4L2_MEMORY_USERPTR</constant>, this is a userspace
  668. pointer to the memory allocated for this plane by an application.
  669. </entry>
  670. </row>
  671. <row>
  672. <entry>__u32</entry>
  673. <entry><structfield>data_offset</structfield></entry>
  674. <entry></entry>
  675. <entry>Offset in bytes to video data in the plane, if applicable.
  676. </entry>
  677. </row>
  678. <row>
  679. <entry>__u32</entry>
  680. <entry><structfield>reserved[11]</structfield></entry>
  681. <entry></entry>
  682. <entry>Reserved for future use. Should be zeroed by an
  683. application.</entry>
  684. </row>
  685. </tbody>
  686. </tgroup>
  687. </table>
  688. <table frame="none" pgwide="1" id="v4l2-buf-type">
  689. <title>enum v4l2_buf_type</title>
  690. <tgroup cols="3">
  691. &cs-def;
  692. <tbody valign="top">
  693. <row>
  694. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE</constant></entry>
  695. <entry>1</entry>
  696. <entry>Buffer of a single-planar video capture stream, see <xref
  697. linkend="capture" />.</entry>
  698. </row>
  699. <row>
  700. <entry><constant>V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE</constant>
  701. </entry>
  702. <entry>9</entry>
  703. <entry>Buffer of a multi-planar video capture stream, see <xref
  704. linkend="capture" />.</entry>
  705. </row>
  706. <row>
  707. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT</constant></entry>
  708. <entry>2</entry>
  709. <entry>Buffer of a single-planar video output stream, see <xref
  710. linkend="output" />.</entry>
  711. </row>
  712. <row>
  713. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE</constant>
  714. </entry>
  715. <entry>10</entry>
  716. <entry>Buffer of a multi-planar video output stream, see <xref
  717. linkend="output" />.</entry>
  718. </row>
  719. <row>
  720. <entry><constant>V4L2_BUF_TYPE_VIDEO_OVERLAY</constant></entry>
  721. <entry>3</entry>
  722. <entry>Buffer for video overlay, see <xref linkend="overlay" />.</entry>
  723. </row>
  724. <row>
  725. <entry><constant>V4L2_BUF_TYPE_VBI_CAPTURE</constant></entry>
  726. <entry>4</entry>
  727. <entry>Buffer of a raw VBI capture stream, see <xref
  728. linkend="raw-vbi" />.</entry>
  729. </row>
  730. <row>
  731. <entry><constant>V4L2_BUF_TYPE_VBI_OUTPUT</constant></entry>
  732. <entry>5</entry>
  733. <entry>Buffer of a raw VBI output stream, see <xref
  734. linkend="raw-vbi" />.</entry>
  735. </row>
  736. <row>
  737. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_CAPTURE</constant></entry>
  738. <entry>6</entry>
  739. <entry>Buffer of a sliced VBI capture stream, see <xref
  740. linkend="sliced" />.</entry>
  741. </row>
  742. <row>
  743. <entry><constant>V4L2_BUF_TYPE_SLICED_VBI_OUTPUT</constant></entry>
  744. <entry>7</entry>
  745. <entry>Buffer of a sliced VBI output stream, see <xref
  746. linkend="sliced" />.</entry>
  747. </row>
  748. <row>
  749. <entry><constant>V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY</constant></entry>
  750. <entry>8</entry>
  751. <entry>Buffer for video output overlay (OSD), see <xref
  752. linkend="osd" />. Status: <link
  753. linkend="experimental">Experimental</link>.</entry>
  754. </row>
  755. <row>
  756. <entry><constant>V4L2_BUF_TYPE_PRIVATE</constant></entry>
  757. <entry>0x80</entry>
  758. <entry>This and higher values are reserved for custom
  759. (driver defined) buffer types.</entry>
  760. </row>
  761. </tbody>
  762. </tgroup>
  763. </table>
  764. <table frame="none" pgwide="1" id="buffer-flags">
  765. <title>Buffer Flags</title>
  766. <tgroup cols="3">
  767. &cs-def;
  768. <tbody valign="top">
  769. <row>
  770. <entry><constant>V4L2_BUF_FLAG_MAPPED</constant></entry>
  771. <entry>0x0001</entry>
  772. <entry>The buffer resides in device memory and has been mapped
  773. into the application's address space, see <xref linkend="mmap" /> for details.
  774. Drivers set or clear this flag when the
  775. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  776. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  777. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called. Set by the driver.</entry>
  778. </row>
  779. <row>
  780. <entry><constant>V4L2_BUF_FLAG_QUEUED</constant></entry>
  781. <entry>0x0002</entry>
  782. <entry>Internally drivers maintain two buffer queues, an
  783. incoming and outgoing queue. When this flag is set, the buffer is
  784. currently on the incoming queue. It automatically moves to the
  785. outgoing queue after the buffer has been filled (capture devices) or
  786. displayed (output devices). Drivers set or clear this flag when the
  787. <constant>VIDIOC_QUERYBUF</constant> ioctl is called. After
  788. (successful) calling the <constant>VIDIOC_QBUF </constant>ioctl it is
  789. always set and after <constant>VIDIOC_DQBUF</constant> always
  790. cleared.</entry>
  791. </row>
  792. <row>
  793. <entry><constant>V4L2_BUF_FLAG_DONE</constant></entry>
  794. <entry>0x0004</entry>
  795. <entry>When this flag is set, the buffer is currently on
  796. the outgoing queue, ready to be dequeued from the driver. Drivers set
  797. or clear this flag when the <constant>VIDIOC_QUERYBUF</constant> ioctl
  798. is called. After calling the <constant>VIDIOC_QBUF</constant> or
  799. <constant>VIDIOC_DQBUF</constant> it is always cleared. Of course a
  800. buffer cannot be on both queues at the same time, the
  801. <constant>V4L2_BUF_FLAG_QUEUED</constant> and
  802. <constant>V4L2_BUF_FLAG_DONE</constant> flag are mutually exclusive.
  803. They can be both cleared however, then the buffer is in "dequeued"
  804. state, in the application domain to say so.</entry>
  805. </row>
  806. <row>
  807. <entry><constant>V4L2_BUF_FLAG_ERROR</constant></entry>
  808. <entry>0x0040</entry>
  809. <entry>When this flag is set, the buffer has been dequeued
  810. successfully, although the data might have been corrupted.
  811. This is recoverable, streaming may continue as normal and
  812. the buffer may be reused normally.
  813. Drivers set this flag when the <constant>VIDIOC_DQBUF</constant>
  814. ioctl is called.</entry>
  815. </row>
  816. <row>
  817. <entry><constant>V4L2_BUF_FLAG_KEYFRAME</constant></entry>
  818. <entry>0x0008</entry>
  819. <entry>Drivers set or clear this flag when calling the
  820. <constant>VIDIOC_DQBUF</constant> ioctl. It may be set by video
  821. capture devices when the buffer contains a compressed image which is a
  822. key frame (or field), &ie; can be decompressed on its own.</entry>
  823. </row>
  824. <row>
  825. <entry><constant>V4L2_BUF_FLAG_PFRAME</constant></entry>
  826. <entry>0x0010</entry>
  827. <entry>Similar to <constant>V4L2_BUF_FLAG_KEYFRAME</constant>
  828. this flags predicted frames or fields which contain only differences to a
  829. previous key frame.</entry>
  830. </row>
  831. <row>
  832. <entry><constant>V4L2_BUF_FLAG_BFRAME</constant></entry>
  833. <entry>0x0020</entry>
  834. <entry>Similar to <constant>V4L2_BUF_FLAG_PFRAME</constant>
  835. this is a bidirectional predicted frame or field. [ooc tbd]</entry>
  836. </row>
  837. <row>
  838. <entry><constant>V4L2_BUF_FLAG_TIMECODE</constant></entry>
  839. <entry>0x0100</entry>
  840. <entry>The <structfield>timecode</structfield> field is valid.
  841. Drivers set or clear this flag when the <constant>VIDIOC_DQBUF</constant>
  842. ioctl is called.</entry>
  843. </row>
  844. <row>
  845. <entry><constant>V4L2_BUF_FLAG_INPUT</constant></entry>
  846. <entry>0x0200</entry>
  847. <entry>The <structfield>input</structfield> field is valid.
  848. Applications set or clear this flag before calling the
  849. <constant>VIDIOC_QBUF</constant> ioctl.</entry>
  850. </row>
  851. <row>
  852. <entry><constant>V4L2_BUF_FLAG_PREPARED</constant></entry>
  853. <entry>0x0400</entry>
  854. <entry>The buffer has been prepared for I/O and can be queued by the
  855. application. Drivers set or clear this flag when the
  856. <link linkend="vidioc-querybuf">VIDIOC_QUERYBUF</link>, <link
  857. linkend="vidioc-qbuf">VIDIOC_PREPARE_BUF</link>, <link
  858. linkend="vidioc-qbuf">VIDIOC_QBUF</link> or <link
  859. linkend="vidioc-qbuf">VIDIOC_DQBUF</link> ioctl is called.</entry>
  860. </row>
  861. <row>
  862. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_INVALIDATE</constant></entry>
  863. <entry>0x0400</entry>
  864. <entry>Caches do not have to be invalidated for this buffer.
  865. Typically applications shall use this flag if the data captured in the buffer
  866. is not going to be touched by the CPU, instead the buffer will, probably, be
  867. passed on to a DMA-capable hardware unit for further processing or output.
  868. </entry>
  869. </row>
  870. <row>
  871. <entry><constant>V4L2_BUF_FLAG_NO_CACHE_CLEAN</constant></entry>
  872. <entry>0x0800</entry>
  873. <entry>Caches do not have to be cleaned for this buffer.
  874. Typically applications shall use this flag for output buffers if the data
  875. in this buffer has not been created by the CPU but by some DMA-capable unit,
  876. in which case caches have not been used.</entry>
  877. </row>
  878. </tbody>
  879. </tgroup>
  880. </table>
  881. <table pgwide="1" frame="none" id="v4l2-memory">
  882. <title>enum v4l2_memory</title>
  883. <tgroup cols="3">
  884. &cs-def;
  885. <tbody valign="top">
  886. <row>
  887. <entry><constant>V4L2_MEMORY_MMAP</constant></entry>
  888. <entry>1</entry>
  889. <entry>The buffer is used for <link linkend="mmap">memory
  890. mapping</link> I/O.</entry>
  891. </row>
  892. <row>
  893. <entry><constant>V4L2_MEMORY_USERPTR</constant></entry>
  894. <entry>2</entry>
  895. <entry>The buffer is used for <link linkend="userp">user
  896. pointer</link> I/O.</entry>
  897. </row>
  898. <row>
  899. <entry><constant>V4L2_MEMORY_OVERLAY</constant></entry>
  900. <entry>3</entry>
  901. <entry>[to do]</entry>
  902. </row>
  903. </tbody>
  904. </tgroup>
  905. </table>
  906. <section>
  907. <title>Timecodes</title>
  908. <para>The <structname>v4l2_timecode</structname> structure is
  909. designed to hold a <xref linkend="smpte12m" /> or similar timecode.
  910. (struct <structname>timeval</structname> timestamps are stored in
  911. &v4l2-buffer; field <structfield>timestamp</structfield>.)</para>
  912. <table frame="none" pgwide="1" id="v4l2-timecode">
  913. <title>struct <structname>v4l2_timecode</structname></title>
  914. <tgroup cols="3">
  915. &cs-str;
  916. <tbody valign="top">
  917. <row>
  918. <entry>__u32</entry>
  919. <entry><structfield>type</structfield></entry>
  920. <entry>Frame rate the timecodes are based on, see <xref
  921. linkend="timecode-type" />.</entry>
  922. </row>
  923. <row>
  924. <entry>__u32</entry>
  925. <entry><structfield>flags</structfield></entry>
  926. <entry>Timecode flags, see <xref linkend="timecode-flags" />.</entry>
  927. </row>
  928. <row>
  929. <entry>__u8</entry>
  930. <entry><structfield>frames</structfield></entry>
  931. <entry>Frame count, 0 ... 23/24/29/49/59, depending on the
  932. type of timecode.</entry>
  933. </row>
  934. <row>
  935. <entry>__u8</entry>
  936. <entry><structfield>seconds</structfield></entry>
  937. <entry>Seconds count, 0 ... 59. This is a binary, not BCD number.</entry>
  938. </row>
  939. <row>
  940. <entry>__u8</entry>
  941. <entry><structfield>minutes</structfield></entry>
  942. <entry>Minutes count, 0 ... 59. This is a binary, not BCD number.</entry>
  943. </row>
  944. <row>
  945. <entry>__u8</entry>
  946. <entry><structfield>hours</structfield></entry>
  947. <entry>Hours count, 0 ... 29. This is a binary, not BCD number.</entry>
  948. </row>
  949. <row>
  950. <entry>__u8</entry>
  951. <entry><structfield>userbits</structfield>[4]</entry>
  952. <entry>The "user group" bits from the timecode.</entry>
  953. </row>
  954. </tbody>
  955. </tgroup>
  956. </table>
  957. <table frame="none" pgwide="1" id="timecode-type">
  958. <title>Timecode Types</title>
  959. <tgroup cols="3">
  960. &cs-def;
  961. <tbody valign="top">
  962. <row>
  963. <entry><constant>V4L2_TC_TYPE_24FPS</constant></entry>
  964. <entry>1</entry>
  965. <entry>24 frames per second, i.&nbsp;e. film.</entry>
  966. </row>
  967. <row>
  968. <entry><constant>V4L2_TC_TYPE_25FPS</constant></entry>
  969. <entry>2</entry>
  970. <entry>25 frames per second, &ie; PAL or SECAM video.</entry>
  971. </row>
  972. <row>
  973. <entry><constant>V4L2_TC_TYPE_30FPS</constant></entry>
  974. <entry>3</entry>
  975. <entry>30 frames per second, &ie; NTSC video.</entry>
  976. </row>
  977. <row>
  978. <entry><constant>V4L2_TC_TYPE_50FPS</constant></entry>
  979. <entry>4</entry>
  980. <entry></entry>
  981. </row>
  982. <row>
  983. <entry><constant>V4L2_TC_TYPE_60FPS</constant></entry>
  984. <entry>5</entry>
  985. <entry></entry>
  986. </row>
  987. </tbody>
  988. </tgroup>
  989. </table>
  990. <table frame="none" pgwide="1" id="timecode-flags">
  991. <title>Timecode Flags</title>
  992. <tgroup cols="3">
  993. &cs-def;
  994. <tbody valign="top">
  995. <row>
  996. <entry><constant>V4L2_TC_FLAG_DROPFRAME</constant></entry>
  997. <entry>0x0001</entry>
  998. <entry>Indicates "drop frame" semantics for counting frames
  999. in 29.97 fps material. When set, frame numbers 0 and 1 at the start of
  1000. each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the
  1001. count.</entry>
  1002. </row>
  1003. <row>
  1004. <entry><constant>V4L2_TC_FLAG_COLORFRAME</constant></entry>
  1005. <entry>0x0002</entry>
  1006. <entry>The "color frame" flag.</entry>
  1007. </row>
  1008. <row>
  1009. <entry><constant>V4L2_TC_USERBITS_field</constant></entry>
  1010. <entry>0x000C</entry>
  1011. <entry>Field mask for the "binary group flags".</entry>
  1012. </row>
  1013. <row>
  1014. <entry><constant>V4L2_TC_USERBITS_USERDEFINED</constant></entry>
  1015. <entry>0x0000</entry>
  1016. <entry>Unspecified format.</entry>
  1017. </row>
  1018. <row>
  1019. <entry><constant>V4L2_TC_USERBITS_8BITCHARS</constant></entry>
  1020. <entry>0x0008</entry>
  1021. <entry>8-bit ISO characters.</entry>
  1022. </row>
  1023. </tbody>
  1024. </tgroup>
  1025. </table>
  1026. </section>
  1027. </section>
  1028. <section id="field-order">
  1029. <title>Field Order</title>
  1030. <para>We have to distinguish between progressive and interlaced
  1031. video. Progressive video transmits all lines of a video image
  1032. sequentially. Interlaced video divides an image into two fields,
  1033. containing only the odd and even lines of the image, respectively.
  1034. Alternating the so called odd and even field are transmitted, and due
  1035. to a small delay between fields a cathode ray TV displays the lines
  1036. interleaved, yielding the original frame. This curious technique was
  1037. invented because at refresh rates similar to film the image would
  1038. fade out too quickly. Transmitting fields reduces the flicker without
  1039. the necessity of doubling the frame rate and with it the bandwidth
  1040. required for each channel.</para>
  1041. <para>It is important to understand a video camera does not expose
  1042. one frame at a time, merely transmitting the frames separated into
  1043. fields. The fields are in fact captured at two different instances in
  1044. time. An object on screen may well move between one field and the
  1045. next. For applications analysing motion it is of paramount importance
  1046. to recognize which field of a frame is older, the <emphasis>temporal
  1047. order</emphasis>.</para>
  1048. <para>When the driver provides or accepts images field by field
  1049. rather than interleaved, it is also important applications understand
  1050. how the fields combine to frames. We distinguish between top (aka odd) and
  1051. bottom (aka even) fields, the <emphasis>spatial order</emphasis>: The first line
  1052. of the top field is the first line of an interlaced frame, the first
  1053. line of the bottom field is the second line of that frame.</para>
  1054. <para>However because fields were captured one after the other,
  1055. arguing whether a frame commences with the top or bottom field is
  1056. pointless. Any two successive top and bottom, or bottom and top fields
  1057. yield a valid frame. Only when the source was progressive to begin
  1058. with, &eg; when transferring film to video, two fields may come from
  1059. the same frame, creating a natural order.</para>
  1060. <para>Counter to intuition the top field is not necessarily the
  1061. older field. Whether the older field contains the top or bottom lines
  1062. is a convention determined by the video standard. Hence the
  1063. distinction between temporal and spatial order of fields. The diagrams
  1064. below should make this clearer.</para>
  1065. <para>All video capture and output devices must report the current
  1066. field order. Some drivers may permit the selection of a different
  1067. order, to this end applications initialize the
  1068. <structfield>field</structfield> field of &v4l2-pix-format; before
  1069. calling the &VIDIOC-S-FMT; ioctl. If this is not desired it should
  1070. have the value <constant>V4L2_FIELD_ANY</constant> (0).</para>
  1071. <table frame="none" pgwide="1" id="v4l2-field">
  1072. <title>enum v4l2_field</title>
  1073. <tgroup cols="3">
  1074. &cs-def;
  1075. <tbody valign="top">
  1076. <row>
  1077. <entry><constant>V4L2_FIELD_ANY</constant></entry>
  1078. <entry>0</entry>
  1079. <entry>Applications request this field order when any
  1080. one of the <constant>V4L2_FIELD_NONE</constant>,
  1081. <constant>V4L2_FIELD_TOP</constant>,
  1082. <constant>V4L2_FIELD_BOTTOM</constant>, or
  1083. <constant>V4L2_FIELD_INTERLACED</constant> formats is acceptable.
  1084. Drivers choose depending on hardware capabilities or e.&nbsp;g. the
  1085. requested image size, and return the actual field order. &v4l2-buffer;
  1086. <structfield>field</structfield> can never be
  1087. <constant>V4L2_FIELD_ANY</constant>.</entry>
  1088. </row>
  1089. <row>
  1090. <entry><constant>V4L2_FIELD_NONE</constant></entry>
  1091. <entry>1</entry>
  1092. <entry>Images are in progressive format, not interlaced.
  1093. The driver may also indicate this order when it cannot distinguish
  1094. between <constant>V4L2_FIELD_TOP</constant> and
  1095. <constant>V4L2_FIELD_BOTTOM</constant>.</entry>
  1096. </row>
  1097. <row>
  1098. <entry><constant>V4L2_FIELD_TOP</constant></entry>
  1099. <entry>2</entry>
  1100. <entry>Images consist of the top (aka odd) field only.</entry>
  1101. </row>
  1102. <row>
  1103. <entry><constant>V4L2_FIELD_BOTTOM</constant></entry>
  1104. <entry>3</entry>
  1105. <entry>Images consist of the bottom (aka even) field only.
  1106. Applications may wish to prevent a device from capturing interlaced
  1107. images because they will have "comb" or "feathering" artefacts around
  1108. moving objects.</entry>
  1109. </row>
  1110. <row>
  1111. <entry><constant>V4L2_FIELD_INTERLACED</constant></entry>
  1112. <entry>4</entry>
  1113. <entry>Images contain both fields, interleaved line by
  1114. line. The temporal order of the fields (whether the top or bottom
  1115. field is first transmitted) depends on the current video standard.
  1116. M/NTSC transmits the bottom field first, all other standards the top
  1117. field first.</entry>
  1118. </row>
  1119. <row>
  1120. <entry><constant>V4L2_FIELD_SEQ_TB</constant></entry>
  1121. <entry>5</entry>
  1122. <entry>Images contain both fields, the top field lines
  1123. are stored first in memory, immediately followed by the bottom field
  1124. lines. Fields are always stored in temporal order, the older one first
  1125. in memory. Image sizes refer to the frame, not fields.</entry>
  1126. </row>
  1127. <row>
  1128. <entry><constant>V4L2_FIELD_SEQ_BT</constant></entry>
  1129. <entry>6</entry>
  1130. <entry>Images contain both fields, the bottom field
  1131. lines are stored first in memory, immediately followed by the top
  1132. field lines. Fields are always stored in temporal order, the older one
  1133. first in memory. Image sizes refer to the frame, not fields.</entry>
  1134. </row>
  1135. <row>
  1136. <entry><constant>V4L2_FIELD_ALTERNATE</constant></entry>
  1137. <entry>7</entry>
  1138. <entry>The two fields of a frame are passed in separate
  1139. buffers, in temporal order, &ie; the older one first. To indicate the field
  1140. parity (whether the current field is a top or bottom field) the driver
  1141. or application, depending on data direction, must set &v4l2-buffer;
  1142. <structfield>field</structfield> to
  1143. <constant>V4L2_FIELD_TOP</constant> or
  1144. <constant>V4L2_FIELD_BOTTOM</constant>. Any two successive fields pair
  1145. to build a frame. If fields are successive, without any dropped fields
  1146. between them (fields can drop individually), can be determined from
  1147. the &v4l2-buffer; <structfield>sequence</structfield> field. Image
  1148. sizes refer to the frame, not fields. This format cannot be selected
  1149. when using the read/write I/O method.<!-- Where it's indistinguishable
  1150. from V4L2_FIELD_SEQ_*. --></entry>
  1151. </row>
  1152. <row>
  1153. <entry><constant>V4L2_FIELD_INTERLACED_TB</constant></entry>
  1154. <entry>8</entry>
  1155. <entry>Images contain both fields, interleaved line by
  1156. line, top field first. The top field is transmitted first.</entry>
  1157. </row>
  1158. <row>
  1159. <entry><constant>V4L2_FIELD_INTERLACED_BT</constant></entry>
  1160. <entry>9</entry>
  1161. <entry>Images contain both fields, interleaved line by
  1162. line, top field first. The bottom field is transmitted first.</entry>
  1163. </row>
  1164. </tbody>
  1165. </tgroup>
  1166. </table>
  1167. <figure id="fieldseq-tb">
  1168. <title>Field Order, Top Field First Transmitted</title>
  1169. <mediaobject>
  1170. <imageobject>
  1171. <imagedata fileref="fieldseq_tb.pdf" format="PS" />
  1172. </imageobject>
  1173. <imageobject>
  1174. <imagedata fileref="fieldseq_tb.gif" format="GIF" />
  1175. </imageobject>
  1176. </mediaobject>
  1177. </figure>
  1178. <figure id="fieldseq-bt">
  1179. <title>Field Order, Bottom Field First Transmitted</title>
  1180. <mediaobject>
  1181. <imageobject>
  1182. <imagedata fileref="fieldseq_bt.pdf" format="PS" />
  1183. </imageobject>
  1184. <imageobject>
  1185. <imagedata fileref="fieldseq_bt.gif" format="GIF" />
  1186. </imageobject>
  1187. </mediaobject>
  1188. </figure>
  1189. </section>