YUV422转YUV420P

记录下,就当自己的工具了, 本来想使用ffmpeg转呢,结果命令行参数太复杂,没搞定,索性直接写吧

#include 
#include 
/*
#include 
#include 
*/

int yuv422_to_yuv420p(unsigned char *pYUV422, unsigned char *pYUV420, int width, int height)
{        
	int i,j;
	unsigned char *pY = pYUV420;
	unsigned char *pU = pYUV420 + width * height;
	unsigned char *pV = pU + (width * height)/4;
	
	unsigned char *pYUVTemp = pYUV422;
	unsigned char *pYUVTempNext = pYUV422 + width * 2;
	        
	for(i = 0; i < height; i += 2) {
		for(j = 0; j < width; j += 2) {
			pY[j] = *pYUVTemp++;
	    	pY[j + width] = *pYUVTempNext++;
	    	                 
	    	pU[j / 2] =(*(pYUVTemp) + *(pYUVTempNext)) / 2;
	    	pYUVTemp++;
	    	pYUVTempNext++;
	    	                 
	    	pY[j + 1] = *pYUVTemp++;
	    	pY[j + 1 + width] = *pYUVTempNext++;
	    	                 
	    	pV[j / 2] =(*(pYUVTemp) + *(pYUVTempNext)) / 2;
	    	pYUVTemp++;
	    	pYUVTempNext++;
		}
	    pYUVTemp += width * 2;
	    pYUVTempNext += width * 2;
	    pY += width * 2;
	    pU += width / 2;
	    pV += width / 2;
	}
	return 1;
}

int main(int argc, char* argv[])
{
//	char *input = "M6_897frames_422.yuv";
//	char *output = "M6_897frames_420.yuv";
//	int total_frames = 897;
	char *input = argv[1];
	char *output = argv[2];
	int total_frames = atoi(argv[3]);
	FILE *fp_yuv422 = fopen(input, "rb");
	FILE *fp_yuv420 = fopen(output, "wb");
	if (!fp_yuv422 || !fp_yuv420) {
		printf("open file failed\n");
		return -1;
	}
	int i = 0;
	for (i = 0; i < total_frames; i++)
	{
		printf("processing frame %d\n", i);
		unsigned char *yuv422_buf = (unsigned char *)malloc(2*1280*720*sizeof(unsigned char));
		unsigned char *yuv420_buf = (unsigned char *)malloc(3*1280*720/2*sizeof(unsigned char));
		
		fread(yuv422_buf, 1, 2*1280*720, fp_yuv422);
		yuv422_to_yuv420p(yuv422_buf, yuv420_buf, 1280, 720);
	
		fwrite(yuv420_buf, 1, 3*1280*720/2, fp_yuv420);
		
		free(yuv422_buf);
		free(yuv420_buf);
	}
	fclose(fp_yuv420);
	fclose(fp_yuv422);
	return 0;
}


你可能感兴趣的:(图像处理)