changeset 192:1e5f64be86fc libavcodec

another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
author uid46427
date Thu, 10 Jan 2002 00:56:05 +0000
parents 883f184537e6
children b691dd3e9088
files common.c common.h mpegaudiodec.c
diffstat 3 files changed, 94 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/common.c	Thu Jan 10 00:53:21 2002 +0000
+++ b/common.c	Thu Jan 10 00:56:05 2002 +0000
@@ -15,6 +15,8 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * alternative bitstream reader by Michael Niedermayer <michaelni@gmx.at>
  */
 #include "common.h"
 #include <math.h>
@@ -174,6 +176,10 @@
 void init_get_bits(GetBitContext *s, 
                    UINT8 *buffer, int buffer_size)
 {
+#ifdef ALT_BITSTREAM_READER
+    s->index=0;
+    s->buffer= buffer;
+#else
     s->buf = buffer;
     s->buf_ptr = buffer;
     s->buf_end = buffer + buffer_size;
@@ -184,8 +190,10 @@
         s->bit_buf |= (*s->buf_ptr++ << (24 - s->bit_cnt));
         s->bit_cnt += 8;
     }
+#endif
 }
 
+#ifndef ALT_BITSTREAM_READER
 /* n must be >= 1 and <= 32 */
 /* also true: n > s->bit_cnt */
 unsigned int get_bits_long(GetBitContext *s, int n)
@@ -241,15 +249,22 @@
     s->bit_cnt = bit_cnt;
     return val;
 }
+#endif
 
 void align_get_bits(GetBitContext *s)
 {
+#ifdef ALT_BITSTREAM_READER
+    s->index= (s->index + 7) & (~7); 
+#else
     int n;
     n = s->bit_cnt & 7;
     if (n > 0) {
         get_bits(s, n);
     }
+#endif
 }
+
+#ifndef ALT_BITSTREAM_READER
 /* This function is identical to get_bits_long(), the */
 /* only diference is that it doesn't touch the buffer */
 /* it is usefull to see the buffer.                   */
@@ -296,6 +311,7 @@
     
     return val;
 }
+#endif
 
 /* VLC decoding */
 
--- a/common.h	Thu Jan 10 00:53:21 2002 +0000
+++ b/common.h	Thu Jan 10 00:56:05 2002 +0000
@@ -8,6 +8,8 @@
 #define CONFIG_WIN32
 #endif
 
+//#define ALT_BITSTREAM_READER
+
 #ifdef HAVE_AV_CONFIG_H
 /* only include the following when compiling package */
 #include "../config.h"
@@ -124,6 +126,7 @@
 
 #endif /* !CONFIG_WIN32 */
 
+
 /* debug stuff */
 #ifdef HAVE_AV_CONFIG_H
 
@@ -180,9 +183,14 @@
 /* bit input */
 
 typedef struct GetBitContext {
+#ifdef ALT_BITSTREAM_READER
+    int index;
+    UINT8 *buffer;
+#else
     UINT32 bit_buf;
     int bit_cnt;
     UINT8 *buf, *buf_ptr, *buf_end;
+#endif
 } GetBitContext;
 
 typedef struct VLC {
@@ -195,10 +203,23 @@
 void init_get_bits(GetBitContext *s, 
                    UINT8 *buffer, int buffer_size);
 
+#ifndef ALT_BITSTREAM_READER
 unsigned int get_bits_long(GetBitContext *s, int n);
 unsigned int show_bits_long(GetBitContext *s, int n);
+#endif
 
 static inline unsigned int get_bits(GetBitContext *s, int n){
+#ifdef ALT_BITSTREAM_READER
+    int index= s->index;
+    uint32_t result= be2me_32( *(uint32_t *)(((uint8_t *)s->buffer)+(index>>3)) );
+
+    result<<= (index&0x07);
+    result>>= 32 - n;
+    index+= n;
+    s->index= index;
+    
+    return result;
+#else
     if(s->bit_cnt>=n){
         /* most common case here */
         unsigned int val = s->bit_buf >> (32 - n);
@@ -210,9 +231,21 @@
 	return val;
     }
     return get_bits_long(s,n);
+#endif
 }
 
 static inline unsigned int get_bits1(GetBitContext *s){
+#ifdef ALT_BITSTREAM_READER
+    int index= s->index;
+    uint32_t result= be2me_32( *(uint32_t *)(((uint8_t *)s->buffer)+(index>>3)) );
+
+    result<<= (index&0x07);
+    result>>= 32 - 1;
+    index++;
+    s->index= index;
+    
+    return result;
+#else
     if(s->bit_cnt>0){
         /* most common case here */
         unsigned int val = s->bit_buf >> 31;
@@ -224,6 +257,7 @@
 	return val;
     }
     return get_bits_long(s,1);
+#endif
 }
 
 /* This function is identical to get_bits(), the only */
@@ -231,15 +265,28 @@
 /* it is usefull to see the buffer.                   */
 static inline unsigned int show_bits(GetBitContext *s, int n)
 {
+#ifdef ALT_BITSTREAM_READER
+    int index= s->index;
+    uint32_t result= be2me_32( *(uint32_t *)(((uint8_t *)s->buffer)+(index>>3)) );
+
+    result<<= (index&0x07);
+    result>>= 32 - n;
+    
+    return result;
+#else
     if(s->bit_cnt>=n) {
         /* most common case here */
         unsigned int val = s->bit_buf >> (32 - n);
         return val;
     }
     return show_bits_long(s,n);
+#endif
 }
 
 static inline void skip_bits(GetBitContext *s, int n){
+#ifdef ALT_BITSTREAM_READER
+    s->index+= n;
+#else
     if(s->bit_cnt>=n){
         /* most common case here */
         s->bit_buf <<= n;
@@ -250,9 +297,13 @@
     } else {
 	get_bits_long(s,n);
     }
+#endif
 }
 
 static inline void skip_bits1(GetBitContext *s){
+#ifdef ALT_BITSTREAM_READER
+    s->index++;
+#else
     if(s->bit_cnt>0){
         /* most common case here */
         s->bit_buf <<= 1;
@@ -263,11 +314,16 @@
     } else {
 	get_bits_long(s,1);
     }
+#endif
 }
 
 static inline int get_bits_count(GetBitContext *s)
 {
+#ifdef ALT_BITSTREAM_READER
+    return s->index;
+#else
     return (s->buf_ptr - s->buf) * 8 - s->bit_cnt;
+#endif
 }
 
 void align_get_bits(GetBitContext *s);
@@ -277,6 +333,13 @@
 void free_vlc(VLC *vlc);
 int get_vlc(GetBitContext *s, VLC *vlc);
 
+#ifdef ALT_BITSTREAM_READER
+#define SHOW_BITS(s, val, n) val= show_bits(s, n);
+#define FLUSH_BITS(n) skip_bits(s, n); 
+#define SAVE_BITS(s) ;
+#define RESTORE_BITS(s) ;
+#else
+
 /* macro to go faster */
 /* n must be <= 24 */
 /* XXX: optimize buffer end test */
@@ -317,7 +380,7 @@
     (s)->bit_buf = bit_buf;\
     (s)->bit_cnt = bit_cnt;\
 }
-
+#endif // !ALT_BITSTREAM_READER
 /* define it to include statistics code (useful only for optimizing
    codec efficiency */
 //#define STATS
--- a/mpegaudiodec.c	Thu Jan 10 00:53:21 2002 +0000
+++ b/mpegaudiodec.c	Thu Jan 10 00:56:05 2002 +0000
@@ -1371,7 +1371,11 @@
     UINT8 *ptr;
 
     /* compute current position in stream */
+#ifdef ALT_BITSTREAM_READER
+    ptr = s->gb.buffer + (s->gb.index>>3);
+#else
     ptr = s->gb.buf_ptr - (s->gb.bit_cnt >> 3);
+#endif    
     /* copy old data before current one */
     ptr -= backstep;
     memcpy(ptr, s->inbuf1[s->inbuf_index ^ 1] + 
@@ -1528,15 +1532,25 @@
                 /* some encoders generate an incorrect size for this
                    part. We must go back into the data */
                 s_index -= 4;
+#ifdef ALT_BITSTREAM_READER
+                s->gb.buffer = last_buf_ptr;
+                s->gb.index = last_bit_cnt;
+#else
                 s->gb.buf_ptr = last_buf_ptr;
                 s->gb.bit_buf = last_bit_buf;
                 s->gb.bit_cnt = last_bit_cnt;
+#endif            
             }
             break;
         }
+#ifdef ALT_BITSTREAM_READER
+        last_buf_ptr = s->gb.buffer;
+        last_bit_cnt = s->gb.index;
+#else
         last_buf_ptr = s->gb.buf_ptr;
         last_bit_buf = s->gb.bit_buf;
         last_bit_cnt = s->gb.bit_cnt;
+#endif
         
         code = get_vlc(&s->gb, vlc);
         dprintf("t=%d code=%d\n", g->count1table_select, code);