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/userspace-api/media/fdl-appendix.rst.
8 .. TODO: replace it to GFDL-1.1-or-later WITH no-invariant-sections
10 file: media/v4l/capture.c
11 =========================
16 * V4L2 video capture example
18 * This program can be used and distributed without restrictions.
20 * This program is provided with the V4L2 API
21 * see https://linuxtv.org/docs.php for more information
29 #include <getopt.h> /* getopt_long() */
31 #include <fcntl.h> /* low-level i/o */
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
40 #include <linux/videodev2.h>
42 #define CLEAR(x) memset(&(x), 0, sizeof(x))
55 static char *dev_name;
56 static enum io_method io = IO_METHOD_MMAP;
58 struct buffer *buffers;
59 static unsigned int n_buffers;
61 static int force_format;
62 static int frame_count = 70;
64 static void errno_exit(const char *s)
66 fprintf(stderr, "%s error %d, %s\\n", s, errno, strerror(errno));
70 static int xioctl(int fh, int request, void *arg)
75 r = ioctl(fh, request, arg);
76 } while (-1 == r && EINTR == errno);
81 static void process_image(const void *p, int size)
84 fwrite(p, size, 1, stdout);
91 static int read_frame(void)
93 struct v4l2_buffer buf;
98 if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
104 /* Could ignore EIO, see spec. */
113 process_image(buffers[0].start, buffers[0].length);
119 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
120 buf.memory = V4L2_MEMORY_MMAP;
122 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
128 /* Could ignore EIO, see spec. */
133 errno_exit("VIDIOC_DQBUF");
137 assert(buf.index < n_buffers);
139 process_image(buffers[buf.index].start, buf.bytesused);
141 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
142 errno_exit("VIDIOC_QBUF");
145 case IO_METHOD_USERPTR:
148 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
149 buf.memory = V4L2_MEMORY_USERPTR;
151 if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
157 /* Could ignore EIO, see spec. */
162 errno_exit("VIDIOC_DQBUF");
166 for (i = 0; i < n_buffers; ++i)
167 if (buf.m.userptr == (unsigned long)buffers[i].start
168 && buf.length == buffers[i].length)
171 assert(i < n_buffers);
173 process_image((void *)buf.m.userptr, buf.bytesused);
175 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
176 errno_exit("VIDIOC_QBUF");
183 static void mainloop(void)
189 while (count-- > 0) {
202 r = select(fd + 1, &fds, NULL, NULL, &tv);
207 errno_exit("select");
211 fprintf(stderr, "select timeout\\n");
217 /* EAGAIN - continue select loop. */
222 static void stop_capturing(void)
224 enum v4l2_buf_type type;
232 case IO_METHOD_USERPTR:
233 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
234 if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
235 errno_exit("VIDIOC_STREAMOFF");
240 static void start_capturing(void)
243 enum v4l2_buf_type type;
251 for (i = 0; i < n_buffers; ++i) {
252 struct v4l2_buffer buf;
255 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
256 buf.memory = V4L2_MEMORY_MMAP;
259 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
260 errno_exit("VIDIOC_QBUF");
262 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
263 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
264 errno_exit("VIDIOC_STREAMON");
267 case IO_METHOD_USERPTR:
268 for (i = 0; i < n_buffers; ++i) {
269 struct v4l2_buffer buf;
272 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
273 buf.memory = V4L2_MEMORY_USERPTR;
275 buf.m.userptr = (unsigned long)buffers[i].start;
276 buf.length = buffers[i].length;
278 if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
279 errno_exit("VIDIOC_QBUF");
281 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
282 if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
283 errno_exit("VIDIOC_STREAMON");
288 static void uninit_device(void)
294 free(buffers[0].start);
298 for (i = 0; i < n_buffers; ++i)
299 if (-1 == munmap(buffers[i].start, buffers[i].length))
300 errno_exit("munmap");
303 case IO_METHOD_USERPTR:
304 for (i = 0; i < n_buffers; ++i)
305 free(buffers[i].start);
312 static void init_read(unsigned int buffer_size)
314 buffers = calloc(1, sizeof(*buffers));
317 fprintf(stderr, "Out of memory\\n");
321 buffers[0].length = buffer_size;
322 buffers[0].start = malloc(buffer_size);
324 if (!buffers[0].start) {
325 fprintf(stderr, "Out of memory\\n");
330 static void init_mmap(void)
332 struct v4l2_requestbuffers req;
337 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
338 req.memory = V4L2_MEMORY_MMAP;
340 if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
341 if (EINVAL == errno) {
342 fprintf(stderr, "%s does not support "
343 "memory mappingn", dev_name);
346 errno_exit("VIDIOC_REQBUFS");
351 fprintf(stderr, "Insufficient buffer memory on %s\\n",
356 buffers = calloc(req.count, sizeof(*buffers));
359 fprintf(stderr, "Out of memory\\n");
363 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
364 struct v4l2_buffer buf;
368 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
369 buf.memory = V4L2_MEMORY_MMAP;
370 buf.index = n_buffers;
372 if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
373 errno_exit("VIDIOC_QUERYBUF");
375 buffers[n_buffers].length = buf.length;
376 buffers[n_buffers].start =
377 mmap(NULL /* start anywhere */,
379 PROT_READ | PROT_WRITE /* required */,
380 MAP_SHARED /* recommended */,
383 if (MAP_FAILED == buffers[n_buffers].start)
388 static void init_userp(unsigned int buffer_size)
390 struct v4l2_requestbuffers req;
395 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
396 req.memory = V4L2_MEMORY_USERPTR;
398 if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
399 if (EINVAL == errno) {
400 fprintf(stderr, "%s does not support "
401 "user pointer i/on", dev_name);
404 errno_exit("VIDIOC_REQBUFS");
408 buffers = calloc(4, sizeof(*buffers));
411 fprintf(stderr, "Out of memory\\n");
415 for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
416 buffers[n_buffers].length = buffer_size;
417 buffers[n_buffers].start = malloc(buffer_size);
419 if (!buffers[n_buffers].start) {
420 fprintf(stderr, "Out of memory\\n");
426 static void init_device(void)
428 struct v4l2_capability cap;
429 struct v4l2_cropcap cropcap;
430 struct v4l2_crop crop;
431 struct v4l2_format fmt;
434 if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
435 if (EINVAL == errno) {
436 fprintf(stderr, "%s is no V4L2 device\\n",
440 errno_exit("VIDIOC_QUERYCAP");
444 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
445 fprintf(stderr, "%s is no video capture device\\n",
452 if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
453 fprintf(stderr, "%s does not support read i/o\\n",
460 case IO_METHOD_USERPTR:
461 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
462 fprintf(stderr, "%s does not support streaming i/o\\n",
470 /* Select video input, video standard and tune here. */
475 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
477 if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
478 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
479 crop.c = cropcap.defrect; /* reset to default */
481 if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
484 /* Cropping not supported. */
487 /* Errors ignored. */
492 /* Errors ignored. */
498 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
500 fmt.fmt.pix.width = 640;
501 fmt.fmt.pix.height = 480;
502 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
503 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
505 if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
506 errno_exit("VIDIOC_S_FMT");
508 /* Note VIDIOC_S_FMT may change width and height. */
510 /* Preserve original settings as set by v4l2-ctl for example */
511 if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
512 errno_exit("VIDIOC_G_FMT");
515 /* Buggy driver paranoia. */
516 min = fmt.fmt.pix.width * 2;
517 if (fmt.fmt.pix.bytesperline < min)
518 fmt.fmt.pix.bytesperline = min;
519 min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
520 if (fmt.fmt.pix.sizeimage < min)
521 fmt.fmt.pix.sizeimage = min;
525 init_read(fmt.fmt.pix.sizeimage);
532 case IO_METHOD_USERPTR:
533 init_userp(fmt.fmt.pix.sizeimage);
538 static void close_device(void)
546 static void open_device(void)
550 if (-1 == stat(dev_name, &st)) {
551 fprintf(stderr, "Cannot identify '%s': %d, %s\\n",
552 dev_name, errno, strerror(errno));
556 if (!S_ISCHR(st.st_mode)) {
557 fprintf(stderr, "%s is no devicen", dev_name);
561 fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
564 fprintf(stderr, "Cannot open '%s': %d, %s\\n",
565 dev_name, errno, strerror(errno));
570 static void usage(FILE *fp, int argc, char **argv)
573 "Usage: %s [options]\\n\\n"
576 "-d | --device name Video device name [%s]n"
577 "-h | --help Print this messagen"
578 "-m | --mmap Use memory mapped buffers [default]n"
579 "-r | --read Use read() callsn"
580 "-u | --userp Use application allocated buffersn"
581 "-o | --output Outputs stream to stdoutn"
582 "-f | --format Force format to 640x480 YUYVn"
583 "-c | --count Number of frames to grab [%i]n"
585 argv[0], dev_name, frame_count);
588 static const char short_options[] = "d:hmruofc:";
590 static const struct option
592 { "device", required_argument, NULL, 'd' },
593 { "help", no_argument, NULL, 'h' },
594 { "mmap", no_argument, NULL, 'm' },
595 { "read", no_argument, NULL, 'r' },
596 { "userp", no_argument, NULL, 'u' },
597 { "output", no_argument, NULL, 'o' },
598 { "format", no_argument, NULL, 'f' },
599 { "count", required_argument, NULL, 'c' },
603 int main(int argc, char **argv)
605 dev_name = "/dev/video0";
611 c = getopt_long(argc, argv,
612 short_options, long_options, &idx);
618 case 0: /* getopt_long() flag */
626 usage(stdout, argc, argv);
638 io = IO_METHOD_USERPTR;
651 frame_count = strtol(optarg, NULL, 0);
657 usage(stderr, argc, argv);
669 fprintf(stderr, "\\n");