source: 3DVCSoftware/branches/HTM-16.0-MV-draft-5/source/Lib/TLibDecoder/TDecSbac.cpp

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

Removed 3D.

  • Property svn:eol-style set to native
File size: 70.6 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-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, m_cCUMergeFlagExtSCModel                   ( 1,             1,                      NUM_MERGE_FLAG_EXT_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
70, m_cCUMergeIdxExtSCModel                    ( 1,             1,                      NUM_MERGE_IDX_EXT_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
71, m_cCUPartSizeSCModel                       ( 1,             1,                      NUM_PART_SIZE_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
72, m_cCUPredModeSCModel                       ( 1,             1,                      NUM_PRED_MODE_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
73, m_cCUIntraPredSCModel                      ( 1,             1,                      NUM_INTRA_PREDICT_CTX                , m_contextModels + m_numContextModels, m_numContextModels)
74, m_cCUChromaPredSCModel                     ( 1,             1,                      NUM_CHROMA_PRED_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
75, m_cCUDeltaQpSCModel                        ( 1,             1,                      NUM_DELTA_QP_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
76, m_cCUInterDirSCModel                       ( 1,             1,                      NUM_INTER_DIR_CTX                    , m_contextModels + m_numContextModels, m_numContextModels)
77, m_cCURefPicSCModel                         ( 1,             1,                      NUM_REF_NO_CTX                       , m_contextModels + m_numContextModels, m_numContextModels)
78, m_cCUMvdSCModel                            ( 1,             1,                      NUM_MV_RES_CTX                       , m_contextModels + m_numContextModels, m_numContextModels)
79, m_cCUQtCbfSCModel                          ( 1,             NUM_QT_CBF_CTX_SETS,    NUM_QT_CBF_CTX_PER_SET               , m_contextModels + m_numContextModels, m_numContextModels)
80, m_cCUTransSubdivFlagSCModel                ( 1,             1,                      NUM_TRANS_SUBDIV_FLAG_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
81, m_cCUQtRootCbfSCModel                      ( 1,             1,                      NUM_QT_ROOT_CBF_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
82, m_cCUSigCoeffGroupSCModel                  ( 1,             2,                      NUM_SIG_CG_FLAG_CTX                  , m_contextModels + m_numContextModels, m_numContextModels)
83, m_cCUSigSCModel                            ( 1,             1,                      NUM_SIG_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
84, m_cCuCtxLastX                              ( 1,             NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY                 , m_contextModels + m_numContextModels, m_numContextModels)
85, m_cCuCtxLastY                              ( 1,             NUM_CTX_LAST_FLAG_SETS, NUM_CTX_LAST_FLAG_XY                 , m_contextModels + m_numContextModels, m_numContextModels)
86, m_cCUOneSCModel                            ( 1,             1,                      NUM_ONE_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
87, m_cCUAbsSCModel                            ( 1,             1,                      NUM_ABS_FLAG_CTX                     , m_contextModels + m_numContextModels, m_numContextModels)
88, m_cMVPIdxSCModel                           ( 1,             1,                      NUM_MVP_IDX_CTX                      , m_contextModels + m_numContextModels, m_numContextModels)
89, m_cSaoMergeSCModel                         ( 1,             1,                      NUM_SAO_MERGE_FLAG_CTX               , m_contextModels + m_numContextModels, m_numContextModels)
90, m_cSaoTypeIdxSCModel                       ( 1,             1,                      NUM_SAO_TYPE_IDX_CTX                 , m_contextModels + m_numContextModels, m_numContextModels)
91, m_cTransformSkipSCModel                    ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_TRANSFORMSKIP_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
92, m_CUTransquantBypassFlagSCModel            ( 1,             1,                      NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX    , m_contextModels + m_numContextModels, m_numContextModels)
93, m_explicitRdpcmFlagSCModel                 ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_EXPLICIT_RDPCM_FLAG_CTX          , m_contextModels + m_numContextModels, m_numContextModels)
94, m_explicitRdpcmDirSCModel                  ( 1,             MAX_NUM_CHANNEL_TYPE,   NUM_EXPLICIT_RDPCM_DIR_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
95, m_cCrossComponentPredictionSCModel         ( 1,             1,                      NUM_CROSS_COMPONENT_PREDICTION_CTX   , m_contextModels + m_numContextModels, m_numContextModels)
96, m_ChromaQpAdjFlagSCModel                   ( 1,             1,                      NUM_CHROMA_QP_ADJ_FLAG_CTX           , m_contextModels + m_numContextModels, m_numContextModels)
97, m_ChromaQpAdjIdcSCModel                    ( 1,             1,                      NUM_CHROMA_QP_ADJ_IDC_CTX            , m_contextModels + m_numContextModels, m_numContextModels)
98
99
100{
101  assert( m_numContextModels <= MAX_NUM_CTX_MOD );
102}
103
104TDecSbac::~TDecSbac()
105{
106}
107
108// ====================================================================================================================
109// Public member functions
110// ====================================================================================================================
111
112Void TDecSbac::resetEntropy(TComSlice* pSlice)
113{
114  SliceType sliceType  = pSlice->getSliceType();
115  Int       qp         = pSlice->getSliceQp();
116
117  if (pSlice->getPPS()->getCabacInitPresentFlag() && pSlice->getCabacInitFlag())
118  {
119    switch (sliceType)
120    {
121    case P_SLICE:           // change initialization table to B_SLICE initialization
122      sliceType = B_SLICE;
123      break;
124    case B_SLICE:           // change initialization table to P_SLICE initialization
125      sliceType = P_SLICE;
126      break;
127    default     :           // should not occur
128      assert(0);
129      break;
130    }
131  }
132
133  m_cCUSplitFlagSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_SPLIT_FLAG );
134  m_cCUSkipFlagSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_SKIP_FLAG );
135  m_cCUMergeFlagExtSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_MERGE_FLAG_EXT );
136  m_cCUMergeIdxExtSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_MERGE_IDX_EXT );
137  m_cCUPartSizeSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_PART_SIZE );
138  m_cCUPredModeSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_PRED_MODE );
139  m_cCUIntraPredSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_INTRA_PRED_MODE );
140  m_cCUChromaPredSCModel.initBuffer               ( sliceType, qp, (UChar*)INIT_CHROMA_PRED_MODE );
141  m_cCUInterDirSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_INTER_DIR );
142  m_cCUMvdSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_MVD );
143  m_cCURefPicSCModel.initBuffer                   ( sliceType, qp, (UChar*)INIT_REF_PIC );
144  m_cCUDeltaQpSCModel.initBuffer                  ( sliceType, qp, (UChar*)INIT_DQP );
145  m_cCUQtCbfSCModel.initBuffer                    ( sliceType, qp, (UChar*)INIT_QT_CBF );
146  m_cCUQtRootCbfSCModel.initBuffer                ( sliceType, qp, (UChar*)INIT_QT_ROOT_CBF );
147  m_cCUSigCoeffGroupSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_SIG_CG_FLAG );
148  m_cCUSigSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_SIG_FLAG );
149  m_cCuCtxLastX.initBuffer                        ( sliceType, qp, (UChar*)INIT_LAST );
150  m_cCuCtxLastY.initBuffer                        ( sliceType, qp, (UChar*)INIT_LAST );
151  m_cCUOneSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_ONE_FLAG );
152  m_cCUAbsSCModel.initBuffer                      ( sliceType, qp, (UChar*)INIT_ABS_FLAG );
153  m_cMVPIdxSCModel.initBuffer                     ( sliceType, qp, (UChar*)INIT_MVP_IDX );
154  m_cSaoMergeSCModel.initBuffer                   ( sliceType, qp, (UChar*)INIT_SAO_MERGE_FLAG );
155  m_cSaoTypeIdxSCModel.initBuffer                 ( sliceType, qp, (UChar*)INIT_SAO_TYPE_IDX );
156  m_cCUTransSubdivFlagSCModel.initBuffer          ( sliceType, qp, (UChar*)INIT_TRANS_SUBDIV_FLAG );
157  m_cTransformSkipSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_TRANSFORMSKIP_FLAG );
158  m_CUTransquantBypassFlagSCModel.initBuffer      ( sliceType, qp, (UChar*)INIT_CU_TRANSQUANT_BYPASS_FLAG );
159  m_explicitRdpcmFlagSCModel.initBuffer           ( sliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_FLAG);
160  m_explicitRdpcmDirSCModel.initBuffer            ( sliceType, qp, (UChar*)INIT_EXPLICIT_RDPCM_DIR);
161  m_cCrossComponentPredictionSCModel.initBuffer   ( sliceType, qp, (UChar*)INIT_CROSS_COMPONENT_PREDICTION );
162  m_ChromaQpAdjFlagSCModel.initBuffer             ( sliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_FLAG );
163  m_ChromaQpAdjIdcSCModel.initBuffer              ( sliceType, qp, (UChar*)INIT_CHROMA_QP_ADJ_IDC );
164
165  for (UInt statisticIndex = 0; statisticIndex < RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS ; statisticIndex++)
166  {
167    m_golombRiceAdaptationStatistics[statisticIndex] = 0;
168  }
169
170  m_pcTDecBinIf->start();
171}
172
173Void TDecSbac::parseTerminatingBit( UInt& ruiBit )
174{
175  m_pcTDecBinIf->decodeBinTrm( ruiBit );
176  if ( ruiBit == 1 )
177  {
178    m_pcTDecBinIf->finish();
179
180#if RExt__DECODER_DEBUG_BIT_STATISTICS
181    TComCodingStatistics::IncrementStatisticEP(STATS__TRAILING_BITS, m_pcBitstream->readOutTrailingBits(),0);
182#else
183    m_pcBitstream->readOutTrailingBits();
184#endif
185  }
186}
187
188
189
190Void TDecSbac::parseRemainingBytes( Bool noTrailingBytesExpected )
191{
192  if (noTrailingBytesExpected)
193  {
194    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
195    assert (numberOfRemainingSubstreamBytes == 0);
196  }
197  else
198  {
199    while (m_pcBitstream->getNumBitsLeft())
200    {
201      UInt trailingNullByte=m_pcBitstream->readByte();
202      if (trailingNullByte!=0)
203      {
204        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
205        assert(trailingNullByte==0);
206      }
207    }
208  }
209}
210
211#if RExt__DECODER_DEBUG_BIT_STATISTICS
212Void TDecSbac::xReadUnaryMaxSymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol, const class TComCodingStatisticsClassType &whichStat )
213#else
214Void TDecSbac::xReadUnaryMaxSymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
215#endif
216{
217  if (uiMaxSymbol == 0)
218  {
219    ruiSymbol = 0;
220    return;
221  }
222
223  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
224
225  if( ruiSymbol == 0 || uiMaxSymbol == 1 )
226  {
227    return;
228  }
229
230  UInt uiSymbol = 0;
231  UInt uiCont;
232
233  do
234  {
235    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
236    uiSymbol++;
237  } while( uiCont && ( uiSymbol < uiMaxSymbol - 1 ) );
238
239  if( uiCont && ( uiSymbol == uiMaxSymbol - 1 ) )
240  {
241    uiSymbol++;
242  }
243
244  ruiSymbol = uiSymbol;
245}
246
247#if RExt__DECODER_DEBUG_BIT_STATISTICS
248Void TDecSbac::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount, const class TComCodingStatisticsClassType &whichStat )
249#else
250Void TDecSbac::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount )
251#endif
252{
253  UInt uiSymbol = 0;
254  UInt uiBit = 1;
255
256  while( uiBit )
257  {
258    m_pcTDecBinIf->decodeBinEP( uiBit RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
259    uiSymbol += uiBit << uiCount++;
260  }
261
262  if ( --uiCount )
263  {
264    UInt bins;
265    m_pcTDecBinIf->decodeBinsEP( bins, uiCount RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
266    uiSymbol += bins;
267  }
268
269  ruiSymbol = uiSymbol;
270}
271
272#if RExt__DECODER_DEBUG_BIT_STATISTICS
273Void TDecSbac::xReadUnarySymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset, const class TComCodingStatisticsClassType &whichStat )
274#else
275Void TDecSbac::xReadUnarySymbol( UInt& ruiSymbol, ContextModel* pcSCModel, Int iOffset )
276#endif
277{
278  m_pcTDecBinIf->decodeBin( ruiSymbol, pcSCModel[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
279
280  if( !ruiSymbol )
281  {
282    return;
283  }
284
285  UInt uiSymbol = 0;
286  UInt uiCont;
287
288  do
289  {
290    m_pcTDecBinIf->decodeBin( uiCont, pcSCModel[ iOffset ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
291    uiSymbol++;
292  } while( uiCont );
293
294  ruiSymbol = uiSymbol;
295}
296
297
298/** Parsing of coeff_abs_level_remaing
299 * \param rSymbol                 reference to coeff_abs_level_remaing
300 * \param rParam                  reference to parameter
301 * \param useLimitedPrefixLength
302 * \param maxLog2TrDynamicRange
303 */
304#if RExt__DECODER_DEBUG_BIT_STATISTICS
305Void TDecSbac::xReadCoefRemainExGolomb ( UInt &rSymbol, UInt &rParam, const Bool useLimitedPrefixLength, const Int maxLog2TrDynamicRange, const class TComCodingStatisticsClassType &whichStat )
306#else
307Void TDecSbac::xReadCoefRemainExGolomb ( UInt &rSymbol, UInt &rParam, const Bool useLimitedPrefixLength, const Int maxLog2TrDynamicRange )
308#endif
309{
310  UInt prefix   = 0;
311  UInt codeWord = 0;
312
313  if (useLimitedPrefixLength)
314  {
315    const UInt longestPossiblePrefix = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange)) + COEF_REMAIN_BIN_REDUCTION;
316
317    do
318    {
319      prefix++;
320      m_pcTDecBinIf->decodeBinEP( codeWord RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
321    } while((codeWord != 0) && (prefix < longestPossiblePrefix));
322  }
323  else
324  {
325    do
326    {
327      prefix++;
328      m_pcTDecBinIf->decodeBinEP( codeWord RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat) );
329    } while( codeWord);
330  }
331
332  codeWord  = 1 - codeWord;
333  prefix -= codeWord;
334  codeWord=0;
335
336  if (prefix < COEF_REMAIN_BIN_REDUCTION )
337  {
338    m_pcTDecBinIf->decodeBinsEP(codeWord,rParam RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
339    rSymbol = (prefix<<rParam) + codeWord;
340  }
341  else if (useLimitedPrefixLength)
342  {
343    const UInt maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange));
344
345    const UInt prefixLength = prefix - COEF_REMAIN_BIN_REDUCTION;
346    const UInt suffixLength = (prefixLength == maximumPrefixLength) ? (maxLog2TrDynamicRange - rParam) : prefixLength;
347
348    m_pcTDecBinIf->decodeBinsEP(codeWord, (suffixLength + rParam) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
349
350    rSymbol = codeWord + ((((1 << prefixLength) - 1) + COEF_REMAIN_BIN_REDUCTION) << rParam);
351  }
352  else
353  {
354    m_pcTDecBinIf->decodeBinsEP(codeWord,prefix-COEF_REMAIN_BIN_REDUCTION+rParam RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(whichStat));
355    rSymbol = (((1<<(prefix-COEF_REMAIN_BIN_REDUCTION))+COEF_REMAIN_BIN_REDUCTION-1)<<rParam)+codeWord;
356  }
357}
358
359/** Parse I_PCM information.
360 * \param pcCU
361 * \param uiAbsPartIdx
362 * \param uiDepth
363 * \returns Void
364 *
365 * If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
366 */
367Void TDecSbac::parseIPCMInfo ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
368{
369  UInt uiSymbol;
370
371  m_pcTDecBinIf->decodeBinTrm(uiSymbol);
372
373#if NH_MV_ENC_DEC_TRAC
374  DTRACE_CU("pcm_flag", uiSymbol)
375#endif
376
377
378  if (uiSymbol == 1)
379  {
380    Bool bIpcmFlag = true;
381    const TComSPS &sps=*(pcCU->getSlice()->getSPS());
382
383    pcCU->setPartSizeSubParts  ( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
384    pcCU->setSizeSubParts      ( sps.getMaxCUWidth()>>uiDepth, sps.getMaxCUHeight()>>uiDepth, uiAbsPartIdx, uiDepth );
385    pcCU->setTrIdxSubParts     ( 0, uiAbsPartIdx, uiDepth );
386    pcCU->setIPCMFlagSubParts  ( bIpcmFlag, uiAbsPartIdx, uiDepth );
387
388    const UInt minCoeffSizeY = pcCU->getPic()->getMinCUWidth() * pcCU->getPic()->getMinCUHeight();
389    const UInt offsetY       = minCoeffSizeY * uiAbsPartIdx;
390    for (UInt ch=0; ch < pcCU->getPic()->getNumberValidComponents(); ch++)
391    {
392      const ComponentID compID = ComponentID(ch);
393      const UInt offset = offsetY >> (pcCU->getPic()->getComponentScaleX(compID) + pcCU->getPic()->getComponentScaleY(compID));
394      Pel * pPCMSample  = pcCU->getPCMSample(compID) + offset;
395      const UInt width  = pcCU->getWidth (uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleX(compID);
396      const UInt height = pcCU->getHeight(uiAbsPartIdx) >> pcCU->getPic()->getComponentScaleY(compID);
397      const UInt sampleBits = pcCU->getSlice()->getSPS()->getPCMBitDepth(toChannelType(compID));
398      for (UInt y=0; y<height; y++)
399      {
400        for (UInt x=0; x<width; x++)
401        {
402          UInt sample;
403          m_pcTDecBinIf->xReadPCMCode(sampleBits, sample);
404          pPCMSample[x] = sample;
405        }
406        pPCMSample += width;
407      }
408    }
409    m_pcTDecBinIf->start();
410  }
411}
412
413Void TDecSbac::parseCUTransquantBypassFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
414{
415  UInt uiSymbol;
416  m_pcTDecBinIf->decodeBin( uiSymbol, m_CUTransquantBypassFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__TQ_BYPASS_FLAG) );
417#if NH_MV_ENC_DEC_TRAC
418  DTRACE_CU("cu_transquant_bypass_flag", uiSymbol); 
419#endif
420
421  pcCU->setCUTransquantBypassSubParts(uiSymbol ? true : false, uiAbsPartIdx, uiDepth);
422}
423
424/** parse skip flag
425 * \param pcCU
426 * \param uiAbsPartIdx
427 * \param uiDepth
428 * \returns Void
429 */
430Void TDecSbac::parseSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
431{
432  if( pcCU->getSlice()->isIntra() )
433  {
434    return;
435  }
436
437  UInt uiSymbol = 0;
438  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx );
439  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SKIP_FLAG) );
440#if !NH_MV_ENC_DEC_TRAC
441  DTRACE_CABAC_VL( g_nSymbolCounter++ );
442  DTRACE_CABAC_T( "\tSkipFlag" );
443  DTRACE_CABAC_T( "\tuiCtxSkip: ");
444  DTRACE_CABAC_V( uiCtxSkip );
445  DTRACE_CABAC_T( "\tuiSymbol: ");
446  DTRACE_CABAC_V( uiSymbol );
447  DTRACE_CABAC_T( "\n");
448#endif
449  if( uiSymbol )
450  {
451    pcCU->setSkipFlagSubParts( true,        uiAbsPartIdx, uiDepth );
452    pcCU->setPredModeSubParts( MODE_INTER,  uiAbsPartIdx, uiDepth );
453    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
454    pcCU->setSizeSubParts( pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth, pcCU->getSlice()->getSPS()->getMaxCUHeight()>>uiDepth, uiAbsPartIdx, uiDepth );
455    pcCU->setMergeFlagSubParts( true , uiAbsPartIdx, 0, uiDepth );
456  }
457#if NH_MV_ENC_DEC_TRAC
458  DTRACE_CU("cu_skip_flag", uiSymbol); 
459#endif
460
461}
462
463
464/** parse merge flag
465 * \param pcCU
466 * \param uiAbsPartIdx
467 * \param uiDepth
468 * \param uiPUIdx
469 * \returns Void
470 */
471Void TDecSbac::parseMergeFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiPUIdx )
472{
473  UInt uiSymbol;
474  m_pcTDecBinIf->decodeBin( uiSymbol, *m_cCUMergeFlagExtSCModel.get( 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_FLAG) );
475#if NH_MV_ENC_DEC_TRAC
476  DTRACE_PU("merge_flag", uiSymbol)
477#endif
478  pcCU->setMergeFlagSubParts( uiSymbol ? true : false, uiAbsPartIdx, uiPUIdx, uiDepth );
479#if !NH_MV_ENC_DEC_TRAC
480  DTRACE_CABAC_VL( g_nSymbolCounter++ );
481  DTRACE_CABAC_T( "\tMergeFlag: " );
482  DTRACE_CABAC_V( uiSymbol );
483  DTRACE_CABAC_T( "\tAddress: " );
484  DTRACE_CABAC_V( pcCU->getCtuRsAddr() );
485  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
486  DTRACE_CABAC_V( uiAbsPartIdx );
487  DTRACE_CABAC_T( "\n" );
488#endif
489}
490
491Void TDecSbac::parseMergeIndex ( TComDataCU* pcCU, UInt& ruiMergeIndex )
492{
493  UInt uiUnaryIdx = 0;
494  UInt uiNumCand = pcCU->getSlice()->getMaxNumMergeCand();
495  if ( uiNumCand > 1 )
496  {
497    for( ; uiUnaryIdx < uiNumCand - 1; ++uiUnaryIdx )
498    {
499      UInt uiSymbol = 0;
500      if ( uiUnaryIdx==0 )
501      {
502        m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_INDEX) );
503      }
504      else
505      {
506        m_pcTDecBinIf->decodeBinEP( uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MERGE_INDEX) );
507      }
508      if( uiSymbol == 0 )
509      {
510        break;
511      }
512    }
513#if NH_MV_ENC_DEC_TRAC
514    DTRACE_PU("merge_idx", uiUnaryIdx)
515#endif
516  }
517  ruiMergeIndex = uiUnaryIdx;
518
519#if !NH_MV_ENC_DEC_TRAC
520  DTRACE_CABAC_VL( g_nSymbolCounter++ )
521  DTRACE_CABAC_T( "\tparseMergeIndex()" )
522  DTRACE_CABAC_T( "\tuiMRGIdx= " )
523  DTRACE_CABAC_V( ruiMergeIndex )
524  DTRACE_CABAC_T( "\n" )
525#endif
526}
527
528Void TDecSbac::parseMVPIdx      ( Int& riMVPIdx )
529{
530  UInt uiSymbol;
531  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) );
532  riMVPIdx = uiSymbol;
533}
534
535Void TDecSbac::parseSplitFlag     ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
536{
537  if( uiDepth == pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize() )
538  {
539    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
540    return;
541  }
542#if RExt__DECODER_DEBUG_BIT_STATISTICS
543  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__SPLIT_FLAG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth]+2);
544#endif
545
546  UInt uiSymbol;
547  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUSplitFlagSCModel.get( 0, 0, pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth ) ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
548#if NH_MV_ENC_DEC_TRAC
549    DTRACE_CU("split_cu_flag", uiSymbol); 
550#else
551  DTRACE_CABAC_VL( g_nSymbolCounter++ )
552  DTRACE_CABAC_T( "\tSplitFlag\n" )
553#endif
554  pcCU->setDepthSubParts( uiDepth + uiSymbol, uiAbsPartIdx );
555
556  return;
557}
558
559/** parse partition size
560 * \param pcCU
561 * \param uiAbsPartIdx
562 * \param uiDepth
563 * \returns Void
564 */
565Void TDecSbac::parsePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
566{
567  UInt uiSymbol, uiMode = 0;
568  PartSize eMode;
569  const UChar cuWidth =UChar(pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth);
570  const UChar cuHeight=UChar(pcCU->getSlice()->getSPS()->getMaxCUHeight()>>uiDepth);
571  const Int log2DiffMaxMinCodingBlockSize = pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize();
572
573#if RExt__DECODER_DEBUG_BIT_STATISTICS
574  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__PART_SIZE, g_aucConvertToBit[cuWidth]+2);
575#endif
576
577  assert ( pcCU->getSlice()->getSPS()->getLog2DiffMaxMinCodingBlockSize() == log2DiffMaxMinCodingBlockSize);
578
579
580  if ( pcCU->isIntra( uiAbsPartIdx ) )
581  {
582    uiSymbol = 1;
583    if( uiDepth == log2DiffMaxMinCodingBlockSize )
584    {
585      m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
586#if NH_MV_ENC_DEC_TRAC         
587        DTRACE_CU("part_mode", uiSymbol)
588#endif       
589    }
590    eMode = uiSymbol ? SIZE_2Nx2N : SIZE_NxN;
591    UInt uiTrLevel = 0;
592    UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)]+2;
593    UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()]+2;
594    uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
595    if( eMode == SIZE_NxN )
596    {
597      pcCU->setTrIdxSubParts( 1+uiTrLevel, uiAbsPartIdx, uiDepth );
598    }
599    else
600    {
601      pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );
602    }
603  }
604  else
605  {
606    UInt uiMaxNumBits = 2;
607
608    if( uiDepth == log2DiffMaxMinCodingBlockSize && !( cuWidth == 8 && cuHeight == 8 ) )
609    {
610      uiMaxNumBits ++;
611    }
612
613    for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
614    {
615      m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, ui) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
616      if ( uiSymbol )
617      {
618        break;
619      }
620      uiMode++;
621    }
622    eMode = (PartSize) uiMode;
623    if ( pcCU->getSlice()->getSPS()->getUseAMP() && uiDepth < log2DiffMaxMinCodingBlockSize )
624    {
625      if (eMode == SIZE_2NxN)
626      {
627        m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 3 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
628        if (uiSymbol == 0)
629        {
630          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
631          eMode = (uiSymbol == 0? SIZE_2NxnU : SIZE_2NxnD);
632        }
633      }
634      else if (eMode == SIZE_Nx2N)
635      {
636        m_pcTDecBinIf->decodeBin(uiSymbol, m_cCUPartSizeSCModel.get( 0, 0, 3 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
637        if (uiSymbol == 0)
638        {
639          m_pcTDecBinIf->decodeBinEP(uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
640          eMode = (uiSymbol == 0? SIZE_nLx2N : SIZE_nRx2N);
641        }
642      }
643    }
644#if NH_MV_ENC_DEC_TRAC         
645      DTRACE_CU("part_mode", eMode )
646#endif
647  }
648  pcCU->setPartSizeSubParts( eMode, uiAbsPartIdx, uiDepth );
649  pcCU->setSizeSubParts( cuWidth, cuHeight, uiAbsPartIdx, uiDepth );
650}
651
652
653/** parse prediction mode
654 * \param pcCU
655 * \param uiAbsPartIdx
656 * \param uiDepth
657 * \returns Void
658 */
659Void TDecSbac::parsePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
660{
661  if( pcCU->getSlice()->isIntra() )
662  {
663    pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
664    return;
665  }
666
667  UInt uiSymbol;
668  Int  iPredMode = MODE_INTER;
669  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUPredModeSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__PRED_MODE) );
670  iPredMode += uiSymbol;
671#if NH_MV_ENC_DEC_TRAC         
672  DTRACE_CU("pred_mode_flag", uiSymbol)
673#endif       
674  pcCU->setPredModeSubParts( (PredMode)iPredMode, uiAbsPartIdx, uiDepth );
675}
676
677
678Void TDecSbac::parseIntraDirLumaAng  ( TComDataCU* pcCU, UInt absPartIdx, UInt depth )
679{
680  PartSize mode = pcCU->getPartitionSize( absPartIdx );
681  UInt partNum = mode==SIZE_NxN?4:1;
682  UInt partOffset = ( pcCU->getPic()->getNumPartitionsInCtu() >> ( pcCU->getDepth(absPartIdx) << 1 ) ) >> 2;
683  UInt mpmPred[4],symbol;
684  Int j,intraPredMode;
685  if (mode==SIZE_NxN)
686  {
687    depth++;
688  }
689#if RExt__DECODER_DEBUG_BIT_STATISTICS
690  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__INTRA_DIR_ANG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>depth]+2, CHANNEL_TYPE_LUMA);
691#endif
692  for (j=0;j<partNum;j++)
693  {
694    m_pcTDecBinIf->decodeBin( symbol, m_cCUIntraPredSCModel.get( 0, 0, 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
695    mpmPred[j] = symbol;
696#if NH_MV_ENC_DEC_TRAC         
697      DTRACE_CU("prev_intra_luma_pred_flag", symbol)
698#endif
699  }
700  for (j=0;j<partNum;j++)
701  {
702    Int preds[NUM_MOST_PROBABLE_MODES] = {-1, -1, -1};
703    pcCU->getIntraDirPredictor(absPartIdx+partOffset*j, preds, COMPONENT_Y);
704    if (mpmPred[j])
705    {
706      m_pcTDecBinIf->decodeBinEP( symbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
707      if (symbol)
708      {
709        m_pcTDecBinIf->decodeBinEP( symbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
710        symbol++;
711      }
712#if NH_MV_ENC_DEC_TRAC         
713        DTRACE_CU("mpm_idx", symbol)
714#endif
715      intraPredMode = preds[symbol];
716    }
717    else
718    {
719      m_pcTDecBinIf->decodeBinsEP( symbol, 5 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
720      intraPredMode = symbol;
721#if NH_MV_ENC_DEC_TRAC         
722        DTRACE_CU("rem_intra_luma_pred_mode", symbol)
723#endif       
724
725      //postponed sorting of MPMs (only in remaining branch)
726      if (preds[0] > preds[1])
727      {
728        std::swap(preds[0], preds[1]);
729      }
730      if (preds[0] > preds[2])
731      {
732        std::swap(preds[0], preds[2]);
733      }
734      if (preds[1] > preds[2])
735      {
736        std::swap(preds[1], preds[2]);
737      }
738      for ( UInt i = 0; i < NUM_MOST_PROBABLE_MODES; i++ )
739      {
740        intraPredMode += ( intraPredMode >= preds[i] );
741      }
742    }
743    pcCU->setIntraDirSubParts(CHANNEL_TYPE_LUMA, (UChar)intraPredMode, absPartIdx+partOffset*j, depth );
744  }
745}
746
747
748Void TDecSbac::parseIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
749{
750  UInt uiSymbol;
751#if RExt__DECODER_DEBUG_BIT_STATISTICS
752  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__INTRA_DIR_ANG, g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()>>uiDepth]+2, CHANNEL_TYPE_CHROMA);
753#endif
754
755  m_pcTDecBinIf->decodeBin( uiSymbol, m_cCUChromaPredSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
756  if( uiSymbol == 0 )
757  {
758#if NH_MV_ENC_DEC_TRAC         
759    DTRACE_CU("intra_chroma_pred_mode", uiSymbol )
760#endif       
761    uiSymbol = DM_CHROMA_IDX;
762  }
763  else
764  {
765    UInt uiIPredMode;
766    m_pcTDecBinIf->decodeBinsEP( uiIPredMode, 2 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
767#if NH_MV_ENC_DEC_TRAC         
768      DTRACE_CU("intra_chroma_pred_mode", uiIPredMode )
769#endif       
770    UInt uiAllowedChromaDir[ NUM_CHROMA_MODE ];
771    pcCU->getAllowedChromaDir( uiAbsPartIdx, uiAllowedChromaDir );
772    uiSymbol = uiAllowedChromaDir[ uiIPredMode ];
773  }
774
775  pcCU->setIntraDirSubParts( CHANNEL_TYPE_CHROMA, uiSymbol, uiAbsPartIdx, uiDepth );
776}
777
778Void TDecSbac::parseInterDir( TComDataCU* pcCU, UInt& ruiInterDir, UInt uiAbsPartIdx )
779{
780  UInt uiSymbol;
781  const UInt uiCtx = pcCU->getCtxInterDir( uiAbsPartIdx );
782  ContextModel *pCtx = m_cCUInterDirSCModel.get( 0 );
783
784  uiSymbol = 0;
785  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N || pcCU->getHeight(uiAbsPartIdx) != 8 )
786  {
787    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + uiCtx ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__INTER_DIR) );
788  }
789
790  if( uiSymbol )
791  {
792    uiSymbol = 2;
793  }
794  else
795  {
796    m_pcTDecBinIf->decodeBin( uiSymbol, *( pCtx + 4 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__INTER_DIR) );
797    assert(uiSymbol == 0 || uiSymbol == 1);
798  }
799
800  uiSymbol++;
801  ruiInterDir = uiSymbol;
802#if NH_MV_ENC_DEC_TRAC
803    DTRACE_PU("inter_pred_idc", ruiInterDir - 1 )   
804#endif
805
806  return;
807}
808
809Void TDecSbac::parseRefFrmIdx( TComDataCU* pcCU, Int& riRefFrmIdx, RefPicList eRefList )
810{
811  UInt uiSymbol;
812
813  ContextModel *pCtx = m_cCURefPicSCModel.get( 0 );
814  m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
815
816  if( uiSymbol )
817  {
818    UInt uiRefNum = pcCU->getSlice()->getNumRefIdx( eRefList ) - 2;
819    pCtx++;
820    UInt ui;
821    for( ui = 0; ui < uiRefNum; ++ui )
822    {
823      if( ui == 0 )
824      {
825        m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
826      }
827      else
828      {
829        m_pcTDecBinIf->decodeBinEP( uiSymbol RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__REF_FRM_IDX) );
830      }
831      if( uiSymbol == 0 )
832      {
833        break;
834      }
835    }
836    uiSymbol = ui + 1;
837  }
838  riRefFrmIdx = uiSymbol;
839#if NH_MV_ENC_DEC_TRAC
840#if ENC_DEC_TRACE
841  if ( eRefList == REF_PIC_LIST_0 )
842  {
843    DTRACE_PU("ref_idx_l0", uiSymbol)
844  }
845  else
846  {
847    DTRACE_PU("ref_idx_l1", uiSymbol)
848  }
849#endif
850#endif
851
852  return;
853}
854
855Void TDecSbac::parseMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth, RefPicList eRefList )
856{
857  UInt uiSymbol;
858  UInt uiHorAbs;
859  UInt uiVerAbs;
860  UInt uiHorSign = 0;
861  UInt uiVerSign = 0;
862  ContextModel *pCtx = m_cCUMvdSCModel.get( 0 );
863
864  if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefList == REF_PIC_LIST_1 && pcCU->getInterDir(uiAbsPartIdx)==3)
865  {
866    uiHorAbs=0;
867    uiVerAbs=0;
868  }
869  else
870  {
871    m_pcTDecBinIf->decodeBin( uiHorAbs, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
872    m_pcTDecBinIf->decodeBin( uiVerAbs, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
873
874    const Bool bHorAbsGr0 = uiHorAbs != 0;
875    const Bool bVerAbsGr0 = uiVerAbs != 0;
876    pCtx++;
877
878    if( bHorAbsGr0 )
879    {
880      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
881      uiHorAbs += uiSymbol;
882    }
883
884    if( bVerAbsGr0 )
885    {
886      m_pcTDecBinIf->decodeBin( uiSymbol, *pCtx RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD) );
887      uiVerAbs += uiSymbol;
888    }
889
890    if( bHorAbsGr0 )
891    {
892      if( 2 == uiHorAbs )
893      {
894        xReadEpExGolomb( uiSymbol, 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
895        uiHorAbs += uiSymbol;
896      }
897
898      m_pcTDecBinIf->decodeBinEP( uiHorSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
899    }
900
901    if( bVerAbsGr0 )
902    {
903      if( 2 == uiVerAbs )
904      {
905        xReadEpExGolomb( uiSymbol, 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
906        uiVerAbs += uiSymbol;
907      }
908
909      m_pcTDecBinIf->decodeBinEP( uiVerSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__MVD_EP) );
910    }
911
912  }
913
914  const TComMv cMv( uiHorSign ? -Int( uiHorAbs ): uiHorAbs, uiVerSign ? -Int( uiVerAbs ) : uiVerAbs );
915  pcCU->getCUMvField( eRefList )->setAllMvd( cMv, pcCU->getPartitionSize( uiAbsPartIdx ), uiAbsPartIdx, uiDepth, uiPartIdx );
916  return;
917}
918
919Void TDecSbac::parseCrossComponentPrediction( TComTU &rTu, ComponentID compID )
920{
921  TComDataCU *pcCU = rTu.getCU();
922
923  if( isLuma(compID) || !pcCU->getSlice()->getPPS()->getPpsRangeExtension().getCrossComponentPredictionEnabledFlag() )
924  {
925    return;
926  }
927
928  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
929
930  if (!pcCU->isIntra(uiAbsPartIdx) || (pcCU->getIntraDir( CHANNEL_TYPE_CHROMA, uiAbsPartIdx ) == DM_CHROMA_IDX))
931  {
932    SChar alpha = 0;
933    UInt symbol = 0;
934
935    DTRACE_CABAC_VL( g_nSymbolCounter++ )
936    DTRACE_CABAC_T("\tparseCrossComponentPrediction()")
937    DTRACE_CABAC_T( "\tAddr=" )
938    DTRACE_CABAC_V( compID )
939    DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
940    DTRACE_CABAC_V( uiAbsPartIdx )
941#if RExt__DECODER_DEBUG_BIT_STATISTICS
942    TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__CROSS_COMPONENT_PREDICTION, (g_aucConvertToBit[rTu.getRect(compID).width] + 2), compID);
943#endif
944    ContextModel *pCtx = m_cCrossComponentPredictionSCModel.get(0, 0) + ((compID == COMPONENT_Cr) ? (NUM_CROSS_COMPONENT_PREDICTION_CTX >> 1) : 0);
945    m_pcTDecBinIf->decodeBin( symbol, pCtx[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
946
947    if(symbol != 0)
948    {
949      // Cross-component prediction alpha is non-zero.
950      UInt sign = 0;
951      m_pcTDecBinIf->decodeBin( symbol, pCtx[1] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
952
953      if (symbol != 0)
954      {
955        // alpha is 2 (symbol=1), 4(symbol=2) or 8(symbol=3).
956        // Read up to two more bits
957        xReadUnaryMaxSymbol( symbol, (pCtx + 2), 1, 2 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
958        symbol += 1;
959      }
960      m_pcTDecBinIf->decodeBin( sign, pCtx[4] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
961
962      alpha = (sign != 0) ? -(1 << symbol) : (1 << symbol);
963    }
964    DTRACE_CABAC_T( "\tAlpha=" )
965    DTRACE_CABAC_V( alpha )
966    DTRACE_CABAC_T( "\n" )
967
968    pcCU->setCrossComponentPredictionAlphaPartRange( alpha, compID, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts( compID ) );
969  }
970}
971
972Void TDecSbac::parseTransformSubdivFlag( UInt& ruiSubdivFlag, UInt uiLog2TransformBlockSize )
973{
974  m_pcTDecBinIf->decodeBin( ruiSubdivFlag, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiLog2TransformBlockSize )
975      RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__TRANSFORM_SUBDIV_FLAG, 5-uiLog2TransformBlockSize))
976                          );
977#if !NH_MV_ENC_DEC_TRAC
978  DTRACE_CABAC_VL( g_nSymbolCounter++ )
979  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
980  DTRACE_CABAC_T( "\tsymbol=" )
981  DTRACE_CABAC_V( ruiSubdivFlag )
982  DTRACE_CABAC_T( "\tctx=" )
983  DTRACE_CABAC_V( uiLog2TransformBlockSize )
984  DTRACE_CABAC_T( "\n" )
985#else
986  DTRACE_TU("split_transform_flag", ruiSubdivFlag )
987#endif
988
989}
990
991Void TDecSbac::parseQtRootCbf( UInt uiAbsPartIdx, UInt& uiQtRootCbf )
992{
993  UInt uiSymbol;
994  const UInt uiCtx = 0;
995  m_pcTDecBinIf->decodeBin( uiSymbol , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__QT_ROOT_CBF) );
996#if !NH_MV_ENC_DEC_TRAC
997  DTRACE_CABAC_VL( g_nSymbolCounter++ )
998  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
999  DTRACE_CABAC_T( "\tsymbol=" )
1000  DTRACE_CABAC_V( uiSymbol )
1001  DTRACE_CABAC_T( "\tctx=" )
1002  DTRACE_CABAC_V( uiCtx )
1003  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1004  DTRACE_CABAC_V( uiAbsPartIdx )
1005  DTRACE_CABAC_T( "\n" )
1006#else
1007  DTRACE_CU( "rqt_root_cbf", uiSymbol )
1008#endif
1009
1010  uiQtRootCbf = uiSymbol;
1011}
1012
1013Void TDecSbac::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1014{
1015  Int qp;
1016  UInt uiDQp;
1017  Int  iDQp;
1018
1019  UInt uiSymbol;
1020
1021  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) );
1022
1023  if( uiDQp >= CU_DQP_TU_CMAX)
1024  {
1025    xReadEpExGolomb( uiSymbol, CU_DQP_EG_k RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DELTA_QP_EP));
1026    uiDQp+=uiSymbol;
1027  }
1028
1029  if ( uiDQp > 0 )
1030  {
1031    UInt uiSign;
1032    Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
1033    m_pcTDecBinIf->decodeBinEP(uiSign RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__DELTA_QP_EP));
1034    iDQp = uiDQp;
1035    if(uiSign)
1036    {
1037      iDQp = -iDQp;
1038    }
1039    qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+qpBdOffsetY)) - qpBdOffsetY;
1040  }
1041  else
1042  {
1043    qp = pcCU->getRefQP(uiAbsPartIdx);
1044  }
1045
1046  pcCU->setQPSubParts(qp, uiAbsPartIdx, uiDepth);
1047  pcCU->setCodedQP(qp);
1048}
1049
1050/** parse chroma qp adjustment, converting to the internal table representation.
1051 * \returns Void
1052 */
1053Void TDecSbac::parseChromaQpAdjustment( TComDataCU* cu, UInt absPartIdx, UInt depth )
1054{
1055  UInt symbol;
1056#if RExt__DECODER_DEBUG_BIT_STATISTICS
1057  const TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__CHROMA_QP_ADJUSTMENT, g_aucConvertToBit[cu->getSlice()->getSPS()->getMaxCUWidth()>>depth]+2, CHANNEL_TYPE_CHROMA);
1058#endif
1059
1060  Int chromaQpOffsetListLen = cu->getSlice()->getPPS()->getPpsRangeExtension().getChromaQpOffsetListLen();
1061
1062  // cu_chroma_qp_offset_flag
1063  m_pcTDecBinIf->decodeBin( symbol, m_ChromaQpAdjFlagSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1064
1065  if (symbol && chromaQpOffsetListLen > 1)
1066  {
1067    // cu_chroma_qp_offset_idx
1068    xReadUnaryMaxSymbol( symbol,  &m_ChromaQpAdjIdcSCModel.get( 0, 0, 0 ), 0, chromaQpOffsetListLen - 1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1069    symbol++;
1070  }
1071  /* NB, symbol = 0 if outer flag is not set,
1072   *              1 if outer flag is set and there is no inner flag
1073   *              1+ otherwise */
1074  cu->setChromaQpAdjSubParts( symbol, absPartIdx, depth );
1075  cu->setCodedChromaQpAdj(symbol);
1076}
1077
1078Void TDecSbac::parseQtCbf( TComTU &rTu, const ComponentID compID, const Bool lowestLevel )
1079{
1080  TComDataCU* pcCU = rTu.getCU();
1081
1082  const UInt absPartIdx       = rTu.GetAbsPartIdxTU(compID);
1083  const UInt TUDepth          = rTu.GetTransformDepthRel();
1084  const UInt uiCtx            = pcCU->getCtxQtCbf( rTu, toChannelType(compID) );
1085  const UInt contextSet       = toChannelType(compID);
1086
1087  const UInt width            = rTu.getRect(compID).width;
1088  const UInt height           = rTu.getRect(compID).height;
1089  const Bool canQuadSplit     = (width >= (MIN_TU_SIZE * 2)) && (height >= (MIN_TU_SIZE * 2));
1090  const UInt coveredPartIdxes = rTu.GetAbsPartIdxNumParts(compID);
1091
1092  //             Since the CBF for chroma is coded at the highest level possible, if sub-TUs are
1093  //             to be coded for a 4x8 chroma TU, their CBFs must be coded at the highest 4x8 level
1094  //             (i.e. where luma TUs are 8x8 rather than 4x4)
1095  //    ___ ___
1096  //   |   |   | <- 4 x (8x8 luma + 4x8 4:2:2 chroma)
1097  //   |___|___|    each quadrant has its own chroma CBF
1098  //   |   |   | _ _ _ _
1099  //   |___|___|        |
1100  //   <--16--->        V
1101  //                   _ _
1102  //                  |_|_| <- 4 x 4x4 luma + 1 x 4x8 4:2:2 chroma
1103  //                  |_|_|    no chroma CBF is coded - instead the parent CBF is inherited
1104  //                  <-8->    if sub-TUs are present, their CBFs had to be coded at the parent level
1105
1106  const UInt lowestTUDepth = TUDepth + ((!lowestLevel && !canQuadSplit) ? 1 : 0); //unsplittable TUs inherit their parent's CBF
1107        UInt lowestTUCBF   = 0;
1108
1109  if ((width != height) && (lowestLevel || !canQuadSplit)) //if sub-TUs are present
1110  {
1111    const UInt subTUDepth        = lowestTUDepth + 1;
1112    const UInt partIdxesPerSubTU = rTu.GetAbsPartIdxNumParts(compID) >> 1;
1113
1114    UInt combinedSubTUCBF = 0;
1115
1116    for (UInt subTU = 0; subTU < 2; subTU++)
1117    {
1118      UInt uiCbf = MAX_UINT;
1119      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)));
1120
1121      const UInt subTUAbsPartIdx = absPartIdx + (subTU * partIdxesPerSubTU);
1122      pcCU->setCbfPartRange((uiCbf << subTUDepth), compID, subTUAbsPartIdx, partIdxesPerSubTU);
1123      combinedSubTUCBF |= uiCbf;
1124#if !NH_MV_ENC_DEC_TRAC 
1125      DTRACE_CABAC_VL( g_nSymbolCounter++ )
1126      DTRACE_CABAC_T( "\tparseQtCbf()" )
1127      DTRACE_CABAC_T( "\tsub-TU=" )
1128      DTRACE_CABAC_V( subTU )
1129      DTRACE_CABAC_T( "\tsymbol=" )
1130      DTRACE_CABAC_V( uiCbf )
1131      DTRACE_CABAC_T( "\tctx=" )
1132      DTRACE_CABAC_V( uiCtx )
1133      DTRACE_CABAC_T( "\tetype=" )
1134      DTRACE_CABAC_V( compID )
1135      DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1136      DTRACE_CABAC_V( subTUAbsPartIdx )
1137      DTRACE_CABAC_T( "\n" )
1138#else
1139  if ( compID == COMPONENT_Cb )
1140  {
1141    DTRACE_TU("cbf_cb", uiCbf )
1142  }
1143  else if ( compID == COMPONENT_Cr )
1144  {
1145    DTRACE_TU("cbf_cr", uiCbf )
1146  }
1147  else
1148  {
1149    DTRACE_TU("cbf_luma", uiCbf )
1150  }
1151#endif
1152
1153    }
1154
1155    //propagate the sub-TU CBF up to the lowest TU level
1156    if (combinedSubTUCBF != 0)
1157    {
1158      pcCU->bitwiseOrCbfPartRange((combinedSubTUCBF << lowestTUDepth), compID, absPartIdx, coveredPartIdxes);
1159      lowestTUCBF = combinedSubTUCBF;
1160    }
1161  }
1162  else
1163  {
1164    UInt uiCbf = MAX_UINT;
1165    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)));
1166
1167    pcCU->setCbfSubParts((uiCbf << lowestTUDepth), compID, absPartIdx, rTu.GetTransformDepthTotalAdj(compID));
1168#if !NH_MV_ENC_DEC_TRAC
1169    DTRACE_CABAC_VL( g_nSymbolCounter++ )
1170    DTRACE_CABAC_T( "\tparseQtCbf()" )
1171    DTRACE_CABAC_T( "\tsymbol=" )
1172    DTRACE_CABAC_V( uiCbf )
1173    DTRACE_CABAC_T( "\tctx=" )
1174    DTRACE_CABAC_V( uiCtx )
1175    DTRACE_CABAC_T( "\tetype=" )
1176    DTRACE_CABAC_V( compID )
1177    DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1178    DTRACE_CABAC_V( rTu.GetAbsPartIdxTU(compID) )
1179    DTRACE_CABAC_T( "\n" )
1180#else
1181    if ( compID == COMPONENT_Cb )
1182    {
1183      DTRACE_TU("cbf_cb", uiCbf )
1184    }
1185    else if ( compID == COMPONENT_Cr )
1186    {
1187      DTRACE_TU("cbf_cr", uiCbf )
1188    }
1189    else
1190    {
1191      DTRACE_TU("cbf_luma", uiCbf )
1192    }
1193#endif
1194    lowestTUCBF = uiCbf;
1195  }
1196
1197  //propagate the lowest level CBF up to the current level
1198  if (lowestTUCBF != 0)
1199  {
1200    for (UInt depth = TUDepth; depth < lowestTUDepth; depth++)
1201    {
1202      pcCU->bitwiseOrCbfPartRange((lowestTUCBF << depth), compID, absPartIdx, coveredPartIdxes);
1203    }
1204  }
1205}
1206
1207
1208Void TDecSbac::parseTransformSkipFlags (TComTU &rTu, ComponentID component)
1209{
1210  TComDataCU* pcCU=rTu.getCU();
1211  UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(component);
1212
1213  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1214  {
1215    return;
1216  }
1217
1218  if (!TUCompRectHasAssociatedTransformSkipFlag(rTu.getRect(component), pcCU->getSlice()->getPPS()->getPpsRangeExtension().getLog2MaxTransformSkipBlockSize()))
1219  {
1220    return;
1221  }
1222
1223  UInt useTransformSkip;
1224
1225  m_pcTDecBinIf->decodeBin( useTransformSkip , m_cTransformSkipSCModel.get( 0, toChannelType(component), 0 )
1226      RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(TComCodingStatisticsClassType(STATS__CABAC_BITS__TRANSFORM_SKIP_FLAGS, component))
1227                          );
1228
1229#if !NH_MV_ENC_DEC_TRAC
1230  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1231  DTRACE_CABAC_T("\tparseTransformSkip()");
1232  DTRACE_CABAC_T( "\tsymbol=" )
1233  DTRACE_CABAC_V( useTransformSkip )
1234  DTRACE_CABAC_T( "\tAddr=" )
1235  DTRACE_CABAC_V( pcCU->getCtuRsAddr() )
1236  DTRACE_CABAC_T( "\tetype=" )
1237  DTRACE_CABAC_V( component )
1238  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1239  DTRACE_CABAC_V( rTu.GetAbsPartIdxTU() )
1240  DTRACE_CABAC_T( "\n" )
1241#endif
1242
1243  pcCU->setTransformSkipPartRange( useTransformSkip, component, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts(component));
1244}
1245
1246
1247/** Parse (X,Y) position of the last significant coefficient
1248 * \param uiPosLastX reference to X component of last coefficient
1249 * \param uiPosLastY reference to Y component of last coefficient
1250 * \param width  Block width
1251 * \param height Block height
1252 * \param component chroma compinent ID
1253 * \param uiScanIdx scan type (zig-zag, hor, ver)
1254 *
1255 * This method decodes the X and Y component within a block of the last significant coefficient.
1256 */
1257Void TDecSbac::parseLastSignificantXY( UInt& uiPosLastX, UInt& uiPosLastY, Int width, Int height, ComponentID component, UInt uiScanIdx )
1258{
1259  UInt uiLast;
1260
1261  ContextModel *pCtxX = m_cCuCtxLastX.get( 0, toChannelType(component) );
1262  ContextModel *pCtxY = m_cCuCtxLastY.get( 0, toChannelType(component) );
1263
1264#if RExt__DECODER_DEBUG_BIT_STATISTICS
1265  TComCodingStatisticsClassType ctype(STATS__CABAC_BITS__LAST_SIG_X_Y, g_aucConvertToBit[width]+2, component);
1266#endif
1267
1268
1269  if ( uiScanIdx == SCAN_VER )
1270  {
1271    swap( width, height );
1272  }
1273
1274  Int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
1275  getLastSignificantContextParameters(component, width, height, blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY);
1276
1277  //------------------
1278
1279  // posX
1280
1281  for( uiPosLastX = 0; uiPosLastX < g_uiGroupIdx[ width - 1 ]; uiPosLastX++ )
1282  {
1283    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxX + blkSizeOffsetX + (uiPosLastX >>shiftX) ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1284
1285    if( !uiLast )
1286    {
1287      break;
1288    }
1289  }
1290
1291  // posY
1292
1293  for( uiPosLastY = 0; uiPosLastY < g_uiGroupIdx[ height - 1 ]; uiPosLastY++ )
1294  {
1295    m_pcTDecBinIf->decodeBin( uiLast, *( pCtxY + blkSizeOffsetY + (uiPosLastY >>shiftY)) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1296
1297    if( !uiLast )
1298    {
1299      break;
1300    }
1301  }
1302
1303  // EP-coded part
1304
1305  if ( uiPosLastX > 3 )
1306  {
1307    UInt uiTemp  = 0;
1308    UInt uiCount = ( uiPosLastX - 2 ) >> 1;
1309    for ( Int i = uiCount - 1; i >= 0; i-- )
1310    {
1311      m_pcTDecBinIf->decodeBinEP( uiLast RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1312      uiTemp += uiLast << i;
1313    }
1314    uiPosLastX = g_uiMinInGroup[ uiPosLastX ] + uiTemp;
1315  }
1316  if ( uiPosLastY > 3 )
1317  {
1318    UInt uiTemp  = 0;
1319    UInt uiCount = ( uiPosLastY - 2 ) >> 1;
1320    for ( Int i = uiCount - 1; i >= 0; i-- )
1321    {
1322      m_pcTDecBinIf->decodeBinEP( uiLast RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype) );
1323      uiTemp += uiLast << i;
1324    }
1325    uiPosLastY = g_uiMinInGroup[ uiPosLastY ] + uiTemp;
1326  }
1327
1328  if( uiScanIdx == SCAN_VER )
1329  {
1330    swap( uiPosLastX, uiPosLastY );
1331  }
1332}
1333
1334Void TDecSbac::parseCoeffNxN(  TComTU &rTu, ComponentID compID )
1335{
1336  TComDataCU* pcCU=rTu.getCU();
1337  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU(compID);
1338  const TComRectangle &rRect=rTu.getRect(compID);
1339  const UInt uiWidth=rRect.width;
1340  const UInt uiHeight=rRect.height;
1341  TCoeff* pcCoef=(pcCU->getCoeff(compID)+rTu.getCoefficientOffset(compID));
1342  const TComSPS &sps=*(pcCU->getSlice()->getSPS());
1343#if !NH_MV_ENC_DEC_TRAC
1344  DTRACE_CABAC_VL( g_nSymbolCounter++ )
1345  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1346  DTRACE_CABAC_V( compID )
1347  DTRACE_CABAC_T( "\twidth=" )
1348  DTRACE_CABAC_V( uiWidth )
1349  DTRACE_CABAC_T( "\theight=" )
1350  DTRACE_CABAC_V( uiHeight )
1351  DTRACE_CABAC_T( "\tdepth=" )
1352//  DTRACE_CABAC_V( rTu.GetTransformDepthTotalAdj(compID) )
1353  DTRACE_CABAC_V( rTu.GetTransformDepthTotal() )
1354  DTRACE_CABAC_T( "\tabspartidx=" )
1355//  DTRACE_CABAC_V( uiAbsPartIdx )
1356  DTRACE_CABAC_V( rTu.GetAbsPartIdxTU(compID) )
1357  DTRACE_CABAC_T( "\ttoCU-X=" )
1358  DTRACE_CABAC_V( pcCU->getCUPelX() )
1359  DTRACE_CABAC_T( "\ttoCU-Y=" )
1360  DTRACE_CABAC_V( pcCU->getCUPelY() )
1361  DTRACE_CABAC_T( "\tCU-addr=" )
1362  DTRACE_CABAC_V(  pcCU->getCtuRsAddr() )
1363  DTRACE_CABAC_T( "\tinCU-X=" )
1364//  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1365  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] )
1366  DTRACE_CABAC_T( "\tinCU-Y=" )
1367// DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1368  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[rTu.GetAbsPartIdxTU(compID)] ] )
1369  DTRACE_CABAC_T( "\tpredmode=" )
1370  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1371  DTRACE_CABAC_T( "\n" )
1372#endif
1373
1374  //--------------------------------------------------------------------------------------------------
1375
1376  if( uiWidth > sps.getMaxTrSize() )
1377  {
1378    std::cerr << "ERROR: parseCoeffNxN was passed a TU with dimensions larger than the maximum allowed size" << std::endl;
1379    assert(false);
1380    exit(1);
1381  }
1382
1383  //--------------------------------------------------------------------------------------------------
1384
1385  //set parameters
1386
1387  const ChannelType  chType            = toChannelType(compID);
1388  const UInt         uiLog2BlockWidth  = g_aucConvertToBit[ uiWidth  ] + 2;
1389  const UInt         uiLog2BlockHeight = g_aucConvertToBit[ uiHeight ] + 2;
1390  const UInt         uiMaxNumCoeff     = uiWidth * uiHeight;
1391  const UInt         uiMaxNumCoeffM1   = uiMaxNumCoeff - 1;
1392
1393  const ChannelType  channelType       = toChannelType(compID);
1394  const Bool         extendedPrecision = sps.getSpsRangeExtension().getExtendedPrecisionProcessingFlag();
1395
1396  const Bool         alignCABACBeforeBypass = sps.getSpsRangeExtension().getCabacBypassAlignmentEnabledFlag();
1397  const Int          maxLog2TrDynamicRange  = sps.getMaxLog2TrDynamicRange(channelType);
1398
1399#if RExt__DECODER_DEBUG_BIT_STATISTICS
1400  TComCodingStatisticsClassType ctype_group(STATS__CABAC_BITS__SIG_COEFF_GROUP_FLAG, uiLog2BlockWidth, compID);
1401  TComCodingStatisticsClassType ctype_map(STATS__CABAC_BITS__SIG_COEFF_MAP_FLAG, uiLog2BlockWidth, compID);
1402  TComCodingStatisticsClassType ctype_gt1(STATS__CABAC_BITS__GT1_FLAG, uiLog2BlockWidth, compID);
1403  TComCodingStatisticsClassType ctype_gt2(STATS__CABAC_BITS__GT2_FLAG, uiLog2BlockWidth, compID);
1404#endif
1405
1406  Bool beValid;
1407  if (pcCU->getCUTransquantBypass(uiAbsPartIdx))
1408  {
1409    beValid = false;
1410    if((!pcCU->isIntra(uiAbsPartIdx)) && pcCU->isRDPCMEnabled(uiAbsPartIdx))
1411    {
1412      parseExplicitRdpcmMode(rTu, compID);
1413    }
1414  }
1415  else
1416  {
1417    beValid = pcCU->getSlice()->getPPS()->getSignHideFlag();
1418  }
1419
1420  UInt absSum = 0;
1421
1422  //--------------------------------------------------------------------------------------------------
1423
1424  if(pcCU->getSlice()->getPPS()->getUseTransformSkip())
1425  {
1426    parseTransformSkipFlags(rTu, compID);
1427    //  This TU has coefficients and is transform skipped. Check whether is inter coded and if yes decode the explicit RDPCM mode
1428    if(pcCU->getTransformSkip(uiAbsPartIdx, compID) && (!pcCU->isIntra(uiAbsPartIdx)) && pcCU->isRDPCMEnabled(uiAbsPartIdx) )
1429    {
1430      parseExplicitRdpcmMode(rTu, compID);
1431      if(pcCU->getExplicitRdpcmMode(compID, uiAbsPartIdx) != RDPCM_OFF)
1432      {
1433        //  Sign data hiding is avoided for horizontal and vertical RDPCM modes
1434        beValid = false;
1435      }
1436    }
1437  }
1438
1439  Int uiIntraMode = -1;
1440  const Bool       bIsLuma = isLuma(compID);
1441  Int isIntra = pcCU->isIntra(uiAbsPartIdx) ? 1 : 0;
1442  if ( isIntra && pcCU->isRDPCMEnabled(uiAbsPartIdx) )
1443  {
1444    const UInt partsPerMinCU = 1<<(2*(sps.getMaxTotalCUDepth() - sps.getLog2DiffMaxMinCodingBlockSize()));
1445    uiIntraMode = pcCU->getIntraDir( toChannelType(compID), uiAbsPartIdx );
1446    uiIntraMode = (uiIntraMode==DM_CHROMA_IDX && !bIsLuma) ? pcCU->getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, rTu.GetChromaFormat(), partsPerMinCU)) : uiIntraMode;
1447    uiIntraMode = ((rTu.GetChromaFormat() == CHROMA_422) && !bIsLuma) ? g_chroma422IntraAngleMappingTable[uiIntraMode] : uiIntraMode;
1448
1449    Bool transformSkip = pcCU->getTransformSkip( uiAbsPartIdx,compID);
1450    Bool rdpcm_lossy = ( transformSkip /*&& isIntra*/ && ( (uiIntraMode == HOR_IDX) || (uiIntraMode == VER_IDX) ) );
1451    if ( rdpcm_lossy )
1452    {
1453      beValid = false;
1454    }
1455  }
1456
1457  //--------------------------------------------------------------------------------------------------
1458
1459  const Bool  bUseGolombRiceParameterAdaptation = sps.getSpsRangeExtension().getPersistentRiceAdaptationEnabledFlag();
1460        UInt &currentGolombRiceStatistic        = m_golombRiceAdaptationStatistics[rTu.getGolombRiceStatisticsIndex(compID)];
1461
1462  //select scans
1463  TUEntropyCodingParameters codingParameters;
1464  getTUEntropyCodingParameters(codingParameters, rTu, compID);
1465
1466  //===== decode last significant =====
1467  UInt uiPosLastX, uiPosLastY;
1468  parseLastSignificantXY( uiPosLastX, uiPosLastY, uiWidth, uiHeight, compID, codingParameters.scanType );
1469  UInt uiBlkPosLast      = uiPosLastX + (uiPosLastY<<uiLog2BlockWidth);
1470  pcCoef[ uiBlkPosLast ] = 1;
1471
1472  //===== decode significance flags =====
1473  UInt uiScanPosLast;
1474  for( uiScanPosLast = 0; uiScanPosLast < uiMaxNumCoeffM1; uiScanPosLast++ )
1475  {
1476    UInt uiBlkPos = codingParameters.scan[ uiScanPosLast ];
1477    if( uiBlkPosLast == uiBlkPos )
1478    {
1479      break;
1480    }
1481  }
1482
1483  ContextModel * const baseCoeffGroupCtx = m_cCUSigCoeffGroupSCModel.get( 0, isChroma(chType) );
1484  ContextModel * const baseCtx = m_cCUSigSCModel.get( 0, 0 ) + getSignificanceMapContextOffset(compID);
1485
1486  const Int  iLastScanSet  = uiScanPosLast >> MLS_CG_SIZE;
1487  UInt c1                  = 1;
1488  UInt uiGoRiceParam       = 0;
1489
1490
1491  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
1492  memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
1493
1494  Int  iScanPosSig             = (Int) uiScanPosLast;
1495  for( Int iSubSet = iLastScanSet; iSubSet >= 0; iSubSet-- )
1496  {
1497    Int  iSubPos   = iSubSet << MLS_CG_SIZE;
1498    uiGoRiceParam  = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR;
1499    Bool updateGolombRiceStatistics = bUseGolombRiceParameterAdaptation; //leave the statistics at 0 when not using the adaptation system
1500    Int numNonZero = 0;
1501
1502    Int lastNZPosInCG  = -1;
1503    Int firstNZPosInCG = 1 << MLS_CG_SIZE;
1504
1505    Bool escapeDataPresentInGroup = false;
1506
1507    Int pos[1 << MLS_CG_SIZE];
1508
1509    if( iScanPosSig == (Int) uiScanPosLast )
1510    {
1511      lastNZPosInCG  = iScanPosSig;
1512      firstNZPosInCG = iScanPosSig;
1513      iScanPosSig--;
1514      pos[ numNonZero ] = uiBlkPosLast;
1515      numNonZero = 1;
1516    }
1517
1518    // decode significant_coeffgroup_flag
1519    Int iCGBlkPos = codingParameters.scanCG[ iSubSet ];
1520    Int iCGPosY   = iCGBlkPos / codingParameters.widthInGroups;
1521    Int iCGPosX   = iCGBlkPos - (iCGPosY * codingParameters.widthInGroups);
1522
1523    if( iSubSet == iLastScanSet || iSubSet == 0)
1524    {
1525      uiSigCoeffGroupFlag[ iCGBlkPos ] = 1;
1526    }
1527    else
1528    {
1529      UInt uiSigCoeffGroup;
1530      UInt uiCtxSig  = TComTrQuant::getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups );
1531      m_pcTDecBinIf->decodeBin( uiSigCoeffGroup, baseCoeffGroupCtx[ uiCtxSig ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_group) );
1532      uiSigCoeffGroupFlag[ iCGBlkPos ] = uiSigCoeffGroup;
1533    }
1534
1535    // decode significant_coeff_flag
1536    const Int patternSigCtx = TComTrQuant::calcPatternSigCtx(uiSigCoeffGroupFlag, iCGPosX, iCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
1537
1538    UInt uiBlkPos, uiSig, uiCtxSig;
1539    for( ; iScanPosSig >= iSubPos; iScanPosSig-- )
1540    {
1541      uiBlkPos  = codingParameters.scan[ iScanPosSig ];
1542      uiSig     = 0;
1543
1544      if( uiSigCoeffGroupFlag[ iCGBlkPos ] )
1545      {
1546        if( iScanPosSig > iSubPos || iSubSet == 0  || numNonZero )
1547        {
1548          uiCtxSig  = TComTrQuant::getSigCtxInc( patternSigCtx, codingParameters, iScanPosSig, uiLog2BlockWidth, uiLog2BlockHeight, chType );
1549          m_pcTDecBinIf->decodeBin( uiSig, baseCtx[ uiCtxSig ] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_map) );
1550        }
1551        else
1552        {
1553          uiSig = 1;
1554        }
1555      }
1556      pcCoef[ uiBlkPos ] = uiSig;
1557      if( uiSig )
1558      {
1559        pos[ numNonZero ] = uiBlkPos;
1560        numNonZero ++;
1561        if( lastNZPosInCG == -1 )
1562        {
1563          lastNZPosInCG = iScanPosSig;
1564        }
1565        firstNZPosInCG = iScanPosSig;
1566      }
1567    }
1568
1569    if( numNonZero > 0 )
1570    {
1571      Bool signHidden = ( lastNZPosInCG - firstNZPosInCG >= SBH_THRESHOLD );
1572
1573      absSum = 0;
1574
1575      const UInt uiCtxSet = getContextSetIndex(compID, iSubSet, (c1 == 0));
1576      c1 = 1;
1577      UInt uiBin;
1578
1579      ContextModel *baseCtxMod = m_cCUOneSCModel.get( 0, 0 ) + (NUM_ONE_FLAG_CTX_PER_SET * uiCtxSet);
1580
1581      Int absCoeff[1 << MLS_CG_SIZE];
1582
1583      for ( Int i = 0; i < numNonZero; i++)
1584      {
1585        absCoeff[i] = 1;
1586      }
1587      Int numC1Flag = min(numNonZero, C1FLAG_NUMBER);
1588      Int firstC2FlagIdx = -1;
1589
1590      for( Int idx = 0; idx < numC1Flag; idx++ )
1591      {
1592        m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[c1] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_gt1) );
1593        if( uiBin == 1 )
1594        {
1595          c1 = 0;
1596          if (firstC2FlagIdx == -1)
1597          {
1598            firstC2FlagIdx = idx;
1599          }
1600          else //if a greater-than-one has been encountered already this group
1601          {
1602            escapeDataPresentInGroup = true;
1603          }
1604        }
1605        else if( (c1 < 3) && (c1 > 0) )
1606        {
1607          c1++;
1608        }
1609        absCoeff[ idx ] = uiBin + 1;
1610      }
1611
1612      if (c1 == 0)
1613      {
1614        baseCtxMod = m_cCUAbsSCModel.get( 0, 0 ) + (NUM_ABS_FLAG_CTX_PER_SET * uiCtxSet);
1615        if ( firstC2FlagIdx != -1)
1616        {
1617          m_pcTDecBinIf->decodeBin( uiBin, baseCtxMod[0] RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_gt2) );
1618          absCoeff[ firstC2FlagIdx ] = uiBin + 2;
1619          if (uiBin != 0)
1620          {
1621            escapeDataPresentInGroup = true;
1622          }
1623        }
1624      }
1625
1626      escapeDataPresentInGroup = escapeDataPresentInGroup || (numNonZero > C1FLAG_NUMBER);
1627
1628      const Bool alignGroup = escapeDataPresentInGroup && alignCABACBeforeBypass;
1629
1630#if RExt__DECODER_DEBUG_BIT_STATISTICS
1631      TComCodingStatisticsClassType ctype_signs((alignGroup ? STATS__CABAC_BITS__ALIGNED_SIGN_BIT    : STATS__CABAC_BITS__SIGN_BIT   ), uiLog2BlockWidth, compID);
1632      TComCodingStatisticsClassType ctype_escs ((alignGroup ? STATS__CABAC_BITS__ALIGNED_ESCAPE_BITS : STATS__CABAC_BITS__ESCAPE_BITS), uiLog2BlockWidth, compID);
1633#endif
1634
1635      if (alignGroup)
1636      {
1637        m_pcTDecBinIf->align();
1638      }
1639
1640      UInt coeffSigns;
1641      if ( signHidden && beValid )
1642      {
1643        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero-1 RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_signs) );
1644        coeffSigns <<= 32 - (numNonZero-1);
1645      }
1646      else
1647      {
1648        m_pcTDecBinIf->decodeBinsEP( coeffSigns, numNonZero RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_signs) );
1649        coeffSigns <<= 32 - numNonZero;
1650      }
1651
1652      Int iFirstCoeff2 = 1;
1653      if (escapeDataPresentInGroup)
1654      {
1655        for( Int idx = 0; idx < numNonZero; idx++ )
1656        {
1657          UInt baseLevel  = (idx < C1FLAG_NUMBER)? (2 + iFirstCoeff2) : 1;
1658
1659          if( absCoeff[ idx ] == baseLevel)
1660          {
1661            UInt uiLevel;
1662            xReadCoefRemainExGolomb( uiLevel, uiGoRiceParam, extendedPrecision, maxLog2TrDynamicRange RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype_escs) );
1663
1664            absCoeff[ idx ] = uiLevel + baseLevel;
1665
1666            if (absCoeff[idx] > (3 << uiGoRiceParam))
1667            {
1668              uiGoRiceParam = bUseGolombRiceParameterAdaptation ? (uiGoRiceParam + 1) : (std::min<UInt>((uiGoRiceParam + 1), 4));
1669            }
1670
1671            if (updateGolombRiceStatistics)
1672            {
1673              const UInt initialGolombRiceParameter = currentGolombRiceStatistic / RExt__GOLOMB_RICE_INCREMENT_DIVISOR;
1674
1675              if (uiLevel >= (3 << initialGolombRiceParameter))
1676              {
1677                currentGolombRiceStatistic++;
1678              }
1679              else if (((uiLevel * 2) < (1 << initialGolombRiceParameter)) && (currentGolombRiceStatistic > 0))
1680              {
1681                currentGolombRiceStatistic--;
1682              }
1683
1684              updateGolombRiceStatistics = false;
1685            }
1686          }
1687
1688          if(absCoeff[ idx ] >= 2)
1689          {
1690            iFirstCoeff2 = 0;
1691          }
1692        }
1693      }
1694
1695      for( Int idx = 0; idx < numNonZero; idx++ )
1696      {
1697        Int blkPos = pos[ idx ];
1698        // Signs applied later.
1699        pcCoef[ blkPos ] = absCoeff[ idx ];
1700        absSum += absCoeff[ idx ];
1701
1702        if ( idx == numNonZero-1 && signHidden && beValid )
1703        {
1704          // Infer sign of 1st element.
1705          if (absSum&0x1)
1706          {
1707            pcCoef[ blkPos ] = -pcCoef[ blkPos ];
1708          }
1709        }
1710        else
1711        {
1712          Int sign = static_cast<Int>( coeffSigns ) >> 31;
1713          pcCoef[ blkPos ] = ( pcCoef[ blkPos ] ^ sign ) - sign;
1714          coeffSigns <<= 1;
1715        }
1716      }
1717    }
1718  }
1719
1720#if ENVIRONMENT_VARIABLE_DEBUG_AND_TEST
1721  printSBACCoeffData(uiPosLastX, uiPosLastY, uiWidth, uiHeight, compID, uiAbsPartIdx, codingParameters.scanType, pcCoef);
1722#endif
1723
1724  return;
1725}
1726
1727Void TDecSbac::parseSaoMaxUvlc ( UInt& val, UInt maxSymbol )
1728{
1729  if (maxSymbol == 0)
1730  {
1731    val = 0;
1732    return;
1733  }
1734
1735  UInt code;
1736  Int  i;
1737  m_pcTDecBinIf->decodeBinEP( code RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1738  if ( code == 0 )
1739  {
1740    val = 0;
1741    return;
1742  }
1743
1744  i=1;
1745  while (1)
1746  {
1747    m_pcTDecBinIf->decodeBinEP( code RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1748    if ( code == 0 )
1749    {
1750      break;
1751    }
1752    i++;
1753    if (i == maxSymbol)
1754    {
1755      break;
1756    }
1757  }
1758
1759  val = i;
1760}
1761
1762Void TDecSbac::parseSaoUflc (UInt uiLength, UInt&  riVal)
1763{
1764  m_pcTDecBinIf->decodeBinsEP ( riVal, uiLength RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1765}
1766
1767Void TDecSbac::parseSaoMerge (UInt&  ruiVal)
1768{
1769  UInt uiCode;
1770  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoMergeSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1771  ruiVal = (Int)uiCode;
1772}
1773
1774Void TDecSbac::parseSaoTypeIdx (UInt&  ruiVal)
1775{
1776  UInt uiCode;
1777  m_pcTDecBinIf->decodeBin( uiCode, m_cSaoTypeIdxSCModel.get( 0, 0, 0 ) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1778  if (uiCode == 0)
1779  {
1780    ruiVal = 0;
1781  }
1782  else
1783  {
1784    m_pcTDecBinIf->decodeBinEP( uiCode RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1785    if (uiCode == 0)
1786    {
1787      ruiVal = 1;
1788    }
1789    else
1790    {
1791      ruiVal = 2;
1792    }
1793  }
1794}
1795
1796Void TDecSbac::parseSaoSign(UInt& val)
1797{
1798  m_pcTDecBinIf->decodeBinEP ( val RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(STATS__CABAC_BITS__SAO) );
1799}
1800
1801Void TDecSbac::parseSAOBlkParam (SAOBlkParam& saoBlkParam
1802                                , Bool* sliceEnabled
1803                                , Bool leftMergeAvail
1804                                , Bool aboveMergeAvail
1805                                , const BitDepths &bitDepths
1806                                )
1807{
1808  UInt uiSymbol;
1809
1810  Bool isLeftMerge = false;
1811  Bool isAboveMerge= false;
1812
1813  if(leftMergeAvail)
1814  {
1815    parseSaoMerge(uiSymbol); //sao_merge_left_flag
1816    isLeftMerge = (uiSymbol?true:false);
1817  }
1818
1819  if( aboveMergeAvail && !isLeftMerge)
1820  {
1821    parseSaoMerge(uiSymbol); //sao_merge_up_flag
1822    isAboveMerge = (uiSymbol?true:false);
1823  }
1824
1825  if(isLeftMerge || isAboveMerge) //merge mode
1826  {
1827    for (UInt componentIndex = 0; componentIndex < MAX_NUM_COMPONENT; componentIndex++)
1828    {
1829      saoBlkParam[componentIndex].modeIdc = (sliceEnabled[componentIndex]) ? SAO_MODE_MERGE : SAO_MODE_OFF;
1830      saoBlkParam[componentIndex].typeIdc = (isLeftMerge)?SAO_MERGE_LEFT:SAO_MERGE_ABOVE;
1831    }
1832  }
1833  else //new or off mode
1834  {
1835    for(Int compId=COMPONENT_Y; compId < MAX_NUM_COMPONENT; compId++)
1836    {
1837      const ComponentID compIdx=ComponentID(compId);
1838      const ComponentID firstCompOfChType = getFirstComponentOfChannel(toChannelType(compIdx));
1839      SAOOffset& ctbParam = saoBlkParam[compIdx];
1840#if O0043_BEST_EFFORT_DECODING
1841      const Int bitDepthOrig = bitDepths.stream[toChannelType(compIdx)];
1842      const Int forceBitDepthAdjust = bitDepthOrig - bitDepths.recon[toChannelType(compIdx)];
1843#else
1844      const Int bitDepthOrig = bitDepths.recon[toChannelType(compIdx)];
1845#endif
1846      const Int maxOffsetQVal=TComSampleAdaptiveOffset::getMaxOffsetQVal(bitDepthOrig);
1847      if(!sliceEnabled[compIdx])
1848      {
1849        //off
1850        ctbParam.modeIdc = SAO_MODE_OFF;
1851        continue;
1852      }
1853
1854      //type
1855      if(compIdx == firstCompOfChType)
1856      {
1857        parseSaoTypeIdx(uiSymbol); //sao_type_idx_luma or sao_type_idx_chroma
1858
1859        assert(uiSymbol ==0 || uiSymbol ==1 || uiSymbol ==2);
1860
1861        if(uiSymbol ==0) //OFF
1862        {
1863          ctbParam.modeIdc = SAO_MODE_OFF;
1864        }
1865        else if(uiSymbol == 1) //BO
1866        {
1867          ctbParam.modeIdc = SAO_MODE_NEW;
1868          ctbParam.typeIdc = SAO_TYPE_START_BO;
1869        }
1870        else //2, EO
1871        {
1872          ctbParam.modeIdc = SAO_MODE_NEW;
1873          ctbParam.typeIdc = SAO_TYPE_START_EO;
1874        }
1875
1876      }
1877      else //Cr, follow Cb SAO type
1878      {
1879        ctbParam.modeIdc = saoBlkParam[COMPONENT_Cb].modeIdc;
1880        ctbParam.typeIdc = saoBlkParam[COMPONENT_Cb].typeIdc;
1881      }
1882
1883      if(ctbParam.modeIdc == SAO_MODE_NEW)
1884      {
1885        Int offset[4];
1886        for(Int i=0; i< 4; i++)
1887        {
1888          parseSaoMaxUvlc(uiSymbol, maxOffsetQVal ); //sao_offset_abs
1889          offset[i] = (Int)uiSymbol;
1890        }
1891
1892        if(ctbParam.typeIdc == SAO_TYPE_START_BO)
1893        {
1894          for(Int i=0; i< 4; i++)
1895          {
1896            if(offset[i] != 0)
1897            {
1898              parseSaoSign(uiSymbol); //sao_offset_sign
1899              if(uiSymbol)
1900              {
1901#if O0043_BEST_EFFORT_DECODING
1902                offset[i] >>= forceBitDepthAdjust;
1903#endif
1904                offset[i] = -offset[i];
1905              }
1906            }
1907          }
1908          parseSaoUflc(NUM_SAO_BO_CLASSES_LOG2, uiSymbol ); //sao_band_position
1909          ctbParam.typeAuxInfo = uiSymbol;
1910
1911          for(Int i=0; i<4; i++)
1912          {
1913            ctbParam.offset[(ctbParam.typeAuxInfo+i)%MAX_NUM_SAO_CLASSES] = offset[i];
1914          }
1915
1916        }
1917        else //EO
1918        {
1919          ctbParam.typeAuxInfo = 0;
1920
1921          if(firstCompOfChType == compIdx)
1922          {
1923            parseSaoUflc(NUM_SAO_EO_TYPES_LOG2, uiSymbol ); //sao_eo_class_luma or sao_eo_class_chroma
1924            ctbParam.typeIdc += uiSymbol;
1925          }
1926          else
1927          {
1928            ctbParam.typeIdc = saoBlkParam[firstCompOfChType].typeIdc;
1929          }
1930          ctbParam.offset[SAO_CLASS_EO_FULL_VALLEY] = offset[0];
1931          ctbParam.offset[SAO_CLASS_EO_HALF_VALLEY] = offset[1];
1932          ctbParam.offset[SAO_CLASS_EO_PLAIN      ] = 0;
1933          ctbParam.offset[SAO_CLASS_EO_HALF_PEAK  ] = -offset[2];
1934          ctbParam.offset[SAO_CLASS_EO_FULL_PEAK  ] = -offset[3];
1935        }
1936      }
1937    }
1938  }
1939}
1940
1941/**
1942 - Initialize our contexts from the nominated source.
1943 .
1944 \param pSrc Contexts to be copied.
1945 */
1946Void TDecSbac::xCopyContextsFrom( const TDecSbac* pSrc )
1947{
1948  memcpy(m_contextModels, pSrc->m_contextModels, m_numContextModels*sizeof(m_contextModels[0]));
1949  memcpy(m_golombRiceAdaptationStatistics, pSrc->m_golombRiceAdaptationStatistics, (sizeof(UInt) * RExt__GOLOMB_RICE_ADAPTATION_STATISTICS_SETS));
1950}
1951
1952Void TDecSbac::xCopyFrom( const TDecSbac* pSrc )
1953{
1954  m_pcTDecBinIf->copyState( pSrc->m_pcTDecBinIf );
1955  xCopyContextsFrom( pSrc );
1956}
1957
1958Void TDecSbac::load ( const TDecSbac* pSrc )
1959{
1960  xCopyFrom(pSrc);
1961}
1962
1963Void TDecSbac::loadContexts ( const TDecSbac* pSrc )
1964{
1965  xCopyContextsFrom(pSrc);
1966}
1967
1968/** Performs CABAC decoding of the explicit RDPCM mode
1969 * \param rTu current TU data structure
1970 * \param compID component identifier
1971 */
1972Void TDecSbac::parseExplicitRdpcmMode( TComTU &rTu, ComponentID compID )
1973{
1974  TComDataCU* cu = rTu.getCU();
1975  const UInt absPartIdx=rTu.GetAbsPartIdxTU(compID);
1976  const TComRectangle &rect = rTu.getRect(compID);
1977  const UInt tuHeight = g_aucConvertToBit[rect.height];
1978  const UInt tuWidth  = g_aucConvertToBit[rect.width];
1979  UInt code = 0;
1980
1981  assert(tuHeight == tuWidth);
1982
1983#if RExt__DECODER_DEBUG_BIT_STATISTICS
1984  const TComCodingStatisticsClassType ctype(STATS__EXPLICIT_RDPCM_BITS, g_aucConvertToBit[cu->getSlice()->getSPS()->getMaxCUWidth()>>rTu.GetTransformDepthTotal()]+2);
1985#endif
1986
1987  m_pcTDecBinIf->decodeBin(code, m_explicitRdpcmFlagSCModel.get (0, toChannelType(compID), 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
1988
1989  if(code == 0)
1990  {
1991    cu->setExplicitRdpcmModePartRange( RDPCM_OFF, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
1992  }
1993  else
1994  {
1995    m_pcTDecBinIf->decodeBin(code, m_explicitRdpcmDirSCModel.get (0, toChannelType(compID), 0) RExt__DECODER_DEBUG_BIT_STATISTICS_PASS_OPT_ARG(ctype));
1996    if(code == 0)
1997    {
1998      cu->setExplicitRdpcmModePartRange( RDPCM_HOR, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
1999    }
2000    else
2001    {
2002      cu->setExplicitRdpcmModePartRange( RDPCM_VER, compID, absPartIdx, rTu.GetAbsPartIdxNumParts(compID));
2003    }
2004  }
2005}
2006
2007
2008
2009
2010
2011
2012//! \}
Note: See TracBrowser for help on using the repository browser.