标签:des blog io os ar for sp 2014 art
抽空做了个集成v4l2api, 实力有限, 有粗糙的地方还望不吝赐教 ^_^
v4l2_lib.h
/* This driver provid v4l2 API * * (You can use this driver as abouve steps) * * * * STEP ONE: define value * * * step 1.1: define the pointer of your device's name * demo : static char *my_device = "/dev/video4"; * * step 1.2: define file descriptor of your device * demo : static int my_file_descriptor = -1; * * step 1.3: define ther buffer pointer of your buffers * demo : struct buffer *my_buffers; * * * * STEP TWO: call api function * * step 2.1: open the device by fun : open_device(my_device, &my_file_descriptor); * * step 2.2: init the device by fun : init_device(my_device, my_file_descriptor, my_buffers); * * step 2.3: start the device to capturing by fun : start_capturing(my_file_descriptor, my_buffers); * * step 2.4: call your process fun that hase be defined * you can grab a frame in your fun by call the fun : read_frame(my_file_descriptor, my_buffers) * and rewrite the fun process_image() in driver.c to implement your attention * * step 2.5: stop the device's capture by fun : stop_capturing(my_file_descriptor); * * step 2.6: uninit the device by fun : uninit_device(my_buffers); * * step 2.7: close the device bu fun : close_device(my_file_descriptor); * * * * FOR MORE CONFIGURATION CAN BE CONFIG BY UPDATE THE BELOW DEFINES * * METALSEED_TAG whether print log * * FRAME_QUEUE_LENGTH (the value of frame queue's length) * * V4L2_IO_METHOD (frame date exchange method) * * V4L2_FORCE_FORMAT (about format set : as bellow parameter) * * V4L2_FORCE_FORMAT_WIDTH * * V4L2_FORCE_FORMAT_HIGHT * * V4L2_FORCE_FORMAT_PIXEL_FORMAT * * V4L2_FORCE_FORMAT_FIELD * */ /* -----------demo----------- int main(int argc, char **argv) { static char *dev_eye1 = "/dev/video4"; static int fd = -1; struct buffer *buffers; open_device(dev_eye1, &fd); init_device(dev_eye1, fd, &buffers); start_capturing(fd, buffers); while(1){ if (read_frame(fd, buffers) == 1) break; } stop_capturing(fd); uninit_device(buffers); close_device(fd); return 0; } */ #ifndef __V4L2_DEVICE_DRIVER_H__ #define __V4L2_DEVICE_DRIVER_H__ enum io_method { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR, }; struct buffer { void *start; size_t length; }; #define METALSEED_TAG 1 // 1 print log, 0 don't print #define V4L2_IO_METHOD IO_METHOD_MMAP #define FRAME_QUEUE_LENGTH 4 //frame queue length #define V4L2_FORCE_FORMAT 1 #define V4L2_FORCE_FORMAT_WIDTH 1280 #define V4L2_FORCE_FORMAT_HIGHT 960 #define V4L2_FORCE_FORMAT_PIXEL_FORMAT V4L2_PIX_FMT_YUYV #define V4L2_FORCE_FORMAT_FIELD V4L2_FIELD_INTERLACED //You can save the frame to file_output by fwrite(frame_pointer, size, 1, file_output); static void process_image(const void *frame_pointer, int frame_size); static int read_frame(int file_descriptor, struct buffer *buffers); static void stop_capturing(int file_descriptor); static void start_capturing(int file_descriptor, struct buffer *buffers); static void init_read(struct buffer **buffers, unsigned int buffer_size); static void init_mmap(char *dev_name, int file_descriptor, struct buffer **buffers); static void init_userp(struct buffer **buffers, unsigned int buffer_size, char *dev_name, int file_descriptor); static void init_device(char *dev_name, int file_descriptor, struct buffer **buffers); static void uninit_device(struct buffer *buffers); static void open_device(char *dev_name, int *file_descriptor); static void close_device(int file_descriptor); #endif //__V4L2_DEVICE_DRIVER_H__
v4l2_lib.c
/* * V4L2 video capture driver * * This program can be used and distributed without restrictions. * * This program is provided with the V4L2 API * * This program is optimized by MetalSeed(mc.gaofei@gmail.com) */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <getopt.h> /* getopt_long() */ #include <fcntl.h> /* low-level i/o */ #include <unistd.h> #include <errno.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/time.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <asm/types.h> #include <linux/videodev2.h> #include "v4l2_lib.h" // #include <malloc.h> // #include <pthread.h> #define CLEAR(x) memset(&(x), 0, sizeof(x)) static unsigned int n_buffers; /*=== set io method ===*/ #ifdef V4L2_IO_METHOD static enum io_method io = V4L2_IO_METHOD; #endif #ifndef V4L2_IO_METHOD static enum io_method io = IO_METHOD_MMAP; #endif /*=== set force format switcher ===*/ #ifdef V4L2_FORCE_FORMAT static int force_format = V4L2_FORCE_FORMAT; #endif #ifndef V4L2_FORCE_FORMAT static int force_format = 0; #endif /* * * */ static void errno_exit(const char *s) { fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno)); exit(EXIT_FAILURE); } static int xioctl(int fh, int request, void *arg) { int r; do { r = ioctl(fh, request, arg); } while (-1 == r && EINTR == errno); return r; } /* * one operation * step1 : VIDIOC_STREAMOFF */ static void stop_capturing(int fd) { enum v4l2_buf_type type; switch (io) { case IO_METHOD_READ: /* Nothing to do. */ break; case IO_METHOD_MMAP: case IO_METHOD_USERPTR: type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type)) errno_exit("VIDIOC_STREAMOFF"); break; } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Stop capturing successfully\n"); } #endif } /* tow operations * step1 : VIDIOC_QBUF(insert buffer to queue) * step2 : VIDIOC_STREAMOFF */ static void start_capturing(int fd, struct buffer *buffers) { unsigned int i; enum v4l2_buf_type type; switch (io) { case IO_METHOD_READ: /* Nothing to do. */ break; case IO_METHOD_MMAP: for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) errno_exit("VIDIOC_QBUF"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) errno_exit("VIDIOC_STREAMON"); break; case IO_METHOD_USERPTR: for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_USERPTR; buf.index = i; buf.m.userptr = (unsigned long)buffers[i].start; buf.length = buffers[i].length; if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) errno_exit("VIDIOC_QBUF"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl(fd, VIDIOC_STREAMON, &type)) errno_exit("VIDIOC_STREAMON"); break; } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Start capturing successfully\n"); } #endif } /* two operations * step1 : munmap buffers * steo2 : free buffers */ static void uninit_device(struct buffer *buffers) { unsigned int i; switch (io) { case IO_METHOD_READ: free(buffers[0].start); break; case IO_METHOD_MMAP: for (i = 0; i < n_buffers; ++i) { if (-1 == munmap(buffers[i].start, buffers[i].length)) { errno_exit("munmap"); } } break; case IO_METHOD_USERPTR: for (i = 0; i < n_buffers; ++i) free(buffers[i].start); break; } free(buffers); #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Uninit device successfully\n"); } #endif } static void init_read(struct buffer **buffers, unsigned int buffer_size) { *buffers = calloc(1, sizeof(**buffers)); if (!(*buffers)) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } (*buffers)[0].length = buffer_size; (*buffers)[0].start = malloc(buffer_size); if (!(*buffers)[0].start) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Initialize read successfully\n"); } #endif } static void init_mmap(char *dev_name, int fd, struct buffer **buffers) { struct v4l2_requestbuffers req; CLEAR(req); req.count = FRAME_QUEUE_LENGTH; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) { if (EINVAL == errno) { fprintf(stderr, "%s does not support " "memory mapping\n", dev_name); exit(EXIT_FAILURE); } else { errno_exit("VIDIOC_REQBUFS"); } } if (req.count < 2) { fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name); exit(EXIT_FAILURE); } *buffers = calloc(req.count, sizeof(**buffers)); if (!(*buffers)) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { struct v4l2_buffer buf; CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = n_buffers; if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf)) errno_exit("VIDIOC_QUERYBUF"); (*buffers)[n_buffers].length = buf.length; (*buffers)[n_buffers].start = mmap(NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, fd, buf.m.offset); if (MAP_FAILED == (*buffers)[n_buffers].start) errno_exit("mmap"); } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Initialize memory mapping successfully\n"); } #endif } static void init_userp(struct buffer **buffers, unsigned int buffer_size, char *dev_name, int fd) { struct v4l2_requestbuffers req; CLEAR(req); req.count = FRAME_QUEUE_LENGTH; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_USERPTR; if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) { if (EINVAL == errno) { fprintf(stderr, "%s does not support " "user pointer i/o\n", dev_name); exit(EXIT_FAILURE); } else { errno_exit("VIDIOC_REQBUFS"); } } *buffers = calloc(FRAME_QUEUE_LENGTH, sizeof(struct buffer)); if (!(*buffers)) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } for (n_buffers = 0; n_buffers < FRAME_QUEUE_LENGTH; ++n_buffers) { (*buffers)[n_buffers].length = buffer_size; (*buffers)[n_buffers].start = malloc(buffer_size); if (!(*buffers)[n_buffers].start) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Initialize user pointer successfully\n"); } #endif } /* six operations * step1 : cap :query camera's capability and check it(is a video device? is it support read? is it support streaming?) * step2 : cropcap:set cropcap's type and get cropcap by VIDIOC_CROPCAP * step3 : set crop parameter by VIDIOC_S_CROP (such as frame type and angle) * step4 : set image format * step5 : set stream parameter * step6 : mmap */ static void init_device(char *dev_name, int fd, struct buffer **buffers) { struct v4l2_capability cap; struct v4l2_cropcap cropcap; struct v4l2_crop crop; struct v4l2_format fmt; struct v4l2_streamparm parm; unsigned int min; if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { if (EINVAL == errno) { fprintf(stderr, "%s is no V4L2 device\n", dev_name); exit(EXIT_FAILURE); } else { errno_exit("VIDIOC_QUERYCAP"); } } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { fprintf(stderr, "%s is no video capture device\n", dev_name); exit(EXIT_FAILURE); } switch (io) { case IO_METHOD_READ: if (!(cap.capabilities & V4L2_CAP_READWRITE)) { fprintf(stderr, "%s does not support read i/o\n", dev_name); exit(EXIT_FAILURE); } break; case IO_METHOD_MMAP: case IO_METHOD_USERPTR: if (!(cap.capabilities & V4L2_CAP_STREAMING)) { fprintf(stderr, "%s does not support streaming i/o\n", dev_name); exit(EXIT_FAILURE); } break; } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Get %s's capabilities successfully.\n", dev_name); printf("[ V4L2 CLUE ] : Driver Name : %s\n[ V4L2 CLUE ] : Card Name : %s\n[ V4L2 CLUE ] : Bus info : %s\n[ V4L2 CLUE ] : Driver Version : %u.%u.%u\n",cap.driver, cap.card, cap.bus_info,(cap.version >> 16) & 0XFF, (cap.version >> 8) & 0XFF, cap.version & 0XFF); } #endif /* not all capture support crop */ /* Select video input, video standard and tune here. */ CLEAR(cropcap); cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* if device support cropcap's type then set crop */ if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) { crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; crop.c = cropcap.defrect; /* reset to default */ if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) { switch (errno) { case EINVAL: /* Cropping not supported. */ break; default: /* Errors ignored. */ break; } } } else { /* Errors ignored. */ } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Set cropcap successfully.\n"); //could add some cropcap log. } #endif #if(METALSEED_TAG == 1) { struct v4l2_fmtdesc fmtdesc; fmtdesc.index = 0; fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; printf("[ V4L2 CLUE ] : Format that this device supported:\n"); while(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != -1) { printf("[ V4L2 CLUE ] : \t%d.%s\n", fmtdesc.index + 1, fmtdesc.description); fmtdesc.index++; } } #endif CLEAR(fmt); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (force_format) { fmt.fmt.pix.width = V4L2_FORCE_FORMAT_WIDTH; fmt.fmt.pix.height = V4L2_FORCE_FORMAT_HIGHT; fmt.fmt.pix.pixelformat = V4L2_FORCE_FORMAT_PIXEL_FORMAT; fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) errno_exit("VIDIOC_S_FMT"); #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Set format by force successfully.\n"); printf("[ V4L2 CLUE ] : Width = %d\n" , fmt.fmt.pix.width); printf("[ V4L2 CLUE ] : Height = %d\n" , fmt.fmt.pix.height); } #endif /* Note VIDIOC_S_FMT may change width and height. */ } else { /* Preserve original settings as set by v4l2-ctl for example */ if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)) errno_exit("VIDIOC_G_FMT"); } /* Buggy driver paranoia. */ min = fmt.fmt.pix.width * 2; if (fmt.fmt.pix.bytesperline < min) fmt.fmt.pix.bytesperline = min; min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; if (fmt.fmt.pix.sizeimage < min) fmt.fmt.pix.sizeimage = min; #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Image size = %d\n" , fmt.fmt.pix.sizeimage); printf("[ V4L2 CLUE ] : Bytes per line = %d\n" , fmt.fmt.pix.bytesperline); } #endif CLEAR(parm); parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; parm.parm.capture.capturemode = 0; parm.parm.capture.capability = V4L2_CAP_TIMEPERFRAME; parm.parm.capture.timeperframe.denominator = 30; //鏃堕棿闂撮殧鍒嗘瘝 parm.parm.capture.timeperframe.numerator = 1; //鍒嗗瓙 if(-1 == xioctl(fd, VIDIOC_S_PARM, &parm)) { errno_exit("VIDIOC_S_PARM"); } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Set stream parameter successfully\n"); printf("[ V4L2 CLUE ] : Stream capability = V4L2_CAP_TIMEPERFRAME\n"); printf("[ V4L2 CLUE ] : Stream timeperframe's donominator = %d\n", parm.parm.capture.timeperframe.denominator); printf("[ V4L2 CLUE ] : Stream timeperframe's numerator = %d\n", parm.parm.capture.timeperframe.numerator ); } #endif switch (io) { case IO_METHOD_READ: init_read(buffers, fmt.fmt.pix.sizeimage); break; case IO_METHOD_MMAP: init_mmap(dev_name, fd, buffers); break; case IO_METHOD_USERPTR: init_userp(buffers, fmt.fmt.pix.sizeimage, dev_name, fd); break; } } /* * close (fd) */ static void close_device(int fd) { if (-1 == close(fd)) errno_exit("close"); fd = -1; #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Close device successfully\n"); } #endif } /* three operations * step 1 : check dev_name and st_mode * step 2 : open(device) */ static void open_device(char *dev_name, int *fd) { struct stat st; if (-1 == stat(dev_name, &st)) { fprintf(stderr, "Cannot identify '%s': %d, %s\n", dev_name, errno, strerror(errno)); exit(EXIT_FAILURE); } if (!S_ISCHR(st.st_mode)) { fprintf(stderr, "%s is no device\n", dev_name); exit(EXIT_FAILURE); } *fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0); if (-1 == *fd) { fprintf(stderr, "Cannot open '%s': %d, %s\n", dev_name, errno, strerror(errno)); exit(EXIT_FAILURE); } #if(METALSEED_TAG == 1) { printf("[ V4L2 CLUE ] : Open %s successfully. fd = %d\n", dev_name, fd); } #endif } static void process_image(const void *frame_pointer, int size) { //fwrite(frame_pointer, size, 1, file_output); save this frame to file_output puts("An image has been process over!"); #if(METALSEED_TAG == 1) { printf("\n[ V4L2 CLUE ] : Process inmage successfully\n"); } #endif } static ctr = 0; static int read_frame(int fd, struct buffer *buffers) { struct v4l2_buffer buf; unsigned int i; switch (io) { case IO_METHOD_READ: if (-1 == read(fd, buffers[0].start, buffers[0].length)) { switch (errno) { case EAGAIN: return 0; case EIO: /* Could ignore EIO, see spec. */ /* fall through */ default: errno_exit("read"); } } process_image(buffers[0].start, buffers[0].length); break; case IO_METHOD_MMAP: CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { switch (errno) { case EAGAIN: return 0; case EIO: /* Could ignore EIO, see spec. */ /* fall through */ default: errno_exit("VIDIOC_DQBUF"); } } assert(buf.index < n_buffers); process_image(buffers[buf.index].start, buf.bytesused); if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) errno_exit("VIDIOC_QBUF"); break; case IO_METHOD_USERPTR: CLEAR(buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_USERPTR; if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { switch (errno) { case EAGAIN: return 0; case EIO: /* Could ignore EIO, see spec. */ /* fall through */ default: errno_exit("VIDIOC_DQBUF"); } } for (i = 0; i < n_buffers; ++i) if (buf.m.userptr == (unsigned long)buffers[i].start && buf.length == buffers[i].length) break; assert(i < n_buffers); process_image((void *)buf.m.userptr, buf.bytesused); if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) errno_exit("VIDIOC_QBUF"); break; } #if(METALSEED_TAG == 1) { printf("\n[ V4L2 CLUE ] : Read frame successfully\n"); } #endif return 1; }
标签:des blog io os ar for sp 2014 art
原文地址:http://blog.csdn.net/metalseed/article/details/39998107