Experimental h264 vulkan video encode
This commit is contained in:
@@ -32,6 +32,7 @@ option(WIVRN_WERROR "Treat warnings as errors" OFF)
|
||||
|
||||
option(WIVRN_USE_NVENC "Enable nvenc (Nvidia) hardware encoder" ON)
|
||||
auto_option(WIVRN_USE_VAAPI "Enable vaapi (AMD/Intel) hardware encoder" AUTO)
|
||||
option(WIVRN_USE_VULKAN_ENCODE "Enable vulkan video encoder" ON)
|
||||
auto_option(WIVRN_USE_X264 "Enable x264 software encoder" AUTO)
|
||||
|
||||
auto_option(WIVRN_USE_PIPEWIRE "Enable pipewire backend" AUTO)
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#cmakedefine01 WIVRN_USE_NVENC
|
||||
#cmakedefine01 WIVRN_USE_VAAPI
|
||||
#cmakedefine01 WIVRN_USE_VULKAN_ENCODE
|
||||
#cmakedefine01 WIVRN_USE_X264
|
||||
|
||||
#cmakedefine01 WIVRN_USE_SYSTEMD
|
||||
|
||||
@@ -126,6 +126,14 @@ if(WIVRN_USE_VAAPI)
|
||||
target_link_libraries(wivrn-server PRIVATE PkgConfig::LIBAV PkgConfig::LIBDRM)
|
||||
endif()
|
||||
|
||||
if(WIVRN_USE_VULKAN_ENCODE)
|
||||
target_sources(wivrn-server PRIVATE
|
||||
encoder/video_encoder_vulkan.cpp
|
||||
encoder/video_encoder_vulkan_h264.cpp
|
||||
#encoder/video_encoder_vulkan_h265.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
if(WIVRN_USE_X264)
|
||||
target_sources(wivrn-server PRIVATE encoder/video_encoder_x264.cpp)
|
||||
target_link_libraries(wivrn-server PRIVATE PkgConfig::X264)
|
||||
|
||||
@@ -49,6 +49,20 @@ std::vector<const char *> wivrn_comp_target::wanted_device_extensions = {
|
||||
#ifdef VK_EXT_image_drm_format_modifier
|
||||
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
|
||||
#endif
|
||||
|
||||
// For vulkan video encode
|
||||
#ifdef VK_KHR_video_queue
|
||||
VK_KHR_VIDEO_QUEUE_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_queue
|
||||
VK_KHR_VIDEO_ENCODE_QUEUE_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_h264
|
||||
VK_KHR_VIDEO_ENCODE_H264_EXTENSION_NAME,
|
||||
#endif
|
||||
#ifdef VK_KHR_video_encode_h265
|
||||
VK_KHR_VIDEO_ENCODE_H265_EXTENSION_NAME,
|
||||
#endif
|
||||
};
|
||||
|
||||
static void target_init_semaphores(struct wivrn_comp_target * cn);
|
||||
@@ -151,6 +165,15 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
|
||||
cn->images = U_TYPED_ARRAY_CALLOC(struct comp_target_image, cn->image_count);
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
auto [video_profiles, encoder_flags] = VideoEncoder::get_create_image_info(cn->settings);
|
||||
|
||||
vk::VideoProfileListInfoKHR video_profile_list{
|
||||
.profileCount = uint32_t(video_profiles.size()),
|
||||
.pProfiles = video_profiles.data(),
|
||||
};
|
||||
#endif
|
||||
|
||||
cn->psc.images.resize(cn->image_count);
|
||||
std::vector<vk::Image> rgb;
|
||||
for (uint32_t i = 0; i < cn->image_count; i++)
|
||||
@@ -158,11 +181,18 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
std::array formats = {
|
||||
vk::Format::eR8Unorm,
|
||||
vk::Format::eR8G8Unorm,
|
||||
format,
|
||||
};
|
||||
vk::ImageFormatListCreateInfo formats_info{
|
||||
.viewFormatCount = formats.size(),
|
||||
.pViewFormats = formats.data(),
|
||||
};
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
if (video_profile_list.profileCount)
|
||||
formats_info.pNext = &video_profile_list;
|
||||
#endif
|
||||
|
||||
auto & image = cn->psc.images[i].image;
|
||||
image = image_allocation(
|
||||
device, {
|
||||
@@ -179,7 +209,11 @@ static VkResult create_images(struct wivrn_comp_target * cn, vk::ImageUsageFlags
|
||||
.arrayLayers = 1,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = vk::ImageTiling::eOptimal,
|
||||
.usage = flags | vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
.usage = flags
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
| encoder_flags
|
||||
#endif
|
||||
| vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -38,6 +38,10 @@
|
||||
#if WIVRN_USE_X264
|
||||
#include "video_encoder_x264.h"
|
||||
#endif
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
#include "video_encoder_vulkan_h264.h"
|
||||
// #include "video_encoder_vulkan_h265.h"
|
||||
#endif
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
@@ -106,6 +110,25 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
|
||||
{
|
||||
using namespace std::string_literals;
|
||||
std::unique_ptr<VideoEncoder> res;
|
||||
if (settings.encoder_name == encoder_vulkan)
|
||||
{
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
switch (settings.codec)
|
||||
{
|
||||
case video_codec::h264:
|
||||
res = video_encoder_vulkan_h264::create(wivrn_vk, settings, fps);
|
||||
break;
|
||||
case video_codec::h265:
|
||||
throw std::runtime_error("h265 not supported for vulkan video encode");
|
||||
// res = video_encoder_vulkan_h265::create(wivrn_vk, settings, fps);
|
||||
// break;
|
||||
case video_codec::av1:
|
||||
throw std::runtime_error("av1 not supported for vulkan video encode");
|
||||
}
|
||||
#else
|
||||
throw std::runtime_error("Vulkan video encode not enabled");
|
||||
#endif
|
||||
}
|
||||
if (settings.encoder_name == encoder_x264)
|
||||
{
|
||||
#if WIVRN_USE_X264
|
||||
@@ -156,6 +179,32 @@ std::unique_ptr<VideoEncoder> VideoEncoder::Create(
|
||||
return res;
|
||||
}
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> VideoEncoder::get_create_image_info(const std::vector<encoder_settings> & settings)
|
||||
{
|
||||
std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> result;
|
||||
for (const auto & item: settings)
|
||||
{
|
||||
if (item.encoder_name == encoder_vulkan)
|
||||
{
|
||||
result.second |= vk::ImageUsageFlagBits::eVideoEncodeSrcKHR;
|
||||
switch (item.codec)
|
||||
{
|
||||
case h264:
|
||||
result.first.push_back(video_encoder_vulkan_h264::video_profile_info.get());
|
||||
break;
|
||||
case h265:
|
||||
// result.first.push_back(video_encoder_vulkan_h265::video_profile_info.get());
|
||||
break;
|
||||
case av1:
|
||||
throw std::runtime_error("av1 not supported for vulkan video encode");
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
static const uint64_t idr_throttle = 100;
|
||||
|
||||
VideoEncoder::VideoEncoder(bool async_send) :
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "driver/clock_offset.h"
|
||||
#include "wivrn_config.h"
|
||||
#include "wivrn_packets.h"
|
||||
|
||||
#include <atomic>
|
||||
@@ -42,6 +43,7 @@ class wivrn_session;
|
||||
inline const char * encoder_nvenc = "nvenc";
|
||||
inline const char * encoder_vaapi = "vaapi";
|
||||
inline const char * encoder_x264 = "x264";
|
||||
inline const char * encoder_vulkan = "vulkan";
|
||||
|
||||
class VideoEncoder
|
||||
{
|
||||
@@ -104,6 +106,10 @@ public:
|
||||
int input_height,
|
||||
float fps);
|
||||
|
||||
#if WIVRN_USE_VULKAN_ENCODE
|
||||
static std::pair<std::vector<vk::VideoProfileInfoKHR>, vk::ImageUsageFlags> get_create_image_info(const std::vector<encoder_settings> &);
|
||||
#endif
|
||||
|
||||
VideoEncoder(bool async_send = false);
|
||||
virtual ~VideoEncoder();
|
||||
|
||||
|
||||
@@ -0,0 +1,469 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "video_encoder_vulkan.h"
|
||||
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
static uint32_t align(uint32_t value, uint32_t alignment)
|
||||
{
|
||||
if (alignment == 0)
|
||||
return value;
|
||||
return alignment * (1 + (value - 1) / alignment);
|
||||
}
|
||||
|
||||
static vk::VideoEncodeCapabilitiesKHR patch_capabilities(vk::VideoEncodeCapabilitiesKHR caps)
|
||||
{
|
||||
if (caps.rateControlModes & (vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr | vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr) and caps.maxBitrate == 0)
|
||||
{
|
||||
U_LOG_W("Invalid encode capabilities, disabling rate control");
|
||||
caps.rateControlModes = vk::VideoEncodeRateControlModeFlagBitsKHR::eDefault;
|
||||
}
|
||||
return caps;
|
||||
}
|
||||
|
||||
vk::VideoFormatPropertiesKHR wivrn::video_encoder_vulkan::select_video_format(
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
const vk::PhysicalDeviceVideoFormatInfoKHR & format_info)
|
||||
{
|
||||
for (const auto & video_fmt_prop: physical_device.getVideoFormatPropertiesKHR(format_info))
|
||||
{
|
||||
// TODO: do something smart if there is more than one
|
||||
return video_fmt_prop;
|
||||
}
|
||||
throw std::runtime_error("No suitable image format found");
|
||||
}
|
||||
|
||||
wivrn::video_encoder_vulkan::video_encoder_vulkan(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR in_encode_caps, float fps, uint64_t bitrate) :
|
||||
VideoEncoder(true), vk(vk), encode_caps(patch_capabilities(in_encode_caps)), rect(rect), fps(fps)
|
||||
{
|
||||
// Initialize Rate control
|
||||
U_LOG_D("Supported rate control modes: %s", vk::to_string(encode_caps.rateControlModes).c_str());
|
||||
|
||||
if (encode_caps.rateControlModes & (vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr | vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr))
|
||||
{
|
||||
U_LOG_D("Maximum bitrate: %ld", encode_caps.maxBitrate / 1'000'000);
|
||||
if (encode_caps.maxBitrate < bitrate)
|
||||
{
|
||||
U_LOG_W("Configured bitrate %ldMB/s is higher than max supported %ld",
|
||||
bitrate / 1'000'000,
|
||||
encode_caps.maxBitrate / 1'000'000);
|
||||
}
|
||||
}
|
||||
|
||||
rate_control_layer = vk::VideoEncodeRateControlLayerInfoKHR{
|
||||
.averageBitrate = std::min(bitrate, encode_caps.maxBitrate),
|
||||
.maxBitrate = std::min(2 * bitrate, encode_caps.maxBitrate),
|
||||
.frameRateNumerator = uint32_t(fps * 1'000'000),
|
||||
.frameRateDenominator = 1'000'000,
|
||||
};
|
||||
rate_control = vk::VideoEncodeRateControlInfoKHR{
|
||||
.layerCount = 1,
|
||||
.pLayers = &rate_control_layer,
|
||||
.virtualBufferSizeInMs = 5'000,
|
||||
.initialVirtualBufferSizeInMs = 4'000,
|
||||
};
|
||||
|
||||
if (encode_caps.rateControlModes & vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr)
|
||||
{
|
||||
rate_control_layer.maxBitrate = rate_control_layer.averageBitrate;
|
||||
rate_control->rateControlMode = vk::VideoEncodeRateControlModeFlagBitsKHR::eCbr;
|
||||
}
|
||||
else if (encode_caps.rateControlModes & vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr)
|
||||
{
|
||||
rate_control->rateControlMode = vk::VideoEncodeRateControlModeFlagBitsKHR::eVbr;
|
||||
}
|
||||
else
|
||||
{
|
||||
U_LOG_W("No suitable rate control available, reverting to default");
|
||||
rate_control.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_caps,
|
||||
const vk::VideoProfileInfoKHR & video_profile,
|
||||
void * video_session_create_next,
|
||||
void * session_params_next)
|
||||
{
|
||||
fence = vk.device.createFence({});
|
||||
|
||||
vk::VideoProfileListInfoKHR video_profile_list{
|
||||
.profileCount = 1,
|
||||
.pProfiles = &video_profile,
|
||||
};
|
||||
|
||||
// Input image
|
||||
vk::VideoFormatPropertiesKHR picture_format = select_video_format(
|
||||
vk.physical_device,
|
||||
vk::PhysicalDeviceVideoFormatInfoKHR{
|
||||
.pNext = &video_profile_list,
|
||||
.imageUsage = vk::ImageUsageFlagBits::eVideoEncodeSrcKHR,
|
||||
});
|
||||
|
||||
if (picture_format.format != vk::Format::eG8B8R82Plane420Unorm)
|
||||
throw std::runtime_error("Unsupported format " +
|
||||
vk::to_string(picture_format.format) +
|
||||
" for encoder input image");
|
||||
|
||||
// Decode picture buffer (DPB) images
|
||||
vk::VideoFormatPropertiesKHR reference_picture_format = select_video_format(
|
||||
vk.physical_device,
|
||||
vk::PhysicalDeviceVideoFormatInfoKHR{
|
||||
.pNext = &video_profile_list,
|
||||
.imageUsage = vk::ImageUsageFlagBits::eVideoEncodeDpbKHR,
|
||||
});
|
||||
{
|
||||
// TODO: check format capabilities
|
||||
// TODO: use multiple images if array levels are not supported
|
||||
|
||||
vk::Extent3D aligned_extent{
|
||||
.width = align(rect.extent.width, video_caps.pictureAccessGranularity.width),
|
||||
.height = align(rect.extent.height, video_caps.pictureAccessGranularity.height),
|
||||
.depth = 1,
|
||||
};
|
||||
|
||||
vk::ImageCreateInfo img_create_info{
|
||||
.pNext = &video_profile_list,
|
||||
.flags = reference_picture_format.imageCreateFlags,
|
||||
.imageType = reference_picture_format.imageType,
|
||||
.format = reference_picture_format.format,
|
||||
.extent = aligned_extent,
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = num_dpb_slots,
|
||||
.samples = vk::SampleCountFlagBits::e1,
|
||||
.tiling = reference_picture_format.imageTiling,
|
||||
.usage = vk::ImageUsageFlagBits::eVideoEncodeDpbKHR,
|
||||
.sharingMode = vk::SharingMode::eExclusive,
|
||||
};
|
||||
|
||||
dpb_image = image_allocation(
|
||||
vk.device,
|
||||
img_create_info,
|
||||
{
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
});
|
||||
}
|
||||
|
||||
// Output buffer
|
||||
{
|
||||
// very conservative bound
|
||||
output_buffer_size = rect.extent.width * rect.extent.height * 3;
|
||||
output_buffer_size = align(output_buffer_size, video_caps.minBitstreamBufferSizeAlignment);
|
||||
output_buffer = buffer_allocation(
|
||||
vk.device,
|
||||
{.pNext = &video_profile_list,
|
||||
.size = output_buffer_size,
|
||||
.usage = vk::BufferUsageFlagBits::eVideoEncodeDstKHR,
|
||||
.sharingMode = vk::SharingMode::eExclusive},
|
||||
{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
|
||||
.usage = VMA_MEMORY_USAGE_AUTO,
|
||||
});
|
||||
}
|
||||
|
||||
// video session
|
||||
{
|
||||
vk::ExtensionProperties std_header_version = this->std_header_version();
|
||||
|
||||
video_session =
|
||||
vk.device.createVideoSessionKHR(vk::VideoSessionCreateInfoKHR{
|
||||
.pNext = video_session_create_next,
|
||||
.queueFamilyIndex = vk.encode_queue_family_index,
|
||||
//.flags = vk::VideoSessionCreateFlagBitsKHR::eAllowEncodeParameterOptimizations,
|
||||
.pVideoProfile = &video_profile,
|
||||
.pictureFormat = picture_format.format,
|
||||
.maxCodedExtent = rect.extent,
|
||||
.referencePictureFormat = reference_picture_format.format,
|
||||
.maxDpbSlots = num_dpb_slots,
|
||||
.maxActiveReferencePictures = uint32_t(num_dpb_slots - 1),
|
||||
.pStdHeaderVersion = &std_header_version,
|
||||
});
|
||||
|
||||
auto video_req = video_session.getMemoryRequirements();
|
||||
// FIXME: allocating on a single device memory seems to fail
|
||||
std::vector<vk::BindVideoSessionMemoryInfoKHR> video_session_bind;
|
||||
for (const auto & req: video_req)
|
||||
{
|
||||
vk::MemoryAllocateInfo alloc_info{
|
||||
.allocationSize = req.memoryRequirements.size,
|
||||
.memoryTypeIndex = vk.get_memory_type(req.memoryRequirements.memoryTypeBits, {}),
|
||||
};
|
||||
|
||||
const auto & mem_item = mem.emplace_back(vk.device.allocateMemory(alloc_info));
|
||||
video_session_bind.push_back({
|
||||
.memoryBindIndex = req.memoryBindIndex,
|
||||
.memory = *mem_item,
|
||||
.memoryOffset = 0,
|
||||
.memorySize = alloc_info.allocationSize,
|
||||
});
|
||||
}
|
||||
video_session.bindMemory(video_session_bind);
|
||||
}
|
||||
|
||||
// input image view
|
||||
image_view_template_next = {
|
||||
.usage = picture_format.imageUsageFlags,
|
||||
};
|
||||
image_view_template = {
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = picture_format.format,
|
||||
.components = picture_format.componentMapping,
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1},
|
||||
};
|
||||
|
||||
// DPB image views
|
||||
{
|
||||
vk::ImageViewCreateInfo img_view_create_info{
|
||||
.image = dpb_image,
|
||||
.viewType = vk::ImageViewType::e2D,
|
||||
.format = reference_picture_format.format,
|
||||
.components = reference_picture_format.componentMapping,
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.layerCount = 1},
|
||||
};
|
||||
for (size_t i = 0; i < num_dpb_slots; ++i)
|
||||
{
|
||||
img_view_create_info.subresourceRange.baseArrayLayer = i;
|
||||
dpb_image_views.push_back(vk.device.createImageView(img_view_create_info));
|
||||
}
|
||||
}
|
||||
|
||||
// DPB video picture resource info
|
||||
{
|
||||
for (auto & dpb_image_view: dpb_image_views)
|
||||
{
|
||||
dpb_resource.push_back(
|
||||
{
|
||||
.codedExtent = rect.extent,
|
||||
.imageViewBinding = *dpb_image_view,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DPB slot info
|
||||
{
|
||||
auto std_slots = setup_slot_info(num_dpb_slots);
|
||||
assert(std_slots.size() == num_dpb_slots);
|
||||
for (size_t i = 0; i < num_dpb_slots; ++i)
|
||||
{
|
||||
dpb_slots.push_back({
|
||||
.pNext = std_slots[i],
|
||||
.slotIndex = -1,
|
||||
.pPictureResource = nullptr,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// video session parameters
|
||||
{
|
||||
video_session_parameters = vk.device.createVideoSessionParametersKHR({
|
||||
.pNext = session_params_next,
|
||||
.videoSession = *video_session,
|
||||
});
|
||||
}
|
||||
|
||||
// query pool
|
||||
{
|
||||
vk::StructureChain query_pool_create = {
|
||||
vk::QueryPoolCreateInfo{
|
||||
.queryType = vk::QueryType::eVideoEncodeFeedbackKHR,
|
||||
.queryCount = 1,
|
||||
|
||||
},
|
||||
vk::QueryPoolVideoEncodeFeedbackCreateInfoKHR{
|
||||
.pNext = &video_profile,
|
||||
.encodeFeedbackFlags =
|
||||
vk::VideoEncodeFeedbackFlagBitsKHR::eBitstreamBufferOffset |
|
||||
vk::VideoEncodeFeedbackFlagBitsKHR::eBitstreamBytesWritten,
|
||||
},
|
||||
};
|
||||
|
||||
query_pool = vk.device.createQueryPool(query_pool_create.get());
|
||||
}
|
||||
|
||||
// command pool and buffer
|
||||
{
|
||||
command_pool = vk.device.createCommandPool({
|
||||
.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer,
|
||||
.queueFamilyIndex = vk.encode_queue_family_index,
|
||||
});
|
||||
|
||||
command_buffer = std::move(vk.device.allocateCommandBuffers({.commandPool = *command_pool,
|
||||
.commandBufferCount = 1})[0]);
|
||||
}
|
||||
}
|
||||
|
||||
wivrn::video_encoder_vulkan::~video_encoder_vulkan()
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<uint8_t> wivrn::video_encoder_vulkan::get_encoded_parameters(void * next)
|
||||
{
|
||||
auto [feedback, encoded] = vk.device.getEncodedVideoSessionParametersKHR({
|
||||
.pNext = next,
|
||||
.videoSessionParameters = *video_session_parameters,
|
||||
});
|
||||
return encoded;
|
||||
}
|
||||
|
||||
std::optional<wivrn::VideoEncoder::data> wivrn::video_encoder_vulkan::encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t encode_slot)
|
||||
{
|
||||
command_buffer.reset();
|
||||
command_buffer.begin(vk::CommandBufferBeginInfo{
|
||||
.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit,
|
||||
});
|
||||
command_buffer.resetQueryPool(*query_pool, 0, 1);
|
||||
|
||||
if (idr)
|
||||
{
|
||||
frame_num = 0;
|
||||
for (auto & slot: dpb_slots)
|
||||
{
|
||||
slot.slotIndex = -1;
|
||||
slot.pPictureResource = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const auto slot = frame_num % dpb_slots.size();
|
||||
|
||||
dpb_slots[slot].slotIndex = -1;
|
||||
dpb_slots[slot].pPictureResource = &dpb_resource[slot];
|
||||
|
||||
command_buffer.beginVideoCodingKHR({
|
||||
.pNext = (session_initialized and rate_control) ? &rate_control.value() : nullptr,
|
||||
.videoSession = *video_session,
|
||||
.videoSessionParameters = *video_session_parameters,
|
||||
.referenceSlotCount = uint32_t(dpb_slots.size()),
|
||||
.pReferenceSlots = dpb_slots.data(),
|
||||
});
|
||||
|
||||
dpb_slots[slot].slotIndex = slot;
|
||||
|
||||
if (not session_initialized)
|
||||
{
|
||||
// Initialize encoding session and rate control
|
||||
vk::VideoCodingControlInfoKHR control_info{
|
||||
.flags = vk::VideoCodingControlFlagBitsKHR::eReset,
|
||||
};
|
||||
if (rate_control)
|
||||
{
|
||||
control_info.flags |= vk::VideoCodingControlFlagBitsKHR::eEncodeRateControl;
|
||||
control_info.pNext = &rate_control.value();
|
||||
}
|
||||
command_buffer.controlVideoCodingKHR(control_info);
|
||||
|
||||
// Set decoded picture buffer to correct layout
|
||||
vk::ImageMemoryBarrier2 dpb_barrier{
|
||||
.srcStageMask = vk::PipelineStageFlagBits2KHR::eNone,
|
||||
.srcAccessMask = vk::AccessFlagBits2::eNone,
|
||||
.dstStageMask = vk::PipelineStageFlagBits2KHR::eVideoEncodeKHR,
|
||||
.dstAccessMask = vk::AccessFlagBits2::eVideoEncodeReadKHR | vk::AccessFlagBits2::eVideoEncodeWriteKHR,
|
||||
.oldLayout = vk::ImageLayout::eUndefined,
|
||||
.newLayout = vk::ImageLayout::eVideoEncodeDpbKHR,
|
||||
.image = dpb_image,
|
||||
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = uint32_t(dpb_slots.size())},
|
||||
};
|
||||
command_buffer.pipelineBarrier2({
|
||||
.imageMemoryBarrierCount = 1,
|
||||
.pImageMemoryBarriers = &dpb_barrier,
|
||||
});
|
||||
session_initialized = true;
|
||||
}
|
||||
|
||||
std::vector<vk::VideoReferenceSlotInfoKHR> ref_slots;
|
||||
for (size_t i = 0; i < dpb_slots.size(); ++i)
|
||||
{
|
||||
auto & x = dpb_slots[(i + slot) % dpb_slots.size()];
|
||||
if (x.slotIndex >= 0 and x.slotIndex != slot)
|
||||
ref_slots.push_back(x);
|
||||
}
|
||||
|
||||
vk::VideoEncodeInfoKHR encode_info{
|
||||
.pNext = encode_info_next(frame_num, slot, ref_slots),
|
||||
.dstBuffer = output_buffer,
|
||||
.dstBufferOffset = 0,
|
||||
.dstBufferRange = output_buffer_size,
|
||||
.srcPictureResource = {.codedExtent = rect.extent,
|
||||
.baseArrayLayer = 0,
|
||||
.imageViewBinding = image_views_slots[encode_slot]},
|
||||
.pSetupReferenceSlot = &dpb_slots[slot],
|
||||
.referenceSlotCount = uint32_t(ref_slots.size()),
|
||||
.pReferenceSlots = ref_slots.data(),
|
||||
};
|
||||
|
||||
command_buffer.beginQuery(*query_pool, 0, {});
|
||||
command_buffer.encodeVideoKHR(encode_info);
|
||||
command_buffer.endQuery(*query_pool, 0);
|
||||
command_buffer.endVideoCodingKHR(vk::VideoEndCodingInfoKHR{});
|
||||
command_buffer.end();
|
||||
|
||||
vk::SubmitInfo2 submit{};
|
||||
vk::CommandBufferSubmitInfo cmd_info{
|
||||
.commandBuffer = *command_buffer,
|
||||
};
|
||||
submit.setCommandBufferInfos(cmd_info);
|
||||
vk.encode_queue.submit2(submit, *fence);
|
||||
++frame_num;
|
||||
|
||||
if (idr)
|
||||
send_idr_data();
|
||||
|
||||
if (auto res = vk.device.waitForFences(*fence, true, 1'000'000'000);
|
||||
res != vk::Result::eSuccess)
|
||||
{
|
||||
throw std::runtime_error("wait for fences: " + vk::to_string(res));
|
||||
}
|
||||
|
||||
// Feedback = offset / size / has overrides
|
||||
auto [res, feedback] = query_pool.getResults<uint32_t>(0, 1, 3 * sizeof(uint32_t), 0, vk::QueryResultFlagBits::eWait);
|
||||
if (res != vk::Result::eSuccess)
|
||||
{
|
||||
std::cerr << "device.getQueryPoolResults: " << vk::to_string(res) << std::endl;
|
||||
}
|
||||
|
||||
vk.device.resetFences(*fence);
|
||||
|
||||
return data{
|
||||
.encoder = this,
|
||||
.span = std::span(((uint8_t *)output_buffer.map()) + feedback[0], feedback[1]),
|
||||
};
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan::present_image(vk::Image src_yuv, vk::raii::CommandBuffer &, uint8_t slot)
|
||||
{
|
||||
auto it = image_views.find(src_yuv);
|
||||
if (it != image_views.end())
|
||||
image_views_slots[slot] = *it->second;
|
||||
else
|
||||
{
|
||||
image_view_template.image = src_yuv;
|
||||
image_views_slots[slot] = *image_views.emplace(src_yuv, vk.device.createImageView(image_view_template)).first->second;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
|
||||
#include "video_encoder.h"
|
||||
#include "vk/allocation.h"
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class video_encoder_vulkan : public VideoEncoder
|
||||
{
|
||||
wivrn_vk_bundle & vk;
|
||||
const vk::VideoEncodeCapabilitiesKHR encode_caps;
|
||||
|
||||
vk::raii::Fence fence = nullptr;
|
||||
|
||||
vk::raii::VideoSessionKHR video_session = nullptr;
|
||||
vk::raii::VideoSessionParametersKHR video_session_parameters = nullptr;
|
||||
|
||||
vk::raii::QueryPool query_pool = nullptr;
|
||||
vk::raii::CommandPool command_pool = nullptr;
|
||||
|
||||
vk::raii::CommandBuffer command_buffer = nullptr;
|
||||
|
||||
buffer_allocation output_buffer;
|
||||
size_t output_buffer_size;
|
||||
|
||||
vk::ImageViewUsageCreateInfo image_view_template_next;
|
||||
vk::ImageViewCreateInfo image_view_template;
|
||||
std::unordered_map<VkImage, vk::raii::ImageView> image_views; // for input images
|
||||
std::array<vk::ImageView, num_slots> image_views_slots;
|
||||
|
||||
image_allocation dpb_image;
|
||||
std::vector<vk::raii::ImageView> dpb_image_views;
|
||||
std::vector<vk::VideoPictureResourceInfoKHR> dpb_resource;
|
||||
std::vector<vk::VideoReferenceSlotInfoKHR> dpb_slots;
|
||||
|
||||
std::vector<vk::raii::DeviceMemory> mem;
|
||||
|
||||
vk::VideoFormatPropertiesKHR select_video_format(
|
||||
vk::raii::PhysicalDevice & physical_device,
|
||||
const vk::PhysicalDeviceVideoFormatInfoKHR &);
|
||||
|
||||
uint32_t frame_num = 0;
|
||||
bool session_initialized = false;
|
||||
const vk::Rect2D rect;
|
||||
const float fps;
|
||||
|
||||
vk::VideoEncodeRateControlLayerInfoKHR rate_control_layer;
|
||||
|
||||
protected:
|
||||
const uint8_t num_dpb_slots = 2;
|
||||
std::optional<vk::VideoEncodeRateControlInfoKHR> rate_control;
|
||||
|
||||
video_encoder_vulkan(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR encode_caps, float fps, uint64_t bitrate);
|
||||
|
||||
void init(const vk::VideoCapabilitiesKHR & video_caps,
|
||||
const vk::VideoProfileInfoKHR & video_profile,
|
||||
void * video_session_create_next,
|
||||
void * session_params_next);
|
||||
|
||||
virtual ~video_encoder_vulkan();
|
||||
|
||||
std::vector<uint8_t> get_encoded_parameters(void * next);
|
||||
|
||||
virtual void send_idr_data() = 0;
|
||||
|
||||
virtual std::vector<void *> setup_slot_info(size_t dpb_size) = 0;
|
||||
virtual void * encode_info_next(uint32_t frame_num, size_t slot, const std::vector<vk::VideoReferenceSlotInfoKHR> & reference_slot) = 0;
|
||||
virtual vk::ExtensionProperties std_header_version() = 0;
|
||||
|
||||
public:
|
||||
std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot) override;
|
||||
void present_image(vk::Image src_yuv, vk::raii::CommandBuffer & cmd_buf, uint8_t slot) override;
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -0,0 +1,352 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "video_encoder_vulkan_h264.h"
|
||||
#include "encoder/encoder_settings.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
|
||||
static StdVideoH264LevelIdc compute_level(const StdVideoH264SequenceParameterSet & sps, float fps, uint32_t num_dpb_frames, size_t bitrate)
|
||||
{
|
||||
// H264 required level, table A-1 of specification
|
||||
struct limit
|
||||
{
|
||||
StdVideoH264LevelIdc level;
|
||||
size_t macrobloc_per_s;
|
||||
size_t frame_size; // in macroblocks
|
||||
size_t dpb_size; // in macroblocks
|
||||
size_t bit_rate; // in kb/s
|
||||
};
|
||||
const std::array limits = {
|
||||
// clang-format off
|
||||
// level mb_per_s frame dpb bitrate
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_1_0, 1'485 , 99 , 396 , 64 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_1_1, 3'000 , 396 , 900 , 192 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_1_2, 6'000 , 396 , 2'376 , 384 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_1_3, 11'880 , 396 , 2'376 , 768 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_2_0, 11'880 , 396 , 2'376 , 2'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_2_1, 19'800 , 792 , 4'752 , 4'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_2_2, 20'250 , 1'620 , 8'100 , 4'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_3_0, 40'500 , 1'620 , 8'100 , 10'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_3_1, 108'000 , 3'600 , 18'000 , 14'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_3_2, 216'000 , 5'120 , 20'480 , 20'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_4_0, 245'760 , 8'192 , 32'768 , 20'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_4_1, 245'760 , 8'192 , 32'768 , 50'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_4_2, 522'240 , 8'704 , 34'816 , 50'000 },
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_5_0, 589'824 , 22'080 , 110'400, 135'000},
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_5_1, 983'040 , 36'864 , 184'320, 240'000},
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_5_2, 2'073'600 , 36'864 , 184'320, 240'000},
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_6_0, 4'177'920 , 139'264, 696'320, 240'000},
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_6_1, 8'355'840 , 139'264, 696'320, 480'000},
|
||||
limit{STD_VIDEO_H264_LEVEL_IDC_6_2, 16'711'680, 139'264, 696'320, 800'000},
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
const size_t frame_size = (sps.pic_width_in_mbs_minus1 + 1) * (sps.pic_height_in_map_units_minus1 + 1);
|
||||
const size_t macroblocks_per_s = frame_size * fps + 1;
|
||||
const size_t dpb_size = frame_size * num_dpb_frames;
|
||||
|
||||
for (const auto & level: limits)
|
||||
{
|
||||
if (level.macrobloc_per_s >= macroblocks_per_s and level.frame_size >= frame_size and level.dpb_size >= dpb_size and 1000 * level.bit_rate >= bitrate)
|
||||
return level.level;
|
||||
}
|
||||
|
||||
return STD_VIDEO_H264_LEVEL_IDC_6_2;
|
||||
}
|
||||
|
||||
wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR encode_caps, float fps, uint64_t bitrate) :
|
||||
video_encoder_vulkan(vk, rect, encode_caps, fps, bitrate),
|
||||
sps{
|
||||
.flags =
|
||||
{
|
||||
.constraint_set0_flag = 1,
|
||||
.constraint_set1_flag = 1,
|
||||
.constraint_set2_flag = 0,
|
||||
.constraint_set3_flag = 0,
|
||||
.constraint_set4_flag = 0,
|
||||
.constraint_set5_flag = 0,
|
||||
.direct_8x8_inference_flag = 1,
|
||||
.mb_adaptive_frame_field_flag = 0,
|
||||
.frame_mbs_only_flag = 1,
|
||||
.delta_pic_order_always_zero_flag = 1,
|
||||
.separate_colour_plane_flag = 0,
|
||||
.gaps_in_frame_num_value_allowed_flag = 0,
|
||||
.qpprime_y_zero_transform_bypass_flag = 0,
|
||||
.frame_cropping_flag = (rect.extent.width % 16) || (rect.extent.height) % 16,
|
||||
.seq_scaling_matrix_present_flag = 0,
|
||||
.vui_parameters_present_flag = 0,
|
||||
},
|
||||
.profile_idc = STD_VIDEO_H264_PROFILE_IDC_BASELINE,
|
||||
.level_idc = STD_VIDEO_H264_LEVEL_IDC_INVALID,
|
||||
.chroma_format_idc = STD_VIDEO_H264_CHROMA_FORMAT_IDC_420,
|
||||
.seq_parameter_set_id = 0,
|
||||
.bit_depth_luma_minus8 = 0,
|
||||
.bit_depth_chroma_minus8 = 0,
|
||||
.log2_max_frame_num_minus4 = 0,
|
||||
.pic_order_cnt_type = STD_VIDEO_H264_POC_TYPE_2,
|
||||
.offset_for_non_ref_pic = 0,
|
||||
.offset_for_top_to_bottom_field = 0,
|
||||
.log2_max_pic_order_cnt_lsb_minus4 = 0,
|
||||
.num_ref_frames_in_pic_order_cnt_cycle = 0,
|
||||
.max_num_ref_frames = uint8_t(num_dpb_slots - 1),
|
||||
.reserved1 = 0,
|
||||
.pic_width_in_mbs_minus1 = (rect.extent.width - 1) / 16,
|
||||
.pic_height_in_map_units_minus1 = (rect.extent.height - 1) / 16,
|
||||
.frame_crop_left_offset = 0,
|
||||
.frame_crop_right_offset = (rect.extent.width % 16) / 2,
|
||||
.frame_crop_top_offset = 0,
|
||||
.frame_crop_bottom_offset = (rect.extent.height % 16) / 2,
|
||||
.reserved2 = 0,
|
||||
.pOffsetForRefFrame = nullptr,
|
||||
.pScalingLists = nullptr,
|
||||
.pSequenceParameterSetVui = nullptr,
|
||||
},
|
||||
pps{
|
||||
.flags =
|
||||
{
|
||||
.transform_8x8_mode_flag = 0,
|
||||
.redundant_pic_cnt_present_flag = 0,
|
||||
.constrained_intra_pred_flag = 0,
|
||||
.deblocking_filter_control_present_flag = 0,
|
||||
.weighted_pred_flag = 0,
|
||||
.bottom_field_pic_order_in_frame_present_flag = 0,
|
||||
.entropy_coding_mode_flag = 1,
|
||||
.pic_scaling_matrix_present_flag = 0,
|
||||
},
|
||||
.seq_parameter_set_id = 0,
|
||||
.pic_parameter_set_id = 0,
|
||||
.num_ref_idx_l0_default_active_minus1 = 0,
|
||||
.num_ref_idx_l1_default_active_minus1 = 0,
|
||||
.weighted_bipred_idc = STD_VIDEO_H264_WEIGHTED_BIPRED_IDC_DEFAULT,
|
||||
.pic_init_qp_minus26 = 0,
|
||||
.pic_init_qs_minus26 = 0,
|
||||
.chroma_qp_index_offset = 0,
|
||||
.second_chroma_qp_index_offset = 0,
|
||||
.pScalingLists = nullptr,
|
||||
}
|
||||
{
|
||||
sps.level_idc = compute_level(sps, fps, num_dpb_slots, bitrate);
|
||||
if (not std::ranges::any_of(vk.device_extensions, [](std::string_view ext) { return ext == VK_KHR_VIDEO_ENCODE_H264_EXTENSION_NAME; }))
|
||||
{
|
||||
throw std::runtime_error("Vulkan video encode H264 extension not available");
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<void *> wivrn::video_encoder_vulkan_h264::setup_slot_info(size_t dpb_size)
|
||||
{
|
||||
dpb_std_info.resize(dpb_size, {});
|
||||
dpb_std_slots.reserve(dpb_size);
|
||||
std::vector<void *> res;
|
||||
for (size_t i = 0; i < dpb_size; ++i)
|
||||
{
|
||||
dpb_std_slots.push_back({
|
||||
.pStdReferenceInfo = &dpb_std_info[i],
|
||||
});
|
||||
res.push_back(&dpb_std_slots[i]);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h264::create(
|
||||
wivrn_vk_bundle & vk,
|
||||
encoder_settings & settings,
|
||||
float fps)
|
||||
{
|
||||
vk::Rect2D rect{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
|
||||
auto [video_caps, encode_caps, encode_h264_caps] =
|
||||
vk.physical_device.getVideoCapabilitiesKHR<
|
||||
vk::VideoCapabilitiesKHR,
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH264CapabilitiesKHR>(video_profile_info.get());
|
||||
|
||||
std::unique_ptr<video_encoder_vulkan_h264> self(new video_encoder_vulkan_h264(vk, rect, encode_caps, fps, settings.bitrate));
|
||||
|
||||
vk::VideoEncodeH264SessionParametersAddInfoKHR h264_add_info{};
|
||||
h264_add_info.setStdSPSs(self->sps);
|
||||
h264_add_info.setStdPPSs(self->pps);
|
||||
|
||||
vk::VideoEncodeH264SessionParametersCreateInfoKHR h264_session_params{
|
||||
.maxStdSPSCount = 1,
|
||||
.maxStdPPSCount = 1,
|
||||
.pParametersAddInfo = &h264_add_info,
|
||||
};
|
||||
|
||||
vk::VideoEncodeH264SessionCreateInfoKHR session_create_info{
|
||||
.useMaxLevelIdc = false,
|
||||
};
|
||||
|
||||
if (encode_h264_caps.requiresGopRemainingFrames)
|
||||
{
|
||||
self->gop_info = vk::VideoEncodeH264GopRemainingFrameInfoKHR{
|
||||
.useGopRemainingFrames = true,
|
||||
.gopRemainingI = 0,
|
||||
.gopRemainingP = std::numeric_limits<uint32_t>::max(),
|
||||
.gopRemainingB = 0,
|
||||
};
|
||||
self->rate_control_h264 = vk::VideoEncodeH264RateControlInfoKHR{
|
||||
.pNext = &self->gop_info,
|
||||
.gopFrameCount = std::numeric_limits<uint32_t>::max(),
|
||||
.idrPeriod = std::numeric_limits<uint32_t>::max(),
|
||||
};
|
||||
self->rate_control->pNext = &self->rate_control_h264;
|
||||
}
|
||||
|
||||
self->init(video_caps, video_profile_info.get(), &session_create_info, &h264_session_params);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> wivrn::video_encoder_vulkan_h264::get_sps_pps()
|
||||
{
|
||||
vk::VideoEncodeH264SessionParametersGetInfoKHR next{
|
||||
.writeStdSPS = true,
|
||||
.writeStdPPS = true,
|
||||
};
|
||||
return get_encoded_parameters(&next);
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan_h264::send_idr_data()
|
||||
{
|
||||
auto data = get_sps_pps();
|
||||
SendData(data, false);
|
||||
}
|
||||
|
||||
void * wivrn::video_encoder_vulkan_h264::encode_info_next(uint32_t frame_num, size_t slot, const std::vector<vk::VideoReferenceSlotInfoKHR> & ref_slots)
|
||||
{
|
||||
slice_header = {
|
||||
.flags =
|
||||
{
|
||||
.direct_spatial_mv_pred_flag = 0, //?
|
||||
.num_ref_idx_active_override_flag = 0,
|
||||
.reserved = 0,
|
||||
},
|
||||
.first_mb_in_slice = 0,
|
||||
.slice_type = ref_slots.empty() ? STD_VIDEO_H264_SLICE_TYPE_I
|
||||
: STD_VIDEO_H264_SLICE_TYPE_P,
|
||||
.slice_alpha_c0_offset_div2 = 0,
|
||||
.slice_beta_offset_div2 = 0,
|
||||
.slice_qp_delta = 0,
|
||||
.reserved1 = 0,
|
||||
.cabac_init_idc = STD_VIDEO_H264_CABAC_INIT_IDC_0,
|
||||
.disable_deblocking_filter_idc = STD_VIDEO_H264_DISABLE_DEBLOCKING_FILTER_IDC_ENABLED,
|
||||
.pWeightTable = nullptr,
|
||||
};
|
||||
nalu_slice_info = vk::VideoEncodeH264NaluSliceInfoKHR{
|
||||
.pStdSliceHeader = &slice_header,
|
||||
};
|
||||
reference_lists_info = {
|
||||
.flags =
|
||||
{
|
||||
.ref_pic_list_modification_flag_l0 = 0,
|
||||
.ref_pic_list_modification_flag_l1 = 0,
|
||||
.reserved = 0,
|
||||
},
|
||||
.num_ref_idx_l0_active_minus1 = 0,
|
||||
.num_ref_idx_l1_active_minus1 = 0,
|
||||
.RefPicList0 = {},
|
||||
.RefPicList1 = {},
|
||||
.refList0ModOpCount = 0,
|
||||
.refList1ModOpCount = 0,
|
||||
.refPicMarkingOpCount = 0,
|
||||
.reserved1 = {},
|
||||
.pRefList0ModOperations = nullptr,
|
||||
.pRefList1ModOperations = nullptr,
|
||||
.pRefPicMarkingOperations = nullptr,
|
||||
};
|
||||
std::ranges::fill(reference_lists_info.RefPicList0, STD_VIDEO_H264_NO_REFERENCE_PICTURE);
|
||||
std::ranges::fill(reference_lists_info.RefPicList1, STD_VIDEO_H264_NO_REFERENCE_PICTURE);
|
||||
|
||||
for (size_t i = 0; i < ref_slots.size(); ++i)
|
||||
reference_lists_info.RefPicList0[i] = ref_slots[i].slotIndex;
|
||||
|
||||
std_picture_info = {
|
||||
.flags =
|
||||
{
|
||||
.IdrPicFlag = uint32_t(ref_slots.empty() ? 1 : 0),
|
||||
.is_reference = 1,
|
||||
.no_output_of_prior_pics_flag = 0,
|
||||
.long_term_reference_flag = 0,
|
||||
.adaptive_ref_pic_marking_mode_flag = 0,
|
||||
.reserved = 0,
|
||||
},
|
||||
.seq_parameter_set_id = 0,
|
||||
.pic_parameter_set_id = 0,
|
||||
.idr_pic_id = idr_id,
|
||||
.primary_pic_type = ref_slots.empty() ? STD_VIDEO_H264_PICTURE_TYPE_IDR
|
||||
: STD_VIDEO_H264_PICTURE_TYPE_P,
|
||||
.frame_num = frame_num & ((1 << (sps.log2_max_frame_num_minus4 + 4)) - 1),
|
||||
.PicOrderCnt = int32_t((2 * frame_num) & ((1 << (sps.log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1)),
|
||||
.temporal_id = 0,
|
||||
.reserved1 = {},
|
||||
.pRefLists = &reference_lists_info,
|
||||
};
|
||||
picture_info = vk::VideoEncodeH264PictureInfoKHR{
|
||||
.naluSliceEntryCount = 1,
|
||||
.pNaluSliceEntries = &nalu_slice_info,
|
||||
.pStdPictureInfo = &std_picture_info,
|
||||
.generatePrefixNalu = false, // check if useful, check if supported
|
||||
};
|
||||
|
||||
{
|
||||
auto & i = dpb_std_info[slot];
|
||||
i.primary_pic_type = std_picture_info.primary_pic_type;
|
||||
i.FrameNum = std_picture_info.frame_num;
|
||||
i.PicOrderCnt = std_picture_info.PicOrderCnt;
|
||||
}
|
||||
|
||||
if (ref_slots.empty())
|
||||
++idr_id;
|
||||
|
||||
return &picture_info;
|
||||
}
|
||||
vk::ExtensionProperties wivrn::video_encoder_vulkan_h264::std_header_version()
|
||||
{
|
||||
vk::ExtensionProperties std_header_version{
|
||||
.specVersion = VK_MAKE_VIDEO_STD_VERSION(1, 0, 0),
|
||||
};
|
||||
strcpy(std_header_version.extensionName,
|
||||
VK_STD_VULKAN_VIDEO_CODEC_H264_ENCODE_EXTENSION_NAME);
|
||||
return std_header_version;
|
||||
}
|
||||
|
||||
decltype(wivrn::video_encoder_vulkan_h264::video_profile_info) wivrn::video_encoder_vulkan_h264::video_profile_info = vk::StructureChain{
|
||||
vk::VideoProfileInfoKHR{
|
||||
.videoCodecOperation =
|
||||
vk::VideoCodecOperationFlagBitsKHR::eEncodeH264,
|
||||
.chromaSubsampling = vk::VideoChromaSubsamplingFlagBitsKHR::e420,
|
||||
.lumaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
.chromaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
},
|
||||
vk::VideoEncodeH264ProfileInfoKHR{
|
||||
.stdProfileIdc = STD_VIDEO_H264_PROFILE_IDC_BASELINE,
|
||||
},
|
||||
vk::VideoEncodeUsageInfoKHR{
|
||||
.videoUsageHints = vk::VideoEncodeUsageFlagBitsKHR::eStreaming,
|
||||
.videoContentHints = vk::VideoEncodeContentFlagBitsKHR::eRendered,
|
||||
.tuningMode = vk::VideoEncodeTuningModeKHR::eUltraLowLatency,
|
||||
}};
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "video_encoder_vulkan.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class video_encoder_vulkan_h264 : public video_encoder_vulkan
|
||||
{
|
||||
uint16_t idr_id = 0;
|
||||
StdVideoH264SequenceParameterSet sps;
|
||||
StdVideoH264PictureParameterSet pps;
|
||||
|
||||
StdVideoEncodeH264SliceHeader slice_header;
|
||||
vk::VideoEncodeH264NaluSliceInfoKHR nalu_slice_info;
|
||||
|
||||
StdVideoEncodeH264PictureInfo std_picture_info;
|
||||
vk::VideoEncodeH264PictureInfoKHR picture_info;
|
||||
|
||||
StdVideoEncodeH264ReferenceListsInfo reference_lists_info;
|
||||
|
||||
std::vector<StdVideoEncodeH264ReferenceInfo> dpb_std_info;
|
||||
std::vector<vk::VideoEncodeH264DpbSlotInfoKHR> dpb_std_slots;
|
||||
|
||||
vk::VideoEncodeH264GopRemainingFrameInfoKHR gop_info;
|
||||
vk::VideoEncodeH264RateControlInfoKHR rate_control_h264;
|
||||
|
||||
video_encoder_vulkan_h264(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR encode_caps, float fps, uint64_t bitrate);
|
||||
|
||||
protected:
|
||||
std::vector<void *> setup_slot_info(size_t dpb_size) override;
|
||||
|
||||
void * encode_info_next(uint32_t frame_num, size_t slot, const std::vector<vk::VideoReferenceSlotInfoKHR> &) override;
|
||||
virtual vk::ExtensionProperties std_header_version() override;
|
||||
|
||||
void send_idr_data() override;
|
||||
|
||||
public:
|
||||
static std::unique_ptr<video_encoder_vulkan_h264> create(wivrn_vk_bundle & vk,
|
||||
encoder_settings & settings,
|
||||
float fps);
|
||||
|
||||
std::vector<uint8_t> get_sps_pps();
|
||||
|
||||
static const vk::StructureChain<vk::VideoProfileInfoKHR, vk::VideoEncodeH264ProfileInfoKHR, vk::VideoEncodeUsageInfoKHR> video_profile_info;
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -0,0 +1,411 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "video_encoder_vulkan_h265.h"
|
||||
#include "encoder/encoder_settings.h"
|
||||
#include "util/u_logging.h"
|
||||
#include "utils/wivrn_vk_bundle.h"
|
||||
|
||||
wivrn::video_encoder_vulkan_h265::video_encoder_vulkan_h265(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR encode_caps, float fps, uint64_t bitrate) :
|
||||
video_encoder_vulkan(vk, rect, encode_caps, fps, bitrate),
|
||||
vps{
|
||||
.flags{
|
||||
.vps_temporal_id_nesting_flag = 0,
|
||||
.vps_sub_layer_ordering_info_present_flag = 0,
|
||||
.vps_timing_info_present_flag = 0,
|
||||
.vps_poc_proportional_to_timing_flag = 0,
|
||||
},
|
||||
.vps_video_parameter_set_id = 0,
|
||||
.vps_max_sub_layers_minus1 = 0,
|
||||
.reserved1 = 0,
|
||||
.reserved2 = 0,
|
||||
.vps_num_units_in_tick = 0,
|
||||
.vps_time_scale = 0,
|
||||
.vps_num_ticks_poc_diff_one_minus1 = 0,
|
||||
},
|
||||
sps{
|
||||
.flags{
|
||||
.sps_temporal_id_nesting_flag = 0,
|
||||
.separate_colour_plane_flag = 0,
|
||||
.conformance_window_flag = 0,
|
||||
.sps_sub_layer_ordering_info_present_flag = 0,
|
||||
.scaling_list_enabled_flag = 0,
|
||||
.sps_scaling_list_data_present_flag = 0,
|
||||
.amp_enabled_flag = 0,
|
||||
.sample_adaptive_offset_enabled_flag = 0,
|
||||
.pcm_enabled_flag = 0,
|
||||
.pcm_loop_filter_disabled_flag = 0,
|
||||
.long_term_ref_pics_present_flag = 0,
|
||||
.sps_temporal_mvp_enabled_flag = 0,
|
||||
.strong_intra_smoothing_enabled_flag = 0,
|
||||
.vui_parameters_present_flag = 0,
|
||||
.sps_extension_present_flag = 0,
|
||||
.sps_range_extension_flag = 0,
|
||||
.transform_skip_rotation_enabled_flag = 0,
|
||||
.transform_skip_context_enabled_flag = 0,
|
||||
.implicit_rdpcm_enabled_flag = 0,
|
||||
.explicit_rdpcm_enabled_flag = 0,
|
||||
.extended_precision_processing_flag = 0,
|
||||
.intra_smoothing_disabled_flag = 0,
|
||||
.high_precision_offsets_enabled_flag = 0,
|
||||
.persistent_rice_adaptation_enabled_flag = 0,
|
||||
.cabac_bypass_alignment_enabled_flag = 0,
|
||||
.sps_scc_extension_flag = 0,
|
||||
.sps_curr_pic_ref_enabled_flag = 0,
|
||||
.palette_mode_enabled_flag = 0,
|
||||
.sps_palette_predictor_initializers_present_flag = 0,
|
||||
.intra_boundary_filtering_disabled_flag = 0,
|
||||
},
|
||||
.chroma_format_idc = STD_VIDEO_H265_CHROMA_FORMAT_IDC_420,
|
||||
.pic_width_in_luma_samples = 0,
|
||||
.pic_height_in_luma_samples = 0,
|
||||
.sps_video_parameter_set_id = 0,
|
||||
.sps_max_sub_layers_minus1 = 0,
|
||||
.sps_seq_parameter_set_id = 0,
|
||||
.bit_depth_luma_minus8 = 0,
|
||||
.bit_depth_chroma_minus8 = 0,
|
||||
.log2_max_pic_order_cnt_lsb_minus4 = 0,
|
||||
.log2_min_luma_coding_block_size_minus3 = 0,
|
||||
.log2_diff_max_min_luma_coding_block_size = 0,
|
||||
.log2_min_luma_transform_block_size_minus2 = 0,
|
||||
.log2_diff_max_min_luma_transform_block_size = 0,
|
||||
.max_transform_hierarchy_depth_inter = 0,
|
||||
.max_transform_hierarchy_depth_intra = 0,
|
||||
.num_short_term_ref_pic_sets = 0,
|
||||
.num_long_term_ref_pics_sps = 0,
|
||||
.pcm_sample_bit_depth_luma_minus1 = 0,
|
||||
.pcm_sample_bit_depth_chroma_minus1 = 0,
|
||||
.log2_min_pcm_luma_coding_block_size_minus3 = 0,
|
||||
.log2_diff_max_min_pcm_luma_coding_block_size = 0,
|
||||
.reserved1 = 0,
|
||||
.reserved2 = 0,
|
||||
.palette_max_size = 0,
|
||||
.delta_palette_max_predictor_size = 0,
|
||||
.motion_vector_resolution_control_idc = 0,
|
||||
.sps_num_palette_predictor_initializers_minus1 = 0,
|
||||
.conf_win_left_offset = 0,
|
||||
.conf_win_right_offset = 0,
|
||||
.conf_win_top_offset = 0,
|
||||
.conf_win_bottom_offset = 0,
|
||||
},
|
||||
pps{
|
||||
.flags =
|
||||
{
|
||||
.dependent_slice_segments_enabled_flag = 0,
|
||||
.output_flag_present_flag = 0,
|
||||
.sign_data_hiding_enabled_flag = 0,
|
||||
.cabac_init_present_flag = 0,
|
||||
.constrained_intra_pred_flag = 0,
|
||||
.transform_skip_enabled_flag = 0,
|
||||
.cu_qp_delta_enabled_flag = 0,
|
||||
.pps_slice_chroma_qp_offsets_present_flag = 0,
|
||||
.weighted_pred_flag = 0,
|
||||
.weighted_bipred_flag = 0,
|
||||
.transquant_bypass_enabled_flag = 0,
|
||||
.tiles_enabled_flag = 0,
|
||||
.entropy_coding_sync_enabled_flag = 0,
|
||||
.uniform_spacing_flag = 0,
|
||||
.loop_filter_across_tiles_enabled_flag = 0,
|
||||
.pps_loop_filter_across_slices_enabled_flag = 0,
|
||||
.deblocking_filter_control_present_flag = 0,
|
||||
.deblocking_filter_override_enabled_flag = 0,
|
||||
.pps_deblocking_filter_disabled_flag = 0,
|
||||
.pps_scaling_list_data_present_flag = 0,
|
||||
.lists_modification_present_flag = 0,
|
||||
.slice_segment_header_extension_present_flag = 0,
|
||||
.pps_extension_present_flag = 0,
|
||||
.cross_component_prediction_enabled_flag = 0,
|
||||
.chroma_qp_offset_list_enabled_flag = 0,
|
||||
.pps_curr_pic_ref_enabled_flag = 0,
|
||||
.residual_adaptive_colour_transform_enabled_flag = 0,
|
||||
.pps_slice_act_qp_offsets_present_flag = 0,
|
||||
.pps_palette_predictor_initializers_present_flag = 0,
|
||||
.monochrome_palette_flag = 0,
|
||||
.pps_range_extension_flag = 0,
|
||||
},
|
||||
.pps_pic_parameter_set_id = 0,
|
||||
.pps_seq_parameter_set_id = 0,
|
||||
.sps_video_parameter_set_id = 0,
|
||||
.num_extra_slice_header_bits = 0,
|
||||
.num_ref_idx_l0_default_active_minus1 = 0,
|
||||
.num_ref_idx_l1_default_active_minus1 = 0,
|
||||
.init_qp_minus26 = 0,
|
||||
.diff_cu_qp_delta_depth = 0,
|
||||
.pps_cb_qp_offset = 0,
|
||||
.pps_cr_qp_offset = 0,
|
||||
.pps_beta_offset_div2 = 0,
|
||||
.pps_tc_offset_div2 = 0,
|
||||
.log2_parallel_merge_level_minus2 = 0,
|
||||
.log2_max_transform_skip_block_size_minus2 = 0,
|
||||
.diff_cu_chroma_qp_offset_depth = 0,
|
||||
.chroma_qp_offset_list_len_minus1 = 0,
|
||||
.cb_qp_offset_list = {},
|
||||
.cr_qp_offset_list = {},
|
||||
.log2_sao_offset_scale_luma = 0,
|
||||
.log2_sao_offset_scale_chroma = 0,
|
||||
.pps_act_y_qp_offset_plus5 = 0,
|
||||
.pps_act_cb_qp_offset_plus5 = 0,
|
||||
.pps_act_cr_qp_offset_plus3 = 0,
|
||||
.pps_num_palette_predictor_initializers = 0,
|
||||
.luma_bit_depth_entry_minus8 = 0,
|
||||
.chroma_bit_depth_entry_minus8 = 0,
|
||||
.num_tile_columns_minus1 = 0,
|
||||
.num_tile_rows_minus1 = 0,
|
||||
.reserved1 = 0,
|
||||
.reserved2 = 0,
|
||||
.column_width_minus1 = {},
|
||||
.row_height_minus1 = {},
|
||||
.reserved3 = 0,
|
||||
}
|
||||
{
|
||||
if (not std::ranges::any_of(vk.device_extensions, [](std::string_view ext) { return ext == VK_KHR_VIDEO_ENCODE_H265_EXTENSION_NAME; }))
|
||||
{
|
||||
throw std::runtime_error("Vulkan video encode H265 extension not available");
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<void *> wivrn::video_encoder_vulkan_h265::setup_slot_info(size_t dpb_size)
|
||||
{
|
||||
dpb_std_info.resize(dpb_size, {});
|
||||
dpb_std_slots.reserve(dpb_size);
|
||||
std::vector<void *> res;
|
||||
for (size_t i = 0; i < dpb_size; ++i)
|
||||
{
|
||||
dpb_std_slots.push_back({
|
||||
.pStdReferenceInfo = &dpb_std_info[i],
|
||||
});
|
||||
res.push_back(&dpb_std_slots[i]);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
std::unique_ptr<wivrn::video_encoder_vulkan_h265> wivrn::video_encoder_vulkan_h265::create(
|
||||
wivrn_vk_bundle & vk,
|
||||
encoder_settings & settings,
|
||||
float fps)
|
||||
{
|
||||
vk::Rect2D rect{
|
||||
.offset = {
|
||||
.x = settings.offset_x,
|
||||
.y = settings.offset_y,
|
||||
},
|
||||
.extent = {
|
||||
.width = settings.width,
|
||||
.height = settings.height,
|
||||
},
|
||||
};
|
||||
|
||||
auto [video_caps, encode_caps, encode_h265_caps] =
|
||||
vk.physical_device.getVideoCapabilitiesKHR<
|
||||
vk::VideoCapabilitiesKHR,
|
||||
vk::VideoEncodeCapabilitiesKHR,
|
||||
vk::VideoEncodeH265CapabilitiesKHR>(video_profile_info.get());
|
||||
|
||||
std::unique_ptr<video_encoder_vulkan_h265> self(new video_encoder_vulkan_h265(vk, rect, encode_caps, fps, settings.bitrate));
|
||||
|
||||
vk::VideoEncodeH265SessionParametersAddInfoKHR h265_add_info{};
|
||||
h265_add_info.setStdVPSs(self->vps);
|
||||
h265_add_info.setStdSPSs(self->sps);
|
||||
h265_add_info.setStdPPSs(self->pps);
|
||||
|
||||
vk::VideoEncodeH265SessionParametersCreateInfoKHR h265_session_params{
|
||||
.maxStdVPSCount = 1,
|
||||
.maxStdSPSCount = 1,
|
||||
.maxStdPPSCount = 1,
|
||||
.pParametersAddInfo = &h265_add_info,
|
||||
};
|
||||
|
||||
vk::VideoEncodeH265SessionCreateInfoKHR session_create_info{
|
||||
.useMaxLevelIdc = false,
|
||||
};
|
||||
|
||||
if (encode_h265_caps.requiresGopRemainingFrames)
|
||||
{
|
||||
self->gop_info = vk::VideoEncodeH265GopRemainingFrameInfoKHR{
|
||||
.useGopRemainingFrames = true,
|
||||
.gopRemainingI = 0,
|
||||
.gopRemainingP = std::numeric_limits<uint32_t>::max(),
|
||||
.gopRemainingB = 0,
|
||||
};
|
||||
self->rate_control_h265 = vk::VideoEncodeH265RateControlInfoKHR{
|
||||
.pNext = &self->gop_info,
|
||||
.gopFrameCount = std::numeric_limits<uint32_t>::max(),
|
||||
.idrPeriod = std::numeric_limits<uint32_t>::max(),
|
||||
};
|
||||
self->rate_control->pNext = &self->rate_control_h265;
|
||||
}
|
||||
|
||||
self->init(video_caps, video_profile_info.get(), &session_create_info, &h265_session_params);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> wivrn::video_encoder_vulkan_h265::get_vps_sps_pps()
|
||||
{
|
||||
vk::VideoEncodeH265SessionParametersGetInfoKHR next{
|
||||
.writeStdVPS = true,
|
||||
.writeStdSPS = true,
|
||||
.writeStdPPS = true,
|
||||
};
|
||||
return get_encoded_parameters(&next);
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan_h265::send_idr_data()
|
||||
{
|
||||
auto data = get_vps_sps_pps();
|
||||
SendData(data, false);
|
||||
}
|
||||
|
||||
void * wivrn::video_encoder_vulkan_h265::encode_info_next(uint32_t frame_num, size_t slot, std::optional<size_t> ref)
|
||||
{
|
||||
slice_header = {
|
||||
.flags =
|
||||
{
|
||||
.first_slice_segment_in_pic_flag = 0,
|
||||
.dependent_slice_segment_flag = 0,
|
||||
.slice_sao_luma_flag = 0,
|
||||
.slice_sao_chroma_flag = 0,
|
||||
.num_ref_idx_active_override_flag = 0,
|
||||
.mvd_l1_zero_flag = 0,
|
||||
.cabac_init_flag = 0,
|
||||
.cu_chroma_qp_offset_enabled_flag = 0,
|
||||
.deblocking_filter_override_flag = 0,
|
||||
.slice_deblocking_filter_disabled_flag = 0,
|
||||
.collocated_from_l0_flag = 0,
|
||||
.slice_loop_filter_across_slices_enabled_flag = 0,
|
||||
},
|
||||
.slice_type = ref ? STD_VIDEO_H265_SLICE_TYPE_P
|
||||
: STD_VIDEO_H265_SLICE_TYPE_I,
|
||||
.slice_segment_address = 0,
|
||||
.collocated_ref_idx = 0,
|
||||
.MaxNumMergeCand = 0,
|
||||
.slice_cb_qp_offset = 0,
|
||||
.slice_cr_qp_offset = 0,
|
||||
.slice_beta_offset_div2 = 0,
|
||||
.slice_tc_offset_div2 = 0,
|
||||
.slice_act_y_qp_offset = 0,
|
||||
.slice_act_cb_qp_offset = 0,
|
||||
.slice_act_cr_qp_offset = 0,
|
||||
.slice_qp_delta = 0,
|
||||
};
|
||||
nalu_slice_info = vk::VideoEncodeH265NaluSliceSegmentInfoKHR{
|
||||
.pStdSliceSegmentHeader = &slice_header,
|
||||
};
|
||||
reference_lists_info = {
|
||||
.flags =
|
||||
{
|
||||
.ref_pic_list_modification_flag_l0 = 0,
|
||||
.ref_pic_list_modification_flag_l1 = 0,
|
||||
},
|
||||
.num_ref_idx_l0_active_minus1 = 0,
|
||||
.num_ref_idx_l1_active_minus1 = 0,
|
||||
.RefPicList0 = {},
|
||||
.RefPicList1 = {},
|
||||
.list_entry_l0 = {},
|
||||
.list_entry_l1 = {},
|
||||
};
|
||||
std::fill(reference_lists_info.RefPicList0,
|
||||
reference_lists_info.RefPicList0 + sizeof(reference_lists_info.RefPicList0),
|
||||
STD_VIDEO_H265_NO_REFERENCE_PICTURE);
|
||||
std::fill(reference_lists_info.RefPicList1,
|
||||
reference_lists_info.RefPicList1 + sizeof(reference_lists_info.RefPicList1),
|
||||
STD_VIDEO_H265_NO_REFERENCE_PICTURE);
|
||||
if (ref)
|
||||
{
|
||||
reference_lists_info.RefPicList0[0] = *ref;
|
||||
}
|
||||
|
||||
std_picture_info = {
|
||||
.flags =
|
||||
{
|
||||
.is_reference = 1,
|
||||
.IrapPicFlag = uint32_t(ref ? 0 : 1),
|
||||
.used_for_long_term_reference = 0,
|
||||
.discardable_flag = 0,
|
||||
.cross_layer_bla_flag = 0,
|
||||
.pic_output_flag = 0,
|
||||
.no_output_of_prior_pics_flag = 0,
|
||||
.short_term_ref_pic_set_sps_flag = 0,
|
||||
.slice_temporal_mvp_enabled_flag = 0,
|
||||
},
|
||||
.pic_type = ref ? STD_VIDEO_H265_PICTURE_TYPE_P
|
||||
: STD_VIDEO_H265_PICTURE_TYPE_IDR,
|
||||
.sps_video_parameter_set_id = 0,
|
||||
.pps_seq_parameter_set_id = 0,
|
||||
.pps_pic_parameter_set_id = 0,
|
||||
.short_term_ref_pic_set_idx = 0,
|
||||
.PicOrderCntVal = 0,
|
||||
.TemporalId = 0,
|
||||
};
|
||||
picture_info = vk::VideoEncodeH265PictureInfoKHR{
|
||||
.naluSliceSegmentEntryCount = 1,
|
||||
.pNaluSliceSegmentEntries = &nalu_slice_info,
|
||||
.pStdPictureInfo = &std_picture_info,
|
||||
};
|
||||
|
||||
dpb_std_info[slot].pic_type = std_picture_info.pic_type;
|
||||
|
||||
if (not ref)
|
||||
++idr_id;
|
||||
|
||||
return &picture_info;
|
||||
}
|
||||
vk::ExtensionProperties wivrn::video_encoder_vulkan_h265::std_header_version()
|
||||
{
|
||||
// FIXME: update to version 1.0
|
||||
vk::ExtensionProperties std_header_version{
|
||||
.specVersion = 0x0000900b, // VK_MAKE_VIDEO_STD_VERSION(1, 0, 0),
|
||||
};
|
||||
strcpy(std_header_version.extensionName,
|
||||
VK_STD_VULKAN_VIDEO_CODEC_H265_ENCODE_EXTENSION_NAME);
|
||||
return std_header_version;
|
||||
}
|
||||
|
||||
decltype(wivrn::video_encoder_vulkan_h265::video_profile_info) wivrn::video_encoder_vulkan_h265::video_profile_info = vk::StructureChain{
|
||||
vk::VideoProfileInfoKHR{
|
||||
.videoCodecOperation =
|
||||
vk::VideoCodecOperationFlagBitsKHR::eEncodeH265,
|
||||
.chromaSubsampling = vk::VideoChromaSubsamplingFlagBitsKHR::e420,
|
||||
.lumaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
.chromaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
},
|
||||
vk::VideoEncodeH265ProfileInfoKHR{
|
||||
.stdProfileIdc = STD_VIDEO_H265_PROFILE_IDC_MAIN,
|
||||
},
|
||||
vk::VideoEncodeUsageInfoKHR{
|
||||
.videoUsageHints = vk::VideoEncodeUsageFlagBitsKHR::eStreaming,
|
||||
.videoContentHints = vk::VideoEncodeContentFlagBitsKHR::eRendered,
|
||||
.tuningMode = vk::VideoEncodeTuningModeKHR::eUltraLowLatency,
|
||||
}};
|
||||
vk::StructureChain video_profile_info{
|
||||
vk::VideoProfileInfoKHR{
|
||||
.videoCodecOperation =
|
||||
vk::VideoCodecOperationFlagBitsKHR::eEncodeH265,
|
||||
.chromaSubsampling = vk::VideoChromaSubsamplingFlagBitsKHR::e420,
|
||||
.lumaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
.chromaBitDepth = vk::VideoComponentBitDepthFlagBitsKHR::e8,
|
||||
},
|
||||
vk::VideoEncodeH265ProfileInfoKHR{
|
||||
.stdProfileIdc = STD_VIDEO_H265_PROFILE_IDC_MAIN,
|
||||
},
|
||||
vk::VideoEncodeUsageInfoKHR{
|
||||
.videoUsageHints = vk::VideoEncodeUsageFlagBitsKHR::eStreaming,
|
||||
.videoContentHints = vk::VideoEncodeContentFlagBitsKHR::eRendered,
|
||||
.tuningMode = vk::VideoEncodeTuningModeKHR::eUltraLowLatency,
|
||||
}};
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "video_encoder_vulkan.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
namespace wivrn
|
||||
{
|
||||
class video_encoder_vulkan_h265 : public video_encoder_vulkan
|
||||
{
|
||||
uint16_t idr_id = 0;
|
||||
StdVideoH265VideoParameterSet vps;
|
||||
StdVideoH265SequenceParameterSet sps;
|
||||
StdVideoH265PictureParameterSet pps;
|
||||
|
||||
StdVideoEncodeH265SliceSegmentHeader slice_header;
|
||||
vk::VideoEncodeH265NaluSliceSegmentInfoKHR nalu_slice_info;
|
||||
|
||||
StdVideoEncodeH265PictureInfo std_picture_info;
|
||||
vk::VideoEncodeH265PictureInfoKHR picture_info;
|
||||
|
||||
StdVideoEncodeH265ReferenceListsInfo reference_lists_info;
|
||||
|
||||
std::vector<StdVideoEncodeH265ReferenceInfo> dpb_std_info;
|
||||
std::vector<vk::VideoEncodeH265DpbSlotInfoKHR> dpb_std_slots;
|
||||
|
||||
vk::VideoEncodeH265GopRemainingFrameInfoKHR gop_info;
|
||||
vk::VideoEncodeH265RateControlInfoKHR rate_control_h265;
|
||||
|
||||
video_encoder_vulkan_h265(wivrn_vk_bundle & vk, vk::Rect2D rect, vk::VideoEncodeCapabilitiesKHR encode_caps, float fps, uint64_t bitrate);
|
||||
|
||||
protected:
|
||||
std::vector<void *> setup_slot_info(size_t dpb_size) override;
|
||||
|
||||
void * encode_info_next(uint32_t frame_num, size_t slot, std::optional<size_t> ref) override;
|
||||
virtual vk::ExtensionProperties std_header_version() override;
|
||||
|
||||
void send_idr_data() override;
|
||||
|
||||
public:
|
||||
static std::unique_ptr<video_encoder_vulkan_h265> create(wivrn_vk_bundle & vk,
|
||||
encoder_settings & settings,
|
||||
float fps);
|
||||
|
||||
std::vector<uint8_t> get_vps_sps_pps();
|
||||
|
||||
static const vk::StructureChain<vk::VideoProfileInfoKHR, vk::VideoEncodeH265ProfileInfoKHR, vk::VideoEncodeUsageInfoKHR> video_profile_info;
|
||||
};
|
||||
} // namespace wivrn
|
||||
@@ -23,6 +23,16 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
[[maybe_unused]] vk::raii::Queue make_queue(vk::raii::Device & device, VkQueue queue, uint32_t family_index, uint32_t index)
|
||||
{
|
||||
if (queue == VK_NULL_HANDLE)
|
||||
return nullptr;
|
||||
return vk::raii::Queue(device, family_index, index);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
wivrn::wivrn_vk_bundle::wivrn_vk_bundle(vk_bundle & vk, std::span<const char *> requested_instance_extensions, std::span<const char *> requested_device_extensions) :
|
||||
vk(vk),
|
||||
instance(vk_ctx, vk.instance),
|
||||
@@ -35,7 +45,14 @@ wivrn::wivrn_vk_bundle::wivrn_vk_bundle(vk_bundle & vk, std::span<const char *>
|
||||
.vulkanApiVersion = VK_MAKE_VERSION(1, 3, 0), // FIXME: sync with wivrn_session.cpp
|
||||
}),
|
||||
queue(device, vk.queue_family_index, vk.queue_index),
|
||||
queue_family_index(vk.queue_family_index)
|
||||
queue_family_index(vk.queue_family_index),
|
||||
#ifdef VK_KHR_video_encode_queue
|
||||
encode_queue(make_queue(device, vk.encode_queue, vk.encode_queue_family_index, vk.encode_queue_index)),
|
||||
encode_queue_family_index(vk.encode_queue_family_index)
|
||||
#else
|
||||
encode_queue(nullptr),
|
||||
encode_queue_family_index(vk::QueueFamilyIgnored)
|
||||
#endif
|
||||
{
|
||||
// This is manually synced with monado code
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ struct wivrn_vk_bundle
|
||||
vk::raii::Queue queue;
|
||||
uint32_t queue_family_index;
|
||||
|
||||
vk::raii::Queue encode_queue;
|
||||
uint32_t encode_queue_family_index;
|
||||
|
||||
std::vector<const char *> instance_extensions;
|
||||
std::vector<const char *> device_extensions;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user