• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2  * \copy
3  *     Copyright (c)  2009-2013, Cisco Systems
4  *     All rights reserved.
5  *
6  *     Redistribution and use in source and binary forms, with or without
7  *     modification, are permitted provided that the following conditions
8  *     are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *
13  *        * Redistributions in binary form must reproduce the above copyright
14  *          notice, this list of conditions and the following disclaimer in
15  *          the documentation and/or other materials provided with the
16  *          distribution.
17  *
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *     POSSIBILITY OF SUCH DAMAGE.
30  *
31  *
32  * \file    bit_stream.cpp
33  *
34  * \brief   Reading / writing bit-stream
35  *
36  * \date    03/10/2009 Created
37  *
38  *************************************************************************************
39  */
40 #include "bit_stream.h"
41 #include "error_code.h"
42 
43 namespace WelsDec {
44 
GetValue4Bytes(uint8_t * pDstNal)45 inline uint32_t GetValue4Bytes (uint8_t* pDstNal) {
46   uint32_t uiValue = 0;
47   uiValue = (pDstNal[0] << 24) | (pDstNal[1] << 16) | (pDstNal[2] << 8) | (pDstNal[3]);
48   return uiValue;
49 }
50 
InitReadBits(PBitStringAux pBitString,intX_t iEndOffset)51 int32_t InitReadBits (PBitStringAux pBitString, intX_t iEndOffset) {
52   if (pBitString->pCurBuf >= (pBitString->pEndBuf - iEndOffset)) {
53     return ERR_INFO_INVALID_ACCESS;
54   }
55   pBitString->uiCurBits  = GetValue4Bytes (pBitString->pCurBuf);
56   pBitString->pCurBuf  += 4;
57   pBitString->iLeftBits = -16;
58   return ERR_NONE;
59 }
60 
61 /*!
62  * \brief   input bits for decoder or initialize bitstream writing in encoder
63  *
64  * \param   pBitString  Bit string auxiliary pointer
65  * \param   kpBuf       bit-stream buffer
66  * \param   kiSize      size in bits for decoder; size in bytes for encoder
67  *
68  * \return  0: success, other: fail
69  */
DecInitBits(PBitStringAux pBitString,const uint8_t * kpBuf,const int32_t kiSize)70 int32_t DecInitBits (PBitStringAux pBitString, const uint8_t* kpBuf, const int32_t kiSize) {
71   const int32_t kiSizeBuf = (kiSize + 7) >> 3;
72   uint8_t* pTmp = (uint8_t*)kpBuf;
73 
74   if (NULL == pTmp)
75     return ERR_INFO_INVALID_ACCESS;
76 
77   pBitString->pStartBuf = pTmp;             // buffer to start position
78   pBitString->pEndBuf   = pTmp + kiSizeBuf; // buffer + length
79   pBitString->iBits     = kiSize;           // count bits of overall bitstreaming inputindex;
80   pBitString->pCurBuf   = pBitString->pStartBuf;
81   int32_t iErr = InitReadBits (pBitString, 0);
82   if (iErr) {
83     return iErr;
84   }
85   return ERR_NONE;
86 }
87 
RBSP2EBSP(uint8_t * pDstBuf,uint8_t * pSrcBuf,const int32_t kiSize)88 void RBSP2EBSP (uint8_t* pDstBuf, uint8_t* pSrcBuf, const int32_t kiSize) {
89   uint8_t* pSrcPointer = pSrcBuf;
90   uint8_t* pDstPointer = pDstBuf;
91   uint8_t* pSrcEnd = pSrcBuf + kiSize;
92   int32_t iZeroCount = 0;
93 
94   while (pSrcPointer < pSrcEnd) {
95     if (iZeroCount == 2 && *pSrcPointer <= 3) {
96       //add the code 0x03
97       *pDstPointer++ = 3;
98       iZeroCount = 0;
99     }
100     if (*pSrcPointer == 0) {
101       ++ iZeroCount;
102     } else {
103       iZeroCount = 0;
104     }
105     *pDstPointer++ = *pSrcPointer++;
106   }
107 }
108 
109 } // namespace WelsDec
110 
111