YUV420sp与YUV420p互转

JNIEXPORT jbyteArray JNICALL
Java_com_uestc_smileteeth_view_recordvideo_RecordVideoLib_YUV420spToYUV420p(JNIEnv *env,
                                                                            jclass type,
                                                                            jbyteArray yuv420sp_,
                                                                            jint width,
                                                                            jint height) {
    jbyte *yuv420sp = (*env)->GetByteArrayElements(env, yuv420sp_, NULL);

    jbyteArray yuv420pRet = (*env)->NewByteArray(env, (*env)->GetArrayLength(env, yuv420sp_));
    jbyte *yuv420p = (*env)->GetByteArrayElements(env, yuv420pRet, NULL);
    int i, j;
    int y_size = width * height;

    unsigned char *y = yuv420sp;
    unsigned char *vu = yuv420sp + y_size;

    unsigned char *y_tmp = yuv420p;
    unsigned char *u_tmp = yuv420p + y_size;
    unsigned char *v_tmp = yuv420p + y_size * 5 / 4;

    // y
    memcpy(y_tmp, y, y_size);

    // v,u
    for (j = 0, i = 0; j < y_size / 2; j += 2, i++) {
        v_tmp[i] = vu[j];
        u_tmp[i] = vu[j + 1];
    }

    (*env)->ReleaseByteArrayElements(env, yuv420sp_, yuv420sp, 0);
    (*env)->ReleaseByteArrayElements(env, yuv420pRet, yuv420p, 0);
    return yuv420pRet;
}

yuv420p转yuv420sp

JNIEXPORT jbyteArray JNICALL
Java_com_uestc_smileteeth_view_recordvideo_RecordVideoLib_YUV420pToYUV420sp(JNIEnv *env,
                                                                            jclass type,
                                                                            jbyteArray yuv420p_,
                                                                            jint width,
                                                                            jint height) {
    jbyte *yuv420p = (*env)->GetByteArrayElements(env, yuv420p_, NULL);

    jbyteArray yuv420spRet = (*env)->NewByteArray(env, (*env)->GetArrayLength(env, yuv420p_));
    jbyte *yuv420sp = (*env)->GetByteArrayElements(env, yuv420spRet, NULL);
    int i, j;
    int y_size = width * height;

    unsigned char *y = yuv420sp;
    unsigned char *vu = yuv420sp + y_size;

    unsigned char *y_tmp = yuv420p;
    unsigned char *u_tmp = yuv420p + y_size;
    unsigned char *v_tmp = yuv420p + y_size * 5 / 4;

    // y
    memcpy(y, y_tmp, y_size);

    // v,u
    for (j = 0, i = 0; j < y_size / 2; j += 2, i++) {
        vu[j] = v_tmp[i];
        vu[j + 1] = u_tmp[i];
    }

    (*env)->ReleaseByteArrayElements(env, yuv420p_, yuv420p, 0);
    (*env)->ReleaseByteArrayElements(env, yuv420spRet, yuv420sp, 0);
    return yuv420spRet;
}

原文链接:https://blog.csdn.net/Emily2015/article/details/79271199
posted @ 2020-04-22 08:46  wywdahai  阅读(1531)  评论(0编辑  收藏  举报