summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/vl/vl_vlc.h
blob: 4db1334d6a46976d441a57185a6bfe8c4101ff85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**************************************************************************
 *
 * Copyright 2011 Christian König.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#ifndef vl_vlc_h
#define vl_vlc_h

#include <assert.h>

#include <pipe/p_compiler.h>

#include <util/u_math.h>
#include "util/u_pointer.h"

struct vl_vlc
{
   uint64_t buffer;
   unsigned valid_bits;
   uint32_t *data;
   uint32_t *end;
};

struct vl_vlc_entry
{
   int8_t length;
   int8_t value;
};

struct vl_vlc_compressed
{
   uint16_t bitcode;
   struct vl_vlc_entry entry;
};

static INLINE void
vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_vlc_compressed *src, unsigned src_size)
{
   unsigned i, bits = util_logbase2(dst_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;
   }
}

static INLINE void
vl_vlc_fillbits(struct vl_vlc *vlc)
{
   if (vlc->valid_bits < 32) {
      uint32_t value = *vlc->data;

      //assert(vlc->data <= vlc->end);

#ifndef PIPE_ARCH_BIG_ENDIAN
      value = util_bswap32(value);
#endif

      vlc->buffer |= (uint64_t)value << (32 - vlc->valid_bits);
      ++vlc->data;
      vlc->valid_bits += 32;
   }
}

static INLINE void
vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len)
{
   assert(vlc);
   assert(data && len);

   vlc->buffer = 0;
   vlc->valid_bits = 0;

   /* align the data pointer */
   while (pointer_to_uintptr(data) & 3) {
      vlc->buffer |= (uint64_t)*data << (56 - vlc->valid_bits);
      ++data;
      --len;
      vlc->valid_bits += 8;
   }
   vlc->data = (uint32_t*)data;
   vlc->end = (uint32_t*)(data + len);

   vl_vlc_fillbits(vlc);
   vl_vlc_fillbits(vlc);
}

static INLINE unsigned
vl_vlc_bytes_left(struct vl_vlc *vlc)
{
   return ((uint8_t*)vlc->end)-((uint8_t*)vlc->data);
}

static INLINE unsigned
vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits)
{
   //assert(vlc->valid_bits >= num_bits);

   return vlc->buffer >> (64 - num_bits);
}

static INLINE void
vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits)
{
   //assert(vlc->valid_bits > num_bits);

   vlc->buffer <<= num_bits;
   vlc->valid_bits -= num_bits;
}

static INLINE unsigned
vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits)
{
   unsigned value;

   //assert(vlc->valid_bits >= num_bits);

   value = vlc->buffer >> (64 - num_bits);
   vl_vlc_eatbits(vlc, num_bits);

   return value;
}

static INLINE signed
vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits)
{
   signed value;

   //assert(vlc->valid_bits >= num_bits);

   value = ((int64_t)vlc->buffer) >> (64 - num_bits);
   vl_vlc_eatbits(vlc, num_bits);

   return value;
}

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 */