Files
AVA2/shared/gr2/src/bitknit2.cpp
T
shenleiandClaude Opus 5.5 9077aa389a AVA2 client Windows parity passes (gr2 BitKnit2/64-bit, GamePhase handlers, python modules)
- gr2: BitKnit2 decompression, 64-bit LE files, compressed curves
- net: remaining GamePhase handlers, generated GC packet table
- python: event/chat/chr/minimap/guild/quest/messenger/shop-search and
  other native modules ported against the AVA2 Windows client
- render: D3D8 fixed-function lighting in Gr2ModelRenderer, terrain
  without baked shade; cape/NPC equipment attachment
- tests + audits updated; docs/PORT-PLAN.md and WINDOWS-PARITY.md

Batch 4 (render pipeline: background.*, app.RenderGame/UpdateGame,
camera bindings, main.cpp fake-content removal) is still in progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:43:01 +09:00

257 lines
8.7 KiB
C++

// Granny BitKnit2 decoder. Provenance and framing: see bitknit2.h
// (BG3ModStudio Bitknit2Decompressor.cpp, MIT, (c) 2025 Thomas A. Rieck;
// cross-checked against powzix/ooz bitknit.cpp).
#include "bitknit2.h"
#include <cstring>
#include <memory>
namespace gr2 {
namespace {
constexpr uint16_t kMagic = 0x75B1;
constexpr size_t kQuantum = 65536;
// Adaptive frequency model: N symbols over a 15-bit cumulative range, with a
// coarse residue->symbol lookup (residue >> SHIFT) and a rebuild every 1024 symbols.
template <unsigned N, unsigned SHIFT, unsigned BOOST>
struct Model {
uint16_t lookup[(0x8000u >> SHIFT) + 4];
uint16_t cdf[N + 1];
uint16_t freq[N];
uint32_t adapt;
void build_lookup() {
uint16_t* p = lookup;
for (unsigned i = 0; i < N; ++i) {
// cdf[i+1] >= 1 always holds (initial table and the smoothed rebuild
// are both strictly increasing), so the index never goes negative.
uint16_t* end = &lookup[(unsigned(cdf[i + 1]) - 1) >> SHIFT];
do {
p[0] = p[1] = p[2] = p[3] = uint16_t(i);
p += 4;
} while (p <= end);
p = end + 1;
}
}
void init_uniform() {
for (unsigned i = 0; i <= N; ++i) cdf[i] = uint16_t(0x8000u * i / N);
reset();
}
void reset() {
for (auto& f : freq) f = 1;
adapt = 1024;
build_lookup();
}
void rebuild(unsigned sym) {
adapt = 1024;
freq[sym] = uint16_t(freq[sym] + BOOST);
uint32_t sum = 0;
for (unsigned i = 0; i < N; ++i) {
sum += freq[i];
freq[i] = 1;
cdf[i + 1] = uint16_t(cdf[i + 1] + ((sum - cdf[i + 1]) >> 1));
}
build_lookup();
}
unsigned decode(uint32_t& state) {
const uint32_t residue = state & 0x7FFFu;
unsigned sym = lookup[residue >> SHIFT];
while (sym < N - 1 && residue >= cdf[sym + 1]) ++sym;
const uint32_t lo = cdf[sym];
state = (state >> 15) * (uint32_t(cdf[sym + 1]) - lo) + (residue - lo);
freq[sym] = uint16_t(freq[sym] + 31);
if (--adapt == 0) rebuild(sym);
return sym;
}
};
using LiteralModel = Model<300, 6, 725>;
using DistLsbModel = Model<40, 9, 985>;
using DistBitsModel = Model<21, 9, 1004>;
struct Decoder {
const uint8_t* src;
const uint8_t* src_end;
uint8_t* dst_start;
uint8_t* dst;
uint8_t* dst_end;
uint32_t state1 = 0, state2 = 0;
uint32_t last_dist = 1;
uint32_t recent[8] = {1, 1, 1, 1, 1, 1, 1, 1};
uint32_t recent_mask = (7u << 21) | (6u << 18) | (5u << 15) | (4u << 12) |
(3u << 9) | (2u << 6) | (1u << 3) | 0u;
LiteralModel lit[4];
DistLsbModel lsb[4];
DistBitsModel bits;
bool read16(uint32_t& v) {
if (src_end - src < 2) return false;
v = uint32_t(src[0]) | (uint32_t(src[1]) << 8);
src += 2;
return true;
}
// Pull 16 bits into state1 if it dropped below the rANS floor, then swap
// the two interleaved states.
bool renorm() {
if (state1 < 0x10000) {
uint32_t w;
if (!read16(w)) return false;
state1 = (state1 << 16) | w;
}
const uint32_t t = state1;
state1 = state2;
state2 = t;
return true;
}
bool quantum_header() {
uint32_t lo, hi;
if (!read16(lo) || !read16(hi)) return false;
// The u32 is stored as [low16][high16]; the fields assume [high16][low16].
const uint32_t v = (lo << 16) | hi;
if (v < 0x10000) return false;
uint32_t a = v >> 4;
const uint32_t n = v & 0xF;
uint32_t w;
if (a < 0x10000) {
if (!read16(w)) return false;
a = (a << 16) | w;
}
uint32_t s1 = a >> n;
if (s1 < 0x10000) {
if (!read16(w)) return false;
s1 = (s1 << 16) | w;
}
if (!read16(w)) return false;
a = (a << 16) | w;
const uint32_t top = 1u << (n + 16);
state1 = s1;
state2 = top | (a & (top - 1));
return true;
}
bool decode_quantum(uint8_t* q_end) {
if (!quantum_header()) return false;
if (dst == dst_start) { // no previous byte to delta against
*dst++ = uint8_t(state1);
state1 >>= 8;
if (!renorm()) return false;
}
while (dst < q_end) {
unsigned sym = lit[size_t(dst - dst_start) & 3].decode(state1);
if (!renorm()) return false;
if (sym < 256) {
// last_dist <= bytes produced: it is 1 here only after the first
// byte, and every later value passed the match-distance check.
*dst = uint8_t(sym + dst[-ptrdiff_t(last_dist)]);
++dst;
if (dst >= q_end) break;
sym = lit[size_t(dst - dst_start) & 3].decode(state1);
if (!renorm()) return false;
if (sym < 256) {
*dst = uint8_t(sym + dst[-ptrdiff_t(last_dist)]);
++dst;
continue;
}
}
if (sym >= 288) { // long copy length: 1..12 extra bits
const unsigned nb = sym - 287;
sym = (state1 & ((1u << nb) - 1)) + (1u << nb) + 286;
state1 >>= nb;
if (!renorm()) return false;
}
const uint32_t copy_len = sym - 254;
const unsigned dsym = lsb[size_t(dst - dst_start) & 3].decode(state1);
if (!renorm()) return false;
uint32_t dist;
if (dsym >= 8) { // explicit distance
const unsigned nb = bits.decode(state1);
if (!renorm()) return false;
dist = state1 & ((1u << (nb & 0xF)) - 1);
state1 >>= (nb & 0xF);
if (!renorm()) return false;
if (nb >= 0x10) {
uint32_t w;
if (!read16(w)) return false;
dist = (dist << 16) | w;
}
dist = (32u << nb) + (dist << 5) + dsym - 39;
recent[(recent_mask >> 21) & 7] = recent[(recent_mask >> 18) & 7];
recent[(recent_mask >> 18) & 7] = dist;
} else { // recent-distance cache hit: move to front
const unsigned shift = 3 * dsym;
const uint32_t idx = (recent_mask >> shift) & 7;
const uint32_t mask = ~7u << shift;
dist = recent[idx];
recent_mask = (recent_mask & mask) | ((idx + 8 * recent_mask) & ~mask);
}
if (dist == 0 || dist > size_t(dst - dst_start)) return false;
if (copy_len > size_t(dst_end - dst)) return false;
const uint8_t* from = dst - dist;
for (uint32_t i = 0; i < copy_len; ++i) dst[i] = from[i]; // overlapping copy
dst += copy_len;
last_dist = dist;
}
// A well-formed quantum leaves both states at the rANS floor.
return state1 == 0x10000 && state2 == 0x10000;
}
bool run() {
uint32_t magic;
if (!read16(magic) || magic != kMagic) return false;
while (dst < dst_end) {
const size_t q_start = size_t(dst - dst_start) & ~(kQuantum - 1);
const size_t total = size_t(dst_end - dst_start);
uint8_t* q_end = dst_start + (total - q_start < kQuantum ? total : q_start + kQuantum);
if (src_end - src < 2) return false;
if (src[0] == 0 && src[1] == 0) { // stored quantum
src += 2;
const size_t n = size_t(q_end - dst);
if (size_t(src_end - src) < n) return false;
std::memcpy(dst, src, n);
dst += n;
src += n;
} else if (!decode_quantum(q_end)) {
return false;
}
}
return true;
}
};
} // namespace
bool bitknit2_decompress(const uint8_t* src, size_t src_size, uint8_t* dst, size_t dst_size) {
if (!src || !dst || dst_size == 0) return false;
auto d = std::make_unique<Decoder>();
d->src = src;
d->src_end = src + src_size;
d->dst_start = d->dst = dst;
d->dst_end = dst + dst_size;
for (auto& m : d->lit) {
// 264 regular symbols share the range; the 36 long-length symbols get one unit each.
for (unsigned i = 0; i < 264; ++i) m.cdf[i] = uint16_t((0x8000u - 300 + 264) * i / 264);
for (unsigned i = 264; i <= 300; ++i) m.cdf[i] = uint16_t(0x8000u - 300 + i);
m.reset();
}
for (auto& m : d->lsb) m.init_uniform();
d->bits.init_uniform();
return d->run();
}
} // namespace gr2