source: 3DVCSoftware/branches/HTM-10.2-dev3-Qualcomm/source/Lib/TLibEncoder/TEncSbac.cpp

Last change on this file was 914, checked in by qualcomm, 11 years ago

Integration of H0131

  • Property svn:eol-style set to native
File size: 77.3 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6* Copyright (c) 2010-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    m_pcBinIf->encodeBin( isDimMode( dir ) ? 0 : 1, m_cAngleFlagSCModel.get( 0, 0, pcCU->getCtxAngleFlag( absPartIdx ) ) );
1246  }
1247  if( isDimMode( dir ) )
1248  {
1249    UInt uiCodeIdx = 0;
1250
1251    switch( getDimType( dir ) )
1252    {
1253    case DMM1_IDX: uiCodeIdx = 0; break;
1254    case DMM4_IDX: uiCodeIdx = 1; break;
1255    default:                      break;
1256    }
1257    //mode coding
1258    m_pcBinIf->encodeBin( uiCodeIdx == 0 ? 0 : 1, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1259  }
1260}
1261#endif
1262
1263
1264Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1265{
1266  const UInt uiInterDir = pcCU->getInterDir( uiAbsPartIdx ) - 1;
1267  const UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1268  ContextModel *pCtx    = m_cCUInterDirSCModel.get( 0 );
1269  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1270  {
1271    m_pcBinIf->encodeBin( uiInterDir == 2 ? 1 : 0, *( pCtx + uiCtx ) );
1272  }
1273  if (uiInterDir < 2)
1274  {
1275    m_pcBinIf->encodeBin( uiInterDir, *( pCtx + 4 ) );
1276  }
1277#if H_MV_ENC_DEC_TRAC
1278  DTRACE_PU("inter_pred_idc", uiInterDir ); 
1279#endif
1280
1281  return;
1282}
1283
1284Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1285{
1286  {
1287    Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1288    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1289    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), *pCtx );
1290   
1291    if( iRefFrame > 0 )
1292    {
1293      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1294      pCtx++;
1295      iRefFrame--;
1296      for( UInt ui = 0; ui < uiRefNum; ++ui )
1297      {
1298        const UInt uiSymbol = ui == iRefFrame ? 0 : 1;
1299        if( ui == 0 )
1300        {
1301          m_pcBinIf->encodeBin( uiSymbol, *pCtx );       
1302        }
1303        else
1304        {
1305          m_pcBinIf->encodeBinEP( uiSymbol );
1306        }
1307        if( uiSymbol == 0 )
1308        {
1309          break;
1310        }
1311      }
1312    }
1313#if H_MV_ENC_DEC_TRAC
1314#if ENC_DEC_TRACE
1315    iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ); 
1316    if ( eRefList == REF_PIC_LIST_0 )
1317    {
1318      DTRACE_PU("ref_idx_l0", iRefFrame)
1319    }
1320    else
1321    {
1322      DTRACE_PU("ref_idx_l1", iRefFrame)
1323    }
1324#endif
1325#endif
1326  }
1327  return;
1328}
1329
1330Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1331{
1332  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1333  {
1334    return;
1335  }
1336
1337  const TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1338  const Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1339  const Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1340  ContextModel* pCtx = m_cCUMvdSCModel.get( 0 );
1341
1342  m_pcBinIf->encodeBin( iHor != 0 ? 1 : 0, *pCtx );
1343  m_pcBinIf->encodeBin( iVer != 0 ? 1 : 0, *pCtx );
1344
1345  const Bool bHorAbsGr0 = iHor != 0;
1346  const Bool bVerAbsGr0 = iVer != 0;
1347  const UInt uiHorAbs   = 0 > iHor ? -iHor : iHor;
1348  const UInt uiVerAbs   = 0 > iVer ? -iVer : iVer;
1349  pCtx++;
1350
1351  if( bHorAbsGr0 )
1352  {
1353    m_pcBinIf->encodeBin( uiHorAbs > 1 ? 1 : 0, *pCtx );
1354  }
1355
1356  if( bVerAbsGr0 )
1357  {
1358    m_pcBinIf->encodeBin( uiVerAbs > 1 ? 1 : 0, *pCtx );
1359  }
1360
1361  if( bHorAbsGr0 )
1362  {
1363    if( uiHorAbs > 1 )
1364    {
1365      xWriteEpExGolomb( uiHorAbs-2, 1 );
1366    }
1367
1368    m_pcBinIf->encodeBinEP( 0 > iHor ? 1 : 0 );
1369  }
1370
1371  if( bVerAbsGr0 )
1372  {
1373    if( uiVerAbs > 1 )
1374    {
1375      xWriteEpExGolomb( uiVerAbs-2, 1 );
1376    }
1377
1378    m_pcBinIf->encodeBinEP( 0 > iVer ? 1 : 0 );
1379  }
1380 
1381  return;
1382}
1383
1384Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1385{
1386  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getRefQP( uiAbsPartIdx );
1387 
1388  Int qpBdOffsetY =  pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1389  iDQp = (iDQp + 78 + qpBdOffsetY + (qpBdOffsetY/2)) % (52 + qpBdOffsetY) - 26 - (qpBdOffsetY/2);
1390
1391  UInt uiAbsDQp = (UInt)((iDQp > 0)? iDQp  : (-iDQp));
1392  UInt TUValue = min((Int)uiAbsDQp, CU_DQP_TU_CMAX);
1393  xWriteUnaryMaxSymbol( TUValue, &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1394  if( uiAbsDQp >= CU_DQP_TU_CMAX )
1395  {
1396    xWriteEpExGolomb( uiAbsDQp - CU_DQP_TU_CMAX, CU_DQP_EG_k );
1397  }
1398
1399  if ( uiAbsDQp > 0)
1400  {
1401    UInt uiSign = (iDQp > 0 ? 0 : 1);
1402    m_pcBinIf->encodeBinEP(uiSign);
1403  }
1404
1405  return;
1406}
1407
1408Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1409{
1410  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1411  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1412  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1413#if !H_MV_ENC_DEC_TRAC
1414  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1415  DTRACE_CABAC_T( "\tparseQtCbf()" )
1416  DTRACE_CABAC_T( "\tsymbol=" )
1417  DTRACE_CABAC_V( uiCbf )
1418  DTRACE_CABAC_T( "\tctx=" )
1419  DTRACE_CABAC_V( uiCtx )
1420  DTRACE_CABAC_T( "\tetype=" )
1421  DTRACE_CABAC_V( eType )
1422  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1423  DTRACE_CABAC_V( uiAbsPartIdx )
1424  DTRACE_CABAC_T( "\n" )
1425#endif
1426}
1427
1428void TEncSbac::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
1429{
1430  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1431  {
1432    return;
1433  }
1434  if(width != 4 || height != 4)
1435  {
1436    return;
1437  }
1438
1439  UInt useTransformSkip = pcCU->getTransformSkip( uiAbsPartIdx,eTType);
1440  m_pcBinIf->encodeBin( useTransformSkip, m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1441#if !H_MV_ENC_DEC_TRAC
1442  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1443  DTRACE_CABAC_T("\tparseTransformSkip()");
1444  DTRACE_CABAC_T( "\tsymbol=" )
1445  DTRACE_CABAC_V( useTransformSkip )
1446  DTRACE_CABAC_T( "\tAddr=" )
1447  DTRACE_CABAC_V( pcCU->getAddr() )
1448  DTRACE_CABAC_T( "\tetype=" )
1449  DTRACE_CABAC_V( eTType )
1450  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1451  DTRACE_CABAC_V( uiAbsPartIdx )
1452  DTRACE_CABAC_T( "\n" )
1453#endif
1454}
1455
1456/** Code I_PCM information.
1457 * \param pcCU pointer to CU
1458 * \param uiAbsPartIdx CU index
1459 * \returns Void
1460 */
1461Void TEncSbac::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1462{
1463  UInt uiIPCM = (pcCU->getIPCMFlag(uiAbsPartIdx) == true)? 1 : 0;
1464
1465  Bool writePCMSampleFlag = pcCU->getIPCMFlag(uiAbsPartIdx);
1466
1467  m_pcBinIf->encodeBinTrm (uiIPCM);
1468#if H_MV_ENC_DEC_TRAC         
1469  DTRACE_CU("pcm_flag", uiIPCM)
1470#endif
1471  if (writePCMSampleFlag)
1472  {
1473    m_pcBinIf->encodePCMAlignBits();
1474
1475    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
1476    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
1477    UInt uiChromaOffset = uiLumaOffset>>2;
1478    Pel* piPCMSample;
1479    UInt uiWidth;
1480    UInt uiHeight;
1481    UInt uiSampleBits;
1482    UInt uiX, uiY;
1483
1484    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
1485    uiWidth = pcCU->getWidth(uiAbsPartIdx);
1486    uiHeight = pcCU->getHeight(uiAbsPartIdx);
1487    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
1488
1489    for(uiY = 0; uiY < uiHeight; uiY++)
1490    {
1491      for(uiX = 0; uiX < uiWidth; uiX++)
1492      {
1493        UInt uiSample = piPCMSample[uiX];
1494
1495        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1496      }
1497      piPCMSample += uiWidth;
1498    }
1499
1500    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
1501    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1502    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1503    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1504
1505    for(uiY = 0; uiY < uiHeight; uiY++)
1506    {
1507      for(uiX = 0; uiX < uiWidth; uiX++)
1508      {
1509        UInt uiSample = piPCMSample[uiX];
1510
1511        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1512      }
1513      piPCMSample += uiWidth;
1514    }
1515
1516    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
1517    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
1518    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
1519    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
1520
1521    for(uiY = 0; uiY < uiHeight; uiY++)
1522    {
1523      for(uiX = 0; uiX < uiWidth; uiX++)
1524      {
1525        UInt uiSample = piPCMSample[uiX];
1526
1527        m_pcBinIf->xWritePCMCode(uiSample, uiSampleBits);
1528      }
1529      piPCMSample += uiWidth;
1530    }
1531    m_pcBinIf->resetBac();
1532  }
1533}
1534
1535Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1536{
1537  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1538  UInt uiCtx = 0;
1539  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1540#if !H_MV_ENC_DEC_TRAC
1541  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1542  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1543  DTRACE_CABAC_T( "\tsymbol=" )
1544  DTRACE_CABAC_V( uiCbf )
1545  DTRACE_CABAC_T( "\tctx=" )
1546  DTRACE_CABAC_V( uiCtx )
1547  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1548  DTRACE_CABAC_V( uiAbsPartIdx )
1549  DTRACE_CABAC_T( "\n" )
1550#else
1551  DTRACE_CU( "rqt_root_cbf", uiCbf )
1552#endif
1553}
1554
1555Void TEncSbac::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
1556{
1557  // this function is only used to estimate the bits when cbf is 0
1558  // and will never be called when writing the bistream. do not need to write log
1559  UInt uiCbf = 0;
1560  UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1561  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA : eType, uiCtx ) );
1562}
1563
1564Void TEncSbac::codeQtRootCbfZero( TComDataCU* pcCU )
1565{
1566  // this function is only used to estimate the bits when cbf is 0
1567  // and will never be called when writing the bistream. do not need to write log
1568  UInt uiCbf = 0;
1569  UInt uiCtx = 0;
1570  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1571}
1572
1573/** Encode (X,Y) position of the last significant coefficient
1574 * \param uiPosX X component of last coefficient
1575 * \param uiPosY Y component of last coefficient
1576 * \param width  Block width
1577 * \param height Block height
1578 * \param eTType plane type / luminance or chrominance
1579 * \param uiScanIdx scan type (zig-zag, hor, ver)
1580 * This method encodes the X and Y component within a block of the last significant coefficient.
1581 */
1582Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1583{ 
1584  // swap
1585  if( uiScanIdx == SCAN_VER )
1586  {
1587    swap( uiPosX, uiPosY );
1588  }
1589
1590  UInt uiCtxLast;
1591  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1592  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1593  UInt uiGroupIdxX    = g_uiGroupIdx[ uiPosX ];
1594  UInt uiGroupIdxY    = g_uiGroupIdx[ uiPosY ];
1595
1596
1597  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1598  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1599  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1600  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1601  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1602  // posX
1603  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxX; uiCtxLast++ )
1604  {
1605      m_pcBinIf->encodeBin( 1, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1606  }
1607  if( uiGroupIdxX < g_uiGroupIdx[ width - 1 ])
1608  {
1609      m_pcBinIf->encodeBin( 0, *( pCtxX + blkSizeOffsetX + (uiCtxLast >>shiftX) ) );
1610  }
1611
1612  // posY
1613  for( uiCtxLast = 0; uiCtxLast < uiGroupIdxY; uiCtxLast++ )
1614  {
1615    m_pcBinIf->encodeBin( 1, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1616  }
1617  if( uiGroupIdxY < g_uiGroupIdx[ height - 1 ])
1618  {
1619    m_pcBinIf->encodeBin( 0, *( pCtxY + blkSizeOffsetY + (uiCtxLast >>shiftY) ) );
1620  }
1621  if ( uiGroupIdxX > 3 )
1622  {     
1623    UInt uiCount = ( uiGroupIdxX - 2 ) >> 1;
1624    uiPosX       = uiPosX - g_uiMinInGroup[ uiGroupIdxX ];
1625    for (Int i = uiCount - 1 ; i >= 0; i-- )
1626    {
1627      m_pcBinIf->encodeBinEP( ( uiPosX >> i ) & 1 );
1628    }
1629  }
1630  if ( uiGroupIdxY > 3 )
1631  {     
1632    UInt uiCount = ( uiGroupIdxY - 2 ) >> 1;
1633    uiPosY       = uiPosY - g_uiMinInGroup[ uiGroupIdxY ];
1634    for ( Int i = uiCount - 1 ; i >= 0; i-- )
1635    {
1636      m_pcBinIf->encodeBinEP( ( uiPosY >> i ) & 1 );
1637    }
1638  }
1639}
1640
1641Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1642{
1643#if !H_MV_ENC_DEC_TRAC
1644  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1645  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1646  DTRACE_CABAC_V( eTType )
1647  DTRACE_CABAC_T( "\twidth=" )
1648  DTRACE_CABAC_V( uiWidth )
1649  DTRACE_CABAC_T( "\theight=" )
1650  DTRACE_CABAC_V( uiHeight )
1651  DTRACE_CABAC_T( "\tdepth=" )
1652  DTRACE_CABAC_V( uiDepth )
1653  DTRACE_CABAC_T( "\tabspartidx=" )
1654  DTRACE_CABAC_V( uiAbsPartIdx )
1655  DTRACE_CABAC_T( "\ttoCU-X=" )
1656  DTRACE_CABAC_V( pcCU->getCUPelX() )
1657  DTRACE_CABAC_T( "\ttoCU-Y=" )
1658  DTRACE_CABAC_V( pcCU->getCUPelY() )
1659  DTRACE_CABAC_T( "\tCU-addr=" )
1660  DTRACE_CABAC_V(  pcCU->getAddr() )
1661  DTRACE_CABAC_T( "\tinCU-X=" )
1662  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1663  DTRACE_CABAC_T( "\tinCU-Y=" )
1664  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1665  DTRACE_CABAC_T( "\tpredmode=" )
1666  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1667  DTRACE_CABAC_T( "\n" )
1668#endif
1669  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1670  {
1671    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1672    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1673  }
1674 
1675  UInt uiNumSig = 0;
1676 
1677  // compute number of significant coefficients
1678  uiNumSig = TEncEntropy::countNonZeroCoeffs(pcCoef, uiWidth * uiHeight);
1679 
1680  if ( uiNumSig == 0 )
1681    return;
1682  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1683  {
1684    codeTransformSkipFlags( pcCU,uiAbsPartIdx, uiWidth, uiHeight, eTType );
1685  }
1686  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1687 
1688  //----- encode significance map -----
1689  const UInt   uiLog2BlockSize = g_aucConvertToBit[ uiWidth ] + 2;
1690  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1691  const UInt *scan = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize - 1 ];
1692 
1693  Bool beValid;
1694  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1695  {
1696    beValid = false;
1697  }
1698  else 
1699  {
1700    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1701  }
1702
1703  // Find position of last coefficient
1704  Int scanPosLast = -1;
1705  Int posLast;
1706
1707  const UInt * scanCG;
1708  {
1709    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0 ];
1710    if( uiLog2BlockSize == 3 )
1711    {
1712      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1713    }
1714    else if( uiLog2BlockSize == 5 )
1715    {
1716      scanCG = g_sigLastScanCG32x32;
1717    }
1718  }
1719  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1720  static const UInt uiShift = MLS_CG_SIZE >> 1;
1721  const UInt uiNumBlkSide = uiWidth >> uiShift;
1722
1723    ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1724
1725    do
1726    {
1727      posLast = scan[ ++scanPosLast ];
1728
1729      // get L1 sig map
1730      UInt uiPosY    = posLast >> uiLog2BlockSize;
1731      UInt uiPosX    = posLast - ( uiPosY << uiLog2BlockSize );
1732      UInt uiBlkIdx  = uiNumBlkSide * (uiPosY >> uiShift) + (uiPosX >> uiShift);
1733      if( pcCoef[ posLast ] )
1734      {
1735        uiSigCoeffGroupFlag[ uiBlkIdx ] = 1;
1736      }
1737
1738      uiNumSig -= ( pcCoef[ posLast ] != 0 );
1739    }
1740    while ( uiNumSig > 0 );
1741
1742  // Code position of last coefficient
1743  Int posLastY = posLast >> uiLog2BlockSize;
1744  Int posLastX = posLast - ( posLastY << uiLog2BlockSize );
1745  codeLastSignificantXY(posLastX, posLastY, uiWidth, uiHeight, eTType, uiScanIdx);
1746 
1747  //===== code significance flag =====
1748  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1749  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1750
1751
1752  const Int  iLastScanSet      = scanPosLast >> LOG2_SCAN_SET_SIZE;
1753  UInt c1 = 1;
1754  UInt uiGoRiceParam           = 0;
1755  Int  iScanPosSig             = scanPosLast;
1756
1757  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1758  {
1759    Int numNonZero = 0;
1760    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1761    uiGoRiceParam    = 0;
1762    Int absCoeff[16];
1763    UInt coeffSigns = 0;
1764
1765    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1766
1767    if( iScanPosSig == scanPosLast )
1768    {
1769      absCoeff[ 0 ] = abs( pcCoef[ posLast ] );
1770      coeffSigns    = ( pcCoef[ posLast ] < 0 );
1771      numNonZero    = 1;
1772      lastNZPosInCG  = iScanPosSig;
1773      firstNZPosInCG = iScanPosSig;
1774      iScanPosSig--;
1775    }
1776
1777      // encode significant_coeffgroup_flag
1778      Int iCGBlkPos = scanCG[ iSubSet ];
1779      Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1780      Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1781      if( iSubSet == iLastScanSet || iSubSet == 0)
1782      {
1783        uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1784      }
1785      else
1786      {
1787          UInt uiSigCoeffGroup   = (uiSigCoeffGroupFlag[ iCGBlkPos ] != 0);
1788          UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1789          m_pcBinIf->encodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1790      }
1791     
1792      // encode significant_coeff_flag
1793      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1794      {
1795        Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1796        UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1797        for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1798        {
1799          uiBlkPos  = scan[ iScanPosSig ]; 
1800          uiPosY    = uiBlkPos >> uiLog2BlockSize;
1801          uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1802          uiSig     = (pcCoef[ uiBlkPos ] != 0);
1803          if( iScanPosSig > iSubPos || iSubSet == 0 || numNonZero )
1804          {
1805            uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1806            m_pcBinIf->encodeBin( uiSig, baseCtx[ uiCtxSig ] );
1807          }
1808          if( uiSig )
1809          {
1810            absCoeff[ numNonZero ] = abs( pcCoef[ uiBlkPos ] );
1811            coeffSigns = 2 * coeffSigns + ( pcCoef[ uiBlkPos ] < 0 );
1812            numNonZero++;
1813            if( lastNZPosInCG == -1 )
1814            {
1815              lastNZPosInCG = iScanPosSig;
1816            }
1817            firstNZPosInCG = iScanPosSig;
1818          }
1819        }
1820      }
1821      else
1822      {
1823        iScanPosSig = iSubPos - 1;
1824      }
1825
1826    if( numNonZero > 0 )
1827    {
1828      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1829      UInt uiCtxSet = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1830     
1831      if( c1 == 0 )
1832      {
1833        uiCtxSet++;
1834      }
1835      c1 = 1;
1836      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1837     
1838      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1839      Int firstC2FlagIdx = -1;
1840      for( Int idx = 0; idx < numC1Flag; idx++ )
1841      {
1842        UInt uiSymbol = absCoeff[ idx ] > 1;
1843        m_pcBinIf->encodeBin( uiSymbol, baseCtxMod[c1] );
1844        if( uiSymbol )
1845        {
1846          c1 = 0;
1847
1848          if (firstC2FlagIdx == -1)
1849          {
1850            firstC2FlagIdx = idx;
1851          }
1852        }
1853        else if( (c1 < 3) && (c1 > 0) )
1854        {
1855          c1++;
1856        }
1857      }
1858     
1859      if (c1 == 0)
1860      {
1861
1862        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1863        if ( firstC2FlagIdx != -1)
1864        {
1865          UInt symbol = absCoeff[ firstC2FlagIdx ] > 2;
1866          m_pcBinIf->encodeBin( symbol, baseCtxMod[0] );
1867        }
1868      }
1869     
1870      if( beValid && signHidden )
1871      {
1872        m_pcBinIf->encodeBinsEP( (coeffSigns >> 1), numNonZero-1 );
1873      }
1874      else
1875      {
1876        m_pcBinIf->encodeBinsEP( coeffSigns, numNonZero );
1877      }
1878     
1879      Int iFirstCoeff2 = 1;   
1880      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1881      {
1882        for ( Int idx = 0; idx < numNonZero; idx++ )
1883        {
1884          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2 ) : 1;
1885
1886          if( absCoeff[ idx ] >= baseLevel)
1887          {
1888            xWriteCoefRemainExGolomb( absCoeff[ idx ] - baseLevel, uiGoRiceParam );
1889            if(absCoeff[idx] > 3*(1<<uiGoRiceParam))
1890            {
1891               uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1892            }
1893          }
1894          if(absCoeff[ idx ] >= 2) 
1895          {
1896            iFirstCoeff2 = 0;
1897          }
1898        }       
1899      }
1900    }
1901  }
1902
1903  return;
1904}
1905
1906/** code SAO offset sign
1907 * \param code sign value
1908 */
1909Void TEncSbac::codeSAOSign( UInt code )
1910{
1911  m_pcBinIf->encodeBinEP( code );
1912}
1913
1914Void TEncSbac::codeSaoMaxUvlc    ( UInt code, UInt maxSymbol )
1915{
1916  if (maxSymbol == 0)
1917  {
1918    return;
1919  }
1920
1921  Int i;
1922  Bool bCodeLast = ( maxSymbol > code );
1923
1924  if ( code == 0 )
1925  {
1926    m_pcBinIf->encodeBinEP( 0 );
1927  }
1928  else
1929  {
1930    m_pcBinIf->encodeBinEP( 1 );
1931    for ( i=0; i<code-1; i++ )
1932    {
1933      m_pcBinIf->encodeBinEP( 1 );
1934    }
1935    if( bCodeLast )
1936    {
1937      m_pcBinIf->encodeBinEP( 0 );
1938    }
1939  }
1940}
1941
1942
1943/** Code SAO EO class or BO band position
1944 * \param uiLength
1945 * \param uiCode
1946 */
1947Void TEncSbac::codeSaoUflc       ( UInt uiLength, UInt uiCode )
1948{
1949   m_pcBinIf->encodeBinsEP ( uiCode, uiLength );
1950}
1951/** Code SAO merge flags
1952 * \param uiCode
1953 * \param uiCompIdx
1954 */
1955Void TEncSbac::codeSaoMerge       ( UInt uiCode )
1956{
1957  if (uiCode == 0)
1958  {
1959    m_pcBinIf->encodeBin(0,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1960  }
1961  else
1962  {
1963    m_pcBinIf->encodeBin(1,  m_cSaoMergeSCModel.get( 0, 0, 0 ));
1964  }
1965}
1966/** Code SAO type index
1967 * \param uiCode
1968 */
1969Void TEncSbac::codeSaoTypeIdx       ( UInt uiCode)
1970{
1971  if (uiCode == 0)
1972  {
1973    m_pcBinIf->encodeBin( 0, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1974  }
1975  else
1976  {
1977    m_pcBinIf->encodeBin( 1, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
1978    m_pcBinIf->encodeBinEP( uiCode == 1 ? 0 : 1 );
1979  }
1980}
1981/*!
1982 ****************************************************************************
1983 * \brief
1984 *   estimate bit cost for CBP, significant map and significant coefficients
1985 ****************************************************************************
1986 */
1987Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
1988{
1989  estCBFBit( pcEstBitsSbac );
1990
1991  estSignificantCoeffGroupMapBit( pcEstBitsSbac, eTType );
1992 
1993  // encode significance map
1994  estSignificantMapBit( pcEstBitsSbac, width, height, eTType );
1995 
1996  // encode significant coefficients
1997  estSignificantCoefficientsBit( pcEstBitsSbac, eTType );
1998}
1999
2000/*!
2001 ****************************************************************************
2002 * \brief
2003 *    estimate bit cost for each CBP bit
2004 ****************************************************************************
2005 */
2006Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac )
2007{
2008  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2009
2010  for( UInt uiCtxInc = 0; uiCtxInc < 3*NUM_QT_CBF_CTX; uiCtxInc++ )
2011  {
2012    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2013    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2014  }
2015
2016  pCtx = m_cCUQtRootCbfSCModel.get( 0 );
2017 
2018  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2019  {
2020    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = pCtx[ uiCtxInc ].getEntropyBits( 0 );
2021    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = pCtx[ uiCtxInc ].getEntropyBits( 1 );
2022  }
2023}
2024
2025
2026/*!
2027 ****************************************************************************
2028 * \brief
2029 *    estimate SAMBAC bit cost for significant coefficient group map
2030 ****************************************************************************
2031 */
2032Void TEncSbac::estSignificantCoeffGroupMapBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2033{
2034  Int firstCtx = 0, numCtx = NUM_SIG_CG_FLAG_CTX;
2035
2036  for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2037  {
2038    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2039    {
2040      pcEstBitsSbac->significantCoeffGroupBits[ ctxIdx ][ uiBin ] = m_cCUSigCoeffGroupSCModel.get(  0, eTType, ctxIdx ).getEntropyBits( uiBin );
2041    }
2042  }
2043}
2044
2045
2046/*!
2047 ****************************************************************************
2048 * \brief
2049 *    estimate SAMBAC bit cost for significant coefficient map
2050 ****************************************************************************
2051 */
2052Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, Int width, Int height, TextType eTType )
2053{
2054  Int firstCtx = 1, numCtx = 8;
2055  if (max(width, height) >= 16)
2056  {
2057    firstCtx = (eTType == TEXT_LUMA) ? 21 : 12;
2058    numCtx = (eTType == TEXT_LUMA) ? 6 : 3;
2059  }
2060  else if (width == 8)
2061  {
2062    firstCtx = 9;
2063    numCtx = (eTType == TEXT_LUMA) ? 12 : 3;
2064  }
2065 
2066  if (eTType == TEXT_LUMA )
2067  {
2068    for( UInt bin = 0; bin < 2; bin++ )
2069    {
2070      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, 0 ).getEntropyBits( bin );
2071    }
2072
2073    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2074    {
2075      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2076      {
2077        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, ctxIdx ).getEntropyBits( uiBin );
2078      }
2079    }
2080  }
2081  else
2082  {
2083    for( UInt bin = 0; bin < 2; bin++ )
2084    {
2085      pcEstBitsSbac->significantBits[ 0 ][ bin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + 0 ).getEntropyBits( bin );
2086    }
2087    for ( Int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++ )
2088    {
2089      for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2090      {
2091        pcEstBitsSbac->significantBits[ ctxIdx ][ uiBin ] = m_cCUSigSCModel.get(  0, 0, NUM_SIG_FLAG_CTX_LUMA + ctxIdx ).getEntropyBits( uiBin );
2092      }
2093    }
2094  }
2095  Int iBitsX = 0, iBitsY = 0;
2096  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
2097
2098  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
2099  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
2100  shiftX = eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
2101  shiftY = eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
2102
2103  Int ctx;
2104  ContextModel *pCtxX      = m_cCuCtxLastX.get( 0, eTType );
2105  for (ctx = 0; ctx < g_uiGroupIdx[ width - 1 ]; ctx++)
2106  {
2107    Int ctxOffset = blkSizeOffsetX + (ctx >>shiftX);
2108    pcEstBitsSbac->lastXBits[ ctx ] = iBitsX + pCtxX[ ctxOffset ].getEntropyBits( 0 );
2109    iBitsX += pCtxX[ ctxOffset ].getEntropyBits( 1 );
2110  }
2111  pcEstBitsSbac->lastXBits[ctx] = iBitsX;
2112  ContextModel *pCtxY      = m_cCuCtxLastY.get( 0, eTType );
2113  for (ctx = 0; ctx < g_uiGroupIdx[ height - 1 ]; ctx++)
2114  {
2115    Int ctxOffset = blkSizeOffsetY + (ctx >>shiftY);
2116    pcEstBitsSbac->lastYBits[ ctx ] = iBitsY + pCtxY[ ctxOffset ].getEntropyBits( 0 );
2117    iBitsY += pCtxY[ ctxOffset ].getEntropyBits( 1 );
2118  }
2119  pcEstBitsSbac->lastYBits[ctx] = iBitsY;
2120}
2121
2122/*!
2123 ****************************************************************************
2124 * \brief
2125 *    estimate bit cost of significant coefficient
2126 ****************************************************************************
2127 */
2128Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, TextType eTType )
2129{
2130  if (eTType==TEXT_LUMA)
2131  {
2132    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0);
2133    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0);
2134
2135    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
2136    {
2137      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2138      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2139    }
2140
2141    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
2142    {
2143      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2144      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2145    }
2146  }
2147  else
2148  {
2149    ContextModel *ctxOne = m_cCUOneSCModel.get(0, 0) + NUM_ONE_FLAG_CTX_LUMA;
2150    ContextModel *ctxAbs = m_cCUAbsSCModel.get(0, 0) + NUM_ABS_FLAG_CTX_LUMA;
2151
2152    for (Int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
2153    {
2154      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 0 ] = ctxOne[ ctxIdx ].getEntropyBits( 0 );
2155      pcEstBitsSbac->m_greaterOneBits[ ctxIdx ][ 1 ] = ctxOne[ ctxIdx ].getEntropyBits( 1 );   
2156    }
2157
2158    for (Int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
2159    {
2160      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 0 ] = ctxAbs[ ctxIdx ].getEntropyBits( 0 );
2161      pcEstBitsSbac->m_levelAbsBits[ ctxIdx ][ 1 ] = ctxAbs[ ctxIdx ].getEntropyBits( 1 );   
2162    }
2163  }
2164}
2165
2166/**
2167 - Initialize our context information from the nominated source.
2168 .
2169 \param pSrc From where to copy context information.
2170 */
2171Void TEncSbac::xCopyContextsFrom( TEncSbac* pSrc )
2172{ 
2173  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2174}
2175
2176Void  TEncSbac::loadContexts ( TEncSbac* pScr)
2177{
2178  this->xCopyContextsFrom(pScr);
2179}
2180
2181Void TEncSbac::codeSAOOffsetParam(Int compIdx, SAOOffset& ctbParam, Bool sliceEnabled)
2182{
2183  UInt uiSymbol;
2184  if(!sliceEnabled)
2185  {
2186    assert(ctbParam.modeIdc == SAO_MODE_OFF);
2187    return;
2188  }
2189
2190  //type
2191  if(compIdx == SAO_Y || compIdx == SAO_Cb)
2192  {
2193    //sao_type_idx_luma or sao_type_idx_chroma
2194    if(ctbParam.modeIdc == SAO_MODE_OFF)
2195    {
2196      uiSymbol =0;
2197    }
2198    else if(ctbParam.typeIdc == SAO_TYPE_BO) //BO
2199    {
2200      uiSymbol = 1;
2201    }
2202    else
2203    {
2204      assert(ctbParam.typeIdc < SAO_TYPE_START_BO); //EO
2205      uiSymbol = 2;
2206    }
2207    codeSaoTypeIdx(uiSymbol); 
2208  }
2209
2210  if(ctbParam.modeIdc == SAO_MODE_NEW)
2211  {
2212    Int numClasses = (ctbParam.typeIdc == SAO_TYPE_BO)?4:NUM_SAO_EO_CLASSES; 
2213    Int offset[4];
2214    Int k=0;
2215    for(Int i=0; i< numClasses; i++)
2216    {
2217      if(ctbParam.typeIdc != SAO_TYPE_BO && i == SAO_CLASS_EO_PLAIN)
2218      {
2219        continue;
2220      }
2221      Int classIdx = (ctbParam.typeIdc == SAO_TYPE_BO)?(  (ctbParam.typeAuxInfo+i)% NUM_SAO_BO_CLASSES   ):i;
2222      offset[k] = ctbParam.offset[classIdx];
2223      k++;
2224    }
2225
2226    for(Int i=0; i< 4; i++)
2227    {
2228      codeSaoMaxUvlc((offset[i]<0)?(-offset[i]):(offset[i]),  g_saoMaxOffsetQVal[compIdx] ); //sao_offset_abs
2229    }
2230
2231
2232    if(ctbParam.typeIdc == SAO_TYPE_BO)
2233    {
2234      for(Int i=0; i< 4; i++)
2235      {
2236        if(offset[i] != 0)
2237        {
2238          codeSAOSign((offset[i]< 0)?1:0);
2239        }
2240      }
2241
2242      codeSaoUflc(NUM_SAO_BO_CLASSES_LOG2, ctbParam.typeAuxInfo ); //sao_band_position
2243    }
2244    else //EO
2245    {
2246      if(compIdx == SAO_Y || compIdx == SAO_Cb)
2247      {
2248        assert(ctbParam.typeIdc - SAO_TYPE_START_EO >=0);
2249        codeSaoUflc(NUM_SAO_EO_TYPES_LOG2, ctbParam.typeIdc - SAO_TYPE_START_EO ); //sao_eo_class_luma or sao_eo_class_chroma
2250      }
2251    }
2252
2253  }
2254}
2255
2256
2257Void TEncSbac::codeSAOBlkParam(SAOBlkParam& saoBlkParam
2258                              , Bool* sliceEnabled
2259                              , Bool leftMergeAvail
2260                              , Bool aboveMergeAvail
2261                              , Bool onlyEstMergeInfo // = false
2262                              )
2263{
2264
2265  Bool isLeftMerge = false;
2266  Bool isAboveMerge= false;
2267
2268  if(leftMergeAvail)
2269  {
2270    isLeftMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_LEFT));
2271    codeSaoMerge( isLeftMerge?1:0  ); //sao_merge_left_flag
2272  }
2273
2274  if( aboveMergeAvail && !isLeftMerge)
2275  {
2276    isAboveMerge = ((saoBlkParam[SAO_Y].modeIdc == SAO_MODE_MERGE) && (saoBlkParam[SAO_Y].typeIdc == SAO_MERGE_ABOVE)); 
2277    codeSaoMerge( isAboveMerge?1:0  ); //sao_merge_left_flag
2278  }
2279
2280  if(onlyEstMergeInfo)
2281  {
2282    return; //only for RDO
2283  }
2284
2285  if(!isLeftMerge && !isAboveMerge) //not merge mode
2286  {
2287    for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
2288    {
2289      codeSAOOffsetParam(compIdx, saoBlkParam[compIdx], sliceEnabled[compIdx]);
2290    }
2291  }
2292}
2293
2294#if H_3D_INTER_SDC
2295Void TEncSbac::codeDeltaDC( TComDataCU* pcCU, UInt absPartIdx )
2296{
2297  if( !( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2298  {
2299    assert( 0 );
2300  }
2301
2302  UInt uiNumSegments = 0;
2303  UInt dimDeltaDC = 0;
2304
2305  if( pcCU->isIntra( absPartIdx ) )
2306  {
2307    UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2308    uiNumSegments = isDimMode( dir ) ? 2 : 1;
2309
2310    if( pcCU->getSDCFlag( absPartIdx ) )
2311    {
2312      if( uiNumSegments == 1 )
2313      {
2314        dimDeltaDC = pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) ? 1 : 0;
2315      }
2316      else
2317      {
2318        dimDeltaDC = ( pcCU->getSDCSegmentDCOffset( 0, absPartIdx ) || pcCU->getSDCSegmentDCOffset( 1, absPartIdx ) ) ? 1 : 0;
2319      }
2320    }
2321    else
2322    {
2323      dimDeltaDC = isDimDeltaDC( dir );
2324    }
2325#if MTK_DELTA_DC_FLAG_ONE_CONTEXT_H0084_H0100_H0113
2326    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2327#else
2328    m_pcBinIf->encodeBin( dimDeltaDC, m_cDdcFlagSCModel.get( 0, 0, uiNumSegments-1 ) );
2329#endif
2330  }
2331  else //all-zero inter SDC is not allowed
2332  {
2333    uiNumSegments = 1;
2334    dimDeltaDC = 1;
2335  }
2336
2337  if( dimDeltaDC )
2338  {
2339    for( UInt segment = 0; segment < uiNumSegments; segment++ )
2340    {
2341      Pel deltaDC = 0;
2342
2343      if( pcCU->isIntra( absPartIdx ) )
2344      {
2345        UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2346        deltaDC = pcCU->getSDCFlag( absPartIdx ) ? pcCU->getSDCSegmentDCOffset( segment, absPartIdx ) : pcCU->getDimDeltaDC( getDimType( dir ), segment, absPartIdx );
2347      }
2348      else
2349      {
2350        deltaDC = pcCU->getSDCSegmentDCOffset( segment, absPartIdx );
2351      }
2352
2353      xCodeDimDeltaDC( deltaDC, uiNumSegments );
2354    }
2355  }
2356}
2357
2358Void TEncSbac::codeSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2359{
2360  UInt uiSymbol = pcCU->getSDCFlag( uiAbsPartIdx ) ? 1 : 0;
2361  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2362
2363  m_pcBinIf->encodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2364}
2365
2366#endif
2367
2368#if H_3D_DBBP
2369Void TEncSbac::codeDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
2370{
2371  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
2372  AOF( ePartSize == RWTH_DBBP_PACK_MODE );
2373  AOF( pcCU->getSlice()->getVPS()->getUseDBBP(pcCU->getSlice()->getLayerIdInVps()) );
2374  AOF( !pcCU->getSlice()->getIsDepth() );
2375 
2376  UInt uiSymbol = pcCU->getDBBPFlag( uiAbsPartIdx ) ? 1 : 0;
2377  m_pcBinIf->encodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) );
2378}
2379#endif
2380
2381//! \}
Note: See TracBrowser for help on using the repository browser.