mirror of
https://github.com/WiVRn/WiVRn.git
synced 2026-11-21 09:31:33 -08:00
Get maximum size from nvenc capabilities
This commit is contained in:
1 parent
64e6e32219
commit
bef30c3b91
10 files changed
+326
-229
No files matched your search
@@ -29,6 +29,9 @@
|
||||
|
||||
#include "wivrn_config.h"
|
||||
|
||||
#ifdef WIVRN_USE_NVENC
|
||||
#include "video_encoder_nvenc.h"
|
||||
#endif
|
||||
#ifdef WIVRN_USE_VAAPI
|
||||
#include "ffmpeg/video_encoder_va.h"
|
||||
#endif
|
||||
@@ -89,34 +92,44 @@ void print_encoders(const std::vector<xrt::drivers::wivrn::encoder_settings> & e
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<xrt::drivers::wivrn::encoder_settings> get_encoder_default_settings(vk::PhysicalDevice physical_device, uint16_t width, uint16_t height, uint64_t bitrate)
|
||||
static void check_scale(std::string_view encoder_name, video_codec codec, uint16_t width, uint16_t height, std::array<double, 2> & scale)
|
||||
{
|
||||
xrt::drivers::wivrn::encoder_settings settings{};
|
||||
settings.width = width;
|
||||
settings.height = height;
|
||||
settings.codec = xrt::drivers::wivrn::h265;
|
||||
settings.bitrate = bitrate;
|
||||
|
||||
settings.video_height = settings.height;
|
||||
settings.video_width = settings.width;
|
||||
#ifdef WIVRN_USE_NVENC
|
||||
if (encoder_name == encoder_nvenc)
|
||||
{
|
||||
auto max = VideoEncoderNvenc::get_max_size(codec);
|
||||
if (width * scale[0] > max[0])
|
||||
{
|
||||
scale[0] = double(max[0] - 1) / width;
|
||||
U_LOG_W("Image is too wide for encoder, reducing scale to %f", scale[0]);
|
||||
}
|
||||
if (height * scale[1] > max[1])
|
||||
{
|
||||
scale[1] = double(max[1] - 1) / width;
|
||||
U_LOG_W("Image is too tall for encoder, reducing scale to %f", scale[0]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static std::vector<configuration::encoder> get_encoder_default_settings(vk::PhysicalDevice physical_device)
|
||||
{
|
||||
if (is_nvidia(physical_device))
|
||||
{
|
||||
#ifdef WIVRN_USE_NVENC
|
||||
settings.encoder_name = encoder_nvenc;
|
||||
return {{.name = encoder_nvenc}};
|
||||
#elif defined(WIVRN_USE_X264)
|
||||
settings.encoder_name = encoder_x264;
|
||||
settings.codec = xrt::drivers::wivrn::h264;
|
||||
U_LOG_W("nvidia GPU detected, but cuda support not compiled");
|
||||
U_LOG_W("nvidia GPU detected, but nvenc support not compiled");
|
||||
return {{.name = encoder_x264, .codec = h264}};
|
||||
#else
|
||||
U_LOG_E("no suitable encoder available (compile with x264 or cuda support)");
|
||||
U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)");
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef WIVRN_USE_VAAPI
|
||||
settings.encoder_name = encoder_vaapi;
|
||||
return {{.name = encoder_vaapi}};
|
||||
// Split encoders have been reported to cause issues, don't use them in default configuration
|
||||
#ifdef WIVRN_SPLIT_ENCODERS
|
||||
/* Split in 3 parts:
|
||||
@@ -134,33 +147,36 @@ static std::vector<xrt::drivers::wivrn::encoder_settings> get_encoder_default_se
|
||||
* Decoder can start work as fast as possible, reducing idle time.
|
||||
*
|
||||
*/
|
||||
settings.width = std::ceil(width * 0.5);
|
||||
std::vector<xrt::drivers::wivrn::encoder_settings> encoders(3, settings);
|
||||
|
||||
encoders[0].height = std::ceil(height * 0.25);
|
||||
encoders[0].height += encoders[0].height % 2;
|
||||
encoders[1].height = height - encoders[0].height;
|
||||
encoders[1].offset_y = encoders[0].height;
|
||||
encoders[2].offset_x = settings.width;
|
||||
for (auto & e: encoders)
|
||||
{
|
||||
e.video_width = e.width;
|
||||
e.video_height = e.height;
|
||||
}
|
||||
split_bitrate(encoders, bitrate);
|
||||
return encoders;
|
||||
return {
|
||||
{
|
||||
.name = encoder_vaapi,
|
||||
.width = 0.5,
|
||||
.height = 0.25,
|
||||
.group = 0,
|
||||
},
|
||||
{
|
||||
.name = encoder_vaapi,
|
||||
.width = 0.5,
|
||||
.height = 0.75,
|
||||
.offset_y = 0.25,
|
||||
.group = 0,
|
||||
},
|
||||
{
|
||||
.name = encoder_vaapi,
|
||||
.width = 0.5,
|
||||
.offset_y = 0.5,
|
||||
.group = 0,
|
||||
},
|
||||
};
|
||||
#endif
|
||||
#elif defined(WIVRN_USE_X264)
|
||||
settings.encoder_name = encoder_x264;
|
||||
settings.codec = xrt::drivers::wivrn::h264;
|
||||
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available");
|
||||
return {{.name = encoder_x264, .codec = h264}};
|
||||
#else
|
||||
U_LOG_E("no suitable encoder available (compile with x264 or ffmpeg support)");
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
return {settings};
|
||||
}
|
||||
|
||||
static void make_even(uint16_t & value, uint16_t max)
|
||||
@@ -169,42 +185,57 @@ static void make_even(uint16_t & value, uint16_t max)
|
||||
value = std::min(value, max);
|
||||
}
|
||||
|
||||
std::vector<encoder_settings> xrt::drivers::wivrn::get_encoder_settings(vk::PhysicalDevice physical_device, uint16_t width, uint16_t height)
|
||||
std::vector<encoder_settings> xrt::drivers::wivrn::get_encoder_settings(vk::PhysicalDevice physical_device, uint32_t & width, uint32_t & height)
|
||||
{
|
||||
configuration config;
|
||||
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, bitrate);
|
||||
std::vector<xrt::drivers::wivrn::encoder_settings> res;
|
||||
int next_group = 0;
|
||||
for (const auto & encoder: config.encoders)
|
||||
{
|
||||
xrt::drivers::wivrn::encoder_settings settings{};
|
||||
settings.encoder_name = encoder.name;
|
||||
settings.width = std::ceil(encoder.width.value_or(1) * width);
|
||||
settings.height = std::ceil(encoder.height.value_or(1) * height);
|
||||
settings.video_width = settings.width;
|
||||
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);
|
||||
make_even(settings.width, width - settings.offset_x);
|
||||
make_even(settings.height, height - settings.offset_y);
|
||||
settings.codec = encoder.codec.value_or(xrt::drivers::wivrn::h264);
|
||||
settings.group = encoder.group.value_or(next_group);
|
||||
settings.options = encoder.options;
|
||||
settings.device = encoder.device;
|
||||
|
||||
next_group = std::max(next_group, settings.group + 1);
|
||||
res.push_back(settings);
|
||||
}
|
||||
split_bitrate(res, bitrate);
|
||||
return res;
|
||||
config = configuration::read_user_configuration();
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
U_LOG_E("Failed to read encoder configuration: %s", e.what());
|
||||
}
|
||||
return get_encoder_default_settings(physical_device, width, height, default_bitrate);
|
||||
if (config.encoders.empty())
|
||||
config.encoders = get_encoder_default_settings(physical_device);
|
||||
uint64_t bitrate = config.bitrate.value_or(default_bitrate);
|
||||
auto scale = config.scale.value_or(std::array<double, 2>{0.8, 0.8});
|
||||
for (const auto & encoder: config.encoders)
|
||||
{
|
||||
check_scale(encoder.name,
|
||||
encoder.codec.value_or(xrt::drivers::wivrn::h264),
|
||||
std::ceil(encoder.width.value_or(1) * width),
|
||||
std::ceil(encoder.height.value_or(1) * height),
|
||||
scale);
|
||||
}
|
||||
|
||||
width *= scale[0];
|
||||
width += width % 2;
|
||||
height *= scale[1];
|
||||
height += height % 2;
|
||||
|
||||
std::vector<xrt::drivers::wivrn::encoder_settings> res;
|
||||
int next_group = 0;
|
||||
for (const auto & encoder: config.encoders)
|
||||
{
|
||||
xrt::drivers::wivrn::encoder_settings settings{};
|
||||
settings.encoder_name = encoder.name;
|
||||
settings.width = std::ceil(encoder.width.value_or(1) * width);
|
||||
settings.height = std::ceil(encoder.height.value_or(1) * height);
|
||||
settings.video_width = settings.width;
|
||||
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);
|
||||
make_even(settings.width, width - settings.offset_x);
|
||||
make_even(settings.height, height - settings.offset_y);
|
||||
settings.codec = encoder.codec.value_or(xrt::drivers::wivrn::h265);
|
||||
settings.group = encoder.group.value_or(next_group);
|
||||
settings.options = encoder.options;
|
||||
settings.device = encoder.device;
|
||||
|
||||
next_group = std::max(next_group, settings.group + 1);
|
||||
res.push_back(settings);
|
||||
}
|
||||
split_bitrate(res, bitrate);
|
||||
return res;
|
||||
}
|
||||
Reference in new issue
Block a user