2

我正在尝试使用 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;
}
4

2 回答 2

3

您应该在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;
}
于 2014-09-05T21:20:12.887 回答
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没有实现它。

于 2013-12-10T20:35:05.677 回答