当前位置:网站首页>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;
}
边栏推荐
- Fundamentals - IO intensive computing and CPU intensive computing
- The difference between NiO and traditional IO
- 地图其他篇总目录
- Four methods of JS array splicing [easy to understand]
- Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
- Design and practice of new generation cloud native database
- Copy ‘XXXX‘ to effectively final temp variable
- MySQL系列之事务日志Redo log学习笔记
- 基于LSTM模型实现新闻分类
- Unity 使用Sqlite
猜你喜欢

pytest合集(2)— pytest运行方式
![[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial](/img/b0/029cdea72483ed9bc8a0d66908983a.png)
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial

名单揭晓 | 2021年度中国杰出知识产权服务团队

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

Introduction and download of the latest version of airserver2022
![[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial](/img/ac/655fd534ef7ab9d991d8fe1c884853.png)
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial

Is PMP certificate really useful?

工控设备安全加密的意义和措施

焱融看 | 混合云时代下,如何制定多云策略

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
随机推荐
MQ learning notes
idea中类中显示成员变量和方法
选择在同花顺上炒股开户可以吗?安全吗?
pytest合集(2)— pytest运行方式
Matlab traverses images, string arrays and other basic operations
Difference and use between require and import
Icml2022 | interventional contrastive learning based on meta semantic regularization
Show member variables and methods in classes in idea
A debugging to understand the slot mechanism of redis cluster
What is the difference between consonants and Initials? (difference between initials and consonants)
List announced | outstanding intellectual property service team in China in 2021
打出三位数的所有水仙花数「建议收藏」
JS how to get a list of elements in a collection object
vscode的使用
十三届蓝桥杯B组国赛
js数组拼接的四种方法[通俗易懂]
基础—io密集型计算和cpu密集型计算
首席信息官对高绩效IT团队定义的探讨和分析
GaussDB(DWS)主动预防排查
[deep learning] use deep learning to monitor your girlfriend's wechat chat?