当前位置:网站首页>QT 使用FFmpeg4将argb的Qimage转换成YUV422P
QT 使用FFmpeg4将argb的Qimage转换成YUV422P
2022-07-01 21:43:00 【F小志】
前言
用FFmpeg4实现一个非常简单的argb转换yuv422p的函数
1.通过QPixmap获取QLabel显示界面并转成image,在传入转换函数
QPixmap pixmap(subtitleLabel->size());
pixmap.fill(Qt::transparent);//设置pixmap背景透明
subtitleLabel->render(&pixmap);
QImage image = pixmap.toImage();
unsigned char *imgBuf = image.bits();
argbToyuv(image.width(),image.height(),imgBuf);
2.创建rgbBuffer并填充image的rgbBuf数据,创建yuvBuffer,创建SwsContext转换上下文,使用sws_scale进行转换
#define HI_ALIGN_UP(x, a) ( ( ((x) + ((a) - 1) ) / (a) ) * (a) )
int argbToyuv(int width, int height, unsigned char *rgbBuf)
{
int ret, numBytes = 0;
uint8_t *src_data[4], *dst_data[4];
int src_linesize[4], dst_linesize[4];
int src_w = width, src_h = height, dst_w, dst_h;
dst_w = HI_ALIGN_UP(src_w, 16);//像素对齐
dst_h = HI_ALIGN_UP(src_h, 16);
enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_ARGB, dst_pix_fmt = AV_PIX_FMT_YUV422P;
SwsContext *sws_ctx = NULL;
/* create argbBuffer 创建转换前,填充image数据的buffer*/
numBytes = av_image_get_buffer_size(src_pix_fmt, src_w, src_h, 1);//1920*1080*4
uint8_t *argbBuffer = NULL;
(uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
av_image_fill_arrays(src_data, src_linesize, argbBuffer, src_pix_fmt, src_w, src_h, 1);
src_data[0] = rgbBuf;
src_data[1] = src_data[0]+(width*height);
src_data[2] = src_data[1]+(width*2*height);
// FILE *output_rgb=fopen("/opt/vw30/bin/subtitle_rgb","ab+");
// fwrite(rgbBuf,(src_w*src_h)*3,1,output_rgb);
// fclose(output_rgb);
/* create yuvBuffer 创建转换后接收的buffer*/
numBytes = av_image_get_buffer_size(dst_pix_fmt, dst_w, dst_h, 1);
uint8_t *yuvBuffer = NULL;
yuvBuffer = (uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
av_image_fill_arrays(dst_data, dst_linesize, yuvBuffer ,dst_pix_fmt, dst_w, dst_h, 1);
/* create scaling context 创建转换上下文*/
sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
dst_w, dst_h, dst_pix_fmt,
SWS_BILINEAR, NULL, NULL, NULL);
if (!sws_ctx) {
fprintf(stderr,
"Impossible to create scale context for the conversion "
"fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
ret = AVERROR(EINVAL);
goto end;
}
/*sws_scale 转换函数*/
ret = sws_scale(sws_ctx, src_data, src_linesize, 0, src_h, dst_data, dst_linesize);
if (ret < 0){
qDebug()<<"sws_scale error";
goto end;
}
end:
if(yuvBuffer){
av_free(yuvBuffer);
}
if(argbBuffer){
av_free(argbBuffer);
}
if(sws_ctx){
sws_freeContext(sws_ctx);
}
// FILE *output=fopen("/opt/vw30/bin/subtitle.yuv","ab+");
// fwrite(dst_data[0],dst_w*dst_h,1,output);
// fwrite(dst_data[1],dst_w*dst_h/4,1,output);
// fwrite(dst_data[2],dst_w*dst_h/4,1,output);
return 0;
}
边栏推荐
- 名单揭晓 | 2021年度中国杰出知识产权服务团队
- 能升职加薪?PMP证书含金量浅析
- Matlab traverses images, string arrays and other basic operations
- Flume面试题
- 从MLPerf谈起:如何引领AI加速器的下一波浪潮
- Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan
- Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
- "The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
- LIS (longest ascending subsequence) problem that can be understood [easy to understand]
- [NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
猜你喜欢
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
Flume面试题
多种智能指针
K-means based user portrait clustering model
91.(cesium篇)cesium火箭发射模拟
I received a letter from CTO inviting me to interview machine learning engineer
企业架构与项目管理的关联和区别
Introduction and download of the latest version of airserver2022
js如何获取集合对象中某元素列表
List announced | outstanding intellectual property service team in China in 2021
随机推荐
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
信标委云原生专题组组长,任重道远!
Four methods of JS array splicing [easy to understand]
Can I choose to open an account for stock trading on flush? Is it safe?
收到一封CTO来信,邀约面试机器学习工程师
基础—io密集型计算和cpu密集型计算
[noip2013] building block competition [noip2018] road laying greed / difference
Separate the letters and numbers in the string so that the letters come first and the array comes last
Introduction and download of the latest version of airserver2022
【深度学习】利用深度学习监控女朋友的微信聊天?
函数基本学习之一
ngnix基础知识
选择在同花顺上炒股开户可以吗?安全吗?
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
CSDN购买的课程从哪里可以进入
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
pytest合集(2)— pytest运行方式
【MySQL】explain的基本使用以及各列的作用
月入1W+的自媒体达人都会用到的运营工具
Classify boost libraries by function