NV12转YU12
YU12格式也叫I420格式,是YUV420p其中的一種,NV12是YUV420sp的一種。YU12和NV21中YUV數據的排列方式為:
YU12:YYYYYYYY UU VV
NV12:YYYYYYYY UV UV
針對數據的排列結構,本文將NV12轉為YU12。
主要轉換接口實現為:
int NV12toYU12(char *data, char *out, int width, int height);
功能描述 : Nv12格式轉YU12
輸入參數 :
data : NV12圖片數據指針
out :YU12圖片地址
width: 圖像寬度
height : 圖像高度
輸出參數 : out,YU12圖片地址
返 回 值 : int類型
具體代碼如下:
/* *********************************************************************** * 文件名稱:Nv12toYU12.c * 文件描述:Nv12格式轉YU12 * 作 者:Young Fan *日 期:2021-09-16 ************************************************************************ */ #include <stdio.h> #include <stdlib.h> #include <string.h>/*****************************************************************************函 數 名: NV12toYU12功能描述 : Nv12格式轉YU12輸入參數 : data NV12圖片數據指針out 輸出的YU12圖片地址width 圖像寬度height 圖像高度輸出參數 : 光柵格式yuv數據返 回 值 : int *****************************************************************************/ int NV12toYU12(char *data, char *out, int width, int height) {if (data == NULL || out == NULL){printf("error:empty pointer \n");return -1;}if (width <= 0 || height <= 0){printf("error:width or height is not positive \n");return -1;}//分別獲取NV12數據中的Y、U、V分量。NV12:YYYYYYYY UVUV => YU12: YYYYYYYY UU VV//Ymemcpy(out, data, width * height);printf("Y分量轉換完成!\n");//Uchar * ptr1 = out + width * height; //YU12中U的首地址char * ptr2 = data + width * height;int n = 0;while (n < width * height / 4){//賦值*(ptr1) = *(ptr2);ptr1++;ptr2 = ptr2 + 2;n++;}printf("U分量轉換完成!\n");//Vptr1 = out + width * height * 5 / 4; //YU12中V的首地址ptr2 = data + width * height + 1; n = 0;while (n < width * height / 4){//賦值*(ptr1) = *(ptr2);ptr1++;ptr2 = ptr2 + 2;n++;}printf("V分量轉換完成!\n");return 0; }int main(void) { int width = 512;int height = 288;int size = width * height * 3 / 2; char *data = (char *)malloc(size);char *out = (char *)malloc(size);memset(data, 0, size);memset(out, 0, size);FILE *fp = fopen("./pic/NV12.yuv","rb");fread(data, 1, size, fp);int ret = NV12toYU12(data, out, width, height);if (ret < 0){printf("error: NV12toYU12 failed, ret %d\n", ret);return -1;}//保存已轉換好的YU12數據到本地FILE *out_fp = fopen("./out_YU12.yuv", "wb");fwrite(out, 1, size, out_fp);printf("整體轉換完成并將YU12圖像保存在本地!\n");//釋放資源fclose(fp);fclose(out_fp);if(data != NULL){free(data);data = NULL;}if(out != NULL){free(out);out = NULL;}return 0; }打印輸出如下:
輸出圖片:
原圖:
總結
- 上一篇: url长度限制
- 下一篇: JAVA实现文本翻译功能_java实现简