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

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

JCT3V-I0099. Changes of sub-PU size signaling and lim_qt_pred_flag signaling in VPS extension 2. Remove use_qtl_flag and use_pc_flag.

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