1 /*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 /*******************************************************************************
17 File: bitbuffer.c
18
19 Content: Bit Buffer Management functions
20
21 *******************************************************************************/
22
23 #include "bitbuffer.h"
24
25 /*****************************************************************************
26 *
27 * function name: CreateBitBuffer
28 * description: create and init Bit Buffer Management
29 *
30 *****************************************************************************/
CreateBitBuffer(HANDLE_BIT_BUF hBitBuf,UWord8 * pBitBufBase,Word16 bitBufSize)31 HANDLE_BIT_BUF CreateBitBuffer(HANDLE_BIT_BUF hBitBuf,
32 UWord8 *pBitBufBase,
33 Word16 bitBufSize)
34 {
35 assert(bitBufSize*8 <= 32768);
36
37 hBitBuf->pBitBufBase = pBitBufBase;
38 hBitBuf->pBitBufEnd = pBitBufBase + bitBufSize - 1;
39
40 hBitBuf->pWriteNext = pBitBufBase;
41
42 hBitBuf->cache = 0;
43
44 hBitBuf->wBitPos = 0;
45 hBitBuf->cntBits = 0;
46
47 hBitBuf->size = (bitBufSize << 3);
48 hBitBuf->isValid = 1;
49
50 return hBitBuf;
51 }
52
53 /*****************************************************************************
54 *
55 * function name: DeleteBitBuffer
56 * description: uninit Bit Buffer Management
57 *
58 *****************************************************************************/
DeleteBitBuffer(HANDLE_BIT_BUF * hBitBuf)59 void DeleteBitBuffer(HANDLE_BIT_BUF *hBitBuf)
60 {
61 if(*hBitBuf)
62 (*hBitBuf)->isValid = 0;
63 *hBitBuf = NULL;
64 }
65
66 /*****************************************************************************
67 *
68 * function name: ResetBitBuf
69 * description: reset Bit Buffer Management
70 *
71 *****************************************************************************/
ResetBitBuf(HANDLE_BIT_BUF hBitBuf,UWord8 * pBitBufBase,Word16 bitBufSize)72 void ResetBitBuf(HANDLE_BIT_BUF hBitBuf,
73 UWord8 *pBitBufBase,
74 Word16 bitBufSize)
75 {
76 hBitBuf->pBitBufBase = pBitBufBase;
77 hBitBuf->pBitBufEnd = pBitBufBase + bitBufSize - 1;
78
79
80 hBitBuf->pWriteNext = pBitBufBase;
81
82 hBitBuf->wBitPos = 0;
83 hBitBuf->cntBits = 0;
84
85 hBitBuf->cache = 0;
86 }
87
88 /*****************************************************************************
89 *
90 * function name: CopyBitBuf
91 * description: copy Bit Buffer Management
92 *
93 *****************************************************************************/
CopyBitBuf(HANDLE_BIT_BUF hBitBufSrc,HANDLE_BIT_BUF hBitBufDst)94 void CopyBitBuf(HANDLE_BIT_BUF hBitBufSrc,
95 HANDLE_BIT_BUF hBitBufDst)
96 {
97 *hBitBufDst = *hBitBufSrc;
98 }
99
100 /*****************************************************************************
101 *
102 * function name: GetBitsAvail
103 * description: get available bits
104 *
105 *****************************************************************************/
GetBitsAvail(HANDLE_BIT_BUF hBitBuf)106 Word16 GetBitsAvail(HANDLE_BIT_BUF hBitBuf)
107 {
108 return hBitBuf->cntBits;
109 }
110
111 /*****************************************************************************
112 *
113 * function name: WriteBits
114 * description: write bits to the buffer
115 *
116 *****************************************************************************/
WriteBits(HANDLE_BIT_BUF hBitBuf,UWord32 writeValue,Word16 noBitsToWrite)117 Word16 WriteBits(HANDLE_BIT_BUF hBitBuf,
118 UWord32 writeValue,
119 Word16 noBitsToWrite)
120 {
121 Word16 wBitPos;
122
123 assert(noBitsToWrite <= (Word16)sizeof(Word32)*8);
124
125 if(noBitsToWrite == 0)
126 return noBitsToWrite;
127
128 hBitBuf->cntBits += noBitsToWrite;
129
130 wBitPos = hBitBuf->wBitPos;
131 wBitPos += noBitsToWrite;
132 writeValue &= ~(0xffffffff << noBitsToWrite); // Mask out everything except the lowest noBitsToWrite bits
133 writeValue <<= 32 - wBitPos;
134 writeValue |= hBitBuf->cache;
135
136 while (wBitPos >= 8)
137 {
138 UWord8 tmp;
139 tmp = (UWord8)((writeValue >> 24) & 0xFF);
140
141 *hBitBuf->pWriteNext++ = tmp;
142 writeValue <<= 8;
143 wBitPos -= 8;
144 }
145
146 hBitBuf->wBitPos = wBitPos;
147 hBitBuf->cache = writeValue;
148
149 return noBitsToWrite;
150 }
151