天天看點

nv21轉jpg c語言,YUV格式、RGB格式、JPEG格式、MJPEG格式之間的轉換(C程式)之一...

1. Convert YUV420p to YUV422sp

2. Convert YUV422sp to YUV420sp

3. Convert YUV420sp to YUV420p

4. Convert YUV422sp to YUV420p

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;

}

}

}

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)

{

line1 = i * width * 2;

line2 = j * width * 2;

m = width * y;

y = y + 1;

n = width * y;

y = y + 1;

for (int k = 0; k < (width * 2); k += 4)

{

unsigned char Y1, Y2, U, V;

unsigned char Y3, Y4, U2, V2;

Y1 = InBuff[line1++];

U  = InBuff[line1++];

Y2 = InBuff[line1++];

V  = InBuff[line1++];

Y3 = InBuff[line2++];

U2 = InBuff[line2++];

Y4 = InBuff[line2++];

V2 = InBuff[line2++];

OutBuff[m++] = Y1;

OutBuff[m++] = Y2;

OutBuff[n++] = Y3;

OutBuff[n++] = Y4;

OutBuff[u++] = (V + V2)/2;

OutBuff[u++] = (U + U2)/2;

}

}

}

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++];

}

}

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)

{

line1 = i * width * 2;

line2 = j * width * 2;

m = width * y;

y = y + 1;

n = width * y;

y = y + 1;

for (int k = 0; k < (width * 2); k += 4)

{

unsigned char Y1, Y2, U, V;

unsigned char Y3, Y4, U2, V2;

Y1 = InBuff[line1++];

U  = InBuff[line1++];

Y2 = InBuff[line1++];

V  = InBuff[line1++];

Y3 = InBuff[line2++];

U2 = InBuff[line2++];

Y4 = InBuff[line2++];

V2 = InBuff[line2++];

OutBuff[m++] = Y1;

OutBuff[m++] = Y2;

OutBuff[n++] = Y3;

OutBuff[n++] = Y4;

OutBuff[u++] = (U + U2)/2;

OutBuff[u++] = (V + V2)/2;

}

}

#endif

}