upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/bcpack_stream.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/src/bcpack_stream.h')
-rw-r--r--runtime/src/bcpack_stream.h222
1 files changed, 222 insertions, 0 deletions
diff --git a/runtime/src/bcpack_stream.h b/runtime/src/bcpack_stream.h
new file mode 100644
index 000000000..787686929
--- /dev/null
+++ b/runtime/src/bcpack_stream.h
@@ -0,0 +1,222 @@
1// Shared rANS block stream used by the runtime decoder and the offline history fitter.
2#pragma once
3#include <algorithm>
4#include <array>
5#include <bit>
6#include <cstdint>
7#include <span>
8
9namespace leonida::bcpack::detail {
10using namespace std;
11struct Seed {
12 int16_t values[8];
13 bool fitted;
14};
15struct BC6Mode {
16 int code, transformed, base, delta[3], regions, first, count;
17};
18#include "bcpack_tables.inc"
19
20using Symbols = array<uint16_t, 16>;
21constexpr uint32_t home = 1u << 24;
22
23struct Model {
24 uint32_t base[11]{};
25 const uint32_t *frequency{};
26 int width{};
27 array<uint8_t, 256> first{};
28 uint64_t reciprocal[9]{};
29 void init(int w, int q) {
30 width = w;
31 frequency = freq[w][q];
32 for (int p = 1; p <= w; ++p) {
33 base[p] = base[p - 1] + (frequency[p - 1] << max(0, p - 2));
34 reciprocal[p] = ((uint64_t{1} << 32) + frequency[p] - 1) / frequency[p];
35 }
36 unsigned p = 0;
37 for (unsigned window = 0; window < first.size(); ++window) {
38 while (p < unsigned(width) && base[p + 1] <= (window << 8)) ++p;
39 first[window] = uint8_t(p);
40 }
41 }
42 int symbol(uint32_t state, bool fast) const {
43 const uint32_t cf = state & 65535;
44 if (fast) {
45 unsigned p = first[cf >> 8];
46 while (p < unsigned(width) && cf >= base[p + 1]) ++p;
47 if (!p) return 0;
48 const auto quotient = uint32_t(((cf - base[p]) * reciprocal[p]) >> 32);
49 return int((1u << (p - 1)) + quotient);
50 }
51 int p = width;
52 while (p && cf < base[p]) --p;
53 return p ? (1 << (p - 1)) + (cf - base[p]) / frequency[p] : 0;
54 }
55 uint32_t advance(uint32_t state, unsigned sym) const {
56 const int p = bit_width(sym);
57 uint32_t f = frequency[p],
58 c = base[p] + f * (sym - (p ? 1u << (p - 1) : 0u));
59 if (!c) {
60 --f;
61 ++c;
62 }
63 return f * (state >> 16) + (state & 65535) - c;
64 }
65};
66
67// VIctory's GPL-3.0 BCPack decoder uses the high slot byte to locate a bucket.
68// Build this accelerator solely from Leonida's own rANS frequencies.
69inline const Model& model_for(int w, int q) {
70 static const auto models = [] {
71 array<array<Model, 16>, 8> result{};
72 for (int width = 1; width <= 8; ++width)
73 for (int quality = 0; quality < 16; ++quality)
74 result[width - 1][quality].init(width, quality);
75 return result;
76 }();
77 return models[w - 1][q];
78}
79
80enum class Phase { header, length, long_length, symbols };
81struct Lane {
82 Phase phase{Phase::header};
83 const Model* model{};
84 uint32_t state{};
85 int bytes{}, byte{}, zeros{}, symbol{}, slot{};
86 size_t remaining{};
87 bool flush{}, extended{}, buffered{}, held{}, full{}, done{};
88
89 bool step(int width, span<const uint8_t> &input, int &budget, bool fast) {
90 if (done || (held && full))
91 return true;
92 if (!buffered && (phase != Phase::symbols || bytes)) {
93 if (!budget)
94 return true;
95 if (input.empty())
96 return false;
97 --budget;
98 byte = input.front();
99 input = input.subspan(1);
100 buffered = true;
101 }
102 if (held) {
103 state = model->advance(state, symbol);
104 slot = symbol;
105 full = true;
106 held = false;
107 }
108 if (phase == Phase::header) {
109 model = &model_for(width - (byte >> 6), byte & 15);
110 flush = (byte & 16) != 0;
111 extended = (byte & 32) != 0;
112 zeros = 0;
113 buffered = false;
114 phase = Phase::length;
115 } else if (phase == Phase::length || phase == Phase::long_length) {
116 if (phase == Phase::long_length) {
117 bytes += (byte << 8) + 257;
118 phase = Phase::symbols;
119 } else {
120 bytes = byte + !extended;
121 phase = extended ? Phase::long_length : Phase::symbols;
122 }
123 buffered = false;
124 } else {
125 if (state < home) {
126 if (bytes) {
127 state = (state << 8) | byte;
128 buffered = false;
129 --bytes;
130 } else if (!flush) {
131 done = remaining == 0;
132 phase = Phase::header;
133 return true;
134 }
135 if (!bytes && flush && state < home) {
136 if (!state || zeros >= 8)
137 return false;
138 while (state < home && zeros < 8) {
139 state <<= 8;
140 ++zeros;
141 }
142 }
143 }
144 held = state > home && remaining;
145 if (held) {
146 symbol = model->symbol(state, fast);
147 if (symbol >= (1 << model->width))
148 return false;
149 --remaining;
150 }
151 if (!bytes && flush && state == home) {
152 done = !remaining;
153 phase = Phase::header;
154 state = 0;
155 } else if (!bytes && !flush && !remaining && !held) {
156 done = true;
157 } else if (state == home || (state > home && !held)) {
158 return false;
159 }
160 }
161 return true;
162 }
163};
164
165inline int unzig(int value) { return value & 1 ? -((value + 1) >> 1) : value >> 1; }
166inline int interleave(int a, int b, int bits = 4) {
167 int v = 0;
168 for (int k = 0; k < bits; ++k)
169 v |= (((a >> k) & 1) | (((b >> k) & 1) << 1)) << (2 * k);
170 return v;
171}
172
173struct BC7Format {
174 int subsets, color, alpha, pbits, index, partition;
175};
176constexpr BC7Format bc7_formats[] = {{3, 4, 0, 1, 3, 4}, {2, 6, 0, 2, 3, 6},
177 {3, 5, 0, 0, 2, 6}, {2, 7, 0, 1, 2, 6},
178 {1, 5, 6, 0, 3, 0}, {1, 7, 8, 0, 2, 0},
179 {1, 7, 7, 1, 4, 0}, {2, 5, 5, 1, 2, 6}};
180
181
182template<class Block>
183bool visit_blocks(unsigned codec, span<const uint8_t> source, size_t size, Block block, bool fast = true) {
184 const bool single = codec == 1 || codec == 4;
185 const unsigned count = codec >= 6 ? 16 : 12;
186 const size_t elements = size / 8;
187 array<Lane, 16> lanes{};
188 for (auto &lane : lanes) lane.remaining = elements;
189 Symbols first{};
190 size_t emitted = 0;
191 while (elements) {
192 bool ready = true, done = true;
193 int budget = 8;
194 for (unsigned l = 0; l < count; ++l) {
195 auto &lane = lanes[l];
196 if (!lane.step(widths[codec][l], source, budget, fast))
197 return false;
198 ready &= lane.full;
199 done &= lane.done;
200 }
201 if (ready) {
202 if (emitted == elements)
203 return false;
204 Symbols tuple{};
205 for (unsigned l = 0; l < count; ++l) {
206 tuple[l] = uint16_t(lanes[l].slot);
207 lanes[l].full = false;
208 }
209 if (single || emitted % 2) {
210 const size_t i = single ? emitted : emitted / 2;
211 if (!block(i, single ? tuple : first, tuple)) return false;
212 } else {
213 first = tuple;
214 }
215 ++emitted;
216 }
217 if (done)
218 break;
219 }
220 return emitted == elements && ranges::all_of(source, [](uint8_t v) { return v == 0; });
221}
222} // namespace leonida::bcpack::detail