最近在弄YUV图像格式转换的问题,想要将YV16格式转换到I420,但是出来的结果是转换成了YV12,也就是转换结果中的UV分量互换了。
代码如下,想确认下YV16中的UV分量存储顺序是V前,U后吧,那这样的话代码逻辑应该是没有问题的?
另外我也看到过一个别人分享的YV16转I420,看他的代码逻辑,表现出来的YV16格式是U前,V后,所以就很困惑,还望有人能够解惑!
//----------------------------------我的代码
/** YV16 422P
yyyy yyyy
vvvv
uuuu
-> I420 420P
yyyy yyyy
uu
vv
*/
void yuv422p_to_yuv420p(unsigned char* yuv422p, unsigned char* yuv420p, int width, int height)
{
int i, j;
int y_size = width * height;
unsigned char* y = yuv422p;
unsigned char* v = yuv422p + y_size;
unsigned char* u = yuv422p + y_size + y_size/2;
unsigned char* y_tmp = yuv420p;
unsigned char* u_tmp = yuv420p + y_size;
unsigned char* v_tmp = yuv420p + y_size + y_size/ 4;
// y
memcpy(y_tmp, y, y_size);
// u
for(j=0; j< height; j=j+2)
{
for(i=0; i<width/2; i=i++)
{
u_tmp[width/2 * (j/2) + i] = u[width/2 * j + i];
v_tmp[width/2 * (j/2) + i] = v[width/2 * j + i];
}
}
}
//------------------别人的代码
int yv16_to_i420(unsigned char* pImageBuf, int width, int height)
{
int i, j;
int y_size,uvw;
unsigned char* p_u;
unsigned char* p_v;
if(NULL == pImageBuf)
return -1;
uvw = width>>1;
y_size = width * height;
p_u = pImageBuf + y_size;
p_v = p_u + (y_size>>1);
i = uvw;
for(j = 2;j<height;j+=2)
{
memcpy(p_u + i,p_u+j*uvw, uvw);
i+= uvw;
}
p_u += (y_size>>2);
i = 0;
for (j = 0; j < height; j+=2)
{
memcpy(p_u + i, p_v+j*uvw,uvw);
i+= uvw;
}
return 0;
}