Linux V4L2之camera

一、硬件知识

1. 摄像头硬件结构和工作原理,如图1&图2 

  

 

  外部光线穿过lens镜头,经过红外滤光片后光学图像投射到传感器上,然后光学图像被转换成电信号,电信号再经过模数转换变为数字信号,数字信号经过DSP加工处理,

再被送到接收端进行处理,最终转换成屏幕上能够看到的图像。 其中:

1)滤光片作用:

  a. 滤除红外线,滤除对可见光有干扰的红外光,使成像效果更清晰。

  b. 修整进来的光线,感光芯片由感光体(CELL)构成,最好的光线是直射进来,但为了怕干扰到邻近感光体

           就需要对光线加以修整,因此那片滤光片不是玻璃,而是石英片,利用石英的物理偏光特性,把进来

           的光线保留直射部份,反射掉斜射部份,避免去影响旁边的感光点。

2)常见的sensor传感器主要有两种:

  a. CCD传感器(Chagre Couled Device),即电荷耦合器。

  b. CMOS传感器(Complementary Metal-Oxide Semiconductor),即互补性金属氧化物半导体。

          CCD的优势在于成像质量好,但是制造工艺复杂,成本高昂,且耗电高。在相同分辨率下,CMOS价格

     比CCD便宜,但图像质量相比CCD来说要低一些。CMOS影像传感器相对CCD具有耗电低的优势,加上随

          着工艺技术的进步,CMOS的画质水平也不断地在提高,所以目前市面上的手机摄像头都采用CMOS传感器。

3)DSP (DIGITAL SIGNAL PROCESSING):

  主要是通过一系列复杂的数学算法运算,对数字图像信号参数进行优化处理,并把处理后的信号通过USB等接口传到PC等设备。结构框架:  ISP(image signal processor)(镜像信号处理器)、JPEG encoder(JPEG图像解码器)、USB device controller(USB设备控制器)

 

  对于低分辨率来说(300W像素以下), 一般摄像头自带DSP/ISP处理模块,提供简单的自动白平衡、 gamma、sharpness等功能,而高分辨率或者需要提供更增强功能时,可以使用处理器自带的ISP模块(前提是处理器有)。

一般ISP支持输出YUV、RGB、JPEG格式。

 

2. 摄像头引脚作用&硬件连接:  

  目前摄像头接口主要采用MIPI CSI & DVP, 前者是串行(多组差分信号线), 后者是并口传输(8/10bit)。 图3以DVP接口介绍各个引脚作用及硬件连接:

  

DVP分为三个部分:

1)输入总线:

   a. data为sensor的数据管脚,可输出8/10bit并口数据到处理器接受端。

   b. VSYNC为帧同步信号管脚,一个VSYNC信号结束表示一个画面的数据已经传输完毕。

   c. HSYNC为行同步信号管脚,一个HSYNC信号结束表示一行的数据已经传输完毕。

   p. PCLK为像素同步信号管脚,一个PCLK信号结束表示一个像素的数据(大小取决格式)已经传输完毕。

  以上管脚的关系如下图:

             

2)输出总线 :

   a. PDN(power down enable), camera使能管脚,当PDN=1时, 一切对camera操作都是无效的。

   b. RESET, 复位管脚, 低电平有效。

   c. XCLK(MCLK), sensor的工作时钟管脚,可由外部晶振或者处理器提供。

   d. I2C总线, 处理器与sensor通信管脚, 用于配置sensor。

 

3)Power:

   a. AVDD 模拟电压

   b. DOVDD GPIO口数字电压

   c. DVDD 核工作电压

 

 

二、成像原理

  本节主要讲解目前主流的bayer格式图片的成像原理, bayer格式图片是伊士曼·柯达公司科学家Bryce Bayer发明的,Bryce Bayer所发明的拜耳阵列被广泛运用数字图像。

   对于彩色图像,需要采集最基本的颜色,如RGB三种颜色,最简单的方法就是用滤镜的方法,红色的滤镜透过红色的波长,绿色的滤镜透过绿色的波长,蓝色的滤镜透过蓝色的波长。如果要同时采集三个基本色,则需要三块滤镜,这样价格昂贵,且不好制造,因为三块滤镜都必须保证每一个像素点都对齐。当用bayer格式的时候,很好的解决了这个问题。bayer 格式在每个像素(pixel)上只设置一种颜色滤镜,外部光线在每个像素点上存储是单色的, 因此经过ADC转换出来的原始数据称为RAW RGB DATA,通过分析人眼对颜色的感知发现,人眼对绿色比较敏感,所以一般bayer格式的图片绿色格式的像素是是R和B像素的和。

  

  当Image Sensor往外逐行输出数据时,像素的序列为GRGRGR.../BGBGBG...(交替)。这样阵列的设计,使得RGB传感器为全色传感器的1/3。

  每一个像素仅仅包括了光谱的一部分,必须通过插值来实现每个像素的RGB值。为了从Bayer格式得到每个像素的RGB格式,我们需要通过插值填补缺失的2个色彩。插值的方法有很多(包括领域、线性、3*3等),速度与质量权衡,最好是线性插值补偿算法。

  从图5 Sensor 像素阵列来看, 存在4中分布格式:

                       

  

  对于图6(a)(b)来说, G像素的R、B分量分别取两个邻域的平均値,由于存在(a)(b)两种分布情况,所以直接去4个相邻域的平均値既是G像素的R&B分量。

  对于图6(c)来说,R像素的G分量可以取4个相邻域的平均值,B分量取外围4个域的平均值。

  对于图6(d)来说,B像素的G分量可以取4个相邻域的平均值,R分量取外围4个域的平均值。

  当然, RAW RGB DATA也可以转换成YUV格式或者 先转成RGB再转换YUV,就不详解了......

 

三、V4L2软件架构

1. 概述

         Video4 for Linux 2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口。凡是内核中的子系统都有抽象底层硬件的差异,为上层提供统一的接口和提取出公共代码避免代码冗余等。 V4L2支持三类设备:视频输入输出设备、VBI设备和radio设备(其实还支持更多类型的设备,暂不讨论),分别会在/dev目录下产生videoX、radioX和vbiX设备节点。 图7是V4L2在linux系统中的结构图:

 

  

 

Linux系统中视频输入设备主要包括以下四个部分:

  字符设备驱动:V4L2本身就是一个字符设备,具有字符设备所有的特性,暴露接口给用户空间;

  V4L2驱动核心:主要是构建一个内核中标准视频设备驱动的框架,为视频操作提供统一的接口函数;

  平台V4L2设备驱动:在V4L2框架下,根据平台自身的特性实现与平台相关的V4L2驱动部分,包括注册video_device和v4l2_dev;

  具体的sensor驱动:主要上电、提供工作时钟、视频图像裁剪、流IO开启等,实现各种设备控制方法供上层调用并注册v4l2_subdev。

 

 

2. 详解V4L2框架

  v4L2的核心源码位于drivers/media/v4l2-core,根据功能可以划分为四类:

  字符设备模块:由v4l2-dev.c实现,主要作用申请字符主设备号、注册class和提供video device注册注销等相关函数;

  V4L2基础框架:由v4l2-device.c、v4l2-subdev.c、v4l2-fh.c、v4l2-ctrls.c等文件构建V4L2基础框架;

  videobuf管理:由videobuf2-core.c、videobuf2-dma-contig.c、videobuf2-dma-sg.c、videobuf2-memops.c、videobuf2-vmalloc.c、v4l2-mem2mem.c等文件实现,完成videobuffer的分配、管理和注销;

  Ioctl框架:由v4l2-ioctl.c文件实现,构建V4L2ioctl的框架。

 

2.1 V4L2基础框架如图8:

  

  上图V4L2框架是一个标准的树形结构,v4l2_device充当了父设备,通过链表把所有注册到其下的子设备管理起来,这些设备可以是GRABBER、VBI或RADIO。V4l2_subdev是子设备,v4l2_subdev结构体包含了对设备操作的ops和ctrls,这部分代码和硬件相关,需要驱动工程师根据硬件实现控制上下电、读取ID、饱和度、对比度和视频数据流打开关闭等接口函数。Video_device用于创建子设备节点,把操作设备的接口暴露给用户空间。V4l2_fh是每个子设备的文件句柄,在打开设备节点文件时设置,方便上层索引到v4l2_ctrl_handler,v4l2_ctrl_handler管理设备的ctrls,这些ctrls(摄像头设备)包括调节饱和度、对比度和白平衡等。

         结构体v4l2_device、video_device、v4l2_subdev和v4l2_ctrl_handler是构成框架的主要元素,现分别介绍:

 

1. struct v4l2_device :
    v4l2_device在v4l2框架中充当所有v4l2_subdev的父设备,管理着注册在其下的子设备
    
struct v4l2_device {
    structlist_head subdevs;  //用链表管理注册的subdev
    charname[V4L2_DEVICE_NAME_SIZE];  //device 名字
    structkref ref;  //引用计数
    .........
};

    可以看出v4l2_device的主要作用是管理注册在其下的子设备,方便系统查找引用到。
v4l2_device的注册和注销:
    int v4l2_device_register(struct device*dev, struct v4l2_device *v4l2_dev)
    static void v4l2_device_release(struct kref *ref)

2. struct v4l2_subdev :
    v4l2_subdev代表子设备,包含了子设备的相关属性和操作。结构体原型:

struct v4l2_subdev {
    struct v4l2_device *v4l2_dev;  //指向父设备
    conststruct v4l2_subdev_ops *ops; //提供一些控制v4l2设备的接口
    conststruct v4l2_subdev_internal_ops *internal_ops; //向V4L2框架提供的接口函数
    structv4l2_ctrl_handler *ctrl_handler; //subdev控制接口
    charname[V4L2_SUBDEV_NAME_SIZE]; 
    struct video_device *devnode;  
    ..........
};

    每个子设备驱动都需要实现一个v4l2_subdev结构体,v4l2_subdev可以内嵌到其它结构体中,也可以独立使用。
    结构体中包含了对子设备操作的成员v4l2_subdev_ops和v4l2_subdev_internal_ops
            struct v4l2_subdev_ops {
                const struct v4l2_subdev_core_ops *core; //视频设备通用的操作:初始化、加载FW、上电和RESET等
                const struct v4l2_subdev_tuner_ops *tuner; //tuner特有的操作
                const struct v4l2_subdev_audio_ops *audio; //audio特有的操作
                const struct v4l2_subdev_video_ops *video; //视频设备的特有操作:裁剪图像、开关视频流等
                const struct v4l2_subdev_pad_ops *pad;
                ..........
            };
            struct v4l2_subdev_internal_ops {
                /* 当subdev注册时被调用,读取IC的ID来进行识别 */
                int(*registered)(struct v4l2_subdev *sd);
                void(*unregistered)(struct v4l2_subdev *sd);
                /* 当设备节点被打开时调用,通常会给设备上电和设置视频捕捉FMT */
                int(*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
                int(*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
            };

    视频设备通常需要实现core和video成员,这两个OPS中的操作都是可选的,但是对于视频流设备video->s_stream(开启或关闭流IO)必须要实现。v4l2_subdev_internal_ops是向V4L2框架提供的接口,只能被V4L2框架层调用。在注册或打开子设备时,进行一些辅助性操作。
    Subdev的注册和注销:
                        int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev, struct v4l2_subdev *sd)
                        void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)


3. struct video_device
    video_device结构体用于在/dev目录下生成设备节点文件,把操作设备的接口暴露给用户空间

struct video_device
{
    const struct v4l2_file_operations *fops;  //V4L2设备操作集合
    struct cdev *cdev; //字符设备

    struct v4l2_device *v4l2_dev;
    struct v4l2_ctrl_handler *ctrl_handler;

    struct vb2_queue *queue; //指向video buffer队列
    int vfl_type;      /* device type */

    intminor;  //次设备号

    /*ioctl回调函数集,提供file_operations中的ioctl调用 */
    const struct v4l2_ioctl_ops *ioctl_ops;
    ..........
};
    
    Video_device分配和释放, 用于分配和释放video_device结构体:
        struct video_device *video_device_alloc(void)
        void video_device_release(struct video_device *vdev)
    video_device注册和注销,实现video_device结构体的相关成员后,就可以调用下面的接口进行注册:
        static inline int __must_check video_register_device(struct video_device *vdev, inttype, int nr)
        void video_unregister_device(struct video_device*vdev);
            vdev:需要注册和注销的video_device;
            type:设备类型,包括VFL_TYPE_GRABBER、VFL_TYPE_VBI、VFL_TYPE_RADIO和VFL_TYPE_SUBDEV。
            nr:设备节点名编号,如/dev/video[nr]。

4. struct v4l2_ctrl_handler
    v4l2_ctrl_handler是用于保存子设备控制方法集的结构体,结构体如下: 
struct v4l2_ctrl_handler {
    struct list_head ctrls;
    struct list_head ctrl_refs;
    struct v4l2_ctrl_ref *cached;
    struct v4l2_ctrl_ref **buckets;
    v4l2_ctrl_notify_fnc notify;
    u16 nr_of_buckets;
    int error;
};

    其中成员ctrls作为链表存储包括设置亮度、饱和度、对比度和清晰度等方法,可以通过v4l2_ctrl_new_xxx()函数创建具体方法并添加到链表ctrls。

 

 

2.2 videobuf管理

  在讲解v4l2的buffer管理前,先介绍v4l2的IO访问, V4L2支持三种不同IO访问方式(内核中还支持了其它的访问方式,暂不讨论):

  read和write:是基本帧IO访问方式,通过read读取每一帧数据,数据需要在内核和用户之间拷贝,这种方式访问速度可能会非常慢;

  内存映射缓冲区(V4L2_MEMORY_MMAP):是在内核空间开辟缓冲区,应用通过mmap()系统调用映射到用户地址空间。这些缓冲区可以是大而连续DMA缓冲区、通过vmalloc()创建的虚拟缓冲区,或者直接在设备的IO内存中开辟的缓冲区(如果硬件支持);

  用户空间缓冲区(V4L2_MEMORY_USERPTR):是用户空间的应用中开辟缓冲区,用户与内核空间之间交换缓冲区指针。很明显,在这种情况下是不需要mmap()调用的,但驱动为有效的支持用户空间缓冲区,其工作将也会更困难。

  read和write方式属于帧IO访问方式,每一帧都要通过IO操作,需要用户和内核之间数据拷贝,而后两种是流IO访问方式,不需要内存拷贝,访问速度比较快。内存映射缓冲区访问方式是比较常用的方式。

  现以V4L2_MEMORY_MMAP简单介绍数据流通过程:

             

 

   Camera sensor捕捉到图像数据通过并口或MIPI传输到CAMIF(camera interface),CAMIF可以对图像数据进行调整(翻转、裁剪和格式转换等)。然后DMA控制器设置DMA通道请求AHB将图像数据传到分配好的DMA缓冲区。待图像数据传输到DMA缓冲区之后,mmap操作把缓冲区映射到用户空间,应用就可以直接访问缓冲区的数据。而为了使设备支持流IO这种方式,v4l2需要实现对video buffer的管理,即实现:

/* vb2_queue代表一个videobuffer队列,vb2_buffer是这个队列中的成员,vb2_mem_ops是缓冲内存的操作函数集,vb2_ops用来管理队列 */
struct vb2_queue {
    enum v4l2_buf_type type;  //buffer类型
    unsigned int io_modes;  //访问IO的方式:mmap、userptr etc
    const struct vb2_ops *ops;  //buffer队列操作函数集合
    const struct vb2_mem_ops *mem_ops;  //buffer memory操作集合
    struct vb2_buffer *bufs[VIDEO_MAX_FRAME];  //代表每个frame buffer
    unsignedint num_buffers;  //分配的buffer个数
    ..........
};


/* vb2_mem_ops包含了内存映射缓冲区、用户空间缓冲区的内存操作方法 */
struct vb2_mem_ops {
    void *(*alloc)(void *alloc_ctx, unsignedlong size);  //分配视频缓存
    void (*put)(void *buf_priv);  //释放视频缓存

    /* 获取用户空间视频缓冲区指针 */
    void *(*get_userptr)(void *alloc_ctx, unsigned long vaddr, unsignedlong size, int write);
    void (*put_userptr)(void *buf_priv);  //释放用户空间视频缓冲区指针
    /* 用于缓存同步 */
    void (*prepare)(void *buf_priv);
    void (*finish)(void *buf_priv);
    /* 缓存虚拟地址 & 物理地址 */
    void *(*vaddr)(void *buf_priv);
    void *(*cookie)(void *buf_priv);

    unsignedint (*num_users)(void *buf_priv);  //返回当期在用户空间的buffer数
    int (*mmap)(void *buf_priv, structvm_area_struct *vma);  //把缓冲区映射到用户空间
    ..............
};

/* mem_ops由kernel自身实现并提供了三种类型的视频缓存区操作方法:连续的DMA缓冲区、集散的DMA缓冲区以及vmalloc创建的缓冲区,分别由videobuf2-dma-contig.c、videobuf2-dma-sg.c和videobuf-vmalloc.c文件实现,可以根据实际情况来使用。*/


/* vb2_ops是用来管理buffer队列的函数集合,包括队列和缓冲区初始化等 */
struct vb2_ops {
    //队列初始化
    int(*queue_setup)(struct vb2_queue *q, const struct v4l2_format *fmt,
                       unsigned int *num_buffers, unsigned int*num_planes,
                       unsigned int sizes[], void *alloc_ctxs[]);

    //释放和获取设备操作锁
    void(*wait_prepare)(struct vb2_queue *q);
    void(*wait_finish)(struct vb2_queue *q);

    //对buffer的操作
    int(*buf_init)(struct vb2_buffer *vb);
    int(*buf_prepare)(struct vb2_buffer *vb);
    int(*buf_finish)(struct vb2_buffer *vb);
    void(*buf_cleanup)(struct vb2_buffer *vb);

    //开始/停止视频流
    int(*start_streaming)(struct vb2_queue *q, unsigned int count);
    int(*stop_streaming)(struct vb2_queue *q);

    //把VB传递给驱动,以填充frame数据
    void(*buf_queue)(struct vb2_buffer *vb);
};

  

  一个frame buffer(vb2_buffer/v4l2_buffer)可以有三种状态:

    1. 在驱动的输入队列中,驱动程序将会对此队列中的缓冲区进行处理,用户空间通过IOCTL:VIDIOC_QBUF 把缓冲区放入到队列。对于一个视频捕获设备,传入队列中的缓冲区是空的,驱动会往其中填充数据;

    2. 在驱动的输出队列中,这些缓冲区已由驱动处理过,对于一个视频捕获设备,缓存区已经填充了视频数据,正等用户空间来认领;

    3. 用户空间状态的队列,已经通过IOCTL:VIDIOC_DQBUF传出到用户空间的缓冲区,此时缓冲区由用户空 间拥有,驱动无法访问。

  这三种状态的切换如下图所示:

                 

 

  

         最终落脚点的struct v4l2_buffer结构如下:

struct v4l2_buffer {
    __u32 index;  //buffer 序号
    __u32 type;  //buffer类型
    __u32 bytesused;  //缓冲区已使用byte数
    structtimeval timestamp;  //时间戳,代表帧捕获的时间

    __u32 memory;  //表示缓冲区是内存映射缓冲区还是用户空间缓冲区
    union {
        __u32 offset;  //内核缓冲区的位置
        unsignedlong userptr;   //缓冲区的用户空间地址
        structv4l2_plane *planes;
        __s32 fd;
    } m;
    __u32 length;   //缓冲区大小,单位byte
};

  当用户空间拿到v4l2_buffer,可以获取到缓冲区的相关信息。Byteused是图像数据所占的字节数,如果是V4L2_MEMORY_MMAP方式,m.offset是内核空间图像数据存放的开始地址,会传递给mmap函数作为一个偏移,通过mmap映射返回一个缓冲区指针p,p+byteused是图像数据在进程的虚拟地址空间所占区域;如果是用户指针缓冲区的方式,可以获取的图像数据开始地址的指针m.userptr,userptr是一个用户空间的指针,userptr+byteused便是所占的虚拟地址空间,应用可以直接访问

 

2.3 Ioctl框架如图:

  

  用户空间通过打开/dev/目录下的设备节点,获取到文件的file结构体,通过系统调用ioctl把cmd和arg传入到内核。通过一系列的调用后最终会调用到__video_do_ioctl函数,然后通过cmd检索v4l2_ioctls[],判断是INFO_FL_STD还是INFO_FL_FUNC。如果是INFO_FL_STD会直接调用到视频设备驱动中video_device->v4l2_ioctl_ops函数集。如果是INFO_FL_FUNC会先调用到v4l2自己实现的标准回调函数,然后根据arg再调用到video_device->v4l2_ioctl_ops或v4l2_fh->v4l2_ctrl_handler函数集。

 

四、用户空间访问 camera & 示例程序

 

/* 
 *  V4L2 video capture example 
 * 
 *  This program can be used and distributed without restrictions. 
 * 
 *      This program is provided with the V4L2 API 
 * see http://linuxtv.org/docs.php for more information 
 */  
  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <assert.h>  
  
#include <getopt.h>             /* getopt_long() */  
  
#include <fcntl.h>              /* low-level i/o */  
#include <unistd.h>  
#include <errno.h>  
#include <sys/stat.h>  
#include <sys/types.h>  
#include <sys/time.h>  
#include <sys/mman.h>  
#include <sys/ioctl.h>  
#include <linux/videodev2.h>  

#define CLEAR(x) memset(&(x), 0, sizeof(x))  
  
enum io_method {  
        IO_METHOD_READ,  
        IO_METHOD_MMAP,  
        IO_METHOD_USERPTR,  
};  
  
struct buffer {  
        void   *start;  
        size_t  length;  
};  
  
static char            *dev_name;  
static enum io_method   io = IO_METHOD_MMAP;  
static int              fd = -1;  
struct buffer          *buffers;  
static unsigned int     n_buffers;  
static int              out_buf;  
static int              force_format;  
static int              frame_count = 4;  
 
static void errno_exit(const char *s)  
{  
        fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));  
        exit(EXIT_FAILURE);  
}  
  
static int xioctl(int fh, int request, void *arg)  
{  
        int r;  
  
        do {  
                r = ioctl(fh, request, arg);  
        } while (-1 == r && EINTR == errno);  
  
        return r;  
}  
  
static void process_image(const void *p, int size)  
{
        if (out_buf)  
                fwrite(p, size, 1, stdout);  
  
        fflush(stderr);  
        fprintf(stderr, ".");  
        fflush(stdout);  
}  

static void store_image(const char *buf_start, int size, int index)
{
    char path[20];
    
    snprintf(path, sizeof(path), "./yuyv%d.yuv", index); 
    int fd = open(path, O_WRONLY|O_CREAT, 00700);
        if (-1 == fd) {  
                fprintf(stderr, "Cannot open '%s': %d, %s\n",  
                         path, errno, strerror(errno));  
                exit(EXIT_FAILURE);  
        }  

    write(fd, buf_start, size);  
    close(fd);  
}

static int read_frame(void)  
{  
        struct v4l2_buffer buf;  
        unsigned int i;  
  
        switch (io) {  
        case IO_METHOD_READ:  
                if (-1 == read(fd, buffers[0].start, buffers[0].length)) {  
                        switch (errno) {  
                        case EAGAIN:  
                                return 0;  
  
                        case EIO:  
                                /* Could ignore EIO, see spec. */  
  
                                /* fall through */  
  
                        default:  
                                errno_exit("read");  
                        }  
                }  
  
                process_image(buffers[0].start, buffers[0].length);  
                break;  
  
        case IO_METHOD_MMAP:  
                CLEAR(buf);  
  
                buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                buf.memory = V4L2_MEMORY_MMAP;  
                if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {  
                        switch (errno) {  
                        case EAGAIN:  
                                return 0;  
  
                        case EIO:  
                                /* Could ignore EIO, see spec. */  
  
                                /* fall through */  
  
                        default:  
                                errno_exit("VIDIOC_DQBUF");  
                        }  
                }  
                assert(buf.index < n_buffers);  

          //printf("buf.bytesused = %d\n", buf.bytesused);
                process_image(buffers[buf.index].start, buf.bytesused);  
        store_image(buffers[buf.index].start, buf.bytesused, buf.index);
  
                if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
                        errno_exit("VIDIOC_QBUF");  
                break;  
  
        case IO_METHOD_USERPTR:  
                CLEAR(buf);  
  
                buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                buf.memory = V4L2_MEMORY_USERPTR;  
  
                if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {  
                        switch (errno) {  
                        case EAGAIN:  
                                return 0;  
  
                        case EIO:  
                                /* Could ignore EIO, see spec. */  
  
                                /* fall through */  
  
                        default:  
                                errno_exit("VIDIOC_DQBUF");  
                        }  
                }  
  
                for (i = 0; i < n_buffers; ++i)  
                        if (buf.m.userptr == (unsigned long)buffers[i].start  
                            && buf.length == buffers[i].length)  
                                break;  
  
                assert(i < n_buffers);  
  
                process_image((void *)buf.m.userptr, buf.bytesused);  
  
                if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
                        errno_exit("VIDIOC_QBUF");  
                break;  
        }  
  
        return 1;  
}  
  
/* two operations 
 * step1 : delay 
 * step2 : read frame 
 */  
static void mainloop(void)  
{  
        unsigned int count;  
  
        count = frame_count;  
  
        while (count-- > 0) {  
                for (;;) {  
                        fd_set fds;  
                        struct timeval tv;  
                        int r;  
  
                        FD_ZERO(&fds);  
                        FD_SET(fd, &fds);  
  
                        /* Timeout. */  
                        tv.tv_sec = 2;  
                        tv.tv_usec = 0;  
  
                        r = select(fd + 1, &fds, NULL, NULL, &tv);  
  
                        if (-1 == r) {  
                                if (EINTR == errno)  
                                        continue;  
                                errno_exit("select");  
                        }  
  
                        if (0 == r) {  
                                fprintf(stderr, "select timeout\n"); 
                                exit(EXIT_FAILURE);  
                        }  
  
                        if (read_frame())  
                                break;  
                        /* EAGAIN - continue select loop. */  
                }  
        }  
}  
/* 
 * one operation 
 * step1 : VIDIOC_STREAMOFF 
 */  
static void stop_capturing(void)  
{  
        enum v4l2_buf_type type;  
        switch (io) {  
        case IO_METHOD_READ:  
                /* Nothing to do. */  
                break;  
  
        case IO_METHOD_MMAP:  
        case IO_METHOD_USERPTR:  
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))  
                        errno_exit("VIDIOC_STREAMOFF");  
                break;  
        }  
}  
  
/* tow operations 
 * step1 : VIDIOC_QBUF(insert buffer to queue) 
 * step2 : VIDIOC_STREAMOFF 
 */  
static void start_capturing(void)  
{  
        unsigned int i;  
        enum v4l2_buf_type type;  
  
        switch (io) {  
        case IO_METHOD_READ:  
                /* Nothing to do. */  
                break;  
  
        case IO_METHOD_MMAP:  
                for (i = 0; i < n_buffers; ++i) {  
                        struct v4l2_buffer buf;  
  
                        CLEAR(buf);  
                        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                        buf.memory = V4L2_MEMORY_MMAP;  
                        buf.index = i;  
  
                        if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
                                errno_exit("VIDIOC_QBUF");  
                } 
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))  
                        errno_exit("VIDIOC_STREAMON");  
        break;  
  
        case IO_METHOD_USERPTR:  
                for (i = 0; i < n_buffers; ++i) {  
                        struct v4l2_buffer buf;  
  
                        CLEAR(buf);  
                        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                        buf.memory = V4L2_MEMORY_USERPTR;  
                        buf.index = i;  
                        buf.m.userptr = (unsigned long)buffers[i].start;  
                        buf.length = buffers[i].length;  
  
                        if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
                                errno_exit("VIDIOC_QBUF");  
                }  
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))  
                        errno_exit("VIDIOC_STREAMON");  
                break;  
        }  
}  
  
/* two operations 
 * step1 : munmap buffers 
 * steo2 : free buffers 
 */  
static void uninit_device(void)  
{  
        unsigned int i;  
  
        switch (io) {  
        case IO_METHOD_READ:  
                free(buffers[0].start);  
                break;  
  
        case IO_METHOD_MMAP:  
                for (i = 0; i < n_buffers; ++i)  
                        if (-1 == munmap(buffers[i].start, buffers[i].length))  
                                errno_exit("munmap");  
                break;  
  
        case IO_METHOD_USERPTR:  
                for (i = 0; i < n_buffers; ++i)  
                        free(buffers[i].start);  
                break;  
        }  
  
        free(buffers);  
}  
  
static void init_read(unsigned int buffer_size)  
{  
        buffers = calloc(1, sizeof(*buffers));  
  
        if (!buffers) {  
                fprintf(stderr, "Out of memory\n");  
                exit(EXIT_FAILURE);  
        }  
  
        buffers[0].length = buffer_size;  
        buffers[0].start = malloc(buffer_size);  
  
        if (!buffers[0].start) {  
                fprintf(stderr, "Out of memory\n");  
                exit(EXIT_FAILURE);  
        }  
}  
  
static void init_mmap(void)  
{  
        struct v4l2_requestbuffers req;  
  
        CLEAR(req);  
  
        req.count = 4;  
        req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
        req.memory = V4L2_MEMORY_MMAP;  

        if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {  
                if (EINVAL == errno) {  
                        fprintf(stderr, "%s does not support "  
                                 "memory mapping\n", dev_name);  
                        exit(EXIT_FAILURE);  
                } else {  
                        errno_exit("VIDIOC_REQBUFS");  
                }  
        }  
  
        if (req.count < 2) {  
                fprintf(stderr, "Insufficient buffer memory on %s\n",  
                         dev_name);  
                exit(EXIT_FAILURE);  
        }  
        buffers = calloc(req.count, sizeof(*buffers));  
  
        if (!buffers) {  
                fprintf(stderr, "Out of memory\n");  
                exit(EXIT_FAILURE);  
        }  
  
        for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {  
                struct v4l2_buffer buf;  
  
                CLEAR(buf);  
  
                buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                buf.memory      = V4L2_MEMORY_MMAP;  
                buf.index       = n_buffers;  
  
                if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))  
                        errno_exit("VIDIOC_QUERYBUF");  
  
                buffers[n_buffers].length = buf.length;
        printf("buffers[%d].length=%d\n", n_buffers, buffers[n_buffers].length);
                buffers[n_buffers].start =  
                        mmap(NULL /* start anywhere */,  
                              buf.length,  
                              PROT_READ | PROT_WRITE /* required */,  
                              MAP_SHARED /* recommended */,  
                              fd, buf.m.offset);  
  
                if (MAP_FAILED == buffers[n_buffers].start)  
                        errno_exit("mmap");  
        }  
}  
  
static void init_userp(unsigned int buffer_size)  
{  
        struct v4l2_requestbuffers req;  
  
        CLEAR(req);  
  
        req.count  = 4;  
        req.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
        req.memory = V4L2_MEMORY_USERPTR;  
  
        if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {  
                if (EINVAL == errno) {  
                        fprintf(stderr, "%s does not support "  
                                 "user pointer i/o\n", dev_name);  
                        exit(EXIT_FAILURE);  
                } else {  
                        errno_exit("VIDIOC_REQBUFS");  
                }  
        }  
  
        buffers = calloc(4, sizeof(*buffers));  
  
        if (!buffers) {  
                fprintf(stderr, "Out of memory\n");  
                exit(EXIT_FAILURE);  
        }  
  
        for (n_buffers = 0; n_buffers < 4; ++n_buffers) {  
                buffers[n_buffers].length = buffer_size;  
                buffers[n_buffers].start = malloc(buffer_size);  
  
                if (!buffers[n_buffers].start) {  
                        fprintf(stderr, "Out of memory\n");  
                        exit(EXIT_FAILURE);  
                }  
        }  
}  
  
/* five operations 
 * step1 : cap :query camera's capability and check it(is a video device? is it support read? is it support streaming?) 
 * step2 : cropcap:set cropcap's type and get cropcap by VIDIOC_CROPCAP 
 * step3 : set crop parameter by VIDIOC_S_CROP (such as frame type and angle) 
 * step4 : set fmt 
 * step5 : mmap 
 */  
static void init_device(void)  
{  
        struct v4l2_capability cap;  
        struct v4l2_cropcap cropcap;  
        struct v4l2_crop crop;  
        struct v4l2_format fmt;  
        unsigned int min; 

    
        if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {  
                if (EINVAL == errno) {  
                        fprintf(stderr, "%s is no V4L2 device\n",  
                                 dev_name);  
                        exit(EXIT_FAILURE);  
                } else {  
                        errno_exit("VIDIOC_QUERYCAP");  
                }  
        }  
        if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {  
                fprintf(stderr, "%s is no video capture device\n",  
                         dev_name);  
                exit(EXIT_FAILURE);  
        }  

        switch (io) {  
        case IO_METHOD_READ:  
                if (!(cap.capabilities & V4L2_CAP_READWRITE)) {  
                        fprintf(stderr, "%s does not support read i/o\n",  
                                 dev_name);  
                        exit(EXIT_FAILURE);  
                }  
                break;  
  
        case IO_METHOD_MMAP:  
        case IO_METHOD_USERPTR:  
                if (!(cap.capabilities & V4L2_CAP_STREAMING)) {  
                        fprintf(stderr, "%s does not support streaming i/o\n",  
                                 dev_name);  
                        exit(EXIT_FAILURE);  
                }  
                break;  
        }  
  
  
        /* Select video input, video standard and tune here. */  
        CLEAR(cropcap);  
  
        cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
        /* if device support cropcap's type then set crop */  
        if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {  
                crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
                crop.c = cropcap.defrect; /* reset to default */  
  
                if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {  
                        switch (errno) {  
                        case EINVAL:  
                                /* Cropping not supported. */  
                                break;  
                        default:  
                                /* Errors ignored. */  
                                break;  
                        }  
                }  
        } else {  
                /* Errors ignored. */  
        }  
  
  
        CLEAR(fmt);  
  
        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
        if (force_format) {  
                fmt.fmt.pix.width       = 640;  
                fmt.fmt.pix.height      = 480;  
                fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;  
                fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED; 
  
          printf("set %d*%d YUYV format\n", fmt.fmt.pix.width, fmt.fmt.pix.height);
                if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))  
                        errno_exit("VIDIOC_S_FMT");  
  
                /* Note VIDIOC_S_FMT may change width and height. */  
        } else {  
                /* Preserve original settings as set by v4l2-ctl for example */  
                if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))  
                        errno_exit("VIDIOC_G_FMT");  
        }  
  
        /* Buggy driver paranoia. */  
        min = fmt.fmt.pix.width * 2;  
        if (fmt.fmt.pix.bytesperline < min)  
                fmt.fmt.pix.bytesperline = min;  
        min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;  
        if (fmt.fmt.pix.sizeimage < min)  
                fmt.fmt.pix.sizeimage = min;  
  
        switch (io) {  
        case IO_METHOD_READ:  
                init_read(fmt.fmt.pix.sizeimage);  
                break;  
  
        case IO_METHOD_MMAP:  
                init_mmap();  
                break;  
  
        case IO_METHOD_USERPTR:  
                init_userp(fmt.fmt.pix.sizeimage);  
                break;  
        }  
}  
  
/* 
 * close (fd) 
 */  
static void close_device(void)  
{  
        if (-1 == close(fd))  
                errno_exit("close");  
  
        fd = -1;  
}  
  
/* three operations 
 * step 1 : check dev_name and st_mode 
 * step 2 : open(device) 
 */  
static void open_device(void)  
{  
        struct stat st;  
  
        if (-1 == stat(dev_name, &st)) {  
                fprintf(stderr, "Cannot identify '%s': %d, %s\n",  
                         dev_name, errno, strerror(errno));  
                exit(EXIT_FAILURE);  
        }  
  
        if (!S_ISCHR(st.st_mode)) {  
                fprintf(stderr, "%s is no device\n", dev_name);  
                exit(EXIT_FAILURE);  
        }  
  
        fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);  
  
        if (-1 == fd) {  
                fprintf(stderr, "Cannot open '%s': %d, %s\n",  
                         dev_name, errno, strerror(errno));  
                exit(EXIT_FAILURE);  
        }  
}  
  
static void usage(FILE *fp, int argc, char **argv)  
{  
        fprintf(fp,  
                 "Usage: %s [options]\n\n"  
                 "Version 1.3\n"  
                 "Options:\n"  
                 "-d | --device name   Video device name [%s]\n"  
                 "-h | --help          Print this message\n"  
                 "-m | --mmap          Use memory mapped buffers [default]\n"  
                 "-r | --read          Use read() calls\n"  
                 "-u | --userp         Use application allocated buffers\n"  
                 "-o | --output        Outputs stream to stdout\n"  
                 "-f | --format        Force format to 640x480 YUYV\n"  
                 "-c | --count         Number of frames to grab [%i]\n"  
                 "",  
                 argv[0], dev_name, frame_count);  
}  
  
static const char short_options[] = "d:hmruofc:";  
  
static const struct option  
long_options[] = {  
        { "device", required_argument, NULL, 'd' },  
        { "help",   no_argument,       NULL, 'h' },  
        { "mmap",   no_argument,       NULL, 'm' },  
        { "read",   no_argument,       NULL, 'r' },  
        { "userp",  no_argument,       NULL, 'u' },  
        { "output", no_argument,       NULL, 'o' },  
        { "format", no_argument,       NULL, 'f' },  
        { "count",  required_argument, NULL, 'c' },  
        { 0, 0, 0, 0 }  
};  
  
int main(int argc, char **argv)  
{  
        dev_name = "/dev/video0";  
  
        for (;;) {  
                int idx;  
                int c;  
  
                c = getopt_long(argc, argv,  
                                short_options, long_options, &idx);  
  
                if (-1 == c)  
                        break;  
  
                switch (c) {  
                case 0: /* getopt_long() flag */  
                        break;  
  
                case 'd':  
                        dev_name = optarg;  
                        break;  
  
                case 'h':  
                        usage(stdout, argc, argv);  
                        exit(EXIT_SUCCESS);  
  
                case 'm':  
                        io = IO_METHOD_MMAP;  
                        break;  
  
                case 'r':  
                        io = IO_METHOD_READ;  
                        break;  
  
                case 'u':  
                        io = IO_METHOD_USERPTR;  
                        break;  
  
                case 'o':  
                        out_buf++;  
                        break;  
  
                case 'f':  
                        force_format++;  
                        break;  
  
                case 'c':  
                        errno = 0;  
                        frame_count = strtol(optarg, NULL, 0);  
                        if (errno)  
                                errno_exit(optarg);  
                        break;  
  
                default:  
                        usage(stderr, argc, argv);  
                        exit(EXIT_FAILURE);  
                }  
        }  
  
        open_device();  
    init_device(); 

        start_capturing();  
        mainloop();  
        stop_capturing();  
        uninit_device(); 
        close_device();  
        fprintf(stderr, "\n");  
        return 0;  
} 

  

/ # ./a.out -f
set 640*480 YUYV format
buffers[0].length=614400
buffers[1].length=614400
buffers[2].length=614400
buffers[3].length=614400
....
/ # sz yuyv2.yuv

用图片查看器“RawImageViewer.exe” :(由于我的摄像头配置成扫码模式 所以是黑白图)

 

 

新增YUV分布存储数据示例:

/* https://blog.csdn.net/li_wen01/article/details/53557949 */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <assert.h>

#define FILE_VIDEO "/dev/video0"
unsigned char* video_path;
#define IMG_PATH "./image%d.yuv"
#define IMG_NUM		8


struct user_img{
	void *start;
	int length;
};
 
struct user_img *usr_buf;
static unsigned int n_buffer = 0;
static unsigned int nplanes;
 
int init_mmap(int fd)
{
	int i;
	struct v4l2_requestbuffers reqbufs;
 	struct v4l2_buffer buf;


	memset(&reqbufs, 0, sizeof(reqbufs));
	reqbufs.count = IMG_NUM;
	reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;    
	reqbufs.memory = V4L2_MEMORY_MMAP;				

	/* VIDIOC_REQBUFS: frame buffer alloc by kernel and tell userspace how many frames had alloc */
	if(-1 == ioctl(fd,VIDIOC_REQBUFS,&reqbufs)) {
		printf("Fail to ioctl 'VIDIOC_REQBUFS'");
		exit(EXIT_FAILURE);
	}
         n_buffer = reqbufs.count;                                               
          printf("n_buffer = %d\n", n_buffer);                                    
          usr_buf = calloc(reqbufs.count, sizeof(struct user_img));               
          if(usr_buf == NULL) {                                                   
                  printf("Out of memory\n");                                      
                  exit(-1);                                                       
          }                                                                       
                                                                                  
          /*map kernel frame buffer to user process*/                             
          for(i = 0; i < n_buffer; ++i) {                                         
                  memset(&buf, 0, sizeof(buf));                                   
                  buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                  
                  buf.memory = V4L2_MEMORY_MMAP;                                  
                  buf.index = i;                                                  
                                                                                  
                  struct v4l2_plane planes[VIDEO_MAX_PLANES];                     
                  memset(planes, 0, VIDEO_MAX_PLANES * sizeof(struct v4l2_plane));
                  /* when type=V4L2_BUF_TYPE_VIDEO_CAPTURE, length means frame size
                   * when type=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, length means how many planes */
                  buf.length = nplanes;                                           
                  buf.m.planes = planes;                                          
                                                                                  
                  /* VIDIOC_QUERYBUF: get the frame buffer size and physical address since we need it mmmap to userspace */
                  if(-1 == ioctl(fd,VIDIOC_QUERYBUF,&buf)) 
                 {                                                               
                          printf("Fail to ioctl : VIDIOC_QUERYBUF");              
                          exit(EXIT_FAILURE);                                     
                  }                                                               
                                                                                  
                  /* FIXME: usr_buf[0].start=frame addr but not planes[0].m.mem_offset
                   * usr_buf[0].start = planes[0].m.mem_offset + planes[1].m.mem_offset + ... planes[nplanes-1].m.mem_offset
                   * in this case nplanes=1, So frame buffer has only 1 plane     
                   */                                                             
                  usr_buf[i].length = buf.m.planes[0].length;                     
                  usr_buf[i].start = (char *)mmap(NULL,buf.m.planes[0].length,PROT_READ | PROT_WRITE,MAP_SHARED, fd, buf.m.planes[0].m.mem_offset);
                  printf("fuzk index[%d]  length=%d, phyaddr=%p, virtaddr=%p\n",  
                          buf.index, usr_buf[i].length, buf.m.planes[0].m.mem_offset, usr_buf[i].start);
                                                                                  
                  if(MAP_FAILED == usr_buf[i].start) {                            
                          printf("Fail to mmap");                                 
                          exit(EXIT_FAILURE);                                     
                  }                                                               
                                                                                  
                  if(-1 == ioctl(fd, VIDIOC_QBUF, &buf)) {                        
                          printf("Fail to ioctl 'VIDIOC_QBUF'");                  
                          exit(EXIT_FAILURE);                                     
                  }                                                               
          }  
}                                                                               
                                                                                  
 int open_camera(void)                                                           
 {                                                                               
         int fd;                                                                 
         struct v4l2_input inp;                                                  
                                                                                 
         fd = open(FILE_VIDEO, O_RDWR | O_NONBLOCK,0);                           
         if(fd < 0) {                                                            
                 printf("%s open err \n", FILE_VIDEO);                           
                 exit(EXIT_FAILURE);                                             
         }                                                                       
                                                                                 
         /* must act VIDIOC_S_INPUT otherwise select will timeout */             
         inp.index = 1;                                                          
         if (-1 == ioctl (fd, VIDIOC_S_INPUT, &inp)) {                           
                 printf("VIDIOC_S_INPUT fail\n");                                
         }                                                                       
                                                                                 
         return fd;                                                              
 }                                                                               
               
 int init_camera(int fd)                                                         
121 {                                                                               
122         struct v4l2_capability cap;                                             
123         struct v4l2_format tv_fmt;                                              
124         struct v4l2_fmtdesc fmtdesc;                                            
125         struct v4l2_control ctrl;                                               
126         int ret;                                                                
127         int i;                                                                  
128                                                                                 
129                                                                                 
130         /* check video decive driver capability */                              
131         if(ret=ioctl(fd, VIDIOC_QUERYCAP, &cap)<0) {                            
132                 printf("fail to ioctl VIDEO_QUERYCAP \n");                      
133                 exit(EXIT_FAILURE);                                             
134         }                                                                       
135                                                                                 
136         /*judge wherher or not to be a video-get device, some driver forget add to this support list */
137         if(!(cap.capabilities & V4L2_BUF_TYPE_VIDEO_CAPTURE)){                  
138                 printf("The Current device is not a video capture device \n");  
139         }                                                                       
140         if(!(cap.capabilities & V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) {          
141                 printf("The Current device is not V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE\n");
142         }                                                                       
143                                                                                 
144         /*judge whether or not to supply the form of video stream*/             
145         if(!(cap.capabilities & V4L2_CAP_STREAMING))
145         if(!(cap.capabilities & V4L2_CAP_STREAMING))                            
146         {                                                                       
147                 printf("The Current device does not support streaming i/o\n");  
148                 exit(EXIT_FAILURE);                                             
149         }                                                                       
150                                                                                 
151         printf("\ncamera driver name is : %s\n",cap.driver);                    
152         printf("camera device name is : %s\n",cap.card);                        
153         printf("camera bus information: %s\n",cap.bus_info);                    
154                                                                                 
155         /*display the format device support*/                                   
156         memset(&fmtdesc, 0, sizeof(fmtdesc));                                   
157         fmtdesc.index = 0 ;                 /* the number to check */           
158         fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                        
159                                                                                 
160         printf("find V4L2_PIX_FMT_NV21 0x%x\n", V4L2_PIX_FMT_NV21);             
161         for (int i = 0; i < 12; i++) {                                          
162                 fmtdesc.index = i;                                              
163                 if (-1 == ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) {               
164                         break;                                                  
165                 }                                                               
166                 printf("format index = %d, name = %s, v4l2 pixel format = %x\n",
167                          i, fmtdesc.description, fmtdesc.pixelformat);          
168         }               
169         printf("\n");                                                           
170                                                                                 
171         /*set the form of camera capture data*/                                 
172         tv_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                       
173         tv_fmt.fmt.pix_mp.width = 1280;                                         
174         tv_fmt.fmt.pix_mp.height = 720;                                         
175         /* vidioc_try_fmt_vid_cap_mplane() */                                   
176         tv_fmt.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV21;                      
177         tv_fmt.fmt.pix_mp.field = V4L2_FIELD_NONE;                              
178         if(ioctl(fd, VIDIOC_S_FMT, &tv_fmt)< 0) {                               
179                 printf("VIDIOC_S_FMT set err\n");                               
180                 close(fd);                                                      
181                 exit(-1);                                                       
182         }                                                                       
183                                                                                 
184         if(ioctl(fd, VIDIOC_G_FMT, &tv_fmt) < 0) {                              
185                 printf("VIDIOC_G_FMT get err\n");                               
186                 close(fd);                                                      
187                 exit(-1);                                                       
188         } else {                                                                
189                 nplanes = tv_fmt.fmt.pix_mp.num_planes;                         
190                 printf("VIDIOC_G_FMT resolution = %d*%d num_planes = %d\n", tv_fmt.fmt.pix_mp.width,
191                          tv_fmt.fmt.pix_mp.height, tv_fmt.fmt.pix_mp.num_planes);
192         }  
193                                                                                 
194         init_mmap(fd);                                                          
195 }                                                                               
196                                                                                 
197 int start_capture(int fd)                                                       
198 {                                                                               
199         enum v4l2_buf_type type;                                                
200                                                                                 
201                                                                                 
202         type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                              
203         if(-1 == ioctl(fd, VIDIOC_STREAMON, &type)) {                           
204                 printf("VIDIOC_STREAMON");                                      
205                 close(fd);                                                      
206                 exit(EXIT_FAILURE);                                             
207         }                                                                       
208                                                                                 
209         return 0;                                                               
210 }                                                                               
211                                                                                 
212 int process_image(void *addr, int length)                                       
213 {                                                                               
214         FILE *fp;                                                               
215                                                                                 
216         static int num = 0;                                                     
217       
218         char image_name[20];                                                    
219         sprintf(image_name, IMG_PATH, num++);                                   
220         if((fp = fopen(image_name, "w")) == NULL)                               
221         {                                                                       
222                 printf("Fail to fopen");                                        
223                 exit(EXIT_FAILURE);                                             
224         }                                                                       
225         fwrite(addr, length, 1, fp);                                            
226         usleep(500);                                                            
227         fclose(fp);                                                             
228         return 0;                                                               
229 }                                                                               
230                                                                                 
231 int read_frame(int fd)                                                          
232 {                                                                               
233         struct v4l2_buffer buf;                                                 
234         unsigned int i;                                                         
235         struct v4l2_plane planes[VIDEO_MAX_PLANES];                             
236                                                                                 
237         memset(&buf, 0, sizeof(buf));                                           
238         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                          
239         buf.memory = V4L2_MEMORY_MMAP;                                          
240         buf.length = nplanes;                                                   
241         buf.m.planes = planes;
242                                                                                 
243         if(-1 == ioctl(fd, VIDIOC_DQBUF,&buf)) {                                
244                 printf("Fail to ioctl 'VIDIOC_DQBUF'");                         
245                 exit(EXIT_FAILURE);                                             
246         }                                                                       
247                                                                                 
248         assert(buf.index < n_buffer);                                           
249                                                                                 
250         process_image(usr_buf[buf.index].start, usr_buf[buf.index].length);     
251         if(-1 == ioctl(fd, VIDIOC_QBUF,&buf)) {                                 
252                 printf("Fail to ioctl 'VIDIOC_QBUF'");                          
253                 exit(EXIT_FAILURE);                                             
254         }                                                                       
255         return 1;                                                               
256 }                                                                               
257                                                                                 
258                                                                                 
259 int mainloop(int fd)                                                            
260 {                                                                               
261         int count = 10;                                                         
262         while(count-- > 0) {                                                    
263                 for(;;) {                                                       
264                         fd_set fds;                                             
265                         struct timeval tv; 
266                         int r;                                                  
267                                                                                 
268                         FD_ZERO(&fds);                                          
269                         FD_SET(fd,&fds);                                        
270                                                                                 
271                         /*Timeout*/                                             
272                         tv.tv_sec = 5;                                          
273                         tv.tv_usec = 0;                                         
274                         r = select(fd + 1,&fds,NULL,NULL,&tv);                  
275                                                                                 
276                         if(-1 == r) {                                           
277                                  if(EINTR == errno)                             
278                                         continue;                               
279                                 printf("Fail to select");                       
280                                 exit(EXIT_FAILURE);                             
281                         }                                                       
282                         if(0 == r)                                              
283                         {                                                       
284                                 printf("select Timeout\n");                     
285                                 exit(-1);                                       
286                         }                                                       
287                                                                                 
288                         if(read_frame(fd))                                      
289                                 break;                                          
290                 }                                                               
291         } 
292         return 0;                                                               
293 }                                                                               
294                                                                                 
295 void stop_capture(int fd)                                                       
296 {                                                                               
297         enum v4l2_buf_type type;                                                
298         type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;                              
299         if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type)) {                            
300                 printf("Fail to ioctl 'VIDIOC_STREAMOFF'");                     
301                 exit(EXIT_FAILURE);                                             
302         }                                                                       
303 }                                                                               
304                                                                                 
305 void close_camera_device(int fd)                                                
306 {                                                                               
307         unsigned int i;                                                         
308         for(i = 0; i < n_buffer; i++)                                           
309         {                                                                       
310                 if(-1 == munmap(usr_buf[i].start, usr_buf[i].length))           
311                 {                                                               
312                         exit(-1);                                               
313                 }                                                               
314         }                                                                       
315                                                                                 
316         free(usr_buf); 
317                                                                                 
318         if(-1 == close(fd))                                                     
319         {                                                                       
320                 printf("Fail to close fd");                                     
321                 exit(EXIT_FAILURE);                                             
322         }                                                                       
323 }                                                                               
324                                                                                 
325                                                                                 
326 void main(int argc, char *argv[])                                               
327 {                                                                               
328         int fd;                                                                 
329         video_path = FILE_VIDEO;                                                
330         if(argc > 2) {                                                          
331                 video_path = argv[1];                                           
332                 printf("camera path: %s\n", video_path);                        
333         }                                                                       
334         fd = open_camera();                                                     
335         init_camera(fd);                                                        
336         start_capture(fd);                                                      
337         mainloop(fd);                                                           
338         stop_capture(fd);                                                       
339         close_camera_device(fd);                                                
340 }     

  

 

posted @ 2019-04-24 17:52  Vedic  阅读(14166)  评论(0编辑  收藏  举报