10-bit encoding as a global setting

This commit is contained in:
galister
2025-07-11 23:09:37 +02:00
committed by xytovl
parent d64d5909ee
commit ed32676299
3 changed files with 53 additions and 43 deletions
+3 -1
View File
@@ -147,7 +147,6 @@ configuration::encoder parse_encoder(const nlohmann::json & item)
SET_IF(codec); SET_IF(codec);
if (e.codec == wivrn::video_codec(-1)) if (e.codec == wivrn::video_codec(-1))
throw std::runtime_error("invalid codec value " + item["codec"].get<std::string>()); throw std::runtime_error("invalid codec value " + item["codec"].get<std::string>());
SET_IF(use_10bit);
SET_IF(options); SET_IF(options);
SET_IF(device); SET_IF(device);
return e; return e;
@@ -201,6 +200,9 @@ configuration::configuration()
if (auto it = json.find("use-steamvr-lh"); it != json.end()) if (auto it = json.find("use-steamvr-lh"); it != json.end())
use_steamvr_lh = *it; use_steamvr_lh = *it;
if (auto it = json.find("encode_10bit"); it != json.end())
encode_10bit = *it;
if (auto it = json.find("tcp-only"); it != json.end()) if (auto it = json.find("tcp-only"); it != json.end())
tcp_only = *it; tcp_only = *it;
else if (auto it = json.find("tcp_only"); it != json.end()) else if (auto it = json.find("tcp_only"); it != json.end())
+1 -1
View File
@@ -50,7 +50,6 @@ struct configuration
std::optional<double> offset_y; std::optional<double> offset_y;
std::optional<int> group; std::optional<int> group;
std::optional<wivrn::video_codec> codec; std::optional<wivrn::video_codec> codec;
std::optional<bool> use_10bit;
std::map<std::string, std::string> options; std::map<std::string, std::string> options;
std::optional<std::string> device; std::optional<std::string> device;
}; };
@@ -63,6 +62,7 @@ struct configuration
std::vector<std::string> application; std::vector<std::string> application;
bool debug_gui = false; bool debug_gui = false;
bool use_steamvr_lh = false; bool use_steamvr_lh = false;
bool encode_10bit = false;
bool tcp_only = false; bool tcp_only = false;
service_publication publication = service_publication::avahi; service_publication publication = service_publication::avahi;
+49 -41
View File
@@ -144,7 +144,7 @@ static constexpr auto ffmpeg_version()
return result; return result;
} }
static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & codecs) static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & codecs, bool use_10bit)
{ {
video_encoder_ffmpeg::mute_logs mute; video_encoder_ffmpeg::mute_logs mute;
encoder_settings s{ encoder_settings s{
@@ -159,6 +159,12 @@ static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & b
}; };
for (auto codec: codecs) for (auto codec: codecs)
{ {
if (codec == wivrn::video_codec::h264)
{
U_LOG_D("Will not use h264: 10-bit not supported");
continue;
}
if constexpr (ffmpeg_version()[0] < 6) if constexpr (ffmpeg_version()[0] < 6)
{ {
if (codec == wivrn::video_codec::h264) if (codec == wivrn::video_codec::h264)
@@ -212,14 +218,16 @@ static bool probe_nvenc(wivrn_vk_bundle & bundle)
} }
#endif #endif
static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, configuration::encoder & config) static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, configuration::encoder & config, bool encode_10bit)
{ {
if (config.name.empty()) if (config.name.empty())
{ {
if (is_nvidia(*bundle.physical_device)) if (is_nvidia(*bundle.physical_device))
{ {
#if WIVRN_USE_NVENC #if WIVRN_USE_NVENC
if (probe_nvenc(bundle)) if (encode_10bit)
U_LOG_W("cannot use nvenc due to missing 10-bit support (disable 10-bit to use nvenc)");
else if (probe_nvenc(bundle))
config.name = encoder_nvenc; config.name = encoder_nvenc;
else else
#else #else
@@ -227,8 +235,13 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
#endif #endif
{ {
#if WIVRN_USE_X264 #if WIVRN_USE_X264
config.name = encoder_x264; if (encode_10bit)
config.codec = h264; U_LOG_E("no encoder found with 10-bit support (disable 10-bit to use x264)");
else
{
config.name = encoder_x264;
config.codec = h264;
}
#else #else
U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)"); U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)");
#endif #endif
@@ -239,9 +252,14 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
#if WIVRN_USE_VAAPI #if WIVRN_USE_VAAPI
config.name = encoder_vaapi; config.name = encoder_vaapi;
#elif WIVRN_USE_X264 #elif WIVRN_USE_X264
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available"); if (use_10bit)
config.name = encoder_x264; U_LOG_E("no encoder found with 10-bit support (disable 10-bit to use x264)");
config.codec = h264; else
{
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available");
config.name = encoder_x264;
config.codec = h264;
}
#else #else
U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)"); U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)");
#endif #endif
@@ -251,35 +269,41 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
#if WIVRN_USE_VAAPI #if WIVRN_USE_VAAPI
if (config.name == encoder_vaapi and not config.codec) if (config.name == encoder_vaapi and not config.codec)
{ {
config.codec = filter_codecs_vaapi(bundle, headset_codecs); config.codec = filter_codecs_vaapi(bundle, headset_codecs, encode_10bit);
if (not config.codec) if (not config.codec)
{ {
U_LOG_W("Failed to initialize vaapi, fallback to software encoding"); #if WIVRN_USE_X264
config.name = encoder_x264; if (encode_10bit)
config.codec = h264; U_LOG_E("Failed to initialize vaapi, but can't use x264 due to 10-bit (disable 10-bit to use x264)");
else
{
U_LOG_W("Failed to initialize vaapi, fallback to software encoding");
config.name = encoder_x264;
config.codec = h264;
}
#else
U_LOG_E("Failed to initialize vaapi");
#endif
} }
} }
#endif #endif
if (config.name == encoder_vulkan and not config.codec) if (config.name == encoder_vulkan and not config.codec)
config.codec = h264; config.codec = h264; // this will fail if 10-bit is enabled
#if WIVRN_USE_X264 #if WIVRN_USE_X264
if (config.name == encoder_x264) if (config.name == encoder_x264)
config.codec = h264; config.codec = h264; // this will fail if 10-bit is enabled
#endif #endif
if (not config.codec) if (not config.codec)
config.codec = h264; config.codec = encode_10bit ? h265 : h264;
if (not config.use_10bit)
config.use_10bit = false;
} }
static std::vector<configuration::encoder> get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs) static std::vector<configuration::encoder> get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, bool encode_10bit)
{ {
configuration::encoder base; configuration::encoder base;
fill_defaults(bundle, headset_codecs, base); fill_defaults(bundle, headset_codecs, base, encode_10bit);
#ifdef WIVRN_SPLIT_ENCODERS #ifdef WIVRN_SPLIT_ENCODERS
if (base.name != encoder_x264 and base.name != encoder_nvenc) if (base.name != encoder_x264 and base.name != encoder_nvenc)
@@ -337,7 +361,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
{ {
configuration config; configuration config;
if (config.encoders.empty()) if (config.encoders.empty())
config.encoders = get_encoder_default_settings(bundle, info.supported_codecs); config.encoders = get_encoder_default_settings(bundle, info.supported_codecs, config.encode_10bit);
if (not config.encoder_passthrough) if (not config.encoder_passthrough)
config.encoder_passthrough.emplace(); config.encoder_passthrough.emplace();
@@ -345,7 +369,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
config.encoder_passthrough->height = 1; config.encoder_passthrough->height = 1;
config.encoder_passthrough->offset_x = 0; config.encoder_passthrough->offset_x = 0;
config.encoder_passthrough->offset_y = 0; config.encoder_passthrough->offset_y = 0;
fill_defaults(bundle, info.supported_codecs, *config.encoder_passthrough); fill_defaults(bundle, info.supported_codecs, *config.encoder_passthrough, config.encode_10bit);
uint64_t bitrate = config.bitrate.value_or(default_bitrate); uint64_t bitrate = config.bitrate.value_or(default_bitrate);
std::array<double, 2> default_scale; std::array<double, 2> default_scale;
@@ -363,29 +387,13 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
for (auto & encoder: config.encoders) for (auto & encoder: config.encoders)
{ {
fill_defaults(bundle, info.supported_codecs, encoder); fill_defaults(bundle, info.supported_codecs, encoder, config.encode_10bit);
assert(encoder.codec); assert(encoder.codec);
check_scale(encoder.name, check_scale(encoder.name,
*encoder.codec, *encoder.codec,
std::ceil(encoder.width.value_or(1) * width), std::ceil(encoder.width.value_or(1) * width),
std::ceil(encoder.height.value_or(1) * height), std::ceil(encoder.height.value_or(1) * height),
scale); scale);
if (maybe_10bit.has_value())
{
if (maybe_10bit.value() != encoder.use_10bit)
throw std::runtime_error("all encoders must use the same 10-bit setting");
}
else
maybe_10bit = *encoder.use_10bit;
}
// TODO: detach alpha into separate vkImage.
// for now, if the main encoder is 10-bit, we also force alpha to be 10-bit
if (maybe_10bit.value_or(false) && !*config.encoder_passthrough->use_10bit)
{
config.encoder_passthrough->name = config.encoders[0].name;
config.encoder_passthrough->codec = config.encoders[0].codec;
config.encoder_passthrough->use_10bit = true;
} }
width = align(width * scale[0], 64); width = align(width * scale[0], 64);
@@ -418,7 +426,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
settings.video_width = settings.width; settings.video_width = settings.width;
settings.video_height = settings.height; settings.video_height = settings.height;
settings.codec = *encoder.codec; settings.codec = *encoder.codec;
settings.use_10bit = *encoder.use_10bit; settings.use_10bit = config.encode_10bit;
if (encoder.group) if (encoder.group)
settings.group = *encoder.group; settings.group = *encoder.group;
else else
@@ -449,7 +457,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
settings.video_width = settings.width; settings.video_width = settings.width;
settings.video_height = settings.height; settings.video_height = settings.height;
settings.codec = *encoder.codec; settings.codec = *encoder.codec;
settings.use_10bit = *encoder.use_10bit; settings.use_10bit = config.encode_10bit;
if (encoder.group) if (encoder.group)
settings.group = *encoder.group; settings.group = *encoder.group;
else else