其他分享
首页 > 其他分享> > FFmpeg sws_scale帧伸缩和像素转换

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


标签:pBGRFrame,scale,FFmpeg,BGR24,height,PIX,width,pYUVFrame,sws
来源: https://blog.51cto.com/fengyuzaitu/2450099