Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
194 lines (155 sloc) 5.51 KB

概述

libyuv是Google开源的实现各种YUV与RGB之间相互转换、旋转、缩放的库。它是跨平台的,可在Windows、Linux、Mac、Android等操作系统,x86、x64、arm架构上进行编译运行,支持SSE、AVX、NEON等SIMD指令加速。

环境配置

操作系统:ubuntu 16.05 ndk版本:android-ndk-r16b

开始编译

第一步:先克隆出最新的libyuv版本

git clone git@github.com:lemenkov/libyuv.git

第二步:重命名libyuvjni,编辑Android.mk文件,注释掉JPEG相关的配置

第三步:新建Application.mk文件

APP_ABI := armeabi-v7a x86 x86_64
APP_PLATFORM := android-16
APP_STL := stlport_static  
APP_CPPFLAGS += -fno-rtti  

开始编译ndk-build

工程实践

还是基于之前的graphic4android工程,新建native-libyuv工程,配置可以参考之前的配置:

enum Type {
    TYPE_YUV420P_TO_RGB24 = 0,
    TYPE_NV12_TO_RGB24 = 1,
    TYPE_NV21_TO_RGB24 = 2
};

/**
 * I420就是YUV420P
 * @param yuvData
 * @param rgb24
 * @param width
 * @param height
 */
void I420_TO_RGB24(unsigned char *yuvData, unsigned char *rgb24, int width, int height) {

    unsigned char *ybase = yuvData;
    unsigned char *ubase = &yuvData[width * height];
    unsigned char *vbase = &yuvData[width * height * 5 / 4];
    //YUV420P转RGB24
    libyuv::I420ToRGB24(ybase, width, ubase, width / 2, vbase, width / 2,
                        rgb24,
                        width * 3, width, height);

}

/**
 * NV12属于YUV420SP
 * @param yuvData
 * @param rgb24
 * @param width
 * @param height
 */
void NV12_TO_RGB24(unsigned char *yuvData, unsigned char *rgb24, int width, int height) {

    unsigned char *ybase = yuvData;
    unsigned char *uvbase = &yuvData[width * height];
    //NV12转RGB24
    libyuv::NV12ToRGB24(ybase, width, uvbase, width,
                        rgb24,
                        width * 3, width, height);

}

/**
 * NV21属于YUV420SP
 * @param yuvData
 * @param rgb24
 * @param width
 * @param height
 */
void NV21_TO_RGB24(unsigned char *yuvData, unsigned char *rgb24, int width, int height) {

    unsigned char *ybase = yuvData;
    unsigned char *vubase = &yuvData[width * height];
    //NV21转RGB24
    libyuv::NV21ToRGB24(ybase, width, vubase, width,
                        rgb24,
                        width * 3, width, height);

}

void drawYUV(const char *path, int type, int width, int height, ANativeWindow_Buffer buffer) {
    FILE *file = fopen(path, "rb");

    int frameSize = width * height * 3 / 2;

    unsigned char *yuvData = new unsigned char[frameSize];

    fread(yuvData, 1, frameSize, file);

    unsigned char *rgb24 = new unsigned char[width * height * 3];

    //YUV转RGB24
    switch (type) {
        case TYPE_YUV420P_TO_RGB24:
            //YUV420P转RGB24
            I420_TO_RGB24(yuvData, rgb24, width, height);
            break;
        case TYPE_NV12_TO_RGB24:
            //YUV420SP转RGB24
            NV12_TO_RGB24(yuvData, rgb24, width, height);
            break;
        case TYPE_NV21_TO_RGB24:
            //YUV420SP转RGB24
            NV21_TO_RGB24(yuvData, rgb24, width, height);
            break;
    }

    uint32_t *line = (uint32_t *) buffer.bits;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int index = y * width + x;
            line[x] = rgb24[index * 3] << 16
                      | rgb24[index * 3 + 1] << 8
                      | rgb24[index * 3 + 2];
        }
        line = line + buffer.stride;
    }

    //释放内存
    delete[] yuvData;
    delete[] rgb24;

    //关闭文件句柄
    fclose(file);
}

void yuv2rgb(JNIEnv *env, jobject obj, jstring yuvPath, jint type, jint width, jint height,
             jobject surface) {

    const char *path = env->GetStringUTFChars(yuvPath, 0);

    //获取目标surface
    ANativeWindow *window = ANativeWindow_fromSurface(env, surface);
    if (NULL == window) {
        ThrowException(env, "java/lang/RuntimeException", "unable to get native window");
        return;
    }
    //默认的是RGB_565
    int32_t result = ANativeWindow_setBuffersGeometry(window, 0, 0, WINDOW_FORMAT_RGBA_8888);
    if (result < 0) {
        ThrowException(env, "java/lang/RuntimeException", "unable to set buffers geometry");
        //释放窗口
        ANativeWindow_release(window);
        window = NULL;
        return;
    }
    ANativeWindow_acquire(window);

    ANativeWindow_Buffer buffer;
    //锁定窗口的绘图表面
    if (ANativeWindow_lock(window, &buffer, NULL) < 0) {
        ThrowException(env, "java/lang/RuntimeException", "unable to lock native window");
        //释放窗口
        ANativeWindow_release(window);
        window = NULL;
        return;
    }

    //绘制YUV420P
    drawYUV(path, type, width, height, buffer);

    //解锁窗口的绘图表面
    if (ANativeWindow_unlockAndPost(window) < 0) {
        ThrowException(env, "java/lang/RuntimeException",
                       "unable to unlock and post to native window");
    }

    env->ReleaseStringUTFChars(yuvPath, path);
    //释放
    ANativeWindow_release(window);
}

直接调用libyuv库里的函数即可完成转换过程

You can’t perform that action at this time.