From d175284358b75c4c7a29422dd7fed31cb9065e8d Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 3 Mar 2024 09:13:15 +0100 Subject: [PATCH] Configure bitrate globally --- docs/configuration.md | 18 ++++++++---------- server/driver/configuration.cpp | 6 +++++- server/driver/configuration.h | 2 +- server/encoder/encoder_settings.cpp | 27 ++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index e538f644..51e34c25 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -20,6 +20,11 @@ The x and y resolution of the streamed video are half the size on the headsed, r ``` Scales x by a 0.75 factor, and y by a 0.5 factor. +## `bitrate` +Default value: `50000000` (50Mb/s) + +Bitrate of the video, in bit/s. Split among decoders based on size and codecs. + ## `encoders` A list of encoders to use. @@ -36,11 +41,6 @@ Default value: `h265` One of `h264` or `h265`. If using `x264` encoder, value is ignored and `h264` is used. -### `bitrate` -Default value: `50000000` (50Mb/s) - -Bitrate of the video, in bit/s. - ### `width`, `height`, `offset_x`, `offset_y` (advanced) Default values: full image (`width` = 1, `height` = 1, `offset_x` = 0, `offset_y` = 0) @@ -55,10 +55,10 @@ Identifier (number) of the encoder group. Encoders with the same identifier are 1. Simple encoder ```json { + "bitrate": 50000000, "encoders": [ { "encoder": "vaapi", - "bitrate": 50000000, "codec": "h265" } ] @@ -69,10 +69,10 @@ Creates a single encoder, using vaapi hardware encoding, h265 video codec (HEVC) 2. Hardware + software encoder ```json { + "bitrate": 50000000, "encoders": [ { "encoder": "vaapi", - "bitrate": 25000000, "codec": "h265", "width": 0.5, "height": 1, @@ -81,7 +81,6 @@ Creates a single encoder, using vaapi hardware encoding, h265 video codec (HEVC) }, { "encoder": "x264", - "bitrate": 25000000, "codec": "h264", "width": 0.5, "height": 1, @@ -96,10 +95,10 @@ Creates a hardware encoder for left eye, and a software encoder for right eye. 3. 2 Hardware encoders ```json { + "bitrate": 50000000, "encoders": [ { "encoder": "vaapi", - "bitrate": 25000000, "codec": "h265", "width": 0.5, "height": 1, @@ -109,7 +108,6 @@ Creates a hardware encoder for left eye, and a software encoder for right eye. }, { "encoder": "vaapi", - "bitrate": 25000000, "codec": "h264", "width": 0.5, "height": 1, diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index 86e8c6bc..021da82c 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -74,6 +74,11 @@ configuration configuration::read_user_configuration() result.scale = json["scale"]; } + if (json.contains("bitrate")) + { + result.bitrate = json["bitrate"]; + } + if (json.contains("encoders")) { for (const auto & encoder: json["encoders"]) @@ -89,7 +94,6 @@ configuration configuration::read_user_configuration() SET_IF(height); SET_IF(offset_x); SET_IF(offset_y); - SET_IF(bitrate); SET_IF(group); SET_IF(codec); if (e.codec == xrt::drivers::wivrn::video_codec(-1)) diff --git a/server/driver/configuration.h b/server/driver/configuration.h index e0811b83..b9df430b 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -35,7 +35,6 @@ struct configuration std::optional height; std::optional offset_x; std::optional offset_y; - std::optional bitrate; std::optional group; std::optional codec; std::map options; @@ -43,6 +42,7 @@ struct configuration }; std::vector encoders; + std::optional bitrate; std::optional> scale; std::vector application; diff --git a/server/encoder/encoder_settings.cpp b/server/encoder/encoder_settings.cpp index c7a5b646..8f90e08c 100644 --- a/server/encoder/encoder_settings.cpp +++ b/server/encoder/encoder_settings.cpp @@ -43,6 +43,30 @@ static bool is_nvidia(vk::PhysicalDevice physical_device) return props.vendorID == 0x10DE; } +static void split_bitrate(std::vector &encoders, uint64_t bitrate) +{ + double total_weight = 0; + for (auto& encoder: encoders) + { + double w = encoder.width * encoder.height; + switch (encoder.codec) + { + case xrt::drivers::wivrn::h264: + w *= 2; + break; + case xrt::drivers::wivrn::h265: + break; + } + encoder.bitrate = w; + total_weight += w; + } + + for (auto& encoder: encoders) + { + encoder.bitrate = encoder.bitrate * bitrate / total_weight; + } +} + static std::vector get_encoder_default_settings(vk::PhysicalDevice physical_device, uint16_t width, uint16_t height) { xrt::drivers::wivrn::encoder_settings settings{}; @@ -89,6 +113,7 @@ std::vector xrt::drivers::wivrn::get_encoder_settings(vk::Phys try { const auto & config = configuration::read_user_configuration(); + uint64_t bitrate = config.bitrate.value_or(default_bitrate); if (config.encoders.empty()) return get_encoder_default_settings(physical_device, width, height); std::vector res; @@ -103,7 +128,6 @@ std::vector xrt::drivers::wivrn::get_encoder_settings(vk::Phys settings.video_height = settings.height; settings.offset_x = std::ceil(encoder.offset_x.value_or(0) * width); settings.offset_y = std::ceil(encoder.offset_y.value_or(0) * height); - settings.bitrate = encoder.bitrate.value_or(default_bitrate); settings.codec = encoder.codec.value_or(xrt::drivers::wivrn::h264); settings.group = encoder.group.value_or(next_group); settings.options = encoder.options; @@ -112,6 +136,7 @@ std::vector xrt::drivers::wivrn::get_encoder_settings(vk::Phys next_group = std::max(next_group, settings.group + 1); res.push_back(settings); } + split_bitrate(res, bitrate); return res; } catch (const std::exception & e)