nvenc: support for changing framerate at runtime

This commit is contained in:
aomiki
2025-10-29 19:19:22 +01:00
committed by xytovl
parent 1635bdabce
commit 3c91e4eca8
2 changed files with 41 additions and 14 deletions
+38 -13
View File
@@ -130,16 +130,22 @@ static void check_profile_guid_supported(std::shared_ptr<video_encoder_nvenc_sha
} }
} }
NV_ENC_RC_PARAMS video_encoder_nvenc::get_rc_params(uint64_t bitrate) NV_ENC_RC_PARAMS video_encoder_nvenc::get_rc_params(uint64_t bitrate, float framerate)
{ {
return { return {
.rateControlMode = NV_ENC_PARAMS_RC_CBR, .rateControlMode = NV_ENC_PARAMS_RC_CBR,
.averageBitRate = static_cast<uint32_t>(bitrate), .averageBitRate = static_cast<uint32_t>(bitrate),
.vbvBufferSize = static_cast<uint32_t>(bitrate / fps), .vbvBufferSize = static_cast<uint32_t>(bitrate / framerate),
.vbvInitialDelay = static_cast<uint32_t>(bitrate / fps), .vbvInitialDelay = static_cast<uint32_t>(bitrate / framerate),
.multiPass = NV_ENC_TWO_PASS_QUARTER_RESOLUTION}; .multiPass = NV_ENC_TWO_PASS_QUARTER_RESOLUTION};
} }
void video_encoder_nvenc::set_init_params_fps(float framerate)
{
init_params.frameRateNum = framerate;
init_params.frameRateDen = 1;
}
video_encoder_nvenc::video_encoder_nvenc( video_encoder_nvenc::video_encoder_nvenc(
wivrn_vk_bundle & vk, wivrn_vk_bundle & vk,
encoder_settings & settings, encoder_settings & settings,
@@ -148,7 +154,8 @@ video_encoder_nvenc::video_encoder_nvenc(
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true), video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, true),
vk(vk), vk(vk),
shared_state(video_encoder_nvenc_shared_state::get()), shared_state(video_encoder_nvenc_shared_state::get()),
fps(fps) fps(fps),
bitrate(settings.bitrate)
{ {
if (settings.bit_depth != 8 && settings.bit_depth != 10) if (settings.bit_depth != 8 && settings.bit_depth != 10)
throw std::runtime_error("nvenc encoder only supports 8-bit and 10-bit encoding"); throw std::runtime_error("nvenc encoder only supports 8-bit and 10-bit encoding");
@@ -194,7 +201,7 @@ video_encoder_nvenc::video_encoder_nvenc(
config = preset_config.presetCfg; config = preset_config.presetCfg;
// Bitrate control // Bitrate control
config.rcParams = get_rc_params(settings.bitrate); config.rcParams = get_rc_params(bitrate, fps);
config.gopLength = NVENC_INFINITE_GOPLENGTH; config.gopLength = NVENC_INFINITE_GOPLENGTH;
config.frameIntervalP = 1; config.frameIntervalP = 1;
@@ -276,12 +283,13 @@ video_encoder_nvenc::video_encoder_nvenc(
.encodeHeight = settings.video_height, .encodeHeight = settings.video_height,
.darWidth = settings.video_width, .darWidth = settings.video_width,
.darHeight = settings.video_height, .darHeight = settings.video_height,
.frameRateNum = (uint32_t)fps,
.frameRateDen = 1,
.enableEncodeAsync = 0, .enableEncodeAsync = 0,
.enablePTD = 1, .enablePTD = 1,
.encodeConfig = &config, .encodeConfig = &config,
.tuningInfo = tuningInfo}; .tuningInfo = tuningInfo};
set_init_params_fps(fps);
NVENC_CHECK(shared_state->fn.nvEncInitializeEncoder(session_handle, &init_params)); NVENC_CHECK(shared_state->fn.nvEncInitializeEncoder(session_handle, &init_params));
NV_ENC_CREATE_BITSTREAM_BUFFER out_buf_params{ NV_ENC_CREATE_BITSTREAM_BUFFER out_buf_params{
@@ -416,10 +424,23 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(bool idr, std::ch
{ {
CU_CHECK(shared_state->cuda_fn->cuCtxPushCurrent(shared_state->cuda)); CU_CHECK(shared_state->cuda_fn->cuCtxPushCurrent(shared_state->cuda));
if (auto bitrate = pending_bitrate.exchange(0)) auto new_bitrate = pending_bitrate.exchange(0);
auto new_framerate = pending_framerate.exchange(0);
if (new_bitrate || new_framerate)
{ {
NV_ENC_RC_PARAMS old_rc = config.rcParams; if (new_framerate)
config.rcParams = get_rc_params(bitrate); U_LOG_I("nvenc: reconfiguring framerate, new value: %f", new_framerate);
else
new_framerate = fps;
if (new_bitrate)
U_LOG_I("nvenc: reconfiguring bitrate, new value: %d", new_bitrate);
else
new_bitrate = bitrate;
config.rcParams = get_rc_params(new_bitrate, new_framerate);
set_init_params_fps(new_framerate);
NV_ENC_RECONFIGURE_PARAMS reconfig_params{ NV_ENC_RECONFIGURE_PARAMS reconfig_params{
.version = NV_ENC_RECONFIGURE_PARAMS_VER, .version = NV_ENC_RECONFIGURE_PARAMS_VER,
@@ -430,13 +451,17 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(bool idr, std::ch
try try
{ {
NVENC_CHECK(shared_state->fn.nvEncReconfigureEncoder(session_handle, &reconfig_params)); NVENC_CHECK(shared_state->fn.nvEncReconfigureEncoder(session_handle, &reconfig_params));
fps = new_framerate;
bitrate = new_bitrate;
idr = true; idr = true;
U_LOG_I("nvenc: reconfiguring bitrate succeeded, new value: %d", bitrate);
U_LOG_I("nvenc: reconfiguring succeeded.");
} }
catch (const std::exception & e) catch (const std::exception & e)
{ {
U_LOG_E("nvenc: failed to reconfigure bitrate to %d", bitrate); U_LOG_E("nvenc: reconfiguring failed.");
config.rcParams = old_rc; config.rcParams = get_rc_params(bitrate, fps);
set_init_params_fps(fps);
} }
} }
+3 -1
View File
@@ -53,9 +53,11 @@ private:
std::array<in_t, num_slots> in; std::array<in_t, num_slots> in;
float fps; float fps;
uint64_t bitrate;
int bytesPerPixel = 1; int bytesPerPixel = 1;
NV_ENC_RC_PARAMS get_rc_params(uint64_t bitrate); NV_ENC_RC_PARAMS get_rc_params(uint64_t bitrate, float framerate);
void set_init_params_fps(float framerate);
public: public:
video_encoder_nvenc(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx); video_encoder_nvenc(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx);