Try to only use acknowledged frames as reference
This commit is contained in:
@@ -218,7 +218,13 @@ VideoEncoder::~VideoEncoder()
|
||||
shared_sender->wait_idle(this);
|
||||
}
|
||||
|
||||
void VideoEncoder::SyncNeeded()
|
||||
void VideoEncoder::on_feedback(const from_headset::feedback & feedback)
|
||||
{
|
||||
if (not feedback.sent_to_decoder)
|
||||
sync_needed = true;
|
||||
}
|
||||
|
||||
void VideoEncoder::reset()
|
||||
{
|
||||
sync_needed = true;
|
||||
}
|
||||
@@ -233,9 +239,9 @@ void VideoEncoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd
|
||||
next_present = (next_present + 1) % num_slots;
|
||||
}
|
||||
|
||||
void VideoEncoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & video_cmd_buf, vk::Fence fence)
|
||||
void VideoEncoder::present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & video_cmd_buf, vk::Fence fence, uint64_t frame_index)
|
||||
{
|
||||
present_image(sync_needed.load(), y_cbcr, video_cmd_buf, fence, next_present);
|
||||
present_image(y_cbcr, video_cmd_buf, fence, next_present, frame_index);
|
||||
}
|
||||
|
||||
void VideoEncoder::Encode(wivrn_session & cnx,
|
||||
|
||||
@@ -115,10 +115,10 @@ public:
|
||||
|
||||
void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf);
|
||||
// for vulkan video (command buffer is on a video queue)
|
||||
void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence fence);
|
||||
void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence fence, uint64_t frame_index);
|
||||
|
||||
// The other end lost a frame and needs to resynchronize
|
||||
void SyncNeeded();
|
||||
virtual void on_feedback(const from_headset::feedback &);
|
||||
virtual void reset();
|
||||
|
||||
void Encode(wivrn_session & cnx,
|
||||
const to_headset::video_stream_data_shard::view_info_t & view_info,
|
||||
@@ -128,7 +128,7 @@ protected:
|
||||
// called on present to submit command buffers for the image.
|
||||
virtual void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, uint8_t slot) {};
|
||||
// for vulkan video (command buffer is on a video queue)
|
||||
virtual void present_image(bool idr, vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot) {};
|
||||
virtual void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot, uint64_t frame_index) {};
|
||||
// called when command buffer finished executing
|
||||
virtual std::optional<data> encode(bool idr, std::chrono::steady_clock::time_point target_timestamp, uint8_t slot) = 0;
|
||||
|
||||
|
||||
@@ -231,6 +231,20 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
|
||||
.layerCount = 1},
|
||||
};
|
||||
|
||||
// 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_info.push_back({
|
||||
.pNext = std_slots[i],
|
||||
.slotIndex = -1,
|
||||
.pPictureResource = nullptr,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DPB image views
|
||||
{
|
||||
vk::ImageViewCreateInfo img_view_create_info{
|
||||
@@ -246,33 +260,22 @@ void wivrn::video_encoder_vulkan::init(const vk::VideoCapabilitiesKHR & video_ca
|
||||
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.push_back(
|
||||
{
|
||||
.image_view = vk.device.createImageView(img_view_create_info),
|
||||
.info = dpb_info[i],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DPB video picture resource info
|
||||
{
|
||||
for (auto & dpb_image_view: dpb_image_views)
|
||||
for (auto & item: dpb)
|
||||
{
|
||||
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,
|
||||
});
|
||||
item.resource = vk::VideoPictureResourceInfoKHR{
|
||||
.codedExtent = rect.extent,
|
||||
.imageViewBinding = *item.image_view,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +344,7 @@ std::optional<wivrn::VideoEncoder::data> wivrn::video_encoder_vulkan::encode(boo
|
||||
};
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk::raii::CommandBuffer & command_buffer, vk::Fence fence, uint8_t encode_slot)
|
||||
void wivrn::video_encoder_vulkan::present_image(vk::Image src_yuv, vk::raii::CommandBuffer & command_buffer, vk::Fence fence, uint8_t encode_slot, uint64_t frame_index)
|
||||
{
|
||||
fences[encode_slot] = fence;
|
||||
auto it = image_views.find(src_yuv);
|
||||
@@ -356,30 +359,63 @@ void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk:
|
||||
|
||||
command_buffer.resetQueryPool(*query_pool, 0, 1);
|
||||
|
||||
if (idr)
|
||||
auto slot = std::ranges::min_element(
|
||||
dpb,
|
||||
[](const auto & a, const auto & b) {
|
||||
return a.frame_index + 1 < b.frame_index + 1;
|
||||
});
|
||||
size_t slot_index = std::distance(dpb.begin(), slot);
|
||||
slot->info.slotIndex = -1;
|
||||
|
||||
auto last_ack = this->last_ack.load();
|
||||
dpb_item * ref_slot = nullptr;
|
||||
for (size_t i = 0; i < dpb.size(); ++i)
|
||||
{
|
||||
frame_num = 0;
|
||||
for (auto & slot: dpb_slots)
|
||||
if (dpb[i].frame_index == last_ack and dpb[i].info.slotIndex != -1)
|
||||
{
|
||||
slot.slotIndex = -1;
|
||||
slot.pPictureResource = nullptr;
|
||||
ref_slot = &dpb[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const auto slot = frame_num % dpb_slots.size();
|
||||
// Avoid sending many IDR in a row
|
||||
if (frame_num < 100 and not ref_slot)
|
||||
{
|
||||
for (size_t i = 0; i < dpb.size(); ++i)
|
||||
{
|
||||
if (dpb[i].info.slotIndex != -1)
|
||||
{
|
||||
if (not ref_slot or ref_slot->frame_index < dpb[i].frame_index)
|
||||
{
|
||||
ref_slot = &dpb[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dpb_slots[slot].slotIndex = -1;
|
||||
dpb_slots[slot].pPictureResource = &dpb_resource[slot];
|
||||
if (not ref_slot)
|
||||
{
|
||||
frame_num = 0;
|
||||
for (auto & slot: dpb)
|
||||
{
|
||||
slot.info.slotIndex = -1;
|
||||
slot.info.pPictureResource = nullptr;
|
||||
slot.frame_index = -1;
|
||||
}
|
||||
}
|
||||
slot->frame_index = frame_index;
|
||||
slot->info.pPictureResource = &slot->resource;
|
||||
|
||||
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(),
|
||||
.referenceSlotCount = uint32_t(dpb_info.size()),
|
||||
.pReferenceSlots = dpb_info.data(),
|
||||
});
|
||||
|
||||
dpb_slots[slot].slotIndex = slot;
|
||||
slot->info.slotIndex = slot_index;
|
||||
|
||||
if (not session_initialized)
|
||||
{
|
||||
@@ -407,7 +443,7 @@ void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk:
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = uint32_t(dpb_slots.size())},
|
||||
.layerCount = uint32_t(dpb.size())},
|
||||
};
|
||||
command_buffer.pipelineBarrier2({
|
||||
.imageMemoryBarrierCount = 1,
|
||||
@@ -416,29 +452,18 @@ void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk:
|
||||
session_initialized = true;
|
||||
}
|
||||
|
||||
std::optional<vk::VideoReferenceSlotInfoKHR> ref_slot;
|
||||
for (size_t i = 1; i < dpb_slots.size(); ++i)
|
||||
{
|
||||
auto & x = dpb_slots[(dpb_slots.size() + slot - i) % dpb_slots.size()];
|
||||
if (x.slotIndex >= 0 and x.slotIndex != slot)
|
||||
{
|
||||
ref_slot = x;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
vk::VideoEncodeInfoKHR encode_info{
|
||||
.pNext = encode_info_next(frame_num, slot, ref_slot),
|
||||
.pNext = encode_info_next(frame_num, slot_index, ref_slot ? std::make_optional(ref_slot->info.slotIndex) : std::nullopt),
|
||||
.dstBuffer = output_buffer,
|
||||
.dstBufferOffset = 0,
|
||||
.dstBufferRange = output_buffer_size,
|
||||
.srcPictureResource = {.codedExtent = rect.extent,
|
||||
.baseArrayLayer = 0,
|
||||
.imageViewBinding = image_view},
|
||||
.pSetupReferenceSlot = &dpb_slots[slot],
|
||||
.referenceSlotCount = uint32_t(ref_slot ? 1 : 0),
|
||||
.pReferenceSlots = &*ref_slot,
|
||||
.pSetupReferenceSlot = &slot->info,
|
||||
};
|
||||
if (ref_slot)
|
||||
encode_info.setReferenceSlots(ref_slot->info);
|
||||
|
||||
command_buffer.beginQuery(*query_pool, 0, {});
|
||||
command_buffer.encodeVideoKHR(encode_info);
|
||||
@@ -448,3 +473,14 @@ void wivrn::video_encoder_vulkan::present_image(bool idr, vk::Image src_yuv, vk:
|
||||
|
||||
++frame_num;
|
||||
}
|
||||
|
||||
void wivrn::video_encoder_vulkan::on_feedback(const from_headset::feedback & feedback)
|
||||
{
|
||||
if (feedback.sent_to_decoder)
|
||||
{
|
||||
auto prev = last_ack.load();
|
||||
while (prev < feedback.frame_index and last_ack.compare_exchange_weak(prev, feedback.frame_index))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,9 +45,17 @@ class video_encoder_vulkan : public VideoEncoder
|
||||
std::array<vk::Fence, num_slots> fences;
|
||||
|
||||
image_allocation dpb_image;
|
||||
std::vector<vk::raii::ImageView> dpb_image_views;
|
||||
std::vector<vk::VideoPictureResourceInfoKHR> dpb_resource;
|
||||
std::vector<vk::VideoReferenceSlotInfoKHR> dpb_slots;
|
||||
|
||||
struct dpb_item
|
||||
{
|
||||
vk::raii::ImageView image_view;
|
||||
vk::VideoPictureResourceInfoKHR resource;
|
||||
vk::VideoReferenceSlotInfoKHR & info;
|
||||
uint64_t frame_index = -1;
|
||||
};
|
||||
|
||||
std::vector<dpb_item> dpb;
|
||||
std::vector<vk::VideoReferenceSlotInfoKHR> dpb_info;
|
||||
|
||||
std::vector<vk::raii::DeviceMemory> mem;
|
||||
|
||||
@@ -56,6 +64,7 @@ class video_encoder_vulkan : public VideoEncoder
|
||||
const vk::PhysicalDeviceVideoFormatInfoKHR &);
|
||||
|
||||
uint32_t frame_num = 0;
|
||||
std::atomic<uint64_t> last_ack = 0;
|
||||
bool session_initialized = false;
|
||||
const vk::Rect2D rect;
|
||||
const float fps;
|
||||
@@ -80,11 +89,12 @@ protected:
|
||||
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::optional<vk::VideoReferenceSlotInfoKHR> & reference_slot) = 0;
|
||||
virtual void * encode_info_next(uint32_t frame_num, size_t slot, std::optional<int32_t> 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(bool idr, vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot) override;
|
||||
void present_image(vk::Image y_cbcr, vk::raii::CommandBuffer & cmd_buf, vk::Fence, uint8_t slot, uint64_t frame_index) override;
|
||||
void on_feedback(const from_headset::feedback &) override;
|
||||
};
|
||||
} // namespace wivrn
|
||||
|
||||
@@ -236,7 +236,7 @@ void wivrn::video_encoder_vulkan_h264::send_idr_data()
|
||||
SendData(data, false);
|
||||
}
|
||||
|
||||
void * wivrn::video_encoder_vulkan_h264::encode_info_next(uint32_t frame_num, size_t slot, const std::optional<vk::VideoReferenceSlotInfoKHR> & ref_slot)
|
||||
void * wivrn::video_encoder_vulkan_h264::encode_info_next(uint32_t frame_num, size_t slot, std::optional<int32_t> ref_slot)
|
||||
{
|
||||
slice_header = {
|
||||
.flags =
|
||||
@@ -282,7 +282,7 @@ void * wivrn::video_encoder_vulkan_h264::encode_info_next(uint32_t frame_num, si
|
||||
std::ranges::fill(reference_lists_info.RefPicList1, STD_VIDEO_H264_NO_REFERENCE_PICTURE);
|
||||
|
||||
if (ref_slot)
|
||||
reference_lists_info.RefPicList0[0] = ref_slot->slotIndex;
|
||||
reference_lists_info.RefPicList0[0] = *ref_slot;
|
||||
const uint32_t frame_num_mask = ((1 << (sps.log2_max_frame_num_minus4 + 4)) - 1);
|
||||
std_picture_info = {
|
||||
.flags =
|
||||
@@ -319,15 +319,18 @@ void * wivrn::video_encoder_vulkan_h264::encode_info_next(uint32_t frame_num, si
|
||||
|
||||
if (ref_slot)
|
||||
{
|
||||
auto ref_frame = dpb_std_info[ref_slot->slotIndex].FrameNum;
|
||||
if ((ref_frame + 1) % frame_num_mask != i.FrameNum)
|
||||
auto ref_frame = dpb_std_info[*ref_slot].FrameNum;
|
||||
if (((ref_frame + 1) & frame_num_mask) != i.FrameNum)
|
||||
{
|
||||
reference_lists_info.flags.ref_pic_list_modification_flag_l0 = 1;
|
||||
reference_lists_info.refList0ModOpCount = 1;
|
||||
reference_lists_info.pRefList0ModOperations = &ref_mod;
|
||||
ref_mod = {
|
||||
.modification_of_pic_nums_idc = STD_VIDEO_H264_MODIFICATION_OF_PIC_NUMS_IDC_SHORT_TERM_ADD,
|
||||
.abs_diff_pic_num_minus1 = uint16_t(1 + i.FrameNum - ref_frame),
|
||||
reference_lists_info.refList0ModOpCount = ref_mod.size();
|
||||
reference_lists_info.pRefList0ModOperations = ref_mod.data();
|
||||
ref_mod[0] = {
|
||||
.modification_of_pic_nums_idc = STD_VIDEO_H264_MODIFICATION_OF_PIC_NUMS_IDC_SHORT_TERM_SUBTRACT,
|
||||
.abs_diff_pic_num_minus1 = uint16_t(uint16_t(i.FrameNum - ref_frame - 1) & frame_num_mask),
|
||||
};
|
||||
ref_mod[1] = {
|
||||
.modification_of_pic_nums_idc = STD_VIDEO_H264_MODIFICATION_OF_PIC_NUMS_IDC_END,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ class video_encoder_vulkan_h264 : public video_encoder_vulkan
|
||||
vk::VideoEncodeH264PictureInfoKHR picture_info;
|
||||
|
||||
StdVideoEncodeH264ReferenceListsInfo reference_lists_info;
|
||||
StdVideoEncodeH264RefListModEntry ref_mod;
|
||||
std::array<StdVideoEncodeH264RefListModEntry, 2> ref_mod;
|
||||
|
||||
std::vector<StdVideoEncodeH264ReferenceInfo> dpb_std_info;
|
||||
std::vector<vk::VideoEncodeH264DpbSlotInfoKHR> dpb_std_slots;
|
||||
@@ -52,7 +52,7 @@ class video_encoder_vulkan_h264 : public video_encoder_vulkan
|
||||
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::optional<vk::VideoReferenceSlotInfoKHR> &) override;
|
||||
void * encode_info_next(uint32_t frame_num, size_t slot, std::optional<int32_t>) override;
|
||||
virtual vk::ExtensionProperties std_header_version() override;
|
||||
|
||||
void send_idr_data() override;
|
||||
|
||||
Reference in New Issue
Block a user