关于RGB、YUV介绍可以参考 RGB 与YUV颜色模型及存储格式
1.covert RGB to NV21
void encodeYUV420sp(unsigned char* yuv420sp, unsigned char* rgb, int width, int height)
{
if (yuv420sp == NULL || rgb == NULL)
return;
int frameSize = width*height;
int yIndex = 0;
int uvIndex = frameSize;
int R, G, B, Y, U, V;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
B = rgb[(i * width + j) * 3 + 0];
G = rgb[(i * width + j) * 3 + 1];
R = rgb[(i * 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;
yuv420sp[yIndex++] = (unsig