简体   繁体   English

通过C / C ++在JPEG像素格式的v4l2中设置/获取相机jpeg压缩质量

[英]setting/getting camera jpeg compression quality in v4l2 with JPEG pixel format via C/C++

Can you please show how to get and set the JPEG compression quality (with JPEG pixel format) in V4L2 via C++? 您能否说明如何通过C ++在V4L2中获取和设置JPEG压缩质量(使用JPEG像素格式)?

I can detect the various pixel formats supported by the camera and the corresponding resolutions and frame-rates. 我可以检测到相机支持的各种像素格式以及相应的分辨率和帧速率。 I can also select them and capture JPEG images accordingly. 我也可以选择它们并相应地捕获JPEG图像。 However I fail in setting and getting the jpeg quality. 但是我无法设置和获取jpeg质量。

I am using Linux Mint and Logitech c910 camera. 我正在使用Linux Mint和Logitech c910相机。

The camera seems to expose this parameter since 相机似乎公开了此参数,因为

v4l2-ctl --all

produces 产生

JPEG Compression Controls

        compression_quality (int)    : min=50 max=87 step=1 default=75 value=75
 [...]

However, the code 但是,代码

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <iostream>
using namespace std;

int main()
{
    int fd = open("/dev/video0", O_RDWR);
    if (fd == -1) 
    {   
        perror("open: "); return 1; 
    }

    {   
        // first option
        v4l2_jpegcompression ctrl={0};
        if(ioctl(fd, VIDIOC_G_JPEGCOMP, &ctrl)<0)
        {
            perror("VIDIOC_G_JPEGCOMP:");
        }
        else
        {
            cout<<"QUALITY:"<<ctrl.quality<<endl; 
        }
    }

    {   
        // second option
        v4l2_ext_control extCtrl={0};
        extCtrl.id = V4L2_CID_JPEG_COMPRESSION_QUALITY;
        extCtrl.size = 0;
        extCtrl.value = 100;

        v4l2_ext_controls extCtrls={0};
        extCtrls.controls = &extCtrl;
        extCtrls.count = 1;
        extCtrls.ctrl_class = V4L2_CTRL_CLASS_JPEG;

        if(ioctl(fd, VIDIOC_G_EXT_CTRLS, &extCtrls)<0)
        { 
             perror("VIDIOC_G_EXT_CTRLS:");
        }
    }

    {   
        // third option
        v4l2_ext_control extCtrl={0};
        extCtrl.id = V4L2_CID_JPEG_COMPRESSION_QUALITY;
        extCtrl.size = 0;
        extCtrl.value = 100;

        v4l2_ext_controls extCtrls={0};
        extCtrls.controls = &extCtrl;
        extCtrls.count = 1;
        extCtrls.ctrl_class = V4L2_CID_JPEG_CLASS;

        if(ioctl(fd, VIDIOC_G_EXT_CTRLS, &extCtrls)<0)
        { 
            perror("VIDIOC_G_EXT_CTRLS V4L2_CID_JPEG_CLASS:");
        }
    }

    close(fd);
    return 0;
}

which can be compiled via 可以通过编译

g++ query.cpp -o query.out

produces: 生产:

VIDIOC_G_JPEGCOMP:: Inappropriate ioctl for device
VIDIOC_G_EXT_CTRLS:: Invalid argument
VIDIOC_G_EXT_CTRLS V4L2_CID_JPEG_CLASS:: Invalid argument
void check_controls(int fd) {

    struct v4l2_queryctrl qctrl;

    memset(&qctrl, 0, sizeof(qctrl));

    qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
    while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl)) {

        printf("ID = %08x\n", qctrl.id);
        /* ... */
        qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
    }
}

This will print in hex a list of mapped controls as in v4l2-controls.h 如v4l2-controls.h中那样,它将以十六进制打印映射的控件列表。

V4L2_CID_JPEG_COMPRESSION_QUALITY is not mapped V4L2_CID_JPEG_COMPRESSION_QUALITY未映射

You may be able to map this in 您可能可以将其映射到

https://www.kernel.org/doc/html/latest/media/v4l-drivers/uvcvideo.html https://www.kernel.org/doc/html/latest/media/v4l-drivers/uvcvideo.html

29.1.5. 29.1.5。 IOCTL reference 29.1.5.1. IOCTL参考29.1.5.1。 UVCIOC_CTRL_MAP - Map a UVC control to a V4L2 control UVCIOC_CTRL_MAP-将UVC控件映射到V4L2控件

You can check what parameters the camera supports by using lsusb -v. 您可以使用lsusb -v检查相机支持的参数。 The ioctl VIDIOC_G_JPEGCOMP or VIDIOC_G_JPEGCOMP does not appear in the v4l2 driver. ioctl VIDIOC_G_JPEGCOMP或VIDIOC_G_JPEGCOMP没有出现在v4l2驱动程序中。 I think they are not used as cameras don't support them. 我认为它们不被使用,因为相机不支持它们。 Most cameras stream MJPEG automatically controlling quality to deliver a target bitrate. 大多数相机会流式传输MJPEG,以自动控制质量以提供目标比特率。 They don't seem to support changing QP or bitrate in MJPEG. 他们似乎不支持更改MJPEG中的QP或比特率。 I think the idea is that the camera delivers a high bitrate and then use ffmpeg to re-encode to H264 for example. 我认为这个想法是,相机提供了高比特率,然后使用ffmpeg重新编码为H264。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM