Skip to content

Commit

Permalink
Avoid unnecessary encoder probing during startup and launch
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Apr 16, 2023
1 parent 8f74c3b commit d973e5b
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions src/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ namespace video {
enum flag_e {
PASSED, // Is supported
REF_FRAMES_RESTRICT, // Set maximum reference frames
REF_FRAMES_AUTOSELECT, // Allow encoder to select maximum reference frames (If !REF_FRAMES_RESTRICT --> REF_FRAMES_AUTOSELECT)
SLICE, // Allow frame to be partitioned into multiple slices
CBR, // Some encoders don't support CBR, if not supported --> attempt constant quantatication parameter instead
DYNAMIC_RANGE, // hdr
VUI_PARAMETERS, // AMD encoder with VAAPI doesn't add VUI parameters to SPS
Expand All @@ -294,8 +292,6 @@ namespace video {
switch (flag) {
_CONVERT(PASSED);
_CONVERT(REF_FRAMES_RESTRICT);
_CONVERT(REF_FRAMES_AUTOSELECT);
_CONVERT(SLICE);
_CONVERT(CBR);
_CONVERT(DYNAMIC_RANGE);
_CONVERT(VUI_PARAMETERS);
Expand Down Expand Up @@ -1222,7 +1218,7 @@ namespace video {
ctx->slices = std::max(config.slicesPerFrame, config::video.min_threads);
}

if (!video_format[encoder_t::SLICE]) {
if (encoder.flags & SINGLE_SLICE_ONLY) {
ctx->slices = 1;
}

Expand Down Expand Up @@ -1870,17 +1866,14 @@ namespace video {
encoder.h264.capabilities.set();
encoder.hevc.capabilities.set();

encoder.hevc[encoder_t::PASSED] = test_hevc;

// First, test encoder viability
config_t config_max_ref_frames { 1920, 1080, 60, 1000, 1, 1, 1, 0, 0 };
config_t config_autoselect { 1920, 1080, 60, 1000, 1, 0, 1, 0, 0 };

retry:
auto max_ref_frames_h264 = validate_config(disp, encoder, config_max_ref_frames);
auto autoselect_h264 = validate_config(disp, encoder, config_autoselect);

if (max_ref_frames_h264 < 0 && autoselect_h264 < 0) {
auto autoselect_h264 = max_ref_frames_h264 >= 0 ? max_ref_frames_h264 : validate_config(disp, encoder, config_autoselect);
if (autoselect_h264 < 0) {
if (encoder.h264.qp && encoder.h264[encoder_t::CBR]) {
// It's possible the encoder isn't accepting Constant Bit Rate. Turn off CBR and make another attempt
encoder.h264.capabilities.set();
Expand All @@ -1900,27 +1893,24 @@ namespace video {
}

encoder.h264[encoder_t::REF_FRAMES_RESTRICT] = max_ref_frames_h264 >= 0;
encoder.h264[encoder_t::REF_FRAMES_AUTOSELECT] = autoselect_h264 >= 0;
encoder.h264[encoder_t::PASSED] = true;

encoder.h264[encoder_t::SLICE] = validate_config(disp, encoder, config_max_ref_frames);
if (test_hevc) {
config_max_ref_frames.videoFormat = 1;
config_autoselect.videoFormat = 1;

retry_hevc:
auto max_ref_frames_hevc = validate_config(disp, encoder, config_max_ref_frames);
auto autoselect_hevc = validate_config(disp, encoder, config_autoselect);

// If HEVC must be supported, but it is not supported
if (max_ref_frames_hevc < 0 && autoselect_hevc < 0) {
auto autoselect_hevc = max_ref_frames_hevc >= 0 ? max_ref_frames_hevc : validate_config(disp, encoder, config_autoselect);
if (autoselect_hevc < 0) {
if (encoder.hevc.qp && encoder.hevc[encoder_t::CBR]) {
// It's possible the encoder isn't accepting Constant Bit Rate. Turn off CBR and make another attempt
encoder.hevc.capabilities.set();
encoder.hevc[encoder_t::CBR] = false;
goto retry_hevc;
}

// If HEVC must be supported, but it is not supported
if (force_hevc) {
return false;
}
Expand All @@ -1931,20 +1921,13 @@ namespace video {
}

encoder.hevc[encoder_t::REF_FRAMES_RESTRICT] = max_ref_frames_hevc >= 0;
encoder.hevc[encoder_t::REF_FRAMES_AUTOSELECT] = autoselect_hevc >= 0;

encoder.hevc[encoder_t::PASSED] = max_ref_frames_hevc >= 0 || autoselect_hevc >= 0;
}

std::vector<std::pair<encoder_t::flag_e, config_t>> configs {
{ encoder_t::DYNAMIC_RANGE, { 1920, 1080, 60, 1000, 1, 0, 3, 1, 1 } },
};

if (!(encoder.flags & SINGLE_SLICE_ONLY)) {
configs.emplace_back(
std::pair<encoder_t::flag_e, config_t> { encoder_t::SLICE, { 1920, 1080, 60, 1000, 2, 1, 1, 0, 0 } });
}

for (auto &[flag, config] : configs) {
auto h264 = config;
auto hevc = config;
Expand All @@ -1960,11 +1943,6 @@ namespace video {
}
}

if (encoder.flags & SINGLE_SLICE_ONLY) {
encoder.h264.capabilities[encoder_t::SLICE] = false;
encoder.hevc.capabilities[encoder_t::SLICE] = false;
}

encoder.h264[encoder_t::VUI_PARAMETERS] = encoder.h264[encoder_t::VUI_PARAMETERS] && !config::sunshine.flags[config::flag::FORCE_VIDEO_HEADER_REPLACE];
encoder.hevc[encoder_t::VUI_PARAMETERS] = encoder.hevc[encoder_t::VUI_PARAMETERS] && !config::sunshine.flags[config::flag::FORCE_VIDEO_HEADER_REPLACE];

Expand Down

0 comments on commit d973e5b

Please sign in to comment.