当前位置:网站首页>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;
}
边栏推荐
- 【juc学习之路第8天】Condition
- 十三届蓝桥杯B组国赛
- Case of camera opening by tour
- Make a three digit number of all daffodils "recommended collection"
- 中通笔试题:翻转字符串,例如abcd打印出dcba
- Application of real estate management based on 3D GIS
- Significance and measures of security encryption of industrial control equipment
- 测试撤销1
- js如何获取集合对象中某元素列表
- MySQL learning notes - SQL optimization of optimization
猜你喜欢

MySQL系列之事务日志Redo log学习笔记

pytest合集(2)— pytest運行方式

Spark面试题

Basic operation of binary tree

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

【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
![[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

JS how to get a list of elements in a collection object

Training on the device with MIT | 256Kb memory

PMP与NPDP之间的区别是什么?
随机推荐
按照功能对Boost库进行分类
List announced | outstanding intellectual property service team in China in 2021
Show member variables and methods in classes in idea
TOPS,处理器运算能力单位、每秒钟可进行一万亿次
String type conversion BigDecimal, date type
linux下清理系统缓存并释放内存
91.(cesium篇)cesium火箭发射模拟
微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
PCB plug hole technology~
【MySQL】explain的基本使用以及各列的作用
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
中通笔试题:翻转字符串,例如abcd打印出dcba
burpsuite简单抓包教程[通俗易懂]
Case of camera opening by tour
“丝路正青春 风采看福建”在闽外籍青年短视频大赛火热征集作品中
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Using closures to switch toggle by clicking a button
Manually implement function isinstanceof (child, parent)