音視頻入門-09-RGB&YUV互轉-使用開源庫

音視頻入門文章目錄

介紹開源庫

使用第三方開源庫來簡化開發,屏蔽一些底層的複雜度,節省大量編寫代碼的時間。

libyuv: Google 開源的實現各種 YUV 與 RGB 之間相互轉換、旋轉、縮放的庫。

yuv2rgb:C library for fast image conversion between yuv420p and rgb24.

使用開源庫

libyuv

FFmpeg 生成代碼所需文件:

ffmpeg -i rainbow.bmp -video_size 700x700 -pix_fmt yuv444p rainbow-yuv444p.yuv
ffmpeg -i rainbow.bmp -video_size 700x700 -pix_fmt yuv420p rainbow-yuv420p.yuv
ffmpeg -i rainbow.bmp -video_size 700x700 -pix_fmt rgb24 rainbow-rgb24.rgb

YUV444P to RGB32

將 FFmpeg 生成的 YUV444P 格式轉換成 RGB32 格式。
[rainbow-yuv444p.yuv] -> [rainbow-yuv444p-to-rgb32-libyuv.rgb]
#include <stdio.h>
#include "libyuv.h"

void libyuv_I444_to_Rgb(char *srcFileName, char *dstFileName, int width, int height) {
    FILE *src_file = fopen(srcFileName, "rb");
    FILE *dst_file = fopen(dstFileName, "wb");

    int size_src = width * height * 3;
    int size_dest = width * height * 4;
    char *buffer_src = (char *)malloc(size_src);
    char *buffer_dest = (char *)malloc(size_dest);

    fread(buffer_src, 1, size_src, src_file);
    
    I444ToARGB(
        (const u_int8_t*)buffer_src, width,
        (const u_int8_t*)(buffer_src + width * height), width,
        (const u_int8_t*)(buffer_src + 2 * width * height), width,
        (u_int8_t*)buffer_dest, width * 4,
        width, height);
    
    fwrite(buffer_dest, 1, size_dest, dst_file);

    free(buffer_src);
    free(buffer_dest);
    fclose(dst_file);
    fclose(src_file);
}

int main() {
    int width = 700, height = 700;
    libyuv_I444_to_Rgb("/Users/staff/Desktop/rainbow-yuv444p.yuv", "/Users/staff/Desktop/rainbow-yuv444p-to-rgb32-libyuv.yuv", width, height);
    return 0;
}

FFplay 顯示生成的 rainbow-yuv444p-to-rgb32-libyuv.rgb

ffplay -f rawvideo -pixel_format rgb32 -video_size 700x700 rainbow-yuv444p-to-rgb32-libyuv.rgb

yuv444p-to-rgb32-libyuv.jpg

RGB24 to YUV420p

將 FFmpeg 生成的 RGB24 格式轉換成 YUV420p 格式。
[rainbow-rgb24.rgb] -> [rainbow-rgb24-to-yuv420p-libyuv.yuv]
#include <stdio.h>
#include "libyuv.h"

void libyuv_Rgb24_to_Yuv420p(char *rgbFileName, char *yuvFileName, int width, int height) {
    FILE *rgb_file = fopen(rgbFileName, "rb");
    FILE *yuv_file = fopen(yuvFileName, "wb");

    int size_rgb = width * height * 3;
    int size_yuv = width * height * 3 / 2;
    uint8_t *buffer_rgb = (uint8_t *)malloc(size_rgb);
    uint8_t *buffer_yuv = (uint8_t *)malloc(size_yuv);

    fread(buffer_rgb, 1, size_rgb, rgb_file);

    // RGB to BGR
//    uint8_t temp;
//    for(int i = 0; i < size_rgb; i+=3) {
//        temp = buffer_rgb[i + 0];
//        buffer_rgb[i + 0] = buffer_rgb[i + 2];
//        buffer_rgb[i + 2] = temp;
//    }

//    RGB24ToI420(
    RAWToI420(
            buffer_rgb, width*3,
            buffer_yuv, width,
            buffer_yuv + width*height, (width+1)/2,
            buffer_yuv + width*height + ((width+1)/2)*((height+1)/2), (width+1)/2,
            width, height);

    fwrite(buffer_yuv, 1, size_yuv, yuv_file);

    free(buffer_rgb);
    free(buffer_yuv);
    fclose(yuv_file);
    fclose(rgb_file);
}

int main() {
    int width = 700, height = 700;
    libyuv_Rgb24_to_Yuv420p("/Users/staff/Desktop/rainbow-rgb24.rgb", "/Users/staff/Desktop/rainbow-rgb24-to-yuv420p-libyuv.yuv", width, height);
    return 0;
}

FFplay 顯示生成的 rainbow-rgb24-to-yuv420p-libyuv.yuv

ffplay -f rawvideo -pixel_format yuv420p -video_size 700x700 rainbow-rgb24-to-yuv420p-libyuv.yuv

rgb24-to-yuv420p-libyuv.png

descampsa/yuv2rgb

YUV420p to RGB24

將 FFmpeg 生成的 YUV420P 格式轉換成 RGB24 格式。
[rainbow-yuv420p.yuv] -> [rainbow-yuv420p-to-rgb24-descampsa.rgb]
#include <stdio.h>
#include "descampsa/yuv2rgb/yuv_rgb.h"

void yuv420pToRgb24(uint8_t *YUV, uint8_t *RGB, uint32_t width, uint32_t height) {
    const YCbCrType yuv_format = YCBCR_601;
    // const YCbCrType yuv_format = YCBCR_709;
    // const YCbCrType yuv_format = YCBCR_JPEG;

    yuv420_rgb24_std(
        width,
        height,
        YUV,
        YUV+width*height,
        YUV+width*height+((width+1)/2)*((height+1)/2),
        width,
        (width+1)/2,
        RGB,
        width*3,
        yuv_format);

}

int main() {
    uint32_t width = 700, height = 700;
    uint8_t RGB[width*height*3];
    uint8_t YUV[width*height*3/2];

    FILE *yuv420pFile = fopen("/Users/staff/Desktop/rainbow-yuv420p.yuv", "rb");
    fread(YUV, sizeof(YUV), 1, yuv420pFile);

    yuv420pToRgb24(YUV, RGB, width, height);

    FILE *rgb24File = fopen("/Users/staff/Desktop/rainbow-yuv420p-to-rgb24-descampsa.rgb", "wb");
    fwrite(RGB, sizeof(RGB), 1, rgb24File);

    fclose(rgb24File);
    fclose(yuv420pFile);
    return 0;
}

FFplay 顯示生成的 rainbow-yuv420p-to-rgb24-descampsa.rgb

ffplay -f rawvideo -pixel_format rgb24 -video_size 700x700 rainbow-yuv420p-to-rgb24-descampsa.rgb

yuv420p-to-rgb24-descampsa.jpg

RGB24 to YUV420p

將 FFmpeg 生成的 RGB24 格式轉換成 YUV420p 格式。
[rainbow-rgb24.rgb] -> [rainbow-rgb24-to-yuv420p-descampsa.yuv]
#include <stdio.h>
#include "descampsa/yuv2rgb/yuv_rgb.h"

void rgb24ToYuv420p(uint8_t *RGB, uint8_t *YUV, uint32_t width, uint32_t height) {
    const YCbCrType yuv_format = YCBCR_601;
//    const YCbCrType yuv_format = YCBCR_709;
    //const YCbCrType yuv_format = YCBCR_JPEG;

    rgb24_yuv420_std(
        width,
        height,
        RGB,
        width*3,
        YUV,
        YUV+width*height,
        YUV+width*height+((width+1)/2)*((height+1)/2),
        width,
        width/2,
        yuv_format);
}

int main() {
    uint32_t width = 700, height = 700;
    uint8_t RGB[width*height*3];
    uint8_t YUV[width*height*3/2];

    FILE *rgb24File = fopen("/Users/staff/Desktop/rainbow-rgb24.rgb", "rb");
    fread(RGB, sizeof(RGB), 1, rgb24File);

    rgb24ToYuv420p(RGB, YUV, width, height);

    FILE *yuvFile = fopen("/Users/staff/Desktop/rainbow-rgb24-to-yuv420p-descampsa.yuv", "wb");
    fwrite(YUV, sizeof(YUV), 1, yuvFile);

    fclose(rgb24File);
    fclose(yuvFile);
    return 0;
}

FFplay 顯示生成的 rainbow-rgb24-to-yuv420p-descampsa.yuv

ffplay -f rawvideo -pixel_format yuv420p -video_size 700x700 rainbow-rgb24-to-yuv420p-descampsa.yuv

rgb24-to-yuv420p-descampsa.jpg


代碼:
09-rgb-to-yuv-library

參考資料:

libyuv/libyuv

descampsa/yuv2rgb

latelee/yuv2rgb

dmilos/color

ibireme/yy_color_convertor

andrechen/yuv2rgb

內容有誤?聯繫作者:

聯繫作者


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章