本文讲述windows 平台下ffmpeg如何利用intel media SDK 进行 h264硬编码(测试版本为3.2.2)。
ffmeg硬编编码的流程与软件编码流程相同,唯一不同的地方在初始化encoder。软件编码基本流程前文有
介绍,本文不再赘述。下面介绍硬编编码初始化encoder代码。
int InitEncoderCodec( int iWidth, int iHeight)
{
AVCodec * pH264Codec = avcodec_find_encoder_by_name("h264_qsv");
//AVCodec * pH264Codec = avcodec_find_encoder_by_name("libx264");
//AVCodec * pH264Codec = avcodec_find_encoder(AV_CODEC_ID_H264);
if(NULL == pH264Codec)
{
printf("%s", "avcodec_find_encoder failed");
return -1;
}
outPutEncContext = avcodec_alloc_context3(pH264Codec);
outPutEncContext->gop_size = 12;
//outPutEncContext->framerate = 30;
//outPutEncContext->has_b_frames = 0;
outPutEncContext->max_b_frames = 0;
//outPutEncContext = avcodec_alloc_context3(pH264Codec);
outPutEncContext->codec_id = pH264Codec->id;
outPutEncContext->time_base.num =context->streams[0]->codec->time_base.num;
outPutEncContext->time_base.den = context->streams[0]->codec->time_base.den;
outPutEncContext->pix_fmt = *pH264Codec->pix_fmts;
outPutEncContext->width = iWidth;
outPutEncContext->height = iHeight;
//outPutEncContext->bit_rate = 4* 1024 * 1024;
AVDictionary *options = nullptr;
outPutEncContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;;
//av_dict_set(&options,"preset","ultrafast",0); //"ultrafast"s
//av_dict_set(&options,"preset","veryfast",0);
//av_opt_set(outPutEncContext->priv_data,"tune","zerolatency",0);
//av_opt_set(outPutEncContext->priv_data,"profile","baseline",0);
int ret = avcodec_open2(outPutEncContext, pH264Codec, &options);
AVQSVContext *qsv = (AVQSVContext *)outPutEncContext->hwaccel_context;
if (ret < 0)
{
printf("%s", "open codec failed");
return ret;
}
return 1;
}
硬编码与软件编码性能对比(测试机器,I7 5500)
视频复杂度低的情况下(例如编码抓取桌面视频(桌面视频无明显变化) 1080p,25fps):
软件编码所占CPU 在30% -40%之间。硬件编码所在的cpu在18-25左右
视频负载度高的情况下(例如编码抓取桌面视频(桌面播放高清影视视频) 1080p,25fps)
软件编码所占CPU 在50% -70%之间。硬件编码所在的cpu在18-25左右
场景复杂度对软编的影响非常大,对硬编的影响不明显。
如有交流请加流媒体/Ffmpeg/音视频 127903734 ,或QQ350197870。