【问题标题】:How to decode an MJPEG to raw RGB (or YUV) data如何将 MJPEG 解码为原始 RGB(或 YUV)数据
【发布时间】:2020-01-07 16:16:31
【问题描述】:

我正在使用 Video4Linux2 打开与连接到我机器的摄像头的连接。我可以从我的相机设备请求 YUV 或 MJPEG 数据。由于增加相机请求的分辨率,同时也请求 YUV,导致程序减慢超过相机的刷新率(大概是因为在这段时间内发送的数据太多),我需要使用来自的 MJPEG 数据相机。我被困了一段时间,在网上找到的关于如何解码 MJPEG 的资源很少。

顺便说一下,我有以下所有数据:

unsigned char *data; // pointing to the data for the most current mjpeg frame from v4l2
size_t data_size; // the size (in bytes) of the mjpeg frame received from v4l2

unsigned char *r, *g, *b; // three heap allocated arrays in which to store the resulting data
// Can easily be altered to represent an array of structs holding all 3 components,
// as well as using yuv at different rates.

我所需要的只是将我的 mjpeg 帧实时转换为原始数据,RGB 或 YUV。 我听说过 libjpeg、mjpegtools、nvjpeg 等库,但是我无法找到很多关于如何使用它们来解码我所在位置的 mjpeg。任何帮助将不胜感激!

【问题讨论】:

标签: c++ c jpeg mjpeg v4l2


【解决方案1】:

我通过 cmets 中链接的来源找到了答案。我的工作示例如下:

// variables:
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
unsigned int width, height;
// data points to the mjpeg frame received from v4l2.
unsigned char *data;
size_t data_size;
// a *to be allocated* heap array to put the data for
// all the pixels after conversion to RGB.
unsigned char *pixels;

// ... In the initialization of the program:
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
pixels = new unsigned char[width * height * sizeof(Pixel)];

// ... Every frame:
if (!(data == nullptr) && data_size > 0) {
    jpeg_mem_src(&cinfo, data, data_size);
    int rc = jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    while (cinfo.output_scanline < cinfo.output_height) {
        unsigned char *temp_array[] = {pixels + (cinfo.output_scanline) * width * 3};
        jpeg_read_scanlines(&cinfo, temp_array, 1);
    }

    jpeg_finish_decompress(&cinfo);
}

如果这仍然不适用于试图解决相同问题的任何人,请尝试合并“霍夫曼表”,如第二条评论中所述,某些相机需要这些表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多