admin管理员组

文章数量:821332

yv12、I420、nv12、nv21相互转换

YUV420P,Y,U,V三个分量都是平面格式,分为I420和YV12。I420格式和YV12格式的不同处在U平面和V平面的位置不同。在I420格式中,U平面紧跟在Y平面之后,然后才是V平面(即:YUV);但YV12则是相反(即:YVU)。 YUV420SP, Y分量平面格式,UV打包格式, 即NV12。 NV12与NV21类似,U 和 V 交错排列,不同在于UV顺序。 I420: YYYYYYYY UU VV    =>YUV420P YV12: YYYYYYYY VV UU    =>YUV420P NV12: YYYYYYYY UVUV     =>YUV420SP NV21: YYYYYYYY VUVU     =>YUV420SP 1、 NV21 nv12 private void swapNV21ToNV12( byte [] nv21, byte [] nv12, int width, int height){ if (nv21 == null || nv12 == null ) return ; int framesize = width*height; int i = 0 ,j = 0 ; System. arraycopy (nv21, 0 , nv12, 0 , framesize); for (j = 0 ; j < framesize/ 2 ; j+= 2 ) { nv12[framesize + j + 1 ] = nv21[j + framesize]; }
for (j = 0 ; j < framesize/ 2 ; j += 2 ) { nv12[framesize + j] = nv21[j + framesize + 1 ]; } }
2、YV12转I420 private void swapYV12toI420( byte [] yv12bytes, byte [] i420bytes, int width , int height) { System. arraycopy (yv12bytes, 0 , i420bytes, 0 , width *height); System. arraycopy (yv12bytes, width *height+ width *height/ 4 , i420bytes, width *height, width *height/ 4 ); System. arraycopy (yv12bytes, width *height, i420bytes, width *height+ width *height/ 4 , width *height/ 4 ); }
3、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 + 1 ] = yv12bytes[nLenY + i]; nv12bytes[nLenY + 2 * i] = yv12bytes[nLenY + nLenU + i]; } }
4、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]; }


本文标签: yv12I420nv12nv21相互转换