yuv nv21 nv12 i420 从代码地址理解记录

对于nv21 nv12的理解,理论自己有些理解,但是对于y分量 u分量 v分量的理解,有些不深入

参考网络代码,自己深入理解一下

I420: YYYYYYYY UU VV    =>YUV420P
YV12: YYYYYYYY VV UU    =>YUV420P
NV12: YYYYYYYY UVUV     =>YUV420SP
NV21: YYYYYYYY VUVU     =>YUV420SP


NV21: YYYYYYYY VUVU     =>YUV420SP
-----》转换成 
NV12: YYYYYYYY UVUV     =>YUV420SP
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); //Y分量及UV全部处理,再细化UV分量
    for (j = 0; j < framesize/2; j+=2)
    {
        nv12[framesize + j + 1] = nv21[j + framesize];
        // nv21 +framesize +j + 1 //表示 U分量
        // nv21[j + framesize]; //表示U分量
        // U分量与U分量之间的处理
    }

    for (j = 0; j < framesize/2; j += 2)
    {
        nv12[framesize + j] = nv21[j + framesize + 1];
        // nv12[framesize + j] //V分量
        // nv21[j + framesize + 1]; // 分量
         v 分量与v 分量之间的处理
    }
}


YV12: YYYYYYYY VV UU    =>YUV420P  // Y分量 U wdith*heith/4
-----》转换成 
I420: YYYYYYYY UU VV    =>YUV420P

2、YV12转I420
// 源码:public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos,int length);
private void swapYV12toI420(byte[] yv12bytes, byte[] i420bytes, int width, int height)
{
    System.arraycopy(yv12bytes, 0, i420bytes, 0,width*height); // Y 分量
    System.arraycopy(yv12bytes, width*height+width*height/4, i420bytes, width*height,width*height/4);
    // yv12  width*height+width*height/4  V 分量  width*height+width*height/4
    // i420  V 分量  width*height
    System.arraycopy(yv12bytes, width*height, i420bytes, width*height+width*height/4,width*height/4);
    //yv12  U 分量width*height
    //i420  U 分量  width*height+width*height/4
}

YV12: YYYYYYYY VV UU    =>YUV420P
-----》转换成 
NV12: YYYYYYYY UVUV     =>YUV420SP

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 U 
        nv12bytes[nLenY + 2 * i] = yv12bytes[nLenY + nLenU + i];
    }
}

NV12: YYYYYYYY UVUV     =>YUV420SP
-----》转换成 
I420: YYYYYYYY UU VV    =>YUV420P
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];
    }

extern "C" JNIEXPORT void JNICALL
Java_com_xxxxx_NV21toI420(
        JNIEnv* env, jclass clazz, jbyteArray nv21bytes,jint width,jint height,jbyteArray i420bytes) {
    unsigned char* nv21bytes_buf = (unsigned char*)(env->GetByteArrayElements(nv21bytes, NULL));
    if ( nv21bytes_buf == NULL ) {
        return;
    }
    unsigned char* i420bytes_buf = (unsigned char*)(env->GetByteArrayElements(i420bytes, NULL));
    if ( i420bytes_buf == NULL ) {
        return;
    }
    memcpy(i420bytes_buf,nv21bytes_buf,width*height);

    int total = width *height;
    int nLen = total / 4;
    for ( int i = 0; i < nLen; i++){
        memcpy(i420bytes_buf+total+i,nv21bytes_buf+total+2*i,1);
        memcpy(i420bytes_buf+total+nLen+i,nv21bytes_buf+total+2*i+1,1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值