最近在做視訊編碼,經過一段時間的折騰,終于可以把V4L2采集到的圖檔資料編碼成H264視訊,并且成功将工程移植到了ARM開發闆上。無奈開發闆性能太低,每秒鐘隻能編碼一幀資料,檢視CPU已經跑到100%,計劃換另外一種編碼方式,先把遇到的問題和開發過程記錄下來,做個備忘,也可以給大家一個參考。
我的整個開發流程是:
(1)通過V4L2采集攝像頭資料,儲存成圖檔
(2)通過V4l2采集攝像頭資料,儲存成yuv視訊
(3)下載下傳編譯X264庫,将該庫移植到ARM開發闆
(4)通過V4L2采集攝像頭YUV資料,調用X264庫将資料編碼成H264視訊
在整個開發過程中,不少代碼是參考網上資料,自己也做了不少的優化處理。
首先就是V4L2資料采集,確定linux的驅動和攝像頭是可以正常工作的。V4L2程式設計的一般流程是:
我一般是先在PC機上測試代碼,代碼沒有問題之後再移植到ARM開發闆上,這樣可以保證應用層的代碼沒有沒有問題,如果出問題,那就是開發闆驅動的問題比較大。下面直接上代碼。我在PC機上使用的是中星微的USB攝像頭,在我的PC機上,它隻支援JPEG格式。代碼如下:
/*=============================================================================
# FileName: v4l2.c
# Desc: this program aim to get image from USB camera,
# used the V4L2 interface.
# Author: Licaibiao
# Version:
# LastChange: 2016-12-10
# History:
=============================================================================*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>
#define FILE_VIDEO "/dev/video0"
#define JPG "./out/image%d.jpg"
typedef struct{
void *start;
int length;
}BUFTYPE;
BUFTYPE *usr_buf;
static unsigned int n_buffer = 0;
/*set video capture ways(mmap)*/
int init_mmap(int fd)
{
/*to request frame cache, contain requested counts*/
struct v4l2_requestbuffers reqbufs;
memset(&reqbufs, 0, sizeof(reqbufs));
reqbufs.count = 1; /*the number of buffer*/
reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbufs.memory = V4L2_MEMORY_MMAP;
if(-1 == ioctl(fd,VIDIOC_REQBUFS,&reqbufs))
{
perror("Fail to ioctl 'VIDIOC_REQBUFS'");
exit(EXIT_FAILURE);
}
n_buffer = reqbufs.count;
printf("n_buffer = %d\n", n_buffer);
//usr_buf = calloc(reqbufs.count, sizeof(usr_buf));
usr_buf = calloc(reqbufs.count, sizeof(BUFTYPE));/*** (4) ***/
if(usr_buf == NULL)
{
printf("Out of memory\n");
exit(-1);
}
/*map kernel cache to user process*/
for(n_buffer = 0; n_buffer < reqbufs.count; ++n_buffer)
{
//stand for a frame
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = n_buffer;
/*check the information of the kernel cache requested*/
if(-1 == ioctl(fd,VIDIOC_QUERYBUF,&buf))
{
perror("Fail to ioctl : VIDIOC_QUERYBUF");
exit(EXIT_FAILURE);
}
usr_buf[n_buffer].length = buf.length;
usr_buf[n_buffer].start = (char *)mmap(NULL,buf.length,PROT_READ | PROT_WRITE,MAP_PRIVATE, fd,buf.m.offset);
if(MAP_FAILED == usr_buf[n_buffer].start)
{
perror("Fail to mmap");
exit(EXIT_FAILURE);
}
}
}
int open_camera(void)
{
int fd;
/*open video device with block */
fd = open(FILE_VIDEO, O_RDONLY);
if(fd < 0)
{
fprintf(stderr, "%s open err \n", FILE_VIDEO);
exit(EXIT_FAILURE);
};
return fd;
}
int init_camera(int fd)
{
struct v4l2_capability cap; /* decive fuction, such as video input */
struct v4l2_format tv_fmt; /* frame format */
struct v4l2_fmtdesc fmtdesc; /* detail control value */
struct v4l2_control ctrl;
int ret;
/*show all the support format*/
memset(&fmtdesc, 0, sizeof(fmtdesc));
fmtdesc.index = 0 ; /* the number to check */
fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* check video decive driver capability */
if(ret=ioctl(fd, VIDIOC_QUERYCAP, &cap)<0)
{
fprintf(stderr, "fail to ioctl VIDEO_QUERYCAP \n");
exit(EXIT_FAILURE);
}
/*judge wherher or not to be a video-get device*/
if(!(cap.capabilities & V4L2_BUF_TYPE_VIDEO_CAPTURE))
{
fprintf(stderr, "The Current device is not a video capture device \n");
exit(EXIT_FAILURE);
}
/*judge whether or not to supply the form of video stream*/
if(!(cap.capabilities & V4L2_CAP_STREAMING))
{
printf("The Current device does not support streaming i/o\n");
exit(EXIT_FAILURE);
}
printf("\ncamera driver name is : %s\n",cap.driver);
printf("camera device name is : %s\n",cap.card);
printf("camera bus information: %s\n",cap.bus_info);
/*display the format device support*/
while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1)
{
printf("\nsupport device %d.%s\n\n",fmtdesc.index+1,fmtdesc.description);
fmtdesc.index++;
}
/*set the form of camera capture data*/
tv_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /*v4l2_buf_typea,camera must use V4L2_BUF_TYPE_VIDEO_CAPTURE*/
tv_fmt.fmt.pix.width = 640;
tv_fmt.fmt.pix.height = 480;
tv_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG; /*V4L2_PIX_FMT_YYUV*/
tv_fmt.fmt.pix.field = V4L2_FIELD_NONE; /*V4L2_FIELD_NONE*/
if (ioctl(fd, VIDIOC_S_FMT, &tv_fmt)< 0)
{
fprintf(stderr,"VIDIOC_S_FMT set err\n");
exit(-1);
close(fd);
}
init_mmap(fd);
}
int start_capture(int fd)
{
unsigned int i;
enum v4l2_buf_type type;
/*place the kernel cache to a queue*/
for(i = 0; i < n_buffer; i++)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if(-1 == ioctl(fd, VIDIOC_QBUF, &buf))
{
perror("Fail to ioctl 'VIDIOC_QBUF'");
exit(EXIT_FAILURE);
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd, VIDIOC_STREAMON, &type))
{
printf("i=%d.\n", i);
perror("VIDIOC_STREAMON");
close(fd);
exit(EXIT_FAILURE);
}
return 0;
}
int process_image(void *addr, int length)
{
FILE *fp;
static int num = 0;
char image_name[20];
sprintf(image_name, JPG, num++);
if((fp = fopen(image_name, "w")) == NULL)
{
perror("Fail to fopen");
exit(EXIT_FAILURE);
}
fwrite(addr, length, 1, fp);
usleep(500);
fclose(fp);
return 0;
}
int read_frame(int fd)
{
struct v4l2_buffer buf;
unsigned int i;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
//put cache from queue
if(-1 == ioctl(fd, VIDIOC_DQBUF,&buf))
{
perror("Fail to ioctl 'VIDIOC_DQBUF'");
exit(EXIT_FAILURE);
}
assert(buf.index < n_buffer);
//read process space's data to a file
process_image(usr_buf[buf.index].start, usr_buf[buf.index].length);
if(-1 == ioctl(fd, VIDIOC_QBUF,&buf))
{
perror("Fail to ioctl 'VIDIOC_QBUF'");
exit(EXIT_FAILURE);
}
return 1;
}
int mainloop(int fd)
{
int count = 10;
while(count-- > 0)
{
for(;;)
{
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd,&fds);
/*Timeout*/
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1,&fds,NULL,NULL,&tv);
if(-1 == r)
{
if(EINTR == errno)
continue;
perror("Fail to select");
exit(EXIT_FAILURE);
}
if(0 == r)
{
fprintf(stderr,"select Timeout\n");
exit(-1);
}
if(read_frame(fd))
break;
}
}
return 0;
}
void stop_capture(int fd)
{
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type))
{
perror("Fail to ioctl 'VIDIOC_STREAMOFF'");
exit(EXIT_FAILURE);
}
}
void close_camera_device(int fd)
{
unsigned int i;
for(i = 0;i < n_buffer; i++)
{
if(-1 == munmap(usr_buf[i].start,usr_buf[i].length))
{
exit(-1);
}
}
free(usr_buf);
if(-1 == close(fd))
{
perror("Fail to close fd");
exit(EXIT_FAILURE);
}
}
void main(void)
{
int fd;
fd = open_camera();
init_camera(fd);
start_capture(fd);
mainloop(fd);
stop_capture(fd);
close_camera_device(fd);
}
程式編譯運作結果如下:
[email protected]:/home/share/v4l2_test# gcc v4l2.c -o v4l2_test
[email protected]:/home/share/v4l2_test# ./v4l2_test
camera driver name is : gspca_zc3xx
camera device name is : PC Camera
camera bus information: usb-0000:02:00.0-2.1
support device 1.JPEG
n_buffer = 1
[email protected]:/home/share/v4l2_test# ls
out v4l2.c v4l2_test
[email protected]:/home/share/v4l2_test# cd out/
[email protected]:/home/share/v4l2_test/out# ls
image0.jpg image1.jpg image2.jpg image3.jpg image4.jpg image5.jpg image6.jpg image7.jpg image8.jpg image9.jpg
[email protected]:/home/share/v4l2_test/out#
[email protected]:/home/share/v4l2_test/out#
從運作的結果中我們可以看到攝像頭的驅動名字是:gspca_zc3xx,攝像頭産家起的名字是 :PC Camera,總線是usb-0000:02:00.0-2.1,該攝像頭支援的image 格式隻有JPEG。這裡需要注意:
(1)在初始化攝像頭函數init_camera 裡面的tv_fmt.fmt.pix.pixelformat 參數一定是需要該攝像頭所支援的。
(2)變量 tv_fmt.fmt.pix.field 是攝像頭field的設定,如果設定不正确會出現花屏的現象。field 了解可以參考kickxxx的:field了解
(3)我這裡采用了記憶體映射mmap, mmap的失敗與camera的打開方式有關。我在PC機上,camera以阻塞方式打開,在記憶體映射的時候,mmap 的flag 參數需要填寫MAP_PRIVATE,但是我在開發闆上時,flag隻能是MAP_SHARED,這個時候,我的camera隻能是以O_NONBLOCK非阻塞方式打開,具體的原因有待研究。
(4)上面的代碼,如果reqbufs.count 的值設定大于1的話,編碼的時候,編出來的圖檔有一些是空的,如果是設定為1就不會出現這個問題。這個有待研究。(該問題已經定位到,并且已更新上面的代碼,詳見一個sizeof引發的記憶體越界)
最後,打開儲存的圖檔就是個人的自拍照了,從師弟那拿的一個攝像頭,不知道像素是多少,拍的照片噪點多多,清晰度差。
本文參考文檔:
http://blog.csdn.net/zgyulongfei/article/details/7526249
http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.html