source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncSbac.cpp @ 332

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

Merged branch 6.1-Cleanup@329.

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