source: 3DVCSoftware/branches/HTM-5.0-Qualcomm/source/Lib/TLibEncoder/TEncSbac.cpp

Last change on this file was 195, checked in by tech, 12 years ago

Changed macro switch names to match conventions.

  • Property svn:eol-style set to native
File size: 90.0 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2012, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TEncSbac.cpp
35    \brief    SBAC encoder class
36*/
37
38#include "TEncTop.h"
39#include "TEncSbac.h"
40
41#include <map>
42#include <algorithm>
43
44#if RWTH_SDC_DLT_B0036
45#define GetNumDepthValues()     (pcCU->getSlice()->getSPS()->getNumDepthValues())
46#define GetBitsPerDepthValue()  (pcCU->getSlice()->getSPS()->getBitsPerDepthValue())
47#endif
48
49//! \ingroup TLibEncoder
50//! \{
51
52// ====================================================================================================================
53// Constructor / destructor / create / destroy
54// ====================================================================================================================
55
56TEncSbac::TEncSbac()
57// new structure here
58: m_pcBitIf                   ( NULL )
59, m_pcSlice                   ( NULL )
60, m_pcBinIf                   ( NULL )
61, m_bAlfCtrl                  ( false )
62, m_uiCoeffCost               ( 0 )
63, m_uiMaxAlfCtrlDepth         ( 0 )
64, m_numContextModels          ( 0 )
65, m_cCUSplitFlagSCModel       ( 1,             1,               NUM_SPLIT_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels )
66, m_cCUSkipFlagSCModel        ( 1,             1,               NUM_SKIP_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
67#if LGE_ILLUCOMP_B0045
68, m_cCUICFlagSCModel          ( 1,             1,               NUM_IC_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
69#endif
70, m_cCUMergeFlagExtSCModel    ( 1,             1,               NUM_MERGE_FLAG_EXT_CTX        , m_contextModels + m_numContextModels, m_numContextModels)
71, m_cCUMergeIdxExtSCModel     ( 1,             1,               NUM_MERGE_IDX_EXT_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
72#if HHI_INTER_VIEW_RESIDUAL_PRED
73, m_cResPredFlagSCModel       ( 1,             1,               NUM_RES_PRED_FLAG_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
74#endif
75, m_cCUPartSizeSCModel        ( 1,             1,               NUM_PART_SIZE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
76, m_cCUPredModeSCModel        ( 1,             1,               NUM_PRED_MODE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
77, m_cCUAlfCtrlFlagSCModel     ( 1,             1,               NUM_ALF_CTRL_FLAG_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
78, m_cCUIntraPredSCModel       ( 1,             1,               NUM_ADI_CTX                   , m_contextModels + m_numContextModels, m_numContextModels)
79, m_cCUChromaPredSCModel      ( 1,             1,               NUM_CHROMA_PRED_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
80, m_cCUDeltaQpSCModel         ( 1,             1,               NUM_DELTA_QP_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
81, m_cCUInterDirSCModel        ( 1,             1,               NUM_INTER_DIR_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cCURefPicSCModel          ( 1,             1,               NUM_REF_NO_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cCUMvdSCModel             ( 1,             1,               NUM_MV_RES_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
84, m_cCUQtCbfSCModel           ( 1,             2,               NUM_QT_CBF_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
85, m_cCUTransSubdivFlagSCModel ( 1,             1,               NUM_TRANS_SUBDIV_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
86, m_cCUQtRootCbfSCModel       ( 1,             1,               NUM_QT_ROOT_CBF_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cCUSigCoeffGroupSCModel   ( 1,             2,               NUM_SIG_CG_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cCUSigSCModel             ( 1,             1,               NUM_SIG_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
89, m_cCuCtxLastX               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
90, m_cCuCtxLastY               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
91, m_cCUOneSCModel             ( 1,             1,               NUM_ONE_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
92, m_cCUAbsSCModel             ( 1,             1,               NUM_ABS_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
93, m_cMVPIdxSCModel            ( 1,             1,               NUM_MVP_IDX_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
94, m_cALFFlagSCModel           ( 1,             1,               NUM_ALF_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
95, m_cALFUvlcSCModel           ( 1,             1,               NUM_ALF_UVLC_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
96, m_cALFSvlcSCModel           ( 1,             1,               NUM_ALF_SVLC_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
97#if AMP_CTX
98, m_cCUAMPSCModel             ( 1,             1,               NUM_CU_AMP_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
99#else
100, m_cCUXPosiSCModel           ( 1,             1,               NUM_CU_X_POS_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
101, m_cCUYPosiSCModel           ( 1,             1,               NUM_CU_Y_POS_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
102#endif
103, m_cSaoFlagSCModel           ( 1,             1,               NUM_SAO_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
104, m_cSaoUvlcSCModel           ( 1,             1,               NUM_SAO_UVLC_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
105, m_cSaoSvlcSCModel           ( 1,             1,               NUM_SAO_SVLC_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
106#if SAO_UNIT_INTERLEAVING
107, m_cSaoMergeLeftSCModel      ( 1,             1,               NUM_SAO_MERGE_LEFT_FLAG_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
108, m_cSaoMergeUpSCModel        ( 1,             1,               NUM_SAO_MERGE_UP_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
109, m_cSaoTypeIdxSCModel        ( 1,             1,               NUM_SAO_TYPE_IDX_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
110#endif
111#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
112, m_cDmmFlagSCModel           ( 1,             1,               NUM_DMM_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
113, m_cDmmModeSCModel           ( 1,             1,               NUM_DMM_MODE_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
114, m_cDmmDataSCModel           ( 1,             1,               NUM_DMM_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
115#endif
116#if LGE_EDGE_INTRA_A0070
117, m_cEdgeIntraSCModel         ( 1,             1,               NUM_EDGE_INTRA_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
118#if LGE_EDGE_INTRA_DELTA_DC
119, m_cEdgeIntraDeltaDCSCModel  ( 1,             1,               NUM_EDGE_INTRA_DELTA_DC_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
120#endif
121#endif
122#if RWTH_SDC_DLT_B0036
123, m_cSDCFlagSCModel             ( 1,             1,                 SDC_NUM_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
124, m_cSDCResidualFlagSCModel     ( 1,             2,  SDC_NUM_RESIDUAL_FLAG_CTX  , m_contextModels + m_numContextModels, m_numContextModels)
125, m_cSDCResidualSignFlagSCModel ( 1,             2,  SDC_NUM_SIGN_FLAG_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
126, m_cSDCResidualSCModel         ( 1,             2,  SDC_NUM_RESIDUAL_CTX       , m_contextModels + m_numContextModels, m_numContextModels)
127, m_cSDCPredModeSCModel             ( 1,             3,                 SDC_NUM_PRED_MODE_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
128#endif
129{
130  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
131  m_iSliceGranularity = 0;
132}
133
134TEncSbac::~TEncSbac()
135{
136}
137
138// ====================================================================================================================
139// Public member functions
140// ====================================================================================================================
141
142Void TEncSbac::resetEntropy           ()
143{
144  Int  iQp              = m_pcSlice->getSliceQp();
145  SliceType eSliceType  = m_pcSlice->getSliceType();
146 
147#if CABAC_INIT_FLAG
148  Int  encCABACTableIdx = m_pcSlice->getPPS()->getEncCABACTableIdx();
149  if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag())
150  {
151    eSliceType = (SliceType) encCABACTableIdx;
152  }
153#endif
154
155  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
156 
157  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
158#if LGE_ILLUCOMP_B0045
159  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
160#endif
161  m_cCUAlfCtrlFlagSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_ALF_CTRL_FLAG );
162  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
163  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
164#if HHI_INTER_VIEW_RESIDUAL_PRED
165  m_cResPredFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_RES_PRED_FLAG );
166#endif
167  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
168#if AMP_CTX
169  m_cCUAMPSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS );
170#else
171  m_cCUXPosiSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_CU_X_POS );
172  m_cCUYPosiSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_CU_Y_POS );
173#endif
174  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
175  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
176  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
177  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
178  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
179  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
180  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
181  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
182  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
183  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
184  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
185  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
186  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
187  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
188  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
189  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
190  m_cALFFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_FLAG );
191  m_cALFUvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_UVLC );
192  m_cALFSvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_SVLC );
193  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
194  m_cSaoFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_FLAG );
195  m_cSaoUvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_UVLC );
196  m_cSaoSvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_SVLC );
197#if SAO_UNIT_INTERLEAVING
198  m_cSaoMergeLeftSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_LEFT_FLAG );
199  m_cSaoMergeUpSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_UP_FLAG );
200  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
201#endif
202#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
203  m_cDmmFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_FLAG );
204  m_cDmmModeSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_MODE );
205  m_cDmmDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_DATA );
206#endif
207#if LGE_EDGE_INTRA_A0070
208  m_cEdgeIntraSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_EDGE_INTRA );
209#if LGE_EDGE_INTRA_DELTA_DC
210  m_cEdgeIntraDeltaDCSCModel.initBuffer  ( eSliceType, iQp, (UChar*)INIT_EDGE_INTRA_DELTA_DC );
211#endif
212#endif
213#if RWTH_SDC_DLT_B0036
214  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
215  m_cSDCResidualFlagSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
216  m_cSDCResidualSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
217  m_cSDCResidualSignFlagSCModel.initBuffer  ( eSliceType, iQp, (UChar*)INIT_SDC_SIGN_FLAG );
218  m_cSDCPredModeSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_PRED_MODE );
219#endif
220
221  // new structure
222  m_uiLastQp = iQp;
223 
224  m_pcBinIf->start();
225 
226  return;
227}
228
229#if CABAC_INIT_FLAG
230/** The function does the following:
231 * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and
232 * stores the index of the closest table.  This index is used for the next P/B slice when cabac_init_present_flag is true.
233 */
234Void TEncSbac::determineCabacInitIdx()
235{
236  Int  qp              = m_pcSlice->getSliceQp();
237
238  if (!m_pcSlice->isIntra())
239  {
240    SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE};
241
242    UInt bestCost             = MAX_UINT;
243    SliceType bestSliceType   = aSliceTypeChoices[0];
244    for (UInt idx=0; idx<2; idx++)
245    {
246      UInt curCost          = 0;
247      SliceType curSliceType  = aSliceTypeChoices[idx];
248
249      curCost  = m_cCUSplitFlagSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG );
250      curCost += m_cCUSkipFlagSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG );
251#if LGE_ILLUCOMP_B0045
252      curCost += m_cCUICFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_IC_FLAG );
253#endif
254      curCost += m_cCUAlfCtrlFlagSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_ALF_CTRL_FLAG );
255      curCost += m_cCUMergeFlagExtSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT);
256      curCost += m_cCUMergeIdxExtSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT);
257#if HHI_INTER_VIEW_RESIDUAL_PRED
258      curCost += m_cResPredFlagSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_RES_PRED_FLAG);
259#endif
260      curCost += m_cCUPartSizeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PART_SIZE );
261#if AMP_CTX
262      curCost += m_cCUAMPSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_CU_AMP_POS );
263#else
264      curCost += m_cCUXPosiSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_CU_X_POS );
265      curCost += m_cCUYPosiSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_CU_Y_POS );
266#endif
267      curCost += m_cCUPredModeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PRED_MODE );
268      curCost += m_cCUIntraPredSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
269      curCost += m_cCUChromaPredSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
270      curCost += m_cCUInterDirSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_INTER_DIR );
271      curCost += m_cCUMvdSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_MVD );
272      curCost += m_cCURefPicSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_REF_PIC );
273      curCost += m_cCUDeltaQpSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_DQP );
274      curCost += m_cCUQtCbfSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_QT_CBF );
275      curCost += m_cCUQtRootCbfSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
276      curCost += m_cCUSigCoeffGroupSCModel.calcCost   ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
277      curCost += m_cCUSigSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_SIG_FLAG );
278      curCost += m_cCuCtxLastX.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
279      curCost += m_cCuCtxLastY.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
280      curCost += m_cCUOneSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ONE_FLAG );
281      curCost += m_cCUAbsSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ABS_FLAG );
282      curCost += m_cMVPIdxSCModel.calcCost            ( curSliceType, qp, (UChar*)INIT_MVP_IDX );
283      curCost += m_cALFFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_ALF_FLAG );
284      curCost += m_cALFUvlcSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_ALF_UVLC );
285      curCost += m_cALFSvlcSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_ALF_SVLC );
286      curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
287      curCost += m_cSaoFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_SAO_FLAG );
288      curCost += m_cSaoUvlcSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_SAO_UVLC );
289      curCost += m_cSaoSvlcSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_SAO_SVLC );
290#if SAO_UNIT_INTERLEAVING
291      curCost += m_cSaoMergeLeftSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_LEFT_FLAG );
292      curCost += m_cSaoMergeUpSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_UP_FLAG );
293      curCost += m_cSaoTypeIdxSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
294#endif
295
296      if (curCost < bestCost)
297      {
298        bestSliceType = curSliceType;
299        bestCost      = curCost;
300      }
301    }
302    m_pcSlice->getPPS()->setEncCABACTableIdx( bestSliceType );
303  }
304  else
305  {
306    m_pcSlice->getPPS()->setEncCABACTableIdx( I_SLICE );
307  } 
308
309  #if CABAC_INIT_FLAG && FIX_POZNAN_CABAC_INIT_FLAG
310    m_pcSlice->getPPS()->setEncPrevPOC( m_pcSlice->getPOC() );
311  #endif
312}
313#endif
314
315
316/** The function does the followng: Write out terminate bit. Flush CABAC. Intialize CABAC states. Start CABAC.
317 */
318Void TEncSbac::updateContextTables( SliceType eSliceType, Int iQp, Bool bExecuteFinish )
319{
320  m_pcBinIf->encodeBinTrm(1);
321  if (bExecuteFinish) m_pcBinIf->finish();
322  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
323 
324  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
325#if LGE_ILLUCOMP_B0045
326  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
327#endif
328  m_cCUAlfCtrlFlagSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_ALF_CTRL_FLAG );
329  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
330  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
331#if HHI_INTER_VIEW_RESIDUAL_PRED
332  m_cResPredFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_RES_PRED_FLAG );
333#endif
334  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
335#if AMP_CTX
336  m_cCUAMPSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS );
337#else
338  m_cCUXPosiSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_CU_X_POS );
339  m_cCUYPosiSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_CU_Y_POS );
340#endif
341  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
342  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
343  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
344  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
345  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
346  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
347  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
348  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
349  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
350  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
351  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
352  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
353  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
354  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
355  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
356  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
357  m_cALFFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_FLAG );
358  m_cALFUvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_UVLC );
359  m_cALFSvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_ALF_SVLC );
360  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
361  m_cSaoFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_FLAG );
362  m_cSaoUvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_UVLC );
363  m_cSaoSvlcSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_SAO_SVLC );
364#if SAO_UNIT_INTERLEAVING
365  m_cSaoMergeLeftSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_LEFT_FLAG );
366  m_cSaoMergeUpSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_UP_FLAG );
367  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
368#endif
369#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
370  m_cDmmFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_FLAG );
371  m_cDmmModeSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_MODE );
372  m_cDmmDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DMM_DATA );
373#endif
374#if RWTH_SDC_DLT_B0036
375  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
376  m_cSDCResidualFlagSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
377  m_cSDCResidualSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
378  m_cSDCResidualSignFlagSCModel.initBuffer  ( eSliceType, iQp, (UChar*)INIT_SDC_SIGN_FLAG );
379  m_cSDCPredModeSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_PRED_MODE );
380#endif
381 
382  m_pcBinIf->start();
383}
384
385Void TEncSbac::writeTileMarker( UInt uiTileIdx, UInt uiBitsUsed )
386{
387  for (Int iShift=uiBitsUsed-1; iShift>=0; iShift--)
388  {
389    m_pcBinIf->encodeBinEP ( (uiTileIdx & (1 << iShift)) >> iShift );
390  }
391}
392
393void TEncSbac::codeSEI(const SEI&)
394{
395  assert(0);
396}
397
398#if VIDYO_VPS_INTEGRATION|QC_MVHEVC_B0046
399Void TEncSbac::codeVPS( TComVPS* pcVPS )
400{
401  assert (0);
402  return;
403}
404#endif
405
406#if HHI_MPI
407Void TEncSbac::codeSPS( TComSPS* pcSPS, Bool bIsDepth )
408#else
409Void TEncSbac::codeSPS( TComSPS* pcSPS )
410#endif
411{
412  assert (0);
413  return;
414}
415
416Void TEncSbac::codePPS( TComPPS* pcPPS )
417{
418  assert (0);
419  return;
420}
421
422Void TEncSbac::codeSliceHeader( TComSlice* pcSlice )
423{
424  assert (0);
425  return;
426}
427
428#if TILES_WPP_ENTRY_POINT_SIGNALLING
429Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice )
430{
431  assert (0);
432  return;
433}
434#else
435Void TEncSbac::codeSliceHeaderSubstreamTable( TComSlice* pcSlice )
436{
437  assert (0);
438}
439#endif
440
441Void TEncSbac::codeTerminatingBit( UInt uilsLast )
442{
443  m_pcBinIf->encodeBinTrm( uilsLast );
444}
445
446Void TEncSbac::codeSliceFinish()
447{
448  m_pcBinIf->finish();
449}
450
451#if OL_FLUSH
452Void TEncSbac::codeFlush()
453{
454  m_pcBinIf->flush();
455}
456
457Void TEncSbac::encodeStart()
458{
459  m_pcBinIf->start();
460}
461#endif
462
463Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset )
464{
465  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] );
466 
467  if( 0 == uiSymbol)
468  {
469    return;
470  }
471 
472  while( uiSymbol-- )
473  {
474    m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] );
475  }
476 
477  return;
478}
479
480Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
481{
482  if (uiMaxSymbol == 0)
483  {
484    return;
485  }
486 
487  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] );
488 
489  if ( uiSymbol == 0 )
490  {
491    return;
492  }
493 
494  Bool bCodeLast = ( uiMaxSymbol > uiSymbol );
495 
496  while( --uiSymbol )
497  {
498    m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] );
499  }
500  if( bCodeLast )
501  {
502    m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] );
503  }
504 
505  return;
506}
507
508Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount )
509{
510  UInt bins = 0;
511  Int numBins = 0;
512 
513  while( uiSymbol >= (UInt)(1<<uiCount) )
514  {
515    bins = 2 * bins + 1;
516    numBins++;
517    uiSymbol -= 1 << uiCount;
518    uiCount  ++;
519  }
520  bins = 2 * bins + 0;
521  numBins++;
522 
523  bins = (bins << uiCount) | uiSymbol;
524  numBins += uiCount;
525 
526  assert( numBins <= 32 );
527  m_pcBinIf->encodeBinsEP( bins, numBins );
528}
529
530/** Coding of coeff_abs_level_minus3
531 * \param uiSymbol value of coeff_abs_level_minus3
532 * \param ruiGoRiceParam reference to Rice parameter
533 * \returns Void
534 */
535Void TEncSbac::xWriteGoRiceExGolomb( UInt uiSymbol, UInt &ruiGoRiceParam )
536{
537  UInt uiMaxVlc     = g_auiGoRiceRange[ ruiGoRiceParam ];
538  Bool bExGolomb    = ( uiSymbol > uiMaxVlc );
539  UInt uiCodeWord   = min<UInt>( uiSymbol, ( uiMaxVlc + 1 ) );
540  UInt uiQuotient   = uiCodeWord >> ruiGoRiceParam;
541  UInt uiMaxPreLen  = g_auiGoRicePrefixLen[ ruiGoRiceParam ];
542 
543  UInt binValues;
544  Int numBins;
545 
546  if ( uiQuotient >= uiMaxPreLen )
547  {
548    numBins = uiMaxPreLen;
549    binValues = ( 1 << numBins ) - 1;
550  }
551  else
552  {
553    numBins = uiQuotient + 1;
554    binValues = ( 1 << numBins ) - 2;
555  }
556 
557  m_pcBinIf->encodeBinsEP( ( binValues << ruiGoRiceParam ) + uiCodeWord - ( uiQuotient << ruiGoRiceParam ), numBins + ruiGoRiceParam );
558 
559#if EIGHT_BITS_RICE_CODE
560  ruiGoRiceParam = g_aauiGoRiceUpdate[ ruiGoRiceParam ][ min<UInt>( uiSymbol, 23 ) ];
561#else
562  ruiGoRiceParam = g_aauiGoRiceUpdate[ ruiGoRiceParam ][ min<UInt>( uiSymbol, 15 ) ];
563#endif
564 
565  if( bExGolomb )
566  {
567    uiSymbol -= uiMaxVlc + 1;
568    xWriteEpExGolomb( uiSymbol, 0 );
569  }
570}
571
572// SBAC RD
573Void  TEncSbac::load ( TEncSbac* pSrc)
574{
575  this->xCopyFrom(pSrc);
576}
577
578Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
579{
580  m_pcBinIf->copyState( pSrc->m_pcBinIf );
581 
582  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
583}
584
585
586Void  TEncSbac::store( TEncSbac* pDest)
587{
588  pDest->xCopyFrom( this );
589}
590
591
592Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
593{
594  m_pcBinIf->copyState( pSrc->m_pcBinIf );
595 
596  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
597  this->m_uiLastQp    = pSrc->m_uiLastQp;
598 
599  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
600}
601
602#if HHI_INTER_VIEW_MOTION_PRED
603Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList, Int iNum )
604#else
605Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
606#endif
607{
608  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
609#if HHI_INTER_VIEW_MOTION_PRED
610#else
611  Int iNum = AMVP_MAX_NUM_CANDS;
612#endif
613
614  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
615}
616
617Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
618{
619  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
620
621#if OL_QTLIMIT_PREDCODING_B0068
622  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
623  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
624  Bool bDepthMapDetect   = (pcTexture != NULL);
625  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
626 
627  if(bDepthMapDetect && !bIntraSliceDetect && sps->getUseQTLPC() && pcCU->getPic()->getReduceBitsFlag())
628  {
629    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
630    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
631    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
632    if (pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize( uiCUIdx ) != SIZE_NxN)
633    {
634      assert( eSize == SIZE_2Nx2N );
635      return;
636    }
637  }
638#endif
639
640  if ( pcCU->isIntra( uiAbsPartIdx ) )
641  {
642    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
643    {
644      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
645    }
646    return;
647  }
648 
649  switch(eSize)
650  {
651  case SIZE_2Nx2N:
652    {
653      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
654      break;
655    }
656  case SIZE_2NxN:
657  case SIZE_2NxnU:
658  case SIZE_2NxnD:
659    {
660      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
661      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
662      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
663      {
664        if (eSize == SIZE_2NxN)
665        {
666#if AMP_CTX           
667          m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 ));
668#else
669          m_pcBinIf->encodeBin(1, m_cCUYPosiSCModel.get( 0, 0, 0 ));
670#endif
671        }
672        else
673        {
674#if AMP_CTX
675          m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 ));         
676          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
677#else
678          m_pcBinIf->encodeBin(0, m_cCUYPosiSCModel.get( 0, 0, 0 ));
679          m_pcBinIf->encodeBin((eSize == SIZE_2NxnU? 0: 1), m_cCUYPosiSCModel.get( 0, 0, 1 ));
680#endif
681        }
682      }
683      break;
684    }
685  case SIZE_Nx2N:
686  case SIZE_nLx2N:
687  case SIZE_nRx2N:
688    {
689      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
690      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
691      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getSlice()->getSPS()->getDisInter4x4() && pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
692      {
693        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
694      }
695      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
696      {
697        if (eSize == SIZE_Nx2N)
698        {
699#if AMP_CTX
700          m_pcBinIf->encodeBin(1, m_cCUAMPSCModel.get( 0, 0, 0 ));
701#else
702          m_pcBinIf->encodeBin(1, m_cCUXPosiSCModel.get( 0, 0, 0 ));
703#endif
704        }
705        else
706        {
707#if AMP_CTX
708          m_pcBinIf->encodeBin(0, m_cCUAMPSCModel.get( 0, 0, 0 ));
709          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
710#else
711          m_pcBinIf->encodeBin(0, m_cCUXPosiSCModel.get( 0, 0, 0 ));
712          m_pcBinIf->encodeBin((eSize == SIZE_nLx2N? 0: 1), m_cCUXPosiSCModel.get( 0, 0, 1 ));
713#endif
714        }
715      }
716      break;
717    }
718  case SIZE_NxN:
719    {
720      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getSlice()->getSPS()->getDisInter4x4() && pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
721      {
722        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
723        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
724        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
725      }
726      break;
727    }
728  default:
729    {
730      assert(0);
731    }
732  }
733}
734
735/** code prediction mode
736 * \param pcCU
737 * \param uiAbsPartIdx 
738 * \returns Void
739 */
740Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
741{
742#if RWTH_SDC_DLT_B0036
743  if ( pcCU->getSlice()->isIntra() )
744  {
745    assert( pcCU->isIntra(uiAbsPartIdx) );
746    return;
747  }
748#endif
749 
750  // get context function is here
751  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
752  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
753}
754
755Void TEncSbac::codeAlfCtrlFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
756{
757  if (!m_bAlfCtrl)
758    return;
759 
760  if( pcCU->getDepth(uiAbsPartIdx) > m_uiMaxAlfCtrlDepth && !pcCU->isFirstAbsZorderIdxInDepth(uiAbsPartIdx, m_uiMaxAlfCtrlDepth))
761  {
762    return;
763  }
764 
765  const UInt uiSymbol = pcCU->getAlfCtrlFlag( uiAbsPartIdx ) ? 1 : 0;
766  m_pcBinIf->encodeBin( uiSymbol, *m_cCUAlfCtrlFlagSCModel.get( 0 ) );
767}
768
769Void TEncSbac::codeAlfCtrlDepth()
770{
771  if (!m_bAlfCtrl)
772    return;
773 
774  UInt uiDepth = m_uiMaxAlfCtrlDepth;
775  xWriteUnaryMaxSymbol(uiDepth, m_cALFUvlcSCModel.get(0), 1, g_uiMaxCUDepth-1);
776}
777
778/** code skip flag
779 * \param pcCU
780 * \param uiAbsPartIdx
781 * \returns Void
782 */
783Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
784{
785  // get context function is here
786  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
787  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
788  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
789  DTRACE_CABAC_VL( g_nSymbolCounter++ );
790  DTRACE_CABAC_T( "\tSkipFlag" );
791  DTRACE_CABAC_T( "\tuiCtxSkip: ");
792  DTRACE_CABAC_V( uiCtxSkip );
793  DTRACE_CABAC_T( "\tuiSymbol: ");
794  DTRACE_CABAC_V( uiSymbol );
795  DTRACE_CABAC_T( "\n");
796}
797
798#if LGE_ILLUCOMP_B0045
799/** code Illumination Compensation flag
800 * \param pcCU
801 * \param uiAbsPartIdx
802 * \returns Void
803 */
804Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
805{
806  // get context function is here
807  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
808  UInt uiCtxIC  = pcCU->getCtxICFlag( uiAbsPartIdx ) ;
809  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, uiCtxIC ) );
810  DTRACE_CABAC_VL( g_nSymbolCounter++ );
811  DTRACE_CABAC_T( "\tICFlag" );
812  DTRACE_CABAC_T( "\tuiCtxIC: ");
813  DTRACE_CABAC_V( uiCtxIC );
814  DTRACE_CABAC_T( "\tuiSymbol: ");
815  DTRACE_CABAC_V( uiSymbol );
816  DTRACE_CABAC_T( "\n");
817}
818#endif
819
820/** code merge flag
821 * \param pcCU
822 * \param uiAbsPartIdx
823 * \returns Void
824 */
825Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
826{
827  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
828  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
829
830  DTRACE_CABAC_VL( g_nSymbolCounter++ );
831  DTRACE_CABAC_T( "\tMergeFlag: " );
832  DTRACE_CABAC_V( uiSymbol );
833  DTRACE_CABAC_T( "\tAddress: " );
834  DTRACE_CABAC_V( pcCU->getAddr() );
835  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
836  DTRACE_CABAC_V( uiAbsPartIdx );
837  DTRACE_CABAC_T( "\n" );
838}
839
840/** code merge index
841 * \param pcCU
842 * \param uiAbsPartIdx
843 * \returns Void
844 */
845Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
846{
847  UInt uiNumCand = MRG_MAX_NUM_CANDS;
848#if !MRG_IDX_CTX_RED
849  UInt auiCtx[4] = { 0, 1, 2, 3 };
850#endif
851  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
852  uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
853#if HHI_MPI
854  const Bool bMVIAvailable = pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE;
855  if( bMVIAvailable )
856  {
857    const Bool bUseMVI = pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1;
858    if( bUseMVI )
859    {
860      uiUnaryIdx = (UInt)HHI_MPI_MERGE_POS;
861    }
862    else if( (Int)uiUnaryIdx >= (Int)HHI_MPI_MERGE_POS )
863    {
864      uiUnaryIdx++;
865    }
866  }
867#endif
868  if ( uiNumCand > 1 )
869  {
870    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
871    {
872      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
873#if MRG_IDX_CTX_RED
874      if ( ui==0 )
875      {
876        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
877      }
878      else
879      {
880        m_pcBinIf->encodeBinEP( uiSymbol );
881      }
882#else
883      m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, auiCtx[ui] ) );
884#endif
885      if( uiSymbol == 0 )
886      {
887        break;
888      }
889    }
890  }
891  DTRACE_CABAC_VL( g_nSymbolCounter++ );
892  DTRACE_CABAC_T( "\tparseMergeIndex()" );
893  DTRACE_CABAC_T( "\tuiMRGIdx= " );
894  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
895  DTRACE_CABAC_T( "\n" );
896}
897
898#if HHI_INTER_VIEW_RESIDUAL_PRED
899Void
900TEncSbac::codeResPredFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
901{
902  UInt  uiCtx     = pcCU->getCtxResPredFlag( uiAbsPartIdx );
903  UInt  uiSymbol  = ( pcCU->getResPredFlag( uiAbsPartIdx ) ? 1 : 0 );
904  m_pcBinIf->encodeBin( uiSymbol, m_cResPredFlagSCModel.get( 0, 0, uiCtx ) );
905}
906#endif
907
908Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
909{
910  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
911    return;
912 
913  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
914  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
915 
916  assert( uiCtx < 3 );
917
918#if OL_QTLIMIT_PREDCODING_B0068
919  Bool bCodeSplitFlag    = true;
920
921  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
922  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
923  Bool bDepthMapDetect   = (pcTexture != NULL);
924  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
925
926  if(bDepthMapDetect && !bIntraSliceDetect && sps->getUseQTLPC() && pcCU->getPic()->getReduceBitsFlag())
927  {
928    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
929    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
930    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
931    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
932  }
933
934  if(!bCodeSplitFlag)
935  {
936    assert(uiCurrSplitFlag == 0);
937    return;
938  }
939#endif
940
941  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
942  DTRACE_CABAC_VL( g_nSymbolCounter++ )
943  DTRACE_CABAC_T( "\tSplitFlag\n" )
944  return;
945}
946
947Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
948{
949  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
950  DTRACE_CABAC_VL( g_nSymbolCounter++ )
951  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
952  DTRACE_CABAC_T( "\tsymbol=" )
953  DTRACE_CABAC_V( uiSymbol )
954  DTRACE_CABAC_T( "\tctx=" )
955  DTRACE_CABAC_V( uiCtx )
956  DTRACE_CABAC_T( "\n" )
957}
958
959#if LGE_EDGE_INTRA_A0070
960Void TEncSbac::xCodeEdgeIntraInfo( TComDataCU* pcCU, UInt uiPartIdx )
961{
962  UInt   uiDepth        = pcCU->getDepth( uiPartIdx ) + (pcCU->getPartitionSize( uiPartIdx ) == SIZE_NxN ? 1 : 0);
963  UInt   uiCtxEdgeIntra = pcCU->getCtxEdgeIntra( uiPartIdx );
964  UChar* pucSymbolList  = pcCU->getEdgeCode( uiPartIdx );
965  UChar  ucEdgeNumber   = pcCU->getEdgeNumber( uiPartIdx );
966  Bool   bLeft          = pcCU->getEdgeLeftFirst( uiPartIdx );
967  UChar  ucStart        = pcCU->getEdgeStartPos( uiPartIdx );
968  UInt   uiSymbol;
969
970  // 1. Top(0) or Left(1)
971  uiSymbol = (bLeft == false) ? 0 : 1;
972  m_pcBinIf->encodeBinEP( uiSymbol );
973
974  // 2. Start position (lowest bit first)
975  uiSymbol = ucStart;
976  for( UInt ui = 6; ui > uiDepth; ui-- ) // 64(0)->6, 32(1)->5, 16(2)->4, 8(3)->3, 4(4)->2
977  {
978    m_pcBinIf->encodeBinEP( uiSymbol & 0x1 );
979    uiSymbol >>= 1;
980  }
981
982  // 3. Number of edges
983  uiSymbol = ucEdgeNumber > 0 ? ucEdgeNumber - 1 : 0;
984  for( UInt ui = 7; ui > uiDepth; ui-- ) // 64(0)->7, 32(1)->6, 16(2)->5, 8(3)->4, 4(4)->3
985  {
986    m_pcBinIf->encodeBinEP( uiSymbol & 0x1 );
987    uiSymbol >>= 1;
988  }
989
990  if(uiSymbol != 0)
991  {
992    printf(" ucEdgeNumber %d at depth %d\n",ucEdgeNumber, uiDepth);
993    assert(false);
994  }
995
996  // 4. Edges
997  for( Int iPtr2 = 0; iPtr2 < ucEdgeNumber; iPtr2++ )
998  {
999    UInt uiReorderSymbol = pucSymbolList[iPtr2];
1000
1001    //printf ("Ptr = %d, Symbol = %d\n", iPtr2, uiSymbol);
1002
1003    // Left-friendly direction (Top start)
1004    // 0 (   0deg) => 0
1005    // 1 (  45deg) => 10
1006    // 2 ( -45deg) => 110
1007    // 3 (  90deg) => 1110
1008    // 4 ( -90deg) => 11110
1009    // 5 ( 135deg) => 111110
1010    // 6 (-135deg) => 111111
1011    // Right-friendly direction (Left start)
1012    // 0 (   0deg) => 0
1013    // 2 ( -45deg) => 10
1014    // 1 (  45deg) => 110
1015    // 4 ( -90deg) => 1110
1016    // 3 (  90deg) => 11110
1017    // 6 (-135deg) => 111110
1018    // 5 ( 135deg) => 111111
1019
1020    // refer to a paper "An efficient chain code with Huffman coding"
1021
1022    for( UInt ui = 0; ui < uiReorderSymbol; ui++ )
1023    {
1024      m_pcBinIf->encodeBin( 1, m_cEdgeIntraSCModel.get( 0, 0, uiCtxEdgeIntra ) );
1025    }
1026
1027    if( uiReorderSymbol != 6 )
1028      m_pcBinIf->encodeBin( 0, m_cEdgeIntraSCModel.get( 0, 0, uiCtxEdgeIntra ) );
1029  }
1030}
1031#endif
1032
1033Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt uiAbsPartIdx )
1034{
1035  UInt uiDir         = pcCU->getLumaIntraDir( uiAbsPartIdx );
1036
1037#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
1038  if( pcCU->getSlice()->getSPS()->getUseDMM() && pcCU->getWidth( uiAbsPartIdx ) <= DMM_WEDGEMODEL_MAX_SIZE )
1039  {
1040#if LGE_EDGE_INTRA_A0070
1041    m_pcBinIf->encodeBin( uiDir >= NUM_INTRA_MODE && uiDir < EDGE_INTRA_IDX, m_cDmmFlagSCModel.get(0, 0, 0) );
1042#else
1043    m_pcBinIf->encodeBin( uiDir >= NUM_INTRA_MODE, m_cDmmFlagSCModel.get(0, 0, 0) );
1044#endif
1045  }
1046#if LGE_EDGE_INTRA_A0070
1047  if( uiDir >= NUM_INTRA_MODE && uiDir < EDGE_INTRA_IDX )
1048#else
1049  if( uiDir >= NUM_INTRA_MODE )
1050#endif
1051  {
1052    assert( pcCU->getWidth( uiAbsPartIdx ) <= DMM_WEDGEMODEL_MAX_SIZE );
1053    UInt uiDMMode = uiDir - NUM_INTRA_MODE;
1054
1055#if HHI_DMM_WEDGE_INTRA && HHI_DMM_PRED_TEX
1056    m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cDmmModeSCModel.get(0, 0, 0) );
1057    m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cDmmModeSCModel.get(0, 0, 0) );
1058
1059    if( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1060    {
1061      m_pcBinIf->encodeBin( (uiDMMode & 0x04) >> 2, m_cDmmModeSCModel.get(0, 0, 0) );
1062    }
1063#else
1064    m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cDmmModeSCModel.get(0, 0, 0) );
1065
1066    if( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1067    {
1068      m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cDmmModeSCModel.get(0, 0, 0) );
1069    }
1070#endif
1071#if HHI_DMM_WEDGE_INTRA
1072    if( uiDir == DMM_WEDGE_FULL_IDX )          { xCodeWedgeFullInfo          ( pcCU, uiAbsPartIdx ); }
1073    if( uiDir == DMM_WEDGE_FULL_D_IDX )        { xCodeWedgeFullDeltaInfo     ( pcCU, uiAbsPartIdx ); }
1074    if( uiDir == DMM_WEDGE_PREDDIR_IDX )       { xCodeWedgePredDirInfo       ( pcCU, uiAbsPartIdx ); }
1075    if( uiDir == DMM_WEDGE_PREDDIR_D_IDX )     { xCodeWedgePredDirDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1076#endif
1077#if HHI_DMM_PRED_TEX
1078    if( uiDir == DMM_WEDGE_PREDTEX_D_IDX )     { xCodeWedgePredTexDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1079    if( uiDir == DMM_CONTOUR_PREDTEX_D_IDX )   { xCodeContourPredTexDeltaInfo( pcCU, uiAbsPartIdx ); }
1080#endif
1081  }
1082  else
1083#if LGE_EDGE_INTRA_A0070
1084    if ( uiDir >= EDGE_INTRA_IDX)
1085    {
1086      m_pcBinIf->encodeBin( 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1087      m_pcBinIf->encodeBinsEP( 63, 6 );
1088      xCodeEdgeIntraInfo( pcCU, uiAbsPartIdx );
1089#if LGE_EDGE_INTRA_DELTA_DC
1090      m_pcBinIf->encodeBin( (uiDir == EDGE_INTRA_DELTA_IDX), m_cEdgeIntraDeltaDCSCModel.get(0, 0, 0) );
1091      if( uiDir == EDGE_INTRA_DELTA_IDX )
1092      {
1093        Int iDeltaDC0 = pcCU->getEdgeDeltaDC0( uiAbsPartIdx );
1094        Int iDeltaDC1 = pcCU->getEdgeDeltaDC1( uiAbsPartIdx );
1095
1096        xWriteExGolombLevel( UInt( abs( iDeltaDC0 ) ), m_cEdgeIntraDeltaDCSCModel.get(0, 0, 1) );
1097        if ( iDeltaDC0 != 0 )
1098        {
1099          UInt uiSign = iDeltaDC0 > 0 ? 0 : 1;
1100          m_pcBinIf->encodeBinEP( uiSign );
1101        }
1102        xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cEdgeIntraDeltaDCSCModel.get(0, 0, 1) );
1103        if ( iDeltaDC1 != 0 )
1104        {
1105          UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
1106          m_pcBinIf->encodeBinEP( uiSign );
1107        }
1108      }
1109#endif
1110    }
1111    else
1112#endif // LGE_EDGE_INTRA
1113  {
1114#endif
1115#if !LOGI_INTRA_NAME_3MPM
1116  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
1117#endif
1118#if LGE_EDGE_INTRA_A0070
1119  Bool bCodeEdgeIntra = false;
1120  if( pcCU->getSlice()->getSPS()->isDepth() )
1121  {
1122    UInt uiPUWidth = pcCU->getWidth( uiAbsPartIdx ) >> (pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN ? 1 : 0);
1123    if( uiPUWidth <= LGE_EDGE_INTRA_MAX_SIZE && uiPUWidth >= LGE_EDGE_INTRA_MIN_SIZE )
1124      bCodeEdgeIntra = true;
1125  }
1126#endif
1127 
1128#if LOGI_INTRA_NAME_3MPM
1129  Int uiPreds[3] = {-1, -1, -1};
1130#else
1131  Int uiPreds[2] = {-1, -1};
1132#endif
1133  Int uiPredNum = pcCU->getIntraDirLumaPredictor(uiAbsPartIdx, uiPreds); 
1134
1135  Int uiPredIdx = -1;
1136
1137  for(UInt i = 0; i < uiPredNum; i++)
1138  {
1139    if(uiDir == uiPreds[i])
1140    {
1141      uiPredIdx = i;
1142    }
1143  }
1144 
1145  if(uiPredIdx != -1)
1146  {
1147    m_pcBinIf->encodeBin( 1, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1148#if LOGI_INTRA_NAME_3MPM
1149    m_pcBinIf->encodeBinEP( uiPredIdx ? 1 : 0 );
1150    if (uiPredIdx)
1151    {
1152      m_pcBinIf->encodeBinEP( uiPredIdx-1 );
1153    }
1154#else
1155    m_pcBinIf->encodeBinEP( uiPredIdx );
1156#endif
1157  }
1158  else
1159  {
1160    m_pcBinIf->encodeBin( 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1161 
1162#if LOGI_INTRA_NAME_3MPM
1163    if (uiPreds[0] > uiPreds[1])
1164    { 
1165      std::swap(uiPreds[0], uiPreds[1]); 
1166    }
1167    if (uiPreds[0] > uiPreds[2])
1168    {
1169      std::swap(uiPreds[0], uiPreds[2]);
1170    }
1171    if (uiPreds[1] > uiPreds[2])
1172    {
1173      std::swap(uiPreds[1], uiPreds[2]);
1174    }
1175#endif
1176
1177    for(Int i = (uiPredNum - 1); i >= 0; i--)
1178    {
1179      uiDir = uiDir > uiPreds[i] ? uiDir - 1 : uiDir;
1180    }
1181
1182#if LOGI_INTRA_NAME_3MPM
1183    m_pcBinIf->encodeBinsEP( uiDir, 5 );
1184#if LGE_EDGE_INTRA_A0070
1185  if (bCodeEdgeIntra)
1186    if (uiDir == 31) m_pcBinIf->encodeBinsEP(0,1);
1187#endif
1188#else
1189    if ( uiDir < 31 )
1190    {
1191      m_pcBinIf->encodeBinsEP( uiDir, g_aucIntraModeBitsAng[ iIntraIdx ] - 1 );
1192    }
1193    else
1194    {
1195      m_pcBinIf->encodeBinsEP( 31, 5 );
1196      m_pcBinIf->encodeBinEP( uiDir - 31 );
1197    }
1198#endif
1199   }
1200#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
1201  }
1202#endif
1203  return;
1204}
1205
1206Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1207{
1208  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1209
1210  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1211  {
1212    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1213  } 
1214  else if( uiIntraDirChroma == LM_CHROMA_IDX )
1215  {
1216    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1217    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 1 ) );
1218  }
1219  else
1220  { 
1221    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1222    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1223
1224    for( Int i = 0; i < NUM_CHROMA_MODE - 2; i++ )
1225    {
1226      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1227      {
1228        uiIntraDirChroma = i;
1229        break;
1230      }
1231    }
1232    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1233
1234    if (pcCU->getSlice()->getSPS()->getUseLMChroma())
1235    {
1236      m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 1 ));
1237    }
1238#if  CHROMA_MODE_CODING
1239    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1240#else
1241    xWriteUnaryMaxSymbol( uiIntraDirChroma, m_cCUChromaPredSCModel.get( 0, 0 ) + 1, 0, 3 );
1242#endif
1243  }
1244  return;
1245}
1246
1247Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1248{
1249  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1250  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1251  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1252  m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1253
1254  return;
1255}
1256
1257Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1258{
1259  if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getInterDir( uiAbsPartIdx ) != 3)
1260  {
1261    Int iRefFrame = pcCU->getSlice()->getRefIdxOfLC(eRefList, pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ));
1262
1263    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1264    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1265
1266    if( iRefFrame > 0 )
1267    {
1268      xWriteUnaryMaxSymbol( iRefFrame - 1, pCtx + 1, 1, pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_C )-2 );
1269    }
1270  }
1271  else
1272  {
1273    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1274    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1275    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1276   
1277    if( iRefFrame > 0 )
1278    {
1279      xWriteUnaryMaxSymbol( iRefFrame - 1, pCtx + 1, 1, pcCU->getSlice()->getNumRefIdx( eRefList )-2 );
1280    }
1281  }
1282  return;
1283}
1284
1285Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1286{
1287#if H0111_MVD_L1_ZERO
1288  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1289  {
1290    return;
1291  }
1292#endif
1293
1294  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1295  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1296  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1297  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1298
1299  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1300  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1301
1302  const Bool bHorAbsGr0 = iHor != 0;
1303  const Bool bVerAbsGr0 = iVer != 0;
1304  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1305  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1306  pCtx++;
1307
1308  if( bHorAbsGr0 )
1309  {
1310    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1311  }
1312
1313  if( bVerAbsGr0 )
1314  {
1315    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1316  }
1317
1318  if( bHorAbsGr0 )
1319  {
1320    if( uiHorAbs > 1 )
1321    {
1322      xWriteEpExGolomb( uiHorAbs-2, 1 );
1323    }
1324
1325    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1326  }
1327
1328  if( bVerAbsGr0 )
1329  {
1330    if( uiVerAbs > 1 )
1331    {
1332      xWriteEpExGolomb( uiVerAbs-2, 1 );
1333    }
1334
1335    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1336  }
1337 
1338  return;
1339}
1340
1341Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1342{
1343  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1344 
1345#if H0736_AVC_STYLE_QP_RANGE
1346  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1347  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1348#else
1349#if LOSSLESS_CODING
1350  if(pcCU->getSlice()->getSPS()->getUseLossless())
1351  {
1352    if(iDQp > 25)
1353    {
1354      iDQp = iDQp - 52;
1355    }
1356    if(iDQp < -26)
1357    {
1358      iDQp = iDQp + 52;
1359    }
1360  }
1361#endif
1362#endif
1363
1364  if ( iDQp == 0 )
1365  {
1366    m_pcBinIf->encodeBin( 0, m_cCUDeltaQpSCModel.get( 0, 0, 0 ) );
1367  }
1368  else
1369  {
1370    m_pcBinIf->encodeBin( 1, m_cCUDeltaQpSCModel.get( 0, 0, 0 ) );
1371   
1372    UInt uiSign = (iDQp > 0 ? 0 : 1);
1373#if !H0736_AVC_STYLE_QP_RANGE
1374    UInt uiQpBdOffsetY = 6*(g_uiBitIncrement + g_uiBitDepth - 8);
1375#endif
1376
1377    m_pcBinIf->encodeBinEP(uiSign);
1378
1379#if H0736_AVC_STYLE_QP_RANGE
1380    assert(iDQp >= -(26+(qpBdOffsetY/2)));
1381    assert(iDQp <=  (25+(qpBdOffsetY/2)));
1382
1383    UInt uiMaxAbsDQpMinus1 = 24 + (qpBdOffsetY/2) + (uiSign);
1384#else
1385    assert(iDQp >= -(26+(Int)(uiQpBdOffsetY/2)));
1386    assert(iDQp <=  (25+(Int)(uiQpBdOffsetY/2)));
1387
1388    UInt uiMaxAbsDQpMinus1 = 24 + (uiQpBdOffsetY/2) + (uiSign);
1389#endif
1390    UInt uiAbsDQpMinus1 = (UInt)((iDQp > 0)? iDQp  : (-iDQp)) - 1;
1391    xWriteUnaryMaxSymbol( uiAbsDQpMinus1, &m_cCUDeltaQpSCModel.get( 0, 0, 1 ), 1, uiMaxAbsDQpMinus1);
1392  }
1393 
1394  return;
1395}
1396
1397Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1398{
1399  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1400  UInt uiCtx = pcCU->getCtxQtCbf( uiAbsPartIdx, eType, uiTrDepth );
1401  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1402  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1403  DTRACE_CABAC_T( "\tparseQtCbf()" )
1404  DTRACE_CABAC_T( "\tsymbol=" )
1405  DTRACE_CABAC_V( uiCbf )
1406  DTRACE_CABAC_T( "\tctx=" )
1407  DTRACE_CABAC_V( uiCtx )
1408  DTRACE_CABAC_T( "\tetype=" )
1409  DTRACE_CABAC_V( eType )
1410  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1411  DTRACE_CABAC_V( uiAbsPartIdx )
1412  DTRACE_CABAC_T( "\n" )
1413}
1414
1415#if BURST_IPCM
1416/** Code I_PCM information.
1417 * \param pcCU pointer to CU
1418 * \param uiAbsPartIdx CU index
1419 * \param numIPCM the number of succesive IPCM blocks with the same size
1420 * \param firstIPCMFlag
1421 * \returns Void
1422 */
1423Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Int numIPCM, Bool firstIPCMFlag)
1424#else
1425/** Code I_PCM information.
1426 * \param pcCU pointer to CU
1427 * \param uiAbsPartIdx CU index
1428 * \returns Void
1429 *
1430 * If I_PCM flag indicates that the CU is I_PCM, code its PCM alignment bits and codes. 
1431 */
1432Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx)
1433#endif
1434{
1435  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1436
1437#if BURST_IPCM
1438  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1439
1440  if( uiIPCM == 0 || firstIPCMFlag)
1441  {
1442    m_pcBinIf->encodeBinTrm (uiIPCM);
1443
1444    if ( firstIPCMFlag )
1445    {
1446      m_pcBinIf->encodeNumSubseqIPCM( numIPCM - 1 );
1447      m_pcBinIf->encodePCMAlignBits();
1448    }
1449  }
1450#else
1451  m_pcBinIf->encodeBinTrm (uiIPCM);
1452#endif
1453
1454#if BURST_IPCM
1455  if (writePCMSampleFlag)
1456#else
1457  if (uiIPCM)
1458#endif
1459  {
1460#if !BURST_IPCM
1461    m_pcBinIf->encodePCMAlignBits();
1462#endif
1463    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1464    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1465    UInt uiChromaOffset = uiLumaOffset>>2;
1466    Pel* piPCMSample;
1467    UInt uiWidth;
1468    UInt uiHeight;
1469    UInt uiSampleBits;
1470    UInt uiX, uiY;
1471
1472    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1473    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1474    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1475    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1476
1477    for(uiY = 0; uiY < uiHeight; uiY++)
1478    {
1479      for(uiX = 0; uiX < uiWidth; uiX++)
1480      {
1481        UInt uiSample = piPCMSample[uiX];
1482
1483        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1484      }
1485      piPCMSample += uiWidth;
1486    }
1487
1488    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1489    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1490    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1491    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1492
1493    for(uiY = 0; uiY < uiHeight; uiY++)
1494    {
1495      for(uiX = 0; uiX < uiWidth; uiX++)
1496      {
1497        UInt uiSample = piPCMSample[uiX];
1498
1499        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1500      }
1501      piPCMSample += uiWidth;
1502    }
1503
1504    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1505    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1506    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1507    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1508
1509    for(uiY = 0; uiY < uiHeight; uiY++)
1510    {
1511      for(uiX = 0; uiX < uiWidth; uiX++)
1512      {
1513        UInt uiSample = piPCMSample[uiX];
1514
1515        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1516      }
1517      piPCMSample += uiWidth;
1518    }
1519#if BURST_IPCM
1520    numIPCM--;
1521    if(numIPCM == 0)
1522    {
1523      m_pcBinIf->resetBac();
1524    }
1525#else
1526    m_pcBinIf->resetBac();
1527#endif
1528  }
1529}
1530
1531Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1532{
1533  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1534  UInt uiCtx = 0;
1535  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1536  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1537  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1538  DTRACE_CABAC_T( "\tsymbol=" )
1539  DTRACE_CABAC_V( uiCbf )
1540  DTRACE_CABAC_T( "\tctx=" )
1541  DTRACE_CABAC_V( uiCtx )
1542  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1543  DTRACE_CABAC_V( uiAbsPartIdx )
1544  DTRACE_CABAC_T( "\n" )
1545}
1546
1547/** Encode (X,Y) position of the last significant coefficient
1548 * \param uiPosX X component of last coefficient
1549 * \param uiPosY Y component of last coefficient
1550 * \param width  Block width
1551 * \param height Block height
1552 * \param eTType plane type / luminance or chrominance
1553 * \param uiScanIdx scan type (zig-zag, hor, ver)
1554 * This method encodes the X and Y component within a block of the last significant coefficient.
1555 */
1556Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1557{ 
1558  // swap
1559  if( uiScanIdx == SCAN_VER )
1560  {
1561    swap( uiPosX, uiPosY );
1562  }
1563
1564  UInt uiCtxLast;
1565  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1566  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1567  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1568  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1569
1570  // posX
1571#if LAST_CTX_REDUCTION
1572  Int widthCtx = eTType? 4: width;
1573  const UInt *puiCtxIdxX = g_uiLastCtx + ( g_aucConvertToBit[ widthCtx ] * ( g_aucConvertToBit[ widthCtx ] + 3 ) );
1574#else
1575  const UInt *puiCtxIdxX = g_uiLastCtx + ( g_aucConvertToBit[ width ] * ( g_aucConvertToBit[ width ] + 3 ) );
1576#endif
1577  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1578  {
1579#if LAST_CTX_REDUCTION
1580    if (eTType)
1581    {
1582      m_pcBinIf->encodeBin( 1, *( pCtxX + (uiCtxLast>>g_aucConvertToBit[ width ]) ) );
1583    }
1584    else
1585    {
1586#endif
1587      m_pcBinIf->encodeBin( 1, *( pCtxX + puiCtxIdxX[ uiCtxLast ] ) );
1588#if LAST_CTX_REDUCTION
1589    }
1590#endif
1591  }
1592  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1593  {
1594#if LAST_CTX_REDUCTION
1595    if ( eTType )
1596    {
1597      m_pcBinIf->encodeBin( 0, *( pCtxX + (uiCtxLast>>g_aucConvertToBit[ width ]) ) );
1598    }
1599    else
1600    {
1601#endif
1602      m_pcBinIf->encodeBin( 0, *( pCtxX + puiCtxIdxX[ uiCtxLast ] ) );
1603#if LAST_CTX_REDUCTION
1604    }
1605#endif
1606  }
1607
1608  // posY
1609#if LAST_CTX_REDUCTION
1610  Int heightCtx = eTType? 4: height;
1611  const UInt *puiCtxIdxY = g_uiLastCtx + ( g_aucConvertToBit[ heightCtx ] * ( g_aucConvertToBit[ heightCtx ] + 3 ) );
1612#else
1613  const UInt *puiCtxIdxY = g_uiLastCtx + ( g_aucConvertToBit[ height ] * ( g_aucConvertToBit[ height ] + 3 ) );
1614#endif
1615  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1616  {
1617#if LAST_CTX_REDUCTION
1618    if (eTType)
1619    {
1620      m_pcBinIf->encodeBin( 1, *( pCtxY + (uiCtxLast>>g_aucConvertToBit[ height ])));
1621    }
1622    else
1623    {
1624#endif
1625      m_pcBinIf->encodeBin( 1, *( pCtxY + puiCtxIdxY[ uiCtxLast ] ) );
1626#if LAST_CTX_REDUCTION
1627    }
1628#endif
1629  }
1630  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1631  {
1632#if LAST_CTX_REDUCTION
1633    if (eTType)
1634    {
1635      m_pcBinIf->encodeBin( 0, *( pCtxY + (uiCtxLast>>g_aucConvertToBit[ height ]) ) );
1636    }
1637    else
1638    {
1639#endif
1640      m_pcBinIf->encodeBin( 0, *( pCtxY + puiCtxIdxY[ uiCtxLast ] ) );
1641#if LAST_CTX_REDUCTION
1642    }
1643#endif
1644    }
1645  if ( uiGroupIdxX > 3 )
1646  {     
1647    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1648    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1649    for (Int i = uiCount - 1 ; i >= 0; i-- )
1650    {
1651      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1652    }
1653  }
1654  if ( uiGroupIdxY > 3 )
1655  {     
1656    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1657    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1658    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1659    {
1660      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1661    }
1662  }
1663}
1664
1665Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1666{
1667  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1668  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1669  DTRACE_CABAC_V( eTType )
1670  DTRACE_CABAC_T( "\twidth=" )
1671  DTRACE_CABAC_V( uiWidth )
1672  DTRACE_CABAC_T( "\theight=" )
1673  DTRACE_CABAC_V( uiHeight )
1674  DTRACE_CABAC_T( "\tdepth=" )
1675  DTRACE_CABAC_V( uiDepth )
1676  DTRACE_CABAC_T( "\tabspartidx=" )
1677  DTRACE_CABAC_V( uiAbsPartIdx )
1678  DTRACE_CABAC_T( "\ttoCU-X=" )
1679  DTRACE_CABAC_V( pcCU->getCUPelX() )
1680  DTRACE_CABAC_T( "\ttoCU-Y=" )
1681  DTRACE_CABAC_V( pcCU->getCUPelY() )
1682  DTRACE_CABAC_T( "\tCU-addr=" )
1683  DTRACE_CABAC_V(  pcCU->getAddr() )
1684  DTRACE_CABAC_T( "\tinCU-X=" )
1685  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1686  DTRACE_CABAC_T( "\tinCU-Y=" )
1687  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1688  DTRACE_CABAC_T( "\tpredmode=" )
1689  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1690  DTRACE_CABAC_T( "\n" )
1691
1692  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1693  {
1694    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1695    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1696  }
1697 
1698  UInt uiNumSig = 0;
1699 
1700  // compute number of significant coefficients
1701  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1702 
1703  if ( uiNumSig == 0 )
1704    return;
1705 
1706  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1707 
1708  //----- encode significance map -----
1709  const UInt   uiLog2BlockSize   = g_aucConvertToBit[ uiWidth ] + 2;
1710  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1711  if (uiScanIdx == SCAN_ZIGZAG)
1712  {
1713    // Map zigzag to diagonal scan
1714    uiScanIdx = SCAN_DIAG;
1715  }
1716  Int blockType = uiLog2BlockSize;
1717  if (uiWidth != uiHeight)
1718  {
1719    uiScanIdx = SCAN_DIAG;
1720    blockType = 4;
1721  }
1722 
1723  const UInt * scan;
1724  if (uiWidth == uiHeight)
1725  {
1726    scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1727  }
1728  else
1729  {
1730    scan = g_sigScanNSQT[ uiLog2BlockSize - 2 ];
1731  }
1732 
1733#if MULTIBITS_DATA_HIDING
1734  UInt const tsig = pcCU->getSlice()->getPPS()->getTSIG();
1735#if LOSSLESS_CODING
1736  Bool beValid; 
1737  if (pcCU->isLosslessCoded(uiAbsPartIdx))
1738  {
1739    beValid = false;
1740  }
1741  else 
1742  {
1743    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1744  }
1745#else
1746  Bool beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1747#endif
1748#endif
1749
1750  // Find position of last coefficient
1751  Int scanPosLast = -1;
1752  Int posLast;
1753
1754  const UInt * scanCG;
1755  if (uiWidth == uiHeight)
1756  {
1757    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1758#if MULTILEVEL_SIGMAP_EXT
1759    if( uiLog2BlockSize == 3 )
1760    {
1761      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1762    }
1763    else if( uiLog2BlockSize == 5 )
1764    {
1765      scanCG = g_sigLastScanCG32x32;
1766    }
1767#endif
1768  }
1769  else
1770  {
1771    scanCG = g_sigCGScanNSQT[ uiLog2BlockSize - 2 ];
1772  }
1773  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1774  static const UInt uiShift = MLS_CG_SIZE >> 1;
1775  const UInt uiNumBlkSide = uiWidth >> uiShift;
1776
1777#if !MULTILEVEL_SIGMAP_EXT
1778  if( blockType > 3 )
1779  {
1780#endif
1781    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1782
1783    do
1784    {
1785      posLast = scan[ ++scanPosLast ];
1786
1787      // get L1 sig map
1788      UInt uiPosY    = posLast >> uiLog2BlockSize;
1789      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1790      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1791#if MULTILEVEL_SIGMAP_EXT
1792      if( uiWidth == 8 && uiHeight == 8 && (uiScanIdx == SCAN_HOR || uiScanIdx == SCAN_VER) )
1793      {
1794        if( uiScanIdx == SCAN_HOR )
1795        {
1796          uiBlkIdx = uiPosY >> 1;
1797        }
1798        else if( uiScanIdx == SCAN_VER )
1799        {
1800          uiBlkIdx = uiPosX >> 1;
1801        }
1802      }
1803#endif
1804      if( pcCoef[ posLast ] )
1805      {
1806        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1807      }
1808
1809      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1810    }
1811    while ( uiNumSig > 0 );
1812#if !MULTILEVEL_SIGMAP_EXT
1813  }
1814  else
1815  {
1816 
1817  do
1818  {
1819    posLast = scan[ ++scanPosLast ];
1820    uiNumSig -= ( pcCoef[ posLast ] != 0 );
1821  }
1822  while ( uiNumSig > 0 );
1823
1824  }
1825#endif
1826
1827  // Code position of last coefficient
1828  Int posLastY = posLast >> uiLog2BlockSize;
1829  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1830  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1831 
1832  //===== code significance flag =====
1833  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1834  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1835
1836
1837  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1838  UInt uiNumOne                = 0;
1839  UInt uiGoRiceParam           = 0;
1840  Int  iScanPosSig             = scanPosLast;
1841
1842  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1843  {
1844    Int numNonZero = 0;
1845    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1846    uiGoRiceParam    = 0;
1847    Int absCoeff[16];
1848    UInt coeffSigns = 0;
1849
1850#if MULTIBITS_DATA_HIDING
1851    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1852#endif
1853
1854    if( iScanPosSig == scanPosLast )
1855    {
1856      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1857      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1858      numNonZero    = 1;
1859#if MULTIBITS_DATA_HIDING
1860      lastNZPosInCG  = iScanPosSig;
1861      firstNZPosInCG = iScanPosSig;
1862#endif
1863      iScanPosSig--;
1864    }
1865
1866#if !MULTILEVEL_SIGMAP_EXT
1867    if( blockType > 3 )
1868    {
1869#endif
1870      // encode significant_coeffgroup_flag
1871      Int iCGBlkPos = scanCG[ iSubSet ];
1872      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1873      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1874#if MULTILEVEL_SIGMAP_EXT
1875      if( uiWidth == 8 && uiHeight == 8 && (uiScanIdx == SCAN_HOR || uiScanIdx == SCAN_VER) )
1876      {
1877        iCGPosY = (uiScanIdx == SCAN_HOR ? iCGBlkPos : 0);
1878        iCGPosX = (uiScanIdx == SCAN_VER ? iCGBlkPos : 0);
1879      }
1880#endif
1881#if !REMOVE_INFER_SIGGRP
1882      Bool bInferredCGFlag = false;
1883#endif
1884#if REMOVE_INFER_SIGGRP
1885      if( iSubSet == iLastScanSet || iSubSet == 0)
1886#else     
1887      if( iSubSet == iLastScanSet )
1888#endif
1889      {
1890        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1891      }
1892      else
1893      {
1894#if !REMOVE_INFER_SIGGRP
1895#if MULTILEVEL_SIGMAP_EXT
1896        if( !TComTrQuant::bothCGNeighboursOne( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiScanIdx, uiWidth, uiHeight ) && ( iSubSet ) )
1897#else
1898        if( !TComTrQuant::bothCGNeighboursOne( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight ) && ( iSubSet ) )
1899#endif
1900        {
1901#endif
1902          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1903#if MULTILEVEL_SIGMAP_EXT
1904          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiScanIdx, uiWidth, uiHeight );
1905#else
1906          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1907#endif
1908          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1909#if !REMOVE_INFER_SIGGRP
1910        }
1911        else
1912        {
1913          uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1914          bInferredCGFlag = true;
1915        }
1916#endif
1917      }
1918     
1919      // encode significant_coeff_flag
1920      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1921      {
1922        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1923        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1924        {
1925          uiBlkPos  = scan[ iScanPosSig ]; 
1926          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1927          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1928          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1929#if REMOVE_INFER_SIGGRP
1930          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1931#else
1932          if( iScanPosSig > iSubPos || bInferredCGFlag || numNonZero )
1933#endif
1934          {
1935            uiCtxSig  = TComTrQuant::getSigCtxInc( pcCoef, uiPosX, uiPosY, blockType, uiWidth, uiHeight, eTType );
1936            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1937          }
1938          if( uiSig )
1939          {
1940            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1941            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1942            numNonZero++;
1943#if MULTIBITS_DATA_HIDING
1944            if( lastNZPosInCG == -1 )
1945            {
1946              lastNZPosInCG = iScanPosSig;
1947            }
1948            firstNZPosInCG = iScanPosSig;
1949#endif
1950          }
1951        }
1952      }
1953      else
1954      {
1955        iScanPosSig = iSubPos - 1;
1956      }
1957#if !MULTILEVEL_SIGMAP_EXT
1958    } 
1959    else
1960    {
1961
1962    for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1963    {
1964      UInt  uiBlkPos  = scan[ iScanPosSig ]; 
1965      UInt  uiPosY    = uiBlkPos >> uiLog2BlockSize;
1966      UInt  uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1967      UInt  uiSig     = 0; 
1968      if( pcCoef[ uiBlkPos ] != 0 )
1969      {
1970        uiSig = 1;
1971        absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1972        coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1973        numNonZero++;
1974#if MULTIBITS_DATA_HIDING
1975        if( lastNZPosInCG == -1 )
1976        {
1977          lastNZPosInCG = iScanPosSig;
1978        }
1979        firstNZPosInCG = iScanPosSig;
1980#endif
1981      }     
1982      UInt  uiCtxSig  = TComTrQuant::getSigCtxInc( pcCoef, uiPosX, uiPosY, blockType, uiWidth, uiHeight, eTType );
1983      m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1984    }
1985
1986    }
1987#endif
1988
1989    if( numNonZero > 0 )
1990    {
1991#if MULTIBITS_DATA_HIDING
1992      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= (Int)tsig );
1993#endif  // MULTIBITS_DATA_HIDING
1994
1995      UInt c1 = 1;
1996#if !RESTRICT_GR1GR2FLAG_NUMBER
1997      UInt c2 = 0;
1998#endif
1999#if LEVEL_CTX_LUMA_RED
2000      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
2001#else
2002      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 3 : 0;
2003#endif
2004     
2005      if( uiNumOne > 0 )
2006      {
2007        uiCtxSet++;
2008#if !LEVEL_CTX_LUMA_RED
2009        if( uiNumOne > 3 && eTType==TEXT_LUMA)
2010        {
2011          uiCtxSet++;
2012        }
2013#endif
2014      }
2015     
2016      uiNumOne       >>= 1;
2017      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
2018     
2019#if RESTRICT_GR1GR2FLAG_NUMBER
2020      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
2021      Int firstC2FlagIdx = -1;
2022      for( Int idx = 0; idx < numC1Flag; idx++ )
2023#else
2024      for ( Int idx = 0; idx < numNonZero; idx++ )
2025#endif
2026      {
2027        UInt uiSymbol = absCoeff[ idx ] > 1;
2028        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
2029        if( uiSymbol )
2030        {
2031          c1 = 0;
2032
2033#if RESTRICT_GR1GR2FLAG_NUMBER
2034          if (firstC2FlagIdx == -1)
2035          {
2036            firstC2FlagIdx = idx;
2037          }
2038#endif
2039        }
2040        else if( (c1 < 3) && (c1 > 0) )
2041        {
2042          c1++;
2043        }
2044      }
2045     
2046      if (c1 == 0)
2047      {
2048
2049#if RESTRICT_GR1GR2FLAG_NUMBER
2050        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
2051        if ( firstC2FlagIdx != -1)
2052        {
2053          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
2054          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
2055        }
2056#else   
2057        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + 3 * uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + 3 * uiCtxSet;
2058        for ( Int idx = 0; idx < numNonZero; idx++ )
2059        {
2060          if( absCoeff[ idx ] > 1 )
2061          {
2062            UInt symbol = absCoeff[ idx ] > 2;
2063            m_pcBinIf->encodeBin( symbol, baseCtxMod[c2] );
2064            c2 += (c2 < 2);
2065            uiNumOne++;
2066          }
2067        }
2068#endif
2069      }
2070     
2071#if MULTIBITS_DATA_HIDING
2072      if( beValid && signHidden )
2073      {
2074        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
2075      }
2076      else
2077      {
2078        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
2079      }
2080#else
2081      m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
2082#endif
2083     
2084#if RESTRICT_GR1GR2FLAG_NUMBER
2085      Int iFirstCoeff2 = 1;   
2086      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
2087#else
2088      if (c1 == 0)
2089#endif
2090      {
2091        for ( Int idx = 0; idx < numNonZero; idx++ )
2092        {
2093#if RESTRICT_GR1GR2FLAG_NUMBER
2094          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
2095
2096          if( absCoeff[ idx ] >= baseLevel)
2097          {
2098            xWriteGoRiceExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam ); 
2099          }
2100          if(absCoeff[ idx ] >= 2) 
2101          {
2102            iFirstCoeff2 = 0;
2103            uiNumOne++;
2104          }
2105#else
2106          if ( absCoeff[ idx ] > 2 )
2107          {
2108            xWriteGoRiceExGolomb( absCoeff[ idx ]  - 3, uiGoRiceParam );           
2109          }
2110#endif
2111        }       
2112      }
2113    }
2114    else
2115    {
2116      uiNumOne >>= 1;
2117    }
2118  }
2119
2120  return;
2121}
2122
2123Void TEncSbac::codeAlfFlag       ( UInt uiCode )
2124{
2125  UInt uiSymbol = ( ( uiCode == 0 ) ? 0 : 1 );
2126  m_pcBinIf->encodeBin( uiSymbol, m_cALFFlagSCModel.get( 0, 0, 0 ) );
2127}
2128
2129Void TEncSbac::codeAlfCtrlFlag( UInt uiSymbol )
2130{
2131  m_pcBinIf->encodeBin( uiSymbol, m_cCUAlfCtrlFlagSCModel.get( 0, 0, 0) );
2132}
2133
2134Void TEncSbac::codeAlfUvlc       ( UInt uiCode )
2135{
2136  Int i;
2137 
2138  if ( uiCode == 0 )
2139  {
2140    m_pcBinIf->encodeBin( 0, m_cALFUvlcSCModel.get( 0, 0, 0 ) );
2141  }
2142  else
2143  {
2144    m_pcBinIf->encodeBin( 1, m_cALFUvlcSCModel.get( 0, 0, 0 ) );
2145    for ( i=0; i<uiCode-1; i++ )
2146    {
2147      m_pcBinIf->encodeBin( 1, m_cALFUvlcSCModel.get( 0, 0, 1 ) );
2148    }
2149    m_pcBinIf->encodeBin( 0, m_cALFUvlcSCModel.get( 0, 0, 1 ) );
2150  }
2151}
2152
2153Void TEncSbac::codeAlfSvlc       ( Int iCode )
2154{
2155  Int i;
2156 
2157  if ( iCode == 0 )
2158  {
2159    m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 0 ) );
2160  }
2161  else
2162  {
2163    m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 0 ) );
2164   
2165    // write sign
2166    if ( iCode > 0 )
2167    {
2168      m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 1 ) );
2169    }
2170    else
2171    {
2172      m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 1 ) );
2173      iCode = -iCode;
2174    }
2175   
2176    // write magnitude
2177    for ( i=0; i<iCode-1; i++ )
2178    {
2179      m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 2 ) );
2180    }
2181    m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 2 ) );
2182  }
2183}
2184
2185Void TEncSbac::codeSaoFlag       ( UInt uiCode )
2186{
2187  UInt uiSymbol = ( ( uiCode == 0 ) ? 0 : 1 );
2188  m_pcBinIf->encodeBin( uiSymbol, m_cSaoFlagSCModel.get( 0, 0, 0 ) );
2189}
2190
2191Void TEncSbac::codeSaoUvlc       ( UInt uiCode )
2192{
2193  Int i;
2194
2195  if ( uiCode == 0 )
2196  {
2197    m_pcBinIf->encodeBin( 0, m_cSaoUvlcSCModel.get( 0, 0, 0 ) );
2198  }
2199  else
2200  {
2201    m_pcBinIf->encodeBin( 1, m_cSaoUvlcSCModel.get( 0, 0, 0 ) );
2202    for ( i=0; i<uiCode-1; i++ )
2203    {
2204      m_pcBinIf->encodeBin( 1, m_cSaoUvlcSCModel.get( 0, 0, 1 ) );
2205    }
2206    m_pcBinIf->encodeBin( 0, m_cSaoUvlcSCModel.get( 0, 0, 1 ) );
2207  }
2208}
2209
2210Void TEncSbac::codeSaoSvlc       ( Int iCode )
2211{
2212  Int i;
2213
2214  if ( iCode == 0 )
2215  {
2216    m_pcBinIf->encodeBin( 0, m_cSaoSvlcSCModel.get( 0, 0, 0 ) );
2217  }
2218  else
2219  {
2220    m_pcBinIf->encodeBin( 1, m_cSaoSvlcSCModel.get( 0, 0, 0 ) );
2221
2222    // write sign
2223    if ( iCode > 0 )
2224    {
2225      m_pcBinIf->encodeBin( 0, m_cSaoSvlcSCModel.get( 0, 0, 1 ) );
2226    }
2227    else
2228    {
2229      m_pcBinIf->encodeBin( 1, m_cSaoSvlcSCModel.get( 0, 0, 1 ) );
2230      iCode = -iCode;
2231    }
2232
2233    // write magnitude
2234    for ( i=0; i<iCode-1; i++ )
2235    {
2236      m_pcBinIf->encodeBin( 1, m_cSaoSvlcSCModel.get( 0, 0, 2 ) );
2237    }
2238    m_pcBinIf->encodeBin( 0, m_cSaoSvlcSCModel.get( 0, 0, 2 ) );
2239  }
2240}
2241#if SAO_UNIT_INTERLEAVING
2242/** Code SAO band position
2243 * \param uiCode
2244 */
2245Void TEncSbac::codeSaoUflc       ( UInt uiCode )
2246{
2247  for (Int i=0;i<5;i++)
2248  {
2249    m_pcBinIf->encodeBinEP ( (uiCode>>i) &0x01 );
2250  }
2251}
2252/** Code SAO merge left flag
2253 * \param uiCode
2254 * \param uiCompIdx
2255 */
2256Void TEncSbac::codeSaoMergeLeft       ( UInt uiCode, UInt uiCompIdx )
2257{
2258  if (uiCode == 0)
2259  {
2260    m_pcBinIf->encodeBin(0,  m_cSaoMergeLeftSCModel.get( 0, 0, uiCompIdx ));
2261  }
2262  else
2263  {
2264    m_pcBinIf->encodeBin(1,  m_cSaoMergeLeftSCModel.get( 0, 0, uiCompIdx ));
2265  }
2266}
2267/** Code SAO merge up flag
2268 * \param uiCode
2269 */
2270Void TEncSbac::codeSaoMergeUp       ( UInt uiCode)
2271{
2272  if (uiCode == 0)
2273  {
2274    m_pcBinIf->encodeBin(0,  m_cSaoMergeUpSCModel.get( 0, 0, 0 ));
2275  }
2276  else
2277  {
2278    m_pcBinIf->encodeBin(1,  m_cSaoMergeUpSCModel.get( 0, 0, 0 ));
2279  }
2280}
2281/** Code SAO type index
2282 * \param uiCode
2283 */
2284Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
2285{
2286  Int i;
2287  if ( uiCode == 0 )
2288  {
2289    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2290  }
2291  else
2292  {
2293    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2294    for ( i=0; i<uiCode-1; i++ )
2295    {
2296      m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 1 ) );
2297    }
2298    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 1 ) );
2299  }
2300}
2301#endif
2302/*!
2303 ****************************************************************************
2304 * \brief
2305 *   estimate bit cost for CBP, significant map and significant coefficients
2306 ****************************************************************************
2307 */
2308Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2309{
2310  estCBFBit( pcEstBitsSbac, 0, eTType );
2311
2312  estSignificantCoeffGroupMapBit( pcEstBitsSbac, 0, eTType );
2313 
2314  // encode significance map
2315  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
2316 
2317  // encode significant coefficients
2318  estSignificantCoefficientsBit( pcEstBitsSbac, 0, eTType );
2319}
2320
2321/*!
2322 ****************************************************************************
2323 * \brief
2324 *    estimate bit cost for each CBP bit
2325 ****************************************************************************
2326 */
2327Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2328{
2329  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2330
2331  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2332  {
2333    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2334    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2335  }
2336
2337  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2338 
2339  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2340  {
2341    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2342    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2343  }
2344}
2345
2346
2347/*!
2348 ****************************************************************************
2349 * \brief
2350 *    estimate SAMBAC bit cost for significant coefficient group map
2351 ****************************************************************************
2352 */
2353Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2354{
2355  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2356
2357  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2358  {
2359    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2360    {
2361      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2362    }
2363  }
2364}
2365
2366
2367/*!
2368 ****************************************************************************
2369 * \brief
2370 *    estimate SAMBAC bit cost for significant coefficient map
2371 ****************************************************************************
2372 */
2373Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2374{
2375  Int firstCtx = 0, numCtx = (eTType == TEXT_LUMA) ? 9 : 6;
2376  if (std::max(width, height) >= 16)
2377  {
2378    firstCtx = (eTType == TEXT_LUMA) ? 20 : 17;
2379    numCtx = (eTType == TEXT_LUMA) ? 7 : 4;   
2380  }
2381  else if (width == 8)
2382  {
2383    firstCtx = (eTType == TEXT_LUMA) ? 9 : 6;
2384    numCtx = 11;
2385  }
2386 
2387  if (eTType == TEXT_LUMA )
2388  {
2389    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2390    {
2391      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2392      {
2393        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2394      }
2395    }
2396  }
2397  else
2398  {
2399    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2400    {
2401      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2402      {
2403        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2404      }
2405    }
2406  }
2407  Int iBitsX = 0, iBitsY = 0;
2408  const UInt *puiCtxIdx;
2409  Int ctx;
2410#if LAST_CTX_REDUCTION
2411  Int widthCtx = eTType? 4 : width;
2412  puiCtxIdx = g_uiLastCtx + (g_aucConvertToBit[ widthCtx ]*(g_aucConvertToBit[ widthCtx ]+3));
2413#else 
2414  puiCtxIdx = g_uiLastCtx + (g_aucConvertToBit[ width ]*(g_aucConvertToBit[ width ]+3));
2415#endif
2416  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2417  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2418  {
2419    Int ctxOffset = puiCtxIdx[ ctx ];
2420#if LAST_CTX_REDUCTION
2421    if (eTType)
2422    {
2423      Int ctxOffsetC =  ctx>>g_aucConvertToBit[ width ];
2424      pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffsetC ].getEntropyBits( 0 );
2425      iBitsX += pCtxX[ ctxOffsetC].getEntropyBits( 1 );
2426    }
2427    else
2428    {
2429#endif
2430      pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2431      iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2432#if LAST_CTX_REDUCTION
2433    }
2434#endif
2435    }
2436  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2437
2438#if LAST_CTX_REDUCTION
2439  Int heightCtx = eTType? 4 : height;
2440  puiCtxIdx = g_uiLastCtx + (g_aucConvertToBit[ heightCtx ]*(g_aucConvertToBit[ heightCtx ]+3));
2441#else
2442  puiCtxIdx = g_uiLastCtx + (g_aucConvertToBit[ height ]*(g_aucConvertToBit[ height ]+3));
2443#endif
2444  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2445  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2446  {
2447    Int ctxOffset = puiCtxIdx[ ctx ];
2448#if LAST_CTX_REDUCTION
2449    if (eTType)
2450    {
2451      Int ctxOffsetC =  ctx>>g_aucConvertToBit[ height ];
2452      pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffsetC ].getEntropyBits( 0 );
2453      iBitsY += pCtxY[ctxOffsetC].getEntropyBits( 1 );
2454    }
2455    else
2456    {
2457#endif
2458      pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2459      iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2460#if LAST_CTX_REDUCTION
2461    }
2462#endif
2463    }
2464  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2465}
2466
2467/*!
2468 ****************************************************************************
2469 * \brief
2470 *    estimate bit cost of significant coefficient
2471 ****************************************************************************
2472 */
2473Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2474{
2475  if (eTType==TEXT_LUMA)
2476  {
2477    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2478    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2479
2480    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2481    {
2482      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2483      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2484    }
2485
2486    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2487    {
2488      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2489      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2490    }
2491  }
2492  else
2493  {
2494    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2495    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2496
2497    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2498    {
2499      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2500      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2501    }
2502
2503    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2504    {
2505      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2506      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2507    }
2508  }
2509}
2510
2511/**
2512 - Initialize our context information from the nominated source.
2513 .
2514 \param pSrc From where to copy context information.
2515 */
2516Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2517{ 
2518  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2519}
2520
2521Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2522{
2523  this->xCopyContextsFrom(pScr);
2524}
2525
2526#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX || (LGE_EDGE_INTRA_A0070 && LGE_EDGE_INTRA_DELTA_DC)
2527Void TEncSbac::xWriteExGolombLevel( UInt uiSymbol, ContextModel& rcSCModel  )
2528{
2529  if( uiSymbol )
2530  {
2531    m_pcBinIf->encodeBin( 1, rcSCModel );
2532    UInt uiCount = 0;
2533    Bool bNoExGo = (uiSymbol < 13);
2534
2535    while( --uiSymbol && ++uiCount < 13 )
2536    {
2537      m_pcBinIf->encodeBin( 1, rcSCModel );
2538    }
2539    if( bNoExGo )
2540    {
2541      m_pcBinIf->encodeBin( 0, rcSCModel );
2542    }
2543    else
2544    {
2545      xWriteEpExGolomb( uiSymbol, 0 );
2546    }
2547  }
2548  else
2549  {
2550    m_pcBinIf->encodeBin( 0, rcSCModel );
2551  }
2552
2553  return;
2554}
2555#endif
2556#if HHI_DMM_WEDGE_INTRA
2557Void TEncSbac::xCodeWedgeFullInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2558{
2559  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
2560  Int iBits = g_aucWedgeFullBitsListIdx[iIntraIdx];
2561
2562  UInt uiTabIdx = pcCU->getWedgeFullTabIdx( uiAbsPartIdx );
2563
2564  for ( Int i = 0; i < iBits; i++ )
2565  {
2566    m_pcBinIf->encodeBin( ( uiTabIdx >> i ) & 1, m_cDmmDataSCModel.get(0, 0, 0) );
2567  }
2568}
2569
2570Void TEncSbac::xCodeWedgeFullDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2571{
2572  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
2573  Int iBits = g_aucWedgeFullBitsListIdx[iIntraIdx];
2574
2575  UInt uiTabIdx = pcCU->getWedgeFullTabIdx( uiAbsPartIdx );
2576
2577  for ( Int i = 0; i < iBits; i++ )
2578  {
2579    m_pcBinIf->encodeBin( ( uiTabIdx >> i ) & 1, m_cDmmDataSCModel.get(0, 0, 0) );
2580  }
2581
2582  Int iDeltaDC1 = pcCU->getWedgeFullDeltaDC1( uiAbsPartIdx );
2583  Int iDeltaDC2 = pcCU->getWedgeFullDeltaDC2( uiAbsPartIdx );
2584
2585  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2586  if ( iDeltaDC1 != 0 )
2587  {
2588    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
2589    m_pcBinIf->encodeBinEP( uiSign );
2590  }
2591  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2592  if ( iDeltaDC2 != 0 )
2593  {
2594    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
2595    m_pcBinIf->encodeBinEP( uiSign );
2596  }
2597}
2598
2599Void TEncSbac::xCodeWedgePredDirInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2600{
2601  if( DMM_WEDGE_PREDDIR_DELTAEND_MAX > 0 )
2602  {
2603    Int iDeltaEnd = pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx );
2604    m_pcBinIf->encodeBin( (iDeltaEnd!=0), m_cDmmDataSCModel.get(0, 0, 2) );
2605
2606    if( iDeltaEnd != 0 )
2607    {
2608      UInt uiAbsValMinus1 = abs(iDeltaEnd)-1;
2609      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x01),      m_cDmmDataSCModel.get(0, 0, 2) );
2610      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x02) >> 1, m_cDmmDataSCModel.get(0, 0, 2) );
2611
2612      UInt uiSign = iDeltaEnd > 0 ? 0 : 1;
2613      m_pcBinIf->encodeBinEP( uiSign );
2614    }
2615  }
2616}
2617
2618Void TEncSbac::xCodeWedgePredDirDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2619{
2620  if( DMM_WEDGE_PREDDIR_DELTAEND_MAX > 0 )
2621  {
2622    Int iDeltaEnd = pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx );
2623    m_pcBinIf->encodeBin( (iDeltaEnd!=0), m_cDmmDataSCModel.get(0, 0, 2) );
2624
2625    if( iDeltaEnd != 0 )
2626    {
2627      UInt uiAbsValMinus1 = abs(iDeltaEnd)-1;
2628      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x01),      m_cDmmDataSCModel.get(0, 0, 2) );
2629      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x02) >> 1, m_cDmmDataSCModel.get(0, 0, 2) );
2630
2631      UInt uiSign = iDeltaEnd > 0 ? 0 : 1;
2632      m_pcBinIf->encodeBinEP( uiSign );
2633    }
2634  }
2635
2636  Int iDeltaDC1 = pcCU->getWedgePredDirDeltaDC1( uiAbsPartIdx );
2637  Int iDeltaDC2 = pcCU->getWedgePredDirDeltaDC2( uiAbsPartIdx );
2638
2639  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2640  if ( iDeltaDC1 != 0 )
2641  {
2642    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
2643    m_pcBinIf->encodeBinEP( uiSign );
2644  }
2645  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2646  if ( iDeltaDC2 != 0 )
2647  {
2648    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
2649    m_pcBinIf->encodeBinEP( uiSign );
2650  }
2651}
2652#endif
2653#if HHI_DMM_PRED_TEX
2654Void TEncSbac::xCodeWedgePredTexDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2655{
2656  Int iDeltaDC1 = pcCU->getWedgePredTexDeltaDC1( uiAbsPartIdx );
2657  Int iDeltaDC2 = pcCU->getWedgePredTexDeltaDC2( uiAbsPartIdx );
2658
2659  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2660  if ( iDeltaDC1 != 0 )
2661  {
2662    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
2663    m_pcBinIf->encodeBinEP( uiSign );
2664  }
2665  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2666  if ( iDeltaDC2 != 0 )
2667  {
2668    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
2669    m_pcBinIf->encodeBinEP( uiSign );
2670  }
2671}
2672
2673Void TEncSbac::xCodeContourPredTexDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
2674{
2675  Int iDeltaDC1 = pcCU->getContourPredTexDeltaDC1( uiAbsPartIdx );
2676  Int iDeltaDC2 = pcCU->getContourPredTexDeltaDC2( uiAbsPartIdx );
2677
2678  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2679  if ( iDeltaDC1 != 0 )
2680  {
2681    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
2682    m_pcBinIf->encodeBinEP( uiSign );
2683  }
2684  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cDmmDataSCModel.get(0, 0, 1) );
2685  if ( iDeltaDC2 != 0 )
2686  {
2687    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
2688    m_pcBinIf->encodeBinEP( uiSign );
2689  }
2690}
2691#endif
2692
2693#if RWTH_SDC_DLT_B0036
2694Void TEncSbac::codeSDCPredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
2695{
2696  assert( pcCU->getSlice()->getSPS()->isDepth() );
2697  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
2698  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
2699 
2700  UInt uiPredMode     = pcCU->getLumaIntraDir(uiAbsPartIdx);
2701  UInt uiCtx          = 0;
2702 
2703  UInt uiMPModeIdx    = 0;
2704 
2705  for(Int i=0; i<RWTH_SDC_NUM_PRED_MODES-1; i++)
2706  {
2707    UInt uiBit = (uiPredMode == g_auiSDCPredModes[uiMPModeIdx]) ? 1 : 0;
2708    m_pcBinIf->encodeBin( uiBit, m_cSDCPredModeSCModel.get( 0, i, uiCtx ) );
2709   
2710    // if mode is most probable mode, we are done here
2711    if ( uiBit == 1 )
2712      break;
2713   
2714    // else: get next most probable pred mode
2715    uiMPModeIdx = (uiMPModeIdx+1)%RWTH_SDC_NUM_PRED_MODES;
2716  }
2717 
2718#if HHI_DMM_WEDGE_INTRA
2719  if( uiPredMode == DMM_WEDGE_FULL_IDX )          { xCodeWedgeFullInfo          ( pcCU, uiAbsPartIdx ); }
2720  if( uiPredMode == DMM_WEDGE_PREDDIR_IDX )       { xCodeWedgePredDirInfo       ( pcCU, uiAbsPartIdx ); }
2721#endif
2722 
2723  AOF(uiPredMode == g_auiSDCPredModes[uiMPModeIdx]);
2724}
2725
2726Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2727{
2728  // get context function is here
2729  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2730  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2731  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2732 
2733}
2734
2735Void TEncSbac::codeSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
2736{
2737  assert( pcCU->getSlice()->getSPS()->isDepth() );
2738  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
2739  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
2740  assert( uiSegment < 2 );
2741 
2742  Pel segmentDCOffset = pcCU->getSDCSegmentDCOffset(uiSegment, uiAbsPartIdx);
2743 
2744  UInt uiResidual = segmentDCOffset == 0 ? 0 : 1;
2745  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
2746  UInt uiAbsIdx   = abs(segmentDCOffset);
2747  UInt uiBit = 0;
2748 
2749  UInt uiMaxResidualBits  = GetBitsPerDepthValue();
2750  assert( uiMaxResidualBits <= g_uiBitDepth );
2751 
2752  // residual flag
2753  m_pcBinIf->encodeBin( uiResidual, m_cSDCResidualFlagSCModel.get( 0, uiSegment, 0 ) ); //TODO depthmap: more sophisticated context selection
2754 
2755  if (uiResidual)
2756  {
2757    // encode sign bit of residual
2758    m_pcBinIf->encodeBin( uiSign, m_cSDCResidualSignFlagSCModel.get( 0, uiSegment, 0 ) ); //TODO depthmap: more sophisticated context selection
2759       
2760    assert(uiAbsIdx < GetNumDepthValues());
2761   
2762    // encode residual magnitude
2763    uiAbsIdx -= 1;
2764    for (Int i=0; i<uiMaxResidualBits; i++)
2765    {
2766      uiBit = (uiAbsIdx & (1<<i))>>i;
2767     
2768      m_pcBinIf->encodeBin( uiBit, m_cSDCResidualSCModel.get( 0, uiSegment, i ) ); //TODO depthmap: more sophisticated context selection
2769    }
2770   
2771  }
2772}
2773#endif
2774//! \}
Note: See TracBrowser for help on using the repository browser.