Get maximum size from nvenc capabilities
This commit is contained in:
@@ -23,8 +23,6 @@
|
||||
#include "math/m_space.h"
|
||||
#include "utils/scoped_lock.h"
|
||||
#include "xrt_cast.h"
|
||||
#include <condition_variable>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
@@ -99,7 +97,7 @@ struct encoder_thread_param
|
||||
|
||||
static void * comp_wivrn_present_thread(void * void_param);
|
||||
|
||||
static void create_encoders(wivrn_comp_target * cn, std::vector<encoder_settings> & _settings)
|
||||
static void create_encoders(wivrn_comp_target * cn)
|
||||
{
|
||||
auto vk = get_vk(cn);
|
||||
assert(cn->encoders.empty());
|
||||
@@ -109,12 +107,11 @@ static void create_encoders(wivrn_comp_target * cn, std::vector<encoder_settings
|
||||
auto & desc = cn->desc;
|
||||
desc.width = cn->width;
|
||||
desc.height = cn->height;
|
||||
desc.fps = cn->fps;
|
||||
desc.foveation = cn->cnx->get_foveation_parameters();
|
||||
desc.foveation = cn->cnx->set_foveated_size(desc.width, desc.height);
|
||||
|
||||
std::map<int, encoder_thread_param> thread_params;
|
||||
|
||||
for (auto & settings: _settings)
|
||||
for (auto & settings: cn->settings)
|
||||
{
|
||||
uint8_t stream_index = cn->encoders.size();
|
||||
auto & encoder = cn->encoders.emplace_back(
|
||||
@@ -139,7 +136,7 @@ static void create_encoders(wivrn_comp_target * cn, std::vector<encoder_settings
|
||||
cn->cnx->send_control(desc);
|
||||
}
|
||||
|
||||
static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags flags, const std::vector<xrt::drivers::wivrn::encoder_settings> & settings)
|
||||
static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags flags)
|
||||
{
|
||||
auto vk = get_vk(cn);
|
||||
assert(cn->image_count > 0);
|
||||
@@ -230,13 +227,24 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
|
||||
.queueFamilyIndex = vk->queue_family_index,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
catch (std::exception & e)
|
||||
{
|
||||
U_LOG_E("Compositor target init failed: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
cn->settings = get_encoder_settings(*cn->wivrn_bundle->physical_device, cn->c->settings.preferred.width, cn->c->settings.preferred.height);
|
||||
print_encoders(cn->settings);
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
U_LOG_E("Failed to create video encoder: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool comp_wivrn_check_ready(struct comp_target * ct)
|
||||
@@ -323,31 +331,16 @@ static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_
|
||||
ct->surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
|
||||
cn->image_count = 3;
|
||||
cn->format = ct->format;
|
||||
cn->width = create_info->extent.width;
|
||||
cn->height = create_info->extent.height;
|
||||
cn->color_space = create_info->color_space;
|
||||
|
||||
auto settings = get_encoder_settings(*cn->wivrn_bundle->physical_device, cn->width, cn->height);
|
||||
|
||||
VkResult res = create_images(cn, vk::ImageUsageFlags(create_info->image_usage), settings);
|
||||
VkResult res = create_images(cn, vk::ImageUsageFlags(create_info->image_usage));
|
||||
if (res != VK_SUCCESS)
|
||||
{
|
||||
vk_print_result(get_vk(cn), __FILE__, __LINE__, __func__, res, "create_images");
|
||||
// TODO
|
||||
abort();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
print_encoders(settings);
|
||||
create_encoders(cn, settings);
|
||||
}
|
||||
catch (const std::exception & e)
|
||||
{
|
||||
U_LOG_E("Failed to create video encoder: %s", e.what());
|
||||
abort();
|
||||
}
|
||||
create_encoders(cn);
|
||||
}
|
||||
|
||||
static bool comp_wivrn_has_images(struct comp_target * ct)
|
||||
@@ -670,5 +663,6 @@ wivrn_comp_target::wivrn_comp_target(std::shared_ptr<xrt::drivers::wivrn::wivrn_
|
||||
set_title = comp_wivrn_set_title;
|
||||
flush = comp_wivrn_flush;
|
||||
this->fps = fps;
|
||||
desc.fps = fps;
|
||||
this->c = c;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "vk/allocation.h"
|
||||
|
||||
#include "driver/wivrn_pacer.h"
|
||||
#include "encoder/encoder_settings.h"
|
||||
#include <condition_variable>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
@@ -92,6 +93,7 @@ struct wivrn_comp_target : public comp_target
|
||||
os_thread_helper_destroy(&thread);
|
||||
}
|
||||
};
|
||||
std::vector<encoder_settings> settings;
|
||||
to_headset::video_stream_description desc{};
|
||||
std::list<encoder_thread> encoder_threads;
|
||||
std::vector<std::shared_ptr<VideoEncoder>> encoders;
|
||||
|
||||
+54
-51
@@ -214,16 +214,6 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx,
|
||||
|
||||
auto eye_width = info.recommended_eye_width;
|
||||
auto eye_height = info.recommended_eye_height;
|
||||
auto scale = config.scale.value_or(std::array<double, 2>{0.8, 0.8});
|
||||
int foveated_eye_width = info.recommended_eye_width * scale[0];
|
||||
if (foveated_eye_width > 2048 and not config.scale)
|
||||
{
|
||||
scale[1] = scale[0] = 2048. / info.recommended_eye_width;
|
||||
U_LOG_I("Encoded video would be %d wide, setting scale to %f", 2 * foveated_eye_width, scale[0]);
|
||||
foveated_eye_width = info.recommended_eye_width * scale[0];
|
||||
}
|
||||
int foveated_eye_height = info.recommended_eye_height * scale[1];
|
||||
foveated_eye_height += foveated_eye_height % 2;
|
||||
|
||||
// Setup info.
|
||||
hmd->view_count = 2;
|
||||
@@ -231,62 +221,23 @@ wivrn_hmd::wivrn_hmd(std::shared_ptr<xrt::drivers::wivrn::wivrn_session> cnx,
|
||||
hmd->blend_mode_count = 1;
|
||||
hmd->distortion.models = XRT_DISTORTION_MODEL_COMPUTE;
|
||||
hmd->distortion.preferred = XRT_DISTORTION_MODEL_COMPUTE;
|
||||
hmd->screens[0].w_pixels = foveated_eye_width * 2;
|
||||
hmd->screens[0].h_pixels = foveated_eye_height;
|
||||
hmd->screens[0].w_pixels = eye_width * 2;
|
||||
hmd->screens[0].h_pixels = eye_height;
|
||||
hmd->screens[0].nominal_frame_interval_ns = 1000000000 / info.preferred_refresh_rate;
|
||||
|
||||
// Left
|
||||
hmd->views[0].display.w_pixels = eye_width;
|
||||
hmd->views[0].display.h_pixels = eye_height;
|
||||
hmd->views[0].viewport.x_pixels = 0;
|
||||
hmd->views[0].viewport.y_pixels = 0;
|
||||
hmd->views[0].viewport.w_pixels = foveated_eye_width;
|
||||
hmd->views[0].viewport.h_pixels = foveated_eye_height;
|
||||
hmd->views[0].rot = u_device_rotation_ident;
|
||||
|
||||
// Right
|
||||
hmd->views[1].display.w_pixels = eye_width;
|
||||
hmd->views[1].display.h_pixels = eye_height;
|
||||
hmd->views[1].viewport.x_pixels = foveated_eye_width;
|
||||
hmd->views[1].viewport.y_pixels = 0;
|
||||
hmd->views[1].viewport.w_pixels = foveated_eye_width;
|
||||
hmd->views[1].viewport.h_pixels = foveated_eye_height;
|
||||
hmd->views[1].rot = u_device_rotation_ident;
|
||||
|
||||
// FOV from headset info packet
|
||||
hmd->distortion.fov[0] = xrt_cast(info.fov[0]);
|
||||
hmd->distortion.fov[1] = xrt_cast(info.fov[1]);
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
foveation_parameters[i].x.scale = scale[0];
|
||||
foveation_parameters[i].y.scale = scale[1];
|
||||
const auto & view = hmd->views[i];
|
||||
const auto & fov = hmd->distortion.fov[i];
|
||||
float l = tan(fov.angle_left);
|
||||
float r = tan(fov.angle_right);
|
||||
float t = tan(fov.angle_up);
|
||||
float b = tan(fov.angle_down);
|
||||
if (scale[0] < 1)
|
||||
{
|
||||
float cu = (r + l) / (l - r);
|
||||
foveation_parameters[i].x.center = cu;
|
||||
|
||||
std::tie(foveation_parameters[i].x.a, foveation_parameters[i].x.b) = solve_foveation(scale[0], cu);
|
||||
}
|
||||
|
||||
if (scale[1] < 1)
|
||||
{
|
||||
float cv = (t + b) / (t - b);
|
||||
foveation_parameters[i].y.center = cv;
|
||||
|
||||
std::tie(foveation_parameters[i].y.a, foveation_parameters[i].y.b) = solve_foveation(scale[1], cv);
|
||||
}
|
||||
}
|
||||
|
||||
// Distortion information
|
||||
compute_distortion = wivrn_hmd_compute_distortion;
|
||||
u_distortion_mesh_fill_in_compute(this);
|
||||
}
|
||||
|
||||
void wivrn_hmd::update_inputs()
|
||||
@@ -347,6 +298,58 @@ void wivrn_hmd::get_view_poses(const xrt_vec3 * default_eye_relation,
|
||||
}
|
||||
}
|
||||
|
||||
decltype(wivrn_hmd::foveation_parameters) wivrn_hmd::set_foveated_size(uint32_t width, uint32_t height)
|
||||
{
|
||||
assert(width % 2 == 0);
|
||||
uint32_t eye_width = width / 2;
|
||||
std::array<double, 2> scale{
|
||||
double(eye_width) / hmd->views[0].display.w_pixels,
|
||||
double(height) / hmd->views[0].display.h_pixels,
|
||||
};
|
||||
|
||||
hmd->screens[0].w_pixels = width;
|
||||
hmd->screens[0].h_pixels = height;
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
auto & view = hmd->views[i];
|
||||
view.viewport.x_pixels = i * eye_width;
|
||||
view.viewport.y_pixels = 0;
|
||||
|
||||
view.viewport.w_pixels = eye_width;
|
||||
view.viewport.h_pixels = height;
|
||||
|
||||
foveation_parameters[i].x.scale = scale[0];
|
||||
foveation_parameters[i].y.scale = scale[1];
|
||||
|
||||
const auto & fov = hmd->distortion.fov[i];
|
||||
float l = tan(fov.angle_left);
|
||||
float r = tan(fov.angle_right);
|
||||
float t = tan(fov.angle_up);
|
||||
float b = tan(fov.angle_down);
|
||||
if (scale[0] < 1)
|
||||
{
|
||||
float cu = (r + l) / (l - r);
|
||||
foveation_parameters[i].x.center = cu;
|
||||
|
||||
std::tie(foveation_parameters[i].x.a, foveation_parameters[i].x.b) = solve_foveation(scale[0], cu);
|
||||
}
|
||||
|
||||
if (scale[1] < 1)
|
||||
{
|
||||
float cv = (t + b) / (t - b);
|
||||
foveation_parameters[i].y.center = cv;
|
||||
|
||||
std::tie(foveation_parameters[i].y.a, foveation_parameters[i].y.b) = solve_foveation(scale[1], cv);
|
||||
}
|
||||
}
|
||||
|
||||
// Distortion information
|
||||
compute_distortion = wivrn_hmd_compute_distortion;
|
||||
u_distortion_mesh_fill_in_compute(this);
|
||||
return foveation_parameters;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Functions
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "view_list.h"
|
||||
#include "wivrn_session.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
@@ -66,8 +67,5 @@ public:
|
||||
|
||||
void update_tracking(const from_headset::tracking &, const clock_offset &);
|
||||
|
||||
const auto & get_foveation_parameters()
|
||||
{
|
||||
return foveation_parameters;
|
||||
}
|
||||
decltype(foveation_parameters) set_foveated_size(uint32_t width, uint32_t height);
|
||||
};
|
||||
|
||||
@@ -292,9 +292,9 @@ void wivrn_session::run(std::weak_ptr<wivrn_session> weak_self)
|
||||
}
|
||||
}
|
||||
|
||||
std::array<to_headset::video_stream_description::foveation_parameter, 2> wivrn_session::get_foveation_parameters()
|
||||
std::array<to_headset::video_stream_description::foveation_parameter, 2> wivrn_session::set_foveated_size(uint32_t width, uint32_t height)
|
||||
{
|
||||
return hmd->get_foveation_parameters();
|
||||
return hmd->set_foveated_size(width, height);
|
||||
}
|
||||
|
||||
void wivrn_session::dump_time(const std::string & event, uint64_t frame, uint64_t time, uint8_t stream, const char * extra)
|
||||
|
||||
@@ -126,7 +126,7 @@ public:
|
||||
connection.send_control(std::forward<T>(packet));
|
||||
}
|
||||
|
||||
std::array<to_headset::video_stream_description::foveation_parameter, 2> get_foveation_parameters();
|
||||
std::array<to_headset::video_stream_description::foveation_parameter, 2> set_foveated_size(uint32_t width, uint32_t height);
|
||||
|
||||
void dump_time(const std::string & event, uint64_t frame, uint64_t time, uint8_t stream = -1, const char * extra = "");
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ struct encoder_settings : public to_headset::video_stream_description::item
|
||||
std::optional<std::string> device;
|
||||
};
|
||||
|
||||
std::vector<encoder_settings> get_encoder_settings(vk::PhysicalDevice physical_device, uint16_t width, uint16_t height);
|
||||
std::vector<encoder_settings> get_encoder_settings(vk::PhysicalDevice physical_device, uint32_t & width, uint32_t & height);
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
if (status != NV_ENC_SUCCESS) \
|
||||
{ \
|
||||
U_LOG_E("%s:%d: %d", __FILE__, __LINE__, status); \
|
||||
throw std::runtime_error("TODO"); \
|
||||
throw std::runtime_error("nvenc error"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
if (status != NV_ENC_SUCCESS) \
|
||||
{ \
|
||||
U_LOG_E("%s:%d: %d, %s", __FILE__, __LINE__, status, fn.nvEncGetLastErrorString(session_handle)); \
|
||||
throw std::runtime_error("TODO"); \
|
||||
throw std::runtime_error("nvenc error"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
@@ -73,37 +73,34 @@ void VideoEncoderNvenc::deleter::operator()(NvencFunctions * fn)
|
||||
nvenc_free_functions(&fn);
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, float fps) :
|
||||
vk(vk), fps(fps), bitrate(settings.bitrate)
|
||||
static auto init()
|
||||
{
|
||||
std::unique_ptr<CudaFunctions, VideoEncoderNvenc::deleter> cuda_fn;
|
||||
std::unique_ptr<NvencFunctions, VideoEncoderNvenc::deleter> nvenc_fn;
|
||||
void * session_handle;
|
||||
|
||||
{
|
||||
CudaFunctions * tmp = nullptr;
|
||||
if (cuda_load_functions(&tmp, nullptr))
|
||||
throw std::runtime_error("Failed to load CUDA");
|
||||
cuda_fn.reset(tmp);
|
||||
}
|
||||
|
||||
{
|
||||
NvencFunctions * tmp = nullptr;
|
||||
if (nvenc_load_functions(&tmp, nullptr))
|
||||
throw std::runtime_error("Failed to load nvenc");
|
||||
nvenc_fn.reset(tmp);
|
||||
}
|
||||
rect = vk::Rect2D{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
pitch = settings.video_width;
|
||||
|
||||
CU_CHECK(cuda_fn->cuInit(0));
|
||||
|
||||
CUcontext cuda;
|
||||
CU_CHECK(cuda_fn->cuCtxCreate(&cuda, 0, 0));
|
||||
|
||||
fn.version = NV_ENCODE_API_FUNCTION_LIST_VER;
|
||||
NV_ENCODE_API_FUNCTION_LIST fn{
|
||||
.version = NV_ENCODE_API_FUNCTION_LIST_VER,
|
||||
};
|
||||
NVENC_CHECK_NOENCODER(nvenc_fn->NvEncodeAPICreateInstance(&fn));
|
||||
|
||||
{
|
||||
@@ -120,9 +117,40 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
NVENC_CHECK_NOENCODER(fn.nvEncOpenEncodeSessionEx(¶ms, &session_handle));
|
||||
}
|
||||
|
||||
return std::make_tuple(std::move(cuda_fn), std::move(nvenc_fn), fn, cuda, session_handle);
|
||||
}
|
||||
|
||||
static auto encode_guid(video_codec codec)
|
||||
{
|
||||
switch (codec)
|
||||
{
|
||||
case h264:
|
||||
return NV_ENC_CODEC_H264_GUID;
|
||||
case h265:
|
||||
return NV_ENC_CODEC_HEVC_GUID;
|
||||
}
|
||||
throw std::out_of_range("Invalid codec " + std::to_string(codec));
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, float fps) :
|
||||
vk(vk), fps(fps), bitrate(settings.bitrate)
|
||||
{
|
||||
std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init();
|
||||
rect = vk::Rect2D{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
pitch = settings.video_width;
|
||||
|
||||
uint32_t count;
|
||||
std::vector<GUID> presets;
|
||||
auto encodeGUID = settings.codec == video_codec::h264 ? NV_ENC_CODEC_H264_GUID : NV_ENC_CODEC_HEVC_GUID;
|
||||
auto encodeGUID = encode_guid(settings.codec);
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetCount(session_handle, encodeGUID, &count));
|
||||
presets.resize(count);
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetGUIDs(session_handle, encodeGUID, presets.data(), count, &count));
|
||||
@@ -143,21 +171,17 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
printf(" Preset {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n", i.Data1, i.Data2, i.Data3, i.Data4[0], i.Data4[1], i.Data4[2], i.Data4[3], i.Data4[4], i.Data4[5], i.Data4[6], i.Data4[7]);
|
||||
}
|
||||
|
||||
NV_ENC_CAPS_PARAM cap_param{};
|
||||
cap_param.version = NV_ENC_CAPS_PARAM_VER;
|
||||
cap_param.capsToQuery = NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION;
|
||||
int cap_value;
|
||||
NVENC_CHECK(fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, &cap_value));
|
||||
|
||||
// auto presetGUID = codec == video_codec::h264 ? NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID :
|
||||
// NV_ENC_PRESET_P7_GUID;
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
auto presetGUID = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
|
||||
#pragma GCC diagnostic pop
|
||||
NV_ENC_PRESET_CONFIG preset_config;
|
||||
preset_config.version = NV_ENC_PRESET_CONFIG_VER;
|
||||
preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
|
||||
NV_ENC_PRESET_CONFIG preset_config{
|
||||
.version = NV_ENC_PRESET_CONFIG_VER,
|
||||
.presetCfg = {
|
||||
.version = NV_ENC_CONFIG_VER,
|
||||
}};
|
||||
NVENC_CHECK(fn.nvEncGetEncodePresetConfig(session_handle, encodeGUID, presetGUID, &preset_config));
|
||||
|
||||
NV_ENC_CONFIG params = preset_config.presetCfg;
|
||||
@@ -186,32 +210,25 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
break;
|
||||
}
|
||||
|
||||
NV_ENC_INITIALIZE_PARAMS params2{};
|
||||
params2.version = NV_ENC_INITIALIZE_PARAMS_VER;
|
||||
params2.encodeGUID = encodeGUID;
|
||||
params2.presetGUID = presetGUID;
|
||||
params2.encodeWidth = settings.video_width;
|
||||
params2.encodeHeight = settings.video_height;
|
||||
params2.darWidth = settings.video_width;
|
||||
params2.darHeight = settings.video_height;
|
||||
params2.frameRateNum = (uint32_t)fps;
|
||||
params2.frameRateDen = 1;
|
||||
params2.enableEncodeAsync = 0;
|
||||
params2.enablePTD = 1;
|
||||
params2.reportSliceOffsets = 0;
|
||||
params2.enableSubFrameWrite = 0;
|
||||
params2.enableExternalMEHints = 0;
|
||||
params2.enableMEOnlyMode = 0;
|
||||
params2.enableWeightedPrediction = 0;
|
||||
params2.enableOutputInVidmem = 0;
|
||||
params2.encodeConfig = ¶ms;
|
||||
params2.maxEncodeWidth = 0;
|
||||
params2.maxEncodeHeight = 0;
|
||||
params2.bufferFormat = NV_ENC_BUFFER_FORMAT_UNDEFINED;
|
||||
NV_ENC_INITIALIZE_PARAMS params2{
|
||||
.version = NV_ENC_INITIALIZE_PARAMS_VER,
|
||||
.encodeGUID = encodeGUID,
|
||||
.presetGUID = presetGUID,
|
||||
.encodeWidth = settings.video_width,
|
||||
.encodeHeight = settings.video_height,
|
||||
.darWidth = settings.video_width,
|
||||
.darHeight = settings.video_height,
|
||||
.frameRateNum = (uint32_t)fps,
|
||||
.frameRateDen = 1,
|
||||
.enableEncodeAsync = 0,
|
||||
.enablePTD = 1,
|
||||
.encodeConfig = ¶ms,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncInitializeEncoder(session_handle, ¶ms2));
|
||||
|
||||
NV_ENC_CREATE_BITSTREAM_BUFFER params3;
|
||||
params3.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
|
||||
NV_ENC_CREATE_BITSTREAM_BUFFER params3{
|
||||
.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncCreateBitstreamBuffer(session_handle, ¶ms3));
|
||||
bitstreamBuffer = params3.bitstreamBuffer;
|
||||
|
||||
@@ -250,6 +267,7 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
.handleType = vk::ExternalMemoryHandleTypeFlagBitsKHR::eOpaqueFd,
|
||||
});
|
||||
|
||||
CU_CHECK(cuda_fn->cuCtxPushCurrent(cuda));
|
||||
{
|
||||
CUDA_EXTERNAL_MEMORY_HANDLE_DESC param{
|
||||
.type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
|
||||
@@ -267,20 +285,27 @@ VideoEncoderNvenc::VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_setting
|
||||
CU_CHECK(cuda_fn->cuExternalMemoryGetMappedBuffer(&frame, extmem, &map_param));
|
||||
}
|
||||
|
||||
NV_ENC_REGISTER_RESOURCE param3{};
|
||||
param3.version = NV_ENC_REGISTER_RESOURCE_VER;
|
||||
param3.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
|
||||
param3.width = rect.extent.width;
|
||||
param3.height = rect.extent.height;
|
||||
param3.pitch = pitch;
|
||||
param3.resourceToRegister = (void *)frame;
|
||||
param3.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12;
|
||||
param3.bufferUsage = NV_ENC_INPUT_IMAGE;
|
||||
NV_ENC_REGISTER_RESOURCE param3{
|
||||
.version = NV_ENC_REGISTER_RESOURCE_VER,
|
||||
.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR,
|
||||
.width = rect.extent.width,
|
||||
.height = rect.extent.height,
|
||||
.pitch = pitch,
|
||||
.resourceToRegister = (void *)frame,
|
||||
.bufferFormat = NV_ENC_BUFFER_FORMAT_NV12,
|
||||
.bufferUsage = NV_ENC_INPUT_IMAGE,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncRegisterResource(session_handle, ¶m3));
|
||||
nvenc_resource = param3.registeredResource;
|
||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
||||
}
|
||||
|
||||
VideoEncoderNvenc::~VideoEncoderNvenc()
|
||||
{
|
||||
if (session_handle)
|
||||
fn.nvEncDestroyEncoder(session_handle);
|
||||
}
|
||||
|
||||
void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf)
|
||||
{
|
||||
cmd_buf.copyImageToBuffer(
|
||||
@@ -288,7 +313,7 @@ void VideoEncoderNvenc::PresentImage(yuv_converter & src_yuv, vk::raii::CommandB
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
*yuv_buffer,
|
||||
vk::BufferImageCopy{
|
||||
.bufferRowLength = uint32_t(pitch),
|
||||
.bufferRowLength = pitch,
|
||||
.imageSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.layerCount = 1,
|
||||
@@ -334,32 +359,69 @@ void VideoEncoderNvenc::Encode(bool idr, std::chrono::steady_clock::time_point p
|
||||
param4.registeredResource = nvenc_resource;
|
||||
NVENC_CHECK(fn.nvEncMapInputResource(session_handle, ¶m4));
|
||||
|
||||
NV_ENC_PIC_PARAMS param{};
|
||||
param.version = NV_ENC_PIC_PARAMS_VER;
|
||||
param.inputWidth = rect.extent.width;
|
||||
param.inputHeight = rect.extent.height;
|
||||
param.inputPitch = pitch;
|
||||
param.encodePicFlags = (uint32_t)(idr ? NV_ENC_PIC_TYPE_IDR : NV_ENC_PIC_TYPE_P);
|
||||
param.frameIdx = 0;
|
||||
param.inputTimeStamp = 0;
|
||||
param.inputBuffer = param4.mappedResource;
|
||||
param.outputBitstream = bitstreamBuffer;
|
||||
param.bufferFmt = param4.mappedBufferFmt;
|
||||
param.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
|
||||
NV_ENC_PIC_PARAMS param{
|
||||
.version = NV_ENC_PIC_PARAMS_VER,
|
||||
.inputWidth = rect.extent.width,
|
||||
.inputHeight = rect.extent.height,
|
||||
.inputPitch = pitch,
|
||||
.encodePicFlags = uint32_t(idr ? NV_ENC_PIC_TYPE_IDR : NV_ENC_PIC_TYPE_P),
|
||||
.frameIdx = 0,
|
||||
.inputTimeStamp = 0,
|
||||
.inputBuffer = param4.mappedResource,
|
||||
.outputBitstream = bitstreamBuffer,
|
||||
.bufferFmt = param4.mappedBufferFmt,
|
||||
.pictureStruct = NV_ENC_PIC_STRUCT_FRAME,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncEncodePicture(session_handle, ¶m));
|
||||
|
||||
NV_ENC_LOCK_BITSTREAM param2{};
|
||||
param2.version = NV_ENC_LOCK_BITSTREAM_VER;
|
||||
param2.doNotWait = 0;
|
||||
param2.outputBitstream = bitstreamBuffer;
|
||||
NV_ENC_LOCK_BITSTREAM param2{
|
||||
.version = NV_ENC_LOCK_BITSTREAM_VER,
|
||||
.doNotWait = 0,
|
||||
.outputBitstream = bitstreamBuffer,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncLockBitstream(session_handle, ¶m2));
|
||||
|
||||
SendData(
|
||||
{(uint8_t *)param2.bitstreamBufferPtr, (uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes}, true);
|
||||
SendData({
|
||||
(uint8_t *)param2.bitstreamBufferPtr,
|
||||
(uint8_t *)param2.bitstreamBufferPtr + param2.bitstreamSizeInBytes,
|
||||
},
|
||||
true);
|
||||
|
||||
NVENC_CHECK(fn.nvEncUnlockBitstream(session_handle, bitstreamBuffer));
|
||||
|
||||
CU_CHECK(cuda_fn->cuCtxPopCurrent(NULL));
|
||||
}
|
||||
|
||||
std::array<int, 2> VideoEncoderNvenc::get_max_size(video_codec codec)
|
||||
{
|
||||
auto [cuda_fn, nvenc_fn, fn, cuda, session_handle] = init();
|
||||
std::array<int, 2> result;
|
||||
std::exception_ptr ex;
|
||||
try
|
||||
{
|
||||
auto encodeGUID = encode_guid(codec);
|
||||
for (auto [cap, res]: {
|
||||
std::pair{NV_ENC_CAPS_WIDTH_MAX, &result[0]},
|
||||
{NV_ENC_CAPS_WIDTH_MAX, &result[1]},
|
||||
})
|
||||
{
|
||||
NV_ENC_CAPS_PARAM cap_param{
|
||||
.version = NV_ENC_CAPS_PARAM_VER,
|
||||
.capsToQuery = NV_ENC_CAPS_WIDTH_MAX,
|
||||
};
|
||||
NVENC_CHECK(fn.nvEncGetEncodeCaps(session_handle, encodeGUID, &cap_param, res));
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
ex = std::current_exception();
|
||||
}
|
||||
cuda_fn->cuCtxPopCurrent(NULL);
|
||||
fn.nvEncDestroyEncoder(session_handle);
|
||||
if (ex)
|
||||
std::rethrow_exception(ex);
|
||||
U_LOG_D("nvenc maximum encoded size: %dx%d", result[0], result[1]);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "video_encoder.h"
|
||||
#include <array>
|
||||
#include <ffnvcodec/dynlink_cuda.h>
|
||||
#include <ffnvcodec/dynlink_loader.h>
|
||||
#include <ffnvcodec/nvEncodeAPI.h>
|
||||
@@ -30,11 +31,14 @@ namespace xrt::drivers::wivrn
|
||||
|
||||
class VideoEncoderNvenc : public VideoEncoder
|
||||
{
|
||||
public:
|
||||
struct deleter
|
||||
{
|
||||
void operator()(CudaFunctions * fn);
|
||||
void operator()(NvencFunctions * fn);
|
||||
};
|
||||
|
||||
private:
|
||||
wivrn_vk_bundle & vk;
|
||||
// relevant part of the input image to encode
|
||||
vk::Rect2D rect;
|
||||
@@ -43,7 +47,7 @@ class VideoEncoderNvenc : public VideoEncoder
|
||||
std::unique_ptr<NvencFunctions, deleter> nvenc_fn;
|
||||
NV_ENCODE_API_FUNCTION_LIST fn;
|
||||
CUcontext cuda;
|
||||
void * session_handle;
|
||||
void * session_handle = nullptr;
|
||||
NV_ENC_OUTPUT_PTR bitstreamBuffer;
|
||||
|
||||
vk::raii::Buffer yuv_buffer = nullptr;
|
||||
@@ -53,16 +57,19 @@ class VideoEncoderNvenc : public VideoEncoder
|
||||
vk::Image luma;
|
||||
vk::Image chroma;
|
||||
CUdeviceptr frame;
|
||||
size_t pitch;
|
||||
uint32_t pitch;
|
||||
NV_ENC_REGISTERED_PTR nvenc_resource;
|
||||
float fps;
|
||||
int bitrate;
|
||||
|
||||
public:
|
||||
VideoEncoderNvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, float fps);
|
||||
~VideoEncoderNvenc();
|
||||
|
||||
void PresentImage(yuv_converter & src_yuv, vk::raii::CommandBuffer & cmd_buf) override;
|
||||
void Encode(bool idr, std::chrono::steady_clock::time_point pts) override;
|
||||
|
||||
static std::array<int, 2> get_max_size(video_codec);
|
||||
};
|
||||
|
||||
} // namespace xrt::drivers::wivrn
|
||||
|
||||
Reference in New Issue
Block a user