nx_rangecoder.nx
buildroot/runtime/nx_rangecoder.nx
about
nx_rangecoder.nx -- sovereign BINARY RANGE CODER (CABAC/VP8-class), the entropy-coding SOTA lever (task #31).
The current nx_ventropy is CAVLC (fixed 4-bit run + 5-bit level-length) -> wasteful when the real symbol
distribution is skewed (it almost always is). A range coder with ADAPTIVE per-context probabilities codes
each binary decision at its true entropy (a p=0.97 flag costs ~0.04 bit, not 1). Design = the LZMA range
coder (carry via a cache byte + a pending-0xFF counter -- exact, well-understood), 12-bit probabilities,
adaptive update toward the observed bit. Bit-exact by construction; a divergence shows in the roundtrip gate.
license_tier: ORIGINAL
dependencies 1 imports · 4 importers
imports: nx_ventropy.nx
imported by: nx_rangecoder_gain_gate.nxnx_rangecoder_sig.nxnx_vcodec.nxnx_vcodec_sigcoder_gate.nx
structs
| none |
consts
| 11 | const RC_MAGIC_4096: i64 = 4096 |
| 12 | const RC_MAGIC_4095: i64 = 4095 |
| 13 | const RC_NCTX: i64 = 11 // contexts used by rc_block_encode/decode (probs array must be >= this, seeded 2048) |
| 14 | const RC_NCTX8: i64 = 24 // total contexts when 8x8 blocks are in the stream (11 for 4x4 + 13 for 8x8, below) |
functions
| 21 | func rc_enc_init(st: *i64) -> i64 { st[0]=0; st[1]=0xFFFFFFFF; st[2]=0; st[3]=1; st[4]=0; return 0 } |
| 23 | func rc_shift_low(st: *i64, out: *u8) -> i64 |
| 41 | func rc_enc_bit(st: *i64, out: *u8, p: i64, bit: i64) -> i64 |
| 47 | func rc_enc_flush(st: *i64, out: *u8) -> i64 { var i: i64=0; while i < 5 { rc_shift_low(st, out); i=i+1 } return st[4] } |
| 50 | func rc_dec_init(st: *i64, in_: *u8) -> i64 |
| 55 | func rc_dec_bit(st: *i64, in_: *u8, p: i64) -> i64 |
| 64 | func rc_p_update(p: i64, bit: i64) -> i64 { if bit == 0 { return p + ((RC_MAGIC_4096 - p) >> 5) } return p - (p >> 5) } |
| 69 | func rc_bits_q8(p: i64, bit: i64) -> i64 |
| 82 | func rc_enc_ctx(st: *i64, out: *u8, probs: *i64, ctx: i64, bit: i64) -> i64 |
| 84 | func rc_dec_ctx(st: *i64, in_: *u8, probs: *i64, ctx: i64) -> i64 |
| 91 | func rc_block_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64) -> i64 |
| 106 | func rc_block_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64) -> i64 |
| 125 | func rc_block64_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64, zz8: *i64) -> i64 |
| 140 | func rc_block64_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64, zz8: *i64) -> i64 |
| 174 | func rc_ilog2(n: i64) -> i64 { var r: i64=0; var t: i64=n; while t > 1 { t = t >> 1; r = r + 1 } return r } called by 1: rc_p_update_adv |
| 175 | func rc_p_update_adv(p: i64, bit: i64, n: i64, cfg: *i64) -> i64 |
| 193 | func rc_enc_ctx_adv(st: *i64, out: *u8, probs: *i64, cnt: *i64, ctx: i64, bit: i64, cfg: *i64) -> i64 called by 3: rc_block64_encode_advrcs_enc_level_advrc_sig_encode_adv calls 2: rc_enc_bitrc_p_update_adv |
| 198 | func rc_dec_ctx_adv(st: *i64, in_: *u8, probs: *i64, cnt: *i64, ctx: i64, cfg: *i64) -> i64 called by 3: rc_block64_decode_advrcs_dec_level_advrc_sig_decode_adv calls 2: rc_dec_bitrc_p_update_adv |
| 205 | func rc_block64_encode_adv(coeffs: *i64, st: *i64, out: *u8, probs: *i64, cnt: *i64, zz8: *i64, cfg: *i64) -> i64 |
| 220 | func rc_block64_decode_adv(coeffs: *i64, st: *i64, in_: *u8, probs: *i64, cnt: *i64, zz8: *i64, cfg: *i64) -> i64 |