/* Values for 'capabilities' field */
//#define V4L2_CAP_VIDEO_CAPTURE 0x00000001 /* Is a video capture device */
//#define V4L2_CAP_VIDEO_OUTPUT 0x00000002 /* Is a video output device */
//#define V4L2_CAP_VIDEO_OVERLAY 0x00000004 /* Can do video overlay */
//#define V4L2_CAP_VBI_CAPTURE 0x00000010 /* Is a raw VBI capture device */
//#define V4L2_CAP_VBI_OUTPUT 0x00000020 /* Is a raw VBI output device */
//#define V4L2_CAP_SLICED_VBI_CAPTURE 0x00000040 /* Is a sliced VBI capture device */
//#define V4L2_CAP_SLICED_VBI_OUTPUT 0x00000080 /* Is a sliced VBI output device */
//#define V4L2_CAP_RDS_CAPTURE 0x00000100 /* RDS data capture */
//#define V4L2_CAP_VIDEO_OUTPUT_OVERLAY 0x00000200 /* Can do video output overlay */
//#define V4L2_CAP_HW_FREQ_SEEK 0x00000400 /* Can do hardware frequency seek */
//#define V4L2_CAP_RDS_OUTPUT 0x00000800 /* Is an RDS encoder */
//#define V4L2_CAP_TUNER 0x00010000 /* has a tuner */
//#define V4L2_CAP_AUDIO 0x00020000 /* has audio support */
//#define V4L2_CAP_RADIO 0x00040000 /* is a radio device */
//#define V4L2_CAP_MODULATOR 0x00080000 /* has a modulator */
//#define V4L2_CAP_READWRITE 0x01000000 /* read/write systemcalls */
//#define V4L2_CAP_ASYNCIO 0x02000000 /* async I/O */
//#define V4L2_CAP_STREAMING 0x04000000 /* streaming I/O ioctls */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#define VIDEO_DEV "/dev/video0"
int g_videofd = -1;
struct v4l2_buffer buf;
//打开设备
int open_device(void)
{
g_videofd = open(VIDEO_DEV,O_RDWR);
if(g_videofd == -1){
perror("open");
return -1;
}
return 0;
}
//关闭设备
void close_device(void)
{
close(g_videofd);
}
//查询设备功能信息
int cap_deviceinfo(void)
{
struct v4l2_capability cap;
if(ioctl(g_videofd, VIDIOC_QUERYCAP, &cap) < 0){
perror("ioctl");
return -1;
}
else{
printf("=====================================\n");
printf("driver:\t\t%s\n",cap.driver);
printf("card:\t\t%s\n",cap.card);
printf("bus_info:\t%s\n",cap.bus_info);
printf("version:\t%d\n",cap.version);
printf("capabilities:\t%#x\n",cap.capabilities);
}
//1
if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == V4L2_CAP_VIDEO_CAPTURE)
{
printf("Device %s: supports capture.\n", VIDEO_DEV);
}
//2
if ((cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) == V4L2_CAP_VIDEO_OUTPUT)
{
printf("Device %s: supports output.\n", VIDEO_DEV);
}
//3
if ((cap.capabilities & V4L2_CAP_VIDEO_OVERLAY) == V4L2_CAP_VIDEO_OVERLAY)
{
printf("Device %s: supports overlay.\n", VIDEO_DEV);
}
//4
if ((cap.capabilities & V4L2_CAP_VBI_CAPTURE) == V4L2_CAP_VBI_CAPTURE)
{
printf("Device %s: supports vbi capture.\n", VIDEO_DEV);
}
//5
if ((cap.capabilities & V4L2_CAP_VBI_OUTPUT) == V4L2_CAP_VBI_OUTPUT)
{
printf("Device %s: supports vbi output.\n", VIDEO_DEV);
}
//6
if ((cap.capabilities & V4L2_CAP_SLICED_VBI_CAPTURE) == V4L2_CAP_SLICED_VBI_CAPTURE)
{
printf("Device %s: supports sliced vbi capture.\n", VIDEO_DEV);
}
//7
if ((cap.capabilities & V4L2_CAP_SLICED_VBI_OUTPUT) == V4L2_CAP_SLICED_VBI_OUTPUT)
{
printf("Device %s: supports sliced sliced vbi output.\n", VIDEO_DEV);
}
//8
if ((cap.capabilities & V4L2_CAP_RDS_CAPTURE) == V4L2_CAP_RDS_CAPTURE)
{
printf("Device %s: supports rds capture.\n", VIDEO_DEV);
}
//9
if ((cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_OVERLAY) == V4L2_CAP_VIDEO_OUTPUT_OVERLAY)
{
printf("Device %s: supports video output overlay.\n", VIDEO_DEV);
}
//10
if ((cap.capabilities & V4L2_CAP_HW_FREQ_SEEK) == V4L2_CAP_HW_FREQ_SEEK)
{
printf("Device %s: supports hw freq seek.\n", VIDEO_DEV);
}
//11
if ((cap.capabilities & V4L2_CAP_RDS_OUTPUT) == V4L2_CAP_RDS_OUTPUT)
{
printf("Device %s: supports rds output.\n", VIDEO_DEV);
}
//12
if ((cap.capabilities & V4L2_CAP_TUNER) == V4L2_CAP_TUNER)
{
printf("Device %s: supports tuner.\n", VIDEO_DEV);
}
//13
if ((cap.capabilities & V4L2_CAP_AUDIO) == V4L2_CAP_AUDIO)
{
printf("Device %s: supports audio.\n", VIDEO_DEV);
}
//14
if ((cap.capabilities & V4L2_CAP_RADIO) == V4L2_CAP_RADIO)
{
printf("Device %s: supports radio.\n", VIDEO_DEV);
}
//15
if ((cap.capabilities & V4L2_CAP_MODULATOR) == V4L2_CAP_MODULATOR)
{
printf("Device %s: supports modulator.\n", VIDEO_DEV);
}
//16
if ((cap.capabilities & V4L2_CAP_READWRITE) == V4L2_CAP_READWRITE)
{
printf("Device %s: supports readwrite.\n", VIDEO_DEV);
}
//17
if((cap.capabilities & V4L2_CAP_ASYNCIO) == V4L2_CAP_ASYNCIO)
{
printf("Device %s: supports asyncio.\n",VIDEO_DEV);
}
//18
if((cap.capabilities & V4L2_CAP_STREAMING) == V4L2_CAP_STREAMING)
{
printf("Device %s: supports streaming.\n",VIDEO_DEV);
}
return 0;
}
//初始化设备信息
int init_V4L2(void)
{
if(open_device() < 0)
return -1;
if(cap_deviceinfo() < 0)
return -1;
return 0;
}
//获取当前视频设备支持的格式
int get_enum_fmt(void)
{
struct v4l2_fmtdesc fmtdesc;
memset(&fmtdesc,0,sizeof(struct v4l2_fmtdesc));
fmtdesc.index = 0;
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
while(ioctl(g_videofd,VIDIOC_ENUM_FMT,&fmtdesc) != -1)
{ /*if(ioctl(g_videofd,VIDIOC_ENUM_FMT,&fmtdesc) < 0)
{
perror("ioctl enum fmt");
return -1;
}
*/
//fmtdesc.index++;
printf("====================================\n");
printf("index is %x\n",fmtdesc.index);
printf("type is %x\n",fmtdesc.type);
printf("flags is %#x\n",fmtdesc.flags);
printf("description is %s\n",fmtdesc.description);
//printf("pixelformat is %#x\n",fmtdesc.pixelformat);
printf("pixelformat is %c %c %c %c\n",
fmtdesc.pixelformat & 0xFF,
(fmtdesc.pixelformat >> 8) & 0xFF,
(fmtdesc.pixelformat >> 16) & 0xFF,
(fmtdesc.pixelformat >> 24) & 0xFF);
fmtdesc.index++;
}
return 0;
}
//获取当前的视频格式以及信息
int get_fmt()
{
struct v4l2_format format;
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(g_videofd,VIDIOC_G_FMT,&format) < 0)
{
perror("get fmt");
return -1;
}
// struct v4l2_pix_format pix_format;
// pixformat = format.fmt.pix;
printf("========================================\n");
printf("width:%d\n",format.fmt.pix.width);
printf("height:%d\n",format.fmt.pix.height);
//printf("pixelformat:%x\n",format.fmt.pix.pixelformat);
printf("pixelformat is %c %c %c %c\n",
format.fmt.pix.pixelformat & 0xFF,
(format.fmt.pix.pixelformat >> 8) & 0xFF,
(format.fmt.pix.pixelformat >> 16) & 0xFF,
(format.fmt.pix.pixelformat >> 24) & 0xFF);
switch(format.fmt.pix.field)
{
case V4L2_FIELD_ANY:
printf("V4L2_FIELD_ANY\n");
break;
case V4L2_FIELD_NONE:
printf("field:V4L2_FIELD_NONE\n");
break;
case V4L2_FIELD_TOP:
printf("field:V4L2_FIELD_TOP\n");
break;
case V4L2_FIELD_BOTTOM:
printf("field:V4L2_FIELD_BOTTOM\n");
break;
case V4L2_FIELD_INTERLACED:
printf("field:V4L2_FIELD_INTERLACED\n");
break;
case V4L2_FIELD_SEQ_TB:
printf("field:V4L2_FIELD_SEQ_TB\n");
break;
case V4L2_FIELD_SEQ_BT:
printf("field:V4L2_FIELD_SEQ_BT\n");
break;
case V4L2_FIELD_ALTERNATE:
printf("field:V4L2_FIELD_ALTERNATE\n");
break;
case V4L2_FIELD_INTERLACED_TB:
printf("field:V4L2_FIELD_INT