Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-codec / rfx_bitstream.h
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * RemoteFX Codec Library - Bit Stream
4  *
5  * Copyright 2011 Vic Lee
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #ifndef __RFX_BITSTREAM_H
21 #define __RFX_BITSTREAM_H
22
23 #include <freerdp/codec/rfx.h>
24
25 struct _RFX_BITSTREAM
26 {
27         uint8* buffer;
28         int nbytes;
29         int byte_pos;
30         int bits_left;
31 };
32 typedef struct _RFX_BITSTREAM RFX_BITSTREAM;
33
34 #define rfx_bitstream_attach(bs, _buffer, _nbytes) do { \
35         bs->buffer = (uint8*) (_buffer); \
36         bs->nbytes = (_nbytes); \
37         bs->byte_pos = 0; \
38         bs->bits_left = 8; } while (0)
39
40 #define rfx_bitstream_get_bits(bs, _nbits, _r) do { \
41         int nbits = _nbits; \
42         int b; \
43         uint16 n = 0; \
44         while (bs->byte_pos < bs->nbytes && nbits > 0) \
45         { \
46                 b = nbits; \
47                 if (b > bs->bits_left) \
48                         b = bs->bits_left; \
49                 if (n) \
50                         n <<= b; \
51                 n |= (bs->buffer[bs->byte_pos] >> (bs->bits_left - b)) & ((1 << b) - 1); \
52                 bs->bits_left -= b; \
53                 nbits -= b; \
54                 if (bs->bits_left == 0) \
55                 { \
56                         bs->bits_left = 8; \
57                         bs->byte_pos++; \
58                 } \
59         } \
60         _r = n; } while (0)
61
62 #define rfx_bitstream_put_bits(bs, _bits, _nbits) do { \
63         uint16 bits = (_bits); \
64         int nbits = (_nbits); \
65         int b; \
66         while (bs->byte_pos < bs->nbytes && nbits > 0) \
67         { \
68                 b = nbits; \
69                 if (b > bs->bits_left) \
70                         b = bs->bits_left; \
71                 bs->buffer[bs->byte_pos] &= ~(((1 << b) - 1) << (bs->bits_left - b)); \
72                 bs->buffer[bs->byte_pos] |= ((bits >> (nbits - b)) & ((1 << b) - 1)) << (bs->bits_left - b); \
73                 bs->bits_left -= b; \
74                 nbits -= b; \
75                 if (bs->bits_left == 0) \
76                 { \
77                         bs->bits_left = 8; \
78                         bs->byte_pos++; \
79                 } \
80         } } while (0)
81
82 #define rfx_bitstream_eos(_bs) ((_bs)->byte_pos >= (_bs)->nbytes)
83 #define rfx_bitstream_left(_bs) ((_bs)->byte_pos >= (_bs)->nbytes ? 0 : ((_bs)->nbytes - (_bs)->byte_pos - 1) * 8 + (_bs)->bits_left)
84 #define rfx_bitstream_get_processed_bytes(_bs) ((_bs)->bits_left < 8 ? (_bs)->byte_pos + 1 : (_bs)->byte_pos)
85
86 #endif /* __RFX_BITSTREAM_H */