draw: don't crash if GS doesn't emit anything
[mesa.git] / src / gallium / auxiliary / vl / vl_vlc.h
index 8c5b3aca47d0b83fd9a132f2456deeb719405977..6223fabf577f63204f2d711b432368f9609a99f4 100644 (file)
  *
  **************************************************************************/
 
-/**
- * This file is based uppon slice_xvmc.c and vlc.h from the xine project,
- * which in turn is based on mpeg2dec. The following is the original copyright:
- *
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
- * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
- *
- * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
- * See http://libmpeg2.sourceforge.net/ for updates.
- *
- * mpeg2dec is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpeg2dec is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+/*
+ * Functions for fast bitwise access to multiple probably unaligned input buffers
  */
 
 #ifndef vl_vlc_h
 #define vl_vlc_h
 
+#include "pipe/p_compiler.h"
+
+#include "util/u_math.h"
+#include "util/u_pointer.h"
+#include "util/u_debug.h"
+
 struct vl_vlc
 {
-   uint32_t buf; /* current 32 bit working set of buffer */
-   int bits;     /* used bits in working set */
-   const uint8_t *ptr; /* buffer with stream data */
-   const uint8_t *max; /* ptr+len of buffer */
+   uint64_t buffer;
+   signed invalid_bits;
+   const uint8_t *data;
+   const uint8_t *end;
+
+   unsigned          num_inputs;
+   const void *const *inputs;
+   const unsigned    *sizes;
+   unsigned          bytes_left;
 };
 
-static inline void
-vl_vlc_restart(struct vl_vlc *vlc)
+struct vl_vlc_entry
 {
-   vlc->buf = (vlc->ptr[0] << 24) | (vlc->ptr[1] << 16) | (vlc->ptr[2] << 8) | vlc->ptr[3];
-   vlc->bits = -16;
-   vlc->ptr += 4;
-}
+   int8_t length;
+   int8_t value;
+};
 
-static inline void
-vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len)
+struct vl_vlc_compressed
 {
-   vlc->ptr = data;
-   vlc->max = data + len;
-   vl_vlc_restart(vlc);
-}
+   uint16_t bitcode;
+   struct vl_vlc_entry entry;
+};
 
-static inline bool
-vl_vlc_getbyte(struct vl_vlc *vlc)
+/**
+ * initalize and decompress a lookup table
+ */
+static INLINE void
+vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_vlc_compressed *src, unsigned src_size)
 {
-   vlc->buf <<= 8;
-   vlc->buf |= vlc->ptr[0];
-   vlc->ptr++;
-   return vlc->ptr < vlc->max;
+   unsigned i, bits = util_logbase2(dst_size);
+
+   assert(dst && dst_size);
+   assert(src && src_size);
+
+   for (i=0;i<dst_size;++i) {
+      dst[i].length = 0;
+      dst[i].value = 0;
+   }
+
+   for(; src_size > 0; --src_size, ++src) {
+      for(i=0; i<(1 << (bits - src->entry.length)); ++i)
+         dst[src->bitcode >> (16 - bits) | i] = src->entry;
+   }
 }
 
-#define vl_vlc_getword(vlc, shift)                                      \
-do {                                                                    \
-   (vlc)->buf |= (((vlc)->ptr[0] << 8) | (vlc)->ptr[1]) << (shift);     \
-   (vlc)->ptr += 2;                                                     \
-} while (0)
-
-/* make sure that there are at least 16 valid bits in bit_buf */
-#define vl_vlc_needbits(vlc)                    \
-do {                                            \
-    if ((vlc)->bits >= 0) {                      \
-       vl_vlc_getword(vlc, (vlc)->bits);       \
-       (vlc)->bits -= 16;                      \
-    }                                           \
-} while (0)
-
-/* make sure that the full 32 bit of the buffer are valid */
-static inline void
-vl_vlc_need32bits(struct vl_vlc *vlc)
+/**
+ * switch over to next input buffer
+ */
+static INLINE void
+vl_vlc_next_input(struct vl_vlc *vlc)
 {
-   vl_vlc_needbits(vlc);
-   if (vlc->bits > -8) {
-      unsigned n = -vlc->bits;
-      vlc->buf <<= n;
-      vlc->buf |= *vlc->ptr << 8;
-      vlc->bits = -8;
-      vlc->ptr++;
+   const uint8_t* data = vlc->inputs[0];
+   unsigned len = vlc->sizes[0];
+
+   assert(vlc);
+   assert(vlc->num_inputs);
+
+   vlc->bytes_left -= len;
+
+   /* align the data pointer */
+   while (len && pointer_to_uintptr(data) & 3) {
+      vlc->buffer |= (uint64_t)*data << (24 + vlc->invalid_bits);
+      ++data;
+      --len;
+      vlc->invalid_bits -= 8;
    }
-   if (vlc->bits > -16) {
-      unsigned n = -vlc->bits - 8;
-      vlc->buf <<= n;
-      vlc->buf |= *vlc->ptr;
-      vlc->bits = -16;
-      vlc->ptr++;
+   vlc->data = data;
+   vlc->end = data + len;
+
+   --vlc->num_inputs;
+   ++vlc->inputs;
+   ++vlc->sizes;
+}
+
+/**
+ * fill the bit buffer, so that at least 32 bits are valid
+ */
+static INLINE void
+vl_vlc_fillbits(struct vl_vlc *vlc)
+{
+   assert(vlc);
+
+   /* as long as the buffer needs to be filled */
+   while (vlc->invalid_bits > 0) {
+      unsigned bytes_left = vlc->end - vlc->data;
+
+      /* if this input is depleted */
+      if (bytes_left == 0) {
+
+         if (vlc->num_inputs)
+            /* go on to next input */
+            vl_vlc_next_input(vlc);
+         else
+            /* or give up since we don't have anymore inputs */
+            return;
+
+      } else if (bytes_left >= 4) {
+
+         /* enough bytes in buffer, read in a whole dword */
+         uint64_t value = *(const uint32_t*)vlc->data;
+
+#ifndef PIPE_ARCH_BIG_ENDIAN
+         value = util_bswap32(value);
+#endif
+
+         vlc->buffer |= value << vlc->invalid_bits;
+         vlc->data += 4;
+         vlc->invalid_bits -= 32;
+
+         /* buffer is now definitely filled up avoid the loop test */
+         break;
+
+      } else while (vlc->data < vlc->end) {
+
+         /* not enough bytes left in buffer, read single bytes */
+         vlc->buffer |= (uint64_t)*vlc->data << (24 + vlc->invalid_bits);
+         ++vlc->data;
+         vlc->invalid_bits -= 8;
+      }
    }
 }
 
-/* remove num valid bits from bit_buf */
-#define vl_vlc_dumpbits(vlc, num)       \
-do {                                   \
-    (vlc)->buf <<= (num);              \
-    (vlc)->bits += (num);              \
-} while (0)
+/**
+ * initialize vlc structure and start reading from first input buffer
+ */
+static INLINE void
+vl_vlc_init(struct vl_vlc *vlc, unsigned num_inputs,
+            const void *const *inputs, const unsigned *sizes)
+{
+   unsigned i;
+
+   assert(vlc);
+   assert(num_inputs);
 
-/* take num bits from the high part of bit_buf and zero extend them */
-#define vl_vlc_ubits(vlc, num) (((uint32_t)((vlc)->buf)) >> (32 - (num)))
+   vlc->buffer = 0;
+   vlc->invalid_bits = 32;
+   vlc->num_inputs = num_inputs;
+   vlc->inputs = inputs;
+   vlc->sizes = sizes;
+   vlc->bytes_left = 0;
 
-/* take num bits from the high part of bit_buf and sign extend them */
-#define vl_vlc_sbits(vlc, num) (((int32_t)((vlc)->buf)) >> (32 - (num)))
+   for (i = 0; i < num_inputs; ++i)
+      vlc->bytes_left += sizes[i];
+
+   vl_vlc_next_input(vlc);
+   vl_vlc_fillbits(vlc);
+   vl_vlc_fillbits(vlc);
+}
+
+/**
+ * number of bits still valid in bit buffer
+ */
+static INLINE unsigned
+vl_vlc_valid_bits(struct vl_vlc *vlc)
+{
+   return 32 - vlc->invalid_bits;
+}
+
+/**
+ * number of bits left over all inbut buffers
+ */
+static INLINE unsigned
+vl_vlc_bits_left(struct vl_vlc *vlc)
+{
+   signed bytes_left = vlc->end - vlc->data;
+   bytes_left += vlc->bytes_left;
+   return bytes_left * 8 + vl_vlc_valid_bits(vlc);
+}
+
+/**
+ * get num_bits from bit buffer without removing them
+ */
+static INLINE unsigned
+vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits)
+{
+   assert(vl_vlc_valid_bits(vlc) >= num_bits || vlc->data >= vlc->end);
+   return vlc->buffer >> (64 - num_bits);
+}
+
+/**
+ * remove num_bits from bit buffer
+ */
+static INLINE void
+vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits)
+{
+   assert(vl_vlc_valid_bits(vlc) >= num_bits);
+
+   vlc->buffer <<= num_bits;
+   vlc->invalid_bits += num_bits;
+}
+
+/**
+ * get num_bits from bit buffer with removing them
+ */
+static INLINE unsigned
+vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits)
+{
+   unsigned value;
+
+   assert(vl_vlc_valid_bits(vlc) >= num_bits);
+
+   value = vlc->buffer >> (64 - num_bits);
+   vl_vlc_eatbits(vlc, num_bits);
+
+   return value;
+}
+
+/**
+ * treat num_bits as signed value and remove them from bit buffer
+ */
+static INLINE signed
+vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits)
+{
+   signed value;
+
+   assert(vl_vlc_valid_bits(vlc) >= num_bits);
+
+   value = ((int64_t)vlc->buffer) >> (64 - num_bits);
+   vl_vlc_eatbits(vlc, num_bits);
+
+   return value;
+}
+
+/**
+ * lookup a value and length in a decompressed table
+ */
+static INLINE int8_t
+vl_vlc_get_vlclbf(struct vl_vlc *vlc, const struct vl_vlc_entry *tbl, unsigned num_bits)
+{
+   tbl += vl_vlc_peekbits(vlc, num_bits);
+   vl_vlc_eatbits(vlc, tbl->length);
+   return tbl->value;
+}
 
 #endif /* vl_vlc_h */