Get maximum size from nvenc capabilities
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
if (status != NV_ENC_SUCCESS) \
|
||||
{ \
|
||||
U_LOG_E("%s:%d: %d", __FILE__, __LINE__, status); \
|
||||
throw std::runtime_error("TODO"); \
|
||||
throw std::runtime_error("nvenc error"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
if (status != NV_ENC_SUCCESS) \
|
||||
{ \
|
||||
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, fn.nvEncGetLastErrorString(session_handle)); \
|
||||
throw std::runtime_error("TODO"); \
|
||||
throw std::runtime_error("nvenc error"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -73,37 +73,34 @@ void VideoEncoderNvenc::deleter::operator()(NvencFunctions * fn)
|
||||
nvenc_free_functions(&fn);
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, float fps) :
|
||||
vk(vk), fps(fps), bitrate(settings.bitrate)
|
||||
static auto init()
|
||||
{
|
||||
std::unique_ptr<CudaFunctions, VideoEncoderNvenc::deleter> cuda_fn;
|
||||
std::unique_ptr<NvencFunctions, VideoEncoderNvenc::deleter> nvenc_fn;
|
||||
void * session_handle;
|
||||
|
||||
{
|
||||
CudaFunctions * tmp = nullptr;
|
||||
if (cuda_load_functions(&tmp, nullptr))
|
||||
throw std::runtime_error("Failed to load CUDA");
|
||||
cuda_fn.reset(tmp);
|
||||
}
|
||||
|
||||
{
|
||||
NvencFunctions * tmp = nullptr;
|
||||
if (nvenc_load_functions(&tmp, nullptr))
|
||||
throw std::runtime_error("Failed to load nvenc");
|
||||
nvenc_fn.reset(tmp);
|
||||
}
|
||||
rect = vk::Rect2D{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
pitch = settings.video_width;
|
||||
|
||||
CU_CHECK(cuda_fn->cuInit(0));
|
||||
|
||||
CUcontext cuda;
|
||||
CU_CHECK(cuda_fn->cuCtxCreate(&cuda, 0, 0));
|
||||
|
||||
fn.version = NV_ENCODE_API_FUNCTION_LIST_VER;
|
||||
NV_ENCODE_API_FUNCTION_LIST fn{
|
||||
.version = NV_ENCODE_API_FUNCTION_LIST_VER,
|
||||
};
|
||||
NVENC_CHECK_NOENCODER(nvenc_fn->NvEncodeAPICreateInstance(&fn));
|
||||
|
||||
{
|
||||
@@ -120,9 +117,40 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
NVENC_CHECK_NOENCODER(fn.nvEncOpenEncodeSessionEx(¶ms, &session_handle));
|
||||
}
|
||||
|
||||
return std::make_tuple(std::move(cuda_fn), std::move(nvenc_fn), fn, cuda, session_handle);
|
||||
}
|
||||
|
||||
static auto encode_guid(video_codec codec)
|
||||
{
|
||||
switch (codec)
|
||||
{
|
||||
case h264:
|
||||
return NV_ENC_CODEC_H264_GUID;
|
||||
case h265:
|
||||
return NV_ENC_CODEC_HEVC_GUID;
|
||||
}
|
||||
throw std::out_of_range("Invalid codec " + std::to_string(codec));
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, float fps) :
|
||||
vk(vk), fps(fps), bitrate(settings.bitrate)
|
||||
{
|
||||
std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init();
|
||||
rect = vk::Rect2D{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
pitch = settings.video_width;
|
||||
|
||||
uint32_t count;
|
||||
std::vector<GUID> presets;
|
||||
auto encodeGUID = settings.codec == video_codec::h264 ? NV_ENC_CODEC_H264_GUID : NV_ENC_CODEC_HEVC_GUID;
|
||||
auto encodeGUID = encode_guid(settings.codec);
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetCount(session_handle, encodeGUID, &count));
|
||||
presets.resize(count);
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetGUIDs(session_handle, encodeGUID, presets.data(), count, &count));
|
||||
@@ -143,21 +171,17 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
printf(" Preset {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n", i.Data1, i.Data2, i.Data3, i.Data4[0], i.Data4[1], i.Data4[2], i.Data4[3], i.Data4[4], i.Data4[5], i.Data4[6], i.Data4[7]);
|
||||
}
|
||||
|
||||
NV_ENC_CAPS_PARAM cap_param{};
|
||||
cap_param.version = NV_ENC_CAPS_PARAM_VER;
|
||||
cap_param.capsToQuery = NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION;
|
||||
int cap_value;
|
||||
NVENC_CHECK(fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, &cap_value));
|
||||
|
||||
// auto presetGUID = codec == video_codec::h264 ? NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID :
|
||||
// NV_ENC_PRESET_P7_GUID;
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
auto presetGUID = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
|
||||
#pragma GCC diagnostic pop
|
||||
NV_ENC_PRESET_CONFIG preset_config;
|
||||
preset_config.version = NV_ENC_PRESET_CONFIG_VER;
|
||||
preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
|
||||
NV_ENC_PRESET_CONFIG preset_config{
|
||||
.version = NV_ENC_PRESET_CONFIG_VER,
|
||||
.presetCfg = {
|
||||
.version = NV_ENC_CONFIG_VER,
|
||||
}};
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetConfig(session_handle, encodeGUID, presetGUID, &preset_config));
|
||||
|
||||
NV_ENC_CONFIG params = preset_config.presetCfg;
|
||||
@@ -186,32 +210,25 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
break;
|
||||
}
|
||||
|
||||
NV_ENC_INITIALIZE_PARAMS params2{};
|
||||
params2.version = NV_ENC_INITIALIZE_PARAMS_VER;
|
||||
params2.encodeGUID = encodeGUID;
|
||||
params2.presetGUID = presetGUID;
|
||||
params2.encodeWidth = settings.video_width;
|
||||
params2.encodeHeight = settings.video_height;
|
||||
params2.darWidth = settings.video_width;
|
||||
params2.darHeight = settings.video_height;
|
||||
params2.frameRateNum = (uint32_t)fps;
|
||||
params2.frameRateDen = 1;
|
||||
params2.enableEncodeAsync = 0;
|
||||
params2.enablePTD = 1;
|
||||
params2.reportSliceOffsets = 0;
|
||||
params2.enableSubFrameWrite = 0;
|
||||
params2.enableExternalMEHints = 0;
|
||||
params2.enableMEOnlyMode = 0;
|
||||
params2.enableWeightedPrediction = 0;
|
||||
params2.enableOutputInVidmem = 0;
|
||||
params2.encodeConfig = ¶ms;
|
||||
params2.maxEncodeWidth = 0;
|
||||
params2.maxEncodeHeight = 0;
|
||||
params2.bufferFormat = NV_ENC_BUFFER_FORMAT_UNDEFINED;
|
||||
NV_ENC_INITIALIZE_PARAMS params2{
|
||||
.version = NV_ENC_INITIALIZE_PARAMS_VER,
|
||||
.encodeGUID = encodeGUID,
|
||||
.presetGUID = presetGUID,
|
||||
.encodeWidth = settings.video_width,
|
||||
.encodeHeight = settings.video_height,
|
||||
.darWidth = settings.video_width,
|
||||
.darHeight = settings.video_height,
|
||||
.frameRateNum = (uint32_t)fps,
|
||||
.frameRateDen = 1,
|
||||
.enableEncodeAsync = 0,
|
||||
.enablePTD = 1,
|
||||
.encodeConfig = ¶ms,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncInitializeEncoder(session_handle, ¶ms2));
|
||||
|
||||
NV_ENC_CREATE_BITSTREAM_BUFFER params3;
|
||||
params3.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
|
||||
NV_ENC_CREATE_BITSTREAM_BUFFER params3{
|
||||
.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncCreateBitstreamBuffer(session_handle, ¶ms3));
|
||||
bitstreamBuffer = params3.bitstreamBuffer;
|
||||
|
||||
@@ -250,6 +267,7 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
.handleType = vk::ExternalMemoryHandleTypeFlagBitsKHR::eOpaqueFd,
|
||||
});
|
||||
|
||||
CU_CHECK(cuda_fn->cuCtxPushCurrent(cuda));
|
||||
{
|
||||
CUDA_EXTERNAL_MEMORY_HANDLE_DESC param{
|
||||
.type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
|
||||
@@ -267,20 +285,27 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
CU_CHECK(cuda_fn->cuExternalMemoryGetMappedBuffer(&frame, extmem, &map_param));
|
||||
}
|
||||
|
||||
NV_ENC_REGISTER_RESOURCE param3{};
|
||||
param3.version = NV_ENC_REGISTER_RESOURCE_VER;
|
||||
param3.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
|
||||
param3.width = rect.extent.width;
|
||||
param3.height = rect.extent.height;
|
||||
param3.pitch = pitch;
|
||||
param3.resourceToRegister = (void *)frame;
|
||||
param3.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12;
|
||||
param3.bufferUsage = NV_ENC_INPUT_IMAGE;
|
||||
NV_ENC_REGISTER_RESOURCE param3{
|
||||
.version = NV_ENC_REGISTER_RESOURCE_VER,
|
||||
.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR,
|
||||
.width = rect.extent.width,
|
||||
.height = rect.extent.height,
|
||||
.pitch = pitch,
|
||||
.resourceToRegister = (void *)frame,
|
||||
.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12,
|
||||
.bufferUsage = NV_ENC_INPUT_IMAGE,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncRegisterResource(session_handle, ¶m3));
|
||||
nvenc_resource = param3.registeredResource;
|
||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::~VideoEncoderNvenc()
|
||||
{
|
||||
if (session_handle)
|
||||
fn.nvEncDestroyEncoder(session_handle);
|
||||
}
|
||||
|
||||
void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf)
|
||||
{
|
||||
cmd_buf.copyImageToBuffer(
|
||||
@@ -288,7 +313,7 @@ void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandB
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
*yuv_buffer,
|
||||
vk::BufferImageCopy{
|
||||
.bufferRowLength = uint32_t(pitch),
|
||||
.bufferRowLength = pitch,
|
||||
.imageSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.layerCount = 1,
|
||||
@@ -334,32 +359,69 @@ void VideoEncoderNvenc::Encode(bool idr, std::chrono::steady_clock::time_point p
|
||||
param4.registeredResource = nvenc_resource;
|
||||
NVENC_CHECK(fn.nvEncMapInputResource(session_handle, ¶m4));
|
||||
|
||||
NV_ENC_PIC_PARAMS param{};
|
||||
param.version = NV_ENC_PIC_PARAMS_VER;
|
||||
param.inputWidth = rect.extent.width;
|
||||
param.inputHeight = rect.extent.height;
|
||||
param.inputPitch = pitch;
|
||||
param.encodePicFlags = (uint32_t)(idr ? NV_ENC_PIC_TYPE_IDR : NV_ENC_PIC_TYPE_P);
|
||||
param.frameIdx = 0;
|
||||
param.inputTimeStamp = 0;
|
||||
param.inputBuffer = param4.mappedResource;
|
||||
param.outputBitstream = bitstreamBuffer;
|
||||
param.bufferFmt = param4.mappedBufferFmt;
|
||||
param.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
|
||||
NV_ENC_PIC_PARAMS param{
|
||||
.version = NV_ENC_PIC_PARAMS_VER,
|
||||
.inputWidth = rect.extent.width,
|
||||
.inputHeight = rect.extent.height,
|
||||
.inputPitch = pitch,
|
||||
.encodePicFlags = uint32_t(idr ? NV_ENC_PIC_TYPE_IDR : NV_ENC_PIC_TYPE_P),
|
||||
.frameIdx = 0,
|
||||
.inputTimeStamp = 0,
|
||||
.inputBuffer = param4.mappedResource,
|
||||
.outputBitstream = bitstreamBuffer,
|
||||
.bufferFmt = param4.mappedBufferFmt,
|
||||
.pictureStruct = NV_ENC_PIC_STRUCT_FRAME,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncEncodePicture(session_handle, ¶m));
|
||||
|
||||
NV_ENC_LOCK_BITSTREAM param2{};
|
||||
param2.version = NV_ENC_LOCK_BITSTREAM_VER;
|
||||
param2.doNotWait = 0;
|
||||
param2.outputBitstream = bitstreamBuffer;
|
||||
NV_ENC_LOCK_BITSTREAM param2{
|
||||
.version = NV_ENC_LOCK_BITSTREAM_VER,
|
||||
.doNotWait = 0,
|
||||
.outputBitstream = bitstreamBuffer,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncLockBitstream(session_handle, ¶m2));
|
||||
|
||||
SendData(
|
||||
{(uint8_t *)param2.bitstreamBufferPtr, (uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes}, true);
|
||||
SendData({
|
||||
(uint8_t *)param2.bitstreamBufferPtr,
|
||||
(uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes,
|
||||
},
|
||||
true);
|
||||
|
||||
NVENC_CHECK(fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer));
|
||||
|
||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
||||
}
|
||||
|
||||
std::array<int, 2> VideoEncoderNvenc::get_max_size(video_codec codec)
|
||||
{
|
||||
auto [cuda_fn, nvenc_fn, fn, cuda, session_handle] = init();
|
||||
std::array<int, 2> result;
|
||||
std::exception_ptr ex;
|
||||
try
|
||||
{
|
||||
auto encodeGUID = encode_guid(codec);
|
||||
for (auto [cap, res]: {
|
||||
std::pair{NV_ENC_CAPS_WIDTH_MAX, &result[0]},
|
||||
{NV_ENC_CAPS_WIDTH_MAX, &result[1]},
|
||||
})
|
||||
{
|
||||
NV_ENC_CAPS_PARAM cap_param{
|
||||
.version = NV_ENC_CAPS_PARAM_VER,
|
||||
.capsToQuery = NV_ENC_CAPS_WIDTH_MAX,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, res));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ex = std::current_exception();
|
||||
}
|
||||
cuda_fn->cuCtxPopCurrent(NULL);
|
||||
fn.nvEncDestroyEncoder(session_handle);
|
||||
if (ex)
|
||||
std::rethrow_exception(ex);
|
||||
U_LOG_D("nvenc maximum encoded size: %dx%d", result[0], result[1]);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
Reference in New Issue
Block a user