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

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

Merged 12.0-dev1@1065.

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