分离YUV420P像素数据中的Y、U、V分量【转载】
只做一些修改,是为转载。
- 增加头文件
- 并入main函数
- 修改输出文件后缀名
在调用时,填写的四个参数
simplest_yuv420_split(“foreman_qcif.yuv”, 176, 144, 300);
“foreman_qcif.yuv”:yuv文件名
176:文件width
144:文件height
300:frame numbers
/**
* Split Y, U, V planes in YUV420P 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
int simplest_yuv420_split(const char *url, int w, int h, int num) {
FILE *fp = fopen(url, "rb+");
//额外修改三个分量输出文件的后缀
FILE *fp1 = fopen("output_420_y.yuv", "wb+");
FILE *fp2 = fopen("output_420_u.yuv", "wb+");
FILE *fp3 = fopen("output_420_v.yuv", "wb+");
/* orginal code
FILE *fp1=fopen("output_420_y.y","wb+");
FILE *fp2=fopen("output_420_u.y","wb+");
FILE *fp3=fopen("output_420_v.y","wb+");
*/
unsigned char *pic = (unsigned char *)malloc(w*h * 3 / 2);
for (int i = 0; i < num; i++) {
fread(pic, 1, w*h * 3 / 2, fp);
//Y
fwrite(pic, 1, w*h, fp1);
//U
fwrite(pic + w * h, 1, w*h / 4, fp2);
//V
fwrite(pic + w * h * 5 / 4, 1, w*h / 4, fp3);
}
free(pic);
fclose(fp);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
int main()
{
simplest_yuv420_split("foreman_qcif.yuv", 176, 144, 300);
return 0;
}
之所以把输出文件后缀修改为.yuv是方便使用YUView打开