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

Last change on this file since 805 was 805, checked in by mediatek-htm, 10 years ago

Integration of CABAC simplification for IC flag in JCT3V-G0061. The MACRO is "MTK_IC_FLAG_CABAC_SIMP_G0061".

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

  • Property svn:eol-style set to native
File size: 75.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-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     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPWSCModel.get( 0, 0, 3 ) );
1003  }
1004#if H_MV_ENC_DEC_TRAC
1005  DTRACE_CU("iv_res_pred_weight_idx", iW); 
1006#endif
1007}
1008#endif
1009
1010#if H_3D_IC
1011/** code Illumination Compensation flag
1012 * \param pcCU
1013 * \param uiAbsPartIdx
1014 * \returns Void
1015 */
1016Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1017{
1018  // get context function is here
1019  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
1020#if MTK_IC_FLAG_CABAC_SIMP_G0061
1021  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) );
1022#else
1023  UInt uiCtxIC  = pcCU->getCtxICFlag( uiAbsPartIdx ) ;
1024  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, uiCtxIC ) );
1025#endif
1026#if !H_MV_ENC_DEC_TRAC
1027  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1028  DTRACE_CABAC_T( "\tICFlag" );
1029  DTRACE_CABAC_T( "\tuiCtxIC: ");
1030  DTRACE_CABAC_V( uiCtxIC );
1031  DTRACE_CABAC_T( "\tuiSymbol: ");
1032  DTRACE_CABAC_V( uiSymbol );
1033  DTRACE_CABAC_T( "\n");
1034#else
1035  DTRACE_CU("ic_flag", uiSymbol ); 
1036#endif
1037}
1038#endif
1039
1040
1041Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1042{
1043  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1044    return;
1045 
1046  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
1047  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
1048 
1049  assert( uiCtx < 3 );
1050#if H_3D_QTLPC
1051  Bool bCodeSplitFlag    = true;
1052
1053  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
1054  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
1055  Bool bDepthMapDetect   = (pcTexture != NULL);
1056  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
1057
1058  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);
1059
1060  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC())
1061  {
1062    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
1063    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
1064    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
1065    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
1066  }
1067
1068  if(!bCodeSplitFlag)
1069  {
1070    assert(uiCurrSplitFlag == 0);
1071    return;
1072  }
1073#endif
1074  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
1075#if !H_MV_ENC_DEC_TRAC
1076  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1077  DTRACE_CABAC_T( "\tSplitFlag\n" )
1078#else
1079  DTRACE_CU("split_cu_flag", uiCurrSplitFlag ); 
1080#endif
1081  return;
1082}
1083
1084Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1085{
1086  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
1087#if !H_MV_ENC_DEC_TRAC
1088  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1089  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1090  DTRACE_CABAC_T( "\tsymbol=" )
1091  DTRACE_CABAC_V( uiSymbol )
1092  DTRACE_CABAC_T( "\tctx=" )
1093  DTRACE_CABAC_V( uiCtx )
1094  DTRACE_CABAC_T( "\n" )
1095#endif
1096}
1097
1098Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
1099{
1100  UInt dir[4],j;
1101  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
1102  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
1103  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1104  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
1105  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1106  for (j=0;j<partNum;j++)
1107  {
1108    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
1109#if H_3D_DIM
1110    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1111    {
1112      codeIntraDepth( pcCU, absPartIdx+partOffset*j );
1113    }
1114    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1115#if H_3D_DIM_SDC
1116      if( !pcCU->getSDCFlag( absPartIdx+partOffset*j ) )
1117#endif
1118    {
1119#endif
1120    predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
1121    for(UInt i = 0; i < predNum[j]; i++)
1122    {
1123      if(dir[j] == preds[j][i])
1124      {
1125        predIdx[j] = i;
1126      }
1127    }
1128    m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1129#if H_MV_ENC_DEC_TRAC
1130    DTRACE_CU("prev_intra_luma_pred_flag", (predIdx[j] != -1)? 1 : 0); 
1131#endif
1132#if H_3D_DIM
1133    }
1134#endif
1135
1136  } 
1137  for (j=0;j<partNum;j++)
1138  {
1139#if H_3D_DIM
1140    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1141#if H_3D_DIM_SDC
1142      if( !pcCU->getSDCFlag( absPartIdx+partOffset*j ) )
1143#endif
1144    {
1145#endif
1146    if(predIdx[j] != -1)
1147    {
1148      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
1149      if (predIdx[j])
1150      {
1151        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
1152      }
1153#if H_MV_ENC_DEC_TRAC
1154      DTRACE_CU("mpm_idx", predIdx[j] ); 
1155#endif
1156    }
1157    else
1158    {
1159      if (preds[j][0] > preds[j][1])
1160      { 
1161        std::swap(preds[j][0], preds[j][1]); 
1162      }
1163      if (preds[j][0] > preds[j][2])
1164      {
1165        std::swap(preds[j][0], preds[j][2]);
1166      }
1167      if (preds[j][1] > preds[j][2])
1168      {
1169        std::swap(preds[j][1], preds[j][2]);
1170      }
1171      for(Int i = (predNum[j] - 1); i >= 0; i--)
1172      {
1173        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
1174      }
1175      m_pcBinIf->encodeBinsEP( dir[j], 5 );
1176#if H_MV_ENC_DEC_TRAC
1177      DTRACE_CU("rem_intra_luma_pred_mode", dir[j] ); 
1178#endif
1179    }
1180#if H_3D_DIM
1181    }
1182#endif
1183  }
1184  return;
1185}
1186
1187Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1188{
1189  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1190
1191  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1192  {
1193    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1194#if H_MV_ENC_DEC_TRAC
1195    DTRACE_CU("intra_chroma_pred_mode", 0 ); 
1196#endif
1197  }
1198  else
1199  { 
1200    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1201    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1202
1203    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1204    {
1205      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1206      {
1207        uiIntraDirChroma = i;
1208        break;
1209      }
1210    }
1211    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1212
1213    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1214#if H_MV_ENC_DEC_TRAC
1215    DTRACE_CU("intra_chroma_pred_mode", uiIntraDirChroma ); 
1216#endif
1217
1218  }
1219  return;
1220}
1221
1222#if H_3D_DIM
1223Void TEncSbac::codeIntraDepth( TComDataCU* pcCU, UInt absPartIdx )
1224{
1225  codeIntraDepthMode( pcCU, absPartIdx );
1226
1227  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1228  UInt dimType = getDimType( dir );
1229
1230  switch( dimType )
1231  {
1232#if H_3D_DIM_DMM
1233  case( DMM1_IDX ):
1234    {
1235      xCodeDmm1WedgeIdx( pcCU->getDmmWedgeTabIdx( dimType, absPartIdx ), g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1236    } break;
1237  case( DMM4_IDX ): break;
1238#endif
1239  default: break;
1240  }
1241
1242  if( dimType < DIM_NUM_TYPE || pcCU->getSDCFlag( absPartIdx ) )
1243  {
1244    UInt dimDeltaDC;
1245    Pel  deltaDC;
1246    UInt uiNumSegments = ( dir == PLANAR_IDX ) ? 1 : 2;
1247    if( pcCU->getSDCFlag( absPartIdx ) )
1248    {
1249      if( uiNumSegments==1 )
1250      {
1251        dimDeltaDC = pcCU->getSDCSegmentDCOffset(0, absPartIdx) ? 1 : 0;
1252      }
1253      else
1254      {
1255        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset(0, absPartIdx) || pcCU->getSDCSegmentDCOffset(1, absPartIdx) ) ? 1 : 0;
1256      }
1257    }
1258    else
1259    {
1260      dimDeltaDC = isDimDeltaDC( dir );
1261    }
1262
1263    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get(0, 0, uiNumSegments-1) );
1264
1265    if( dimDeltaDC ) 
1266    {
1267      for( UInt segment = 0; segment < uiNumSegments; segment++ )
1268      {
1269        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset(segment, absPartIdx) : pcCU->getDimDeltaDC( dimType, segment, absPartIdx );
1270        xCodeDimDeltaDC( deltaDC, uiNumSegments );
1271      }
1272    }
1273  }
1274}
1275
1276Void TEncSbac::codeIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx )
1277{
1278  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}};
1279  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}};
1280  UInt dir = pcCU->getLumaIntraDir( absPartIdx );
1281  UInt puIdx = (pcCU->getWidth(absPartIdx) == 64) ? 2 : ( (pcCU->getPartitionSize(absPartIdx) == SIZE_NxN && pcCU->getWidth(absPartIdx) == 8) ? 0 : 1 );
1282  UInt codeIdx = 0;
1283
1284  if( dir < NUM_INTRA_MODE )
1285  { 
1286    codeIdx = 1; 
1287  }
1288  if( isDimMode( dir ) )
1289  {
1290    switch( getDimType( dir ) )
1291    {
1292    case DMM1_IDX: codeIdx = 3; break;
1293    case DMM4_IDX: codeIdx = 4; break;
1294    default:                    break;
1295    }
1296  }
1297
1298#if H_3D_DIM_SDC
1299  if( pcCU->getSDCFlag( absPartIdx ) )
1300  {
1301    switch( dir )
1302    {
1303      case PLANAR_IDX:  codeIdx = 0; break;
1304      default:          codeIdx = 2; break;
1305    }
1306  }
1307#endif
1308  //mode coding
1309  for( UInt i = 0; i < codeWordLenTable[puIdx][codeIdx]; i++ )
1310  {
1311    UInt bit = codeWordTable[puIdx][codeIdx] & ( 1<<(codeWordLenTable[puIdx][codeIdx] - i - 1) );
1312    UInt ctxDepthMode = puIdx*3 + ( (i >= 2) ? 2 : i );
1313    m_pcBinIf->encodeBin( bit!=0 , m_cDepthIntraModeSCModel.get(0, 0, ctxDepthMode) );
1314  }
1315}
1316#endif
1317
1318
1319Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1320{
1321  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1322  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1323  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1324  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1325  {
1326    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1327  }
1328  if (uiInterDir < 2)
1329  {
1330    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1331  }
1332#if H_MV_ENC_DEC_TRAC
1333  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1334#endif
1335
1336  return;
1337}
1338
1339Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1340{
1341  {
1342    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1343    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1344    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1345   
1346    if( iRefFrame > 0 )
1347    {
1348      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1349      pCtx++;
1350      iRefFrame--;
1351      for( UInt ui = 0; ui < uiRefNum; ++ui )
1352      {
1353        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1354        if( ui == 0 )
1355        {
1356          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1357        }
1358        else
1359        {
1360          m_pcBinIf->encodeBinEP( uiSymbol );
1361        }
1362        if( uiSymbol == 0 )
1363        {
1364          break;
1365        }
1366      }
1367    }
1368#if H_MV_ENC_DEC_TRAC
1369#if ENC_DEC_TRACE
1370    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1371    if ( eRefList == REF_PIC_LIST_0 )
1372    {
1373      DTRACE_PU("ref_idx_l0", iRefFrame)
1374    }
1375    else
1376    {
1377      DTRACE_PU("ref_idx_l1", iRefFrame)
1378    }
1379#endif
1380#endif
1381  }
1382  return;
1383}
1384
1385Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1386{
1387  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1388  {
1389    return;
1390  }
1391
1392  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1393  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1394  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1395  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1396
1397  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1398  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1399
1400  const Bool bHorAbsGr0 = iHor != 0;
1401  const Bool bVerAbsGr0 = iVer != 0;
1402  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1403  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1404  pCtx++;
1405
1406  if( bHorAbsGr0 )
1407  {
1408    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1409  }
1410
1411  if( bVerAbsGr0 )
1412  {
1413    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1414  }
1415
1416  if( bHorAbsGr0 )
1417  {
1418    if( uiHorAbs > 1 )
1419    {
1420      xWriteEpExGolomb( uiHorAbs-2, 1 );
1421    }
1422
1423    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1424  }
1425
1426  if( bVerAbsGr0 )
1427  {
1428    if( uiVerAbs > 1 )
1429    {
1430      xWriteEpExGolomb( uiVerAbs-2, 1 );
1431    }
1432
1433    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1434  }
1435 
1436  return;
1437}
1438
1439Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1440{
1441  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1442 
1443  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1444  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1445
1446  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1447  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1448  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1449  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1450  {
1451    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1452  }
1453
1454  if ( uiAbsDQp > 0)
1455  {
1456    UInt uiSign = (iDQp > 0 ? 0 : 1);
1457    m_pcBinIf->encodeBinEP(uiSign);
1458  }
1459
1460  return;
1461}
1462
1463Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1464{
1465  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1466  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1467  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1468#if !H_MV_ENC_DEC_TRAC
1469  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1470  DTRACE_CABAC_T( "\tparseQtCbf()" )
1471  DTRACE_CABAC_T( "\tsymbol=" )
1472  DTRACE_CABAC_V( uiCbf )
1473  DTRACE_CABAC_T( "\tctx=" )
1474  DTRACE_CABAC_V( uiCtx )
1475  DTRACE_CABAC_T( "\tetype=" )
1476  DTRACE_CABAC_V( eType )
1477  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1478  DTRACE_CABAC_V( uiAbsPartIdx )
1479  DTRACE_CABAC_T( "\n" )
1480#endif
1481}
1482
1483void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1484{
1485  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1486  {
1487    return;
1488  }
1489  if(width != 4 || height != 4)
1490  {
1491    return;
1492  }
1493
1494  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1495  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1496#if !H_MV_ENC_DEC_TRAC
1497  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1498  DTRACE_CABAC_T("\tparseTransformSkip()");
1499  DTRACE_CABAC_T( "\tsymbol=" )
1500  DTRACE_CABAC_V( useTransformSkip )
1501  DTRACE_CABAC_T( "\tAddr=" )
1502  DTRACE_CABAC_V( pcCU->getAddr() )
1503  DTRACE_CABAC_T( "\tetype=" )
1504  DTRACE_CABAC_V( eTType )
1505  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1506  DTRACE_CABAC_V( uiAbsPartIdx )
1507  DTRACE_CABAC_T( "\n" )
1508#endif
1509}
1510
1511/** Code I_PCM information.
1512 * \param pcCU pointer to CU
1513 * \param uiAbsPartIdx CU index
1514 * \returns Void
1515 */
1516Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1517{
1518  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1519
1520  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1521
1522  m_pcBinIf->encodeBinTrm (uiIPCM);
1523#if H_MV_ENC_DEC_TRAC         
1524  DTRACE_CU("pcm_flag", uiIPCM)
1525#endif
1526  if (writePCMSampleFlag)
1527  {
1528    m_pcBinIf->encodePCMAlignBits();
1529
1530    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1531    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1532    UInt uiChromaOffset = uiLumaOffset>>2;
1533    Pel* piPCMSample;
1534    UInt uiWidth;
1535    UInt uiHeight;
1536    UInt uiSampleBits;
1537    UInt uiX, uiY;
1538
1539    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1540    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1541    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1542    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1543
1544    for(uiY = 0; uiY < uiHeight; uiY++)
1545    {
1546      for(uiX = 0; uiX < uiWidth; uiX++)
1547      {
1548        UInt uiSample = piPCMSample[uiX];
1549
1550        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1551      }
1552      piPCMSample += uiWidth;
1553    }
1554
1555    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1556    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1557    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1558    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1559
1560    for(uiY = 0; uiY < uiHeight; uiY++)
1561    {
1562      for(uiX = 0; uiX < uiWidth; uiX++)
1563      {
1564        UInt uiSample = piPCMSample[uiX];
1565
1566        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1567      }
1568      piPCMSample += uiWidth;
1569    }
1570
1571    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1572    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1573    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1574    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1575
1576    for(uiY = 0; uiY < uiHeight; uiY++)
1577    {
1578      for(uiX = 0; uiX < uiWidth; uiX++)
1579      {
1580        UInt uiSample = piPCMSample[uiX];
1581
1582        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1583      }
1584      piPCMSample += uiWidth;
1585    }
1586    m_pcBinIf->resetBac();
1587  }
1588}
1589
1590Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1591{
1592  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1593  UInt uiCtx = 0;
1594  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1595#if !H_MV_ENC_DEC_TRAC
1596  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1597  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1598  DTRACE_CABAC_T( "\tsymbol=" )
1599  DTRACE_CABAC_V( uiCbf )
1600  DTRACE_CABAC_T( "\tctx=" )
1601  DTRACE_CABAC_V( uiCtx )
1602  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1603  DTRACE_CABAC_V( uiAbsPartIdx )
1604  DTRACE_CABAC_T( "\n" )
1605#else
1606  DTRACE_CU( "rqt_root_cbf", uiCbf )
1607#endif
1608}
1609
1610Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1611{
1612  // this function is only used to estimate the bits when cbf is 0
1613  // and will never be called when writing the bistream. do not need to write log
1614  UInt uiCbf = 0;
1615  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1616  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1617}
1618
1619Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1620{
1621  // this function is only used to estimate the bits when cbf is 0
1622  // and will never be called when writing the bistream. do not need to write log
1623  UInt uiCbf = 0;
1624  UInt uiCtx = 0;
1625  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1626}
1627
1628/** Encode (X,Y) position of the last significant coefficient
1629 * \param uiPosX X component of last coefficient
1630 * \param uiPosY Y component of last coefficient
1631 * \param width  Block width
1632 * \param height Block height
1633 * \param eTType plane type / luminance or chrominance
1634 * \param uiScanIdx scan type (zig-zag, hor, ver)
1635 * This method encodes the X and Y component within a block of the last significant coefficient.
1636 */
1637Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1638{ 
1639  // swap
1640  if( uiScanIdx == SCAN_VER )
1641  {
1642    swap( uiPosX, uiPosY );
1643  }
1644
1645  UInt uiCtxLast;
1646  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1647  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1648  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1649  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1650
1651
1652  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1653  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1654  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1655  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1656  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1657  // posX
1658  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1659  {
1660      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1661  }
1662  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1663  {
1664      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1665  }
1666
1667  // posY
1668  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1669  {
1670    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1671  }
1672  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1673  {
1674    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1675  }
1676  if ( uiGroupIdxX > 3 )
1677  {     
1678    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1679    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1680    for (Int i = uiCount - 1 ; i >= 0; i-- )
1681    {
1682      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1683    }
1684  }
1685  if ( uiGroupIdxY > 3 )
1686  {     
1687    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1688    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1689    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1690    {
1691      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1692    }
1693  }
1694}
1695
1696Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1697{
1698#if !H_MV_ENC_DEC_TRAC
1699  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1700  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1701  DTRACE_CABAC_V( eTType )
1702  DTRACE_CABAC_T( "\twidth=" )
1703  DTRACE_CABAC_V( uiWidth )
1704  DTRACE_CABAC_T( "\theight=" )
1705  DTRACE_CABAC_V( uiHeight )
1706  DTRACE_CABAC_T( "\tdepth=" )
1707  DTRACE_CABAC_V( uiDepth )
1708  DTRACE_CABAC_T( "\tabspartidx=" )
1709  DTRACE_CABAC_V( uiAbsPartIdx )
1710  DTRACE_CABAC_T( "\ttoCU-X=" )
1711  DTRACE_CABAC_V( pcCU->getCUPelX() )
1712  DTRACE_CABAC_T( "\ttoCU-Y=" )
1713  DTRACE_CABAC_V( pcCU->getCUPelY() )
1714  DTRACE_CABAC_T( "\tCU-addr=" )
1715  DTRACE_CABAC_V(  pcCU->getAddr() )
1716  DTRACE_CABAC_T( "\tinCU-X=" )
1717  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1718  DTRACE_CABAC_T( "\tinCU-Y=" )
1719  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1720  DTRACE_CABAC_T( "\tpredmode=" )
1721  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1722  DTRACE_CABAC_T( "\n" )
1723#endif
1724  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1725  {
1726    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1727    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1728  }
1729 
1730  UInt uiNumSig = 0;
1731 
1732  // compute number of significant coefficients
1733  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1734 
1735  if ( uiNumSig == 0 )
1736    return;
1737  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1738  {
1739    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1740  }
1741  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1742 
1743  //----- encode significance map -----
1744  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1745  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1746  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1747 
1748  Bool beValid;
1749  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1750  {
1751    beValid = false;
1752  }
1753  else 
1754  {
1755    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1756  }
1757
1758  // Find position of last coefficient
1759  Int scanPosLast = -1;
1760  Int posLast;
1761
1762  const UInt * scanCG;
1763  {
1764    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1765    if( uiLog2BlockSize == 3 )
1766    {
1767      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1768    }
1769    else if( uiLog2BlockSize == 5 )
1770    {
1771      scanCG = g_sigLastScanCG32x32;
1772    }
1773  }
1774  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1775  static const UInt uiShift = MLS_CG_SIZE >> 1;
1776  const UInt uiNumBlkSide = uiWidth >> uiShift;
1777
1778    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1779
1780    do
1781    {
1782      posLast = scan[ ++scanPosLast ];
1783
1784      // get L1 sig map
1785      UInt uiPosY    = posLast >> uiLog2BlockSize;
1786      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1787      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1788      if( pcCoef[ posLast ] )
1789      {
1790        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1791      }
1792
1793      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1794    }
1795    while ( uiNumSig > 0 );
1796
1797  // Code position of last coefficient
1798  Int posLastY = posLast >> uiLog2BlockSize;
1799  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1800  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1801 
1802  //===== code significance flag =====
1803  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1804  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1805
1806
1807  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1808  UInt c1 = 1;
1809  UInt uiGoRiceParam           = 0;
1810  Int  iScanPosSig             = scanPosLast;
1811
1812  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1813  {
1814    Int numNonZero = 0;
1815    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1816    uiGoRiceParam    = 0;
1817    Int absCoeff[16];
1818    UInt coeffSigns = 0;
1819
1820    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1821
1822    if( iScanPosSig == scanPosLast )
1823    {
1824      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1825      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1826      numNonZero    = 1;
1827      lastNZPosInCG  = iScanPosSig;
1828      firstNZPosInCG = iScanPosSig;
1829      iScanPosSig--;
1830    }
1831
1832      // encode significant_coeffgroup_flag
1833      Int iCGBlkPos = scanCG[ iSubSet ];
1834      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1835      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1836      if( iSubSet == iLastScanSet || iSubSet == 0)
1837      {
1838        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1839      }
1840      else
1841      {
1842          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1843          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1844          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1845      }
1846     
1847      // encode significant_coeff_flag
1848      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1849      {
1850        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1851        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1852        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1853        {
1854          uiBlkPos  = scan[ iScanPosSig ]; 
1855          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1856          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1857          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1858          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1859          {
1860            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1861            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1862          }
1863          if( uiSig )
1864          {
1865            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1866            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1867            numNonZero++;
1868            if( lastNZPosInCG == -1 )
1869            {
1870              lastNZPosInCG = iScanPosSig;
1871            }
1872            firstNZPosInCG = iScanPosSig;
1873          }
1874        }
1875      }
1876      else
1877      {
1878        iScanPosSig = iSubPos - 1;
1879      }
1880
1881    if( numNonZero > 0 )
1882    {
1883      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1884      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1885     
1886      if( c1 == 0 )
1887      {
1888        uiCtxSet++;
1889      }
1890      c1 = 1;
1891      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1892     
1893      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1894      Int firstC2FlagIdx = -1;
1895      for( Int idx = 0; idx < numC1Flag; idx++ )
1896      {
1897        UInt uiSymbol = absCoeff[ idx ] > 1;
1898        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1899        if( uiSymbol )
1900        {
1901          c1 = 0;
1902
1903          if (firstC2FlagIdx == -1)
1904          {
1905            firstC2FlagIdx = idx;
1906          }
1907        }
1908        else if( (c1 < 3) && (c1 > 0) )
1909        {
1910          c1++;
1911        }
1912      }
1913     
1914      if (c1 == 0)
1915      {
1916
1917        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1918        if ( firstC2FlagIdx != -1)
1919        {
1920          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1921          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1922        }
1923      }
1924     
1925      if( beValid && signHidden )
1926      {
1927        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1928      }
1929      else
1930      {
1931        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1932      }
1933     
1934      Int iFirstCoeff2 = 1;   
1935      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1936      {
1937        for ( Int idx = 0; idx < numNonZero; idx++ )
1938        {
1939          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1940
1941          if( absCoeff[ idx ] >= baseLevel)
1942          {
1943            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1944            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1945            {
1946               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1947            }
1948          }
1949          if(absCoeff[ idx ] >= 2) 
1950          {
1951            iFirstCoeff2 = 0;
1952          }
1953        }       
1954      }
1955    }
1956  }
1957
1958  return;
1959}
1960
1961/** code SAO offset sign
1962 * \param code sign value
1963 */
1964Void TEncSbac::codeSAOSign( UInt code )
1965{
1966  m_pcBinIf->encodeBinEP( code );
1967}
1968
1969Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1970{
1971  if (maxSymbol == 0)
1972  {
1973    return;
1974  }
1975
1976  Int i;
1977  Bool bCodeLast = ( maxSymbol > code );
1978
1979  if ( code == 0 )
1980  {
1981    m_pcBinIf->encodeBinEP( 0 );
1982  }
1983  else
1984  {
1985    m_pcBinIf->encodeBinEP( 1 );
1986    for ( i=0; i<code-1; i++ )
1987    {
1988      m_pcBinIf->encodeBinEP( 1 );
1989    }
1990    if( bCodeLast )
1991    {
1992      m_pcBinIf->encodeBinEP( 0 );
1993    }
1994  }
1995}
1996
1997
1998/** Code SAO EO class or BO band position
1999 * \param uiLength
2000 * \param uiCode
2001 */
2002Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
2003{
2004   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
2005}
2006/** Code SAO merge flags
2007 * \param uiCode
2008 * \param uiCompIdx
2009 */
2010Void TEncSbac::codeSaoMerge       ( UInt uiCode )
2011{
2012  if (uiCode == 0)
2013  {
2014    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2015  }
2016  else
2017  {
2018    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
2019  }
2020}
2021/** Code SAO type index
2022 * \param uiCode
2023 */
2024Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
2025{
2026  if (uiCode == 0)
2027  {
2028    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2029  }
2030  else
2031  {
2032    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2033    m_pcBinIf->encodeBinEP( uiCode <= 4 ? 1 : 0 );
2034  }
2035}
2036/*!
2037 ****************************************************************************
2038 * \brief
2039 *   estimate bit cost for CBP, significant map and significant coefficients
2040 ****************************************************************************
2041 */
2042Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2043{
2044  estCBFBit( pcEstBitsSbac );
2045
2046  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
2047 
2048  // encode significance map
2049  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
2050 
2051  // encode significant coefficients
2052  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
2053}
2054
2055/*!
2056 ****************************************************************************
2057 * \brief
2058 *    estimate bit cost for each CBP bit
2059 ****************************************************************************
2060 */
2061Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
2062{
2063  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2064
2065  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2066  {
2067    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2068    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2069  }
2070
2071  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2072 
2073  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2074  {
2075    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2076    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2077  }
2078}
2079
2080
2081/*!
2082 ****************************************************************************
2083 * \brief
2084 *    estimate SAMBAC bit cost for significant coefficient group map
2085 ****************************************************************************
2086 */
2087Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2088{
2089  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2090
2091  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2092  {
2093    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2094    {
2095      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2096    }
2097  }
2098}
2099
2100
2101/*!
2102 ****************************************************************************
2103 * \brief
2104 *    estimate SAMBAC bit cost for significant coefficient map
2105 ****************************************************************************
2106 */
2107Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2108{
2109  Int firstCtx = 1, numCtx = 8;
2110  if (max(width, height) >= 16)
2111  {
2112    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2113    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2114  }
2115  else if (width == 8)
2116  {
2117    firstCtx = 9;
2118    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2119  }
2120 
2121  if (eTType == TEXT_LUMA )
2122  {
2123    for( UInt bin = 0; bin < 2; bin++ )
2124    {
2125      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2126    }
2127
2128    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2129    {
2130      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2131      {
2132        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2133      }
2134    }
2135  }
2136  else
2137  {
2138    for( UInt bin = 0; bin < 2; bin++ )
2139    {
2140      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2141    }
2142    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2143    {
2144      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2145      {
2146        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2147      }
2148    }
2149  }
2150  Int iBitsX = 0, iBitsY = 0;
2151  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2152
2153  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2154  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2155  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2156  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2157
2158  Int ctx;
2159  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2160  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2161  {
2162    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2163    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2164    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2165  }
2166  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2167  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2168  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2169  {
2170    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2171    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2172    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2173  }
2174  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2175}
2176
2177/*!
2178 ****************************************************************************
2179 * \brief
2180 *    estimate bit cost of significant coefficient
2181 ****************************************************************************
2182 */
2183Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2184{
2185  if (eTType==TEXT_LUMA)
2186  {
2187    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2188    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2189
2190    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2191    {
2192      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2193      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2194    }
2195
2196    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2197    {
2198      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2199      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2200    }
2201  }
2202  else
2203  {
2204    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2205    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2206
2207    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2208    {
2209      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2210      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2211    }
2212
2213    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2214    {
2215      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2216      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2217    }
2218  }
2219}
2220
2221/**
2222 - Initialize our context information from the nominated source.
2223 .
2224 \param pSrc From where to copy context information.
2225 */
2226Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2227{ 
2228  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2229}
2230
2231Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2232{
2233  this->xCopyContextsFrom(pScr);
2234}
2235
2236#if H_3D_INTER_SDC
2237Void TEncSbac::codeInterSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2238{
2239  UInt uiSymbol = pcCU->getInterSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2240  UInt uiCtxInterSDCFlag = pcCU->getCtxInterSDCFlag( uiAbsPartIdx );
2241
2242  m_pcBinIf->encodeBin( uiSymbol, m_cInterSDCFlagSCModel.get( 0, 0, uiCtxInterSDCFlag ) );
2243}
2244
2245Void TEncSbac::codeInterSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
2246{
2247  Pel segmentDCOffset = pcCU->getInterSDCSegmentDCOffset( uiSegment, uiAbsPartIdx );
2248
2249  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
2250  UInt uiAbsIdx   = abs( segmentDCOffset );
2251
2252  assert( uiAbsIdx > 0 );
2253  uiAbsIdx--;
2254  xWriteExGolombLevel( uiAbsIdx, m_cInterSDCResidualSCModel.get( 0, 0, 0 ) );
2255  m_pcBinIf->encodeBin( uiSign, m_cInterSDCResidualSignFlagSCModel.get( 0, 0, 0 ) );
2256}
2257#endif
2258//! \}
Note: See TracBrowser for help on using the repository browser.