/*
将保存有YUV420像素格式的帧,转换成BGR24像素格式,并且按照帧指定的尺寸进行缩放
*/
void VideoDecodec::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_POINT, NULL, NULL, NULL);


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


 //释放环境

 sws_freeContext(pSwsCtx);
}


进阶

sws_scale提供如下的算法对图像进行伸缩变换

/* values for the flags, the stuff on the command line is different */
#define SWS_FAST_BILINEAR     1
#define SWS_BILINEAR          2
#define SWS_BICUBIC           4
#define SWS_X                 8
#define SWS_POINT          0x10
#define SWS_AREA           0x20
#define SWS_BICUBLIN       0x40
#define SWS_GAUSS          0x80
#define SWS_SINC          0x100
#define SWS_LANCZOS       0x200
#define SWS_SPLINE        0x400

目前需要对图像像素进行压缩,方便算法加快识别,每种算法生成的像素图片尺寸差不多