h264: use 6-bit state_idx in CABAC decoder (this eliminates boundary check)
[nihav.git] / nihav-itu / src / codecs / h264 / cabac_coder.rs
index 453ebc9fcb1d140565efcddb3404f861ab3fe6f3..baa8caff030757b11561eb2869ba021e1fd91c48 100644 (file)
@@ -131,7 +131,7 @@ impl<'a> CABAC<'a> {
     }
     pub fn decode_bit(&mut self, idx: usize) -> bool {
         let mut val_mps = (self.states[idx] & 0x80) != 0;
-        let state_idx = (self.states[idx] & 0x7F) as usize;
+        let state_idx = (self.states[idx] & 0x3F) as usize;
         let range_idx = ((self.cod_range >> 6) & 3) as usize;
         let range_lps = u16::from(RANGE_TBL_LPS[range_idx + state_idx * 4]);
         self.cod_range -= range_lps;
@@ -143,13 +143,13 @@ impl<'a> CABAC<'a> {
                 val_mps
             };
         self.states[idx] = if bit == val_mps {
-                TRANS_IDX_MPS[state_idx]
+                TRANS_IDX_MPS[state_idx] + (if val_mps { 0x80 } else { 0 })
             } else {
                 if state_idx == 0 {
                     val_mps = !val_mps;
                 }
-                TRANS_IDX_LPS[state_idx]
-            } + (if val_mps { 0x80 } else { 0 });
+                TRANS_IDX_LPS[state_idx] + (if val_mps { 0x80 } else { 0 })
+            };
         self.renorm();
         bit
     }
@@ -188,7 +188,7 @@ impl<'a> CABAC<'a> {
             }
             self.cod_range  <<= shift;
             self.cod_offset <<= shift;
-            self.cod_offset  |= u16::from(self.bitbuf >> (16 - shift));
+            self.cod_offset  |= self.bitbuf >> (16 - shift);
             self.bitbuf <<= shift;
             self.bits -= shift;
         }