所使用的转换公式:
C = Y - 16
D = U - 128
E = V - 128
R = ( 298 * C + 409 * E + 128) >> 8
G = ( 298 * C - 100 * D - 208 * E + 128) >> 8
B = ( 298 * C + 516 * D + 128) >> 8
代码如下,前面三个参数是YUV数据的Y、U、V分量;destARGB保存转换后的RGB数据,存放的格式是每个像素32位,从头到尾分别是8位的A、R、G、B值,这里A值没有用;后面两个参数分别是视频的宽和高的值,以像素为单位。代码中有很多+256和对256进行比较的操作,主要是为了实现对无符号数的操作,Java没有无符号类型的。
public int YUV420toRGB32(byte[] srcY, byte[] srcU, byte[] srcV, int[] destARGB, int width, int height) {
int wrap, x, y, pyID, destID, puvID, destID_1,tmp;
int sy, su, sv;
wrap = width;
pyID = 0;
destID = 0;
destID_1 = 0;
puvID = 0;
tmp = 0;
for (y = 0; y < height; y += 2) {
for (x = 0; x < width; x += 2) {
sy = srcY[pyID] >= 0 ? srcY[pyID] : srcY[pyID] + 256;
su = srcU[puvID] >= 0 ? srcU[puvID] : srcU[puvID] + 256;
sv = srcV[puvID] >= 0 ? srcV[puvID] : srcV[puvID] + 256;
destARGB[destID] = 0;
tmp = (int) ((298 * (sy - 16) + 409 * (sv - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0) : 255)) << 8;
tmp = (int) ((298 * (sy - 16) - 100 * (su - 128) - 208 * (sv - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) + 516 * (su - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0): 255));
sy = srcY[pyID + 1] >= 0 ? srcY[pyID + 1] : srcY[pyID + 1] + 256;
destID_1 = destID + 1;
destARGB[destID_1] = 0;
tmp = (int) ((298 * (sy - 16) + 409 * (sv - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) - 100 * (su - 128) - 208 * (sv - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) + 516 * (su - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255));
pyID += wrap;
destID += wrap;
sy = srcY[pyID] >= 0 ? srcY[pyID] : srcY[pyID] + 256;
destARGB[destID] = 0;
tmp = (int) ((298 * (sy - 16) + 409 * (sv - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) - 100 * (su - 128) - 208 * (sv - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) + 516 * (su - 128) + 128) >> 8);
destARGB[destID] = (destARGB[destID] | (tmp < 256 ? (tmp>0?tmp:0): 255));
sy = srcY[pyID + 1] >= 0 ? srcY[pyID + 1] : srcY[pyID + 1] + 256;
destID_1 = destID + 1;
destARGB[destID_1] = 0;
tmp = (int) ((298 * (sy - 16) + 409 * (sv - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) - 100 * (su - 128) - 208 * (sv - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255)) << 8;
tmp = (int) ((298 * (sy - 16) + 516 * (su - 128) + 128) >> 8);
destARGB[destID_1] = (destARGB[destID_1] | (tmp < 256 ? (tmp>0?tmp:0): 255));
pyID += (-wrap + 2);
destID += (-wrap + 2);
puvID++;
}
pyID += wrap;
destID += wrap;
}
return 0;
}