source: 3DVCSoftware/trunk/source/Lib/TLibDecoder/TDecSbac.cpp @ 758

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

Merged HTM-9.1-dev0-MediaTek@757. (3D-HEVC HLS)

  • Property svn:eol-style set to native
File size: 72.5 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license. 
5 *
6 * Copyright (c) 2010-2013, ITU/ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file     TDecSbac.cpp
35    \brief    Context-adaptive entropy decoder class
36*/
37
38#include "TDecSbac.h"
39
40//! \ingroup TLibDecoder
41//! \{
42
43//////////////////////////////////////////////////////////////////////
44// Construction/Destruction
45//////////////////////////////////////////////////////////////////////
46
47TDecSbac::TDecSbac() 
48// new structure here
49: m_pcBitstream               ( 0 )
50, m_pcTDecBinIf               ( NULL )
51, m_numContextModels          ( 0 )
52, m_cCUSplitFlagSCModel       ( 1,             1,               NUM_SPLIT_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels )
53, m_cCUSkipFlagSCModel        ( 1,             1,               NUM_SKIP_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
54, m_cCUMergeFlagExtSCModel    ( 1,             1,               NUM_MERGE_FLAG_EXT_CTX        , m_contextModels + m_numContextModels, m_numContextModels)
55, m_cCUMergeIdxExtSCModel     ( 1,             1,               NUM_MERGE_IDX_EXT_CTX         , m_contextModels + m_numContextModels, m_numContextModels)
56#if H_3D_ARP
57, m_cCUPUARPWSCModel          ( 1,             1,               NUM_ARPW_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
58#endif
59#if H_3D_IC
60, m_cCUICFlagSCModel          ( 1,             1,               NUM_IC_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
61#endif
62, m_cCUPartSizeSCModel        ( 1,             1,               NUM_PART_SIZE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
63, m_cCUPredModeSCModel        ( 1,             1,               NUM_PRED_MODE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
64, m_cCUIntraPredSCModel       ( 1,             1,               NUM_ADI_CTX                   , m_contextModels + m_numContextModels, m_numContextModels)
65, m_cCUChromaPredSCModel      ( 1,             1,               NUM_CHROMA_PRED_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
66, m_cCUDeltaQpSCModel         ( 1,             1,               NUM_DELTA_QP_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
67, m_cCUInterDirSCModel        ( 1,             1,               NUM_INTER_DIR_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
68, m_cCURefPicSCModel          ( 1,             1,               NUM_REF_NO_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
69, m_cCUMvdSCModel             ( 1,             1,               NUM_MV_RES_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
70, m_cCUQtCbfSCModel           ( 1,             2,               NUM_QT_CBF_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
71, m_cCUTransSubdivFlagSCModel ( 1,             1,               NUM_TRANS_SUBDIV_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
72, m_cCUQtRootCbfSCModel       ( 1,             1,               NUM_QT_ROOT_CBF_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
73, m_cCUSigCoeffGroupSCModel   ( 1,             2,               NUM_SIG_CG_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
74, m_cCUSigSCModel             ( 1,             1,               NUM_SIG_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
75, m_cCuCtxLastX               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
76, m_cCuCtxLastY               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          , m_contextModels + m_numContextModels, m_numContextModels)
77, m_cCUOneSCModel             ( 1,             1,               NUM_ONE_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
78, m_cCUAbsSCModel             ( 1,             1,               NUM_ABS_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
79, m_cMVPIdxSCModel            ( 1,             1,               NUM_MVP_IDX_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
80, m_cCUAMPSCModel             ( 1,             1,               NUM_CU_AMP_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
81, m_cSaoMergeSCModel      ( 1,             1,               NUM_SAO_MERGE_FLAG_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cSaoTypeIdxSCModel        ( 1,             1,               NUM_SAO_TYPE_IDX_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cTransformSkipSCModel     ( 1,             2,               NUM_TRANSFORMSKIP_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
84, m_CUTransquantBypassFlagSCModel( 1,          1,               NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX, m_contextModels + m_numContextModels, m_numContextModels)
85#if H_3D_DIM
86, m_cDepthIntraModeSCModel    ( 1,             1,               NUM_DEPTH_INTRA_MODE_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cDdcFlagSCModel           ( 1,             1,               NUM_DDC_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cDdcDataSCModel           ( 1,             1,               NUM_DDC_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
89#if H_3D_DIM_DMM
90, m_cDmm1DataSCModel          ( 1,             1,               NUM_DMM1_DATA_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
91#if !SEC_DMM3_RBC_F0147
92, m_cDmm3DataSCModel          ( 1,             1,               NUM_DMM3_DATA_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
93#endif
94#endif
95#if H_3D_DIM_RBC
96, m_cRbcDataSCModel           ( 1,             1,               NUM_RBC_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
97#endif
98#if H_3D_DIM_SDC
99, m_cSDCResidualFlagSCModel     ( 1,             1,             SDC_NUM_RESIDUAL_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
100, m_cSDCResidualSCModel         ( 1,             1,             SDC_NUM_RESIDUAL_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
101#endif
102#endif
103#if H_3D_INTER_SDC
104, m_cInterSDCFlagSCModel             ( 1,             1,  NUM_INTER_SDC_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
105, m_cInterSDCResidualSCModel         ( 1,             1,  NUM_INTER_SDC_RESIDUAL_CTX       , m_contextModels + m_numContextModels, m_numContextModels)
106, m_cInterSDCResidualSignFlagSCModel ( 1,             1,  NUM_INTER_SDC_SIGN_FLAG_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
107#endif
108{
109  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
110}
111
112TDecSbac::~TDecSbac()
113{
114}
115
116// ====================================================================================================================
117// Public member functions
118// ====================================================================================================================
119
120Void TDecSbac::resetEntropy(TComSlice* pSlice)
121{
122  SliceType sliceType  = pSlice->getSliceType();
123  Int       qp         = pSlice->getSliceQp();
124
125  if (pSlice->getPPS()->getCabacInitPresentFlag() && pSlice->getCabacInitFlag())
126  {
127    switch (sliceType)
128    {
129    case P_SLICE:           // change initialization table to B_SLICE initialization
130      sliceType = B_SLICE; 
131      break;
132    case B_SLICE:           // change initialization table to P_SLICE initialization
133      sliceType = P_SLICE; 
134      break;
135    default     :           // should not occur
136      assert(0);
137    }
138  }
139
140  m_cCUSplitFlagSCModel.initBuffer       ( sliceType, qp, (UChar*)INIT_SPLIT_FLAG );
141  m_cCUSkipFlagSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_SKIP_FLAG );
142  m_cCUMergeFlagExtSCModel.initBuffer    ( sliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT );
143  m_cCUMergeIdxExtSCModel.initBuffer     ( sliceType, qp, (UChar*)INIT_MERGE_IDX_EXT );
144#if H_3D_ARP
145  m_cCUPUARPWSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_ARPW );
146#endif
147#if H_3D_IC
148  m_cCUICFlagSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_IC_FLAG );
149#endif
150  m_cCUPartSizeSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_PART_SIZE );
151  m_cCUAMPSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_CU_AMP_POS );
152  m_cCUPredModeSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_PRED_MODE );
153  m_cCUIntraPredSCModel.initBuffer       ( sliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
154  m_cCUChromaPredSCModel.initBuffer      ( sliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
155  m_cCUInterDirSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_INTER_DIR );
156  m_cCUMvdSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_MVD );
157  m_cCURefPicSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_REF_PIC );
158  m_cCUDeltaQpSCModel.initBuffer         ( sliceType, qp, (UChar*)INIT_DQP );
159  m_cCUQtCbfSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_QT_CBF );
160  m_cCUQtRootCbfSCModel.initBuffer       ( sliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
161  m_cCUSigCoeffGroupSCModel.initBuffer   ( sliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
162  m_cCUSigSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_SIG_FLAG );
163  m_cCuCtxLastX.initBuffer               ( sliceType, qp, (UChar*)INIT_LAST );
164  m_cCuCtxLastY.initBuffer               ( sliceType, qp, (UChar*)INIT_LAST );
165  m_cCUOneSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_ONE_FLAG );
166  m_cCUAbsSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_ABS_FLAG );
167  m_cMVPIdxSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_MVP_IDX );
168  m_cSaoMergeSCModel.initBuffer      ( sliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
169  m_cSaoTypeIdxSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
170
171  m_cCUTransSubdivFlagSCModel.initBuffer ( sliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
172  m_cTransformSkipSCModel.initBuffer     ( sliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
173  m_CUTransquantBypassFlagSCModel.initBuffer( sliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
174#if H_3D_DIM
175  m_cDepthIntraModeSCModel.initBuffer    ( sliceType, qp, (UChar*)INIT_DEPTH_INTRA_MODE );
176  m_cDdcFlagSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_DDC_FLAG );
177  m_cDdcDataSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_DDC_DATA );
178#if H_3D_DIM_DMM
179  m_cDmm1DataSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_DMM1_DATA );
180#if !SEC_DMM3_RBC_F0147
181  m_cDmm3DataSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_DMM3_DATA );
182#endif
183#endif
184#if H_3D_DIM_RBC
185  m_cRbcDataSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_RBC_DATA );
186#endif
187#if H_3D_DIM_SDC
188  m_cSDCResidualFlagSCModel.initBuffer    ( sliceType, qp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
189  m_cSDCResidualSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_SDC_RESIDUAL );
190#endif
191#endif
192#if H_3D_INTER_SDC
193  m_cInterSDCFlagSCModel.initBuffer       ( sliceType, qp, (UChar*)INIT_INTER_SDC_FLAG );
194  m_cInterSDCResidualSCModel.initBuffer   ( sliceType, qp, (UChar*)INIT_INTER_SDC_RESIDUAL );
195  m_cInterSDCResidualSignFlagSCModel.initBuffer ( sliceType, qp, (UChar*)INIT_INTER_SDC_SIGN_FLAG );
196#endif
197  m_uiLastDQpNonZero  = 0;
198 
199  // new structure
200  m_uiLastQp          = qp;
201 
202  m_pcTDecBinIf->start();
203}
204
205/** The function does the following: Read out terminate bit. Flush CABAC. Byte-align for next tile.
206 *  Intialize CABAC states. Start CABAC.
207 */
208Void TDecSbac::updateContextTables( SliceType eSliceType, Int iQp )
209{
210  UInt uiBit;
211  m_pcTDecBinIf->decodeBinTrm(uiBit);
212  assert(uiBit); // end_of_sub_stream_one_bit must be equal to 1
213  m_pcTDecBinIf->finish(); 
214  m_pcBitstream->readOutTrailingBits();
215  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_SPLIT_FLAG );
216  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SKIP_FLAG );
217  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_MERGE_FLAG_EXT );
218  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_MERGE_IDX_EXT );
219#if H_3D_ARP
220  m_cCUPUARPWSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_ARPW );
221#endif
222#if H_3D_IC
223  m_cCUICFlagSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_IC_FLAG );
224#endif
225  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PART_SIZE );
226  m_cCUAMPSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_CU_AMP_POS );
227  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_PRED_MODE );
228  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTRA_PRED_MODE );
229  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_CHROMA_PRED_MODE );
230  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_INTER_DIR );
231  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_MVD );
232  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_REF_PIC );
233  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DQP );
234  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_QT_CBF );
235  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_QT_ROOT_CBF );
236  m_cCUSigCoeffGroupSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_SIG_CG_FLAG );
237  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_SIG_FLAG );
238  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
239  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (UChar*)INIT_LAST );
240  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ONE_FLAG );
241  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (UChar*)INIT_ABS_FLAG );
242  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_MVP_IDX );
243  m_cSaoMergeSCModel.initBuffer      ( eSliceType, iQp, (UChar*)INIT_SAO_MERGE_FLAG );
244  m_cSaoTypeIdxSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SAO_TYPE_IDX );
245  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
246  m_cTransformSkipSCModel.initBuffer     ( eSliceType, iQp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
247  m_CUTransquantBypassFlagSCModel.initBuffer( eSliceType, iQp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
248
249#if H_3D_DIM
250  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
251  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
252  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
253#if H_3D_DIM_DMM
254  m_cDmm1DataSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_DMM1_DATA );
255#if !SEC_DMM3_RBC_F0147
256  m_cDmm3DataSCModel.initBuffer          ( eSliceType, iQp, (UChar*)INIT_DMM3_DATA );
257#endif
258#endif
259#if H_3D_DIM_RBC
260  m_cRbcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_RBC_DATA );
261#endif
262#if H_3D_DIM_SDC
263  m_cSDCResidualFlagSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
264  m_cSDCResidualSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
265#endif
266#endif
267#if H_3D_INTER_SDC
268  m_cInterSDCFlagSCModel.initBuffer       ( eSliceType, iQp, (UChar*)INIT_INTER_SDC_FLAG );
269  m_cInterSDCResidualSCModel.initBuffer   ( eSliceType, iQp, (UChar*)INIT_INTER_SDC_RESIDUAL );
270  m_cInterSDCResidualSignFlagSCModel.initBuffer ( eSliceType, iQp, (UChar*)INIT_INTER_SDC_SIGN_FLAG );
271#endif
272  m_pcTDecBinIf->start();
273}
274
275Void TDecSbac::parseTerminatingBit( UInt& ruiBit )
276{
277  m_pcTDecBinIf->decodeBinTrm( ruiBit );
278  if ( ruiBit )
279  {
280    m_pcTDecBinIf->finish();
281  }
282}
283
284
285Void TDecSbac::xReadUnaryMaxSymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
286{
287  if (uiMaxSymbol == 0)
288  {
289    ruiSymbol = 0;
290    return;
291  }
292 
293  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] );
294 
295  if( ruiSymbol == 0 || uiMaxSymbol == 1 )
296  {
297    return;
298  }
299 
300  UInt uiSymbol = 0;
301  UInt uiCont;
302 
303  do
304  {
305    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] );
306    uiSymbol++;
307  }
308  while( uiCont && ( uiSymbol < uiMaxSymbol - 1 ) );
309 
310  if( uiCont && ( uiSymbol == uiMaxSymbol - 1 ) )
311  {
312    uiSymbol++;
313  }
314 
315  ruiSymbol = uiSymbol;
316}
317
318Void TDecSbac::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount )
319{
320  UInt uiSymbol = 0;
321  UInt uiBit = 1;
322 
323  while( uiBit )
324  {
325    m_pcTDecBinIf->decodeBinEP( uiBit );
326    uiSymbol += uiBit << uiCount++;
327  }
328 
329  if ( --uiCount )
330  {
331    UInt bins;
332    m_pcTDecBinIf->decodeBinsEP( bins, uiCount );
333    uiSymbol += bins;
334  }
335 
336  ruiSymbol = uiSymbol;
337}
338
339Void TDecSbac::xReadUnarySymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset )
340{
341  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] );
342 
343  if( !ruiSymbol )
344  {
345    return;
346  }
347 
348  UInt uiSymbol = 0;
349  UInt uiCont;
350 
351  do
352  {
353    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] );
354    uiSymbol++;
355  }
356  while( uiCont );
357 
358  ruiSymbol = uiSymbol;
359}
360
361
362/** Parsing of coeff_abs_level_remaing
363 * \param ruiSymbol reference to coeff_abs_level_remaing
364 * \param ruiParam reference to parameter
365 * \returns Void
366 */
367Void TDecSbac::xReadCoefRemainExGolomb ( UInt &rSymbol, UInt &rParam )
368{
369
370  UInt prefix   = 0;
371  UInt codeWord = 0;
372  do
373  {
374    prefix++;
375    m_pcTDecBinIf->decodeBinEP( codeWord );
376  }
377  while( codeWord);
378  codeWord  = 1 - codeWord;
379  prefix -= codeWord;
380  codeWord=0;
381  if (prefix < COEF_REMAIN_BIN_REDUCTION )
382  {
383    m_pcTDecBinIf->decodeBinsEP(codeWord,rParam);
384    rSymbol = (prefix<<rParam) + codeWord;
385  }
386  else
387  {
388    m_pcTDecBinIf->decodeBinsEP(codeWord,prefix-COEF_REMAIN_BIN_REDUCTION+rParam);
389    rSymbol = (((1<<(prefix-COEF_REMAIN_BIN_REDUCTION))+COEF_REMAIN_BIN_REDUCTION-1)<<rParam)+codeWord;
390  }
391}
392
393#if H_3D_DIM
394Void TDecSbac::xReadExGolombLevel( UInt& ruiSymbol, ContextModel& rcSCModel  )
395{
396  UInt uiSymbol;
397  UInt uiCount = 0;
398  do
399  {
400    m_pcTDecBinIf->decodeBin( uiSymbol, rcSCModel );
401    uiCount++;
402  }
403  while( uiSymbol && ( uiCount != 13 ) );
404
405  ruiSymbol = uiCount - 1;
406
407  if( uiSymbol )
408  {
409    xReadEpExGolomb( uiSymbol, 0 );
410    ruiSymbol += uiSymbol + 1;
411  }
412
413  return;
414}
415
416#if QC_DIM_DELTADC_UNIFY_F0132
417Void TDecSbac::xParseDimDeltaDC( Pel& rValDeltaDC, UInt uiNumSeg )
418{
419  UInt absValDeltaDC = 0;
420  xReadExGolombLevel( absValDeltaDC, m_cDdcDataSCModel.get(0, 0, 0) );
421  rValDeltaDC = (Pel)absValDeltaDC + ( uiNumSeg > 1 ? 0 : 1 );
422
423  if( rValDeltaDC != 0 )
424  {
425    UInt uiSign;
426    m_pcTDecBinIf->decodeBinEP( uiSign );
427    if ( uiSign )
428    {
429      rValDeltaDC = -rValDeltaDC;
430    }
431  }
432}
433#else
434Void TDecSbac::xParseDimDeltaDC( Pel& rValDeltaDC, UInt dimType )
435{
436  UInt absValDeltaDC = 0;
437  xReadExGolombLevel( absValDeltaDC, m_cDdcDataSCModel.get(0, 0, (RBC_IDX == dimType) ? 1 : 0) );
438  rValDeltaDC = (Pel)absValDeltaDC;
439
440  if( rValDeltaDC != 0 )
441  {
442    UInt uiSign;
443    m_pcTDecBinIf->decodeBinEP( uiSign );
444    if ( uiSign )
445    {
446      rValDeltaDC = -rValDeltaDC;
447    }
448  }
449}
450#endif
451
452#if H_3D_DIM_DMM
453Void TDecSbac::xParseDmm1WedgeIdx( UInt& ruiTabIdx, Int iNumBit )
454{
455  UInt uiSymbol, uiIdx = 0;
456  for( Int i = 0; i < iNumBit; i++ )
457  {
458    m_pcTDecBinIf->decodeBin( uiSymbol, m_cDmm1DataSCModel.get(0, 0, 0) );
459    uiIdx += uiSymbol << i;
460  }
461  ruiTabIdx = uiIdx;
462}
463#if !SEC_DMM3_RBC_F0147
464Void TDecSbac::xParseDmm3WedgeIdx( UInt& ruiIntraIdx, Int iNumBit )
465{
466  UInt uiSymbol, uiIdx = 0;
467  for( Int i = 0; i < iNumBit; i++ )
468  {
469    m_pcTDecBinIf->decodeBin( uiSymbol, m_cDmm3DataSCModel.get(0, 0, 0) );
470    uiIdx += uiSymbol << i;
471  }
472  ruiIntraIdx = uiIdx;
473}
474#endif
475#endif
476#if H_3D_DIM_RBC
477Void TDecSbac::xParseRbcEdge( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
478{
479  UInt uiSymbol = 0;
480
481  // 1. Top(0) or Left(1)
482  UChar ucLeft;
483  m_pcTDecBinIf->decodeBinEP( uiSymbol );
484  ucLeft = uiSymbol;
485
486  // 2. Start position (lowest bit first)
487  UChar ucStart = 0;
488  for( UInt ui = 0; ui < 6 - uiDepth; ui++ )
489  {
490    m_pcTDecBinIf->decodeBinEP( uiSymbol );
491    ucStart |= (uiSymbol << ui);
492  }
493
494  // 3. Number of edges
495  UChar ucMax = 0;
496  for( UInt ui = 0; ui < 7 - uiDepth; ui++ )
497  {
498    m_pcTDecBinIf->decodeBinEP( uiSymbol );
499    ucMax |= (uiSymbol << ui);
500  }
501  ucMax++; // +1
502
503  // 4. Edges
504  UChar* pucSymbolList = (UChar*) xMalloc( UChar, 256 * RBC_MAX_EDGE_NUM_PER_4x4 );
505  for( Int iPtr = 0; iPtr < ucMax; iPtr++ )
506  {
507    UChar ucEdge = 0;
508    UInt  uiReorderEdge = 0;
509    for( UInt ui = 0; ui < 6; ui++ )
510    {
511      m_pcTDecBinIf->decodeBin( uiSymbol, m_cRbcDataSCModel.get( 0, 0, 0 ) );
512      ucEdge <<= 1;
513      ucEdge |= uiSymbol;
514      if( uiSymbol == 0 )
515        break;
516    }
517
518    switch( ucEdge )
519    {
520    case 0 :  // "0"       
521      uiReorderEdge = 0;
522      break;
523    case 2 :  // "10"
524      uiReorderEdge = 1;
525      break;
526    case 6 :  // "110"
527      uiReorderEdge = 2;
528      break;
529    case 14 : // "1110"
530      uiReorderEdge = 3;
531      break;
532    case 30 : // "11110"
533      uiReorderEdge = 4;
534      break;
535    case 62 : // "111110"
536      uiReorderEdge = 5;
537      break;
538    case 63 : // "111111"
539      uiReorderEdge = 6;
540      break;
541    default :
542      printf("parseIntraEdgeChain: error (unknown code %d)\n",ucEdge);
543      assert(false);
544      break;
545    }
546    pucSymbolList[iPtr] = uiReorderEdge;
547  }
548  /////////////////////
549  // Edge Reconstruction
550  Bool* pbRegion = pcCU->getEdgePartition( uiAbsPartIdx );
551  pcCU->reconPartition( uiAbsPartIdx, uiDepth, ucLeft == 1, ucStart, ucMax, pucSymbolList, pbRegion );
552  xFree( pucSymbolList );
553}
554#endif
555#if H_3D_DIM_SDC
556Void TDecSbac::xParseSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSegment )
557{
558  assert( pcCU->getSlice()->getIsDepth() );
559  assert( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N );
560  assert( pcCU->getSDCFlag(uiAbsPartIdx) );
561  assert( uiSegment < 2 );
562 
563  UInt uiResidual = 0;
564  UInt uiBit      = 0;
565  UInt uiAbsIdx   = 0;
566  UInt uiSign     = 0;
567  Int  iIdx       = 0;
568 
569#if H_3D_DIM_DLT
570#if DLT_DIFF_CODING_IN_PPS
571  UInt uiMaxResidualBits = pcCU->getSlice()->getPPS()->getDLT()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
572#else
573  UInt uiMaxResidualBits = pcCU->getSlice()->getVPS()->getBitsPerDepthValue( pcCU->getSlice()->getLayerIdInVps() );
574#endif
575#else
576  UInt uiMaxResidualBits = g_bitDepthY;
577#endif
578  assert( uiMaxResidualBits <= g_bitDepthY );
579 
580  m_pcTDecBinIf->decodeBin(uiResidual, m_cSDCResidualFlagSCModel.get( 0, 0, 0 ) );
581 
582#if H_MV_ENC_DEC_TRAC
583  DTRACE_CU("sdc_residual_flag[i]", uiResidual)
584#endif
585 
586  if (uiResidual)
587  {
588    // decode residual sign bit
589    m_pcTDecBinIf->decodeBinEP(uiSign);
590#if H_MV_ENC_DEC_TRAC
591    DTRACE_CU("sdc_residual_sign_flag[i]", uiSign)
592#endif
593   
594    // decode residual magnitude
595    // prefix part
596    UInt uiCount = 0;
597#if H_3D_DIM_DLT
598#if DLT_DIFF_CODING_IN_PPS
599    UInt uiNumDepthValues = pcCU->getSlice()->getPPS()->getDLT()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
600#else
601    UInt uiNumDepthValues = pcCU->getSlice()->getVPS()->getNumDepthValues( pcCU->getSlice()->getLayerIdInVps() );
602#endif
603#else
604    UInt uiNumDepthValues = ((1 << g_bitDepthY)-1);
605#endif
606    UInt uiPrefixThreshold = ((uiNumDepthValues * 3) >> 2);
607    for ( UInt ui = 0; ui < uiPrefixThreshold; ui++)
608    {
609      m_pcTDecBinIf->decodeBin( uiBit, m_cSDCResidualSCModel.get(0, 0, 0) );
610      if ( uiBit == 0 )
611        break;
612      else
613        uiCount++;
614    }
615    // suffix part
616    if ( uiCount == uiPrefixThreshold )
617    {
618      for ( UInt ui = 0; ui < numBitsForValue(uiNumDepthValues - uiPrefixThreshold); ui++ )
619      {
620        m_pcTDecBinIf->decodeBinEP( uiBit );
621        uiAbsIdx |= uiBit << ui;
622      }
623      uiAbsIdx += uiCount;
624    }
625    else
626    {
627      uiAbsIdx = uiCount;
628    }
629 
630#if H_MV_ENC_DEC_TRAC
631    DTRACE_CU("sdc_residual_abs_minus1[i]", uiAbsIdx)
632#endif
633   
634    uiAbsIdx += 1;
635    iIdx =(Int)(uiSign ? -1 : 1)*uiAbsIdx;
636  }
637 
638  pcCU->setSDCSegmentDCOffset(iIdx, uiSegment, uiAbsPartIdx);
639}
640#endif
641#endif
642
643/** Parse I_PCM information.
644 * \param pcCU
645 * \param uiAbsPartIdx
646 * \param uiDepth
647 * \returns Void
648 *
649 * If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
650 */
651Void TDecSbac::parseIPCMInfo ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
652{
653  UInt uiSymbol;
654
655    m_pcTDecBinIf->decodeBinTrm(uiSymbol);
656
657#if H_MV_ENC_DEC_TRAC
658      DTRACE_CU("pcm_flag", uiSymbol)
659#endif
660    if (uiSymbol)
661    {
662    Bool bIpcmFlag = true;
663
664    pcCU->setPartSizeSubParts  ( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
665    pcCU->setSizeSubParts      ( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
666    pcCU->setTrIdxSubParts     ( 0, uiAbsPartIdx, uiDepth );
667    pcCU->setIPCMFlagSubParts  ( bIpcmFlag, uiAbsPartIdx, uiDepth );
668
669    UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
670    UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
671    UInt uiChromaOffset = uiLumaOffset>>2;
672
673    Pel* piPCMSample;
674    UInt uiWidth;
675    UInt uiHeight;
676    UInt uiSampleBits;
677    UInt uiX, uiY;
678
679    piPCMSample = pcCU->getPCMSampleY() + uiLumaOffset;
680    uiWidth = pcCU->getWidth(uiAbsPartIdx);
681    uiHeight = pcCU->getHeight(uiAbsPartIdx);
682    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
683
684    for(uiY = 0; uiY < uiHeight; uiY++)
685    {
686      for(uiX = 0; uiX < uiWidth; uiX++)
687      {
688        UInt uiSample;
689        m_pcTDecBinIf->xReadPCMCode(uiSampleBits, uiSample);
690        piPCMSample[uiX] = uiSample;
691      }
692      piPCMSample += uiWidth;
693    }
694
695    piPCMSample = pcCU->getPCMSampleCb() + uiChromaOffset;
696    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
697    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
698    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
699
700    for(uiY = 0; uiY < uiHeight; uiY++)
701    {
702      for(uiX = 0; uiX < uiWidth; uiX++)
703      {
704        UInt uiSample;
705        m_pcTDecBinIf->xReadPCMCode(uiSampleBits, uiSample);
706        piPCMSample[uiX] = uiSample;
707      }
708      piPCMSample += uiWidth;
709    }
710
711    piPCMSample = pcCU->getPCMSampleCr() + uiChromaOffset;
712    uiWidth = pcCU->getWidth(uiAbsPartIdx)/2;
713    uiHeight = pcCU->getHeight(uiAbsPartIdx)/2;
714    uiSampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
715
716    for(uiY = 0; uiY < uiHeight; uiY++)
717    {
718      for(uiX = 0; uiX < uiWidth; uiX++)
719      {
720        UInt uiSample;
721        m_pcTDecBinIf->xReadPCMCode(uiSampleBits, uiSample);
722        piPCMSample[uiX] = uiSample;
723      }
724      piPCMSample += uiWidth;
725    }
726
727    m_pcTDecBinIf->start();
728  }
729}
730
731Void TDecSbac::parseCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
732{
733  UInt uiSymbol;
734  m_pcTDecBinIf->decodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) );
735#if H_MV_ENC_DEC_TRAC
736  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
737#endif
738  pcCU->setCUTransquantBypassSubParts(uiSymbol ? true : false, uiAbsPartIdx, uiDepth);
739}
740
741/** parse skip flag
742 * \param pcCU
743 * \param uiAbsPartIdx
744 * \param uiDepth
745 * \returns Void
746 */
747Void TDecSbac::parseSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
748{
749  if( pcCU->getSlice()->isIntra() )
750  {
751    return;
752  }
753 
754  UInt uiSymbol = 0;
755  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx );
756  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
757#if !H_MV_ENC_DEC_TRAC
758  DTRACE_CABAC_VL( g_nSymbolCounter++ );
759  DTRACE_CABAC_T( "\tSkipFlag" );
760  DTRACE_CABAC_T( "\tuiCtxSkip: ");
761  DTRACE_CABAC_V( uiCtxSkip );
762  DTRACE_CABAC_T( "\tuiSymbol: ");
763  DTRACE_CABAC_V( uiSymbol );
764  DTRACE_CABAC_T( "\n");
765#endif
766 
767  if( uiSymbol )
768  {
769    pcCU->setSkipFlagSubParts( true,        uiAbsPartIdx, uiDepth );
770    pcCU->setPredModeSubParts( MODE_INTER,  uiAbsPartIdx, uiDepth );
771    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
772    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
773    pcCU->setMergeFlagSubParts( true , uiAbsPartIdx, 0, uiDepth );
774  }
775#if H_MV_ENC_DEC_TRAC
776  DTRACE_CU("cu_skip_flag", uiSymbol); 
777#endif
778}
779
780/** parse merge flag
781 * \param pcCU
782 * \param uiAbsPartIdx
783 * \param uiDepth
784 * \param uiPUIdx
785 * \returns Void
786 */
787Void TDecSbac::parseMergeFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiPUIdx )
788{
789  UInt uiSymbol;
790  m_pcTDecBinIf->decodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) );
791#if H_MV_ENC_DEC_TRAC
792  DTRACE_PU("merge_flag", uiSymbol)
793#endif
794  pcCU->setMergeFlagSubParts( uiSymbol ? true : false, uiAbsPartIdx, uiPUIdx, uiDepth );
795
796#if !H_MV_ENC_DEC_TRAC
797  DTRACE_CABAC_VL( g_nSymbolCounter++ );
798  DTRACE_CABAC_T( "\tMergeFlag: " );
799  DTRACE_CABAC_V( uiSymbol );
800  DTRACE_CABAC_T( "\tAddress: " );
801  DTRACE_CABAC_V( pcCU->getAddr() );
802  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
803  DTRACE_CABAC_V( uiAbsPartIdx );
804  DTRACE_CABAC_T( "\n" );
805#endif
806}
807
808Void TDecSbac::parseMergeIndex ( TComDataCU* pcCU, UInt& ruiMergeIndex )
809{
810  UInt uiUnaryIdx = 0;
811  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
812  if ( uiNumCand > 1 )
813  {
814    for( ; uiUnaryIdx < uiNumCand - 1; ++uiUnaryIdx )
815    {
816      UInt uiSymbol = 0;
817      if ( uiUnaryIdx==0 )
818      {
819        m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) );
820      }
821      else
822      {
823        m_pcTDecBinIf->decodeBinEP( uiSymbol );
824      }
825      if( uiSymbol == 0 )
826      {
827        break;
828      }
829    }
830#if H_MV_ENC_DEC_TRAC
831    DTRACE_PU("merge_idx", uiUnaryIdx)
832#endif
833  }
834  ruiMergeIndex = uiUnaryIdx;
835
836#if !H_MV_ENC_DEC_TRAC
837  DTRACE_CABAC_VL( g_nSymbolCounter++ )
838  DTRACE_CABAC_T( "\tparseMergeIndex()" )
839  DTRACE_CABAC_T( "\tuiMRGIdx= " )
840  DTRACE_CABAC_V( ruiMergeIndex )
841  DTRACE_CABAC_T( "\n" )
842#endif
843}
844
845Void TDecSbac::parseMVPIdx      ( Int& riMVPIdx )
846{
847  UInt uiSymbol;
848  xReadUnaryMaxSymbol(uiSymbol, m_cMVPIdxSCModel.get(0), 1, AMVP_MAX_NUM_CANDS-1);
849  riMVPIdx = uiSymbol;
850}
851
852Void TDecSbac::parseSplitFlag     ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
853{
854  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
855  {
856    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
857    return;
858  }
859
860  UInt uiSymbol;
861#if H_3D_QTLPC
862  Bool bParseSplitFlag    = true;
863
864  TComSPS *sps            = pcCU->getPic()->getSlice(0)->getSPS();
865  TComPic *pcTexture      = pcCU->getSlice()->getTexturePic();
866  Bool bDepthMapDetect    = (pcTexture != NULL);
867  Bool bIntraSliceDetect  = (pcCU->getSlice()->getSliceType() == I_SLICE);
868
869  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);
870
871  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL() && sps->getUsePC())
872  {
873    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
874    assert(pcTextureCU->getDepth(uiAbsPartIdx) >= uiDepth);
875    bParseSplitFlag         = (pcTextureCU->getDepth(uiAbsPartIdx) > uiDepth);
876  }
877
878  if(bParseSplitFlag)
879  {
880#endif
881    m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSplitFlagSCModel.get( 0, 0, pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ) ) );
882#if H_MV_ENC_DEC_TRAC
883    DTRACE_CU("split_cu_flag", uiSymbol); 
884#else
885    DTRACE_CABAC_VL( g_nSymbolCounter++ )
886    DTRACE_CABAC_T( "\tSplitFlag\n" )
887#endif
888#if H_3D_QTLPC
889  }
890  else
891  {
892    uiSymbol = 0;
893  }
894#endif
895  pcCU->setDepthSubParts( uiDepth + uiSymbol, uiAbsPartIdx );
896 
897  return;
898}
899
900/** parse partition size
901 * \param pcCU
902 * \param uiAbsPartIdx
903 * \param uiDepth
904 * \returns Void
905 */
906Void TDecSbac::parsePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
907{
908  UInt uiSymbol, uiMode = 0;
909  PartSize eMode;
910
911#if H_3D_QTLPC
912  Bool bParsePartSize    = true;
913  TComSPS *sps           = pcCU->getPic()->getSlice(0)->getSPS();
914  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
915  Bool bDepthMapDetect   = (pcTexture != NULL);
916  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
917
918  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);
919
920  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && sps->getUseQTL() && sps->getUsePC())
921  {
922    TComDataCU *pcTextureCU = pcTexture->getCU(pcCU->getAddr());
923    assert(pcTextureCU->getDepth(uiAbsPartIdx) >= uiDepth);
924    if (pcTextureCU->getDepth(uiAbsPartIdx) == uiDepth && pcTextureCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN)
925    {
926      bParsePartSize = false;
927      eMode          = SIZE_2Nx2N;
928    }
929  }
930#endif
931
932 
933  if ( pcCU->isIntra( uiAbsPartIdx ) )
934  {
935#if H_3D_QTLPC
936    if(bParsePartSize)
937    {
938#endif
939     uiSymbol = 1;
940      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
941      {
942        m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 0) );
943#if H_MV_ENC_DEC_TRAC         
944        DTRACE_CU("part_mode", uiSymbol)
945#endif       
946      }
947      eMode = uiSymbol ? SIZE_2Nx2N : SIZE_NxN;
948#if H_3D_QTLPC
949    }
950#endif
951    UInt uiTrLevel = 0;   
952    UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)]+2;
953    UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()]+2;
954    uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
955    if( eMode == SIZE_NxN )
956    {
957      pcCU->setTrIdxSubParts( 1+uiTrLevel, uiAbsPartIdx, uiDepth );
958    }
959    else
960    {
961      pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );
962    }
963  }
964  else
965  {
966#if H_3D_QTLPC
967    if(bParsePartSize)
968    {
969#endif
970      UInt uiMaxNumBits = 2;
971      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && !( (g_uiMaxCUWidth>>uiDepth) == 8 && (g_uiMaxCUHeight>>uiDepth) == 8 ) )
972      {
973        uiMaxNumBits ++;
974      }
975      for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
976      {
977        m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, ui) );
978        if ( uiSymbol )
979        {
980          break;
981        }
982        uiMode++;
983      }
984      eMode = (PartSize) uiMode;
985      if ( pcCU->getSlice()->getSPS()->getAMPAcc( uiDepth ) )
986      {
987        if (eMode == SIZE_2NxN)
988        {
989          m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUAMPSCModel.get( 0, 0, 0 ));
990          if (uiSymbol == 0)
991          {
992            m_pcTDecBinIf->decodeBinEP(uiSymbol);
993            eMode = (uiSymbol == 0? SIZE_2NxnU : SIZE_2NxnD);
994          }
995        }
996        else if (eMode == SIZE_Nx2N)
997        {
998          m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUAMPSCModel.get( 0, 0, 0 ));
999          if (uiSymbol == 0)
1000          {
1001            m_pcTDecBinIf->decodeBinEP(uiSymbol);
1002            eMode = (uiSymbol == 0? SIZE_nLx2N : SIZE_nRx2N);
1003          }
1004        }
1005      }
1006#if H_MV_ENC_DEC_TRAC         
1007      DTRACE_CU("part_mode", eMode )
1008#endif
1009#if H_3D_QTLPC
1010    }
1011#endif
1012  }
1013  pcCU->setPartSizeSubParts( eMode, uiAbsPartIdx, uiDepth );
1014  pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
1015}
1016
1017/** parse prediction mode
1018 * \param pcCU
1019 * \param uiAbsPartIdx
1020 * \param uiDepth
1021 * \returns Void
1022 */
1023Void TDecSbac::parsePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1024{
1025  if( pcCU->getSlice()->isIntra() )
1026  {
1027    pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
1028    return;
1029  }
1030 
1031  UInt uiSymbol;
1032  Int  iPredMode = MODE_INTER;
1033  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPredModeSCModel.get( 0, 0, 0 ) );
1034  iPredMode += uiSymbol;
1035#if H_MV_ENC_DEC_TRAC         
1036  DTRACE_CU("pred_mode_flag", uiSymbol)
1037#endif       
1038  pcCU->setPredModeSubParts( (PredMode)iPredMode, uiAbsPartIdx, uiDepth );
1039}
1040
1041Void TDecSbac::parseIntraDirLumaAng  ( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
1042{
1043  PartSize mode = pcCU->getPartitionSize( absPartIdx );
1044  UInt partNum = mode==SIZE_NxN?4:1;
1045  UInt partOffset = ( pcCU->getPic()->getNumPartInCU() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1046  UInt mpmPred[4],symbol;
1047  Int j,intraPredMode;   
1048  if (mode==SIZE_NxN)
1049  {
1050    depth++;
1051  }
1052  for (j=0;j<partNum;j++)
1053  {
1054#if H_3D_DIM
1055    if( pcCU->getSlice()->getVpsDepthModesFlag() )
1056    {
1057      parseIntraDepth( pcCU, absPartIdx+partOffset*j, depth );
1058    }
1059    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1060#if H_3D_DIM_SDC
1061      if( !pcCU->getSDCFlag( absPartIdx+partOffset*j ) )
1062#endif
1063    {
1064#endif
1065    m_pcTDecBinIf->decodeBin( symbol, m_cCUIntraPredSCModel.get( 0, 0, 0) );
1066    mpmPred[j] = symbol;
1067#if H_MV_ENC_DEC_TRAC         
1068    DTRACE_CU("prev_intra_luma_pred_flag", symbol)
1069#endif
1070#if H_3D_DIM
1071    }
1072#endif
1073  }
1074  for (j=0;j<partNum;j++)
1075  {
1076#if H_3D_DIM
1077    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1078#if H_3D_DIM_SDC
1079      if( !pcCU->getSDCFlag( absPartIdx+partOffset*j ) )
1080#endif
1081    {
1082#endif
1083    Int preds[3] = {-1, -1, -1};
1084    Int predNum = pcCU->getIntraDirLumaPredictor(absPartIdx+partOffset*j, preds); 
1085    if (mpmPred[j])
1086    {
1087      m_pcTDecBinIf->decodeBinEP( symbol );
1088      if (symbol)
1089      {
1090        m_pcTDecBinIf->decodeBinEP( symbol );
1091        symbol++;
1092      }
1093#if H_MV_ENC_DEC_TRAC         
1094      DTRACE_CU("mpm_idx", symbol)
1095#endif
1096      intraPredMode = preds[symbol];
1097    }
1098    else
1099    {
1100      m_pcTDecBinIf->decodeBinsEP( symbol, 5 );
1101      intraPredMode = symbol;
1102#if H_MV_ENC_DEC_TRAC         
1103      DTRACE_CU("rem_intra_luma_pred_mode", symbol)
1104#endif       
1105      //postponed sorting of MPMs (only in remaining branch)
1106      if (preds[0] > preds[1])
1107      { 
1108        std::swap(preds[0], preds[1]); 
1109      }
1110      if (preds[0] > preds[2])
1111      {
1112        std::swap(preds[0], preds[2]);
1113      }
1114      if (preds[1] > preds[2])
1115      {
1116        std::swap(preds[1], preds[2]);
1117      }
1118      for ( Int i = 0; i < predNum; i++ )
1119      {
1120        intraPredMode += ( intraPredMode >= preds[i] );
1121      }
1122    }
1123    pcCU->setLumaIntraDirSubParts( (UChar)intraPredMode, absPartIdx+partOffset*j, depth );
1124#if H_3D_DIM
1125    }
1126#endif
1127  }
1128}
1129
1130Void TDecSbac::parseIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1131{
1132  UInt uiSymbol;
1133
1134  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUChromaPredSCModel.get( 0, 0, 0 ) );
1135
1136  if( uiSymbol == 0 )
1137  {
1138#if H_MV_ENC_DEC_TRAC         
1139    DTRACE_CU("intra_chroma_pred_mode", uiSymbol )
1140#endif       
1141    uiSymbol = DM_CHROMA_IDX;
1142  } 
1143  else 
1144  {
1145    {
1146      UInt uiIPredMode;
1147      m_pcTDecBinIf->decodeBinsEP( uiIPredMode, 2 );
1148#if H_MV_ENC_DEC_TRAC         
1149      DTRACE_CU("intra_chroma_pred_mode", uiIPredMode )
1150#endif       
1151      UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1152      pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1153      uiSymbol = uiAllowedChromaDir[ uiIPredMode ];
1154    }
1155  }
1156  pcCU->setChromIntraDirSubParts( uiSymbol, uiAbsPartIdx, uiDepth );
1157  return;
1158}
1159
1160#if H_3D_DIM
1161Void TDecSbac::parseIntraDepth( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
1162{
1163  parseIntraDepthMode( pcCU, absPartIdx, depth );
1164
1165  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1166  UInt dimType = getDimType( dir );
1167
1168  switch( dimType )
1169  {
1170#if H_3D_DIM_DMM
1171  case( DMM1_IDX ):
1172    {
1173      UInt uiTabIdx = 0;
1174      xParseDmm1WedgeIdx( uiTabIdx, g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1175      pcCU->setDmmWedgeTabIdxSubParts( uiTabIdx, dimType, absPartIdx, depth );
1176    } break;
1177#if !SEC_DMM3_RBC_F0147
1178  case( DMM3_IDX ):
1179    {
1180      UInt uiIntraIdx = 0;
1181      xParseDmm3WedgeIdx( uiIntraIdx, g_dmm3IntraTabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1182      pcCU->setDmm3IntraTabIdxSubParts( uiIntraIdx, absPartIdx, depth );
1183    } break;
1184#endif
1185  case( DMM4_IDX ): break;
1186#endif
1187#if H_3D_DIM_RBC
1188  case( RBC_IDX ): 
1189    {
1190      xParseRbcEdge( pcCU, absPartIdx, depth );
1191    } break;
1192#endif
1193  default: break;
1194  }
1195
1196#if H_3D_DIM_SDC && !QC_DIM_DELTADC_UNIFY_F0132
1197  if( pcCU->getSDCFlag(absPartIdx) )
1198  {
1199    assert(pcCU->getPartitionSize(absPartIdx)==SIZE_2Nx2N);
1200    pcCU->setTrIdxSubParts(0, absPartIdx, depth);
1201    pcCU->setCbfSubParts(1, 1, 1, absPartIdx, depth);
1202
1203    UInt uiNumSegments = ( dir == DC_IDX || dir == PLANAR_IDX )? 1 : 2;
1204    for (UInt uiSeg=0; uiSeg<uiNumSegments; uiSeg++)
1205    {
1206      xParseSDCResidualData(pcCU, absPartIdx, depth, uiSeg);
1207    }
1208  }
1209  else
1210  {
1211#endif
1212#if QC_DIM_DELTADC_UNIFY_F0132
1213    if( dimType < DIM_NUM_TYPE || pcCU->getSDCFlag( absPartIdx ) )
1214#else
1215    if( dimType < DIM_NUM_TYPE )
1216#endif
1217    {
1218      UInt symbol;
1219#if QC_DIM_DELTADC_UNIFY_F0132
1220      UInt uiNumSegments = ( dir == PLANAR_IDX ) ? 1 : 2;
1221
1222      if( pcCU->getSDCFlag( absPartIdx ) )
1223      {
1224        assert(pcCU->getPartitionSize(absPartIdx)==SIZE_2Nx2N);
1225        pcCU->setTrIdxSubParts(0, absPartIdx, depth);
1226        pcCU->setCbfSubParts(1, 1, 1, absPartIdx, depth);
1227      }
1228
1229      m_pcTDecBinIf->decodeBin( symbol, m_cDdcFlagSCModel.get(0, 0, uiNumSegments-1) );
1230
1231      if( symbol )
1232      {
1233        if( !pcCU->getSDCFlag( absPartIdx ) )
1234        {
1235          dir += symbol;
1236        }
1237      }
1238      for( UInt segment = 0; segment < uiNumSegments; segment++ )
1239      {
1240        Pel valDeltaDC = 0;
1241        if( symbol )
1242        {
1243          xParseDimDeltaDC( valDeltaDC, uiNumSegments );
1244        }
1245
1246        if( pcCU->getSDCFlag( absPartIdx ) )
1247        {
1248          pcCU->setSDCSegmentDCOffset( valDeltaDC, segment, absPartIdx );
1249        }
1250        else
1251        {
1252          pcCU->setDimDeltaDC( dimType, segment, absPartIdx, valDeltaDC );
1253        }
1254      }
1255#else
1256      m_pcTDecBinIf->decodeBin( symbol, m_cDdcFlagSCModel.get(0, 0, (RBC_IDX == dimType) ? 1 : 0) );
1257      if( symbol )
1258      {
1259        dir += symbol;
1260        for( UInt segment = 0; segment < 2; segment++ )
1261        {
1262          Pel valDeltaDC = 0;
1263          xParseDimDeltaDC( valDeltaDC, dimType );
1264          pcCU->setDimDeltaDC( dimType, segment, absPartIdx, valDeltaDC );
1265        }
1266      }
1267#endif
1268    }
1269#if H_3D_DIM_SDC && !QC_DIM_DELTADC_UNIFY_F0132
1270  }
1271#endif
1272
1273  pcCU->setLumaIntraDirSubParts( (UChar)dir, absPartIdx, depth );
1274}
1275
1276Void TDecSbac::parseIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
1277{
1278  UInt puIdx = (pcCU->getWidth(absPartIdx) == 64) ? 2 : ( (pcCU->getPartitionSize(absPartIdx) == SIZE_NxN && pcCU->getWidth(absPartIdx) == 8) ? 0 : 1 );
1279  UInt dir = 0;
1280  Bool sdcFlag = 0;
1281  UInt symbol = 1;
1282  UInt modeCode = 0 ;
1283  UInt binNum = 0;
1284  UInt ctxDepthMode = 0;
1285
1286  if( puIdx == 2 )
1287  {
1288    {
1289      ctxDepthMode = puIdx*3 + binNum;
1290      m_pcTDecBinIf->decodeBin( symbol, m_cDepthIntraModeSCModel.get(0,0,ctxDepthMode) );
1291      modeCode = (modeCode<<1) + symbol;
1292      binNum++;
1293    }
1294         if( modeCode == 0 ) { dir = PLANAR_IDX; sdcFlag = 1;}
1295    else if( modeCode == 1 ) { dir = 0;          sdcFlag = 0;}
1296  }
1297  else if( puIdx == 0 )
1298  {
1299#if SEC_DMM3_RBC_F0147
1300    while( binNum < 1 && symbol )
1301#else
1302    while( binNum < 3 && symbol )
1303#endif
1304    {
1305      ctxDepthMode = puIdx*3 + ((binNum >= 2) ? 2 : binNum);
1306      m_pcTDecBinIf->decodeBin( symbol, m_cDepthIntraModeSCModel.get(0,0,ctxDepthMode) );
1307      modeCode = (modeCode<<1) + symbol;
1308      binNum++;
1309    }
1310#if SEC_DMM3_RBC_F0147
1311    if( modeCode == 0 ) { dir = 0;                       sdcFlag = 0;}
1312    else if( modeCode == 1 ) { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 0;}
1313#else
1314         if( modeCode == 0 ) { dir = 0;                       sdcFlag = 0;}
1315    else if( modeCode == 2 ) { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 0;}
1316    else if( modeCode == 6 ) { dir = (2*DMM3_IDX+DIM_OFFSET); sdcFlag = 0;}
1317    else if( modeCode == 7 ) { dir = (2* RBC_IDX+DIM_OFFSET); sdcFlag = 0;}
1318#endif
1319  }
1320  else
1321  {
1322#if SEC_DMM3_RBC_F0147
1323    while( binNum < 4 && symbol )
1324    {
1325      ctxDepthMode = puIdx*3 + ((binNum >= 2) ? 2 : binNum);
1326      m_pcTDecBinIf->decodeBin( symbol, m_cDepthIntraModeSCModel.get(0,0,ctxDepthMode) );
1327      modeCode = (modeCode<<1) + symbol;
1328      binNum++;
1329    }
1330    if ( modeCode == 0 )        { dir =  PLANAR_IDX;             sdcFlag = 1;}
1331    else if ( modeCode == 2 )   { dir = 5;                       sdcFlag = 0;}
1332    else if ( modeCode == 6 )   { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 1;}
1333    else if ( modeCode == 14 )  { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 0;}
1334    else if ( modeCode == 15 )  { dir = (2*DMM4_IDX+DIM_OFFSET); sdcFlag = 0;}
1335#else
1336    UInt maxBinNum = 0;
1337    m_pcTDecBinIf->decodeBinEP(symbol);
1338    if( symbol == 1 )
1339    {
1340      maxBinNum = 3;
1341    }
1342    else
1343    {
1344      maxBinNum = 2;
1345      symbol = 1;
1346    }
1347    while( binNum<maxBinNum && symbol )
1348    {
1349      ctxDepthMode = puIdx*3 + ( binNum >= 2 ? 2 : binNum );
1350      m_pcTDecBinIf->decodeBin(symbol,m_cDepthIntraModeSCModel.get(0,0,ctxDepthMode));
1351      modeCode = (modeCode<<1)+symbol;
1352      binNum++;
1353    }
1354    if( maxBinNum == 3 )
1355    {
1356      if ( modeCode == 0 )       { dir =  PLANAR_IDX;             sdcFlag = 1;}
1357      else if ( modeCode == 2 )  { dir = (2* RBC_IDX+DIM_OFFSET); sdcFlag = 0;}
1358      else if ( modeCode == 6 )  { dir = (2*DMM3_IDX+DIM_OFFSET); sdcFlag = 0;}
1359      else if ( modeCode == 7 )  { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 0;}
1360    }
1361    else
1362    {
1363      if ( modeCode == 0 )       { dir = 5;                       sdcFlag = 0;}
1364      else if ( modeCode == 2 )  { dir = (2*DMM1_IDX+DIM_OFFSET); sdcFlag = 1;}
1365      else if ( modeCode == 3 )  { dir = (2*DMM4_IDX+DIM_OFFSET); sdcFlag = 0;}
1366    }
1367#endif
1368  }
1369  pcCU->setLumaIntraDirSubParts( (UChar)dir, absPartIdx, depth );
1370#if H_3D_DIM_SDC
1371  pcCU->setSDCFlagSubParts( sdcFlag, absPartIdx, depth ); 
1372#endif
1373}
1374#endif
1375
1376Void TDecSbac::parseInterDir( TComDataCU* pcCU, UInt& ruiInterDir, UInt uiAbsPartIdx )
1377{
1378  UInt uiSymbol;
1379  const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx );
1380  ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 );
1381  uiSymbol = 0;
1382  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1383  {
1384    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + uiCtx ) );
1385  }
1386
1387  if( uiSymbol )
1388  {
1389    uiSymbol = 2;
1390  }
1391  else
1392  {
1393    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + 4 ) );
1394    assert(uiSymbol == 0 || uiSymbol == 1);
1395  }
1396
1397  uiSymbol++;
1398  ruiInterDir = uiSymbol;
1399#if H_MV_ENC_DEC_TRAC
1400    DTRACE_PU("inter_pred_idc", ruiInterDir - 1 )   
1401#endif
1402
1403  return;
1404}
1405
1406Void TDecSbac::parseRefFrmIdx( TComDataCU* pcCU, Int& riRefFrmIdx, RefPicList eRefList )
1407{
1408  UInt uiSymbol;
1409  {
1410    ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1411    m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx );
1412
1413    if( uiSymbol )
1414    {
1415      UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1416      pCtx++;
1417      UInt ui;
1418      for( ui = 0; ui < uiRefNum; ++ui )
1419      {
1420        if( ui == 0 )
1421        {
1422          m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx );
1423        }
1424        else
1425        {
1426          m_pcTDecBinIf->decodeBinEP( uiSymbol );
1427        }
1428        if( uiSymbol == 0 )
1429        {
1430          break;
1431        }
1432      }
1433      uiSymbol = ui + 1;
1434    }
1435    riRefFrmIdx = uiSymbol;
1436  }
1437
1438#if H_MV_ENC_DEC_TRAC
1439#if ENC_DEC_TRACE
1440  if ( eRefList == REF_PIC_LIST_0 )
1441  {
1442    DTRACE_PU("ref_idx_l0", uiSymbol)
1443  }
1444  else
1445  {
1446    DTRACE_PU("ref_idx_l1", uiSymbol)
1447  }
1448#endif
1449#endif
1450  return;
1451}
1452
1453Void TDecSbac::parseMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth, RefPicList eRefList )
1454{
1455  UInt uiSymbol;
1456  UInt uiHorAbs;
1457  UInt uiVerAbs;
1458  UInt uiHorSign = 0;
1459  UInt uiVerSign = 0;
1460  ContextModel *pCtx = m_cCUMvdSCModel.get( 0 );
1461
1462  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1463  {
1464    uiHorAbs=0;
1465    uiVerAbs=0;
1466  }
1467  else
1468  {
1469    m_pcTDecBinIf->decodeBin( uiHorAbs, *pCtx );
1470    m_pcTDecBinIf->decodeBin( uiVerAbs, *pCtx );
1471
1472    const Bool bHorAbsGr0 = uiHorAbs != 0;
1473    const Bool bVerAbsGr0 = uiVerAbs != 0;
1474    pCtx++;
1475
1476    if( bHorAbsGr0 )
1477    {
1478      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx );
1479      uiHorAbs += uiSymbol;
1480    }
1481
1482    if( bVerAbsGr0 )
1483    {
1484      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx );
1485      uiVerAbs += uiSymbol;
1486    }
1487
1488    if( bHorAbsGr0 )
1489    {
1490      if( 2 == uiHorAbs )
1491      {
1492        xReadEpExGolomb( uiSymbol, 1 );
1493        uiHorAbs += uiSymbol;
1494      }
1495
1496      m_pcTDecBinIf->decodeBinEP( uiHorSign );
1497    }
1498
1499    if( bVerAbsGr0 )
1500    {
1501      if( 2 == uiVerAbs )
1502      {
1503        xReadEpExGolomb( uiSymbol, 1 );
1504        uiVerAbs += uiSymbol;
1505      }
1506
1507      m_pcTDecBinIf->decodeBinEP( uiVerSign );
1508    }
1509
1510  }
1511
1512  const TComMv cMv( uiHorSign ? -Int( uiHorAbs ): uiHorAbs, uiVerSign ? -Int( uiVerAbs ) : uiVerAbs );
1513  pcCU->getCUMvField( eRefList )->setAllMvd( cMv, pcCU->getPartitionSize( uiAbsPartIdx ), uiAbsPartIdx, uiDepth, uiPartIdx );
1514  return;
1515}
1516
1517
1518Void TDecSbac::parseTransformSubdivFlag( UInt& ruiSubdivFlag, UInt uiLog2TransformBlockSize )
1519{
1520  m_pcTDecBinIf->decodeBin( ruiSubdivFlag, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiLog2TransformBlockSize ) );
1521#if !H_MV_ENC_DEC_TRAC
1522  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1523  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1524  DTRACE_CABAC_T( "\tsymbol=" )
1525  DTRACE_CABAC_V( ruiSubdivFlag )
1526  DTRACE_CABAC_T( "\tctx=" )
1527  DTRACE_CABAC_V( uiLog2TransformBlockSize )
1528  DTRACE_CABAC_T( "\n" )
1529#endif
1530}
1531
1532Void TDecSbac::parseQtRootCbf( UInt uiAbsPartIdx, UInt& uiQtRootCbf )
1533{
1534  UInt uiSymbol;
1535  const UInt uiCtx = 0;
1536  m_pcTDecBinIf->decodeBin( uiSymbol , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1537#if !H_MV_ENC_DEC_TRAC
1538  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1539  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1540  DTRACE_CABAC_T( "\tsymbol=" )
1541  DTRACE_CABAC_V( uiSymbol )
1542  DTRACE_CABAC_T( "\tctx=" )
1543  DTRACE_CABAC_V( uiCtx )
1544  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1545  DTRACE_CABAC_V( uiAbsPartIdx )
1546  DTRACE_CABAC_T( "\n" )
1547#else
1548  DTRACE_CU( "rqt_root_cbf", uiSymbol )
1549#endif
1550 
1551  uiQtRootCbf = uiSymbol;
1552}
1553
1554Void TDecSbac::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1555{
1556  Int qp;
1557  UInt uiDQp;
1558  Int  iDQp;
1559 
1560  UInt uiSymbol;
1561
1562  xReadUnaryMaxSymbol (uiDQp,  &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX);
1563
1564  if( uiDQp >= CU_DQP_TU_CMAX)
1565  {
1566    xReadEpExGolomb( uiSymbol, CU_DQP_EG_k );
1567    uiDQp+=uiSymbol;
1568  }
1569
1570  if ( uiDQp > 0 )
1571  {
1572    UInt uiSign;
1573    Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY();
1574    m_pcTDecBinIf->decodeBinEP(uiSign);
1575    iDQp = uiDQp;
1576    if(uiSign)
1577    {
1578      iDQp = -iDQp;
1579    }
1580    qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+qpBdOffsetY)) - qpBdOffsetY;
1581  }
1582  else 
1583  {
1584    qp = pcCU->getRefQP(uiAbsPartIdx);
1585  }
1586  pcCU->setQPSubParts(qp, uiAbsPartIdx, uiDepth); 
1587  pcCU->setCodedQP(qp);
1588}
1589
1590Void TDecSbac::parseQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth, UInt uiDepth )
1591{
1592  UInt uiSymbol;
1593  const UInt uiCtx = pcCU->getCtxQtCbf( eType, uiTrDepth );
1594  m_pcTDecBinIf->decodeBin( uiSymbol , m_cCUQtCbfSCModel.get( 0, eType ? TEXT_CHROMA: eType, uiCtx ) );
1595#if !H_MV_ENC_DEC_TRAC 
1596  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1597  DTRACE_CABAC_T( "\tparseQtCbf()" )
1598  DTRACE_CABAC_T( "\tsymbol=" )
1599  DTRACE_CABAC_V( uiSymbol )
1600  DTRACE_CABAC_T( "\tctx=" )
1601  DTRACE_CABAC_V( uiCtx )
1602  DTRACE_CABAC_T( "\tetype=" )
1603  DTRACE_CABAC_V( eType )
1604  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1605  DTRACE_CABAC_V( uiAbsPartIdx )
1606  DTRACE_CABAC_T( "\n" )
1607#endif
1608 
1609  pcCU->setCbfSubParts( uiSymbol << uiTrDepth, eType, uiAbsPartIdx, uiDepth );
1610}
1611
1612void TDecSbac::parseTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, UInt uiDepth, TextType eTType)
1613{
1614  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1615  {
1616    return;
1617  }
1618  if(width != 4 || height != 4)
1619  {
1620    return;
1621  }
1622 
1623  UInt useTransformSkip;
1624  m_pcTDecBinIf->decodeBin( useTransformSkip , m_cTransformSkipSCModel.get( 0, eTType? TEXT_CHROMA: TEXT_LUMA, 0 ) );
1625  if(eTType!= TEXT_LUMA)
1626  {
1627    const UInt uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - uiDepth;
1628    if(uiLog2TrafoSize == 2) 
1629    { 
1630      uiDepth --;
1631    }
1632  }
1633#if !H_MV_ENC_DEC_TRAC
1634  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1635  DTRACE_CABAC_T("\tparseTransformSkip()");
1636  DTRACE_CABAC_T( "\tsymbol=" )
1637  DTRACE_CABAC_V( useTransformSkip )
1638  DTRACE_CABAC_T( "\tAddr=" )
1639  DTRACE_CABAC_V( pcCU->getAddr() )
1640  DTRACE_CABAC_T( "\tetype=" )
1641  DTRACE_CABAC_V( eTType )
1642  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1643  DTRACE_CABAC_V( uiAbsPartIdx )
1644  DTRACE_CABAC_T( "\n" )
1645#endif
1646
1647  pcCU->setTransformSkipSubParts( useTransformSkip, eTType, uiAbsPartIdx, uiDepth);
1648}
1649
1650/** Parse (X,Y) position of the last significant coefficient
1651 * \param uiPosLastX reference to X component of last coefficient
1652 * \param uiPosLastY reference to Y component of last coefficient
1653 * \param width  Block width
1654 * \param height Block height
1655 * \param eTType plane type / luminance or chrominance
1656 * \param uiScanIdx scan type (zig-zag, hor, ver)
1657 *
1658 * This method decodes the X and Y component within a block of the last significant coefficient.
1659 */
1660Void TDecSbac::parseLastSignificantXY( UInt& uiPosLastX, UInt& uiPosLastY, Int width, Int height, TextType eTType, UInt uiScanIdx )
1661{
1662  UInt uiLast;
1663  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, eTType );
1664  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, eTType );
1665
1666  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1667  blkSizeOffsetX = eTType ? 0: (g_aucConvertToBit[ width ] *3 + ((g_aucConvertToBit[ width ] +1)>>2));
1668  blkSizeOffsetY = eTType ? 0: (g_aucConvertToBit[ height ]*3 + ((g_aucConvertToBit[ height ]+1)>>2));
1669  shiftX= eTType ? g_aucConvertToBit[ width  ] :((g_aucConvertToBit[ width  ]+3)>>2);
1670  shiftY= eTType ? g_aucConvertToBit[ height ] :((g_aucConvertToBit[ height ]+3)>>2);
1671  // posX
1672  for( uiPosLastX = 0; uiPosLastX < g_uiGroupIdx[ width - 1 ]; uiPosLastX++ )
1673  {
1674    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxX + blkSizeOffsetX + (uiPosLastX >>shiftX) ) );
1675    if( !uiLast )
1676    {
1677      break;
1678    }
1679  }
1680
1681  // posY
1682  for( uiPosLastY = 0; uiPosLastY < g_uiGroupIdx[ height - 1 ]; uiPosLastY++ )
1683  {
1684    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxY + blkSizeOffsetY + (uiPosLastY >>shiftY)) );
1685    if( !uiLast )
1686    {
1687      break;
1688    }
1689  }
1690  if ( uiPosLastX > 3 )
1691  {
1692    UInt uiTemp  = 0;
1693    UInt uiCount = ( uiPosLastX - 2 ) >> 1;
1694    for ( Int i = uiCount - 1; i >= 0; i-- )
1695    {
1696      m_pcTDecBinIf->decodeBinEP( uiLast );
1697      uiTemp += uiLast << i;
1698    }
1699    uiPosLastX = g_uiMinInGroup[ uiPosLastX ] + uiTemp;
1700  }
1701  if ( uiPosLastY > 3 )
1702  {
1703    UInt uiTemp  = 0;
1704    UInt uiCount = ( uiPosLastY - 2 ) >> 1;
1705    for ( Int i = uiCount - 1; i >= 0; i-- )
1706    {
1707      m_pcTDecBinIf->decodeBinEP( uiLast );
1708      uiTemp += uiLast << i;
1709    }
1710    uiPosLastY = g_uiMinInGroup[ uiPosLastY ] + uiTemp;
1711  }
1712 
1713  if( uiScanIdx == SCAN_VER )
1714  {
1715    swap( uiPosLastX, uiPosLastY );
1716  }
1717}
1718
1719Void TDecSbac::parseCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1720{
1721#if !H_MV_ENC_DEC_TRAC
1722  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1723  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1724  DTRACE_CABAC_V( eTType )
1725  DTRACE_CABAC_T( "\twidth=" )
1726  DTRACE_CABAC_V( uiWidth )
1727  DTRACE_CABAC_T( "\theight=" )
1728  DTRACE_CABAC_V( uiHeight )
1729  DTRACE_CABAC_T( "\tdepth=" )
1730  DTRACE_CABAC_V( uiDepth )
1731  DTRACE_CABAC_T( "\tabspartidx=" )
1732  DTRACE_CABAC_V( uiAbsPartIdx )
1733  DTRACE_CABAC_T( "\ttoCU-X=" )
1734  DTRACE_CABAC_V( pcCU->getCUPelX() )
1735  DTRACE_CABAC_T( "\ttoCU-Y=" )
1736  DTRACE_CABAC_V( pcCU->getCUPelY() )
1737  DTRACE_CABAC_T( "\tCU-addr=" )
1738  DTRACE_CABAC_V(  pcCU->getAddr() )
1739  DTRACE_CABAC_T( "\tinCU-X=" )
1740  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1741  DTRACE_CABAC_T( "\tinCU-Y=" )
1742  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1743  DTRACE_CABAC_T( "\tpredmode=" )
1744  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1745  DTRACE_CABAC_T( "\n" )
1746#endif
1747 
1748  if( uiWidth > pcCU->getSlice()->getSPS()->getMaxTrSize() )
1749  {
1750    uiWidth  = pcCU->getSlice()->getSPS()->getMaxTrSize();
1751    uiHeight = pcCU->getSlice()->getSPS()->getMaxTrSize();
1752  }
1753  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1754  {
1755    parseTransformSkipFlags( pcCU, uiAbsPartIdx, uiWidth, uiHeight, uiDepth, eTType);
1756  }
1757
1758  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1759 
1760  //----- parse significance map -----
1761  const UInt  uiLog2BlockSize   = g_aucConvertToBit[ uiWidth ] + 2;
1762  const UInt  uiMaxNumCoeff     = uiWidth * uiHeight;
1763  const UInt  uiMaxNumCoeffM1   = uiMaxNumCoeff - 1;
1764  UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1765 
1766  //===== decode last significant =====
1767  UInt uiPosLastX, uiPosLastY;
1768  parseLastSignificantXY( uiPosLastX, uiPosLastY, uiWidth, uiHeight, eTType, uiScanIdx );
1769  UInt uiBlkPosLast      = uiPosLastX + (uiPosLastY<<uiLog2BlockSize);
1770  pcCoef[ uiBlkPosLast ] = 1;
1771
1772  //===== decode significance flags =====
1773  UInt uiScanPosLast;
1774  const UInt *scan   = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize-1 ];
1775  for( uiScanPosLast = 0; uiScanPosLast < uiMaxNumCoeffM1; uiScanPosLast++ )
1776  {
1777    UInt uiBlkPos = scan[ uiScanPosLast ];
1778    if( uiBlkPosLast == uiBlkPos )
1779    {
1780      break;
1781    }
1782  }
1783
1784  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, eTType );
1785  ContextModel * const baseCtx = (eTType==TEXT_LUMA) ? m_cCUSigSCModel.get( 0, 0 ) : m_cCUSigSCModel.get( 0, 0 ) + NUM_SIG_FLAG_CTX_LUMA;
1786
1787  const Int  iLastScanSet      = uiScanPosLast >> LOG2_SCAN_SET_SIZE;
1788  UInt c1 = 1;
1789  UInt uiGoRiceParam           = 0;
1790
1791  Bool beValid; 
1792  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1793  {
1794    beValid = false;
1795  }
1796  else 
1797  {
1798    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag() > 0;
1799  }
1800  UInt absSum = 0;
1801
1802  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1803  ::memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1804  const UInt uiNumBlkSide = uiWidth >> (MLS_CG_SIZE >> 1);
1805  const UInt * scanCG;
1806  {
1807    scanCG = g_auiSigLastScan[ uiScanIdx ][ uiLog2BlockSize > 3 ? uiLog2BlockSize-2-1 : 0  ];   
1808    if( uiLog2BlockSize == 3 )
1809    {
1810      scanCG = g_sigLastScan8x8[ uiScanIdx ];
1811    }
1812    else if( uiLog2BlockSize == 5 )
1813    {
1814      scanCG = g_sigLastScanCG32x32;
1815    }
1816  }
1817  Int  iScanPosSig             = (Int) uiScanPosLast;
1818  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1819  {
1820    Int  iSubPos     = iSubSet << LOG2_SCAN_SET_SIZE;
1821    uiGoRiceParam    = 0;
1822    Int numNonZero = 0;
1823   
1824    Int lastNZPosInCG = -1, firstNZPosInCG = SCAN_SET_SIZE;
1825
1826    Int pos[SCAN_SET_SIZE];
1827    if( iScanPosSig == (Int) uiScanPosLast )
1828    {
1829      lastNZPosInCG  = iScanPosSig;
1830      firstNZPosInCG = iScanPosSig;
1831      iScanPosSig--;
1832      pos[ numNonZero ] = uiBlkPosLast;
1833      numNonZero = 1;
1834    }
1835
1836    // decode significant_coeffgroup_flag
1837    Int iCGBlkPos = scanCG[ iSubSet ];
1838    Int iCGPosY   = iCGBlkPos / uiNumBlkSide;
1839    Int iCGPosX   = iCGBlkPos - (iCGPosY * uiNumBlkSide);
1840    if( iSubSet == iLastScanSet || iSubSet == 0)
1841    {
1842      uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1843    }
1844    else
1845    {
1846      UInt uiSigCoeffGroup;
1847      UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1848      m_pcTDecBinIf->decodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] );
1849      uiSigCoeffGroupFlag[ iCGBlkPos ] = uiSigCoeffGroup;
1850    }
1851
1852    // decode significant_coeff_flag
1853    Int patternSigCtx = TComTrQuant::calcPatternSigCtx( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, uiWidth, uiHeight );
1854    UInt uiBlkPos, uiPosY, uiPosX, uiSig, uiCtxSig;
1855    for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1856    {
1857      uiBlkPos  = scan[ iScanPosSig ];
1858      uiPosY    = uiBlkPos >> uiLog2BlockSize;
1859      uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1860      uiSig     = 0;
1861     
1862      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1863      {
1864        if( iScanPosSig > iSubPos || iSubSet == 0  || numNonZero )
1865        {
1866          uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, uiScanIdx, uiPosX, uiPosY, uiLog2BlockSize, eTType );
1867          m_pcTDecBinIf->decodeBin( uiSig, baseCtx[ uiCtxSig ] );
1868        }
1869        else
1870        {
1871          uiSig = 1;
1872        }
1873      }
1874      pcCoef[ uiBlkPos ] = uiSig;
1875      if( uiSig )
1876      {
1877        pos[ numNonZero ] = uiBlkPos;
1878        numNonZero ++;
1879        if( lastNZPosInCG == -1 )
1880        {
1881          lastNZPosInCG = iScanPosSig;
1882        }
1883        firstNZPosInCG = iScanPosSig;
1884      }
1885    }
1886   
1887    if( numNonZero )
1888    {
1889      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1890      absSum = 0;
1891      UInt uiCtxSet    = (iSubSet > 0 && eTType==TEXT_LUMA) ? 2 : 0;
1892      UInt uiBin;
1893      if( c1 == 0 )
1894      {
1895        uiCtxSet++;
1896      }
1897      c1 = 1;
1898      ContextModel *baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUOneSCModel.get( 0, 0 ) + 4 * uiCtxSet : m_cCUOneSCModel.get( 0, 0 ) + NUM_ONE_FLAG_CTX_LUMA + 4 * uiCtxSet;
1899      Int absCoeff[SCAN_SET_SIZE];
1900
1901      for ( Int i = 0; i < numNonZero; i++) absCoeff[i] = 1;   
1902      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1903      Int firstC2FlagIdx = -1;
1904
1905      for( Int idx = 0; idx < numC1Flag; idx++ )
1906      {
1907        m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[c1] );
1908        if( uiBin == 1 )
1909        {
1910          c1 = 0;
1911          if (firstC2FlagIdx == -1)
1912          {
1913            firstC2FlagIdx = idx;
1914          }
1915        }
1916        else if( (c1 < 3) && (c1 > 0) )
1917        {
1918          c1++;
1919        }
1920        absCoeff[ idx ] = uiBin + 1;
1921      }
1922     
1923      if (c1 == 0)
1924      {
1925        baseCtxMod = ( eTType==TEXT_LUMA ) ? m_cCUAbsSCModel.get( 0, 0 ) + uiCtxSet : m_cCUAbsSCModel.get( 0, 0 ) + NUM_ABS_FLAG_CTX_LUMA + uiCtxSet;
1926        if ( firstC2FlagIdx != -1)
1927        {
1928          m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[0] ); 
1929          absCoeff[ firstC2FlagIdx ] = uiBin + 2;
1930        }
1931      }
1932
1933      UInt coeffSigns;
1934      if ( signHidden && beValid )
1935      {
1936        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero-1 );
1937        coeffSigns <<= 32 - (numNonZero-1);
1938      }
1939      else
1940      {
1941        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero );
1942        coeffSigns <<= 32 - numNonZero;
1943      }
1944     
1945      Int iFirstCoeff2 = 1;   
1946      if (c1 == 0 || numNonZero > C1FLAG_NUMBER)
1947      {
1948        for( Int idx = 0; idx < numNonZero; idx++ )
1949        {
1950          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2) : 1;
1951
1952          if( absCoeff[ idx ] == baseLevel)
1953          {
1954            UInt uiLevel;
1955            xReadCoefRemainExGolomb( uiLevel, uiGoRiceParam );
1956            absCoeff[ idx ] = uiLevel + baseLevel;
1957            if(absCoeff[idx]>3*(1<<uiGoRiceParam))
1958            {
1959              uiGoRiceParam = min<UInt>(uiGoRiceParam+ 1, 4);
1960            }
1961          }
1962
1963          if(absCoeff[ idx ] >= 2) 
1964          {
1965            iFirstCoeff2 = 0;
1966          }
1967        }
1968      }
1969
1970      for( Int idx = 0; idx < numNonZero; idx++ )
1971      {
1972        Int blkPos = pos[ idx ];
1973        // Signs applied later.
1974        pcCoef[ blkPos ] = absCoeff[ idx ];
1975        absSum += absCoeff[ idx ];
1976
1977        if ( idx == numNonZero-1 && signHidden && beValid )
1978        {
1979          // Infer sign of 1st element.
1980          if (absSum&0x1)
1981          {
1982            pcCoef[ blkPos ] = -pcCoef[ blkPos ];
1983          }
1984        }
1985        else
1986        {
1987          Int sign = static_cast<Int>( coeffSigns ) >> 31;
1988          pcCoef[ blkPos ] = ( pcCoef[ blkPos ] ^ sign ) - sign;
1989          coeffSigns <<= 1;
1990        }
1991      }
1992    }
1993  }
1994 
1995  return;
1996}
1997
1998
1999Void TDecSbac::parseSaoMaxUvlc ( UInt& val, UInt maxSymbol )
2000{
2001  if (maxSymbol == 0)
2002  {
2003    val = 0;
2004    return;
2005  }
2006
2007  UInt code;
2008  Int  i;
2009  m_pcTDecBinIf->decodeBinEP( code );
2010  if ( code == 0 )
2011  {
2012    val = 0;
2013    return;
2014  }
2015
2016  i=1;
2017  while (1)
2018  {
2019    m_pcTDecBinIf->decodeBinEP( code );
2020    if ( code == 0 )
2021    {
2022      break;
2023    }
2024    i++;
2025    if (i == maxSymbol) 
2026    {
2027      break;
2028    }
2029  }
2030
2031  val = i;
2032}
2033Void TDecSbac::parseSaoUflc (UInt uiLength, UInt&  riVal)
2034{
2035  m_pcTDecBinIf->decodeBinsEP ( riVal, uiLength );
2036}
2037Void TDecSbac::parseSaoMerge (UInt&  ruiVal)
2038{
2039  UInt uiCode;
2040  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoMergeSCModel.get( 0, 0, 0 ) );
2041  ruiVal = (Int)uiCode;
2042}
2043Void TDecSbac::parseSaoTypeIdx (UInt&  ruiVal)
2044{
2045  UInt uiCode;
2046  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) );
2047  if (uiCode == 0) 
2048  {
2049    ruiVal = 0;
2050  }
2051  else
2052  {
2053    m_pcTDecBinIf->decodeBinEP( uiCode ); 
2054    if (uiCode == 0)
2055    {
2056      ruiVal = 5;
2057    }
2058    else
2059    {
2060      ruiVal = 1;
2061    }
2062  }
2063}
2064
2065inline Void copySaoOneLcuParam(SaoLcuParam* psDst,  SaoLcuParam* psSrc)
2066{
2067  Int i;
2068  psDst->partIdx = psSrc->partIdx;
2069  psDst->typeIdx    = psSrc->typeIdx;
2070  if (psDst->typeIdx != -1)
2071  {
2072    psDst->subTypeIdx = psSrc->subTypeIdx ;
2073    psDst->length  = psSrc->length;
2074    for (i=0;i<psDst->length;i++)
2075    {
2076      psDst->offset[i] = psSrc->offset[i];
2077    }
2078  }
2079  else
2080  {
2081    psDst->length  = 0;
2082    for (i=0;i<SAO_BO_LEN;i++)
2083    {
2084      psDst->offset[i] = 0;
2085    }
2086  }
2087}
2088
2089Void TDecSbac::parseSaoOffset(SaoLcuParam* psSaoLcuParam, UInt compIdx)
2090{
2091  UInt uiSymbol;
2092  static Int iTypeLength[MAX_NUM_SAO_TYPE] =
2093  {
2094    SAO_EO_LEN,
2095    SAO_EO_LEN,
2096    SAO_EO_LEN,
2097    SAO_EO_LEN,
2098    SAO_BO_LEN
2099  }; 
2100
2101  if (compIdx==2)
2102  {
2103    uiSymbol = (UInt)( psSaoLcuParam->typeIdx + 1);
2104  }
2105  else
2106  {
2107    parseSaoTypeIdx(uiSymbol);
2108  }
2109  psSaoLcuParam->typeIdx = (Int)uiSymbol - 1;
2110  if (uiSymbol)
2111  {
2112    psSaoLcuParam->length = iTypeLength[psSaoLcuParam->typeIdx];
2113
2114    Int bitDepth = compIdx ? g_bitDepthC : g_bitDepthY;
2115    Int offsetTh = 1 << min(bitDepth - 5,5);
2116
2117    if( psSaoLcuParam->typeIdx == SAO_BO )
2118    {
2119      for(Int i=0; i< psSaoLcuParam->length; i++)
2120      {
2121        parseSaoMaxUvlc(uiSymbol, offsetTh -1 );
2122        psSaoLcuParam->offset[i] = uiSymbol;
2123      }   
2124      for(Int i=0; i< psSaoLcuParam->length; i++)
2125      {
2126        if (psSaoLcuParam->offset[i] != 0) 
2127        {
2128          m_pcTDecBinIf->decodeBinEP ( uiSymbol);
2129          if (uiSymbol)
2130          {
2131            psSaoLcuParam->offset[i] = -psSaoLcuParam->offset[i] ;
2132          }
2133        }
2134      }
2135      parseSaoUflc(5, uiSymbol );
2136      psSaoLcuParam->subTypeIdx = uiSymbol;
2137    }
2138    else if( psSaoLcuParam->typeIdx < 4 )
2139    {
2140      parseSaoMaxUvlc(uiSymbol, offsetTh -1 ); psSaoLcuParam->offset[0] = uiSymbol;
2141      parseSaoMaxUvlc(uiSymbol, offsetTh -1 ); psSaoLcuParam->offset[1] = uiSymbol;
2142      parseSaoMaxUvlc(uiSymbol, offsetTh -1 ); psSaoLcuParam->offset[2] = -(Int)uiSymbol;
2143      parseSaoMaxUvlc(uiSymbol, offsetTh -1 ); psSaoLcuParam->offset[3] = -(Int)uiSymbol;
2144     if (compIdx != 2)
2145     {
2146       parseSaoUflc(2, uiSymbol );
2147       psSaoLcuParam->subTypeIdx = uiSymbol;
2148       psSaoLcuParam->typeIdx += psSaoLcuParam->subTypeIdx;
2149     }
2150   }
2151  }
2152  else
2153  {
2154    psSaoLcuParam->length = 0;
2155  }
2156}
2157
2158Void TDecSbac::parseSaoOneLcuInterleaving(Int rx, Int ry, SAOParam* pSaoParam, TComDataCU* pcCU, Int iCUAddrInSlice, Int iCUAddrUpInSlice, Int allowMergeLeft, Int allowMergeUp)
2159{
2160  Int iAddr = pcCU->getAddr();
2161  UInt uiSymbol;
2162  for (Int iCompIdx=0; iCompIdx<3; iCompIdx++)
2163  {
2164    pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeUpFlag    = 0;
2165    pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeLeftFlag  = 0;
2166    pSaoParam->saoLcuParam[iCompIdx][iAddr].subTypeIdx     = 0;
2167    pSaoParam->saoLcuParam[iCompIdx][iAddr].typeIdx        = -1;
2168    pSaoParam->saoLcuParam[iCompIdx][iAddr].offset[0]     = 0;
2169    pSaoParam->saoLcuParam[iCompIdx][iAddr].offset[1]     = 0;
2170    pSaoParam->saoLcuParam[iCompIdx][iAddr].offset[2]     = 0;
2171    pSaoParam->saoLcuParam[iCompIdx][iAddr].offset[3]     = 0;
2172
2173  }
2174 if (pSaoParam->bSaoFlag[0] || pSaoParam->bSaoFlag[1] )
2175  {
2176    if (rx>0 && iCUAddrInSlice!=0 && allowMergeLeft)
2177    {
2178      parseSaoMerge(uiSymbol); 
2179      pSaoParam->saoLcuParam[0][iAddr].mergeLeftFlag = (Bool)uiSymbol; 
2180    }
2181    if (pSaoParam->saoLcuParam[0][iAddr].mergeLeftFlag==0)
2182    {
2183      if ((ry > 0) && (iCUAddrUpInSlice>=0) && allowMergeUp)
2184      {
2185        parseSaoMerge(uiSymbol);
2186        pSaoParam->saoLcuParam[0][iAddr].mergeUpFlag = (Bool)uiSymbol;
2187      }
2188    }
2189  }
2190
2191  for (Int iCompIdx=0; iCompIdx<3; iCompIdx++)
2192  {
2193    if ((iCompIdx == 0  && pSaoParam->bSaoFlag[0]) || (iCompIdx > 0  && pSaoParam->bSaoFlag[1]) )
2194    {
2195      if (rx>0 && iCUAddrInSlice!=0 && allowMergeLeft)
2196      {
2197        pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeLeftFlag = pSaoParam->saoLcuParam[0][iAddr].mergeLeftFlag;
2198      }
2199      else
2200      {
2201        pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeLeftFlag = 0;
2202      }
2203
2204      if (pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeLeftFlag==0)
2205      {
2206        if ((ry > 0) && (iCUAddrUpInSlice>=0) && allowMergeUp)
2207        {
2208          pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeUpFlag = pSaoParam->saoLcuParam[0][iAddr].mergeUpFlag;
2209        }
2210        else
2211        {
2212          pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeUpFlag = 0;
2213        }
2214        if (!pSaoParam->saoLcuParam[iCompIdx][iAddr].mergeUpFlag)
2215        {
2216          pSaoParam->saoLcuParam[2][iAddr].typeIdx = pSaoParam->saoLcuParam[1][iAddr].typeIdx;
2217          parseSaoOffset(&(pSaoParam->saoLcuParam[iCompIdx][iAddr]), iCompIdx);
2218        }
2219        else
2220        {
2221          copySaoOneLcuParam(&pSaoParam->saoLcuParam[iCompIdx][iAddr], &pSaoParam->saoLcuParam[iCompIdx][iAddr-pSaoParam->numCuInWidth]);
2222        }
2223      }
2224      else
2225      {
2226        copySaoOneLcuParam(&pSaoParam->saoLcuParam[iCompIdx][iAddr],  &pSaoParam->saoLcuParam[iCompIdx][iAddr-1]);
2227      }
2228    }
2229    else
2230    {
2231      pSaoParam->saoLcuParam[iCompIdx][iAddr].typeIdx = -1;
2232      pSaoParam->saoLcuParam[iCompIdx][iAddr].subTypeIdx = 0;
2233    }
2234  }
2235}
2236
2237/**
2238 - Initialize our contexts from the nominated source.
2239 .
2240 \param pSrc Contexts to be copied.
2241 */
2242Void TDecSbac::xCopyContextsFrom( TDecSbac* pSrc )
2243{
2244  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2245}
2246
2247Void TDecSbac::xCopyFrom( TDecSbac* pSrc )
2248{
2249  m_pcTDecBinIf->copyState( pSrc->m_pcTDecBinIf );
2250
2251  m_uiLastQp           = pSrc->m_uiLastQp;
2252  xCopyContextsFrom( pSrc );
2253
2254}
2255
2256Void TDecSbac::load ( TDecSbac* pScr )
2257{
2258  xCopyFrom(pScr);
2259}
2260
2261Void TDecSbac::loadContexts ( TDecSbac* pScr )
2262{
2263  xCopyContextsFrom(pScr);
2264}
2265
2266#if H_3D_ARP
2267Void TDecSbac::parseARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2268{
2269  UInt uiMaxW = pcCU->getSlice()->getARPStepNum() - 1;
2270  UInt uiW = 0;
2271  UInt uiOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
2272  UInt uiCode = 0;
2273
2274  assert ( uiMaxW > 0 );
2275
2276  m_pcTDecBinIf->decodeBin( uiCode , m_cCUPUARPWSCModel.get( 0, 0, 0 + uiOffset ) );
2277
2278  uiW = uiCode;
2279  if( 1 == uiW )   
2280  {
2281    m_pcTDecBinIf->decodeBin( uiCode , m_cCUPUARPWSCModel.get( 0, 0, 3 ) );
2282    uiW += ( 1 == uiCode ? 1 : 0 );
2283  }
2284#if H_MV_ENC_DEC_TRAC
2285  DTRACE_CU("iv_res_pred_weight_idx", uiW )
2286#endif
2287  pcCU->setARPWSubParts( ( UChar )( uiW ) , uiAbsPartIdx, uiDepth ); 
2288}
2289#endif
2290
2291#if H_3D_IC
2292/** parse illumination compensation flag
2293 * \param pcCU
2294 * \param uiAbsPartIdx
2295 * \param uiDepth
2296 * \returns Void
2297 */
2298Void TDecSbac::parseICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2299{ 
2300  UInt uiSymbol = 0;
2301  UInt uiCtxIC = pcCU->getCtxICFlag( uiAbsPartIdx );
2302  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, uiCtxIC ) );
2303#if !H_MV_ENC_DEC_TRAC
2304  DTRACE_CABAC_VL( g_nSymbolCounter++ );
2305  DTRACE_CABAC_T( "\tICFlag" );
2306  DTRACE_CABAC_T( "\tuiCtxIC: ");
2307  DTRACE_CABAC_V( uiCtxIC );
2308  DTRACE_CABAC_T( "\tuiSymbol: ");
2309  DTRACE_CABAC_V( uiSymbol );
2310  DTRACE_CABAC_T( "\n");
2311#else
2312  DTRACE_CU("ic_flag", uiSymbol)
2313#endif
2314 
2315  pcCU->setICFlagSubParts( uiSymbol ? true : false , uiAbsPartIdx, 0, uiDepth );
2316}
2317#endif
2318
2319#if H_3D_INTER_SDC
2320Void TDecSbac::parseInterSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2321{
2322  UInt uiSymbol = 0;
2323  UInt uiCtxInterSDCFlag = pcCU->getCtxInterSDCFlag( uiAbsPartIdx );
2324
2325  m_pcTDecBinIf->decodeBin( uiSymbol, m_cInterSDCFlagSCModel.get( 0, 0, uiCtxInterSDCFlag ) );
2326
2327  if( uiSymbol )
2328  {
2329    pcCU->setInterSDCFlagSubParts( true, uiAbsPartIdx, 0, uiDepth );
2330    pcCU->setTrIdxSubParts( 0, uiAbsPartIdx, uiDepth );
2331    pcCU->setCbfSubParts( 1, 1, 1, uiAbsPartIdx, uiDepth );
2332  }
2333  else
2334  {
2335    pcCU->setInterSDCFlagSubParts( false, uiAbsPartIdx, 0, uiDepth);
2336  }
2337}
2338
2339Void TDecSbac::parseInterSDCResidualData ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSegment )
2340{
2341  UInt uiAbsIdx   = 0;
2342  UInt uiSign     = 0;
2343  Int  iIdx       = 0;
2344
2345  xReadExGolombLevel( uiAbsIdx, m_cInterSDCResidualSCModel.get( 0, 0, 0 ) );
2346
2347  uiAbsIdx++;
2348  m_pcTDecBinIf->decodeBin( uiSign, m_cInterSDCResidualSignFlagSCModel.get( 0, 0, 0 ) );
2349  iIdx = (Int)( uiSign ? -1 : 1 ) * uiAbsIdx;
2350
2351  pcCU->setInterSDCSegmentDCOffset( iIdx, uiSegment, uiAbsPartIdx );
2352}
2353#endif
2354
2355//! \}
Note: See TracBrowser for help on using the repository browser.