forge
Explore
Log in

Don't you dare go Hollow

Bulk-read CABAC renorm and zero-fill bit supply

Browse files at this commit

f3d387e36379e52e60e57ae849f343cd4f8edfbcAhmet Kaan GümüşParent: 57ad7eb
  • perf
    • bulk-read CABAC renorm and zero-fill bit supply
Show full message
perf(h264): bulk-read CABAC renorm and zero-fill bit supply

RenormD doubles codIRange by leading_zeros-23 positions in one shift and
supplies the matching bits with one bulk read, replacing the per-bit
loop; BitCursor::bits_zero_fill is the engine's supply contract made
explicit (zero-filled past the RBSP, position accounting unchanged), so
bit() drops its Result plumbing. The coeff_abs_level prefix loop hoists
the inc0/inc1 context spelling out of the per-bin branch.

Byte-exact A/B vs d27a904d on the five fixture classes: all
display-order frame hashes identical. ec-h264 lib suite green.
crates/ec-h264/src/bits.rsmodified+37 -0
@@ -132,6 +132,43 @@ impl<'a> BitCursor<'a> {
132132 Ok(v)
133133 }
134134
135+ /// Read `n` bits (`n <= 32`) MSB-first, zero-filled past the end of the
136+ /// stream — the CABAC engine's supply contract (see the module note in
137+ /// `cabac.rs`): a conformant stream never depends on those bits, and the
138+ /// phantom reads do not advance `bit_position` beyond the real ones.
139+ #[inline]
140+ pub fn bits_zero_fill(&mut self, n: u32) -> u32 {
141+ debug_assert!(n <= 32);
142+ if n == 0 {
143+ return 0;
144+ }
145+ if self.cached < n {
146+ self.refill();
147+ }
148+ if self.cached >= n {
149+ let v = (self.cache >> (64 - n)) as u32;
150+ self.cache <<= n;
151+ self.cached -= n;
152+ return v;
153+ }
154+ // Genuinely past the end: consume what is cached, zero-fill the rest.
155+ let have = self.cached;
156+ let v = if have == 0 {
157+ 0
158+ } else {
159+ ((self.cache >> (64 - have)) as u32) << (n - have)
160+ };
161+ self.cache <<= have;
162+ self.cached = 0;
163+ v
164+ }
165+
166+ /// One bit, zero-filled past the end (see [`bits_zero_fill`]).
167+ #[inline]
168+ pub fn bit_zero_fill(&mut self) -> u32 {
169+ self.bits_zero_fill(1)
170+ }
171+
135172 /// Count leading zero bits up to and including the terminating one bit
136173 /// (level_prefix / exp-Golomb prefix). Capped at `cap` zeros.
137174 #[inline]
crates/ec-h264/src/cabac.rsmodified+16 -11
@@ -188,16 +188,20 @@ impl<'a> Cabac<'a> {
188188 /// One bit of slice data; zero past the end (see the module note).
189189 #[inline]
190190 fn bit(&mut self) -> u32 {
191- u32::from(self.r.read_bit().unwrap_or(false))
191+ self.r.bit_zero_fill()
192192 }
193193
194194 /// 9.3.3.2.2 RenormD.
195+ ///
196+ /// codIRange is at most 510, so the number of doublings the loop form
197+ /// would take is `leading_zeros - 23` — zero when no renorm is due. One
198+ /// bulk read replaces the per-bit supply, and the position accounting of
199+ /// the zero-filled read matches the loop's bit-by-bit one exactly.
195200 #[inline]
196201 fn renorm(&mut self) {
197- while self.range < 256 {
198- self.range <<= 1;
199- self.offset = (self.offset << 1) | self.bit();
200- }
202+ let shifts = self.range.leading_zeros() - 23;
203+ self.range <<= shifts;
204+ self.offset = (self.offset << shifts) | self.r.bits_zero_fill(shifts);
201205 }
202206
203207 /// 9.3.3.2.1 DecodeDecision, with the state transition of 9.3.3.2.1.1.
@@ -787,14 +791,15 @@ impl<'a> Cabac<'a> {
787791 4.min(1 + num_eq1) as usize
788792 };
789793 let inc1 = 5 + (4 - u32::from(chroma_dc)).min(num_gt1) as usize;
790- // Prefix: TU with cMax = uCoff = 14.
794+ // Prefix: TU with cMax = uCoff = 14. The first bin reads the
795+ // inc0 context, every later one the inc1 context, so the two
796+ // spellings are hoisted out of the loop.
791797 let mut prefix = 0u32;
792- while prefix < 14 {
793- let ctx_idx = abs_base + if prefix == 0 { inc0 } else { inc1 };
794- if !self.decision(ctx_idx) {
795- break;
798+ if self.decision(abs_base + inc0) {
799+ prefix = 1;
800+ while prefix < 14 && self.decision(abs_base + inc1) {
801+ prefix += 1;
796802 }
797- prefix += 1;
798803 }
799804 let abs_minus1 = if prefix < 14 {
800805 prefix