code wiki / (root) / nx_rangecoder.nx

nx_rangecoder.nx

buildroot/runtime/nx_rangecoder.nx

14107 B232 linesdepth 2pulls 2 transitivereach 90 importersview sourcekind library
docsdependenciesstructsconstsfunctions

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

nx_ventropy.nx nx_rangecoder.nx nx_rangecoder_gain_gate.nx nx_rangecoder_sig.nx nx_vcodec.nx nx_vcodec_sigcoder_gate.nx

imports: nx_ventropy.nx

imported by: nx_rangecoder_gain_gate.nxnx_rangecoder_sig.nxnx_vcodec.nxnx_vcodec_sigcoder_gate.nx

structs

none

consts

11const RC_MAGIC_4096: i64 = 4096
12const RC_MAGIC_4095: i64 = 4095
13const RC_NCTX: i64 = 11 // contexts used by rc_block_encode/decode (probs array must be >= this, seeded 2048)
14const RC_NCTX8: i64 = 24 // total contexts when 8x8 blocks are in the stream (11 for 4x4 + 13 for 8x8, below)

functions

21func rc_enc_init(st: *i64) -> i64 { st[0]=0; st[1]=0xFFFFFFFF; st[2]=0; st[3]=1; st[4]=0; return 0 }
23func rc_shift_low(st: *i64, out: *u8) -> i64
41func rc_enc_bit(st: *i64, out: *u8, p: i64, bit: i64) -> i64
47func 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] }
50func rc_dec_init(st: *i64, in_: *u8) -> i64
55func rc_dec_bit(st: *i64, in_: *u8, p: i64) -> i64
64func rc_p_update(p: i64, bit: i64) -> i64 { if bit == 0 { return p + ((RC_MAGIC_4096 - p) >> 5) } return p - (p >> 5) }
69func rc_bits_q8(p: i64, bit: i64) -> i64
82func rc_enc_ctx(st: *i64, out: *u8, probs: *i64, ctx: i64, bit: i64) -> i64
84func rc_dec_ctx(st: *i64, in_: *u8, probs: *i64, ctx: i64) -> i64
91func rc_block_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64) -> i64
106func rc_block_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64) -> i64
125func rc_block64_encode(coeffs: *i64, st: *i64, out: *u8, probs: *i64, zz8: *i64) -> i64
140func rc_block64_decode(coeffs: *i64, st: *i64, in_: *u8, probs: *i64, zz8: *i64) -> i64
called by 1: vc_dec_sub8 calls 2: rc_dec_ctxve_zzd
174func 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
175func rc_p_update_adv(p: i64, bit: i64, n: i64, cfg: *i64) -> i64
193func rc_enc_ctx_adv(st: *i64, out: *u8, probs: *i64, cnt: *i64, ctx: i64, bit: i64, cfg: *i64) -> i64
198func rc_dec_ctx_adv(st: *i64, in_: *u8, probs: *i64, cnt: *i64, ctx: i64, cfg: *i64) -> i64
205func rc_block64_encode_adv(coeffs: *i64, st: *i64, out: *u8, probs: *i64, cnt: *i64, zz8: *i64, cfg: *i64) -> i64
220func rc_block64_decode_adv(coeffs: *i64, st: *i64, in_: *u8, probs: *i64, cnt: *i64, zz8: *i64, cfg: *i64) -> i64