簡體   English   中英

如何使用 v4l2loopback 將圖像渲染到 /dev/video0?

[英]How to render images to /dev/video0 using v4l2loopback?

我一直在嘗試將圖像渲染到 /dev/video。 我可以得到一些東西來展示,但它有點亂。

我首先開始嘗試渲染普通的 RGB24 圖像(基於此示例https://stackoverflow.com/a/44648382/3818491 ),但結果(下圖)是一個打亂的圖像。

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <sys/ioctl.h>
#include <linux/videodev2.h>

#include <CImg.h>

#define VIDEO_OUT "/dev/video0" // V4L2 Loopack

#define WIDTH  1280
#define HEIGHT 720

int main() {
    using namespace cimg_library;

    CImg<uint8_t> canvas(WIDTH, HEIGHT, 1, 3);
    const uint8_t red[] = {255, 0, 0};
    const uint8_t purple[] = {255, 0, 255};

    int fd;
    if ((fd = open(VIDEO_OUT, O_RDWR)) == -1) {
      std::cerr << "Unable to open video output!\n";
      return 1;
    }

    struct v4l2_format vid_format;
    vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;

    if (ioctl(fd, VIDIOC_G_FMT, &vid_format) == -1) {
      std::cerr << "Unable to get video format data. Errro: " << errno << '\n';
      return 1;
    }

    size_t framesize = canvas.size();
    int width = canvas.width(), height = canvas.height();

    vid_format.fmt.pix.width       = width;
    vid_format.fmt.pix.height      = height;
    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
    vid_format.fmt.pix.sizeimage   = framesize;
    vid_format.fmt.pix.field       = V4L2_FIELD_NONE;

    if (ioctl(fd, VIDIOC_S_FMT, &vid_format) == -1) {
      std::cerr << "Unable to set video format! Errno: " << errno << '\n';
      return 1;
    }

    std::cout << "Stream running!\n";
    while (true) {
      canvas.draw_plasma();
      canvas.draw_rectangle(
        100, 100, 100 + 100, 100 + 100, red, 1);
      canvas.draw_text(5,5, "Hello World!", purple);
      canvas.draw_text(5, 20, "Image freshly rendered with the CImg Library!", red);

      write(fd, canvas.data(), framesize);
    }
}

rgb24壞了

所以我檢查了(我認為)/dev/video 所期望的似乎是 YUV420P。

v4l2-ctl --list-formats-ext                                                                                                                                                                              130 ↵

ioctl: VIDIOC_ENUM_FMT
    Type: Video Capture

    [0]: 'YU12' (Planar YUV 4:2:0)
        Size: Discrete 1280x720
            Interval: Discrete 0.033s (30.000 fps)

所以我嘗試將幀轉換為該格式(使用代碼快速測試)。

將規格調整為:

    vid_format.fmt.pix.width       = width;
    vid_format.fmt.pix.height      = height;
    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
    vid_format.fmt.pix.sizeimage   = width*height*3/2; // size of yuv buffer
    vid_format.fmt.pix.field       = V4L2_FIELD_NONE;

這導致了這個(這似乎來自我收集的 yuv420 圖像的結構,但仍然渲染不正確)。

yuv420p壞了

/dev/video0 期望什么?

經過大量的修改后,我設法生成了一個有效的 YUYV 視頻/圖像以發送到 /dev/video0。

首先,我制作一個緩沖區來保存框架:

// Allocate buffer for the YUUV frame
std::vector<uint8_t> buffer;
buffer.resize(vid_format.fmt.pix.sizeimage);

然后我將當前的 canvas 以 YUYV 格式寫入緩沖區。

bool skip = true;
cimg_forXY(canvas, cx, cy) {
  size_t row = cy * width * 2;
  uint8_t r, g, b, y;
  r = canvas(cx, cy, 0);
  g = canvas(cx, cy, 1);
  b = canvas(cx, cy, 2);

  y = std::clamp<uint8_t>(r * .299000 + g * .587000 + b * .114000, 0, 255);
  buffer[row + cx * 2] = y;
  if (!skip) {
    uint8_t u, v;
    u = std::clamp<uint8_t>(r * -.168736 + g * -.331264 + b * .500000 + 128, 0, 255);
    v = std::clamp<uint8_t>(r * .500000 + g * -.418688 + b * -.081312 + 128, 0, 255);
    buffer[row + (cx - 1) * 2 + 1] = u;
    buffer[row + (cx - 1) * 2 + 3] = v;
  }
  skip = !skip;
}

注意:CImg 具有RGBtoYUV具有就地 RGB 到 YUV 的轉換,但由於某種原因,在 uint8_t canvas 上調用它只是將其歸零。

它還具有get_YUVtoRGB ,它(分配並)返回一個CImg<float> canvas,我認為您將每個值乘以 255 以縮放為一個字節,但是,無論我嘗試什么都沒有給出正確的顏色。 編輯:我可能忘記了 +128 偏差(盡管我仍然不喜歡為每一幀重新分配)

我的完整代碼在這里(如果有人想做類似的事情)https://gist.github.com/MacDue/36199c3f3ca04bd9fd40a1bc2067ef72

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM