1 .. -*- coding: utf-8; mode: rst -*-
3 file: media/v4l/capture.c
4 =========================
9 * V4L2 video capture example
11 * This program can be used and distributed without restrictions.
13 * This program is provided with the V4L2 API
14 * see https://linuxtv.org/docs.php for more information
22 #include <getopt.h> /* getopt_long() */
24 #include <fcntl.h> /* low-level i/o */
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
33 #include <linux/videodev2.h>
35 #define CLEAR(x) memset(&(x), 0, sizeof(x))
48 static char *dev_name;
49 static enum io_method io = IO_METHOD_MMAP;
51 struct buffer *buffers;
52 static unsigned int n_buffers;
54 static int force_format;
55 static int frame_count = 70;
57 static void errno_exit(const char *s)
59 fprintf(stderr, "%s error %d, %s\\n", s, errno, strerror(errno));
63 static int xioctl(int fh, int request, void *arg)
68 r = ioctl(fh, request, arg);
69 } while (-1 == r && EINTR == errno);
74 static void process_image(const void *p, int size)
77 fwrite(p, size, 1, stdout);
84 static int read_frame(void)
86 struct v4l2_buffer buf;
91 if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
97 /* Could ignore EIO, see spec. */
106 process_image(buffers[0].start, buffers[0].length);
112 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
113 buf.memory = V4L2_MEMORY_MMAP;
115 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
121 /* Could ignore EIO, see spec. */
126 errno_exit("VIDIOC_DQBUF");
130 assert(buf.index < n_buffers);
132 process_image(buffers[buf.index].start, buf.bytesused);
134 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
135 errno_exit("VIDIOC_QBUF");
138 case IO_METHOD_USERPTR:
141 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
142 buf.memory = V4L2_MEMORY_USERPTR;
144 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
150 /* Could ignore EIO, see spec. */
155 errno_exit("VIDIOC_DQBUF");
159 for (i = 0; i < n_buffers; ++i)
160 if (buf.m.userptr == (unsigned long)buffers[i].start
161 && buf.length == buffers[i].length)
164 assert(i < n_buffers);
166 process_image((void *)buf.m.userptr, buf.bytesused);
168 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
169 errno_exit("VIDIOC_QBUF");
176 static void mainloop(void)
182 while (count-- > 0) {
195 r = select(fd + 1, &fds, NULL, NULL, &tv);
200 errno_exit("select");
204 fprintf(stderr, "select timeout\\n");
210 /* EAGAIN - continue select loop. */
215 static void stop_capturing(void)
217 enum v4l2_buf_type type;
225 case IO_METHOD_USERPTR:
226 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
227 if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
228 errno_exit("VIDIOC_STREAMOFF");
233 static void start_capturing(void)
236 enum v4l2_buf_type type;
244 for (i = 0; i < n_buffers; ++i) {
245 struct v4l2_buffer buf;
248 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
249 buf.memory = V4L2_MEMORY_MMAP;
252 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
253 errno_exit("VIDIOC_QBUF");
255 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
256 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
257 errno_exit("VIDIOC_STREAMON");
260 case IO_METHOD_USERPTR:
261 for (i = 0; i < n_buffers; ++i) {
262 struct v4l2_buffer buf;
265 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
266 buf.memory = V4L2_MEMORY_USERPTR;
268 buf.m.userptr = (unsigned long)buffers[i].start;
269 buf.length = buffers[i].length;
271 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
272 errno_exit("VIDIOC_QBUF");
274 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
275 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
276 errno_exit("VIDIOC_STREAMON");
281 static void uninit_device(void)
287 free(buffers[0].start);
291 for (i = 0; i < n_buffers; ++i)
292 if (-1 == munmap(buffers[i].start, buffers[i].length))
293 errno_exit("munmap");
296 case IO_METHOD_USERPTR:
297 for (i = 0; i < n_buffers; ++i)
298 free(buffers[i].start);
305 static void init_read(unsigned int buffer_size)
307 buffers = calloc(1, sizeof(*buffers));
310 fprintf(stderr, "Out of memory\\n");
314 buffers[0].length = buffer_size;
315 buffers[0].start = malloc(buffer_size);
317 if (!buffers[0].start) {
318 fprintf(stderr, "Out of memory\\n");
323 static void init_mmap(void)
325 struct v4l2_requestbuffers req;
330 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
331 req.memory = V4L2_MEMORY_MMAP;
333 if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
334 if (EINVAL == errno) {
335 fprintf(stderr, "%s does not support "
336 "memory mappingn", dev_name);
339 errno_exit("VIDIOC_REQBUFS");
344 fprintf(stderr, "Insufficient buffer memory on %s\\n",
349 buffers = calloc(req.count, sizeof(*buffers));
352 fprintf(stderr, "Out of memory\\n");
356 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
357 struct v4l2_buffer buf;
361 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
362 buf.memory = V4L2_MEMORY_MMAP;
363 buf.index = n_buffers;
365 if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
366 errno_exit("VIDIOC_QUERYBUF");
368 buffers[n_buffers].length = buf.length;
369 buffers[n_buffers].start =
370 mmap(NULL /* start anywhere */,
372 PROT_READ | PROT_WRITE /* required */,
373 MAP_SHARED /* recommended */,
376 if (MAP_FAILED == buffers[n_buffers].start)
381 static void init_userp(unsigned int buffer_size)
383 struct v4l2_requestbuffers req;
388 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
389 req.memory = V4L2_MEMORY_USERPTR;
391 if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
392 if (EINVAL == errno) {
393 fprintf(stderr, "%s does not support "
394 "user pointer i/on", dev_name);
397 errno_exit("VIDIOC_REQBUFS");
401 buffers = calloc(4, sizeof(*buffers));
404 fprintf(stderr, "Out of memory\\n");
408 for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
409 buffers[n_buffers].length = buffer_size;
410 buffers[n_buffers].start = malloc(buffer_size);
412 if (!buffers[n_buffers].start) {
413 fprintf(stderr, "Out of memory\\n");
419 static void init_device(void)
421 struct v4l2_capability cap;
422 struct v4l2_cropcap cropcap;
423 struct v4l2_crop crop;
424 struct v4l2_format fmt;
427 if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
428 if (EINVAL == errno) {
429 fprintf(stderr, "%s is no V4L2 device\\n",
433 errno_exit("VIDIOC_QUERYCAP");
437 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
438 fprintf(stderr, "%s is no video capture device\\n",
445 if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
446 fprintf(stderr, "%s does not support read i/o\\n",
453 case IO_METHOD_USERPTR:
454 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
455 fprintf(stderr, "%s does not support streaming i/o\\n",
463 /* Select video input, video standard and tune here. */
468 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
470 if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
471 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
472 crop.c = cropcap.defrect; /* reset to default */
474 if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
477 /* Cropping not supported. */
480 /* Errors ignored. */
485 /* Errors ignored. */
491 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
493 fmt.fmt.pix.width = 640;
494 fmt.fmt.pix.height = 480;
495 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
496 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
498 if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
499 errno_exit("VIDIOC_S_FMT");
501 /* Note VIDIOC_S_FMT may change width and height. */
503 /* Preserve original settings as set by v4l2-ctl for example */
504 if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
505 errno_exit("VIDIOC_G_FMT");
508 /* Buggy driver paranoia. */
509 min = fmt.fmt.pix.width * 2;
510 if (fmt.fmt.pix.bytesperline < min)
511 fmt.fmt.pix.bytesperline = min;
512 min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
513 if (fmt.fmt.pix.sizeimage < min)
514 fmt.fmt.pix.sizeimage = min;
518 init_read(fmt.fmt.pix.sizeimage);
525 case IO_METHOD_USERPTR:
526 init_userp(fmt.fmt.pix.sizeimage);
531 static void close_device(void)
539 static void open_device(void)
543 if (-1 == stat(dev_name, &st)) {
544 fprintf(stderr, "Cannot identify '%s': %d, %s\\n",
545 dev_name, errno, strerror(errno));
549 if (!S_ISCHR(st.st_mode)) {
550 fprintf(stderr, "%s is no devicen", dev_name);
554 fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
557 fprintf(stderr, "Cannot open '%s': %d, %s\\n",
558 dev_name, errno, strerror(errno));
563 static void usage(FILE *fp, int argc, char **argv)
566 "Usage: %s [options]\\n\\n"
569 "-d | --device name Video device name [%s]n"
570 "-h | --help Print this messagen"
571 "-m | --mmap Use memory mapped buffers [default]n"
572 "-r | --read Use read() callsn"
573 "-u | --userp Use application allocated buffersn"
574 "-o | --output Outputs stream to stdoutn"
575 "-f | --format Force format to 640x480 YUYVn"
576 "-c | --count Number of frames to grab [%i]n"
578 argv[0], dev_name, frame_count);
581 static const char short_options[] = "d:hmruofc:";
583 static const struct option
585 { "device", required_argument, NULL, 'd' },
586 { "help", no_argument, NULL, 'h' },
587 { "mmap", no_argument, NULL, 'm' },
588 { "read", no_argument, NULL, 'r' },
589 { "userp", no_argument, NULL, 'u' },
590 { "output", no_argument, NULL, 'o' },
591 { "format", no_argument, NULL, 'f' },
592 { "count", required_argument, NULL, 'c' },
596 int main(int argc, char **argv)
598 dev_name = "/dev/video0";
604 c = getopt_long(argc, argv,
605 short_options, long_options, &idx);
611 case 0: /* getopt_long() flag */
619 usage(stdout, argc, argv);
631 io = IO_METHOD_USERPTR;
644 frame_count = strtol(optarg, NULL, 0);
650 usage(stderr, argc, argv);
662 fprintf(stderr, "\\n");