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