code wiki / (root) / nx_vmvres.nx

nx_vmvres.nx source

↩ module page · 22 lines · 1138 B

1// nx_vmvres.nx -- efficient MV-RESIDUAL coding. After MV prediction (nx_vmvpred) most residual components are exactly 0 2// (neighbouring blocks share motion on a pan/head-pan), so paying the varint's fixed 5-bit length prefix on every 3// component is wasteful. Code instead: [1 bit nonzero-flag]; only if nonzero, the signed varint. A 0 residual costs 1 4// bit instead of 5 -> MVP's magnitude win finally shows up in the total bitrate. Composes nx_ventropy. license_tier: ORIGINAL 5import "nx_ventropy.nx" 6 7// write a signed MV-residual component into buf at bit bp; returns the new bit position. 8func mvr_put(buf: *u8, bp: i64, v: i64) -> i64 { 9 if v == 0 { return nx_bw_put(buf, bp, 0, 1) } 10 let p: i64 = nx_bw_put(buf, bp, 1, 1) 11 return ve_vput(buf, p, v) 12} 13// read a residual component back from buf at bit bp. 14func mvr_get(buf: *u8, bp: i64) -> i64 { 15 if nx_br_get(buf, bp, 1) == 0 { return 0 } 16 return ve_vval(buf, bp + 1) 17} 18// bit length of the component at bp (so the reader can advance). 19func mvr_len(buf: *u8, bp: i64) -> i64 { 20 if nx_br_get(buf, bp, 1) == 0 { return 1 } 21 return 1 + ve_vlen(buf, bp + 1) 22}