Reduce some copies in serialization
Also update a bunch of stuff
This commit is contained in:
@@ -22,13 +22,11 @@
|
||||
#include "rs.h"
|
||||
#include "scenes/stream.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "utils/typename.h"
|
||||
#include "wivrn_serialization.h"
|
||||
|
||||
using namespace xrt::drivers::wivrn::to_headset;
|
||||
using shard_set = shard_accumulator::shard_set;
|
||||
using data_shard = shard_accumulator::data_shard;
|
||||
using parity_shard = shard_accumulator::parity_shard;
|
||||
|
||||
shard_set::shard_set(uint8_t stream_index)
|
||||
{
|
||||
@@ -40,7 +38,6 @@ void shard_set::reset(uint64_t frame_index)
|
||||
num_shards = 0;
|
||||
min_for_reconstruction = -1;
|
||||
data.clear();
|
||||
parity.clear();
|
||||
|
||||
uint8_t stream_index = feedback.stream_index;
|
||||
feedback = {};
|
||||
@@ -79,125 +76,9 @@ uint16_t shard_set::insert(data_shard && shard)
|
||||
if (not data[idx])
|
||||
++num_shards;
|
||||
data[idx] = std::move(shard);
|
||||
if (auto i = try_reconstruct())
|
||||
return *i;
|
||||
return idx;
|
||||
}
|
||||
|
||||
std::optional<uint16_t> shard_set::insert(parity_shard && shard)
|
||||
{
|
||||
XrTime now = application::now();
|
||||
if (empty())
|
||||
feedback.received_first_packet = now;
|
||||
feedback.received_last_packet = now;
|
||||
|
||||
auto idx = shard.parity_element;
|
||||
min_for_reconstruction = shard.data_shard_count;
|
||||
if (idx >= parity.size())
|
||||
parity.resize(idx + 1);
|
||||
if (not parity[idx])
|
||||
++num_shards;
|
||||
parity[idx] = std::move(shard);
|
||||
|
||||
return try_reconstruct();
|
||||
}
|
||||
|
||||
parity_shard & shard_set::get_parity()
|
||||
{
|
||||
for (auto & shard: parity)
|
||||
{
|
||||
if (shard)
|
||||
return *shard;
|
||||
}
|
||||
// this function shall be called only when at least one parity shard exists
|
||||
assert(false);
|
||||
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
std::optional<uint16_t> shard_set::try_reconstruct()
|
||||
{
|
||||
if (parity.empty())
|
||||
return {};
|
||||
if (num_shards < min_for_reconstruction)
|
||||
return {};
|
||||
|
||||
static std::once_flag once;
|
||||
std::call_once(once, reed_solomon_init);
|
||||
auto & p = get_parity();
|
||||
std::unique_ptr<reed_solomon, void (*)(reed_solomon *)> rs(
|
||||
reed_solomon_new(p.data_shard_count, p.num_parity_elements), reed_solomon_release);
|
||||
|
||||
size_t shard_size = p.payload.size();
|
||||
|
||||
std::vector<uint8_t *> rs_shards;
|
||||
std::vector<uint8_t> rs_marks;
|
||||
|
||||
std::vector<std::vector<uint8_t>> data_raw;
|
||||
data.resize(p.data_shard_count);
|
||||
data_raw.reserve(p.data_shard_count);
|
||||
for (auto & shard: data)
|
||||
{
|
||||
if (shard)
|
||||
{
|
||||
xrt::drivers::wivrn::serialization_packet s;
|
||||
s.serialize(*shard);
|
||||
data_raw.emplace_back(std::move(s)).resize(shard_size);
|
||||
rs_marks.push_back(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
data_raw.emplace_back(shard_size, 0);
|
||||
rs_marks.push_back(1);
|
||||
}
|
||||
rs_shards.push_back(data_raw.back().data());
|
||||
}
|
||||
// WARNING: do not use p after this line, the array it points to has been modified
|
||||
parity.resize(p.num_parity_elements);
|
||||
std::vector<std::vector<uint8_t>> dummy;
|
||||
dummy.reserve(p.num_parity_elements);
|
||||
for (auto & shard: parity)
|
||||
{
|
||||
if (shard)
|
||||
{
|
||||
rs_shards.push_back(shard->payload.data());
|
||||
rs_marks.push_back(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto d = dummy.emplace_back(shard_size).data();
|
||||
rs_shards.push_back(d);
|
||||
rs_marks.push_back(1);
|
||||
}
|
||||
}
|
||||
|
||||
int err = reed_solomon_reconstruct(rs.get(), rs_shards.data(), rs_marks.data(), data.size() + parity.size(), shard_size);
|
||||
if (err)
|
||||
{
|
||||
spdlog::info("reed_solomon_reconstruct failed");
|
||||
return {};
|
||||
}
|
||||
std::optional<uint16_t> first_reconstructed;
|
||||
for (size_t i = 0; i < data.size(); ++i)
|
||||
{
|
||||
if (not data[i])
|
||||
{
|
||||
xrt::drivers::wivrn::deserialization_packet d(data_raw[i]);
|
||||
data[i] = d.deserialize<data_shard>();
|
||||
if (not first_reconstructed)
|
||||
first_reconstructed = i;
|
||||
}
|
||||
}
|
||||
assert(first_reconstructed);
|
||||
spdlog::info("reed_solomon_reconstruct success frame {}, reconstructed {} (out of {} shards)",
|
||||
data[0]->frame_idx,
|
||||
*first_reconstructed,
|
||||
data.size());
|
||||
|
||||
feedback.reconstructed = application::now();
|
||||
return first_reconstructed;
|
||||
}
|
||||
|
||||
static void debug_why_not_sent(const shard_set & shards)
|
||||
{
|
||||
const auto & frame = shards.data;
|
||||
@@ -207,10 +88,7 @@ static void debug_why_not_sent(const shard_set & shards)
|
||||
return;
|
||||
}
|
||||
int frame_idx = -1;
|
||||
int expected_data = -1;
|
||||
int expected_parity = -1;
|
||||
size_t data = 0;
|
||||
size_t parity = 0;
|
||||
for (const auto & shard: frame)
|
||||
{
|
||||
if (shard)
|
||||
@@ -219,18 +97,8 @@ static void debug_why_not_sent(const shard_set & shards)
|
||||
++data;
|
||||
}
|
||||
}
|
||||
for (const auto & shard: shards.parity)
|
||||
{
|
||||
if (shard)
|
||||
{
|
||||
frame_idx = shard->frame_idx;
|
||||
expected_data = shard->data_shard_count;
|
||||
expected_parity = shard->num_parity_elements;
|
||||
++parity;
|
||||
}
|
||||
}
|
||||
|
||||
spdlog::info("frame {} was not sent with {}/{} data, {}/{} parity shards)", frame_idx, data, expected_data, parity, expected_parity);
|
||||
spdlog::info("frame {} was not sent with {} data shards", frame_idx, data);
|
||||
}
|
||||
|
||||
void shard_accumulator::advance()
|
||||
@@ -239,8 +107,7 @@ void shard_accumulator::advance()
|
||||
next.reset(current.frame_index() + 1);
|
||||
}
|
||||
|
||||
template <typename Shard>
|
||||
void shard_accumulator::push_shard(Shard && shard)
|
||||
void shard_accumulator::push_shard(video_stream_data_shard&& shard)
|
||||
{
|
||||
assert(current.frame_index() + 1 == next.frame_index());
|
||||
|
||||
@@ -251,7 +118,6 @@ void shard_accumulator::push_shard(Shard && shard)
|
||||
}
|
||||
else if (frame_diff == 0)
|
||||
{
|
||||
// Due to error correction, inserting a shard might create a valid one before
|
||||
auto shard_idx = current.insert(std::move(shard));
|
||||
try_submit_frame(shard_idx);
|
||||
}
|
||||
@@ -290,9 +156,6 @@ void shard_accumulator::push_shard(Shard && shard)
|
||||
}
|
||||
}
|
||||
|
||||
template void shard_accumulator::push_shard<parity_shard>(parity_shard && shard);
|
||||
template void shard_accumulator::push_shard<video_stream_data_shard>(video_stream_data_shard && shard);
|
||||
|
||||
void shard_accumulator::try_submit_frame(std::optional<uint16_t> shard_idx)
|
||||
{
|
||||
if (shard_idx)
|
||||
|
||||
@@ -30,9 +30,6 @@ using decoder_impl = ::ffmpeg::decoder;
|
||||
#endif
|
||||
|
||||
#include "wivrn_packets.h"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
@@ -42,21 +39,15 @@ class shard_accumulator
|
||||
|
||||
public:
|
||||
using data_shard = xrt::drivers::wivrn::to_headset::video_stream_data_shard;
|
||||
using parity_shard = xrt::drivers::wivrn::to_headset::video_stream_parity_shard;
|
||||
struct shard_set
|
||||
{
|
||||
size_t num_shards = 0;
|
||||
size_t min_for_reconstruction = -1;
|
||||
std::vector<std::optional<data_shard>> data;
|
||||
std::vector<std::optional<parity_shard>> parity;
|
||||
void reset(uint64_t frame_index);
|
||||
bool empty() const;
|
||||
|
||||
uint16_t insert(data_shard &&);
|
||||
std::optional<uint16_t> insert(parity_shard &&);
|
||||
|
||||
parity_shard & get_parity();
|
||||
std::optional<uint16_t> try_reconstruct();
|
||||
|
||||
xrt::drivers::wivrn::from_headset::feedback feedback{};
|
||||
|
||||
@@ -93,8 +84,7 @@ public:
|
||||
next.reset(1);
|
||||
}
|
||||
|
||||
template <typename Shard>
|
||||
void push_shard(Shard &&);
|
||||
void push_shard(xrt::drivers::wivrn::to_headset::video_stream_data_shard&&);
|
||||
|
||||
auto & desc() const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user