NV21 to I420 in android

2019-08-10 03:10发布

问题:

EDIT : I came across this libyuv that does the NV21 to I420 conversion, but i don't really understand how to call it.

// Convert NV21 to I420.  Same as NV12 but u and v pointers swapped.
LIBYUV_API
int NV21ToI420(const uint8* src_y, int src_stride_y,
           const uint8* src_vu, int src_stride_vu,
           uint8* dst_y, int dst_stride_y,
           uint8* dst_u, int dst_stride_u,
           uint8* dst_v, int dst_stride_v,
           int width, int height) 

I am passing the NV21 byte[] obtained from camera callback to the jni layer and converting it to unsigned char* as below

int yuvBufLen = env->GetArrayLength(yuvNV21);
unsigned char* yuvNV21Buf = new unsigned char[yuvBufLen];
env->GetByteArrayRegion(yuvNV21, 0, yuvBufLen,reinterpret_cast<jbyte*>(yuvNV21Buf));

Now it is not clear to me how do i get the different parameters required to call the libyuv function NV21ToI420. What does each of the following parameters represent and how to obtain them from the unsigned char* yuvNV21Buf I have ??

const uint8* src_y,
int src_stride_y,
const uint8* src_vu,
int src_stride_vu,
uint8* dst_y,
int dst_stride_y,
uint8* dst_u,
int dst_stride_u,
uint8* dst_v,
int dst_stride_v

I have checked this obtain yuv420 in ios which explains how to get all the required parameters to call libyuv::NV12ToI420.

Can someone please explain me how to achieve this??

I am capturing frame byte[] from camera through the preview callback below

@Override
        public void onPreviewFrame(byte[] frameData, Camera camera) {

The frameData am getting is in NV21 format and I am trying to convert NV21 to I420.

回答1:

I could not get to work the method I posted in the question, it kept crashing.

But thanks to Peter I found this NV21ToI420 conversion and it worked perfect for me.

Simply it needs the NV21 and the I420 pointer.

unsigned char* yuvPtr; //NV21 data
unsigned char* I420 = new unsigned char[sourceWidth*sourceHeight*1.5]; //destination I420 pointer where the converted yuv will be stored 
NV21toI420(yuvPtr, I420, sourceWidth, sourceHeight);

Thats all....