Home | History | Annotate | Download | only in src
      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: updateBitBufWordPtr
     28 * description:  update Bit Buffer pointer
     29 *
     30 *****************************************************************************/
     31 static void updateBitBufWordPtr(HANDLE_BIT_BUF hBitBuf,
     32                                 UWord8 **pBitBufWord,
     33                                 Word16   cnt)
     34 {
     35   *pBitBufWord += cnt;
     36 
     37 
     38   if(*pBitBufWord > hBitBuf->pBitBufEnd) {
     39     *pBitBufWord -= (hBitBuf->pBitBufEnd - hBitBuf->pBitBufBase + 1);
     40   }
     41 
     42   if(*pBitBufWord < hBitBuf->pBitBufBase) {
     43     *pBitBufWord += (hBitBuf->pBitBufEnd - hBitBuf->pBitBufBase + 1);
     44   }
     45 }
     46 
     47 
     48 /*****************************************************************************
     49 *
     50 * function name: CreateBitBuffer
     51 * description:  create and init Bit Buffer Management
     52 *
     53 *****************************************************************************/
     54 HANDLE_BIT_BUF CreateBitBuffer(HANDLE_BIT_BUF hBitBuf,
     55                                UWord8 *pBitBufBase,
     56                                Word16  bitBufSize)
     57 {
     58   assert(bitBufSize*8 <= 32768);
     59 
     60   hBitBuf->pBitBufBase = pBitBufBase;
     61   hBitBuf->pBitBufEnd  = pBitBufBase + bitBufSize - 1;
     62 
     63   hBitBuf->pWriteNext  = pBitBufBase;
     64 
     65   hBitBuf->cache       = 0;
     66 
     67   hBitBuf->wBitPos     = 0;
     68   hBitBuf->cntBits     = 0;
     69 
     70   hBitBuf->size        = (bitBufSize << 3);
     71   hBitBuf->isValid     = 1;
     72 
     73   return hBitBuf;
     74 }
     75 
     76 /*****************************************************************************
     77 *
     78 * function name: DeleteBitBuffer
     79 * description:  uninit Bit Buffer Management
     80 *
     81 *****************************************************************************/
     82 void DeleteBitBuffer(HANDLE_BIT_BUF *hBitBuf)
     83 {
     84   if(*hBitBuf)
     85 	(*hBitBuf)->isValid = 0;
     86   *hBitBuf = NULL;
     87 }
     88 
     89 /*****************************************************************************
     90 *
     91 * function name: ResetBitBuf
     92 * description:  reset Bit Buffer Management
     93 *
     94 *****************************************************************************/
     95 void ResetBitBuf(HANDLE_BIT_BUF hBitBuf,
     96                  UWord8 *pBitBufBase,
     97                  Word16  bitBufSize)
     98 {
     99   hBitBuf->pBitBufBase = pBitBufBase;
    100   hBitBuf->pBitBufEnd  = pBitBufBase + bitBufSize - 1;
    101 
    102 
    103   hBitBuf->pWriteNext  = pBitBufBase;
    104 
    105   hBitBuf->wBitPos     = 0;
    106   hBitBuf->cntBits     = 0;
    107 
    108   hBitBuf->cache	   = 0;
    109 }
    110 
    111 /*****************************************************************************
    112 *
    113 * function name: CopyBitBuf
    114 * description:  copy Bit Buffer Management
    115 *
    116 *****************************************************************************/
    117 void CopyBitBuf(HANDLE_BIT_BUF hBitBufSrc,
    118                 HANDLE_BIT_BUF hBitBufDst)
    119 {
    120   *hBitBufDst = *hBitBufSrc;
    121 }
    122 
    123 /*****************************************************************************
    124 *
    125 * function name: GetBitsAvail
    126 * description:  get available bits
    127 *
    128 *****************************************************************************/
    129 Word16 GetBitsAvail(HANDLE_BIT_BUF hBitBuf)
    130 {
    131   return hBitBuf->cntBits;
    132 }
    133 
    134 /*****************************************************************************
    135 *
    136 * function name: WriteBits
    137 * description:  write bits to the buffer
    138 *
    139 *****************************************************************************/
    140 Word16 WriteBits(HANDLE_BIT_BUF hBitBuf,
    141                  UWord32 writeValue,
    142                  Word16 noBitsToWrite)
    143 {
    144   Word16 wBitPos;
    145 
    146   assert(noBitsToWrite <= (Word16)sizeof(Word32)*8);
    147 
    148   if(noBitsToWrite == 0)
    149 	  return noBitsToWrite;
    150 
    151   hBitBuf->cntBits += noBitsToWrite;
    152 
    153   wBitPos = hBitBuf->wBitPos;
    154   wBitPos += noBitsToWrite;
    155   writeValue &= ~(0xffffffff << noBitsToWrite); // Mask out everything except the lowest noBitsToWrite bits
    156   writeValue <<= 32 - wBitPos;
    157   writeValue |= hBitBuf->cache;
    158 
    159   while (wBitPos >= 8)
    160   {
    161 	  UWord8 tmp;
    162 	  tmp = (UWord8)((writeValue >> 24) & 0xFF);
    163 
    164 	  *hBitBuf->pWriteNext++ = tmp;
    165 	  writeValue <<= 8;
    166 	  wBitPos -= 8;
    167   }
    168 
    169   hBitBuf->wBitPos = wBitPos;
    170   hBitBuf->cache = writeValue;
    171 
    172   return noBitsToWrite;
    173 }
    174