/*
將保存有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
目前需要對圖像像素進行壓縮,方便算法加快識別,每種算法生成的像素圖片尺寸差不多