From 2cc868f8be1d84a9193a0dee5493de6bcc3c2f22 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 14:37:28 +0800 Subject: [PATCH 1/9] fix(amf): pin default rate control on both encoder paths The standalone AMF path only set TARGET_BITRATE unless the user explicitly configured amd_rc (f754302e2), leaving the driver default (Latency Constrained VBR) in charge: a soft target with no peak or VBV constraint, so actual output easily exceeds the client's bitrate. The avcodec-compatible path always pinned CBR + peak + per-frame VBV, which is why enabling that toggle was reported to fix the overshoot on AMD. Extract the derivation into amf_avcodec_compat::rate_control_*() and use it on both paths: an explicit amd_rc passes through, otherwise CBR (client requests always carry peak == target). Peak and VBV are now set unconditionally, including set_bitrate for runtime ABR changes. Drop the user_configured_rate_control gate and the manages_rate_control field. New unit tests lock the derivation: an unset rc resolves to CBR per codec, explicit modes pass through unchanged, and the VBV budget stays at one frame of bitrate while surviving a degenerate framerate. --- src/amf/amf_avcodec_compat.cpp | 16 ++++- src/amf/amf_avcodec_compat.h | 15 ++++- src/amf/amf_d3d11.cpp | 58 ++++++++--------- src/amf/amf_d3d11.h | 1 - tests/CMakeLists.txt | 20 ++++++ tests/tools/amf_rate_control_test_stubs.cpp | 9 +++ tests/unit/test_amf_rate_control.cpp | 72 +++++++++++++++++++++ 7 files changed, 158 insertions(+), 33 deletions(-) create mode 100644 tests/tools/amf_rate_control_test_stubs.cpp create mode 100644 tests/unit/test_amf_rate_control.cpp 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..df3a2c603 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); @@ -1385,29 +1389,23 @@ namespace amf { 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; + auto vbv_size = amf_avcodec_compat::vbv_buffer_size(bitrate_kbps, current_config); 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); - } + 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); - } + encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, bitrate); + encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE, vbv_size); } 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); - } + encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, bitrate); + encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE, vbv_size); } if (res == AMF_OK) { 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..5395da9c0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -576,6 +576,26 @@ 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. +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 + 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}) +if (WIN32) + 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") +endif () +add_test(NAME amf_rate_control_unit_tests COMMAND amf_rate_control_unit_tests) + # 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 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 From 03c85a51b1ee67cef3c1dc42ddd34add5ada867a Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:11:44 +0800 Subject: [PATCH 2/9] fix(amf): guard runtime bitrate results; scope AMF tests to Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit set_bitrate() now checks every SetProperty result: a rejected runtime peak/VBV update is logged and reported instead of silently keeping the session's initial budget. amf_rate_control_unit_tests moves inside if(WIN32) — the AMF headers are staged on Windows alone — and the test file joins the aggregate's REMOVE_ITEM list: it pulls boost headers transitively via src/video.h, which is what broke the Windows CI build. --- src/amf/amf_d3d11.cpp | 46 +++++++++++++++++++++++++------------------ tests/CMakeLists.txt | 25 +++++++++++++---------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index df3a2c603..4e5f2a9d5 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1390,31 +1390,39 @@ namespace amf { auto bitrate = static_cast(bitrate_kbps) * 1000; auto vbv_size = amf_avcodec_compat::vbv_buffer_size(bitrate_kbps, current_config); - AMF_RESULT res; - if (video_format == 0) { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_TARGET_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE, vbv_size); + const wchar_t *target_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_TARGET_BITRATE : + (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE : + AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE; + const wchar_t *peak_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_PEAK_BITRATE : + (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE : + AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE; + const wchar_t *vbv_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE : + (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE : + AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE; + + // Check every result: a driver that rejects the runtime peak/VBV update + // keeps the session's initial budget, and the caller must not mistake a + // stale constraint for an applied one. + const auto target_res = encoder->SetProperty(target_prop, bitrate); + const auto peak_res = encoder->SetProperty(peak_prop, bitrate); + const auto vbv_res = encoder->SetProperty(vbv_prop, vbv_size); + + if (target_res != AMF_OK) { + BOOST_LOG(warning) << "AMF: set_bitrate failed, error: " << target_res; + return false; } - else if (video_format == 1) { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE, vbv_size); + if (peak_res != AMF_OK) { + BOOST_LOG(warning) << "AMF: runtime peak bitrate update rejected, error: " << peak_res; } - else { - res = encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE, bitrate); - encoder->SetProperty(AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE, vbv_size); + if (vbv_res != AMF_OK) { + BOOST_LOG(warning) << "AMF: runtime VBV budget update rejected, error: " << vbv_res + << " (budget stays at the session's initial size)"; } - - if (res == AMF_OK) { + if (peak_res == AMF_OK && vbv_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; + return peak_res == AMF_OK && vbv_res == AMF_OK; } void diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5395da9c0..8317c958d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -579,22 +579,24 @@ 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. -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 - 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}) +# 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 + 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 () -add_test(NAME amf_rate_control_unit_tests COMMAND amf_rate_control_unit_tests) # 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. @@ -1428,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) From 05b4aea175aec00a6f407cafb6d50ab873bb6d41 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:19:11 +0800 Subject: [PATCH 3/9] fix(amf): roll back a partially rejected runtime bitrate update TARGET/PEAK/VBV are one rate-control budget: snapshot all three before writing, and if the driver rejects any write, restore the properties that did land. The encoder now keeps one coherent budget instead of a half-updated one, and set_bitrate() only reports success when the complete budget applied. --- src/amf/amf_d3d11.cpp | 49 +++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 4e5f2a9d5..60fec3960 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1401,28 +1401,45 @@ namespace amf { (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE : AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE; - // Check every result: a driver that rejects the runtime peak/VBV update - // keeps the session's initial budget, and the caller must not mistake a - // stale constraint for an applied one. + // 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; + const bool snapshot_ok = encoder->GetProperty(target_prop, &old_target) == AMF_OK && + encoder->GetProperty(peak_prop, &old_peak) == AMF_OK && + encoder->GetProperty(vbv_prop, &old_vbv) == AMF_OK; + const auto target_res = encoder->SetProperty(target_prop, bitrate); const auto peak_res = encoder->SetProperty(peak_prop, bitrate); const auto vbv_res = encoder->SetProperty(vbv_prop, vbv_size); - if (target_res != AMF_OK) { - BOOST_LOG(warning) << "AMF: set_bitrate failed, error: " << target_res; + 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 << ")"; + if (snapshot_ok) { + // Roll back whatever landed: an unapplied property keeps its snapshot + // value, so only the writes that took effect need restoring. + auto rollback = [&](const wchar_t *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; + } + }; + rollback("target bitrate", target_prop, old_target, target_res); + rollback("peak bitrate", peak_prop, old_peak, peak_res); + rollback("VBV budget", vbv_prop, old_vbv, vbv_res); + } + else { + BOOST_LOG(warning) << "AMF: could not snapshot the current budget before writing; the encoder may keep a mixed budget"; + } return false; } - if (peak_res != AMF_OK) { - BOOST_LOG(warning) << "AMF: runtime peak bitrate update rejected, error: " << peak_res; - } - if (vbv_res != AMF_OK) { - BOOST_LOG(warning) << "AMF: runtime VBV budget update rejected, error: " << vbv_res - << " (budget stays at the session's initial size)"; - } - if (peak_res == AMF_OK && vbv_res == AMF_OK) { - BOOST_LOG(info) << "AMF: bitrate dynamically changed to " << bitrate_kbps << " Kbps"; - } - return peak_res == AMF_OK && vbv_res == AMF_OK; + + BOOST_LOG(info) << "AMF: bitrate dynamically changed to " << bitrate_kbps << " Kbps"; + return true; } void From 36f7791de82cc01c547abe4b8247a6508e6aee17 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:25:11 +0800 Subject: [PATCH 4/9] fix(amf): skip the runtime VBV write where AMF marks it static MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AMF 1.5.3 classifies the HEVC VBV size as static (settable only before Init) while H.264 and AV1 list it as dynamic — a runtime write on HEVC stores the value without applying it. Skip the HEVC write so the runtime budget update only carries properties that actually apply; the session keeps the Init-time VBV budget until it is recreated. The snapshot/rollback now covers exactly the properties written. --- src/amf/amf_d3d11.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 60fec3960..659b8c8ee 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1397,9 +1397,15 @@ namespace amf { const wchar_t *peak_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_PEAK_BITRATE : (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE : AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE; + // AMF 1.5.3 classifies the HEVC VBV size as static — settable only before + // Init() — while H.264 and AV1 list it as dynamic. A runtime write on HEVC + // stores the value without ever applying it, so skip it there: 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. const wchar_t *vbv_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE : - (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_VBV_BUFFER_SIZE : - AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE; + (video_format == 2) ? AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE : + nullptr; // 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 @@ -1407,13 +1413,15 @@ namespace amf { amf_int64 old_target = 0; amf_int64 old_peak = 0; amf_int64 old_vbv = 0; - const bool snapshot_ok = encoder->GetProperty(target_prop, &old_target) == AMF_OK && - encoder->GetProperty(peak_prop, &old_peak) == AMF_OK && - encoder->GetProperty(vbv_prop, &old_vbv) == AMF_OK; + bool snapshot_ok = encoder->GetProperty(target_prop, &old_target) == AMF_OK && + encoder->GetProperty(peak_prop, &old_peak) == AMF_OK; + if (vbv_prop) { + snapshot_ok = snapshot_ok && encoder->GetProperty(vbv_prop, &old_vbv) == AMF_OK; + } const auto target_res = encoder->SetProperty(target_prop, bitrate); const auto peak_res = encoder->SetProperty(peak_prop, bitrate); - const auto vbv_res = encoder->SetProperty(vbv_prop, vbv_size); + const auto vbv_res = vbv_prop ? encoder->SetProperty(vbv_prop, 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" @@ -1430,7 +1438,9 @@ namespace amf { }; rollback("target bitrate", target_prop, old_target, target_res); rollback("peak bitrate", peak_prop, old_peak, peak_res); - rollback("VBV budget", vbv_prop, old_vbv, vbv_res); + if (vbv_prop) { + rollback("VBV budget", vbv_prop, old_vbv, vbv_res); + } } else { BOOST_LOG(warning) << "AMF: could not snapshot the current budget before writing; the encoder may keep a mixed budget"; From 1b21542912b0546a317da22970d4a9cf664e979c Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:31:16 +0800 Subject: [PATCH 5/9] refactor(amf): collapse runtime bitrate property selection into one table One constexpr row per codec replaces three parallel ternary chains, and the VBV budget is only computed for codecs whose runtime VBV property exists (it is static on HEVC). --- src/amf/amf_d3d11.cpp | 56 +++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 659b8c8ee..cbfc63b12 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1389,23 +1389,27 @@ namespace amf { if (!encoder || bitrate_kbps <= 0) return false; auto bitrate = static_cast(bitrate_kbps) * 1000; - auto vbv_size = amf_avcodec_compat::vbv_buffer_size(bitrate_kbps, current_config); - - const wchar_t *target_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_TARGET_BITRATE : - (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_TARGET_BITRATE : - AMF_VIDEO_ENCODER_AV1_TARGET_BITRATE; - const wchar_t *peak_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_PEAK_BITRATE : - (video_format == 1) ? AMF_VIDEO_ENCODER_HEVC_PEAK_BITRATE : - AMF_VIDEO_ENCODER_AV1_PEAK_BITRATE; - // AMF 1.5.3 classifies the HEVC VBV size as static — settable only before - // Init() — while H.264 and AV1 list it as dynamic. A runtime write on HEVC - // stores the value without ever applying it, so skip it there: the session - // keeps the budget chosen at Init until it is recreated. For CBR + + // 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. - const wchar_t *vbv_prop = (video_format == 0) ? AMF_VIDEO_ENCODER_VBV_BUFFER_SIZE : - (video_format == 2) ? AMF_VIDEO_ENCODER_AV1_VBV_BUFFER_SIZE : - nullptr; + 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 }, + }; + const auto &props = budget_props[video_format]; + 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 @@ -1413,15 +1417,15 @@ namespace amf { amf_int64 old_target = 0; amf_int64 old_peak = 0; amf_int64 old_vbv = 0; - bool snapshot_ok = encoder->GetProperty(target_prop, &old_target) == AMF_OK && - encoder->GetProperty(peak_prop, &old_peak) == AMF_OK; - if (vbv_prop) { - snapshot_ok = snapshot_ok && encoder->GetProperty(vbv_prop, &old_vbv) == AMF_OK; + 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; } - const auto target_res = encoder->SetProperty(target_prop, bitrate); - const auto peak_res = encoder->SetProperty(peak_prop, bitrate); - const auto vbv_res = vbv_prop ? encoder->SetProperty(vbv_prop, vbv_size) : AMF_OK; + 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" @@ -1436,10 +1440,10 @@ namespace amf { BOOST_LOG(error) << "AMF: failed to roll back " << what << ", error: " << res; } }; - rollback("target bitrate", target_prop, old_target, target_res); - rollback("peak bitrate", peak_prop, old_peak, peak_res); - if (vbv_prop) { - rollback("VBV budget", vbv_prop, old_vbv, vbv_res); + 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); } } else { From 6f7eed82784111970cdfeb170e8f0147d732af91 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:42:29 +0800 Subject: [PATCH 6/9] fix(amf): narrow rollback labels and guard the codec table lookup The rollback lambda took its label as const wchar_t* while the call sites pass narrow string literals - there is no such conversion, so MSVC rejected the build. Take const char* for the label. Map an out-of-range video_format to the H.264 row before indexing the property table, matching the get_codec_id() default branch. --- src/amf/amf_d3d11.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index cbfc63b12..5942be73d 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1407,7 +1407,9 @@ namespace amf { { 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 }, }; - const auto &props = budget_props[video_format]; + // 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 }; @@ -1433,7 +1435,7 @@ namespace amf { if (snapshot_ok) { // Roll back whatever landed: an unapplied property keeps its snapshot // value, so only the writes that took effect need restoring. - auto rollback = [&](const wchar_t *what, const wchar_t *prop, amf_int64 value, AMF_RESULT write_res) { + 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) { From 08b1780ddaf3dd0af4c137dfcf5a8714044ca5e7 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 16:52:32 +0800 Subject: [PATCH 7/9] fix(amf): refuse to write the bitrate budget when the snapshot fails A snapshot failure left the writes running with no way to restore the old budget if a later write was rejected. Abort before writing anything instead: the encoder stays exactly as it was and the caller retains its previous allowance and retries on a later budget tick. Rollback failures now say explicitly that a mixed budget is possible and recommend recreating the session if they repeat. --- src/amf/amf_d3d11.cpp | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 5942be73d..44d806a23 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1424,6 +1424,15 @@ namespace amf { 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; + } const auto target_res = encoder->SetProperty(props.target, bitrate); const auto peak_res = encoder->SetProperty(props.peak, bitrate); @@ -1432,24 +1441,21 @@ namespace amf { 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 << ")"; - if (snapshot_ok) { - // 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; - } - }; - 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); + // 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"; } - } - else { - BOOST_LOG(warning) << "AMF: could not snapshot the current budget before writing; the encoder may keep a mixed budget"; + }; + 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; } From 4bbab82a418d373043fe89a23b0b5c1104870098 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 17:00:02 +0800 Subject: [PATCH 8/9] fix(tests): link Boost::format for the AMF rate-control suite On CI the boost fetch is limited to the declared components, and Boost::boost aliases only the base headers shim - boost/format.hpp (pulled in transitively via src/video.h -> stat_trackers.h) lives in the format component, whose target every other suite links explicitly. --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8317c958d..5f742a765 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -587,7 +587,7 @@ if (WIN32) ${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 + 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}) From c0948b34bf1807789dde7cbde0a0f83a3e00ca22 Mon Sep 17 00:00:00 2001 From: qiin2333 <414382190@qq.com> Date: Tue, 17 Nov 2026 17:02:51 +0800 Subject: [PATCH 9/9] docs(amf): spell out set_bitrate return semantics true means the complete budget was accepted. false means nothing was written (snapshot failure) or the update was rolled back to the snapshot; a rollback write itself being rejected is the sole path that can leave a mixed budget behind, and a session in that state should be recreated rather than re-driven. --- src/amf/amf_d3d11.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/amf/amf_d3d11.cpp b/src/amf/amf_d3d11.cpp index 44d806a23..419e010dd 100644 --- a/src/amf/amf_d3d11.cpp +++ b/src/amf/amf_d3d11.cpp @@ -1384,6 +1384,13 @@ 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;