shithub: openh264

Download patch

ref: 2d5a94ca75b4421185704e6f8f6b4c33e55cdda6
parent: 9243fdf58ad3de02a79e51ab71dfa1ac1c63d5d0
parent: ed1140b846a06c4ccee89e884fc8e58e8271b2e1
author: huili2 <[email protected]>
date: Mon Mar 16 06:47:44 EDT 2015

Merge pull request #1847 from huili2/bit_stream

merge bit_stream.h for encoder&decoder

--- a/codec/build/iOS/enc/welsenc/welsenc.xcodeproj/project.pbxproj
+++ b/codec/build/iOS/enc/welsenc/welsenc.xcodeproj/project.pbxproj
@@ -154,6 +154,7 @@
 		4CE4470618BC605C0017DF25 /* welsEncoderExt.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = welsEncoderExt.cpp; sourceTree = "<group>"; };
 		6CA38DA21991CACE003EAAE0 /* svc_motion_estimation.S */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; path = svc_motion_estimation.S; sourceTree = "<group>"; };
 		6CA38DA41991D31A003EAAE0 /* svc_motion_estimation_aarch64_neon.S */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.asm; name = svc_motion_estimation_aarch64_neon.S; path = arm64/svc_motion_estimation_aarch64_neon.S; sourceTree = "<group>"; };
+		98FE4C1A1AB0200C0031E2B4 /* golomb_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = golomb_common.h; path = ../../../common/inc/golomb_common.h; sourceTree = "<group>"; };
 		9AED664819469FAF009A3567 /* welsCodecTrace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = welsCodecTrace.h; path = ../../../common/inc/welsCodecTrace.h; sourceTree = "<group>"; };
 		9AED664C19469FC1009A3567 /* welsCodecTrace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = welsCodecTrace.cpp; path = ../../../common/src/welsCodecTrace.cpp; sourceTree = "<group>"; };
 		9AED66651946A2B3009A3567 /* utils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = utils.cpp; path = ../../../common/src/utils.cpp; sourceTree = "<group>"; };
@@ -254,6 +255,7 @@
 		4CE446A918BC605C0017DF25 /* inc */ = {
 			isa = PBXGroup;
 			children = (
+				98FE4C1A1AB0200C0031E2B4 /* golomb_common.h */,
 				F7E9997F19EBD3CE009B1021 /* svc_set_mb_syn.h */,
 				F7E9997E19EBD3C6009B1021 /* set_mb_syn_cabac.h */,
 				9AED66671946A2C4009A3567 /* utils.h */,
--- a/codec/build/win32/enc/WelsEncCore.vcproj
+++ b/codec/build/win32/enc/WelsEncCore.vcproj
@@ -494,11 +494,7 @@
 				RelativePath="..\..\..\encoder\core\inc\au_set.h"
 				>
 			</File>
-			<File
-				RelativePath="..\..\..\encoder\core\inc\bit_stream.h"
-				>
-			</File>
-			<File
+                        <File
 				RelativePath="..\..\..\common\inc\copy_mb.h"
 				>
 			</File>
@@ -551,6 +547,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\common\inc\golomb_common.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\common\inc\ls_defines.h"
 				>
 			</File>
@@ -715,7 +715,7 @@
 				>
 			</File>
 			<File
-				RelativePath="..\..\..\encoder\core\inc\wels_common_defs.h"
+				RelativePath="..\..\..\common\inc\wels_common_defs.h"
 				>
 			</File>
 			<File
--- /dev/null
+++ b/codec/common/inc/golomb_common.h
@@ -1,0 +1,166 @@
+/*!
+ * \copy
+ *     Copyright (c)  2009-2013, Cisco Systems
+ *     All rights reserved.
+ *
+ *     Redistribution and use in source and binary forms, with or without
+ *     modification, are permitted provided that the following conditions
+ *     are met:
+ *
+ *        * Redistributions of source code must retain the above copyright
+ *          notice, this list of conditions and the following disclaimer.
+ *
+ *        * Redistributions in binary form must reproduce the above copyright
+ *          notice, this list of conditions and the following disclaimer in
+ *          the documentation and/or other materials provided with the
+ *          distribution.
+ *
+ *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ *     POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * \file	golomb_common.h
+ *
+ * \brief	Exponential Golomb entropy coding/decoding routine
+ *
+ * \date	03/12/2015 Created
+ *
+ *************************************************************************************
+ */
+#ifndef EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
+#define EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
+
+#include "typedefs.h"
+
+namespace WelsCommon {
+
+#define WRITE_BE_32(ptr, val) do { \
+        (ptr)[0] = (val) >> 24; \
+        (ptr)[1] = (val) >> 16; \
+        (ptr)[2] = (val) >>  8; \
+        (ptr)[3] = (val) >>  0; \
+    } while (0)
+/************************************************************************/
+/* GOLOMB CODIMG FOR WELS COMMON                                        */
+/************************************************************************/
+
+
+/*!
+ * \brief	initialize bitstream writing
+ *
+ * \param	pBs		Bit string auxiliary pointer
+ * \param	pBuf	bit-stream pBuffer
+ * \param	iSize	iSize in bits for decoder; iSize in bytes for encoder
+ *
+ * \return	iSize of pBuffer pData in byte; failed in -1 return
+ */
+static inline int32_t InitBits (SBitStringAux* pBs, const uint8_t* kpBuf, const int32_t kiSize) {
+  uint8_t* ptr = (uint8_t*)kpBuf;
+
+  pBs->pStartBuf	= ptr;
+  pBs->pCurBuf	= ptr;
+  pBs->pEndBuf	= ptr + kiSize;
+  pBs->iLeftBits	= 32;
+  pBs->uiCurBits	= 0;
+
+  return kiSize;
+}
+
+static inline int32_t BsWriteBits (PBitStringAux pBitString, int32_t iLen, const uint32_t kuiValue) {
+  if (iLen < pBitString->iLeftBits) {
+    pBitString->uiCurBits = (pBitString->uiCurBits << iLen) | kuiValue;
+    pBitString->iLeftBits -= iLen;
+  } else {
+    iLen -= pBitString->iLeftBits;
+    pBitString->uiCurBits = (pBitString->uiCurBits << pBitString->iLeftBits) | (kuiValue >> iLen);
+    WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits);
+    pBitString->pCurBuf += 4;
+    pBitString->uiCurBits = kuiValue & ((1 << iLen) - 1);
+    pBitString->iLeftBits = 32 - iLen;
+  }
+  return 0;
+}
+
+/*
+ *	Write 1 bit
+ */
+static inline int32_t BsWriteOneBit (PBitStringAux pBitString, const uint32_t kuiValue) {
+  BsWriteBits (pBitString, 1, kuiValue);
+  return 0;
+}
+
+static inline int32_t BsFlush (PBitStringAux pBitString) {
+  WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits << pBitString->iLeftBits);
+  pBitString->pCurBuf += 4 - pBitString->iLeftBits / 8;
+  pBitString->iLeftBits = 32;
+  pBitString->uiCurBits = 0;
+  return 0;
+}
+
+/*
+ *	Write unsigned exp golomb codes
+ */
+
+static inline int32_t BsWriteUE (PBitStringAux pBitString, const uint32_t kuiValue) {
+  uint32_t iTmpValue = kuiValue + 1;
+  if (256 > kuiValue) {
+    BsWriteBits (pBitString, g_kuiGolombUELength[kuiValue], kuiValue + 1);
+  } else {
+    uint32_t n = 0;
+    if (iTmpValue & 0xffff0000) {
+      iTmpValue >>= 16;
+      n += 16;
+    }
+    if (iTmpValue & 0xff00) {
+      iTmpValue >>= 8;
+      n += 8;
+    }
+
+    //n += (g_kuiGolombUELength[iTmpValue] >> 1);
+
+    n += (g_kuiGolombUELength[iTmpValue - 1] >> 1);
+    BsWriteBits (pBitString, (n << 1) + 1, kuiValue + 1);
+  }
+  return 0;
+}
+
+/*
+ *	Write signed exp golomb codes
+ */
+static inline int32_t BsWriteSE (PBitStringAux pBitString, const int32_t kiValue) {
+  uint32_t iTmpValue;
+  if (0 == kiValue) {
+    BsWriteOneBit (pBitString, 1);
+  } else if (0 < kiValue) {
+    iTmpValue = (kiValue << 1) - 1;
+    BsWriteUE (pBitString, iTmpValue);
+  } else {
+    iTmpValue = ((-kiValue) << 1);
+    BsWriteUE (pBitString, iTmpValue);
+  }
+  return 0;
+}
+
+
+/*
+ *	Write RBSP trailing bits
+ */
+static inline int32_t BsRbspTrailingBits (PBitStringAux pBitString) {
+  BsWriteOneBit (pBitString, 1);
+  BsFlush (pBitString);
+
+  return 0;
+}
+
+}
+#endif//EXPONENTIAL_GOLOMB_ENTROPY_CODING_COMMON_H__
--- a/codec/common/inc/wels_common_defs.h
+++ b/codec/common/inc/wels_common_defs.h
@@ -73,42 +73,43 @@
 extern const uint8_t g_kuiCabacRangeLps[64][4];
 extern const int8_t g_kiCabacGlobalContextIdx[WELS_CONTEXT_COUNT][4][2];
 extern const uint8_t g_kuiStateTransTable[64][2];
+extern const uint32_t g_kuiGolombUELength[256];
 /*
  *	NAL Unit Type (5 Bits)
  */
 enum EWelsNalUnitType {
-  NAL_UNIT_UNSPEC_0			= 0,
+  NAL_UNIT_UNSPEC_0             = 0,
   NAL_UNIT_CODED_SLICE		= 1,
   NAL_UNIT_CODED_SLICE_DPA	= 2,
   NAL_UNIT_CODED_SLICE_DPB	= 3,
   NAL_UNIT_CODED_SLICE_DPC	= 4,
   NAL_UNIT_CODED_SLICE_IDR	= 5,
-  NAL_UNIT_SEI				= 6,
-  NAL_UNIT_SPS				= 7,
-  NAL_UNIT_PPS				= 8,
+  NAL_UNIT_SEI		        = 6,
+  NAL_UNIT_SPS		        = 7,
+  NAL_UNIT_PPS		        = 8,
   NAL_UNIT_AU_DELIMITER		= 9,
-  NAL_UNIT_END_OF_SEQ			= 10,
-  NAL_UNIT_END_OF_STR			= 11,
+  NAL_UNIT_END_OF_SEQ		= 10,
+  NAL_UNIT_END_OF_STR		= 11,
   NAL_UNIT_FILLER_DATA		= 12,
-  NAL_UNIT_SPS_EXT			= 13,
-  NAL_UNIT_PREFIX				= 14,
-  NAL_UNIT_SUBSET_SPS			= 15,
-  NAL_UNIT_RESV_16			= 16,
-  NAL_UNIT_RESV_17			= 17,
-  NAL_UNIT_RESV_18			= 18,
+  NAL_UNIT_SPS_EXT		= 13,
+  NAL_UNIT_PREFIX		= 14,
+  NAL_UNIT_SUBSET_SPS		= 15,
+  NAL_UNIT_RESV_16		= 16,
+  NAL_UNIT_RESV_17		= 17,
+  NAL_UNIT_RESV_18		= 18,
   NAL_UNIT_AUX_CODED_SLICE	= 19,
   NAL_UNIT_CODED_SLICE_EXT	= 20,
-  NAL_UNIT_RESV_21			= 21,
-  NAL_UNIT_RESV_22			= 22,
-  NAL_UNIT_RESV_23			= 23,
-  NAL_UNIT_UNSPEC_24			= 24,
-  NAL_UNIT_UNSPEC_25			= 25,
-  NAL_UNIT_UNSPEC_26			= 26,
-  NAL_UNIT_UNSPEC_27			= 27,
-  NAL_UNIT_UNSPEC_28			= 28,
-  NAL_UNIT_UNSPEC_29			= 29,
-  NAL_UNIT_UNSPEC_30			= 30,
-  NAL_UNIT_UNSPEC_31			= 31
+  NAL_UNIT_RESV_21		= 21,
+  NAL_UNIT_RESV_22		= 22,
+  NAL_UNIT_RESV_23		= 23,
+  NAL_UNIT_UNSPEC_24		= 24,
+  NAL_UNIT_UNSPEC_25		= 25,
+  NAL_UNIT_UNSPEC_26		= 26,
+  NAL_UNIT_UNSPEC_27		= 27,
+  NAL_UNIT_UNSPEC_28		= 28,
+  NAL_UNIT_UNSPEC_29		= 29,
+  NAL_UNIT_UNSPEC_30		= 30,
+  NAL_UNIT_UNSPEC_31		= 31
 };
 
 /*
@@ -118,7 +119,7 @@
 enum EWelsNalRefIdc {
   NRI_PRI_LOWEST	= 0,
   NRI_PRI_LOW		= 1,
-  NRI_PRI_HIGH	= 2,
+  NRI_PRI_HIGH	        = 2,
   NRI_PRI_HIGHEST	= 3
 };
 
@@ -139,14 +140,14 @@
 
 #define IS_VCL_NAL(t, ext_idx)			(g_keTypeMap[t][ext_idx] == VCL)
 #define IS_PARAM_SETS_NALS(t)			( (t) == NAL_UNIT_SPS || (t) == NAL_UNIT_PPS || (t) == NAL_UNIT_SUBSET_SPS )
-#define IS_SPS_NAL(t)					( (t) == NAL_UNIT_SPS )
+#define IS_SPS_NAL(t)				( (t) == NAL_UNIT_SPS )
 #define IS_SUBSET_SPS_NAL(t)			( (t) == NAL_UNIT_SUBSET_SPS )
-#define IS_PPS_NAL(t)					( (t) == NAL_UNIT_PPS )
-#define IS_SEI_NAL(t)					( (t) == NAL_UNIT_SEI )
-#define IS_PREFIX_NAL(t)				( (t) == NAL_UNIT_PREFIX )
+#define IS_PPS_NAL(t)				( (t) == NAL_UNIT_PPS )
+#define IS_SEI_NAL(t)				( (t) == NAL_UNIT_SEI )
+#define IS_PREFIX_NAL(t)			( (t) == NAL_UNIT_PREFIX )
 #define IS_SUBSET_SPS_USED(t)			( (t) == NAL_UNIT_SUBSET_SPS || (t) == NAL_UNIT_CODED_SLICE_EXT )
 #define IS_VCL_NAL_AVC_BASE(t)			( (t) == NAL_UNIT_CODED_SLICE || (t) == NAL_UNIT_CODED_SLICE_IDR )
-#define IS_NEW_INTRODUCED_SVC_NAL(t)	( (t) == NAL_UNIT_PREFIX || (t) == NAL_UNIT_CODED_SLICE_EXT )
+#define IS_NEW_INTRODUCED_SVC_NAL(t)            ( (t) == NAL_UNIT_PREFIX || (t) == NAL_UNIT_CODED_SLICE_EXT )
 
 
 /* Base SSlice Types
@@ -159,8 +160,8 @@
   P_SLICE	= 0,
   B_SLICE	= 1,
   I_SLICE	= 2,
-  SP_SLICE = 3,
-  SI_SLICE = 4,
+  SP_SLICE      = 3,
+  SI_SLICE      = 4,
   UNKNOWN_SLICE = 5
 };
 
@@ -201,14 +202,29 @@
  *	Memory Management Control Operation (MMCO) code
  */
 enum EMmcoCode {
-  MMCO_END			= 0,
+  MMCO_END		= 0,
   MMCO_SHORT2UNUSED	= 1,
   MMCO_LONG2UNUSED	= 2,
-  MMCO_SHORT2LONG		= 3,
+  MMCO_SHORT2LONG	= 3,
   MMCO_SET_MAX_LONG	= 4,
-  MMCO_RESET			= 5,
-  MMCO_LONG			= 6
+  MMCO_RESET		= 5,
+  MMCO_LONG		= 6
 };
+
+/*
+ *	Bit-stream auxiliary reading / writing
+ */
+typedef struct TagBitStringAux {
+  uint8_t* pStartBuf;	// buffer to start position
+  uint8_t* pEndBuf;	// buffer + length
+  int32_t  iBits;       // count bits of overall bitstreaming input
+
+  intX_t   iIndex;      //only for cavlc usage
+  uint8_t* pCurBuf;	// current reading position
+  uint32_t uiCurBits;
+  int32_t  iLeftBits;	// count number of available bits left ([1, 8]),
+  // need pointer to next byte start position in case 0 bit left then 8 instead
+} SBitStringAux, *PBitStringAux;
 
 /////////intra16x16  Luma
 #define I16_PRED_INVALID   -1
--- a/codec/common/src/common_tables.cpp
+++ b/codec/common/src/common_tables.cpp
@@ -712,5 +712,26 @@
 
 };
 
+// extern at svc_enc_golomb.h, golomb_common.h
+
+const uint32_t g_kuiGolombUELength[256] = {
+  1,  3,  3,  5,  5,  5,  5,  7,  7,  7,  7,  7,  7,  7,  7,     //14
+  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, //30
+  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,//46
+  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,//62
+  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,//
+  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
+  17
+};
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 }
--- a/codec/decoder/core/inc/bit_stream.h
+++ b/codec/decoder/core/inc/bit_stream.h
@@ -35,22 +35,12 @@
 #define WELS_BIT_STREAM_H__
 
 #include "typedefs.h"
-namespace WelsDec {
+#include "wels_common_defs.h"
+#include "golomb_common.h"
 
-/*
- *	Bit-stream auxiliary reading / writing
- */
-typedef struct TagBitStringAux {
-  uint8_t*		pStartBuf;	// buffer to start position
-  uint8_t*		pEndBuf;	// buffer + length
-  int32_t     iBits;       // count bits of overall bitstreaming input
+using namespace WelsCommon;
 
-  intX_t     iIndex;      //only for cavlc usage
-  uint8_t*		pCurBuf;	// current reading position
-  uint32_t    uiCurBits;
-  int32_t		iLeftBits;	// count number of available bits left ([1, 8]),
-// need pointer to next byte start position in case 0 bit left then 8 instead
-} SBitStringAux, *PBitStringAux;
+namespace WelsDec {
 
 /*!
  * \brief	input bits for decoder or initialize bitstream writing in encoder
@@ -61,18 +51,10 @@
  *
  * \return	size of buffer data in byte; failed in -1 return
  */
-int32_t InitBits (PBitStringAux pBitString, const uint8_t* kpBuf, const int32_t kiSize);
+int32_t DecInitBits (PBitStringAux pBitString, const uint8_t* kpBuf, const int32_t kiSize);
 
 int32_t InitReadBits (PBitStringAux pBitString, intX_t iEndOffset);
 
-//The following for writing bs in decoder for Parse Only purpose
-void DecInitBitsForEncoding (PBitStringAux pBitString, uint8_t* kpBuf, const int32_t kiSize);
-int32_t DecBsWriteBits (PBitStringAux pBitString, int32_t iLen, const uint32_t kuiValue);
-int32_t DecBsWriteOneBit (PBitStringAux pBitString, const uint32_t kuiValue);
-int32_t DecBsFlush (PBitStringAux pBitString);
-int32_t DecBsWriteUe (PBitStringAux pBitString, const uint32_t kuiValue);
-int32_t DecBsWriteSe (PBitStringAux pBitString, const int32_t kiValue);
-int32_t DecBsRbspTrailingBits (PBitStringAux pBitString);
 void RBSP2EBSP (uint8_t* pDstBuf, uint8_t* pSrcBuf, const int32_t kiSize);
 
 } // namespace WelsDec
--- a/codec/decoder/core/src/au_parser.cpp
+++ b/codec/decoder/core/src/au_parser.cpp
@@ -246,9 +246,9 @@
       pBs = &pCtx->sBs;
       iBitSize = (iNalSize << 3) - BsGetTrailingBits (pNal + iNalSize - 1); // convert into bit
 
-      iErr = InitBits (pBs, pNal, iBitSize);
+      iErr = DecInitBits (pBs, pNal, iBitSize);
       if (iErr) {
-        WelsLog (pLogCtx, WELS_LOG_ERROR, "NAL_UNIT_PREFIX: InitBits() fail due invalid access.");
+        WelsLog (pLogCtx, WELS_LOG_ERROR, "NAL_UNIT_PREFIX: DecInitBits() fail due invalid access.");
         pCtx->iErrorCode	|= dsBitstreamError;
         return NULL;
       }
@@ -375,7 +375,7 @@
 
     pBs = &pCurAu->pNalUnitsList[uiAvailNalNum - 1]->sNalData.sVclNal.sSliceBitsRead;
     iBitSize = (iNalSize << 3) - BsGetTrailingBits (pNal + iNalSize - 1); // convert into bit
-    iErr = InitBits (pBs, pNal, iBitSize);
+    iErr = DecInitBits (pBs, pNal, iBitSize);
     if (iErr) {
       ForceClearCurrentNal (pCurAu);
       if (uiAvailNalNum > 1) {
@@ -384,7 +384,7 @@
           pCtx->bAuReadyFlag = true;
         }
       }
-      WelsLog (pLogCtx, WELS_LOG_ERROR, "NAL_UNIT_CODED_SLICE: InitBits() fail due invalid access.");
+      WelsLog (pLogCtx, WELS_LOG_ERROR, "NAL_UNIT_CODED_SLICE: DecInitBits() fail due invalid access.");
       pCtx->iErrorCode	|= dsBitstreamError;
       return NULL;
     }
@@ -592,7 +592,7 @@
   case NAL_UNIT_SPS:
   case NAL_UNIT_SUBSET_SPS:
     if (iBitSize > 0) {
-      iErr = InitBits (pBs, pRbsp, iBitSize);
+      iErr = DecInitBits (pBs, pRbsp, iBitSize);
       if (ERR_NONE != iErr) {
         if (pCtx->eErrorConMethod == ERROR_CON_DISABLE)
           pCtx->iErrorCode |= dsNoParamSets;
@@ -614,7 +614,7 @@
 
   case NAL_UNIT_PPS:
     if (iBitSize > 0) {
-      iErr = InitBits (pBs, pRbsp, iBitSize);
+      iErr = DecInitBits (pBs, pRbsp, iBitSize);
       if (ERR_NONE != iErr) {
         if (pCtx->eErrorConMethod == ERROR_CON_DISABLE)
           pCtx->iErrorCode |= dsNoParamSets;
@@ -1149,46 +1149,46 @@
         pCtx->iErrorCode |= dsOutOfMemory;
         return pCtx->iErrorCode;
       }
-      DecInitBitsForEncoding (&sSubsetSpsBs, pBsBuf, (int32_t) (pBs->pEndBuf - pBs->pStartBuf));
-      DecBsWriteBits (&sSubsetSpsBs, 8, 77); //profile_idc, forced to Main profile
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet0Flag); // constraint_set0_flag
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet1Flag); // constraint_set1_flag
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet2Flag); // constraint_set2_flag
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet3Flag); // constraint_set3_flag
-      DecBsWriteBits (&sSubsetSpsBs, 4, 0); //constraint_set4_flag, constraint_set5_flag, reserved_zero_2bits
-      DecBsWriteBits (&sSubsetSpsBs, 8, pSps->uiLevelIdc); //level_idc
-      DecBsWriteUe (&sSubsetSpsBs, pSps->iSpsId); //sps_id
-      DecBsWriteUe (&sSubsetSpsBs, pSps->uiLog2MaxFrameNum - 4); //log2_max_frame_num_minus4
-      DecBsWriteUe (&sSubsetSpsBs, pSps->uiPocType); //pic_order_cnt_type
+      InitBits (&sSubsetSpsBs, pBsBuf, (int32_t) (pBs->pEndBuf - pBs->pStartBuf));
+      BsWriteBits (&sSubsetSpsBs, 8, 77); //profile_idc, forced to Main profile
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet0Flag); // constraint_set0_flag
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet1Flag); // constraint_set1_flag
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet2Flag); // constraint_set2_flag
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bConstraintSet3Flag); // constraint_set3_flag
+      BsWriteBits (&sSubsetSpsBs, 4, 0); //constraint_set4_flag, constraint_set5_flag, reserved_zero_2bits
+      BsWriteBits (&sSubsetSpsBs, 8, pSps->uiLevelIdc); //level_idc
+      BsWriteUE (&sSubsetSpsBs, pSps->iSpsId); //sps_id
+      BsWriteUE (&sSubsetSpsBs, pSps->uiLog2MaxFrameNum - 4); //log2_max_frame_num_minus4
+      BsWriteUE (&sSubsetSpsBs, pSps->uiPocType); //pic_order_cnt_type
       if (pSps->uiPocType == 0) {
-        DecBsWriteUe (&sSubsetSpsBs, pSps->iLog2MaxPocLsb - 4); //log2_max_pic_order_cnt_lsb_minus4
+        BsWriteUE (&sSubsetSpsBs, pSps->iLog2MaxPocLsb - 4); //log2_max_pic_order_cnt_lsb_minus4
       } else if (pSps->uiPocType == 1) {
-        DecBsWriteOneBit (&sSubsetSpsBs, pSps->bDeltaPicOrderAlwaysZeroFlag); //delta_pic_order_always_zero_flag
-        DecBsWriteSe (&sSubsetSpsBs, pSps->iOffsetForNonRefPic); //offset_for_no_ref_pic
-        DecBsWriteSe (&sSubsetSpsBs, pSps->iOffsetForTopToBottomField); //offset_for_top_to_bottom_field
-        DecBsWriteUe (&sSubsetSpsBs, pSps->iNumRefFramesInPocCycle); //num_ref_frames_in_pic_order_cnt_cycle
+        BsWriteOneBit (&sSubsetSpsBs, pSps->bDeltaPicOrderAlwaysZeroFlag); //delta_pic_order_always_zero_flag
+        BsWriteSE (&sSubsetSpsBs, pSps->iOffsetForNonRefPic); //offset_for_no_ref_pic
+        BsWriteSE (&sSubsetSpsBs, pSps->iOffsetForTopToBottomField); //offset_for_top_to_bottom_field
+        BsWriteUE (&sSubsetSpsBs, pSps->iNumRefFramesInPocCycle); //num_ref_frames_in_pic_order_cnt_cycle
         for (int32_t i = 0; i < pSps->iNumRefFramesInPocCycle; ++i) {
-          DecBsWriteSe (&sSubsetSpsBs, pSps->iOffsetForRefFrame[i]); //offset_for_ref_frame[i]
+          BsWriteSE (&sSubsetSpsBs, pSps->iOffsetForRefFrame[i]); //offset_for_ref_frame[i]
         }
       }
-      DecBsWriteUe (&sSubsetSpsBs, pSps->iNumRefFrames); //max_num_ref_frames
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bGapsInFrameNumValueAllowedFlag); //gaps_in_frame_num_value_allowed_flag
-      DecBsWriteUe (&sSubsetSpsBs, pSps->iMbWidth - 1); //pic_width_in_mbs_minus1
-      DecBsWriteUe (&sSubsetSpsBs, pSps->iMbHeight - 1); //pic_height_in_map_units_minus1
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bFrameMbsOnlyFlag); //frame_mbs_only_flag
+      BsWriteUE (&sSubsetSpsBs, pSps->iNumRefFrames); //max_num_ref_frames
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bGapsInFrameNumValueAllowedFlag); //gaps_in_frame_num_value_allowed_flag
+      BsWriteUE (&sSubsetSpsBs, pSps->iMbWidth - 1); //pic_width_in_mbs_minus1
+      BsWriteUE (&sSubsetSpsBs, pSps->iMbHeight - 1); //pic_height_in_map_units_minus1
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bFrameMbsOnlyFlag); //frame_mbs_only_flag
       if (!pSps->bFrameMbsOnlyFlag) {
-        DecBsWriteOneBit (&sSubsetSpsBs, pSps->bMbaffFlag); //mb_adaptive_frame_field_flag
+        BsWriteOneBit (&sSubsetSpsBs, pSps->bMbaffFlag); //mb_adaptive_frame_field_flag
       }
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bDirect8x8InferenceFlag); //direct_8x8_inference_flag
-      DecBsWriteOneBit (&sSubsetSpsBs, pSps->bFrameCroppingFlag); //frame_cropping_flag
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bDirect8x8InferenceFlag); //direct_8x8_inference_flag
+      BsWriteOneBit (&sSubsetSpsBs, pSps->bFrameCroppingFlag); //frame_cropping_flag
       if (pSps->bFrameCroppingFlag) {
-        DecBsWriteUe (&sSubsetSpsBs, pSps->sFrameCrop.iLeftOffset); //frame_crop_left_offset
-        DecBsWriteUe (&sSubsetSpsBs, pSps->sFrameCrop.iRightOffset); //frame_crop_right_offset
-        DecBsWriteUe (&sSubsetSpsBs, pSps->sFrameCrop.iTopOffset); //frame_crop_top_offset
-        DecBsWriteUe (&sSubsetSpsBs, pSps->sFrameCrop.iBottomOffset); //frame_crop_bottom_offset
+        BsWriteUE (&sSubsetSpsBs, pSps->sFrameCrop.iLeftOffset); //frame_crop_left_offset
+        BsWriteUE (&sSubsetSpsBs, pSps->sFrameCrop.iRightOffset); //frame_crop_right_offset
+        BsWriteUE (&sSubsetSpsBs, pSps->sFrameCrop.iTopOffset); //frame_crop_top_offset
+        BsWriteUE (&sSubsetSpsBs, pSps->sFrameCrop.iBottomOffset); //frame_crop_bottom_offset
       }
-      DecBsWriteOneBit (&sSubsetSpsBs, 0); //vui_parameters_present_flag
-      DecBsRbspTrailingBits (&sSubsetSpsBs); //finished, rbsp trailing bit
+      BsWriteOneBit (&sSubsetSpsBs, 0); //vui_parameters_present_flag
+      BsRbspTrailingBits (&sSubsetSpsBs); //finished, rbsp trailing bit
       int32_t iRbspSize = (int32_t) (sSubsetSpsBs.pCurBuf - sSubsetSpsBs.pStartBuf);
       RBSP2EBSP (pSpsBs->pSpsBsBuf + 5, sSubsetSpsBs.pStartBuf, iRbspSize);
       pSpsBs->uiSpsBsLen = (uint16_t) (sSubsetSpsBs.pCurBuf - sSubsetSpsBs.pStartBuf + 5);
--- a/codec/decoder/core/src/bit_stream.cpp
+++ b/codec/decoder/core/src/bit_stream.cpp
@@ -67,7 +67,7 @@
  *
  * \return	0: success, other: fail
  */
-int32_t InitBits (PBitStringAux pBitString, const uint8_t* kpBuf, const int32_t kiSize) {
+int32_t DecInitBits (PBitStringAux pBitString, const uint8_t* kpBuf, const int32_t kiSize) {
   const int32_t kiSizeBuf = (kiSize + 7) >> 3;
   uint8_t* pTmp = (uint8_t*)kpBuf;
 
@@ -83,115 +83,6 @@
     return iErr;
   }
   return ERR_NONE;
-}
-
-//Following for write bs in decoder
-void DecInitBitsForEncoding (PBitStringAux pBitString, uint8_t* pBuf, const int32_t kiSize) {
-  uint8_t* pPtr = pBuf;
-  pBitString->pStartBuf = pPtr;
-  pBitString->pCurBuf = pPtr;
-  pBitString->pEndBuf = pPtr + kiSize;
-  pBitString->iLeftBits = 32;
-  pBitString->uiCurBits = 0;
-}
-
-#define WRITE_BE_32(ptr, val) do { \
-    (ptr)[0] = (val) >> 24; \
-    (ptr)[1] = (val) >> 16; \
-    (ptr)[2] = (val) >>  8; \
-    (ptr)[3] = (val) >>  0; \
-  } while (0);
-
-int32_t DecBsWriteBits (PBitStringAux pBitString, int32_t iLen, const uint32_t kuiValue) {
-  if (iLen < pBitString->iLeftBits) {
-    pBitString->uiCurBits = (pBitString->uiCurBits << iLen) | kuiValue;
-    pBitString->iLeftBits -= iLen;
-  } else {
-    iLen -= pBitString->iLeftBits;
-    pBitString->uiCurBits = (pBitString->uiCurBits << pBitString->iLeftBits) | (kuiValue >> iLen);
-    WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits);
-    pBitString->pCurBuf += 4;
-    pBitString->uiCurBits = kuiValue & ((1 << iLen) - 1);
-    pBitString->iLeftBits = 32 - iLen;
-  }
-  return 0;
-}
-
-int32_t DecBsWriteOneBit (PBitStringAux pBitString, const uint32_t kuiValue) {
-  DecBsWriteBits (pBitString, 1, kuiValue);
-  return 0;
-}
-
-int32_t DecBsFlush (PBitStringAux pBitString) {
-  WRITE_BE_32 (pBitString->pCurBuf, pBitString->uiCurBits << pBitString->iLeftBits);
-  pBitString->pCurBuf += 4 - pBitString->iLeftBits / 8;
-  pBitString->iLeftBits = 32;
-  pBitString->uiCurBits = 0;
-  return 0;
-}
-
-const uint32_t g_kuiDecGolombUELength[256] = {
-  1,  3,  3,  5,  5,  5,  5,  7,  7,  7,  7,  7,  7,  7,  7,     //14
-  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, //30
-  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,//46
-  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,//62
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,//
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  17
-};
-
-int32_t DecBsWriteUe (PBitStringAux pBitString, const uint32_t kuiValue) {
-  uint32_t iTmpValue = kuiValue + 1;
-  if (256 > kuiValue) {
-    DecBsWriteBits (pBitString, g_kuiDecGolombUELength[kuiValue], kuiValue + 1);
-  } else {
-    uint32_t n = 0;
-    if (iTmpValue & 0xffff0000) {
-      iTmpValue >>= 16;
-      n += 16;
-    }
-    if (iTmpValue & 0xff) {
-      iTmpValue >>= 8;
-      n += 8;
-    }
-
-    //n += (g_kuiDecGolombUELength[iTmpValue] >> 1);
-
-    n += (g_kuiDecGolombUELength[iTmpValue - 1] >> 1);
-    DecBsWriteBits (pBitString, (n << 1) + 1, kuiValue + 1);
-  }
-  return 0;
-}
-
-int32_t DecBsWriteSe (PBitStringAux pBitString, const int32_t kiValue) {
-  uint32_t iTmpValue;
-  if (0 == kiValue) {
-    DecBsWriteOneBit (pBitString, 1);
-  } else if (0 < kiValue) {
-    iTmpValue = (kiValue << 1) - 1;
-    DecBsWriteUe (pBitString, iTmpValue);
-  } else {
-    iTmpValue = ((-kiValue) << 1);
-    DecBsWriteUe (pBitString, iTmpValue);
-  }
-  return 0;
-}
-
-int32_t DecBsRbspTrailingBits (PBitStringAux pBitString) {
-  DecBsWriteOneBit (pBitString, 1);
-  DecBsFlush (pBitString);
-
-  return 0;
 }
 
 void RBSP2EBSP (uint8_t* pDstBuf, uint8_t* pSrcBuf, const int32_t kiSize) {
--- a/codec/encoder/core/inc/au_set.h
+++ b/codec/encoder/core/inc/au_set.h
@@ -42,7 +42,6 @@
 #ifndef WELS_ACCESS_UNIT_WRITER_H__
 #define WELS_ACCESS_UNIT_WRITER_H__
 
-#include "bit_stream.h"
 #include "parameter_sets.h"
 #include "param_svc.h"
 #include "utils.h"
--- a/codec/encoder/core/inc/bit_stream.h
+++ /dev/null
@@ -1,77 +1,0 @@
-/*!
- * \copy
- *     Copyright (c)  2013, Cisco Systems
- *     All rights reserved.
- *
- *     Redistribution and use in source and binary forms, with or without
- *     modification, are permitted provided that the following conditions
- *     are met:
- *
- *        * Redistributions of source code must retain the above copyright
- *          notice, this list of conditions and the following disclaimer.
- *
- *        * Redistributions in binary form must reproduce the above copyright
- *          notice, this list of conditions and the following disclaimer in
- *          the documentation and/or other materials provided with the
- *          distribution.
- *
- *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- *     POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-//bit_stream.h	-	bit-stream reading and / writing auxiliary pData
-#ifndef WELS_BIT_STREAM_H__
-#define WELS_BIT_STREAM_H__
-
-#include "typedefs.h"
-
-namespace WelsEnc {
-//#include "macros.h"
-
-/*
- *	auxiliary struct for bit-stream reading / writing
- */
-typedef struct TagBitStringAux {
-  uint8_t*		pBuf;		// pBuffer to start position
-  uint8_t*		pBufEnd;	// pBuffer + length
-  uint8_t*		pBufPtr;	// current writing position
-  uint32_t    uiCurBits;
-  int32_t		iLeftBits;	// count number of available bits left ([1, 8]),
-// need pointer to next byte start position in case 0 bit left then 8 instead
-} SBitStringAux;
-
-/*!
- * \brief	input bits for decoder or initialize bitstream writing in encoder
- *
- * \param	pBs		Bit string auxiliary pointer
- * \param	pBuf		bit-stream pBuffer
- * \param	iSize	iSize in bits for decoder; iSize in bytes for encoder
- *
- * \return	iSize of pBuffer pData in byte; failed in -1 return
- */
-static inline int32_t InitBits (SBitStringAux* pBs, const uint8_t* kpBuf, const int32_t kiSize) {
-  uint8_t* ptr = (uint8_t*)kpBuf;
-
-  pBs->pBuf			= ptr;
-  pBs->pBufPtr		= ptr;
-  pBs->pBufEnd		= ptr + kiSize;
-  pBs->iLeftBits	= 32;
-  pBs->uiCurBits = 0;
-
-  return kiSize;
-}
-
-}
-
-#endif//WELS_BIT_STREAM_H__
--- a/codec/encoder/core/inc/mb_cache.h
+++ b/codec/encoder/core/inc/mb_cache.h
@@ -35,7 +35,6 @@
 #define WELS_MACROBLOCK_CACHE_H__
 
 #include "typedefs.h"
-#include "wels_common_basis.h"
 #include "wels_const.h"
 #include "macros.h"
 
--- a/codec/encoder/core/inc/nal_encap.h
+++ b/codec/encoder/core/inc/nal_encap.h
@@ -41,7 +41,6 @@
 #define WELS_NAL_UNIT_ENCAPSULATION_H__
 
 #include "typedefs.h"
-#include "bit_stream.h"
 #include "nal_prefix.h"
 
 //SBitStringAux
--- a/codec/encoder/core/inc/nal_prefix.h
+++ b/codec/encoder/core/inc/nal_prefix.h
@@ -35,7 +35,6 @@
 #define WELS_NAL_UNIT_PREFIX_H__
 
 #include "typedefs.h"
-#include "wels_common_basis.h"
 #include "slice.h"
 
 namespace WelsEnc {
--- a/codec/encoder/core/inc/param_svc.h
+++ b/codec/encoder/core/inc/param_svc.h
@@ -46,7 +46,6 @@
 #include "codec_def.h"
 #include "macros.h"
 #include "wels_const.h"
-#include "wels_common_basis.h"
 #include "rc.h"
 #include "svc_enc_slice_segment.h"
 #include "as264_common.h"
--- a/codec/encoder/core/inc/set_mb_syn_cabac.h
+++ b/codec/encoder/core/inc/set_mb_syn_cabac.h
@@ -42,8 +42,10 @@
 #define SET_MB_SYN_CABAC_H_
 
 #include "typedefs.h"
-#include "bit_stream.h"
 #include "wels_common_defs.h"
+
+using namespace WelsCommon;
+
 namespace WelsEnc {
 
 #define  WELS_QP_MAX    51
--- a/codec/encoder/core/inc/set_mb_syn_cavlc.h
+++ b/codec/encoder/core/inc/set_mb_syn_cavlc.h
@@ -42,7 +42,6 @@
 #define SET_MB_SYN_CAVLC_H_
 
 #include "typedefs.h"
-#include "bit_stream.h"
 #include "wels_func_ptr_def.h"
 
 namespace WelsEnc {
--- a/codec/encoder/core/inc/slice.h
+++ b/codec/encoder/core/inc/slice.h
@@ -41,7 +41,6 @@
 #include "picture.h"
 #include "parameter_sets.h"
 #include "svc_enc_slice_segment.h"
-#include "bit_stream.h"
 #include "set_mb_syn_cabac.h"
 
 namespace WelsEnc {
--- a/codec/encoder/core/inc/svc_enc_frame.h
+++ b/codec/encoder/core/inc/svc_enc_frame.h
@@ -42,7 +42,6 @@
 #include "slice.h"
 #include "picture.h"
 #include "svc_enc_macroblock.h"
-#include "bit_stream.h"
 
 
 #include "svc_enc_slice_segment.h"
--- a/codec/encoder/core/inc/svc_enc_golomb.h
+++ b/codec/encoder/core/inc/svc_enc_golomb.h
@@ -29,9 +29,9 @@
  *     POSSIBILITY OF SUCH DAMAGE.
  *
  *
- * \file	golomb.h
+ * \file	svc_enc_golomb.h
  *
- * \brief	Exponential Golomb entropy coding/decoding routine
+ * \brief	Exponential Golomb entropy coding routine
  *
  * \date	03/13/2009 Created
  *
@@ -40,61 +40,24 @@
 #ifndef WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
 #define WELS_EXPONENTIAL_GOLOMB_ENTROPY_CODING_H__
 
-#include "typedefs.h"
-#include "bit_stream.h"
-#include "macros.h"
+#include "wels_common_defs.h"
+#include "golomb_common.h"
 
+using namespace WelsCommon;
+
 namespace WelsEnc {
 
-#define WRITE_BE_32(ptr, val) do { \
-        (ptr)[0] = (val) >> 24; \
-        (ptr)[1] = (val) >> 16; \
-        (ptr)[2] = (val) >>  8; \
-        (ptr)[3] = (val) >>  0; \
-    } while (0)
 /************************************************************************/
-/* GOLOMB CODIMG FOR WELS ENCODER                                       */
+/* GOLOMB CODIMG FOR WELS ENCODER ONLY                                  */
 /************************************************************************/
 
-/*
- *	Exponential Golomb codes encoding routines
- */
 
-#define    CAVLC_BS_INIT( pBs )  \
-	uint8_t  * pBufPtr = pBs->pBufPtr; \
-	uint32_t   uiCurBits = pBs->uiCurBits; \
-	int32_t    iLeftBits = pBs->iLeftBits;
-
-#define    CAVLC_BS_UNINIT( pBs ) \
-	pBs->pBufPtr = pBufPtr;  \
-	pBs->uiCurBits = uiCurBits;  \
-	pBs->iLeftBits = iLeftBits;
-
-#define    CAVLC_BS_WRITE( n,  v ) \
-	{  \
-	if ( (n) < iLeftBits ) {\
-	    uiCurBits = (uiCurBits<<(n))|(v);\
-		iLeftBits -= (n);\
-	}\
-	else {\
-	    (n) -= iLeftBits;\
-		uiCurBits = (uiCurBits<<iLeftBits) | ((v)>>(n));\
-		WRITE_BE_32(pBufPtr, uiCurBits);\
-		pBufPtr += 4;\
-		uiCurBits = (v) & ((1<<(n))-1);\
-		iLeftBits = 32 - (n);\
-	}\
-	} ;
-
-extern const uint32_t g_uiGolombUELength[256];
-
-
 /*
  *	Get size of unsigned exp golomb codes
  */
 static inline uint32_t BsSizeUE (const uint32_t kiValue) {
 if (256 > kiValue) {
-  return g_uiGolombUELength[kiValue];
+  return g_kuiGolombUELength[kiValue];
 } else {
   uint32_t n = 0;
   uint32_t iTmpValue = kiValue + 1;
@@ -108,8 +71,8 @@
     n += 8;
   }
 
-  //n += (g_uiGolombUELength[iTmpValue] >> 1);
-  n += (g_uiGolombUELength[iTmpValue - 1] >> 1);
+  //n += (g_kuiGolombUELength[iTmpValue] >> 1);
+  n += (g_kuiGolombUELength[iTmpValue - 1] >> 1);
   return ((n << 1) + 1);
 
 }
@@ -132,90 +95,6 @@
 }
 
 /*
- *	Get size of truncated exp golomb codes
- */
-static inline int32_t BsSizeTE (const int32_t kiX, const int32_t kiValue) {
-return 0;
-}
-
-
-
-static inline int32_t BsWriteBits (SBitStringAux* pBs, int32_t n, const uint32_t kuiValue) {
-if (n < pBs->iLeftBits) {
-  pBs->uiCurBits = (pBs->uiCurBits << n) | kuiValue;
-  pBs->iLeftBits -= n;
-} else {
-  n -= pBs->iLeftBits;
-  pBs->uiCurBits = (pBs->uiCurBits << pBs->iLeftBits) | (kuiValue >> n);
-  WRITE_BE_32 (pBs->pBufPtr, pBs->uiCurBits);
-  pBs->pBufPtr += 4;
-  pBs->uiCurBits = kuiValue & ((1 << n) - 1);
-  pBs->iLeftBits = 32 - n;
-}
-return 0;
-}
-
-/*
- *	Write 1 bit
- */
-static inline int32_t BsWriteOneBit (SBitStringAux* pBs, const uint32_t kuiValue) {
-BsWriteBits (pBs, 1, kuiValue);
-
-return 0;
-}
-
-
-static inline void BsFlush (SBitStringAux* pBs) {
-WRITE_BE_32 (pBs->pBufPtr, pBs->uiCurBits << pBs->iLeftBits);
-pBs->pBufPtr += 4 - pBs->iLeftBits / 8;
-pBs->iLeftBits = 32;
-pBs->uiCurBits = 0;	//  for future writing safe, 5/19/2010
-}
-/*
- *	Write unsigned exp golomb codes
- */
-static inline void BsWriteUE (SBitStringAux* pBs, const uint32_t kuiValue) {
-uint32_t iTmpValue = kuiValue + 1;
-if (256 > kuiValue)	{
-  BsWriteBits (pBs, g_uiGolombUELength[kuiValue], kuiValue + 1);
-} else {
-  uint32_t n = 0;
-
-  if (iTmpValue & 0xffff0000) {
-    iTmpValue >>= 16;
-    n += 16;
-  }
-  if (iTmpValue & 0xff00) {
-    iTmpValue >>= 8;
-    n += 8;
-  }
-
-  //n += (g_uiGolombUELength[iTmpValue] >> 1);
-
-  n += (g_uiGolombUELength[iTmpValue - 1] >> 1);
-  BsWriteBits (pBs, (n << 1) + 1, kuiValue + 1);
-}
-return;
-}
-
-/*
- *	Write signed exp golomb codes
- */
-static inline void BsWriteSE (SBitStringAux* pBs, int32_t iValue) {
-uint32_t iTmpValue;
-if (0 == iValue) {
-  BsWriteOneBit (pBs, 1);
-} else if (0 < iValue) {
-  iTmpValue = (iValue << 1) - 1;
-  BsWriteUE (pBs, iTmpValue);
-} else {
-  iTmpValue = ((-iValue) << 1);
-  BsWriteUE (pBs, iTmpValue);
-}
-return;
-}
-
-/*
  *	Write truncated exp golomb codes
  */
 static inline void BsWriteTE (SBitStringAux* pBs, const int32_t kiX, const uint32_t kuiValue) {
@@ -226,23 +105,8 @@
 }
 }
 
-
-/*
- *	Write RBSP trailing bits
- */
-static inline void BsRbspTrailingBits (SBitStringAux* pBs) {
-BsWriteOneBit (pBs, 1);
-BsFlush (pBs);
-}
-
-
-static inline bool   BsCheckByteAlign (SBitStringAux* pBs) {
-return ! (pBs->iLeftBits & 0x7);
-}
-
-
 static inline int32_t BsGetBitsPos (SBitStringAux* pBs) {
-return (int32_t) (((pBs->pBufPtr - pBs->pBuf) << 3) + 32 - pBs->iLeftBits);
+return (int32_t) (((pBs->pCurBuf - pBs->pStartBuf) << 3) + 32 - pBs->iLeftBits);
 }
 
 static inline void BsAlign( SBitStringAux* pBs )
--- a/codec/encoder/core/inc/svc_encode_mb.h
+++ b/codec/encoder/core/inc/svc_encode_mb.h
@@ -44,7 +44,6 @@
 #include "typedefs.h"
 #include "wels_common_basis.h"
 #include "slice.h"
-#include "bit_stream.h"
 #include "encoder_context.h"
 #include "wels_func_ptr_def.h"
 
--- a/codec/encoder/core/inc/vlc_encoder.h
+++ b/codec/encoder/core/inc/vlc_encoder.h
@@ -33,7 +33,6 @@
 #ifndef WELS_VLC_ENCODER_H__
 #define WELS_VLC_ENCODER_H__
 
-#include "bit_stream.h"
 #include "svc_enc_golomb.h"
 
 /************************************************************************/
--- a/codec/encoder/core/src/encoder.cpp
+++ b/codec/encoder/core/src/encoder.cpp
@@ -48,6 +48,7 @@
 #include "mc.h"
 #include "sample.h"
 
+#include "svc_enc_golomb.h"
 #include "svc_base_layer_md.h"
 #include "svc_mode_decision.h"
 #include "set_mb_syn_cavlc.h"
--- a/codec/encoder/core/src/encoder_data_tables.cpp
+++ b/codec/encoder/core/src/encoder_data_tables.cpp
@@ -75,29 +75,6 @@
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-// extern at svc_enc_golomb.h
-
-const uint32_t g_uiGolombUELength[256] = {
-  1,  3,  3,  5,  5,  5,  5,  7,  7,  7,  7,  7,  7,  7,  7,    //14
-  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9, //30
-  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, //46
-  11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, //62
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, //
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-  17
-};
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
--- a/codec/encoder/core/src/encoder_ext.cpp
+++ b/codec/encoder/core/src/encoder_ext.cpp
@@ -3366,11 +3366,11 @@
   iNal	= pCtx->pOut->iNalIndex;
   pBs	=	&pCtx->pOut->sBsWrite;	// SBitStringAux instance for non VCL NALs decoding
 
-  if ((pBs->pBufEnd - pBs->pBufPtr) < iLen || iNal >= pCtx->pOut->iCountNals) {
+  if ((pBs->pEndBuf - pBs->pCurBuf) < iLen || iNal >= pCtx->pOut->iCountNals) {
 #if GOM_TRACE_FLAG
     WelsLog (& (pCtx->sLogCtx), WELS_LOG_ERROR,
              "[RC] paddingcal pBuffer overflow, bufferlen=%lld, paddinglen=%d, iNalIdx= %d, iCountNals= %d",
-             static_cast<long long int> (pBs->pBufEnd - pBs->pBufPtr), iLen, iNal, pCtx->pOut->iCountNals);
+             static_cast<long long int> (pBs->pEndBuf - pBs->pCurBuf), iLen, iNal, pCtx->pOut->iCountNals);
 #endif
     return ENC_RETURN_MEMOVERFLOWFOUND;
   }
--- a/codec/encoder/core/src/set_mb_syn_cabac.cpp
+++ b/codec/encoder/core/src/set_mb_syn_cabac.cpp
@@ -40,7 +40,6 @@
 #include <string.h>
 #include "typedefs.h"
 #include "macros.h"
-#include "wels_common_defs.h"
 #include "set_mb_syn_cabac.h"
 #include "encoder.h"
 
--- a/codec/encoder/core/src/set_mb_syn_cavlc.cpp
+++ b/codec/encoder/core/src/set_mb_syn_cavlc.cpp
@@ -49,6 +49,38 @@
   0, 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7
 };
 
+
+/*
+ *	Exponential Golomb codes encoding routines
+ */
+
+#define    CAVLC_BS_INIT( pBs )  \
+  uint8_t  * pBufPtr = pBs->pCurBuf; \
+  uint32_t   uiCurBits = pBs->uiCurBits; \
+  int32_t    iLeftBits = pBs->iLeftBits;
+
+#define    CAVLC_BS_UNINIT( pBs ) \
+  pBs->pCurBuf = pBufPtr;  \
+  pBs->uiCurBits = uiCurBits;  \
+  pBs->iLeftBits = iLeftBits;
+
+#define    CAVLC_BS_WRITE( n,  v ) \
+  {  \
+  if ( (n) < iLeftBits ) {\
+  uiCurBits = (uiCurBits<<(n))|(v);\
+  iLeftBits -= (n);\
+  }\
+  else {\
+  (n) -= iLeftBits;\
+  uiCurBits = (uiCurBits<<iLeftBits) | ((v)>>(n));\
+  WRITE_BE_32(pBufPtr, uiCurBits);\
+  pBufPtr += 4;\
+  uiCurBits = (v) & ((1<<(n))-1);\
+  iLeftBits = 32 - (n);\
+  }\
+  } ;
+
+
 int32_t CavlcParamCal_c (int16_t* pCoffLevel, uint8_t* pRun, int16_t* pLevel, int32_t* pTotalCoeff ,
                          int32_t iLastIndex) {
   int32_t iTotalZeros = 0;
@@ -201,7 +233,7 @@
 
 void StashMBStatusCavlc (SDynamicSlicingStack* pDss, SSlice* pSlice, int32_t iMbSkipRun) {
   SBitStringAux* pBs = pSlice->pSliceBsa;
-  pDss->pBsStackBufPtr	= pBs->pBufPtr;
+  pDss->pBsStackBufPtr	= pBs->pCurBuf;
   pDss->uiBsStackCurBits	= pBs->uiCurBits;
   pDss->iBsStackLeftBits	= pBs->iLeftBits;
   pDss->uiLastMbQp =  pSlice->uiLastMbQp;
@@ -209,7 +241,7 @@
 }
 int32_t StashPopMBStatusCavlc (SDynamicSlicingStack* pDss, SSlice* pSlice) {
   SBitStringAux* pBs = pSlice->pSliceBsa;
-  pBs->pBufPtr		= pDss->pBsStackBufPtr;
+  pBs->pCurBuf		= pDss->pBsStackBufPtr;
   pBs->uiCurBits	= pDss->uiBsStackCurBits;
   pBs->iLeftBits	= pDss->iBsStackLeftBits;
   pSlice->uiLastMbQp = pDss->uiLastMbQp;
@@ -232,7 +264,7 @@
   SBitStringAux* pBs = pSlice->pSliceBsa;
   if (bEntropyCodingModeFlag) {
     WelsCabacEncodeFlush (&pSlice->sCabacCtx);
-    pBs->pBufPtr = WelsCabacEncodeGetPtr (&pSlice->sCabacCtx);
+    pBs->pCurBuf = WelsCabacEncodeGetPtr (&pSlice->sCabacCtx);
 
   } else {
     BsRbspTrailingBits (pBs);
@@ -239,7 +271,7 @@
     BsFlush (pBs);
   }
 }
-void InitCoeffFunc (SWelsFuncPtrList* pFuncList, const uint32_t uiCpuFlag,int32_t iEntropyCodingModeFlag) {
+void InitCoeffFunc (SWelsFuncPtrList* pFuncList, const uint32_t uiCpuFlag, int32_t iEntropyCodingModeFlag) {
   pFuncList->pfCavlcParamCal = CavlcParamCal_c;
 
 #if defined(X86_ASM)
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -651,7 +651,7 @@
   int32_t iNalIdx					= 0;
   int32_t iNalSize					= 0;
   int32_t iReturn = ENC_RETURN_SUCCESS;
-  const int32_t kiWrittenLength = (int32_t) (pSliceBs->sBsWrite.pBufPtr - pSliceBs->sBsWrite.pBuf);
+  const int32_t kiWrittenLength = (int32_t) (pSliceBs->sBsWrite.pCurBuf - pSliceBs->sBsWrite.pStartBuf);
 
   iSliceSize				= 0;
   assert (kiNalCnt <= 2);
--- a/codec/encoder/core/src/svc_set_mb_syn_cabac.cpp
+++ b/codec/encoder/core/src/svc_set_mb_syn_cabac.cpp
@@ -474,7 +474,8 @@
   }
   return iCount;
 }
-int32_t WelsWriteMbResidualCabac (SWelsFuncPtrList* pFuncList,SSlice* pSlice, SMbCache* sMbCacheInfo, SMB* pCurMb, SCabacCtx* pCabacCtx,
+int32_t WelsWriteMbResidualCabac (SWelsFuncPtrList* pFuncList, SSlice* pSlice, SMbCache* sMbCacheInfo, SMB* pCurMb,
+                                  SCabacCtx* pCabacCtx,
                                   int16_t iMbWidth, uint32_t uiChromaQpIndexOffset) {
 
   const uint16_t uiMbType = pCurMb->uiMbType;
@@ -498,7 +499,7 @@
 
     if (uiMbType == MB_TYPE_INTRA16x16) {
       //Luma DC
-      int iNonZeroCount = pFuncList->pfGetNoneZeroCount(pMbCache->pDct->iLumaI16x16Dc);
+      int iNonZeroCount = pFuncList->pfGetNoneZeroCount (pMbCache->pDct->iLumaI16x16Dc);
       WelsWriteBlockResidualCabac (pMbCache, pCurMb, iMbWidth, pCabacCtx, LUMA_DC, 0, iNonZeroCount,
                                    pMbCache->pDct->iLumaI16x16Dc, 15);
       if (iNonZeroCount)
@@ -572,7 +573,7 @@
 
   /* init cabac */
   WelsCabacContextInit (pEncCtx, &pSlice->sCabacCtx, pSlice->iCabacInitIdc);
-  WelsCabacEncodeInit (&pSlice->sCabacCtx, pBs->pBufPtr, pBs->pBufEnd);
+  WelsCabacEncodeInit (&pSlice->sCabacCtx, pBs->pCurBuf, pBs->pEndBuf);
 }
 
 int32_t WelsSpatialWriteMbSynCabac (sWelsEncCtx* pEncCtx, SSlice* pSlice, SMB* pCurMb) {
@@ -680,7 +681,8 @@
     if (uiMbType != MB_TYPE_INTRA16x16) {
       WelsCabacMbCbp (pCurMb, iMbWidth, pCabacCtx);
     }
-    iRet = WelsWriteMbResidualCabac (pEncCtx->pFuncList,pSlice, pMbCache, pCurMb, pCabacCtx, iMbWidth, uiChromaQpIndexOffset);
+    iRet = WelsWriteMbResidualCabac (pEncCtx->pFuncList, pSlice, pMbCache, pCurMb, pCabacCtx, iMbWidth,
+                                     uiChromaQpIndexOffset);
   }
   if (!IS_INTRA (pCurMb->uiMbType))
     pCurMb->uiChromPredMode = 0;
--- a/codec/encoder/core/src/svc_set_mb_syn_cavlc.cpp
+++ b/codec/encoder/core/src/svc_set_mb_syn_cavlc.cpp
@@ -209,7 +209,7 @@
 }
 
 int32_t CheckBitstreamBuffer (const uint32_t	kuiSliceIdx, sWelsEncCtx* pEncCtx,  SBitStringAux* pBs) {
-  const intX_t iLeftLength = pBs->pBufEnd - pBs->pBufPtr - 1;
+  const intX_t iLeftLength = pBs->pEndBuf - pBs->pCurBuf - 1;
   assert (iLeftLength > 0);
 
   if (iLeftLength < MAX_MACROBLOCK_SIZE_IN_BYTE_x2) {