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

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

H0092 integration

  • Property svn:eol-style set to native
File size: 77.2 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6* Copyright (c) 2010-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    Bool bNoExGo = (uiSymbol < 13);
485
486    while( --uiSymbol && ++uiCount < 13 )
487    {
488      m_pcBinIf->encodeBin( 1, rcSCModel );
489    }
490    if( bNoExGo )
491    {
492      m_pcBinIf->encodeBin( 0, rcSCModel );
493    }
494    else
495    {
496      xWriteEpExGolomb( uiSymbol, 0 );
497    }
498  }
499  else
500  {
501    m_pcBinIf->encodeBin( 0, rcSCModel );
502  }
503
504  return;
505}
506
507Void TEncSbac::xCodeDimDeltaDC( Pel valDeltaDC, UInt uiNumSeg )
508{
509  xWriteExGolombLevel( UInt( abs( valDeltaDC ) - ( uiNumSeg > 1 ? 0 : 1 ) ), m_cDdcDataSCModel.get(0, 0, 0) );
510  if( valDeltaDC != 0 )
511  {
512    UInt uiSign = valDeltaDC > 0 ? 0 : 1;
513    m_pcBinIf->encodeBinEP( uiSign );
514  }
515}
516
517#if H_3D_DIM_DMM
518Void TEncSbac::xCodeDmm1WedgeIdx( UInt uiTabIdx, Int iNumBit )
519{
520  for ( Int i = 0; i < iNumBit; i++ )
521  {
522#if MTK_DMM_SIMP_CODE_H0092
523      m_pcBinIf->encodeBinEP( ( uiTabIdx >> i ) & 1 );
524#else
525      m_pcBinIf->encodeBin( ( uiTabIdx >> i ) & 1, m_cDmm1DataSCModel.get(0, 0, 0) );
526#endif
527  }
528}
529
530#endif
531
532#if H_3D_DIM_SDC
533Void TEncSbac::xCodeSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiSegment )
534{
535  assert( pcCU->getSlice()->getIsDepth() );
536  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
537  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
538  assert( uiSegment < 2 );
539 
540  Pel segmentDCOffset = pcCU->getSDCSegmentDCOffset(uiSegment, uiAbsPartIdx);
541 
542  UInt uiResidual = segmentDCOffset == 0 ? 0 : 1;
543  UInt uiSign     = segmentDCOffset < 0 ? 1 : 0;
544  UInt uiAbsIdx   = abs(segmentDCOffset);
545
546#if H_3D_DIM_DLT
547  UInt uiMaxResidualBits = pcCU->getSlice()->getPPS()->getDLT()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
548#else
549  UInt uiMaxResidualBits = g_bitDepthY;
550#endif
551  assert( uiMaxResidualBits <= g_bitDepthY );
552 
553  // residual flag
554  m_pcBinIf->encodeBin( uiResidual, m_cSDCResidualFlagSCModel.get( 0, 0, 0 ) );
555#if H_MV_ENC_DEC_TRAC
556  DTRACE_CU("sdc_residual_flag[i]", uiResidual)
557#endif 
558 
559  if (uiResidual)
560  {
561    // encode sign bit of residual
562    m_pcBinIf->encodeBinEP( uiSign );
563#if H_MV_ENC_DEC_TRAC
564    DTRACE_CU("sdc_residual_sign_flag[i]", uiSign)
565#endif
566#if H_3D_DIM_DLT   
567    UInt uiNumDepthValues = pcCU->getSlice()->getPPS()->getDLT()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
568#else
569    UInt uiNumDepthValues = ((1 << g_bitDepthY)-1);
570#endif
571
572    assert(uiAbsIdx <= uiNumDepthValues);
573   
574    // encode residual magnitude
575    uiAbsIdx -= 1;
576
577#if H_MV_ENC_DEC_TRAC
578    DTRACE_CU("sdc_residual_abs_minus1[i]", uiAbsIdx)
579#endif
580
581    // prefix part
582    if ( uiAbsIdx == 0 )
583    {
584      m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
585    }
586    else
587    {
588      UInt l = uiAbsIdx;
589      UInt k = 0;
590      UInt uiPrefixThreshold = ((uiNumDepthValues * 3) >> 2);
591      while ( l > 0 && k < uiPrefixThreshold )
592      {
593        m_pcBinIf->encodeBin( 1, m_cSDCResidualSCModel.get(0, 0, 0) );
594        l--;
595        k++;
596      }
597      if ( uiAbsIdx < uiPrefixThreshold )
598      {
599        m_pcBinIf->encodeBin( 0, m_cSDCResidualSCModel.get(0, 0, 0) );
600      }     
601      else
602      { // suffix part
603        uiAbsIdx -= uiPrefixThreshold;
604        UInt uiSuffixLength = numBitsForValue(uiNumDepthValues - uiPrefixThreshold);
605        UInt uiBitInfo = 0;
606        for ( Int i = 0; i < uiSuffixLength; i++)
607        {
608          uiBitInfo = ( uiAbsIdx & ( 1 << i ) ) >> i;
609          m_pcBinIf->encodeBinEP( uiBitInfo);
610        }
611      }
612    }
613   
614  }
615}
616#endif
617#endif
618
619// SBAC RD
620Void  TEncSbac::load ( TEncSbac* pSrc)
621{
622  this->xCopyFrom(pSrc);
623}
624
625Void  TEncSbac::loadIntraDirModeLuma( TEncSbac* pSrc)
626{
627  m_pcBinIf->copyState( pSrc->m_pcBinIf );
628 
629  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
630}
631#if H_3D_DIM
632Void TEncSbac::loadIntraDepthMode( TEncSbac* pSrc)
633{
634  m_pcBinIf->copyState( pSrc->m_pcBinIf );
635
636  this->m_cDepthIntraModeSCModel .copyFrom( &pSrc->m_cDepthIntraModeSCModel );
637  this->m_cDdcFlagSCModel        .copyFrom( &pSrc->m_cDdcFlagSCModel );
638  this->m_cAngleFlagSCModel      .copyFrom( &pSrc->m_cAngleFlagSCModel );
639}
640#endif
641
642Void  TEncSbac::store( TEncSbac* pDest)
643{
644  pDest->xCopyFrom( this );
645}
646
647
648Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
649{
650  m_pcBinIf->copyState( pSrc->m_pcBinIf );
651 
652  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
653  this->m_uiLastQp    = pSrc->m_uiLastQp;
654 
655  memcpy( m_contextModels, pSrc->m_contextModels, m_numContextModels * sizeof( ContextModel ) );
656}
657
658Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
659{
660  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
661  Int iNum = AMVP_MAX_NUM_CANDS;
662
663  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
664#if H_MV_ENC_DEC_TRAC
665#if ENC_DEC_TRACE
666  if ( eRefList == REF_PIC_LIST_0 )
667  {
668    DTRACE_PU("mvp_l0_flag", iSymbol)
669  }
670  else
671  {
672    DTRACE_PU("mvp_l1_flag", iSymbol)
673  }
674#endif
675#endif
676}
677
678Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
679{
680  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
681#if H_3D_QTLPC
682  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
683  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
684  Bool bDepthMapDetect   = (pcTexture != NULL);
685  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
686 
687  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);
688
689  Bool depthDependent = false;
690  UInt uiTexturePart = eSize;
691  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC() )
692  {
693    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
694    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
695    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
696    if(pcTextureCU->getDepth(uiCUIdx) == uiDepth )
697    {
698      depthDependent = true;
699      uiTexturePart = pcTextureCU->getPartitionSize( uiCUIdx );
700    }
701    if (pcTextureCU->getDepth(uiCUIdx) == uiDepth && pcTextureCU->getPartitionSize( uiCUIdx ) == SIZE_2Nx2N)
702    {
703      assert( eSize == SIZE_2Nx2N );
704      return;
705    }
706  }
707#endif
708  if ( pcCU->isIntra( uiAbsPartIdx ) )
709  {
710    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
711    {
712      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
713#if H_MV_ENC_DEC_TRAC
714      DTRACE_CU("part_mode", eSize == SIZE_2Nx2N? 1 : 0)
715#endif       
716    }
717    return;
718  }
719 
720#if H_MV_ENC_DEC_TRAC         
721  DTRACE_CU("part_mode", eSize )
722#endif       
723#if H_3D_QTLPC
724    if (depthDependent==false || uiTexturePart == SIZE_NxN|| uiTexturePart == SIZE_2Nx2N)
725    {
726#endif
727  switch(eSize)
728  {
729    case SIZE_2Nx2N:
730    {
731      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
732      break;
733    }
734    case SIZE_2NxN:
735    case SIZE_2NxnU:
736    case SIZE_2NxnD:
737    {
738      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
739      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
740      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
741      {
742        if (eSize == SIZE_2NxN)
743        {
744          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
745        }
746        else
747        {
748          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
749          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
750        }
751      }
752      break;
753    }
754    case SIZE_Nx2N:
755    case SIZE_nLx2N:
756    case SIZE_nRx2N:
757    {
758      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
759      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
760      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
761      {
762        m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
763      }
764      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
765      {
766        if (eSize == SIZE_Nx2N)
767        {
768          m_pcBinIf->encodeBin(1, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
769        }
770        else
771        {
772          m_pcBinIf->encodeBin(0, m_cCUPartSizeSCModel.get( 0, 0, 3 ));
773          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
774        }
775      }
776      break;
777    }
778    case SIZE_NxN:
779    {
780      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( pcCU->getWidth(uiAbsPartIdx) == 8 && pcCU->getHeight(uiAbsPartIdx) == 8 ) )
781      {
782        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
783        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
784        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
785      }
786      break;
787    }
788    default:
789    {
790      assert(0);
791    }
792  }
793#if H_3D_QTLPC
794    }
795    else if(uiTexturePart == SIZE_2NxN || uiTexturePart == SIZE_2NxnU || uiTexturePart == SIZE_2NxnD)
796    {
797      //assert(eSize!=SIZE_NxN);
798      //assert(eSize!=SIZE_Nx2N);
799      //assert(eSize==SIZE_2Nx2N || eSize==SIZE_2NxN || eSize==SIZE_2NxnU || eSize==SIZE_2NxnD);
800      switch(eSize)
801      {
802      case SIZE_2Nx2N:
803        {
804          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
805          break;
806        }
807      case SIZE_2NxN:
808        {
809          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
810          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
811          {     
812            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
813          }
814          break;
815        }
816      case SIZE_2NxnU:
817      case SIZE_2NxnD:
818        {
819          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
820          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
821          m_pcBinIf->encodeBinEP((eSize == SIZE_2NxnU? 0: 1));
822          break;
823        }
824      default:
825        {
826          assert(0);
827        }
828      }
829    }
830    else if(uiTexturePart == SIZE_Nx2N|| uiTexturePart==SIZE_nLx2N || uiTexturePart==SIZE_nRx2N)
831    {
832      //assert(eSize!=SIZE_NxN);
833      //assert(eSize!=SIZE_2NxN);
834      //assert(eSize==SIZE_2Nx2N ||eSize==SIZE_Nx2N || eSize==SIZE_nLx2N || eSize==SIZE_nRx2N);
835      switch(eSize)
836      {
837      case SIZE_2Nx2N:
838        {
839          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
840          break;
841        }
842      case SIZE_Nx2N:
843        {
844          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
845          if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
846          {     
847            m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
848          }
849          break;
850        }
851      case SIZE_nLx2N:
852      case SIZE_nRx2N:
853        {
854          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
855          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
856          m_pcBinIf->encodeBinEP((eSize == SIZE_nLx2N? 0: 1));
857          break;
858        }
859      default:
860        {
861          assert(0);
862        }
863      }
864    }
865    else
866    {
867      printf("uiTexturePart=%d",uiTexturePart);
868      assert(0);
869    }
870#endif
871}
872
873/** code prediction mode
874 * \param pcCU
875 * \param uiAbsPartIdx 
876 * \returns Void
877 */
878Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
879{
880#if H_3D_DIM_SDC
881  if ( pcCU->getSlice()->isIntra() )
882  {
883    assert( pcCU->isIntra(uiAbsPartIdx) );
884    return;
885  }
886#endif
887 
888  // get context function is here
889  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
890  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
891#if H_MV_ENC_DEC_TRAC
892  DTRACE_CU("pred_mode_flag", iPredMode == MODE_INTER ? 0 : 1); 
893#endif
894}
895
896Void TEncSbac::codeCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
897{
898  UInt uiSymbol = pcCU->getCUTransquantBypass(uiAbsPartIdx);
899  m_pcBinIf->encodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
900#if H_MV_ENC_DEC_TRAC
901  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
902#endif
903}
904
905/** code skip flag
906 * \param pcCU
907 * \param uiAbsPartIdx
908 * \returns Void
909 */
910Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
911{
912  // get context function is here
913  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
914  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
915  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
916#if !H_MV_ENC_DEC_TRAC
917  DTRACE_CABAC_VL( g_nSymbolCounter++ );
918  DTRACE_CABAC_T( "\tSkipFlag" );
919  DTRACE_CABAC_T( "\tuiCtxSkip: ");
920  DTRACE_CABAC_V( uiCtxSkip );
921  DTRACE_CABAC_T( "\tuiSymbol: ");
922  DTRACE_CABAC_V( uiSymbol );
923  DTRACE_CABAC_T( "\n");
924#else
925  DTRACE_CU("cu_skip_flag", uiSymbol); 
926#endif
927}
928
929/** code merge flag
930 * \param pcCU
931 * \param uiAbsPartIdx
932 * \returns Void
933 */
934Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
935{
936  const UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
937  m_pcBinIf->encodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
938
939#if H_MV_ENC_DEC_TRAC
940  DTRACE_PU("merge_flag", uiSymbol); 
941#else
942  DTRACE_CABAC_VL( g_nSymbolCounter++ );
943  DTRACE_CABAC_T( "\tMergeFlag: " );
944  DTRACE_CABAC_V( uiSymbol );
945  DTRACE_CABAC_T( "\tAddress: " );
946  DTRACE_CABAC_V( pcCU->getAddr() );
947  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
948  DTRACE_CABAC_V( uiAbsPartIdx );
949  DTRACE_CABAC_T( "\n" );
950#endif
951}
952
953/** code merge index
954 * \param pcCU
955 * \param uiAbsPartIdx
956 * \returns Void
957 */
958Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
959{
960  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
961  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
962  if ( uiNumCand > 1 )
963  {
964    for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
965    {
966      const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
967      if ( ui==0 )
968      {
969        m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
970      }
971      else
972      {
973        m_pcBinIf->encodeBinEP( uiSymbol );
974      }
975      if( uiSymbol == 0 )
976      {
977        break;
978      }
979    }
980#if H_MV_ENC_DEC_TRAC
981    DTRACE_PU("merge_idx", uiUnaryIdx); 
982#endif
983  }
984#if !H_MV_ENC_DEC_TRAC
985  DTRACE_CABAC_VL( g_nSymbolCounter++ );
986  DTRACE_CABAC_T( "\tparseMergeIndex()" );
987  DTRACE_CABAC_T( "\tuiMRGIdx= " );
988  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
989  DTRACE_CABAC_T( "\n" );
990#endif
991}
992
993#if H_3D_ARP
994Void TEncSbac::codeARPW( TComDataCU* pcCU, UInt uiAbsPartIdx )
995{
996  Int  iW = (Int)pcCU->getARPW( uiAbsPartIdx );
997  Int  iMaxW = pcCU->getSlice()->getARPStepNum() - 1;
998  assert( iMaxW > 0);
999
1000  Int nOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
1001  Int nBinNum = iW + ( iW != iMaxW );
1002  m_pcBinIf->encodeBin( iW ? 1 : 0 , m_cCUPUARPWSCModel.get( 0, 0, 0 + nOffset ) );
1003  if( nBinNum > 1 )
1004  {
1005     m_pcBinIf->encodeBin( ( iW == iMaxW ) ? 1 : 0, m_cCUPUARPWSCModel.get( 0, 0, 2 ) );
1006  }
1007#if H_MV_ENC_DEC_TRAC
1008  DTRACE_CU("iv_res_pred_weight_idx", iW); 
1009#endif
1010}
1011#endif
1012
1013#if H_3D_IC
1014/** code Illumination Compensation flag
1015 * \param pcCU
1016 * \param uiAbsPartIdx
1017 * \returns Void
1018 */
1019Void TEncSbac::codeICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
1020{
1021  // get context function is here
1022  UInt uiSymbol = pcCU->getICFlag( uiAbsPartIdx ) ? 1 : 0;
1023  m_pcBinIf->encodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) );
1024#if !H_MV_ENC_DEC_TRAC
1025  DTRACE_CABAC_VL( g_nSymbolCounter++ );
1026  DTRACE_CABAC_T( "\tICFlag" );
1027  DTRACE_CABAC_T( "\tuiSymbol: ");
1028  DTRACE_CABAC_V( uiSymbol );
1029  DTRACE_CABAC_T( "\n");
1030#else
1031  DTRACE_CU("ic_flag", uiSymbol ); 
1032#endif
1033}
1034#endif
1035
1036
1037Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1038{
1039  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
1040    return;
1041 
1042  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
1043  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
1044 
1045  assert( uiCtx < 3 );
1046#if H_3D_QTLPC
1047  Bool bCodeSplitFlag    = true;
1048
1049  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
1050  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
1051  Bool bDepthMapDetect   = (pcTexture != NULL);
1052  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
1053
1054  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);
1055
1056  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && pcCU->getPic()->getReduceBitsFlag() && sps->getUseQTL() && sps->getUsePC())
1057  {
1058    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
1059    UInt uiCUIdx            = (pcCU->getZorderIdxInCU() == 0) ? uiAbsPartIdx : pcCU->getZorderIdxInCU();
1060    assert(pcTextureCU->getDepth(uiCUIdx) >= uiDepth);
1061    bCodeSplitFlag          = (pcTextureCU->getDepth(uiCUIdx) > uiDepth);
1062  }
1063
1064  if(!bCodeSplitFlag)
1065  {
1066    assert(uiCurrSplitFlag == 0);
1067    return;
1068  }
1069#endif
1070  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
1071#if !H_MV_ENC_DEC_TRAC
1072  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1073  DTRACE_CABAC_T( "\tSplitFlag\n" )
1074#else
1075  DTRACE_CU("split_cu_flag", uiCurrSplitFlag ); 
1076#endif
1077  return;
1078}
1079
1080Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
1081{
1082  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
1083#if !H_MV_ENC_DEC_TRAC
1084  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1085  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1086  DTRACE_CABAC_T( "\tsymbol=" )
1087  DTRACE_CABAC_V( uiSymbol )
1088  DTRACE_CABAC_T( "\tctx=" )
1089  DTRACE_CABAC_V( uiCtx )
1090  DTRACE_CABAC_T( "\n" )
1091#endif
1092}
1093
1094Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt absPartIdx, Bool isMultiple)
1095{
1096  UInt dir[4],j;
1097  Int preds[4][3] = {{-1, -1, -1},{-1, -1, -1},{-1, -1, -1},{-1, -1, -1}};
1098  Int predNum[4], predIdx[4] ={ -1,-1,-1,-1};
1099  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1100  UInt partNum = isMultiple?(mode==SIZE_NxN?4:1):1;
1101  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1102  for (j=0;j<partNum;j++)
1103  {
1104    dir[j] = pcCU->getLumaIntraDir( absPartIdx+partOffset*j );
1105#if H_3D_DIM
1106    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1107    {
1108      codeIntraDepth( pcCU, absPartIdx+partOffset*j );
1109    }
1110    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1111    {
1112#endif
1113      predNum[j] = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds[j]); 
1114      for(UInt i = 0; i < predNum[j]; i++)
1115      {
1116        if(dir[j] == preds[j][i])
1117        {
1118          predIdx[j] = i;
1119        }
1120      }
1121      m_pcBinIf->encodeBin((predIdx[j] != -1)? 1 : 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1122#if H_MV_ENC_DEC_TRAC
1123      DTRACE_CU("prev_intra_luma_pred_flag", (predIdx[j] != -1)? 1 : 0); 
1124#endif
1125#if H_3D_DIM
1126    }
1127#endif
1128
1129  } 
1130  for (j=0;j<partNum;j++)
1131  {
1132#if H_3D_DIM
1133    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1134    {
1135#endif
1136    if(predIdx[j] != -1)
1137    {
1138      m_pcBinIf->encodeBinEP( predIdx[j] ? 1 : 0 );
1139      if (predIdx[j])
1140      {
1141        m_pcBinIf->encodeBinEP( predIdx[j]-1 );
1142      }
1143#if H_MV_ENC_DEC_TRAC
1144      DTRACE_CU("mpm_idx", predIdx[j] ); 
1145#endif
1146    }
1147    else
1148    {
1149      if (preds[j][0] > preds[j][1])
1150      { 
1151        std::swap(preds[j][0], preds[j][1]); 
1152      }
1153      if (preds[j][0] > preds[j][2])
1154      {
1155        std::swap(preds[j][0], preds[j][2]);
1156      }
1157      if (preds[j][1] > preds[j][2])
1158      {
1159        std::swap(preds[j][1], preds[j][2]);
1160      }
1161      for(Int i = (predNum[j] - 1); i >= 0; i--)
1162      {
1163        dir[j] = dir[j] > preds[j][i] ? dir[j] - 1 : dir[j];
1164      }
1165      m_pcBinIf->encodeBinsEP( dir[j], 5 );
1166#if H_MV_ENC_DEC_TRAC
1167      DTRACE_CU("rem_intra_luma_pred_mode", dir[j] ); 
1168#endif
1169    }
1170#if H_3D_DIM
1171    }
1172#endif
1173  }
1174  return;
1175}
1176
1177Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1178{
1179  UInt uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1180
1181  if( uiIntraDirChroma == DM_CHROMA_IDX ) 
1182  {
1183    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1184#if H_MV_ENC_DEC_TRAC
1185    DTRACE_CU("intra_chroma_pred_mode", 0 ); 
1186#endif
1187  }
1188  else
1189  { 
1190    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1191    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1192
1193    for( Int i = 0; i < NUM_CHROMA_MODE - 1; i++ )
1194    {
1195      if( uiIntraDirChroma == uiAllowedChromaDir[i] )
1196      {
1197        uiIntraDirChroma = i;
1198        break;
1199      }
1200    }
1201    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1202
1203    m_pcBinIf->encodeBinsEP( uiIntraDirChroma, 2 );
1204#if H_MV_ENC_DEC_TRAC
1205    DTRACE_CU("intra_chroma_pred_mode", uiIntraDirChroma ); 
1206#endif
1207
1208  }
1209  return;
1210}
1211
1212#if H_3D_DIM
1213Void TEncSbac::codeIntraDepth( TComDataCU* pcCU, UInt absPartIdx )
1214{
1215  codeIntraDepthMode( pcCU, absPartIdx );
1216
1217  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1218  UInt dimType = getDimType( dir );
1219
1220  switch( dimType )
1221  {
1222#if H_3D_DIM_DMM
1223  case( DMM1_IDX ):
1224    {
1225      xCodeDmm1WedgeIdx( pcCU->getDmmWedgeTabIdx( dimType, absPartIdx ), g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1226    } break;
1227  case( DMM4_IDX ): break;
1228#endif
1229  default: break;
1230  }
1231}
1232
1233Void TEncSbac::codeIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx )
1234{
1235  UInt dir = pcCU->getLumaIntraDir( absPartIdx );
1236
1237  if( ( pcCU->getSlice()->getSPS()->getMaxCUWidth() >> pcCU->getDepth( absPartIdx ) ) < 64 ) //DMM and HEVC intra modes are both allowed
1238  {
1239    m_pcBinIf->encodeBin( isDimMode( dir ) ? 0 : 1, m_cAngleFlagSCModel.get( 0, 0, pcCU->getCtxAngleFlag( absPartIdx ) ) );
1240  }
1241  if( isDimMode( dir ) )
1242  {
1243    UInt uiCodeIdx = 0;
1244
1245    switch( getDimType( dir ) )
1246    {
1247    case DMM1_IDX: uiCodeIdx = 0; break;
1248    case DMM4_IDX: uiCodeIdx = 1; break;
1249    default:                      break;
1250    }
1251    //mode coding
1252    m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1253  }
1254}
1255#endif
1256
1257
1258Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1259{
1260  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1261  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1262  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1263  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1264  {
1265    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1266  }
1267  if (uiInterDir < 2)
1268  {
1269    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1270  }
1271#if H_MV_ENC_DEC_TRAC
1272  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1273#endif
1274
1275  return;
1276}
1277
1278Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1279{
1280  {
1281    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1282    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1283    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1284   
1285    if( iRefFrame > 0 )
1286    {
1287      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1288      pCtx++;
1289      iRefFrame--;
1290      for( UInt ui = 0; ui < uiRefNum; ++ui )
1291      {
1292        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1293        if( ui == 0 )
1294        {
1295          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1296        }
1297        else
1298        {
1299          m_pcBinIf->encodeBinEP( uiSymbol );
1300        }
1301        if( uiSymbol == 0 )
1302        {
1303          break;
1304        }
1305      }
1306    }
1307#if H_MV_ENC_DEC_TRAC
1308#if ENC_DEC_TRACE
1309    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1310    if ( eRefList == REF_PIC_LIST_0 )
1311    {
1312      DTRACE_PU("ref_idx_l0", iRefFrame)
1313    }
1314    else
1315    {
1316      DTRACE_PU("ref_idx_l1", iRefFrame)
1317    }
1318#endif
1319#endif
1320  }
1321  return;
1322}
1323
1324Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1325{
1326  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1327  {
1328    return;
1329  }
1330
1331  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1332  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1333  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1334  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1335
1336  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1337  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1338
1339  const Bool bHorAbsGr0 = iHor != 0;
1340  const Bool bVerAbsGr0 = iVer != 0;
1341  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1342  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1343  pCtx++;
1344
1345  if( bHorAbsGr0 )
1346  {
1347    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1348  }
1349
1350  if( bVerAbsGr0 )
1351  {
1352    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1353  }
1354
1355  if( bHorAbsGr0 )
1356  {
1357    if( uiHorAbs > 1 )
1358    {
1359      xWriteEpExGolomb( uiHorAbs-2, 1 );
1360    }
1361
1362    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1363  }
1364
1365  if( bVerAbsGr0 )
1366  {
1367    if( uiVerAbs > 1 )
1368    {
1369      xWriteEpExGolomb( uiVerAbs-2, 1 );
1370    }
1371
1372    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1373  }
1374 
1375  return;
1376}
1377
1378Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1379{
1380  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1381 
1382  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1383  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1384
1385  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1386  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1387  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1388  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1389  {
1390    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1391  }
1392
1393  if ( uiAbsDQp > 0)
1394  {
1395    UInt uiSign = (iDQp > 0 ? 0 : 1);
1396    m_pcBinIf->encodeBinEP(uiSign);
1397  }
1398
1399  return;
1400}
1401
1402Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1403{
1404  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1405  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1406  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1407#if !H_MV_ENC_DEC_TRAC
1408  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1409  DTRACE_CABAC_T( "\tparseQtCbf()" )
1410  DTRACE_CABAC_T( "\tsymbol=" )
1411  DTRACE_CABAC_V( uiCbf )
1412  DTRACE_CABAC_T( "\tctx=" )
1413  DTRACE_CABAC_V( uiCtx )
1414  DTRACE_CABAC_T( "\tetype=" )
1415  DTRACE_CABAC_V( eType )
1416  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1417  DTRACE_CABAC_V( uiAbsPartIdx )
1418  DTRACE_CABAC_T( "\n" )
1419#endif
1420}
1421
1422void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1423{
1424  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1425  {
1426    return;
1427  }
1428  if(width != 4 || height != 4)
1429  {
1430    return;
1431  }
1432
1433  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1434  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1435#if !H_MV_ENC_DEC_TRAC
1436  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1437  DTRACE_CABAC_T("\tparseTransformSkip()");
1438  DTRACE_CABAC_T( "\tsymbol=" )
1439  DTRACE_CABAC_V( useTransformSkip )
1440  DTRACE_CABAC_T( "\tAddr=" )
1441  DTRACE_CABAC_V( pcCU->getAddr() )
1442  DTRACE_CABAC_T( "\tetype=" )
1443  DTRACE_CABAC_V( eTType )
1444  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1445  DTRACE_CABAC_V( uiAbsPartIdx )
1446  DTRACE_CABAC_T( "\n" )
1447#endif
1448}
1449
1450/** Code I_PCM information.
1451 * \param pcCU pointer to CU
1452 * \param uiAbsPartIdx CU index
1453 * \returns Void
1454 */
1455Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1456{
1457  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1458
1459  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1460
1461  m_pcBinIf->encodeBinTrm (uiIPCM);
1462#if H_MV_ENC_DEC_TRAC         
1463  DTRACE_CU("pcm_flag", uiIPCM)
1464#endif
1465  if (writePCMSampleFlag)
1466  {
1467    m_pcBinIf->encodePCMAlignBits();
1468
1469    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1470    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1471    UInt uiChromaOffset = uiLumaOffset>>2;
1472    Pel* piPCMSample;
1473    UInt uiWidth;
1474    UInt uiHeight;
1475    UInt uiSampleBits;
1476    UInt uiX, uiY;
1477
1478    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1479    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1480    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1481    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1482
1483    for(uiY = 0; uiY < uiHeight; uiY++)
1484    {
1485      for(uiX = 0; uiX < uiWidth; uiX++)
1486      {
1487        UInt uiSample = piPCMSample[uiX];
1488
1489        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1490      }
1491      piPCMSample += uiWidth;
1492    }
1493
1494    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1495    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1496    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1497    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1498
1499    for(uiY = 0; uiY < uiHeight; uiY++)
1500    {
1501      for(uiX = 0; uiX < uiWidth; uiX++)
1502      {
1503        UInt uiSample = piPCMSample[uiX];
1504
1505        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1506      }
1507      piPCMSample += uiWidth;
1508    }
1509
1510    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1511    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1512    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1513    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1514
1515    for(uiY = 0; uiY < uiHeight; uiY++)
1516    {
1517      for(uiX = 0; uiX < uiWidth; uiX++)
1518      {
1519        UInt uiSample = piPCMSample[uiX];
1520
1521        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1522      }
1523      piPCMSample += uiWidth;
1524    }
1525    m_pcBinIf->resetBac();
1526  }
1527}
1528
1529Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1530{
1531  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1532  UInt uiCtx = 0;
1533  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1534#if !H_MV_ENC_DEC_TRAC
1535  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1536  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1537  DTRACE_CABAC_T( "\tsymbol=" )
1538  DTRACE_CABAC_V( uiCbf )
1539  DTRACE_CABAC_T( "\tctx=" )
1540  DTRACE_CABAC_V( uiCtx )
1541  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1542  DTRACE_CABAC_V( uiAbsPartIdx )
1543  DTRACE_CABAC_T( "\n" )
1544#else
1545  DTRACE_CU( "rqt_root_cbf", uiCbf )
1546#endif
1547}
1548
1549Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1550{
1551  // this function is only used to estimate the bits when cbf is 0
1552  // and will never be called when writing the bistream. do not need to write log
1553  UInt uiCbf = 0;
1554  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1555  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1556}
1557
1558Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1559{
1560  // this function is only used to estimate the bits when cbf is 0
1561  // and will never be called when writing the bistream. do not need to write log
1562  UInt uiCbf = 0;
1563  UInt uiCtx = 0;
1564  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1565}
1566
1567/** Encode (X,Y) position of the last significant coefficient
1568 * \param uiPosX X component of last coefficient
1569 * \param uiPosY Y component of last coefficient
1570 * \param width  Block width
1571 * \param height Block height
1572 * \param eTType plane type / luminance or chrominance
1573 * \param uiScanIdx scan type (zig-zag, hor, ver)
1574 * This method encodes the X and Y component within a block of the last significant coefficient.
1575 */
1576Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1577{ 
1578  // swap
1579  if( uiScanIdx == SCAN_VER )
1580  {
1581    swap( uiPosX, uiPosY );
1582  }
1583
1584  UInt uiCtxLast;
1585  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1586  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1587  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1588  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1589
1590
1591  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1592  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1593  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1594  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1595  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1596  // posX
1597  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1598  {
1599      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1600  }
1601  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1602  {
1603      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1604  }
1605
1606  // posY
1607  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1608  {
1609    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1610  }
1611  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1612  {
1613    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1614  }
1615  if ( uiGroupIdxX > 3 )
1616  {     
1617    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1618    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1619    for (Int i = uiCount - 1 ; i >= 0; i-- )
1620    {
1621      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1622    }
1623  }
1624  if ( uiGroupIdxY > 3 )
1625  {     
1626    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1627    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1628    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1629    {
1630      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1631    }
1632  }
1633}
1634
1635Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1636{
1637#if !H_MV_ENC_DEC_TRAC
1638  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1639  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1640  DTRACE_CABAC_V( eTType )
1641  DTRACE_CABAC_T( "\twidth=" )
1642  DTRACE_CABAC_V( uiWidth )
1643  DTRACE_CABAC_T( "\theight=" )
1644  DTRACE_CABAC_V( uiHeight )
1645  DTRACE_CABAC_T( "\tdepth=" )
1646  DTRACE_CABAC_V( uiDepth )
1647  DTRACE_CABAC_T( "\tabspartidx=" )
1648  DTRACE_CABAC_V( uiAbsPartIdx )
1649  DTRACE_CABAC_T( "\ttoCU-X=" )
1650  DTRACE_CABAC_V( pcCU->getCUPelX() )
1651  DTRACE_CABAC_T( "\ttoCU-Y=" )
1652  DTRACE_CABAC_V( pcCU->getCUPelY() )
1653  DTRACE_CABAC_T( "\tCU-addr=" )
1654  DTRACE_CABAC_V(  pcCU->getAddr() )
1655  DTRACE_CABAC_T( "\tinCU-X=" )
1656  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1657  DTRACE_CABAC_T( "\tinCU-Y=" )
1658  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1659  DTRACE_CABAC_T( "\tpredmode=" )
1660  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1661  DTRACE_CABAC_T( "\n" )
1662#endif
1663  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1664  {
1665    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1666    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1667  }
1668 
1669  UInt uiNumSig = 0;
1670 
1671  // compute number of significant coefficients
1672  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1673 
1674  if ( uiNumSig == 0 )
1675    return;
1676  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1677  {
1678    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1679  }
1680  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1681 
1682  //----- encode significance map -----
1683  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1684  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1685  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1686 
1687  Bool beValid;
1688  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1689  {
1690    beValid = false;
1691  }
1692  else 
1693  {
1694    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1695  }
1696
1697  // Find position of last coefficient
1698  Int scanPosLast = -1;
1699  Int posLast;
1700
1701  const UInt * scanCG;
1702  {
1703    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1704    if( uiLog2BlockSize == 3 )
1705    {
1706      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1707    }
1708    else if( uiLog2BlockSize == 5 )
1709    {
1710      scanCG = g_sigLastScanCG32x32;
1711    }
1712  }
1713  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1714  static const UInt uiShift = MLS_CG_SIZE >> 1;
1715  const UInt uiNumBlkSide = uiWidth >> uiShift;
1716
1717    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1718
1719    do
1720    {
1721      posLast = scan[ ++scanPosLast ];
1722
1723      // get L1 sig map
1724      UInt uiPosY    = posLast >> uiLog2BlockSize;
1725      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1726      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1727      if( pcCoef[ posLast ] )
1728      {
1729        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1730      }
1731
1732      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1733    }
1734    while ( uiNumSig > 0 );
1735
1736  // Code position of last coefficient
1737  Int posLastY = posLast >> uiLog2BlockSize;
1738  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1739  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1740 
1741  //===== code significance flag =====
1742  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1743  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1744
1745
1746  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1747  UInt c1 = 1;
1748  UInt uiGoRiceParam           = 0;
1749  Int  iScanPosSig             = scanPosLast;
1750
1751  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1752  {
1753    Int numNonZero = 0;
1754    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1755    uiGoRiceParam    = 0;
1756    Int absCoeff[16];
1757    UInt coeffSigns = 0;
1758
1759    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1760
1761    if( iScanPosSig == scanPosLast )
1762    {
1763      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1764      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1765      numNonZero    = 1;
1766      lastNZPosInCG  = iScanPosSig;
1767      firstNZPosInCG = iScanPosSig;
1768      iScanPosSig--;
1769    }
1770
1771      // encode significant_coeffgroup_flag
1772      Int iCGBlkPos = scanCG[ iSubSet ];
1773      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1774      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1775      if( iSubSet == iLastScanSet || iSubSet == 0)
1776      {
1777        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1778      }
1779      else
1780      {
1781          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1782          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1783          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1784      }
1785     
1786      // encode significant_coeff_flag
1787      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1788      {
1789        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1790        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1791        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1792        {
1793          uiBlkPos  = scan[ iScanPosSig ]; 
1794          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1795          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1796          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1797          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1798          {
1799            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1800            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1801          }
1802          if( uiSig )
1803          {
1804            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1805            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1806            numNonZero++;
1807            if( lastNZPosInCG == -1 )
1808            {
1809              lastNZPosInCG = iScanPosSig;
1810            }
1811            firstNZPosInCG = iScanPosSig;
1812          }
1813        }
1814      }
1815      else
1816      {
1817        iScanPosSig = iSubPos - 1;
1818      }
1819
1820    if( numNonZero > 0 )
1821    {
1822      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1823      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1824     
1825      if( c1 == 0 )
1826      {
1827        uiCtxSet++;
1828      }
1829      c1 = 1;
1830      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1831     
1832      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1833      Int firstC2FlagIdx = -1;
1834      for( Int idx = 0; idx < numC1Flag; idx++ )
1835      {
1836        UInt uiSymbol = absCoeff[ idx ] > 1;
1837        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1838        if( uiSymbol )
1839        {
1840          c1 = 0;
1841
1842          if (firstC2FlagIdx == -1)
1843          {
1844            firstC2FlagIdx = idx;
1845          }
1846        }
1847        else if( (c1 < 3) && (c1 > 0) )
1848        {
1849          c1++;
1850        }
1851      }
1852     
1853      if (c1 == 0)
1854      {
1855
1856        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1857        if ( firstC2FlagIdx != -1)
1858        {
1859          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1860          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1861        }
1862      }
1863     
1864      if( beValid && signHidden )
1865      {
1866        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1867      }
1868      else
1869      {
1870        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1871      }
1872     
1873      Int iFirstCoeff2 = 1;   
1874      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1875      {
1876        for ( Int idx = 0; idx < numNonZero; idx++ )
1877        {
1878          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1879
1880          if( absCoeff[ idx ] >= baseLevel)
1881          {
1882            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1883            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1884            {
1885               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1886            }
1887          }
1888          if(absCoeff[ idx ] >= 2) 
1889          {
1890            iFirstCoeff2 = 0;
1891          }
1892        }       
1893      }
1894    }
1895  }
1896
1897  return;
1898}
1899
1900/** code SAO offset sign
1901 * \param code sign value
1902 */
1903Void TEncSbac::codeSAOSign( UInt code )
1904{
1905  m_pcBinIf->encodeBinEP( code );
1906}
1907
1908Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1909{
1910  if (maxSymbol == 0)
1911  {
1912    return;
1913  }
1914
1915  Int i;
1916  Bool bCodeLast = ( maxSymbol > code );
1917
1918  if ( code == 0 )
1919  {
1920    m_pcBinIf->encodeBinEP( 0 );
1921  }
1922  else
1923  {
1924    m_pcBinIf->encodeBinEP( 1 );
1925    for ( i=0; i<code-1; i++ )
1926    {
1927      m_pcBinIf->encodeBinEP( 1 );
1928    }
1929    if( bCodeLast )
1930    {
1931      m_pcBinIf->encodeBinEP( 0 );
1932    }
1933  }
1934}
1935
1936
1937/** Code SAO EO class or BO band position
1938 * \param uiLength
1939 * \param uiCode
1940 */
1941Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
1942{
1943   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
1944}
1945/** Code SAO merge flags
1946 * \param uiCode
1947 * \param uiCompIdx
1948 */
1949Void TEncSbac::codeSaoMerge       ( UInt uiCode )
1950{
1951  if (uiCode == 0)
1952  {
1953    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1954  }
1955  else
1956  {
1957    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1958  }
1959}
1960/** Code SAO type index
1961 * \param uiCode
1962 */
1963Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
1964{
1965  if (uiCode == 0)
1966  {
1967    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1968  }
1969  else
1970  {
1971    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1972    m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 );
1973  }
1974}
1975/*!
1976 ****************************************************************************
1977 * \brief
1978 *   estimate bit cost for CBP, significant map and significant coefficients
1979 ****************************************************************************
1980 */
1981Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
1982{
1983  estCBFBit( pcEstBitsSbac );
1984
1985  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
1986 
1987  // encode significance map
1988  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
1989 
1990  // encode significant coefficients
1991  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
1992}
1993
1994/*!
1995 ****************************************************************************
1996 * \brief
1997 *    estimate bit cost for each CBP bit
1998 ****************************************************************************
1999 */
2000Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
2001{
2002  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2003
2004  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2005  {
2006    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2007    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2008  }
2009
2010  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2011 
2012  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2013  {
2014    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2015    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2016  }
2017}
2018
2019
2020/*!
2021 ****************************************************************************
2022 * \brief
2023 *    estimate SAMBAC bit cost for significant coefficient group map
2024 ****************************************************************************
2025 */
2026Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2027{
2028  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2029
2030  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2031  {
2032    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2033    {
2034      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2035    }
2036  }
2037}
2038
2039
2040/*!
2041 ****************************************************************************
2042 * \brief
2043 *    estimate SAMBAC bit cost for significant coefficient map
2044 ****************************************************************************
2045 */
2046Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2047{
2048  Int firstCtx = 1, numCtx = 8;
2049  if (max(width, height) >= 16)
2050  {
2051    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2052    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2053  }
2054  else if (width == 8)
2055  {
2056    firstCtx = 9;
2057    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2058  }
2059 
2060  if (eTType == TEXT_LUMA )
2061  {
2062    for( UInt bin = 0; bin < 2; bin++ )
2063    {
2064      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2065    }
2066
2067    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2068    {
2069      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2070      {
2071        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2072      }
2073    }
2074  }
2075  else
2076  {
2077    for( UInt bin = 0; bin < 2; bin++ )
2078    {
2079      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2080    }
2081    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2082    {
2083      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2084      {
2085        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2086      }
2087    }
2088  }
2089  Int iBitsX = 0, iBitsY = 0;
2090  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2091
2092  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2093  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2094  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2095  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2096
2097  Int ctx;
2098  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2099  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2100  {
2101    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2102    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2103    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2104  }
2105  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2106  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2107  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2108  {
2109    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2110    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2111    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2112  }
2113  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2114}
2115
2116/*!
2117 ****************************************************************************
2118 * \brief
2119 *    estimate bit cost of significant coefficient
2120 ****************************************************************************
2121 */
2122Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2123{
2124  if (eTType==TEXT_LUMA)
2125  {
2126    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2127    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2128
2129    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2130    {
2131      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2132      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2133    }
2134
2135    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2136    {
2137      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2138      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2139    }
2140  }
2141  else
2142  {
2143    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2144    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2145
2146    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2147    {
2148      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2149      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2150    }
2151
2152    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2153    {
2154      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2155      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2156    }
2157  }
2158}
2159
2160/**
2161 - Initialize our context information from the nominated source.
2162 .
2163 \param pSrc From where to copy context information.
2164 */
2165Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2166{ 
2167  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2168}
2169
2170Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2171{
2172  this->xCopyContextsFrom(pScr);
2173}
2174
2175Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled)
2176{
2177  UInt uiSymbol;
2178  if(!sliceEnabled)
2179  {
2180    assert(ctbParam.modeIdc == SAO_MODE_OFF);
2181    return;
2182  }
2183
2184  //type
2185  if(compIdx == SAO_Y || compIdx == SAO_Cb)
2186  {
2187    //sao_type_idx_luma or sao_type_idx_chroma
2188    if(ctbParam.modeIdc == SAO_MODE_OFF)
2189    {
2190      uiSymbol =0;
2191    }
2192    else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO
2193    {
2194      uiSymbol = 1;
2195    }
2196    else
2197    {
2198      assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO
2199      uiSymbol = 2;
2200    }
2201    codeSaoTypeIdx(uiSymbol); 
2202  }
2203
2204  if(ctbParam.modeIdc == SAO_MODE_NEW)
2205  {
2206    Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; 
2207    Int offset[4];
2208    Int k=0;
2209    for(Int i=0; i< numClasses; i++)
2210    {
2211      if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN)
2212      {
2213        continue;
2214      }
2215      Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?(  (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES   ):i;
2216      offset[k] = ctbParam.offset[classIdx];
2217      k++;
2218    }
2219
2220    for(Int i=0; i< 4; i++)
2221    {
2222      codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]),  g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs
2223    }
2224
2225
2226    if(ctbParam.typeIdc == SAO_TYPE_BO)
2227    {
2228      for(Int i=0; i< 4; i++)
2229      {
2230        if(offset[i] != 0)
2231        {
2232          codeSAOSign((offset[i]< 0)?1:0);
2233        }
2234      }
2235
2236      codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position
2237    }
2238    else //EO
2239    {
2240      if(compIdx == SAO_Y || compIdx == SAO_Cb)
2241      {
2242        assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0);
2243        codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma
2244      }
2245    }
2246
2247  }
2248}
2249
2250
2251Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam
2252                              , Bool* sliceEnabled
2253                              , Bool leftMergeAvail
2254                              , Bool aboveMergeAvail
2255                              , Bool onlyEstMergeInfo // = false
2256                              )
2257{
2258
2259  Bool isLeftMerge = false;
2260  Bool isAboveMerge= false;
2261
2262  if(leftMergeAvail)
2263  {
2264    isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT));
2265    codeSaoMerge( isLeftMerge?1:0  ); //sao_merge_left_flag
2266  }
2267
2268  if( aboveMergeAvail && !isLeftMerge)
2269  {
2270    isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); 
2271    codeSaoMerge( isAboveMerge?1:0  ); //sao_merge_left_flag
2272  }
2273
2274  if(onlyEstMergeInfo)
2275  {
2276    return; //only for RDO
2277  }
2278
2279  if(!isLeftMerge && !isAboveMerge) //not merge mode
2280  {
2281    for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
2282    {
2283      codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]);
2284    }
2285  }
2286}
2287
2288#if H_3D_INTER_SDC
2289Void TEncSbac::codeDeltaDC( TComDataCU* pcCU, UInt absPartIdx )
2290{
2291  if( !( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2292  {
2293    assert( 0 );
2294  }
2295
2296  UInt uiNumSegments = 0;
2297  UInt dimDeltaDC = 0;
2298
2299  if( pcCU->isIntra( absPartIdx ) )
2300  {
2301    UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2302    uiNumSegments = isDimMode( dir ) ? 2 : 1;
2303
2304    if( pcCU->getSDCFlag( absPartIdx ) )
2305    {
2306      if( uiNumSegments == 1 )
2307      {
2308        dimDeltaDC = pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) ? 1 : 0;
2309      }
2310      else
2311      {
2312        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) || pcCU->getSDCSegmentDCOffset( 1, absPartIdx ) ) ? 1 : 0;
2313      }
2314    }
2315    else
2316    {
2317      dimDeltaDC = isDimDeltaDC( dir );
2318    }
2319#if MTK_DELTA_DC_FLAG_ONE_CONTEXT_H0084_H0100_H0113
2320    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2321#else
2322    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, uiNumSegments-1 ) );
2323#endif
2324  }
2325  else //all-zero inter SDC is not allowed
2326  {
2327    uiNumSegments = 1;
2328    dimDeltaDC = 1;
2329  }
2330
2331  if( dimDeltaDC )
2332  {
2333    for( UInt segment = 0; segment < uiNumSegments; segment++ )
2334    {
2335      Pel deltaDC = 0;
2336
2337      if( pcCU->isIntra( absPartIdx ) )
2338      {
2339        UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2340        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset( segment, absPartIdx ) : pcCU->getDimDeltaDC( getDimType( dir ), segment, absPartIdx );
2341      }
2342      else
2343      {
2344        deltaDC = pcCU->getSDCSegmentDCOffset( segment, absPartIdx );
2345      }
2346
2347      xCodeDimDeltaDC( deltaDC, uiNumSegments );
2348    }
2349  }
2350}
2351
2352Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2353{
2354  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2355  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2356
2357  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2358}
2359
2360#endif
2361
2362#if H_3D_DBBP
2363Void TEncSbac::codeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2364{
2365  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
2366  AOF( ePartSize == RWTH_DBBP_PACK_MODE );
2367  AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
2368  AOF( !pcCU->getSlice()->getIsDepth() );
2369 
2370  UInt uiSymbol = pcCU->getDBBPFlag( uiAbsPartIdx ) ? 1 : 0;
2371  m_pcBinIf->encodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) );
2372}
2373#endif
2374
2375//! \}
Note: See TracBrowser for help on using the repository browser.