本文講述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左右
場景復雜度對軟編的影響非常大,對硬編的影響不明顯。
如需交流,可以加QQ群1038388075,766718184,或者QQ:350197870
視頻教程 播放地址: http://www.iqiyi.com/u/1426749687
視頻下載地址:http://www.chungen90.com/?news_3/
Demo下載地址: http://www.chungen90.com/?news_2