cabac: Ensure 2-byte cabac loads are on 2-byte boundry
authorJohn Cox <jc@kynesim.co.uk>
Wed, 20 Jan 2016 17:48:30 +0000 (17:48 +0000)
committerMichael Niedermayer <michael@niedermayer.cc>
Fri, 22 Jan 2016 01:31:32 +0000 (02:31 +0100)
Ensure that cabac init sets the bitstream pointer to an even value.
It is often faster to load from an aligned boundry

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
libavcodec/cabac.c

index 5bf5bc2..f2c239d 100644 (file)
@@ -183,10 +183,19 @@ int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
 #if CABAC_BITS == 16
     c->low =  (*c->bytestream++)<<18;
     c->low+=  (*c->bytestream++)<<10;
+    // Keep our fetches on a 2-byte boundry as this should avoid ever having to
+    // do unaligned loads if the compiler (or asm) optimises the double byte
+    // load into a single instruction
+    if(((uintptr_t)c->bytestream & 1) == 0) {
+        c->low += (1 << 9);
+    }
+    else {
+        c->low += ((*c->bytestream++) << 2) + 2;
+    }
 #else
     c->low =  (*c->bytestream++)<<10;
-#endif
     c->low+= ((*c->bytestream++)<<2) + 2;
+#endif
     c->range= 0x1FE;
     if ((c->range<<(CABAC_BITS+1)) < c->low)
         return AVERROR_INVALIDDATA;