changeset 3570:c03e0bb35c16

faster (and simpler) bitstream reader (in C)
author michael
date Mon, 17 Dec 2001 21:53:49 +0000
parents d14494d84c29
children ceaccd11b8ec
files liba52/bitstream.c liba52/bitstream.h
diffstat 2 files changed, 39 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/liba52/bitstream.c	Mon Dec 17 21:19:57 2001 +0000
+++ b/liba52/bitstream.c	Mon Dec 17 21:53:49 2001 +0000
@@ -31,7 +31,12 @@
 
 #define BUFFER_SIZE 4096
 
+#ifdef ALT_BITSTREAM_READER
+int indx=0;
+uint32_t * buffer_start;
+#else
 static uint32_t * buffer_start;
+#endif
 
 uint32_t bits_left;
 uint32_t current_word;
@@ -43,6 +48,9 @@
     align = (int)buf & 3;
     buffer_start = (uint32_t *) (buf - align);
     bits_left = 0;
+#ifdef ALT_BITSTREAM_READER
+    indx=0;
+#endif
     bitstream_get (align * 8);
 }
 
--- a/liba52/bitstream.h	Mon Dec 17 21:19:57 2001 +0000
+++ b/liba52/bitstream.h	Mon Dec 17 21:53:49 2001 +0000
@@ -21,6 +21,9 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+// alternative (faster) bitstram reader (reades upto 3 bytes over the end of the input)
+#define ALT_BITSTREAM_READER
+ 
 /* (stolen from the kernel) */
 #ifdef WORDS_BIGENDIAN
 
@@ -46,18 +49,33 @@
 #	endif
 #endif
 
+#ifdef ALT_BITSTREAM_READER
+extern uint32_t *buffer_start; 
+extern int indx;
+#else
 extern uint32_t bits_left;
 extern uint32_t current_word;
+#endif
 
 void bitstream_set_ptr (uint8_t * buf);
 uint32_t bitstream_get_bh(uint32_t num_bits);
 int32_t bitstream_get_bh_2(uint32_t num_bits);
 
+
 static inline uint32_t 
-bitstream_get(uint32_t num_bits)
+bitstream_get(uint32_t num_bits) // note num_bits is practically a constant due to inlineing
 {
+#ifdef ALT_BITSTREAM_READER
+    uint32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
+
+    result<<= (indx&0x07);
+    result>>= 32 - num_bits;
+    indx+= num_bits;
+    
+    return result;
+#else
     uint32_t result;
-	
+    
     if(num_bits < bits_left) {
 	result = (current_word << (32 - bits_left)) >> (32 - num_bits);
 	bits_left -= num_bits;
@@ -65,11 +83,21 @@
     }
 
     return bitstream_get_bh(num_bits);
+#endif
 }
 
 static inline int32_t 
 bitstream_get_2(uint32_t num_bits)
 {
+#ifdef ALT_BITSTREAM_READER
+    int32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
+
+    result<<= (indx&0x07);
+    result>>= 32 - num_bits;
+    indx+= num_bits;
+        
+    return result;
+#else
     int32_t result;
 	
     if(num_bits < bits_left) {
@@ -79,4 +107,5 @@
     }
 
     return bitstream_get_bh_2(num_bits);
+#endif
 }