1 /**************************************************************************
3 * Copyright 2011 Christian König.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include <pipe/p_compiler.h>
35 #include <util/u_math.h>
36 #include "util/u_pointer.h"
52 struct vl_vlc_compressed
55 struct vl_vlc_entry entry
;
59 vl_vlc_init_table(struct vl_vlc_entry
*dst
, unsigned dst_size
, const struct vl_vlc_compressed
*src
, unsigned src_size
)
61 unsigned i
, bits
= util_logbase2(dst_size
);
63 for (i
=0;i
<dst_size
;++i
) {
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
;
75 vl_vlc_fillbits(struct vl_vlc
*vlc
)
77 if (vlc
->valid_bits
< 32) {
78 uint32_t value
= *vlc
->data
;
80 //assert(vlc->data <= vlc->end);
82 #ifndef PIPE_ARCH_BIG_ENDIAN
83 value
= util_bswap32(value
);
86 vlc
->buffer
|= (uint64_t)value
<< (32 - vlc
->valid_bits
);
88 vlc
->valid_bits
+= 32;
93 vl_vlc_init(struct vl_vlc
*vlc
, const uint8_t *data
, unsigned len
)
101 /* align the data pointer */
102 while (pointer_to_uintptr(data
) & 3) {
103 vlc
->buffer
|= (uint64_t)*data
<< (56 - vlc
->valid_bits
);
106 vlc
->valid_bits
+= 8;
108 vlc
->data
= (uint32_t*)data
;
109 vlc
->end
= (uint32_t*)(data
+ len
);
111 vl_vlc_fillbits(vlc
);
112 vl_vlc_fillbits(vlc
);
115 static INLINE
unsigned
116 vl_vlc_bytes_left(struct vl_vlc
*vlc
)
118 return ((uint8_t*)vlc
->end
)-((uint8_t*)vlc
->data
);
121 static INLINE
unsigned
122 vl_vlc_peekbits(struct vl_vlc
*vlc
, unsigned num_bits
)
124 //assert(vlc->valid_bits >= num_bits);
126 return vlc
->buffer
>> (64 - num_bits
);
130 vl_vlc_eatbits(struct vl_vlc
*vlc
, unsigned num_bits
)
132 //assert(vlc->valid_bits > num_bits);
134 vlc
->buffer
<<= num_bits
;
135 vlc
->valid_bits
-= num_bits
;
138 static INLINE
unsigned
139 vl_vlc_get_uimsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
143 //assert(vlc->valid_bits >= num_bits);
145 value
= vlc
->buffer
>> (64 - num_bits
);
146 vl_vlc_eatbits(vlc
, num_bits
);
152 vl_vlc_get_simsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
156 //assert(vlc->valid_bits >= num_bits);
158 value
= ((int64_t)vlc
->buffer
) >> (64 - num_bits
);
159 vl_vlc_eatbits(vlc
, num_bits
);
165 vl_vlc_get_vlclbf(struct vl_vlc
*vlc
, const struct vl_vlc_entry
*tbl
, unsigned num_bits
)
167 tbl
+= vl_vlc_peekbits(vlc
, num_bits
);
168 vl_vlc_eatbits(vlc
, tbl
->length
);
172 #endif /* vl_vlc_h */