从内存中保存yuv文件

数据是NV12
YYYY
YYYY
YYYY
YYYY
UVUV
UVUV
	
    	frame_y_buf = (u8 *)malloc(width * height);
		if (frame_y_buf == NULL) {
			printf("allocate frame_y_buf error, line:%d!\n", __LINE__);
			rval = -1;
			break;
		}
		frame_uv_buf = (u8 *)malloc(width * height / 2);
		if (frame_uv_buf == NULL) {
			printf("allocate frame_uv_buf error, line:%d!\n", __LINE__);
			rval = -1;
			break;
		}	


    // Read the frame,去除pitch
	for(i = 0; i < yuv_height; i++) {
		memcpy(frame_y_buf + i * yuv_width , y_addr + pitch * i, yuv_width);
		if (i < yuv_height / 2) {
			memcpy(frame_uv_buf + i * yuv_width, uv_addr + pitch * i, yuv_width);
		}
	}

    /* "wb+" 读写打开或建立一个二进制文件,允许读和写。*/
	FILE *fp = fopen("/sdcard/111.yuv", "wb+");
	fwrite(frame_y_buf, 1, yuv_width * yuv_height, fp);
	fwrite(frame_uv_buf, 1, yuv_width * yuv_height / 2, fp);
	fclose(fp);
	fp = NULL;
	
	//
	memset(frame_y_buf, 0, yuv_width * yuv_height);
	memset(frame_uv_buf, 0, yuv_width * yuv_height / 2);
	FILE *pFile = fopen("/sdcard/YUVSemiPlanar.yuv", "rb");
	fread(frame_y_buf, sizeof(unsigned char), 640 * 480, pFile);
	fread(frame_uv_buf, sizeof(unsigned char), 640 * 480 / 2, pFile);

	fp = fopen("/sdcard/222.yuv", "wb+");
	fwrite(frame_y_buf, 1, yuv_width * yuv_height, fp);
	fwrite(frame_uv_buf, 1, yuv_width * yuv_height / 2, fp);
	fclose(fp);
	fp = NULL;

 

下面是将NV12格式的图像数据保存YUV格式文件的C语言算法: ```c #include <stdio.h> // 定义NV12格式图像的宽和高 #define WIDTH 640 #define HEIGHT 480 // 将NV12格式的图像数据保存YUV格式文件 void nv12_to_yuv(unsigned char* nv12_data, int nv12_size, const char* yuv_filename) { // 打开YUV格式文件 FILE* yuv_file = fopen(yuv_filename, "wb"); if (yuv_file == NULL) { printf("Error: Failed to open YUV file\n"); return; } // 计算Y分量和UV分量的大小 int y_size = WIDTH * HEIGHT; int uv_size = y_size / 2; // 写入Y分量到YUV文件 fwrite(nv12_data, 1, y_size, yuv_file); // 将UV分量写入到YUV文件 unsigned char* uv_data = nv12_data + y_size; for (int i = 0; i < uv_size; i += 2) { fwrite(&uv_data[i + 1], 1, 1, yuv_file); fwrite(&uv_data[i], 1, 1, yuv_file); } // 关闭YUV文件 fclose(yuv_file); } int main() { // 读取NV12格式的图像数据 FILE* nv12_file = fopen("input.nv12", "rb"); if (nv12_file == NULL) { printf("Error: Failed to open NV12 file\n"); return 1; } fseek(nv12_file, 0, SEEK_END); int nv12_size = ftell(nv12_file); fseek(nv12_file, 0, SEEK_SET); unsigned char* nv12_data = (unsigned char*)malloc(nv12_size); fread(nv12_data, 1, nv12_size, nv12_file); fclose(nv12_file); // 将NV12格式的图像数据保存YUV格式文件 nv12_to_yuv(nv12_data, nv12_size, "output.yuv"); // 释放内存 free(nv12_data); return 0; } ``` 上述代码,`nv12_data` 表示NV12格式的图像数据,`nv12_size` 表示数据大小,`yuv_filename` 表示保存YUV格式文件名。函数 `nv12_to_yuv` 将NV12格式的图像数据转换为YUV格式并保存文件。在主函数,先读取NV12格式的图像数据,然后调用 `nv12_to_yuv` 函数将NV12格式的数据转换为YUV格式并保存文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值