转换NV12为YUV420格式的百转工具
最编程
2024-08-15 16:35:30
...
大家好,又见面了,我是你们的朋友全栈君。
YU12格式也叫I420格式,是YUV420p其中的一种,NV12是YUV420sp的一种。YU12和NV21中YUV数据的排列方式为: YU12:YYYYYYYY UU VV NV12:YYYYYYYY UV UV 针对数据的排列结构,本文将NV12转为YU12。
主要转换接口实现为:
int NV12toYU12(char *data, char *out, int width, int height);
功能描述 : Nv12格式转YU12 输入参数 : data : NV12图片数据指针 out :YU12图片地址 width: 图像宽度 height : 图像高度 输出参数 : out,YU12图片地址 返 回 值 : int类型
具体代码如下:
/* *********************************************************************** * 文件名称:Nv12toYU12.c * 文件描述:Nv12格式转YU12 * 作 者:Young Fan *日 期:2021-09-16 ************************************************************************ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***************************************************************************** 函 数 名: NV12toYU12 功能描述 : Nv12格式转YU12 输入参数 : data NV12图片数据指针 out 输出的YU12图片地址 width 图像宽度 height 图像高度 输出参数 : 光栅格式yuv数据 返 回 值 : int *****************************************************************************/
int NV12toYU12(char *data, char *out, int width, int height)
{
if (data == NULL || out == NULL)
{
printf("error:empty pointer \n");
return -1;
}
if (width <= 0 || height <= 0)
{
printf("error:width or height is not positive \n");
return -1;
}
//分别获取NV12数据中的Y、U、V分量。NV12:YYYYYYYY UVUV => YU12: YYYYYYYY UU VV
//Y
memcpy(out, data, width * height);
printf("Y分量转换完成!\n");
//U
char * ptr1 = out + width * height; //YU12中U的首地址
char * ptr2 = data + width * height;
int n = 0;
while (n < width * height / 4)
{
//赋值
*(ptr1) = *(ptr2);
ptr1++;
ptr2 = ptr2 + 2;
n++;
}
printf("U分量转换完成!\n");
//V
ptr1 = out + width * height * 5 / 4; //YU12中V的首地址
ptr2 = data + width * height + 1;
n = 0;
while (n < width * height / 4)
{
//赋值
*(ptr1) = *(ptr2);
ptr1++;
ptr2 = ptr2 + 2;
n++;
}
printf("V分量转换完成!\n");
return 0;
}
int main(void)
{
int width = 512;
int height = 288;
int size = width * height * 3 / 2;
char *data = (char *)malloc(size);
char *out = (char *)malloc(size);
memset(data, 0, size);
memset(out, 0, size);
FILE *fp = fopen("./pic/NV12.yuv","rb");
fread(data, 1, size, fp);
int ret = NV12toYU12(data, out, width, height);
if (ret < 0)
{
printf("error: NV12toYU12 failed, ret %d\n", ret);
return -1;
}
//保存已转换好的YU12数据到本地
FILE *out_fp = fopen("./out_YU12.yuv", "wb");
fwrite(out, 1, size, out_fp);
printf("整体转换完成并将YU12图像保存在本地!\n");
//释放资源
fclose(fp);
fclose(out_fp);
if(data != NULL)
{
free(data);
data = NULL;
}
if(out != NULL)
{
free(out);
out = NULL;
}
return 0;
}
打印输出如下:
输出图片:
原图:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
推荐阅读
-
转换NV12为YUV420格式的百转工具
-
源码为您提供,零基础搭建的免费网址导航站--中国妖怪百集,收集了大量古代文献中的妖怪,非常详细的记录了妖怪的来历、描述甚至图片,让您对中国的妖怪文化有一个整体的了解,茶余饭后的消遣非常有趣,类似的网站还有很多,期待您的探索! 导航站的另一个特点是汇集了大量咖啡平时用的非常好的工具软件,其中大部分都是在线的,不需要下载安装,包括文档协作、分享,图片、音频、视频处理,格式转换,文件传输,各个网站的视频下载等等,兼职就是一个工具百宝箱! 如果你觉得这些网站不符合你的使用习惯,或者想添加一些自己收藏的网站,那么把源码交给你,只需简单修改就可以变成符合自己使用习惯的独特导航了! 如何修改 导航站采用纯静态构建,下载源代码后,只需要修改 html 文件中的代码块即可,代码块结构如下: 只需要修改四个地方,分别对应网站的四个关键信息: 修改相应网站的 URL 地址 修改相应网站的徽标 修改相应网站的名称