当前位置:网站首页>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;
}
边栏推荐
- Medium pen test questions: flip the string, such as ABCD, print out DCBA
- 同花顺股票开户选哪个券商好手机开户是安全么?
- PCB线路板塞孔工艺的那些事儿~
- An operation tool used by we media professionals who earn 1w+ a month
- 手动实现function isInstanceOf(child,Parent)
- News classification based on LSTM model
- Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
- GaussDB(DWS)主动预防排查
- [monomer] recommended configuration of streaming information i-bpsv3 server
- What is the difference between consonants and Initials? (difference between initials and consonants)
猜你喜欢

Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc

“丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中

Flume interview questions

Pytest Collection (2) - mode de fonctionnement pytest

上半年暂停考试要补考?包含监理工程师、建筑师等十项考试

基于YOLOv5的口罩佩戴检测方法

手动实现function isInstanceOf(child,Parent)

小 P 周刊 Vol.11
![[deep learning] use deep learning to monitor your girlfriend's wechat chat?](/img/03/ecf50eacc91c0633b0d9689cdad2c2.png)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?

PMP证书真的有用吗?
随机推荐
ngnix基础知识
中通笔试题:翻转字符串,例如abcd打印出dcba
物联网rfid等
都能看懂的LIS(最长上升子序列)问题[通俗易懂]
[monomer] recommended configuration of streaming information i-bpsv3 server
【MySQL】索引的创建、查看和删除
《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
游览器打开摄像头案例
"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
ICML2022 | 基于元语义正则化的介入性对比学习
企业架构与项目管理的关联和区别
Spark面试题
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
Aidl basic use
为什么数字化转型战略必须包括持续测试?
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
Mask wearing detection method based on yolov5
AirServer2022最新版功能介绍及下载
Unity 使用Sqlite