使用libyuv与ffmpeg实现yuv_rgb格式转换性能分析

两组对比:同样分辨率的文件,通过所消耗的时长判断
1:YUV to RGB
2:YUV 尺寸缩放

结果:(文件有84帧图像) 则每帧耗时如下
libyuv_I420_to_Rgb 623066
ffmpeg_I420_to_Rgb 1815963

libyuv_swscale 996027
ffmpeg_swscale 1387750

可以看出libyuv要更好一点,yuv_to_rgb转换时大概为3倍, 缩放大概为一倍多

#include 
#include 

extern "C"
{
#include "libswscale/swscale.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
};

#include "libyuv.h"

static inline uint64_t get_clockfreq(void)
{
    static LARGE_INTEGER clock_freq;
    QueryPerformanceFrequency(&clock_freq);
    return clock_freq.QuadPart;
}

uint64_t os_gettime_ns(void)
{
    LARGE_INTEGER current_time;
    double time_val;
    QueryPerformanceCounter(¤t_time);
    time_val = (double)current_time.QuadPart;
    time_val *= 1000000000.0;
    time_val /= (double)get_clockfreq();
    return (uint64_t)time_val;
}


int libyuv_I420_to_Rgb()
{   
    const int width = 1280, height = 720;
    FILE *src_file = fopen("1280x720.yuv", "rb");
    FILE *dst_file = fopen("1280x720.rgb", "wb");

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

    uint64_t start_time = os_gettime_ns();
    while (1){
        if (fread(buffer_src, 1, size_src, src_file) != size_src){
            //fseek(src_file, 0, 0);
            //fread(buffer_src, 1, size_src, src_file);
            break;
        }
        //libyuv::I420ToARGB(const uint8* src_y, int src_stride_y,
        //  const uint8* src_u, int src_stride_u,
        //  const uint8* src_v, int src_stride_v,
        //  uint8* dst_argb, int dst_stride_argb,
        //  int width, int height);
        libyuv::I420ToARGB((const uint8*)buffer_src, width,
            (const uint8*)(buffer_src + width * height), width / 2,
            (const uint8*)(buffer_src + width * height * 5 / 4), width / 2,
            (uint8*)buffer_dest, width * 4,
            width, height);
        //fwrite(buffer_dest, 1, size_dest, dst_file);
    }
    uint64_t stop_time = os_gettime_ns();
    printf("------ %ld \n", stop_time - start_time);

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

int ffmpeg_I420_to_Rgb()
{
    const int width = 1280, height = 720;
    FILE *src_file = fopen("1280x720.yuv", "rb");
    FILE *dst_file = fopen("1280x720.rgb", "wb");

    uint8_t *src_data[4];
    int src_linesize[4];

    uint8_t *dst_data[4];
    int dst_linesize[4];

    struct SwsContext *img_convert_ctx;

    int size_src = width * height * 3 / 2;
    char *buffer_src = (char *)malloc(size_src);

    if (av_image_alloc(src_data, src_linesize, width, height, AV_PIX_FMT_YUV420P, 1) < 0){
        return -1;
    }
    if (av_image_alloc(dst_data, dst_linesize, width, height, AV_PIX_FMT_BGRA, 1) < 0) {
        return -1;
    }

    img_convert_ctx = sws_alloc_context();
    av_opt_show2(img_convert_ctx, stdout, AV_OPT_FLAG_VIDEO_PARAM, NULL);
    av_opt_set_int(img_convert_ctx, "sws_flags", SWS_FAST_BILINEAR | SWS_PRINT_INFO, NULL);
    av_opt_set_int(img_convert_ctx, "srcw", width, NULL);
    av_opt_set_int(img_convert_ctx, "srch", height, NULL);
    av_opt_set_int(img_convert_ctx, "src_format", AV_PIX_FMT_YUV420P, NULL);
    av_opt_set_int(img_convert_ctx, "src_range", 1, NULL);
    av_opt_set_int(img_convert_ctx, "dstw", width, NULL);
    av_opt_set_int(img_convert_ctx, "dsth", height, NULL);
    av_opt_set_int(img_convert_ctx, "dst_format", AV_PIX_FMT_BGRA, NULL);
    av_opt_set_int(img_convert_ctx, "dst_range", 1, NULL);
    sws_init_context(img_convert_ctx, NULL, NULL);

    uint64_t start_time = os_gettime_ns();
    while (1){
        if (fread(buffer_src, 1, size_src, src_file) != size_src){
            //fseek(src_file, 0, 0);
            //fread(buffer_src, 1, size_src, src_file);
            break;
        }
        memcpy(src_data[0], buffer_src, width * height);                    //Y
        memcpy(src_data[1], buffer_src + width * height, width * height / 4);      //U
        memcpy(src_data[2], buffer_src + width * height * 5 / 4, width * height / 4);  //V
        sws_scale(img_convert_ctx, src_data, src_linesize, 0, height, dst_data, dst_linesize);
        //fwrite(dst_data[0], 1, width * height * 4, dst_file);
    }
    uint64_t stop_time = os_gettime_ns();
    printf("------ %ld\n", stop_time - start_time);

    sws_freeContext(img_convert_ctx);

    free(buffer_src);
    fclose(src_file);
    fclose(dst_file);
    av_freep(&src_data[0]);
    av_freep(&dst_data[0]);
    return 0;
}

int libyuv_swscale()
{
    const int width_src = 1280, height_src = 720;
    const int width_dest = 640, height_dest = 320;
    FILE *src_file = fopen("1280x720.yuv", "rb");
    FILE *dst_file = fopen("640x320.yuv", "wb");

    int size_src = width_src * height_src * 3 / 2;
    int size_dest = width_dest * height_dest * 3 / 2;
    char *buffer_src = (char *)malloc(size_src);
    char *buffer_dest = (char *)malloc(size_dest);

    uint64_t start_time = os_gettime_ns();
    while (1){
        if (fread(buffer_src, 1, size_src, src_file) != size_src){
            //fseek(src_file, 0, 0);
            //fread(buffer_src, 1, size_src, src_file);
            break;
        }
        //int I420Scale(const uint8* src_y, int src_stride_y,
        //  const uint8* src_u, int src_stride_u,
        //  const uint8* src_v, int src_stride_v,
        //  int src_width, int src_height,
        //  uint8* dst_y, int dst_stride_y,
        //  uint8* dst_u, int dst_stride_u,
        //  uint8* dst_v, int dst_stride_v,
        //  int dst_width, int dst_height,
        //enum FilterMode filtering);
        libyuv::I420Scale((const uint8*)buffer_src, width_src,
            (const uint8*)(buffer_src + width_src * height_src), width_src / 2,
            (const uint8*)(buffer_src + width_src * height_src * 5 / 4), width_src / 2,
            width_src, height_src,
            (uint8*)buffer_dest, width_dest,
            (uint8*)(buffer_dest + width_dest * height_dest), width_dest / 2,
            (uint8*)(buffer_dest + width_dest * height_dest * 5 / 4), width_dest / 2,
            width_dest, height_dest,
            libyuv::kFilterNone);

        //fwrite(buffer_dest, 1, size_dest, dst_file);
    }
    uint64_t stop_time = os_gettime_ns();
    printf("------ %ld\n", stop_time - start_time);

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


int ffmpeg_swscale()
{
    const int width_src = 1280, height_src = 720;
    const int width_dest = 640, height_dest = 320;
    FILE *src_file = fopen("1280x720.yuv", "rb");
    FILE *dst_file = fopen("640x320.yuv", "wb");

    uint8_t *src_data[4];
    int src_linesize[4];

    uint8_t *dst_data[4];
    int dst_linesize[4];

    struct SwsContext *img_convert_ctx;

    int size_src = width_src * height_src * 3 / 2;
    char *buffer_src = (char *)malloc(size_src);

    if (av_image_alloc(src_data, src_linesize, width_src, height_src, AV_PIX_FMT_YUV420P, 1) < 0){
        return -1;
    }
    if (av_image_alloc(dst_data, dst_linesize, width_dest, height_dest, AV_PIX_FMT_YUV420P, 1) < 0) {
        return -1;
    }

    img_convert_ctx = sws_alloc_context();
    av_opt_show2(img_convert_ctx, stdout, AV_OPT_FLAG_VIDEO_PARAM, NULL);
    av_opt_set_int(img_convert_ctx, "sws_flags", SWS_FAST_BILINEAR | SWS_PRINT_INFO, NULL);
    av_opt_set_int(img_convert_ctx, "srcw", width_src, NULL);
    av_opt_set_int(img_convert_ctx, "srch", height_src, NULL);
    av_opt_set_int(img_convert_ctx, "src_format", AV_PIX_FMT_YUV420P, NULL);
    av_opt_set_int(img_convert_ctx, "src_range", 1, NULL);
    av_opt_set_int(img_convert_ctx, "dstw", width_dest, NULL);
    av_opt_set_int(img_convert_ctx, "dsth", height_dest, NULL);
    av_opt_set_int(img_convert_ctx, "dst_format", AV_PIX_FMT_YUV420P, NULL);
    av_opt_set_int(img_convert_ctx, "dst_range", 1, NULL);
    sws_init_context(img_convert_ctx, NULL, NULL);

    uint64_t start_time = os_gettime_ns();
    while (1){
        if (fread(buffer_src, 1, size_src, src_file) != size_src){
            //fseek(src_file, 0, 0);
            //fread(buffer_src, 1, size_src, src_file);
            break;
        }
        memcpy(src_data[0], buffer_src, width_src * height_src);                    //Y
        memcpy(src_data[1], buffer_src + width_src * height_src, width_src * height_src / 4);      //U
        memcpy(src_data[2], buffer_src + width_src * height_src * 5 / 4, width_src * height_src / 4);  //V

        sws_scale(img_convert_ctx, src_data, src_linesize, 0, height_src, dst_data, dst_linesize);

        //fwrite(dst_data[0], 1, width_dest * height_dest, dst_file);
        //fwrite(dst_data[1], 1, width_dest * height_dest / 4, dst_file);
        //fwrite(dst_data[2], 1, width_dest * height_dest / 4, dst_file);
    }
    uint64_t stop_time = os_gettime_ns();
    printf("------ %ld\n", stop_time - start_time);

    sws_freeContext(img_convert_ctx);

    free(buffer_src);
    fclose(src_file);
    fclose(dst_file);
    av_freep(&src_data[0]);
    av_freep(&dst_data[0]);
    return 0;
}

int main(int argc, char* argv[])
{
    libyuv_I420_to_Rgb();
    //ffmpeg_I420_to_Rgb();
    //libyuv_swscale();
    //ffmpeg_swscale();
    getchar();
    return 0;
}

你可能感兴趣的:(ffmpeg)