本篇主要介绍视频编码,将转码之后的YUV420像素数据编码为H264裸数据。
技术简介
使用ffmpeg的编码器
使用模块(库)
使用ffmpeg的avcodec-58.dll
主要流程和代码
1、初始化编码器
int H264Encoder::init(int width, int height, int framerate, int bitrate, int qb, int gopTime)
{
int err = ERROR_CODE_OK;
if (m_inited) {
return err;
}
AVDictionary* options = nullptr;
av_dict_set(&options, "preset", "superfast", 0);
av_dict_set(&options, "tune", "zerolatency", 0);
do {
m_ringBuffer = new HELPER::RingBuffer<AVFrame>();
if (m_ringBuffer == nullptr) {
err = ERROR_CODE_ALLOC_FAILED;
break;
}
if (!m_ringBuffer->init(H264_ENCODER_RINGBUFFER_SIZE)) {
err = ERROR_CODE_ALLOC_FAILED;
break;
}
m_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (m_codec == nullptr) {
err = ERROR_CODE_FFMPEG_FIND_ENCODER_FAILED;
break;
}
m_encodeContext = avcodec_alloc_context3(m_codec);
if (m_encodeContext == nullptr) {
err = ERROR_CODE_FFMPEG_ALLOC_CONTEXT_FAILED;
break;
}
m_encodeContext->codec_type = AVMEDIA_TYPE_VIDEO;
m_encodeContext->codec_id = AV_CODEC_ID_H264;
m_encodeContext->pix_fmt = AV_PIX_FMT_YUV420P;
m_encodeContext->width = width;
m_encodeContext->height = height;
m_encodeContext->time_base = {
1, framerate };
m_encodeContext->framerate = {
framerate, 1 };
m_encodeContext->bit_rate = bitrate;
m_encodeContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
m_encodeContext->max_b_frames =