"""Chunk-local dictionary ANS coding. Each chunk builds its own frequency histogram and uses it to drive zstd. Scenes have very different token distributions, so adapting per chunk improves compression significantly. Tokens are 18-bit packed before zstd to avoid waste. """ import struct import zlib from collections import Counter from mediatok.entropy.pack import pack_tokens, unpack_tokens def ans_encode(tokens: list[int]) -> bytes: """Encode tokens using chunk-local frequency table + 18-bit pack + zstd.""" if not tokens: return b"" freq = Counter(tokens) meta = struct.pack(" list[int]: if not payload: return [] hlen = struct.unpack_from("