-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.cpp
More file actions
238 lines (204 loc) · 8.28 KB
/
decoder.cpp
File metadata and controls
238 lines (204 loc) · 8.28 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "decoder.h"
#include <algorithm>
#include <array>
namespace {
bool consumeTrailingPadding(BitReader &reader, std::string &error) {
const std::size_t remaining = reader.bitsRemaining();
if (remaining >= 8) {
error = "Trailing non-padding bytes found";
return false;
}
if (remaining == 0) {
return true;
}
uint32_t tail = 0;
if (!reader.readBits(static_cast<int>(remaining), tail)) {
error = "Failed to consume trailing padding";
return false;
}
if (tail != 0) {
error = "Trailing padding bits must be zero";
return false;
}
return true;
}
} // namespace
void Decoder::reset() {
previousFrame_ = Gray4Frame();
hasPreviousFrame_ = false;
}
bool Decoder::decodeChangeMap(BitReader &reader,
int totalBlocks,
std::vector<uint8_t> &changed,
std::string &error) {
changed.assign(static_cast<std::size_t>(totalBlocks), 0);
uint32_t numRuns = 0;
if (!reader.readBits(16, numRuns)) {
error = "Failed to read change-map run count";
return false;
}
bool state = false;
int cursor = 0;
for (uint32_t runIndex = 0; runIndex < numRuns; ++runIndex) {
uint32_t runLen = 0;
if (!reader.readBits(16, runLen)) {
error = "Failed to read change-map run length";
return false;
}
if (cursor + static_cast<int>(runLen) > totalBlocks) {
error = "Change-map overflows block count";
return false;
}
if (runLen > 0) {
std::fill(changed.begin() + cursor,
changed.begin() + cursor + static_cast<int>(runLen),
state ? static_cast<uint8_t>(1) : static_cast<uint8_t>(0));
cursor += static_cast<int>(runLen);
}
state = !state;
}
if (cursor != totalBlocks) {
error = "Change-map did not cover all blocks";
return false;
}
return true;
}
DecodeResult Decoder::decode(const std::vector<uint8_t> &packetBytes) {
DecodeResult result;
BitReader reader(packetBytes);
BitstreamHeader header;
if (!readHeader(reader, header, result.error)) {
return result;
}
const int expectedBlocks = totalBlockCount(header.width, header.height, header.blockSize);
if (expectedBlocks != header.totalBlocks) {
result.error = "Header block count mismatch";
return result;
}
Gray4Frame frame(header.width, header.height);
std::vector<uint8_t> rawSamples;
if (header.frameType == FrameType::Keyframe) {
for (int block = 0; block < expectedBlocks; ++block) {
const BlockGeometry geom = blockGeometry(block, header.width, header.height, header.blockSize);
bool useRaw = false;
if (!reader.readBit(useRaw)) {
result.error = "Failed to read keyframe block mode";
return result;
}
if (useRaw) {
rawSamples.assign(static_cast<std::size_t>(geom.w * geom.h), 0);
for (std::size_t i = 0; i < rawSamples.size(); ++i) {
uint32_t sample = 0;
if (!reader.readBits(4, sample)) {
result.error = "Failed to read keyframe raw block";
return result;
}
rawSamples[i] = static_cast<uint8_t>(sample);
}
writeRawBlock(frame, geom, rawSamples);
} else {
std::array<uint8_t, 16> cells{};
for (int i = 0; i < 16; ++i) {
uint32_t value = 0;
if (!reader.readBits(4, value)) {
result.error = "Failed to read keyframe 4x4 block";
return result;
}
cells[static_cast<std::size_t>(i)] = static_cast<uint8_t>(value);
}
paintCells4x4(frame, geom, cells);
}
}
} else {
if (!hasPreviousFrame_) {
result.error = "Interframe received without reference frame";
return result;
}
if (previousFrame_.width != header.width || previousFrame_.height != header.height) {
result.error = "Interframe dimensions do not match reference frame";
return result;
}
frame = previousFrame_;
std::vector<uint8_t> changed;
if (!decodeChangeMap(reader, expectedBlocks, changed, result.error)) {
return result;
}
uint16_t changedCount = 0;
for (int block = 0; block < expectedBlocks; ++block) {
if (changed[static_cast<std::size_t>(block)] == 0) {
continue;
}
++changedCount;
const BlockGeometry geom = blockGeometry(block, header.width, header.height, header.blockSize);
uint32_t modeBits = 0;
if (!reader.readBits(2, modeBits)) {
result.error = "Failed to read inter block mode";
return result;
}
if (modeBits == static_cast<uint32_t>(BlockPayloadMode::Residual4x4)) {
std::array<int, 16> qResidual{};
for (int i = 0; i < 16; ++i) {
uint32_t packed = 0;
if (!reader.readBits(4, packed)) {
result.error = "Failed to read residual block";
return result;
}
qResidual[static_cast<std::size_t>(i)] = unpackSigned4(static_cast<uint8_t>(packed));
}
const std::array<uint8_t, 16> prevCells = sampleBlockToCells4x4(previousFrame_, geom);
std::array<uint8_t, 16> reconstructedCells{};
for (int i = 0; i < 16; ++i) {
const int residualStep = std::max(1, static_cast<int>(header.residualStep));
const int value = static_cast<int>(prevCells[static_cast<std::size_t>(i)]) +
qResidual[static_cast<std::size_t>(i)] * residualStep;
reconstructedCells[static_cast<std::size_t>(i)] = clamp4(value);
}
paintCells4x4(frame, geom, reconstructedCells);
} else if (modeBits == static_cast<uint32_t>(BlockPayloadMode::Absolute4x4)) {
std::array<uint8_t, 16> cells{};
for (int i = 0; i < 16; ++i) {
uint32_t value = 0;
if (!reader.readBits(4, value)) {
result.error = "Failed to read absolute block";
return result;
}
cells[static_cast<std::size_t>(i)] = static_cast<uint8_t>(value);
}
paintCells4x4(frame, geom, cells);
} else if (modeBits == static_cast<uint32_t>(BlockPayloadMode::Raw8x8)) {
rawSamples.assign(static_cast<std::size_t>(geom.w * geom.h), 0);
for (std::size_t i = 0; i < rawSamples.size(); ++i) {
uint32_t sample = 0;
if (!reader.readBits(4, sample)) {
result.error = "Failed to read inter raw block";
return result;
}
rawSamples[i] = static_cast<uint8_t>(sample);
}
writeRawBlock(frame, geom, rawSamples);
} else {
result.error = "Reserved block mode encountered";
return result;
}
}
if (header.changedBlocks != changedCount) {
// Not fatal: decoder trusts the explicit change map.
}
}
if (!consumeTrailingPadding(reader, result.error)) {
return result;
}
previousFrame_ = frame;
hasPreviousFrame_ = true;
result.ok = true;
result.frame = std::move(frame);
result.meta.frameType = header.frameType;
result.meta.mode = header.mode;
result.meta.frameIndex = header.frameIndex;
result.meta.totalBlocks = header.totalBlocks;
result.meta.changedBlocks = header.changedBlocks;
result.meta.width = header.width;
result.meta.height = header.height;
result.meta.keyframeInterval = header.keyframeInterval;
return result;
}