一、V4L2的用户层核心结构体
1)用于查询摄像头支持的视频数据格式以及其他相关参数
//格式 枚举结构体
struct v4l2_fmtdesc {
__u32 index; /* Format number */
__u32 type; /* enum v4l2_buf_type */
__u32 flags;
__u8 description[32]; /* Description string */
__u3 pixelformat; /* Format fourcc */
__u32 reserved[4];
};
index:摄像头索引号
一般从 0 开始查询所有支持的格式
type:
视频采集格式
V4L2_BUF_TYPE_VIDEO_CAPTURE = 1,
一般填写第一个:代表采集摄像头数据
V4L2_BUF_TYPE_VIDEO_OUTPUT = 2,
V4L2_BUF_TYPE_VIDEO_OVERLAY = 3,
V4L2_BUF_TYPE_VBI_CAPTURE = 4,
V4L2_BUF_TYPE_VBI_OUTPUT = 5,
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE = 6,
V4L2_BUF_TYPE_SLICED_VBI_OUTPUT = 7,
description:
返回的摄像头支持格式
pixelformat:
摄像头支持的帧格式
2)查看视频格式详细支持分辨率
struct v4l2_frmsizeenum {
__u32 index; /* Frame size number */
__u32 pixel_format; /* Pixel format */
__u32 type; /* Frame size type the device supports. */
union { /* Frame size */
struct v4l2_frmsize_discrete discrete;
struct v4l2_frmsize_stepwise stepwise;
};
__u32 reserved[2]; /* Reserved space for future use */
};
index : 同上
type:同上
pixel_format:
你要查看的摄像头帧格式支持的的分辨率
例如查看MJPEG格式支持的的分辨率
V4L2_PIX_FMT_MJPEG
详细定义在 videodev2.h里面
discrete.width:
就是支持的宽度
discrete.height
就是支持的高度
3)查看分辨率下所支持的帧率
struct v4l2_frmivalenum {
__u32 index; /* Frame format index */
__u32 pixel_format; /* Pixel format */
__u32 width; /* Frame width */
__u32 height; /* Frame height */
__u32 type; /* Frame interval type the device supports. */
union { /* Frame interval */
struct v4l2_fract discrete;
struct v4l2_frmival_stepwise stepwise;
};
__u32 reserved[2]; /* Reserved space for future use */
};
index:
同上
type:
同上 填V4L2_BUF_TYPE_VIDEO_CAPTURE
pixel_format:
填写想要查看的视频格式
例如V4L2_PIX_FMT_MJPEG 查看MJPEG视频格式
width:
查看视频格式的分辨率的宽度
height:
查看视频格式的分辨率的高度
discrete:
返回的帧率则在此结构体的 denominator 保存
discrete.denominator保存的就是帧率
4)设置分辨率下所支持的帧率
struct v4l2_format {
__u32 type;
union {
struct v4l2_pix_format pix; /* V4L2_BUF_TYPE_VIDEO_CAPTURE */
struct v4l2_pix_format_mplane pix_mp; /* V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE */
struct v4l2_window win; /* V4L2_BUF_TYPE_VIDEO_OVERLAY */
struct v4l2_vbi_format vbi; /* V4L2_BUF_TYPE_VBI_CAPTURE */
struct v4l2_sliced_vbi_format sliced; /* V4L2_BUF_TYPE_SLICED_VBI_CAPTURE */
struct v4l2_sdr_format sdr; /* V4L2_BUF_TYPE_SDR_CAPTURE */
__u8 raw_data[200]; /* user-defined */
} fmt;
};
type:同上
fmt.pix:
{
width: 设置分辨率的宽度
height:设置的分辨看的高度
pixelformat:设置的视频输出格式
V4L2_PIX_FMT_MJPEG
}
5)V4L2申请内存结构体
struct v4l2_requestbuffers {
__u32 count;
__u32 type; /* enum v4l2_buf_type */
__u32 memory; /* enum v4l2_memory */
__u32 reserved[2];
};
type:同上
V4L2_BUF_TYPE_VIDEO_CAPTURE
count:
往往会申请四个缓冲区
memory:
一般采取内存映射方法
V4L2_MEMORY_MMAP
//查询内存 映射到进程空间
struct v4l2_buffer {
__u32 index;
__u32 type;
__u32 bytesused;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;
/* memory location */
__u32 memory;
union {
__u32 offset;
unsigned long userptr;
struct v4l2_plane *planes;
__s32 fd;
} m;
__u32 length;
__u32 reserved2;
__u32 reserved;
};
type:
同上 - V4L2_BUF_TYPE_VIDEO_CAPTURE
memory:
采取内存映射
V4L2_MEMORY_MMAP
index :
索引号
申请几个就会有几个
length:
申请的内存长度
m:
offt : 申请内存的偏移地址
四、V4L2 ioctl指令集合
V4L2 ioctl指令集合:
VIDIOC_ENUM_FMT://枚举所有格式
VIDIOC_ENUM_FRAMESIZES://查看视频格式下 支持的所有的分辨率
VIDIOC_ENUM_FRAMEINTERVALS://查看视频格式下分辨率支持的帧率
VIDIOC_S_FMT://设置摄像头视频格式
VIDIOC_REQBUFS://申请内存
VIDIOC_QUERYBUF://查询映射内存
VIDIOC_QBUF://加入缓存队列
VIDIOC_STREAMON://开启数据采集
VIDIOC_DQBUF://提取缓存队列
VIDIOC_STREAMOFF//停止数据采集
五、V4L2 查看格式示例
/*********************************************************************************
* V4L2 查看格式示例
************************************************************************************/
int V4L2_Temp1(void)
{
struct v4l2_fmtdesc videoformat;
int fd = open("/dev/video0",O_RDWR);
if(fd < 0 )
{
perror("open");
printf("摄像头打开失败\r\n");
return -1;
}
printf("摄像头打开成功\r\n");
videoformat.index = 0;
videoformat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//视频采集格式
while(0==ioctl(fd,VIDIOC_ENUM_FMT,&videoformat))//枚举所有支持格式
{
printf("fmt: %s <0x%s>\n", videoformat.description, (char *)&videoformat.pixelformat);
videoformat.index++;
}
close(fd);
}
六、V4L2 查看支持分辨率示例
/***********************************************************************************************
* V4L2 查看支持分辨率示例
* format:
* V4L2_PIX_FMT_MJPEG //大部分摄像头均支持该模式
* V4L2_PIX_FMT_YUYV
* V4L2_PIX_FMT_NV12
* *******************************************************************************************/
int V4L2_Temp2(unsigned int format)
{
struct v4l2_frmsizeenum frmsize;
int fd = open