v4l2grab.c.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <programlisting>
  2. /* V4L2 video picture grabber
  3. Copyright (C) 2009 Mauro Carvalho Chehab &lt;mchehab@infradead.org&gt;
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. */
  12. #include &lt;stdio.h&gt;
  13. #include &lt;stdlib.h&gt;
  14. #include &lt;string.h&gt;
  15. #include &lt;fcntl.h&gt;
  16. #include &lt;errno.h&gt;
  17. #include &lt;sys/ioctl.h&gt;
  18. #include &lt;sys/types.h&gt;
  19. #include &lt;sys/time.h&gt;
  20. #include &lt;sys/mman.h&gt;
  21. #include &lt;linux/videodev2.h&gt;
  22. #include "../libv4l/include/libv4l2.h"
  23. #define CLEAR(x) memset(&amp;(x), 0, sizeof(x))
  24. struct buffer {
  25. void *start;
  26. size_t length;
  27. };
  28. static void xioctl(int fh, int request, void *arg)
  29. {
  30. int r;
  31. do {
  32. r = v4l2_ioctl(fh, request, arg);
  33. } while (r == -1 &amp;&amp; ((errno == EINTR) || (errno == EAGAIN)));
  34. if (r == -1) {
  35. fprintf(stderr, "error %d, %s\n", errno, strerror(errno));
  36. exit(EXIT_FAILURE);
  37. }
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. struct <link linkend="v4l2-format">v4l2_format</link> fmt;
  42. struct <link linkend="v4l2-buffer">v4l2_buffer</link> buf;
  43. struct <link linkend="v4l2-requestbuffers">v4l2_requestbuffers</link> req;
  44. enum <link linkend="v4l2-buf-type">v4l2_buf_type</link> type;
  45. fd_set fds;
  46. struct timeval tv;
  47. int r, fd = -1;
  48. unsigned int i, n_buffers;
  49. char *dev_name = "/dev/video0";
  50. char out_name[256];
  51. FILE *fout;
  52. struct buffer *buffers;
  53. fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
  54. if (fd &lt; 0) {
  55. perror("Cannot open device");
  56. exit(EXIT_FAILURE);
  57. }
  58. CLEAR(fmt);
  59. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  60. fmt.fmt.pix.width = 640;
  61. fmt.fmt.pix.height = 480;
  62. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
  63. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  64. xioctl(fd, VIDIOC_S_FMT, &amp;fmt);
  65. if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
  66. printf("Libv4l didn't accept RGB24 format. Can't proceed.\n");
  67. exit(EXIT_FAILURE);
  68. }
  69. if ((fmt.fmt.pix.width != 640) || (fmt.fmt.pix.height != 480))
  70. printf("Warning: driver is sending image at %dx%d\n",
  71. fmt.fmt.pix.width, fmt.fmt.pix.height);
  72. CLEAR(req);
  73. req.count = 2;
  74. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  75. req.memory = V4L2_MEMORY_MMAP;
  76. xioctl(fd, VIDIOC_REQBUFS, &amp;req);
  77. buffers = calloc(req.count, sizeof(*buffers));
  78. for (n_buffers = 0; n_buffers &lt; req.count; ++n_buffers) {
  79. CLEAR(buf);
  80. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  81. buf.memory = V4L2_MEMORY_MMAP;
  82. buf.index = n_buffers;
  83. xioctl(fd, VIDIOC_QUERYBUF, &amp;buf);
  84. buffers[n_buffers].length = buf.length;
  85. buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
  86. PROT_READ | PROT_WRITE, MAP_SHARED,
  87. fd, buf.m.offset);
  88. if (MAP_FAILED == buffers[n_buffers].start) {
  89. perror("mmap");
  90. exit(EXIT_FAILURE);
  91. }
  92. }
  93. for (i = 0; i &lt; n_buffers; ++i) {
  94. CLEAR(buf);
  95. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  96. buf.memory = V4L2_MEMORY_MMAP;
  97. buf.index = i;
  98. xioctl(fd, VIDIOC_QBUF, &amp;buf);
  99. }
  100. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  101. xioctl(fd, VIDIOC_STREAMON, &amp;type);
  102. for (i = 0; i &lt; 20; i++) {
  103. do {
  104. FD_ZERO(&amp;fds);
  105. FD_SET(fd, &amp;fds);
  106. /* Timeout. */
  107. tv.tv_sec = 2;
  108. tv.tv_usec = 0;
  109. r = select(fd + 1, &amp;fds, NULL, NULL, &amp;tv);
  110. } while ((r == -1 &amp;&amp; (errno = EINTR)));
  111. if (r == -1) {
  112. perror("select");
  113. return errno;
  114. }
  115. CLEAR(buf);
  116. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  117. buf.memory = V4L2_MEMORY_MMAP;
  118. xioctl(fd, VIDIOC_DQBUF, &amp;buf);
  119. sprintf(out_name, "out%03d.ppm", i);
  120. fout = fopen(out_name, "w");
  121. if (!fout) {
  122. perror("Cannot open image");
  123. exit(EXIT_FAILURE);
  124. }
  125. fprintf(fout, "P6\n%d %d 255\n",
  126. fmt.fmt.pix.width, fmt.fmt.pix.height);
  127. fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
  128. fclose(fout);
  129. xioctl(fd, VIDIOC_QBUF, &amp;buf);
  130. }
  131. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  132. xioctl(fd, VIDIOC_STREAMOFF, &amp;type);
  133. for (i = 0; i &lt; n_buffers; ++i)
  134. v4l2_munmap(buffers[i].start, buffers[i].length);
  135. v4l2_close(fd);
  136. return 0;
  137. }
  138. </programlisting>