source: 3DVCSoftware/branches/HTM-10.1-dev0/source/Lib/TLibEncoder/TEncSbac.cpp @ 881

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

Cleanups part 6.

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