FFmpeg sws_scale帧伸缩和像素转换

void ConvertYUVFrameToBGRFrame(AVFrame* pYUVFrame, AVFrame* pBGRFrame)
{
 int nBGRFrameSize = av_image_get_buffer_size(AV_PIX_FMT_BGR24, pBGRFrame->width, pBGRFrame->height, 1);
 uint8_t* pszBGRBuffer = (uint8_t*)av_malloc(nBGRFrameSize);


 //将pszBGRBuffer挂载在pBGRFrame帧的图片缓存指针,需要手动删除
 av_image_fill_arrays(pBGRFrame->data, pBGRFrame->linesize, pszBGRBuffer, AV_PIX_FMT_BGR24, pBGRFrame->width, pBGRFrame->height, 1);

 struct SwsContext *pSwsCtx = sws_getContext(pYUVFrame->width, pYUVFrame->height, AV_PIX_FMT_YUVJ420P,
  pBGRFrame->width, pBGRFrame->height, AV_PIX_FMT_BGR24,
  SWS_BICUBIC, NULL, NULL, NULL);


 //注意需要将0填充srcSliceY,否则调用失败
 sws_scale(pSwsCtx, pYUVFrame->data,
  pYUVFrame->linesize, 0, pYUVFrame->height,
  pBGRFrame->data, pBGRFrame->linesize);
}


调用例子

  AVFrame *pBGRFrame = NULL;
  pBGRFrame = av_frame_alloc();
  pBGRFrame->width = 1920 / 2;
  pBGRFrame->height = 1080 / 2;
  pBGRFrame->format = AV_PIX_FMT_BGR24;
  pVideoc->ConvertYUVFrameToBGRFrame(pFrame, pBGRFrame);

说明:对图片的尺寸进行缩小,然后转换像素格式为BGR24


上一篇:面试题 16.06. 最小差


下一篇:2019系统优化软件排行(综合麻省理工学院,剑桥大学的统计数据)