How to convert RGB from YUV420p for ffmpeg encoder?

You can use libswscale from FFmpeg like this:

#include <libswscale/swscale.h>
SwsContext * ctx = sws_getContext(imgWidth, imgHeight,
                                  AV_PIX_FMT_RGB24, imgWidth, imgHeight,
                                  AV_PIX_FMT_YUV420P, 0, 0, 0, 0);
uint8_t * inData[1] = { rgb24Data }; // RGB24 have one plane
int inLinesize[1] = { 3*imgWidth }; // RGB stride
sws_scale(ctx, inData, inLinesize, 0, imgHeight, dst_picture.data, dst_picture.linesize);

Note that you should create an instance of the SwsContext object only once, not for each frame.

Leave a Comment