1 .. -*- coding: utf-8; mode: rst -*-
3 file: media/v4l/v4l2grab.c
4 ==========================
8 /* V4L2 video picture grabber
9 Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@infradead.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation version 2 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
30 #include <linux/videodev2.h>
31 #include "../libv4l/include/libv4l2.h"
33 #define CLEAR(x) memset(&(x), 0, sizeof(x))
40 static void xioctl(int fh, int request, void *arg)
45 r = v4l2_ioctl(fh, request, arg);
46 } while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
49 fprintf(stderr, "error %d, %s\\n", errno, strerror(errno));
54 int main(int argc, char **argv)
56 struct v4l2_format fmt;
57 struct v4l2_buffer buf;
58 struct v4l2_requestbuffers req;
59 enum v4l2_buf_type type;
63 unsigned int i, n_buffers;
64 char *dev_name = "/dev/video0";
67 struct buffer *buffers;
69 fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
71 perror("Cannot open device");
76 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
77 fmt.fmt.pix.width = 640;
78 fmt.fmt.pix.height = 480;
79 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
80 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
81 xioctl(fd, VIDIOC_S_FMT, &fmt);
82 if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
83 printf("Libv4l didn't accept RGB24 format. Can't proceed.\\n");
86 if ((fmt.fmt.pix.width != 640) || (fmt.fmt.pix.height != 480))
87 printf("Warning: driver is sending image at %dx%d\\n",
88 fmt.fmt.pix.width, fmt.fmt.pix.height);
92 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
93 req.memory = V4L2_MEMORY_MMAP;
94 xioctl(fd, VIDIOC_REQBUFS, &req);
96 buffers = calloc(req.count, sizeof(*buffers));
97 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
100 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
101 buf.memory = V4L2_MEMORY_MMAP;
102 buf.index = n_buffers;
104 xioctl(fd, VIDIOC_QUERYBUF, &buf);
106 buffers[n_buffers].length = buf.length;
107 buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
108 PROT_READ | PROT_WRITE, MAP_SHARED,
111 if (MAP_FAILED == buffers[n_buffers].start) {
117 for (i = 0; i < n_buffers; ++i) {
119 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
120 buf.memory = V4L2_MEMORY_MMAP;
122 xioctl(fd, VIDIOC_QBUF, &buf);
124 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
126 xioctl(fd, VIDIOC_STREAMON, &type);
127 for (i = 0; i < 20; i++) {
136 r = select(fd + 1, &fds, NULL, NULL, &tv);
137 } while ((r == -1 && (errno = EINTR)));
144 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
145 buf.memory = V4L2_MEMORY_MMAP;
146 xioctl(fd, VIDIOC_DQBUF, &buf);
148 sprintf(out_name, "out%03d.ppm", i);
149 fout = fopen(out_name, "w");
151 perror("Cannot open image");
154 fprintf(fout, "P6\\n%d %d 255\\n",
155 fmt.fmt.pix.width, fmt.fmt.pix.height);
156 fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
159 xioctl(fd, VIDIOC_QBUF, &buf);
162 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
163 xioctl(fd, VIDIOC_STREAMOFF, &type);
164 for (i = 0; i < n_buffers; ++i)
165 v4l2_munmap(buffers[i].start, buffers[i].length);