1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /************************************************************************** |
|
21 nok_bits.h - Interface for bitstream handling. |
|
22 |
|
23 Author(s): Juha Ojanpera |
|
24 Copyright (c) 1999-2004 by Nokia Research Center, Audio-Visual Systems. |
|
25 *************************************************************************/ |
|
26 |
|
27 #ifndef NOKBITSTREAM_H_ |
|
28 #define NOKBITSTREAM_H_ |
|
29 |
|
30 /*-- Project Headers. --*/ |
|
31 #include "defines.h" |
|
32 |
|
33 /*-- Bitstream supports any length... --*/ |
|
34 #define BITSMODULO_BUFFER |
|
35 |
|
36 #ifndef BITSMODULO_BUFFER |
|
37 #define MOD_OPCODE(x, y) (x & y) |
|
38 #else |
|
39 #define MOD_OPCODE(x, y) (x % y) |
|
40 #endif /*-- MODULO_BUFFER --*/ |
|
41 |
|
42 const uint32 bsBitMask[] = |
|
43 {0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, |
|
44 0x1FFFL, 0x3FFFL, 0x7FFFL, 0xFFFFL, 0x1FFFFL, 0x3FFFFL, 0x7FFFFL, 0xFFFFFL, |
|
45 0x1FFFFFL, 0x3FFFFFL, 0x7FFFFFL, 0xFFFFFFL, 0x1FFFFFFL, 0x3FFFFFFL, 0x7FFFFFFL, |
|
46 0xFFFFFFFL, 0x1FFFFFFFL, 0x3FFFFFFFL, 0x7FFFFFFFL, 0xFFFFFFFFL}; |
|
47 |
|
48 #ifdef BYTE_16bit |
|
49 typedef uint16 DSP_BYTE; |
|
50 #else |
|
51 typedef uint8 DSP_BYTE; |
|
52 #endif |
|
53 |
|
54 /** |
|
55 * Data structure for accessing bits from input bitstram. |
|
56 */ |
|
57 typedef enum BYTE_MODE |
|
58 { |
|
59 BIT8 = 0, |
|
60 BIT16 |
|
61 |
|
62 } BYTE_MODE; |
|
63 |
|
64 #define MAX_BUFS (1) |
|
65 |
|
66 typedef struct BufMapperStr |
|
67 { |
|
68 uint8 numBufs; |
|
69 uint8 **bsBuf; |
|
70 uint32 *bsBufLen; |
|
71 uint32 *bytesRead; |
|
72 |
|
73 } BufMapper; |
|
74 |
|
75 class TBitStream |
|
76 { |
|
77 public: |
|
78 |
|
79 |
|
80 /** |
|
81 * Bitstream (modulo/ring) buffer. */ |
|
82 DSP_BYTE *dsp_buffer; |
|
83 uint8 *bit_buffer; |
|
84 |
|
85 /** |
|
86 * Number of bits in the data type. |
|
87 */ |
|
88 int16 slotBits; |
|
89 |
|
90 /* Which bit buffer is to be used. */ |
|
91 BYTE_MODE mode; |
|
92 |
|
93 /** |
|
94 * Size of the buffer. |
|
95 */ |
|
96 uint32 buf_len; |
|
97 |
|
98 /** |
|
99 * Bit counter. |
|
100 */ |
|
101 int16 bit_counter; |
|
102 |
|
103 /** |
|
104 * Read index. |
|
105 */ |
|
106 uint32 buf_index; |
|
107 |
|
108 /** |
|
109 * Write index. |
|
110 */ |
|
111 uint32 buf_writeIndex; |
|
112 |
|
113 /** |
|
114 * Bitmask for 'buf_index'. |
|
115 */ |
|
116 uint32 buf_mask; |
|
117 |
|
118 /** |
|
119 * Number of bytes read from the buffer. This field will be used to check |
|
120 * whether there are enough bits in the buffer to result a successfull |
|
121 * decoding of current frame. |
|
122 */ |
|
123 uint32 slots_read; |
|
124 |
|
125 /** |
|
126 * Number of bits read from the buffer. This will be resetted on a call-by-call |
|
127 * basis and therefore gives the number of bits that each call read from |
|
128 * the buffer. The buffer is updated according to this value. |
|
129 */ |
|
130 uint32 bits_read; |
|
131 |
|
132 }; |
|
133 |
|
134 /** |
|
135 * Bitstream manipulation methods. |
|
136 */ |
|
137 |
|
138 |
|
139 /** |
|
140 * Initializes input bitstream. |
|
141 * |
|
142 * @param bs Bitstream handle |
|
143 * @param bit_buffer Input bitstream |
|
144 * @param size Size of input bitstream in bytes |
|
145 */ |
|
146 IMPORT_C void BsInit(TBitStream *bs, uint8 *bit_buffer, uint32 size); |
|
147 |
|
148 IMPORT_C void BsInit1(TBitStream *bs, uint8 *bit_buffer, uint32 size); |
|
149 |
|
150 IMPORT_C void BsInit2(TBitStream *bs, DSP_BYTE *dsp_buffer, uint32 size); |
|
151 |
|
152 |
|
153 /** |
|
154 * Retrieves size of bitstream in bytes. |
|
155 * |
|
156 * @param bs Bitstream handle |
|
157 * |
|
158 * @return Size of bitstream |
|
159 */ |
|
160 IMPORT_C uint32 BsGetBufSize(TBitStream *bs); |
|
161 |
|
162 IMPORT_C uint32 BsGetBufOriginalSize(TBitStream *bs); |
|
163 |
|
164 /** |
|
165 * Retrieves number of bits read from the bitstream. |
|
166 * |
|
167 * @param bs Bitstream handle |
|
168 */ |
|
169 IMPORT_C uint32 BsGetBitsRead(TBitStream *bs); |
|
170 |
|
171 IMPORT_C void BsSetBitsRead(TBitStream *bs, uint32 bits_read); |
|
172 IMPORT_C void BsClearBitsRead(TBitStream *bs); |
|
173 |
|
174 /** |
|
175 * Resets bitstream. |
|
176 * |
|
177 * @param bs Bitstream handle |
|
178 */ |
|
179 IMPORT_C void BsReset(TBitStream *bs); |
|
180 |
|
181 /** |
|
182 * Retrieves the number of bytes left in the bitstream. |
|
183 * |
|
184 * @param bs Bitstream handle |
|
185 * |
|
186 * @return Number of bytes left in the bitstream |
|
187 */ |
|
188 IMPORT_C uint32 BsSlotsLeft(TBitStream *bs); |
|
189 |
|
190 /** |
|
191 * Appends bits from one bitstream to another bitstream. |
|
192 * |
|
193 * @param bs Source btstream handle |
|
194 * @param br Destination btstream handle |
|
195 * @param n Number of bytes to copy |
|
196 */ |
|
197 IMPORT_C void BsMoveBytes(TBitStream *bs, TBitStream *br, int16 n); |
|
198 |
|
199 /** |
|
200 * Appends bits from one specified buffer to bitstream. |
|
201 * |
|
202 * @param bs Destination btstream handle |
|
203 * @param outBuf Source buffer |
|
204 * @param bytesToCopy Number of bytes to copy |
|
205 * |
|
206 * @return Number of bytes appended |
|
207 */ |
|
208 IMPORT_C uint32 BsCopyBytes(TBitStream *bs, uint8 *outBuf, uint32 bytesToCopy); |
|
209 |
|
210 /** |
|
211 * Appends bits from one source to destination bitstream. |
|
212 * |
|
213 * @param bs Destination btstream handle |
|
214 * @param outBuf Source bitstream handle |
|
215 * @param bitsToCopy Number of bits to copy |
|
216 */ |
|
217 IMPORT_C void |
|
218 BsCopyBits(TBitStream *bsSrc, TBitStream *bsDst, int32 bitsToCopy); |
|
219 |
|
220 /** |
|
221 * Rewinds the read index of the bitstream. |
|
222 * |
|
223 * @param bs Bitstream handle |
|
224 * @param n Number of bits to rewind |
|
225 */ |
|
226 IMPORT_C void BsRewindNBits(TBitStream *bs, uint32 nBits); |
|
227 |
|
228 IMPORT_C void BsBufferUpdate(TBitStream *bs, int32 bytesRead); |
|
229 |
|
230 IMPORT_C void BsSaveBufState(TBitStream *bsSrc, TBitStream *bsDst); |
|
231 |
|
232 /** |
|
233 * Writes bits to bitstream. |
|
234 * |
|
235 * @param bs Bitstream handle |
|
236 * @param n Number of bits to write |
|
237 * @param word Data bits to write |
|
238 */ |
|
239 IMPORT_C void BsPutBits(TBitStream *bs, int16 n, uint32 word); |
|
240 |
|
241 /** |
|
242 * Byte aligns bitstream by writing '0' bits. |
|
243 * |
|
244 * @param bs Bitstream handle |
|
245 * |
|
246 * @return Number of bits written |
|
247 */ |
|
248 IMPORT_C int16 BsPutBitsByteAlign(TBitStream *bs); |
|
249 |
|
250 /** |
|
251 * Reads bits from bitstream. |
|
252 * |
|
253 * @param bs Bitstream handle |
|
254 * @param n Number of bits to read |
|
255 * |
|
256 * @return Read data bits |
|
257 */ |
|
258 IMPORT_C uint32 BsGetBits(TBitStream *bs, int16 n); |
|
259 |
|
260 /** |
|
261 * Byte aligns bitstream by advanding read index. |
|
262 * |
|
263 * @param bs Bitstream handle |
|
264 * |
|
265 * @return Number of bits skipped |
|
266 */ |
|
267 IMPORT_C int16 BsByteAlign(TBitStream *bs); |
|
268 |
|
269 /** |
|
270 * Reads bits from bitstream without updating read index. |
|
271 * |
|
272 * @param bs Bitstream handle |
|
273 * @param n Number of bits to lookahead |
|
274 * |
|
275 * @return Read data bits |
|
276 */ |
|
277 IMPORT_C uint32 BsLookAhead(TBitStream *bs, int16 n); |
|
278 |
|
279 /** |
|
280 * Advances bitstream read index (8 bits at maximum!). |
|
281 * |
|
282 * @param bs Bitstream handle |
|
283 * @param n Number of bits to skip. Note that this can at maximum be 8 bits! |
|
284 */ |
|
285 IMPORT_C void BsSkipBits(TBitStream *bs, int16 n); |
|
286 |
|
287 /** |
|
288 * Advances bitstream read index. |
|
289 * |
|
290 * @param bs Bitstream handle |
|
291 * @param n Number of bits to skip |
|
292 */ |
|
293 IMPORT_C void BsSkipNBits(TBitStream *bs, int32 n); |
|
294 |
|
295 #endif /*-- BITSTREAM_H_ --*/ |
|