diff --git a/docs/configuration.md b/docs/configuration.md index 961e3e66c..030864881 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2780,6 +2780,40 @@ editing the `conf` file in a text editor. Use the examples as reference. +### amd_max_au_size + + + + + + + + + + + + + + +
Description + Maximum Access Unit (frame) size for rate control, in bits. This is a last-resort + control for capping oversized encoded frames that would otherwise exceed Sunshine's + four-block FEC capacity and be sent without FEC protection. + @note{This option only applies to H.264 and HEVC when using amdvce [encoder](#encoder). + AV1 does not support this option.} + @note{The FEC ceiling depends on the negotiated packet size and [fec_percentage](#fec_percentage), + not the average bitrate or frame rate. The approximate encoded-payload ceiling is + `4 * floor(25500 / (100 + fec_percentage)) * (packet_size - 16) * 8` bits, before + allowing for the short frame header or codec-header replacements. With 20% FEC and + 1024-byte packets, 6400000 bits provides conservative headroom.} + @warning{Setting this unnecessarily low can reduce image quality, especially for + keyframes. Leave it empty unless oversized frames are actually causing FEC to be skipped.} +
Default@code{} + + @endcode
Example@code{} + amd_max_au_size = 6400000 + @endcode
+ ### amd_quality @@ -2904,40 +2938,6 @@ editing the `conf` file in a text editor. Use the examples as reference.
-### amd_max_au_size - - - - - - - - - - - - - - -
Description - Maximum Access Unit (frame) size for rate control, in bits. When set, the encoder - will cap the maximum encoded frame size. This prevents oversized frames from exceeding - FEC shard limits (DATA_SHARDS_MAX=255), which otherwise causes FEC to be skipped for - those frames. This is especially important for Wi-Fi streaming where packet loss on - large unprotected frames leads to dropped frames and stuttering. - @note{This option only applies to H.264 and HEVC when using amdvce [encoder](#encoder). - AV1 does not support this option.} - @note{Works with all rate control modes (CQP, CBR, VBR). Most beneficial with CQP - where frame sizes are otherwise unbounded.} - @note{Value is in bits. To calculate: (target_bitrate_mbps / fps / 1.2) × 1000000, - where 1.2 accounts for ~20% FEC overhead. Examples: 800000 for ~50 Mbps at 60fps, - 1600000 for ~100 Mbps at 60fps, 400000 for ~50 Mbps at 120fps.} -
Default@code{} - - @endcode
Example@code{} - amd_max_au_size = 800000 - @endcode
- ## VideoToolbox Encoder ### vt_coder diff --git a/src/config.cpp b/src/config.cpp index 6d6444cc5..376fdf676 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1272,6 +1272,24 @@ namespace config { } } + /** + * @brief Consume an optional integer setting only when it meets a minimum value. + * + * @param vars Parsed configuration entries; consumed keys are erased. + * @param name Configuration key to consume. + * @param input Destination field updated when the setting exists and meets the minimum. + * @param minimum Lowest accepted value. + */ + void int_at_least_f(std::unordered_map &vars, const std::string &name, std::optional &input, int minimum) { + auto temp = input; + + int_f(vars, name, temp); + + if (!temp || *temp >= minimum) { + input = temp; + } + } + /** * @brief Convert common textual boolean forms to a boolean value. * @@ -1631,7 +1649,7 @@ namespace config { bool_f(vars, "amd_preanalysis", (bool &) video.amd.amd_preanalysis); bool_f(vars, "amd_vbaq", (bool &) video.amd.amd_vbaq); bool_f(vars, "amd_enforce_hrd", (bool &) video.amd.amd_enforce_hrd); - int_f(vars, "amd_max_au_size", video.amd.amd_max_au_size); + int_at_least_f(vars, "amd_max_au_size", video.amd.amd_max_au_size, -1); int_f(vars, "vt_coder", video.vt.vt_coder, vt::coder_from_view); int_f(vars, "vt_software", video.vt.vt_allow_sw, vt::allow_software_from_view); diff --git a/src/config.h b/src/config.h index ac768b745..ee2dab4df 100644 --- a/src/config.h +++ b/src/config.h @@ -94,7 +94,7 @@ namespace config { std::optional amd_quality_av1; std::optional amd_preanalysis; std::optional amd_vbaq; - std::optional amd_max_au_size; + std::optional amd_max_au_size; ///< Maximum AMF H.264/HEVC access unit size in bits; unset uses the encoder default. int amd_coder; } amd; ///< AMD AMF encoder options. @@ -384,6 +384,15 @@ namespace config { extern input_t input; extern sunshine_t sunshine; +#ifdef SUNSHINE_TESTS + /** + * @brief Apply parsed configuration entries to the global runtime configuration for testing. + * + * @param vars Parsed configuration entries to consume. + */ + void apply_config(std::unordered_map &&vars); +#endif + /** * @brief Parse serialized text into the corresponding runtime representation. * diff --git a/src_assets/common/assets/web/config.html b/src_assets/common/assets/web/config.html index 96b35a87f..f8df1ae4d 100644 --- a/src_assets/common/assets/web/config.html +++ b/src_assets/common/assets/web/config.html @@ -337,11 +337,11 @@ "amd_usage": "ultralowlatency", "amd_rc": "vbr_latency", "amd_enforce_hrd": "disabled", + "amd_max_au_size": "", "amd_quality": "balanced", "amd_preanalysis": "disabled", "amd_vbaq": "enabled", "amd_coder": "auto", - "amd_max_au_size": "", }, }, { diff --git a/src_assets/common/assets/web/configs/tabs/encoders/AmdAmfEncoder.vue b/src_assets/common/assets/web/configs/tabs/encoders/AmdAmfEncoder.vue index 738e27672..de9d3f3a9 100644 --- a/src_assets/common/assets/web/configs/tabs/encoders/AmdAmfEncoder.vue +++ b/src_assets/common/assets/web/configs/tabs/encoders/AmdAmfEncoder.vue @@ -61,7 +61,7 @@ const config = ref(props.config)
+ placeholder="-1" min="-1" max="2147483647" v-model="config.amd_max_au_size" />
{{ $t('config.amd_max_au_size_desc') }}
diff --git a/src_assets/common/assets/web/public/assets/locale/en.json b/src_assets/common/assets/web/public/assets/locale/en.json index e0c5470e5..07c659660 100644 --- a/src_assets/common/assets/web/public/assets/locale/en.json +++ b/src_assets/common/assets/web/public/assets/locale/en.json @@ -124,7 +124,7 @@ "amd_enforce_hrd": "AMF Hypothetical Reference Decoder (HRD) Enforcement", "amd_enforce_hrd_desc": "Increases the constraints on rate control to meet HRD model requirements. This greatly reduces bitrate overflows, but may cause encoding artifacts or reduced quality on certain cards.", "amd_max_au_size": "AMF Max AU Size (bits)", - "amd_max_au_size_desc": "Maximum Access Unit (frame) size in bits. Empty or -1 disables. Prevents oversized frames from exceeding FEC limits on Wi-Fi. H.264 and HEVC only. Formula: (target_bitrate_mbps / fps / 1.2) * 1000000. Example at 50 Mbps 60fps: (50/60/1.2)*1000000 = 694444. Recommended: 800000 for ~50 Mbps Wi-Fi.", + "amd_max_au_size_desc": "Maximum encoded H.264 or HEVC Access Unit size in bits. Empty or -1 leaves the AMF default. Use only to cap oversized frames that exceed Sunshine's FEC capacity. The limit depends on packet size and FEC percentage, not bitrate or frame rate. See the documentation for sizing.", "amd_preanalysis": "AMF Preanalysis", "amd_preanalysis_desc": "This enables rate-control preanalysis, which may increase quality at the expense of increased encoding latency.", "amd_quality": "AMF Quality", diff --git a/tests/unit/test_video.cpp b/tests/unit/test_video.cpp index 9a7ad1fe2..01da369d1 100644 --- a/tests/unit/test_video.cpp +++ b/tests/unit/test_video.cpp @@ -7,6 +7,8 @@ // standard includes #include +#include +#include #include #include #include @@ -100,8 +102,91 @@ TEST(AmfH264OptionsTest, CoderUsesConfiguredValue) { ASSERT_TRUE(std::holds_alternative(coder_option->value)); EXPECT_EQ(&config::video.amd.amd_coder, std::get(coder_option->value)); } + +/** + * @brief Parameterized coverage for the AMF maximum access-unit-size option mappings. + */ +struct AmfMaxAuSizeOptionsTest: testing::TestWithParam> {}; + +TEST_P(AmfMaxAuSizeOptionsTest, UsesConfiguredValueForSupportedCodecsOnly) { + const auto &[codec, supported] = GetParam(); + const auto option = std::ranges::find(codec->common_options, "max_au_size"sv, &video::encoder_t::option_t::name); + + if (!supported) { + EXPECT_EQ(codec->common_options.end(), option); + return; + } + + ASSERT_NE(codec->common_options.end(), option); + ASSERT_TRUE(std::holds_alternative *>(option->value)); + EXPECT_EQ(&config::video.amd.amd_max_au_size, std::get *>(option->value)); +} + +INSTANTIATE_TEST_SUITE_P( + AmfCodecOptions, + AmfMaxAuSizeOptionsTest, + testing::Values( + std::make_tuple(&video::amdvce.h264, true), + std::make_tuple(&video::amdvce.hevc, true), + std::make_tuple(&video::amdvce.av1, false) + ) +); #endif +using AmfMaxAuSizeConfigParam = std::tuple>; + +/** + * @brief Parameterized coverage for parsing and validating the AMF maximum access-unit size. + */ +struct AmfMaxAuSizeConfigTest: BaseTest, testing::WithParamInterface { + void SetUp() override { + BaseTest::SetUp(); + config::video.amd.amd_max_au_size.reset(); + config::stream.file_apps = __FILE__; + } + + void TearDown() override { + config::video = original_video; + config::audio = original_audio; + config::stream = original_stream; + config::nvhttp = original_nvhttp; + config::input = original_input; + config::sunshine = original_sunshine; + config::modified_config_settings = original_modified_config_settings; + BaseTest::TearDown(); + } + + config::video_t original_video {config::video}; ///< Video configuration restored after each parameterized test. + config::audio_t original_audio {config::audio}; ///< Audio configuration restored after each parameterized test. + config::stream_t original_stream {config::stream}; ///< Stream configuration restored after each parameterized test. + config::nvhttp_t original_nvhttp {config::nvhttp}; ///< HTTP configuration restored after each parameterized test. + config::input_t original_input {config::input}; ///< Input configuration restored after each parameterized test. + config::sunshine_t original_sunshine {config::sunshine}; ///< Core configuration restored after each parameterized test. + decltype(config::modified_config_settings) original_modified_config_settings {config::modified_config_settings}; ///< Modified settings restored after each parameterized test. +}; + +TEST_P(AmfMaxAuSizeConfigTest, AcceptsOnlyFfmpegSupportedRange) { + const auto &[setting, expected] = GetParam(); + auto vars = config::parse_config(setting); + + config::apply_config(std::move(vars)); + + EXPECT_EQ(expected, config::video.amd.amd_max_au_size); +} + +INSTANTIATE_TEST_SUITE_P( + AmfMaxAuSizeValues, + AmfMaxAuSizeConfigTest, + testing::Values( + AmfMaxAuSizeConfigParam {""sv, std::nullopt}, + AmfMaxAuSizeConfigParam {"amd_max_au_size = -2\n"sv, std::nullopt}, + AmfMaxAuSizeConfigParam {"amd_max_au_size = -1\n"sv, -1}, + AmfMaxAuSizeConfigParam {"amd_max_au_size = 0\n"sv, 0}, + AmfMaxAuSizeConfigParam {"amd_max_au_size = 800000\n"sv, 800000}, + AmfMaxAuSizeConfigParam {"amd_max_au_size = 2147483647\n"sv, std::numeric_limits::max()} + ) +); + struct FramerateX100Test: BaseTest, testing::WithParamInterface> {}; TEST_P(FramerateX100Test, Run) {