1 .. Permission is granted to copy, distribute and/or modify this
2 .. document under the terms of the GNU Free Documentation License,
3 .. Version 1.1 or any later version published by the Free Software
4 .. Foundation, with no Invariant Sections, no Front-Cover Texts
5 .. and no Back-Cover Texts. A copy of the license is included at
6 .. Documentation/media/uapi/fdl-appendix.rst.
8 .. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections
10 file: media/v4l/v4l2grab.c
11 ==========================
15 /* V4L2 video picture grabber
16 Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation version 2 of the License.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
37 #include <linux/videodev2.h>
38 #include "../libv4l/include/libv4l2.h"
40 #define CLEAR(x) memset(&(x), 0, sizeof(x))
47 static void xioctl(int fh, int request, void *arg)
52 r = v4l2_ioctl(fh, request, arg);
53 } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
56 fprintf(stderr, "error %d, %s\\n", errno, strerror(errno));
61 int main(int argc, char **argv)
63 struct v4l2_format fmt;
64 struct v4l2_buffer buf;
65 struct v4l2_requestbuffers req;
66 enum v4l2_buf_type type;
70 unsigned int i, n_buffers;
71 char *dev_name = "/dev/video0";
74 struct buffer *buffers;
76 fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
78 perror("Cannot open device");
83 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
84 fmt.fmt.pix.width = 640;
85 fmt.fmt.pix.height = 480;
86 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
87 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
88 xioctl(fd, VIDIOC_S_FMT, &fmt);
89 if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
90 printf("Libv4l didn't accept RGB24 format. Can't proceed.\\n");
93 if ((fmt.fmt.pix.width != 640) || (fmt.fmt.pix.height != 480))
94 printf("Warning: driver is sending image at %dx%d\\n",
95 fmt.fmt.pix.width, fmt.fmt.pix.height);
99 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
100 req.memory = V4L2_MEMORY_MMAP;
101 xioctl(fd, VIDIOC_REQBUFS, &req);
103 buffers = calloc(req.count, sizeof(*buffers));
104 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
107 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
108 buf.memory = V4L2_MEMORY_MMAP;
109 buf.index = n_buffers;
111 xioctl(fd, VIDIOC_QUERYBUF, &buf);
113 buffers[n_buffers].length = buf.length;
114 buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
115 PROT_READ | PROT_WRITE, MAP_SHARED,
118 if (MAP_FAILED == buffers[n_buffers].start) {
124 for (i = 0; i < n_buffers; ++i) {
126 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
127 buf.memory = V4L2_MEMORY_MMAP;
129 xioctl(fd, VIDIOC_QBUF, &buf);
131 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
133 xioctl(fd, VIDIOC_STREAMON, &type);
134 for (i = 0; i < 20; i++) {
143 r = select(fd + 1, &fds, NULL, NULL, &tv);
144 } while ((r == -1 && (errno = EINTR)));
151 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
152 buf.memory = V4L2_MEMORY_MMAP;
153 xioctl(fd, VIDIOC_DQBUF, &buf);
155 sprintf(out_name, "out%03d.ppm", i);
156 fout = fopen(out_name, "w");
158 perror("Cannot open image");
161 fprintf(fout, "P6\\n%d %d 255\\n",
162 fmt.fmt.pix.width, fmt.fmt.pix.height);
163 fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
166 xioctl(fd, VIDIOC_QBUF, &buf);
169 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
170 xioctl(fd, VIDIOC_STREAMOFF, &type);
171 for (i = 0; i < n_buffers; ++i)
172 v4l2_munmap(buffers[i].start, buffers[i].length);