source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibDecoder/TDecSbac.cpp @ 539

Last change on this file since 539 was 539, checked in by orange, 11 years ago

Integrated QTLPC and added new line mark at the end of TComWedgelet.cpp for MERL

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