YUV格式、RGB格式、JPEG格式、MJPEG格式之间的转换(C程序)之一
2012-12-28 10:52
387 查看
1. Convert YUV420p to YUV422sp
2. Convert YUV422sp to YUV420sp
3. Convert YUV420sp to YUV420p
4. Convert YUV422sp to YUV420p
/* Convert YUV420p to YUV422sp */
void convert_yuv420p_to_yuv422sp(char *inbuf, char *outbuf, int width, int height){
long int i, k, j;
long YSize = width * height;
for (i = 0, j = 1; i < height; i += 2, j += 2)
{
long int line1 = i * width;
long int line2 = j * width;
long int UOffset1 = YSize + line1 / 4;
long int VOffset1 = YSize * 5 / 4 + line1 / 4;
long int m = line1 * 2;
long int n = line2 * 2;
for(k = 0; k < width / 4; k++){
unsigned char Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8;
unsigned char U1, U2, V1, V2;
U1 = inbuf[UOffset1++];U2 = inbuf[UOffset1++];
V1 = inbuf[VOffset1++];V2 = inbuf[VOffset1++];
Y1 = inbuf[line1++];Y2 = inbuf[line1++];
Y3 = inbuf[line1++];Y4 = inbuf[line1++];
Y5 = inbuf[line2++];Y6 = inbuf[line2++];
Y7 = inbuf[line2++];Y8 = inbuf[line2++];
outbuf[m++] = Y1;outbuf[m++] = U1;
outbuf[m++] = Y2;outbuf[m++] = V1;
outbuf[m++] = Y3;outbuf[m++] = U2;
outbuf[m++] = Y4;outbuf[m++] = V2;
outbuf[n++] = Y5;outbuf[n++] = U1;
outbuf[n++] = Y6;outbuf[n++] = V1;
outbuf[n++] = Y7;outbuf[n++] = U2;
outbuf[n++] = Y8;outbuf[n++] = V2;
}
}
}
/* Convert YUV422sp to YUV420sp */
void convert_yuv422sp_to_yuv420sp(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
//LOGI("convert_yuv422sp_to_yuv420sp:width=%d, height=%d\n", width, height);
int line1 = 0, line2 = 0;
int m = 0, n = 0;
int y = 0, u = width * height;
for (int i = 0, j = 1; i < height; i += 2, j += 2)
{
/* Input Buffer Pointer Indexes */
line1 = i * width * 2;
line2 = j * width * 2;
/* Output Buffer Pointer Indexes */
m = width * y;
y = y + 1;
n = width * y;
y = y + 1;
/* Scan two lines at a time */
for (int k = 0; k < (width * 2); k += 4)
{
unsigned char Y1, Y2, U, V;
unsigned char Y3, Y4, U2, V2;
/* Read Input Buffer */
Y1 = InBuff[line1++];
U = InBuff[line1++];
Y2 = InBuff[line1++];
V = InBuff[line1++];
Y3 = InBuff[line2++];
U2 = InBuff[line2++];
Y4 = InBuff[line2++];
V2 = InBuff[line2++];
/* Write Output Buffer */
OutBuff[m++] = Y1;
OutBuff[m++] = Y2;
OutBuff[n++] = Y3;
OutBuff[n++] = Y4;
OutBuff[u++] = (V + V2)/2;
OutBuff[u++] = (U + U2)/2;
}
}
}
/* Convert YUV420sp to YUV420p */
void convert_yuv420sp_to_yuv420p(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
LOGI("convert_yuv422sp_to_yuv420p:width=%d, height=%d\n", width, height);
int i = 0;
// Copy Y first
for (i = 0; i < width * height; i++)
{
OutBuff[i] = InBuff[i];
}
int p = width * height;
int q = (width * height) * 5/4;
// Copy U and V
for (; i < (width * height * 3/2);)
{
// V
OutBuff[q++] = InBuff[i++];
// U
OutBuff[p++] = InBuff[i++];
}
}
/* Convert YUV422sp to YUV420p */
void convert_yuv422sp_to_yuv420p(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
int UOffset = width * height;
int VOffset = (width * height) * 5/4;
int UVSize = (width * height) / 4;
int line1 = 0, line2 = 0;
int m = 0, n = 0;
int y = 0, u = 0, v = 0;
u = UOffset;
v = VOffset;
LOGI("convert_yuv422sp_to_yuv420p:width=%d, height=%d\n", width, height);
#if BLACK_AND_WHITE
memset (OutBuff, 127, (width * height * 2));
for (int i = 0, j = 0; i < (width * height * 2); i+=4)
{
OutBuff[j++] = InBuff[i];
OutBuff[j++] = InBuff[i + 2];
}
#else
for (int i = 0, j = 1; i < height; i += 2, j += 2)
{
/* Input Buffer Pointer Indexes */
line1 = i * width * 2;
line2 = j * width * 2;
/* Output Buffer Pointer Indexes */
m = width * y;
y = y + 1;
n = width * y;
y = y + 1;
/* Scan two lines at a time */
for (int k = 0; k < (width * 2); k += 4)
{
unsigned char Y1, Y2, U, V;
unsigned char Y3, Y4, U2, V2;
/* Read Input Buffer */
Y1 = InBuff[line1++];
U = InBuff[line1++];
Y2 = InBuff[line1++];
V = InBuff[line1++];
Y3 = InBuff[line2++];
U2 = InBuff[line2++];
Y4 = InBuff[line2++];
V2 = InBuff[line2++];
/* Write Output Buffer */
OutBuff[m++] = Y1;
OutBuff[m++] = Y2;
OutBuff[n++] = Y3;
OutBuff[n++] = Y4;
OutBuff[u++] = (U + U2)/2;
OutBuff[u++] = (V + V2)/2;
}
}
#endif
}
2. Convert YUV422sp to YUV420sp
3. Convert YUV420sp to YUV420p
4. Convert YUV422sp to YUV420p
/* Convert YUV420p to YUV422sp */
void convert_yuv420p_to_yuv422sp(char *inbuf, char *outbuf, int width, int height){
long int i, k, j;
long YSize = width * height;
for (i = 0, j = 1; i < height; i += 2, j += 2)
{
long int line1 = i * width;
long int line2 = j * width;
long int UOffset1 = YSize + line1 / 4;
long int VOffset1 = YSize * 5 / 4 + line1 / 4;
long int m = line1 * 2;
long int n = line2 * 2;
for(k = 0; k < width / 4; k++){
unsigned char Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8;
unsigned char U1, U2, V1, V2;
U1 = inbuf[UOffset1++];U2 = inbuf[UOffset1++];
V1 = inbuf[VOffset1++];V2 = inbuf[VOffset1++];
Y1 = inbuf[line1++];Y2 = inbuf[line1++];
Y3 = inbuf[line1++];Y4 = inbuf[line1++];
Y5 = inbuf[line2++];Y6 = inbuf[line2++];
Y7 = inbuf[line2++];Y8 = inbuf[line2++];
outbuf[m++] = Y1;outbuf[m++] = U1;
outbuf[m++] = Y2;outbuf[m++] = V1;
outbuf[m++] = Y3;outbuf[m++] = U2;
outbuf[m++] = Y4;outbuf[m++] = V2;
outbuf[n++] = Y5;outbuf[n++] = U1;
outbuf[n++] = Y6;outbuf[n++] = V1;
outbuf[n++] = Y7;outbuf[n++] = U2;
outbuf[n++] = Y8;outbuf[n++] = V2;
}
}
}
/* Convert YUV422sp to YUV420sp */
void convert_yuv422sp_to_yuv420sp(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
//LOGI("convert_yuv422sp_to_yuv420sp:width=%d, height=%d\n", width, height);
int line1 = 0, line2 = 0;
int m = 0, n = 0;
int y = 0, u = width * height;
for (int i = 0, j = 1; i < height; i += 2, j += 2)
{
/* Input Buffer Pointer Indexes */
line1 = i * width * 2;
line2 = j * width * 2;
/* Output Buffer Pointer Indexes */
m = width * y;
y = y + 1;
n = width * y;
y = y + 1;
/* Scan two lines at a time */
for (int k = 0; k < (width * 2); k += 4)
{
unsigned char Y1, Y2, U, V;
unsigned char Y3, Y4, U2, V2;
/* Read Input Buffer */
Y1 = InBuff[line1++];
U = InBuff[line1++];
Y2 = InBuff[line1++];
V = InBuff[line1++];
Y3 = InBuff[line2++];
U2 = InBuff[line2++];
Y4 = InBuff[line2++];
V2 = InBuff[line2++];
/* Write Output Buffer */
OutBuff[m++] = Y1;
OutBuff[m++] = Y2;
OutBuff[n++] = Y3;
OutBuff[n++] = Y4;
OutBuff[u++] = (V + V2)/2;
OutBuff[u++] = (U + U2)/2;
}
}
}
/* Convert YUV420sp to YUV420p */
void convert_yuv420sp_to_yuv420p(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
LOGI("convert_yuv422sp_to_yuv420p:width=%d, height=%d\n", width, height);
int i = 0;
// Copy Y first
for (i = 0; i < width * height; i++)
{
OutBuff[i] = InBuff[i];
}
int p = width * height;
int q = (width * height) * 5/4;
// Copy U and V
for (; i < (width * height * 3/2);)
{
// V
OutBuff[q++] = InBuff[i++];
// U
OutBuff[p++] = InBuff[i++];
}
}
/* Convert YUV422sp to YUV420p */
void convert_yuv422sp_to_yuv420p(unsigned char *InBuff, unsigned char *OutBuff, int width, int height)
{
int UOffset = width * height;
int VOffset = (width * height) * 5/4;
int UVSize = (width * height) / 4;
int line1 = 0, line2 = 0;
int m = 0, n = 0;
int y = 0, u = 0, v = 0;
u = UOffset;
v = VOffset;
LOGI("convert_yuv422sp_to_yuv420p:width=%d, height=%d\n", width, height);
#if BLACK_AND_WHITE
memset (OutBuff, 127, (width * height * 2));
for (int i = 0, j = 0; i < (width * height * 2); i+=4)
{
OutBuff[j++] = InBuff[i];
OutBuff[j++] = InBuff[i + 2];
}
#else
for (int i = 0, j = 1; i < height; i += 2, j += 2)
{
/* Input Buffer Pointer Indexes */
line1 = i * width * 2;
line2 = j * width * 2;
/* Output Buffer Pointer Indexes */
m = width * y;
y = y + 1;
n = width * y;
y = y + 1;
/* Scan two lines at a time */
for (int k = 0; k < (width * 2); k += 4)
{
unsigned char Y1, Y2, U, V;
unsigned char Y3, Y4, U2, V2;
/* Read Input Buffer */
Y1 = InBuff[line1++];
U = InBuff[line1++];
Y2 = InBuff[line1++];
V = InBuff[line1++];
Y3 = InBuff[line2++];
U2 = InBuff[line2++];
Y4 = InBuff[line2++];
V2 = InBuff[line2++];
/* Write Output Buffer */
OutBuff[m++] = Y1;
OutBuff[m++] = Y2;
OutBuff[n++] = Y3;
OutBuff[n++] = Y4;
OutBuff[u++] = (U + U2)/2;
OutBuff[u++] = (V + V2)/2;
}
}
#endif
}
相关文章推荐
- YUV420 和RGB之间的转换的程序
- ffmpeg命令行实现YUV和RGB之间格式的转换
- YUV420 和RGB之间的转换的程序,YUV采样频率采用cif
- YUY2(YUV) 与 RGB 格式说明及转换程序
- 【OpenGL】用OpenGL shader实现将YUV(YUV420,YV12)转RGB-(直接调用GPU实现,纯硬件方式,效率高) 这段时间一直在搞视频格式的转换问题,终于最近将一个图片的YUV
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- YUY2(YUV) 与 RGB 格式图片的相互转换 以及 基于YUY2(YUV)的blending
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- RGB与YUV图像视频格式的相互转换
- YUV与RGB图像格式之间的关系
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- YUV格式学习:YUV420P、YV12、NV12、NV21格式转换成RGB24
- 使用ffmpeg进行图像格式转换以及图像缩放/sws_scale/linux/c++/c/rgb-yuv420
- RGB与YUV图像视频格式的相互转换
- RGB与YUV图像视频格式的相互转换
- 十六进制颜色值域RGB格式颜色值之间的相互转换
- YUY2(YUV) 与 RGB 格式图片的相互转换 以及 基于YUY2(YUV)的blending
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- FFMPEG 实现 YUV,RGB各种图像原始数据之间的转换(swscale)
- YUV和RGB之间的转换公式