source: 3DVCSoftware/branches/HTM-10.2-dev3-LGE/source/Lib/TLibEncoder/TEncSbac.cpp @ 924

Last change on this file since 924 was 924, checked in by lg, 10 years ago

JCT3V-H0119/JCT3V-H0135

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