diff --git a/src/amf/amf_avcodec_compat.cpp b/src/amf/amf_avcodec_compat.cpp index 8ee82849e..1574dfade 100644 --- a/src/amf/amf_avcodec_compat.cpp +++ b/src/amf/amf_avcodec_compat.cpp @@ -442,11 +442,25 @@ namespace amf { return { async_depth, - true, configure_result, }; } + int + amf_avcodec_compat::rate_control_h264(const amf_config &config, const video::config_t &client_config) { + return auto_rc_h264(config, make_avcodec_context_like(client_config)); + } + + int + amf_avcodec_compat::rate_control_hevc(const amf_config &config, const video::config_t &client_config) { + return auto_rc_hevc(config, make_avcodec_context_like(client_config)); + } + + int + amf_avcodec_compat::rate_control_av1(const amf_config &config, const video::config_t &client_config) { + return auto_rc_av1(config, make_avcodec_context_like(client_config)); + } + int64_t amf_avcodec_compat::vbv_buffer_size(int bitrate_kbps, const video::config_t &client_config) { const auto bitrate = static_cast(bitrate_kbps) * 1000; diff --git a/src/amf/amf_avcodec_compat.h b/src/amf/amf_avcodec_compat.h index 83cee1f77..a252bbd9d 100644 --- a/src/amf/amf_avcodec_compat.h +++ b/src/amf/amf_avcodec_compat.h @@ -19,7 +19,6 @@ namespace amf { struct amf_avcodec_compat_result { int hwsurfaces_in_queue_max = 16; - bool manages_rate_control = true; AMF_RESULT result = AMF_OK; }; @@ -77,6 +76,20 @@ namespace amf { const video::config_t &client_config, const video::sunshine_colorspace_t &colorspace); + // Shared rate-control derivation used by BOTH encoder paths (the standalone + // path and the avcodec-compatible one): honor an explicit rc_mode, otherwise + // pin CBR. Client requests always carry peak == target, so the soft-target + // driver default (Latency Constrained VBR) must never be left in charge — + // it lets short-term bursts exceed the client's bitrate by a wide margin. + static int + rate_control_h264(const amf_config &config, const video::config_t &client_config); + + static int + rate_control_hevc(const amf_config &config, const video::config_t &client_config); + + static int + rate_control_av1(const amf_config &config, const video::config_t &client_config); + static int64_t vbv_buffer_size(int bitrate_kbps, const video::config_t &client_config); }; diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 26263f03b..419e010dd 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -135,8 +135,13 @@ namespace amf { auto bitrate = static_cast(client_config.bitrate) * 1000; auto framerate = AMFConstructRate(client_config.framerate, 1); avcodec_compat_profile = config.avcodec_compat; - user_configured_rate_control = config.rc_mode.has_value(); hwsurfaces_in_queue_max = HWSURFACES_IN_QUEUE_DEFAULT; + // Rate control is always pinned — explicitly chosen amd_rc, otherwise CBR — + // through amf_avcodec_compat::rate_control_*() plus the peak/VBV properties + // set below, identically on the standalone and avcodec-compatible paths. + // Leaving the AMF driver default (Latency Constrained VBR) in charge lets + // short-term bursts exceed the client's bitrate target by a wide margin: + // the encoder must never run without a hard constraint. auto configure_multi_hw_instance = [&](const wchar_t *multi_hw_property, const wchar_t *sav_property, @@ -262,7 +267,6 @@ namespace amf { return false; } hwsurfaces_in_queue_max = compat.hwsurfaces_in_queue_max; - user_configured_rate_control = compat.manages_rate_control; if (video_format == 0) { configure_multi_hw_instance( @@ -293,12 +297,12 @@ namespace amf { // H.264 if (config.usage) encoder->SetProperty(AMF_VIDEO_ENCODER_USAGE, (amf_int64) *config.usage); if (config.quality_preset) encoder->SetProperty(AMF_VIDEO_ENCODER_QUALITY_PRESET, (amf_int64) *config.quality_preset); - if (config.rc_mode) encoder->SetProperty(AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD, (amf_int64) *config.rc_mode); + encoder->SetProperty(AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD, + (amf_int64) amf_avcodec_compat::rate_control_h264(config, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE, bitrate); - } + encoder->SetProperty(AMF_VIDEO_ENCODER_PEAK_BITRATE, bitrate); + encoder->SetProperty(AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE, + amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_FRAMERATE, framerate); if (config.enforce_hrd) encoder->SetProperty(AMF_VIDEO_ENCODER_ENFORCE_HRD, !!(*config.enforce_hrd)); encoder->SetProperty(AMF_VIDEO_ENCODER_IDR_PERIOD, (amf_int64) 0); @@ -378,12 +382,12 @@ namespace amf { // HEVC if (config.usage) encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_USAGE, (amf_int64) *config.usage); if (config.quality_preset) encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_QUALITY_PRESET, (amf_int64) *config.quality_preset); - if (config.rc_mode) encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD, (amf_int64) *config.rc_mode); + encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD, + (amf_int64) amf_avcodec_compat::rate_control_hevc(config, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE, bitrate); - } + encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, bitrate); + encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE, + amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_FRAMERATE, framerate); if (config.enforce_hrd) encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_ENFORCE_HRD, !!(*config.enforce_hrd)); encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_NUM_GOPS_PER_IDR, (amf_int64) 1); @@ -456,12 +460,12 @@ namespace amf { // AV1 if (config.usage) encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_USAGE, (amf_int64) *config.usage); if (config.quality_preset) encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_QUALITY_PRESET, (amf_int64) *config.quality_preset); - if (config.rc_mode) encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD, (amf_int64) *config.rc_mode); + encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD, + (amf_int64) amf_avcodec_compat::rate_control_av1(config, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE, bitrate); - } + encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, bitrate); + encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE, + amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config)); encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_FRAMERATE, framerate); if (config.enforce_hrd) encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_ENFORCE_HRD, !!(*config.enforce_hrd)); encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE, (amf_int64) AMF_VIDEO_ENCODER_AV1_ALIGNMENT_MODE_NO_RESTRICTIONS); @@ -1380,43 +1384,91 @@ namespace amf { return true; } + // Dynamic bitrate update. Returns true only when the complete budget — + // target and peak, plus VBV where the codec accepts it at runtime — was + // accepted by the driver. false means the update never wrote anything + // (snapshot failure) or was rolled back to the snapshot; the sole residual + // case is a rollback write itself being rejected, which can leave a mixed + // budget behind — the caller keeps its previous allowance either way, but a + // session in that state should be recreated rather than re-driven. bool amf_d3d11::set_bitrate(int bitrate_kbps) { if (!encoder || bitrate_kbps <= 0) return false; auto bitrate = static_cast(bitrate_kbps) * 1000; - auto vbv_size = avcodec_compat_profile ? amf_avcodec_compat::vbv_buffer_size(bitrate_kbps, current_config) : bitrate; - AMF_RESULT res; - if (video_format == 0) { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE, vbv_size); - } - } - else if (video_format == 1) { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE, vbv_size); - } + // One row per codec: 0 = H.264, 1 = HEVC, 2 = AV1. AMF 1.5.3 classifies + // the HEVC VBV size as static — settable only before Init() — so its + // runtime entry stays empty while H.264 and AV1 list it as dynamic. A + // runtime write on HEVC stores the value without ever applying it; the + // session keeps the budget chosen at Init until it is recreated. For CBR + // (peak == target) the VBV size is only a smoothing window, so a stale + // window after an HEVC bitrate change stays coherent. + struct budget_props_t { + const wchar_t *target; + const wchar_t *peak; + const wchar_t *vbv; + }; + constexpr budget_props_t budget_props[] = { + { AMF_VIDEO_ENCODER_TARGET_BITRATE, AMF_VIDEO_ENCODER_PEAK_BITRATE, AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE }, + { AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, nullptr }, + { AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE, AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE }, + }; + // Map an out-of-range video_format to the H.264 row, matching the + // get_codec_id() default branch, so the table lookup can never go OOB. + const auto &props = budget_props[video_format >= 0 && video_format <= 2 ? video_format : 0]; + auto vbv_size = props.vbv ? amf_avcodec_compat::vbv_buffer_size(bitrate_kbps, current_config) : + int64_t { 0 }; + + // The trio is one rate-control budget: snapshot the current values first + // so a rejected write can be rolled back and the encoder never sits in a + // half-updated state (target on the new budget, peak/VBV on the old one). + amf_int64 old_target = 0; + amf_int64 old_peak = 0; + amf_int64 old_vbv = 0; + bool snapshot_ok = encoder->GetProperty(props.target, &old_target) == AMF_OK && + encoder->GetProperty(props.peak, &old_peak) == AMF_OK; + if (props.vbv) { + snapshot_ok = snapshot_ok && encoder->GetProperty(props.vbv, &old_vbv) == AMF_OK; + } + if (!snapshot_ok) { + // Without a snapshot there is no way to restore the old budget if a + // write lands but a later one is rejected. Refuse to write at all: the + // caller retains its previous allowance and retries on a later budget + // tick, and the encoder stays exactly as it was. + BOOST_LOG(warning) << "AMF: could not snapshot the current rate-control budget; skipping the bitrate update to " + << bitrate_kbps << " Kbps"; + return false; } - else { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE, bitrate); - if (user_configured_rate_control) { - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE, vbv_size); + + const auto target_res = encoder->SetProperty(props.target, bitrate); + const auto peak_res = encoder->SetProperty(props.peak, bitrate); + const auto vbv_res = props.vbv ? encoder->SetProperty(props.vbv, vbv_size) : AMF_OK; + + if (target_res != AMF_OK || peak_res != AMF_OK || vbv_res != AMF_OK) { + BOOST_LOG(warning) << "AMF: set_bitrate(" << bitrate_kbps << " Kbps) rejected by driver" + << " (target: " << target_res << ", peak: " << peak_res << ", vbv: " << vbv_res << ")"; + // Roll back whatever landed: an unapplied property keeps its snapshot + // value, so only the writes that took effect need restoring. + auto rollback = [&](const char *what, const wchar_t *prop, amf_int64 value, AMF_RESULT write_res) { + if (write_res != AMF_OK) return; + const auto res = encoder->SetProperty(prop, value); + if (res != AMF_OK) { + BOOST_LOG(error) << "AMF: failed to roll back " << what + << ", error: " << res + << " — the encoder may keep a mixed budget; recreate the session if this repeats"; + } + }; + rollback("target bitrate", props.target, old_target, target_res); + rollback("peak bitrate", props.peak, old_peak, peak_res); + if (props.vbv) { + rollback("VBV budget", props.vbv, old_vbv, vbv_res); } + return false; } - if (res == AMF_OK) { - BOOST_LOG(info) << "AMF: bitrate dynamically changed to " << bitrate_kbps << " Kbps"; - } - else { - BOOST_LOG(warning) << "AMF: set_bitrate failed, error: " << res; - } - return res == AMF_OK; + BOOST_LOG(info) << "AMF: bitrate dynamically changed to " << bitrate_kbps << " Kbps"; + return true; } void diff --git a/src/amf/amf_d3d11.h b/src/amf/amf_d3d11.h index cf4dee7e2..992478c74 100644 --- a/src/amf/amf_d3d11.h +++ b/src/amf/amf_d3d11.h @@ -162,7 +162,6 @@ namespace amf { int hwsurfaces_in_queue = 0; static constexpr int HWSURFACES_IN_QUEUE_DEFAULT = 16; int hwsurfaces_in_queue_max = HWSURFACES_IN_QUEUE_DEFAULT; - bool user_configured_rate_control = false; bool avcodec_compat_profile = false; amf_avcodec_scheduler avcodec_scheduler; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3f94a998a..5f742a765 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -576,6 +576,28 @@ if (WIN32) endif () add_test(NAME config_runtime_unit_tests COMMAND config_runtime_unit_tests) +# AMF rate-control derivation shared by both encoder paths (standalone and +# avcodec-compatible). A regression here is what let the driver's soft-target +# default run unconstrained and blow past the client's bitrate target. +# Windows-only: the AMF headers staged with the AMF/ prefix exist on Windows +# alone, and sunshine::host_headers does not carry them on other platforms. +if (WIN32) + add_executable(amf_rate_control_unit_tests + ${CMAKE_SOURCE_DIR}/tests/unit/test_amf_rate_control.cpp + ${CMAKE_SOURCE_DIR}/tests/tools/amf_rate_control_test_stubs.cpp + ${CMAKE_SOURCE_DIR}/src/amf/amf_avcodec_compat.cpp) + target_include_directories(amf_rate_control_unit_tests PRIVATE "${CMAKE_SOURCE_DIR}") + target_link_libraries(amf_rate_control_unit_tests PRIVATE gtest_main Boost::boost Boost::log Boost::format + sunshine::host_headers sunshine_project_options sunshine_coverage) + set_target_properties(amf_rate_control_unit_tests PROPERTIES CXX_STANDARD 23) + target_compile_definitions(amf_rate_control_unit_tests PRIVATE ${SUNSHINE_DEFINITIONS}) + set_target_properties(amf_rate_control_unit_tests PROPERTIES LINK_SEARCH_START_STATIC 1) + # windows.cmake stages AMF headers here for the host build; the compat + # adapter and the test both include them. + target_include_directories(amf_rate_control_unit_tests SYSTEM PRIVATE "${CMAKE_BINARY_DIR}/amf_include") + add_test(NAME amf_rate_control_unit_tests COMMAND amf_rate_control_unit_tests) +endif () + # The configuration-upgrade suite runs on its own so the global defaults it # compares against are the ones a start-up has before any settings file is read. add_executable(config_upgrade_unit_tests @@ -1408,6 +1430,9 @@ list(REMOVE_ITEM TEST_SOURCES ${CMAKE_SOURCE_DIR}/tests/unit/test_uac1_microphone.cpp ${CMAKE_SOURCE_DIR}/tests/unit/test_video_probe.cpp ${CMAKE_SOURCE_DIR}/tests/unit/test_virtual_touchscreen_device.cpp + # test_amf_rate_control pulls boost headers transitively via src/video.h; + # it runs in its own Windows-only target, not the aggregate. + ${CMAKE_SOURCE_DIR}/tests/unit/test_amf_rate_control.cpp ${CMAKE_SOURCE_DIR}/tests/unit/pairing_pending_platform_stubs.cpp) set(ABR_TEST_STUB_SOURCE ${CMAKE_SOURCE_DIR}/tests/unit/abr_test_stubs.cpp) diff --git a/tests/tools/amf_rate_control_test_stubs.cpp b/tests/tools/amf_rate_control_test_stubs.cpp new file mode 100644 index 000000000..9ad5bdfc7 --- /dev/null +++ b/tests/tools/amf_rate_control_test_stubs.cpp @@ -0,0 +1,9 @@ +/** + * @file tests/tools/amf_rate_control_test_stubs.cpp + * @brief Minimal production logging symbols for the standalone AMF rate-control test. + */ +#include "src/logging_severity.h" + +boost::log::sources::severity_logger info(2); +boost::log::sources::severity_logger warning(3); +boost::log::sources::severity_logger error(4); diff --git a/tests/unit/test_amf_rate_control.cpp b/tests/unit/test_amf_rate_control.cpp new file mode 100644 index 000000000..c4b6f68c6 --- /dev/null +++ b/tests/unit/test_amf_rate_control.cpp @@ -0,0 +1,72 @@ +/** + * @file tests/unit/test_amf_rate_control.cpp + * @brief Locks the AMF rate-control derivation shared by both encoder paths + * (standalone and avcodec-compatible). An unset amd_rc must resolve to a hard + * CBR constraint — client requests always carry peak == target, and the + * driver's soft-target Latency Constrained VBR default otherwise lets bursts + * blow past the client's bitrate target — and an explicit amd_rc must pass + * through unchanged. The VBV budget stays at one frame of bitrate. + */ +#include "src/amf/amf_avcodec_compat.h" + +#include + +#include +#include +#include + +namespace { + + class amf_rate_control_test: public ::testing::Test { + protected: + video::config_t client_config; + + void + SetUp() override { + client_config = video::config_t {}; + client_config.framerate = 60; + client_config.bitrate = 20000; + } + }; + + TEST_F(amf_rate_control_test, unset_rc_defaults_to_cbr_on_every_codec) { + const amf::amf_config config; + + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_h264(config, client_config), AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_CBR); + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_hevc(config, client_config), AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_CBR); + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_av1(config, client_config), AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_CBR); + } + + TEST_F(amf_rate_control_test, explicit_rc_mode_passes_through) { + amf::amf_config config; + // display_vram.cpp resolves amd_rc_* into amf_config.rc_mode per codec, so + // each derivation must pass through its own codec's enumerator unchanged. + config.rc_mode = AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR; + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_h264(config, client_config), AMF_VIDEO_ENCODER_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR); + + config.rc_mode = AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR; + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_hevc(config, client_config), AMF_VIDEO_ENCODER_HEVC_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR); + + config.rc_mode = AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR; + EXPECT_EQ(amf::amf_avcodec_compat::rate_control_av1(config, client_config), AMF_VIDEO_ENCODER_AV1_RATE_CONTROL_METHOD_LATENCY_CONSTRAINED_VBR); + } + + TEST_F(amf_rate_control_test, vbv_budget_is_one_frame_of_bitrate) { + const auto expected = static_cast(client_config.bitrate) * 1000 / 60; + EXPECT_EQ(amf::amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config), expected); + + client_config.frameRateNum = 120; + client_config.frameRateDen = 1; + EXPECT_EQ(amf::amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config), + static_cast(client_config.bitrate) * 1000 / 120); + } + + TEST_F(amf_rate_control_test, vbv_budget_survives_degenerate_framerate) { + client_config.framerate = 0; + client_config.frameRateNum = 0; + + EXPECT_EQ(amf::amf_avcodec_compat::vbv_buffer_size(client_config.bitrate, client_config), + static_cast(client_config.bitrate) * 1000); + } + +} // namespace