source: 3DVCSoftware/branches/HTM-9.3-dev2-RWTH/source/Lib/TLibEncoder/TEncSbac.cpp @ 1404

Last change on this file since 1404 was 806, checked in by mediatek-htm, 11 years ago

Integration of CABAC simplification of ARP flag in JCT3V-G0061. The MACRO is "MTK_ARP_FLAG_CABAC_SIMP_G0061".

by Yi-Wen Chen (yiwen.chen@…)

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