代码:
#define SRC_FORMAT AV_PIX_FMT_YUV420P #define DEST_FORMAT AV_PIX_FMT_BGR24 static AVPicture g_oPicture = {0}; static struct SwsContext* g_pScxt = NULL; void yuv2rgb(AVFrame* pFrame, uint8_t* pRgbBuffer) { int width = pFrame->width; int height = pFrame->height; if (g_pScxt == NULL) { g_pScxt = sws_getContext(width, height, SRC_FORMAT, width, height, DEST_FORMAT, SWS_POINT, NULL, NULL, NULL); //AVFrame需要填充,还是这个方便。 avpicture_alloc(&g_oPicture, DEST_FORMAT, width, height); } int num_bytes = av_image_get_buffer_size(DEST_FORMAT, width, height, 32); int retsws = sws_scale(g_pScxt, pFrame->data, pFrame->linesize, 0, height, g_oPicture.data, g_oPicture.linesize); //注意,RGB数据是这样复制的 memcpy(pRgbBuffer, g_oPicture.data[0], num_bytes); //播放完毕后再释放。 //avpicture_free(&g_oPicture); //sws_freeContext(g_pScxt); }