1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 2 <HTML 3 ><HEAD 4 ><TITLE 5 >Streaming I/O (Memory Mapping)</TITLE 6 ><META 7 NAME="GENERATOR" 8 CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK 9 REL="HOME" 10 TITLE="Video for Linux Two API Specification" 11 HREF="book1.htm"><LINK 12 REL="UP" 13 TITLE="Input/Output" 14 HREF="c5742.htm"><LINK 15 REL="PREVIOUS" 16 TITLE="Input/Output" 17 HREF="c5742.htm"><LINK 18 REL="NEXT" 19 TITLE="Streaming I/O (User Pointers)" 20 HREF="x5884.htm"></HEAD 21 ><BODY 22 CLASS="SECTION" 23 BGCOLOR="#FFFFFF" 24 TEXT="#000000" 25 LINK="#0000FF" 26 VLINK="#840084" 27 ALINK="#0000FF" 28 ><DIV 29 CLASS="NAVHEADER" 30 ><TABLE 31 SUMMARY="Header navigation table" 32 WIDTH="100%" 33 BORDER="0" 34 CELLPADDING="0" 35 CELLSPACING="0" 36 ><TR 37 ><TH 38 COLSPAN="3" 39 ALIGN="center" 40 >Video for Linux Two API Specification: Revision 0.24</TH 41 ></TR 42 ><TR 43 ><TD 44 WIDTH="10%" 45 ALIGN="left" 46 VALIGN="bottom" 47 ><A 48 HREF="c5742.htm" 49 ACCESSKEY="P" 50 >Prev</A 51 ></TD 52 ><TD 53 WIDTH="80%" 54 ALIGN="center" 55 VALIGN="bottom" 56 >Chapter 3. Input/Output</TD 57 ><TD 58 WIDTH="10%" 59 ALIGN="right" 60 VALIGN="bottom" 61 ><A 62 HREF="x5884.htm" 63 ACCESSKEY="N" 64 >Next</A 65 ></TD 66 ></TR 67 ></TABLE 68 ><HR 69 ALIGN="LEFT" 70 WIDTH="100%"></DIV 71 ><DIV 72 CLASS="SECTION" 73 ><H1 74 CLASS="SECTION" 75 ><A 76 NAME="MMAP" 77 >3.2. Streaming I/O (Memory Mapping)</A 78 ></H1 79 ><P 80 >Input and output devices support this I/O method when the 81 <CODE 82 CLASS="CONSTANT" 83 >V4L2_CAP_STREAMING</CODE 84 > flag in the 85 <CODE 86 CLASS="STRUCTFIELD" 87 >capabilities</CODE 88 > field of struct <A 89 HREF="r13105.htm#V4L2-CAPABILITY" 90 >v4l2_capability</A 91 > 92 returned by the <A 93 HREF="r13105.htm" 94 ><CODE 95 CLASS="CONSTANT" 96 >VIDIOC_QUERYCAP</CODE 97 ></A 98 > ioctl is set. There are two 99 streaming methods, to determine if the memory mapping flavor is 100 supported applications must call the <A 101 HREF="r13696.htm" 102 ><CODE 103 CLASS="CONSTANT" 104 >VIDIOC_REQBUFS</CODE 105 ></A 106 > ioctl.</P 107 ><P 108 >Streaming is an I/O method where only pointers to buffers 109 are exchanged between application and driver, the data itself is not 110 copied. Memory mapping is primarily intended to map buffers in device 111 memory into the application's address space. Device memory can be for 112 example the video memory on a graphics card with a video capture 113 add-on. However, being the most efficient I/O method available for a 114 long time, many other drivers support streaming as well, allocating 115 buffers in DMA-able main memory.</P 116 ><P 117 >A driver can support many sets of buffers. Each set is 118 identified by a unique buffer type value. The sets are independent and 119 each set can hold a different type of data. To access different sets 120 at the same time different file descriptors must be used.<A 121 NAME="AEN5803" 122 HREF="x5791.htm#FTN.AEN5803" 123 ><SPAN 124 CLASS="footnote" 125 >[1]</SPAN 126 ></A 127 ></P 128 ><P 129 >To allocate device buffers applications call the 130 <A 131 HREF="r13696.htm" 132 ><CODE 133 CLASS="CONSTANT" 134 >VIDIOC_REQBUFS</CODE 135 ></A 136 > ioctl with the desired number of buffers and buffer 137 type, for example <CODE 138 CLASS="CONSTANT" 139 >V4L2_BUF_TYPE_VIDEO_CAPTURE</CODE 140 >. 141 This ioctl can also be used to change the number of buffers or to free 142 the allocated memory, provided none of the buffers are still 143 mapped.</P 144 ><P 145 >Before applications can access the buffers they must map 146 them into their address space with the <A 147 HREF="r13889.htm" 148 ><CODE 149 CLASS="FUNCTION" 150 >mmap()</CODE 151 ></A 152 > function. The 153 location of the buffers in device memory can be determined with the 154 <A 155 HREF="r13022.htm" 156 ><CODE 157 CLASS="CONSTANT" 158 >VIDIOC_QUERYBUF</CODE 159 ></A 160 > ioctl. The <CODE 161 CLASS="STRUCTFIELD" 162 >m.offset</CODE 163 > and 164 <CODE 165 CLASS="STRUCTFIELD" 166 >length</CODE 167 > returned in a struct <A 168 HREF="x5953.htm#V4L2-BUFFER" 169 >v4l2_buffer</A 170 > are 171 passed as sixth and second parameter to the 172 <CODE 173 CLASS="FUNCTION" 174 >mmap()</CODE 175 > function. The offset and length values 176 must not be modified. Remember the buffers are allocated in physical 177 memory, as opposed to virtual memory which can be swapped out to disk. 178 Applications should free the buffers as soon as possible with the 179 <A 180 HREF="r14037.htm" 181 ><CODE 182 CLASS="FUNCTION" 183 >munmap()</CODE 184 ></A 185 > function.</P 186 ><DIV 187 CLASS="EXAMPLE" 188 ><A 189 NAME="AEN5823" 190 ></A 191 ><P 192 ><B 193 >Example 3-1. Mapping buffers</B 194 ></P 195 ><PRE 196 CLASS="PROGRAMLISTING" 197 >struct <A 198 HREF="r13696.htm#V4L2-REQUESTBUFFERS" 199 >v4l2_requestbuffers</A 200 > reqbuf; 201 struct { 202 void *start; 203 size_t length; 204 } *buffers; 205 unsigned int i; 206 207 memset (&reqbuf, 0, sizeof (reqbuf)); 208 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 209 reqbuf.memory = V4L2_MEMORY_MMAP; 210 reqbuf.count = 20; 211 212 if (-1 == ioctl (fd, <A 213 HREF="r13696.htm" 214 ><CODE 215 CLASS="CONSTANT" 216 >VIDIOC_REQBUFS</CODE 217 ></A 218 >, &reqbuf)) { 219 if (errno == EINVAL) 220 printf ("Video capturing or mmap-streaming is not supported\n"); 221 else 222 perror ("VIDIOC_REQBUFS"); 223 224 exit (EXIT_FAILURE); 225 } 226 227 /* We want at least five buffers. */ 228 229 if (reqbuf.count < 5) { 230 /* You may need to free the buffers here. */ 231 printf ("Not enough buffer memory\n"); 232 exit (EXIT_FAILURE); 233 } 234 235 buffers = calloc (reqbuf.count, sizeof (*buffers)); 236 assert (buffers != NULL); 237 238 for (i = 0; i < reqbuf.count; i++) { 239 struct <A 240 HREF="x5953.htm#V4L2-BUFFER" 241 >v4l2_buffer</A 242 > buffer; 243 244 memset (&buffer, 0, sizeof (buffer)); 245 buffer.type = reqbuf.type; 246 buffer.memory = V4L2_MEMORY_MMAP; 247 buffer.index = i; 248 249 if (-1 == ioctl (fd, <A 250 HREF="r13022.htm" 251 ><CODE 252 CLASS="CONSTANT" 253 >VIDIOC_QUERYBUF</CODE 254 ></A 255 >, &buffer)) { 256 perror ("VIDIOC_QUERYBUF"); 257 exit (EXIT_FAILURE); 258 } 259 260 buffers[i].length = buffer.length; /* remember for munmap() */ 261 262 buffers[i].start = mmap (NULL, buffer.length, 263 PROT_READ | PROT_WRITE, /* recommended */ 264 MAP_SHARED, /* recommended */ 265 fd, buffer.m.offset); 266 267 if (MAP_FAILED == buffers[i].start) { 268 /* If you do not exit here you should unmap() and free() 269 the buffers mapped so far. */ 270 perror ("mmap"); 271 exit (EXIT_FAILURE); 272 } 273 } 274 275 /* Cleanup. */ 276 277 for (i = 0; i < reqbuf.count; i++) 278 munmap (buffers[i].start, buffers[i].length); 279 </PRE 280 ></DIV 281 ><P 282 >Conceptually streaming drivers maintain two buffer queues, an incoming 283 and an outgoing queue. They separate the synchronous capture or output 284 operation locked to a video clock from the application which is 285 subject to random disk or network delays and preemption by 286 other processes, thereby reducing the probability of data loss. 287 The queues are organized as FIFOs, buffers will be 288 output in the order enqueued in the incoming FIFO, and were 289 captured in the order dequeued from the outgoing FIFO.</P 290 ><P 291 >The driver may require a minimum number of buffers enqueued 292 at all times to function, apart of this no limit exists on the number 293 of buffers applications can enqueue in advance, or dequeue and 294 process. They can also enqueue in a different order than buffers have 295 been dequeued, and the driver can <SPAN 296 CLASS="emphasis" 297 ><I 298 CLASS="EMPHASIS" 299 >fill</I 300 ></SPAN 301 > enqueued 302 <SPAN 303 CLASS="emphasis" 304 ><I 305 CLASS="EMPHASIS" 306 >empty</I 307 ></SPAN 308 > buffers in any order. <A 309 NAME="AEN5836" 310 HREF="x5791.htm#FTN.AEN5836" 311 ><SPAN 312 CLASS="footnote" 313 >[2]</SPAN 314 ></A 315 > The index number of a buffer (struct <A 316 HREF="x5953.htm#V4L2-BUFFER" 317 >v4l2_buffer</A 318 > 319 <CODE 320 CLASS="STRUCTFIELD" 321 >index</CODE 322 >) plays no role here, it only 323 identifies the buffer.</P 324 ><P 325 >Initially all mapped buffers are in dequeued state, 326 inaccessible by the driver. For capturing applications it is customary 327 to first enqueue all mapped buffers, then to start capturing and enter 328 the read loop. Here the application waits until a filled buffer can be 329 dequeued, and re-enqueues the buffer when the data is no longer 330 needed. Output applications fill and enqueue buffers, when enough 331 buffers are stacked up the output is started with 332 <CODE 333 CLASS="CONSTANT" 334 >VIDIOC_STREAMON</CODE 335 >. In the write loop, when 336 the application runs out of free buffers, it must wait until an empty 337 buffer can be dequeued and reused.</P 338 ><P 339 >To enqueue and dequeue a buffer applications use the 340 <A 341 HREF="r12878.htm" 342 ><CODE 343 CLASS="CONSTANT" 344 >VIDIOC_QBUF</CODE 345 ></A 346 > and <A 347 HREF="r12878.htm" 348 ><CODE 349 CLASS="CONSTANT" 350 >VIDIOC_DQBUF</CODE 351 ></A 352 > ioctl. The status of a buffer being 353 mapped, enqueued, full or empty can be determined at any time using the 354 <A 355 HREF="r13022.htm" 356 ><CODE 357 CLASS="CONSTANT" 358 >VIDIOC_QUERYBUF</CODE 359 ></A 360 > ioctl. Two methods exist to suspend execution of the 361 application until one or more buffers can be dequeued. By default 362 <CODE 363 CLASS="CONSTANT" 364 >VIDIOC_DQBUF</CODE 365 > blocks when no buffer is in the 366 outgoing queue. When the <CODE 367 CLASS="CONSTANT" 368 >O_NONBLOCK</CODE 369 > flag was 370 given to the <A 371 HREF="r14090.htm" 372 ><CODE 373 CLASS="FUNCTION" 374 >open()</CODE 375 ></A 376 > function, <CODE 377 CLASS="CONSTANT" 378 >VIDIOC_DQBUF</CODE 379 > 380 returns immediately with an <SPAN 381 CLASS="ERRORCODE" 382 >EAGAIN</SPAN 383 > error code when no buffer is available. The 384 <A 385 HREF="r14390.htm" 386 ><CODE 387 CLASS="FUNCTION" 388 >select()</CODE 389 ></A 390 > or <A 391 HREF="r14169.htm" 392 ><CODE 393 CLASS="FUNCTION" 394 >poll()</CODE 395 ></A 396 > function are always available.</P 397 ><P 398 >To start and stop capturing or output applications call the 399 <A 400 HREF="r13817.htm" 401 ><CODE 402 CLASS="CONSTANT" 403 >VIDIOC_STREAMON</CODE 404 ></A 405 > and <A 406 HREF="r13817.htm" 407 ><CODE 408 CLASS="CONSTANT" 409 >VIDIOC_STREAMOFF</CODE 410 ></A 411 > ioctl. Note 412 <CODE 413 CLASS="CONSTANT" 414 >VIDIOC_STREAMOFF</CODE 415 > removes all buffers from both 416 queues as a side effect. Since there is no notion of doing anything 417 "now" on a multitasking system, if an application needs to synchronize 418 with another event it should examine the struct <A 419 HREF="x5953.htm#V4L2-BUFFER" 420 >v4l2_buffer</A 421 > 422 <CODE 423 CLASS="STRUCTFIELD" 424 >timestamp</CODE 425 > of captured buffers, or set the 426 field before enqueuing buffers for output.</P 427 ><P 428 >Drivers implementing memory mapping I/O must 429 support the <CODE 430 CLASS="CONSTANT" 431 >VIDIOC_REQBUFS</CODE 432 >, 433 <CODE 434 CLASS="CONSTANT" 435 >VIDIOC_QUERYBUF</CODE 436 >, 437 <CODE 438 CLASS="CONSTANT" 439 >VIDIOC_QBUF</CODE 440 >, <CODE 441 CLASS="CONSTANT" 442 >VIDIOC_DQBUF</CODE 443 >, 444 <CODE 445 CLASS="CONSTANT" 446 >VIDIOC_STREAMON</CODE 447 > and 448 <CODE 449 CLASS="CONSTANT" 450 >VIDIOC_STREAMOFF</CODE 451 > ioctl, the 452 <CODE 453 CLASS="FUNCTION" 454 >mmap()</CODE 455 >, <CODE 456 CLASS="FUNCTION" 457 >munmap()</CODE 458 >, 459 <CODE 460 CLASS="FUNCTION" 461 >select()</CODE 462 > and <CODE 463 CLASS="FUNCTION" 464 >poll()</CODE 465 > 466 function.<A 467 NAME="AEN5878" 468 HREF="x5791.htm#FTN.AEN5878" 469 ><SPAN 470 CLASS="footnote" 471 >[3]</SPAN 472 ></A 473 ></P 474 ><P 475 >[capture example]</P 476 ></DIV 477 ><H3 478 CLASS="FOOTNOTES" 479 >Notes</H3 480 ><TABLE 481 BORDER="0" 482 CLASS="FOOTNOTES" 483 WIDTH="100%" 484 ><TR 485 ><TD 486 ALIGN="LEFT" 487 VALIGN="TOP" 488 WIDTH="5%" 489 ><A 490 NAME="FTN.AEN5803" 491 HREF="x5791.htm#AEN5803" 492 ><SPAN 493 CLASS="footnote" 494 >[1]</SPAN 495 ></A 496 ></TD 497 ><TD 498 ALIGN="LEFT" 499 VALIGN="TOP" 500 WIDTH="95%" 501 ><P 502 >One could use one file descriptor and set the buffer 503 type field accordingly when calling <A 504 HREF="r12878.htm" 505 ><CODE 506 CLASS="CONSTANT" 507 >VIDIOC_QBUF</CODE 508 ></A 509 > etc., but it makes 510 the <CODE 511 CLASS="FUNCTION" 512 >select()</CODE 513 > function ambiguous. We also like the 514 clean approach of one file descriptor per logical stream. Video 515 overlay for example is also a logical stream, although the CPU is not 516 needed for continuous operation.</P 517 ></TD 518 ></TR 519 ><TR 520 ><TD 521 ALIGN="LEFT" 522 VALIGN="TOP" 523 WIDTH="5%" 524 ><A 525 NAME="FTN.AEN5836" 526 HREF="x5791.htm#AEN5836" 527 ><SPAN 528 CLASS="footnote" 529 >[2]</SPAN 530 ></A 531 ></TD 532 ><TD 533 ALIGN="LEFT" 534 VALIGN="TOP" 535 WIDTH="95%" 536 ><P 537 >Random enqueue order permits applications processing 538 images out of order (such as video codecs) to return buffers earlier, 539 reducing the probability of data loss. Random fill order allows 540 drivers to reuse buffers on a LIFO-basis, taking advantage of caches 541 holding scatter-gather lists and the like.</P 542 ></TD 543 ></TR 544 ><TR 545 ><TD 546 ALIGN="LEFT" 547 VALIGN="TOP" 548 WIDTH="5%" 549 ><A 550 NAME="FTN.AEN5878" 551 HREF="x5791.htm#AEN5878" 552 ><SPAN 553 CLASS="footnote" 554 >[3]</SPAN 555 ></A 556 ></TD 557 ><TD 558 ALIGN="LEFT" 559 VALIGN="TOP" 560 WIDTH="95%" 561 ><P 562 >At the driver level <CODE 563 CLASS="FUNCTION" 564 >select()</CODE 565 > and 566 <CODE 567 CLASS="FUNCTION" 568 >poll()</CODE 569 > are the same, and 570 <CODE 571 CLASS="FUNCTION" 572 >select()</CODE 573 > is too important to be optional. The 574 rest should be evident.</P 575 ></TD 576 ></TR 577 ></TABLE 578 ><DIV 579 CLASS="NAVFOOTER" 580 ><HR 581 ALIGN="LEFT" 582 WIDTH="100%"><TABLE 583 SUMMARY="Footer navigation table" 584 WIDTH="100%" 585 BORDER="0" 586 CELLPADDING="0" 587 CELLSPACING="0" 588 ><TR 589 ><TD 590 WIDTH="33%" 591 ALIGN="left" 592 VALIGN="top" 593 ><A 594 HREF="c5742.htm" 595 ACCESSKEY="P" 596 >Prev</A 597 ></TD 598 ><TD 599 WIDTH="34%" 600 ALIGN="center" 601 VALIGN="top" 602 ><A 603 HREF="book1.htm" 604 ACCESSKEY="H" 605 >Home</A 606 ></TD 607 ><TD 608 WIDTH="33%" 609 ALIGN="right" 610 VALIGN="top" 611 ><A 612 HREF="x5884.htm" 613 ACCESSKEY="N" 614 >Next</A 615 ></TD 616 ></TR 617 ><TR 618 ><TD 619 WIDTH="33%" 620 ALIGN="left" 621 VALIGN="top" 622 >Input/Output</TD 623 ><TD 624 WIDTH="34%" 625 ALIGN="center" 626 VALIGN="top" 627 ><A 628 HREF="c5742.htm" 629 ACCESSKEY="U" 630 >Up</A 631 ></TD 632 ><TD 633 WIDTH="33%" 634 ALIGN="right" 635 VALIGN="top" 636 >Streaming I/O (User Pointers)</TD 637 ></TR 638 ></TABLE 639 ></DIV 640 ></BODY 641 ></HTML 642 > 643