• 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    golomb_common.h
33  *
34  * \brief   Exponential Golomb entropy coding/decoding routine
35  *
36  * \date    03/12/2015 Created
37  *
38  *************************************************************************************
39  */
40 #ifndef EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
41 #define EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
42 
43 #include "typedefs.h"
44 
45 namespace WelsCommon {
46 
47 #define WRITE_BE_32(ptr, val) do { \
48         (ptr)[0] = (val) >> 24; \
49         (ptr)[1] = (val) >> 16; \
50         (ptr)[2] = (val) >>  8; \
51         (ptr)[3] = (val) >>  0; \
52     } while (0)
53 /************************************************************************/
54 /* GOLOMB CODIMG FOR WELS COMMON                                        */
55 /************************************************************************/
56 
57 
58 /*!
59  * \brief   initialize bitstream writing
60  *
61  * \param   pBs     Bit string auxiliary pointer
62  * \param   pBuf    bit-stream pBuffer
63  * \param   iSize   iSize in bits for decoder; iSize in bytes for encoder
64  *
65  * \return  iSize of pBuffer pData in byte; failed in -1 return
66  */
InitBits(SBitStringAux * pBs,const uint8_t * kpBuf,const int32_t kiSize)67 static inline int32_t InitBits (SBitStringAux* pBs, const uint8_t* kpBuf, const int32_t kiSize) {
68   uint8_t* ptr = (uint8_t*)kpBuf;
69 
70   pBs->pStartBuf = ptr;
71   pBs->pCurBuf   = ptr;
72   pBs->pEndBuf   = ptr + kiSize;
73   pBs->iLeftBits = 32;
74   pBs->uiCurBits = 0;
75 
76   return kiSize;
77 }
78 
BsWriteBits(PBitStringAux pBitString,int32_t iLen,const uint32_t kuiValue)79 static inline int32_t BsWriteBits (PBitStringAux pBitString, int32_t iLen, const uint32_t kuiValue) {
80   if (iLen < pBitString->iLeftBits) {
81     pBitString->uiCurBits = (pBitString->uiCurBits << iLen) | kuiValue;
82     pBitString->iLeftBits -= iLen;
83   } else {
84     iLen -= pBitString->iLeftBits;
85     pBitString->uiCurBits = (pBitString->uiCurBits << pBitString->iLeftBits) | (kuiValue >> iLen);
86     WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits);
87     pBitString->pCurBuf += 4;
88     pBitString->uiCurBits = kuiValue & ((1 << iLen) - 1);
89     pBitString->iLeftBits = 32 - iLen;
90   }
91   return 0;
92 }
93 
94 /*
95  *  Write 1 bit
96  */
BsWriteOneBit(PBitStringAux pBitString,const uint32_t kuiValue)97 static inline int32_t BsWriteOneBit (PBitStringAux pBitString, const uint32_t kuiValue) {
98   BsWriteBits (pBitString, 1, kuiValue);
99   return 0;
100 }
101 
BsFlush(PBitStringAux pBitString)102 static inline int32_t BsFlush (PBitStringAux pBitString) {
103   WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits << pBitString->iLeftBits);
104   pBitString->pCurBuf += 4 - pBitString->iLeftBits / 8;
105   pBitString->iLeftBits = 32;
106   pBitString->uiCurBits = 0;
107   return 0;
108 }
109 
110 /*
111  *  Write unsigned exp golomb codes
112  */
113 
BsWriteUE(PBitStringAux pBitString,const uint32_t kuiValue)114 static inline int32_t BsWriteUE (PBitStringAux pBitString, const uint32_t kuiValue) {
115   uint32_t iTmpValue = kuiValue + 1;
116   if (256 > kuiValue) {
117     BsWriteBits (pBitString, g_kuiGolombUELength[kuiValue], kuiValue + 1);
118   } else {
119     uint32_t n = 0;
120     if (iTmpValue & 0xffff0000) {
121       iTmpValue >>= 16;
122       n += 16;
123     }
124     if (iTmpValue & 0xff00) {
125       iTmpValue >>= 8;
126       n += 8;
127     }
128 
129     //n += (g_kuiGolombUELength[iTmpValue] >> 1);
130 
131     n += (g_kuiGolombUELength[iTmpValue - 1] >> 1);
132     BsWriteBits (pBitString, (n << 1) + 1, kuiValue + 1);
133   }
134   return 0;
135 }
136 
137 /*
138  *  Write signed exp golomb codes
139  */
BsWriteSE(PBitStringAux pBitString,const int32_t kiValue)140 static inline int32_t BsWriteSE (PBitStringAux pBitString, const int32_t kiValue) {
141   uint32_t iTmpValue;
142   if (0 == kiValue) {
143     BsWriteOneBit (pBitString, 1);
144   } else if (0 < kiValue) {
145     iTmpValue = (kiValue << 1) - 1;
146     BsWriteUE (pBitString, iTmpValue);
147   } else {
148     iTmpValue = ((-kiValue) << 1);
149     BsWriteUE (pBitString, iTmpValue);
150   }
151   return 0;
152 }
153 
154 
155 /*
156  *  Write RBSP trailing bits
157  */
BsRbspTrailingBits(PBitStringAux pBitString)158 static inline int32_t BsRbspTrailingBits (PBitStringAux pBitString) {
159   BsWriteOneBit (pBitString, 1);
160   BsFlush (pBitString);
161 
162   return 0;
163 }
164 
165 }
166 #endif//EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
167