Realization of video reading under Linux (ii)---camera parameter setting

Source: Internet
Author: User

Camera can be set a lot of items, V4L2 support a lot. But Sam's previous use and implication of these settings was to look at the videodev2.h and understand, feeling very jerky. Until the writing of this blog, only to find that V4L2 has a special spec to explain:
Http://www.linuxtv.org/downloads/legacy/video4linux/API/V4L2_API/spec-single/v4l2.html

But there is basically no time to look carefully. Let's take a look at some of the things you see in your document here. Talk about the V4L2 setting in the order of the actual setup process.

1. Query v4l2 feature set: Vidioc_querycap
struct v4l2_capability cap;
int rel = 0;
IOCTL (Handle, Vidioc_querycap, &cap);

Use the IOCTL Vidioc_querycap to query whether the current driver is compliant. Because V4L2 requires all driver and device support for this IOCTL. Therefore, it is possible to determine whether the current device and Dirver support the V4L2 specification through the success of this IOCTL. Of course, this will also be able to get enough information about the capacity of the device.

struct v4l2_capability
{
__u8 driver[16]; The driver name.
__u8 card[32]; Device Name
__u8 bus_info[32]; Store location in bus system
__U32 version; Driver version
__U32 capabilities; Competency Set
__u32 Reserved[4];
};
Capacity concentration includes:


V4l2_cap_video_capture 0x00000001 The device supports the VIDEO CAPTURE interface.
V4l2_cap_video_output 0x00000002 The device supports the VIDEO OUTPUT interface.
V4l2_cap_video_overlay 0x00000004 The device supports the VIDEO OVERLAY interface.
A video overlay device typically stores captured images directly in the video memory of a graphics Card,with hardware cl Ipping and scaling.
V4l2_cap_vbi_capture 0x00000010 The device supports the Raw VBI CAPTURE interface, providing Teletext and Closed Capt Ion data.
V4l2_cap_vbi_output 0x00000020 The device supports the Raw VBI OUTPUT interface.
V4l2_cap_sliced_vbi_capture 0x00000040 The device supports the sliced VBI CAPTURE interface.
V4l2_cap_sliced_vbi_output 0x00000080 The device supports the sliced VBI OUTPUT interface.
V4l2_cap_rds_capture 0x00000100 [to be defined]
#define V4l2_cap_tuner 0x00010000
#define V4l2_cap_audio 0x00020000
#define V4l2_cap_radio 0x00040000
#define V4l2_cap_readwrite 0x01000000
#define V4l2_cap_asyncio 0x02000000
#define V4l2_cap_streaming 0x04000000

Looks familiar, actually is the type inside the driver.

__u8 driver[16]; Driver name, usually: Uvcvideo
__u8 card[32]; Device Name: Factory Chamber of Commerce to fill in.
__u8 bus_info[32]; Bus, usually: usb-hiusb-ehci-2.4
__U32 version;
__U32 capabilities; Usually: v4l2_cap_video_capture | V4l2_cap_streaming
__u32 Reserved[4];

2. Enumerate the image format:vidioc_enum_fmt supported by the device
struct V4L2_FMTDESC fmtdesc;
Fmtdesc.index = 0;
Fmtdesc.type = v4l2_buf_type_video_capture;
RET = IOCTL (Handle, VIDIOC_ENUM_FMT, &fmtdesc);

Use the IOCTL vidioc_enum_fmt to inquire in turn, TYPE: V4l2_buf_type_video_capture. Index starts at 0 and increments sequentially until it returns. Driver fills the rest of the struct struct V4l2_fmtdesc, and returns 1 if index is out of range.
struct V4L2_FMTDESC
{
__U32 index; It needs to be filled, starting with 0 and rising in turn.
Enum V4l2_buf_type type; Camera, fill in the V4l2_buf_type_video_capture
__U32 flags; If compressed, then driver fill in: v4l2_fmt_flag_compressed, otherwise 0
__u8 description[32]; Description of the image format, such as YUV 4:2:2 (YUYV)
__u32 PixelFormat; The supported formats. such as: V4l2_pix_fmt_uyvy
__u32 Reserved[4];
};

This way, you know what image format the current hardware supports. Next, you can set the image. Of course, you can also read the current default settings before setting.

3. Get and set image format:vidioc_g_fmt, vidioc_s_fmt:
3.1: Get the current image Format:
struct V4l2_format format;
memset (&format, 0, sizeof (struct v4l2_format));
Format.type = v4l2_buf_type_video_capture;
IOCTL (Handle, VIDIOC_G_FMT, &format);

Using the IOCTL vidioc_g_fmt. Gets the current settings.
Because the camera is a CAPTURE device, you need to set the TYPE to: v4l2_buf_type_video_capture
Then driver will populate the rest of the content.

struct V4l2_format
{
Enum V4l2_buf_type type; Camera, the user must fill in: V4l2_buf_type_video_capture
Union
{
struct V4l2_pix_format pix; Used by video capture and output devices
struct V4l2_window win;
struct V4l2_vbi_format vbi;
struct V4l2_sliced_vbi_format sliced;
__u8 raw_data[200];
} FMT;
};


Because it is a camera, the PIX is used. Now the analysis is as follows:
struct V4l2_pix_format
{
__u32 width; Image width in pixels.
__u32 height; Image Height in pixels.
__u32 PixelFormat; The image format, the most common are: V4L2_PIX_FMT_YYUV
enum V4l2_field field; Whether to scan line by row, interlaced. Sam typically uses V4l2_field_none to place data row by line (note 1)
__u32 Bytesperline; Number of bytes per line
__u32 Sizeimage; Total number of bytes, Bytesperline * height
Enum V4l2_colorspace ColorSpace; This information supplements the PixelFormat and must is set by the driver
__u32 Priv;
};

3.2: Set Image format:vidioc_s_fmt
Previous through vidioc_enum_fmt already know what format the device supports. So you don't have to guess, just set it up.


Set image Format, using Iocto vidioc_s_fmt.
The struct projects that need to be filled in application are:
struct V4l2_format format;

Format.type = v4l2_buf_type_video_capture;
Format.fmt.pix.width = width;
Format.fmt.pix.height = height;
Format.fmt.pix.pixelformat= Pixelformat;//v4l2_pix_fmt_yuyv;
Format.fmt.pix.field = field;
Io_rel = IOCTL (Handle, VIDIOC_S_FMT, &format);

Saminfo: Before setting the image format, it refers to the data format of each frame, but the behavior of stream, also needs to be set, this is the following said stream set. It contains the number of frames set and modified.

4. Get and set stream information: Vidioc_g_parm, Vidioc_s_parm
Stream information, mainly setting the number of frames.
4.1: Get stream Info:
struct V4l2_streamparm stream_parm;

memset (&stream_parm, 0, sizeof (struct v4l2_streamparm));
Stream_parm.type = v4l2_buf_type_video_capture;

Io_rel = IOCTL (Handle, Vidioc_g_parm, &stream_parm);

The user only needs to populate TYPE v4l2_buf_type_video_capture. The driver will fill the other parts of the structure with good.
struct V4L2_STREAMPARM
{
Enum V4l2_buf_type type;
Union
{
struct V4l2_captureparm capture;
struct V4l2_outputparm output;
__u8 raw_data[200];
} parm;
};

Because it is camera, use capture. It is a struct v4l2_captureparm

struct V4L2_CAPTUREPARM
{
__U32 capability; Whether the number of frames can be controlled by Timeperframe. Yes: V4l2_cap_timeperframe
__u32 Capturemode; Whether it is in HD mode. If it is:
is set to: V4l2_mode_highquality. HD mode will sacrifice other information. Typically set to 0.
struct V4l2_fract timeperframe; The number of frames.
__u32 Extendedmode; Custom-made. If not supported, set to 0
__u32 readbuffers;
__u32 Reserved[4];
};
struct V4l2_fract timeperframe; The number of frames.

struct V4l2_fract {
__u32 numerator; Molecular. Example: 1
__U32 denominator; Denominator. Example: 30
};

4.2: Set the number of frames:
struct V4l2_streamparm stream_parm;
memset (&stream_parm, 0, sizeof (struct v4l2_streamparm));
Stream_parm.type = v4l2_buf_type_video_capture;

Stream_Parm.parm.capture.timeperframe.denominator =denominator;
Stream_Parm.parm.capture.timeperframe.numerator = numerator;

Io_rel = IOCTL (Handle, Vidioc_s_parm, &stream_parm);

Please note that even if the IOCTL returns 0. It is also possible that the setting is not successful. So need to get again.
Of course, even if get discovery is set up successfully. A real scratch frame may not be that high.

5. Use Vidioc_g_ctrl to get some settings:
Some specific settings such as exposure mode (Exposure Type), exposure value (Exposure), Gain (Gain), white balance (white_balance), brightness (brightness), saturation (saturation), contrast ( Contrast) and other information. The current value can be obtained by Vidioc_g_ctrl.

Usage: The APP fills in the ID in the struct. The value item in the struct is filled in by calling Vidioc_g_ctrl,driver.
struct V4l2_control Ctrl;
struct V4l2_control
{
__U32 ID;
__S32 value;
};
Take exposure mode, exposure, and gain as an example;

Exposure Mode:
struct V4l2_control Ctrl;
Ctrl.id = V4l2_cid_exposure_auto;
RET = IOCTL (Handle, Vidioc_g_ctrl, &ctrl);
Ctrl.value is filled by driver. Informs the current exposure mode.
There are several options:
Enum V4l2_exposure_auto_type {
V4l2_exposure_auto = 0,
V4l2_exposure_manual = 1,
V4l2_exposure_shutter_priority = 2,
V4l2_exposure_aperture_priority = 3
};
Exposure:
struct V4l2_control Ctrl;
Ctrl.id = V4l2_cid_exposure_absolute;
RET = IOCTL (Handle, Vidioc_g_ctrl, &ctrl);
Similarly, driver fills in the Ctrl.value. The content is the exposure value.

Gain:
struct V4l2_control Ctrl;
Ctrl.id = V4l2_cid_gain;
RET = IOCTL (Handle, Vidioc_g_ctrl, &ctrl);
Similarly, driver fills in the Ctrl.value. The content is gain.

6. Use Vidioc_queryctrl to get setup specific information:
In many cases, we don't know how to set up some information, for example, how much exposure should be set? What is the range that driver can accept? What is the maximum and minimum value? What is the step size? What is the default value?
can be obtained by Vidioc_queryctrl.

Let's take the gain as an example:
struct V4l2_queryctrl Setting;
Setting.id = V4l2_cid_gain;
RET = IOCTL (Handle, Vidioc_queryctrl, &setting);
Driver will fill in all the information in the structure.

struct V4l2_queryctrl
{
__U32 ID; User settings. Specifies which ID to find.
Enum V4l2_ctrl_type type;
__u8 name[32]; ID corresponding to the name.
__S32 minimum;
__S32 maximum;
__s32 step; Step
__S32 Default_value;
__U32 flags;
__u32 reserved[2];
};
In this way, you know what value to set is legal. So, the next step is to set up.

7. Use Vidioc_s_ctrl to set:
Very simple, set the ID and value. It's good to invoke the IOCTL.
Or take the gain as an example:
struct V4l2_control Ctrl;
Ctrl.id = V4l2_cid_gain;
Ctrl.value = Gain;
RET = IOCTL (Handle, Vidioc_s_ctrl, &ctrl);
Sometimes, the hardware settings are strange, you can set a message, but you can't get information about how to set it up. For example: HD-500 can set the gain. But you can't get the settings.

8. Use the Extended CTRL setting:
Focal length (fouce);

Note 1:enum V4l2_field field; Detailed

9. Set the resolution:

struct V4l2_control control;

memset (&control, 0, sizeof (control));
Control.id = v4l2_cid_auto_white_balance;
Control.value = 1;
if (IOCTL (FD, Vidioc_s_ctrl, &control) < 0)
{
printf ("couldn ' t set auto white balance!\n");
return-1;
}

memset (&control, 0, sizeof (control));
Control.id = V4l2_cid_exposure_auto;
Control.value = 1;

if (IOCTL (FD, Vidioc_s_ctrl, &control) < 0)
{
printf ("couldn ' t set auto exposure!\n");
return-1;
}

memset (&control, 0, sizeof (control));
Control.id = V4l2_cid_hflip;
Control.value = 1;
if (IOCTL (FD, Vidioc_s_ctrl, &control) < 0)
{
printf ("couldn ' t set h flip!\n");
return-1;
}


memset (&control, 0, sizeof (control));
Control.id = V4l2_cid_vflip;
Control.value = 1;
if (IOCTL (FD, Vidioc_s_ctrl, &control) < 0)
{
printf ("couldn ' t set v flip!\n");
return-1;
}

Realization of video reading under Linux (ii)---camera parameter setting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.