RGB转YUV420,支持NV12(420p)和NV21(420sp)

最近项目是用到了RGB格式转YUV420格式,写了一个接口:

enum NV_Mode
{
    NV_NONE = 0,
    NV12_MODE = 1,
    NV21_MODE = 2
};

struct RGB2YUV420Info
{
    unsigned char *pYUV420;
    unsigned char *pRGB;
    NV_Mode eMode;
    int width;
    int height;

    RGB2YUV420Info()
    {
        pYUV420 = nullptr;
        pRGB = nullptr;
        eMode = NV_NONE;
        width = 0;
        height = 0;
    }
};

void RGBToYUV420(RGB2YUV420Info &stInfo)
{
    if (!(stInfo.pYUV420) || !(stInfo.pRGB))
    {
        return;
    }

    int frameSize = stInfo.width * stInfo.height;
    int yIndex = 0;
    int uvIndex = frameSize;

    int R = 0, G = 0, B = 0, Y = 0, U = 0, V = 0;
    for (int i = 0; i < stInfo.height; ++i)
    {
        for (int j = 0; j < stInfo.width; ++j)
        {
            B = stInfo.pRGB[(i * stInfo.width + j) * 3 + 0];
            G = stInfo.pRGB[(i * stInfo.width + j) * 3 + 1];
            R = stInfo.pRGB[(i * stInfo.width + j) * 3 + 2];

            // RGB to YUV
            Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
            U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
            V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

            stInfo.pYUV420[yIndex++] = (unsigned char)((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));

            // NV12  YYYYYYYY UVUV
            // NV21  YYYYYYYY VUVU
            // 偶数判断
            // if ((0 == (i % 2)) && (0 == (j % 2)))
            if (!(i & 1) && !(j & 1))
            {
                if (NV12_MODE == stInfo.eMode)
                {
                    stInfo.pYUV420[uvIndex++] = (unsigned char)((U < 0) ? 0 : ((U > 255) ? 255 : U));
                    stInfo.pYUV420[uvIndex++] = (unsigned char)((V < 0) ? 0 : ((V > 255) ? 255 : V));
                }
                else if (NV21_MODE == stInfo.eMode)
                {
                    stInfo.pYUV420[uvIndex++] = (unsigned char)((V < 0) ? 0 : ((V > 255) ? 255 : V));
                    stInfo.pYUV420[uvIndex++] = (unsigned char)((U < 0) ? 0 : ((U > 255) ? 255 : U));
                }
                else
                {
                    // TODO 不支持的类型
                }
            }
        }
    }

    return;
}

你可能感兴趣的:(笔记,程序代码,算法,数据结构,c++)