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 svc_enc_golomb.h
33 *
34 * \brief Exponential Golomb entropy coding routine
35 *
36 * \date 03/13/2009 Created
37 *
38 *************************************************************************************
39 */
40 #ifndef WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
41 #define WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
42
43 #include "wels_common_defs.h"
44 #include "golomb_common.h"
45
46 using namespace WelsCommon;
47
48 namespace WelsEnc {
49
50 /************************************************************************/
51 /* GOLOMB CODIMG FOR WELS ENCODER ONLY */
52 /************************************************************************/
53
54
55 /*
56 * Get size of unsigned exp golomb codes
57 */
BsSizeUE(const uint32_t kiValue)58 static inline uint32_t BsSizeUE (const uint32_t kiValue) {
59 if (256 > kiValue) {
60 return g_kuiGolombUELength[kiValue];
61 } else {
62 uint32_t n = 0;
63 uint32_t iTmpValue = kiValue + 1;
64
65 if (iTmpValue & 0xffff0000) {
66 iTmpValue >>= 16;
67 n += 16;
68 }
69 if (iTmpValue & 0xff00) {
70 iTmpValue >>= 8;
71 n += 8;
72 }
73
74 //n += (g_kuiGolombUELength[iTmpValue] >> 1);
75 n += (g_kuiGolombUELength[iTmpValue - 1] >> 1);
76 return ((n << 1) + 1);
77
78 }
79 }
80
81 /*
82 * Get size of signed exp golomb codes
83 */
BsSizeSE(const int32_t kiValue)84 static inline uint32_t BsSizeSE (const int32_t kiValue) {
85 uint32_t iTmpValue;
86 if (0 == kiValue) {
87 return 1;
88 } else if (0 < kiValue) {
89 iTmpValue = (kiValue << 1) - 1;
90 return BsSizeUE (iTmpValue);
91 } else {
92 iTmpValue = ((-kiValue) << 1);
93 return BsSizeUE (iTmpValue);
94 }
95 }
96
97 /*
98 * Write truncated exp golomb codes
99 */
BsWriteTE(SBitStringAux * pBs,const int32_t kiX,const uint32_t kuiValue)100 static inline void BsWriteTE (SBitStringAux* pBs, const int32_t kiX, const uint32_t kuiValue) {
101 if (1 == kiX) {
102 BsWriteOneBit (pBs, !kuiValue);
103 } else {
104 BsWriteUE (pBs, kuiValue);
105 }
106 }
107
BsGetBitsPos(SBitStringAux * pBs)108 static inline int32_t BsGetBitsPos (SBitStringAux* pBs) {
109 return (int32_t) (((pBs->pCurBuf - pBs->pStartBuf) << 3) + 32 - pBs->iLeftBits);
110 }
111
BsAlign(SBitStringAux * pBs)112 static inline void BsAlign( SBitStringAux* pBs )
113 {
114 if( pBs->iLeftBits&7 )
115 {
116 pBs->uiCurBits <<= pBs->iLeftBits&7;
117 pBs->uiCurBits |= (1 << (pBs->iLeftBits&7)) - 1;
118 pBs->iLeftBits &= ~7;
119 }
120 BsFlush(pBs );
121 }
122 }
123 #endif//WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
124