v4l2grab.c.rst 4.8 KB

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