[Video and Audio Data Processing] YUV 420數據分離

0. 背景

本文基於雷神的博客,基於Visual Studio 2019,實現YUV數據分離…話說沒想到又見到了大學系主任,講數字圖像處理時候,用到的Lena圖…

在這裏插入圖片描述

1. 下載源代碼,編譯YUV播放器

從雷神的開源github下載YUVplayer源碼,https://github.com/leixiaohua1020/YUVplayer
在這裏插入圖片描述
啓動Visual Studio,打開剛纔下載解壓後的yuvplayer.sln文件.
在這裏插入圖片描述
在這裏插入圖片描述

升級一下工具集,
在這裏插入圖片描述

接下來編譯:

在這裏插入圖片描述

接下來在release目錄下面會找到應用程序,把它拿到桌面,一會使用。
在這裏插入圖片描述

2. 新建一個新項目

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

把Lena圖片,放到剛纔的工程目錄下面,備註,Lena圖,從以下博客獲取:

https://github.com/leixiaohua1020/simplest_mediadata_test

當然你也可以從以下地方獲取:

https://www.ece.rice.edu/~wakin/images/ (備註:這個是佛羅里達的原圖,並不試用此測試)

在這裏插入圖片描述

然後寫代碼,參考:

https://blog.csdn.net/leixiaohua1020/article/details/50534150


extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}


/**
 * Split Y, U, V planes in YUV420P file.
 * @param url  Location of Input YUV file.
 * @param w    Width of Input YUV file.
 * @param h    Height of Input YUV file.
 * @param num  Number of frames to process.
 *
 */
int simplest_yuv420_split(char* url, int w, int h, int num) {
    FILE* fp = fopen(url, "rb+");
    FILE* fp1 = fopen("output_420_y.y", "wb+");
    FILE* fp2 = fopen("output_420_u.y", "wb+");
    FILE* fp3 = fopen("output_420_v.y", "wb+");

    unsigned char* pic = (unsigned char*)malloc(w * h * 3 / 2);

    for (int i = 0; i < num; i++) {

        fread(pic, 1, w * h * 3 / 2, fp);
        //Y
        fwrite(pic, 1, w * h, fp1);
        //U
        fwrite(pic + w * h, 1, w * h / 4, fp2);
        //V
        fwrite(pic + w * h * 5 / 4, 1, w * h / 4, fp3);
    }

    free(pic);
    fclose(fp);
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);

    return 0;
}


int main()
{
    simplest_yuv420_split("lena_256x256_yuv420p.yuv", 256, 256, 1);
    return 0;
}

在這裏插入圖片描述
改成x64,然後編譯;

>C:\Users\Administrator\source\repos\YUV_Split\split.cpp(28,16): 
>error C4996: 'fopen': This function or variable may be unsafe. 
>Consider using fopen_s instead. To disable deprecation, 
>use _CRT_SECURE_NO_WARNINGS. See online help for details

這個報錯解決方法如下:
項目 ->屬性,編輯預處理器: 添加 _CRT_SECURE_NO_WARNINGS

在這裏插入圖片描述
在這裏插入圖片描述

1>C:\Users\Administrator\source\repos\YUV_Split\split.cpp(58,66): 
error C2664:int simplest_yuv420_split(char *,int,int,int): 
無法將參數 1 從“const char [25]”轉換爲“char *
int simplest_yuv420_split(const char* url, int w, int h, int num) 

參數修改加上const即可解決,最終代碼如下:

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
}


/**
 * Split Y, U, V planes in YUV420P file.
 * @param url  Location of Input YUV file.
 * @param w    Width of Input YUV file.
 * @param h    Height of Input YUV file.
 * @param num  Number of frames to process.
 *
 */
int simplest_yuv420_split(const char* url, int w, int h, int num) {
    FILE* fp = fopen(url, "rb+");
    FILE* fp1 = fopen("output_420_y.y", "wb+");
    FILE* fp2 = fopen("output_420_u.y", "wb+");
    FILE* fp3 = fopen("output_420_v.y", "wb+");

    unsigned char* pic = (unsigned char*)malloc(w * h * 3 / 2);

    for (int i = 0; i < num; i++) {

        fread(pic, 1, w * h * 3 / 2, fp);
        //Y
        fwrite(pic, 1, w * h, fp1);
        //U
        fwrite(pic + w * h, 1, w * h / 4, fp2);
        //V
        fwrite(pic + w * h * 5 / 4, 1, w * h / 4, fp3);
    }

//這裏寫的方法可參考我之前的博客,420就是這樣存的,
//https://blog.csdn.net/Codeliang666/article/details/106288043
    free(pic);
    fclose(fp);
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);

    return 0;
}


int main()
{
    simplest_yuv420_split("lena_256x256_yuv420p.yuv", 256, 256, 1);
    return 0;
}

3. 效果

在這裏插入圖片描述

在這裏插入圖片描述
以Y分量來看這些圖片:

在這裏插入圖片描述

然後你會發現u分量是下面這樣的,一臉懵逼:

在這裏插入圖片描述

原因是分辨率不是256256了,需要手動修改成128128

在這裏插入圖片描述

以下是v 分量的.
在這裏插入圖片描述

OVER! 本文到此結束,感謝閱讀!

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