[Video and Audio Data Processing] 生成灰阶测试图

0. 不逼逼,可以编译通过的代码如下

以下写了详细注释:

extern "C"
{
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS

#endif

}
extern "C" {

#include 
#include 
#include 
#include 
}


/**
 * Split Y, U, V planes in YUV444P 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.
 *
 */
 /**
  * Convert YUV420P file to gray picture
  * @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_graybar(int width, int height, int ymin, int ymax, int barnum, 
const char* url_out) {
	int barwidth;  //每个灰阶的宽度
	float lum_inc;
	unsigned char lum_temp;
	int uv_width, uv_height;
	FILE* fp = NULL;
	unsigned char* data_y = NULL;
	unsigned char* data_u = NULL;
	unsigned char* data_v = NULL;
	int t = 0, i = 0, j = 0;

	barwidth = width / barnum; //获取每个灰阶的宽度
	lum_inc = ((float)(ymax - ymin)) / ((float)(barnum - 1)); 
	//根据灰阶获取平均亮度28.333,若以后面的main函数来调用的话
	uv_width = width / 2; //uv分量的宽度
	uv_height = height / 2; // UV分量的高

	data_y = (unsigned char*)malloc(width * height); //给y分量/亮度分量,申请内存空间
	data_u = (unsigned char*)malloc(uv_width * uv_height); //给u分量,色度分量申请内存空间
	data_v = (unsigned char*)malloc(uv_width * uv_height); //给V分量,色度分量申请内存空间

	if ((fp = fopen(url_out, "wb+")) == NULL) {
		printf("Error: Cannot create file!");
		return -1;
	}

	//Output Info
	printf("Y, U, V value from picture's left to right:\n");
	for (t = 0; t < (width / barwidth); t++) {
		lum_temp = ymin + (char)(t * lum_inc);
		printf("%3d, 128, 128\n", lum_temp); //打印出平均的亮度
	}
	//Gen Data
	for (j = 0; j < height; j++) {
		for (i = 0; i < width; i++) {
			t = i / barwidth; //t是int型,恰好可以如下从0,递增的分配亮度
			lum_temp = ymin + (char)(t * lum_inc);
			data_y[j * width + i] = lum_temp;
		}
	}
	for (j = 0; j < uv_height; j++) {
		for (i = 0; i < uv_width; i++) {
			data_u[j * uv_width + i] = 128; 
			//由于目的是生成灰阶图,所以把U、V分量的色度设置为0(128即转换前的0)
		}
	}
	for (j = 0; j < uv_height; j++) {
		for (i = 0; i < uv_width; i++) {
			data_v[j * uv_width + i] = 128;
			//由于目的是生成灰阶图,所以把U、V分量的色度设置为0(128即转换前的0)
		}
	}
	fwrite(data_y, width * height, 1, fp);
	fwrite(data_u, uv_width * uv_height, 1, fp);
	fwrite(data_v, uv_width * uv_height, 1, fp);
	fclose(fp);
	free(data_y);
	free(data_u);
	free(data_v);
	return 0;
}




int main()
{
	simplest_yuv420_graybar(640, 360, 0, 255, 10, "graybar_640x360.yuv");
    return 0;
}

1. 效果如下:

[Video and Audio Data Processing] 生成灰阶测试图_第1张图片

参考链接:

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

你可能感兴趣的:(视音频数据处理)