【Codecs系列】颜色空间转换CSConvert:YUV420P转MonoY

DATE: 2019-10-19


将YUV420P转换成亮度分量Y,比较简单,实现代码如下:

#include 
#include 
#include 

int main(int argc, char** argv)
{
	int width, height;
	int frame_size, frame_size_y;
	FILE *fin, *fou;
	char *y, *u, *v,*u1,*v1;
	int i, frame_num = 0;
	char outname[512] = { 0, };
	printf("\nUsage:YUV420PtoMonoY.exe input.yuv output.yuv width height\n\n");
	if (argc != 5)
	{
		return -1;
	}
	fin = fopen(argv[1], "rb");
	if (fin == NULL)
	{
		printf("error:open %s fail\n", argv[1]);
		return -1;
	}

	sprintf_s(outname,"%s", argv[2]);

	width = atoi(argv[3]);
	height = atoi(argv[4]);

	frame_size = width*height * 3 / 2;
	frame_size_y = width * height;

	y = (char *)malloc(frame_size_y);
	if (y == NULL)
	{
		printf("malloc y fail\n");
		return -1;
	}

	fou = fopen(outname, "wb");
	if (fou == NULL)
	{
		printf("error: open %s fail\n", outname);
		return -1;
	}

	while (width*height == fread(y, 1, width*height, fin))
	{
		fwrite(y, 1, frame_size_y, fou);
		fseek(fin, frame_size_y/2, SEEK_CUR);

		printf("%dth frame ok!!\n", frame_num);
		frame_num++;
	}
	printf("YUV420P to MonoY successfully!!, stotal frames: %d\n",frame_num);
	
	free(y);
	y = NULL;

	fclose(fin);
	fclose(fou);

	system("pause");

	return 0;
}

THE END!

你可能感兴趣的:(Codecs,颜色空间转换,YUV420P,Y,【视音频技术之基础知识】)