YUV420planar转nv21 native
时间: 2023-09-10 14:09:20 浏览: 111
以下是将YUV420planar格式转换为NV21格式的native实现代码:
```c
#include <jni.h>
#include <android/log.h>
#define LOG_TAG "YUV420P2NV21"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
JNIEXPORT void JNICALL
Java_com_example_yuv420p2nv21_Yuv420p2Nv21_yuv420p2Nv21(JNIEnv *env, jclass clazz,
jbyteArray yuv420p, jbyteArray nv21,
jint width, jint height) {
jbyte *yuv420pData = (*env)->GetByteArrayElements(env, yuv420p, NULL);
jbyte *nv21Data = (*env)->GetByteArrayElements(env, nv21, NULL);
int nv21Len = width * height * 3 / 2;
int ySize = width * height;
int uSize = ySize / 4;
int vSize = ySize / 4;
// Copy Y data
memcpy(nv21Data, yuv420pData, ySize);
// Convert VU plane to VU interleaved
for (int i = 0; i < uSize; i++) {
nv21Data[ySize + i * 2] = yuv420pData[ySize + uSize + i];
nv21Data[ySize + i * 2 + 1] = yuv420pData[ySize + i];
}
// Copy U data
memcpy(nv21Data + ySize + uSize * 2, yuv420pData + ySize + vSize, vSize);
(*env)->ReleaseByteArrayElements(env, yuv420p, yuv420pData, 0);
(*env)->ReleaseByteArrayElements(env, nv21, nv21Data, 0);
}
```
这段代码假定输入的YUV420planar数据格式为YUV420planar,即Y、U、V分别存储的格式。函数参数中的yuv420p和nv21均为Java层传进来的byte数组,width和height为图像的宽高。
该实现的原理是将Y数据直接拷贝到NV21数组中,并将U和V交错存储到NV21数组的VU平面中。注意,NV21格式中V和U的顺序是反过来的。
阅读全文