vaapi: 10-bit hevc/av1 encoding
This commit is contained in:
@@ -147,6 +147,7 @@ configuration::encoder parse_encoder(const nlohmann::json & item)
|
|||||||
SET_IF(codec);
|
SET_IF(codec);
|
||||||
if (e.codec == wivrn::video_codec(-1))
|
if (e.codec == wivrn::video_codec(-1))
|
||||||
throw std::runtime_error("invalid codec value " + item["codec"].get<std::string>());
|
throw std::runtime_error("invalid codec value " + item["codec"].get<std::string>());
|
||||||
|
SET_IF(use_10bit);
|
||||||
SET_IF(options);
|
SET_IF(options);
|
||||||
SET_IF(device);
|
SET_IF(device);
|
||||||
return e;
|
return e;
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ struct configuration
|
|||||||
std::optional<double> offset_y;
|
std::optional<double> offset_y;
|
||||||
std::optional<int> group;
|
std::optional<int> group;
|
||||||
std::optional<wivrn::video_codec> codec;
|
std::optional<wivrn::video_codec> codec;
|
||||||
|
std::optional<bool> use_10bit;
|
||||||
std::map<std::string, std::string> options;
|
std::map<std::string, std::string> options;
|
||||||
std::optional<std::string> device;
|
std::optional<std::string> device;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -179,6 +179,13 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
|||||||
|
|
||||||
auto format = vk::Format(cn->format);
|
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);
|
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count);
|
||||||
|
|
||||||
#if WIVRN_USE_VULKAN_ENCODE
|
#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);
|
cn->psc.images.resize(cn->image_count);
|
||||||
for (uint32_t i = 0; i < cn->image_count; i++)
|
for (uint32_t i = 0; i < cn->image_count; i++)
|
||||||
{
|
{
|
||||||
std::array formats = {
|
|
||||||
vk::Format::eR8Unorm,
|
|
||||||
vk::Format::eR8G8Unorm,
|
|
||||||
format,
|
|
||||||
};
|
|
||||||
vk::ImageFormatListCreateInfo formats_info{
|
vk::ImageFormatListCreateInfo formats_info{
|
||||||
.viewFormatCount = formats.size(),
|
.viewFormatCount = formats.size(),
|
||||||
.pViewFormats = formats.data(),
|
.pViewFormats = formats.data(),
|
||||||
@@ -249,7 +251,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
|||||||
.pNext = &usage,
|
.pNext = &usage,
|
||||||
.image = item.image,
|
.image = item.image,
|
||||||
.viewType = vk::ImageViewType::e2DArray,
|
.viewType = vk::ImageViewType::e2DArray,
|
||||||
.format = vk::Format::eR8Unorm,
|
.format = formats[0],
|
||||||
.subresourceRange = {
|
.subresourceRange = {
|
||||||
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
|
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
|
||||||
.levelCount = 1,
|
.levelCount = 1,
|
||||||
@@ -261,7 +263,7 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
|||||||
.pNext = &usage,
|
.pNext = &usage,
|
||||||
.image = item.image,
|
.image = item.image,
|
||||||
.viewType = vk::ImageViewType::e2DArray,
|
.viewType = vk::ImageViewType::e2DArray,
|
||||||
.format = vk::Format::eR8G8Unorm,
|
.format = formats[1],
|
||||||
.subresourceRange = {
|
.subresourceRange = {
|
||||||
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
|
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
|
||||||
.levelCount = 1,
|
.levelCount = 1,
|
||||||
@@ -407,8 +409,11 @@ static void comp_wivrn_create_images(struct comp_target * ct, const struct comp_
|
|||||||
|
|
||||||
target_init_semaphores(cn);
|
target_init_semaphores(cn);
|
||||||
|
|
||||||
// FIXME: select preferred format
|
if (cn->settings[0].use_10bit)
|
||||||
ct->format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
|
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->width = create_info->extent.width;
|
||||||
ct->height = create_info->extent.height;
|
ct->height = create_info->extent.height;
|
||||||
ct->surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
ct->surface_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||||
|
|||||||
@@ -91,8 +91,11 @@ void print_encoders(const std::vector<wivrn::encoder_settings> & encoders)
|
|||||||
group = encoder.group;
|
group = encoder.group;
|
||||||
U_LOG_I("Group %d", group);
|
U_LOG_I("Group %d", group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char * suffix_10bit = encoder.use_10bit ? " 10-bit" : "";
|
||||||
|
|
||||||
std::string codec(magic_enum::enum_name(encoder.codec));
|
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%s)", encoder.encoder_name.c_str(), codec.c_str(), suffix_10bit);
|
||||||
U_LOG_I("\tchannels: %s", std::string(magic_enum::enum_name(encoder.channels)).c_str());
|
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,
|
U_LOG_I("\tsize:%" PRIu16 "x%" PRIu16 " offset:%" PRIu16 "x%" PRIu16,
|
||||||
encoder.width,
|
encoder.width,
|
||||||
@@ -268,6 +271,9 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
|
|||||||
|
|
||||||
if (not config.codec)
|
if (not config.codec)
|
||||||
config.codec = h264;
|
config.codec = h264;
|
||||||
|
|
||||||
|
if (not config.use_10bit)
|
||||||
|
config.use_10bit = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
@@ -353,6 +359,8 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
|||||||
height / passthrough_subsampling,
|
height / passthrough_subsampling,
|
||||||
scale);
|
scale);
|
||||||
|
|
||||||
|
std::optional<bool> maybe_10bit{};
|
||||||
|
|
||||||
for (auto & encoder: config.encoders)
|
for (auto & encoder: config.encoders)
|
||||||
{
|
{
|
||||||
fill_defaults(bundle, info.supported_codecs, encoder);
|
fill_defaults(bundle, info.supported_codecs, encoder);
|
||||||
@@ -362,6 +370,22 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
|||||||
std::ceil(encoder.width.value_or(1) * width),
|
std::ceil(encoder.width.value_or(1) * width),
|
||||||
std::ceil(encoder.height.value_or(1) * height),
|
std::ceil(encoder.height.value_or(1) * height),
|
||||||
scale);
|
scale);
|
||||||
|
if (maybe_10bit.has_value())
|
||||||
|
{
|
||||||
|
if (maybe_10bit.value() != encoder.use_10bit)
|
||||||
|
throw std::runtime_error("all encoders must use the same 10-bit setting");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
maybe_10bit = *encoder.use_10bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: detach alpha into separate vkImage.
|
||||||
|
// for now, if the main encoder is 10-bit, we also force alpha to be 10-bit
|
||||||
|
if (maybe_10bit.value_or(false) && !*config.encoder_passthrough->use_10bit)
|
||||||
|
{
|
||||||
|
config.encoder_passthrough->name = config.encoders[0].name;
|
||||||
|
config.encoder_passthrough->codec = config.encoders[0].codec;
|
||||||
|
config.encoder_passthrough->use_10bit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
width = align(width * scale[0], 64);
|
width = align(width * scale[0], 64);
|
||||||
@@ -394,6 +418,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
|||||||
settings.video_width = settings.width;
|
settings.video_width = settings.width;
|
||||||
settings.video_height = settings.height;
|
settings.video_height = settings.height;
|
||||||
settings.codec = *encoder.codec;
|
settings.codec = *encoder.codec;
|
||||||
|
settings.use_10bit = *encoder.use_10bit;
|
||||||
if (encoder.group)
|
if (encoder.group)
|
||||||
settings.group = *encoder.group;
|
settings.group = *encoder.group;
|
||||||
else
|
else
|
||||||
@@ -424,6 +449,7 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
|
|||||||
settings.video_width = settings.width;
|
settings.video_width = settings.width;
|
||||||
settings.video_height = settings.height;
|
settings.video_height = settings.height;
|
||||||
settings.codec = *encoder.codec;
|
settings.codec = *encoder.codec;
|
||||||
|
settings.use_10bit = *encoder.use_10bit;
|
||||||
if (encoder.group)
|
if (encoder.group)
|
||||||
settings.group = *encoder.group;
|
settings.group = *encoder.group;
|
||||||
else
|
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
|
std::map<std::string, std::string> options; // additional encoder-specific configuration
|
||||||
// encoders in the same group are executed in sequence
|
// encoders in the same group are executed in sequence
|
||||||
int group = 0;
|
int group = 0;
|
||||||
|
bool use_10bit;
|
||||||
std::optional<std::string> device;
|
std::optional<std::string> device;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -124,9 +124,22 @@ std::unordered_map<uint32_t, vk::Format> vulkan_drm_format_map = {
|
|||||||
{DRM_FORMAT_XBGR8888, vk::Format::eR8G8B8A8Unorm},
|
{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},
|
||||||
|
};
|
||||||
|
|
||||||
|
vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, bool use_10bit)
|
||||||
{
|
{
|
||||||
return vulkan_drm_format_map.at(drm_fourcc);
|
if (use_10bit)
|
||||||
|
return vulkan_drm_format_map_10bit.at(drm_fourcc);
|
||||||
|
else
|
||||||
|
return vulkan_drm_format_map.at(drm_fourcc);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -151,9 +164,23 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
|||||||
settings.video_width += settings.video_width % 2;
|
settings.video_width += settings.video_width % 2;
|
||||||
settings.video_height += settings.video_height % 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:
|
||||||
|
sw_format = settings.use_10bit ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (settings.use_10bit)
|
||||||
|
throw std::runtime_error("selected codec does not support 10-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{
|
rect = vk::Rect2D{
|
||||||
.offset = {
|
.offset = {
|
||||||
@@ -199,7 +226,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
|||||||
av_dict_set(&opts, "rc_mode", "CBR", 0);
|
av_dict_set(&opts, "rc_mode", "CBR", 0);
|
||||||
break;
|
break;
|
||||||
case video_codec::h265:
|
case video_codec::h265:
|
||||||
encoder_ctx->profile = FF_PROFILE_HEVC_MAIN;
|
encoder_ctx->profile = settings.use_10bit ? FF_PROFILE_HEVC_MAIN_10 : FF_PROFILE_HEVC_MAIN;
|
||||||
break;
|
break;
|
||||||
case video_codec::av1:
|
case video_codec::av1:
|
||||||
encoder_ctx->profile = FF_PROFILE_AV1_MAIN;
|
encoder_ctx->profile = FF_PROFILE_AV1_MAIN;
|
||||||
@@ -282,7 +309,7 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
|
|||||||
vk::StructureChain image_create_info{
|
vk::StructureChain image_create_info{
|
||||||
vk::ImageCreateInfo{
|
vk::ImageCreateInfo{
|
||||||
.imageType = vk::ImageType::e2D,
|
.imageType = vk::ImageType::e2D,
|
||||||
.format = drm_to_vulkan_fmt(desc->layers[i].format),
|
.format = drm_to_vulkan_fmt(desc->layers[i].format, settings.use_10bit),
|
||||||
.extent = {
|
.extent = {
|
||||||
.width = uint32_t(drm_frame->width / (i == 0 ? 1 : 2)),
|
.width = uint32_t(drm_frame->width / (i == 0 ? 1 : 2)),
|
||||||
.height = uint32_t(drm_frame->height / (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),
|
fps(fps),
|
||||||
bitrate(settings.bitrate)
|
bitrate(settings.bitrate)
|
||||||
{
|
{
|
||||||
|
if (settings.use_10bit)
|
||||||
|
throw std::runtime_error("nvenc encoder does not support 10-bit encoding");
|
||||||
|
|
||||||
std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init();
|
std::tie(cuda_fn, nvenc_fn, fn, cuda, session_handle) = init();
|
||||||
assert(settings.width % 32 == 0);
|
assert(settings.width % 32 == 0);
|
||||||
assert(settings.height % 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::VideoEncodeCapabilitiesKHR,
|
||||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||||
|
|
||||||
|
if (settings.use_10bit)
|
||||||
|
throw std::runtime_error("h264 codec does not support 10-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));
|
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{};
|
vk::VideoEncodeH264SessionParametersAddInfoKHR h264_add_info{};
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ video_encoder_x264::video_encoder_x264(
|
|||||||
uint8_t stream_idx) :
|
uint8_t stream_idx) :
|
||||||
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false)
|
video_encoder(stream_idx, settings.channels, settings.bitrate_multiplier, false)
|
||||||
{
|
{
|
||||||
|
if (settings.use_10bit)
|
||||||
|
throw std::runtime_error("x264 encoder does not support 10-bit encoding");
|
||||||
|
|
||||||
if (settings.codec != h264)
|
if (settings.codec != h264)
|
||||||
{
|
{
|
||||||
U_LOG_W("requested x264 encoder with codec != h264");
|
U_LOG_W("requested x264 encoder with codec != h264");
|
||||||
|
|||||||
Reference in New Issue
Block a user