如何使用video4linux api获取支持的帧大小和网络摄像头设备帧间隔列表?

时间:2013-02-27 12:27:03

标签: c v4l2 video4linux

我正在尝试使用V4L2 api来枚举支持的网络摄像头设备的帧大小和帧速率。我尝试使用以下代码执行此操作但未成功。 ioctl函数始终返回-1。

#include <stdio.h>
#include <fcntl.h>
#include "linux/videodev2.h"
int main(int argc, char **argv) {
    int fd = open("/dev/video0", O_RDWR);
    struct v4l2_frmivalenum frmsize;
    memset(&frmsize, 0, sizeof (struct v4l2_frmsizeenum));
    ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS , &frmsize);
    printf("%u \n", frmsize.he);
    return 0;
}

2 个答案:

答案 0 :(得分:2)

您应该在ioctl VIDIOC_ENUM_FRAMESIZES之前设置in参数以获取帧大小:

Table A.34. struct v4l2_frmivalenum
__u32   index               IN: Index of the given frame interval in the enumeration.
__u32   pixel_format        IN: Pixel format for which the frame intervals are enumerated.
__u32   width               IN: Frame width for which the frame intervals are enumerated.
__u32   height              IN: Frame height for which the frame intervals are enumerated.

ioctl VIDIOC_ENUM_FRAMEINTERVALS获取帧间隔:

Table A.31. struct v4l2_frmsizeenum
__u32   index               IN: Index of the given frame size in the enumeration.
__u32   pixel_format        IN: Pixel format for which the frame sizes are enumerated.

然后,枚举帧大小和帧间隔的实现可以是:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "linux/videodev2.h"
void printFrameInterval(int fd, unsigned int fmt, unsigned int width, unsigned int height)
{
    struct v4l2_frmivalenum frmival;
    memset(&frmival,0,sizeof(frmival));
    frmival.pixel_format = fmt;
    frmival.width = width;
    frmival.height = height;
    while (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) == 0) 
    {
        if (frmival.type == V4L2_FRMIVAL_TYPE_DISCRETE) 
            printf("[%dx%d] %f fps\n", width, height, 1.0*frmival.discrete.denominator/frmival.discrete.numerator);
        else
            printf("[%dx%d] [%f,%f] fps\n", width, height, 1.0*frmival.stepwise.max.denominator/frmival.stepwise.max.numerator, 1.0*frmival.stepwise.min.denominator/frmival.stepwise.min.numerator);
        frmival.index++;    
    }
}

int main(int argc, char **argv) {
    unsigned int width=0, height=0;;
    int fd = open("/dev/video0", O_RDWR);
    struct v4l2_frmsizeenum frmsize;
    memset(&frmsize,0,sizeof(frmsize));
    frmsize.pixel_format = V4L2_PIX_FMT_JPEG;
    while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0)
    {
        if (frmsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) 
        {
            printFrameInterval(fd, frmsize.pixel_format, frmsize.discrete.width, frmsize.discrete.height);
        }
        else
        {
            for (width=frmsize.stepwise.min_width; width< frmsize.stepwise.max_width; width+=frmsize.stepwise.step_width)
                for (height=frmsize.stepwise.min_height; height< frmsize.stepwise.max_height; height+=frmsize.stepwise.step_height)
                    printFrameInterval(fd, frmsize.pixel_format, width, height);

        }
        frmsize.index++;
    }
    return 0;
}

答案 1 :(得分:1)

我认为您使用的驱动程序未实现vidioc_enum_frameintervals

请注意,每个视频驱动程序都会注册v4l2_ioctl_ops

例如,在内核3.7中,drivers/media/usb/s2255/s2255drv.c实现了它。 drivers/staging/media/go7007/go7007-v4l2.c也实现了它。

但是,虚拟视频驱动程序drivers/media/platform/vivi.c未实现它。