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 set_mb_syn_cabac.h
33 *
34 * \brief Seting all syntax elements of mb and encoding residual with cabac
35 *
36 * \date 09/27/2014 Created
37 *
38 *************************************************************************************
39 */
40
41 #ifndef SET_MB_SYN_CABAC_H_
42 #define SET_MB_SYN_CABAC_H_
43
44 #include "typedefs.h"
45 #include "wels_common_defs.h"
46
47 using namespace WelsCommon;
48
49 namespace WelsEnc {
50
51 #define WELS_QP_MAX 51
52
53 typedef uint64_t cabac_low_t;
54 enum { CABAC_LOW_WIDTH = sizeof (cabac_low_t) / sizeof (uint8_t) * 8 };
55
56 typedef struct TagStateCtx {
57 // Packed representation of state and MPS as state << 1 | MPS.
58 uint8_t m_uiStateMps;
59
MpsTagStateCtx60 uint8_t Mps() const { return m_uiStateMps & 1; }
StateTagStateCtx61 uint8_t State() const { return m_uiStateMps >> 1; }
SetTagStateCtx62 void Set (uint8_t uiState, uint8_t uiMps) { m_uiStateMps = uiState * 2 + uiMps; }
63 } SStateCtx;
64 typedef struct TagCabacCtx {
65 cabac_low_t m_uiLow;
66 int32_t m_iLowBitCnt;
67 int32_t m_iRenormCnt;
68 uint32_t m_uiRange;
69 SStateCtx m_sStateCtx[WELS_CONTEXT_COUNT];
70 uint8_t* m_pBufStart;
71 uint8_t* m_pBufEnd;
72 uint8_t* m_pBufCur;
73 } SCabacCtx;
74
75
76 void WelsCabacContextInit (void* pCtx, SCabacCtx* pCbCtx, int32_t iModel);
77 void WelsCabacEncodeInit (SCabacCtx* pCbCtx, uint8_t* pBuf, uint8_t* pEnd);
78 inline void WelsCabacEncodeDecision (SCabacCtx* pCbCtx, int32_t iCtx, uint32_t uiBin);
79 inline void WelsCabacEncodeBypassOne (SCabacCtx* pCbCtx, int32_t uiBin);
80 void WelsCabacEncodeTerminate (SCabacCtx* pCbCtx, uint32_t uiBin);
81 void WelsCabacEncodeUeBypass (SCabacCtx* pCbCtx, int32_t iExpBits, uint32_t uiVal);
82 void WelsCabacEncodeFlush (SCabacCtx* pCbCtx);
83 uint8_t* WelsCabacEncodeGetPtr (SCabacCtx* pCbCtx);
84 int32_t WriteBlockResidualCabac (void* pEncCtx, int16_t* pCoffLevel, int32_t iEndIdx,
85 int32_t iCalRunLevelFlag,
86 int32_t iResidualProperty, int8_t iNC, SBitStringAux* pBs);
87
88
89 // private functions used by public inline functions.
90 void WelsCabacEncodeDecisionLps_ (SCabacCtx* pCbCtx, int32_t iCtx);
91 void WelsCabacEncodeUpdateLowNontrivial_ (SCabacCtx* pCbCtx);
WelsCabacEncodeUpdateLow_(SCabacCtx * pCbCtx)92 inline void WelsCabacEncodeUpdateLow_ (SCabacCtx* pCbCtx) {
93 if (pCbCtx->m_iLowBitCnt + pCbCtx->m_iRenormCnt < CABAC_LOW_WIDTH) {
94 pCbCtx->m_iLowBitCnt += pCbCtx->m_iRenormCnt;
95 pCbCtx->m_uiLow <<= pCbCtx->m_iRenormCnt;
96 } else {
97 WelsCabacEncodeUpdateLowNontrivial_ (pCbCtx);
98 }
99 pCbCtx->m_iRenormCnt = 0;
100 }
101
102 // inline function definitions.
WelsCabacEncodeDecision(SCabacCtx * pCbCtx,int32_t iCtx,uint32_t uiBin)103 void WelsCabacEncodeDecision (SCabacCtx* pCbCtx, int32_t iCtx, uint32_t uiBin) {
104 if (uiBin == pCbCtx->m_sStateCtx[iCtx].Mps()) {
105 const int32_t kiState = pCbCtx->m_sStateCtx[iCtx].State();
106 uint32_t uiRange = pCbCtx->m_uiRange;
107 uint32_t uiRangeLps = g_kuiCabacRangeLps[kiState][(uiRange & 0xff) >> 6];
108 uiRange -= uiRangeLps;
109
110 const int32_t kiRenormAmount = uiRange >> 8 ^ 1;
111 pCbCtx->m_uiRange = uiRange << kiRenormAmount;
112 pCbCtx->m_iRenormCnt += kiRenormAmount;
113 pCbCtx->m_sStateCtx[iCtx].Set (g_kuiStateTransTable[kiState][1], uiBin);
114 } else {
115 WelsCabacEncodeDecisionLps_ (pCbCtx, iCtx);
116 }
117 }
118
WelsCabacEncodeBypassOne(SCabacCtx * pCbCtx,int32_t uiBin)119 void WelsCabacEncodeBypassOne (SCabacCtx* pCbCtx, int32_t uiBin) {
120 const uint32_t kuiBinBitmask = -uiBin;
121 pCbCtx->m_iRenormCnt++;
122 WelsCabacEncodeUpdateLow_ (pCbCtx);
123 pCbCtx->m_uiLow += kuiBinBitmask & pCbCtx->m_uiRange;
124 }
125
126 }
127 #endif
128