271 lines
11 KiB
C++
271 lines
11 KiB
C++
// Mask codec for SAM output: base85 (custom alphabet) + adaptive range coder.
|
|
// Ported from the official meta-sam parser (python/src/meta_sam_parser/
|
|
// _mask_codec.py); verified against its conformance cases in the tests.
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <array>
|
|
|
|
#include "llm/error.h"
|
|
#include "meta/caps.h"
|
|
#include "meta/sam.h"
|
|
|
|
namespace molo::meta {
|
|
|
|
namespace {
|
|
|
|
[[noreturn]] void bad(const std::string& why) {
|
|
throw llm::ApiError::invalid("meta", "sam mask: " + why);
|
|
}
|
|
|
|
constexpr int kRadix = 85;
|
|
constexpr int kPrefix = 5;
|
|
constexpr std::uint32_t kTop = 1u << 24;
|
|
constexpr std::uint32_t kBottom = 1u << 16;
|
|
|
|
int digit_of(char c) {
|
|
static int table[128];
|
|
static bool init = false;
|
|
if (!init) {
|
|
for (int& t : table) t = -1;
|
|
int idx = 0;
|
|
for (int code = 0x21; code < 0x7F; ++code) {
|
|
char ch = static_cast<char>(code);
|
|
if (ch == '"' || ch == '\\' || ch == ',' || ch == ';' || ch == '<' || ch == '>' || ch == '|') continue;
|
|
table[code] = idx++;
|
|
}
|
|
init = true;
|
|
}
|
|
unsigned char u = static_cast<unsigned char>(c);
|
|
int v = u < 128 ? table[u] : -1;
|
|
if (v < 0 || v >= kRadix) bad("payload has an invalid character");
|
|
return v;
|
|
}
|
|
|
|
std::vector<std::uint8_t> unpack(const std::string& p) {
|
|
if (p.size() < kPrefix) bad("payload is missing its length prefix");
|
|
std::uint64_t length = 0;
|
|
for (int i = 0; i < kPrefix; ++i) length = length * kRadix + digit_of(p[i]);
|
|
if (length > MetaCaps::kMaxMaskPixels * 4ull) bad("payload length is unsupported");
|
|
std::uint64_t rem = length % 4;
|
|
std::uint64_t expected = kPrefix + (length / 4) * 5 + (rem == 0 ? 0 : rem + 1);
|
|
if (p.size() != expected) bad("payload length does not match its prefix");
|
|
std::vector<std::uint8_t> out(length);
|
|
std::size_t src = kPrefix, dst = 0;
|
|
std::uint64_t full = length - rem;
|
|
while (dst < full) {
|
|
std::uint64_t v = 0;
|
|
for (int i = 0; i < 5; ++i) v = v * kRadix + digit_of(p[src + i]);
|
|
if (v > 0xFFFFFFFFull) bad("payload group is out of range");
|
|
src += 5;
|
|
out[dst] = (v >> 24) & 0xFF; out[dst + 1] = (v >> 16) & 0xFF;
|
|
out[dst + 2] = (v >> 8) & 0xFF; out[dst + 3] = v & 0xFF;
|
|
dst += 4;
|
|
}
|
|
if (rem) {
|
|
std::uint64_t v = 0;
|
|
for (std::uint64_t i = 0; i < 5; ++i) v = v * kRadix + (i < rem + 1 ? digit_of(p[src + i]) : kRadix - 1);
|
|
if (v > 0xFFFFFFFFull) bad("payload tail is out of range");
|
|
for (std::uint64_t i = 0; i < rem; ++i) out[dst + i] = (v >> (24 - i * 8)) & 0xFF;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
class RangeDecoder {
|
|
public:
|
|
RangeDecoder(const std::uint8_t* d, std::size_t n) : in_(d), n_(n) {
|
|
if (n < 4) bad("payload is missing its finalization");
|
|
for (int i = 0; i < 4; ++i) code_ = (code_ << 8) | read();
|
|
}
|
|
std::uint32_t freq(std::uint32_t total) {
|
|
scaled_ = range_ / total;
|
|
if (scaled_ == 0) bad("invalid coding state");
|
|
std::uint32_t v = (code_ - low_) / scaled_;
|
|
return v >= total ? total - 1 : v;
|
|
}
|
|
void decode(std::uint32_t cum, std::uint32_t f) {
|
|
low_ += scaled_ * cum;
|
|
range_ = scaled_ * f;
|
|
while (true) {
|
|
if ((low_ ^ (low_ + range_)) < kTop) {
|
|
} else if (range_ < kBottom) {
|
|
range_ = (0u - low_) & (kBottom - 1);
|
|
} else {
|
|
break;
|
|
}
|
|
code_ = (code_ << 8) | read();
|
|
low_ <<= 8;
|
|
range_ <<= 8;
|
|
}
|
|
}
|
|
|
|
private:
|
|
std::uint32_t read() {
|
|
if (pos_ >= n_) bad("payload ended before decoding completed");
|
|
return in_[pos_++];
|
|
}
|
|
const std::uint8_t* in_;
|
|
std::size_t n_, pos_ = 0;
|
|
std::uint32_t low_ = 0, range_ = 0xFFFFFFFFu, code_ = 0, scaled_ = 0;
|
|
};
|
|
|
|
std::vector<std::uint8_t> decode_one_bit(const std::string& payload, int width, int height) {
|
|
std::vector<std::uint8_t> raw = unpack(payload.substr(1));
|
|
RangeDecoder dec(raw.data(), raw.size());
|
|
std::vector<std::uint16_t> counts(4096 * 2, 1);
|
|
const int pw = width + 4;
|
|
std::vector<std::uint8_t> padded(static_cast<std::size_t>(pw) * (height + 2), 0);
|
|
std::vector<std::uint8_t> out(static_cast<std::size_t>(width) * height);
|
|
for (int y = 0; y < height; ++y) {
|
|
std::size_t row = static_cast<std::size_t>(y + 2) * pw + 2;
|
|
for (int x = 0; x < width; ++x) {
|
|
std::size_t pos = row + x, prior = pos - pw, earlier = pos - 2 * pw;
|
|
unsigned ctx = padded[pos - 1] | (padded[pos - 2] << 1) | (padded[prior - 2] << 2) |
|
|
(padded[prior - 1] << 3) | (padded[prior] << 4) | (padded[prior + 1] << 5) |
|
|
(padded[prior + 2] << 6) | (padded[earlier - 2] << 7) |
|
|
(padded[earlier - 1] << 8) | (padded[earlier] << 9) |
|
|
(padded[earlier + 1] << 10) | (padded[earlier + 2] << 11);
|
|
std::size_t off = static_cast<std::size_t>(ctx) << 1;
|
|
std::uint32_t zero = counts[off], one = counts[off + 1];
|
|
std::uint32_t v = dec.freq(zero + one);
|
|
int bit = v < zero ? 0 : 1;
|
|
if (bit == 0) dec.decode(0, zero); else dec.decode(zero, one);
|
|
padded[pos] = static_cast<std::uint8_t>(bit);
|
|
out[static_cast<std::size_t>(y) * width + x] = static_cast<std::uint8_t>(bit);
|
|
counts[off + bit] = static_cast<std::uint16_t>(counts[off + bit] + 14);
|
|
if (counts[off] + counts[off + 1] >= 4096) {
|
|
counts[off] = static_cast<std::uint16_t>((counts[off] >> 1) ? (counts[off] >> 1) : 1);
|
|
counts[off + 1] = static_cast<std::uint16_t>((counts[off + 1] >> 1) ? (counts[off + 1] >> 1) : 1);
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
int paeth(int l, int u, int ul) {
|
|
int est = l + u - ul;
|
|
int dl = std::abs(est - l), du = std::abs(est - u), dul = std::abs(est - ul);
|
|
if (dl <= du && dl <= dul) return l;
|
|
return du <= dul ? u : ul;
|
|
}
|
|
|
|
int predict(const std::vector<std::uint8_t>& out, std::size_t off, int x, int y, int mode, int w) {
|
|
if (mode < 0 || mode >= 5) bad("invalid predictor");
|
|
int left = x > 0 ? out[off - 1] : 0;
|
|
int up = y > 0 ? out[off - w] : 0;
|
|
if (mode == 1) return up;
|
|
if (mode == 2) return left;
|
|
if (mode == 3) return (left + up) >> 1;
|
|
int ul = (x > 0 && y > 0) ? out[off - w - 1] : 0;
|
|
if (mode == 0) return paeth(left, up, ul);
|
|
if (ul >= std::max(left, up)) return std::min(left, up);
|
|
if (ul <= std::min(left, up)) return std::max(left, up);
|
|
return left + up - ul;
|
|
}
|
|
|
|
int zero_ctx(const std::vector<std::uint8_t>& r, std::size_t off, int x, int y, int w) {
|
|
int left = x > 0 ? (r[off - 1] == 0) : 1;
|
|
int up = y > 0 ? (r[off - w] == 0) : 1;
|
|
int ul = (x > 0 && y > 0) ? (r[off - w - 1] == 0) : 1;
|
|
int ur = (y > 0 && x < w - 1) ? (r[off - w + 1] == 0) : 1;
|
|
return left | (up << 1) | (ul << 2) | (ur << 3);
|
|
}
|
|
|
|
std::vector<std::uint8_t> decode_lossless(const std::string& payload, int width, int height) {
|
|
std::vector<std::uint8_t> packed = unpack(payload.substr(1));
|
|
if (packed.size() < 5) bad("lossless payload is truncated");
|
|
int selector = packed[0];
|
|
RangeDecoder dec(packed.data() + 1, packed.size() - 1);
|
|
const std::size_t length = static_cast<std::size_t>(width) * height;
|
|
std::vector<std::uint8_t> out(length), resid(length);
|
|
std::vector<std::uint32_t> zero_counts(32, 1);
|
|
std::vector<std::uint16_t> order_one(256 * 256, 1);
|
|
std::vector<std::int32_t> order_one_totals(256, 256);
|
|
std::vector<std::uint16_t> order_zero(256, 2);
|
|
std::int64_t order_zero_total = 512;
|
|
std::map<int, std::array<std::uint16_t, 257>> order_two;
|
|
int previous = 0;
|
|
for (std::size_t off = 0; off < length; ++off) {
|
|
int x = static_cast<int>(off % width), y = static_cast<int>(off / width);
|
|
std::size_t zo = static_cast<std::size_t>(zero_ctx(resid, off, x, y, width)) << 1;
|
|
std::uint32_t zero = zero_counts[zo], nonzero = zero_counts[zo + 1];
|
|
std::uint32_t zf = dec.freq(zero + nonzero);
|
|
int bit = zf < zero ? 0 : 1;
|
|
dec.decode(bit == 0 ? 0 : zero, bit == 0 ? zero : nonzero);
|
|
zero_counts[zo + bit] += 32;
|
|
if (zero_counts[zo] + zero_counts[zo + 1] >= 16384) {
|
|
zero_counts[zo] = (zero_counts[zo] >> 1) ? (zero_counts[zo] >> 1) : 1;
|
|
zero_counts[zo + 1] = (zero_counts[zo + 1] >> 1) ? (zero_counts[zo + 1] >> 1) : 1;
|
|
}
|
|
int symbol = 0;
|
|
if (bit == 1) {
|
|
int above = off >= static_cast<std::size_t>(width) ? resid[off - width] : 0;
|
|
auto it = order_two.find((previous << 8) | above);
|
|
if (it == order_two.end()) {
|
|
std::array<std::uint16_t, 257> z{};
|
|
it = order_two.emplace((previous << 8) | above, z).first;
|
|
}
|
|
auto& second = it->second;
|
|
std::size_t fo = static_cast<std::size_t>(previous) << 8;
|
|
std::int64_t excluded = second[0] + order_one[fo] + order_zero[0];
|
|
std::int64_t total = second[256] + order_one_totals[previous] + order_zero_total - excluded;
|
|
if (total <= 0) bad("lossless payload is malformed");
|
|
std::uint32_t target = dec.freq(static_cast<std::uint32_t>(total));
|
|
std::int64_t cum = 0;
|
|
symbol = 1;
|
|
std::int64_t f = second[1] + order_one[fo + 1] + order_zero[1];
|
|
while (cum + f <= target) {
|
|
cum += f;
|
|
++symbol;
|
|
if (symbol >= 256) bad("lossless payload is malformed");
|
|
f = second[symbol] + order_one[fo + symbol] + order_zero[symbol];
|
|
}
|
|
dec.decode(static_cast<std::uint32_t>(cum), static_cast<std::uint32_t>(f));
|
|
second[symbol] = static_cast<std::uint16_t>(second[symbol] + 14 * 44);
|
|
second[256] = static_cast<std::uint16_t>(second[256] + 14 * 44);
|
|
if (second[256] >= 14 * 3584) {
|
|
std::uint32_t t = 0;
|
|
for (int v = 0; v < 256; ++v) { second[v] >>= 1; t += second[v]; }
|
|
second[256] = static_cast<std::uint16_t>(t);
|
|
}
|
|
order_one[fo + symbol] = static_cast<std::uint16_t>(order_one[fo + symbol] + 56);
|
|
order_one_totals[previous] += 56;
|
|
if (order_one_totals[previous] >= 8192) {
|
|
std::int32_t t = 0;
|
|
for (int v = 0; v < 256; ++v) {
|
|
std::uint16_t s = static_cast<std::uint16_t>((order_one[fo + v] >> 1) ? (order_one[fo + v] >> 1) : 1);
|
|
order_one[fo + v] = s;
|
|
t += s;
|
|
}
|
|
order_one_totals[previous] = t;
|
|
}
|
|
order_zero[symbol] = static_cast<std::uint16_t>(order_zero[symbol] + 32);
|
|
order_zero_total += 32;
|
|
if (order_zero_total >= 4096) {
|
|
std::int64_t t = 0;
|
|
for (int v = 0; v < 256; ++v) { order_zero[v] >>= 1; t += order_zero[v]; }
|
|
order_zero_total = t;
|
|
}
|
|
}
|
|
resid[off] = static_cast<std::uint8_t>(symbol);
|
|
int delta = (symbol & 1) ? -((symbol + 1) >> 1) : (symbol >> 1);
|
|
out[off] = static_cast<std::uint8_t>((predict(out, off, x, y, selector, width) + delta) & 0xFF);
|
|
previous = symbol;
|
|
}
|
|
for (std::uint8_t& v : out) v = v >= 129 ? 1 : 0;
|
|
return out;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::vector<std::uint8_t> SamMask::decode() const {
|
|
if (width < 1 || height < 1 ||
|
|
static_cast<std::size_t>(width) * static_cast<std::size_t>(height) > MetaCaps::kMaxMaskPixels)
|
|
bad("mask dimensions are out of range");
|
|
if (payload.empty() || payload[0] != encoding) bad("payload does not start with its marker");
|
|
if (encoding == '!') return decode_one_bit(payload, width, height);
|
|
if (encoding == '~') return decode_lossless(payload, width, height);
|
|
bad("unsupported encoding");
|
|
}
|
|
|
|
} // namespace molo::meta
|