您的位置:首页 > 运维架构

基于v4l2的视频监控

2016-07-24 15:01 337 查看
1. v4l2简介:

V4L2有一段历史了。大约在1998的秋天,它的光芒第一次出现在Bill Dirks 的眼中。经过长足的发展,它于2002年11 月,发布2.5.46 时,融入了内核主干之中。然而直到今天,仍有一部分内核驱动不支持新的API,这种新旧API 的转换工作仍在进行。同时,V4L2 API也在发展,并在2.6.18 版本中进行了一些重大的改变。支持V4L2的应用依旧相对较少。V4L2在设计时,是要支持很多广泛的设备的,它们之中只有一部分在本质上是真正的视频设备:

可以支持多种设备,它可以有以下几种接口:

1. 视频采集接口(video capture interface):这种应用的设备可以是高频头或者摄像头.V4L2的最初设计就是应用于这种功能的.

2. 视频输出接口(video output interface):可以驱动计算机的外围视频图像设备--像可以输出电视信号格式的设备.

3. 直接传输视频接口(video overlay interface):它的主要工作是把从视频采集设备采集过来的信号直接输出到输出设备之上,而不用经过系统的CPU.

4. 视频间隔消隐信号接口(VBI interface):它可以使应用可以访问传输消隐期的视频信号.

5. 收音机接口(radio interface):可用来处理从AM或FM高频头设备接收来的音频流.

2. v4l2常用命令标识符

VIDIOC_REQBUFS:分配内存

VIDIOC_QUERYBUF:把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址

VIDIOC_QUERYCAP:查询驱动功能

VIDIOC_ENUM_FMT:获取当前驱动支持的视频格式

VIDIOC_S_FMT:设置当前驱动的频捕获格式

VIDIOC_G_FMT:读取当前驱动的频捕获格式

VIDIOC_TRY_FMT:验证当前驱动的显示格式

VIDIOC_CROPCAP:查询驱动的修剪能力

VIDIOC_S_CROP:设置视频信号的边框

VIDIOC_G_CROP:读取视频信号的边框

VIDIOC_QBUF:把数据放回缓存队列

VIDIOC_DQBUF:把数据从缓存中读取出来

VIDIOC_STREAMON:开始视频显示函数

VIDIOC_STREAMOFF:结束视频显示函数

VIDIOC_QUERYSTD:检查当前视频设备支持的标准,例如PAL或NTSC。

废话少说,直接上代码吧


开发环境: 

   1. pc机: ubuntu-12.04

   2. 交叉编译工具: arm-linux-gcc

   3. 开发板: 飞凌FL2440

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#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 <linux/fb.h>
#define CLEAR(x) memset (&(x), 0, sizeof (x))

struct buffer {
void * start;
size_t length;
};

static char * dev_name = NULL;
static int fd = -1;
struct buffer * buffers = NULL;
static unsigned int n_buffers = 0;
static int time_in_sec_capture=5;
static int fbfd = -1;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static char *fbp=NULL;
static long screensize=0;

inline int clip(int value, int min, int max) 
{
return (value > max ? max : value < min ? min : value);
}

/*转换成图片显示在LCD上*/
static void process_image (const void * p)
{

//ConvertYUVToRGB32

unsigned char* in=(char*)p;
int width=320;
int height=240;
int istride=width*2;
int x,y,j;
int y0,u,y1,v,r,g,b;
long location=0;

for ( y = 10; y < height + 10; ++y) 
{
for (j = 0, x=10; j < width * 2 ; j += 4,x +=2) 
{
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;

y0 = in[j];
u = in[j + 1] - 128;
y1 = in[j + 2];
v = in[j + 3] - 128;

r = (298 * y0 + 409 * v + 128) >> 8;
g = (298 * y0 - 100 * u - 208 * v + 128) >> 8;
b = (298 * y0 + 516 * u + 128) >> 8;

fbp[ location + 0] = ((clip(g, 0, 255)&0x1c)<< 3 ) | ( clip(b, 0, 255)  >> 3 );
fbp[ location + 1] = ((clip(r, 0, 255) & 0xf8) ) | ( clip(g, 0, 255)>> 5);

r = (298 * y1 + 409 * v + 128) >> 8;
g = (298 * y1 - 100 * u - 208 * v + 128) >> 8;
b = (298 * y1 + 516 * u + 128) >> 8;

fbp[ location + 2] = ((clip(g, 0, 255)&0x1c)<< 3 ) | ( clip(b, 0, 255)  >> 3 );
fbp[ location + 3] = ((clip(r, 0, 255) & 0xf8) ) | ( clip(g, 0, 255)>> 5);
}
in +=istride;
}

}
/*函数功能:
*读取一帧数据
*/
static int read_frame (void)
{
struct v4l2_buffer buf;
unsigned int i;

CLEAR (buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
//把数据从缓存中读取出来
if (ioctl (fd, VIDIOC_DQBUF, &buf) < 0)
{
printf("ioctl VIDIOC_DQBUF error!\n");
exit(-1);
}

assert(buf.index < n_buffers);

//  printf("v4l2_pix_format->field(%d)\n", buf.field);
//assert (buf.field ==V4L2_FIELD_NONE);

process_image (buffers[buf.index].start);
//VIDIOC_QBUF:把数据放回缓存队列
if (ioctl (fd, VIDIOC_QBUF, &buf) < 0)
{
printf("VIDIOC_QBUF");
exit(-1);
}
return 1;
}
/*
*开始视频显示
*/
static int run (void)
{
unsigned int count;
int frames;
frames = 30 * time_in_sec_capture;
printf("The number is the one!\n");

while (frames-- > 0)
{
printf("The frames is the %d\n",frames);
for (;;)
{
#if 0
fd_set fds;
struct timeval tv;
int r;
FD_ZERO (&fds);
FD_SET (fd, &fds);

tv.tv_sec = 2;
tv.tv_usec = 0;
//	printf("The number is the one!\n");
r = select (fd + 1, &fds, NULL, NULL, &tv);
//printf("r = %d\n", r);
if (-1 == r)
{
if (EINTR == errno)
continue;
printf("select");
}

if (0 == r)
{
printf ("select timeout\n");
return -1;
}
#endif
if (read_frame())
break;

}
}
return 0;
}

static int stop_capturing (void)
{
enum v4l2_buf_type type;

type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl (fd, VIDIOC_STREAMOFF, &type) < 0)
{
printf("ioctl VIDIOC_STREAMOFF error!\n");
return -1;
}
return 0;
}
/*
*函数功能:开始视频捕捉
*VIDIOC_STREAMON
*/
static int start_capturing (void)
{
#if 0
unsigned int i;

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 (ioctl (fd, VIDIOC_QBUF, &buf) < 0)
{
printf("ioctl VIDIOC_QBUF error!\n");
return -1;
}
}
#endif
enum v4l2_buf_type type;

type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
//开始视频显示函数
if (ioctl (fd, VIDIOC_STREAMON, &type) < 0)
{
printf("ioctl VIDIOC_STREAMON error!\n");
return -1;
}
return 0;

}
/*
*函数功能:内存释放
*/
static int uninit_device (void)
{
unsigned int i;

for (i = 0; i < n_buffers; ++i)
{
if (-1 == munmap (buffers[i].start, buffers[i].length))
{
printf("ioctl VIDIOC_STREAMON error!\n");
return -1;
}
}

if (-1 == munmap(fbp, screensize))
{
printf(" Error: framebuffer device munmap() failed.\n");
return -1;
}
free (buffers);
return 0;
}

/*函数功能:内存的操作
*1.frambuffer内存区映射,数据放到fbp指向的地址,LCD上才有视频图像出来
*2.分配缓冲区
*3.分配的数据缓存转换成物理地址
*4.把数据放回缓存队列
*/
static int init_mmap (void)
{
struct v4l2_requestbuffers req;

//mmap framebuffer
fbp = (char *)mmap(NULL,screensize,PROT_READ | PROT_WRITE,MAP_SHARED ,fbfd, 0);//分配显存
if ((int)fbp == -1)
{
printf("Error: failed to map framebuffer device to memory.\n");
return -1 ;
}
memset(fbp, 0, screensize);
CLEAR (req);

req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
//分配内存
if (ioctl (fd, VIDIOC_REQBUFS, &req) < 0)
{
printf("ioctl VIDIOC_REQBUFS error!\n");
return -1;
}

if (req.count < 4)
{    //if (req.count < 2)
printf ("Insufficient buffer memory on %s\n",dev_name);
return -1;
}

buffers = calloc (req.count, sizeof (*buffers));

if (!buffers)
{
printf("Out of memory\n");
return -1;
}

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;
//把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址
if (ioctl (fd, VIDIOC_QUERYBUF, &buf) < 0)
{
printf("ioctl VIDIOC_QUERYBUF error!\n");
return -1;
}
buffers[n_buffers].length = buf.length;
buffers[n_buffers].start =mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);

if (MAP_FAILED == buffers[n_buffers].start)
{
printf("mmap error!\n");
return -1;
}
}
unsigned int i;

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 (ioctl (fd, VIDIOC_QBUF, &buf) < 0)
{
printf("ioctl VIDIOC_QBUF error!\n");
return -1;
}
}
return 0;
}

/*
*函数功能:
*1.frambuffer的初始化,获得一些硬件信息
*		  如:LCD的像素大小
*2.摄像头的初始化 VIDIOC_S_FMT VIDIOC_REQBUFS VIDIOC_QUERYBUF
*/
static int init_device (void)
{
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
unsigned int min;

// Get fixed screen information

if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) < 0)
{
printf("Error reading fixed information.\n");
return -1;
}

// Get variable screen information

if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) < 0)
{
printf("Error reading variable information.\n");
return -1;
}

screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;//获得设备LCD像素的大小

printf("vinfo: xoffset:%d  yoffset:%d bits_per_pixel:%d xres:%d yres:%d\n",vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel, vinfo.xres, vinfo.yres);
printf("finfo: line_length:%d  screensize:%d\n", finfo.line_length, screensize);

//首先使用 VIDIOC_QUERYCAP 命令 来获得当前设备的各个属性放在结构体cap中
if (ioctl (fd, VIDIOC_QUERYCAP, &cap) < 0)
{
printf("ioctl VIDIOC_QUERYCAP error!\n");
return -1;
}

printf("cap.capabilities = %d\n",cap.capabilities);
//查看设备是否支持video capture 的接口
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
{   //#define V4L2_CAP_VIDEO_CAPTURE		0x00000001  /* Is a video capture device */
printf("%s is no video capture device\n",dev_name);
return -1;
}

//查看这个设备是否支持 streaming I/O 操作函数
if (!(cap.capabilities & V4L2_CAP_STREAMING))
{
printf ("%s does not support streaming i/o\n",dev_name);  //#define V4L2_CAP_STREAMING              0x04000000  /* streaming I/O ioctls */
return -1;
}

#if 0

CLEAR (cropcap);

cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;// 数据流类型

//查询驱动的修剪能力
if (ioctl (fd, VIDIOC_CROPCAP, &cropcap) == 0)
{
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect;
//设置视频信号的边框
if (ioctl (fd, VIDIOC_S_CROP, &crop) < 0)
{
printf("VIDIOC_S_CROP\n");
}
}
#endif
CLEAR (fmt); //清零

fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 320;
fmt.fmt.pix.height = 240;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;   //V4L2_PIX_FMT_JPEG;//V4L2_PIX_FMT_YUYV;

//  fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
//设置当前驱动的频捕获格式
if (ioctl (fd, VIDIOC_S_FMT, &fmt) < 0)
{
printf("VIDIOC_S_FMT");
return -1;
}
if(init_mmap () <0)
{
printf("init_mmap error!\n");
return -1;
}
return 0;

}

static int close_device (void)
{
if ( close (fd) < 0)
{
printf("close fd error!\n");
return -1;
}
fd = -1;
if ( close (fbfd) < 0)
{
printf("close fbfd error!\n");
return -1;
}
fbfd= -1;
return 0;
}
/*函数功能:打开视频显示设备frambuffer和
*摄像头设备
*/
static int open_device (void)
{

//open framebuffer
fbfd = open("/dev/fb0", O_RDWR);
if (fbfd==-1)
{
printf("Error: cannot open framebuffer device.\n");
return -1;
}

//open camera
fd = open (dev_name, O_RDWR);  // | O_NONBLOCK

if (-1 == fd)
{
printf ("Cannot open '%s': %d, %s\n",dev_name);
return -1;
}
return 0;
}

int main (int argc,char ** argv)
{
dev_name = "/dev/video0";

if(open_device () < 0)
{
printf("open_device error!\n");
return -1;
}
if(init_device () < 0)
{
printf("init_device error!\n");
return -1;
}

if(start_capturing ()<0)
{
printf("start_capturing error!\n");
return -1;
}
if(run () < 0)
{
printf("run error!\n");
return -1;
}

if(stop_capturing ()< 0);
{
printf("stop_capturing error!\n");
return -1;
}

if(uninit_device () < 0)
{
printf("uninit_device error!\n");
return -1;
}
if(close_device () < 0)
{
printf("close_device error!\n");
return -1;
}

return 0;
}

注意: 作者用的开发板用的是飞凌的FL2440,读者要根据自己的实际情况进行修改。比如摄像头设备驱动节点/dev/video0 LCD设备驱动节点/dev/fb0。读者跟我的可能会不一样。

另外下面关于摄像头参数的代码也需要根据自己的实际情况进行修该

    fmt.fmt.pix.width = 320;   //LCD像素宽度

    fmt.fmt.pix.height = 240; //LCD像素高度

    fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;   //V4L2_PIX_FMT_JPEG;//V4L2_PIX_FMT_YUYV;摄像头输出数据格式

代码下载地址: http://download.csdn.net/detail/cjj1130320082/9584969
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  摄像头 linux 移植 arm