当前位置:网站首页>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;
}
边栏推荐
- Mask wearing detection method based on yolov5
- 都能看懂的LIS(最长上升子序列)问题[通俗易懂]
- [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!
- [monomer] recommended configuration of streaming information i-bpsv3 server
- 灵动微 MM32 多路ADC-DMA配置
- ICML2022 | 基于元语义正则化的介入性对比学习
- Spark面试题
- CSDN购买的课程从哪里可以进入
- linux下清理系统缓存并释放内存
- 测试撤销1
猜你喜欢

Classify boost libraries by function

Basic operation of binary tree

十三届蓝桥杯B组国赛

Talking from mlperf: how to lead the next wave of AI accelerator

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

PMP与NPDP之间的区别是什么?

Mask wearing detection method based on yolov5

What is the difference between PMP and NPDP?
![[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

Pytest collection (2) - pytest operation mode
随机推荐
Separate the letters and numbers in the string so that the letters come first and the array comes last
Make a three digit number of all daffodils "recommended collection"
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
名单揭晓 | 2021年度中国杰出知识产权服务团队
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
MySQL series transaction log redo log learning notes
Introduction and download of the latest version of airserver2022
Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
Case of camera opening by tour
A debugging to understand the slot mechanism of redis cluster
An operation tool used by we media professionals who earn 1w+ a month
JS how to get a list of elements in a collection object
Can I choose to open an account for stock trading on flush? Is it safe?
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
PCB线路板塞孔工艺的那些事儿~
十三届蓝桥杯B组国赛
Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
The correct way to set the bypass route
LIS (longest ascending subsequence) problem that can be understood [easy to understand]