source: 3DVCSoftware/branches/HTM-9.3-dev3-Samsung/source/Lib/TLibEncoder/TEncSbac.cpp @ 792

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

Integration of JCT3V-G0101: InterSDC with multiple DC candidates

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