列出可用的捕获格式 [英] List available capture formats

查看:46
本文介绍了列出可用的捕获格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

V4L 的新手,我决定开始使用 video4linux2 库,以便从我的 C 中的相机捕获帧(我使用带有 Ricoh Co. 相机的 uvcvideo 模块).我遵循了几个指南和教程,并设法获得了一个正在运行的程序.我的问题主要是关于这个通常的代码行:

New to V4L, I decided to start using the video4linux2 library in order to capture a frame from my camera in C (I am using the uvcvideo module with a Ricoh Co. camera). I followed several guides and tutorials, and managed to get a running program. My question is mainly about this usual code line :

struct v4l2_format format = {0};
format.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
// ...

这是我设置捕获时要使用的实际视频格式的地方.如您所见,在此示例中,我使用的是 MJPEG (http://lxr.free-electrons.com/source/include/uapi/linux/videodev2.h#L390).尽管这可能是一种很好的格式,但我的应用程序可能需要简单的 RGB 格式,我猜是每像素一个像素.为此,我尝试使用 RGB 格式常量,例如 V4L2_PIX_FMT_RGB24.现在出于某种原因...... v4l2 不喜欢它.我猜这与硬件有关,但我想尽可能避免 MJPEG 操作.出于测试目的,我尝试使用其他常量和格式,但无论我做什么,v4l2 都会不断更改 pixelformat 字段的值:

This is where I set the actual video format to use when capturing. As you can see, in this sample, I'm using MJPEG (http://lxr.free-electrons.com/source/include/uapi/linux/videodev2.h#L390). Even though this might be a great format and all, my application will probably require simple RGB formatting, pixel per pixel I guess. For this reason, I tried using RGB format constants such as V4L2_PIX_FMT_RGB24. Now for some reason... v4l2 doesn't like it. I'm guessing this is hardware-related, but I'd like to avoid MJPEG manipulations as much as possible. For testing purposes, I tried using other constants and formats, but no matter what I did, v4l2 kept changing the pixelformat field's value :

xioctl(fd, VIDIOC_S_FMT, &format); // This call succeeds with errno != EINTR.
if(format.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24){
    // My program always enters this block when not using MJPEG.
    fprintf(stderr, "Format wasn't accepted by v4l2.");
    exit(4);
}

现在我的问题是:有没有一种方法可以获得可接受的视频格式列表(我的意思是,我的相机/v4l2 接受),从中我可以选择 MJPEG 以外的其他格式?如果您认为我必须坚持使用 MJPEG,您会向我推荐任何允许我操作它并最终在 GUI 框架中绘制捕获的库吗?

Now my question is : is there a way I could get a list of accepted video formats (and I mean, accepted by my camera/v4l2), from which I could pick something else than MJPEG ? If you think I have to stick with MJPEG, would you recommend me any library allowing me to manipulate it, and eventually, to draw back the capture in a GUI frame ?

我使用以下技巧来测试我硬件上的所有可用格式.首先,一些shell脚本来获取所有格式的列表...

I used the following trick to test all available formats on my hardware. First, some shell script to get a list of all formats...

grep 'V4L2_PIX_FMT' /usr/include/linux/videodev2.h | grep define | tr '\t' ' ' | cut -d' ' -f2 | sed 's/$/,/g'

... 在这个 C 程序中使用了它的输出:

... the output of which is used in this C program :

int formats[] = {/* result of above command */};
int i = 0;
struct v4l2_format format = {0};

for(i = 0; i < /* number of lines in previous command output */; i++){
    format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    format.fmt.pix.width = 320;
    format.fmt.pix.height = 240;
    format.fmt.pix.pixelformat = formats[i];
    format.fmt.pix.field = V4L2_FIELD_NONE;
    if(xioctl(fd, VIDIOC_S_FMT, &format) != -1 && format.fmt.pix.pixelformat == formats[i])
        fprintf(stderr, "Accepted : %d\n", i);
}

这个测试表明只有 V4L2_PIX_FMT_YUYVV4L2_PIX_FMT_MJPEG 是有效的.有什么我可以改进的方法,还是与硬件有关?

This test reveals that only V4L2_PIX_FMT_YUYV and V4L2_PIX_FMT_MJPEG are functional. Any way I could improve this, or is it hardware-related ?

推荐答案

在 Linux 中,命令行实用程序 v4l2-ctl 显示网络摄像头本机支持的所有格式 -- 使用 sudo 安装apt-get install v4l-utils,使用 v4l2-ctl -dX --list-formats-ext 运行它,其中 X 是相机索引,如 <代码>/dev/videoX.这些格式通过 uvcvideo 模块报告给 v4l2 内核模块,并且它们由网络摄像头芯片组本地支持.v4l2 仅支持列出的格式,其他任何格式都需要用户编码,而 RGB 很少提供,尽管几乎所有 CCD 都在拜耳 RGGB 中工作.目前最常见的格式是 YUV422(YUYV 或 YUY2)和 MJPEG,具有一定的重叠:MJPEG 为大分辨率实现更大的帧速率.

In Linux, command line utility v4l2-ctl displays all of a webcam's natively supported formats -- install it with sudo apt-get install v4l-utils, run it with v4l2-ctl -dX --list-formats-ext where X is the camera index as in /dev/videoX. These formats are reported to the v4l2 kernel module via uvcvideo module and they are supported natively by the webcam chipset. Only the listed formats are supported by v4l2, anything else would need to be coded by the user, and RGBs are very seldom provided, despite virtually all CCDs working in Bayer RGGB. The most common formats by far are YUV422 (YUYV or YUY2) and MJPEG, with a certain overlap: MJPEG achieve larger frame rates for large resolutions.

可以在 Linux 的 Chromium GetDeviceSupportedFormats() 实现中找到用于列出相机格式的 C++ 代码 这里.

C++ code for listing the camera formats can be found in Chromium GetDeviceSupportedFormats() implementation for Linux here.

如果您必须插入代码以将 YUV 转换为 RGB,我建议使用 libyuv已针对大量架构进行了优化.

If you have to plug code to convert YUV to RGB I'd recommend libyuv which has been optimized for plenty of architectures.

这篇关于列出可用的捕获格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆