YUV420sp与YUV420p互转

Posted yanwei-wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于YUV420sp与YUV420p互转的主要内容,如果未能解决你的问题,请参考以下文章

C++ YUV420文件读取与显示,绘制矩形框,绘制线段(绘制直线),绘制多边形(常用YUV数据格式——YUV420P中的YU12与YUV420SP中的NV21)

C++ YUV420文件读取与显示,绘制矩形框,绘制线段(绘制直线),绘制多边形(常用YUV数据格式——YUV420P中的YU12与YUV420SP中的NV21)

2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,采用YUV420P转YUV420SP的方式。

视频知识点(22)- 教你认清楚YUV420P和YUV420SP的真正差异在哪里

视频知识点(22)- 教你认清楚YUV420P和YUV420SP的真正差异在哪里

2023-02-25:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,YUV420P不要转换成YUV420SP。