基于v4l2的视频监控

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

[objc] view plain copy
  1. #include <stdio.h>    
  2. #include <stdlib.h>    
  3. #include <string.h>    
  4. #include <assert.h>    
  5. #include <getopt.h>      
  6. #include <fcntl.h>      
  7. #include <unistd.h>    
  8. #include <errno.h>    
  9. #include <sys/stat.h>    
  10. #include <sys/types.h>    
  11. #include <sys/time.h>    
  12. #include <sys/mman.h>    
  13. #include <sys/ioctl.h>    
  14. #include <asm/types.h>    
  15. #include <linux/videodev2.h>    
  16. #include <linux/fb.h>    
  17. #define CLEAR(x) memset (&(x), 0, sizeof (x))     
  18.       
  19. struct buffer {     
  20.     voidvoid * start;     
  21.     size_t length;     
  22. };     
  23.       
  24. static charchar * dev_name = NULL;     
  25. static int fd = -1;     
  26. struct buffer * buffers = NULL;     
  27. static unsigned int n_buffers = 0;     
  28. static int time_in_sec_capture=5;     
  29. static int fbfd = -1;     
  30. static struct fb_var_screeninfo vinfo;     
  31. static struct fb_fix_screeninfo finfo;     
  32. static charchar *fbp=NULL;     
  33. static long screensize=0;     
  34.       
  35.       
  36.       
  37. inline int clip(int value, int min, int max)   
[objc] view plain copy
  1. {     
  2.     return (value > max ? max : value < min ? min : value);     
  3. }     
  4.     
  5.  /*转换成图片显示在LCD上*/   
  6. static void process_image (const voidvoid * p)  
[objc] view plain copy
  1. {     
  2.       
  3.          
  4.     //ConvertYUVToRGB32     
  5.    
  6.     unsigned char* in=(char*)p;     
  7.     int width=320;     
  8.     int height=240;     
  9.     int istride=width*2;     
  10.     int x,y,j;     
  11.     int y0,u,y1,v,r,g,b;     
  12.     long location=0;     
  13.       
  14.     for ( y = 10; y < height + 10; ++y)   
[objc] view plain copy
  1. {     
  2.     for (j = 0, x=10; j < width * 2 ; j += 4,x +=2)   
[objc] view plain copy
  1.         {     
  2.             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +     
  3.                 (y+vinfo.yoffset) * finfo.line_length;     
  4.                  
  5.             y0 = in[j];     
  6.             u = in[j + 1] - 128;                     
  7.             y1 = in[j + 2];             
  8.             v = in[j + 3] - 128;             
  9.       
  10.             r = (2298 * y0 + 4409 * v + 128) >> 8;     
  11.             g = (2298 * y0 - 1100 * u - 2208 * v + 128) >> 8;     
  12.             b = (2298 * y0 + 5516 * u + 128) >> 8;     
  13.              
  14.             fbp[ location + 0] = ((clip(g, 0255)&0x1c)<< 3 ) | ( clip(b, 0255)  >> 3 );     
  15.             fbp[ location + 1] = ((clip(r, 0255) & 0xf8) ) | ( clip(g, 0255)>> 5);     
  16.       
  17.             r = (2298 * y1 + 4409 * v + 128) >> 8;     
  18.             g = (2298 * y1 - 1100 * u - 2208 * v + 128) >> 8;     
  19.             b = (2298 * y1 + 5516 * u + 128) >> 8;     
  20.       
  21.             fbp[ location + 2] = ((clip(g, 0255)&0x1c)<< 3 ) | ( clip(b, 0255)  >> 3 );     
  22.             fbp[ location + 3] = ((clip(r, 0255) & 0xf8) ) | ( clip(g, 0255)>> 5);     
  23.         }     
  24.         in +=istride;     
  25.     }   
  26.       
  27. }     
  28. /*函数功能: 
  29. *读取一帧数据 
  30. */  
  31. static int read_frame (void)     
  32. {     
  33.     struct v4l2_buffer buf;     
  34.     unsigned int i;     
  35.       
  36.     CLEAR (buf);     
  37.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  38.     buf.memory = V4L2_MEMORY_MMAP;     
  39.     //把数据从缓存中读取出来  
  40.     if (ioctl (fd, VIDIOC_DQBUF, &buf) < 0)   
  41.     {     
  42.         printf("ioctl VIDIOC_DQBUF error!\n");  
  43.         exit(-1);  
  44.     }     
  45.       
  46.     assert(buf.index < n_buffers);  
  47.       
  48.   //  printf("v4l2_pix_format->field(%d)\n", buf.field);     
  49.     //assert (buf.field ==V4L2_FIELD_NONE);  
  50.       
  51.     process_image (buffers[buf.index].start);  
  52.    //VIDIOC_QBUF:把数据放回缓存队列  
  53.     if (ioctl (fd, VIDIOC_QBUF, &buf) < 0)     
  54.     {  
  55.          printf("VIDIOC_QBUF");  
  56.          exit(-1);  
  57.     }  
  58.     return 1;     
  59. }     
  60. /* 
  61. *开始视频显示 
  62. */      
  63. static int run (void)     
  64. {     
  65.     unsigned int count;     
  66.     int frames;     
  67.     frames = 330 * time_in_sec_capture;     
  68.     printf("The number is the one!\n");  
  69.   
  70.     while (frames-- > 0)   
  71.     {     
  72.             printf("The frames is the %d\n",frames);  
  73.             for (;;)   
  74.        {     
  75.         #if 0  
  76.               fd_set fds;     
  77.               struct timeval tv;     
  78.               int r;     
  79.               FD_ZERO (&fds);     
  80.               FD_SET (fd, &fds);     
  81.               
  82.   
  83.                  
  84.              tv.tv_sec = 2;     
  85.              tv.tv_usec = 0;     
  86.         //  printf("The number is the one!\n");  
  87.              r = select (fd + 1, &fds, NULLNULL, &tv);     
  88.             //printf("r = %d\n", r);  
  89.             if (-1 == r)  
[objc] view plain copy
  1.             {     
  2.                 if (EINTR == errno)     
  3.                     continue;     
  4.                 printf("select");     
  5.             }     
  6.       
  7.             if (0 == r)   
  8.         {     
  9.                 printf ("select timeout\n");     
  10.                 return -1;     
  11.             }     
  12.         #endif     
  13.             if (read_frame())     
  14.                 break;     
  15.                  
  16.         }     
  17.     }  
  18.     return 0;  
  19. }     
  20.       
  21. static int stop_capturing (void)     
  22. {     
  23.     enum v4l2_buf_type type;     
  24.       
  25.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  26.     if (ioctl (fd, VIDIOC_STREAMOFF, &type) < 0)     
  27.     {  
  28.         printf("ioctl VIDIOC_STREAMOFF error!\n");    
  29.         return -1;  
  30.     }  
  31.     return 0;  
  32. }     
  33. /* 
  34. *函数功能:开始视频捕捉 
  35. *VIDIOC_STREAMON 
  36. */      
  37. static int start_capturing (void)     
  38. {    
  39. #if 0  
  40.     unsigned int i;     
  41.       
  42.     for (i = 0; i < n_buffers; ++i)   
  43.     {     
  44.         struct v4l2_buffer buf;     
  45.         CLEAR (buf);     
  46.           
  47.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  48.         buf.memory = V4L2_MEMORY_MMAP;     
  49.         buf.index = i;     
  50.         //把数据放回缓存队列  
  51.         if (ioctl (fd, VIDIOC_QBUF, &buf) < 0)     
  52.         {  
  53.            printf("ioctl VIDIOC_QBUF error!\n");  
  54.            return -1;  
  55.     }  
  56.     }     
  57. #endif  
  58.      enum v4l2_buf_type type;     
  59.   
  60.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  61.     //开始视频显示函数  
  62.     if (ioctl (fd, VIDIOC_STREAMON, &type) < 0)     
  63.     {  
  64.         printf("ioctl VIDIOC_STREAMON error!\n");  
  65.         return -1;  
  66.     }  
  67.     return 0;  
  68.          
  69. }     
  70. /* 
  71. *函数功能:内存释放 
  72. */      
  73. static int uninit_device (void)     
  74. {     
  75.     unsigned int i;     
  76.       
  77.     for (i = 0; i < n_buffers; ++i)     
  78.     {     
  79.         if (-1 == munmap (buffers[i].start, buffers[i].length))     
  80.         {  
  81.             printf("ioctl VIDIOC_STREAMON error!\n");  
  82.         return -1;  
  83.     }    
  84.     }  
  85.          
  86.     if (-1 == munmap(fbp, screensize))   
  87.     {     
  88.         printf(" Error: framebuffer device munmap() failed.\n");     
  89.         return -1;  
  90.     }         
  91.     free (buffers);  
  92.     return 0;  
  93. }     
  94.       
  95. /*函数功能:内存的操作 
  96. *1.frambuffer内存区映射,数据放到fbp指向的地址,LCD上才有视频图像出来 
  97. *2.分配缓冲区 
  98. *3.分配的数据缓存转换成物理地址 
  99. *4.把数据放回缓存队列 
  100. */     
  101. static int init_mmap (void)     
  102. {     
  103.     struct v4l2_requestbuffers req;     
  104.       
  105.     //mmap framebuffer     
  106.     fbp = (charchar *)mmap(NULL,screensize,PROT_READ | PROT_WRITE,MAP_SHARED ,fbfd, 0);//分配显存    
  107.     if ((int)fbp == -1)   
  108.     {     
  109.         printf("Error: failed to map framebuffer device to memory.\n");     
  110.         return -1 ;     
  111.     }     
  112.     memset(fbp, 0, screensize);     
  113.     CLEAR (req);     
  114.       
  115.     req.count = 4;     
  116.     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  117.     req.memory = V4L2_MEMORY_MMAP;     
  118.     //分配内存  
  119.     if (ioctl (fd, VIDIOC_REQBUFS, &req) < 0)   
  120.     {     
  121.         printf("ioctl VIDIOC_REQBUFS error!\n");  
  122.     return -1;  
  123.     }     
  124.       
  125.     if (req.count < 4)   
  126.     {    //if (req.count < 2)     
  127.         printf ("Insufficient buffer memory on %s\n",dev_name);     
  128.         return -1;     
  129.     }     
  130.       
  131.     buffers = calloc (req.countsizeof (*buffers));     
  132.       
  133.     if (!buffers)   
  134.     {     
  135.        printf("Out of memory\n");     
  136.        return -1;     
  137.     }     
  138.       
  139.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers)   
  140.     {     
  141.         struct v4l2_buffer buf;     
  142.       
  143.         CLEAR (buf);     
  144.       
  145.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  146.         buf.memory = V4L2_MEMORY_MMAP;     
  147.         buf.index = n_buffers;     
  148.         //把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址  
  149.         if (ioctl (fd, VIDIOC_QUERYBUF, &buf) < 0)     
  150.         {  
  151.             printf("ioctl VIDIOC_QUERYBUF error!\n");     
  152.         return -1;  
  153.      }  
  154.         buffers[n_buffers].length = buf.length;     
  155.         buffers[n_buffers].start =mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);     
  156.       
  157.         if (MAP_FAILED == buffers[n_buffers].start)     
  158.         {  
  159.            printf("mmap error!\n");  
  160.        return -1;  
  161.         }     
  162.     }  
  163.     unsigned int i;     
  164.       
  165.     for (i = 0; i < n_buffers; ++i)   
  166.     {     
  167.         struct v4l2_buffer buf;     
  168.         CLEAR (buf);     
  169.           
  170.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  171.         buf.memory = V4L2_MEMORY_MMAP;     
  172.         buf.index = i;     
  173.         //把数据放回缓存队列  
  174.         if (ioctl (fd, VIDIOC_QBUF, &buf) < 0)     
  175.         {  
  176.             printf("ioctl VIDIOC_QBUF error!\n");  
  177.         return -1;  
  178.     }  
  179.     }     
  180.     return 0;  
  181. }     
  182.       
  183. /* 
  184. *函数功能: 
  185. *1.frambuffer的初始化,获得一些硬件信息 
  186. *         如:LCD的像素大小 
  187. *2.摄像头的初始化 VIDIOC_S_FMT VIDIOC_REQBUFS VIDIOC_QUERYBUF 
  188. */            
  189. static int init_device (void)     
  190. {     
  191.     struct v4l2_capability cap;     
  192.     struct v4l2_cropcap cropcap;     
  193.     struct v4l2_crop crop;     
  194.     struct v4l2_format fmt;     
  195.     unsigned int min;     
  196.       
  197.       
  198.     // Get fixed screen information     
  199.       
  200.       if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) < 0)   
  201.       {     
  202.             printf("Error reading fixed information.\n");   
  203.         return -1;  
  204.       }     
  205.       
  206.         // Get variable screen information     
  207.       
  208.      if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) < 0)   
  209.      {     
  210.             printf("Error reading variable information.\n");     
  211.             return -1;     
  212.      }     
  213.   
  214.        screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;//获得设备LCD像素的大小  
  215.       
  216.     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);  
  217.     printf("finfo: line_length:%d  screensize:%d\n", finfo.line_length, screensize);       
  218.       
  219.     //首先使用 VIDIOC_QUERYCAP 命令 来获得当前设备的各个属性放在结构体cap中  
  220.     if (ioctl (fd, VIDIOC_QUERYCAP, &cap) < 0)   
  221.     {   
  222.     printf("ioctl VIDIOC_QUERYCAP error!\n");  
  223.     return -1;  
  224.     }     
  225.   
  226.     printf("cap.capabilities = %d\n",cap.capabilities);  
  227.     //查看设备是否支持video capture 的接口  
  228.     if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))   
  229.     {   //#define V4L2_CAP_VIDEO_CAPTURE        0x00000001  /* Is a video capture device */  
  230.         printf("%s is no video capture device\n",dev_name);     
  231.         return -1;     
  232.     }     
  233.       
  234.     //查看这个设备是否支持 streaming I/O 操作函数  
  235.     if (!(cap.capabilities & V4L2_CAP_STREAMING))   
  236.     {     
  237.         printf ("%s does not support streaming i/o\n",dev_name);  //#define V4L2_CAP_STREAMING              0x04000000  /* streaming I/O ioctls */  
  238.         return -1;     
  239.     }     
  240.       
  241. #if 0      
  242.       
  243.     CLEAR (cropcap);     
  244.       
  245.     cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;// 数据流类型     
  246.   
  247.    //查询驱动的修剪能力  
  248.     if (ioctl (fd, VIDIOC_CROPCAP, &cropcap) == 0)   
  249.     {     
  250.         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  251.         crop.c = cropcap.defrect;     
  252.         //设置视频信号的边框  
  253.         if (ioctl (fd, VIDIOC_S_CROP, &crop) < 0)   
  254.     {     
  255.            printf("VIDIOC_S_CROP\n");  
  256.         }     
  257.     }  
  258. #endif      
  259.     CLEAR (fmt); //清零    
  260.      
  261.     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;     
  262.     fmt.fmt.pix.width = 320;  
  263.     fmt.fmt.pix.height = 240;  
  264.     fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;   //V4L2_PIX_FMT_JPEG;//V4L2_PIX_FMT_YUYV;     
  265.    
  266.   //  fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;     
  267.     //设置当前驱动的频捕获格式  
  268.     if (ioctl (fd, VIDIOC_S_FMT, &fmt) < 0)     
  269.     {  
  270.         printf("VIDIOC_S_FMT");    
  271.     return -1;  
  272.     }  
  273.     if(init_mmap () <0)  
  274.     {  
  275.     printf("init_mmap error!\n");  
  276.     return -1;  
  277.     }  
  278.     return 0;  
  279.       
  280. }     
  281.       
  282. static int close_device (void)     
  283. {     
  284.     if ( close (fd) < 0)     
  285.     {  
  286.         printf("close fd error!\n");   
  287.     return -1;  
  288.     }  
  289.     fd = -1;     
  290.     if ( close (fbfd) < 0)     
  291.     {  
  292.         printf("close fbfd error!\n");    
  293.     return -1;  
  294.     }  
  295.     fbfd= -1;    
  296.     return 0;  
  297. }     
  298. /*函数功能:打开视频显示设备frambuffer和 
  299. *摄像头设备 
  300. */     
  301. static int open_device (void)     
  302. {     
  303.    
  304.       
  305.     //open framebuffer     
  306.      fbfd = open("/dev/fb0", O_RDWR);     
  307.      if (fbfd==-1)   
  308.      {     
  309.          printf("Error: cannot open framebuffer device.\n");     
  310.          return -1;     
  311.      }     
  312.       
  313.     //open camera     
  314.     fd = open (dev_name, O_RDWR);  // | O_NONBLOCK  
  315.       
  316.     if (-1 == fd)   
  317.     {     
  318.         printf ("Cannot open '%s': %d, %s\n",dev_name);     
  319.         return -1;     
  320.     }  
  321.     return 0;  
  322. }     
  323.       
  324.     
  325.       
  326. int main (int argc,charchar ** argv)     
  327. {     
  328.     dev_name = "/dev/video0";     
  329.       
  330.    
  331.     if(open_device () < 0)  
  332.     {  
  333.     printf("open_device error!\n");  
  334.     return -1;    
  335.     }  
  336.     if(init_device () < 0)  
  337.     {  
  338.     printf("init_device error!\n");  
  339.     return -1;    
  340.     }      
  341.       
  342.     if(start_capturing ()<0)  
  343.     {  
  344.     printf("start_capturing error!\n");  
  345.     return -1;  
  346.     }  
  347.     if(run () < 0)  
  348.     {  
  349.     printf("run error!\n");  
  350.     return -1;  
  351.     }  
  352.       
  353.     if(stop_capturing ()< 0);  
  354.     {  
  355.     printf("stop_capturing error!\n");  
  356.     return -1;  
  357.     }  
  358.       
  359.    if(uninit_device () < 0)     
  360.     {  
  361.     printf("uninit_device error!\n");  
  362.     return -1;  
  363.     }  
  364.     if(close_device () < 0)  
  365.     {  
  366.     printf("close_device error!\n");  
  367.     return -1;  
  368.     }     
  369.         
  370.     return 0;     
  371. }    

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

扫描二维码关注公众号,回复: 487302 查看本文章

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

    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

猜你喜欢

转载自blog.csdn.net/guo1988kui/article/details/79911658