void convert_yuv420p_to_rgb_buffer(unsigned char* yuv, unsigned char* rgb, unsigned int width, unsigned int height)
{
int y, u, v;
int r, g, b;
unsigned char* rgbPixel = rgb;
unsigned char* yuvPixel = yuv;
unsigned char* uHeader = yuv + width * height;
unsigned char* vHeader = yuv + width * height + (width * height) / 4;
int half_width = width / 2;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
y = *yuvPixel;
yuvPixel++;
u = *(uHeader + (i / 2 + (j / 2 * half_width)));
v = *(vHeader + (i / 2 + (j / 2 * half_width)));
// yuv to rgb, 整數、移位, 減少計算量
r = y + ((360 * (v - 128)) >> 8);
r = r > 255 ? 255 : (r < 0 ? 0 : r);
*rgbPixel = r;
rgbPixel++;
g = y - (((88 * (u - 128) + 184 * (v - 128))) >> 8);
g = g > 255 ? 255 : (g < 0 ? 0 : g);
*rgbPixel = g;
rgbPixel++;
b = y + ((455 * (u - 128)) >> 8);
b = b > 255 ? 255 : (b < 0 ? 0 : b);
*rgbPixel = b;
rgbPixel++;
}
}
}void convert_yuv420sp_to_rgb_buffer(unsigned char* yuv, unsigned char* rgb, unsigned int width, unsigned int height)
{
int y, u, v;
int r, g, b;
unsigned char* rgbPixel = rgb;
unsigned char* yuvPixel = yuv;
unsigned char* uHeader = yuv + width * height;
//unsigned char* vHeader = yuv + width * height + (width * height) / 4;
int half_width = width / 2;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
y = *yuvPixel;
yuvPixel++;
u = *(uHeader + (i / 2 + (j / 2 * half_width)) * 2); // 注意: 因為是整數乘除, 這裡 (i / 2 * 2) 不一定都等于 i, 不要簡化。
v = *(uHeader + (i / 2 + (j / 2 * half_width)) * 2+1);
// yuv to rgb, 整數、移位, 減少計算量
r = y + ((360 * (v - 128)) >> 8);
r = r > 255 ? 255 : (r < 0 ? 0 : r);
*rgbPixel = r;
rgbPixel++;
g = y - (((88 * (u - 128) + 184 * (v - 128))) >> 8);
g = g > 255 ? 255 : (g < 0 ? 0 : g);
*rgbPixel = g;
rgbPixel++;
b = y + ((455 * (u - 128)) >> 8);
b = b > 255 ? 255 : (b < 0 ? 0 : b);
*rgbPixel = b;
rgbPixel++;
}
}
}void convert_yuyv_to_rgb_buffer(unsigned char* yuv, unsigned char* rgb, unsigned int width, unsigned int height)
{
unsigned int i;
unsigned char* rgbPixel = rgb;
unsigned char* yuvPixel = yuv;
int y0, u, y1, v;
int r, g, b;
for (i = 0; i < width * height / 2; i++) {
y0 = *yuvPixel;
yuvPixel++;
u = *yuvPixel;
yuvPixel++;
y1 = *yuvPixel;
yuvPixel++;
v = *yuvPixel;
yuvPixel++;
// yuv to rgb, 整數、移位, 減少計算量
r = y0 + ((360 * (v - 128)) >> 8);
r = r > 255 ? 255 : (r < 0 ? 0 : r);
*rgbPixel = r;
rgbPixel++;
g = y0 - (((88 * (u - 128) + 184 * (v - 128))) >> 8);
g = g > 255 ? 255 : (g < 0 ? 0 : g);
*rgbPixel = g;
rgbPixel++;
b = y0 + ((455 * (u - 128)) >> 8);
b = b > 255 ? 255 : (b < 0 ? 0 : b);
*rgbPixel = b;
rgbPixel++;
// yuv to rgb, 整數、移位, 減少計算量
r = y1 + ((360 * (v - 128)) >> 8);
r = r > 255 ? 255 : (r < 0 ? 0 : r);
*rgbPixel = r;
rgbPixel++;
g = y1 - (((88 * (u - 128) + 184 * (v - 128))) >> 8);
g = g > 255 ? 255 : (g < 0 ? 0 : g);
*rgbPixel = g;
rgbPixel++;
b = y1 + ((455 * (u - 128)) >> 8);
b = b > 255 ? 255 : (b < 0 ? 0 : b);
*rgbPixel = b;
rgbPixel++;
}
}