Bulk-read CABAC renorm and zero-fill bit supply
Browse files at this commitf3d387e36379e52e60e57ae849f343cd4f8edfbcParent: 57ad7eb- perf
- bulk-read CABAC renorm and zero-fill bit supply
2 changed files +53 -11
- crates/ec-h264/src/bits.rs +37 -0
- crates/ec-h264/src/cabac.rs +16 -11
crates/ec-h264/src/bits.rsmodified+37 -0
| @@ -132,6 +132,43 @@ impl<'a> BitCursor<'a> { | ||
| 132 | 132 | Ok(v) |
| 133 | 133 | } |
| 134 | 134 | |
| 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 | + | |
| 135 | 172 | /// Count leading zero bits up to and including the terminating one bit |
| 136 | 173 | /// (level_prefix / exp-Golomb prefix). Capped at `cap` zeros. |
| 137 | 174 | #[inline] |
crates/ec-h264/src/cabac.rsmodified+16 -11
| @@ -188,16 +188,20 @@ impl<'a> Cabac<'a> { | ||
| 188 | 188 | /// One bit of slice data; zero past the end (see the module note). |
| 189 | 189 | #[inline] |
| 190 | 190 | fn bit(&mut self) -> u32 { |
| 191 | - u32::from(self.r.read_bit().unwrap_or(false)) | |
| 191 | + self.r.bit_zero_fill() | |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /// 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. | |
| 195 | 200 | #[inline] |
| 196 | 201 | 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); | |
| 201 | 205 | } |
| 202 | 206 | |
| 203 | 207 | /// 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> { | ||
| 787 | 791 | 4.min(1 + num_eq1) as usize |
| 788 | 792 | }; |
| 789 | 793 | 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. | |
| 791 | 797 | 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; | |
| 796 | 802 | } |
| 797 | - prefix += 1; | |
| 798 | 803 | } |
| 799 | 804 | let abs_minus1 = if prefix < 14 { |
| 800 | 805 | prefix |