天天看点

友善之臂视频监控方案源码学习(6) - 视频采集

转载于http://blog.csdn.net/tandesir/article/details/8437442

【问题描述】在友善之臂视频监控方案源码学习(5) - 输入控制一文中,介绍了input_run完成的功能。本文结合input_run实现的视频采集线程对视频采集进行详细分析。

【解析】

1 涉及到的文件和目录

mjpg-streamer-mini2440-read-only/start_uvc.sh

mjpg-streamer-mini2440-read-only/mjpg_streamer.c

mjpg-streamer-mini2440-read-only/mjpg_streamer.h

mjpg-streamer-mini2440-read-only/plugins/input.h

mjpg-streamer-mini2440-read-only/plugins/input_uvc

2 视频设备初始化

视频设备初始化在input_init函数中完成(mjpg-streamer-mini2440-read-only/plugins/input_uvc/input_uvc.c):

[html]  view plain copy

  1.   if (init_videoIn(videoIn, dev, width, height, fps, format, 1) < 0) {  
  2.     IPRINT("init_VideoIn failed\n");  
  3.     closelog();  
  4.     exit(EXIT_FAILURE);  
  5.   }  

init_videoIn函数中调用的视频初始化代码如下(mjpg-streamer-mini2440-read-only/plugins/input_uvc/V412uvc.c):

[html]  view plain copy

  1. if (init_v4l2 (vd) < 0) {  
  2.     fprintf (stderr, " Init v4L2 failed !! exit fatal \n");  
  3.     goto error;;  
  4.   }  

init_v412代码如下(mjpg-streamer-mini2440-read-only/plugins/input_uvc/V412uvc.c):

[html]  view plain copy

  1. static int init_v4l2(struct vdIn *vd)  
  2. {  
  3.   int i;  
  4.   int ret = 0;  
  5.   if ((vd->fd = open(vd->videodevice, O_RDWR)) == -1) {  
  6.     perror("ERROR opening V4L interface");  
  7.     return -1;  
  8.   }  
  9.   memset(&vd->cap, 0, sizeof(struct v4l2_capability));  
  10.   ret = ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);  
  11.   if (ret < 0) {  
  12.     fprintf(stderr, "Error opening device %s: unable to query device.\n", vd->videodevice);  
  13.     goto fatal;  
  14.   }  
  15.   if ((vd->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {  
  16.     fprintf(stderr, "Error opening device %s: video capture not supported.\n",  
  17.            vd->videodevice);  
  18.     goto fatal;;  
  19.   }  
  20.   if (vd->grabmethod) {  
  21.     if (!(vd->cap.capabilities & V4L2_CAP_STREAMING)) {  
  22.       fprintf(stderr, "%s does not support streaming i/o\n", vd->videodevice);  
  23.       goto fatal;  
  24.     }  
  25.   } else {  
  26.     if (!(vd->cap.capabilities & V4L2_CAP_READWRITE)) {  
  27.       fprintf(stderr, "%s does not support read i/o\n", vd->videodevice);  
  28.       goto fatal;  
  29.     }  
  30.   }  
  31.   memset(&vd->fmt, 0, sizeof(struct v4l2_format));  
  32.   vd->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  33.   vd->fmt.fmt.pix.width = vd->width;  
  34.   vd->fmt.fmt.pix.height = vd->height;  
  35.   vd->fmt.fmt.pix.pixelformat = vd->formatIn;  
  36.   vd->fmt.fmt.pix.field = V4L2_FIELD_ANY;  
  37.   ret = ioctl(vd->fd, VIDIOC_S_FMT, &vd->fmt);  
  38.   if (ret < 0) {  
  39.     perror("Unable to set format");  
  40.     goto fatal;  
  41.   }  
  42.   if ((vd->fmt.fmt.pix.width != vd->width) ||  
  43.       (vd->fmt.fmt.pix.height != vd->height)) {  
  44.     fprintf(stderr, " format asked unavailable get width %d height %d \n", vd->fmt.fmt.pix.width, vd->fmt.fmt.pix.height);  
  45.     vd->width = vd->fmt.fmt.pix.width;  
  46.     vd->height = vd->fmt.fmt.pix.height;  
  47.   }  
  48.     if(vd->fmt.fmt.pix.pixelformat!=vd->formatIn)  
  49.     {  
  50.         char fourcc1[5]={0,0,0,0,0};  
  51.         char fourcc2[5]={0,0,0,0,0};  
  52.         memmove(fourcc1,(char*)&vd->formatIn,4);  
  53.         memmove(fourcc2,(char*)&vd->fmt.fmt.pix.pixelformat,4);  
  54.         fprintf(stderr, " requested %s but got %s format instead\n",fourcc1,fourcc2);  
  55.     vd->formatIn = vd->fmt.fmt.pix.pixelformat;  
  56.     }  
  57.   struct v4l2_streamparm *setfps;  
  58.   setfps = (struct v4l2_streamparm *) calloc(1, sizeof(struct v4l2_streamparm));  
  59.   memset(setfps, 0, sizeof(struct v4l2_streamparm));  
  60.   setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  61.   setfps->parm.capture.timeperframe.numerator = 1;  
  62.   setfps->parm.capture.timeperframe.denominator = vd->fps;  
  63.   ret = ioctl(vd->fd, VIDIOC_S_PARM, setfps);  
  64.   memset(&vd->rb, 0, sizeof(struct v4l2_requestbuffers));  
  65.   vd->rb.count = NB_BUFFER;  
  66.   vd->rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  67.   vd->rb.memory = V4L2_MEMORY_MMAP;  
  68.   ret = ioctl(vd->fd, VIDIOC_REQBUFS, &vd->rb);  
  69.   if (ret < 0) {  
  70.     perror("Unable to allocate buffers");  
  71.     goto fatal;  
  72.   }  
  73.   for (i = 0; i < NB_BUFFER; i++) {  
  74.     memset(&vd->buf, 0, sizeof(struct v4l2_buffer));  
  75.     vd->buf.index = i;  
  76.     vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  77.     vd->buf.memory = V4L2_MEMORY_MMAP;  
  78.     ret = ioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf);  
  79.     if (ret < 0) {  
  80.       perror("Unable to query buffer");  
  81.       goto fatal;  
  82.     }  
  83.     if (debug)  
  84.       fprintf(stderr, "length: %u offset: %u\n", vd->buf.length, vd->buf.m.offset);  
  85.     vd->mem[i] = mmap(0  ,  
  86.                       vd->buf.length, PROT_READ, MAP_SHARED, vd->fd,  
  87.                       vd->buf.m.offset);  
  88.     if (vd->mem[i] == MAP_FAILED) {  
  89.       perror("Unable to map buffer");  
  90.       goto fatal;  
  91.     }  
  92.     if (debug)  
  93.       fprintf(stderr, "Buffer mapped at address %p.\n", vd->mem[i]);  
  94.   }  
  95.   for (i = 0; i < NB_BUFFER; ++i) {  
  96.     memset(&vd->buf, 0, sizeof(struct v4l2_buffer));  
  97.     vd->buf.index = i;  
  98.     vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  99.     vd->buf.memory = V4L2_MEMORY_MMAP;  
  100.     ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);  
  101.     if (ret < 0) {  
  102.       perror("Unable to queue buffer");  
  103.       goto fatal;;  
  104.     }  
  105.   }  
  106.   return 0;  
  107. fatal:  
  108.   return -1;  
  109. }  

该部分主要完成了下述功能:

(1) 打开视频设备

[html]  view plain copy

  1. if ((vd->fd = open(vd->videodevice, O_RDWR)) == -1) {  
  2.     perror("ERROR opening V4L interface");  
  3.     return -1;  
  4.   }  

(2) 开启捕获功能

[html]  view plain copy

  1. memset(&vd->cap, 0, sizeof(struct v4l2_capability));  
  2.   ret = ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);  
  3.   if (ret < 0) {  
  4.     fprintf(stderr, "Error opening device %s: unable to query device.\n", vd->videodevice);  
  5.     goto fatal;  
  6.   }  
  7.   if ((vd->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {  
  8.     fprintf(stderr, "Error opening device %s: video capture not supported.\n",  
  9.            vd->videodevice);  
  10.     goto fatal;;  
  11.   }  
  12.   if (vd->grabmethod) {  
  13.     if (!(vd->cap.capabilities & V4L2_CAP_STREAMING)) {  
  14.       fprintf(stderr, "%s does not support streaming i/o\n", vd->videodevice);  
  15.       goto fatal;  
  16.     }  
  17.   } else {  
  18.     if (!(vd->cap.capabilities & V4L2_CAP_READWRITE)) {  
  19.       fprintf(stderr, "%s does not support read i/o\n", vd->videodevice);  
  20.       goto fatal;  
  21.     }  
  22.   }  

(3)  设置视频格式

[html]  view plain copy

  1.   memset(&vd->fmt, 0, sizeof(struct v4l2_format));  
  2.   vd->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  3.   vd->fmt.fmt.pix.width = vd->width;  
  4.   vd->fmt.fmt.pix.height = vd->height;  
  5.   vd->fmt.fmt.pix.pixelformat = vd->formatIn;  
  6.   vd->fmt.fmt.pix.field = V4L2_FIELD_ANY;  
  7.   ret = ioctl(vd->fd, VIDIOC_S_FMT, &vd->fmt);  
  8.   if (ret < 0) {  
  9.     perror("Unable to set format");  
  10.     goto fatal;  
  11.   }  
  12.   if ((vd->fmt.fmt.pix.width != vd->width) ||  
  13.       (vd->fmt.fmt.pix.height != vd->height)) {  
  14.     fprintf(stderr, " format asked unavailable get width %d height %d \n", vd->fmt.fmt.pix.width, vd->fmt.fmt.pix.height);  
  15.     vd->width = vd->fmt.fmt.pix.width;  
  16.     vd->height = vd->fmt.fmt.pix.height;  
  17.   }  
  18.     if(vd->fmt.fmt.pix.pixelformat!=vd->formatIn)  
  19.     {  
  20.         char fourcc1[5]={0,0,0,0,0};  
  21.         char fourcc2[5]={0,0,0,0,0};  
  22.         memmove(fourcc1,(char*)&vd->formatIn,4);  
  23.         memmove(fourcc2,(char*)&vd->fmt.fmt.pix.pixelformat,4);  
  24.         fprintf(stderr, " requested %s but got %s format instead\n",fourcc1,fourcc2);  
  25.     vd->formatIn = vd->fmt.fmt.pix.pixelformat;  
  26.     }  

(4) 设置帧速率

[html]  view plain copy

  1.  struct v4l2_streamparm *setfps;  
  2.  setfps = (struct v4l2_streamparm *) calloc(1, sizeof(struct v4l2_streamparm));  
  3.  memset(setfps, 0, sizeof(struct v4l2_streamparm));  
  4.  setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  5.  setfps->parm.capture.timeperframe.numerator = 1;  
  6.  setfps->parm.capture.timeperframe.denominator = vd->fps;  
  7.  ret = ioctl(vd->fd, VIDIOC_S_PARM, setfps);  

(5) 设置缓存

[html]  view plain copy

  1.   memset(&vd->rb, 0, sizeof(struct v4l2_requestbuffers));  
  2.   vd->rb.count = NB_BUFFER;  
  3.   vd->rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  4.   vd->rb.memory = V4L2_MEMORY_MMAP;  
  5.   ret = ioctl(vd->fd, VIDIOC_REQBUFS, &vd->rb);  
  6.   if (ret < 0) {  
  7.     perror("Unable to allocate buffers");  
  8.     goto fatal;  
  9.   }  
  10.   for (i = 0; i < NB_BUFFER; i++) {  
  11.     memset(&vd->buf, 0, sizeof(struct v4l2_buffer));  
  12.     vd->buf.index = i;  
  13.     vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  14.     vd->buf.memory = V4L2_MEMORY_MMAP;  
  15.     ret = ioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf);  
  16.     if (ret < 0) {  
  17.       perror("Unable to query buffer");  
  18.       goto fatal;  
  19.     }  
  20.     if (debug)  
  21.       fprintf(stderr, "length: %u offset: %u\n", vd->buf.length, vd->buf.m.offset);  
  22.     vd->mem[i] = mmap(0  ,  
  23.                       vd->buf.length, PROT_READ, MAP_SHARED, vd->fd,  
  24.                       vd->buf.m.offset);  
  25.     if (vd->mem[i] == MAP_FAILED) {  
  26.       perror("Unable to map buffer");  
  27.       goto fatal;  
  28.     }  
  29.     if (debug)  
  30.       fprintf(stderr, "Buffer mapped at address %p.\n", vd->mem[i]);  
  31.   }  
  32.   for (i = 0; i < NB_BUFFER; ++i) {  
  33.     memset(&vd->buf, 0, sizeof(struct v4l2_buffer));  
  34.     vd->buf.index = i;  
  35.     vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  36.     vd->buf.memory = V4L2_MEMORY_MMAP;  
  37.     ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);  
  38.     if (ret < 0) {  
  39.       perror("Unable to queue buffer");  
  40.       goto fatal;;  
  41.     }  

3 视频采集

在input_run中调用了视频采集的线程函数,如下所示:

[html]  view plain copy

  1. int input_run(void) {  
  2.   pglobal->buf = malloc(videoIn->framesizeIn);  
  3.   if (pglobal->buf == NULL) {  
  4.     fprintf(stderr, "could not allocate memory\n");  
  5.     exit(EXIT_FAILURE);  
  6.   }  
  7.   pthread_create(&cam, 0, cam_thread, NULL);  
  8.   pthread_detach(cam);  
  9.   return 0;  
  10. }  

cam_thread定义如下:

[html]  view plain copy

  1. void *cam_thread( void *arg ) {  
  2.   pthread_cleanup_push(cam_cleanup, NULL);  
  3.   while( !pglobal->stop ) {  
  4.     if( uvcGrab(videoIn) < 0 ) {  
  5.       IPRINT("Error grabbing frames\n");  
  6.       exit(EXIT_FAILURE);  
  7.     }  
  8.     DBG("received frame of size: %d\n", videoIn->buf.bytesused);  
  9.     if ( videoIn->buf.bytesused < minimum_size ) {  
  10.       DBG("dropping too small frame, assuming it as broken\n");  
  11.       continue;  
  12.     }  
  13.     pthread_mutex_lock( &pglobal->db );  
  14.     if (videoIn->formatIn != V4L2_PIX_FMT_MJPEG) {   
  15.       DBG("compressing frame\n");   
  16.       pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality, videoIn->formatIn);  
  17.     }  
  18.     else {  
  19.       DBG("copying frame\n");  
  20.       pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused);  
  21.     }  
  22. #if 0  
  23.     if ( (prev_size - global->size)*(prev_size - global->size) > 4*1024*1024 ) {  
  24.         DBG("motion detected (delta: %d kB)\n", (prev_size - global->size) / 1024);  
  25.     }  
  26.     prev_size = global->size;  
  27. #endif  
  28.     pthread_cond_broadcast(&pglobal->db_update);  
  29.     pthread_mutex_unlock( &pglobal->db );  
  30.     DBG("waiting for next frame\n");  
  31.     if ( videoIn->fps < 5 ) {  
  32.       usleep(1000*1000/videoIn->fps);  
  33.     }  
  34.   }  
  35.   DBG("leaving input thread, calling cleanup function now\n");  
  36.   pthread_cleanup_pop(1);  
  37.   return NULL;  
  38. }  

该函数完成下述任务:

(1) 视频抓取

[html]  view plain copy

  1. while( !pglobal->stop ) {  
  2.     if( uvcGrab(videoIn) < 0 ) {  
  3.       IPRINT("Error grabbing frames\n");  
  4.       exit(EXIT_FAILURE);  
  5.     }  

uvcGrab函数实现如下:

[html]  view plain copy

  1. int uvcGrab(struct vdIn *vd)  
  2. {  
  3. #define HEADERFRAME1 0xaf  
  4.   int ret;  
  5.   if (!vd->isstreaming)  
  6.     if (video_enable(vd))  
  7.       goto err;  
  8.   memset(&vd->buf, 0, sizeof(struct v4l2_buffer));  
  9.   vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  10.   vd->buf.memory = V4L2_MEMORY_MMAP;  
  11.   ret = ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf);  
  12.   if (ret < 0) {  
  13.     perror("Unable to dequeue buffer");  
  14.     goto err;  
  15.   }  
  16.   switch (vd->formatIn) {  
  17.     case V4L2_PIX_FMT_MJPEG:  
  18.       if (vd->buf.bytesused <= HEADERFRAME1) {      
  19.         fprintf(stderr, "Ignoring empty buffer ...\n");  
  20.         return 0;  
  21.       }  
  22.       memcpy(vd->tmpbuffer, vd->mem[vd->buf.index], vd->buf.bytesused);  
  23.       if (debug)  
  24.         fprintf(stderr, "bytes in used %d \n", vd->buf.bytesused);  
  25.       break;  
  26.     case V4L2_PIX_FMT_YUYV:  
  27.     default:  
  28.       if (vd->buf.bytesused > vd->framesizeIn)  
  29.         memcpy (vd->framebuffer, vd->mem[vd->buf.index], (size_t) vd->framesizeIn);  
  30.       else  
  31.         memcpy (vd->framebuffer, vd->mem[vd->buf.index], (size_t) vd->buf.bytesused);  
  32.       break;  
  33.       //goto err;  
  34.     break;  
  35.   }  
  36.   ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);  
  37.   if (ret < 0) {  
  38.     perror("Unable to requeue buffer");  
  39.     goto err;  
  40.   }  
  41.   return 0;  
  42. err:  
  43.   vd->signalquit = 0;  
  44.   return -1;  
  45. }  

该函数主要完成了下述功能:

(a) 使能视频设备

[html]  view plain copy

  1. if (!vd->isstreaming)  
  2.     if (video_enable(vd))  
  3.       goto err;  

实质上是调用了下述方法:

[html]  view plain copy

  1. ret = ioctl(vd->fd, VIDIOC_STREAMON, &type);  
  2.   if (ret < 0) {  
  3.     perror("Unable to start capture");  
  4.     return ret;  
  5.   }  

(b) 视频采集

[html]  view plain copy

  1. ret = ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf);  
  2.   if (ret < 0) {  
  3.     perror("Unable to dequeue buffer");  
  4.     goto err;  
  5.   }  

注:采集的视频信息放在vd->buf中。

(2) 视频压缩编码

[html]  view plain copy

  1. if (videoIn->formatIn != V4L2_PIX_FMT_MJPEG) {   
  2.       DBG("compressing frame\n");   
  3.       pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality, videoIn->formatIn);  
  4.     }  
  5.     else {  
  6.       DBG("copying frame\n");  
  7.       pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused);  
  8.     }  

此过程,后续文章进行详述。

4 视频的存储

视频采集的信息存储在vd->buf中,vd->buf怎么和global->buf联系起来的呢?

(1) input_init初始化全局指针

[html]  view plain copy

  1. pglobal = param->global;  

param->global指针指向mjpg-streamer-mini2440-read-only/mjpg_streamer.c主程序main中的global全局指针。

(2) input_run中分配空间

[html]  view plain copy

  1. pglobal->buf = malloc(videoIn->framesizeIn);  
  2.   if (pglobal->buf == NULL) {  
  3.     fprintf(stderr, "could not allocate memory\n");  
  4.     exit(EXIT_FAILURE);  
  5.   }  

(3) cam_thread抓取

[html]  view plain copy

  1. while( !pglobal->stop ) {  
  2.     if( uvcGrab(videoIn) < 0 ) {  
  3.       IPRINT("Error grabbing frames\n");  
  4.       exit(EXIT_FAILURE);  
  5.     }  

抓取的数据存储在videoIn结构中:

[html]  view plain copy

  1. int uvcGrab(struct vdIn *vd)  
  2. {  
  3. ...  
  4. ret = ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf);  
  5.   if (ret < 0) {  
  6.     perror("Unable to dequeue buffer");  
  7.     goto err;  
  8.   }  
  9. }  

(4) global->buf和videoIn->buf的联系

[html]  view plain copy

  1. if (videoIn->formatIn != V4L2_PIX_FMT_MJPEG) {   
  2.       DBG("compressing frame\n");   
  3.       pglobal->size = compress_yuyv_to_jpeg(videoIn, pglobal->buf, videoIn->framesizeIn, gquality, videoIn->formatIn);  
  4.     }  
  5.     else {  
  6.       DBG("copying frame\n");  
  7.       pglobal->size = memcpy_picture(pglobal->buf, videoIn->tmpbuffer, videoIn->buf.bytesused);  
  8.     }  

最终,通过压缩编码将videoIn->buf的数据存储在global->buf中。

 【源码下载】

http://download.csdn.net/detail/tandesir/4915905

转载请标明出处,仅供学习交流,勿用于商业目的

Copyright @ http://blog.csdn.net/tandesir

继续阅读