source: 3DVCSoftware/branches/HTM-10.1-dev0/source/Lib/TLibEncoder/TEncSbac.cpp @ 876

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

Cleanups part 1.

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