只要开始初始化一次,结束后释放就好,中间可以循环转码

AVFrame    *m_pFrameRGB,*m_pFrameYUV;
uint8_t *m_rgbBuffer,*m_yuvBuffer;
struct SwsContext *m_img_convert_ctx; void init() //分配两个Frame,两段buff,一个转换上下文
{
 //为每帧图像分配内存
m_pFrameYUV = av_frame_alloc();
m_pFrameRGB = av_frame_alloc(); // width和heigt为传入的分辨率的大小,分辨率有变化时可以以最大标准申请
int numBytes = avpicture_get_size(AV_PIX_FMT_RGB32, nwidth,nheight);
m_rgbBuffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
int yuvSize = nwidth * nheight * /;
m_yuvBuffer = (uint8_t *)av_malloc(yuvSize);
//特别注意sws_getContext内存泄露问题,
//注意sws_getContext只能调用一次,在初始化时候调用即可,另外调用完后,在析构函数中使用sws_freeContext,将它的内存释放。
//设置图像转换上下文
m_img_convert_ctx = sws_getContext(nwidth, nheight, AV_PIX_FMT_YUV420P, \
                        nwidth, nheight, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);
} void play(char* pbuff_in,int nwidth,int nheight)
{
avpicture_fill((AVPicture *) m_pFrameRGB, m_rgbBuffer, AV_PIX_FMT_RGB32,nwidth, nheight);
avpicture_fill((AVPicture *) m_pFrameYUV, (uint8_t *)pbuff_in, AV_PIX_FMT_YUV420P, nwidth, nheight);
//转换图像格式,将解压出来的YUV420P的图像转换为RGB的图像
sws_scale(m_img_convert_ctx,
(uint8_t const * const *) m_pFrameYUV->data,
m_pFrameYUV->linesize, , nheight, m_pFrameRGB->data,
m_pFrameRGB->linesize);
//把这个RGB数据 用QImage加载
QImage tmpImg((uchar *)m_rgbBuffer,nwidth,nheight,QImage::Format_RGB32);
//把图像复制一份 传递给界面显示
m_mapImage[nWindowIndex] = tmpImg.copy();
} void release()
{
av_frame_free(&m_pFrameYUV);
av_frame_free(&m_pFrameRGB);
av_free(m_rgbBuffer);
av_free(m_yuvBuffer);
sws_freeContext(m_img_convert_ctx);
}
bool YV12ToBGR24_FFmpeg(unsigned char* pYUV,unsigned char* pBGR24,int width,int height)
{
if (width < || height < || pYUV == NULL || pBGR24 == NULL)
return false;
//int srcNumBytes,dstNumBytes;
//uint8_t *pSrc,*pDst;
AVPicture pFrameYUV,pFrameBGR; //pFrameYUV = avpicture_alloc();
//srcNumBytes = avpicture_get_size(PIX_FMT_YUV420P,width,height);
//pSrc = (uint8_t *)malloc(sizeof(uint8_t) * srcNumBytes);
avpicture_fill(&pFrameYUV,pYUV,PIX_FMT_YUV420P,width,height); //U,V互换
uint8_t * ptmp=pFrameYUV.data[];
pFrameYUV.data[]=pFrameYUV.data[];
pFrameYUV.data []=ptmp; //pFrameBGR = avcodec_alloc_frame();
//dstNumBytes = avpicture_get_size(PIX_FMT_BGR24,width,height);
//pDst = (uint8_t *)malloc(sizeof(uint8_t) * dstNumBytes);
avpicture_fill(&pFrameBGR,pBGR24,PIX_FMT_BGR24,width,height); struct SwsContext* imgCtx = NULL;
imgCtx = sws_getContext(width,height,PIX_FMT_YUV420P,width,height,PIX_FMT_BGR24,SWS_BILINEAR,,,); if (imgCtx != NULL){
sws_scale(imgCtx,pFrameYUV.data,pFrameYUV.linesize,,height,pFrameBGR.data,pFrameBGR.linesize);
if(imgCtx){
sws_freeContext(imgCtx);
imgCtx = NULL;
}
return true;
}
else{
sws_freeContext(imgCtx);
imgCtx = NULL;
return false;
}
}

另一种方法:

最新文章

  1. webpack搭建前端一条龙服务
  2. 移动BI来袭我们要做哪些准备?
  3. 删除表空间的时候遇到的问题:ORA-02429: 无法删除用于强制唯一/主键的索引
  4. Codeforces Round #335 (Div. 2)
  5. 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)
  6. installshield
  7. Log4Net总结
  8. Spring中使用quartz插件实现定时任务
  9. 201521123002 《Java程序设计》第6周学习总结
  10. 2018-01-28-M个本地工作点代码同步到N个GIT远端
  11. 010 Editor v8.0.1(32 - bit) 算法逆向分析、注册机编写
  12. 下拉列表模仿placeholder效果
  13. 将txt文本转换为excel格式
  14. linux command 4
  15. Oracle异常:Caused by: java.sql.SQLException: ORA-01536: 超出表空间 &#39;登录名&#39; 的空间限额 (JPA保存数据)
  16. centos 7 服务管理
  17. 全网最详细的用pip安装****模块报错:Could not find a version that satisfies the requirement ****(from version:) No matching distribution found for ****的解决办法(图文详解)
  18. IIS 共享目录读写报错 Access to the path:“\\192.168.0.1\1.txt”is denied解决方案
  19. jenkins slave 安装服务与卸载
  20. nodejs服务器部署教程四

热门文章

  1. 【vue】iView-admin后台管理系统
  2. SQL Server-聚焦事务、隔离级别详解(二十九)
  3. iOS开发简记(7):网络请求模块
  4. centos6.5 squid安装
  5. BZOJ1283 序列 网络流区间覆盖模型
  6. Sparse Principal Component Analysis via Regularized Low Rank Matrix Approximation(Adjusted Variance)
  7. Python—randonm模块介绍
  8. STL vector用法
  9. c++入门之 深入cin
  10. PHP实用代码片段(三)