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.h 18 19 Content: Bit Buffer Management structure and functions 20 21 *******************************************************************************/ 22 23 #ifndef BITBUFFER_H 24 #define BITBUFFER_H 25 26 #include "typedef.h" 27 28 29 enum direction 30 { 31 forwardDirection, 32 backwardDirection 33 }; 34 35 36 /*! 37 The pointer 'pReadNext' points to the next available word, where bits can be read from. The pointer 38 'pWriteNext' points to the next available word, where bits can be written to. The pointer pBitBufBase 39 points to the start of the bitstream buffer and the pointer pBitBufEnd points to the end of the bitstream 40 buffer. The two pointers are used as lower-bound respectively upper-bound address for the modulo addressing 41 mode. 42 43 The element cntBits contains the currently available bits in the bit buffer. It will be incremented when 44 bits are written to the bitstream buffer and decremented when bits are read from the bitstream buffer. 45 */ 46 struct BIT_BUF 47 { 48 UWord8 *pBitBufBase; /*!< pointer points to first position in bitstream buffer */ 49 UWord8 *pBitBufEnd; /*!< pointer points to last position in bitstream buffer */ 50 51 UWord8 *pWriteNext; /*!< pointer points to next available word in bitstream buffer to write */ 52 53 UWord32 cache; 54 55 Word16 wBitPos; /*!< 31<=wBitPos<=0*/ 56 Word16 cntBits; /*!< number of available bits in the bitstream buffer 57 write bits to bitstream buffer => increment cntBits 58 read bits from bitstream buffer => decrement cntBits */ 59 Word16 size; /*!< size of bitbuffer in bits */ 60 Word16 isValid; /*!< indicates whether the instance has been initialized */ 61 }; /* size Word16: 8 */ 62 63 /*! Define pointer to bit buffer structure */ 64 typedef struct BIT_BUF *HANDLE_BIT_BUF; 65 66 67 HANDLE_BIT_BUF CreateBitBuffer(HANDLE_BIT_BUF hBitBuf, 68 UWord8 *pBitBufBase, 69 Word16 bitBufSize); 70 71 72 void DeleteBitBuffer(HANDLE_BIT_BUF *hBitBuf); 73 74 75 Word16 GetBitsAvail(HANDLE_BIT_BUF hBitBuf); 76 77 78 Word16 WriteBits(HANDLE_BIT_BUF hBitBuf, 79 UWord32 writeValue, 80 Word16 noBitsToWrite); 81 82 void ResetBitBuf(HANDLE_BIT_BUF hBitBuf, 83 UWord8 *pBitBufBase, 84 Word16 bitBufSize); 85 86 #define GetNrBitsAvailable(hBitBuf) ( (hBitBuf)->cntBits) 87 #define GetNrBitsRead(hBitBuf) ((hBitBuf)->size-(hBitBuf)->cntBits) 88 89 #endif /* BITBUFFER_H */ 90