iOS libyuv

libyuv静态库

为了方便使用,已经将libyuv源代码打包成了iOS静态库,libyuv静态库

libyuv使用

下面以nv12(yuv420sp)转化为I420(yuv420p)为例:

void nv12_to_yuv420p(unsigned char* nv12, unsigned char* yuv420p, int width, int height) {

    int y_size = width * height;
    int u_size = y_size / 4;
    int v_size = u_size;
    int uv_size = u_size + v_size;

    unsigned char *src_y = malloc(y_size);
    unsigned char *src_uv = malloc(uv_size);
    memcpy(src_y, nv12, y_size);
    memcpy(src_uv, nv12 + y_size, uv_size);

    unsigned char *dst_y = malloc(y_size);
    unsigned char *dst_u = malloc(u_size);
    unsigned char *dst_v = malloc(v_size);

    int src_stride_y = width;
    int src_stride_uv = width;
    int dst_stride_y = width;
    int dst_stride_u = width >> 1;
    int dst_stride_v = dst_stride_u;

    NV12ToI420(src_y, src_stride_y, src_uv, src_stride_uv, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, width, height);
    memcpy(yuv420p, dst_y, y_size);
    memcpy(yuv420p + y_size, dst_u, u_size);
    memcpy(yuv420p + y_size + u_size, dst_v, v_size);

    free(src_y);
    free(src_uv);
    free(dst_y);
    free(dst_u);
    free(dst_v);
}

nv1二转化yuv420p贯彻方式有多样,比如利用libyuv,ffmpeg,除了行使第2方库达成转化,也得以协调完毕,代码如下:

void yuv420sp_to_yuv420p(unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height)
{
    int i, j;
    int y_size = width * height;

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

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

    memcpy(y_tmp, y, y_size);

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

libyuv是谷歌(Google)开源库,可用作图像数据格式的变换,比如录制流编解码时格式的转换,YUV数据转载卡宴GB等

libyuv是谷歌(Google)开源库,可用作图像数据格式的转换,比如摄像流编解码时格式的转移,YUV数据转载福特ExplorerGB等

libyuv使用

下面以nv12(yuv420sp)转化为I420(yuv420p)为例:

void nv12_to_yuv420p(unsigned char* nv12, unsigned char* yuv420p, int width, int height) {

    int y_size = width * height;
    int u_size = y_size / 4;
    int v_size = u_size;
    int uv_size = u_size + v_size;

    unsigned char *src_y = malloc(y_size);
    unsigned char *src_uv = malloc(uv_size);
    memcpy(src_y, nv12, y_size);
    memcpy(src_uv, nv12 + y_size, uv_size);

    unsigned char *dst_y = malloc(y_size);
    unsigned char *dst_u = malloc(u_size);
    unsigned char *dst_v = malloc(v_size);

    int src_stride_y = width;
    int src_stride_uv = width;
    int dst_stride_y = width;
    int dst_stride_u = width >> 1;
    int dst_stride_v = dst_stride_u;

    NV12ToI420(src_y, src_stride_y, src_uv, src_stride_uv, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, width, height);
    memcpy(yuv420p, dst_y, y_size);
    memcpy(yuv420p + y_size, dst_u, u_size);
    memcpy(yuv420p + y_size + u_size, dst_v, v_size);

    free(src_y);
    free(src_uv);
    free(dst_y);
    free(dst_u);
    free(dst_v);
}

nv12转化yuv420p得以实现格局有多样,比如动用libyuv,ffmpeg,除了运用第3方库达成转化,也能够团结落成,代码如下:

void yuv420sp_to_yuv420p(unsigned char* yuv420sp, unsigned char* yuv420p, int width, int height)
{
    int i, j;
    int y_size = width * height;

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

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

    memcpy(y_tmp, y, y_size);

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

libyuv静态库

为了方便使用,已经将libyuv源代码打包成了iOS静态库,libyuv静态库