source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncSbac.cpp @ 872

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

Merged HTM-10.0-dev0@871. (MV-HEVC 7 HLS)

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