From ec42db83bc3c8697db71aa05626107ca356ab027 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Mon, 7 Jul 2025 19:55:30 +0900 Subject: [PATCH] =?UTF-8?q?change=20encode=5F10bit=20=E2=86=92=20bit-depth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/configuration.md | 7 ++- server/driver/configuration.cpp | 4 +- server/driver/configuration.h | 2 +- server/driver/wivrn_comp_target.cpp | 3 +- server/encoder/encoder_settings.cpp | 64 ++++++++++---------- server/encoder/encoder_settings.h | 2 +- server/encoder/ffmpeg/video_encoder_va.cpp | 22 ++++--- server/encoder/video_encoder_nvenc.cpp | 4 +- server/encoder/video_encoder_vulkan_h264.cpp | 4 +- server/encoder/video_encoder_x264.cpp | 4 +- 10 files changed, 66 insertions(+), 50 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ffc1ec76..3ee6ad01 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -22,7 +22,7 @@ Scaling is applied in a foveated fashion: the center has a 1:1 ratio and the res "scale": 0.5 } ``` -The x and y resolution of the streamed video are half the size on the headsed, reduces the required bandwidth and encoding/decoding time by about 4. +The x and y resolution of the streamed video are half the size on the headset, reduces the required bandwidth and encoding/decoding time by about 4. ```json { @@ -36,6 +36,11 @@ Default value: `50000000` (50Mb/s) Bitrate of the video, in bit/s. Split among decoders based on size and codecs. +## `bit-depth` +Default value: `8` (bits) + +Bit depth of the video. 8-bit is supported by all encoders. 10-bit is supported by `vaapi` encoders using `h265` or `av1`. + ## `encoders` A list of encoders to use. diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 1de229dd..002dc5f0 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -200,8 +200,8 @@ configuration::configuration() if (auto it = json.find("use-steamvr-lh"); it != json.end()) use_steamvr_lh = *it; - if (auto it = json.find("encode_10bit"); it != json.end()) - encode_10bit = *it; + if (auto it = json.find("bit-depth"); it != json.end()) + bit_depth = *it; if (auto it = json.find("tcp-only"); it != json.end()) tcp_only = *it; diff --git a/server/driver/configuration.h b/server/driver/configuration.h index d0629c61..1cec3611 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -57,12 +57,12 @@ struct configuration std::vector encoders; std::optional encoder_passthrough; std::optional bitrate; + int bit_depth = 8; std::optional> scale; std::optional> grip_surface; std::vector application; bool debug_gui = false; bool use_steamvr_lh = false; - bool encode_10bit = false; bool tcp_only = false; service_publication publication = service_publication::avahi; diff --git a/server/driver/wivrn_comp_target.cpp b/server/driver/wivrn_comp_target.cpp index 7ba8d936..a849c413 100644 --- a/server/driver/wivrn_comp_target.cpp +++ b/server/driver/wivrn_comp_target.cpp @@ -409,7 +409,8 @@ static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_ target_init_semaphores(cn); - if (cn->settings[0].use_10bit) + // will fail on encoder init if bit_depth is arbitrary garbage + if (cn->settings[0].bit_depth == 10) ct->format = VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16; else ct->format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM; diff --git a/server/encoder/encoder_settings.cpp b/server/encoder/encoder_settings.cpp index 36ce53da..822a6702 100644 --- a/server/encoder/encoder_settings.cpp +++ b/server/encoder/encoder_settings.cpp @@ -23,6 +23,7 @@ #include "utils/wivrn_vk_bundle.h" #include "video_encoder.h" +#include "wivrn_packets.h" #include #include #include @@ -92,10 +93,8 @@ void print_encoders(const std::vector & encoders) U_LOG_I("Group %d", group); } - const char * suffix_10bit = encoder.use_10bit ? " 10-bit" : ""; - std::string codec(magic_enum::enum_name(encoder.codec)); - U_LOG_I("\t%s (%s%s)", encoder.encoder_name.c_str(), codec.c_str(), suffix_10bit); + U_LOG_I("\t%s (%s %d-bit)", encoder.encoder_name.c_str(), codec.c_str(), encoder.bit_depth); U_LOG_I("\tchannels: %s", std::string(magic_enum::enum_name(encoder.channels)).c_str()); U_LOG_I("\tsize:%" PRIu16 "x%" PRIu16 " offset:%" PRIu16 "x%" PRIu16, encoder.width, @@ -144,7 +143,7 @@ static constexpr auto ffmpeg_version() return result; } -static std::optional filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector & codecs, bool use_10bit) +static std::optional filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector & codecs, int bit_depth) { video_encoder_ffmpeg::mute_logs mute; encoder_settings s{ @@ -157,17 +156,18 @@ static std::optional filter_codecs_vaapi(wivrn_vk_bundle & b "vaapi", default_bitrate, }; + s.bit_depth = bit_depth; + 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 (codec == wivrn::video_codec::h264) + if (bit_depth != 8) + { + U_LOG_D("Will not use h264: %d-bit not supported", bit_depth); + continue; + } + if constexpr (ffmpeg_version()[0] < 6) { U_LOG_W("Skip h264 on ffmpeg < 6 due to poor performance"); continue; @@ -218,15 +218,15 @@ static bool probe_nvenc(wivrn_vk_bundle & bundle) } #endif -static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector & headset_codecs, configuration::encoder & config, bool encode_10bit) +static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector & headset_codecs, configuration::encoder & config, int bit_depth) { if (config.name.empty()) { if (is_nvidia(*bundle.physical_device)) { #if WIVRN_USE_NVENC - if (encode_10bit) - U_LOG_W("cannot use nvenc due to missing 10-bit support (disable 10-bit to use nvenc)"); + if (bit_depth != 8) + U_LOG_W("nvenc encoder does not support %d-bit encoding (set 8-bit to use nvenc)", bit_depth); else if (probe_nvenc(bundle)) config.name = encoder_nvenc; else @@ -235,8 +235,8 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector & headset_codecs, bool encode_10bit) +static std::vector get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector & headset_codecs, int bit_depth) { configuration::encoder base; - fill_defaults(bundle, headset_codecs, base, encode_10bit); + fill_defaults(bundle, headset_codecs, base, bit_depth); #ifdef WIVRN_SPLIT_ENCODERS if (base.name != encoder_x264 and base.name != encoder_nvenc) @@ -360,8 +360,12 @@ static uint16_t align(uint16_t value, uint16_t alignment) std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uint32_t & width, uint32_t & height, const from_headset::headset_info_packet & info) { configuration config; + + if (config.bit_depth != 8 && config.bit_depth != 10) + throw std::runtime_error("invalid bit-depth setting. supported values: 8, 10"); + if (config.encoders.empty()) - config.encoders = get_encoder_default_settings(bundle, info.supported_codecs, config.encode_10bit); + config.encoders = get_encoder_default_settings(bundle, info.supported_codecs, config.bit_depth); if (not config.encoder_passthrough) config.encoder_passthrough.emplace(); @@ -369,7 +373,7 @@ std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uin config.encoder_passthrough->height = 1; config.encoder_passthrough->offset_x = 0; config.encoder_passthrough->offset_y = 0; - fill_defaults(bundle, info.supported_codecs, *config.encoder_passthrough, config.encode_10bit); + fill_defaults(bundle, info.supported_codecs, *config.encoder_passthrough, config.bit_depth); uint64_t bitrate = config.bitrate.value_or(default_bitrate); std::array default_scale; @@ -383,11 +387,9 @@ std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uin height / passthrough_subsampling, scale); - std::optional maybe_10bit{}; - for (auto & encoder: config.encoders) { - fill_defaults(bundle, info.supported_codecs, encoder, config.encode_10bit); + fill_defaults(bundle, info.supported_codecs, encoder, config.bit_depth); assert(encoder.codec); check_scale(encoder.name, *encoder.codec, @@ -426,7 +428,7 @@ std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uin settings.video_width = settings.width; settings.video_height = settings.height; settings.codec = *encoder.codec; - settings.use_10bit = config.encode_10bit; + settings.bit_depth = config.bit_depth; if (encoder.group) settings.group = *encoder.group; else @@ -457,7 +459,7 @@ std::vector get_encoder_settings(wivrn_vk_bundle & bundle, uin settings.video_width = settings.width; settings.video_height = settings.height; settings.codec = *encoder.codec; - settings.use_10bit = config.encode_10bit; + settings.bit_depth = config.bit_depth; if (encoder.group) settings.group = *encoder.group; else diff --git a/server/encoder/encoder_settings.h b/server/encoder/encoder_settings.h index 2bf7b3d2..0471b7a7 100644 --- a/server/encoder/encoder_settings.h +++ b/server/encoder/encoder_settings.h @@ -39,7 +39,7 @@ struct encoder_settings : public to_headset::video_stream_description::item std::map options; // additional encoder-specific configuration // encoders in the same group are executed in sequence int group = 0; - bool use_10bit; + int bit_depth; std::optional device; }; diff --git a/server/encoder/ffmpeg/video_encoder_va.cpp b/server/encoder/ffmpeg/video_encoder_va.cpp index c9177acb..bbab42ce 100644 --- a/server/encoder/ffmpeg/video_encoder_va.cpp +++ b/server/encoder/ffmpeg/video_encoder_va.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -134,9 +135,10 @@ std::unordered_map vulkan_drm_format_map_10bit = { {DRM_FORMAT_RG1616, vk::Format::eR10X6G10X6Unorm2Pack16}, }; -vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, bool use_10bit) +/// bit_depth not checked, valid values are 8 and 10 +vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, int bit_depth) { - if (use_10bit) + if (bit_depth == 10) return vulkan_drm_format_map_10bit.at(drm_fourcc); else return vulkan_drm_format_map.at(drm_fourcc); @@ -170,11 +172,16 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, { case video_codec::av1: case video_codec::h265: - sw_format = settings.use_10bit ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12; + if (settings.bit_depth == 10) + sw_format = AV_PIX_FMT_P010; + else if (settings.bit_depth == 8) + sw_format = AV_PIX_FMT_NV12; + else + throw std::runtime_error("selected codec only supports 10-bit or 8-bit encoding"); break; default: - if (settings.use_10bit) - throw std::runtime_error("selected codec does not support 10-bit encoding"); + if (settings.bit_depth != 8) + throw std::runtime_error("selected codec only supports 8-bit encoding"); sw_format = AV_PIX_FMT_NV12; break; } @@ -226,7 +233,8 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, av_dict_set(&opts, "rc_mode", "CBR", 0); break; case video_codec::h265: - encoder_ctx->profile = settings.use_10bit ? FF_PROFILE_HEVC_MAIN_10 : FF_PROFILE_HEVC_MAIN; + // bit_depth is either 8 or 10 here + encoder_ctx->profile = settings.bit_depth == 10 ? FF_PROFILE_HEVC_MAIN_10 : FF_PROFILE_HEVC_MAIN; break; case video_codec::av1: encoder_ctx->profile = FF_PROFILE_AV1_MAIN; @@ -309,7 +317,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk, vk::StructureChain image_create_info{ vk::ImageCreateInfo{ .imageType = vk::ImageType::e2D, - .format = drm_to_vulkan_fmt(desc->layers[i].format, settings.use_10bit), + .format = drm_to_vulkan_fmt(desc->layers[i].format, settings.bit_depth), .extent = { .width = uint32_t(drm_frame->width / (i == 0 ? 1 : 2)), .height = uint32_t(drm_frame->height / (i == 0 ? 1 : 2)), diff --git a/server/encoder/video_encoder_nvenc.cpp b/server/encoder/video_encoder_nvenc.cpp index 36d9c5c1..047b83f5 100644 --- a/server/encoder/video_encoder_nvenc.cpp +++ b/server/encoder/video_encoder_nvenc.cpp @@ -144,8 +144,8 @@ video_encoder_nvenc::video_encoder_nvenc( fps(fps), bitrate(settings.bitrate) { - if (settings.use_10bit) - throw std::runtime_error("nvenc encoder does not support 10-bit encoding"); + if (settings.bit_depth != 8) + throw std::runtime_error("nvenc encoder only supports 8-bit encoding"); std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init(); assert(settings.width % 32 == 0); diff --git a/server/encoder/video_encoder_vulkan_h264.cpp b/server/encoder/video_encoder_vulkan_h264.cpp index 4ad7febc..76405794 100644 --- a/server/encoder/video_encoder_vulkan_h264.cpp +++ b/server/encoder/video_encoder_vulkan_h264.cpp @@ -192,8 +192,8 @@ std::unique_ptr wivrn::video_encoder_vulkan_h2 vk::VideoEncodeCapabilitiesKHR, vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get()); - if (settings.use_10bit) - throw std::runtime_error("h264 codec does not support 10-bit encoding"); + if (settings.bit_depth != 8) + throw std::runtime_error("h264 codec only supports 8-bit encoding"); std::unique_ptr self(new video_encoder_vulkan_h264(vk, rect, video_caps, encode_caps, fps, stream_idx, settings)); diff --git a/server/encoder/video_encoder_x264.cpp b/server/encoder/video_encoder_x264.cpp index 9ff61082..fcb28162 100644 --- a/server/encoder/video_encoder_x264.cpp +++ b/server/encoder/video_encoder_x264.cpp @@ -93,8 +93,8 @@ video_encoder_x264::video_encoder_x264( uint8_t stream_idx) : video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false) { - if (settings.use_10bit) - throw std::runtime_error("x264 encoder does not support 10-bit encoding"); + if (settings.bit_depth != 8) + throw std::runtime_error("x264 encoder only supports 8-bit encoding"); if (settings.codec != h264) {