source: 3DVCSoftware/branches/HTM-14.1-update-dev2/source/Lib/TLibDecoder/TDecSbac.cpp @ 1274

Last change on this file since 1274 was 1274, checked in by tech, 9 years ago

Merged HTM-14.1-update-dev3@1273.

  • Property svn:eol-style set to native
File size: 89.0 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-2015, 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#include "TLibCommon/TComTU.h"
40#include "TLibCommon/TComTrQuant.h"
41
42#if RExt__DECODER_DEBUG_BIT_STATISTICS
43#include "TLibCommon/TComCodingStatistics.h"
44//
45#define RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(a) , a
46#else
47#define RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(a)
48#endif
49
50//! \ingroup TLibDecoder
51//! \{
52
53#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
54#include "../TLibCommon/Debug.h"
55#endif
56
57
58//////////////////////////////////////////////////////////////////////
59// Construction/Destruction
60//////////////////////////////////////////////////////////////////////
61
62TDecSbac::TDecSbac()
63// new structure here
64: m_pcBitstream                              ( 0 )
65, m_pcTDecBinIf                              ( NULL )
66, m_numContextModels                         ( 0 )
67, m_cCUSplitFlagSCModel                      ( 1,             1,                      NUM_SPLIT_FLAG_CTX                   , m_contextModels + m_numContextModels, m_numContextModels)
68, m_cCUSkipFlagSCModel                       ( 1,             1,                      NUM_SKIP_FLAG_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
69#if H_3D
70, m_cCUDISFlagSCModel         ( 1,             1,               NUM_DIS_FLAG_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
71, m_cCUDISTypeSCModel         ( 1,             1,               NUM_DIS_TYPE_CTX             , m_contextModels + m_numContextModels, m_numContextModels)
72#endif
73, m_cCUMergeFlagExtSCModel                   ( 1,             1,                      NUM_MERGE_FLAG_EXT_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
74, m_cCUMergeIdxExtSCModel                    ( 1,             1,                      NUM_MERGE_IDX_EXT_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
75#if NH_3D_ARP
76, m_cCUPUARPWSCModel          ( 1,             1,               NUM_ARPW_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
77#endif
78#if NH_3D_IC
79, m_cCUICFlagSCModel          ( 1,             1,               NUM_IC_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
80#endif
81, m_cCUPartSizeSCModel                       ( 1,             1,                      NUM_PART_SIZE_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cCUPredModeSCModel                       ( 1,             1,                      NUM_PRED_MODE_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cCUIntraPredSCModel                      ( 1,             1,                      NUM_INTRA_PREDICT_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
84, m_cCUChromaPredSCModel                     ( 1,             1,                      NUM_CHROMA_PRED_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
85, m_cCUDeltaQpSCModel                        ( 1,             1,                      NUM_DELTA_QP_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
86, m_cCUInterDirSCModel                       ( 1,             1,                      NUM_INTER_DIR_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cCURefPicSCModel                         ( 1,             1,                      NUM_REF_NO_CTX                       , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cCUMvdSCModel                            ( 1,             1,                      NUM_MV_RES_CTX                       , m_contextModels + m_numContextModels, m_numContextModels)
89, m_cCUQtCbfSCModel                          ( 1,             NUM_QT_CBF_CTX_SETS,    NUM_QT_CBF_CTX_PER_SET               , m_contextModels + m_numContextModels, m_numContextModels)
90, m_cCUTransSubdivFlagSCModel                ( 1,             1,                      NUM_TRANS_SUBDIV_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
91, m_cCUQtRootCbfSCModel                      ( 1,             1,                      NUM_QT_ROOT_CBF_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
92, m_cCUSigCoeffGroupSCModel                  ( 1,             2,                      NUM_SIG_CG_FLAG_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
93, m_cCUSigSCModel                            ( 1,             1,                      NUM_SIG_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
94, m_cCuCtxLastX                              ( 1,             NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY                 , m_contextModels + m_numContextModels, m_numContextModels)
95, m_cCuCtxLastY                              ( 1,             NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY                 , m_contextModels + m_numContextModels, m_numContextModels)
96, m_cCUOneSCModel                            ( 1,             1,                      NUM_ONE_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
97, m_cCUAbsSCModel                            ( 1,             1,                      NUM_ABS_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
98, m_cMVPIdxSCModel                           ( 1,             1,                      NUM_MVP_IDX_CTX                      , m_contextModels + m_numContextModels, m_numContextModels)
99, m_cSaoMergeSCModel                         ( 1,             1,                      NUM_SAO_MERGE_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
100, m_cSaoTypeIdxSCModel                       ( 1,             1,                      NUM_SAO_TYPE_IDX_CTX                 , m_contextModels + m_numContextModels, m_numContextModels)
101, m_cTransformSkipSCModel                    ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_TRANSFORMSKIP_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
102, m_CUTransquantBypassFlagSCModel            ( 1,             1,                      NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
103, m_explicitRdpcmFlagSCModel                 ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_EXPLICIT_RDPCM_FLAG_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
104, m_explicitRdpcmDirSCModel                  ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_EXPLICIT_RDPCM_DIR_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
105, m_cCrossComponentPredictionSCModel         ( 1,             1,                      NUM_CROSS_COMPONENT_PREDICTION_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
106, m_ChromaQpAdjFlagSCModel                   ( 1,             1,                      NUM_CHROMA_QP_ADJ_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
107, m_ChromaQpAdjIdcSCModel                    ( 1,             1,                      NUM_CHROMA_QP_ADJ_IDC_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
108#if H_3D_DIM
109, m_cDepthIntraModeSCModel    ( 1,             1,               NUM_DEPTH_INTRA_MODE_CTX      , m_contextModels + m_numContextModels, m_numContextModels)
110, m_cDdcFlagSCModel           ( 1,             1,               NUM_DDC_FLAG_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
111, m_cDdcDataSCModel           ( 1,             1,               NUM_DDC_DATA_CTX              , m_contextModels + m_numContextModels, m_numContextModels)
112, m_cAngleFlagSCModel         ( 1,             1,               NUM_ANGLE_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
113#if H_3D_DIM_SDC
114, m_cSDCResidualFlagSCModel     ( 1,             1,             SDC_NUM_RESIDUAL_FLAG_CTX     , m_contextModels + m_numContextModels, m_numContextModels)
115, m_cSDCResidualSCModel         ( 1,             1,             SDC_NUM_RESIDUAL_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
116#endif
117#endif
118#if H_3D_DIM_SDC
119, m_cSDCFlagSCModel                  ( 1,             1,  NUM_SDC_FLAG_CTX                 , m_contextModels + m_numContextModels, m_numContextModels)
120#endif
121#if NH_3D_DBBP
122, m_cDBBPFlagSCModel             ( 1,             1,                 DBBP_NUM_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
123#endif
124
125{
126  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
127}
128
129TDecSbac::~TDecSbac()
130{
131}
132
133// ====================================================================================================================
134// Public member functions
135// ====================================================================================================================
136
137Void TDecSbac::resetEntropy(TComSlice* pSlice)
138{
139  SliceType sliceType  = pSlice->getSliceType();
140  Int       qp         = pSlice->getSliceQp();
141
142  if (pSlice->getPPS()->getCabacInitPresentFlag() && pSlice->getCabacInitFlag())
143  {
144    switch (sliceType)
145    {
146    case P_SLICE:           // change initialization table to B_SLICE initialization
147      sliceType = B_SLICE;
148      break;
149    case B_SLICE:           // change initialization table to P_SLICE initialization
150      sliceType = P_SLICE;
151      break;
152    default     :           // should not occur
153      assert(0);
154      break;
155    }
156  }
157
158  m_cCUSplitFlagSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_SPLIT_FLAG );
159  m_cCUSkipFlagSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_SKIP_FLAG );
160#if H_3D
161  m_cCUDISFlagSCModel.initBuffer         ( sliceType, qp, (UChar*)INIT_DIS_FLAG );
162  m_cCUDISTypeSCModel.initBuffer         ( sliceType, qp, (UChar*)INIT_DIS_TYPE );
163#endif
164  m_cCUMergeFlagExtSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT );
165  m_cCUMergeIdxExtSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_MERGE_IDX_EXT );
166#if NH_3D_ARP
167  m_cCUPUARPWSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_ARPW );
168#endif
169#if NH_3D_IC
170  m_cCUICFlagSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_IC_FLAG );
171#endif
172  m_cCUPartSizeSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_PART_SIZE );
173  m_cCUPredModeSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_PRED_MODE );
174  m_cCUIntraPredSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
175  m_cCUChromaPredSCModel.initBuffer               ( sliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
176  m_cCUInterDirSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_INTER_DIR );
177  m_cCUMvdSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_MVD );
178  m_cCURefPicSCModel.initBuffer                   ( sliceType, qp, (UChar*)INIT_REF_PIC );
179  m_cCUDeltaQpSCModel.initBuffer                  ( sliceType, qp, (UChar*)INIT_DQP );
180  m_cCUQtCbfSCModel.initBuffer                    ( sliceType, qp, (UChar*)INIT_QT_CBF );
181  m_cCUQtRootCbfSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
182  m_cCUSigCoeffGroupSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
183  m_cCUSigSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_SIG_FLAG );
184  m_cCuCtxLastX.initBuffer                        ( sliceType, qp, (UChar*)INIT_LAST );
185  m_cCuCtxLastY.initBuffer                        ( sliceType, qp, (UChar*)INIT_LAST );
186  m_cCUOneSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_ONE_FLAG );
187  m_cCUAbsSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_ABS_FLAG );
188  m_cMVPIdxSCModel.initBuffer                     ( sliceType, qp, (UChar*)INIT_MVP_IDX );
189  m_cSaoMergeSCModel.initBuffer                   ( sliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
190  m_cSaoTypeIdxSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
191  m_cCUTransSubdivFlagSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
192  m_cTransformSkipSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
193  m_CUTransquantBypassFlagSCModel.initBuffer      ( sliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
194  m_explicitRdpcmFlagSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_FLAG);
195  m_explicitRdpcmDirSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_DIR);
196  m_cCrossComponentPredictionSCModel.initBuffer   ( sliceType, qp, (UChar*)INIT_CROSS_COMPONENT_PREDICTION );
197  m_ChromaQpAdjFlagSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_FLAG );
198  m_ChromaQpAdjIdcSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_IDC );
199#if H_3D_DIM
200  m_cDepthIntraModeSCModel.initBuffer    ( sliceType, qp, (UChar*)INIT_DEPTH_INTRA_MODE );
201  m_cDdcFlagSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_DDC_FLAG );
202  m_cDdcDataSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_DDC_DATA );
203  m_cAngleFlagSCModel.initBuffer         ( sliceType, qp, (UChar*)INIT_ANGLE_FLAG );
204#if H_3D_DIM_SDC
205  m_cSDCResidualFlagSCModel.initBuffer    ( sliceType, qp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
206  m_cSDCResidualSCModel.initBuffer        ( sliceType, qp, (UChar*)INIT_SDC_RESIDUAL );
207#endif
208#endif
209#if H_3D_DIM_SDC
210  m_cSDCFlagSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_SDC_FLAG );
211#endif
212#if NH_3D_DBBP
213  m_cDBBPFlagSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_DBBP_FLAG );
214#endif
215
216  for (UInt statisticIndex = 0; statisticIndex < RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS ; statisticIndex++)
217  {
218    m_golombRiceAdaptationStatistics[statisticIndex] = 0;
219  }
220
221  m_pcTDecBinIf->start();
222}
223
224Void TDecSbac::parseTerminatingBit( UInt& ruiBit )
225{
226  m_pcTDecBinIf->decodeBinTrm( ruiBit );
227  if ( ruiBit == 1 )
228  {
229    m_pcTDecBinIf->finish();
230
231#if RExt__DECODER_DEBUG_BIT_STATISTICS
232    TComCodingStatistics::IncrementStatisticEP(STATS__TRAILING_BITS, m_pcBitstream->readOutTrailingBits(),0);
233#else
234    m_pcBitstream->readOutTrailingBits();
235#endif
236  }
237}
238
239#if H_3D
240  m_cCUDISFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DIS_FLAG );
241  m_cCUDISTypeSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_DIS_TYPE );
242#endif
243#if H_3D_DIM
244  m_cDepthIntraModeSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_DEPTH_INTRA_MODE );
245  m_cDdcFlagSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_FLAG );
246  m_cDdcDataSCModel.initBuffer           ( eSliceType, iQp, (UChar*)INIT_DDC_DATA );
247  m_cAngleFlagSCModel.initBuffer         ( eSliceType, iQp, (UChar*)INIT_ANGLE_FLAG );
248#if H_3D_DIM_SDC
249  m_cSDCResidualFlagSCModel.initBuffer    ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL_FLAG );
250  m_cSDCResidualSCModel.initBuffer        ( eSliceType, iQp, (UChar*)INIT_SDC_RESIDUAL );
251#endif
252#endif
253#if H_3D_DIM_SDC
254  m_cSDCFlagSCModel.initBuffer            ( eSliceType, iQp, (UChar*)INIT_SDC_FLAG );
255#endif
256
257
258Void TDecSbac::parseRemainingBytes( Bool noTrailingBytesExpected )
259{
260  if (noTrailingBytesExpected)
261  {
262    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
263    assert (numberOfRemainingSubstreamBytes == 0);
264  }
265  else
266  {
267    while (m_pcBitstream->getNumBitsLeft())
268    {
269      UInt trailingNullByte=m_pcBitstream->readByte();
270      if (trailingNullByte!=0)
271      {
272        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
273        assert(trailingNullByte==0);
274      }
275    }
276  }
277}
278
279#if RExt__DECODER_DEBUG_BIT_STATISTICS
280Void TDecSbac::xReadUnaryMaxSymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol, const class TComCodingStatisticsClassType &whichStat )
281#else
282Void TDecSbac::xReadUnaryMaxSymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
283#endif
284{
285  if (uiMaxSymbol == 0)
286  {
287    ruiSymbol = 0;
288    return;
289  }
290
291  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
292
293  if( ruiSymbol == 0 || uiMaxSymbol == 1 )
294  {
295    return;
296  }
297
298  UInt uiSymbol = 0;
299  UInt uiCont;
300
301  do
302  {
303    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
304    uiSymbol++;
305  } while( uiCont && ( uiSymbol < uiMaxSymbol - 1 ) );
306
307  if( uiCont && ( uiSymbol == uiMaxSymbol - 1 ) )
308  {
309    uiSymbol++;
310  }
311
312  ruiSymbol = uiSymbol;
313}
314
315#if RExt__DECODER_DEBUG_BIT_STATISTICS
316Void TDecSbac::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount, const class TComCodingStatisticsClassType &whichStat )
317#else
318Void TDecSbac::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount )
319#endif
320{
321  UInt uiSymbol = 0;
322  UInt uiBit = 1;
323
324  while( uiBit )
325  {
326    m_pcTDecBinIf->decodeBinEP( uiBit RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
327    uiSymbol += uiBit << uiCount++;
328  }
329
330  if ( --uiCount )
331  {
332    UInt bins;
333    m_pcTDecBinIf->decodeBinsEP( bins, uiCount RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
334    uiSymbol += bins;
335  }
336
337  ruiSymbol = uiSymbol;
338}
339
340#if RExt__DECODER_DEBUG_BIT_STATISTICS
341Void TDecSbac::xReadUnarySymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, const class TComCodingStatisticsClassType &whichStat )
342#else
343Void TDecSbac::xReadUnarySymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset )
344#endif
345{
346  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
347
348  if( !ruiSymbol )
349  {
350    return;
351  }
352
353  UInt uiSymbol = 0;
354  UInt uiCont;
355
356  do
357  {
358    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
359    uiSymbol++;
360  } while( uiCont );
361
362  ruiSymbol = uiSymbol;
363}
364
365
366/** Parsing of coeff_abs_level_remaing
367 * \param rSymbol                 reference to coeff_abs_level_remaing
368 * \param rParam                  reference to parameter
369 * \param useLimitedPrefixLength
370 * \param maxLog2TrDynamicRange
371 */
372#if RExt__DECODER_DEBUG_BIT_STATISTICS
373Void TDecSbac::xReadCoefRemainExGolomb ( UInt &rSymbol, UInt &rParam, const Bool useLimitedPrefixLength, const Int maxLog2TrDynamicRange, const class TComCodingStatisticsClassType &whichStat )
374#else
375Void TDecSbac::xReadCoefRemainExGolomb ( UInt &rSymbol, UInt &rParam, const Bool useLimitedPrefixLength, const Int maxLog2TrDynamicRange )
376#endif
377{
378  UInt prefix   = 0;
379  UInt codeWord = 0;
380
381  if (useLimitedPrefixLength)
382  {
383    const UInt longestPossiblePrefix = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange)) + COEF_REMAIN_BIN_REDUCTION;
384
385    do
386    {
387      prefix++;
388      m_pcTDecBinIf->decodeBinEP( codeWord RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
389    } while((codeWord != 0) && (prefix < longestPossiblePrefix));
390  }
391  else
392  {
393    do
394    {
395      prefix++;
396      m_pcTDecBinIf->decodeBinEP( codeWord RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
397    } while( codeWord);
398  }
399
400  codeWord  = 1 - codeWord;
401  prefix -= codeWord;
402  codeWord=0;
403
404  if (prefix < COEF_REMAIN_BIN_REDUCTION )
405  {
406    m_pcTDecBinIf->decodeBinsEP(codeWord,rParam RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
407    rSymbol = (prefix<<rParam) + codeWord;
408  }
409  else if (useLimitedPrefixLength)
410  {
411    const UInt maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange));
412
413    const UInt prefixLength = prefix - COEF_REMAIN_BIN_REDUCTION;
414    const UInt suffixLength = (prefixLength == maximumPrefixLength) ? (maxLog2TrDynamicRange - rParam) : prefixLength;
415
416    m_pcTDecBinIf->decodeBinsEP(codeWord, (suffixLength + rParam) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
417
418    rSymbol = codeWord + ((((1 << prefixLength) - 1) + COEF_REMAIN_BIN_REDUCTION) << rParam);
419  }
420  else
421  {
422    m_pcTDecBinIf->decodeBinsEP(codeWord,prefix-COEF_REMAIN_BIN_REDUCTION+rParam RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
423    rSymbol = (((1<<(prefix-COEF_REMAIN_BIN_REDUCTION))+COEF_REMAIN_BIN_REDUCTION-1)<<rParam)+codeWord;
424  }
425}
426
427#if H_3D_DIM
428Void TDecSbac::xReadExGolombLevel( UInt& ruiSymbol, ContextModel& rcSCModel  )
429{
430  UInt uiSymbol;
431  UInt uiCount = 0;
432  do
433  {
434    m_pcTDecBinIf->decodeBin( uiSymbol, rcSCModel );
435    uiCount++;
436  }
437  while( uiSymbol && ( uiCount != 3 ) );
438  ruiSymbol = uiCount - 1;
439
440  if( uiSymbol )
441  {
442    xReadEpExGolomb( uiSymbol, 0 );
443    ruiSymbol += uiSymbol + 1;
444  }
445
446  return;
447}
448
449Void TDecSbac::xParseDimDeltaDC( Pel& rValDeltaDC, UInt uiNumSeg )
450{
451  UInt absValDeltaDC = 0;
452  xReadExGolombLevel( absValDeltaDC, m_cDdcDataSCModel.get(0, 0, 0) );
453  rValDeltaDC = (Pel)absValDeltaDC + ( uiNumSeg > 1 ? 0 : 1 );
454
455  if( rValDeltaDC != 0 )
456  {
457    UInt uiSign;
458    m_pcTDecBinIf->decodeBinEP( uiSign );
459    if ( uiSign )
460    {
461      rValDeltaDC = -rValDeltaDC;
462    }
463  }
464}
465
466
467#if H_3D_DIM_DMM
468Void TDecSbac::xParseDmm1WedgeIdx( UInt& ruiTabIdx, Int iNumBit )
469{
470  UInt uiSymbol, uiIdx = 0;
471  for( Int i = 0; i < iNumBit; i++ )
472  {
473    m_pcTDecBinIf->decodeBinEP( uiSymbol );
474    uiIdx += uiSymbol << i;
475  }
476  ruiTabIdx = uiIdx;
477}
478#endif
479
480
481#endif
482
483
484/** Parse I_PCM information.
485 * \param pcCU
486 * \param uiAbsPartIdx
487 * \param uiDepth
488 * \returns Void
489 *
490 * If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
491 */
492Void TDecSbac::parseIPCMInfo ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
493{
494  UInt uiSymbol;
495
496  m_pcTDecBinIf->decodeBinTrm(uiSymbol);
497
498#if H_MV_ENC_DEC_TRAC
499  DTRACE_CU("pcm_flag", uiSymbol)
500#endif
501
502
503  if (uiSymbol == 1)
504  {
505    Bool bIpcmFlag = true;
506    const TComSPS &sps=*(pcCU->getSlice()->getSPS());
507
508    pcCU->setPartSizeSubParts  ( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
509    pcCU->setSizeSubParts      ( sps.getMaxCUWidth()>>uiDepth, sps.getMaxCUHeight()>>uiDepth, uiAbsPartIdx, uiDepth );
510    pcCU->setTrIdxSubParts     ( 0, uiAbsPartIdx, uiDepth );
511    pcCU->setIPCMFlagSubParts  ( bIpcmFlag, uiAbsPartIdx, uiDepth );
512
513    const UInt minCoeffSizeY = pcCU->getPic()->getMinCUWidth() * pcCU->getPic()->getMinCUHeight();
514    const UInt offsetY       = minCoeffSizeY * uiAbsPartIdx;
515    for (UInt ch=0; ch < pcCU->getPic()->getNumberValidComponents(); ch++)
516    {
517      const ComponentID compID = ComponentID(ch);
518      const UInt offset = offsetY >> (pcCU->getPic()->getComponentScaleX(compID) + pcCU->getPic()->getComponentScaleY(compID));
519      Pel * pPCMSample  = pcCU->getPCMSample(compID) + offset;
520      const UInt width  = pcCU->getWidth (uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleX(compID);
521      const UInt height = pcCU->getHeight(uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleY(compID);
522      const UInt sampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepth(toChannelType(compID));
523      for (UInt y=0; y<height; y++)
524      {
525        for (UInt x=0; x<width; x++)
526        {
527          UInt sample;
528          m_pcTDecBinIf->xReadPCMCode(sampleBits, sample);
529          pPCMSample[x] = sample;
530        }
531        pPCMSample += width;
532      }
533    }
534    m_pcTDecBinIf->start();
535  }
536}
537
538Void TDecSbac::parseCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
539{
540  UInt uiSymbol;
541  m_pcTDecBinIf->decodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__TQ_BYPASS_FLAG) );
542#if H_MV_ENC_DEC_TRAC
543  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
544#endif
545
546  pcCU->setCUTransquantBypassSubParts(uiSymbol ? true : false, uiAbsPartIdx, uiDepth);
547}
548
549/** parse skip flag
550 * \param pcCU
551 * \param uiAbsPartIdx
552 * \param uiDepth
553 * \returns Void
554 */
555Void TDecSbac::parseSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
556{
557  if( pcCU->getSlice()->isIntra() )
558  {
559    return;
560  }
561
562  UInt uiSymbol = 0;
563  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx );
564  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SKIP_FLAG) );
565#if !H_MV_ENC_DEC_TRAC
566  DTRACE_CABAC_VL( g_nSymbolCounter++ );
567  DTRACE_CABAC_T( "\tSkipFlag" );
568  DTRACE_CABAC_T( "\tuiCtxSkip: ");
569  DTRACE_CABAC_V( uiCtxSkip );
570  DTRACE_CABAC_T( "\tuiSymbol: ");
571  DTRACE_CABAC_V( uiSymbol );
572  DTRACE_CABAC_T( "\n");
573#endif
574  if( uiSymbol )
575  {
576    pcCU->setSkipFlagSubParts( true,        uiAbsPartIdx, uiDepth );
577    pcCU->setPredModeSubParts( MODE_INTER,  uiAbsPartIdx, uiDepth );
578    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
579    pcCU->setSizeSubParts( pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth, pcCU->getSlice()->getSPS()->getMaxCUHeight()>>uiDepth, uiAbsPartIdx, uiDepth );
580    pcCU->setMergeFlagSubParts( true , uiAbsPartIdx, 0, uiDepth );
581  }
582#if H_MV_ENC_DEC_TRAC
583  DTRACE_CU("cu_skip_flag", uiSymbol); 
584#endif
585
586}
587#if H_3D
588Void TDecSbac::parseDIS( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
589{
590  pcCU->setDISFlagSubParts( false,        uiAbsPartIdx, uiDepth );
591  UInt uiSymbol = 0;
592  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUDISFlagSCModel.get( 0, 0, 0 ) );
593  if( uiSymbol )
594  {
595    pcCU->setDISFlagSubParts( true,        uiAbsPartIdx, uiDepth );
596    pcCU->setSkipFlagSubParts( false,        uiAbsPartIdx, uiDepth );
597    pcCU->setSDCFlagSubParts( false,        uiAbsPartIdx, uiDepth );
598    pcCU->setPredModeSubParts( MODE_INTRA,  uiAbsPartIdx, uiDepth );
599    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
600    pcCU->setLumaIntraDirSubParts (DC_IDX, uiAbsPartIdx, uiDepth );
601    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
602    pcCU->setMergeFlagSubParts( false , uiAbsPartIdx, 0, uiDepth );
603    pcCU->setTrIdxSubParts(0, uiAbsPartIdx, uiDepth);
604    pcCU->setCbfSubParts(0, 1, 1, uiAbsPartIdx, uiDepth);
605
606    UInt uiUnaryIdx = 0;
607    UInt uiNumCand  = 4;
608
609    if ( uiNumCand > 1 )
610    {
611      for( ; uiUnaryIdx < uiNumCand - 1; ++uiUnaryIdx )
612      {
613        UInt uiSymbol2 = 0;
614        if ( uiUnaryIdx==0 )
615        {
616          m_pcTDecBinIf->decodeBin( uiSymbol2, m_cCUDISTypeSCModel.get( 0, 0, 0 ) );
617        }
618        else
619        {
620          m_pcTDecBinIf->decodeBinEP( uiSymbol2);
621        }
622        if( uiSymbol2 == 0 )
623        {
624          break;
625        }
626      }
627    }
628    pcCU->setDISTypeSubParts(uiUnaryIdx, uiAbsPartIdx, 0, uiDepth);
629  }
630}
631#endif
632
633
634/** parse merge flag
635 * \param pcCU
636 * \param uiAbsPartIdx
637 * \param uiDepth
638 * \param uiPUIdx
639 * \returns Void
640 */
641Void TDecSbac::parseMergeFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiPUIdx )
642{
643  UInt uiSymbol;
644  m_pcTDecBinIf->decodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_FLAG) );
645#if H_MV_ENC_DEC_TRAC
646  DTRACE_PU("merge_flag", uiSymbol)
647#endif
648  pcCU->setMergeFlagSubParts( uiSymbol ? true : false, uiAbsPartIdx, uiPUIdx, uiDepth );
649#if !H_MV_ENC_DEC_TRAC
650  DTRACE_CABAC_VL( g_nSymbolCounter++ );
651  DTRACE_CABAC_T( "\tMergeFlag: " );
652  DTRACE_CABAC_V( uiSymbol );
653  DTRACE_CABAC_T( "\tAddress: " );
654  DTRACE_CABAC_V( pcCU->getCtuRsAddr() );
655  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
656  DTRACE_CABAC_V( uiAbsPartIdx );
657  DTRACE_CABAC_T( "\n" );
658#endif
659}
660
661Void TDecSbac::parseMergeIndex ( TComDataCU* pcCU, UInt& ruiMergeIndex )
662{
663  UInt uiUnaryIdx = 0;
664  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
665  if ( uiNumCand > 1 )
666  {
667    for( ; uiUnaryIdx < uiNumCand - 1; ++uiUnaryIdx )
668    {
669      UInt uiSymbol = 0;
670      if ( uiUnaryIdx==0 )
671      {
672        m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_INDEX) );
673      }
674      else
675      {
676        m_pcTDecBinIf->decodeBinEP( uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_INDEX) );
677      }
678      if( uiSymbol == 0 )
679      {
680        break;
681      }
682    }
683#if H_MV_ENC_DEC_TRAC
684    DTRACE_PU("merge_idx", uiUnaryIdx)
685#endif
686  }
687  ruiMergeIndex = uiUnaryIdx;
688
689#if !H_MV_ENC_DEC_TRAC
690  DTRACE_CABAC_VL( g_nSymbolCounter++ )
691  DTRACE_CABAC_T( "\tparseMergeIndex()" )
692  DTRACE_CABAC_T( "\tuiMRGIdx= " )
693  DTRACE_CABAC_V( ruiMergeIndex )
694  DTRACE_CABAC_T( "\n" )
695#endif
696}
697
698Void TDecSbac::parseMVPIdx      ( Int& riMVPIdx )
699{
700  UInt uiSymbol;
701  xReadUnaryMaxSymbol(uiSymbol, m_cMVPIdxSCModel.get(0), 1, AMVP_MAX_NUM_CANDS-1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVP_IDX) );
702  riMVPIdx = uiSymbol;
703}
704
705Void TDecSbac::parseSplitFlag     ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
706{
707  if( uiDepth == pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize() )
708  {
709    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
710    return;
711  }
712#if RExt__DECODER_DEBUG_BIT_STATISTICS
713  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__SPLIT_FLAG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth]+2);
714#endif
715
716  UInt uiSymbol;
717#if NH_3D_QTLPC
718  Bool bParseSplitFlag    = true;
719  Bool    bLimQtPredFlag = pcCU->getPic()->getSlice(0)->getQtPredFlag();
720  TComPic *pcTexture      = pcCU->getSlice()->getTexturePic();
721  Bool bDepthMapDetect    = (pcTexture != NULL);
722  Bool bIntraSliceDetect  = (pcCU->getSlice()->getSliceType() == I_SLICE);
723
724  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);
725
726#if H_3D_FCO
727  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && bLimQtPredFlag && pcTexture->getReconMark())
728#else
729  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && bLimQtPredFlag)
730#endif
731  {
732    TComDataCU *pcTextureCU = pcTexture->getCtu(pcCU->getCtuRsAddr());
733    assert(pcTextureCU->getDepth(uiAbsPartIdx) >= uiDepth);
734    bParseSplitFlag         = (pcTextureCU->getDepth(uiAbsPartIdx) > uiDepth);
735  }
736
737  if(bParseSplitFlag)
738  {
739#endif
740  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSplitFlagSCModel.get( 0, 0, pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ) ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
741#if H_MV_ENC_DEC_TRAC
742    DTRACE_CU("split_cu_flag", uiSymbol); 
743#else
744  DTRACE_CABAC_VL( g_nSymbolCounter++ )
745  DTRACE_CABAC_T( "\tSplitFlag\n" )
746#endif
747#if NH_3D_QTLPC
748  }
749  else
750  {
751    uiSymbol = 0;
752  }
753#endif
754  pcCU->setDepthSubParts( uiDepth + uiSymbol, uiAbsPartIdx );
755
756  return;
757}
758
759/** parse partition size
760 * \param pcCU
761 * \param uiAbsPartIdx
762 * \param uiDepth
763 * \returns Void
764 */
765Void TDecSbac::parsePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
766{
767  UInt uiSymbol, uiMode = 0;
768  PartSize eMode;
769  const UChar cuWidth =UChar(pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth);
770  const UChar cuHeight=UChar(pcCU->getSlice()->getSPS()->getMaxCUHeight()>>uiDepth);
771  const Int log2DiffMaxMinCodingBlockSize = pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize();
772
773#if RExt__DECODER_DEBUG_BIT_STATISTICS
774  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__PART_SIZE, g_aucConvertToBit[cuWidth]+2);
775#endif
776
777  assert ( pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize() == log2DiffMaxMinCodingBlockSize);
778
779#if NH_3D_QTLPC
780  Bool bParsePartSize    = true;
781
782  Bool    bLimQtPredFlag = pcCU->getPic()->getSlice(0)->getQtPredFlag();
783  TComPic *pcTexture     = pcCU->getSlice()->getTexturePic();
784  Bool bDepthMapDetect   = (pcTexture != NULL);
785  Bool bIntraSliceDetect = (pcCU->getSlice()->getSliceType() == I_SLICE);
786
787  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);
788
789  Bool depthDependent = false;
790  UInt uiTexturePart = uiMode;
791
792#if H_3D_FCO
793  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && bLimQtPredFlag && pcTexture->getReconMark())
794#else
795  if(bDepthMapDetect && !bIntraSliceDetect && !rapPic && bLimQtPredFlag )
796#endif
797  {
798    TComDataCU *pcTextureCU = pcTexture->getCtu(pcCU->getCtuRsAddr());
799    assert(pcTextureCU->getDepth(uiAbsPartIdx) >= uiDepth);
800    if(pcTextureCU->getDepth(uiAbsPartIdx) == uiDepth )
801    {
802      depthDependent = true;
803      uiTexturePart = pcTextureCU->getPartitionSize( uiAbsPartIdx );
804    }
805    if (pcTextureCU->getDepth(uiAbsPartIdx) == uiDepth && pcTextureCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N)
806    {
807      bParsePartSize = false;
808      eMode          = SIZE_2Nx2N;
809    }
810  }
811#endif
812
813  if ( pcCU->isIntra( uiAbsPartIdx ) )
814  {
815#if NH_3D_QTLPC
816    if(bParsePartSize)
817    {
818#endif
819    uiSymbol = 1;
820    if( uiDepth == log2DiffMaxMinCodingBlockSize )
821    {
822      m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
823#if H_MV_ENC_DEC_TRAC         
824        DTRACE_CU("part_mode", uiSymbol)
825#endif       
826    }
827    eMode = uiSymbol ? SIZE_2Nx2N : SIZE_NxN;
828#if NH_3D_QTLPC
829    }
830#endif
831    UInt uiTrLevel = 0;
832    UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)]+2;
833    UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()]+2;
834    uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
835    if( eMode == SIZE_NxN )
836    {
837      pcCU->setTrIdxSubParts( 1+uiTrLevel, uiAbsPartIdx, uiDepth );
838    }
839    else
840    {
841      pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );
842    }
843  }
844  else
845  {
846#if NH_3D_QTLPC
847    if(bParsePartSize)
848    {
849      if (depthDependent==false || uiTexturePart == SIZE_NxN|| uiTexturePart == SIZE_2Nx2N)
850      {
851#endif
852    UInt uiMaxNumBits = 2;
853
854    if( uiDepth == log2DiffMaxMinCodingBlockSize && !( cuWidth == 8 && cuHeight == 8 ) )
855    {
856      uiMaxNumBits ++;
857    }
858
859    for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
860    {
861      m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, ui) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
862      if ( uiSymbol )
863      {
864        break;
865      }
866      uiMode++;
867    }
868    eMode = (PartSize) uiMode;
869    if ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize )
870    {
871      if (eMode == SIZE_2NxN)
872      {
873        m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 3 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
874        if (uiSymbol == 0)
875        {
876          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
877          eMode = (uiSymbol == 0? SIZE_2NxnU : SIZE_2NxnD);
878        }
879      }
880      else if (eMode == SIZE_Nx2N)
881      {
882        m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 3 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
883        if (uiSymbol == 0)
884        {
885          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
886          eMode = (uiSymbol == 0? SIZE_nLx2N : SIZE_nRx2N);
887        }
888      }
889    }
890#if NH_3D_QTLPC
891      }
892      else if(uiTexturePart == SIZE_2NxN || uiTexturePart == SIZE_2NxnU || uiTexturePart == SIZE_2NxnD)
893      {
894        UInt uiMaxNumBits = 1;
895        if ( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize )
896        {
897          uiMaxNumBits ++;
898        }
899        for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
900        {
901          m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, ui) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
902          if ( uiSymbol )
903          {
904            break;
905          }
906          uiMode++;
907        }
908        eMode = (PartSize) uiMode;
909        if(uiMode && ( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiSymbol==1 )
910        {
911          eMode = SIZE_2NxN;
912        }
913        else if (uiMode && (( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize )==0)  && uiSymbol==0)
914        {
915          eMode = SIZE_2NxN;
916        }
917        else if (uiMode && ( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiSymbol==0)
918        {
919          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
920          eMode = (uiSymbol == 0? SIZE_2NxnU : SIZE_2NxnD);
921        }
922      }
923      else if(uiTexturePart == SIZE_Nx2N|| uiTexturePart==SIZE_nLx2N || uiTexturePart==SIZE_nRx2N)
924      {
925        UInt uiMaxNumBits = 1;
926        if (  ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize ) 
927        {
928          uiMaxNumBits ++;
929        }
930        for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
931        {
932          m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, ui) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
933          if ( uiSymbol )
934          {
935            break;
936          }
937          uiMode++;
938        }
939        eMode = (PartSize) uiMode;
940        if(uiMode && ( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiSymbol==1 )
941        {
942          eMode = SIZE_Nx2N;
943        }
944        else if (uiMode && (( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize )==0)  && uiSymbol==0)
945        {
946          eMode = SIZE_Nx2N;
947        }
948        else if (uiMode && ( ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiDepth < log2DiffMaxMinCodingBlockSize ) && uiSymbol==0)
949        {
950          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
951          eMode = (uiSymbol == 0? SIZE_nLx2N : SIZE_nRx2N);
952        }
953      }
954      else
955      {
956        assert(0);
957      }
958#endif
959#if H_MV_ENC_DEC_TRAC         
960      DTRACE_CU("part_mode", eMode )
961#endif
962#if NH_3D_QTLPC
963    }
964#endif
965  }
966  pcCU->setPartSizeSubParts( eMode, uiAbsPartIdx, uiDepth );
967  pcCU->setSizeSubParts( cuWidth, cuHeight, uiAbsPartIdx, uiDepth );
968}
969
970
971/** parse prediction mode
972 * \param pcCU
973 * \param uiAbsPartIdx
974 * \param uiDepth
975 * \returns Void
976 */
977Void TDecSbac::parsePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
978{
979  if( pcCU->getSlice()->isIntra() )
980  {
981    pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
982    return;
983  }
984
985  UInt uiSymbol;
986  Int  iPredMode = MODE_INTER;
987  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPredModeSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__PRED_MODE) );
988  iPredMode += uiSymbol;
989#if H_MV_ENC_DEC_TRAC         
990  DTRACE_CU("pred_mode_flag", uiSymbol)
991#endif       
992  pcCU->setPredModeSubParts( (PredMode)iPredMode, uiAbsPartIdx, uiDepth );
993}
994
995
996Void TDecSbac::parseIntraDirLumaAng  ( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
997{
998  PartSize mode = pcCU->getPartitionSize( absPartIdx );
999  UInt partNum = mode==SIZE_NxN?4:1;
1000  UInt partOffset = ( pcCU->getPic()->getNumPartitionsInCtu() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
1001  UInt mpmPred[4],symbol;
1002  Int j,intraPredMode;
1003  if (mode==SIZE_NxN)
1004  {
1005    depth++;
1006  }
1007#if RExt__DECODER_DEBUG_BIT_STATISTICS
1008  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__INTRA_DIR_ANG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>depth]+2, CHANNEL_TYPE_LUMA);
1009#endif
1010  for (j=0;j<partNum;j++)
1011  {
1012#if H_3D_DIM
1013    if( pcCU->getSlice()->getIntraSdcWedgeFlag() || pcCU->getSlice()->getIntraContourFlag() )
1014    {
1015      parseIntraDepth( pcCU, absPartIdx+partOffset*j, depth );
1016    }
1017    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1018    {
1019#endif
1020    m_pcTDecBinIf->decodeBin( symbol, m_cCUIntraPredSCModel.get( 0, 0, 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1021    mpmPred[j] = symbol;
1022#if H_MV_ENC_DEC_TRAC         
1023      DTRACE_CU("prev_intra_luma_pred_flag", symbol)
1024#endif
1025#if H_3D_DIM
1026    }
1027#endif
1028  }
1029  for (j=0;j<partNum;j++)
1030  {
1031#if H_3D_DIM
1032    if( pcCU->getLumaIntraDir( absPartIdx+partOffset*j ) < NUM_INTRA_MODE )
1033    {
1034#endif
1035    Int preds[NUM_MOST_PROBABLE_MODES] = {-1, -1, -1};
1036    pcCU->getIntraDirPredictor(absPartIdx+partOffset*j, preds, COMPONENT_Y);
1037    if (mpmPred[j])
1038    {
1039      m_pcTDecBinIf->decodeBinEP( symbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1040      if (symbol)
1041      {
1042        m_pcTDecBinIf->decodeBinEP( symbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1043        symbol++;
1044      }
1045#if H_MV_ENC_DEC_TRAC         
1046        DTRACE_CU("mpm_idx", symbol)
1047#endif
1048      intraPredMode = preds[symbol];
1049    }
1050    else
1051    {
1052      m_pcTDecBinIf->decodeBinsEP( symbol, 5 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1053      intraPredMode = symbol;
1054#if H_MV_ENC_DEC_TRAC         
1055        DTRACE_CU("rem_intra_luma_pred_mode", symbol)
1056#endif       
1057
1058      //postponed sorting of MPMs (only in remaining branch)
1059      if (preds[0] > preds[1])
1060      {
1061        std::swap(preds[0], preds[1]);
1062      }
1063      if (preds[0] > preds[2])
1064      {
1065        std::swap(preds[0], preds[2]);
1066      }
1067      if (preds[1] > preds[2])
1068      {
1069        std::swap(preds[1], preds[2]);
1070      }
1071      for ( UInt i = 0; i < NUM_MOST_PROBABLE_MODES; i++ )
1072      {
1073        intraPredMode += ( intraPredMode >= preds[i] );
1074      }
1075    }
1076    pcCU->setIntraDirSubParts(CHANNEL_TYPE_LUMA, (UChar)intraPredMode, absPartIdx+partOffset*j, depth );
1077#if H_3D_DIM
1078    }
1079#endif
1080  }
1081}
1082
1083
1084Void TDecSbac::parseIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1085{
1086  UInt uiSymbol;
1087#if RExt__DECODER_DEBUG_BIT_STATISTICS
1088  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__INTRA_DIR_ANG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth]+2, CHANNEL_TYPE_CHROMA);
1089#endif
1090
1091  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUChromaPredSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1092  if( uiSymbol == 0 )
1093  {
1094#if H_MV_ENC_DEC_TRAC         
1095    DTRACE_CU("intra_chroma_pred_mode", uiSymbol )
1096#endif       
1097    uiSymbol = DM_CHROMA_IDX;
1098  }
1099  else
1100  {
1101    UInt uiIPredMode;
1102    m_pcTDecBinIf->decodeBinsEP( uiIPredMode, 2 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1103#if H_MV_ENC_DEC_TRAC         
1104      DTRACE_CU("intra_chroma_pred_mode", uiIPredMode )
1105#endif       
1106    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
1107    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
1108    uiSymbol = uiAllowedChromaDir[ uiIPredMode ];
1109  }
1110
1111  pcCU->setIntraDirSubParts( CHANNEL_TYPE_CHROMA, uiSymbol, uiAbsPartIdx, uiDepth );
1112}
1113
1114#if H_3D_DIM
1115Void TDecSbac::parseIntraDepth( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
1116{
1117  parseIntraDepthMode( pcCU, absPartIdx, depth );
1118
1119  UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
1120  UInt dimType = getDimType( dir );
1121
1122  switch( dimType )
1123  {
1124#if H_3D_DIM_DMM
1125  case( DMM1_IDX ):
1126    {
1127      UInt uiTabIdx = 0;
1128      xParseDmm1WedgeIdx( uiTabIdx, g_dmm1TabIdxBits[pcCU->getIntraSizeIdx(absPartIdx)] );
1129
1130      assert( uiTabIdx < g_dmmWedgeLists[ g_aucConvertToBit[ pcCU->getDMM1BasePatternWidth( pcCU->getWidth( absPartIdx ) )]].size());
1131
1132      pcCU->setDmmWedgeTabIdxSubParts( uiTabIdx, dimType, absPartIdx, depth );
1133    } break;
1134  case( DMM4_IDX ): break;
1135#endif
1136  default: break;
1137  }
1138
1139  pcCU->setLumaIntraDirSubParts( (UChar)dir, absPartIdx, depth );
1140}
1141
1142Void TDecSbac::parseIntraDepthMode( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
1143{
1144  UInt uiSymbol, uiIsDimMode;
1145
1146  if( ( pcCU->getSlice()->getSPS()->getMaxCUWidth() >> pcCU->getDepth( absPartIdx ) ) < 64 ) //DMM and HEVC intra modes are both allowed
1147  {
1148    m_pcTDecBinIf->decodeBin( uiSymbol, m_cAngleFlagSCModel.get( 0, 0, 0 ) );
1149  }
1150  else
1151  {
1152    uiSymbol = 1;
1153  }
1154  uiIsDimMode = uiSymbol ? 0 : 1;
1155  pcCU->setLumaIntraDirSubParts( 0, absPartIdx, depth );
1156
1157  //decode DMM index
1158  if( uiIsDimMode )
1159  {
1160    if( pcCU->getSlice()->getIntraSdcWedgeFlag() && pcCU->getSlice()->getIntraContourFlag() )
1161    {
1162      m_pcTDecBinIf->decodeBin( uiSymbol, m_cDepthIntraModeSCModel.get( 0, 0, 0 ) );
1163      if( !uiSymbol )
1164      {
1165        pcCU->setLumaIntraDirSubParts( DIM_OFFSET, absPartIdx, depth );
1166      }
1167      else
1168      {
1169        pcCU->setLumaIntraDirSubParts( ( 1+ DIM_OFFSET ), absPartIdx, depth );
1170      }
1171    }
1172    else if ( pcCU->getSlice()->getIntraSdcWedgeFlag() )
1173    {
1174      pcCU->setLumaIntraDirSubParts( DIM_OFFSET, absPartIdx, depth );
1175    }
1176    else if( pcCU->getSlice()->getIntraContourFlag() )
1177    {
1178      pcCU->setLumaIntraDirSubParts( ( 1+ DIM_OFFSET ), absPartIdx, depth );
1179    }
1180  }
1181}
1182#endif
1183
1184Void TDecSbac::parseInterDir( TComDataCU* pcCU, UInt& ruiInterDir, UInt uiAbsPartIdx )
1185{
1186  UInt uiSymbol;
1187  const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx );
1188  ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 );
1189
1190  uiSymbol = 0;
1191  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
1192  {
1193    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + uiCtx ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__INTER_DIR) );
1194  }
1195
1196  if( uiSymbol )
1197  {
1198    uiSymbol = 2;
1199  }
1200  else
1201  {
1202    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + 4 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__INTER_DIR) );
1203    assert(uiSymbol == 0 || uiSymbol == 1);
1204  }
1205
1206  uiSymbol++;
1207  ruiInterDir = uiSymbol;
1208#if H_MV_ENC_DEC_TRAC
1209    DTRACE_PU("inter_pred_idc", ruiInterDir - 1 )   
1210#endif
1211
1212  return;
1213}
1214
1215Void TDecSbac::parseRefFrmIdx( TComDataCU* pcCU, Int& riRefFrmIdx, RefPicList eRefList )
1216{
1217  UInt uiSymbol;
1218
1219  ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
1220  m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
1221
1222  if( uiSymbol )
1223  {
1224    UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
1225    pCtx++;
1226    UInt ui;
1227    for( ui = 0; ui < uiRefNum; ++ui )
1228    {
1229      if( ui == 0 )
1230      {
1231        m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
1232      }
1233      else
1234      {
1235        m_pcTDecBinIf->decodeBinEP( uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
1236      }
1237      if( uiSymbol == 0 )
1238      {
1239        break;
1240      }
1241    }
1242    uiSymbol = ui + 1;
1243  }
1244  riRefFrmIdx = uiSymbol;
1245#if H_MV_ENC_DEC_TRAC
1246#if ENC_DEC_TRACE
1247  if ( eRefList == REF_PIC_LIST_0 )
1248  {
1249    DTRACE_PU("ref_idx_l0", uiSymbol)
1250  }
1251  else
1252  {
1253    DTRACE_PU("ref_idx_l1", uiSymbol)
1254  }
1255#endif
1256#endif
1257
1258  return;
1259}
1260
1261Void TDecSbac::parseMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth, RefPicList eRefList )
1262{
1263  UInt uiSymbol;
1264  UInt uiHorAbs;
1265  UInt uiVerAbs;
1266  UInt uiHorSign = 0;
1267  UInt uiVerSign = 0;
1268  ContextModel *pCtx = m_cCUMvdSCModel.get( 0 );
1269
1270  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
1271  {
1272    uiHorAbs=0;
1273    uiVerAbs=0;
1274  }
1275  else
1276  {
1277    m_pcTDecBinIf->decodeBin( uiHorAbs, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
1278    m_pcTDecBinIf->decodeBin( uiVerAbs, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
1279
1280    const Bool bHorAbsGr0 = uiHorAbs != 0;
1281    const Bool bVerAbsGr0 = uiVerAbs != 0;
1282    pCtx++;
1283
1284    if( bHorAbsGr0 )
1285    {
1286      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
1287      uiHorAbs += uiSymbol;
1288    }
1289
1290    if( bVerAbsGr0 )
1291    {
1292      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
1293      uiVerAbs += uiSymbol;
1294    }
1295
1296    if( bHorAbsGr0 )
1297    {
1298      if( 2 == uiHorAbs )
1299      {
1300        xReadEpExGolomb( uiSymbol, 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
1301        uiHorAbs += uiSymbol;
1302      }
1303
1304      m_pcTDecBinIf->decodeBinEP( uiHorSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
1305    }
1306
1307    if( bVerAbsGr0 )
1308    {
1309      if( 2 == uiVerAbs )
1310      {
1311        xReadEpExGolomb( uiSymbol, 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
1312        uiVerAbs += uiSymbol;
1313      }
1314
1315      m_pcTDecBinIf->decodeBinEP( uiVerSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
1316    }
1317
1318  }
1319
1320  const TComMv cMv( uiHorSign ? -Int( uiHorAbs ): uiHorAbs, uiVerSign ? -Int( uiVerAbs ) : uiVerAbs );
1321  pcCU->getCUMvField( eRefList )->setAllMvd( cMv, pcCU->getPartitionSize( uiAbsPartIdx ), uiAbsPartIdx, uiDepth, uiPartIdx );
1322  return;
1323}
1324
1325Void TDecSbac::parseCrossComponentPrediction( TComTU &rTu, ComponentID compID )
1326{
1327  TComDataCU *pcCU = rTu.getCU();
1328
1329  if( isLuma(compID) || !pcCU->getSlice()->getPPS()->getPpsRangeExtension().getCrossComponentPredictionEnabledFlag() )
1330  {
1331    return;
1332  }
1333
1334  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
1335
1336  if (!pcCU->isIntra(uiAbsPartIdx) || (pcCU->getIntraDir( CHANNEL_TYPE_CHROMA, uiAbsPartIdx ) == DM_CHROMA_IDX))
1337  {
1338    Char alpha  = 0;
1339    UInt symbol = 0;
1340
1341    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1342    DTRACE_CABAC_T("\tparseCrossComponentPrediction()")
1343    DTRACE_CABAC_T( "\tAddr=" )
1344    DTRACE_CABAC_V( compID )
1345    DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1346    DTRACE_CABAC_V( uiAbsPartIdx )
1347#if RExt__DECODER_DEBUG_BIT_STATISTICS
1348    TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__CROSS_COMPONENT_PREDICTION, (g_aucConvertToBit[rTu.getRect(compID).width] + 2), compID);
1349#endif
1350    ContextModel *pCtx = m_cCrossComponentPredictionSCModel.get(0, 0) + ((compID == COMPONENT_Cr) ? (NUM_CROSS_COMPONENT_PREDICTION_CTX >> 1) : 0);
1351    m_pcTDecBinIf->decodeBin( symbol, pCtx[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1352
1353    if(symbol != 0)
1354    {
1355      // Cross-component prediction alpha is non-zero.
1356      UInt sign = 0;
1357      m_pcTDecBinIf->decodeBin( symbol, pCtx[1] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1358
1359      if (symbol != 0)
1360      {
1361        // alpha is 2 (symbol=1), 4(symbol=2) or 8(symbol=3).
1362        // Read up to two more bits
1363        xReadUnaryMaxSymbol( symbol, (pCtx + 2), 1, 2 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1364        symbol += 1;
1365      }
1366      m_pcTDecBinIf->decodeBin( sign, pCtx[4] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1367
1368      alpha = (sign != 0) ? -(1 << symbol) : (1 << symbol);
1369    }
1370    DTRACE_CABAC_T( "\tAlpha=" )
1371    DTRACE_CABAC_V( alpha )
1372    DTRACE_CABAC_T( "\n" )
1373
1374    pcCU->setCrossComponentPredictionAlphaPartRange( alpha, compID, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts( compID ) );
1375  }
1376}
1377
1378Void TDecSbac::parseTransformSubdivFlag( UInt& ruiSubdivFlag, UInt uiLog2TransformBlockSize )
1379{
1380  m_pcTDecBinIf->decodeBin( ruiSubdivFlag, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiLog2TransformBlockSize )
1381      RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__TRANSFORM_SUBDIV_FLAG, 5-uiLog2TransformBlockSize))
1382                          );
1383#if !H_MV_ENC_DEC_TRAC
1384  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1385  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
1386  DTRACE_CABAC_T( "\tsymbol=" )
1387  DTRACE_CABAC_V( ruiSubdivFlag )
1388  DTRACE_CABAC_T( "\tctx=" )
1389  DTRACE_CABAC_V( uiLog2TransformBlockSize )
1390  DTRACE_CABAC_T( "\n" )
1391#else
1392  DTRACE_TU("split_transform_flag", ruiSubdivFlag )
1393#endif
1394
1395}
1396
1397Void TDecSbac::parseQtRootCbf( UInt uiAbsPartIdx, UInt& uiQtRootCbf )
1398{
1399  UInt uiSymbol;
1400  const UInt uiCtx = 0;
1401  m_pcTDecBinIf->decodeBin( uiSymbol , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__QT_ROOT_CBF) );
1402#if !H_MV_ENC_DEC_TRAC
1403  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1404  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1405  DTRACE_CABAC_T( "\tsymbol=" )
1406  DTRACE_CABAC_V( uiSymbol )
1407  DTRACE_CABAC_T( "\tctx=" )
1408  DTRACE_CABAC_V( uiCtx )
1409  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1410  DTRACE_CABAC_V( uiAbsPartIdx )
1411  DTRACE_CABAC_T( "\n" )
1412#else
1413  DTRACE_CU( "rqt_root_cbf", uiSymbol )
1414#endif
1415
1416  uiQtRootCbf = uiSymbol;
1417}
1418
1419Void TDecSbac::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1420{
1421  Int qp;
1422  UInt uiDQp;
1423  Int  iDQp;
1424
1425  UInt uiSymbol;
1426
1427  xReadUnaryMaxSymbol (uiDQp,  &m_cCUDeltaQpSCModel.get( 0, 0, 0 ), 1, CU_DQP_TU_CMAX RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DELTA_QP_EP) );
1428
1429  if( uiDQp >= CU_DQP_TU_CMAX)
1430  {
1431    xReadEpExGolomb( uiSymbol, CU_DQP_EG_k RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DELTA_QP_EP));
1432    uiDQp+=uiSymbol;
1433  }
1434
1435  if ( uiDQp > 0 )
1436  {
1437    UInt uiSign;
1438    Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
1439    m_pcTDecBinIf->decodeBinEP(uiSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DELTA_QP_EP));
1440    iDQp = uiDQp;
1441    if(uiSign)
1442    {
1443      iDQp = -iDQp;
1444    }
1445    qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+qpBdOffsetY)) - qpBdOffsetY;
1446  }
1447  else
1448  {
1449    qp = pcCU->getRefQP(uiAbsPartIdx);
1450  }
1451
1452  pcCU->setQPSubParts(qp, uiAbsPartIdx, uiDepth);
1453  pcCU->setCodedQP(qp);
1454}
1455
1456/** parse chroma qp adjustment, converting to the internal table representation.
1457 * \returns Void
1458 */
1459Void TDecSbac::parseChromaQpAdjustment( TComDataCU* cu, UInt absPartIdx, UInt depth )
1460{
1461  UInt symbol;
1462#if RExt__DECODER_DEBUG_BIT_STATISTICS
1463  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__CHROMA_QP_ADJUSTMENT, g_aucConvertToBit[cu->getSlice()->getSPS()->getMaxCUWidth()>>depth]+2, CHANNEL_TYPE_CHROMA);
1464#endif
1465
1466  Int chromaQpOffsetListLen = cu->getSlice()->getPPS()->getPpsRangeExtension().getChromaQpOffsetListLen();
1467
1468  // cu_chroma_qp_offset_flag
1469  m_pcTDecBinIf->decodeBin( symbol, m_ChromaQpAdjFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1470
1471  if (symbol && chromaQpOffsetListLen > 1)
1472  {
1473    // cu_chroma_qp_offset_idx
1474    xReadUnaryMaxSymbol( symbol,  &m_ChromaQpAdjIdcSCModel.get( 0, 0, 0 ), 0, chromaQpOffsetListLen - 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1475    symbol++;
1476  }
1477  /* NB, symbol = 0 if outer flag is not set,
1478   *              1 if outer flag is set and there is no inner flag
1479   *              1+ otherwise */
1480  cu->setChromaQpAdjSubParts( symbol, absPartIdx, depth );
1481  cu->setCodedChromaQpAdj(symbol);
1482}
1483
1484Void TDecSbac::parseQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel )
1485{
1486  TComDataCU* pcCU = rTu.getCU();
1487
1488  const UInt absPartIdx       = rTu.GetAbsPartIdxTU(compID);
1489  const UInt TUDepth          = rTu.GetTransformDepthRel();
1490  const UInt uiCtx            = pcCU->getCtxQtCbf( rTu, toChannelType(compID) );
1491  const UInt contextSet       = toChannelType(compID);
1492
1493  const UInt width            = rTu.getRect(compID).width;
1494  const UInt height           = rTu.getRect(compID).height;
1495  const Bool canQuadSplit     = (width >= (MIN_TU_SIZE * 2)) && (height >= (MIN_TU_SIZE * 2));
1496  const UInt coveredPartIdxes = rTu.GetAbsPartIdxNumParts(compID);
1497
1498  //             Since the CBF for chroma is coded at the highest level possible, if sub-TUs are
1499  //             to be coded for a 4x8 chroma TU, their CBFs must be coded at the highest 4x8 level
1500  //             (i.e. where luma TUs are 8x8 rather than 4x4)
1501  //    ___ ___
1502  //   |   |   | <- 4 x (8x8 luma + 4x8 4:2:2 chroma)
1503  //   |___|___|    each quadrant has its own chroma CBF
1504  //   |   |   | _ _ _ _
1505  //   |___|___|        |
1506  //   <--16--->        V
1507  //                   _ _
1508  //                  |_|_| <- 4 x 4x4 luma + 1 x 4x8 4:2:2 chroma
1509  //                  |_|_|    no chroma CBF is coded - instead the parent CBF is inherited
1510  //                  <-8->    if sub-TUs are present, their CBFs had to be coded at the parent level
1511
1512  const UInt lowestTUDepth = TUDepth + ((!lowestLevel && !canQuadSplit) ? 1 : 0); //unsplittable TUs inherit their parent's CBF
1513        UInt lowestTUCBF   = 0;
1514
1515  if ((width != height) && (lowestLevel || !canQuadSplit)) //if sub-TUs are present
1516  {
1517    const UInt subTUDepth        = lowestTUDepth + 1;
1518    const UInt partIdxesPerSubTU = rTu.GetAbsPartIdxNumParts(compID) >> 1;
1519
1520    UInt combinedSubTUCBF = 0;
1521
1522    for (UInt subTU = 0; subTU < 2; subTU++)
1523    {
1524      UInt uiCbf = MAX_UINT;
1525      m_pcTDecBinIf->decodeBin(uiCbf, m_cCUQtCbfSCModel.get(0, contextSet, uiCtx) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__QT_CBF, g_aucConvertToBit[rTu.getRect(compID).width]+2, compID)));
1526
1527      const UInt subTUAbsPartIdx = absPartIdx + (subTU * partIdxesPerSubTU);
1528      pcCU->setCbfPartRange((uiCbf << subTUDepth), compID, subTUAbsPartIdx, partIdxesPerSubTU);
1529      combinedSubTUCBF |= uiCbf;
1530#if !H_MV_ENC_DEC_TRAC 
1531      DTRACE_CABAC_VL( g_nSymbolCounter++ )
1532      DTRACE_CABAC_T( "\tparseQtCbf()" )
1533      DTRACE_CABAC_T( "\tsub-TU=" )
1534      DTRACE_CABAC_V( subTU )
1535      DTRACE_CABAC_T( "\tsymbol=" )
1536      DTRACE_CABAC_V( uiCbf )
1537      DTRACE_CABAC_T( "\tctx=" )
1538      DTRACE_CABAC_V( uiCtx )
1539      DTRACE_CABAC_T( "\tetype=" )
1540      DTRACE_CABAC_V( compID )
1541      DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1542      DTRACE_CABAC_V( subTUAbsPartIdx )
1543      DTRACE_CABAC_T( "\n" )
1544#else
1545  if ( compID == COMPONENT_Cb )
1546  {
1547    DTRACE_TU("cbf_cb", uiCbf )
1548  }
1549  else if ( compID == COMPONENT_Cr )
1550  {
1551    DTRACE_TU("cbf_cr", uiCbf )
1552  }
1553  else
1554  {
1555    DTRACE_TU("cbf_luma", uiCbf )
1556  }
1557#endif
1558
1559    }
1560
1561    //propagate the sub-TU CBF up to the lowest TU level
1562    if (combinedSubTUCBF != 0)
1563    {
1564      pcCU->bitwiseOrCbfPartRange((combinedSubTUCBF << lowestTUDepth), compID, absPartIdx, coveredPartIdxes);
1565      lowestTUCBF = combinedSubTUCBF;
1566    }
1567  }
1568  else
1569  {
1570    UInt uiCbf = MAX_UINT;
1571    m_pcTDecBinIf->decodeBin(uiCbf, m_cCUQtCbfSCModel.get(0, contextSet, uiCtx) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__QT_CBF, g_aucConvertToBit[rTu.getRect(compID).width]+2, compID)));
1572
1573    pcCU->setCbfSubParts((uiCbf << lowestTUDepth), compID, absPartIdx, rTu.GetTransformDepthTotalAdj(compID));
1574#if !H_MV_ENC_DEC_TRAC
1575    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1576    DTRACE_CABAC_T( "\tparseQtCbf()" )
1577    DTRACE_CABAC_T( "\tsymbol=" )
1578    DTRACE_CABAC_V( uiCbf )
1579    DTRACE_CABAC_T( "\tctx=" )
1580    DTRACE_CABAC_V( uiCtx )
1581    DTRACE_CABAC_T( "\tetype=" )
1582    DTRACE_CABAC_V( compID )
1583    DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1584    DTRACE_CABAC_V( rTu.GetAbsPartIdxTU(compID) )
1585    DTRACE_CABAC_T( "\n" )
1586#else
1587    if ( compID == COMPONENT_Cb )
1588    {
1589      DTRACE_TU("cbf_cb", uiCbf )
1590    }
1591    else if ( compID == COMPONENT_Cr )
1592    {
1593      DTRACE_TU("cbf_cr", uiCbf )
1594    }
1595    else
1596    {
1597      DTRACE_TU("cbf_luma", uiCbf )
1598    }
1599#endif
1600    lowestTUCBF = uiCbf;
1601  }
1602
1603  //propagate the lowest level CBF up to the current level
1604  if (lowestTUCBF != 0)
1605  {
1606    for (UInt depth = TUDepth; depth < lowestTUDepth; depth++)
1607    {
1608      pcCU->bitwiseOrCbfPartRange((lowestTUCBF << depth), compID, absPartIdx, coveredPartIdxes);
1609    }
1610  }
1611}
1612
1613
1614Void TDecSbac::parseTransformSkipFlags (TComTU &rTu, ComponentID component)
1615{
1616  TComDataCU* pcCU=rTu.getCU();
1617  UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(component);
1618
1619  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1620  {
1621    return;
1622  }
1623
1624  if (!TUCompRectHasAssociatedTransformSkipFlag(rTu.getRect(component), pcCU->getSlice()->getPPS()->getPpsRangeExtension().getLog2MaxTransformSkipBlockSize()))
1625  {
1626    return;
1627  }
1628
1629  UInt useTransformSkip;
1630
1631  m_pcTDecBinIf->decodeBin( useTransformSkip , m_cTransformSkipSCModel.get( 0, toChannelType(component), 0 )
1632      RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__TRANSFORM_SKIP_FLAGS, component))
1633                          );
1634
1635#if !H_MV_ENC_DEC_TRAC
1636  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1637  DTRACE_CABAC_T("\tparseTransformSkip()");
1638  DTRACE_CABAC_T( "\tsymbol=" )
1639  DTRACE_CABAC_V( useTransformSkip )
1640  DTRACE_CABAC_T( "\tAddr=" )
1641  DTRACE_CABAC_V( pcCU->getCtuRsAddr() )
1642  DTRACE_CABAC_T( "\tetype=" )
1643  DTRACE_CABAC_V( component )
1644  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1645  DTRACE_CABAC_V( rTu.GetAbsPartIdxTU() )
1646  DTRACE_CABAC_T( "\n" )
1647#endif
1648
1649  pcCU->setTransformSkipPartRange( useTransformSkip, component, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts(component));
1650}
1651
1652
1653/** Parse (X,Y) position of the last significant coefficient
1654 * \param uiPosLastX reference to X component of last coefficient
1655 * \param uiPosLastY reference to Y component of last coefficient
1656 * \param width  Block width
1657 * \param height Block height
1658 * \param component chroma compinent ID
1659 * \param uiScanIdx scan type (zig-zag, hor, ver)
1660 *
1661 * This method decodes the X and Y component within a block of the last significant coefficient.
1662 */
1663Void TDecSbac::parseLastSignificantXY( UInt& uiPosLastX, UInt& uiPosLastY, Int width, Int height, ComponentID component, UInt uiScanIdx )
1664{
1665  UInt uiLast;
1666
1667  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, toChannelType(component) );
1668  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, toChannelType(component) );
1669
1670#if RExt__DECODER_DEBUG_BIT_STATISTICS
1671  TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__LAST_SIG_X_Y, g_aucConvertToBit[width]+2, component);
1672#endif
1673
1674
1675  if ( uiScanIdx == SCAN_VER )
1676  {
1677    swap( width, height );
1678  }
1679
1680  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1681  getLastSignificantContextParameters(component, width, height, blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY);
1682
1683  //------------------
1684
1685  // posX
1686
1687  for( uiPosLastX = 0; uiPosLastX < g_uiGroupIdx[ width - 1 ]; uiPosLastX++ )
1688  {
1689    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxX + blkSizeOffsetX + (uiPosLastX >>shiftX) ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1690
1691    if( !uiLast )
1692    {
1693      break;
1694    }
1695  }
1696
1697  // posY
1698
1699  for( uiPosLastY = 0; uiPosLastY < g_uiGroupIdx[ height - 1 ]; uiPosLastY++ )
1700  {
1701    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxY + blkSizeOffsetY + (uiPosLastY >>shiftY)) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1702
1703    if( !uiLast )
1704    {
1705      break;
1706    }
1707  }
1708
1709  // EP-coded part
1710
1711  if ( uiPosLastX > 3 )
1712  {
1713    UInt uiTemp  = 0;
1714    UInt uiCount = ( uiPosLastX - 2 ) >> 1;
1715    for ( Int i = uiCount - 1; i >= 0; i-- )
1716    {
1717      m_pcTDecBinIf->decodeBinEP( uiLast RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1718      uiTemp += uiLast << i;
1719    }
1720    uiPosLastX = g_uiMinInGroup[ uiPosLastX ] + uiTemp;
1721  }
1722  if ( uiPosLastY > 3 )
1723  {
1724    UInt uiTemp  = 0;
1725    UInt uiCount = ( uiPosLastY - 2 ) >> 1;
1726    for ( Int i = uiCount - 1; i >= 0; i-- )
1727    {
1728      m_pcTDecBinIf->decodeBinEP( uiLast RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1729      uiTemp += uiLast << i;
1730    }
1731    uiPosLastY = g_uiMinInGroup[ uiPosLastY ] + uiTemp;
1732  }
1733
1734  if( uiScanIdx == SCAN_VER )
1735  {
1736    swap( uiPosLastX, uiPosLastY );
1737  }
1738}
1739
1740Void TDecSbac::parseCoeffNxN(  TComTU &rTu, ComponentID compID )
1741{
1742  TComDataCU* pcCU=rTu.getCU();
1743  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(compID);
1744  const TComRectangle &rRect=rTu.getRect(compID);
1745  const UInt uiWidth=rRect.width;
1746  const UInt uiHeight=rRect.height;
1747  TCoeff* pcCoef=(pcCU->getCoeff(compID)+rTu.getCoefficientOffset(compID));
1748  const TComSPS &sps=*(pcCU->getSlice()->getSPS());
1749#if !H_MV_ENC_DEC_TRAC
1750  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1751  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1752  DTRACE_CABAC_V( compID )
1753  DTRACE_CABAC_T( "\twidth=" )
1754  DTRACE_CABAC_V( uiWidth )
1755  DTRACE_CABAC_T( "\theight=" )
1756  DTRACE_CABAC_V( uiHeight )
1757  DTRACE_CABAC_T( "\tdepth=" )
1758//  DTRACE_CABAC_V( rTu.GetTransformDepthTotalAdj(compID) )
1759  DTRACE_CABAC_V( rTu.GetTransformDepthTotal() )
1760  DTRACE_CABAC_T( "\tabspartidx=" )
1761//  DTRACE_CABAC_V( uiAbsPartIdx )
1762  DTRACE_CABAC_V( rTu.GetAbsPartIdxTU(compID) )
1763  DTRACE_CABAC_T( "\ttoCU-X=" )
1764  DTRACE_CABAC_V( pcCU->getCUPelX() )
1765  DTRACE_CABAC_T( "\ttoCU-Y=" )
1766  DTRACE_CABAC_V( pcCU->getCUPelY() )
1767  DTRACE_CABAC_T( "\tCU-addr=" )
1768  DTRACE_CABAC_V(  pcCU->getCtuRsAddr() )
1769  DTRACE_CABAC_T( "\tinCU-X=" )
1770//  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1771  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] )
1772  DTRACE_CABAC_T( "\tinCU-Y=" )
1773// DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1774  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] )
1775  DTRACE_CABAC_T( "\tpredmode=" )
1776  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1777  DTRACE_CABAC_T( "\n" )
1778#endif
1779
1780  //--------------------------------------------------------------------------------------------------
1781
1782  if( uiWidth > sps.getMaxTrSize() )
1783  {
1784    std::cerr << "ERROR: parseCoeffNxN was passed a TU with dimensions larger than the maximum allowed size" << std::endl;
1785    assert(false);
1786    exit(1);
1787  }
1788
1789  //--------------------------------------------------------------------------------------------------
1790
1791  //set parameters
1792
1793  const ChannelType  chType            = toChannelType(compID);
1794  const UInt         uiLog2BlockWidth  = g_aucConvertToBit[ uiWidth  ] + 2;
1795  const UInt         uiLog2BlockHeight = g_aucConvertToBit[ uiHeight ] + 2;
1796  const UInt         uiMaxNumCoeff     = uiWidth * uiHeight;
1797  const UInt         uiMaxNumCoeffM1   = uiMaxNumCoeff - 1;
1798
1799  const ChannelType  channelType       = toChannelType(compID);
1800  const Bool         extendedPrecision = sps.getSpsRangeExtension().getExtendedPrecisionProcessingFlag();
1801
1802  const Bool         alignCABACBeforeBypass = sps.getSpsRangeExtension().getCabacBypassAlignmentEnabledFlag();
1803  const Int          maxLog2TrDynamicRange  = sps.getMaxLog2TrDynamicRange(channelType);
1804
1805#if RExt__DECODER_DEBUG_BIT_STATISTICS
1806  TComCodingStatisticsClassType ctype_group(STATS__CABAC_BITS__SIG_COEFF_GROUP_FLAG, uiLog2BlockWidth, compID);
1807  TComCodingStatisticsClassType ctype_map(STATS__CABAC_BITS__SIG_COEFF_MAP_FLAG, uiLog2BlockWidth, compID);
1808  TComCodingStatisticsClassType ctype_gt1(STATS__CABAC_BITS__GT1_FLAG, uiLog2BlockWidth, compID);
1809  TComCodingStatisticsClassType ctype_gt2(STATS__CABAC_BITS__GT2_FLAG, uiLog2BlockWidth, compID);
1810#endif
1811
1812  Bool beValid;
1813  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1814  {
1815    beValid = false;
1816    if((!pcCU->isIntra(uiAbsPartIdx)) && pcCU->isRDPCMEnabled(uiAbsPartIdx))
1817    {
1818      parseExplicitRdpcmMode(rTu, compID);
1819    }
1820  }
1821  else
1822  {
1823    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag();
1824  }
1825
1826  UInt absSum = 0;
1827
1828  //--------------------------------------------------------------------------------------------------
1829
1830  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1831  {
1832    parseTransformSkipFlags(rTu, compID);
1833    //  This TU has coefficients and is transform skipped. Check whether is inter coded and if yes decode the explicit RDPCM mode
1834    if(pcCU->getTransformSkip(uiAbsPartIdx, compID) && (!pcCU->isIntra(uiAbsPartIdx)) && pcCU->isRDPCMEnabled(uiAbsPartIdx) )
1835    {
1836      parseExplicitRdpcmMode(rTu, compID);
1837      if(pcCU->getExplicitRdpcmMode(compID, uiAbsPartIdx) != RDPCM_OFF)
1838      {
1839        //  Sign data hiding is avoided for horizontal and vertical RDPCM modes
1840        beValid = false;
1841      }
1842    }
1843  }
1844
1845  Int uiIntraMode = -1;
1846  const Bool       bIsLuma = isLuma(compID);
1847  Int isIntra = pcCU->isIntra(uiAbsPartIdx) ? 1 : 0;
1848  if ( isIntra && pcCU->isRDPCMEnabled(uiAbsPartIdx) )
1849  {
1850    const UInt partsPerMinCU = 1<<(2*(sps.getMaxTotalCUDepth() - sps.getLog2DiffMaxMinCodingBlockSize()));
1851    uiIntraMode = pcCU->getIntraDir( toChannelType(compID), uiAbsPartIdx );
1852    uiIntraMode = (uiIntraMode==DM_CHROMA_IDX && !bIsLuma) ? pcCU->getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, rTu.GetChromaFormat(), partsPerMinCU)) : uiIntraMode;
1853    uiIntraMode = ((rTu.GetChromaFormat() == CHROMA_422) && !bIsLuma) ? g_chroma422IntraAngleMappingTable[uiIntraMode] : uiIntraMode;
1854
1855    Bool transformSkip = pcCU->getTransformSkip( uiAbsPartIdx,compID);
1856    Bool rdpcm_lossy = ( transformSkip /*&& isIntra*/ && ( (uiIntraMode == HOR_IDX) || (uiIntraMode == VER_IDX) ) );
1857    if ( rdpcm_lossy )
1858    {
1859      beValid = false;
1860    }
1861  }
1862
1863  //--------------------------------------------------------------------------------------------------
1864
1865  const Bool  bUseGolombRiceParameterAdaptation = sps.getSpsRangeExtension().getPersistentRiceAdaptationEnabledFlag();
1866        UInt &currentGolombRiceStatistic        = m_golombRiceAdaptationStatistics[rTu.getGolombRiceStatisticsIndex(compID)];
1867
1868  //select scans
1869  TUEntropyCodingParameters codingParameters;
1870  getTUEntropyCodingParameters(codingParameters, rTu, compID);
1871
1872  //===== decode last significant =====
1873  UInt uiPosLastX, uiPosLastY;
1874  parseLastSignificantXY( uiPosLastX, uiPosLastY, uiWidth, uiHeight, compID, codingParameters.scanType );
1875  UInt uiBlkPosLast      = uiPosLastX + (uiPosLastY<<uiLog2BlockWidth);
1876  pcCoef[ uiBlkPosLast ] = 1;
1877
1878  //===== decode significance flags =====
1879  UInt uiScanPosLast;
1880  for( uiScanPosLast = 0; uiScanPosLast < uiMaxNumCoeffM1; uiScanPosLast++ )
1881  {
1882    UInt uiBlkPos = codingParameters.scan[ uiScanPosLast ];
1883    if( uiBlkPosLast == uiBlkPos )
1884    {
1885      break;
1886    }
1887  }
1888
1889  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, isChroma(chType) );
1890  ContextModel * const baseCtx = m_cCUSigSCModel.get( 0, 0 ) + getSignificanceMapContextOffset(compID);
1891
1892  const Int  iLastScanSet  = uiScanPosLast >> MLS_CG_SIZE;
1893  UInt c1                  = 1;
1894  UInt uiGoRiceParam       = 0;
1895
1896
1897  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1898  memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1899
1900  Int  iScanPosSig             = (Int) uiScanPosLast;
1901  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1902  {
1903    Int  iSubPos   = iSubSet << MLS_CG_SIZE;
1904    uiGoRiceParam  = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR;
1905    Bool updateGolombRiceStatistics = bUseGolombRiceParameterAdaptation; //leave the statistics at 0 when not using the adaptation system
1906    Int numNonZero = 0;
1907
1908    Int lastNZPosInCG  = -1;
1909    Int firstNZPosInCG = 1 << MLS_CG_SIZE;
1910
1911    Bool escapeDataPresentInGroup = false;
1912
1913    Int pos[1 << MLS_CG_SIZE];
1914
1915    if( iScanPosSig == (Int) uiScanPosLast )
1916    {
1917      lastNZPosInCG  = iScanPosSig;
1918      firstNZPosInCG = iScanPosSig;
1919      iScanPosSig--;
1920      pos[ numNonZero ] = uiBlkPosLast;
1921      numNonZero = 1;
1922    }
1923
1924    // decode significant_coeffgroup_flag
1925    Int iCGBlkPos = codingParameters.scanCG[ iSubSet ];
1926    Int iCGPosY   = iCGBlkPos / codingParameters.widthInGroups;
1927    Int iCGPosX   = iCGBlkPos - (iCGPosY * codingParameters.widthInGroups);
1928
1929    if( iSubSet == iLastScanSet || iSubSet == 0)
1930    {
1931      uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1932    }
1933    else
1934    {
1935      UInt uiSigCoeffGroup;
1936      UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups );
1937      m_pcTDecBinIf->decodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_group) );
1938      uiSigCoeffGroupFlag[ iCGBlkPos ] = uiSigCoeffGroup;
1939    }
1940
1941    // decode significant_coeff_flag
1942    const Int patternSigCtx = TComTrQuant::calcPatternSigCtx(uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
1943
1944    UInt uiBlkPos, uiSig, uiCtxSig;
1945    for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1946    {
1947      uiBlkPos  = codingParameters.scan[ iScanPosSig ];
1948      uiSig     = 0;
1949
1950      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1951      {
1952        if( iScanPosSig > iSubPos || iSubSet == 0  || numNonZero )
1953        {
1954          uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, codingParameters, iScanPosSig, uiLog2BlockWidth, uiLog2BlockHeight, chType );
1955          m_pcTDecBinIf->decodeBin( uiSig, baseCtx[ uiCtxSig ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_map) );
1956        }
1957        else
1958        {
1959          uiSig = 1;
1960        }
1961      }
1962      pcCoef[ uiBlkPos ] = uiSig;
1963      if( uiSig )
1964      {
1965        pos[ numNonZero ] = uiBlkPos;
1966        numNonZero ++;
1967        if( lastNZPosInCG == -1 )
1968        {
1969          lastNZPosInCG = iScanPosSig;
1970        }
1971        firstNZPosInCG = iScanPosSig;
1972      }
1973    }
1974
1975    if( numNonZero > 0 )
1976    {
1977      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1978
1979      absSum = 0;
1980
1981      const UInt uiCtxSet = getContextSetIndex(compID, iSubSet, (c1 == 0));
1982      c1 = 1;
1983      UInt uiBin;
1984
1985      ContextModel *baseCtxMod = m_cCUOneSCModel.get( 0, 0 ) + (NUM_ONE_FLAG_CTX_PER_SET * uiCtxSet);
1986
1987      Int absCoeff[1 << MLS_CG_SIZE];
1988
1989      for ( Int i = 0; i < numNonZero; i++)
1990      {
1991        absCoeff[i] = 1;
1992      }
1993      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1994      Int firstC2FlagIdx = -1;
1995
1996      for( Int idx = 0; idx < numC1Flag; idx++ )
1997      {
1998        m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[c1] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_gt1) );
1999        if( uiBin == 1 )
2000        {
2001          c1 = 0;
2002          if (firstC2FlagIdx == -1)
2003          {
2004            firstC2FlagIdx = idx;
2005          }
2006          else //if a greater-than-one has been encountered already this group
2007          {
2008            escapeDataPresentInGroup = true;
2009          }
2010        }
2011        else if( (c1 < 3) && (c1 > 0) )
2012        {
2013          c1++;
2014        }
2015        absCoeff[ idx ] = uiBin + 1;
2016      }
2017
2018      if (c1 == 0)
2019      {
2020        baseCtxMod = m_cCUAbsSCModel.get( 0, 0 ) + (NUM_ABS_FLAG_CTX_PER_SET * uiCtxSet);
2021        if ( firstC2FlagIdx != -1)
2022        {
2023          m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_gt2) );
2024          absCoeff[ firstC2FlagIdx ] = uiBin + 2;
2025          if (uiBin != 0)
2026          {
2027            escapeDataPresentInGroup = true;
2028          }
2029        }
2030      }
2031
2032      escapeDataPresentInGroup = escapeDataPresentInGroup || (numNonZero > C1FLAG_NUMBER);
2033
2034      const Bool alignGroup = escapeDataPresentInGroup && alignCABACBeforeBypass;
2035
2036#if RExt__DECODER_DEBUG_BIT_STATISTICS
2037      TComCodingStatisticsClassType ctype_signs((alignGroup ? STATS__CABAC_BITS__ALIGNED_SIGN_BIT    : STATS__CABAC_BITS__SIGN_BIT   ), uiLog2BlockWidth, compID);
2038      TComCodingStatisticsClassType ctype_escs ((alignGroup ? STATS__CABAC_BITS__ALIGNED_ESCAPE_BITS : STATS__CABAC_BITS__ESCAPE_BITS), uiLog2BlockWidth, compID);
2039#endif
2040
2041      if (alignGroup)
2042      {
2043        m_pcTDecBinIf->align();
2044      }
2045
2046      UInt coeffSigns;
2047      if ( signHidden && beValid )
2048      {
2049        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero-1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_signs) );
2050        coeffSigns <<= 32 - (numNonZero-1);
2051      }
2052      else
2053      {
2054        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_signs) );
2055        coeffSigns <<= 32 - numNonZero;
2056      }
2057
2058      Int iFirstCoeff2 = 1;
2059      if (escapeDataPresentInGroup)
2060      {
2061        for( Int idx = 0; idx < numNonZero; idx++ )
2062        {
2063          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2) : 1;
2064
2065          if( absCoeff[ idx ] == baseLevel)
2066          {
2067            UInt uiLevel;
2068            xReadCoefRemainExGolomb( uiLevel, uiGoRiceParam, extendedPrecision, maxLog2TrDynamicRange RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_escs) );
2069
2070            absCoeff[ idx ] = uiLevel + baseLevel;
2071
2072            if (absCoeff[idx] > (3 << uiGoRiceParam))
2073            {
2074              uiGoRiceParam = bUseGolombRiceParameterAdaptation ? (uiGoRiceParam + 1) : (std::min<UInt>((uiGoRiceParam + 1), 4));
2075            }
2076
2077            if (updateGolombRiceStatistics)
2078            {
2079              const UInt initialGolombRiceParameter = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR;
2080
2081              if (uiLevel >= (3 << initialGolombRiceParameter))
2082              {
2083                currentGolombRiceStatistic++;
2084              }
2085              else if (((uiLevel * 2) < (1 << initialGolombRiceParameter)) && (currentGolombRiceStatistic > 0))
2086              {
2087                currentGolombRiceStatistic--;
2088              }
2089
2090              updateGolombRiceStatistics = false;
2091            }
2092          }
2093
2094          if(absCoeff[ idx ] >= 2)
2095          {
2096            iFirstCoeff2 = 0;
2097          }
2098        }
2099      }
2100
2101      for( Int idx = 0; idx < numNonZero; idx++ )
2102      {
2103        Int blkPos = pos[ idx ];
2104        // Signs applied later.
2105        pcCoef[ blkPos ] = absCoeff[ idx ];
2106        absSum += absCoeff[ idx ];
2107
2108        if ( idx == numNonZero-1 && signHidden && beValid )
2109        {
2110          // Infer sign of 1st element.
2111          if (absSum&0x1)
2112          {
2113            pcCoef[ blkPos ] = -pcCoef[ blkPos ];
2114          }
2115        }
2116        else
2117        {
2118          Int sign = static_cast<Int>( coeffSigns ) >> 31;
2119          pcCoef[ blkPos ] = ( pcCoef[ blkPos ] ^ sign ) - sign;
2120          coeffSigns <<= 1;
2121        }
2122      }
2123    }
2124  }
2125
2126#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
2127  printSBACCoeffData(uiPosLastX, uiPosLastY, uiWidth, uiHeight, compID, uiAbsPartIdx, codingParameters.scanType, pcCoef);
2128#endif
2129
2130  return;
2131}
2132
2133Void TDecSbac::parseSaoMaxUvlc ( UInt& val, UInt maxSymbol )
2134{
2135  if (maxSymbol == 0)
2136  {
2137    val = 0;
2138    return;
2139  }
2140
2141  UInt code;
2142  Int  i;
2143  m_pcTDecBinIf->decodeBinEP( code RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2144  if ( code == 0 )
2145  {
2146    val = 0;
2147    return;
2148  }
2149
2150  i=1;
2151  while (1)
2152  {
2153    m_pcTDecBinIf->decodeBinEP( code RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2154    if ( code == 0 )
2155    {
2156      break;
2157    }
2158    i++;
2159    if (i == maxSymbol)
2160    {
2161      break;
2162    }
2163  }
2164
2165  val = i;
2166}
2167
2168Void TDecSbac::parseSaoUflc (UInt uiLength, UInt&  riVal)
2169{
2170  m_pcTDecBinIf->decodeBinsEP ( riVal, uiLength RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2171}
2172
2173Void TDecSbac::parseSaoMerge (UInt&  ruiVal)
2174{
2175  UInt uiCode;
2176  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoMergeSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2177  ruiVal = (Int)uiCode;
2178}
2179
2180Void TDecSbac::parseSaoTypeIdx (UInt&  ruiVal)
2181{
2182  UInt uiCode;
2183  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2184  if (uiCode == 0)
2185  {
2186    ruiVal = 0;
2187  }
2188  else
2189  {
2190    m_pcTDecBinIf->decodeBinEP( uiCode RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2191    if (uiCode == 0)
2192    {
2193      ruiVal = 1;
2194    }
2195    else
2196    {
2197      ruiVal = 2;
2198    }
2199  }
2200}
2201
2202Void TDecSbac::parseSaoSign(UInt& val)
2203{
2204  m_pcTDecBinIf->decodeBinEP ( val RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
2205}
2206
2207Void TDecSbac::parseSAOBlkParam (SAOBlkParam& saoBlkParam
2208                                , Bool* sliceEnabled
2209                                , Bool leftMergeAvail
2210                                , Bool aboveMergeAvail
2211                                , const BitDepths &bitDepths
2212                                )
2213{
2214  UInt uiSymbol;
2215
2216  Bool isLeftMerge = false;
2217  Bool isAboveMerge= false;
2218
2219  if(leftMergeAvail)
2220  {
2221    parseSaoMerge(uiSymbol); //sao_merge_left_flag
2222    isLeftMerge = (uiSymbol?true:false);
2223  }
2224
2225  if( aboveMergeAvail && !isLeftMerge)
2226  {
2227    parseSaoMerge(uiSymbol); //sao_merge_up_flag
2228    isAboveMerge = (uiSymbol?true:false);
2229  }
2230
2231  if(isLeftMerge || isAboveMerge) //merge mode
2232  {
2233    for (UInt componentIndex = 0; componentIndex < MAX_NUM_COMPONENT; componentIndex++)
2234    {
2235      saoBlkParam[componentIndex].modeIdc = (sliceEnabled[componentIndex]) ? SAO_MODE_MERGE : SAO_MODE_OFF;
2236      saoBlkParam[componentIndex].typeIdc = (isLeftMerge)?SAO_MERGE_LEFT:SAO_MERGE_ABOVE;
2237    }
2238  }
2239  else //new or off mode
2240  {
2241    for(Int compId=COMPONENT_Y; compId < MAX_NUM_COMPONENT; compId++)
2242    {
2243      const ComponentID compIdx=ComponentID(compId);
2244      const ComponentID firstCompOfChType = getFirstComponentOfChannel(toChannelType(compIdx));
2245      SAOOffset& ctbParam = saoBlkParam[compIdx];
2246#if O0043_BEST_EFFORT_DECODING
2247      const Int bitDepthOrig = bitDepths.stream[toChannelType(compIdx)];
2248      const Int forceBitDepthAdjust = bitDepthOrig - bitDepths.recon[toChannelType(compIdx)];
2249#else
2250      const Int bitDepthOrig = bitDepths.recon[toChannelType(compIdx)];
2251#endif
2252      const Int maxOffsetQVal=TComSampleAdaptiveOffset::getMaxOffsetQVal(bitDepthOrig);
2253      if(!sliceEnabled[compIdx])
2254      {
2255        //off
2256        ctbParam.modeIdc = SAO_MODE_OFF;
2257        continue;
2258      }
2259
2260      //type
2261      if(compIdx == firstCompOfChType)
2262      {
2263        parseSaoTypeIdx(uiSymbol); //sao_type_idx_luma or sao_type_idx_chroma
2264
2265        assert(uiSymbol ==0 || uiSymbol ==1 || uiSymbol ==2);
2266
2267        if(uiSymbol ==0) //OFF
2268        {
2269          ctbParam.modeIdc = SAO_MODE_OFF;
2270        }
2271        else if(uiSymbol == 1) //BO
2272        {
2273          ctbParam.modeIdc = SAO_MODE_NEW;
2274          ctbParam.typeIdc = SAO_TYPE_START_BO;
2275        }
2276        else //2, EO
2277        {
2278          ctbParam.modeIdc = SAO_MODE_NEW;
2279          ctbParam.typeIdc = SAO_TYPE_START_EO;
2280        }
2281
2282      }
2283      else //Cr, follow Cb SAO type
2284      {
2285        ctbParam.modeIdc = saoBlkParam[COMPONENT_Cb].modeIdc;
2286        ctbParam.typeIdc = saoBlkParam[COMPONENT_Cb].typeIdc;
2287      }
2288
2289      if(ctbParam.modeIdc == SAO_MODE_NEW)
2290      {
2291        Int offset[4];
2292        for(Int i=0; i< 4; i++)
2293        {
2294          parseSaoMaxUvlc(uiSymbol, maxOffsetQVal ); //sao_offset_abs
2295          offset[i] = (Int)uiSymbol;
2296        }
2297
2298        if(ctbParam.typeIdc == SAO_TYPE_START_BO)
2299        {
2300          for(Int i=0; i< 4; i++)
2301          {
2302            if(offset[i] != 0)
2303            {
2304              parseSaoSign(uiSymbol); //sao_offset_sign
2305              if(uiSymbol)
2306              {
2307#if O0043_BEST_EFFORT_DECODING
2308                offset[i] >>= forceBitDepthAdjust;
2309#endif
2310                offset[i] = -offset[i];
2311              }
2312            }
2313          }
2314          parseSaoUflc(NUM_SAO_BO_CLASSES_LOG2, uiSymbol ); //sao_band_position
2315          ctbParam.typeAuxInfo = uiSymbol;
2316
2317          for(Int i=0; i<4; i++)
2318          {
2319            ctbParam.offset[(ctbParam.typeAuxInfo+i)%MAX_NUM_SAO_CLASSES] = offset[i];
2320          }
2321
2322        }
2323        else //EO
2324        {
2325          ctbParam.typeAuxInfo = 0;
2326
2327          if(firstCompOfChType == compIdx)
2328          {
2329            parseSaoUflc(NUM_SAO_EO_TYPES_LOG2, uiSymbol ); //sao_eo_class_luma or sao_eo_class_chroma
2330            ctbParam.typeIdc += uiSymbol;
2331          }
2332          else
2333          {
2334            ctbParam.typeIdc = saoBlkParam[firstCompOfChType].typeIdc;
2335          }
2336          ctbParam.offset[SAO_CLASS_EO_FULL_VALLEY] = offset[0];
2337          ctbParam.offset[SAO_CLASS_EO_HALF_VALLEY] = offset[1];
2338          ctbParam.offset[SAO_CLASS_EO_PLAIN      ] = 0;
2339          ctbParam.offset[SAO_CLASS_EO_HALF_PEAK  ] = -offset[2];
2340          ctbParam.offset[SAO_CLASS_EO_FULL_PEAK  ] = -offset[3];
2341        }
2342      }
2343    }
2344  }
2345}
2346
2347/**
2348 - Initialize our contexts from the nominated source.
2349 .
2350 \param pSrc Contexts to be copied.
2351 */
2352Void TDecSbac::xCopyContextsFrom( const TDecSbac* pSrc )
2353{
2354  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
2355  memcpy(m_golombRiceAdaptationStatistics, pSrc->m_golombRiceAdaptationStatistics, (sizeof(UInt) * RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS));
2356}
2357
2358Void TDecSbac::xCopyFrom( const TDecSbac* pSrc )
2359{
2360  m_pcTDecBinIf->copyState( pSrc->m_pcTDecBinIf );
2361  xCopyContextsFrom( pSrc );
2362}
2363
2364Void TDecSbac::load ( const TDecSbac* pSrc )
2365{
2366  xCopyFrom(pSrc);
2367}
2368
2369Void TDecSbac::loadContexts ( const TDecSbac* pSrc )
2370{
2371  xCopyContextsFrom(pSrc);
2372}
2373
2374/** Performs CABAC decoding of the explicit RDPCM mode
2375 * \param rTu current TU data structure
2376 * \param compID component identifier
2377 */
2378Void TDecSbac::parseExplicitRdpcmMode( TComTU &rTu, ComponentID compID )
2379{
2380  TComDataCU* cu = rTu.getCU();
2381  const UInt absPartIdx=rTu.GetAbsPartIdxTU(compID);
2382  const TComRectangle &rect = rTu.getRect(compID);
2383  const UInt tuHeight = g_aucConvertToBit[rect.height];
2384  const UInt tuWidth  = g_aucConvertToBit[rect.width];
2385  UInt code = 0;
2386
2387  assert(tuHeight == tuWidth);
2388
2389#if RExt__DECODER_DEBUG_BIT_STATISTICS
2390  const TComCodingStatisticsClassType ctype(STATS__EXPLICIT_RDPCM_BITS, g_aucConvertToBit[cu->getSlice()->getSPS()->getMaxCUWidth()>>rTu.GetTransformDepthTotal()]+2);
2391#endif
2392
2393  m_pcTDecBinIf->decodeBin(code, m_explicitRdpcmFlagSCModel.get (0, toChannelType(compID), 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
2394
2395  if(code == 0)
2396  {
2397    cu->setExplicitRdpcmModePartRange( RDPCM_OFF, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
2398  }
2399  else
2400  {
2401    m_pcTDecBinIf->decodeBin(code, m_explicitRdpcmDirSCModel.get (0, toChannelType(compID), 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
2402    if(code == 0)
2403    {
2404      cu->setExplicitRdpcmModePartRange( RDPCM_HOR, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
2405    }
2406    else
2407    {
2408      cu->setExplicitRdpcmModePartRange( RDPCM_VER, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
2409    }
2410  }
2411}
2412
2413#if NH_3D_ARP
2414Void TDecSbac::parseARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2415{
2416  UInt uiW = 0;
2417  UInt uiOffset = pcCU->getCTXARPWFlag(uiAbsPartIdx);
2418  UInt uiCode = 0;
2419
2420  m_pcTDecBinIf->decodeBin( uiCode , m_cCUPUARPWSCModel.get( 0, 0, 0 + uiOffset ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__ARP_FLAG));
2421
2422  uiW = uiCode;
2423  if( 1 == uiW )   
2424  {
2425    m_pcTDecBinIf->decodeBin( uiCode , m_cCUPUARPWSCModel.get( 0, 0, 2 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__ARP_FLAG));
2426    uiW += ( 1 == uiCode ? 1 : 0 );
2427  }
2428#if H_MV_ENC_DEC_TRAC
2429  DTRACE_CU("iv_res_pred_weight_idx", uiW )
2430#endif
2431  pcCU->setARPWSubParts( ( UChar )( uiW ) , uiAbsPartIdx, uiDepth ); 
2432}
2433#endif
2434
2435#if NH_3D_IC
2436/** parse illumination compensation flag
2437 * \param pcCU
2438 * \param uiAbsPartIdx
2439 * \param uiDepth
2440 * \returns Void
2441 */
2442Void TDecSbac::parseICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2443{ 
2444  UInt uiSymbol = 0;
2445  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUICFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__3D_IC) );
2446
2447#if !H_MV_ENC_DEC_TRAC
2448  DTRACE_CABAC_VL( g_nSymbolCounter++ );
2449  DTRACE_CABAC_T( "\tICFlag" );
2450  DTRACE_CABAC_T( "\tuiCtxIC: ");
2451  DTRACE_CABAC_V( uiCtxIC );
2452  DTRACE_CABAC_T( "\tuiSymbol: ");
2453  DTRACE_CABAC_V( uiSymbol );
2454  DTRACE_CABAC_T( "\n");
2455#else
2456  DTRACE_CU("ic_flag", uiSymbol)
2457#endif
2458 
2459  pcCU->setICFlagSubParts( uiSymbol ? true : false , uiAbsPartIdx, 0, uiDepth );
2460}
2461#endif
2462
2463#if H_3D_INTER_SDC
2464Void TDecSbac::parseDeltaDC( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
2465{
2466  if( ! ( pcCU->getSDCFlag( absPartIdx ) || ( pcCU->isIntra( absPartIdx ) && getDimType( pcCU->getLumaIntraDir( absPartIdx ) ) < DIM_NUM_TYPE ) ) )
2467  {
2468    assert( 0 );
2469  }
2470
2471  UInt symbol = 1;
2472  UInt uiNumSegments = isDimMode( pcCU->getLumaIntraDir( absPartIdx ) ) ? 2 : 1;
2473
2474  if( pcCU->isIntra( absPartIdx ) && pcCU->getSDCFlag( absPartIdx ))
2475  {
2476
2477    m_pcTDecBinIf->decodeBin( symbol, m_cDdcFlagSCModel.get( 0, 0, 0 ) );
2478      assert( pcCU->getPartitionSize( absPartIdx ) == SIZE_2Nx2N );
2479      pcCU->setTrIdxSubParts( 0, absPartIdx, depth );
2480      pcCU->setCbfSubParts( 1, 1, 1, absPartIdx, depth );
2481    }
2482
2483  for( UInt segment = 0; segment < uiNumSegments; segment++ )
2484  {
2485    Pel valDeltaDC = 0;
2486    if( symbol )
2487    {
2488      xParseDimDeltaDC( valDeltaDC, uiNumSegments );
2489    }
2490
2491    if( pcCU->isIntra( absPartIdx ) )
2492    {
2493      UInt dir     = pcCU->getLumaIntraDir( absPartIdx );
2494
2495      if( pcCU->getSDCFlag( absPartIdx ) )
2496      {
2497        pcCU->setSDCSegmentDCOffset( valDeltaDC, segment, absPartIdx );
2498      }
2499      else
2500      {
2501        pcCU->setDimDeltaDC( getDimType( dir ), segment, absPartIdx, valDeltaDC );
2502      }
2503    }
2504    else
2505    {
2506      pcCU->setSDCSegmentDCOffset( valDeltaDC, segment, absPartIdx );
2507    }
2508  }
2509}
2510
2511Void TDecSbac::parseSDCFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2512{
2513  UInt uiSymbol = 0;
2514  UInt uiCtxSDCFlag = pcCU->getCtxSDCFlag( uiAbsPartIdx );
2515
2516  m_pcTDecBinIf->decodeBin( uiSymbol, m_cSDCFlagSCModel.get( 0, 0, uiCtxSDCFlag ) );
2517  DTRACE_CU("dc_only_flag", uiSymbol)
2518  if( uiSymbol )
2519  {
2520    pcCU->setSDCFlagSubParts( true, uiAbsPartIdx, uiDepth );
2521    pcCU->setTrIdxSubParts( 0, uiAbsPartIdx, uiDepth );
2522    pcCU->setCbfSubParts( 1, 1, 1, uiAbsPartIdx, uiDepth );
2523  }
2524  else
2525  {
2526    pcCU->setSDCFlagSubParts( false, uiAbsPartIdx, uiDepth );
2527  }
2528}
2529
2530#endif
2531
2532#if NH_3D_DBBP
2533Void TDecSbac::parseDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
2534{
2535  AOF( pcCU->getSlice()->getDepthBasedBlkPartFlag() );
2536  AOF( !pcCU->getSlice()->getIsDepth() );
2537 
2538  UInt uiSymbol = 0;
2539 
2540  m_pcTDecBinIf->decodeBin( uiSymbol, m_cDBBPFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DBBP_FLAG) );
2541  DTRACE_CU("dbbp_flag", uiSymbol)
2542  PartSize ePartSize = pcCU->getPartitionSize( uiAbsPartIdx );
2543  AOF( ePartSize == SIZE_2NxN || ePartSize == SIZE_Nx2N );
2544  UInt uiPUOffset = ( g_auiPUOffset[UInt( ePartSize )] << ( ( pcCU->getSlice()->getSPS()->getMaxTotalCUDepth() - uiDepth ) << 1 ) ) >> 4;
2545  pcCU->setDBBPFlagSubParts(uiSymbol, uiAbsPartIdx, 0, uiDepth);
2546  pcCU->setDBBPFlagSubParts(uiSymbol, uiAbsPartIdx+uiPUOffset, 1, uiDepth);
2547}
2548#endif
2549
2550
2551//! \}
Note: See TracBrowser for help on using the repository browser.