FreeRDP
Loading...
Searching...
No Matches
rfx_bitstream.h
1
20#ifndef FREERDP_LIB_CODEC_RFX_BITSTREAM_H
21#define FREERDP_LIB_CODEC_RFX_BITSTREAM_H
22
23#include <winpr/assert.h>
24#include <winpr/cast.h>
25
26#include <freerdp/codec/rfx.h>
27
28#ifdef __cplusplus
29extern "C"
30{
31#endif
32
33 typedef struct
34 {
35 BYTE* buffer;
36 uint32_t nbytes;
37 uint32_t byte_pos;
38 uint32_t bits_left;
40
41 static inline void rfx_bitstream_attach(RFX_BITSTREAM* bs, BYTE* WINPR_RESTRICT buffer,
42 size_t nbytes)
43 {
44 WINPR_ASSERT(bs);
45 bs->buffer = (buffer);
46
47 WINPR_ASSERT(nbytes <= UINT32_MAX);
48 bs->nbytes = WINPR_ASSERTING_INT_CAST(uint32_t, nbytes);
49 bs->byte_pos = 0;
50 bs->bits_left = 8;
51 }
52
53 static inline uint32_t rfx_bitstream_get_bits(RFX_BITSTREAM* bs, uint32_t nbits)
54 {
55 UINT16 n = 0;
56 while (bs->byte_pos < bs->nbytes && nbits > 0)
57 {
58 uint32_t b = nbits;
59 if (b > bs->bits_left)
60 b = bs->bits_left;
61 if (n)
62 n <<= b;
63 n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1);
64 bs->bits_left -= b;
65 nbits -= b;
66 if (bs->bits_left == 0)
67 {
68 bs->bits_left = 8;
69 bs->byte_pos++;
70 }
71 }
72 return n;
73 }
74
75 static inline void rfx_bitstream_put_bits(RFX_BITSTREAM* bs, uint32_t _bits, uint32_t _nbits)
76 {
77 UINT16 bits = WINPR_ASSERTING_INT_CAST(UINT16, _bits);
78
79 uint32_t nbits = (_nbits);
80 while (bs->byte_pos < bs->nbytes && nbits > 0)
81 {
82 uint32_t b = nbits;
83 if (b > bs->bits_left)
84 b = bs->bits_left;
85 bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1))
86 << (bs->bits_left - b);
87 bs->bits_left -= b;
88 nbits -= b;
89 if (bs->bits_left == 0)
90 {
91 bs->bits_left = 8;
92 bs->byte_pos++;
93 }
94 }
95 }
96
97 static inline void rfx_bitstream_flush(RFX_BITSTREAM* bs)
98 {
99 WINPR_ASSERT(bs);
100 if (bs->bits_left != 8)
101 {
102 uint32_t _nbits = 8 - bs->bits_left;
103 rfx_bitstream_put_bits(bs, 0, _nbits);
104 }
105 }
106
107 static inline BOOL rfx_bitstream_eos(RFX_BITSTREAM* bs)
108 {
109 WINPR_ASSERT(bs);
110 return ((bs)->byte_pos >= (bs)->nbytes);
111 }
112
113 static inline uint32_t rfx_bitstream_left(RFX_BITSTREAM* bs)
114 {
115 WINPR_ASSERT(bs);
116
117 if ((bs)->byte_pos >= (bs)->nbytes)
118 return 0;
119
120 return ((bs)->nbytes - (bs)->byte_pos - 1) * 8 + (bs)->bits_left;
121 }
122
123 static inline uint32_t rfx_bitstream_get_processed_bytes(RFX_BITSTREAM* bs)
124 {
125 WINPR_ASSERT(bs);
126 if ((bs)->bits_left < 8)
127 return (bs)->byte_pos + 1;
128 return (bs)->byte_pos;
129 }
130
131#ifdef __cplusplus
132}
133#endif
134#endif /* FREERDP_LIB_CODEC_RFX_BITSTREAM_H */