YUV420文件的直观解释

一、如下是一个典型YUV420文件:(320x180)



分解开来看Y/U/V


YUV420文件的直观解释_第1张图片

 


从分解来看能非常直观地看出来,U和V分别是Y的1/4;


如本实例文件是320*180分辨率的,所以Y=320*180 Byte, U=320*180/4 Byte, V=320*180/4 Byte,

整个文件的大小就是Y+U+V=(320*180) + (320*180/4) + (320*180/4) = (320*180) *3/2 Byte


YUV420文件的直观解释_第2张图片


二、简单读写YUV的实例

#include 
#include 
#include 

int main(){
	//读取文件test_yuv420p_320x180.yuv
	FILE* fp_yuv=fopen("test_yuv420p_320x180.yuv","rb");
	//写入文件frame.yuv
	FILE* fp_frame=fopen("frame_320x180_out.yuv","wb");
	//开辟内存读取test_yuv420p_320x180.yuv文件的第一帧的亮度数据(Y)
	char* buffer_y=(char*)malloc(sizeof(char)*320*180);
	char* buffer_u=(char*)malloc(sizeof(char)*320*180/4);
	char* buffer_v=(char*)malloc(sizeof(char)*320*180/4);

	//读取函数,将test_yuv420p_320x180.yuv的第一帧存入buff指向的内存
	for(int i=0;i<30;i++)
	{
		fread(buffer_y,320*180,1,fp_yuv);
		fread(buffer_u,320*180/4,1,fp_yuv);
		fread(buffer_v,320*180/4,1,fp_yuv);

		//for(int i=0;i<320*180/4;i++){
		//	buffer_u[i]=128;
		//	buffer_v[i]=128;
		//}

		memset(buffer_u,128,320*180/4);
		memset(buffer_v,128,320*180/4);

		fwrite(buffer_y,320*180,1,fp_frame);
		fwrite(buffer_u,320*180/4,1,fp_frame);
		fwrite(buffer_v,320*180/4,1,fp_frame);
	}
	//fread(buff,320*180,1,fp_yuv);
	//将buff指向的内存写入frame.yuv
	

	//fprintf演示
	//FILE* fp_demo=fopen("demo.txt","wb");
	//char hello[20]="GuangDianGong";
	//fprintf(fp_demo,"Hello World,%s",hello);
	//fclose(fp_demo);

	free(buffer_y);
	free(buffer_u);
	free(buffer_v);
	fclose(fp_yuv);
	fclose(fp_frame);
	

	return 0;
}


读写工程下载: http://download.csdn.net/detail/mandagod/9879497

你可能感兴趣的:(YUV)