-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlay.hpp
180 lines (153 loc) · 5.86 KB
/
overlay.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (C) 2024 Geon Technologies, LLC
*
* This file is part of composite-comps.
*
* composite-comps is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* composite-comps 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include <map>
#include <span>
#include <vrtgen/vrtgen.hpp>
namespace overlay {
namespace sdds {
static constexpr double TIME_TIC = 250e-12;
static constexpr double TIME_TWO32 = 4294967296.0;
static constexpr uint64_t PS250_PER_SEC = 4000000000;
class overlay {
static constexpr std::size_t DATA_IDX = 56;
static constexpr std::size_t DATA_LEN = 1024;
public:
explicit overlay(std::span<const uint8_t> data) :
m_data(data) {
}
auto ttag() const -> uint64_t {
return vrtgen::swap::from_be(*reinterpret_cast<const uint64_t*>(m_data.data() + 8));
}
auto ttage() const -> uint32_t {
return vrtgen::swap::from_be(*reinterpret_cast<const uint32_t*>(m_data.data() + 16));
}
auto secs() const -> uint32_t {
return static_cast<uint32_t>(ttag() / PS250_PER_SEC);
}
auto psecs() const -> uint64_t {
return (ttag() * uint64_t{250u}) - (static_cast<uint64_t>(secs()) * uint64_t{1'000'000'000'000u});
}
template<typename T>
auto payload() const -> std::span<const T> {
auto data = reinterpret_cast<const T*>(m_data.data() + DATA_IDX);
return std::span<const T>(data, DATA_LEN / sizeof(T));
}
private:
std::span<const uint8_t> m_data;
}; // class overlay
} // namespace sdds
namespace v49 {
auto is_data(const vrtgen::packing::Header& header) -> bool {
using enum vrtgen::packing::PacketType;
return (header.packet_type() == SIGNAL_DATA) || (header.packet_type() == SIGNAL_DATA_STREAM_ID);
}
auto is_context(const vrtgen::packing::Header& header) -> bool {
using enum vrtgen::packing::PacketType;
return (header.packet_type() == CONTEXT);
}
class overlay {
public:
explicit overlay(std::span<const uint8_t> data) :
m_data(data) {
m_header.unpack_from(m_data.data());
auto curr_idx = m_header.size();
if (m_header.packet_type() != vrtgen::packing::PacketType::SIGNAL_DATA) {
m_positions["stream_id"] = curr_idx;
curr_idx += sizeof(uint32_t); // stream_id
}
if (m_header.class_id_enable()) {
m_positions["class_id"] = curr_idx;
curr_idx += 8; // bytes, class_id
}
if (m_header.tsi() != vrtgen::packing::TSI::NONE) {
m_positions["integer_timestamp"] = curr_idx;
curr_idx += sizeof(uint32_t); // integer_timestamp
}
if (m_header.tsf() != vrtgen::packing::TSF::NONE) {
m_positions["fractional_timestamp"] = curr_idx;
curr_idx += sizeof(uint64_t); // fractional_timestamp
}
if (is_data(m_header)) {
m_positions["payload"] = curr_idx;
auto data_header = vrtgen::packing::DataHeader{};
data_header.unpack_from(m_data.data());
if (data_header.trailer_included()) {
m_positions["trailer"] = (m_header.packet_size() - 1) * sizeof(uint32_t)/*word size*/;
}
}
}
auto header() const -> const vrtgen::packing::Header& {
return m_header;
}
auto stream_id() const -> std::optional<uint32_t> {
if (!m_positions.contains("stream_id")) {
return {};
}
auto pos = m_positions.at("stream_id");
return vrtgen::swap::from_be(*reinterpret_cast<const uint32_t*>(m_data.data() + pos));
}
auto class_id() const -> std::optional<vrtgen::packing::ClassIdentifier> {
if (!m_positions.contains("class_id")) {
return {};
}
auto pos = m_positions.at("class_id");
auto class_id = vrtgen::packing::ClassIdentifier{};
class_id.unpack_from(m_data.data() + pos);
return class_id;
}
auto integer_timestamp() const -> std::optional<uint32_t> {
if (!m_positions.contains("integer_timestamp")) {
return {};
}
auto pos = m_positions.at("integer_timestamp");
return vrtgen::swap::from_be(*reinterpret_cast<const uint32_t*>(m_data.data() + pos));
}
auto fractional_timestamp() const -> std::optional<uint64_t> {
if (!m_positions.contains("fractional_timestamp")) {
return {};
}
auto pos = m_positions.at("fractional_timestamp");
return vrtgen::swap::from_be(*reinterpret_cast<const uint64_t*>(m_data.data() + pos));
}
template<typename T>
auto payload() const -> std::span<const T> {
if (!m_positions.contains("payload")) {
return {};
}
auto pos = m_positions.at("payload");
auto data = reinterpret_cast<const T*>(m_data.data() + pos);
return std::span<const T>(data, payload_size() / sizeof(T));
}
auto payload_size() const -> size_t {
if (!m_positions.contains("payload")) {
return {};
}
auto size = (m_header.packet_size() * sizeof(uint32_t)/*word size*/) - m_positions.at("payload");
if (m_positions.contains("trailer")) {
size -= sizeof(uint32_t);
}
return size;
}
private:
std::span<const uint8_t> m_data;
vrtgen::packing::Header m_header;
std::map<std::string, std::size_t> m_positions;
}; // class overlay
} // namespace v49
} // namespace overlay