15 #ifndef RANS_BYTE_HEADER
16 #define RANS_BYTE_HEADER
21 #define RansAssert assert
59 #define RANS_BYTE_L (1u << 23) // lower bound of our normalization interval
65 static inline void RansEncInit(
RansState* r)
71 static inline RansState RansEncRenorm(
RansState x, uint8_t** pptr, uint32_t freq, uint32_t scale_bits)
73 uint32_t x_max = ((
RANS_BYTE_L >> scale_bits) << 8) * freq;
77 *--ptr = (uint8_t) (x & 0xff);
92 static inline void RansEncPut(
RansState* r, uint8_t** pptr, uint32_t start, uint32_t freq, uint32_t scale_bits)
95 RansState x = RansEncRenorm(*r, pptr, freq, scale_bits);
98 *r = ((x / freq) << scale_bits) + (x % freq) + start;
102 static inline void RansEncFlush(
RansState* r, uint8_t** pptr)
105 uint8_t* ptr = *pptr;
108 ptr[0] = (uint8_t) (x >> 0);
109 ptr[1] = (uint8_t) (x >> 8);
110 ptr[2] = (uint8_t) (x >> 16);
111 ptr[3] = (uint8_t) (x >> 24);
118 static inline void RansDecInit(
RansState* r, uint8_t** pptr)
121 uint8_t* ptr = *pptr;
134 static inline uint32_t RansDecGet(
RansState* r, uint32_t scale_bits)
136 return *r & ((1u << scale_bits) - 1);
142 static inline void RansDecAdvance(
RansState* r, uint8_t** pptr, uint32_t start, uint32_t freq, uint32_t scale_bits)
144 uint32_t mask = (1u << scale_bits) - 1;
148 x = freq * (x >> scale_bits) + (x & mask) - start;
152 uint8_t* ptr = *pptr;
183 static inline void RansEncSymbolInit(
RansEncSymbol* s, uint32_t start, uint32_t freq, uint32_t scale_bits)
187 RansAssert(freq <= (1u << scale_bits) - start);
207 s->
cmpl_freq = (uint16_t) ((1 << scale_bits) - freq);
237 s->
bias = start + (1 << scale_bits) - 1;
242 while (freq > (1u << shift))
245 s->
rcp_freq = (uint32_t) (((1ull << (shift + 31)) + freq-1) / freq);
257 static inline void RansDecSymbolInit(
RansDecSymbol* s, uint32_t start, uint32_t freq)
261 s->
start = (uint16_t) start;
262 s->
freq = (uint16_t) freq;
275 uint32_t x_max = sym->
x_max;
278 uint8_t* ptr = *pptr;
280 *--ptr = (uint8_t) (x & 0xff);
282 }
while (x >= x_max);
298 static inline void RansDecAdvanceSymbol(
RansState* r, uint8_t** pptr,
RansDecSymbol const* sym, uint32_t scale_bits)
300 RansDecAdvance(r, pptr, sym->
start, sym->
freq, scale_bits);
306 static inline void RansDecAdvanceStep(
RansState* r, uint32_t start, uint32_t freq, uint32_t scale_bits)
308 uint32_t mask = (1u << scale_bits) - 1;
312 *r = freq * (x >> scale_bits) + (x & mask) - start;
318 RansDecAdvanceStep(r, sym->
start, sym->
freq, scale_bits);
322 static inline void RansDecRenorm(
RansState* r, uint8_t** pptr)
328 uint8_t* ptr = *pptr;
336 #endif // RANS_BYTE_HEADER
uint16_t freq
Definition: rANS_byte.h:179
#define RANS_BYTE_L
Definition: rANS_byte.h:59
uint32_t x_max
Definition: rANS_byte.h:169
Definition: rANS_byte.h:177
uint32_t RansState
Definition: rANS_byte.h:62
#define RansAssert(x)
Definition: rANS_byte.h:23
uint16_t start
Definition: rANS_byte.h:178
uint32_t rcp_freq
Definition: rANS_byte.h:170
uint16_t cmpl_freq
Definition: rANS_byte.h:172
uint16_t rcp_shift
Definition: rANS_byte.h:173
Definition: rANS_byte.h:168
uint32_t bias
Definition: rANS_byte.h:171