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 · 5 importers
imports: nx_ventropy.nx
imported by: nx_rangecoder_gain_gate.nxnx_rangecoder_sig.nxnx_vcodec.nxnx_vcodec_ffv1_entropy_gate.nxnx_vcodec_sigcoder_gate.nx
structs
| none |
consts
| 10 | const RC_MAGIC_4096: i64 = 4096 |
| 11 | const RC_MAGIC_4095: i64 = 4095 |
| 12 | const RC_NCTX: i64 = 11 // contexts used by rc_block_encode/decode (probs array must be >= this, seeded 2048) |
| 13 | const RC_NCTX8: i64 = 24 // total contexts when 8x8 blocks are in the stream (11 for 4x4 + 13 for 8x8, below) |
functions
| 20 | func rc_enc_init(st: *i64) -> i64 { st[0]=0; st[1]=0xFFFFFFFF; st[2]=0; st[3]=1; st[4]=0; return 0 } |
| 22 | func rc_shift_low(st: *i64, out: *u8) -> i64 |
| 40 | func rc_enc_bit(st: *i64, out: *u8, p: i64, bit: i64) -> i64 |
| 46 | 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] } |
| 49 | func rc_dec_init(st: *i64, in_: *u8) -> i64 |
| 54 | func rc_dec_bit(st: *i64, in_: *u8, p: i64) -> i64 called by 1: rc_dec_ctx |
| 63 | func rc_p_update(p: i64, bit: i64) -> i64 { if bit == 0 { return p + ((RC_MAGIC_4096 - p) >> 5) } return p - (p >> 5) } |
| 68 | func rc_bits_q8(p: i64, bit: i64) -> i64 |
| 81 | func rc_enc_ctx(st: *i64, out: *u8, probs: *i64, ctx: i64, bit: i64) -> i64 |
| 83 | func rc_dec_ctx(st: *i64, in_: *u8, probs: *i64, ctx: i64) -> i64 |
| 90 | func rc_block_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64) -> i64 |
| 105 | func rc_block_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64) -> i64 |
| 124 | func rc_block64_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64, zz8: *i64) -> i64 |
| 139 | func rc_block64_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64, zz8: *i64) -> i64 |