yv12转nv12,nv12转I420

yv12跟I420区别其实就是u、v顺序不一样,是平面格式,内存中u、v在连续的一块,nv12是打包格式,u、v交叉。


yv12转nv12:

void swapYV12toNV12(byte[] yv12bytes, byte[] nv12bytes, int width,int height) {

int nLenY = width * height;
int nLenU = nLenY / 4;


System.arraycopy(yv12bytes, 0, nv12bytes, 0, width * height);
for (int i = 0; i < nLenU; i++) {
nv12bytes[nLenY + 2 * i] = yv12bytes[nLenY + i];
nv12bytes[nLenY + 2 * i + 1] = yv12bytes[nLenY + nLenU + i];
}
}



nv12转I420:

void swapNV12toI420(byte[] nv12bytes, byte[] i420bytes, int width,int height) {

int nLenY = width * height;
int nLenU = nLenY / 4;


System.arraycopy(nv12bytes, 0, i420bytes, 0, width * height);
for (int i = 0; i < nLenU; i++) {
i420bytes[nLenY + i] = nv12bytes[nLenY + 2 * i + 1];
i420bytes[nLenY + nLenU + i] = nv12bytes[nLenY + 2 * i];
}
}


转换后如果发现颜色不对,u、v顺序换一下。

你可能感兴趣的:(c++,图像处理(opengl))