歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux 下視頻設備設置的幾個參數 v4l video4linux v4l2 ioctl

Linux 下視頻設備設置的幾個參數 v4l video4linux v4l2 ioctl

日期:2017/3/1 10:01:01   编辑:Linux編程

用一系列的ioctl發命令控制設備。v4l支持的ioctl命令大概有二十幾個,為了盡快的編出一個簡單的圖象捕捉程序,讓我們先來看看幾個主要的命令:

1. ioctl(fd,VIDIOCGCAP,&cap);
該命令主要是為了獲取電視卡的功能信息。例如電視卡的名稱,類型,channel等。參數cap是一個結構,當ioctl命令返回時,結構的各成員就被賦值了,結構體的定義為:
struct video_capability
{
char name[32];
int type;
int channels; /* Num channels */
int audios; /* Num audio devices */
int maxwidth; /* Supported width */
int maxheight; /* And height */
int minwidth; /* Supported width */
int minheight; /* And height */
};
channel 指的是有幾個信號輸入源,例如television,composite,s-video等。



2.ioctl(fd,VIDIOCGCHAN,&vc)
3.ioctl(fd,VIDIOCSCHAN.&vc)
這兩個命令用來取得和設置電視卡的channel信息,例如使用那個輸入源,制式等。
vc 是一個video_channel結構,其定義為:
struct video_capability
{
char name[32];
int type;
int channels; /* Num channels */
int audios; /* Num audio devices */
int maxwidth; /* Supported width */
int maxheight; /* And height */
int minwidth; /* Supported width */
int minheight; /* And height */
};

struct video_channel
{
int channel;
char name[32];
int tuners; //number of tuners for this input
__u32 flags;
__u16 type;
__u16 norm;
};
成員channel代表輸入源,通常,

0: television 1:composite1 2:s-video
name 表示該輸入源的名稱。
norm 表示制式,通常,

0:pal 1:ntsc 2:secam 3:auto

4. ioctl(fd,VIDIOCGMBUF,*mbuf)
獲得電視卡緩存的信息,參數mbuf是video_mbuf結構。其定義如下:
struct video_mbuf
{
int size; /* Total memory to map */
int frames; /* Frames */
int offsets[VIDEO_MAX_FRAME];
};
size是緩存的大小,frames表明該電視卡的緩存可以容納的幀數,數組offsets則表明
對應一幀的起始位置,0幀對應offsets[0],1幀對應offsets[1]....
執行完該命令後,就可以用mmap函數將緩存映射到內存中了。大致用法可以參考以下的代

struct video_mbuf mbuf;
unsigned char *buf1,*buf2;

if(ioctl(fd,VIDIOCGMBUF,&mbuf)<0)
{
perror("VIDIOCGMBUF");
return -1;
}

printf("the frame number is %d/n",mbuf.frames);

buf1 = (unsigned char*)mmap(0,mbuf.size,PROT_READ|PROT_WRITE,MAP_SHARED,fd.0);

buf1 = buf1 + mbuf.offset[0];
buf2 = buf1 + mbuf.offset[1];//當然,如果mbuf.frames=1,就不需要下面的了。
......

Copyright © Linux教程網 All Rights Reserved