首页 > 其他分享 >V4l2 简单介绍和使用

V4l2 简单介绍和使用

时间:2024-01-26 17:12:14浏览次数:36  
标签:int unsigned 介绍 char width fd 简单 V4l2 out

概述: Video for linux 2(简称V4L2),是linux中关于视频设备的内核驱动。
它也是 linux操作系统下用于采集图片、视频和音频数据的 API接口,配合适当的视频采集设备和相应的驱动程序;

作用: 支持许多USB 网络摄像头,电视调谐器和相关设备,使它们的输出标准化,因此程序员可以轻松地向其应用程序添加视频支持。MythTV,tvtime和Tvheadend是使用V4L框架的典型应用程序;
可以实现图片、视频、音频等的采集。在远程会议、可视电话、视频监控系统和嵌入式多媒体终端中都有广泛的应用。

存放位置: Linux中,一切皆文件,视频设备为设备文件,可以像普通文件一样进行读写操作,而采用 V4L2驱动的摄像头设备文件是 /dev/v4l/video0,为了通用,可以建立到一个和普通摄像头一样的 /dev/video0的链接。

实现流程图:

简单例子:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <linux/videodev2.h>
#include <linux/fb.h>

#define   FILE_NAME     "./my.yuyv"
#define   FILE_YUV420   "test.yuv420"
#define   FILE_YUV422   "test.yuv422"
#define   FILE_RGB     "test.rgb32"

#define   WIDTH         640                      // 图片的宽度
#define   HEIGHT        480                     // 图片的高度
#define   COUNT         5                         // 缓冲区个数
#define   FMT           V4L2_PIX_FMT_YUYV         // 图片格式
#define   LEN_YUV422    WIDTH * HEIGHT * 2       //YUV422长度

#define   VIDEO_FILE    "/dev/video0"
#define   FB_FILE       "/dev/fb0"

//文件句柄
int fd = 0;
int m_fb = 0;       //frameBuffer  句柄

FILE *fl = nullptr;
FILE *f420 = nullptr;
FILE *f422 = nullptr;
FILE *frgb32 = nullptr;

//缓冲区地址
uint8_t *pYUV420Buff = NULL;         //yuv420数据缓冲区(输出)
unsigned char *pYUV422 = NULL;       //yuv422数据缓冲区(输出)
unsigned char *pRGBBuff = NULL;      //rgb数据缓冲区(输出)
unsigned char *datas[COUNT];         // 缓冲区数据地址
unsigned char yuv422Buf[LEN_YUV422] = {0};


/**  YUV422  长度大小:  width * height * 2              2 byte
 *   YUV444  长度大小:  width * height * 3              3 byte
 *   YUV420  长度大小:  3 * width * height / 2          1.5 byte
 *   RGB24   长度大小:  3 * width * height              3 byte
*/

//YUYV422转YUV420
int yuyv_to_yuv420p(const unsigned char *in, unsigned char *out, unsigned int width, unsigned int height)
{
    unsigned char *y = out;
    unsigned char *u = out + width*height;
    unsigned char *v = out + width*height + width*height/4;

    unsigned int i,j;
    unsigned int base_h;
    unsigned int is_y = 1, is_u = 1;
    unsigned int y_index = 0, u_index = 0, v_index = 0;

    unsigned long yuv422_length = 2 * width * height;

    //序列为YU YV YU YV,一个yuv422帧的长度 width * height * 2 个字节
    //丢弃偶数行 u v
    for(i=0; i<yuv422_length; i+=2)
    {
        *(y+y_index) = *(in+i);
        y_index++;
    }

    for(i=0; i<height; i+=2)
    {
        base_h = i*width*2;
        for(j=base_h+1; j<base_h+width*2; j+=2)
        {

            if(is_u)
            {
                *(u+u_index) = *(in+j);
                u_index++;
                is_u = 0;
            }
            else
            {
                *(v+v_index) = *(in+j);
                v_index++;
                is_u = 1;
            }
        }
    }

    return 1;
}


//yuyv422转rgb24
void yuyv422_to_rgb24(unsigned char *yuv_buffer,unsigned char *rgb_buffer,int iWidth,int iHeight)
{
    int x;
    int z=0;
    unsigned char *ptr = rgb_buffer;
    unsigned char *yuyv= yuv_buffer;
    for (x = 0; x < iWidth*iHeight; x++)
    {
        int r, g, b;
        int y, u, v;

        if (!z)
        y = yuyv[0] << 8;
        else
        y = yuyv[2] << 8;
        u = yuyv[1] - 128;
        v = yuyv[3] - 128;

        r = (y + (359 * v)) >> 8;
        g = (y - (88 * u) - (183 * v)) >> 8;
        b = (y + (454 * u)) >> 8;

        *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r);
        *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g);
        *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b);

        if(z++)
        {
            z = 0;
            yuyv += 4;
        }
    }
}

void yuv422sp_to_yuv422p( unsigned  char* yuv422sp,  unsigned  char* yuv422p,  int width,  int height)
{
     int i, j;
     int y_size;
     int uv_size;
     unsigned  char* p_y1;
     unsigned  char* p_uv;

     unsigned  char* p_y2;
     unsigned  char* p_u;
     unsigned  char* p_v;

    y_size = uv_size = width * height;

    p_y1 = yuv422sp;
    p_uv = yuv422sp + y_size;

    p_y2 = yuv422p;
    p_u  = yuv422p + y_size;
    p_v  = p_u + width * height /  2;

    memcpy(p_y2, p_y1, y_size);

     for (j =  0, i =  0; j < uv_size; j+= 2, i++)
    {
        p_u[i] = p_uv[j];
        p_v[i] = p_uv[j+ 1];
    }
}


//YUV420转RGB24
void YUV420P_TO_RGB24(unsigned char *yuv420p, unsigned char *rgb24, int width, int height) {
    int index = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int indexY = y * width + x;
            int indexU = width * height + y / 2 * width / 2 + x / 2;
            int indexV = width * height + width * height / 4 + y / 2 * width / 2 + x / 2;

            unsigned char Y = yuv420p[indexY];
            unsigned char U = yuv420p[indexU];
            unsigned char V = yuv420p[indexV];

            rgb24[index++] = Y + 1.402 * (V - 128); //R
            rgb24[index++] = Y - 0.34413 * (U - 128) - 0.71414 * (V - 128); //G
            rgb24[index++] = Y + 1.772 * (U - 128); //B
        }
    }
}

//yuyv422转yuv422
void YUYV422ToYUV422(char* in, unsigned char* out, unsigned int width, unsigned int height)
{
    unsigned int total(width * height);

    char* in_y(in);
    char* in_u(in + 1);
    char* in_v(in + 3);

    char* out_y((char*)out);
    char* out_u((char*)out + width * height);
    char* out_v((char*)out + width * height + 1);

    for (unsigned int i(0); i < total; i += 10)
    {
        *out_y = *in_y; out_y++; in_y += 2;
        *out_y = *in_y; out_y++; in_y += 2;
        *out_u = *in_u; out_u += 2; in_u += 4;
        *out_v = *in_v; out_v += 2; in_v += 4;

        *out_y = *in_y; out_y++; in_y += 2;
        *out_y = *in_y; out_y++; in_y += 2;
        *out_u = *in_u; out_u += 2; in_u += 4;
        *out_v = *in_v; out_v += 2; in_v += 4;

        *out_y = *in_y; out_y++; in_y += 2;
        *out_y = *in_y; out_y++; in_y += 2;
        *out_u = *in_u; out_u += 2; in_u += 4;
        *out_v = *in_v; out_v += 2; in_v += 4;

        *out_y = *in_y; out_y++; in_y += 2;
        *out_y = *in_y; out_y++; in_y += 2;
        *out_u = *in_u; out_u += 2; in_u += 4;
        *out_v = *in_v; out_v += 2; in_v += 4;

        *out_y = *in_y; out_y++; in_y += 2;
        *out_y = *in_y; out_y++; in_y += 2;
        *out_u = *in_u; out_u += 2; in_u += 4;
        *out_v = *in_v; out_v += 2; in_v += 4;
    }
}

//写文件
int writeFile(FILE *fp, void *data, int len){
    if ( fp == nullptr || !data )
        return -1;

    return fwrite(data, len, 1, fp);
}

//读取一帧
int readFrame(int count)
{
    printf("readFrame - %d\n", count);
    struct v4l2_buffer buff;
    memset(&buff, 0, sizeof (buff));
    buff.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buff.memory = V4L2_MEMORY_MMAP;

    //第三个参数存放地址, 否则没办法拿到帧数据
    if ( 0 == ioctl(fd, VIDIOC_DQBUF, &buff) )      //从缓冲区取出一个缓冲帧
    {
        memset(pYUV420Buff, 0, 3 * WIDTH * HEIGHT / 2);
        printf("%p - frame Len: %d\n", datas[buff.index], buff.length);
        //yuyv转yuv420
        yuyv_to_yuv420p(datas[buff.index], pYUV420Buff, WIDTH, HEIGHT);
        wLen = writeFile(f420, pYUV420Buff, 3 * WIDTH * HEIGHT / 2);
        printf("yuv420 writelen: %d\n", wLen);
        //显示一帧到fb输出
    }else{
        perror("get frame fail.\n");
    }

    //将应用层的将帧的缓冲区重新放入视频采集队列
    if ( -1 == ioctl(fd, VIDIOC_QBUF, &buff) )
        return 0;

    return 1;
}

//循环读取
void mainLoop()
{
    //先获取100帧数据, 并写文件
    int count = 50;
    while ( count-- > 0 )
    {
        for(;;){
            //等待获取帧
            fd_set fds;
            struct timeval tv;
            //超时时间设置
            tv.tv_sec = 10;
            tv.tv_usec = 0;

            FD_ZERO(&fds);
            FD_SET(fd, &fds);

            //监听设备套接字
            int res = select(fd + 1, &fds, NULL, NULL, &tv);
            //失败
            if (-1 == res) {
                if (EINTR == errno)
                    continue;
                printf("error.\n");
                exit(0);
            }

            //超时
            if (0 == res) {
                printf("TimeOut.\n");
                exit(0);
            }

            //正常读取一帧
            if ( readFrame(count) ){
                printf("count - %d\n", count);
                break;
            }
        }
    }
}

void stop_capturing(void) {

    enum v4l2_buf_type type;
}



void fun_sig(int sig)
{
    printf("\nsig = %d\n",sig);

    //停止数据流
    enum v4l2_buf_type type;
    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    if ( -1 == ioctl(fd, VIDIOC_STREAMOFF &type) ){
        perror("stop Capture fail.\n");
    }

    //关闭句柄
    fclose(fl);
    fclose(f420);
    fclose(frgb32);
    fclose(f422);
    close(fd);
    exit(0);
}

int main(int argc, char **argv)
{
    //捕捉系统Ctrl+C信号
    signal(SIGINT, fun_sig);
    //捕捉段错误信号
    signal(SIGSEGV, fun_sig);

    /* 第一步:打开摄像头设备文件 */
    int ret, i;
    fd = open(VIDEO_FILE, O_RDWR);
    if (-1 == fd){
        perror("open /dev/video0 fail.\n");
        return -1;
    }

    //获取设备属性, 包括图像的格式
    struct v4l2_fmtdesc fmtdesc;
    memset(&fmtdesc, 0, sizeof (fmtdesc));
    fmtdesc.index = 0;
    fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;         //操作类型为获取图片
    while( ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) == 0 )
    {
        printf("index: %d, description: %s\n", fmtdesc.index+1, fmtdesc.description);
        fmtdesc.index++;
    }

    /* 第二步:设置捕捉图片帧格式 */
    struct v4l2_format format;
    format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  // 操作类型为获取图片
    format.fmt.pix.width = WIDTH;               // 图片的宽度
    format.fmt.pix.height = HEIGHT;             // 图片的高度
    format.fmt.pix.pixelformat = FMT;           // 图片格式
    ret = ioctl(fd, VIDIOC_S_FMT, &format);     // 进行设置(Set)
    if (-1 == ret)
    {
        perror("ioctl VIDIOC_S_FMT");
        close(fd);
        return -2;
    }

    /* 第三步:检查是否设置成功 */
    ret = ioctl(fd, VIDIOC_G_FMT, &format);
    if (-1 == ret)
    {
        perror("ioctl VIDIOC_G_FMT");
        close(fd);
        return -3;
    }
    if (format.fmt.pix.pixelformat == FMT)
    {
        printf("ioctl VIDIOC_S_FMT sucessful\n");
    }
    else{
        printf("ioctl VIDIOC_S_FMT failed\n");
    }

    /* 第四步:让摄像头驱动申请存放图像数据的缓冲区 */
    struct v4l2_requestbuffers reqbuf;
    reqbuf.count = COUNT;                       // 缓冲区个数
    reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  // 缓冲区类型
    reqbuf.memory = V4L2_MEMORY_MMAP;           // 缓冲区的用途:用于内存映射
    ret = ioctl(fd, VIDIOC_REQBUFS, &reqbuf);
    if (-1 == ret)
    {
        perror("ioctl VIDIOC_REQBUFS");
        close(fd);
        return -4;
    }

    /* 第五步:查询每个缓冲区的信息,同时进行内存映射 */
    struct v4l2_buffer buff;
    buff.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    buff.memory = V4L2_MEMORY_MMAP;
    for (i = 0; i < COUNT; i++)
    {
        buff.index = i;
        ret = ioctl(fd, VIDIOC_QUERYBUF, &buff);
        if (-1 == ret)
            break;

        /* 打印缓冲区的长度和偏移量 */
        printf("buf[%d]: len = %d offset: %d\n", i, buff.length, buff.m.offset);

        /* 把每块缓冲区映射到当前进程来 */
        datas[i] = (unsigned char*)mmap(NULL, buff.length, PROT_READ, MAP_SHARED, fd, buff.m.offset);
        if (MAP_FAILED == datas[i])             // 映射失败
        {
            perror("mmap failed");
            return -5;
        }

        /* 把映射成功的缓冲区加入到摄像头驱动的图像数据采集队列里 */
        ret = ioctl(fd, VIDIOC_QBUF, &buff);    // Queue
        if (-1 == ret)
        {
            perror("VIDIOC_QBUF");
            return -6;
        }
    }

    /* 第六步:启动采集 */
    int on = V4L2_BUF_TYPE_VIDEO_CAPTURE;       // 设置启动标志位
    ret = ioctl(fd, VIDIOC_STREAMON, &on);      // 开启摄像头流
    if (-1 == ret)
    {
        perror("ioctl VIDIOC_STREAMON");
        close(fd);
        return -7;
    }

    //申请YUV420图像内存(空间大小为 3 * width * height / 2 )
    //申请RGB32图像内存(空间大小为3 * width * height )
    pYUV420Buff = new uint8_t[3 * WIDTH * HEIGHT / 2];
    pRGBBuff = new unsigned char [3 * WIDTH * HEIGHT];
    pYUV422 = new unsigned char [2 * LEN_YUV422];

    fl = fopen(FILE_NAME, "w");
    if (NULL == fl)
    {
        fprintf(stderr, "open %s failed.\n", FILE_NAME);
        close(fd);
        return -1;
    }

    f420 = fopen(FILE_YUV420, "w");
    if ( NULL == f420 )
    {
        fprintf(stderr, "open %s failed.\n", FILE_YUV420);
        close(fd);
        return -1;
    }

    f422 = fopen(FILE_YUV422, "w");
    if (  f422 == nullptr )
    {
        fprintf(stderr, "open %s failed.\n", FILE_YUV422);
        close(fd);
        return -1;
    }

    frgb32 = fopen(FILE_RGB, "w");
    if ( NULL == frgb32 )
    {
        fprintf(stderr, "open %s failed.\n", FILE_RGB);
        close(fd);
        return -1;
    }

    /* 第七步: 循环获取视频帧数据 */
    mainLoop();

    /* 第八步: 关闭文件句柄 */
    fclose(fl);
    fclose(f420);
    fclose(frgb32);
    fclose(f422);
    close(fd);

    return 0;
}

由于从摄像头读出来的像素格式是YUYV,需要转换成屏幕显示的像素格式,比如YUV420SP,YUV420P等;

 

参考链接:  

Linux - 使用V4L2(总结)_v4l2用法-CSDN博客

v4l2的学习建议和流程解析 - silenceer - 博客园 (cnblogs.com)

 

标签:int,unsigned,介绍,char,width,fd,简单,V4l2,out
From: https://www.cnblogs.com/weijian168/p/17989794

相关文章

  • 【C++】 select函数介绍
    在使用C++语言开发网络应用程序时,常常需要用到select函数。select函数是一种多路复用机制,可以同时监听多个文件描述符上可读、可写、异常等事件,从而让程序能够高效地处理多个连接。下面详细介绍C++中的select函数。1.select函数的定义和作用select函数的定义为:intselect(intn......
  • @PostConstruct用法详解介绍
    1.@PostConstruct介绍定义:在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。说明:被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法......
  • rsync介绍
    参考:https://blog.csdn.net/d1240673769/article/details/130677855https://www.cnblogs.com/sunsky303/p/9153124.html介绍Rsync(remotesynchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间......
  • 29-数据结构介绍
          ......
  • 通达信简单上下轨道指标公式源码副图
    VAR2:=MA(ADVANCE-DECLINE,3)/100;VAR3:=MA(CLOSE,5)*1.01;VAR4:=MA(OPEN,13)*1.01;VAR5:=(MA(HIGH,3)+MA(LOW,5)+MA(CLOSE,3)+MA(OPEN,3)+CLOSE+OPEN)/6+VAR2;VAR6:=IF(VAR4>VAR5,VAR4,VAR5);VAR7:=IF(VAR4<VAR5,VAR4,VAR5);VAR8:=3/100;下轨:MA(VAR7,5)*(1-VAR8......
  • 简单记录一下如何安装python以及pycharm(图文教程)(可供福建专升本理工类同学使用)
    本教程主要给不懂计算机的或者刚刚开始学习python的同学(福建专升本理工类)&网友学习使用,基础操作,比较详细,其他问题等待补充!安装Python1.进入python官网(https://www.python.org/),选择导航栏中的Downloads,然后把鼠标移到windows(你目前使用的操作系统),点击downloadforwindows下面的p......
  • 简单高效的语言
    php和python这两种语言,可以快速开发 python发送邮件#!/usr/bin/python#-*-coding:UTF-8-*-importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeadersender='from@runoob.com'receivers=['429240967@qq.com']#接收邮件,......
  • COCO数据集介绍
    数据集介绍MSCOCO的全称是MicrosoftCommonObjectsinContext,起源于微软于2014年出资标注的MicrosoftCOCO数据集,与ImageNet竞赛一样,被视为是计算机视觉领域最受关注和最权威的比赛之一。该数据集特点如下:ObjectsegmentationRecognitionincontextSuperpixelstuffseg......
  • TSINGSEE青犀智慧水电站视频智能监控系统方案设计与技术应用介绍
    一、背景需求水电站作为国家重要的能源基地,其安全运行对于保障能源供应和社会稳定具有重要意义。然而,传统的人工监控方式存在着诸多问题,如人力成本高、监控范围有限、反应不及时等。因此,水电站急需引进一种先进的视频智能监控系统,以提高监控效率和安全性。二、方案设计TSINGSEE......
  • MixGo CC主控板简单介绍
    MixGoCC是一款为信息科技教育而生的主控板。在兼顾常见的电子功能基础上,与MixGoCE主控(还不知道MixGoCE主控板,点此跳转)相比,使用更大的显示屏进行内容及图像显示,同时集成RFID射频识别、蓝牙相关的功能,功能更加丰富。一、图片示例:正面反面功能说明图二、连接说明打开Mix......