List available capture formats
Asked Answered
G

2

7

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;
// ...

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);
}

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 ?

Barbarian test code

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'

... 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);
}

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 ?

Geometrician answered 21/3, 2014 at 16:10 Comment(2)
linuxtv.org/downloads/v4l-dvb-apis/pixfmt.html ?Nylon
Sorry, I wasn't clear enough, my fault. I am looking for a list of available and accepted formats, meaning formats my camera would accept to use (see my if statement above). When I set something else that MJPEG, the pixelformat attribute is unset by the library, probably because it cannot be handled by my hardware.Geometrician
N
14

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.

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

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

Noleta answered 23/3, 2014 at 20:11 Comment(4)
Nothing else to say, perfect answer : the VIDIOC_ENUM_FMT call was what I needed, and v4l2-ctl uses it :) Thank you! libyuv will probably help me as I litteraly failed all my attempts to convert mathematically. By getting RGB values, I'll be able to draw the frame back using the SDL (and maybe I should have a look at YUV overlays...). Do you know of any library allowing me to draw back MJPEG-encoded frames ?Geometrician
If you want to convert MJPEG them to RGB, libyuv supports this conversion as well as just another color space (pixelformat, to be more concrete), see f.e. code.google.com/p/chromium/codesearch#chromium/src/third_party/… .Noleta
Does libyuv provide C bindings ? The program I'm working on isn't built in C++.Geometrician
Both C and C++ should work, see code.google.com/p/libyuv/source/browse/trunk/include/libyuv/…Noleta
T
7

In order to enumerate the available format you can use ioctl VIDIOC_ENUM_FMT

To print descriptions of capture format supported by /dev/video0 you can process like this :

#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/videodev2.h>

int main()
{
    int fd = v4l2_open("/dev/video0", O_RDWR);
    if (fd != -1)
    {
        struct v4l2_fmtdesc fmtdesc;
        memset(&fmtdesc,0,sizeof(fmtdesc));
        fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        while (ioctl(fd,VIDIOC_ENUM_FMT,&fmtdesc) == 0)
        {    
            printf("%s\n", fmtdesc.description);
            fmtdesc.index++;
        }
        v4l2_close(fd);
    }
}
Teheran answered 6/12, 2015 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.