FreeRDP
rfx_bitstream.h
1 
20 #ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
21 #define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
22 
23 #include <freerdp/codec/rfx.h>
24 
25 typedef struct
26 {
27  BYTE* buffer;
28  int nbytes;
29  int byte_pos;
30  int bits_left;
32 
33 #define rfx_bitstream_attach(bs, _buffer, _nbytes) \
34  do \
35  { \
36  bs->buffer = (BYTE*)(_buffer); \
37  bs->nbytes = (_nbytes); \
38  bs->byte_pos = 0; \
39  bs->bits_left = 8; \
40  } while (0)
41 
42 #define rfx_bitstream_get_bits(bs, _nbits, _r) \
43  do \
44  { \
45  int nbits = _nbits; \
46  int b; \
47  UINT16 n = 0; \
48  while (bs->byte_pos < bs->nbytes && nbits > 0) \
49  { \
50  b = nbits; \
51  if (b > bs->bits_left) \
52  b = bs->bits_left; \
53  if (n) \
54  n <<= b; \
55  n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1); \
56  bs->bits_left -= b; \
57  nbits -= b; \
58  if (bs->bits_left == 0) \
59  { \
60  bs->bits_left = 8; \
61  bs->byte_pos++; \
62  } \
63  } \
64  _r = n; \
65  } while (0)
66 
67 #define rfx_bitstream_put_bits(bs, _bits, _nbits) \
68  do \
69  { \
70  UINT16 bits = (_bits); \
71  int nbits = (_nbits); \
72  int b; \
73  while (bs->byte_pos < bs->nbytes && nbits > 0) \
74  { \
75  b = nbits; \
76  if (b > bs->bits_left) \
77  b = bs->bits_left; \
78  bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1)) \
79  << (bs->bits_left - b); \
80  bs->bits_left -= b; \
81  nbits -= b; \
82  if (bs->bits_left == 0) \
83  { \
84  bs->bits_left = 8; \
85  bs->byte_pos++; \
86  } \
87  } \
88  } while (0)
89 #define rfx_bitstream_flush(bs) \
90  do \
91  { \
92  if (bs->bits_left != 8) \
93  { \
94  int _nbits = 8 - bs->bits_left; \
95  rfx_bitstream_put_bits(bs, 0, _nbits); \
96  } \
97  } while (0)
98 
99 #define rfx_bitstream_eos(_bs) ((_bs)->byte_pos >= (_bs)->nbytes)
100 #define rfx_bitstream_left(_bs) \
101  ((_bs)->byte_pos >= (_bs)->nbytes \
102  ? 0 \
103  : ((_bs)->nbytes - (_bs)->byte_pos - 1) * 8 + (_bs)->bits_left)
104 #define rfx_bitstream_get_processed_bytes(_bs) \
105  ((_bs)->bits_left < 8 ? (_bs)->byte_pos + 1 : (_bs)->byte_pos)
106 
107 #endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */