Remove FEC code, reduce data copies
This commit is contained in:
@@ -244,9 +244,6 @@ struct video_stream_description
|
|||||||
|
|
||||||
class video_stream_data_shard
|
class video_stream_data_shard
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> data;
|
|
||||||
friend serialization_traits<video_stream_data_shard, void>;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
inline static const size_t max_payload_size = 1400;
|
inline static const size_t max_payload_size = 1400;
|
||||||
enum flags : uint8_t
|
enum flags : uint8_t
|
||||||
@@ -274,13 +271,10 @@ public:
|
|||||||
};
|
};
|
||||||
std::optional<view_info_t> view_info;
|
std::optional<view_info_t> view_info;
|
||||||
|
|
||||||
|
// Container for the data, read payload instead
|
||||||
|
std::vector<uint8_t> data;
|
||||||
// Actual video data, may contain multiple NAL units
|
// Actual video data, may contain multiple NAL units
|
||||||
std::span<uint8_t> payload;
|
std::span<uint8_t> payload;
|
||||||
void set_payload(std::vector<uint8_t> && data)
|
|
||||||
{
|
|
||||||
this->data = std::move(data);
|
|
||||||
payload = this->data;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct video_stream_parity_shard
|
struct video_stream_parity_shard
|
||||||
@@ -324,6 +318,7 @@ struct serialization_traits<to_headset::video_stream_data_shard, void>
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
static void serialize(const to_headset::video_stream_data_shard & shard, T & packet)
|
static void serialize(const to_headset::video_stream_data_shard & shard, T & packet)
|
||||||
{
|
{
|
||||||
|
packet.reserve(to_headset::video_stream_data_shard::max_payload_size + 20);
|
||||||
packet.serialize(shard.stream_item_idx);
|
packet.serialize(shard.stream_item_idx);
|
||||||
packet.serialize(shard.frame_idx);
|
packet.serialize(shard.frame_idx);
|
||||||
packet.serialize(shard.shard_idx);
|
packet.serialize(shard.shard_idx);
|
||||||
|
|||||||
@@ -101,6 +101,11 @@ class serialization_packet
|
|||||||
public:
|
public:
|
||||||
serialization_packet() = default;
|
serialization_packet() = default;
|
||||||
|
|
||||||
|
void reserve(size_t n)
|
||||||
|
{
|
||||||
|
buffer.reserve(n);
|
||||||
|
}
|
||||||
|
|
||||||
void write(const void * data, size_t size)
|
void write(const void * data, size_t size)
|
||||||
{
|
{
|
||||||
size_t index = buffer.size();
|
size_t index = buffer.size();
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
#include "wivrn_session.h"
|
#include "wivrn_session.h"
|
||||||
|
|
||||||
#include "video_encoder.h"
|
#include "video_encoder.h"
|
||||||
#include "rs.h"
|
|
||||||
#include "util/u_logging.h"
|
#include "util/u_logging.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -88,81 +87,29 @@ void VideoEncoder::Encode(wivrn_session & cnx,
|
|||||||
bool idr)
|
bool idr)
|
||||||
{
|
{
|
||||||
this->cnx = &cnx;
|
this->cnx = &cnx;
|
||||||
this->frame_idx = frame_index;
|
|
||||||
auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time));
|
auto target_timestamp = std::chrono::steady_clock::time_point(std::chrono::nanoseconds(view_info.display_time));
|
||||||
cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx);
|
cnx.dump_time("encode_begin", frame_index, os_monotonic_get_ns(), stream_idx);
|
||||||
|
|
||||||
shards.clear();
|
// Prepare the video shard template
|
||||||
|
shard.stream_item_idx = stream_idx;
|
||||||
|
shard.frame_idx = frame_index;
|
||||||
|
shard.shard_idx = -1;
|
||||||
|
shard.view_info.reset();
|
||||||
|
|
||||||
Encode(index, idr, target_timestamp);
|
Encode(index, idr, target_timestamp);
|
||||||
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx);
|
cnx.dump_time("encode_end", frame_index, os_monotonic_get_ns(), stream_idx);
|
||||||
if (shards.empty())
|
if (shard.payload.empty())
|
||||||
return;
|
return;
|
||||||
const size_t view_info_size = sizeof(to_headset::video_stream_data_shard::view_info_t);
|
const size_t view_info_size = sizeof(to_headset::video_stream_data_shard::view_info_t);
|
||||||
if (shards.back().payload.size() + view_info_size > to_headset::video_stream_data_shard::max_payload_size)
|
if (shard.payload.size() + view_info_size > to_headset::video_stream_data_shard::max_payload_size)
|
||||||
{
|
{
|
||||||
// Push empty data so previous shard is sent and counters are set
|
// Push empty data so previous shard is sent and counters are set
|
||||||
PushShard({}, to_headset::video_stream_data_shard::start_of_slice | to_headset::video_stream_data_shard::end_of_slice);
|
PushShard({}, to_headset::video_stream_data_shard::start_of_slice | to_headset::video_stream_data_shard::end_of_slice);
|
||||||
}
|
}
|
||||||
shards.back().view_info = view_info;
|
shard.view_info = view_info;
|
||||||
shards.back().flags |= to_headset::video_stream_data_shard::end_of_frame;
|
shard.flags |= to_headset::video_stream_data_shard::end_of_frame;
|
||||||
cnx.send_stream(shards.back());
|
cnx.send_stream(shard);
|
||||||
|
|
||||||
#if WITH_FEC
|
|
||||||
// forward error correction
|
|
||||||
std::vector<std::vector<uint8_t>> data_shards;
|
|
||||||
data_shards.reserve(shards.size());
|
|
||||||
size_t max_size = 0;
|
|
||||||
for (const auto & shard: shards)
|
|
||||||
{
|
|
||||||
serialization_packet packet;
|
|
||||||
packet.serialize(shard);
|
|
||||||
const auto & serialized = data_shards.emplace_back(std::move(packet));
|
|
||||||
max_size = std::max(max_size, serialized.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t parity = std::max<size_t>(1, shards.size() * 0.05);
|
|
||||||
std::vector<std::vector<uint8_t>> parity_shards(parity);
|
|
||||||
|
|
||||||
std::vector<unsigned char *> shard_pointers;
|
|
||||||
|
|
||||||
for (auto & shard: data_shards)
|
|
||||||
{
|
|
||||||
shard.resize(max_size, 0);
|
|
||||||
shard_pointers.push_back(shard.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto & shard: parity_shards)
|
|
||||||
{
|
|
||||||
shard.resize(max_size, 0);
|
|
||||||
shard_pointers.push_back(shard.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::once_flag once;
|
|
||||||
std::call_once(once, reed_solomon_init);
|
|
||||||
|
|
||||||
std::unique_ptr<reed_solomon, void (*)(reed_solomon *)> rs(reed_solomon_new(shards.size(), parity),
|
|
||||||
reed_solomon_release);
|
|
||||||
if (rs)
|
|
||||||
{
|
|
||||||
reed_solomon_encode(rs.get(), shard_pointers.data(), shard_pointers.size(), max_size);
|
|
||||||
|
|
||||||
for (auto & shard: parity_shards)
|
|
||||||
{
|
|
||||||
to_headset::video_stream_parity_shard packet{};
|
|
||||||
packet.stream_item_idx = stream_idx;
|
|
||||||
packet.frame_idx = frame_idx;
|
|
||||||
packet.data_shard_count = data_shards.size();
|
|
||||||
packet.num_parity_elements = parity;
|
|
||||||
packet.payload = std::move(shard);
|
|
||||||
cnx.send_stream(packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
U_LOG_W("failed to setup reed_solomon encoder with %ld data shards", data_shards.size());
|
|
||||||
}
|
|
||||||
cnx.dump_time("rs_end", frame_index, os_monotonic_get_ns(), stream_idx);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoEncoder::SendData(std::vector<uint8_t> && data)
|
void VideoEncoder::SendData(std::vector<uint8_t> && data)
|
||||||
@@ -176,7 +123,7 @@ void VideoEncoder::SendData(std::vector<uint8_t> && data)
|
|||||||
uint8_t flags = to_headset::video_stream_data_shard::start_of_slice;
|
uint8_t flags = to_headset::video_stream_data_shard::start_of_slice;
|
||||||
if (data.size() <= max_payload_size)
|
if (data.size() <= max_payload_size)
|
||||||
{
|
{
|
||||||
PushShard(std::move(data), flags | to_headset::video_stream_data_shard::end_of_slice);
|
PushShard(data, flags | to_headset::video_stream_data_shard::end_of_slice);
|
||||||
}
|
}
|
||||||
auto begin = data.begin();
|
auto begin = data.begin();
|
||||||
auto end = data.end();
|
auto end = data.end();
|
||||||
@@ -191,25 +138,22 @@ void VideoEncoder::SendData(std::vector<uint8_t> && data)
|
|||||||
begin = next;
|
begin = next;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
}
|
}
|
||||||
|
shard.data = std::move(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoEncoder::PushShard(std::vector<uint8_t> && payload, uint8_t flags)
|
void VideoEncoder::PushShard(const std::span<uint8_t> & payload, uint8_t flags)
|
||||||
{
|
{
|
||||||
if (not shards.empty())
|
if (shard.shard_idx != decltype(shard.shard_idx)(-1))
|
||||||
{
|
{
|
||||||
cnx->send_stream(shards.back());
|
cnx->send_stream(shard);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cnx->dump_time("send_start", frame_idx, os_monotonic_get_ns(), stream_idx);
|
cnx->dump_time("send_start", shard.frame_idx, os_monotonic_get_ns(), stream_idx);
|
||||||
}
|
}
|
||||||
to_headset::video_stream_data_shard shard;
|
++shard.shard_idx;
|
||||||
shard.stream_item_idx = stream_idx;
|
|
||||||
shard.frame_idx = frame_idx;
|
|
||||||
shard.shard_idx = shards.size();
|
|
||||||
shard.flags = flags;
|
shard.flags = flags;
|
||||||
shard.set_payload(std::move(payload));
|
shard.payload = payload;
|
||||||
shards.emplace_back(std::move(shard));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace xrt::drivers::wivrn
|
} // namespace xrt::drivers::wivrn
|
||||||
|
|||||||
@@ -47,12 +47,11 @@ protected:
|
|||||||
uint8_t stream_idx;
|
uint8_t stream_idx;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t frame_idx;
|
|
||||||
|
|
||||||
// temporary data
|
// temporary data
|
||||||
wivrn_session * cnx;
|
wivrn_session * cnx;
|
||||||
|
|
||||||
std::vector<to_headset::video_stream_data_shard> shards;
|
// shard waiting to be sent
|
||||||
|
to_headset::video_stream_data_shard shard;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::unique_ptr<VideoEncoder> Create(vk_bundle * vk,
|
static std::unique_ptr<VideoEncoder> Create(vk_bundle * vk,
|
||||||
@@ -92,7 +91,7 @@ protected:
|
|||||||
void SendData(std::vector<uint8_t> && data);
|
void SendData(std::vector<uint8_t> && data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void PushShard(std::vector<uint8_t> && payload, uint8_t flags);
|
void PushShard(const std::span<uint8_t>& payload, uint8_t flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace xrt::drivers::wivrn
|
} // namespace xrt::drivers::wivrn
|
||||||
|
|||||||
Reference in New Issue
Block a user