source: 3DVCSoftware/branches/HTM-10.0-Fix/source/Lib/TLibEncoder/TEncSbac.cpp @ 1417

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

Merged 9.3-dev0@831.

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