Merge branch 'master' of git://anongit.freedesktop.org/mesa/mesa
[mesa.git] / src / gallium / auxiliary / vl / vl_vlc.h
1 /**************************************************************************
2 *
3 * Copyright 2011 Christian König.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef vl_vlc_h
29 #define vl_vlc_h
30
31 #include <assert.h>
32
33 #include <pipe/p_compiler.h>
34
35 #include <util/u_math.h>
36 #include "util/u_pointer.h"
37
38 struct vl_vlc
39 {
40 uint64_t buffer;
41 unsigned valid_bits;
42 uint32_t *data;
43 uint32_t *end;
44 };
45
46 struct vl_vlc_entry
47 {
48 int8_t length;
49 int8_t value;
50 };
51
52 struct vl_vlc_compressed
53 {
54 uint16_t bitcode;
55 struct vl_vlc_entry entry;
56 };
57
58 static INLINE void
59 vl_vlc_init_table(struct vl_vlc_entry *dst, unsigned dst_size, const struct vl_vlc_compressed *src, unsigned src_size)
60 {
61 unsigned i, bits = util_logbase2(dst_size);
62
63 for (i=0;i<dst_size;++i) {
64 dst[i].length = 0;
65 dst[i].value = 0;
66 }
67
68 for(; src_size > 0; --src_size, ++src) {
69 for(i=0; i<(1 << (bits - src->entry.length)); ++i)
70 dst[src->bitcode >> (16 - bits) | i] = src->entry;
71 }
72 }
73
74 static INLINE void
75 vl_vlc_fillbits(struct vl_vlc *vlc)
76 {
77 if (vlc->valid_bits < 32) {
78 uint32_t value = *vlc->data;
79
80 //assert(vlc->data <= vlc->end);
81
82 #ifndef PIPE_ARCH_BIG_ENDIAN
83 value = util_bswap32(value);
84 #endif
85
86 vlc->buffer |= (uint64_t)value << (32 - vlc->valid_bits);
87 ++vlc->data;
88 vlc->valid_bits += 32;
89 }
90 }
91
92 static INLINE void
93 vl_vlc_init(struct vl_vlc *vlc, const uint8_t *data, unsigned len)
94 {
95 assert(vlc);
96 assert(data && len);
97
98 vlc->buffer = 0;
99 vlc->valid_bits = 0;
100
101 /* align the data pointer */
102 while (pointer_to_uintptr(data) & 3) {
103 vlc->buffer |= (uint64_t)*data << (56 - vlc->valid_bits);
104 ++data;
105 --len;
106 vlc->valid_bits += 8;
107 }
108 vlc->data = (uint32_t*)data;
109 vlc->end = (uint32_t*)(data + len);
110
111 vl_vlc_fillbits(vlc);
112 vl_vlc_fillbits(vlc);
113 }
114
115 static INLINE unsigned
116 vl_vlc_bytes_left(struct vl_vlc *vlc)
117 {
118 return ((uint8_t*)vlc->end)-((uint8_t*)vlc->data);
119 }
120
121 static INLINE unsigned
122 vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits)
123 {
124 //assert(vlc->valid_bits >= num_bits);
125
126 return vlc->buffer >> (64 - num_bits);
127 }
128
129 static INLINE void
130 vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits)
131 {
132 //assert(vlc->valid_bits > num_bits);
133
134 vlc->buffer <<= num_bits;
135 vlc->valid_bits -= num_bits;
136 }
137
138 static INLINE unsigned
139 vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits)
140 {
141 unsigned value;
142
143 //assert(vlc->valid_bits >= num_bits);
144
145 value = vlc->buffer >> (64 - num_bits);
146 vl_vlc_eatbits(vlc, num_bits);
147
148 return value;
149 }
150
151 static INLINE signed
152 vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits)
153 {
154 signed value;
155
156 //assert(vlc->valid_bits >= num_bits);
157
158 value = ((int64_t)vlc->buffer) >> (64 - num_bits);
159 vl_vlc_eatbits(vlc, num_bits);
160
161 return value;
162 }
163
164 static INLINE int8_t
165 vl_vlc_get_vlclbf(struct vl_vlc *vlc, const struct vl_vlc_entry *tbl, unsigned num_bits)
166 {
167 tbl += vl_vlc_peekbits(vlc, num_bits);
168 vl_vlc_eatbits(vlc, tbl->length);
169 return tbl->value;
170 }
171
172 #endif /* vl_vlc_h */