Select compatible codec for vaapi when no config is given

This commit is contained in:
Patrick Nicolas
2024-08-31 22:07:56 +02:00
parent a34b8ba928
commit 481c922143
7 changed files with 82 additions and 14 deletions
+5 -1
View File
@@ -210,7 +210,11 @@ static bool comp_wivrn_init_post_vulkan(struct comp_target * ct, uint32_t prefer
try
{
cn->settings = get_encoder_settings(*cn->wivrn_bundle->physical_device, cn->c->settings.preferred.width, cn->c->settings.preferred.height);
cn->settings = get_encoder_settings(
*cn->wivrn_bundle,
cn->c->settings.preferred.width,
cn->c->settings.preferred.height,
cn->cnx->get_info().supported_codecs);
print_encoders(cn->settings);
}
catch (const std::exception & e)
+6 -6
View File
@@ -117,7 +117,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
return XRT_ERROR_DEVICE_CREATION_FAILED;
}
const auto & info = std::get<from_headset::headset_info_packet>(*control);
self->info = std::get<from_headset::headset_info_packet>(*control);
try
{
@@ -126,7 +126,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
"WiVRn(microphone)",
"wivrn.sink",
"WiVRn",
info,
self->info,
*self);
if (self->audio_handle)
self->send_control(self->audio_handle->description());
@@ -136,7 +136,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
U_LOG_E("Failed to register audio device: %s", e.what());
return XRT_ERROR_DEVICE_CREATION_FAILED;
}
self->hmd = std::make_unique<wivrn_hmd>(self, info);
self->hmd = std::make_unique<wivrn_hmd>(self, self->info);
self->left_hand = std::make_unique<wivrn_controller>(0, self->hmd.get(), self);
self->right_hand = std::make_unique<wivrn_controller>(1, self->hmd.get(), self);
@@ -160,14 +160,14 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
devices->xdevs[n++] = self->right_hand.get();
usysds->base.base.static_roles.hand_tracking.right = self->right_hand.get();
}
if (info.eye_gaze)
if (self->info.eye_gaze)
{
self->eye_tracker = std::make_unique<wivrn_eye_tracker>(self->hmd.get(), self);
self->foveation = std::make_unique<wivrn_foveation>();
usysds->base.base.static_roles.eyes = self->eye_tracker.get();
devices->xdevs[n++] = self->eye_tracker.get();
}
if (info.face_tracking2_fb)
if (self->info.face_tracking2_fb)
{
self->fb_face2_tracker = std::make_unique<wivrn_fb_face2_tracker>(self->hmd.get(), self);
usysds->base.base.static_roles.face = self->fb_face2_tracker.get();
@@ -177,7 +177,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi
u_system_devices_static_finalize(usysds, self->left_hand.get(), self->right_hand.get());
wivrn_comp_target_factory ctf(self, info.preferred_refresh_rate);
wivrn_comp_target_factory ctf(self, self->info.preferred_refresh_rate);
auto xret = comp_main_create_system_compositor(self->hmd.get(), &ctf, out_xsysc);
if (xret != XRT_SUCCESS)
{
+5
View File
@@ -72,6 +72,7 @@ class wivrn_session : public std::enable_shared_from_this<wivrn_session>
{
friend wivrn_comp_target_factory;
wivrn_connection connection;
from_headset::headset_info_packet info{};
u_system & xrt_system;
xrt_space_overseer * space_overseer;
@@ -109,6 +110,10 @@ public:
clock_offset get_offset();
bool connected();
const from_headset::headset_info_packet & get_info()
{
return info;
};
void add_predict_offset(std::chrono::nanoseconds off)
{
+45 -5
View File
@@ -20,6 +20,7 @@
#include "encoder_settings.h"
#include "driver/configuration.h"
#include "util/u_logging.h"
#include "utils/wivrn_vk_bundle.h"
#include "video_encoder.h"
#include <cmath>
@@ -115,9 +116,39 @@ static void check_scale(std::string_view encoder_name, video_codec codec, uint16
#endif
}
static std::vector<configuration::encoder> get_encoder_default_settings(vk::PhysicalDevice physical_device)
static std::optional<xrt::drivers::wivrn::video_codec> filter_codecs(wivrn_vk_bundle & bundle, const std::vector<xrt::drivers::wivrn::video_codec> & codecs)
{
if (is_nvidia(physical_device))
VideoEncoderFFMPEG::mute_logs mute;
encoder_settings s{
{
.width = 800,
.height = 600,
.video_width = 800,
.video_height = 600,
},
"vaapi",
default_bitrate,
};
for (auto codec: codecs)
{
try
{
s.codec = codec;
video_encoder_va test(bundle, s, 60);
return codec;
}
catch (...)
{}
U_LOG_I("Video codec %s not supported", std::string(magic_enum::enum_name(codec)).c_str());
}
return {};
}
static std::vector<configuration::encoder> get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector<xrt::drivers::wivrn::video_codec> & headset_codecs)
{
if (is_nvidia(*bundle.physical_device))
{
#ifdef WIVRN_USE_NVENC
return {{.name = encoder_nvenc}};
@@ -132,6 +163,12 @@ static std::vector<configuration::encoder> get_encoder_default_settings(vk::Phys
else
{
#ifdef WIVRN_USE_VAAPI
auto codec = filter_codecs(bundle, headset_codecs);
if (not codec)
{
U_LOG_W("Failed to initialize vaapi, fallback to software encoding");
return {{.name = encoder_x264, .codec = h264}};
}
#ifdef WIVRN_SPLIT_ENCODERS
/* Split in 3 parts:
* +--------+--------+
@@ -154,6 +191,7 @@ static std::vector<configuration::encoder> get_encoder_default_settings(vk::Phys
.width = 0.5,
.height = 0.25,
.group = 0,
.codec = *codec,
},
{
.name = encoder_vaapi,
@@ -161,16 +199,18 @@ static std::vector<configuration::encoder> get_encoder_default_settings(vk::Phys
.height = 0.75,
.offset_y = 0.25,
.group = 0,
.codec = *codec,
},
{
.name = encoder_vaapi,
.width = 0.5,
.offset_x = 0.5,
.group = 0,
.codec = *codec,
},
};
#else
return {{.name = encoder_vaapi}};
return {{.name = encoder_vaapi, .codec = *codec}};
#endif
#elif defined(WIVRN_USE_X264)
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available");
@@ -188,7 +228,7 @@ 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, uint32_t & width, uint32_t & height)
std::vector<encoder_settings> xrt::drivers::wivrn::get_encoder_settings(wivrn_vk_bundle & bundle, uint32_t & width, uint32_t & height, const std::vector<xrt::drivers::wivrn::video_codec> & headset_codecs)
{
configuration config;
try
@@ -200,7 +240,7 @@ std::vector<encoder_settings> xrt::drivers::wivrn::get_encoder_settings(vk::Phys
U_LOG_E("Failed to read encoder configuration: %s", e.what());
}
if (config.encoders.empty())
config.encoders = get_encoder_default_settings(physical_device);
config.encoders = get_encoder_default_settings(bundle, headset_codecs);
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)
+4 -2
View File
@@ -23,7 +23,9 @@
#include <map>
#include <string>
#include <vulkan/vulkan.hpp>
#include <vector>
struct wivrn_vk_bundle;
namespace xrt::drivers::wivrn
{
@@ -39,7 +41,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, uint32_t & width, uint32_t & height);
std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle &, uint32_t & width, uint32_t & height, const std::vector<xrt::drivers::wivrn::video_codec> & headset_codecs);
} // namespace xrt::drivers::wivrn
@@ -83,3 +83,14 @@ std::optional<xrt::drivers::wivrn::VideoEncoder::data> VideoEncoderFFMPEG::encod
}
throw std::runtime_error("frame encoding failed, code " + std::to_string(err));
}
VideoEncoderFFMPEG::mute_logs::mute_logs()
{
if (not getenv("FFMPEG_LOG_LEVEL"))
av_log_set_level(AV_LOG_QUIET);
}
VideoEncoderFFMPEG::mute_logs::~mute_logs()
{
set_log_level();
}
@@ -30,6 +30,12 @@ public:
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point target_timestamp) override;
struct mute_logs
{
mute_logs();
~mute_logs();
};
protected:
VideoEncoderFFMPEG() :
xrt::drivers::wivrn::VideoEncoder(true) {}