2
* V4L2 video capture example
4
* This program can be used and distributed without restrictions.
6
* This program is provided with the V4L2 API
7
* see http://linuxtv.org/docs.php for more information
15
#include <getopt.h> /* getopt_long() */
17
#include <fcntl.h> /* low-level i/o */
21
#include <sys/types.h>
24
#include <sys/ioctl.h>
26
#include <linux/videodev2.h>
28
#define CLEAR(x) memset(&(x), 0, sizeof(x))
41
static char *dev_name;
42
static enum io_method io = IO_METHOD_MMAP;
44
struct buffer *buffers;
45
static unsigned int n_buffers;
47
static int force_format;
48
static int frame_count = 70;
50
static void errno_exit(const char *s)
52
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
56
static int xioctl(int fh, unsigned long int request, void *arg)
61
r = ioctl(fh, request, arg);
62
} while (-1 == r && EINTR == errno);
67
static void process_image(const void *p, int size)
70
fwrite(p, size, 1, stdout);
77
static int read_frame(void)
79
struct v4l2_buffer buf;
84
if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
90
/* Could ignore EIO, see spec. */
99
process_image(buffers[0].start, buffers[0].length);
105
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
106
buf.memory = V4L2_MEMORY_MMAP;
108
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
114
/* Could ignore EIO, see spec. */
119
errno_exit("VIDIOC_DQBUF");
123
assert(buf.index < n_buffers);
125
process_image(buffers[buf.index].start, buf.bytesused);
127
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
128
errno_exit("VIDIOC_QBUF");
131
case IO_METHOD_USERPTR:
134
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
135
buf.memory = V4L2_MEMORY_USERPTR;
137
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
143
/* Could ignore EIO, see spec. */
148
errno_exit("VIDIOC_DQBUF");
152
for (i = 0; i < n_buffers; ++i)
153
if (buf.m.userptr == (unsigned long)buffers[i].start
154
&& buf.length == buffers[i].length)
157
assert(i < n_buffers);
159
process_image((void *)buf.m.userptr, buf.bytesused);
161
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
162
errno_exit("VIDIOC_QBUF");
169
static void mainloop(void)
175
while (count-- > 0) {
188
r = select(fd + 1, &fds, NULL, NULL, &tv);
193
errno_exit("select");
197
fprintf(stderr, "select timeout\n");
203
/* EAGAIN - continue select loop. */
208
static void stop_capturing(void)
210
enum v4l2_buf_type type;
218
case IO_METHOD_USERPTR:
219
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
220
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
221
errno_exit("VIDIOC_STREAMOFF");
226
static void start_capturing(void)
229
enum v4l2_buf_type type;
237
for (i = 0; i < n_buffers; ++i) {
238
struct v4l2_buffer buf;
241
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
242
buf.memory = V4L2_MEMORY_MMAP;
245
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
246
errno_exit("VIDIOC_QBUF");
248
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
249
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
250
errno_exit("VIDIOC_STREAMON");
253
case IO_METHOD_USERPTR:
254
for (i = 0; i < n_buffers; ++i) {
255
struct v4l2_buffer buf;
258
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
259
buf.memory = V4L2_MEMORY_USERPTR;
261
buf.m.userptr = (unsigned long)buffers[i].start;
262
buf.length = buffers[i].length;
264
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
265
errno_exit("VIDIOC_QBUF");
267
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
268
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
269
errno_exit("VIDIOC_STREAMON");
274
static void uninit_device(void)
280
free(buffers[0].start);
284
for (i = 0; i < n_buffers; ++i)
285
if (-1 == munmap(buffers[i].start, buffers[i].length))
286
errno_exit("munmap");
289
case IO_METHOD_USERPTR:
290
for (i = 0; i < n_buffers; ++i)
291
free(buffers[i].start);
298
static void init_read(unsigned int buffer_size)
300
buffers = calloc(1, sizeof(*buffers));
303
fprintf(stderr, "Out of memory\n");
307
buffers[0].length = buffer_size;
308
buffers[0].start = malloc(buffer_size);
310
if (!buffers[0].start) {
311
fprintf(stderr, "Out of memory\n");
316
static void init_mmap(void)
318
struct v4l2_requestbuffers req;
323
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
324
req.memory = V4L2_MEMORY_MMAP;
326
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
327
if (EINVAL == errno) {
328
fprintf(stderr, "%s does not support "
329
"memory mapping\n", dev_name);
332
errno_exit("VIDIOC_REQBUFS");
337
fprintf(stderr, "Insufficient buffer memory on %s\n",
342
buffers = calloc(req.count, sizeof(*buffers));
345
fprintf(stderr, "Out of memory\n");
349
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
350
struct v4l2_buffer buf;
354
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
355
buf.memory = V4L2_MEMORY_MMAP;
356
buf.index = n_buffers;
358
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
359
errno_exit("VIDIOC_QUERYBUF");
361
buffers[n_buffers].length = buf.length;
362
buffers[n_buffers].start =
363
mmap(NULL /* start anywhere */,
365
PROT_READ | PROT_WRITE /* required */,
366
MAP_SHARED /* recommended */,
369
if (MAP_FAILED == buffers[n_buffers].start)
374
static void init_userp(unsigned int buffer_size)
376
struct v4l2_requestbuffers req;
381
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
382
req.memory = V4L2_MEMORY_USERPTR;
384
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
385
if (EINVAL == errno) {
386
fprintf(stderr, "%s does not support "
387
"user pointer i/o\n", dev_name);
390
errno_exit("VIDIOC_REQBUFS");
394
buffers = calloc(4, sizeof(*buffers));
397
fprintf(stderr, "Out of memory\n");
401
for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
402
buffers[n_buffers].length = buffer_size;
403
buffers[n_buffers].start = malloc(buffer_size);
405
if (!buffers[n_buffers].start) {
406
fprintf(stderr, "Out of memory\n");
412
static void init_device(void)
414
struct v4l2_capability cap;
415
struct v4l2_cropcap cropcap;
416
struct v4l2_crop crop;
417
struct v4l2_format fmt;
420
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
421
if (EINVAL == errno) {
422
fprintf(stderr, "%s is no V4L2 device\n",
426
errno_exit("VIDIOC_QUERYCAP");
430
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
431
fprintf(stderr, "%s is no video capture device\n",
438
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
439
fprintf(stderr, "%s does not support read i/o\n",
446
case IO_METHOD_USERPTR:
447
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
448
fprintf(stderr, "%s does not support streaming i/o\n",
456
/* Select video input, video standard and tune here. */
461
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
463
if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
464
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
465
crop.c = cropcap.defrect; /* reset to default */
467
if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
470
/* Cropping not supported. */
473
/* Errors ignored. */
478
/* Errors ignored. */
484
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
486
fmt.fmt.pix.width = 640;
487
fmt.fmt.pix.height = 480;
488
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
489
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
491
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
492
errno_exit("VIDIOC_S_FMT");
494
/* Note VIDIOC_S_FMT may change width and height. */
496
/* Preserve original settings as set by v4l2-ctl for example */
497
if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
498
errno_exit("VIDIOC_G_FMT");
501
/* Buggy driver paranoia. */
502
min = fmt.fmt.pix.width * 2;
503
if (fmt.fmt.pix.bytesperline < min)
504
fmt.fmt.pix.bytesperline = min;
505
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
506
if (fmt.fmt.pix.sizeimage < min)
507
fmt.fmt.pix.sizeimage = min;
511
init_read(fmt.fmt.pix.sizeimage);
518
case IO_METHOD_USERPTR:
519
init_userp(fmt.fmt.pix.sizeimage);
524
static void close_device(void)
532
static void open_device(void)
536
if (-1 == stat(dev_name, &st)) {
537
fprintf(stderr, "Cannot identify '%s': %d, %s\n",
538
dev_name, errno, strerror(errno));
542
if (!S_ISCHR(st.st_mode)) {
543
fprintf(stderr, "%s is no device\n", dev_name);
547
fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
550
fprintf(stderr, "Cannot open '%s': %d, %s\n",
551
dev_name, errno, strerror(errno));
556
static void usage(FILE *fp, int argc, char **argv)
559
"Usage: %s [options]\n\n"
562
"-d | --device name Video device name [%s]\n"
563
"-h | --help Print this message\n"
564
"-m | --mmap Use memory mapped buffers [default]\n"
565
"-r | --read Use read() calls\n"
566
"-u | --userp Use application allocated buffers\n"
567
"-o | --output Outputs stream to stdout\n"
568
"-f | --format Force format to 640x480 YUYV\n"
569
"-c | --count Number of frames to grab [%i]\n"
571
argv[0], dev_name, frame_count);
574
static const char short_options[] = "d:hmruofc:";
576
static const struct option
578
{ "device", required_argument, NULL, 'd' },
579
{ "help", no_argument, NULL, 'h' },
580
{ "mmap", no_argument, NULL, 'm' },
581
{ "read", no_argument, NULL, 'r' },
582
{ "userp", no_argument, NULL, 'u' },
583
{ "output", no_argument, NULL, 'o' },
584
{ "format", no_argument, NULL, 'f' },
585
{ "count", required_argument, NULL, 'c' },
589
int main(int argc, char **argv)
591
dev_name = "/dev/video0";
597
c = getopt_long(argc, argv,
598
short_options, long_options, &idx);
604
case 0: /* getopt_long() flag */
612
usage(stdout, argc, argv);
624
io = IO_METHOD_USERPTR;
637
frame_count = strtol(optarg, NULL, 0);
643
usage(stderr, argc, argv);
655
fprintf(stderr, "\n");