source: 3DVCSoftware/branches/HTM-6.2-dev0/source/Lib/TLibEncoder/TEncSbac.cpp @ 431

Last change on this file since 431 was 427, checked in by tech, 12 years ago

Merged HTM-6.2-dev1-LG

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