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>
51 struct vl_vlc_compressed
54 struct vl_vlc_entry entry
;
58 vl_vlc_init_table(struct vl_vlc_entry
*dst
, unsigned dst_size
, const struct vl_vlc_compressed
*src
, unsigned src_size
)
60 unsigned i
, bits
= util_logbase2(dst_size
);
62 for (i
=0;i
<dst_size
;++i
) {
67 for(; src_size
> 0; --src_size
, ++src
) {
68 for(i
=0; i
<(1 << (bits
- src
->entry
.length
)); ++i
)
69 dst
[src
->bitcode
>> (16 - bits
) | i
] = src
->entry
;
74 vl_vlc_fillbits(struct vl_vlc
*vlc
)
76 if (vlc
->valid_bits
< 32) {
77 uint32_t value
= *vlc
->data
;
79 //assert(vlc->data <= vlc->end);
81 #ifndef PIPE_ARCH_BIG_ENDIAN
82 value
= util_bswap32(value
);
85 vlc
->buffer
|= (uint64_t)value
<< (32 - vlc
->valid_bits
);
87 vlc
->valid_bits
+= 32;
92 vl_vlc_init(struct vl_vlc
*vlc
, const uint8_t *data
, unsigned len
)
100 /* align the data pointer */
101 while((uint64_t)data
& 3) {
102 vlc
->buffer
|= (uint64_t)*data
<< (56 - vlc
->valid_bits
);
105 vlc
->valid_bits
+= 8;
107 vlc
->data
= (uint32_t*)data
;
108 vlc
->end
= (uint32_t*)(data
+ len
);
110 vl_vlc_fillbits(vlc
);
111 vl_vlc_fillbits(vlc
);
114 static INLINE
unsigned
115 vl_vlc_bytes_left(struct vl_vlc
*vlc
)
117 return ((uint8_t*)vlc
->end
)-((uint8_t*)vlc
->data
);
120 static INLINE
unsigned
121 vl_vlc_peekbits(struct vl_vlc
*vlc
, unsigned num_bits
)
123 //assert(vlc->valid_bits >= num_bits);
125 return vlc
->buffer
>> (64 - num_bits
);
129 vl_vlc_eatbits(struct vl_vlc
*vlc
, unsigned num_bits
)
131 //assert(vlc->valid_bits > num_bits);
133 vlc
->buffer
<<= num_bits
;
134 vlc
->valid_bits
-= num_bits
;
137 static INLINE
unsigned
138 vl_vlc_get_uimsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
142 //assert(vlc->valid_bits >= num_bits);
144 value
= vlc
->buffer
>> (64 - num_bits
);
145 vl_vlc_eatbits(vlc
, num_bits
);
151 vl_vlc_get_simsbf(struct vl_vlc
*vlc
, unsigned num_bits
)
155 //assert(vlc->valid_bits >= num_bits);
157 value
= ((int64_t)vlc
->buffer
) >> (64 - num_bits
);
158 vl_vlc_eatbits(vlc
, num_bits
);
164 vl_vlc_get_vlclbf(struct vl_vlc
*vlc
, const struct vl_vlc_entry
*tbl
, unsigned num_bits
)
166 tbl
+= vl_vlc_peekbits(vlc
, num_bits
);
167 vl_vlc_eatbits(vlc
, tbl
->length
);
171 #endif /* vl_vlc_h */