YUV图像格式转换方法实践

一 I420转NV12

1.I420格式

(1)I420是每四个Y共用一组UV,如果一帧I420图像宽带是width,高度是height,1个像素占1个字节,那么共有width✖height个Y,U和V都是width✖height / 4个,因此一帧I420图像占用字节数是width✖height✖3/2。

(2)I420是先存完所有Y后,在接着存完U,最后存V。

YUV图像格式转换方法实践_第1张图片

2.NV12格式

(1)NV12是每四个Y共用一组UV,如果一帧NV12图像宽带是width,高度是height,1个像素占1个字节,那么共有width✖height个Y,U和V都是width✖height / 4个,因此一帧I420图像占用字节数是width✖height✖3/2。

(2)NV12是先存完所有Y后,随后U、V交替存储。

YUV图像格式转换方法实践_第2张图片

3.转换方法

(1)将一帧I420图像中所有Y拷贝到NV12数据区,将一帧I420图像中所有U和V交替拷贝到NV12数据区。

(2)代码:

bool convert_i420_nv12(const char *i420_file_path, size_t width, size_t height, const char *nv12_file_path) {
    if (!i420_file_path || !nv12_file_path) {
        return false;
    }
    FILE *fp = fopen(i420_file_path, "rb");
    if (!fp) {
        return false;
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    size_t y_size = width * height;
    size_t uv_size = y_size / 4;
    if (file_size != (y_size + uv_size * 2)) {
        fclose(fp);
        return false;
    }
    char *i420_content = (char *)malloc(sizeof(char) * file_size);
    if (!i420_content) {
        fclose(fp);
        return false;
    }
    if (file_size != fread(i420_content, 1, file_size, fp)) {
        free(i420_content);
        fclose(fp);
        return false;
    }
    fclose(fp);
    // convert i420 to nv12
    char *nv12_content = (char *)malloc(sizeof(char) * file_size);
    if (!nv12_content) {
        free(i420_content);
        return false;
    }
    // copy y channel
    memcpy(nv12_content, i420_content, y_size);
    // copy uv channel
    char *i420_u_base = i420_content + y_size;
    char *i420_v_base = i420_u_base + uv_size;
    char *nv12_uv_base = nv12_content + y_size;
    int i = 0, j = 0;
    for (;i < uv_size;i++) {
        nv12_uv_base[j] = i420_u_base[i];
        nv12_uv_base[j + 1] = i420_v_base[i];
        j += 2;
    }
    free(i420_content);
    fp = fopen(nv12_file_path, "wb");
    if (!fp) {
        free(nv12_content);
        return false;
    }
    if (file_size != fwrite(nv12_content, 1, file_size, fp)) {
        free(nv12_content);
        fclose(fp);
        return false;
    }
    free(nv12_content);
    fclose(fp);
    return true;
}

 

二 I420转NV21

1.NV21格式

NV21内存布局基本与NV12类似,只是将NV12中U和V次序互换,如下所示:

YUV图像格式转换方法实践_第3张图片

2.转换代码

bool convert_i420_nv21(const char *i420_file_path, size_t width, size_t height, const char *nv21_file_path) {
    if (!i420_file_path || !nv21_file_path) {
        return false;
    }
    FILE *fp = fopen(i420_file_path, "rb");
    if (!fp) {
        return false;
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    size_t y_size = width * height;
    size_t uv_size = y_size / 4;
    if (file_size != (y_size + uv_size * 2)) {
        fclose(fp);
        return false;
    }
    char *i420_content = (char *)malloc(sizeof(char) * file_size);
    if (!i420_content) {
        fclose(fp);
        return false;
    }
    if (file_size != fread(i420_content, 1, file_size, fp)) {
        free(i420_content);
        fclose(fp);
        return false;
    }
    fclose(fp);
    // convert i420 to nv21
    char *nv21_content = (char *)malloc(sizeof(char) * file_size);
    if (!nv21_content) {
        free(i420_content);
        return false;
    }
    // copy y channel
    memcpy(nv21_content, i420_content, y_size);
    // copy uv channel
    char *i420_u_base = i420_content + y_size;
    char *i420_v_base = i420_u_base + uv_size;
    char *nv21_uv_base = nv21_content + y_size;
    int i = 0, j = 0;
    for (;i < uv_size;i++) {
        nv21_uv_base[j] = i420_v_base[i];
        nv21_uv_base[j + 1] = i420_u_base[i];
        j += 2;
    }
    free(i420_content);
    fp = fopen(nv21_file_path, "wb");
    if (!fp) {
        free(nv21_content);
        return false;
    }
    if (file_size != fwrite(nv21_content, 1, file_size, fp)) {
        free(nv21_content);
        fclose(fp);
        return false;
    }
    free(nv21_content);
    fclose(fp);
    return true;
}

 

三 UYVY转NV12

1.UYVY格式

(1)UYVY是每两个Y共用一组UV,如果一帧UYVY图像宽带是width,高度是height,1个像素占1个字节,那么共有width✖height个Y,U和V都是width✖height / 2个,因此一帧I420图像占用字节数是width✖height✖2。

(2)UYVY是每4个像素中,2个Y共用一组UV,一行像素数是width * 2,如下所示:

YUV图像格式转换方法实践_第4张图片

2.转换方法

(1)将一帧UYVY图像中所有Y存入NV12图像的缓存区,隔行存入相应的U和V,如下所示:

(2)转换代码:

bool convert_uyvy_nv12(const char *uyvy_file_path, size_t width, size_t height, const char *nv12_file_path) {
    if (!uyvy_file_path || !nv12_file_path) {
        return false;
    }
    FILE *fp = fopen(uyvy_file_path, "rb");
    if (!fp) {
        return false;
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    size_t frame_size = width * height * 2;
    if (file_size != frame_size) {
        fclose(fp);
        return false;
    }
    char *uyvy_content = (char *)malloc(sizeof(char) * file_size);
    if (!uyvy_content) {
        fclose(fp);
        return false;
    }
    if (file_size != fread(uyvy_content, 1, file_size, fp)) {
        free(uyvy_content);
        fclose(fp);
        return false;
    }
    fclose(fp);
    // convert uyvy to nv12
    frame_size = width * height * 3 / 2;
    char *nv12_content = (char *)malloc(sizeof(char) * frame_size);
    if (!nv12_content) {
        free(uyvy_content);
        return false;
    }
    size_t y_size = width * height;
    size_t pixels_in_a_row = width * 2;
    char *nv12_y_ptr = nv12_content;
    char *nv12_uv_ptr = nv12_content + y_size;
    int lines = 0;
    for (int i = 0;i < file_size;i += 4) {
        // copy y channel
        *nv12_y_ptr++ = uyvy_content[i + 1];
        *nv12_y_ptr++ = uyvy_content[i + 3];
        if (0 == i % pixels_in_a_row) {
            ++lines;
        }
        if (lines % 2) {       // extract the UV value of odd rows
            // copy uv channel
            *nv12_uv_ptr++ = uyvy_content[i];
            *nv12_uv_ptr++ = uyvy_content[i + 2];
        }
    }
    free(uyvy_content);
    fp = fopen(nv12_file_path, "wb");
    if (!fp) {
        free(nv12_content);
        return false;
    }
    if (frame_size != fwrite(nv12_content, 1, frame_size, fp)) {
        free(nv12_content);
        fclose(fp);
        return false;
    }
    free(nv12_content);
    fclose(fp);
    return true;
}

 

四 UYVY转NV21

方法与转NV12类似,代码如下:

bool convert_uyvy_nv21(const char *uyvy_file_path, size_t width, size_t height, const char *nv21_file_path) {
    if (!uyvy_file_path || !nv21_file_path) {
        return false;
    }
    FILE *fp = fopen(uyvy_file_path, "rb");
    if (!fp) {
        return false;
    }
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    size_t frame_size = width * height * 2;
    if (file_size != frame_size) {
        fclose(fp);
        return false;
    }
    char *uyvy_content = (char *)malloc(sizeof(char) * file_size);
    if (!uyvy_content) {
        fclose(fp);
        return false;
    }
    if (file_size != fread(uyvy_content, 1, file_size, fp)) {
        free(uyvy_content);
        fclose(fp);
        return false;
    }
    fclose(fp);
    // convert uyvy to nv12
    frame_size = width * height * 3 / 2;
    char *nv21_content = (char *)malloc(sizeof(char) * frame_size);
    if (!nv21_content) {
        free(uyvy_content);
        return false;
    }
    size_t y_size = width * height;
    size_t pixels_in_a_row = width * 2;
    char *nv21_y_ptr = nv21_content;
    char *nv21_uv_ptr = nv21_content + y_size;
    int lines = 0;
    for (int i = 0;i < file_size;i += 4) {
        // copy y channel
        *nv21_y_ptr++ = uyvy_content[i + 1];
        *nv21_y_ptr++ = uyvy_content[i + 3];
        if (0 == i % pixels_in_a_row) {
            ++lines;
        }
        if (lines % 2) {       // extract the UV value of odd rows
            // copy uv channel
            *nv21_uv_ptr++ = uyvy_content[i + 2];
            *nv21_uv_ptr++ = uyvy_content[i];
        }
    }
    free(uyvy_content);
    fp = fopen(nv21_file_path, "wb");
    if (!fp) {
        free(nv21_content);
        return false;
    }
    if (frame_size != fwrite(nv21_content, 1, frame_size, fp)) {
        free(nv21_content);
        fclose(fp);
        return false;
    }
    free(nv21_content);
    fclose(fp);
    return true;
}

 

五 编译及测试

1.编译:使用g++编译

2.github:GitHub - wangzhicheng2013/common_utility

 

你可能感兴趣的:(Linux,C++算法系列,人工智能,c++,图像处理)