WIP: fixed encoder layout

This commit is contained in:
Patrick Nicolas
2025-12-10 14:00:47 +01:00
parent bf811ac65d
commit 5c50b9a1ad
53 changed files with 503 additions and 1359 deletions
+35 -126
View File
@@ -45,24 +45,22 @@ namespace wivrn
// TODO: size independent bitrate
static const uint64_t default_bitrate = 50'000'000;
// subsampling is also in monado's distortion shader
static const uint8_t passthrough_subsampling = 2;
static const double passthrough_bitrate_factor = 0.05;
#define WIVRN_SPLIT_ENCODERS 1
static bool is_nvidia(vk::PhysicalDevice physical_device)
{
auto props = physical_device.getProperties();
return props.vendorID == 0x10DE;
}
static void split_bitrate(std::vector<wivrn::encoder_settings> & encoders, uint64_t bitrate)
static void split_bitrate(std::array<wivrn::encoder_settings, 3> & encoders, uint64_t bitrate)
{
double total_weight = 0;
for (auto & encoder: encoders)
for (auto [i, encoder]: std::ranges::enumerate_view(encoders))
{
double w = encoder.width * encoder.height;
if (i == 2)
w *= passthrough_bitrate_factor;
switch (encoder.codec)
{
case wivrn::h264:
@@ -84,7 +82,7 @@ static void split_bitrate(std::vector<wivrn::encoder_settings> & encoders, uint6
}
}
void print_encoders(const std::vector<wivrn::encoder_settings> & encoders)
void print_encoders(const std::array<wivrn::encoder_settings, 3> & encoders)
{
int group = -1;
std::stringstream str;
@@ -99,8 +97,7 @@ void print_encoders(const std::vector<wivrn::encoder_settings> & encoders)
else
str << "\n";
str << "\n\t\t" << encoder.encoder_name << " (" << magic_enum::enum_name(encoder.codec) << " " << encoder.bit_depth << "-bit)"
<< "\n\t\tchannels: " << magic_enum::enum_name(encoder.channels)
<< "\n\t\tsize: " << encoder.width << "x" << encoder.height << " offset: " << encoder.offset_x << "x" << encoder.offset_y
<< "\n\t\tsize: " << encoder.width << "x" << encoder.height
<< "\n\t\tbitrate: " << encoder.bitrate / 1'000'000 << "Mbit/s";
}
U_LOG_I("%s", str.str().c_str());
@@ -148,16 +145,13 @@ static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & b
{
video_encoder_ffmpeg::mute_logs mute;
encoder_settings s{
{
.width = 800,
.height = 600,
.video_width = 800,
.video_height = 600,
},
"vaapi",
default_bitrate,
.width = 800,
.height = 600,
.fps = 60,
.encoder_name = "vaapi",
.bitrate = default_bitrate,
.bit_depth = bit_depth,
};
s.bit_depth = bit_depth;
for (auto codec: codecs)
{
@@ -177,7 +171,7 @@ static std::optional<wivrn::video_codec> filter_codecs_vaapi(wivrn_vk_bundle & b
try
{
s.codec = codec;
video_encoder_va test(bundle, s, 60, 0);
video_encoder_va test(bundle, s, 0);
return codec;
}
catch (...)
@@ -195,20 +189,17 @@ static bool probe_nvenc(wivrn_vk_bundle & bundle, int bit_depth)
{
static bool res = [&]() {
encoder_settings s{
{
.width = 800,
.height = 608,
.video_width = 800,
.video_height = 608,
.codec = h264,
},
encoder_nvenc,
default_bitrate,
.width = 800,
.height = 608,
.codec = h264,
.fps = 60,
.encoder_name = encoder_nvenc,
.bitrate = default_bitrate,
.bit_depth = bit_depth,
};
s.bit_depth = bit_depth;
try
{
video_encoder_nvenc test(bundle, s, 60, 0);
video_encoder_nvenc test(bundle, s, 0);
return true;
}
catch (std::exception & e)
@@ -308,44 +299,11 @@ static void fill_defaults(wivrn_vk_bundle & bundle, const std::vector<wivrn::vid
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, int bit_depth)
static std::array<configuration::encoder, 3> 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, bit_depth);
#ifdef WIVRN_SPLIT_ENCODERS
if (base.name != encoder_x264)
{
/* Split in 2 parts:
* +--------+--------+
* | | |
* | left | right |
* | eye | eye |
* | | |
* +--------+--------+
* Encoded sequentially, so the first eye is ready and transmitted earlier.
* Decoder can start work as fast as possible, reducing idle time.
* Splitting on eye boundaries allows for fast-path on headset.
*
*/
return {
{
.name = base.name,
.width = 0.5,
.group = 0,
.codec = base.codec,
},
{
.name = base.name,
.width = 0.5,
.offset_x = 0.5,
.group = 0,
.codec = base.codec,
},
};
}
#endif
return {base};
return {base, base, base};
}
static uint16_t align(uint16_t value, uint16_t alignment)
@@ -353,7 +311,7 @@ static uint16_t align(uint16_t value, uint16_t alignment)
return ((value + alignment - 1) / alignment) * 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)
std::array<encoder_settings, 3> get_encoder_settings(wivrn_vk_bundle & bundle, uint32_t & width, uint32_t & height, const from_headset::headset_info_packet & info)
{
configuration config;
@@ -362,42 +320,27 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
if (config.encoders.empty())
config.encoders = get_encoder_default_settings(bundle, info.supported_codecs, config.bit_depth);
if (not config.encoder_passthrough)
config.encoder_passthrough = config.encoders.front();
config.encoder_passthrough->width = 1;
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, config.bit_depth);
uint64_t bitrate = config.bitrate.value_or(default_bitrate);
std::array<double, 2> default_scale;
default_scale.fill(info.eye_gaze ? 0.35 : 0.5);
auto scale = config.scale.value_or(default_scale);
check_scale(config.encoder_passthrough->name,
*config.encoder_passthrough->codec,
// passthrough stream is subsampled
width / passthrough_subsampling,
height / passthrough_subsampling,
scale);
for (auto & encoder: config.encoders)
{
fill_defaults(bundle, info.supported_codecs, encoder, config.bit_depth);
assert(encoder.codec);
check_scale(encoder.name,
*encoder.codec,
std::ceil(encoder.width.value_or(1) * width),
std::ceil(encoder.height.value_or(1) * height),
std::ceil(width),
std::ceil(height),
scale);
}
width = align(width * scale[0], 64);
height = align(height * scale[1], 64);
std::vector<wivrn::encoder_settings> res;
std::array<wivrn::encoder_settings, 3> res;
std::unordered_map<std::string, int> groups;
int next_group = 0;
for (const auto & encoder: config.encoders)
@@ -409,20 +352,15 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
}
}
for (const auto & encoder: config.encoders)
for (const auto & [i, encoder]: std::ranges::enumerate_view(config.encoders))
{
wivrn::encoder_settings settings{};
settings.channels = to_headset::video_stream_description::channels_t::colour;
settings.subsampling = 1;
settings.encoder_name = encoder.name;
settings.offset_x = align(std::ceil(encoder.offset_x.value_or(0) * width), 32);
settings.offset_y = align(std::ceil(encoder.offset_y.value_or(0) * height), 32);
settings.width = align(std::ceil(encoder.width.value_or(1) * width), 32);
settings.height = align(std::ceil(encoder.height.value_or(1) * height), 32);
settings.width = std::min<uint16_t>(settings.width, width - settings.offset_x);
settings.height = std::min<uint16_t>(settings.height, height - settings.offset_y);
settings.video_width = settings.width;
settings.video_height = settings.height;
settings.width = align(std::ceil(width), 32);
settings.height = align(std::ceil(height), 32);
// alpha is half resolution, but left and right side by side
if (i == 2)
settings.height /= 2;
settings.codec = *encoder.codec;
settings.bit_depth = config.bit_depth;
if (encoder.group)
@@ -436,40 +374,11 @@ std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle & bundle, uin
}
settings.options = encoder.options;
settings.device = encoder.device;
settings.fps = info.preferred_refresh_rate;
res.push_back(settings);
res[i] = settings;
}
split_bitrate(res, bitrate);
// passthrough encoder
{
const auto & encoder = *config.encoder_passthrough;
wivrn::encoder_settings settings{};
settings.channels = to_headset::video_stream_description::channels_t::alpha;
settings.subsampling = passthrough_subsampling;
settings.encoder_name = encoder.name;
settings.width = width / settings.subsampling;
settings.height = height / settings.subsampling;
assert(settings.width % 32 == 0);
assert(settings.height % 32 == 0);
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
{
auto [it, inserted] = groups.emplace(encoder.name, next_group);
settings.group = it->second;
if (inserted)
++next_group;
}
settings.options = encoder.options;
settings.device = encoder.device;
settings.bitrate = bitrate * passthrough_bitrate_factor;
res.push_back(settings);
}
return res;
}
} // namespace wivrn
+7 -4
View File
@@ -24,14 +24,17 @@
#include <map>
#include <optional>
#include <string>
#include <vector>
namespace wivrn
{
struct wivrn_vk_bundle;
struct encoder_settings : public to_headset::video_stream_description::item
struct encoder_settings
{
uint16_t width;
uint16_t height;
video_codec codec; // left, right, alpha
float fps;
// encoder identifier, such as nvenc, vaapi or x264
std::string encoder_name;
uint64_t bitrate; // bit/s
@@ -43,8 +46,8 @@ struct encoder_settings : public to_headset::video_stream_description::item
std::optional<std::string> device;
};
std::vector<encoder_settings> get_encoder_settings(wivrn_vk_bundle &, uint32_t & width, uint32_t & height, const from_headset::headset_info_packet & info);
std::array<encoder_settings, 3> get_encoder_settings(wivrn_vk_bundle &, uint32_t & width, uint32_t & height, const from_headset::headset_info_packet & info);
void print_encoders(const std::vector<wivrn::encoder_settings> & encoders);
void print_encoders(const std::array<wivrn::encoder_settings, 3> & encoders);
} // namespace wivrn
+2 -2
View File
@@ -37,8 +37,8 @@ public:
};
protected:
video_encoder_ffmpeg(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, double bitrate_multiplier) :
wivrn::video_encoder(stream_idx, channels, std::make_unique<default_idr_handler>(), bitrate_multiplier, true) {}
video_encoder_ffmpeg(uint8_t stream_idx, const encoder_settings & settings) :
wivrn::video_encoder(stream_idx, settings, std::make_unique<default_idr_handler>(), true) {}
virtual void push_frame(bool idr, uint8_t slot) = 0;
+12 -34
View File
@@ -157,10 +157,9 @@ vk::Format drm_to_vulkan_fmt(uint32_t drm_fourcc, int bit_depth)
} // namespace
video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
wivrn::encoder_settings & settings,
float fps,
const wivrn::encoder_settings & settings,
uint8_t stream_idx) :
video_encoder_ffmpeg(stream_idx, settings.channels, settings.bitrate_multiplier),
video_encoder_ffmpeg(stream_idx, settings),
synchronization2(vk.vk.features.synchronization_2)
{
auto drm_hw_ctx = make_drm_hw_ctx(vk.physical_device, settings.device);
@@ -173,9 +172,6 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
throw std::system_error(err, av_error_category(), "FFMPEG vaapi hardware context creation failed");
av_buffer_ptr vaapi_hw_ctx(tmp);
settings.video_width += settings.video_width % 2;
settings.video_height += settings.video_height % 2;
AVPixelFormat sw_format;
switch (settings.codec)
@@ -196,19 +192,9 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
break;
}
auto vaapi_frame_ctx = make_hwframe_ctx(vaapi_hw_ctx.get(), AV_PIX_FMT_VAAPI, sw_format, settings.video_width, settings.video_height);
auto vaapi_frame_ctx = make_hwframe_ctx(vaapi_hw_ctx.get(), AV_PIX_FMT_VAAPI, sw_format, extent.width, extent.height);
assert(av_pix_fmt_count_planes(sw_format) == 2);
rect = vk::Rect2D{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
}};
err = av_hwframe_ctx_create_derived(&tmp,
AV_PIX_FMT_DRM_PRIME,
drm_hw_ctx.get(),
@@ -257,11 +243,11 @@ video_encoder_va::video_encoder_va(wivrn_vk_bundle & vk,
av_dict_set(&opts, option.first.c_str(), option.second.c_str(), 0);
}
encoder_ctx->width = settings.video_width;
encoder_ctx->height = settings.video_height;
encoder_ctx->width = extent.width;
encoder_ctx->height = extent.height;
encoder_ctx->time_base = {std::chrono::steady_clock::duration::period::num,
std::chrono::steady_clock::duration::period::den};
encoder_ctx->framerate = AVRational{(int)fps, 1};
encoder_ctx->framerate = AVRational{(int)settings.fps * 1000, 1000};
encoder_ctx->sample_aspect_ratio = AVRational{1, 1};
encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
encoder_ctx->color_range = AVCOL_RANGE_JPEG;
@@ -466,20 +452,16 @@ std::pair<bool, vk::Semaphore> video_encoder_va::present_image(vk::Image y_cbcr,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.srcOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.layerCount = 1,
},
.extent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
}});
@@ -491,20 +473,16 @@ std::pair<bool, vk::Semaphore> video_encoder_va::present_image(vk::Image y_cbcr,
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.srcOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::eColor,
.layerCount = 1,
},
.extent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.width = extent.width / 2,
.height = extent.height / 2,
.depth = 1,
}});
+1 -2
View File
@@ -43,11 +43,10 @@ class video_encoder_va : public video_encoder_ffmpeg
};
av_buffer_ptr drm_frame_ctx;
std::array<in_t, num_slots> in;
vk::Rect2D rect;
bool synchronization2 = false;
public:
video_encoder_va(wivrn_vk_bundle &, wivrn::encoder_settings & settings, float fps, uint8_t stream_index);
video_encoder_va(wivrn_vk_bundle &, const wivrn::encoder_settings & settings, uint8_t stream_index);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+18 -23
View File
@@ -102,26 +102,21 @@ std::shared_ptr<video_encoder::sender> video_encoder::sender::get()
std::unique_ptr<video_encoder> video_encoder::create(
wivrn_vk_bundle & wivrn_vk,
encoder_settings & settings,
uint8_t stream_idx,
int input_width,
int input_height,
float fps)
const encoder_settings & settings,
uint8_t stream_idx)
{
using namespace std::string_literals;
std::unique_ptr<video_encoder> res;
settings.range = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
settings.color_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
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, stream_idx);
res = video_encoder_vulkan_h264::create(wivrn_vk, settings, stream_idx);
break;
case video_codec::h265:
res = video_encoder_vulkan_h265::create(wivrn_vk, settings, fps, stream_idx);
res = video_encoder_vulkan_h265::create(wivrn_vk, settings, stream_idx);
break;
case video_codec::av1:
throw std::runtime_error("av1 not supported for vulkan video encode");
@@ -135,7 +130,7 @@ std::unique_ptr<video_encoder> video_encoder::create(
if (settings.encoder_name == encoder_x264)
{
#if WIVRN_USE_X264
res = std::make_unique<video_encoder_x264>(wivrn_vk, settings, fps, stream_idx);
res = std::make_unique<video_encoder_x264>(wivrn_vk, settings, stream_idx);
#else
throw std::runtime_error("x264 encoder not enabled");
#endif
@@ -143,7 +138,7 @@ std::unique_ptr<video_encoder> video_encoder::create(
if (settings.encoder_name == encoder_nvenc)
{
#if WIVRN_USE_NVENC
res = std::make_unique<video_encoder_nvenc>(wivrn_vk, settings, fps, stream_idx);
res = std::make_unique<video_encoder_nvenc>(wivrn_vk, settings, stream_idx);
#else
throw std::runtime_error("nvenc support not enabled");
#endif
@@ -151,7 +146,7 @@ std::unique_ptr<video_encoder> video_encoder::create(
if (settings.encoder_name == encoder_vaapi)
{
#if WIVRN_USE_VAAPI
res = std::make_unique<video_encoder_va>(wivrn_vk, settings, fps, stream_idx);
res = std::make_unique<video_encoder_va>(wivrn_vk, settings, stream_idx);
#else
throw std::runtime_error("vaapi support not enabled");
#endif
@@ -159,7 +154,7 @@ std::unique_ptr<video_encoder> video_encoder::create(
if (settings.encoder_name == encoder_raw)
{
res = std::make_unique<video_encoder_raw>(wivrn_vk, settings, fps, stream_idx);
res = std::make_unique<video_encoder_raw>(wivrn_vk, settings, stream_idx);
}
if (not res)
@@ -190,12 +185,18 @@ std::unique_ptr<video_encoder> video_encoder::create(
return res;
}
video_encoder::video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, std::unique_ptr<idr_handler> idr, double bitrate_multiplier, bool async_send) :
video_encoder::video_encoder(uint8_t stream_idx,
const encoder_settings & settings,
std::unique_ptr<idr_handler> idr,
bool async_send) :
stream_idx(stream_idx),
channels(channels),
bitrate_multiplier(bitrate_multiplier),
bitrate_multiplier(settings.bitrate_multiplier),
shared_sender(async_send ? sender::get() : nullptr),
idr(std::move(idr))
idr(std::move(idr)),
extent{
.width = settings.width,
.height = settings.height,
}
{
assert(this->idr);
}
@@ -315,7 +316,6 @@ void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool co
ssize_t max_payload_size = cnx->has_stream() ? to_headset::video_stream_data_shard::max_payload_size : std::numeric_limits<uint32_t>::max();
shard.flags = to_headset::video_stream_data_shard::start_of_slice;
auto begin = data.begin();
auto end = data.end();
while (begin != end)
@@ -324,12 +324,8 @@ void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool co
auto next = std::min(end, begin + payload_size);
if (next == end)
{
shard.flags |= to_headset::video_stream_data_shard::end_of_slice;
if (end_of_frame)
{
shard.flags |= to_headset::video_stream_data_shard::end_of_frame;
shard.timing_info = timing_info;
}
}
shard.payload = {begin, next};
try
@@ -344,7 +340,6 @@ void video_encoder::SendData(std::span<uint8_t> data, bool end_of_frame, bool co
// Ignore network errors
}
++shard.shard_idx;
shard.flags = 0;
shard.view_info.reset();
begin = next;
}
+4 -7
View File
@@ -76,7 +76,6 @@ private:
public:
const uint8_t stream_idx;
const to_headset::video_stream_description::channels_t channels;
static const uint8_t num_slots = 2;
const double bitrate_multiplier;
@@ -108,17 +107,15 @@ protected:
std::atomic_uint32_t pending_bitrate;
std::atomic<float> pending_framerate;
std::unique_ptr<idr_handler> idr;
const vk::Extent2D extent;
public:
static std::unique_ptr<video_encoder> create(
wivrn_vk_bundle &,
encoder_settings & settings,
uint8_t stream_idx,
int input_width,
int input_height,
float fps);
const encoder_settings & settings,
uint8_t stream_idx);
video_encoder(uint8_t stream_idx, to_headset::video_stream_description::channels_t channels, std::unique_ptr<idr_handler>, double bitrate_multiplier, bool async_send);
video_encoder(uint8_t stream_idx, const encoder_settings & settings, std::unique_ptr<idr_handler>, bool async_send);
virtual ~video_encoder();
// return value: true if image should be transitioned to queue and layout for vulkan video encode
+34 -49
View File
@@ -150,30 +150,19 @@ void video_encoder_nvenc::set_init_params_fps(float framerate)
video_encoder_nvenc::video_encoder_nvenc(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings.channels, std::make_unique<default_idr_handler>(), settings.bitrate_multiplier, true),
video_encoder(stream_idx, settings, std::make_unique<default_idr_handler>(), true),
vk(vk),
shared_state(video_encoder_nvenc_shared_state::get()),
fps(fps),
fps(settings.fps),
bitrate(settings.bitrate)
{
if (settings.bit_depth != 8 && settings.bit_depth != 10)
throw std::runtime_error("nvenc encoder only supports 8-bit and 10-bit encoding");
assert(settings.width % 32 == 0);
assert(settings.height % 32 == 0);
rect = vk::Rect2D{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
},
};
assert(extent.width % 32 == 0);
assert(extent.height % 32 == 0);
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS session_params = {
.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER,
@@ -284,10 +273,10 @@ video_encoder_nvenc::video_encoder_nvenc(
.version = NV_ENC_INITIALIZE_PARAMS_VER,
.encodeGUID = encodeGUID,
.presetGUID = presetGUID,
.encodeWidth = settings.video_width,
.encodeHeight = settings.video_height,
.darWidth = settings.video_width,
.darHeight = settings.video_height,
.encodeWidth = extent.width,
.encodeHeight = extent.height,
.darWidth = extent.width,
.darHeight = extent.height,
.enableEncodeAsync = 0,
.enablePTD = 1,
.encodeConfig = &config,
@@ -303,7 +292,7 @@ video_encoder_nvenc::video_encoder_nvenc(
NVENC_CHECK(shared_state->fn.nvEncCreateBitstreamBuffer(session_handle, &out_buf_params));
outputBuffer = out_buf_params.bitstreamBuffer;
vk::DeviceSize buffer_size = rect.extent.width * settings.video_height * bytesPerPixel * 3 / 2;
vk::DeviceSize buffer_size = extent.width * extent.height * bytesPerPixel * 3 / 2;
vk::StructureChain buffer_create_info{
vk::BufferCreateInfo{
@@ -365,9 +354,9 @@ video_encoder_nvenc::video_encoder_nvenc(
NV_ENC_REGISTER_RESOURCE resource_params{
.version = NV_ENC_REGISTER_RESOURCE_VER,
.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR,
.width = settings.video_width,
.height = settings.video_height,
.pitch = rect.extent.width * bytesPerPixel,
.width = extent.width,
.height = extent.height,
.pitch = extent.width * bytesPerPixel,
.resourceToRegister = (void *)frame,
.bufferFormat = (bitDepth == NV_ENC_BIT_DEPTH_10 ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT : NV_ENC_BUFFER_FORMAT_NV12),
.bufferUsage = NV_ENC_INPUT_IMAGE,
@@ -392,35 +381,31 @@ std::pair<bool, vk::Semaphore> video_encoder_nvenc::present_image(vk::Image y_cb
*in[slot].yuv,
std::array{
vk::BufferImageCopy{
.bufferRowLength = rect.extent.width,
.bufferRowLength = extent.width,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
},
.imageExtent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
}},
vk::BufferImageCopy{.bufferOffset = rect.extent.width * rect.extent.height * bytesPerPixel, .bufferRowLength = uint32_t(rect.extent.width / 2), .imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
},
.imageExtent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.depth = 1,
}}});
vk::BufferImageCopy{
.bufferOffset = extent.width * extent.height * bytesPerPixel,
.bufferRowLength = uint32_t(extent.width / 2),
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageExtent = {
.width = extent.width / 2,
.height = extent.height / 2,
.depth = 1,
},
}});
return {false, nullptr};
}
@@ -479,9 +464,9 @@ std::optional<video_encoder::data> video_encoder_nvenc::encode(uint8_t slot, uin
NV_ENC_PIC_PARAMS frame_params{
.version = NV_ENC_PIC_PARAMS_VER,
.inputWidth = rect.extent.width,
.inputHeight = rect.extent.height,
.inputPitch = rect.extent.width,
.inputWidth = extent.width,
.inputHeight = extent.height,
.inputPitch = extent.width,
.encodePicFlags = 0,
.frameIdx = 0,
.inputTimeStamp = 0,
+1 -3
View File
@@ -34,8 +34,6 @@ class video_encoder_nvenc : public video_encoder
{
private:
wivrn_vk_bundle & vk;
// relevant part of the input image to encode
vk::Rect2D rect;
std::shared_ptr<video_encoder_nvenc_shared_state> shared_state;
@@ -60,7 +58,7 @@ private:
void set_init_params_fps(float framerate);
public:
video_encoder_nvenc(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx);
video_encoder_nvenc(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
~video_encoder_nvenc();
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+13 -38
View File
@@ -37,33 +37,16 @@ public:
wivrn::video_encoder_raw::video_encoder_raw(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings.channels, std::make_unique<dummy_idr_handler>(), settings.bitrate_multiplier, true)
video_encoder(stream_idx, settings, std::make_unique<dummy_idr_handler>(), true)
{
if (settings.bit_depth != 8)
throw std::runtime_error("Raw encoding is only supported for 8 bit");
rect = vk::Rect2D{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
},
};
vk::DeviceSize buffer_size = settings.width * settings.height;
switch (settings.channels)
{
case to_headset::video_stream_description::channels_t::colour:
buffer_size += buffer_size / 2;
break;
case to_headset::video_stream_description::channels_t::alpha:
break;
}
vk::DeviceSize buffer_size = extent.width * extent.height;
if (stream_idx < 2)
buffer_size += buffer_size / 2;
for (auto & slot: buffers)
{
slot = buffer_allocation(
@@ -86,33 +69,25 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_raw::present_image(vk::Image
vk::BufferImageCopy{
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
},
.imageExtent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
},
},
vk::BufferImageCopy{
.bufferOffset = rect.extent.width * rect.extent.height,
.bufferOffset = extent.width * extent.height,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
},
.imageExtent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.width = extent.width / 2,
.height = extent.height / 2,
.depth = 1,
},
},
@@ -122,7 +97,7 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_raw::present_image(vk::Image
vk::ImageLayout::eTransferSrcOptimal,
buffers[slot],
std::span(regions.data(),
channels == to_headset::video_stream_description::channels_t::colour ? 2 : 1));
stream_idx == 2 ? 2 : 1));
return {false, nullptr};
}
+1 -2
View File
@@ -29,10 +29,9 @@ namespace wivrn
class video_encoder_raw : public video_encoder
{
std::array<buffer_allocation, num_slots> buffers;
vk::Rect2D rect;
public:
video_encoder_raw(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx);
video_encoder_raw(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;
+16 -29
View File
@@ -157,16 +157,13 @@ vk::VideoFormatPropertiesKHR wivrn::video_encoder_vulkan::select_video_format(
wivrn::video_encoder_vulkan::video_encoder_vulkan(
wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & in_encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings) :
video_encoder(stream_idx, settings.channels, std::make_unique<dpb_state>(), settings.bitrate_multiplier, true),
video_encoder(stream_idx, settings, std::make_unique<dpb_state>(), true),
vk(vk),
encode_caps(patch_capabilities(in_encode_caps)),
rect(rect),
num_dpb_slots(std::min(video_caps.maxDpbSlots, 16u))
{
// Initialize Rate control
@@ -192,7 +189,7 @@ wivrn::video_encoder_vulkan::video_encoder_vulkan(
rate_control_layer = vk::VideoEncodeRateControlLayerInfoKHR{
.averageBitrate = std::min(settings.bitrate, encode_caps.maxBitrate),
.maxBitrate = std::min(2 * settings.bitrate, encode_caps.maxBitrate),
.frameRateNumerator = uint32_t(fps * 1'000'000),
.frameRateNumerator = uint32_t(settings.fps * 1'000'000),
.frameRateDenominator = 1'000'000,
};
rate_control = vk::VideoEncodeRateControlInfoKHR{
@@ -224,8 +221,8 @@ wivrn::video_encoder_vulkan::video_encoder_vulkan(
std::max(video_caps.pictureAccessGranularity.height, encode_caps.encodeInputPictureGranularity.height);
aligned_extent = vk::Extent3D{
.width = align(rect.extent.width, std::max(cb_min_size, granWidth)),
.height = align(rect.extent.height, std::max(cb_min_size, granHeight)),
.width = align(extent.width, std::max(cb_min_size, granWidth)),
.height = align(extent.height, std::max(cb_min_size, granHeight)),
.depth = 1,
};
}
@@ -291,7 +288,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
for (auto & item: slot_data)
{
// very conservative bound
size_t output_buffer_size = rect.extent.width * rect.extent.height * 3;
size_t output_buffer_size = extent.width * extent.height * 3;
output_buffer_size = align(output_buffer_size, video_caps.minBitstreamBufferSizeAlignment);
item.output_buffer = buffer_allocation(
vk.device,
@@ -371,11 +368,11 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
.subresourceRange = {.aspectMask = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1},
};
if (rect.offset != vk::Offset2D{0, 0} or not vk.vk.features.video_maintenance_1)
if (not vk.vk.features.video_maintenance_1)
{
image_view_template.subresourceRange.baseArrayLayer = 0;
for (size_t i = 0; i < num_slots; ++i)
@@ -386,8 +383,8 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
.imageType = vk::ImageType::e2D,
.format = picture_format.format,
.extent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
},
.mipLevels = 1,
@@ -444,7 +441,7 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
dpb.items.push_back({
.image_view = std::move(v),
.resource = {
.codedExtent = rect.extent,
.codedExtent = extent,
.imageViewBinding = v1,
},
.info = dpb.infos[i],
@@ -650,44 +647,34 @@ std::pair<bool, vk::Semaphore> wivrn::video_encoder_vulkan::present_image(vk::Im
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.srcOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
.z = 0,
},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = 0,
.layerCount = 1,
},
.extent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
},
},
vk::ImageCopy{
.srcSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.srcOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
.z = 0,
},
.dstSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = 0,
.layerCount = 1,
},
.extent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.width = extent.width / 2,
.height = extent.height / 2,
.depth = 1,
},
},
-3
View File
@@ -66,7 +66,6 @@ class video_encoder_vulkan : public video_encoder
const vk::PhysicalDeviceVideoFormatInfoKHR &);
bool session_initialized = false;
const vk::Rect2D rect;
protected:
const uint8_t num_dpb_slots;
@@ -75,10 +74,8 @@ protected:
std::optional<vk::VideoEncodeRateControlInfoKHR> rate_control;
video_encoder_vulkan(wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings);
+9 -23
View File
@@ -71,13 +71,11 @@ static StdVideoH264LevelIdc compute_level(const StdVideoH264SequenceParameterSet
wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(
wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings) :
video_encoder_vulkan(vk, rect, video_caps, encode_caps, fps, stream_idx, settings),
video_encoder_vulkan(vk, video_caps, encode_caps, stream_idx, settings),
sps{
.flags =
{
@@ -94,7 +92,7 @@ wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(
.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,
.frame_cropping_flag = (extent.width % 16) || (extent.height) % 16,
.seq_scaling_matrix_present_flag = 0,
.vui_parameters_present_flag = 0,
},
@@ -112,12 +110,12 @@ wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(
.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,
.pic_width_in_mbs_minus1 = (extent.width - 1) / 16,
.pic_height_in_map_units_minus1 = (extent.height - 1) / 16,
.frame_crop_left_offset = 0,
.frame_crop_right_offset = (rect.extent.width % 16) / 2,
.frame_crop_right_offset = (extent.width % 16) / 2,
.frame_crop_top_offset = 0,
.frame_crop_bottom_offset = (rect.extent.height % 16) / 2,
.frame_crop_bottom_offset = (extent.height % 16) / 2,
.reserved2 = 0,
.pOffsetForRefFrame = nullptr,
.pScalingLists = nullptr,
@@ -147,7 +145,7 @@ wivrn::video_encoder_vulkan_h264::video_encoder_vulkan_h264(
.pScalingLists = nullptr,
}
{
sps.level_idc = compute_level(sps, fps, num_dpb_slots, settings.bitrate);
sps.level_idc = compute_level(sps, settings.fps, num_dpb_slots, settings.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");
@@ -213,27 +211,15 @@ auto get_video_caps(vk::raii::PhysicalDevice & phys_dev)
std::unique_ptr<wivrn::video_encoder_vulkan_h264> wivrn::video_encoder_vulkan_h264::create(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx)
{
vk::Rect2D rect{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
},
};
if (settings.bit_depth != 8)
throw std::runtime_error("h264 codec only supports 8-bit encoding");
auto [video_caps, encode_caps, encode_h264_caps, video_profile_info] = get_video_caps(vk.physical_device);
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, video_caps, encode_caps, stream_idx, settings));
vk::VideoEncodeH264SessionParametersAddInfoKHR h264_add_info{};
h264_add_info.setStdSPSs(self->sps);
+1 -4
View File
@@ -49,10 +49,8 @@ class video_encoder_vulkan_h264 : public video_encoder_vulkan
vk::VideoEncodeH264RateControlLayerInfoKHR rate_control_layer_h264;
video_encoder_vulkan_h264(wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings);
@@ -66,8 +64,7 @@ protected:
public:
static std::unique_ptr<video_encoder_vulkan_h264> create(wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx);
std::vector<uint8_t> get_sps_pps();
+6 -14
View File
@@ -61,13 +61,11 @@ static uint32_t find_msb(uint32_t v)
wivrn::video_encoder_vulkan_h265::video_encoder_vulkan_h265(
wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings) :
video_encoder_vulkan(vk, rect, video_caps, encode_caps, fps, stream_idx, settings),
video_encoder_vulkan(vk, video_caps, encode_caps, stream_idx, settings),
vui{
.flags = {
.aspect_ratio_info_present_flag = 1,
@@ -97,7 +95,7 @@ wivrn::video_encoder_vulkan_h265::video_encoder_vulkan_h265(
.general_frame_only_constraint_flag = 1,
},
.general_profile_idc = (settings.bit_depth == 10) ? STD_VIDEO_H265_PROFILE_IDC_MAIN_10 : STD_VIDEO_H265_PROFILE_IDC_MAIN,
.general_level_idc = choose_level(rect.extent.width, rect.extent.height, fps)},
.general_level_idc = choose_level(extent.width, extent.height, settings.fps)},
vps{
.flags = {
.vps_temporal_id_nesting_flag = 1, // radv breaks without
@@ -150,9 +148,9 @@ wivrn::video_encoder_vulkan_h265::video_encoder_vulkan_h265(
.motion_vector_resolution_control_idc = 0,
.sps_num_palette_predictor_initializers_minus1 = 0,
.conf_win_left_offset = 0u,
.conf_win_right_offset = (aligned_extent.width - rect.extent.width) >> 1, // 4:2:0
.conf_win_right_offset = (aligned_extent.width - extent.width) >> 1, // 4:2:0
.conf_win_top_offset = 0u,
.conf_win_bottom_offset = (aligned_extent.height - rect.extent.height) >> 1, // 4:2:0
.conf_win_bottom_offset = (aligned_extent.height - extent.height) >> 1, // 4:2:0
.pProfileTierLevel = &ptl,
.pDecPicBufMgr = &dpb,
.pScalingLists = nullptr,
@@ -272,19 +270,13 @@ static auto get_video_caps(vk::raii::PhysicalDevice & phys_dev, int bit_depth)
std::unique_ptr<wivrn::video_encoder_vulkan_h265> wivrn::video_encoder_vulkan_h265::create(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx)
{
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, video_profile_info] = get_video_caps(vk.physical_device, settings.bit_depth);
std::unique_ptr<video_encoder_vulkan_h265> self(
new video_encoder_vulkan_h265(vk, rect, video_caps, encode_caps, fps, stream_idx, settings));
new video_encoder_vulkan_h265(vk, video_caps, encode_caps, stream_idx, settings));
vk::VideoEncodeH265SessionParametersAddInfoKHR add_info{};
add_info.setStdVPSs(self->vps);
+1 -4
View File
@@ -60,10 +60,8 @@ class video_encoder_vulkan_h265 : public video_encoder_vulkan
vk::VideoEncodeH265RateControlLayerInfoKHR rc_layer_h265{};
video_encoder_vulkan_h265(wivrn_vk_bundle & vk,
vk::Rect2D rect,
const vk::VideoCapabilitiesKHR & video_caps,
const vk::VideoEncodeCapabilitiesKHR & encode_caps,
float fps,
uint8_t stream_idx,
const encoder_settings & settings);
@@ -77,8 +75,7 @@ protected:
public:
static std::unique_ptr<video_encoder_vulkan_h265> create(wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx);
std::vector<uint8_t> get_vps_sps_pps();
+20 -46
View File
@@ -88,47 +88,29 @@ void video_encoder_x264::InsertInPendingNal(pending_nal && nal)
video_encoder_x264::video_encoder_x264(
wivrn_vk_bundle & vk,
encoder_settings & settings,
float fps,
const encoder_settings & settings,
uint8_t stream_idx) :
video_encoder(stream_idx, settings.channels, std::make_unique<default_idr_handler>(), settings.bitrate_multiplier, false)
video_encoder(stream_idx, settings, std::make_unique<default_idr_handler>(), 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");
settings.codec = h264;
}
// encoder requires width and height to be even
settings.video_width += settings.video_width % 2;
settings.video_height += settings.video_height % 2;
chroma_width = settings.video_width / 2;
chroma_width = extent.width / 2;
// FIXME: enforce even values
rect = vk::Rect2D{
.offset = {
.x = settings.offset_x,
.y = settings.offset_y,
},
.extent = {
.width = settings.width,
.height = settings.height,
},
};
num_mb = ((settings.video_width + 15) / 16) * ((settings.video_height + 15) / 16);
num_mb = ((extent.width + 15) / 16) * ((extent.height + 15) / 16);
x264_param_default_preset(&param, "ultrafast", "zerolatency");
param.nalu_process = &ProcessCb;
// param.i_slice_max_size = 1300;
param.i_slice_count = 32;
param.i_width = settings.video_width;
param.i_height = settings.video_height;
param.i_width = extent.width;
param.i_height = extent.height;
param.i_log_level = X264_LOG_WARNING;
param.i_fps_num = fps * 1'000'000;
param.i_fps_num = settings.fps * 1'000'000;
param.i_fps_den = 1'000'000;
param.b_repeat_headers = 1;
param.b_aud = 0;
@@ -140,12 +122,12 @@ video_encoder_x264::video_encoder_x264(
param.vui.i_colmatrix = 1; // BT.709
param.vui.i_transfer = 13; // sRGB
param.vui.i_sar_width = settings.width;
param.vui.i_sar_height = settings.height;
param.vui.i_sar_width = extent.width;
param.vui.i_sar_height = extent.height;
param.rc.i_rc_method = X264_RC_ABR;
param.rc.i_bitrate = settings.bitrate / 1000; // x264 uses kbit/s
param.rc.i_vbv_max_bitrate = param.rc.i_bitrate;
param.rc.i_vbv_buffer_size = param.rc.i_bitrate / fps * 1.1;
param.rc.i_vbv_buffer_size = param.rc.i_bitrate / settings.fps * 1.1;
x264_param_apply_profile(&param, "main");
@@ -162,7 +144,7 @@ video_encoder_x264::video_encoder_x264(
i.luma = buffer_allocation(
vk.device,
{
.size = vk::DeviceSize(settings.video_width * settings.video_height),
.size = vk::DeviceSize(extent.width * extent.height),
.usage = vk::BufferUsageFlagBits::eTransferDst,
},
{
@@ -172,7 +154,7 @@ video_encoder_x264::video_encoder_x264(
"x264 luma buffer");
i.chroma = buffer_allocation(
vk.device, {
.size = vk::DeviceSize(settings.video_width * settings.video_height / 2),
.size = vk::DeviceSize(extent.width * extent.height / 2),
.usage = vk::BufferUsageFlagBits::eTransferDst,
},
{
@@ -187,9 +169,9 @@ video_encoder_x264::video_encoder_x264(
pic.img.i_csp = X264_CSP_NV12;
pic.img.i_plane = 2;
pic.img.i_stride[0] = settings.video_width;
pic.img.i_stride[0] = extent.width;
pic.img.plane[0] = (uint8_t *)i.luma.map();
pic.img.i_stride[1] = settings.video_width;
pic.img.i_stride[1] = extent.width;
pic.img.plane[1] = (uint8_t *)i.chroma.map();
}
}
@@ -204,16 +186,12 @@ std::pair<bool, vk::Semaphore> video_encoder_x264::present_image(vk::Image y_cbc
.bufferRowLength = chroma_width * 2,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane0,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x,
.y = rect.offset.y,
},
.imageExtent = {
.width = rect.extent.width,
.height = rect.extent.height,
.width = extent.width,
.height = extent.height,
.depth = 1,
}});
cmd_buf.copyImageToBuffer(
@@ -224,16 +202,12 @@ std::pair<bool, vk::Semaphore> video_encoder_x264::present_image(vk::Image y_cbc
.bufferRowLength = chroma_width,
.imageSubresource = {
.aspectMask = vk::ImageAspectFlagBits::ePlane1,
.baseArrayLayer = uint32_t(channels),
.baseArrayLayer = stream_idx,
.layerCount = 1,
},
.imageOffset = {
.x = rect.offset.x / 2,
.y = rect.offset.y / 2,
},
.imageExtent = {
.width = rect.extent.width / 2,
.height = rect.extent.height / 2,
.width = extent.width / 2,
.height = extent.height / 2,
.depth = 1,
}});
return {false, nullptr};
+1 -3
View File
@@ -47,8 +47,6 @@ class video_encoder_x264 : public video_encoder
std::array<in_t, num_slots> in;
uint32_t chroma_width;
vk::Rect2D rect;
struct pending_nal
{
int first_mb;
@@ -62,7 +60,7 @@ class video_encoder_x264 : public video_encoder
std::list<pending_nal> pending_nals;
public:
video_encoder_x264(wivrn_vk_bundle & vk, encoder_settings & settings, float fps, uint8_t stream_idx);
video_encoder_x264(wivrn_vk_bundle & vk, const encoder_settings & settings, uint8_t stream_idx);
std::pair<bool, vk::Semaphore> present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot, uint64_t frame_index) override;