NVENC编码流程
- 加载NVENCODE API
- 打开编码会话
- 初始化编码器
- 注册输入资源
- 映射注册的输入资源
- 创建输出比特流缓冲
- 编码一帧(同步方式)
- 获取输出(同步方式)
加载API
将NVENC lib中的接口导出,供后续编码流程使用。
NV_ENCODE_API_FUNCTION_LIST m_funList = { NV_ENCODE_API_FUNCTION_LIST_VER };
NVENC_API_CALL(NvEncodeAPICreateInstance(&m_funList));
打开编码会话
编码会话需要传入D3D设备,在此之前需要创建好D3D设备。接口输出的void* m_encoder;
将会用于后续编码接口的第一个参数中。
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encodeSessionExParams = { 0 };
encodeSessionExParams.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
encodeSessionExParams.device = (void*)m_device;
encodeSessionExParams.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
encodeSessionExParams.apiVersion = NVENCAPI_VERSION;
void* m_encoder;
NVENC_API_CALL(m_funList.nvEncOpenEncodeSessionEx(&encodeSessionExParams, &m_encoder));
初始化编码器
可以使用nvEncInitializeEncoder
传入NV_ENC_INITIALIZE_PARAMS
类型的实例,对编码器进行初始化。其中encodeGUID
、encodeWidth
、encodeHeight
三项是必填选项,encodeGUID
表示使用H264编码(NV_ENC_CODEC_H264_GUID
)还是H265(NV_ENC_CODEC_HEVC_GUID
)编码。另外,还可以在此设置使用同步编码还是异步编码模式。
nvEncGetEncodePresetConfig
可以获取预置参数NV_ENC_PRESET_CONFIG
。可以设置码率控制模式、平均码率等数据。
NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } };
m_funList.nvEncGetEncodePresetConfig(m_encoder, NV_ENC_CODEC_H264_GUID, NV_ENC_PRESET_P3_GUID, &presetConfig);
NV_ENC_CONFIG config = { NV_ENC_CONFIG_VER };
memcpy(&config, &presetConfig.presetCfg, sizeof(config));
config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
config.rcParams.averageBitRate = 25000;
config.gopLength = 10;
NV_ENC_INITIALIZE_PARAMS encoder_init_params = { NV_ENC_INITIALIZE_PARAMS_VER };
encoder_init_params.encodeConfig = &config;
encoder_init_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
encoder_init_params.presetGUID = NV_ENC_PRESET_P3_GUID;
encoder_init_params.tuningInfo = NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY;
encoder_init_params.encodeWidth = m_encodeWidth;
encoder_init_params.encodeHeight = m_encodeHeight;
encoder_init_params.darWidth = m_encodeWidth;
encoder_init_params.darHeight = m_encodeHeight;
encoder_init_params.frameRateNum = m_frameRate; // 帧率
encoder_init_params.frameRateDen = 1;
encoder_init_params.enablePTD = 1;
encoder_init_params.maxEncodeWidth = m_encodeWidth;
encoder_init_params.maxEncodeHeight = m_encodeHeight;
encoder_init_params.enableEncodeAsync = 0; // 同步模式
NVENC_API_CALL(m_funList.nvEncInitializeEncoder(m_encoder, &encoder_init_params));
注册输入资源
将输入的纹理指针注册成输入资源,即m_registerResource.resourceToRegister = m_inputTexture;
NV_ENC_REGISTER_RESOURCE m_registerResource;
m_registerResource.version = NV_ENC_REGISTER_RESOURCE_VER;
m_registerResource.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
m_registerResource.resourceToRegister = m_inputTexture;
m_registerResource.width = m_encodeWidth;
m_registerResource.height = m_encodeHeight;
m_registerResource.pitch = 0;
m_registerResource.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12;
m_registerResource.bufferUsage = NV_ENC_INPUT_IMAGE;
m_registerResource.pInputFencePoint = NULL;
m_registerResource.pOutputFencePoint = NULL;
NVENC_API_CALL(m_funList.nvEncRegisterResource(m_encoder, &m_registerResource));
映射注册的输入资源
将注册的输入资源映射到编码器。m_inputMapResource.registeredResource = m_registerResource.registeredResource;
NV_ENC_MAP_INPUT_RESOURCE m_inputMapResource;
m_inputMapResource.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
m_inputMapResource.registeredResource = m_registerResource.registeredResource;
NVENC_API_CALL(m_funList.nvEncMapInputResource(m_encoder, &m_inputMapResource));
创建输出比特流缓冲
创建接收编码出的比特流缓冲。
NV_ENC_CREATE_BITSTREAM_BUFFER m_outputBuffer;
m_outputBuffer.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
NVENC_API_CALL(m_funList.nvEncCreateBitstreamBuffer(m_encoder, &m_outputBuffer));
编码一帧
在编码前需要更新输入纹理数据,即前面已经注册的纹理指针m_inputTexture
,更新后,填充NV_ENC_PIC_PARAMS
结构,将NV_ENC_PIC_PARAMS.inputBuffer
赋值为前面映射的资源,即m_inputMapResource.mappedResource
,再将NV_ENC_PIC_PARAMS.outputBitstream
赋值为前面创建的输出比特流缓冲,即m_outputBuffer.bitstreamBuffer
,然后调用nvEncEncodePicture
对数据进行编码。
NV_ENC_PIC_PARAMS picParams = { 0 };
picParams.version = NV_ENC_PIC_PARAMS_VER;
picParams.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
picParams.inputBuffer = m_inputMapResource.mappedResource;
picParams.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12;
picParams.inputWidth = m_encodeWidth;
picParams.inputHeight = m_encodeHeight;
picParams.outputBitstream = m_outputBuffer.bitstreamBuffer;
picParams.inputTimeStamp = 0;
NVENC_API_CALL(m_funList.nvEncEncodePicture(m_encoder, &picParams));
获取输出
使用nvEncLockBitstream
将数据从编码器中取出,即GPU到CPU。
NV_ENC_LOCK_BITSTREAM lockBitstreamData = { NV_ENC_LOCK_BITSTREAM_VER };
lockBitstreamData.outputBitstream = m_outputBuffer.bitstreamBuffer;
lockBitstreamData.doNotWait = 0;
NVENC_API_CALL(m_funList.nvEncLockBitstream(m_encoder, &lockBitstreamData));
unsigned char* outData = NULL;
int dataSize = lockBitstreamData.bitstreamSizeInBytes;
outData = new unsigned char[dataSize];
memcpy(outData, lockBitstreamData.bitstreamBufferPtr, dataSize);
NVENC_API_CALL(m_funList.nvEncUnlockBitstream(m_encoder, lockBitstreamData.outputBitstream));
``