Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -200,6 +200,9 @@ configuration::configuration()
|
||||
if (auto it = json.find("use-steamvr-lh"); it != json.end())
|
||||
use_steamvr_lh = *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;
|
||||
else if (auto it = json.find("tcp_only"); it != json.end())
|
||||
|
||||
@@ -57,6 +57,7 @@ struct configuration
|
||||
std::vector<encoder> encoders;
|
||||
std::optional<encoder> encoder_passthrough;
|
||||
std::optional<int> bitrate;
|
||||
int bit_depth = 8;
|
||||
std::optional<std::array<double, 2>> scale;
|
||||
std::optional<std::array<float, 3>> grip_surface;
|
||||
std::vector<std::string> application;
|
||||
|
||||
@@ -179,6 +179,13 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
|
||||
auto format = vk::Format(cn->format);
|
||||
|
||||
bool is_10bit = format == vk::Format::eG10X6B10X6R10X62Plane420Unorm3Pack16;
|
||||
|
||||
std::array formats = {
|
||||
is_10bit ? vk::Format::eR10X6UnormPack16 : vk::Format::eR8Unorm,
|
||||
is_10bit ? vk::Format::eR10X6G10X6Unorm2Pack16 : vk::Format::eR8G8Unorm,
|
||||
format};
|
||||
|
||||
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count);
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
@@ -193,11 +200,6 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
cn->psc.images.resize(cn->image_count);
|
||||
for (uint32_t i = 0; i < cn->image_count; i++)
|
||||
{
|
||||
std::array formats = {
|
||||
vk::Format::eR8Unorm,
|
||||
vk::Format::eR8G8Unorm,
|
||||
format,
|
||||
};
|
||||
vk::ImageFormatListCreateInfo formats_info{
|
||||
.viewFormatCount = formats.size(),
|
||||
.pViewFormats = formats.data(),
|
||||
@@ -249,7 +251,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
.pNext = &usage,
|
||||
.image = item.image,
|
||||
.viewType = vk::ImageViewType::e2DArray,
|
||||
.format = vk::Format::eR8Unorm,
|
||||
.format = formats[0],
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
|
||||
.levelCount = 1,
|
||||
@@ -261,7 +263,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
.pNext = &usage,
|
||||
.image = item.image,
|
||||
.viewType = vk::ImageViewType::e2DArray,
|
||||
.format = vk::Format::eR8G8Unorm,
|
||||
.format = formats[1],
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
|
||||
.levelCount = 1,
|
||||
@@ -407,8 +409,12 @@ static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_
|
||||
|
||||
target_init_semaphores(cn);
|
||||
|
||||
// FIXME: select preferred format
|
||||
ct->format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
|
||||
// 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;
|
||||
|
||||
ct->width = create_info->extent.width;
|
||||
ct->height = create_info->extent.height;
|
||||
ct->surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
#include "video_encoder.h"
|
||||
|
||||
#include "wivrn_packets.h"
|
||||
#include <cmath>
|
||||
#include <magic_enum.hpp>
|
||||
#include <string>
|
||||
@@ -91,8 +92,9 @@ void print_encoders(const std::vector<wivrn::encoder_settings> & encoders)
|
||||
group = encoder.group;
|
||||
U_LOG_I("Group %d", group);
|
||||
}
|
||||
|
||||
std::string codec(magic_enum::enum_name(encoder.codec));
|
||||
U_LOG_I("\t%s (%s)", encoder.encoder_name.c_str(), codec.c_str());
|
||||
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,
|
||||
@@ -141,7 +143,7 @@ static constexpr auto ffmpeg_version()
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & codecs)
|
||||
static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & codecs, int bit_depth)
|
||||
{
|
||||
video_encoder_ffmpeg::mute_logs mute;
|
||||
encoder_settings s{
|
||||
@@ -154,11 +156,18 @@ static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & b
|
||||
"vaapi",
|
||||
default_bitrate,
|
||||
};
|
||||
s.bit_depth = bit_depth;
|
||||
|
||||
for (auto codec: codecs)
|
||||
{
|
||||
if constexpr (ffmpeg_version()[0] < 6)
|
||||
if (codec == wivrn::video_codec::h264)
|
||||
{
|
||||
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;
|
||||
@@ -209,14 +218,16 @@ static bool probe_nvenc(wivrn_vk_bundle & bundle)
|
||||
}
|
||||
#endif
|
||||
|
||||
static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, configuration::encoder & config)
|
||||
static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, configuration::encoder & config, int bit_depth)
|
||||
{
|
||||
if (config.name.empty())
|
||||
{
|
||||
if (is_nvidia(*bundle.physical_device))
|
||||
{
|
||||
#if WIVRN_USE_NVENC
|
||||
if (probe_nvenc(bundle))
|
||||
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
|
||||
#else
|
||||
@@ -224,8 +235,13 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
|
||||
#endif
|
||||
{
|
||||
#if WIVRN_USE_X264
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
if (bit_depth != 8)
|
||||
U_LOG_E("no encoder found with %d-bit support (set 8-bit to use x264)", bit_depth);
|
||||
else
|
||||
{
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
}
|
||||
#else
|
||||
U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)");
|
||||
#endif
|
||||
@@ -236,9 +252,14 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
|
||||
#if WIVRN_USE_VAAPI
|
||||
config.name = encoder_vaapi;
|
||||
#elif WIVRN_USE_X264
|
||||
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available");
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
if (bit_depth != 8)
|
||||
U_LOG_E("no encoder found with %d-bit support (set 8-bit to use x264)", bit_depth);
|
||||
else
|
||||
{
|
||||
U_LOG_W("ffmpeg support not compiled, vaapi encoder not available");
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
}
|
||||
#else
|
||||
U_LOG_E("no suitable encoder available (compile with x264 or nvenc support)");
|
||||
#endif
|
||||
@@ -248,32 +269,41 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
|
||||
#if WIVRN_USE_VAAPI
|
||||
if (config.name == encoder_vaapi and not config.codec)
|
||||
{
|
||||
config.codec = filter_codecs_vaapi(bundle, headset_codecs);
|
||||
config.codec = filter_codecs_vaapi(bundle, headset_codecs, bit_depth);
|
||||
if (not config.codec)
|
||||
{
|
||||
U_LOG_W("Failed to initialize vaapi, fallback to software encoding");
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
#if WIVRN_USE_X264
|
||||
if (bit_depth != 8)
|
||||
U_LOG_E("Failed to initialize vaapi, but can't use x264 due to %d-bit encoding (set 8-bit to use x264)", bit_depth);
|
||||
else
|
||||
{
|
||||
U_LOG_W("Failed to initialize vaapi, fallback to software encoding");
|
||||
config.name = encoder_x264;
|
||||
config.codec = h264;
|
||||
}
|
||||
#else
|
||||
U_LOG_E("Failed to initialize vaapi");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (config.name == encoder_vulkan and not config.codec)
|
||||
config.codec = h264;
|
||||
config.codec = h264; // this will fail if 10-bit is enabled
|
||||
|
||||
#if WIVRN_USE_X264
|
||||
if (config.name == encoder_x264)
|
||||
config.codec = h264;
|
||||
config.codec = h264; // this will fail if 10-bit is enabled
|
||||
#endif
|
||||
|
||||
if (not config.codec)
|
||||
config.codec = h264;
|
||||
config.codec = bit_depth == 10 ? h265 : h264;
|
||||
}
|
||||
|
||||
static std::vector<configuration::encoder> get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs)
|
||||
static std::vector<configuration::encoder> get_encoder_default_settings(wivrn_vk_bundle & bundle, const std::vector<wivrn::video_codec> & headset_codecs, int bit_depth)
|
||||
{
|
||||
configuration::encoder base;
|
||||
fill_defaults(bundle, headset_codecs, base);
|
||||
fill_defaults(bundle, headset_codecs, base, bit_depth);
|
||||
|
||||
#ifdef WIVRN_SPLIT_ENCODERS
|
||||
if (base.name != encoder_x264 and base.name != encoder_nvenc)
|
||||
@@ -330,8 +360,12 @@ static uint16_t align(uint16_t value, uint16_t alignment)
|
||||
std::vector<encoder_settings> 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.encoders = get_encoder_default_settings(bundle, info.supported_codecs, config.bit_depth);
|
||||
if (not config.encoder_passthrough)
|
||||
config.encoder_passthrough.emplace();
|
||||
|
||||
@@ -339,7 +373,7 @@ std::vector<encoder_settings> 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);
|
||||
fill_defaults(bundle, info.supported_codecs, *config.encoder_passthrough, config.bit_depth);
|
||||
|
||||
uint64_t bitrate = config.bitrate.value_or(default_bitrate);
|
||||
std::array<double, 2> default_scale;
|
||||
@@ -355,7 +389,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
||||
|
||||
for (auto & encoder: config.encoders)
|
||||
{
|
||||
fill_defaults(bundle, info.supported_codecs, encoder);
|
||||
fill_defaults(bundle, info.supported_codecs, encoder, config.bit_depth);
|
||||
assert(encoder.codec);
|
||||
check_scale(encoder.name,
|
||||
*encoder.codec,
|
||||
@@ -394,6 +428,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
||||
settings.video_width = settings.width;
|
||||
settings.video_height = settings.height;
|
||||
settings.codec = *encoder.codec;
|
||||
settings.bit_depth = config.bit_depth;
|
||||
if (encoder.group)
|
||||
settings.group = *encoder.group;
|
||||
else
|
||||
@@ -424,6 +459,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
||||
settings.video_width = settings.width;
|
||||
settings.video_height = settings.height;
|
||||
settings.codec = *encoder.codec;
|
||||
settings.bit_depth = config.bit_depth;
|
||||
if (encoder.group)
|
||||
settings.group = *encoder.group;
|
||||
else
|
||||
|
||||
@@ -39,6 +39,7 @@ struct encoder_settings : public to_headset::video_stream_description::item
|
||||
std::map<std::string, std::string> options; // additional encoder-specific configuration
|
||||
// encoders in the same group are executed in sequence
|
||||
int group = 0;
|
||||
int bit_depth;
|
||||
std::optional<std::string> device;
|
||||
};
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
#include <drm_fourcc.h>
|
||||
#include <filesystem>
|
||||
#include <libavutil/pixfmt.h>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
@@ -124,9 +125,23 @@ std::unordered_map<uint32_t, vk::Format> vulkan_drm_format_map = {
|
||||
{DRM_FORMAT_XBGR8888, vk::Format::eR8G8B8A8Unorm},
|
||||
};
|
||||
|
||||
vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc)
|
||||
std::unordered_map<uint32_t, vk::Format> vulkan_drm_format_map_10bit = {
|
||||
{DRM_FORMAT_R8, vk::Format::eR10X6UnormPack16},
|
||||
{DRM_FORMAT_R10, vk::Format::eR10X6UnormPack16},
|
||||
{DRM_FORMAT_R16, vk::Format::eR10X6UnormPack16},
|
||||
{DRM_FORMAT_GR88, vk::Format::eR10X6G10X6Unorm2Pack16},
|
||||
{DRM_FORMAT_RG88, vk::Format::eR10X6G10X6Unorm2Pack16},
|
||||
{DRM_FORMAT_GR1616, vk::Format::eR10X6G10X6Unorm2Pack16},
|
||||
{DRM_FORMAT_RG1616, vk::Format::eR10X6G10X6Unorm2Pack16},
|
||||
};
|
||||
|
||||
/// bit_depth not checked, valid values are 8 and 10
|
||||
vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, int bit_depth)
|
||||
{
|
||||
return vulkan_drm_format_map.at(drm_fourcc);
|
||||
if (bit_depth == 10)
|
||||
return vulkan_drm_format_map_10bit.at(drm_fourcc);
|
||||
else
|
||||
return vulkan_drm_format_map.at(drm_fourcc);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -151,9 +166,28 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
||||
settings.video_width += settings.video_width % 2;
|
||||
settings.video_height += settings.video_height % 2;
|
||||
|
||||
auto vaapi_frame_ctx = make_hwframe_ctx(vaapi_hw_ctx.get(), AV_PIX_FMT_VAAPI, AV_PIX_FMT_NV12, settings.video_width, settings.video_height);
|
||||
AVPixelFormat sw_format;
|
||||
|
||||
assert(av_pix_fmt_count_planes(AV_PIX_FMT_NV12) == 2);
|
||||
switch (settings.codec)
|
||||
{
|
||||
case video_codec::av1:
|
||||
case video_codec::h265:
|
||||
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.bit_depth != 8)
|
||||
throw std::runtime_error("selected codec only supports 8-bit encoding");
|
||||
sw_format = AV_PIX_FMT_NV12;
|
||||
break;
|
||||
}
|
||||
|
||||
auto vaapi_frame_ctx = make_hwframe_ctx(vaapi_hw_ctx.get(), AV_PIX_FMT_VAAPI, sw_format, settings.video_width, settings.video_height);
|
||||
assert(av_pix_fmt_count_planes(sw_format) == 2);
|
||||
|
||||
rect = vk::Rect2D{
|
||||
.offset = {
|
||||
@@ -199,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 = 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;
|
||||
@@ -282,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),
|
||||
.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)),
|
||||
|
||||
@@ -144,6 +144,9 @@ video_encoder_nvenc::video_encoder_nvenc(
|
||||
fps(fps),
|
||||
bitrate(settings.bitrate)
|
||||
{
|
||||
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);
|
||||
assert(settings.height % 32 == 0);
|
||||
|
||||
@@ -192,6 +192,9 @@ std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h2
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||
|
||||
if (settings.bit_depth != 8)
|
||||
throw std::runtime_error("h264 codec only supports 8-bit encoding");
|
||||
|
||||
std::unique_ptr<video_encoder_vulkan_h264> self(new video_encoder_vulkan_h264(vk, rect, video_caps, encode_caps, fps, stream_idx, settings));
|
||||
|
||||
vk::VideoEncodeH264SessionParametersAddInfoKHR h264_add_info{};
|
||||
|
||||
@@ -93,6 +93,9 @@ video_encoder_x264::video_encoder_x264(
|
||||
uint8_t stream_idx) :
|
||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false)
|
||||
{
|
||||
if (settings.bit_depth != 8)
|
||||
throw std::runtime_error("x264 encoder only supports 8-bit encoding");
|
||||
|
||||
if (settings.codec != h264)
|
||||
{
|
||||
U_LOG_W("requested x264 encoder with codec != h264");
|
||||
|
||||
Reference in New Issue
Block a user