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

Last change on this file since 1030 was 1030, checked in by tech, 11 years ago

Merged 11.2-dev2-Samsung@1026.

  • Property svn:eol-style set to native
File size: 79.7 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 H_3D_DIM_SDC
567Void TEncSbac::xCodeSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
568{
569  assert( pcCU->getSlice()->getIsDepth() );
570  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
571  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
572  assert( uiSegment < 2 );
573 
574  Pel segmentDCOffset = pcCU->getSDCSegmentDCOffset(uiSegment, uiAbsPartIdx);
575 
576  UInt uiResidual = segmentDCOffset == 0 ? 0 : 1;
577  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
578  UInt uiAbsIdx   = abs(segmentDCOffset);
579
580#if H_3D_DIM_DLT
581  UInt uiMaxResidualBits = pcCU->getSlice()->getPPS()->getDLT()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
582#else
583  UInt uiMaxResidualBits = g_bitDepthY;
584#endif
585  assert( uiMaxResidualBits <= g_bitDepthY );
586 
587  // residual flag
588  m_pcBinIf->encodeBin( uiResidual, m_cSDCResidualFlagSCModel.get( 0, 0, 0 ) );
589#if H_MV_ENC_DEC_TRAC
590  DTRACE_CU("sdc_residual_flag[i]", uiResidual)
591#endif 
592 
593  if (uiResidual)
594  {
595    // encode sign bit of residual
596    m_pcBinIf->encodeBinEP( uiSign );
597#if H_MV_ENC_DEC_TRAC
598    DTRACE_CU("sdc_residual_sign_flag[i]", uiSign)
599#endif
600#if H_3D_DIM_DLT   
601    UInt uiNumDepthValues = pcCU->getSlice()->getPPS()->getDLT()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
602#else
603    UInt uiNumDepthValues = ((1 << g_bitDepthY)-1);
604#endif
605
606    assert(uiAbsIdx <= uiNumDepthValues);
607   
608    // encode residual magnitude
609    uiAbsIdx -= 1;
610
611#if H_MV_ENC_DEC_TRAC
612    DTRACE_CU("sdc_residual_abs_minus1[i]", uiAbsIdx)
613#endif
614
615    // prefix part
616    if ( uiAbsIdx == 0 )
617    {
618      m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
619    }
620    else
621    {
622      UInt l = uiAbsIdx;
623      UInt k = 0;
624      UInt uiPrefixThreshold = ((uiNumDepthValues * 3) >> 2);
625      while ( l > 0 && k < uiPrefixThreshold )
626      {
627        m_pcBinIf->encodeBin( 1, m_cSDCResidualSCModel.get(0, 0, 0) );
628        l--;
629        k++;
630      }
631      if ( uiAbsIdx < uiPrefixThreshold )
632      {
633        m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
634      }     
635      else
636      { // suffix part
637        uiAbsIdx -= uiPrefixThreshold;
638        UInt uiSuffixLength = numBitsForValue(uiNumDepthValues - uiPrefixThreshold);
639        UInt uiBitInfo = 0;
640        for ( Int i = 0; i < uiSuffixLength; i++)
641        {
642          uiBitInfo = ( uiAbsIdx & ( 1 << i ) ) >> i;
643          m_pcBinIf->encodeBinEP( uiBitInfo);
644        }
645      }
646    }
647   
648  }
649}
650#endif
651#endif
652
653// SBAC RD
654Void  TEncSbac::load ( TEncSbac* pSrc)
655{
656  this->xCopyFrom(pSrc);
657}
658
659Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
660{
661  m_pcBinIf->copyState( pSrc->m_pcBinIf );
662 
663  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
664}
665#if H_3D_DIM
666Void TEncSbac::loadIntraDepthMode( TEncSbac* pSrc)
667{
668  m_pcBinIf->copyState( pSrc->m_pcBinIf );
669
670  this->m_cDepthIntraModeSCModel .copyFrom( &pSrc->m_cDepthIntraModeSCModel );
671  this->m_cDdcFlagSCModel        .copyFrom( &pSrc->m_cDdcFlagSCModel );
672  this->m_cAngleFlagSCModel      .copyFrom( &pSrc->m_cAngleFlagSCModel );
673}
674#endif
675
676Void  TEncSbac::store( TEncSbac* pDest)
677{
678  pDest->xCopyFrom( this );
679}
680
681
682Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
683{
684  m_pcBinIf->copyState( pSrc->m_pcBinIf );
685 
686  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
687  this->m_uiLastQp    = pSrc->m_uiLastQp;
688 
689  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
690}
691
692Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
693{
694  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
695  Int iNum = AMVP_MAX_NUM_CANDS;
696
697  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
698#if H_MV_ENC_DEC_TRAC
699#if ENC_DEC_TRACE
700  if ( eRefList == REF_PIC_LIST_0 )
701  {
702    DTRACE_PU("mvp_l0_flag", iSymbol)
703  }
704  else
705  {
706    DTRACE_PU("mvp_l1_flag", iSymbol)
707  }
708#endif
709#endif
710}
711
712Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
713{
714  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
715#if H_3D_QTLPC
716#if MTK_I0099_VPS_EX2
717  TComVPS *vps           = pcCU->getPic()->getSlice(0)->getVPS();
718  Bool    bLimQtPredFlag = vps->getLimQtPredFlag(pcCU->getPic()->getSlice(0)->getLayerId());
719#else
720  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
721#endif
722  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
723  Bool bDepthMapDetect   = (pcTexture != NULL);
724  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
725 
726  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);
727
728  Bool depthDependent = false;
729  UInt uiTexturePart = eSize;
730#if MTK_I0099_VPS_EX2
731  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && bLimQtPredFlag )
732#else
733  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC() )
734#endif
735  {
736    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
737    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
738    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
739    if(pcTextureCU->getDepth(uiCUIdx) == uiDepth )
740    {
741      depthDependent = true;
742      uiTexturePart = pcTextureCU->getPartitionSize( uiCUIdx );
743    }
744    if (pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize( uiCUIdx ) == SIZE_2Nx2N)
745    {
746      assert( eSize == SIZE_2Nx2N );
747      return;
748    }
749  }
750#endif
751  if ( pcCU->isIntra( uiAbsPartIdx ) )
752  {
753    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
754    {
755      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
756#if H_MV_ENC_DEC_TRAC
757      DTRACE_CU("part_mode", eSize == SIZE_2Nx2N? 1 : 0)
758#endif       
759    }
760    return;
761  }
762 
763#if H_MV_ENC_DEC_TRAC         
764  DTRACE_CU("part_mode", eSize )
765#endif       
766#if H_3D_QTLPC
767    if (depthDependent==false || uiTexturePart == SIZE_NxN|| uiTexturePart == SIZE_2Nx2N)
768    {
769#endif
770  switch(eSize)
771  {
772    case SIZE_2Nx2N:
773    {
774      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
775      break;
776    }
777    case SIZE_2NxN:
778    case SIZE_2NxnU:
779    case SIZE_2NxnD:
780    {
781      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
782      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
783      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
784      {
785        if (eSize == SIZE_2NxN)
786        {
787          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
788        }
789        else
790        {
791          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
792          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
793        }
794      }
795      break;
796    }
797    case SIZE_Nx2N:
798    case SIZE_nLx2N:
799    case SIZE_nRx2N:
800    {
801      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
802      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
803      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
804      {
805        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
806      }
807      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
808      {
809        if (eSize == SIZE_Nx2N)
810        {
811          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
812        }
813        else
814        {
815          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
816          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
817        }
818      }
819      break;
820    }
821    case SIZE_NxN:
822    {
823      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
824      {
825        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
826        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
827        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
828      }
829      break;
830    }
831    default:
832    {
833      assert(0);
834    }
835  }
836#if H_3D_QTLPC
837    }
838    else if(uiTexturePart == SIZE_2NxN || uiTexturePart == SIZE_2NxnU || uiTexturePart == SIZE_2NxnD)
839    {
840      //assert(eSize!=SIZE_NxN);
841      //assert(eSize!=SIZE_Nx2N);
842      //assert(eSize==SIZE_2Nx2N || eSize==SIZE_2NxN || eSize==SIZE_2NxnU || eSize==SIZE_2NxnD);
843      switch(eSize)
844      {
845      case SIZE_2Nx2N:
846        {
847          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
848          break;
849        }
850      case SIZE_2NxN:
851        {
852          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
853          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
854          {     
855            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
856          }
857          break;
858        }
859      case SIZE_2NxnU:
860      case SIZE_2NxnD:
861        {
862          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
863          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
864          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
865          break;
866        }
867      default:
868        {
869          assert(0);
870        }
871      }
872    }
873    else if(uiTexturePart == SIZE_Nx2N|| uiTexturePart==SIZE_nLx2N || uiTexturePart==SIZE_nRx2N)
874    {
875      //assert(eSize!=SIZE_NxN);
876      //assert(eSize!=SIZE_2NxN);
877      //assert(eSize==SIZE_2Nx2N ||eSize==SIZE_Nx2N || eSize==SIZE_nLx2N || eSize==SIZE_nRx2N);
878      switch(eSize)
879      {
880      case SIZE_2Nx2N:
881        {
882          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
883          break;
884        }
885      case SIZE_Nx2N:
886        {
887          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
888          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
889          {     
890            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
891          }
892          break;
893        }
894      case SIZE_nLx2N:
895      case SIZE_nRx2N:
896        {
897          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
898          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
899          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
900          break;
901        }
902      default:
903        {
904          assert(0);
905        }
906      }
907    }
908    else
909    {
910      printf("uiTexturePart=%d",uiTexturePart);
911      assert(0);
912    }
913#endif
914}
915
916/** code prediction mode
917 * \param pcCU
918 * \param uiAbsPartIdx 
919 * \returns Void
920 */
921Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
922{
923#if H_3D_DIM_SDC
924  if ( pcCU->getSlice()->isIntra() )
925  {
926    assert( pcCU->isIntra(uiAbsPartIdx) );
927    return;
928  }
929#endif
930 
931  // get context function is here
932  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
933  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
934#if H_MV_ENC_DEC_TRAC
935  DTRACE_CU("pred_mode_flag", iPredMode == MODE_INTER ? 0 : 1); 
936#endif
937}
938
939Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
940{
941  UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx);
942  m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
943#if H_MV_ENC_DEC_TRAC
944  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
945#endif
946}
947
948/** code skip flag
949 * \param pcCU
950 * \param uiAbsPartIdx
951 * \returns Void
952 */
953Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
954{
955  // get context function is here
956  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
957  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
958  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
959#if !H_MV_ENC_DEC_TRAC
960  DTRACE_CABAC_VL( g_nSymbolCounter++ );
961  DTRACE_CABAC_T( "\tSkipFlag" );
962  DTRACE_CABAC_T( "\tuiCtxSkip: ");
963  DTRACE_CABAC_V( uiCtxSkip );
964  DTRACE_CABAC_T( "\tuiSymbol: ");
965  DTRACE_CABAC_V( uiSymbol );
966  DTRACE_CABAC_T( "\n");
967#else
968  DTRACE_CU("cu_skip_flag", uiSymbol); 
969#endif
970}
971
972/** code merge flag
973 * \param pcCU
974 * \param uiAbsPartIdx
975 * \returns Void
976 */
977Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
978{
979  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
980  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
981
982#if H_MV_ENC_DEC_TRAC
983  DTRACE_PU("merge_flag", uiSymbol); 
984#else
985  DTRACE_CABAC_VL( g_nSymbolCounter++ );
986  DTRACE_CABAC_T( "\tMergeFlag: " );
987  DTRACE_CABAC_V( uiSymbol );
988  DTRACE_CABAC_T( "\tAddress: " );
989  DTRACE_CABAC_V( pcCU->getAddr() );
990  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
991  DTRACE_CABAC_V( uiAbsPartIdx );
992  DTRACE_CABAC_T( "\n" );
993#endif
994}
995
996/** code merge index
997 * \param pcCU
998 * \param uiAbsPartIdx
999 * \returns Void
1000 */
1001Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
1002{
1003  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
1004  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
1005  if ( uiNumCand > 1 )
1006  {
1007    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
1008    {
1009      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
1010      if ( ui==0 )
1011      {
1012        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
1013      }
1014      else
1015      {
1016        m_pcBinIf->encodeBinEP( uiSymbol );
1017      }
1018      if( uiSymbol == 0 )
1019      {
1020        break;
1021      }
1022    }
1023#if H_MV_ENC_DEC_TRAC
1024    DTRACE_PU("merge_idx", uiUnaryIdx); 
1025#endif
1026  }
1027#if !H_MV_ENC_DEC_TRAC
1028  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1029  DTRACE_CABAC_T( "\tparseMergeIndex()" );
1030  DTRACE_CABAC_T( "\tuiMRGIdx= " );
1031  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
1032  DTRACE_CABAC_T( "\n" );
1033#endif
1034}
1035
1036#if H_3D_ARP
1037Void TEncSbac::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
1038{
1039  Int  iW = (Int)pcCU->getARPW( uiAbsPartIdx );
1040  Int  iMaxW = pcCU->getSlice()->getARPStepNum() - 1;
1041  assert( iMaxW > 0);
1042
1043  Int nOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
1044  Int nBinNum = iW + ( iW != iMaxW );
1045  m_pcBinIf->encodeBin( iW ? 1 : 0 , m_cCUPUARPWSCModel.get( 0, 0, 0 + nOffset ) );
1046  if( nBinNum > 1 )
1047  {
1048     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPWSCModel.get( 0, 0, 2 ) );
1049  }
1050#if H_MV_ENC_DEC_TRAC
1051  DTRACE_CU("iv_res_pred_weight_idx", iW); 
1052#endif
1053}
1054#endif
1055
1056#if H_3D_IC
1057/** code Illumination Compensation flag
1058 * \param pcCU
1059 * \param uiAbsPartIdx
1060 * \returns Void
1061 */
1062Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1063{
1064  // get context function is here
1065  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
1066  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) );
1067#if !H_MV_ENC_DEC_TRAC
1068  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1069  DTRACE_CABAC_T( "\tICFlag" );
1070  DTRACE_CABAC_T( "\tuiSymbol: ");
1071  DTRACE_CABAC_V( uiSymbol );
1072  DTRACE_CABAC_T( "\n");
1073#else
1074  DTRACE_CU("ic_flag", uiSymbol ); 
1075#endif
1076}
1077#endif
1078
1079
1080Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1081{
1082  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1083    return;
1084 
1085  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
1086  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
1087 
1088  assert( uiCtx < 3 );
1089#if H_3D_QTLPC
1090  Bool bCodeSplitFlag    = true;
1091
1092#if MTK_I0099_VPS_EX2
1093  TComVPS *vps           = pcCU->getPic()->getSlice(0)->getVPS();
1094  Bool    bLimQtPredFlag = vps->getLimQtPredFlag(pcCU->getPic()->getSlice(0)->getLayerId());
1095#else
1096  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
1097#endif
1098  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
1099  Bool bDepthMapDetect   = (pcTexture != NULL);
1100  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
1101
1102  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);
1103
1104#if MTK_I0099_VPS_EX2
1105  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && bLimQtPredFlag  )
1106#else
1107  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC())
1108#endif
1109  {
1110    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
1111    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
1112    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
1113    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
1114  }
1115
1116  if(!bCodeSplitFlag)
1117  {
1118    assert(uiCurrSplitFlag == 0);
1119    return;
1120  }
1121#endif
1122  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
1123#if !H_MV_ENC_DEC_TRAC
1124  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1125  DTRACE_CABAC_T( "\tSplitFlag\n" )
1126#else
1127  DTRACE_CU("split_cu_flag", uiCurrSplitFlag ); 
1128#endif
1129  return;
1130}
1131
1132Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1133{
1134  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
1135#if !H_MV_ENC_DEC_TRAC
1136  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1137  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1138  DTRACE_CABAC_T( "\tsymbol=" )
1139  DTRACE_CABAC_V( uiSymbol )
1140  DTRACE_CABAC_T( "\tctx=" )
1141  DTRACE_CABAC_V( uiCtx )
1142  DTRACE_CABAC_T( "\n" )
1143#endif
1144}
1145
1146Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
1147{
1148  UInt dir[4],j;
1149  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
1150  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
1151  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1152  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
1153  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1154  for (j=0;j<partNum;j++)
1155  {
1156    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
1157#if H_3D_DIM
1158#if SEPARATE_FLAG_I0085
1159    if( pcCU->getSlice()->getVpsDepthModesFlag() ||  pcCU->getSlice()->getIVPFlag() )
1160#else
1161    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1162#endif
1163    {
1164      codeIntraDepth( pcCU, absPartIdx+partOffset*j );
1165    }
1166    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1167    {
1168#endif
1169      predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
1170      for(UInt i = 0; i < predNum[j]; i++)
1171      {
1172        if(dir[j] == preds[j][i])
1173        {
1174          predIdx[j] = i;
1175        }
1176      }
1177      m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1178#if H_MV_ENC_DEC_TRAC
1179      DTRACE_CU("prev_intra_luma_pred_flag", (predIdx[j] != -1)? 1 : 0); 
1180#endif
1181#if H_3D_DIM
1182    }
1183#endif
1184
1185  } 
1186  for (j=0;j<partNum;j++)
1187  {
1188#if H_3D_DIM
1189    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1190    {
1191#endif
1192    if(predIdx[j] != -1)
1193    {
1194      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
1195      if (predIdx[j])
1196      {
1197        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
1198      }
1199#if H_MV_ENC_DEC_TRAC
1200      DTRACE_CU("mpm_idx", predIdx[j] ); 
1201#endif
1202    }
1203    else
1204    {
1205      if (preds[j][0] > preds[j][1])
1206      { 
1207        std::swap(preds[j][0], preds[j][1]); 
1208      }
1209      if (preds[j][0] > preds[j][2])
1210      {
1211        std::swap(preds[j][0], preds[j][2]);
1212      }
1213      if (preds[j][1] > preds[j][2])
1214      {
1215        std::swap(preds[j][1], preds[j][2]);
1216      }
1217      for(Int i = (predNum[j] - 1); i >= 0; i--)
1218      {
1219        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
1220      }
1221      m_pcBinIf->encodeBinsEP( dir[j], 5 );
1222#if H_MV_ENC_DEC_TRAC
1223      DTRACE_CU("rem_intra_luma_pred_mode", dir[j] ); 
1224#endif
1225    }
1226#if H_3D_DIM
1227    }
1228#endif
1229  }
1230  return;
1231}
1232
1233Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1234{
1235  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1236
1237  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1238  {
1239    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1240#if H_MV_ENC_DEC_TRAC
1241    DTRACE_CU("intra_chroma_pred_mode", 0 ); 
1242#endif
1243  }
1244  else
1245  { 
1246    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1247    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1248
1249    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1250    {
1251      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1252      {
1253        uiIntraDirChroma = i;
1254        break;
1255      }
1256    }
1257    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1258
1259    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1260#if H_MV_ENC_DEC_TRAC
1261    DTRACE_CU("intra_chroma_pred_mode", uiIntraDirChroma ); 
1262#endif
1263
1264  }
1265  return;
1266}
1267
1268#if H_3D_DIM
1269Void TEncSbac::codeIntraDepth( TComDataCU* pcCU, UInt absPartIdx )
1270{
1271  codeIntraDepthMode( pcCU, absPartIdx );
1272
1273  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1274  UInt dimType = getDimType( dir );
1275
1276  switch( dimType )
1277  {
1278#if H_3D_DIM_DMM
1279  case( DMM1_IDX ):
1280    {
1281      xCodeDmm1WedgeIdx( pcCU->getDmmWedgeTabIdx( dimType, absPartIdx ), g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1282    } break;
1283  case( DMM4_IDX ): break;
1284#endif
1285  default: break;
1286  }
1287}
1288
1289Void TEncSbac::codeIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx )
1290{
1291  UInt dir = pcCU->getLumaIntraDir( absPartIdx );
1292
1293  if( ( pcCU->getSlice()->getSPS()->getMaxCUWidth() >> pcCU->getDepth( absPartIdx ) ) < 64 ) //DMM and HEVC intra modes are both allowed
1294  {
1295    m_pcBinIf->encodeBin( isDimMode( dir ) ? 0 : 1, m_cAngleFlagSCModel.get( 0, 0, 0 ) );
1296  }
1297  if( isDimMode( dir ) )
1298  {
1299    UInt uiCodeIdx = 0;
1300
1301    switch( getDimType( dir ) )
1302    {
1303    case DMM1_IDX: uiCodeIdx = 0; break;
1304    case DMM4_IDX: uiCodeIdx = 1; break;
1305    default:                      break;
1306    }
1307    //mode coding
1308#if SEPARATE_FLAG_I0085
1309    if( pcCU->getSlice()->getVpsDepthModesFlag() && pcCU->getSlice()->getIVPFlag())
1310    {
1311      m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1312    }
1313#else
1314    m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1315#endif
1316  }
1317}
1318#endif
1319
1320
1321Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1322{
1323  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1324  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1325  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1326  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1327  {
1328    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1329  }
1330  if (uiInterDir < 2)
1331  {
1332    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1333  }
1334#if H_MV_ENC_DEC_TRAC
1335  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1336#endif
1337
1338  return;
1339}
1340
1341Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1342{
1343  {
1344    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1345    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1346    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1347   
1348    if( iRefFrame > 0 )
1349    {
1350      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1351      pCtx++;
1352      iRefFrame--;
1353      for( UInt ui = 0; ui < uiRefNum; ++ui )
1354      {
1355        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1356        if( ui == 0 )
1357        {
1358          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1359        }
1360        else
1361        {
1362          m_pcBinIf->encodeBinEP( uiSymbol );
1363        }
1364        if( uiSymbol == 0 )
1365        {
1366          break;
1367        }
1368      }
1369    }
1370#if H_MV_ENC_DEC_TRAC
1371#if ENC_DEC_TRACE
1372    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1373    if ( eRefList == REF_PIC_LIST_0 )
1374    {
1375      DTRACE_PU("ref_idx_l0", iRefFrame)
1376    }
1377    else
1378    {
1379      DTRACE_PU("ref_idx_l1", iRefFrame)
1380    }
1381#endif
1382#endif
1383  }
1384  return;
1385}
1386
1387Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1388{
1389  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1390  {
1391    return;
1392  }
1393
1394  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1395  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1396  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1397  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1398
1399  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1400  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1401
1402  const Bool bHorAbsGr0 = iHor != 0;
1403  const Bool bVerAbsGr0 = iVer != 0;
1404  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1405  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1406  pCtx++;
1407
1408  if( bHorAbsGr0 )
1409  {
1410    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1411  }
1412
1413  if( bVerAbsGr0 )
1414  {
1415    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1416  }
1417
1418  if( bHorAbsGr0 )
1419  {
1420    if( uiHorAbs > 1 )
1421    {
1422      xWriteEpExGolomb( uiHorAbs-2, 1 );
1423    }
1424
1425    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1426  }
1427
1428  if( bVerAbsGr0 )
1429  {
1430    if( uiVerAbs > 1 )
1431    {
1432      xWriteEpExGolomb( uiVerAbs-2, 1 );
1433    }
1434
1435    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1436  }
1437 
1438  return;
1439}
1440
1441Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1442{
1443  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1444 
1445  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1446  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1447
1448  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1449  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1450  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1451  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1452  {
1453    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1454  }
1455
1456  if ( uiAbsDQp > 0)
1457  {
1458    UInt uiSign = (iDQp > 0 ? 0 : 1);
1459    m_pcBinIf->encodeBinEP(uiSign);
1460  }
1461
1462  return;
1463}
1464
1465Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1466{
1467  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1468  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1469  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1470#if !H_MV_ENC_DEC_TRAC
1471  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1472  DTRACE_CABAC_T( "\tparseQtCbf()" )
1473  DTRACE_CABAC_T( "\tsymbol=" )
1474  DTRACE_CABAC_V( uiCbf )
1475  DTRACE_CABAC_T( "\tctx=" )
1476  DTRACE_CABAC_V( uiCtx )
1477  DTRACE_CABAC_T( "\tetype=" )
1478  DTRACE_CABAC_V( eType )
1479  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1480  DTRACE_CABAC_V( uiAbsPartIdx )
1481  DTRACE_CABAC_T( "\n" )
1482#endif
1483}
1484
1485void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1486{
1487  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1488  {
1489    return;
1490  }
1491  if(width != 4 || height != 4)
1492  {
1493    return;
1494  }
1495
1496  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1497  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1498#if !H_MV_ENC_DEC_TRAC
1499  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1500  DTRACE_CABAC_T("\tparseTransformSkip()");
1501  DTRACE_CABAC_T( "\tsymbol=" )
1502  DTRACE_CABAC_V( useTransformSkip )
1503  DTRACE_CABAC_T( "\tAddr=" )
1504  DTRACE_CABAC_V( pcCU->getAddr() )
1505  DTRACE_CABAC_T( "\tetype=" )
1506  DTRACE_CABAC_V( eTType )
1507  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1508  DTRACE_CABAC_V( uiAbsPartIdx )
1509  DTRACE_CABAC_T( "\n" )
1510#endif
1511}
1512
1513/** Code I_PCM information.
1514 * \param pcCU pointer to CU
1515 * \param uiAbsPartIdx CU index
1516 * \returns Void
1517 */
1518Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1519{
1520  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1521
1522  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1523
1524  m_pcBinIf->encodeBinTrm (uiIPCM);
1525#if H_MV_ENC_DEC_TRAC         
1526  DTRACE_CU("pcm_flag", uiIPCM)
1527#endif
1528  if (writePCMSampleFlag)
1529  {
1530    m_pcBinIf->encodePCMAlignBits();
1531
1532    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1533    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1534    UInt uiChromaOffset = uiLumaOffset>>2;
1535    Pel* piPCMSample;
1536    UInt uiWidth;
1537    UInt uiHeight;
1538    UInt uiSampleBits;
1539    UInt uiX, uiY;
1540
1541    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1542    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1543    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1544    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1545
1546    for(uiY = 0; uiY < uiHeight; uiY++)
1547    {
1548      for(uiX = 0; uiX < uiWidth; uiX++)
1549      {
1550        UInt uiSample = piPCMSample[uiX];
1551
1552        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1553      }
1554      piPCMSample += uiWidth;
1555    }
1556
1557    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1558    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1559    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1560    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1561
1562    for(uiY = 0; uiY < uiHeight; uiY++)
1563    {
1564      for(uiX = 0; uiX < uiWidth; uiX++)
1565      {
1566        UInt uiSample = piPCMSample[uiX];
1567
1568        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1569      }
1570      piPCMSample += uiWidth;
1571    }
1572
1573    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1574    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1575    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1576    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1577
1578    for(uiY = 0; uiY < uiHeight; uiY++)
1579    {
1580      for(uiX = 0; uiX < uiWidth; uiX++)
1581      {
1582        UInt uiSample = piPCMSample[uiX];
1583
1584        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1585      }
1586      piPCMSample += uiWidth;
1587    }
1588    m_pcBinIf->resetBac();
1589  }
1590}
1591
1592Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1593{
1594  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1595  UInt uiCtx = 0;
1596  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1597#if !H_MV_ENC_DEC_TRAC
1598  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1599  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1600  DTRACE_CABAC_T( "\tsymbol=" )
1601  DTRACE_CABAC_V( uiCbf )
1602  DTRACE_CABAC_T( "\tctx=" )
1603  DTRACE_CABAC_V( uiCtx )
1604  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1605  DTRACE_CABAC_V( uiAbsPartIdx )
1606  DTRACE_CABAC_T( "\n" )
1607#else
1608  DTRACE_CU( "rqt_root_cbf", uiCbf )
1609#endif
1610}
1611
1612Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1613{
1614  // this function is only used to estimate the bits when cbf is 0
1615  // and will never be called when writing the bistream. do not need to write log
1616  UInt uiCbf = 0;
1617  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1618  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1619}
1620
1621Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1622{
1623  // this function is only used to estimate the bits when cbf is 0
1624  // and will never be called when writing the bistream. do not need to write log
1625  UInt uiCbf = 0;
1626  UInt uiCtx = 0;
1627  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1628}
1629
1630/** Encode (X,Y) position of the last significant coefficient
1631 * \param uiPosX X component of last coefficient
1632 * \param uiPosY Y component of last coefficient
1633 * \param width  Block width
1634 * \param height Block height
1635 * \param eTType plane type / luminance or chrominance
1636 * \param uiScanIdx scan type (zig-zag, hor, ver)
1637 * This method encodes the X and Y component within a block of the last significant coefficient.
1638 */
1639Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1640{ 
1641  // swap
1642  if( uiScanIdx == SCAN_VER )
1643  {
1644    swap( uiPosX, uiPosY );
1645  }
1646
1647  UInt uiCtxLast;
1648  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1649  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1650  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1651  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1652
1653
1654  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1655  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1656  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1657  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1658  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1659  // posX
1660  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1661  {
1662      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1663  }
1664  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1665  {
1666      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1667  }
1668
1669  // posY
1670  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1671  {
1672    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1673  }
1674  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1675  {
1676    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1677  }
1678  if ( uiGroupIdxX > 3 )
1679  {     
1680    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1681    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1682    for (Int i = uiCount - 1 ; i >= 0; i-- )
1683    {
1684      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1685    }
1686  }
1687  if ( uiGroupIdxY > 3 )
1688  {     
1689    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1690    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1691    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1692    {
1693      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1694    }
1695  }
1696}
1697
1698Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1699{
1700#if !H_MV_ENC_DEC_TRAC
1701  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1702  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1703  DTRACE_CABAC_V( eTType )
1704  DTRACE_CABAC_T( "\twidth=" )
1705  DTRACE_CABAC_V( uiWidth )
1706  DTRACE_CABAC_T( "\theight=" )
1707  DTRACE_CABAC_V( uiHeight )
1708  DTRACE_CABAC_T( "\tdepth=" )
1709  DTRACE_CABAC_V( uiDepth )
1710  DTRACE_CABAC_T( "\tabspartidx=" )
1711  DTRACE_CABAC_V( uiAbsPartIdx )
1712  DTRACE_CABAC_T( "\ttoCU-X=" )
1713  DTRACE_CABAC_V( pcCU->getCUPelX() )
1714  DTRACE_CABAC_T( "\ttoCU-Y=" )
1715  DTRACE_CABAC_V( pcCU->getCUPelY() )
1716  DTRACE_CABAC_T( "\tCU-addr=" )
1717  DTRACE_CABAC_V(  pcCU->getAddr() )
1718  DTRACE_CABAC_T( "\tinCU-X=" )
1719  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1720  DTRACE_CABAC_T( "\tinCU-Y=" )
1721  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1722  DTRACE_CABAC_T( "\tpredmode=" )
1723  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1724  DTRACE_CABAC_T( "\n" )
1725#endif
1726  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1727  {
1728    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1729    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1730  }
1731 
1732  UInt uiNumSig = 0;
1733 
1734  // compute number of significant coefficients
1735  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1736 
1737  if ( uiNumSig == 0 )
1738    return;
1739  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1740  {
1741    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1742  }
1743  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1744 
1745  //----- encode significance map -----
1746  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1747  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1748  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1749 
1750  Bool beValid;
1751  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1752  {
1753    beValid = false;
1754  }
1755  else 
1756  {
1757    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1758  }
1759
1760  // Find position of last coefficient
1761  Int scanPosLast = -1;
1762  Int posLast;
1763
1764  const UInt * scanCG;
1765  {
1766    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1767    if( uiLog2BlockSize == 3 )
1768    {
1769      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1770    }
1771    else if( uiLog2BlockSize == 5 )
1772    {
1773      scanCG = g_sigLastScanCG32x32;
1774    }
1775  }
1776  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1777  static const UInt uiShift = MLS_CG_SIZE >> 1;
1778  const UInt uiNumBlkSide = uiWidth >> uiShift;
1779
1780    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1781
1782    do
1783    {
1784      posLast = scan[ ++scanPosLast ];
1785
1786      // get L1 sig map
1787      UInt uiPosY    = posLast >> uiLog2BlockSize;
1788      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1789      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1790      if( pcCoef[ posLast ] )
1791      {
1792        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1793      }
1794
1795      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1796    }
1797    while ( uiNumSig > 0 );
1798
1799  // Code position of last coefficient
1800  Int posLastY = posLast >> uiLog2BlockSize;
1801  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1802  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1803 
1804  //===== code significance flag =====
1805  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1806  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1807
1808
1809  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1810  UInt c1 = 1;
1811  UInt uiGoRiceParam           = 0;
1812  Int  iScanPosSig             = scanPosLast;
1813
1814  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1815  {
1816    Int numNonZero = 0;
1817    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1818    uiGoRiceParam    = 0;
1819    Int absCoeff[16];
1820    UInt coeffSigns = 0;
1821
1822    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1823
1824    if( iScanPosSig == scanPosLast )
1825    {
1826      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1827      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1828      numNonZero    = 1;
1829      lastNZPosInCG  = iScanPosSig;
1830      firstNZPosInCG = iScanPosSig;
1831      iScanPosSig--;
1832    }
1833
1834      // encode significant_coeffgroup_flag
1835      Int iCGBlkPos = scanCG[ iSubSet ];
1836      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1837      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1838      if( iSubSet == iLastScanSet || iSubSet == 0)
1839      {
1840        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1841      }
1842      else
1843      {
1844          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1845          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1846          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1847      }
1848     
1849      // encode significant_coeff_flag
1850      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1851      {
1852        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1853        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1854        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1855        {
1856          uiBlkPos  = scan[ iScanPosSig ]; 
1857          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1858          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1859          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1860          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1861          {
1862            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1863            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1864          }
1865          if( uiSig )
1866          {
1867            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1868            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1869            numNonZero++;
1870            if( lastNZPosInCG == -1 )
1871            {
1872              lastNZPosInCG = iScanPosSig;
1873            }
1874            firstNZPosInCG = iScanPosSig;
1875          }
1876        }
1877      }
1878      else
1879      {
1880        iScanPosSig = iSubPos - 1;
1881      }
1882
1883    if( numNonZero > 0 )
1884    {
1885      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1886      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1887     
1888      if( c1 == 0 )
1889      {
1890        uiCtxSet++;
1891      }
1892      c1 = 1;
1893      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1894     
1895      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1896      Int firstC2FlagIdx = -1;
1897      for( Int idx = 0; idx < numC1Flag; idx++ )
1898      {
1899        UInt uiSymbol = absCoeff[ idx ] > 1;
1900        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1901        if( uiSymbol )
1902        {
1903          c1 = 0;
1904
1905          if (firstC2FlagIdx == -1)
1906          {
1907            firstC2FlagIdx = idx;
1908          }
1909        }
1910        else if( (c1 < 3) && (c1 > 0) )
1911        {
1912          c1++;
1913        }
1914      }
1915     
1916      if (c1 == 0)
1917      {
1918
1919        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1920        if ( firstC2FlagIdx != -1)
1921        {
1922          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1923          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1924        }
1925      }
1926     
1927      if( beValid && signHidden )
1928      {
1929        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1930      }
1931      else
1932      {
1933        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1934      }
1935     
1936      Int iFirstCoeff2 = 1;   
1937      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1938      {
1939        for ( Int idx = 0; idx < numNonZero; idx++ )
1940        {
1941          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1942
1943          if( absCoeff[ idx ] >= baseLevel)
1944          {
1945            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1946            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1947            {
1948               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1949            }
1950          }
1951          if(absCoeff[ idx ] >= 2) 
1952          {
1953            iFirstCoeff2 = 0;
1954          }
1955        }       
1956      }
1957    }
1958  }
1959
1960  return;
1961}
1962
1963/** code SAO offset sign
1964 * \param code sign value
1965 */
1966Void TEncSbac::codeSAOSign( UInt code )
1967{
1968  m_pcBinIf->encodeBinEP( code );
1969}
1970
1971Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1972{
1973  if (maxSymbol == 0)
1974  {
1975    return;
1976  }
1977
1978  Int i;
1979  Bool bCodeLast = ( maxSymbol > code );
1980
1981  if ( code == 0 )
1982  {
1983    m_pcBinIf->encodeBinEP( 0 );
1984  }
1985  else
1986  {
1987    m_pcBinIf->encodeBinEP( 1 );
1988    for ( i=0; i<code-1; i++ )
1989    {
1990      m_pcBinIf->encodeBinEP( 1 );
1991    }
1992    if( bCodeLast )
1993    {
1994      m_pcBinIf->encodeBinEP( 0 );
1995    }
1996  }
1997}
1998
1999
2000/** Code SAO EO class or BO band position
2001 * \param uiLength
2002 * \param uiCode
2003 */
2004Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
2005{
2006   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
2007}
2008/** Code SAO merge flags
2009 * \param uiCode
2010 * \param uiCompIdx
2011 */
2012Void TEncSbac::codeSaoMerge       ( UInt uiCode )
2013{
2014  if (uiCode == 0)
2015  {
2016    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2017  }
2018  else
2019  {
2020    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2021  }
2022}
2023/** Code SAO type index
2024 * \param uiCode
2025 */
2026Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
2027{
2028  if (uiCode == 0)
2029  {
2030    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2031  }
2032  else
2033  {
2034    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2035    m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 );
2036  }
2037}
2038/*!
2039 ****************************************************************************
2040 * \brief
2041 *   estimate bit cost for CBP, significant map and significant coefficients
2042 ****************************************************************************
2043 */
2044Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2045{
2046  estCBFBit( pcEstBitsSbac );
2047
2048  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
2049 
2050  // encode significance map
2051  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
2052 
2053  // encode significant coefficients
2054  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
2055}
2056
2057/*!
2058 ****************************************************************************
2059 * \brief
2060 *    estimate bit cost for each CBP bit
2061 ****************************************************************************
2062 */
2063Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
2064{
2065  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2066
2067  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2068  {
2069    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2070    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2071  }
2072
2073  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2074 
2075  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2076  {
2077    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2078    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2079  }
2080}
2081
2082
2083/*!
2084 ****************************************************************************
2085 * \brief
2086 *    estimate SAMBAC bit cost for significant coefficient group map
2087 ****************************************************************************
2088 */
2089Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2090{
2091  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2092
2093  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2094  {
2095    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2096    {
2097      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2098    }
2099  }
2100}
2101
2102
2103/*!
2104 ****************************************************************************
2105 * \brief
2106 *    estimate SAMBAC bit cost for significant coefficient map
2107 ****************************************************************************
2108 */
2109Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2110{
2111  Int firstCtx = 1, numCtx = 8;
2112  if (max(width, height) >= 16)
2113  {
2114    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2115    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2116  }
2117  else if (width == 8)
2118  {
2119    firstCtx = 9;
2120    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2121  }
2122 
2123  if (eTType == TEXT_LUMA )
2124  {
2125    for( UInt bin = 0; bin < 2; bin++ )
2126    {
2127      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2128    }
2129
2130    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2131    {
2132      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2133      {
2134        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2135      }
2136    }
2137  }
2138  else
2139  {
2140    for( UInt bin = 0; bin < 2; bin++ )
2141    {
2142      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2143    }
2144    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2145    {
2146      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2147      {
2148        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2149      }
2150    }
2151  }
2152  Int iBitsX = 0, iBitsY = 0;
2153  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2154
2155  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2156  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2157  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2158  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2159
2160  Int ctx;
2161  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2162  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2163  {
2164    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2165    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2166    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2167  }
2168  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2169  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2170  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2171  {
2172    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2173    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2174    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2175  }
2176  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2177}
2178
2179/*!
2180 ****************************************************************************
2181 * \brief
2182 *    estimate bit cost of significant coefficient
2183 ****************************************************************************
2184 */
2185Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2186{
2187  if (eTType==TEXT_LUMA)
2188  {
2189    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2190    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2191
2192    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2193    {
2194      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2195      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2196    }
2197
2198    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2199    {
2200      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2201      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2202    }
2203  }
2204  else
2205  {
2206    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2207    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2208
2209    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2210    {
2211      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2212      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2213    }
2214
2215    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2216    {
2217      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2218      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2219    }
2220  }
2221}
2222
2223/**
2224 - Initialize our context information from the nominated source.
2225 .
2226 \param pSrc From where to copy context information.
2227 */
2228Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2229{ 
2230  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2231}
2232
2233Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2234{
2235  this->xCopyContextsFrom(pScr);
2236}
2237
2238Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled)
2239{
2240  UInt uiSymbol;
2241  if(!sliceEnabled)
2242  {
2243    assert(ctbParam.modeIdc == SAO_MODE_OFF);
2244    return;
2245  }
2246
2247  //type
2248  if(compIdx == SAO_Y || compIdx == SAO_Cb)
2249  {
2250    //sao_type_idx_luma or sao_type_idx_chroma
2251    if(ctbParam.modeIdc == SAO_MODE_OFF)
2252    {
2253      uiSymbol =0;
2254    }
2255    else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO
2256    {
2257      uiSymbol = 1;
2258    }
2259    else
2260    {
2261      assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO
2262      uiSymbol = 2;
2263    }
2264    codeSaoTypeIdx(uiSymbol); 
2265  }
2266
2267  if(ctbParam.modeIdc == SAO_MODE_NEW)
2268  {
2269    Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; 
2270    Int offset[4];
2271    Int k=0;
2272    for(Int i=0; i< numClasses; i++)
2273    {
2274      if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN)
2275      {
2276        continue;
2277      }
2278      Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?(  (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES   ):i;
2279      offset[k] = ctbParam.offset[classIdx];
2280      k++;
2281    }
2282
2283    for(Int i=0; i< 4; i++)
2284    {
2285      codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]),  g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs
2286    }
2287
2288
2289    if(ctbParam.typeIdc == SAO_TYPE_BO)
2290    {
2291      for(Int i=0; i< 4; i++)
2292      {
2293        if(offset[i] != 0)
2294        {
2295          codeSAOSign((offset[i]< 0)?1:0);
2296        }
2297      }
2298
2299      codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position
2300    }
2301    else //EO
2302    {
2303      if(compIdx == SAO_Y || compIdx == SAO_Cb)
2304      {
2305        assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0);
2306        codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma
2307      }
2308    }
2309
2310  }
2311}
2312
2313
2314Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam
2315                              , Bool* sliceEnabled
2316                              , Bool leftMergeAvail
2317                              , Bool aboveMergeAvail
2318                              , Bool onlyEstMergeInfo // = false
2319                              )
2320{
2321
2322  Bool isLeftMerge = false;
2323  Bool isAboveMerge= false;
2324
2325  if(leftMergeAvail)
2326  {
2327    isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT));
2328    codeSaoMerge( isLeftMerge?1:0  ); //sao_merge_left_flag
2329  }
2330
2331  if( aboveMergeAvail && !isLeftMerge)
2332  {
2333    isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); 
2334    codeSaoMerge( isAboveMerge?1:0  ); //sao_merge_left_flag
2335  }
2336
2337  if(onlyEstMergeInfo)
2338  {
2339    return; //only for RDO
2340  }
2341
2342  if(!isLeftMerge && !isAboveMerge) //not merge mode
2343  {
2344    for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
2345    {
2346      codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]);
2347    }
2348  }
2349}
2350
2351#if H_3D_INTER_SDC
2352Void TEncSbac::codeDeltaDC( TComDataCU* pcCU, UInt absPartIdx )
2353{
2354  if( !( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2355  {
2356    assert( 0 );
2357  }
2358
2359#if HS_DMM_SIGNALLING_I0120
2360  UInt uiNumSegments = isDimMode( pcCU->getLumaIntraDir( absPartIdx ) ) ? 2 : 1;
2361  UInt dimDeltaDC = 1;
2362#else
2363  UInt uiNumSegments = 0;
2364  UInt dimDeltaDC = 0;
2365#endif
2366
2367#if HS_DMM_SIGNALLING_I0120
2368  if( pcCU->isIntra( absPartIdx ) && pcCU->getSDCFlag( absPartIdx ))
2369  {
2370#else
2371  if( pcCU->isIntra( absPartIdx ) )
2372  {
2373    UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2374    uiNumSegments = isDimMode( dir ) ? 2 : 1;
2375
2376    if( pcCU->getSDCFlag( absPartIdx ) )
2377    {
2378#endif
2379      if( uiNumSegments == 1 )
2380      {
2381        dimDeltaDC = pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) ? 1 : 0;
2382      }
2383      else
2384      {
2385        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) || pcCU->getSDCSegmentDCOffset( 1, absPartIdx ) ) ? 1 : 0;
2386      }
2387#if !HS_DMM_SIGNALLING_I0120
2388    }
2389    else
2390    {
2391      dimDeltaDC = isDimDeltaDC( dir );
2392    }
2393#endif
2394    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2395  }
2396#if !HS_DMM_SIGNALLING_I0120
2397  else //all-zero inter SDC is not allowed
2398  {
2399    uiNumSegments = 1;
2400    dimDeltaDC = 1;
2401  }
2402#endif
2403
2404  if( dimDeltaDC )
2405  {
2406    for( UInt segment = 0; segment < uiNumSegments; segment++ )
2407    {
2408      Pel deltaDC = 0;
2409
2410      if( pcCU->isIntra( absPartIdx ) )
2411      {
2412        UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2413        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset( segment, absPartIdx ) : pcCU->getDimDeltaDC( getDimType( dir ), segment, absPartIdx );
2414      }
2415      else
2416      {
2417        deltaDC = pcCU->getSDCSegmentDCOffset( segment, absPartIdx );
2418      }
2419
2420      xCodeDimDeltaDC( deltaDC, uiNumSegments );
2421    }
2422  }
2423}
2424
2425Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2426{
2427  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2428  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2429
2430  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2431}
2432
2433#endif
2434
2435#if H_3D_DBBP
2436Void TEncSbac::codeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2437{
2438#if SEC_DBBP_EXPLICIT_SIG_I0077
2439  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
2440  AOF( ePartSize == SIZE_2NxN || ePartSize == SIZE_Nx2N );
2441#endif
2442  AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
2443  AOF( !pcCU->getSlice()->getIsDepth() );
2444 
2445  UInt uiSymbol = pcCU->getDBBPFlag( uiAbsPartIdx ) ? 1 : 0;
2446  m_pcBinIf->encodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) );
2447}
2448#endif
2449
2450//! \}
Note: See TracBrowser for help on using the repository browser.