惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 邗影

关于音频PA结构腔体 球面渲染 关于linux和shi脚本常用的通配符 关于fread读不满一块和读不满count的返回值 如何去掉merge UBI的覆盖写和擦除 DTSI 多个模块共用同一个 GPIO 引脚冲突 问题汇总 debug比release程序启动的快 关于CPU占用优先级的调整 关于蜂鸣器发声 麦序与声源定位 关于squashfs压缩挂载 软连接生成 线程退出未定义行为 linux文件查找 C语言弱函数 数据传输报错port unreachable D3D11绘制三角形 关于延迟测试 v4l2检测 关于数据库的性能 流媒体小记包含一些面试问题 流媒体ZLM配置vscode远程开发 关于SPS中的帧率问题 Linux编译问题 SSl冲突问题 D3D初始化窗口显示 关于线程池
v4l2视频采集
邗影 · 2025-08-10 · via 博客园 - 邗影
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/mman.h>
#include<linux/videodev2.h>

int main()
{
    int fd = open("/dev/video0",O_RDWR);
    if(fd <0)
    {
       printf("open error");
       return -1;
    }
    
    struct v4l2_fmtdesc fmt;
    
    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//must
    int i =0;
    while(1)
    {
            fmt.index=i++;//must 
        int ret =ioctl(fd,VIDIOC_ENUM_FMT,&fmt);
        if(ret <0){
           printf("----ioctl enum ok-- %d\n",fmt.index);
           break;
        }
        else{
            printf("fmt index:%d\n",fmt.index);
             printf("fmt flag:%d\n",fmt.flags);
              printf("fmt description: %s\n",fmt.description);
              unsigned char*ptr=&fmt.pixelformat;
               printf("fmt pixel fmt: %c %c %c %c\n",ptr[0],ptr[1],ptr[2],ptr[3]);
        }
    }
    //set cap fmt  video0:1jpeg,2YUV
    struct v4l2_format  sfmt;
    sfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    sfmt.fmt.pix.width = 1920;
    sfmt.fmt.pix.height =1080;
    sfmt.fmt.pix.pixelformat=V4L2_PIX_FMT_YUYV;
    int ret =ioctl(fd,VIDIOC_S_FMT,&sfmt);
    if(ret <0)
    {
        printf("set fmt error\n");
        
    }
    
    struct v4l2_format  sfmt2;
    sfmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    
    ret =ioctl(fd,VIDIOC_G_FMT,&sfmt2);
    if(ret <0)
    {
        printf("set fmt error\n");
        
    }else{
    //设置的与实际采集得到的可能并不一样,比如设置1080P,实际采集的是720P
     printf("sfmt.fmt.pix.width %d\n",sfmt.fmt.pix.width);
     printf("sfmt.fmt.pix.height %d\n",sfmt.fmt.pix.height);
    
    }
    
    //malloc kernal space
    struct v4l2_requestbuffers v4l2buf;
    v4l2buf.type= V4L2_BUF_TYPE_VIDEO_CAPTURE;
    v4l2buf.count = 4;//max is 4
    v4l2buf.memory = V4L2_MEMORY_MMAP;//ying she
    ret =ioctl(fd,VIDIOC_REQBUFS,&v4l2buf);
    if(ret <0)
    {
        printf("req memory error");
    }
    //printf("-----------------2--------------\n");
    //map
    struct v4l2_buffer buf;
    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    unsigned char* mptr[4];//save map to user mem first address
    int lensize[4];
    //check kernal memory
    for(int i =0;i<4;i++)
    {
        buf.index = i;
        ret =ioctl(fd,VIDIOC_QUERYBUF,&buf);
        if(ret <0)
        {
            printf("check kernal mem error\n");
        }else{
          //map user memory
          mptr[i]=mmap(NULL,buf.length,PROT_READ|PROT_WRITE,MAP_SHARED,fd,buf.m.offset);
          lensize[i] = buf.length;
          //use over
          ret =ioctl(fd,VIDIOC_QBUF,&buf);
          if(ret<0)
          {
             printf("mmp error");
          }
        }
    }
    //printf("-----------------3--------------\n");
    //start cap
    int type =V4L2_BUF_TYPE_VIDEO_CAPTURE;
    ret = ioctl(fd,VIDIOC_STREAMON,&type);
    if(ret <0)
    {
        printf("stream on error");
    }
    //printf("-----------------3-0-------------\n");
    
    //get data from queue
    FILE* fp=fopen("./m.yuyv","w+");
    int capnum =100;
    while(capnum-- >0)//帧率有单独的控制属性可以设置
    {
        
        struct v4l2_buffer readbuf;
        readbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        ret =ioctl(fd,VIDIOC_DQBUF,&readbuf);//use virtul machine usb 3.1,or not will hold
        if(ret <0)
        {
            printf("read data error");
        }else{
            
            int size = fwrite(mptr[readbuf.index],readbuf.length,1,fp);
            fflush(fp);
            printf("write size:%d",size);
            ret = ioctl(fd,VIDIOC_QBUF,&readbuf);
            
            if(ret <0)
            {
            printf("qbuf error\n");
            }
            
        }
    
    }
    printf("-----------------4--------------\n");
    //stop cap
    ret = ioctl(fd,VIDIOC_STREAMOFF,&type);
    if(ret <0)
    {
        printf("stop error\n");
    }
    //release map
    
    for(int i = 0;i<4;i++)
    {
      munmap(mptr[i],lensize[i]);
    }
    
    fclose(fp);
    close(fd);
    return 0;
}