v4l2介绍

v4l2最初是用于在linux上捕捉图像,后来发展成为一个视频硬件接口框架。

使用这个指令可以查看这个硬件解码器支持的输入格式,以及输出格式。

v4l2-ctl -d /dev/video9 --all
root@phyboard-mira-imx6-3:/# v4l2-ctl -d /dev/video9 --all
Driver Info (not using libv4l2):
        Driver name   : coda
        Card type     : CODA960
        Bus info      : platform:coda
        Driver version: 4.14.39
        Capabilities  : 0x84208000
                Video Memory-to-Memory
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04208000
                Video Memory-to-Memory
                Streaming
                Extended Pix Format
Priority: 2
Format Video Capture:
        Width/Height      : 1920/1088
        Pixel Format      : 'NV12'
        Field             : None
        Bytes per Line    : 1920
        Size Image        : 3133440
        Colorspace        : Rec. 709
        Transfer Function : Default
        YCbCr/HSV Encoding: Default
        Quantization      : Default
        Flags             :
Format Video Output:
        Width/Height      : 1920/1088
        Pixel Format      : 'H264'
        Field             : None
        Bytes per Line    : 0
        Size Image        : 3133440
        Colorspace        : Rec. 709
        Transfer Function : Default
        YCbCr/HSV Encoding: Default
        Quantization      : Default
        Flags             :
Selection: compose, Left 0, Top 0, Width 1920, Height 1088
Selection: compose_default, Left 0, Top 0, Width 1920, Height 1088
Selection: compose_bounds, Left 0, Top 0, Width 1920, Height 1088
Selection: compose_padded, Left 0, Top 0, Width 1920, Height 1088
Selection: crop, Left 0, Top 0, Width 1920, Height 1088
Selection: crop_default, Left 0, Top 0, Width 1920, Height 1088
Selection: crop_bounds, Left 0, Top 0, Width 1920, Height 1088
Streaming Parameters Video Output:
        Capabilities     : timeperframe
        Frames per second: 30.000 (30/1)
        Write buffers    : 0

User Controls

                horizontal_flip (bool)   : default=0 value=0
                  vertical_flip (bool)   : default=0 value=0

从以上信息可以看到 /dev/video9 是VPU的接口,可以将NV12的视频压缩为H264格式的视频。

v4l2的例子

参考文档:

一个使用v4l2的接口来读取vpu解码设备信息的例子:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>

int main(int argc, char *argv[]) {

	int fd;
	struct v4l2_capability cap; 
	struct v4l2_fmtdesc fmtdesc; 

	fd = open("/dev/video9", O_RDWR);
	if (fd == -1)
	{
		perror("Opening Video device");
		return 1;
	}

	ioctl(fd,VIDIOC_QUERYCAP,&cap); 
	printf("Driver Name:%s\nCard Name:%s\nBus info:%s\ncaps:0x%x\n",\
		cap.driver,\
		cap.card,\
		cap.bus_info,\
		cap.device_caps); 

	fmtdesc.index=0; 
	fmtdesc.type=V4L2_BUF_TYPE_VIDEO_CAPTURE; 

	printf("input format:\n"); 
	while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1) 
	{ 
		printf("/t%d.%s\n",fmtdesc.index+1,fmtdesc.description); 
		fmtdesc.index++; 
	} 

	fmtdesc.index=0; 
	fmtdesc.type=V4L2_BUF_TYPE_VIDEO_OUTPUT; 

	printf("out format:\n"); 
	while(ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc)!=-1) 
	{ 
		printf("/t%d.%s\n",fmtdesc.index+1,fmtdesc.description); 
		fmtdesc.index++; 
	} 
	
	close(fd);
	return 0;
}

编译方式:

$CC $CFLAGS v4l2.c -o v4l2

执行结果:

root@phyboard-mira-imx6-3:~# ./v4l2
Driver Name:coda
Card Name:CODA960
Bus info:platform:coda
caps:0x4208000
input format:
/t1.Y/CbCr 4:2:0
/t2.Planar YUV 4:2:0
/t3.Planar YVU 4:2:0
/t4.YUYV 4:2:2
out format:
/t1.H.264
/t2.MPEG-2 ES
/t3.MPEG-4 part 2 ES
  • No labels