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_FPS 30
#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);
void process_image(FILE * file_pionter,const void *frame_pointer, int frame_size);
int read_frame(int file_descriptor, struct buffer *buffers, FILE * file_pionter);
void stop_capturing(int file_descriptor);
void start_capturing(int file_descriptor, struct buffer *buffers);
void init_read(struct buffer **buffers, unsigned int buffer_size);
void init_mmap(char *dev_name, int file_descriptor, struct buffer **buffers);
void init_userp(struct buffer **buffers, unsigned int buffer_size, char *dev_name, int file_descriptor);
void init_device(char *dev_name, int file_descriptor, struct buffer **buffers);
void uninit_device(struct buffer *buffers);
void open_device(char *dev_name, int *file_descriptor);
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 "camera_api.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