v4l2

简述

linux kernel v4l2驱动的编写,可以分为四步:

  1. 注册struct v4l2_device, struct vb2_queue, struct video_device

  2. 填充struct vb2_ops, struct v4l2_file_operations, v4l2_ioctl_ops

  3. 注销

linux kernel v4l2驱动的测试,可以分为二步:

  1. 通过v4l2-ctl捕捉RAW图

  2. 通过gst-launch-1.0转换成.jpeg格式

linux kernel v4l2驱动的编写

1. 注册struct v4l2_device, struct vb2_queue, struct video_device

struct device *dev;
struct v4l2_device v4l2_dev;
struct vb2_queue *queue;
struct video_device *vdev;
struct list_head list;
struct mutex mutex;

// 注册struct v4l2_device
v4l2_device_register(dev, &v4l2_dev);

// 初始化链表
INIT_LIST_HEAD(&list);
// 初始化互斥表
mutex_init(&mutex);

queue->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
// 应用层采用mmap方法进行读取数据
queue->io_modes = VB2_MMAP;
// buffers队列管理
queue->ops = &xxx_queue_ops;
// buffers内存管理
queue->mem_ops = &vb2_vmalloc_memops;
queue->buf_struct_size = sizeof(struct xxx_buffer);
queue->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
queue->lock = &mutex;
queue->drv_priv = xxx;
// 初始化struct vb2_queue
vb2_queue_init(queue);

vdev->fops = &xxx_fops;
vdev->ioctl_ops = &xxx_ioctl_ops;
// V4L2_CAP_VIDEO_CAPTURE, V4L2_CAP_STREAMING代表一个摄像头
vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
vdev->release = xxx_release;
vdev->v4l2_dev = &v4l2_dev;
vdev->queue = queue;
video_set_drvdata(vdev, xxx);
// 注册struct video_device
video_register_device(vdev, VFL_TYPE_GRABBER, -1);

2. 填充struct vb2_ops, struct v4l2_file_operations, v4l2_ioctl_ops

3. 注销

linux应用层

1. 通过v4l2-ctl捕捉RAW图

分辨率为640x480,像素格式为RGBP(rgb565),使用mmap方法捕捉一张RAW图,保存为grab-640x360-rgb565.raw

2. 通过gst-launch-1.0转换成.jpeg格式

blocksize=460800,代表RAW图的大小,即width * height * bytes_per_pix

bytes_per_pix是多少?因为rgb565 = 16bits,即16/8=2bytes

即 640 360 2 = 460800

参考网址

V4L2摄像头概述

V4L2框架-videobuf2

Last updated

Was this helpful?