FFmpegH264Encoder
FFmpegH264Encoder is to use the FFmpeg library to encode video YUV data into H264 data. On C3V we will use the encoder named h264_v4l2m2m which is a hardware encoder.
API Instructions
namespace com { namespace sunplus { namespace media {
using H264DataCallback = std::function<void(AVPacket* packet, int index)>;
class FFmpegH264Encoder {
public:
FFmpegH264Encoder();
~FFmpegH264Encoder();
public:
int init(VideoStreamParam_t param, H264DataCallback dataCallback);
void uninit();
AVCodecContext* getAVCodecContext();
int encode(AVFrame* yuvFrame);
int flush();
};
}}}
Constructors
FFmpegH264Encoder();
init
Set encoding parameters, such as gop, bitrate, fps, etc, and initialize encode
/**
* Set encoding parameters and initialize encode.
*
* @param param set parameters of the encoder.
*
* @param dataCallback to get h264 packet.
*
*/
int init(VideoStreamParam_t param, H264DataCallback dataCallback);
Sample
uninit
Release all resources allocated by the init method and close the encoder.
getAVCodecContext
Get AVCodecContext to get the info of the encoder, such as width, height, spspps, etc.
encode
Send the yuv frame to the encoder. the yuvFrame must be freed with av_frame_unref()+av_frame_free() when it is no longer needed.
flush
Send a NULL frame, in which case it is considered a flush packet.
If the encoder still has packets buffered, it will return them after this call.
Sample Code
This is a sample of H264 encoding using FFmpegH264Encoder. FFmpegV4L2VideoSource provides the yuv source, then gets the yuv frame from FFmpegVideoProvider and sends it to the encoder.
You can also use the source code of FFmpegH264Provider as the sample code of FFmpegH264Encoder.
the flow of encode h264:
create h264 encoder --> init --> create video provider --> provider prepare --> create the thread of get frame --> send to encoder --> get h264 data by H264DataCallback.
Test Result
Â