LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

Linux v4l2子系统(9):libv4l2

libv4l是一个对v4l2设备抽象层,包括一系列库文件和测试程序。

1 v4l2库文件

libv4l1.so:支持v4l1应用访问v4l2设备。

libv4l2.so:支持对v4l2设备的open/ioctl/close/poll等访问。

libv4l2convert.so:是为v4l2应用使用的libv4l2.so增加多种pixelformats的wrapper。

2 libv4l工具

3.1 media-ctl

用于获取以及配置v4l2设备:

media-ctl -d /dev/media1 -p

显示/dev/media1对应的Media设备拓扑结构如下,参考Media Graph

Media controller API version 5.10.110

Media device information
------------------------
driver          rkisp0-vir0
model           rkisp0
serial
bus info
hw revision     0x0
driver version  5.10.110

Device topology
- entity 1: rkisp-isp-subdev (4 pads, 10 links)--此Entity包含4个Pad,10个Link。
            type V4L2 subdev subtype Unknown flags 0
            device node name /dev/v4l-subdev3
        pad0: Sink--此Sink可能有4个输入,当前Active输入是rkcif-mipi-lvds2。
                [fmt:SBGGR10_1X10/2112x1568 field:none
                 crop.bounds:(0,0)/2112x1568
                 crop:(0,0)/2112x1568]
                <- "rkisp_rawrd0_m":0 []
                <- "rkisp_rawrd2_s":0 []
                <- "rkisp_rawrd1_l":0 []
                <- "rkcif-mipi-lvds2":0 [ENABLED]
        pad1: Sink
                <- "rkisp-input-params":0 [ENABLED]
        pad2: Source
                [fmt:YUYV8_2X8/2112x1568 field:none colorspace:smpte170m quantization:full-range
                 crop.bounds:(0,0)/2112x1568
                 crop:(0,0)/2112x1568]
                -> "rkisp_mainpath":0 [ENABLED]
                -> "rkisp_selfpath":0 [ENABLED]
                -> "rkisp_fbcpath":0 [ENABLED]
                -> "rkisp_iqtool":0 [ENABLED]
        pad3: Source
                -> "rkisp-statistics":0 [ENABLED]

- entity 6: rkisp_mainpath (1 pad, 1 link)
            type Node subtype V4L flags 0
            device node name /dev/video11
        pad0: Sink
                <- "rkisp-isp-subdev":2 [ENABLED]

- entity 12: rkisp_selfpath (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video12
        pad0: Sink
                <- "rkisp-isp-subdev":2 [ENABLED]

- entity 18: rkisp_fbcpath (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video13
        pad0: Sink
                <- "rkisp-isp-subdev":2 [ENABLED]

- entity 24: rkisp_iqtool (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video14
        pad0: Sink
                <- "rkisp-isp-subdev":2 [ENABLED]

- entity 30: rkisp_rawrd0_m (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video15
        pad0: Source
                -> "rkisp-isp-subdev":0 []

- entity 36: rkisp_rawrd2_s (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video16
        pad0: Source
                -> "rkisp-isp-subdev":0 []

- entity 42: rkisp_rawrd1_l (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video17
        pad0: Source
                -> "rkisp-isp-subdev":0 []

- entity 48: rkisp-statistics (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video18
        pad0: Sink
                <- "rkisp-isp-subdev":3 [ENABLED]

- entity 54: rkisp-input-params (1 pad, 1 link)
             type Node subtype V4L flags 0
             device node name /dev/video19
        pad0: Source
                -> "rkisp-isp-subdev":1 [ENABLED]

- entity 60: rkcif-mipi-lvds2 (1 pad, 1 link)--此Entity包含1个Pad;一个Source Link,输出到rkisp-isp-subdev0。
             type V4L2 subdev subtype Unknown flags 0
             device node name /dev/v4l-subdev4
        pad0: Source
                [fmt:SBGGR10_1X10/2112x1568@10000/300000 field:none]
                -> "rkisp-isp-subdev":0 [ENABLED]

3.2 v4l2-ctl

v44l2-ctl可以读取并配置v4l2设备的controls。

摄像头识别检测:

复制代码
v4l2-ctl --list-devices
rk_hdmirx (fdee0000.hdmirx-controller):
        /dev/video20

rkisp-statistics (platform: rkisp):
        /dev/video18
        /dev/video19

rkcif-mipi-lvds2 (platform:rkcif):
        /dev/media0

rkcif (platform:rkcif-mipi-lvds2):
        /dev/video0
        /dev/video1
        /dev/video2
        /dev/video3
        /dev/video4
        /dev/video5
        /dev/video6
        /dev/video7
        /dev/video8
        /dev/video9
        /dev/video10

rkisp_mainpath (platform:rkisp0-vir0):
        /dev/video11
        /dev/video12
        /dev/video13
        /dev/video14
        /dev/video15
        /dev/video16
        /dev/video17
        /dev/media1
复制代码

查看摄像头支持的格式:

复制代码
v4l2-ctl --list-formats-ext -d /dev/video11
ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture Multiplanar

        [0]: 'UYVY' (UYVY 4:2:2)
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [1]: 'NV16' (Y/CbCr 4:2:2)
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [2]: 'NV61' (Y/CrCb 4:2:2)
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [3]: 'NV21' (Y/CrCb 4:2:0)
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [4]: 'NV12' (Y/CbCr 4:2:0)
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [5]: 'NM21' (Y/CrCb 4:2:0 (N-C))
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
        [6]: 'NM12' (Y/CbCr 4:2:0 (N-C))
                Size: Stepwise 32x16 - 2112x1568 with step 8/8
复制代码

摄像头采集格式查询:

复制代码
v4l2-ctl -V -d /dev/video11
Format Video Capture Multiplanar:
        Width/Height      : 32/0
        Pixel Format      : 'UYVY' (UYVY 4:2:2)
        Field             : None
        Number of planes  : 1
        Flags             :
        Colorspace        : Default
        Transfer Function : Default
        YCbCr/HSV Encoding: Default
        Quantization      : Full Range
        Plane 0           :
           Bytes per Line : 64
           Size Image     : 0
复制代码

摄像头图像预览:

gst-launch-1.0 v4l2src device=/dev/video11 ! \video/x-raw,format=NV12,width=640,height=480,framerate=30/1 ! autovideosink

摄像头录像:

v4l2-ctl -d /dev/video0 --set-fmt-video=width=1920,height=1080,pixelformat=1 --stream-mmap=3 --stream-count=20 --stream-to=/tmp/image.yuv --stream-out-pattern=1

3.3 v4l2-dbg

提供对v4l2设备的调试功能,主要通过设置VIDIOC_DBG_G_REGISTER/VIDIOC_DBG_S_REGISTER/VIDIOC_DBG_G_CHIP_INFO等ioctl命令。

3.4 v4l2-sysfs-path

检查当前系统的media设备并显示设备节点。

3.5 v4l2-compliance

对v4l2设备进行兼容性测试,对一系列ioctl命令进行有效性检查。

v4l2-compliance -d /dev/video11

参照RK3588 Media Graph分析如下:

v4l2-compliance 1.22.1, 64 bits, 64-bit time_t

Compliance test for rkisp_v6 device /dev/video11:

Driver Info:
        Driver name      : rkisp_v6
        Card type        : rkisp_mainpath
        Bus info         : platform:rkisp0-vir0--设备对应的总线和驱动。
        Driver version   : 2.0.0
        Capabilities     : 0x84201000
                Video Capture Multiplanar
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps      : 0x04201000
                Video Capture Multiplanar
                Streaming
                Extended Pix Format
Media Driver Info:
        Driver name      : rkisp0-vir0--驱动名称。
        Model            : rkisp0
        Serial           :
        Bus info         :
        Media version    : 5.10.110
        Hardware revision: 0x00000000 (0)
        Driver version   : 5.10.110
Interface Info:
        ID               : 0x03000007
        Type             : V4L Video
Entity Info:
        ID               : 0x00000006 (6)
        Name             : rkisp_mainpath--Media Graph Entity名称。
        Function         : V4L2 I/O
        Pad 0x01000009   : 0: Sink
          Link 0x0200000a: from remote pad 0x1000004 of entity 'rkisp-isp-subdev' (Unknown V4L2 Sub-Device): Data, Enabled--此Entity的Sink来源。
--下面是各种ioctl的检查,以及结查结果:OK表示功能正常,或者Not Supported。;FAIL表示失败,并指出失败位置。
Required ioctls:
        test MC information (see 'Media Driver Info' above): OK
                fail: v4l2-compliance.cpp(644): (vcap.version >> 16) < 3
        test VIDIOC_QUERYCAP: FAIL
                fail: v4l2-compliance.cpp(783): doioctl(node, ioc, nullptr) != ENOTTY
        test invalid ioctls: FAIL

Allow for multiple opens:
        test second /dev/video11 open: OK
                fail: v4l2-compliance.cpp(644): (vcap.version >> 16) < 3
        test VIDIOC_QUERYCAP: FAIL
        test VIDIOC_G/S_PRIORITY: OK
        test for unlimited opens: OK

Debug ioctls:
        test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported)
        test VIDIOC_LOG_STATUS: OK (Not Supported)

Input ioctls:
        test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported)
        test VIDIOC_ENUMAUDIO: OK (Not Supported)
        test VIDIOC_G/S/ENUMINPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDIO: OK (Not Supported)
        Inputs: 0 Audio Inputs: 0 Tuners: 0

Output ioctls:
        test VIDIOC_G/S_MODULATOR: OK (Not Supported)
        test VIDIOC_G/S_FREQUENCY: OK (Not Supported)
        test VIDIOC_ENUMAUDOUT: OK (Not Supported)
        test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported)
        test VIDIOC_G/S_AUDOUT: OK (Not Supported)
        Outputs: 0 Audio Outputs: 0 Modulators: 0

Input/Output configuration ioctls:
        test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported)
        test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported)
        test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported)
        test VIDIOC_G/S_EDID: OK (Not Supported)

Control ioctls:
        test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK
        test VIDIOC_QUERYCTRL: OK
        test VIDIOC_G/S_CTRL: OK
        test VIDIOC_G/S/TRY_EXT_CTRLS: OK
                fail: v4l2-test-controls.cpp(872): subscribe event for control 'Image Processing Controls' failed
        test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: FAIL
        test VIDIOC_G/S_JPEGCOMP: OK (Not Supported)
        Standard Controls: 2 Private Controls: 0

Format ioctls:
        test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK
        test VIDIOC_G/S_PARM: OK (Not Supported)
        test VIDIOC_G_FBUF: OK (Not Supported)
                fail: v4l2-test-formats.cpp(358): !colorspace
                fail: v4l2-test-formats.cpp(481): testColorspace(!node->is_io_mc, pix_mp.pixelformat, pix_mp.colorspace, pix_mp.ycbcr_enc, pix_mp.quantization)
        test VIDIOC_G_FMT: FAIL
                fail: v4l2-test-formats.cpp(358): !colorspace
                fail: v4l2-test-formats.cpp(481): testColorspace(!node->is_io_mc, pix_mp.pixelformat, pix_mp.colorspace, pix_mp.ycbcr_enc, pix_mp.quantization)
        test VIDIOC_TRY_FMT: FAIL
                warn: v4l2-test-formats.cpp(1147): S_FMT cannot handle an invalid pixelformat.
                warn: v4l2-test-formats.cpp(1148): This may or may not be a problem. For more information see:
                warn: v4l2-test-formats.cpp(1149): http://www.mail-archive.com/linux-media@vger.kernel.org/msg56550.html
                fail: v4l2-test-formats.cpp(363): colorspace >= 0xff
                fail: v4l2-test-formats.cpp(481): testColorspace(!node->is_io_mc, pix_mp.pixelformat, pix_mp.colorspace, pix_mp.ycbcr_enc, pix_mp.quantization)
        test VIDIOC_S_FMT: FAIL
        test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported)
                fail: v4l2-test-formats.cpp(1517): doioctl(node, VIDIOC_G_SELECTION, &sel) != EINVAL
        test Cropping: FAIL
        test Composing: OK (Not Supported)
        test Scaling: OK

Codec ioctls:
        test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported)
        test VIDIOC_G_ENC_INDEX: OK (Not Supported)
        test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)

Buffer ioctls:
                fail: v4l2-test-buffers.cpp(607): q.reqbufs(node, 1)
        test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: FAIL
                fail: v4l2-test-buffers.cpp(783): VIDIOC_EXPBUF is supported, but the V4L2_MEMORY_MMAP support is missing or malfunctioning.
                fail: v4l2-test-buffers.cpp(784): VIDIOC_EXPBUF is supported, but the V4L2_MEMORY_MMAP support is missing, probably due to earlier failing format tests.
        test VIDIOC_EXPBUF: OK (Not Supported)
        test Requests: OK (Not Supported)

Total for rkisp_v6 device /dev/video11: 46, Succeeded: 37, Failed: 9, Warnings: 3

3.6 qv4l2

 qv4l2可以对Linux下v4l2设备进行调试、预览、录制。

3.7 v4l2grab

v4l2grab用于从v4l2设备抓图图像:

 v4l2grab --device /dev/video11  -W 2112 -H 1568 -o test.jpeg -q 100

posted on 2024-04-14 23:59  ArnoldLu  阅读(1038)  评论(0编辑  收藏  举报

导航