fix: Validate AMD max_au_size and update docs

Adds explicit validation for `amd_max_au_size` so only unset or values >= -1 are applied, matching FFmpeg/AMF expectations. Updates the web config input bounds and wording to clarify this as a last-resort FEC protection cap (H.264/HEVC only), and refreshes configuration docs with improved sizing guidance. Also expands unit coverage for codec option mapping and config parsing edge cases, including range boundaries.
This commit is contained in:
ReenigneArcher committed 2026-08-21 10:10:36 -04:00
1 parent 8d3f1abe3b
commit 8a0a2854be
7 files changed
+151 -39

No files matched your search

+34 -34
View File
@@ -2780,6 +2780,40 @@ editing the `conf` file in a text editor. Use the examples as reference.
</tr>
</table>
### amd_max_au_size
<table>
<tr>
<td>Description</td>
<td colspan="2">
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.}
</td>
</tr>
<tr>
<td>Default</td>
<td colspan="2">@code{}
@endcode</td>
</tr>
<tr>
<td>Example</td>
<td colspan="2">@code{}
amd_max_au_size = 6400000
@endcode</td>
</tr>
</table>
### amd_quality
<table>
@@ -2904,40 +2938,6 @@ editing the `conf` file in a text editor. Use the examples as reference.
</tr>
</table>
### amd_max_au_size
<table>
<tr>
<td>Description</td>
<td colspan="2">
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.}
</td>
</tr>
<tr>
<td>Default</td>
<td colspan="2">@code{}
@endcode</td>
</tr>
<tr>
<td>Example</td>
<td colspan="2">@code{}
amd_max_au_size = 800000
@endcode</td>
</tr>
</table>
## VideoToolbox Encoder
### vt_coder
+19 -1
View File
@@ -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<std::string, std::string> &vars, const std::string &name, std::optional<int> &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);
+10 -1
View File
@@ -94,7 +94,7 @@ namespace config {
std::optional<int> amd_quality_av1;
std::optional<int> amd_preanalysis;
std::optional<int> amd_vbaq;
std::optional<int> amd_max_au_size;
std::optional<int> 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<std::string, std::string> &&vars);
#endif
/**
* @brief Parse serialized text into the corresponding runtime representation.
*
+1 -1
View File
@@ -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": "",
},
},
{
@@ -61,7 +61,7 @@ const config = ref(props.config)
<div class="mb-3">
<label for="amd_max_au_size" class="form-label">{{ $t('config.amd_max_au_size') }}</label>
<input type="number" class="form-control" id="amd_max_au_size"
placeholder="-1" min="-1" v-model="config.amd_max_au_size" />
placeholder="-1" min="-1" max="2147483647" v-model="config.amd_max_au_size" />
<div class="form-text">{{ $t('config.amd_max_au_size_desc') }}</div>
</div>
</div>
@@ -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",
+85
View File
@@ -7,6 +7,8 @@
// standard includes
#include <algorithm>
#include <limits>
#include <optional>
#include <tuple>
#include <utility>
#include <vector>
@@ -100,8 +102,91 @@ TEST(AmfH264OptionsTest, CoderUsesConfiguredValue) {
ASSERT_TRUE(std::holds_alternative<int *>(coder_option->value));
EXPECT_EQ(&config::video.amd.amd_coder, std::get<int *>(coder_option->value));
}
/**
* @brief Parameterized coverage for the AMF maximum access-unit-size option mappings.
*/
struct AmfMaxAuSizeOptionsTest: testing::TestWithParam<std::tuple<const video::encoder_t::codec_t *, bool>> {};
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<std::optional<int> *>(option->value));
EXPECT_EQ(&config::video.amd.amd_max_au_size, std::get<std::optional<int> *>(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<std::string_view, std::optional<int>>;
/**
* @brief Parameterized coverage for parsing and validating the AMF maximum access-unit size.
*/
struct AmfMaxAuSizeConfigTest: BaseTest, testing::WithParamInterface<AmfMaxAuSizeConfigParam> {
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<int>::max()}
)
);
struct FramerateX100Test: BaseTest, testing::WithParamInterface<std::tuple<std::int32_t, AVRational>> {};
TEST_P(FramerateX100Test, Run) {