49
typedef unsigned char* position_type;
47
51
encoder(unsigned char* buf, unsigned size);
48
52
void put_8(unsigned char c);
49
53
void put_16(bm::short_t s);
50
54
void put_16(const bm::short_t* s, unsigned count);
51
55
void put_32(bm::word_t w);
52
56
void put_32(const bm::word_t* w, unsigned count);
57
void put_prefixed_array_32(unsigned char c,
58
const bm::word_t* w, unsigned count);
59
void put_prefixed_array_16(unsigned char c,
60
const bm::short_t* s, unsigned count,
53
62
unsigned size() const;
63
unsigned char* get_pos() const;
64
void set_pos(unsigned char* buf_pos);
55
66
unsigned char* buf_;
56
67
unsigned char* start_;
137
Byte based writer for un-aligned bit streaming
141
template<class TEncoder>
145
bit_out(TEncoder& dest)
146
: dest_(dest), used_bits_(0), accum_(0)
152
dest_.put_32(accum_);
155
void put_bit(unsigned value)
157
BM_ASSERT(value <= 1);
158
accum_ |= (value << used_bits_);
159
if (++used_bits_ == (sizeof(accum_) * 8))
163
void put_bits(unsigned value, unsigned count)
165
unsigned used = used_bits_;
166
unsigned acc = accum_;
170
mask >>= (sizeof(accum_) * 8) - count;
175
acc |= value << used;
177
unsigned free_bits = (sizeof(accum_) * 8) - used;
178
if (count <= free_bits)
198
if (++used_bits_ == (sizeof(accum_) * 8))
202
void put_zero_bits(register unsigned count)
204
register unsigned used = used_bits_;
205
unsigned free_bits = (sizeof(accum_) * 8) - used;
206
if (count >= free_bits)
212
for ( ;count >= sizeof(accum_) * 8; count -= sizeof(accum_) * 8)
222
accum_ |= (1 << used);
223
if (++used == (sizeof(accum_) * 8))
230
void gamma(unsigned value)
236
bm::bsr_asm32(value);
238
bm::ilog2_LUT(value);
241
// Put zeroes + 1 bit
243
unsigned used = used_bits_;
244
unsigned acc = accum_;
245
const unsigned acc_bits = (sizeof(acc) * 8);
246
unsigned free_bits = acc_bits - used;
249
unsigned count = logv;
250
if (count >= free_bits)
256
for ( ;count >= acc_bits; count -= acc_bits)
267
if (++used == acc_bits)
274
// Put the value bits
277
unsigned mask = (~0);
278
mask >>= acc_bits - logv;
283
acc |= value << used;
284
free_bits = acc_bits - used;
285
if (logv <= free_bits)
314
dest_.put_32(accum_);
315
used_bits_ = accum_ = 0;
318
bit_out(const bit_out&);
319
bit_out& operator=(const bit_out&);
322
TEncoder& dest_; ///< Bit stream target
323
unsigned used_bits_; ///< Bits used in the accumulator
324
unsigned accum_; ///< write bit accumulator
329
Byte based reader for un-aligned bit streaming
333
template<class TDecoder>
337
bit_in(TDecoder& decoder)
339
used_bits_(sizeof(accum_) * 8),
346
if (used_bits_ == (sizeof(accum_) * 8))
348
accum_ = src_.get_32();
352
unsigned acc = accum_ & 1;
357
unsigned eat_zero_bits()
359
if (used_bits_ == (sizeof(accum_) * 8))
361
accum_ = src_.get_32();
373
accum_ = accum_ >> 1;
374
if (++used_bits_ == (sizeof(accum_) * 8))
376
accum_ = src_.get_32();
390
bit_in(const bit_in&);
391
bit_in& operator=(const bit_in&);
393
TDecoder& src_; ///< Source of bytes
394
unsigned used_bits_; ///< Bits used in the accumulator
395
unsigned accum_; ///< read bit accumulator
400
Functor for Elias Gamma encoding
402
template<typename T, typename TBitIO>
406
gamma_encoder(TBitIO& bout) : bout_(bout)
413
void operator()(T value)
418
gamma_encoder(const gamma_encoder&);
419
gamma_encoder& operator=(const gamma_encoder&);
126
425
// ----------------------------------------------------------------
127
426
// Implementation details.
137
436
: buf_(buf), start_(buf), size_(size)
440
\grief Encode 8-bit prefix + an array
442
inline void encoder::put_prefixed_array_32(unsigned char c,
451
\grief Encode 8-bit prefix + an array
453
inline void encoder::put_prefixed_array_16(unsigned char c,
454
const bm::short_t* s,
460
put_16((bm::short_t) count);
142
466
\fn void encoder::put_8(unsigned char c)
166
495
inline void encoder::put_16(const bm::short_t* s, unsigned count)
497
#if (BM_UNALIGNED_ACCESS_OK == 1)
498
unsigned short* buf = (unsigned short*)buf_;
499
const bm::short_t* s_end = s + count;
505
buf_ = (unsigned char*)buf;
168
507
unsigned char* buf = buf_;
169
508
const bm::short_t* s_end = s + count;
191
531
return (unsigned)(buf_ - start_);
535
\brief Get current memory stream position
537
inline encoder::position_type encoder::get_pos() const
543
\brief Set current memory stream position
545
inline void encoder::set_pos(encoder::position_type buf_pos)
195
552
\fn void encoder::put_32(bm::word_t w)
196
553
\brief Puts 32 bits word into encoding buffer.
199
556
BMFORCEINLINE void encoder::put_32(bm::word_t w)
558
#if (BM_UNALIGNED_ACCESS_OK == 1)
559
*((bm::word_t*) buf_) = w;
201
562
*buf_++ = (unsigned char) w;
202
563
*buf_++ = (unsigned char) (w >> 8);
203
564
*buf_++ = (unsigned char) (w >> 16);
204
565
*buf_++ = (unsigned char) (w >> 24);
211
573
void encoder::put_32(const bm::word_t* w, unsigned count)
575
#if (BM_UNALIGNED_ACCESS_OK == 1)
576
bm::word_t* buf = (bm::word_t*)buf_;
577
const bm::word_t* w_end = w + count;
583
buf_ = (unsigned char*)buf;
213
585
unsigned char* buf = buf_;
214
586
const bm::word_t* w_end = w + count;
260
637
BMFORCEINLINE bm::word_t decoder::get_32()
262
bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
639
#if (BM_UNALIGNED_ACCESS_OK == 1)
640
bm::word_t a = *((bm::word_t*)buf_);
642
bm::word_t a = buf_[0]+ ((unsigned)buf_[1] << 8) +
263
643
((unsigned)buf_[2] << 16) + ((unsigned)buf_[3] << 24);