vl_vlc.h revision 19bcd21ed151cf1716f2f87dff0f712231aa2ce7
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 38struct vl_vlc 39{ 40 uint64_t buffer; 41 unsigned valid_bits; 42 uint32_t *data; 43 uint32_t *end; 44}; 45 46struct vl_vlc_entry 47{ 48 int8_t length; 49 int8_t value; 50}; 51 52struct vl_vlc_compressed 53{ 54 uint16_t bitcode; 55 struct vl_vlc_entry entry; 56}; 57 58static INLINE void 59vl_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 74static INLINE void 75vl_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 92static INLINE void 93vl_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 115static INLINE unsigned 116vl_vlc_bits_left(struct vl_vlc *vlc) 117{ 118 signed bytes_left = ((uint8_t*)vlc->end)-((uint8_t*)vlc->data); 119 return bytes_left * 8 + vlc->valid_bits; 120} 121 122static INLINE unsigned 123vl_vlc_peekbits(struct vl_vlc *vlc, unsigned num_bits) 124{ 125 //assert(vlc->valid_bits >= num_bits); 126 127 return vlc->buffer >> (64 - num_bits); 128} 129 130static INLINE void 131vl_vlc_eatbits(struct vl_vlc *vlc, unsigned num_bits) 132{ 133 //assert(vlc->valid_bits > num_bits); 134 135 vlc->buffer <<= num_bits; 136 vlc->valid_bits -= num_bits; 137} 138 139static INLINE unsigned 140vl_vlc_get_uimsbf(struct vl_vlc *vlc, unsigned num_bits) 141{ 142 unsigned value; 143 144 //assert(vlc->valid_bits >= num_bits); 145 146 value = vlc->buffer >> (64 - num_bits); 147 vl_vlc_eatbits(vlc, num_bits); 148 149 return value; 150} 151 152static INLINE signed 153vl_vlc_get_simsbf(struct vl_vlc *vlc, unsigned num_bits) 154{ 155 signed value; 156 157 //assert(vlc->valid_bits >= num_bits); 158 159 value = ((int64_t)vlc->buffer) >> (64 - num_bits); 160 vl_vlc_eatbits(vlc, num_bits); 161 162 return value; 163} 164 165static INLINE int8_t 166vl_vlc_get_vlclbf(struct vl_vlc *vlc, const struct vl_vlc_entry *tbl, unsigned num_bits) 167{ 168 tbl += vl_vlc_peekbits(vlc, num_bits); 169 vl_vlc_eatbits(vlc, tbl->length); 170 return tbl->value; 171} 172 173#endif /* vl_vlc_h */ 174