格式转换至yuv422转 yuv420

 

//pYUV为422,yuv为420
/*ok!
* brief:pyuv is yuv422sp srcIn, and yuv is yuv420p desOut 
*/
int YUV422To420(unsigned char *pYUV, unsigned char*yuv, int lWidth, int lHeight)
{        
int i,j;
unsigned char*pY = yuv;
unsigned char *pU = yuv + lWidth*lHeight;
unsigned char *pV = pU + (lWidth*lHeight)/4;
 
unsigned char *pYUVTemp = pYUV;
unsigned char *pYUVTempNext = pYUV+lWidth*2;
         
 for(i=0; i<lheight; i+=2) <="" div="">
 {
    for(j=0; j<lwidth; j+=2) <="" div="">
     {
       pY[j] = *pYUVTemp++;
       pY[j+lWidth] = *pYUVTempNext++;
                         
       pU[j/2] =(*(pYUVTemp) + *(pYUVTempNext))/2;
       pYUVTemp++;
       pYUVTempNext++;
                         
       pY[j+1] = *pYUVTemp++;
       pY[j+1+lWidth] = *pYUVTempNext++;
                         
       pV[j/2] =(*(pYUVTemp) + *(pYUVTempNext))/2;
       pYUVTemp++;
       pYUVTempNext++;
     }
       pYUVTemp+=lWidth*2;
       pYUVTempNext+=lWidth*2;
       pY+=lWidth*2;
       pU+=lWidth/2;
       pV+=lWidth/2;
   }
         
       return 1;
}

 

 

大意了,每四行计算时,牵引错误,试试下面的代码吧,不过这种代码很有优化的余地
书籍可以参考“视频技术手册”,上面有YUV422到YUV420采样转换的讲解

C/C++ code
 


int YUV422To420(unsigned char *pYUV, unsigned int *yuv, int lWidth, int lHeight)
{ //sp
int i,j;
unsigned int *pY = yuv;
unsigned int *pU = yuv + lWidth*lHeight;
unsigned int *pV = pU + (lWidth*lHeight)/4;

unsigned char *pYUVTemp = pYUV;
unsigned char *pYUVTempNext = pYUV+lWidth*2;
unsigned char *pYUV2 = pYUVTempNext + lWidth*2;
unsigned char *pYUV3 = pYUVTempNext + lWidth*4;

for(i=0; i<lHeight; i+=4)
{
for(j=0; j<lWidth; j+=4)
{
// Y0
pY[j] = pYUVTemp[j];
pY[j+lWidth] = pYUVTempNext[j];
pY[j+lWidth*2] = pYUV2[j];
pY[j+lWidth*3] = pYUV3[j];
// U
pU[j/2] = (3*pYUVTemp[j+1] + pYUV2[j+1])/4;
pU[j/2+lWidth/2] = (pYUVTempNext[j+1] + 3*pYUV3[j+1])/4;
// Y1
pY[j+1] = pYUVTemp[j+2];
pY[j+lWidth+1] = pYUVTempNext[j+2];
pY[j+lWidth*2+1] = pYUV2[j+2];
pY[j+lWidth*3+1] = pYUV3[j+2];
// V
pV[j/2] = (3*pYUVTemp[j+3] + pYUV2[j+3])/4;
pV[j/2+lWidth/2] = (pYUVTempNext[j+3] + 3*pYUV3[j+3])/4;

}
// 分别加四行
pYUVTemp+=lWidth*2*4;
pYUVTempNext+=lWidth*2*4;
pYUV2+=lWidth*2*4;
pYUV3+=lWidth*2*4;
// Y加上四行
pY+=lWidth*4;
// UV加两行
pU+=lWidth;
pV+=lWidth;
}

return 1;
}


http://www.xuebuyuan.com/1541892.html

http://bbs.csdn.net/topics/370045077

http://blog.csdn.net/searchsun/article/details/2443867  yuv pannle packet

http://blog.chinaunix.net/uid-29181887-id-3992543.html

http://www.videolan.org/developers/x264.html x264编码器

http://www.cnblogs.com/zhengbin/p/5023640.html js 黑客帝国特效

posted @ 2014-11-16 22:54  midu  阅读(1109)  评论(0编辑  收藏  举报