4

我正在使用 video4linux 编写一个 c++ 网络摄像头查看器。我需要一个 RGB24 输出(交错的R8B8G8)来显示。我可以使用 YUYV、GREY8 或 RGB24 为几乎所有低分辨率网络摄像头获取视频输入。但我还需要从高分辨率网络摄像头获取输入,这些摄像头在需要高帧率时使用MJPEG进行压缩。

我能够使用V4L2_PIX_FMT_MJPEG像素格式获取 MJPEG 流,但接收到的帧缓冲区被压缩。

如何快速将其转换为 RGB24?

我可以为此使用 libjpeg 吗?

4

2 回答 2

2

我发现最快的解决方案decode_jpeg_raw来自mjpegtools,它将 jpeg 数据解码为平面 YUV420。那么yuv420到rgb24的转换就是通过这个函数来完成的:

inline int clip(int value) {
    return (value > 255) ? 255 : (value < 0) ? 0 : value;
}

static void yuv420_to_rgb24(
/* luminance (source) */const uint8_t* const y
/* u chrominance (source) */, const uint8_t* u
/* v chrominance (source) */, const uint8_t* v
/* rgb interleaved (destination) */, uint8_t* const dst
/* jpeg size */, int const size
/* image width */, int const width) {

    const int lineSize = width * 3;

    uint8_t* r1 = dst;
    uint8_t* g1 = r1 + 1;
    uint8_t* b1 = r1 + 2;

    uint8_t* r2 = r1 + lineSize;
    uint8_t* g2 = r2 + 1;
    uint8_t* b2 = r2 + 2;

    const uint8_t* y1 = y;
    const uint8_t* y2 = y + width;

    uint8_t* const end = r1 + size;

    int c1 = 0;
    int c2 = 0;
    int e = 0;
    int d = 0;

    while (r1 != end) {
        uint8_t* const lineEnd = r2;
        /* line by line */
        while (r1 != lineEnd) {
            /* first pixel */
            c1 = *y1 - 16;
            c2 = *y2 - 16;
            d = *u - 128;
            e = *v - 128;

            *r1 = clip(c1 + ((454 * e) >> 8));
            *g1 = clip(c1 - ((88 * e + 183 * d) >> 8));
            *b1 = clip(c1 + ((359 * d) >> 8));

            *r2 = clip(c2 + ((454 * e) >> 8));
            *g2 = clip(c2 - ((88 * e + 183 * d) >> 8));
            *b2 = clip(c2 + ((359 * d) >> 8));

            r1 += 3;
            g1 += 3;
            b1 += 3;

            r2 += 3;
            g2 += 3;
            b2 += 3;

            ++y1;
            ++y2;

            /* second pixel */

            c1 = *y1 - 16;
            c2 = *y2 - 16;
            d = *u - 128;
            e = *v - 128;

            *r1 = clip(c1 + ((454 * e) >> 8));
            *g1 = clip(c1 - ((88 * e + 183 * d) >> 8));
            *b1 = clip(c1 + ((359 * d) >> 8));

            *r2 = clip(c2 + ((454 * e) >> 8));
            *g2 = clip(c2 - ((88 * e + 183 * d) >> 8));
            *b2 = clip(c2 + ((359 * d) >> 8));

            r1 += 3;
            g1 += 3;
            b1 += 3;

            r2 += 3;
            g2 += 3;
            b2 += 3;

            ++y1;
            ++y2;

            ++u;
            ++v;
        }
        r1 += lineSize;
        g1 += lineSize;
        b1 += lineSize;
        r2 += lineSize;
        g2 += lineSize;
        b2 += lineSize;
        y1 += width;
        y2 += width;
    }
}
于 2012-01-17T17:34:36.237 回答
0

是的,您可以为此使用 libjpeg,但通常 libjpeg 的输出是 YUV420 或 YUV422。您可以改用该代码: http: //mxhaard.free.fr/spca50x/Download/gspcav1-20071224.tar.gz(检查解码器源,有一个运行良好的小型 jpeg 解码器直接处理颜色转换,因此输出为 RGB888)

于 2012-01-12T15:00:40.343 回答