source: 3DVCSoftware/branches/HTM-11.2-dev0/source/Lib/TLibEncoder/TEncSbac.cpp

Last change on this file was 1034, checked in by tech, 10 years ago

Fix tickets #76 Unused functions

  • Property svn:eol-style set to native
File size: 79.8 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-2014, 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//! \ingroup TLibEncoder
45//! \{
46
47// ====================================================================================================================
48// Constructor / destructor / create / destroy
49// ====================================================================================================================
50
51TEncSbac::TEncSbac()
52// new structure here
53: m_pcBitIf                   ( NULL )
54, m_pcSlice                   ( NULL )
55, m_pcBinIf                   ( NULL )
56, m_uiCoeffCost               ( 0 )
57, m_numContextModels          ( 0 )
58, m_cCUSplitFlagSCModel       ( 1,             1,               NUM_SPLIT_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels )
59, m_cCUSkipFlagSCModel        ( 1,             1,               NUM_SKIP_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
60#if MTK_SINGLE_DEPTH_MODE_I0095
61, m_cCUSingleDepthFlagSCModel        ( 1,             1,               NUM_SINGLEDEPTH_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
62, m_cSingleDepthValueSCModel         ( 1,             1,               NUM_SINGLE_DEPTH_VALUE_DATA_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
63#endif
64, m_cCUMergeFlagExtSCModel    ( 1,             1,               NUM_MERGE_FLAG_EXT_CTX        , m_contextModels + m_numContextModels, m_numContextModels)
65, m_cCUMergeIdxExtSCModel     ( 1,             1,               NUM_MERGE_IDX_EXT_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
66#if H_3D_ARP
67, m_cCUPUARPWSCModel          ( 1,             1,               NUM_ARPW_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
68#endif
69#if H_3D_IC
70, m_cCUICFlagSCModel          ( 1,             1,               NUM_IC_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
71#endif
72, m_cCUPartSizeSCModel        ( 1,             1,               NUM_PART_SIZE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
73, m_cCUPredModeSCModel        ( 1,             1,               NUM_PRED_MODE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
74, m_cCUIntraPredSCModel       ( 1,             1,               NUM_ADI_CTX                   , m_contextModels + m_numContextModels, m_numContextModels)
75, m_cCUChromaPredSCModel      ( 1,             1,               NUM_CHROMA_PRED_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
76, m_cCUDeltaQpSCModel         ( 1,             1,               NUM_DELTA_QP_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
77, m_cCUInterDirSCModel        ( 1,             1,               NUM_INTER_DIR_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
78, m_cCURefPicSCModel          ( 1,             1,               NUM_REF_NO_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
79, m_cCUMvdSCModel             ( 1,             1,               NUM_MV_RES_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
80, m_cCUQtCbfSCModel           ( 1,             2,               NUM_QT_CBF_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
81, m_cCUTransSubdivFlagSCModel ( 1,             1,               NUM_TRANS_SUBDIV_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cCUQtRootCbfSCModel       ( 1,             1,               NUM_QT_ROOT_CBF_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cCUSigCoeffGroupSCModel   ( 1,             2,               NUM_SIG_CG_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
84, m_cCUSigSCModel             ( 1,             1,               NUM_SIG_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
85, m_cCuCtxLastX               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
86, m_cCuCtxLastY               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cCUOneSCModel             ( 1,             1,               NUM_ONE_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cCUAbsSCModel             ( 1,             1,               NUM_ABS_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
89, m_cMVPIdxSCModel            ( 1,             1,               NUM_MVP_IDX_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
90, m_cSaoMergeSCModel          ( 1,             1,               NUM_SAO_MERGE_FLAG_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
91, m_cSaoTypeIdxSCModel        ( 1,             1,               NUM_SAO_TYPE_IDX_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
92, m_cTransformSkipSCModel     ( 1,             2,               NUM_TRANSFORMSKIP_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
93, m_CUTransquantBypassFlagSCModel( 1,          1,               NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX, m_contextModels + m_numContextModels, m_numContextModels)
94#if H_3D_DIM
95, m_cDepthIntraModeSCModel    ( 1,             1,               NUM_DEPTH_INTRA_MODE_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
96, m_cDdcFlagSCModel           ( 1,             1,               NUM_DDC_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
97, m_cDdcDataSCModel           ( 1,             1,               NUM_DDC_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
98, m_cAngleFlagSCModel         ( 1,             1,               NUM_ANGLE_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
99#if H_3D_DIM_SDC
100, m_cSDCResidualFlagSCModel   ( 1,             1,               SDC_NUM_RESIDUAL_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
101, m_cSDCResidualSCModel       ( 1,             1,               SDC_NUM_RESIDUAL_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
102#endif
103#endif
104#if H_3D_DIM_SDC
105, m_cSDCFlagSCModel                  ( 1,             1,  NUM_SDC_FLAG_CTX                 , m_contextModels + m_numContextModels, m_numContextModels)
106#endif
107#if H_3D_DBBP
108, m_cDBBPFlagSCModel             ( 1,             1,                 DBBP_NUM_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
109#endif
110{
111  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
112}
113
114TEncSbac::~TEncSbac()
115{
116}
117
118// ====================================================================================================================
119// Public member functions
120// ====================================================================================================================
121
122Void TEncSbac::resetEntropy           ()
123{
124  Int  iQp              = m_pcSlice->getSliceQp();
125  SliceType eSliceType  = m_pcSlice->getSliceType();
126 
127  Int  encCABACTableIdx = m_pcSlice->getPPS()->getEncCABACTableIdx();
128  if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag())
129  {
130    eSliceType = (SliceType) encCABACTableIdx;
131  }
132
133  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
134 
135  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
136#if MTK_SINGLE_DEPTH_MODE_I0095
137  m_cCUSingleDepthFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SINGLEDEPTH_FLAG );
138  m_cSingleDepthValueSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_SINGLE_DEPTH_VALUE_DATA );
139#endif
140  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
141  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
142#if H_3D_ARP
143  m_cCUPUARPWSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_ARPW );
144#endif
145#if H_3D_IC
146  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
147#endif
148  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
149  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
150  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
151  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
152  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
153  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
154  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
155  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
156  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
157  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
158  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
159  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
160  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
161  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
162  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
163  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
164  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
165  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
166  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
167  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
168  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
169  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
170
171#if H_3D_DIM
172  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
173  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
174  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
175  m_cAngleFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_ANGLE_FLAG );
176#if H_3D_DIM_SDC
177  m_cSDCResidualFlagSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
178  m_cSDCResidualSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
179#endif
180#endif
181#if H_3D_DIM_SDC
182  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
183#endif
184#if H_3D_DBBP
185  m_cDBBPFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_DBBP_FLAG );
186#endif
187  // new structure
188  m_uiLastQp = iQp;
189 
190  m_pcBinIf->start();
191 
192  return;
193}
194
195/** The function does the following:
196 * If current slice type is P/B then it determines the distance of initialisation type 1 and 2 from the current CABAC states and
197 * stores the index of the closest table.  This index is used for the next P/B slice when cabac_init_present_flag is true.
198 */
199Void TEncSbac::determineCabacInitIdx()
200{
201  Int  qp              = m_pcSlice->getSliceQp();
202
203  if (!m_pcSlice->isIntra())
204  {
205    SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE};
206
207    UInt bestCost             = MAX_UINT;
208    SliceType bestSliceType   = aSliceTypeChoices[0];
209    for (UInt idx=0; idx<2; idx++)
210    {
211      UInt curCost          = 0;
212      SliceType curSliceType  = aSliceTypeChoices[idx];
213
214      curCost  = m_cCUSplitFlagSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_SPLIT_FLAG );
215      curCost += m_cCUSkipFlagSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SKIP_FLAG );
216#if MTK_SINGLE_DEPTH_MODE_I0095
217      curCost += m_cCUSingleDepthFlagSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SINGLEDEPTH_FLAG );
218      curCost += m_cSingleDepthValueSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_SINGLE_DEPTH_VALUE_DATA );
219#endif
220      curCost += m_cCUMergeFlagExtSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT);
221      curCost += m_cCUMergeIdxExtSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_MERGE_IDX_EXT);
222#if H_3D_ARP
223      curCost += m_cCUPUARPWSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_ARPW );
224#endif
225#if H_3D_IC
226      curCost += m_cCUICFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_IC_FLAG );
227#endif
228#if H_3D_DIM_SDC
229      curCost += m_cSDCFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_SDC_FLAG );
230#endif
231#if H_3D_DBBP
232      curCost += m_cDBBPFlagSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_DBBP_FLAG );
233#endif
234      curCost += m_cCUPartSizeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PART_SIZE );
235      curCost += m_cCUPredModeSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_PRED_MODE );
236      curCost += m_cCUIntraPredSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
237      curCost += m_cCUChromaPredSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
238      curCost += m_cCUInterDirSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_INTER_DIR );
239      curCost += m_cCUMvdSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_MVD );
240      curCost += m_cCURefPicSCModel.calcCost          ( curSliceType, qp, (UChar*)INIT_REF_PIC );
241      curCost += m_cCUDeltaQpSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_DQP );
242      curCost += m_cCUQtCbfSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_QT_CBF );
243      curCost += m_cCUQtRootCbfSCModel.calcCost       ( curSliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
244      curCost += m_cCUSigCoeffGroupSCModel.calcCost   ( curSliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
245      curCost += m_cCUSigSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_SIG_FLAG );
246      curCost += m_cCuCtxLastX.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
247      curCost += m_cCuCtxLastY.calcCost               ( curSliceType, qp, (UChar*)INIT_LAST );
248      curCost += m_cCUOneSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ONE_FLAG );
249      curCost += m_cCUAbsSCModel.calcCost             ( curSliceType, qp, (UChar*)INIT_ABS_FLAG );
250      curCost += m_cMVPIdxSCModel.calcCost            ( curSliceType, qp, (UChar*)INIT_MVP_IDX );
251      curCost += m_cCUTransSubdivFlagSCModel.calcCost ( curSliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
252      curCost += m_cSaoMergeSCModel.calcCost      ( curSliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
253      curCost += m_cSaoTypeIdxSCModel.calcCost        ( curSliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
254      curCost += m_cTransformSkipSCModel.calcCost     ( curSliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
255      curCost += m_CUTransquantBypassFlagSCModel.calcCost( curSliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
256#if H_3D_DIM
257#if SEPARATE_FLAG_I0085
258    if( m_pcSlice->getVpsDepthModesFlag() || m_pcSlice->getIVPFlag() )
259#else
260    if( m_pcSlice->getVpsDepthModesFlag() )
261#endif
262    {
263      curCost += m_cDepthIntraModeSCModel.calcCost    ( curSliceType, qp, (UChar*)INIT_DEPTH_INTRA_MODE );
264      curCost += m_cDdcFlagSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_DDC_FLAG );
265      curCost += m_cDdcDataSCModel.calcCost           ( curSliceType, qp, (UChar*)INIT_DDC_DATA );
266      curCost += m_cAngleFlagSCModel.calcCost         ( curSliceType, qp, (UChar*)INIT_ANGLE_FLAG ); 
267    }
268#endif
269      if (curCost < bestCost)
270      {
271        bestSliceType = curSliceType;
272        bestCost      = curCost;
273      }
274    }
275    m_pcSlice->getPPS()->setEncCABACTableIdx( bestSliceType );
276  }
277  else
278  {
279    m_pcSlice->getPPS()->setEncCABACTableIdx( I_SLICE );
280  } 
281}
282
283
284/** The function does the followng: Write out terminate bit. Flush CABAC. Intialize CABAC states. Start CABAC.
285 */
286Void TEncSbac::updateContextTables( SliceType eSliceType, Int iQp, Bool bExecuteFinish )
287{
288  m_pcBinIf->encodeBinTrm(1);
289  if (bExecuteFinish) m_pcBinIf->finish();
290  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
291 
292  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
293#if MTK_SINGLE_DEPTH_MODE_I0095
294  m_cCUSingleDepthFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SINGLEDEPTH_FLAG );
295  m_cSingleDepthValueSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_SINGLE_DEPTH_VALUE_DATA );
296#endif 
297  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT);
298  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT);
299#if H_3D_ARP
300  m_cCUPUARPWSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_ARPW );
301#endif
302#if H_3D_IC
303  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
304#endif
305  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
306  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
307  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
308  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
309  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
310  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
311  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
312  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
313  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
314  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
315  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
316  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
317  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
318  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
319  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
320  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
321  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
322  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
323  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
324  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
325  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
326  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
327#if H_3D_DIM
328  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
329  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
330  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
331  m_cAngleFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_ANGLE_FLAG );
332#if H_3D_DIM_SDC
333  m_cSDCResidualFlagSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
334  m_cSDCResidualSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
335#endif
336#endif
337#if H_3D_DIM_SDC
338  m_cSDCFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
339#endif
340#if H_3D_DBBP
341  m_cDBBPFlagSCModel.initBuffer              ( eSliceType, iQp, (UChar*)INIT_DBBP_FLAG );
342#endif
343  m_pcBinIf->start();
344}
345
346Void TEncSbac::codeVPS( TComVPS* pcVPS )
347{
348  assert (0);
349  return;
350}
351
352#if H_3D
353Void TEncSbac::codeSPS( TComSPS* pcSPS, Int viewIndex, Bool depthFlag )
354#else
355Void TEncSbac::codeSPS( TComSPS* pcSPS )
356#endif
357{
358  assert (0);
359  return;
360}
361
362Void TEncSbac::codePPS( TComPPS* pcPPS )
363{
364  assert (0);
365  return;
366}
367
368Void TEncSbac::codeSliceHeader( TComSlice* pcSlice )
369{
370  assert (0);
371  return;
372}
373
374Void TEncSbac::codeTilesWPPEntryPoint( TComSlice* pSlice )
375{
376  assert (0);
377  return;
378}
379
380Void TEncSbac::codeTerminatingBit( UInt uilsLast )
381{
382  m_pcBinIf->encodeBinTrm( uilsLast );
383}
384
385Void TEncSbac::codeSliceFinish()
386{
387  m_pcBinIf->finish();
388}
389
390Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset )
391{
392  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] );
393 
394  if( 0 == uiSymbol)
395  {
396    return;
397  }
398 
399  while( uiSymbol-- )
400  {
401    m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] );
402  }
403 
404  return;
405}
406
407Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
408{
409  if (uiMaxSymbol == 0)
410  {
411    return;
412  }
413 
414  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] );
415 
416  if ( uiSymbol == 0 )
417  {
418    return;
419  }
420 
421  Bool bCodeLast = ( uiMaxSymbol > uiSymbol );
422 
423  while( --uiSymbol )
424  {
425    m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] );
426  }
427  if( bCodeLast )
428  {
429    m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] );
430  }
431 
432  return;
433}
434
435Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount )
436{
437  UInt bins = 0;
438  Int numBins = 0;
439 
440  while( uiSymbol >= (UInt)(1<<uiCount) )
441  {
442    bins = 2 * bins + 1;
443    numBins++;
444    uiSymbol -= 1 << uiCount;
445    uiCount  ++;
446  }
447  bins = 2 * bins + 0;
448  numBins++;
449 
450  bins = (bins << uiCount) | uiSymbol;
451  numBins += uiCount;
452 
453  assert( numBins <= 32 );
454  m_pcBinIf->encodeBinsEP( bins, numBins );
455}
456#if MTK_SINGLE_DEPTH_MODE_I0095
457Void TEncSbac::codeSingleDepthMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
458{
459  UInt uiSymbol = pcCU->getSingleDepthFlag(uiAbsPartIdx ) ? 1 : 0;
460  m_pcBinIf->encodeBin( uiSymbol, m_cCUSingleDepthFlagSCModel.get( 0, 0, 0 ) );
461  if(uiSymbol)
462  {
463    UInt uiUnaryIdx = (UInt) pcCU->getSingleDepthValue(uiAbsPartIdx);
464    UInt uiNumCand = MTK_SINGLE_DEPTH_MODE_CANDIDATE_LIST_SIZE;
465    if ( uiNumCand > 1 )
466    {
467      for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
468      {
469        const UInt uiSymbol2 = ui == uiUnaryIdx ? 0 : 1;
470        if ( ui==0 )
471        {
472          m_pcBinIf->encodeBin( uiSymbol2, m_cSingleDepthValueSCModel.get( 0, 0, 0 ) );
473        }
474        else
475        {
476          m_pcBinIf->encodeBinEP( uiSymbol2 );
477        }
478        if( uiSymbol2 == 0 )
479        {
480          break;
481        }
482      }
483    }
484  }
485}
486#endif
487/** Coding of coeff_abs_level_minus3
488 * \param uiSymbol value of coeff_abs_level_minus3
489 * \param ruiGoRiceParam reference to Rice parameter
490 * \returns Void
491 */
492Void TEncSbac::xWriteCoefRemainExGolomb ( UInt symbol, UInt &rParam )
493{
494  Int codeNumber  = (Int)symbol;
495  UInt length;
496  if (codeNumber < (COEF_REMAIN_BIN_REDUCTION << rParam))
497  {
498    length = codeNumber>>rParam;
499    m_pcBinIf->encodeBinsEP( (1<<(length+1))-2 , length+1);
500    m_pcBinIf->encodeBinsEP((codeNumber%(1<<rParam)),rParam);
501  }
502  else
503  {
504    length = rParam;
505    codeNumber  = codeNumber - ( COEF_REMAIN_BIN_REDUCTION << rParam);
506    while (codeNumber >= (1<<length))
507    {
508      codeNumber -=  (1<<(length++));   
509    }
510    m_pcBinIf->encodeBinsEP((1<<(COEF_REMAIN_BIN_REDUCTION+length+1-rParam))-2,COEF_REMAIN_BIN_REDUCTION+length+1-rParam);
511    m_pcBinIf->encodeBinsEP(codeNumber,length);
512  }
513}
514
515#if H_3D_DIM
516Void TEncSbac::xWriteExGolombLevel( UInt uiSymbol, ContextModel& rcSCModel  )
517{
518  if( uiSymbol )
519  {
520    m_pcBinIf->encodeBin( 1, rcSCModel );
521    UInt uiCount = 0;
522    Bool bNoExGo = ( uiSymbol < 3 );
523
524    while( --uiSymbol && ++uiCount < 3 )
525    {
526      m_pcBinIf->encodeBin( 1, rcSCModel );
527    }
528    if( bNoExGo )
529    {
530      m_pcBinIf->encodeBin( 0, rcSCModel );
531    }
532    else
533    {
534      xWriteEpExGolomb( uiSymbol, 0 );
535    }
536  }
537  else
538  {
539    m_pcBinIf->encodeBin( 0, rcSCModel );
540  }
541
542  return;
543}
544
545Void TEncSbac::xCodeDimDeltaDC( Pel valDeltaDC, UInt uiNumSeg )
546{
547  xWriteExGolombLevel( UInt( abs( valDeltaDC ) - ( uiNumSeg > 1 ? 0 : 1 ) ), m_cDdcDataSCModel.get(0, 0, 0) );
548  if( valDeltaDC != 0 )
549  {
550    UInt uiSign = valDeltaDC > 0 ? 0 : 1;
551    m_pcBinIf->encodeBinEP( uiSign );
552  }
553}
554
555#if H_3D_DIM_DMM
556Void TEncSbac::xCodeDmm1WedgeIdx( UInt uiTabIdx, Int iNumBit )
557{
558  for ( Int i = 0; i < iNumBit; i++ )
559  {
560    m_pcBinIf->encodeBinEP( ( uiTabIdx >> i ) & 1 );
561  }
562}
563
564#endif
565
566#if !FIX_TICKET_76
567#if H_3D_DIM_SDC
568Void TEncSbac::xCodeSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
569{
570  assert( pcCU->getSlice()->getIsDepth() );
571  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
572  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
573  assert( uiSegment < 2 );
574 
575  Pel segmentDCOffset = pcCU->getSDCSegmentDCOffset(uiSegment, uiAbsPartIdx);
576 
577  UInt uiResidual = segmentDCOffset == 0 ? 0 : 1;
578  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
579  UInt uiAbsIdx   = abs(segmentDCOffset);
580
581#if H_3D_DIM_DLT
582  UInt uiMaxResidualBits = pcCU->getSlice()->getPPS()->getDLT()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
583#else
584  UInt uiMaxResidualBits = g_bitDepthY;
585#endif
586  assert( uiMaxResidualBits <= g_bitDepthY );
587 
588  // residual flag
589  m_pcBinIf->encodeBin( uiResidual, m_cSDCResidualFlagSCModel.get( 0, 0, 0 ) );
590#if H_MV_ENC_DEC_TRAC
591  DTRACE_CU("sdc_residual_flag[i]", uiResidual)
592#endif 
593 
594  if (uiResidual)
595  {
596    // encode sign bit of residual
597    m_pcBinIf->encodeBinEP( uiSign );
598#if H_MV_ENC_DEC_TRAC
599    DTRACE_CU("sdc_residual_sign_flag[i]", uiSign)
600#endif
601#if H_3D_DIM_DLT   
602    UInt uiNumDepthValues = pcCU->getSlice()->getPPS()->getDLT()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
603#else
604    UInt uiNumDepthValues = ((1 << g_bitDepthY)-1);
605#endif
606
607    assert(uiAbsIdx <= uiNumDepthValues);
608   
609    // encode residual magnitude
610    uiAbsIdx -= 1;
611
612#if H_MV_ENC_DEC_TRAC
613    DTRACE_CU("sdc_residual_abs_minus1[i]", uiAbsIdx)
614#endif
615
616    // prefix part
617    if ( uiAbsIdx == 0 )
618    {
619      m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
620    }
621    else
622    {
623      UInt l = uiAbsIdx;
624      UInt k = 0;
625      UInt uiPrefixThreshold = ((uiNumDepthValues * 3) >> 2);
626      while ( l > 0 && k < uiPrefixThreshold )
627      {
628        m_pcBinIf->encodeBin( 1, m_cSDCResidualSCModel.get(0, 0, 0) );
629        l--;
630        k++;
631      }
632      if ( uiAbsIdx < uiPrefixThreshold )
633      {
634        m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
635      }     
636      else
637      { // suffix part
638        uiAbsIdx -= uiPrefixThreshold;
639        UInt uiSuffixLength = numBitsForValue(uiNumDepthValues - uiPrefixThreshold);
640        UInt uiBitInfo = 0;
641        for ( Int i = 0; i < uiSuffixLength; i++)
642        {
643          uiBitInfo = ( uiAbsIdx & ( 1 << i ) ) >> i;
644          m_pcBinIf->encodeBinEP( uiBitInfo);
645        }
646      }
647    }
648   
649  }
650}
651#endif
652#endif
653#endif
654
655
656// SBAC RD
657Void  TEncSbac::load ( TEncSbac* pSrc)
658{
659  this->xCopyFrom(pSrc);
660}
661
662Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
663{
664  m_pcBinIf->copyState( pSrc->m_pcBinIf );
665 
666  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
667}
668#if H_3D_DIM
669Void TEncSbac::loadIntraDepthMode( TEncSbac* pSrc)
670{
671  m_pcBinIf->copyState( pSrc->m_pcBinIf );
672
673  this->m_cDepthIntraModeSCModel .copyFrom( &pSrc->m_cDepthIntraModeSCModel );
674  this->m_cDdcFlagSCModel        .copyFrom( &pSrc->m_cDdcFlagSCModel );
675  this->m_cAngleFlagSCModel      .copyFrom( &pSrc->m_cAngleFlagSCModel );
676}
677#endif
678
679Void  TEncSbac::store( TEncSbac* pDest)
680{
681  pDest->xCopyFrom( this );
682}
683
684
685Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
686{
687  m_pcBinIf->copyState( pSrc->m_pcBinIf );
688 
689  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
690  this->m_uiLastQp    = pSrc->m_uiLastQp;
691 
692  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
693}
694
695Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
696{
697  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
698  Int iNum = AMVP_MAX_NUM_CANDS;
699
700  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
701#if H_MV_ENC_DEC_TRAC
702#if ENC_DEC_TRACE
703  if ( eRefList == REF_PIC_LIST_0 )
704  {
705    DTRACE_PU("mvp_l0_flag", iSymbol)
706  }
707  else
708  {
709    DTRACE_PU("mvp_l1_flag", iSymbol)
710  }
711#endif
712#endif
713}
714
715Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
716{
717  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
718#if H_3D_QTLPC
719#if MTK_I0099_VPS_EX2
720  TComVPS *vps           = pcCU->getPic()->getSlice(0)->getVPS();
721  Bool    bLimQtPredFlag = vps->getLimQtPredFlag(pcCU->getPic()->getSlice(0)->getLayerId());
722#else
723  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
724#endif
725  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
726  Bool bDepthMapDetect   = (pcTexture != NULL);
727  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
728 
729  Bool rapPic     = (pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
730
731  Bool depthDependent = false;
732  UInt uiTexturePart = eSize;
733#if MTK_I0099_VPS_EX2
734  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && bLimQtPredFlag )
735#else
736  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC() )
737#endif
738  {
739    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
740    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
741    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
742    if(pcTextureCU->getDepth(uiCUIdx) == uiDepth )
743    {
744      depthDependent = true;
745      uiTexturePart = pcTextureCU->getPartitionSize( uiCUIdx );
746    }
747    if (pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize( uiCUIdx ) == SIZE_2Nx2N)
748    {
749      assert( eSize == SIZE_2Nx2N );
750      return;
751    }
752  }
753#endif
754  if ( pcCU->isIntra( uiAbsPartIdx ) )
755  {
756    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
757    {
758      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
759#if H_MV_ENC_DEC_TRAC
760      DTRACE_CU("part_mode", eSize == SIZE_2Nx2N? 1 : 0)
761#endif       
762    }
763    return;
764  }
765 
766#if H_MV_ENC_DEC_TRAC         
767  DTRACE_CU("part_mode", eSize )
768#endif       
769#if H_3D_QTLPC
770    if (depthDependent==false || uiTexturePart == SIZE_NxN|| uiTexturePart == SIZE_2Nx2N)
771    {
772#endif
773  switch(eSize)
774  {
775    case SIZE_2Nx2N:
776    {
777      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
778      break;
779    }
780    case SIZE_2NxN:
781    case SIZE_2NxnU:
782    case SIZE_2NxnD:
783    {
784      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
785      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
786      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
787      {
788        if (eSize == SIZE_2NxN)
789        {
790          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
791        }
792        else
793        {
794          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
795          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
796        }
797      }
798      break;
799    }
800    case SIZE_Nx2N:
801    case SIZE_nLx2N:
802    case SIZE_nRx2N:
803    {
804      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
805      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
806      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
807      {
808        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
809      }
810      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
811      {
812        if (eSize == SIZE_Nx2N)
813        {
814          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
815        }
816        else
817        {
818          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
819          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
820        }
821      }
822      break;
823    }
824    case SIZE_NxN:
825    {
826      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
827      {
828        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
829        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
830        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
831      }
832      break;
833    }
834    default:
835    {
836      assert(0);
837    }
838  }
839#if H_3D_QTLPC
840    }
841    else if(uiTexturePart == SIZE_2NxN || uiTexturePart == SIZE_2NxnU || uiTexturePart == SIZE_2NxnD)
842    {
843      //assert(eSize!=SIZE_NxN);
844      //assert(eSize!=SIZE_Nx2N);
845      //assert(eSize==SIZE_2Nx2N || eSize==SIZE_2NxN || eSize==SIZE_2NxnU || eSize==SIZE_2NxnD);
846      switch(eSize)
847      {
848      case SIZE_2Nx2N:
849        {
850          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
851          break;
852        }
853      case SIZE_2NxN:
854        {
855          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
856          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
857          {     
858            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
859          }
860          break;
861        }
862      case SIZE_2NxnU:
863      case SIZE_2NxnD:
864        {
865          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
866          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
867          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
868          break;
869        }
870      default:
871        {
872          assert(0);
873        }
874      }
875    }
876    else if(uiTexturePart == SIZE_Nx2N|| uiTexturePart==SIZE_nLx2N || uiTexturePart==SIZE_nRx2N)
877    {
878      //assert(eSize!=SIZE_NxN);
879      //assert(eSize!=SIZE_2NxN);
880      //assert(eSize==SIZE_2Nx2N ||eSize==SIZE_Nx2N || eSize==SIZE_nLx2N || eSize==SIZE_nRx2N);
881      switch(eSize)
882      {
883      case SIZE_2Nx2N:
884        {
885          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
886          break;
887        }
888      case SIZE_Nx2N:
889        {
890          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
891          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
892          {     
893            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
894          }
895          break;
896        }
897      case SIZE_nLx2N:
898      case SIZE_nRx2N:
899        {
900          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
901          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
902          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
903          break;
904        }
905      default:
906        {
907          assert(0);
908        }
909      }
910    }
911    else
912    {
913      printf("uiTexturePart=%d",uiTexturePart);
914      assert(0);
915    }
916#endif
917}
918
919/** code prediction mode
920 * \param pcCU
921 * \param uiAbsPartIdx 
922 * \returns Void
923 */
924Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
925{
926#if H_3D_DIM_SDC
927  if ( pcCU->getSlice()->isIntra() )
928  {
929    assert( pcCU->isIntra(uiAbsPartIdx) );
930    return;
931  }
932#endif
933 
934  // get context function is here
935  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
936  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
937#if H_MV_ENC_DEC_TRAC
938  DTRACE_CU("pred_mode_flag", iPredMode == MODE_INTER ? 0 : 1); 
939#endif
940}
941
942Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
943{
944  UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx);
945  m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
946#if H_MV_ENC_DEC_TRAC
947  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
948#endif
949}
950
951/** code skip flag
952 * \param pcCU
953 * \param uiAbsPartIdx
954 * \returns Void
955 */
956Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
957{
958  // get context function is here
959  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
960  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
961  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
962#if !H_MV_ENC_DEC_TRAC
963  DTRACE_CABAC_VL( g_nSymbolCounter++ );
964  DTRACE_CABAC_T( "\tSkipFlag" );
965  DTRACE_CABAC_T( "\tuiCtxSkip: ");
966  DTRACE_CABAC_V( uiCtxSkip );
967  DTRACE_CABAC_T( "\tuiSymbol: ");
968  DTRACE_CABAC_V( uiSymbol );
969  DTRACE_CABAC_T( "\n");
970#else
971  DTRACE_CU("cu_skip_flag", uiSymbol); 
972#endif
973}
974
975/** code merge flag
976 * \param pcCU
977 * \param uiAbsPartIdx
978 * \returns Void
979 */
980Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
981{
982  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
983  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
984
985#if H_MV_ENC_DEC_TRAC
986  DTRACE_PU("merge_flag", uiSymbol); 
987#else
988  DTRACE_CABAC_VL( g_nSymbolCounter++ );
989  DTRACE_CABAC_T( "\tMergeFlag: " );
990  DTRACE_CABAC_V( uiSymbol );
991  DTRACE_CABAC_T( "\tAddress: " );
992  DTRACE_CABAC_V( pcCU->getAddr() );
993  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
994  DTRACE_CABAC_V( uiAbsPartIdx );
995  DTRACE_CABAC_T( "\n" );
996#endif
997}
998
999/** code merge index
1000 * \param pcCU
1001 * \param uiAbsPartIdx
1002 * \returns Void
1003 */
1004Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
1005{
1006  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
1007  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
1008  if ( uiNumCand > 1 )
1009  {
1010    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
1011    {
1012      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
1013      if ( ui==0 )
1014      {
1015        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
1016      }
1017      else
1018      {
1019        m_pcBinIf->encodeBinEP( uiSymbol );
1020      }
1021      if( uiSymbol == 0 )
1022      {
1023        break;
1024      }
1025    }
1026#if H_MV_ENC_DEC_TRAC
1027    DTRACE_PU("merge_idx", uiUnaryIdx); 
1028#endif
1029  }
1030#if !H_MV_ENC_DEC_TRAC
1031  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1032  DTRACE_CABAC_T( "\tparseMergeIndex()" );
1033  DTRACE_CABAC_T( "\tuiMRGIdx= " );
1034  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
1035  DTRACE_CABAC_T( "\n" );
1036#endif
1037}
1038
1039#if H_3D_ARP
1040Void TEncSbac::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
1041{
1042  Int  iW = (Int)pcCU->getARPW( uiAbsPartIdx );
1043  Int  iMaxW = pcCU->getSlice()->getARPStepNum() - 1;
1044  assert( iMaxW > 0);
1045
1046  Int nOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
1047  Int nBinNum = iW + ( iW != iMaxW );
1048  m_pcBinIf->encodeBin( iW ? 1 : 0 , m_cCUPUARPWSCModel.get( 0, 0, 0 + nOffset ) );
1049  if( nBinNum > 1 )
1050  {
1051     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPWSCModel.get( 0, 0, 2 ) );
1052  }
1053#if H_MV_ENC_DEC_TRAC
1054  DTRACE_CU("iv_res_pred_weight_idx", iW); 
1055#endif
1056}
1057#endif
1058
1059#if H_3D_IC
1060/** code Illumination Compensation flag
1061 * \param pcCU
1062 * \param uiAbsPartIdx
1063 * \returns Void
1064 */
1065Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1066{
1067  // get context function is here
1068  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
1069  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) );
1070#if !H_MV_ENC_DEC_TRAC
1071  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1072  DTRACE_CABAC_T( "\tICFlag" );
1073  DTRACE_CABAC_T( "\tuiSymbol: ");
1074  DTRACE_CABAC_V( uiSymbol );
1075  DTRACE_CABAC_T( "\n");
1076#else
1077  DTRACE_CU("ic_flag", uiSymbol ); 
1078#endif
1079}
1080#endif
1081
1082
1083Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1084{
1085  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1086    return;
1087 
1088  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
1089  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
1090 
1091  assert( uiCtx < 3 );
1092#if H_3D_QTLPC
1093  Bool bCodeSplitFlag    = true;
1094
1095#if MTK_I0099_VPS_EX2
1096  TComVPS *vps           = pcCU->getPic()->getSlice(0)->getVPS();
1097  Bool    bLimQtPredFlag = vps->getLimQtPredFlag(pcCU->getPic()->getSlice(0)->getLayerId());
1098#else
1099  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
1100#endif
1101  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
1102  Bool bDepthMapDetect   = (pcTexture != NULL);
1103  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
1104
1105  Bool rapPic     = (pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_W_RADL || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP || pcCU->getSlice()->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA);
1106
1107#if MTK_I0099_VPS_EX2
1108  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && bLimQtPredFlag  )
1109#else
1110  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC())
1111#endif
1112  {
1113    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
1114    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
1115    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
1116    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
1117  }
1118
1119  if(!bCodeSplitFlag)
1120  {
1121    assert(uiCurrSplitFlag == 0);
1122    return;
1123  }
1124#endif
1125  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
1126#if !H_MV_ENC_DEC_TRAC
1127  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1128  DTRACE_CABAC_T( "\tSplitFlag\n" )
1129#else
1130  DTRACE_CU("split_cu_flag", uiCurrSplitFlag ); 
1131#endif
1132  return;
1133}
1134
1135Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1136{
1137  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
1138#if !H_MV_ENC_DEC_TRAC
1139  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1140  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1141  DTRACE_CABAC_T( "\tsymbol=" )
1142  DTRACE_CABAC_V( uiSymbol )
1143  DTRACE_CABAC_T( "\tctx=" )
1144  DTRACE_CABAC_V( uiCtx )
1145  DTRACE_CABAC_T( "\n" )
1146#endif
1147}
1148
1149Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
1150{
1151  UInt dir[4],j;
1152  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
1153  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
1154  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1155  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
1156  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1157  for (j=0;j<partNum;j++)
1158  {
1159    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
1160#if H_3D_DIM
1161#if SEPARATE_FLAG_I0085
1162    if( pcCU->getSlice()->getVpsDepthModesFlag() ||  pcCU->getSlice()->getIVPFlag() )
1163#else
1164    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1165#endif
1166    {
1167      codeIntraDepth( pcCU, absPartIdx+partOffset*j );
1168    }
1169    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1170    {
1171#endif
1172      predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
1173      for(UInt i = 0; i < predNum[j]; i++)
1174      {
1175        if(dir[j] == preds[j][i])
1176        {
1177          predIdx[j] = i;
1178        }
1179      }
1180      m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1181#if H_MV_ENC_DEC_TRAC
1182      DTRACE_CU("prev_intra_luma_pred_flag", (predIdx[j] != -1)? 1 : 0); 
1183#endif
1184#if H_3D_DIM
1185    }
1186#endif
1187
1188  } 
1189  for (j=0;j<partNum;j++)
1190  {
1191#if H_3D_DIM
1192    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1193    {
1194#endif
1195    if(predIdx[j] != -1)
1196    {
1197      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
1198      if (predIdx[j])
1199      {
1200        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
1201      }
1202#if H_MV_ENC_DEC_TRAC
1203      DTRACE_CU("mpm_idx", predIdx[j] ); 
1204#endif
1205    }
1206    else
1207    {
1208      if (preds[j][0] > preds[j][1])
1209      { 
1210        std::swap(preds[j][0], preds[j][1]); 
1211      }
1212      if (preds[j][0] > preds[j][2])
1213      {
1214        std::swap(preds[j][0], preds[j][2]);
1215      }
1216      if (preds[j][1] > preds[j][2])
1217      {
1218        std::swap(preds[j][1], preds[j][2]);
1219      }
1220      for(Int i = (predNum[j] - 1); i >= 0; i--)
1221      {
1222        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
1223      }
1224      m_pcBinIf->encodeBinsEP( dir[j], 5 );
1225#if H_MV_ENC_DEC_TRAC
1226      DTRACE_CU("rem_intra_luma_pred_mode", dir[j] ); 
1227#endif
1228    }
1229#if H_3D_DIM
1230    }
1231#endif
1232  }
1233  return;
1234}
1235
1236Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1237{
1238  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1239
1240  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1241  {
1242    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1243#if H_MV_ENC_DEC_TRAC
1244    DTRACE_CU("intra_chroma_pred_mode", 0 ); 
1245#endif
1246  }
1247  else
1248  { 
1249    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1250    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1251
1252    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1253    {
1254      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1255      {
1256        uiIntraDirChroma = i;
1257        break;
1258      }
1259    }
1260    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1261
1262    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1263#if H_MV_ENC_DEC_TRAC
1264    DTRACE_CU("intra_chroma_pred_mode", uiIntraDirChroma ); 
1265#endif
1266
1267  }
1268  return;
1269}
1270
1271#if H_3D_DIM
1272Void TEncSbac::codeIntraDepth( TComDataCU* pcCU, UInt absPartIdx )
1273{
1274  codeIntraDepthMode( pcCU, absPartIdx );
1275
1276  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1277  UInt dimType = getDimType( dir );
1278
1279  switch( dimType )
1280  {
1281#if H_3D_DIM_DMM
1282  case( DMM1_IDX ):
1283    {
1284      xCodeDmm1WedgeIdx( pcCU->getDmmWedgeTabIdx( dimType, absPartIdx ), g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1285    } break;
1286  case( DMM4_IDX ): break;
1287#endif
1288  default: break;
1289  }
1290}
1291
1292Void TEncSbac::codeIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx )
1293{
1294  UInt dir = pcCU->getLumaIntraDir( absPartIdx );
1295
1296  if( ( pcCU->getSlice()->getSPS()->getMaxCUWidth() >> pcCU->getDepth( absPartIdx ) ) < 64 ) //DMM and HEVC intra modes are both allowed
1297  {
1298    m_pcBinIf->encodeBin( isDimMode( dir ) ? 0 : 1, m_cAngleFlagSCModel.get( 0, 0, 0 ) );
1299  }
1300  if( isDimMode( dir ) )
1301  {
1302    UInt uiCodeIdx = 0;
1303
1304    switch( getDimType( dir ) )
1305    {
1306    case DMM1_IDX: uiCodeIdx = 0; break;
1307    case DMM4_IDX: uiCodeIdx = 1; break;
1308    default:                      break;
1309    }
1310    //mode coding
1311#if SEPARATE_FLAG_I0085
1312    if( pcCU->getSlice()->getVpsDepthModesFlag() && pcCU->getSlice()->getIVPFlag())
1313    {
1314      m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1315    }
1316#else
1317    m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1318#endif
1319  }
1320}
1321#endif
1322
1323
1324Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1325{
1326  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1327  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1328  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1329  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1330  {
1331    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1332  }
1333  if (uiInterDir < 2)
1334  {
1335    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1336  }
1337#if H_MV_ENC_DEC_TRAC
1338  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1339#endif
1340
1341  return;
1342}
1343
1344Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1345{
1346  {
1347    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1348    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1349    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1350   
1351    if( iRefFrame > 0 )
1352    {
1353      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1354      pCtx++;
1355      iRefFrame--;
1356      for( UInt ui = 0; ui < uiRefNum; ++ui )
1357      {
1358        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1359        if( ui == 0 )
1360        {
1361          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1362        }
1363        else
1364        {
1365          m_pcBinIf->encodeBinEP( uiSymbol );
1366        }
1367        if( uiSymbol == 0 )
1368        {
1369          break;
1370        }
1371      }
1372    }
1373#if H_MV_ENC_DEC_TRAC
1374#if ENC_DEC_TRACE
1375    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1376    if ( eRefList == REF_PIC_LIST_0 )
1377    {
1378      DTRACE_PU("ref_idx_l0", iRefFrame)
1379    }
1380    else
1381    {
1382      DTRACE_PU("ref_idx_l1", iRefFrame)
1383    }
1384#endif
1385#endif
1386  }
1387  return;
1388}
1389
1390Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1391{
1392  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1393  {
1394    return;
1395  }
1396
1397  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1398  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1399  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1400  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1401
1402  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1403  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1404
1405  const Bool bHorAbsGr0 = iHor != 0;
1406  const Bool bVerAbsGr0 = iVer != 0;
1407  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1408  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1409  pCtx++;
1410
1411  if( bHorAbsGr0 )
1412  {
1413    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1414  }
1415
1416  if( bVerAbsGr0 )
1417  {
1418    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1419  }
1420
1421  if( bHorAbsGr0 )
1422  {
1423    if( uiHorAbs > 1 )
1424    {
1425      xWriteEpExGolomb( uiHorAbs-2, 1 );
1426    }
1427
1428    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1429  }
1430
1431  if( bVerAbsGr0 )
1432  {
1433    if( uiVerAbs > 1 )
1434    {
1435      xWriteEpExGolomb( uiVerAbs-2, 1 );
1436    }
1437
1438    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1439  }
1440 
1441  return;
1442}
1443
1444Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1445{
1446  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1447 
1448  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1449  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1450
1451  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1452  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1453  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1454  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1455  {
1456    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1457  }
1458
1459  if ( uiAbsDQp > 0)
1460  {
1461    UInt uiSign = (iDQp > 0 ? 0 : 1);
1462    m_pcBinIf->encodeBinEP(uiSign);
1463  }
1464
1465  return;
1466}
1467
1468Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1469{
1470  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1471  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1472  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1473#if !H_MV_ENC_DEC_TRAC
1474  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1475  DTRACE_CABAC_T( "\tparseQtCbf()" )
1476  DTRACE_CABAC_T( "\tsymbol=" )
1477  DTRACE_CABAC_V( uiCbf )
1478  DTRACE_CABAC_T( "\tctx=" )
1479  DTRACE_CABAC_V( uiCtx )
1480  DTRACE_CABAC_T( "\tetype=" )
1481  DTRACE_CABAC_V( eType )
1482  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1483  DTRACE_CABAC_V( uiAbsPartIdx )
1484  DTRACE_CABAC_T( "\n" )
1485#endif
1486}
1487
1488void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1489{
1490  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1491  {
1492    return;
1493  }
1494  if(width != 4 || height != 4)
1495  {
1496    return;
1497  }
1498
1499  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1500  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1501#if !H_MV_ENC_DEC_TRAC
1502  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1503  DTRACE_CABAC_T("\tparseTransformSkip()");
1504  DTRACE_CABAC_T( "\tsymbol=" )
1505  DTRACE_CABAC_V( useTransformSkip )
1506  DTRACE_CABAC_T( "\tAddr=" )
1507  DTRACE_CABAC_V( pcCU->getAddr() )
1508  DTRACE_CABAC_T( "\tetype=" )
1509  DTRACE_CABAC_V( eTType )
1510  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1511  DTRACE_CABAC_V( uiAbsPartIdx )
1512  DTRACE_CABAC_T( "\n" )
1513#endif
1514}
1515
1516/** Code I_PCM information.
1517 * \param pcCU pointer to CU
1518 * \param uiAbsPartIdx CU index
1519 * \returns Void
1520 */
1521Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1522{
1523  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1524
1525  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1526
1527  m_pcBinIf->encodeBinTrm (uiIPCM);
1528#if H_MV_ENC_DEC_TRAC         
1529  DTRACE_CU("pcm_flag", uiIPCM)
1530#endif
1531  if (writePCMSampleFlag)
1532  {
1533    m_pcBinIf->encodePCMAlignBits();
1534
1535    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1536    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1537    UInt uiChromaOffset = uiLumaOffset>>2;
1538    Pel* piPCMSample;
1539    UInt uiWidth;
1540    UInt uiHeight;
1541    UInt uiSampleBits;
1542    UInt uiX, uiY;
1543
1544    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1545    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1546    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1547    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1548
1549    for(uiY = 0; uiY < uiHeight; uiY++)
1550    {
1551      for(uiX = 0; uiX < uiWidth; uiX++)
1552      {
1553        UInt uiSample = piPCMSample[uiX];
1554
1555        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1556      }
1557      piPCMSample += uiWidth;
1558    }
1559
1560    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1561    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1562    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1563    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1564
1565    for(uiY = 0; uiY < uiHeight; uiY++)
1566    {
1567      for(uiX = 0; uiX < uiWidth; uiX++)
1568      {
1569        UInt uiSample = piPCMSample[uiX];
1570
1571        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1572      }
1573      piPCMSample += uiWidth;
1574    }
1575
1576    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1577    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1578    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1579    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1580
1581    for(uiY = 0; uiY < uiHeight; uiY++)
1582    {
1583      for(uiX = 0; uiX < uiWidth; uiX++)
1584      {
1585        UInt uiSample = piPCMSample[uiX];
1586
1587        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1588      }
1589      piPCMSample += uiWidth;
1590    }
1591    m_pcBinIf->resetBac();
1592  }
1593}
1594
1595Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1596{
1597  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1598  UInt uiCtx = 0;
1599  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1600#if !H_MV_ENC_DEC_TRAC
1601  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1602  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1603  DTRACE_CABAC_T( "\tsymbol=" )
1604  DTRACE_CABAC_V( uiCbf )
1605  DTRACE_CABAC_T( "\tctx=" )
1606  DTRACE_CABAC_V( uiCtx )
1607  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1608  DTRACE_CABAC_V( uiAbsPartIdx )
1609  DTRACE_CABAC_T( "\n" )
1610#else
1611  DTRACE_CU( "rqt_root_cbf", uiCbf )
1612#endif
1613}
1614
1615Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1616{
1617  // this function is only used to estimate the bits when cbf is 0
1618  // and will never be called when writing the bistream. do not need to write log
1619  UInt uiCbf = 0;
1620  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1621  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1622}
1623
1624Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1625{
1626  // this function is only used to estimate the bits when cbf is 0
1627  // and will never be called when writing the bistream. do not need to write log
1628  UInt uiCbf = 0;
1629  UInt uiCtx = 0;
1630  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1631}
1632
1633/** Encode (X,Y) position of the last significant coefficient
1634 * \param uiPosX X component of last coefficient
1635 * \param uiPosY Y component of last coefficient
1636 * \param width  Block width
1637 * \param height Block height
1638 * \param eTType plane type / luminance or chrominance
1639 * \param uiScanIdx scan type (zig-zag, hor, ver)
1640 * This method encodes the X and Y component within a block of the last significant coefficient.
1641 */
1642Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1643{ 
1644  // swap
1645  if( uiScanIdx == SCAN_VER )
1646  {
1647    swap( uiPosX, uiPosY );
1648  }
1649
1650  UInt uiCtxLast;
1651  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1652  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1653  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1654  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1655
1656
1657  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1658  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1659  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1660  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1661  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1662  // posX
1663  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1664  {
1665      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1666  }
1667  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1668  {
1669      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1670  }
1671
1672  // posY
1673  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1674  {
1675    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1676  }
1677  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1678  {
1679    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1680  }
1681  if ( uiGroupIdxX > 3 )
1682  {     
1683    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1684    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1685    for (Int i = uiCount - 1 ; i >= 0; i-- )
1686    {
1687      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1688    }
1689  }
1690  if ( uiGroupIdxY > 3 )
1691  {     
1692    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1693    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1694    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1695    {
1696      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1697    }
1698  }
1699}
1700
1701Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1702{
1703#if !H_MV_ENC_DEC_TRAC
1704  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1705  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1706  DTRACE_CABAC_V( eTType )
1707  DTRACE_CABAC_T( "\twidth=" )
1708  DTRACE_CABAC_V( uiWidth )
1709  DTRACE_CABAC_T( "\theight=" )
1710  DTRACE_CABAC_V( uiHeight )
1711  DTRACE_CABAC_T( "\tdepth=" )
1712  DTRACE_CABAC_V( uiDepth )
1713  DTRACE_CABAC_T( "\tabspartidx=" )
1714  DTRACE_CABAC_V( uiAbsPartIdx )
1715  DTRACE_CABAC_T( "\ttoCU-X=" )
1716  DTRACE_CABAC_V( pcCU->getCUPelX() )
1717  DTRACE_CABAC_T( "\ttoCU-Y=" )
1718  DTRACE_CABAC_V( pcCU->getCUPelY() )
1719  DTRACE_CABAC_T( "\tCU-addr=" )
1720  DTRACE_CABAC_V(  pcCU->getAddr() )
1721  DTRACE_CABAC_T( "\tinCU-X=" )
1722  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1723  DTRACE_CABAC_T( "\tinCU-Y=" )
1724  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1725  DTRACE_CABAC_T( "\tpredmode=" )
1726  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1727  DTRACE_CABAC_T( "\n" )
1728#endif
1729  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1730  {
1731    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1732    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1733  }
1734 
1735  UInt uiNumSig = 0;
1736 
1737  // compute number of significant coefficients
1738  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1739 
1740  if ( uiNumSig == 0 )
1741    return;
1742  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1743  {
1744    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1745  }
1746  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1747 
1748  //----- encode significance map -----
1749  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1750  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1751  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1752 
1753  Bool beValid;
1754  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1755  {
1756    beValid = false;
1757  }
1758  else 
1759  {
1760    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1761  }
1762
1763  // Find position of last coefficient
1764  Int scanPosLast = -1;
1765  Int posLast;
1766
1767  const UInt * scanCG;
1768  {
1769    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1770    if( uiLog2BlockSize == 3 )
1771    {
1772      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1773    }
1774    else if( uiLog2BlockSize == 5 )
1775    {
1776      scanCG = g_sigLastScanCG32x32;
1777    }
1778  }
1779  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1780  static const UInt uiShift = MLS_CG_SIZE >> 1;
1781  const UInt uiNumBlkSide = uiWidth >> uiShift;
1782
1783    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1784
1785    do
1786    {
1787      posLast = scan[ ++scanPosLast ];
1788
1789      // get L1 sig map
1790      UInt uiPosY    = posLast >> uiLog2BlockSize;
1791      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1792      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1793      if( pcCoef[ posLast ] )
1794      {
1795        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1796      }
1797
1798      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1799    }
1800    while ( uiNumSig > 0 );
1801
1802  // Code position of last coefficient
1803  Int posLastY = posLast >> uiLog2BlockSize;
1804  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1805  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1806 
1807  //===== code significance flag =====
1808  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1809  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1810
1811
1812  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1813  UInt c1 = 1;
1814  UInt uiGoRiceParam           = 0;
1815  Int  iScanPosSig             = scanPosLast;
1816
1817  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1818  {
1819    Int numNonZero = 0;
1820    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1821    uiGoRiceParam    = 0;
1822    Int absCoeff[16];
1823    UInt coeffSigns = 0;
1824
1825    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1826
1827    if( iScanPosSig == scanPosLast )
1828    {
1829      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1830      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1831      numNonZero    = 1;
1832      lastNZPosInCG  = iScanPosSig;
1833      firstNZPosInCG = iScanPosSig;
1834      iScanPosSig--;
1835    }
1836
1837      // encode significant_coeffgroup_flag
1838      Int iCGBlkPos = scanCG[ iSubSet ];
1839      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1840      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1841      if( iSubSet == iLastScanSet || iSubSet == 0)
1842      {
1843        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1844      }
1845      else
1846      {
1847          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1848          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1849          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1850      }
1851     
1852      // encode significant_coeff_flag
1853      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1854      {
1855        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1856        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1857        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1858        {
1859          uiBlkPos  = scan[ iScanPosSig ]; 
1860          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1861          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1862          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1863          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1864          {
1865            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1866            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1867          }
1868          if( uiSig )
1869          {
1870            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1871            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1872            numNonZero++;
1873            if( lastNZPosInCG == -1 )
1874            {
1875              lastNZPosInCG = iScanPosSig;
1876            }
1877            firstNZPosInCG = iScanPosSig;
1878          }
1879        }
1880      }
1881      else
1882      {
1883        iScanPosSig = iSubPos - 1;
1884      }
1885
1886    if( numNonZero > 0 )
1887    {
1888      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1889      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1890     
1891      if( c1 == 0 )
1892      {
1893        uiCtxSet++;
1894      }
1895      c1 = 1;
1896      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1897     
1898      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1899      Int firstC2FlagIdx = -1;
1900      for( Int idx = 0; idx < numC1Flag; idx++ )
1901      {
1902        UInt uiSymbol = absCoeff[ idx ] > 1;
1903        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1904        if( uiSymbol )
1905        {
1906          c1 = 0;
1907
1908          if (firstC2FlagIdx == -1)
1909          {
1910            firstC2FlagIdx = idx;
1911          }
1912        }
1913        else if( (c1 < 3) && (c1 > 0) )
1914        {
1915          c1++;
1916        }
1917      }
1918     
1919      if (c1 == 0)
1920      {
1921
1922        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1923        if ( firstC2FlagIdx != -1)
1924        {
1925          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1926          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1927        }
1928      }
1929     
1930      if( beValid && signHidden )
1931      {
1932        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1933      }
1934      else
1935      {
1936        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1937      }
1938     
1939      Int iFirstCoeff2 = 1;   
1940      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1941      {
1942        for ( Int idx = 0; idx < numNonZero; idx++ )
1943        {
1944          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1945
1946          if( absCoeff[ idx ] >= baseLevel)
1947          {
1948            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1949            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1950            {
1951               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1952            }
1953          }
1954          if(absCoeff[ idx ] >= 2) 
1955          {
1956            iFirstCoeff2 = 0;
1957          }
1958        }       
1959      }
1960    }
1961  }
1962
1963  return;
1964}
1965
1966/** code SAO offset sign
1967 * \param code sign value
1968 */
1969Void TEncSbac::codeSAOSign( UInt code )
1970{
1971  m_pcBinIf->encodeBinEP( code );
1972}
1973
1974Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1975{
1976  if (maxSymbol == 0)
1977  {
1978    return;
1979  }
1980
1981  Int i;
1982  Bool bCodeLast = ( maxSymbol > code );
1983
1984  if ( code == 0 )
1985  {
1986    m_pcBinIf->encodeBinEP( 0 );
1987  }
1988  else
1989  {
1990    m_pcBinIf->encodeBinEP( 1 );
1991    for ( i=0; i<code-1; i++ )
1992    {
1993      m_pcBinIf->encodeBinEP( 1 );
1994    }
1995    if( bCodeLast )
1996    {
1997      m_pcBinIf->encodeBinEP( 0 );
1998    }
1999  }
2000}
2001
2002
2003/** Code SAO EO class or BO band position
2004 * \param uiLength
2005 * \param uiCode
2006 */
2007Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
2008{
2009   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
2010}
2011/** Code SAO merge flags
2012 * \param uiCode
2013 * \param uiCompIdx
2014 */
2015Void TEncSbac::codeSaoMerge       ( UInt uiCode )
2016{
2017  if (uiCode == 0)
2018  {
2019    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2020  }
2021  else
2022  {
2023    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2024  }
2025}
2026/** Code SAO type index
2027 * \param uiCode
2028 */
2029Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
2030{
2031  if (uiCode == 0)
2032  {
2033    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2034  }
2035  else
2036  {
2037    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2038    m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 );
2039  }
2040}
2041/*!
2042 ****************************************************************************
2043 * \brief
2044 *   estimate bit cost for CBP, significant map and significant coefficients
2045 ****************************************************************************
2046 */
2047Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2048{
2049  estCBFBit( pcEstBitsSbac );
2050
2051  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
2052 
2053  // encode significance map
2054  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
2055 
2056  // encode significant coefficients
2057  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
2058}
2059
2060/*!
2061 ****************************************************************************
2062 * \brief
2063 *    estimate bit cost for each CBP bit
2064 ****************************************************************************
2065 */
2066Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
2067{
2068  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2069
2070  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2071  {
2072    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2073    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2074  }
2075
2076  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2077 
2078  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2079  {
2080    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2081    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2082  }
2083}
2084
2085
2086/*!
2087 ****************************************************************************
2088 * \brief
2089 *    estimate SAMBAC bit cost for significant coefficient group map
2090 ****************************************************************************
2091 */
2092Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2093{
2094  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2095
2096  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2097  {
2098    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2099    {
2100      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2101    }
2102  }
2103}
2104
2105
2106/*!
2107 ****************************************************************************
2108 * \brief
2109 *    estimate SAMBAC bit cost for significant coefficient map
2110 ****************************************************************************
2111 */
2112Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2113{
2114  Int firstCtx = 1, numCtx = 8;
2115  if (max(width, height) >= 16)
2116  {
2117    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2118    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2119  }
2120  else if (width == 8)
2121  {
2122    firstCtx = 9;
2123    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2124  }
2125 
2126  if (eTType == TEXT_LUMA )
2127  {
2128    for( UInt bin = 0; bin < 2; bin++ )
2129    {
2130      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2131    }
2132
2133    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2134    {
2135      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2136      {
2137        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2138      }
2139    }
2140  }
2141  else
2142  {
2143    for( UInt bin = 0; bin < 2; bin++ )
2144    {
2145      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2146    }
2147    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2148    {
2149      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2150      {
2151        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2152      }
2153    }
2154  }
2155  Int iBitsX = 0, iBitsY = 0;
2156  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2157
2158  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2159  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2160  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2161  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2162
2163  Int ctx;
2164  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2165  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2166  {
2167    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2168    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2169    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2170  }
2171  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2172  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2173  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2174  {
2175    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2176    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2177    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2178  }
2179  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2180}
2181
2182/*!
2183 ****************************************************************************
2184 * \brief
2185 *    estimate bit cost of significant coefficient
2186 ****************************************************************************
2187 */
2188Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2189{
2190  if (eTType==TEXT_LUMA)
2191  {
2192    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2193    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2194
2195    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2196    {
2197      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2198      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2199    }
2200
2201    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2202    {
2203      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2204      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2205    }
2206  }
2207  else
2208  {
2209    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2210    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2211
2212    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2213    {
2214      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2215      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2216    }
2217
2218    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2219    {
2220      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2221      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2222    }
2223  }
2224}
2225
2226/**
2227 - Initialize our context information from the nominated source.
2228 .
2229 \param pSrc From where to copy context information.
2230 */
2231Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2232{ 
2233  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2234}
2235
2236Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2237{
2238  this->xCopyContextsFrom(pScr);
2239}
2240
2241Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled)
2242{
2243  UInt uiSymbol;
2244  if(!sliceEnabled)
2245  {
2246    assert(ctbParam.modeIdc == SAO_MODE_OFF);
2247    return;
2248  }
2249
2250  //type
2251  if(compIdx == SAO_Y || compIdx == SAO_Cb)
2252  {
2253    //sao_type_idx_luma or sao_type_idx_chroma
2254    if(ctbParam.modeIdc == SAO_MODE_OFF)
2255    {
2256      uiSymbol =0;
2257    }
2258    else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO
2259    {
2260      uiSymbol = 1;
2261    }
2262    else
2263    {
2264      assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO
2265      uiSymbol = 2;
2266    }
2267    codeSaoTypeIdx(uiSymbol); 
2268  }
2269
2270  if(ctbParam.modeIdc == SAO_MODE_NEW)
2271  {
2272    Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; 
2273    Int offset[4];
2274    Int k=0;
2275    for(Int i=0; i< numClasses; i++)
2276    {
2277      if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN)
2278      {
2279        continue;
2280      }
2281      Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?(  (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES   ):i;
2282      offset[k] = ctbParam.offset[classIdx];
2283      k++;
2284    }
2285
2286    for(Int i=0; i< 4; i++)
2287    {
2288      codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]),  g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs
2289    }
2290
2291
2292    if(ctbParam.typeIdc == SAO_TYPE_BO)
2293    {
2294      for(Int i=0; i< 4; i++)
2295      {
2296        if(offset[i] != 0)
2297        {
2298          codeSAOSign((offset[i]< 0)?1:0);
2299        }
2300      }
2301
2302      codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position
2303    }
2304    else //EO
2305    {
2306      if(compIdx == SAO_Y || compIdx == SAO_Cb)
2307      {
2308        assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0);
2309        codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma
2310      }
2311    }
2312
2313  }
2314}
2315
2316
2317Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam
2318                              , Bool* sliceEnabled
2319                              , Bool leftMergeAvail
2320                              , Bool aboveMergeAvail
2321                              , Bool onlyEstMergeInfo // = false
2322                              )
2323{
2324
2325  Bool isLeftMerge = false;
2326  Bool isAboveMerge= false;
2327
2328  if(leftMergeAvail)
2329  {
2330    isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT));
2331    codeSaoMerge( isLeftMerge?1:0  ); //sao_merge_left_flag
2332  }
2333
2334  if( aboveMergeAvail && !isLeftMerge)
2335  {
2336    isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); 
2337    codeSaoMerge( isAboveMerge?1:0  ); //sao_merge_left_flag
2338  }
2339
2340  if(onlyEstMergeInfo)
2341  {
2342    return; //only for RDO
2343  }
2344
2345  if(!isLeftMerge && !isAboveMerge) //not merge mode
2346  {
2347    for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
2348    {
2349      codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]);
2350    }
2351  }
2352}
2353
2354#if H_3D_INTER_SDC
2355Void TEncSbac::codeDeltaDC( TComDataCU* pcCU, UInt absPartIdx )
2356{
2357  if( !( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2358  {
2359    assert( 0 );
2360  }
2361
2362#if HS_DMM_SIGNALLING_I0120
2363  UInt uiNumSegments = isDimMode( pcCU->getLumaIntraDir( absPartIdx ) ) ? 2 : 1;
2364  UInt dimDeltaDC = 1;
2365#else
2366  UInt uiNumSegments = 0;
2367  UInt dimDeltaDC = 0;
2368#endif
2369
2370#if HS_DMM_SIGNALLING_I0120
2371  if( pcCU->isIntra( absPartIdx ) && pcCU->getSDCFlag( absPartIdx ))
2372  {
2373#else
2374  if( pcCU->isIntra( absPartIdx ) )
2375  {
2376    UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2377    uiNumSegments = isDimMode( dir ) ? 2 : 1;
2378
2379    if( pcCU->getSDCFlag( absPartIdx ) )
2380    {
2381#endif
2382      if( uiNumSegments == 1 )
2383      {
2384        dimDeltaDC = pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) ? 1 : 0;
2385      }
2386      else
2387      {
2388        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) || pcCU->getSDCSegmentDCOffset( 1, absPartIdx ) ) ? 1 : 0;
2389      }
2390#if !HS_DMM_SIGNALLING_I0120
2391    }
2392    else
2393    {
2394      dimDeltaDC = isDimDeltaDC( dir );
2395    }
2396#endif
2397    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2398  }
2399#if !HS_DMM_SIGNALLING_I0120
2400  else //all-zero inter SDC is not allowed
2401  {
2402    uiNumSegments = 1;
2403    dimDeltaDC = 1;
2404  }
2405#endif
2406
2407  if( dimDeltaDC )
2408  {
2409    for( UInt segment = 0; segment < uiNumSegments; segment++ )
2410    {
2411      Pel deltaDC = 0;
2412
2413      if( pcCU->isIntra( absPartIdx ) )
2414      {
2415        UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2416        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset( segment, absPartIdx ) : pcCU->getDimDeltaDC( getDimType( dir ), segment, absPartIdx );
2417      }
2418      else
2419      {
2420        deltaDC = pcCU->getSDCSegmentDCOffset( segment, absPartIdx );
2421      }
2422
2423      xCodeDimDeltaDC( deltaDC, uiNumSegments );
2424    }
2425  }
2426}
2427
2428Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2429{
2430  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2431  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2432
2433  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2434}
2435
2436#endif
2437
2438#if H_3D_DBBP
2439Void TEncSbac::codeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2440{
2441#if SEC_DBBP_EXPLICIT_SIG_I0077
2442  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
2443  AOF( ePartSize == SIZE_2NxN || ePartSize == SIZE_Nx2N );
2444#endif
2445  AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
2446  AOF( !pcCU->getSlice()->getIsDepth() );
2447 
2448  UInt uiSymbol = pcCU->getDBBPFlag( uiAbsPartIdx ) ? 1 : 0;
2449  m_pcBinIf->encodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) );
2450}
2451#endif
2452
2453//! \}
Note: See TracBrowser for help on using the repository browser.