diff common.h @ 1875:45a1592dadca libavcodec

* moving some of the commonly used bit reading/writing functions from common.c -> common.h so that they can be inlined. + performace gain ~1% (measured with DV decoding) + code bloat 0.05% Looks like a win-win solution.
author romansh
date Fri, 12 Mar 2004 23:39:38 +0000
parents 3054613980a8
children fbcf02596520
line wrap: on
line diff
--- a/common.h	Thu Mar 11 13:22:39 2004 +0000
+++ b/common.h	Fri Mar 12 23:39:38 2004 +0000
@@ -292,11 +292,55 @@
 #endif
 } PutBitContext;
 
-void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size);
+static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
+{
+    s->buf = buffer;
+    s->buf_end = s->buf + buffer_size;
+#ifdef ALT_BITSTREAM_WRITER
+    s->index=0;
+    ((uint32_t*)(s->buf))[0]=0;
+//    memset(buffer, 0, buffer_size);
+#else
+    s->buf_ptr = s->buf;
+    s->bit_left=32;
+    s->bit_buf=0;
+#endif
+}
+
+/* return the number of bits output */
+static inline int put_bits_count(PutBitContext *s)
+{
+#ifdef ALT_BITSTREAM_WRITER
+    return s->index;
+#else
+    return (s->buf_ptr - s->buf) * 8 + 32 - s->bit_left;
+#endif
+}
 
-int put_bits_count(PutBitContext *s);
+static inline int put_bits_left(PutBitContext* s)
+{
+    return (s->buf_end - s->buf) * 8 - put_bits_count(s);
+}
+
+/* pad the end of the output stream with zeros */
+static inline void flush_put_bits(PutBitContext *s)
+{
+#ifdef ALT_BITSTREAM_WRITER
+    align_put_bits(s);
+#else
+    s->bit_buf<<= s->bit_left;
+    while (s->bit_left < 32) {
+        /* XXX: should test end of buffer */
+        *s->buf_ptr++=s->bit_buf >> 24;
+        s->bit_buf<<=8;
+        s->bit_left+=8;
+    }
+    s->bit_left=32;
+    s->bit_buf=0;
+#endif
+}
+
 void align_put_bits(PutBitContext *s);
-void flush_put_bits(PutBitContext *s);
 void put_string(PutBitContext * pbc, char *s, int put_zero);
 
 /* bit input */
@@ -318,8 +362,6 @@
     int size_in_bits;
 } GetBitContext;
 
-static inline int get_bits_count(GetBitContext *s);
-
 #define VLC_TYPE int16_t
 
 typedef struct VLC {
@@ -825,8 +867,57 @@
     skip_bits(s, 1);
 }
 
-void init_get_bits(GetBitContext *s,
-                   const uint8_t *buffer, int buffer_size);
+/**
+ * init GetBitContext.
+ * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
+ * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
+ * @param bit_size the size of the buffer in bits
+ */
+static inline void init_get_bits(GetBitContext *s,
+                   const uint8_t *buffer, int bit_size)
+{
+    const int buffer_size= (bit_size+7)>>3;
+
+    s->buffer= buffer;
+    s->size_in_bits= bit_size;
+    s->buffer_end= buffer + buffer_size;
+#ifdef ALT_BITSTREAM_READER
+    s->index=0;
+#elif defined LIBMPEG2_BITSTREAM_READER
+#ifdef LIBMPEG2_BITSTREAM_READER_HACK
+  if ((int)buffer&1) {
+     /* word alignment */
+    s->cache = (*buffer++)<<24;
+    s->buffer_ptr = buffer;
+    s->bit_count = 16-8;
+  } else
+#endif
+  {
+    s->buffer_ptr = buffer;
+    s->bit_count = 16;
+    s->cache = 0;
+  }
+#elif defined A32_BITSTREAM_READER
+    s->buffer_ptr = (uint32_t*)buffer;
+    s->bit_count = 32;
+    s->cache0 = 0;
+    s->cache1 = 0;
+#endif
+    {
+        OPEN_READER(re, s)
+        UPDATE_CACHE(re, s)
+        UPDATE_CACHE(re, s)
+        CLOSE_READER(re, s)
+    }
+#ifdef A32_BITSTREAM_READER
+    s->cache1 = 0;
+#endif
+}
+
+static inline int get_bits_left(GetBitContext *s)
+{
+    return s->size_in_bits - get_bits_count(s);
+}
 
 int check_marker(GetBitContext *s, const char *msg);
 void align_get_bits(GetBitContext *s);