source: 3DVCSoftware/branches/0.3-poznan-univ/source/Lib/TLibEncoder/TEncSbac.cpp @ 41

Last change on this file since 41 was 41, checked in by poznan-univ, 13 years ago

Adjustment for FlexCO, and high-level syntax improvement.

  • Property svn:eol-style set to native
File size: 76.4 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-2011, 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 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
35
36/** \file     TEncSbac.cpp
37    \brief    SBAC encoder class
38*/
39
40#include "TEncTop.h"
41#include "TEncSbac.h"
42
43#include <map>
44
45extern UChar  stateMappingTable[113];
46extern Int entropyBits[128];
47
48// ====================================================================================================================
49// Constructor / destructor / create / destroy
50// ====================================================================================================================
51
52TEncSbac::TEncSbac()
53// new structure here
54: m_pcBitIf                   ( NULL )
55, m_pcSlice                   ( NULL )
56, m_pcBinIf                   ( NULL )
57, m_bAlfCtrl                  ( false )
58, m_uiCoeffCost               ( 0 )
59, m_uiMaxAlfCtrlDepth         ( 0 )
60, m_cCUSplitFlagSCModel       ( 1,             1,               NUM_SPLIT_FLAG_CTX            )
61, m_cCUSkipFlagSCModel        ( 1,             1,               NUM_SKIP_FLAG_CTX             )
62, m_cCUMergeFlagExtSCModel    ( 1,             1,               NUM_MERGE_FLAG_EXT_CTX        )
63, m_cCUMergeIdxExtSCModel     ( 1,             1,               NUM_MERGE_IDX_EXT_CTX         )
64, m_cCUMVMergeIdxExtSCModel   ( 1,             1,               NUM_MV_MERGE_IDX_EXT_CTX      )
65, m_cResPredFlagSCModel       ( 1,             1,               NUM_RES_PRED_FLAG_CTX         )
66, m_cCUPartSizeSCModel        ( 1,             1,               NUM_PART_SIZE_CTX             )
67, m_cCUPredModeSCModel        ( 1,             1,               NUM_PRED_MODE_CTX             )
68, m_cCUAlfCtrlFlagSCModel     ( 1,             1,               NUM_ALF_CTRL_FLAG_CTX         )
69, m_cCUIntraPredSCModel       ( 1,             1,               NUM_ADI_CTX                   )
70#if ADD_PLANAR_MODE
71, m_cPlanarFlagSCModel        ( 1,             1,               NUM_PLANARFLAG_CTX            )
72#endif
73, m_cCUChromaPredSCModel      ( 1,             1,               NUM_CHROMA_PRED_CTX           )
74, m_cCUDeltaQpSCModel         ( 1,             1,               NUM_DELTA_QP_CTX              )
75, m_cCUInterDirSCModel        ( 1,             1,               NUM_INTER_DIR_CTX             )
76, m_cCURefPicSCModel          ( 1,             1,               NUM_REF_NO_CTX                )
77, m_cCUMvdSCModel             ( 1,             2,               NUM_MV_RES_CTX                )
78, m_cCUQtCbfSCModel           ( 1,             3,               NUM_QT_CBF_CTX                )
79, m_cCUTransSubdivFlagSCModel ( 1,             1,               NUM_TRANS_SUBDIV_FLAG_CTX     )
80, m_cCUQtRootCbfSCModel       ( 1,             1,               NUM_QT_ROOT_CBF_CTX           )
81#if SIMPLE_CONTEXT_SIG
82, m_cCUSigSCModel             ( 4,             2,               NUM_SIG_FLAG_CTX              )
83#else
84, m_cCUSigSCModel             ( MAX_CU_DEPTH,  2,               NUM_SIG_FLAG_CTX              )
85#endif
86#if PCP_SIGMAP_SIMPLE_LAST
87, m_cCuCtxLastX               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          )
88, m_cCuCtxLastY               ( 1,             2,               NUM_CTX_LAST_FLAG_XY          )
89#else
90, m_cCULastSCModel            ( MAX_CU_DEPTH,  2,               NUM_LAST_FLAG_CTX             )
91#endif
92, m_cCUOneSCModel             ( 1,             2,               NUM_ONE_FLAG_CTX              )
93, m_cCUAbsSCModel             ( 1,             2,               NUM_ABS_FLAG_CTX              )
94, m_cMVPIdxSCModel            ( 1,             1,               NUM_MVP_IDX_CTX               )
95, m_cALFFlagSCModel           ( 1,             1,               NUM_ALF_FLAG_CTX              )
96, m_cALFUvlcSCModel           ( 1,             1,               NUM_ALF_UVLC_CTX              )
97, m_cALFSvlcSCModel           ( 1,             1,               NUM_ALF_SVLC_CTX              )
98#if MTK_SAO
99, m_cAOFlagSCModel            ( 1,             1,               NUM_AO_FLAG_CTX              )
100, m_cAOUvlcSCModel            ( 1,             1,               NUM_AO_UVLC_CTX              )
101, m_cAOSvlcSCModel            ( 1,             1,               NUM_AO_SVLC_CTX              )
102#endif
103, m_cViewIdxSCModel           ( 1,             1,               NUM_VIEW_IDX_CTX              )
104#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
105, m_cIntraDMMSCModel          ( 1,             1,               NUM_DMM_CTX                   )
106, m_cIntraWedgeSCModel        ( 1,             1,               NUM_WEDGE_CTX                 )
107#endif
108{
109
110}
111
112TEncSbac::~TEncSbac()
113{
114}
115
116// ====================================================================================================================
117// Public member functions
118// ====================================================================================================================
119
120Void TEncSbac::resetEntropy           ()
121{
122  Int  iQp              = m_pcSlice->getSliceQp();
123  SliceType eSliceType  = m_pcSlice->getSliceType();
124 
125  m_cCUSplitFlagSCModel.initBuffer       ( eSliceType, iQp, (Short*)INIT_SPLIT_FLAG );
126 
127  m_cCUSkipFlagSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_SKIP_FLAG );
128  m_cCUAlfCtrlFlagSCModel.initBuffer     ( eSliceType, iQp, (Short*)INIT_ALF_CTRL_FLAG );
129  m_cCUMergeFlagExtSCModel.initBuffer    ( eSliceType, iQp, (Short*)INIT_MERGE_FLAG_EXT);
130  m_cCUMergeIdxExtSCModel.initBuffer     ( eSliceType, iQp, (Short*)INIT_MERGE_IDX_EXT);
131  m_cCUMVMergeIdxExtSCModel.initBuffer   ( eSliceType, iQp, (Short*)INIT_MV_MERGE_IDX_EXT );
132  m_cResPredFlagSCModel.initBuffer       ( eSliceType, iQp, (Short*)INIT_RES_PRED_FLAG );
133  m_cCUPartSizeSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_PART_SIZE );
134  m_cCUPredModeSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_PRED_MODE );
135  m_cCUIntraPredSCModel.initBuffer       ( eSliceType, iQp, (Short*)INIT_INTRA_PRED_MODE );
136#if ADD_PLANAR_MODE
137  m_cPlanarFlagSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_PLANARFLAG );
138#endif
139  m_cCUChromaPredSCModel.initBuffer      ( eSliceType, iQp, (Short*)INIT_CHROMA_PRED_MODE );
140  m_cCUInterDirSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_INTER_DIR );
141  m_cCUMvdSCModel.initBuffer             ( eSliceType, iQp, (Short*)INIT_MVD );
142  m_cCURefPicSCModel.initBuffer          ( eSliceType, iQp, (Short*)INIT_REF_PIC );
143  m_cCUDeltaQpSCModel.initBuffer         ( eSliceType, iQp, (Short*)INIT_DQP );
144  m_cCUQtCbfSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_QT_CBF );
145  m_cCUQtRootCbfSCModel.initBuffer       ( eSliceType, iQp, (Short*)INIT_QT_ROOT_CBF );
146  m_cCUSigSCModel.initBuffer             ( eSliceType, iQp, (Short*)INIT_SIG_FLAG );
147#if PCP_SIGMAP_SIMPLE_LAST
148  m_cCuCtxLastX.initBuffer               ( eSliceType, iQp, (Short*)INIT_LAST_X );
149  m_cCuCtxLastY.initBuffer               ( eSliceType, iQp, (Short*)INIT_LAST_Y );
150#else
151  m_cCULastSCModel.initBuffer            ( eSliceType, iQp, (Short*)INIT_LAST_FLAG );
152#endif
153  m_cCUOneSCModel.initBuffer             ( eSliceType, iQp, (Short*)INIT_ONE_FLAG );
154  m_cCUAbsSCModel.initBuffer             ( eSliceType, iQp, (Short*)INIT_ABS_FLAG );
155  m_cMVPIdxSCModel.initBuffer            ( eSliceType, iQp, (Short*)INIT_MVP_IDX );
156  m_cALFFlagSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_ALF_FLAG );
157  m_cALFUvlcSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_ALF_UVLC );
158  m_cALFSvlcSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_ALF_SVLC );
159  m_cCUTransSubdivFlagSCModel.initBuffer ( eSliceType, iQp, (Short*)INIT_TRANS_SUBDIV_FLAG );
160#if MTK_SAO
161  m_cAOFlagSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_AO_FLAG );
162  m_cAOUvlcSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_AO_UVLC );
163  m_cAOSvlcSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_AO_SVLC );
164#endif
165  m_cViewIdxSCModel.initBuffer           ( eSliceType, iQp, (Short*)INIT_VIEW_IDX );
166#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
167  m_cIntraDMMSCModel.initBuffer          ( eSliceType, iQp, (Short*)INIT_INTRA_DMM );
168  m_cIntraWedgeSCModel.initBuffer        ( eSliceType, iQp, (Short*)INIT_INTRA_WEDGELET );
169#endif
170
171  // new structure
172  m_uiLastQp = iQp;
173 
174  m_pcBinIf->start();
175 
176  return;
177}
178
179Void TEncSbac::codeNALUnitHeader( NalUnitType eNalUnitType, NalRefIdc eNalRefIdc, UInt TemporalId, Bool bOutputFlag )
180{
181  assert (0);
182  return;
183}
184
185void TEncSbac::codeSEI(const SEI&)
186{
187  assert(0);
188}
189
190Void TEncSbac::codeSPS( TComSPS* pcSPS )
191{
192  assert (0);
193  return;
194}
195
196Void TEncSbac::codePPS( TComPPS* pcPPS )
197{
198  assert (0);
199  return;
200}
201
202Void TEncSbac::codeSliceHeader( TComSlice* pcSlice )
203{
204  assert (0);
205  return;
206}
207
208Void TEncSbac::codeTerminatingBit( UInt uilsLast )
209{
210  m_pcBinIf->encodeBinTrm( uilsLast );
211}
212
213Void TEncSbac::codeSliceFinish()
214{
215  m_pcBinIf->finish();
216}
217
218
219
220Void TEncSbac::xWriteUnarySymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset )
221{
222  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[0] );
223 
224  if( 0 == uiSymbol)
225  {
226    return;
227  }
228 
229  while( uiSymbol-- )
230  {
231    m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ iOffset ] );
232  }
233 
234  return;
235}
236
237Void TEncSbac::xWriteUnaryMaxSymbol( UInt uiSymbol, ContextModel* pcSCModel, Int iOffset, UInt uiMaxSymbol )
238{
239  if (uiMaxSymbol == 0)
240  {
241    return;
242  }
243 
244  m_pcBinIf->encodeBin( uiSymbol ? 1 : 0, pcSCModel[ 0 ] );
245 
246  if ( uiSymbol == 0 )
247  {
248    return;
249  }
250 
251  Bool bCodeLast = ( uiMaxSymbol > uiSymbol );
252 
253  while( --uiSymbol )
254  {
255    m_pcBinIf->encodeBin( 1, pcSCModel[ iOffset ] );
256  }
257  if( bCodeLast )
258  {
259    m_pcBinIf->encodeBin( 0, pcSCModel[ iOffset ] );
260  }
261 
262  return;
263}
264
265Void TEncSbac::xWriteEpExGolomb( UInt uiSymbol, UInt uiCount )
266{
267  while( uiSymbol >= (UInt)(1<<uiCount) )
268  {
269    m_pcBinIf->encodeBinEP( 1 );
270    uiSymbol -= 1<<uiCount;
271    uiCount  ++;
272  }
273  m_pcBinIf->encodeBinEP( 0 );
274  while( uiCount-- )
275  {
276    m_pcBinIf->encodeBinEP( (uiSymbol>>uiCount) & 1 );
277  }
278 
279  return;
280}
281
282#if E253
283/** Coding of coeff_abs_level_minus3
284 * \param uiSymbol value of coeff_abs_level_minus3
285 * \param ruiGoRiceParam reference to Rice parameter
286 * \returns Void
287 */
288Void TEncSbac::xWriteGoRiceExGolomb( UInt uiSymbol, UInt &ruiGoRiceParam )
289{
290  UInt uiCount      = 0;
291  UInt uiMaxVlc     = g_auiGoRiceRange[ ruiGoRiceParam ];
292  Bool bExGolomb    = ( uiSymbol > uiMaxVlc );
293  UInt uiCodeWord   = min<UInt>( uiSymbol, ( uiMaxVlc + 1 ) );
294  UInt uiQuotient   = uiCodeWord >> ruiGoRiceParam;
295  UInt uiUnaryPart  = uiQuotient;
296  UInt uiRemainder  = 1;
297  UInt uiMaxPreLen  = g_auiGoRicePrefixLen[ ruiGoRiceParam ];
298
299  if( uiUnaryPart )
300  {
301    m_pcBinIf->encodeBinEP( 1 );
302    uiCount++;
303
304    while( --uiUnaryPart && uiCount < uiMaxPreLen )
305    {
306      m_pcBinIf->encodeBinEP( 1 );
307      uiCount++;
308    }
309
310    if( uiCount < uiMaxPreLen )
311    {
312      m_pcBinIf->encodeBinEP( uiUnaryPart ? 1 : 0 );
313    }
314  }
315  else
316  {
317    m_pcBinIf->encodeBinEP( 0 );
318  }
319
320  for( UInt ui = 0; ui < ruiGoRiceParam; ui++ )
321  {
322    m_pcBinIf->encodeBinEP( ( uiRemainder & uiCodeWord ) ? 1 : 0 );
323    uiRemainder = uiRemainder << 1;
324  }
325
326  ruiGoRiceParam = g_aauiGoRiceUpdate[ ruiGoRiceParam ][ min<UInt>( uiSymbol, 15 ) ];
327
328  if( bExGolomb )
329  {
330    uiSymbol -= uiMaxVlc + 1;
331    xWriteEpExGolomb( uiSymbol, 0 );
332  }
333
334  return;
335}
336#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
337Void TEncSbac::xWriteExGolombLevel( UInt uiSymbol, ContextModel& rcSCModel  )
338{
339  if( uiSymbol )
340  {
341    m_pcBinIf->encodeBin( 1, rcSCModel );
342    UInt uiCount = 0;
343    Bool bNoExGo = (uiSymbol < 13);
344
345    while( --uiSymbol && ++uiCount < 13 )
346    {
347      m_pcBinIf->encodeBin( 1, rcSCModel );
348    }
349    if( bNoExGo )
350    {
351      m_pcBinIf->encodeBin( 0, rcSCModel );
352    }
353    else
354    {
355      xWriteEpExGolomb( uiSymbol, 0 );
356    }
357  }
358  else
359  {
360    m_pcBinIf->encodeBin( 0, rcSCModel );
361  }
362
363  return;
364}
365#endif
366#else
367Void TEncSbac::xWriteExGolombLevel( UInt uiSymbol, ContextModel& rcSCModel  )
368{
369  if( uiSymbol )
370  {
371    m_pcBinIf->encodeBin( 1, rcSCModel );
372    UInt uiCount = 0;
373    Bool bNoExGo = (uiSymbol < 13);
374   
375    while( --uiSymbol && ++uiCount < 13 )
376    {
377      m_pcBinIf->encodeBin( 1, rcSCModel );
378    }
379    if( bNoExGo )
380    {
381      m_pcBinIf->encodeBin( 0, rcSCModel );
382    }
383    else
384    {
385      xWriteEpExGolomb( uiSymbol, 0 );
386    }
387  }
388  else
389  {
390    m_pcBinIf->encodeBin( 0, rcSCModel );
391  }
392 
393  return;
394}
395#endif
396
397// SBAC RD
398Void  TEncSbac::load ( TEncSbac* pScr)
399{
400  this->xCopyFrom(pScr);
401}
402
403Void  TEncSbac::store( TEncSbac* pDest)
404{
405  pDest->xCopyFrom( this );
406}
407
408
409Void TEncSbac::xCopyFrom( TEncSbac* pSrc )
410{
411  m_pcBinIf->copyState( pSrc->m_pcBinIf );
412 
413  this->m_uiCoeffCost = pSrc->m_uiCoeffCost;
414  this->m_uiLastQp    = pSrc->m_uiLastQp;
415 
416  this->m_cCUSplitFlagSCModel      .copyFrom( &pSrc->m_cCUSplitFlagSCModel       );
417  this->m_cCUSkipFlagSCModel       .copyFrom( &pSrc->m_cCUSkipFlagSCModel        );
418  this->m_cCUMergeFlagExtSCModel  .copyFrom( &pSrc->m_cCUMergeFlagExtSCModel);
419  this->m_cCUMergeIdxExtSCModel   .copyFrom( &pSrc->m_cCUMergeIdxExtSCModel);
420  this->m_cCUMVMergeIdxExtSCModel .copyFrom( &pSrc->m_cCUMVMergeIdxExtSCModel );
421  this->m_cResPredFlagSCModel     .copyFrom( &pSrc->m_cResPredFlagSCModel        );
422  this->m_cCUPartSizeSCModel       .copyFrom( &pSrc->m_cCUPartSizeSCModel        );
423  this->m_cCUPredModeSCModel       .copyFrom( &pSrc->m_cCUPredModeSCModel        );
424  this->m_cCUIntraPredSCModel      .copyFrom( &pSrc->m_cCUIntraPredSCModel       );
425#if ADD_PLANAR_MODE
426  this->m_cPlanarFlagSCModel       .copyFrom( &pSrc->m_cPlanarFlagSCModel        );
427#endif
428  this->m_cCUChromaPredSCModel     .copyFrom( &pSrc->m_cCUChromaPredSCModel      );
429  this->m_cCUDeltaQpSCModel        .copyFrom( &pSrc->m_cCUDeltaQpSCModel         );
430  this->m_cCUInterDirSCModel       .copyFrom( &pSrc->m_cCUInterDirSCModel        );
431  this->m_cCURefPicSCModel         .copyFrom( &pSrc->m_cCURefPicSCModel          );
432  this->m_cCUMvdSCModel            .copyFrom( &pSrc->m_cCUMvdSCModel             );
433  this->m_cCUQtCbfSCModel          .copyFrom( &pSrc->m_cCUQtCbfSCModel           );
434  this->m_cCUTransSubdivFlagSCModel.copyFrom( &pSrc->m_cCUTransSubdivFlagSCModel );
435  this->m_cCUQtRootCbfSCModel      .copyFrom( &pSrc->m_cCUQtRootCbfSCModel       );
436  this->m_cCUSigSCModel            .copyFrom( &pSrc->m_cCUSigSCModel             );
437#if PCP_SIGMAP_SIMPLE_LAST
438  this->m_cCuCtxLastX              .copyFrom( &pSrc->m_cCuCtxLastX               );
439  this->m_cCuCtxLastY              .copyFrom( &pSrc->m_cCuCtxLastY               );
440#else
441  this->m_cCULastSCModel           .copyFrom( &pSrc->m_cCULastSCModel            );
442#endif
443  this->m_cCUOneSCModel            .copyFrom( &pSrc->m_cCUOneSCModel             );
444  this->m_cCUAbsSCModel            .copyFrom( &pSrc->m_cCUAbsSCModel             );
445  this->m_cMVPIdxSCModel           .copyFrom( &pSrc->m_cMVPIdxSCModel            );
446  this->m_cViewIdxSCModel          .copyFrom( &pSrc->m_cViewIdxSCModel           );
447#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
448  this->m_cIntraDMMSCModel         .copyFrom( &pSrc->m_cIntraDMMSCModel          );
449  this->m_cIntraWedgeSCModel       .copyFrom( &pSrc->m_cIntraWedgeSCModel        );
450#endif
451}
452
453Void TEncSbac::codeMVPIdx ( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
454{
455  Int iSymbol = pcCU->getMVPIdx(eRefList, uiAbsPartIdx);
456  Int iNum    = pcCU->getMVPNum(eRefList, uiAbsPartIdx);
457
458  xWriteUnaryMaxSymbol(iSymbol, m_cMVPIdxSCModel.get(0), 1, iNum-1);
459}
460
461Void TEncSbac::codePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
462{
463  PartSize eSize         = pcCU->getPartitionSize( uiAbsPartIdx );
464 
465  if ( pcCU->getSlice()->isInterB() && pcCU->isIntra( uiAbsPartIdx ) )
466  {
467    m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
468#if HHI_RMP_SWITCH
469    if( pcCU->getSlice()->getSPS()->getUseRMP() )
470#endif
471    {
472      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
473      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
474    }
475#if HHI_DISABLE_INTER_NxN_SPLIT
476    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
477#endif
478    {
479      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 3) );
480    }
481#if MTK_DISABLE_INTRA_NxN_SPLIT
482    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
483#endif
484    {
485      m_pcBinIf->encodeBin( (eSize == SIZE_2Nx2N? 0 : 1), m_cCUPartSizeSCModel.get( 0, 0, 4) );
486    }
487    return;
488  }
489 
490  if ( pcCU->isIntra( uiAbsPartIdx ) )
491  {
492#if MTK_DISABLE_INTRA_NxN_SPLIT
493    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
494#endif
495    {
496      m_pcBinIf->encodeBin( eSize == SIZE_2Nx2N? 1 : 0, m_cCUPartSizeSCModel.get( 0, 0, 0 ) );
497    }
498    return;
499  }
500 
501  switch(eSize)
502  {
503    case SIZE_2Nx2N:
504    {
505      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 0) );
506      break;
507    }
508    case SIZE_2NxN:
509    {
510      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
511      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 1) );
512     
513      break;
514    }
515    case SIZE_Nx2N:
516    {
517      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
518      m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
519      m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 2) );
520     
521      break;
522    }
523    case SIZE_NxN:
524    {
525#if HHI_DISABLE_INTER_NxN_SPLIT
526      if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
527#endif
528      {
529        m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 0) );
530#if HHI_RMP_SWITCH
531        if( pcCU->getSlice()->getSPS()->getUseRMP() )
532#endif
533        {
534          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 1) );
535          m_pcBinIf->encodeBin( 0, m_cCUPartSizeSCModel.get( 0, 0, 2) );
536        }
537       
538        if (pcCU->getSlice()->isInterB())
539        {
540          m_pcBinIf->encodeBin( 1, m_cCUPartSizeSCModel.get( 0, 0, 3) );
541        }
542      }
543      break;
544    }
545    default:
546    {
547      assert(0);
548    }
549  }
550}
551
552/** code prediction mode
553 * \param pcCU
554 * \param uiAbsPartIdx 
555 * \returns Void
556 */
557Void TEncSbac::codePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx )
558{
559  // get context function is here
560  Int iPredMode = pcCU->getPredictionMode( uiAbsPartIdx );
561  if (pcCU->getSlice()->isInterB() )
562  {
563    return;
564  }
565
566  m_pcBinIf->encodeBin( iPredMode == MODE_INTER ? 0 : 1, m_cCUPredModeSCModel.get( 0, 0, 1 ) );
567}
568
569Void TEncSbac::codeAlfCtrlFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
570{
571  if (!m_bAlfCtrl)
572    return;
573 
574  if( pcCU->getDepth(uiAbsPartIdx) > m_uiMaxAlfCtrlDepth && !pcCU->isFirstAbsZorderIdxInDepth(uiAbsPartIdx, m_uiMaxAlfCtrlDepth))
575  {
576    return;
577  }
578 
579  // get context function is here
580  UInt uiSymbol = pcCU->getAlfCtrlFlag( uiAbsPartIdx ) ? 1 : 0;
581 
582  m_pcBinIf->encodeBin( uiSymbol, m_cCUAlfCtrlFlagSCModel.get( 0, 0, pcCU->getCtxAlfCtrlFlag( uiAbsPartIdx) ) );
583}
584
585Void TEncSbac::codeAlfCtrlDepth()
586{
587  if (!m_bAlfCtrl)
588    return;
589 
590  UInt uiDepth = m_uiMaxAlfCtrlDepth;
591  xWriteUnaryMaxSymbol(uiDepth, m_cALFUvlcSCModel.get(0), 1, g_uiMaxCUDepth-1);
592}
593
594/** code skip flag
595 * \param pcCU
596 * \param uiAbsPartIdx
597 * \returns Void
598 */
599Void TEncSbac::codeSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
600{
601  // get context function is here
602  UInt uiSymbol = pcCU->isSkipped( uiAbsPartIdx ) ? 1 : 0;
603  UInt uiCtxSkip = pcCU->getCtxSkipFlag( uiAbsPartIdx ) ;
604  m_pcBinIf->encodeBin( uiSymbol, m_cCUSkipFlagSCModel.get( 0, 0, uiCtxSkip ) );
605  DTRACE_CABAC_V( g_nSymbolCounter++ );
606  DTRACE_CABAC_T( "\tSkipFlag" );
607  DTRACE_CABAC_T( "\tuiCtxSkip: ");
608  DTRACE_CABAC_V( uiCtxSkip );
609  DTRACE_CABAC_T( "\tuiSymbol: ");
610  DTRACE_CABAC_V( uiSymbol );
611  DTRACE_CABAC_T( "\n");
612}
613
614/** code merge flag
615 * \param pcCU
616 * \param uiAbsPartIdx
617 * \returns Void
618 */
619Void TEncSbac::codeMergeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
620{
621  UInt uiCtx = 0;
622#if CHANGE_MERGE_CONTEXT
623  uiCtx = pcCU->getCtxMergeFlag( uiAbsPartIdx );
624#else
625  for(UInt uiIter = 0; uiIter < MRG_MAX_NUM_CANDS; uiIter++ )
626  {
627    if( pcCU->getNeighbourCandIdx( uiIter, uiAbsPartIdx ) == uiIter + 1 )
628    {
629      if( uiIter == 0 )
630      {
631        uiCtx++;
632      }
633      else if( uiIter == 1 )
634      {
635        uiCtx++;
636      }
637    }
638  }
639#endif
640  UInt uiSymbol = pcCU->getMergeFlag( uiAbsPartIdx ) ? 1 : 0;
641  m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeFlagExtSCModel.get( 0, 0, uiCtx ) );
642
643  DTRACE_CABAC_V( g_nSymbolCounter++ );
644  DTRACE_CABAC_T( "\tMergeFlag: " );
645  DTRACE_CABAC_V( uiSymbol );
646  DTRACE_CABAC_T( "\tAddress: " );
647  DTRACE_CABAC_V( pcCU->getAddr() );
648  DTRACE_CABAC_T( "\tuiAbsPartIdx: " );
649  DTRACE_CABAC_V( uiAbsPartIdx );
650  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
651  {
652    DTRACE_CABAC_T( "\tNumMrgCand: " );
653    DTRACE_CABAC_V( ui );
654    DTRACE_CABAC_T( "\t==\t" );
655    DTRACE_CABAC_V( UInt( pcCU->getNeighbourCandIdx( ui, uiAbsPartIdx ) ) );
656  }
657  DTRACE_CABAC_T( "\n" );
658}
659
660
661
662#if HHI_INTER_VIEW_MOTION_PRED || HHI_MPI || POZNAN_DBMP
663Void TEncSbac::codeMergeIndexMV( TComDataCU* pcCU, UInt uiAbsPartIdx )
664{
665  //--- set number of candidates and availability ---
666  Bool  abAvailable[ MRG_MAX_NUM_CANDS ];
667  UInt  uiNumCand = 0;
668#if HHI_MPI
669  const Bool bMVIAvailable = pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE;
670  const UInt uiMviMergePos = bMVIAvailable ? HHI_MPI_MERGE_POS : MRG_MAX_NUM_CANDS;
671#endif
672#if POZNAN_DBMP
673  const Bool bDBMPAvailable = pcCU->getSlice()->getMP()->isDBMPEnabled();
674#endif
675  for( UInt uiIdx = 0; uiIdx < MRG_MAX_NUM_CANDS; uiIdx++ )
676  {
677#if HHI_MPI
678    if( uiIdx == uiMviMergePos )
679    {
680      abAvailable[ uiIdx ] = true;
681      uiNumCand++;
682    }
683    else if( uiIdx > uiMviMergePos )
684    {
685      if( ( abAvailable[ uiIdx ] = ( pcCU->getNeighbourCandIdx( uiIdx - 1, uiAbsPartIdx ) == uiIdx ) ) )
686      {
687        uiNumCand++;
688      }
689    }
690    else
691#endif
692    if( ( abAvailable[ uiIdx ] = ( pcCU->getNeighbourCandIdx( uiIdx, uiAbsPartIdx ) == uiIdx + 1 ) ) )
693    {
694      uiNumCand++;
695    }
696  }
697
698#if POZNAN_DBMP
699  if(bDBMPAvailable)
700  {
701    UInt uiDBMPMergePos = POZNAN_DBMP_MERGE_POS;
702#if HHI_MPI
703        if(bMVIAvailable && uiDBMPMergePos>=uiMviMergePos) uiDBMPMergePos++;
704#endif
705        for( Int iIdx = MRG_MAX_NUM_CANDS-2; iIdx >= (Int)uiDBMPMergePos; iIdx-- )
706        {
707                Int iWrIdx = iIdx+1;
708#if HHI_MPI
709                if(bMVIAvailable) 
710                {
711                        if(iIdx==(Int)uiMviMergePos) continue;
712                        if(iWrIdx==(Int)uiMviMergePos) iWrIdx++;
713                }
714#endif
715                if(iWrIdx>=MRG_MAX_NUM_CANDS) continue;
716
717                abAvailable[ iWrIdx ] = abAvailable[ iIdx ];
718        }
719        abAvailable[ uiDBMPMergePos ] = ( pcCU->getNeighbourCandIdx( POZNAN_DBMP_MRG_CAND, uiAbsPartIdx ) == POZNAN_DBMP_MRG_CAND + 1 );
720       
721        uiNumCand = 0; for( UInt uiIdx = 0; uiIdx < MRG_MAX_NUM_CANDS; uiIdx++ ) if(abAvailable[ uiIdx ]) uiNumCand++;
722  }
723#endif
724
725  AOF( uiNumCand > 1 );
726
727  //--- determine contexts ---
728  AOF( NUM_MV_MERGE_IDX_EXT_CTX >= MRG_MAX_NUM_CANDS - 1 );
729  UInt  auiCtx[ MRG_MAX_NUM_CANDS - 1 ];
730  for( UInt uiIdx = 0; uiIdx < MRG_MAX_NUM_CANDS - 1; uiIdx++ )
731  {
732    auiCtx[ uiIdx ] = MRG_MAX_NUM_CANDS;
733    if( uiNumCand > uiIdx + 1 )
734    {
735      Bool bAvComb    = false;
736      for( UInt uiCId = uiIdx; uiCId < MRG_MAX_NUM_CANDS - 1; uiCId++ )
737      {
738        bAvComb = ( bAvComb || abAvailable[ uiCId ] );
739        if( abAvailable[ uiCId + 1 ] && bAvComb )
740        {
741          auiCtx[ uiIdx ] = uiCId;
742          break;
743        }
744      }
745      AOT( auiCtx[ uiIdx ] == MRG_MAX_NUM_CANDS );
746    }
747  }
748
749  //--- determine unary index ---
750  UInt  uiMergeIdx  = pcCU->getMergeIndex( uiAbsPartIdx );
751#if POZNAN_DBMP
752  if(bDBMPAvailable)
753  {
754        if(uiMergeIdx==POZNAN_DBMP_MRG_CAND)  uiMergeIdx = POZNAN_DBMP_MERGE_POS;
755        else if(uiMergeIdx>=POZNAN_DBMP_MERGE_POS) uiMergeIdx++;
756  }
757#endif
758#if HHI_MPI
759  if( bMVIAvailable )
760  {
761    const Bool bUseMVI = pcCU->getTextureModeDepth( uiAbsPartIdx ) != -1;
762    if( bUseMVI )
763    {
764      uiMergeIdx = HHI_MPI_MERGE_POS;
765    }
766    else if( uiMergeIdx >= HHI_MPI_MERGE_POS )
767    {
768      uiMergeIdx++;
769    }
770  }
771#endif
772  UInt  uiUnaryIdx  = uiMergeIdx;
773  for( UInt uiIdx = 0; uiIdx < uiMergeIdx; uiIdx++ )
774  {
775    if( ! abAvailable[ uiIdx ] )
776    {
777      uiUnaryIdx--;
778    }
779  }
780
781  //--- code unary index ---
782  for( UInt ui = 0; ui < uiNumCand - 1; ui++ )
783  {
784    const UInt uiSymbol = ( ui == uiUnaryIdx ? 0 : 1 );
785    m_pcBinIf->encodeBin( uiSymbol, m_cCUMVMergeIdxExtSCModel.get( 0, 0, auiCtx[ui] ) );
786    if( uiSymbol == 0 )
787    {
788      break;
789    }
790  }
791
792  //--- trace ---
793  DTRACE_CABAC_V( g_nSymbolCounter++ );
794  DTRACE_CABAC_T( "\tparseMergeIndex():" );
795  DTRACE_CABAC_T( "   MRGIdx = " );
796  DTRACE_CABAC_V( uiMergeIdx );
797  DTRACE_CABAC_T( "   UnaryIdx = " );
798  DTRACE_CABAC_V( uiUnaryIdx );
799  DTRACE_CABAC_T( "   NumCand = " );
800  DTRACE_CABAC_V( uiNumCand );
801  DTRACE_CABAC_T( "   Availibility = " );
802  for( UInt uiIdx = 0; uiIdx < MRG_MAX_NUM_CANDS; uiIdx++ )
803  {
804    DTRACE_CABAC_V( abAvailable[ uiIdx ] ? 1 : 0 );
805  }
806  DTRACE_CABAC_T( "   Contexts = " );
807  for( UInt uiIdx = 0; uiIdx < MRG_MAX_NUM_CANDS - 1; uiIdx++ )
808  {
809    DTRACE_CABAC_V( auiCtx[ uiIdx ] );
810  }
811  DTRACE_CABAC_T( "\n" );
812}
813#endif
814
815
816/** code merge index
817 * \param pcCU
818 * \param uiAbsPartIdx
819 * \returns Void
820 */
821Void TEncSbac::codeMergeIndex( TComDataCU* pcCU, UInt uiAbsPartIdx )
822{
823/* todo jacek niech sie wypowie
824#if HHI_INTER_VIEW_MOTION_PRED || HHI_MPI
825#if HHI_INTER_VIEW_MOTION_PRED && HHI_MPI
826  if( ( pcCU->getSlice()->getSPS()->getViewId() > 0 && ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() & PDM_USE_FOR_MERGE ) == PDM_USE_FOR_MERGE ) ||
827      ( pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) )
828#elif HHI_MPI
829  if( pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )
830#else
831  if( pcCU->getSlice()->getSPS()->getViewId() > 0 && ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() & PDM_USE_FOR_MERGE ) == PDM_USE_FOR_MERGE )
832#endif//*/
833#if HHI_INTER_VIEW_MOTION_PRED || HHI_MPI || POZNAN_DBMP
834  if( 
835#if HHI_INTER_VIEW_MOTION_PRED   
836          ( pcCU->getSlice()->getSPS()->getViewId() > 0 && ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() & PDM_USE_FOR_MERGE ) == PDM_USE_FOR_MERGE ) ||
837#endif
838#if HHI_MPI
839      ( pcCU->getSlice()->getSPS()->getUseMVI() && pcCU->getSlice()->getSliceType() != I_SLICE && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N )  ||
840#endif
841#if     POZNAN_DBMP 
842          ( pcCU->getSlice()->getMP()->isDBMPEnabled() ) ||
843#endif
844          0
845    )
846  {
847    codeMergeIndexMV( pcCU, uiAbsPartIdx );
848    return;
849  }
850#endif
851
852  Bool bLeftInvolved = false;
853  Bool bAboveInvolved = false;
854  Bool bCollocatedInvolved = false;
855  Bool bCornerInvolved = false;
856  Bool bCornerBLInvolved = false;
857  UInt uiNumCand = 0;
858  for( UInt uiIter = 0; uiIter < MRG_MAX_NUM_CANDS; ++uiIter )
859  {
860    if( pcCU->getNeighbourCandIdx( uiIter, uiAbsPartIdx ) == uiIter + 1 )
861    {
862      uiNumCand++;
863      if( uiIter == 0 )
864      {
865        bLeftInvolved = true;
866      }
867      else if( uiIter == 1 )
868      {
869        bAboveInvolved = true;
870      }
871      else if( uiIter == 2 )
872      {
873        bCollocatedInvolved = true;
874      }
875      else if( uiIter == 3 )
876      {
877        bCornerInvolved = true;
878      }
879      else if( uiIter == 4 )
880      {
881        bCornerBLInvolved = true;
882      }
883    }
884  }
885  assert( uiNumCand > 1 );
886
887  UInt auiCtx[4] = { 0, 0, 0, 3 };
888  if( bLeftInvolved && bAboveInvolved )
889  {
890    auiCtx[0] = 0;
891  }
892  else if( bLeftInvolved || bAboveInvolved )
893  {
894    auiCtx[0] = bCollocatedInvolved ? 1 : 2;
895  }
896  else
897  {
898    auiCtx[0] = bCollocatedInvolved ? 2 : 3;
899  }
900
901  if( uiNumCand >= 3 )
902  {
903    if( bAboveInvolved )
904    {
905      auiCtx[1] = bCollocatedInvolved ? 1 : 2;
906    }
907    else
908    {
909      auiCtx[1] = bCollocatedInvolved ? 2 : 3;
910    }
911  }
912
913  if( uiNumCand >= 4 )
914  {
915    auiCtx[2] =  bCollocatedInvolved ? 2 : 3;
916  }
917
918  UInt uiUnaryIdx = pcCU->getMergeIndex( uiAbsPartIdx );
919
920  if( !bCornerInvolved && uiUnaryIdx > 3 )
921  {
922    --uiUnaryIdx;
923  }
924  if( !bCollocatedInvolved && uiUnaryIdx > 2 )
925  {
926    --uiUnaryIdx;
927  }
928  if( !bAboveInvolved && uiUnaryIdx > 1 )
929  {
930    --uiUnaryIdx;
931  }
932  if( !bLeftInvolved && uiUnaryIdx > 0 )
933  {
934    --uiUnaryIdx;
935  }
936
937  for( UInt ui = 0; ui < uiNumCand - 1; ++ui )
938  {
939    const UInt uiSymbol = ui == uiUnaryIdx ? 0 : 1;
940    m_pcBinIf->encodeBin( uiSymbol, m_cCUMergeIdxExtSCModel.get( 0, 0, auiCtx[ui] ) );
941    if( uiSymbol == 0 )
942    {
943      break;
944    }
945  }
946
947  DTRACE_CABAC_V( g_nSymbolCounter++ );
948  DTRACE_CABAC_T( "\tparseMergeIndex()" );
949  DTRACE_CABAC_T( "\tuiMRGIdx= " );
950  DTRACE_CABAC_V( pcCU->getMergeIndex( uiAbsPartIdx ) );
951  DTRACE_CABAC_T( "\tuiNumCand= " );
952  DTRACE_CABAC_V( uiNumCand );
953  DTRACE_CABAC_T( "\tbLeftInvolved= " );
954  DTRACE_CABAC_V( bLeftInvolved );
955  DTRACE_CABAC_T( "\tbAboveInvolved= " );
956  DTRACE_CABAC_V( bAboveInvolved );
957  DTRACE_CABAC_T( "\tbCollocatedInvolved= " );
958  DTRACE_CABAC_V( bCollocatedInvolved );
959  DTRACE_CABAC_T( "\tbCornerRTInvolved= " );
960  DTRACE_CABAC_V( bCornerInvolved );
961  DTRACE_CABAC_T( "\tbCornerBLInvolved= " );
962  DTRACE_CABAC_V( bCornerBLInvolved );
963  DTRACE_CABAC_T( "\n" );
964}
965
966
967#if HHI_INTER_VIEW_RESIDUAL_PRED
968Void
969TEncSbac::codeResPredFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
970{
971  UInt  uiCtx     = 0;
972  UInt  uiSymbol  = ( pcCU->getResPredFlag( uiAbsPartIdx ) ? 1 : 0 );
973  m_pcBinIf->encodeBin( uiSymbol, m_cResPredFlagSCModel.get( 0, 0, uiCtx ) );
974}
975#endif
976
977
978Void TEncSbac::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
979{
980  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
981    return;
982 
983  UInt uiCtx           = pcCU->getCtxSplitFlag( uiAbsPartIdx, uiDepth );
984  UInt uiCurrSplitFlag = ( pcCU->getDepth( uiAbsPartIdx ) > uiDepth ) ? 1 : 0;
985 
986  assert( uiCtx < 3 );
987  m_pcBinIf->encodeBin( uiCurrSplitFlag, m_cCUSplitFlagSCModel.get( 0, 0, uiCtx ) );
988  DTRACE_CABAC_V( g_nSymbolCounter++ )
989  DTRACE_CABAC_T( "\tSplitFlag\n" )
990  return;
991}
992
993Void TEncSbac::codeTransformSubdivFlag( UInt uiSymbol, UInt uiCtx )
994{
995  m_pcBinIf->encodeBin( uiSymbol, m_cCUTransSubdivFlagSCModel.get( 0, 0, uiCtx ) );
996  DTRACE_CABAC_V( g_nSymbolCounter++ )
997  DTRACE_CABAC_T( "\tparseTransformSubdivFlag()" )
998  DTRACE_CABAC_T( "\tsymbol=" )
999  DTRACE_CABAC_V( uiSymbol )
1000  DTRACE_CABAC_T( "\tctx=" )
1001  DTRACE_CABAC_V( uiCtx )
1002  DTRACE_CABAC_T( "\n" )
1003}
1004
1005#if HHI_DMM_WEDGE_INTRA
1006Void TEncSbac::xCodeWedgeFullInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1007{
1008  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
1009  Int iBits = g_aucWedgeFullBitsListIdx[iIntraIdx];
1010
1011  UInt uiTabIdx = pcCU->getWedgeFullTabIdx( uiAbsPartIdx );
1012
1013  for ( Int i = 0; i < iBits; i++ )
1014  {
1015    m_pcBinIf->encodeBin( ( uiTabIdx >> i ) & 1, m_cIntraWedgeSCModel.get(0, 0, 0) );
1016  }
1017}
1018
1019Void TEncSbac::xCodeWedgeFullDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1020   {
1021  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
1022  Int iBits = g_aucWedgeFullBitsListIdx[iIntraIdx];
1023
1024  UInt uiTabIdx = pcCU->getWedgeFullTabIdx( uiAbsPartIdx );
1025
1026  for ( Int i = 0; i < iBits; i++ )
1027  {
1028    m_pcBinIf->encodeBin( ( uiTabIdx >> i ) & 1, m_cIntraWedgeSCModel.get(0, 0, 0) );
1029  }
1030
1031  Int iDeltaDC1 = pcCU->getWedgeFullDeltaDC1( uiAbsPartIdx );
1032  Int iDeltaDC2 = pcCU->getWedgeFullDeltaDC2( uiAbsPartIdx );
1033
1034   xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1035   if ( iDeltaDC1 != 0 )
1036   {
1037     UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
1038     m_pcBinIf->encodeBinEP( uiSign );
1039   }
1040   xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1041   if ( iDeltaDC2 != 0 )
1042   {
1043     UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
1044     m_pcBinIf->encodeBinEP( uiSign );
1045   }
1046}
1047
1048Void TEncSbac::xCodeWedgePredDirInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1049{
1050  if( DMM_WEDGE_PREDDIR_DELTAEND_MAX > 0 )
1051  {
1052    Int iDeltaEnd = pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx );
1053    m_pcBinIf->encodeBin( (iDeltaEnd!=0), m_cIntraWedgeSCModel.get(0, 0, 3) );
1054
1055    if( iDeltaEnd != 0 )
1056    {
1057      UInt uiAbsValMinus1 = abs(iDeltaEnd)-1;
1058      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x01),      m_cIntraWedgeSCModel.get(0, 0, 3) );
1059      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x02) >> 1, m_cIntraWedgeSCModel.get(0, 0, 3) );
1060
1061      UInt uiSign = iDeltaEnd > 0 ? 0 : 1;
1062      m_pcBinIf->encodeBinEP( uiSign );
1063    }
1064  }
1065}
1066
1067Void TEncSbac::xCodeWedgePredDirDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1068{
1069  if( DMM_WEDGE_PREDDIR_DELTAEND_MAX > 0 )
1070  {
1071    Int iDeltaEnd = pcCU->getWedgePredDirDeltaEnd( uiAbsPartIdx );
1072    m_pcBinIf->encodeBin( (iDeltaEnd!=0), m_cIntraWedgeSCModel.get(0, 0, 3) );
1073
1074    if( iDeltaEnd != 0 )
1075  {
1076      UInt uiAbsValMinus1 = abs(iDeltaEnd)-1;
1077      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x01),      m_cIntraWedgeSCModel.get(0, 0, 3) );
1078      m_pcBinIf->encodeBin( (uiAbsValMinus1 & 0x02) >> 1, m_cIntraWedgeSCModel.get(0, 0, 3) );
1079
1080      UInt uiSign = iDeltaEnd > 0 ? 0 : 1;
1081      m_pcBinIf->encodeBinEP( uiSign );
1082    }
1083  }
1084 
1085  Int iDeltaDC1 = pcCU->getWedgePredDirDeltaDC1( uiAbsPartIdx );
1086  Int iDeltaDC2 = pcCU->getWedgePredDirDeltaDC2( uiAbsPartIdx );
1087
1088  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1089  if ( iDeltaDC1 != 0 )
1090  {
1091    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
1092    m_pcBinIf->encodeBinEP( uiSign );
1093  }
1094  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1095  if ( iDeltaDC2 != 0 )
1096  {
1097    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
1098    m_pcBinIf->encodeBinEP( uiSign );
1099  }
1100}
1101#endif
1102#if HHI_DMM_PRED_TEX
1103Void TEncSbac::xCodeWedgePredTexDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1104{
1105  Int iDeltaDC1 = pcCU->getWedgePredTexDeltaDC1( uiAbsPartIdx );
1106  Int iDeltaDC2 = pcCU->getWedgePredTexDeltaDC2( uiAbsPartIdx );
1107
1108  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1109  if ( iDeltaDC1 != 0 )
1110  {
1111    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
1112    m_pcBinIf->encodeBinEP( uiSign );
1113  }
1114  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1115  if ( iDeltaDC2 != 0 )
1116  {
1117    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
1118    m_pcBinIf->encodeBinEP( uiSign );
1119  }
1120}
1121
1122Void TEncSbac::xCodeContourPredTexDeltaInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
1123{
1124  Int iDeltaDC1 = pcCU->getContourPredTexDeltaDC1( uiAbsPartIdx );
1125  Int iDeltaDC2 = pcCU->getContourPredTexDeltaDC2( uiAbsPartIdx );
1126
1127  xWriteExGolombLevel( UInt( abs( iDeltaDC1 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1128  if ( iDeltaDC1 != 0 )
1129  {
1130    UInt uiSign = iDeltaDC1 > 0 ? 0 : 1;
1131    m_pcBinIf->encodeBinEP( uiSign );
1132  }
1133  xWriteExGolombLevel( UInt( abs( iDeltaDC2 ) ), m_cIntraWedgeSCModel.get(0, 0, 1) );
1134  if ( iDeltaDC2 != 0 )
1135  {
1136    UInt uiSign = iDeltaDC2 > 0 ? 0 : 1;
1137    m_pcBinIf->encodeBinEP( uiSign );
1138  }
1139}
1140#endif
1141
1142#if MTK_DCM_MPM
1143Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt uiAbsPartIdx )
1144{
1145  UInt uiDir = pcCU->getLumaIntraDir( uiAbsPartIdx );
1146
1147#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
1148  if ( pcCU->getSlice()->getSPS()->isDepth() && pcCU->getSlice()->getSPS()->getUseDMM() && pcCU->getWidth( uiAbsPartIdx ) < 64 )
1149  {
1150    m_pcBinIf->encodeBin( uiDir > MAX_MODE_ID_INTRA_DIR, m_cIntraDMMSCModel.get(0, 0, 0) );
1151  }
1152  if( uiDir > MAX_MODE_ID_INTRA_DIR )
1153  {
1154    assert( pcCU->getWidth( uiAbsPartIdx ) < 64 );
1155    UInt uiDMMode = uiDir - (MAX_MODE_ID_INTRA_DIR+1);
1156
1157#if HHI_DMM_WEDGE_INTRA && HHI_DMM_PRED_TEX
1158    m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cIntraDMMSCModel.get(0, 0, 1) );
1159    m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cIntraDMMSCModel.get(0, 0, 1) );
1160
1161      if ( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1162      {
1163      m_pcBinIf->encodeBin( (uiDMMode & 0x04) >> 2, m_cIntraDMMSCModel.get(0, 0, 1) );
1164        }
1165#else
1166                                  m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cIntraDMMSCModel.get(0, 0, 1) );
1167
1168    if ( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1169    {
1170      m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cIntraDMMSCModel.get(0, 0, 1) );
1171    }
1172#endif
1173#if HHI_DMM_WEDGE_INTRA
1174    if( uiDir == DMM_WEDGE_FULL_IDX )          { xCodeWedgeFullInfo          ( pcCU, uiAbsPartIdx ); }
1175    if( uiDir == DMM_WEDGE_FULL_D_IDX )        { xCodeWedgeFullDeltaInfo     ( pcCU, uiAbsPartIdx ); }
1176    if( uiDir == DMM_WEDGE_PREDDIR_IDX )       { xCodeWedgePredDirInfo       ( pcCU, uiAbsPartIdx ); }
1177    if( uiDir == DMM_WEDGE_PREDDIR_D_IDX )     { xCodeWedgePredDirDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1178#endif
1179#if HHI_DMM_PRED_TEX
1180#if FLEX_CODING_ORDER
1181    if ( !pcCU->getSlice()->getSPS()->getUseDMM34() )
1182    {
1183      assert( uiDir != DMM_WEDGE_PREDTEX_D_IDX );
1184      assert( uiDir != DMM_CONTOUR_PREDTEX_D_IDX );
1185    }
1186#endif
1187    if( uiDir == DMM_WEDGE_PREDTEX_D_IDX )     { xCodeWedgePredTexDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1188    if( uiDir == DMM_CONTOUR_PREDTEX_D_IDX )   { xCodeContourPredTexDeltaInfo( pcCU, uiAbsPartIdx ); }
1189#endif
1190  }
1191  else
1192#endif
1193  {
1194  Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
1195#if ADD_PLANAR_MODE
1196  UInt planarFlag    = 0;
1197  if (uiDir == PLANAR_IDX)
1198  {
1199    uiDir = 2;
1200    planarFlag = 1;
1201  }
1202#endif
1203 
1204  Int uiPreds[2] = {-1, -1};
1205  Int uiPredNum = pcCU->getIntraDirLumaPredictor(uiAbsPartIdx, uiPreds); 
1206
1207  Int uiPredIdx = -1;
1208
1209  for(UInt i = 0; i < uiPredNum; i++)
1210  {
1211    if(uiDir == uiPreds[i])
1212    {
1213      uiPredIdx = i;
1214    }
1215  }
1216 
1217  if(uiPredIdx != -1)
1218  {
1219    m_pcBinIf->encodeBin( 1, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1220    if(uiPredNum == 2)
1221    {
1222      m_pcBinIf->encodeBin( uiPredIdx, m_cCUIntraPredSCModel.get( 0, 0, 2 ) );
1223    }
1224
1225  }
1226  else
1227  {
1228    m_pcBinIf->encodeBin( 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1229 
1230    for(Int i = (uiPredNum - 1); i >= 0; i--)
1231    {
1232      uiDir = uiDir > uiPreds[i] ? uiDir - 1 : uiDir;
1233    }
1234
1235    if ( g_aucIntraModeBitsAng[iIntraIdx] < 6 )
1236    {
1237      m_pcBinIf->encodeBin((uiDir & 0x01), m_cCUIntraPredSCModel.get(0, 0, 1));
1238      if ( g_aucIntraModeBitsAng[iIntraIdx] > 2 ) { m_pcBinIf->encodeBin((uiDir & 0x02) >> 1, m_cCUIntraPredSCModel.get(0, 0, 1));}
1239      if ( g_aucIntraModeBitsAng[iIntraIdx] > 3 ) { m_pcBinIf->encodeBin((uiDir & 0x04) >> 2, m_cCUIntraPredSCModel.get(0, 0, 1));}
1240      if ( g_aucIntraModeBitsAng[iIntraIdx] > 4 ) { m_pcBinIf->encodeBin((uiDir & 0x08) >> 3, m_cCUIntraPredSCModel.get(0, 0, 1));}
1241    }
1242    else
1243    {
1244      if (uiDir < 31){ // uiDir is here 0...32, 5 bits for uiDir 0...30, 31 is an escape code for coding one more bit for 31 and 32
1245        m_pcBinIf->encodeBin((uiDir & 0x01),      m_cCUIntraPredSCModel.get(0, 0, 1));
1246        m_pcBinIf->encodeBin((uiDir & 0x02) >> 1, m_cCUIntraPredSCModel.get(0, 0, 1));
1247        m_pcBinIf->encodeBin((uiDir & 0x04) >> 2, m_cCUIntraPredSCModel.get(0, 0, 1));
1248        m_pcBinIf->encodeBin((uiDir & 0x08) >> 3, m_cCUIntraPredSCModel.get(0, 0, 1));
1249        m_pcBinIf->encodeBin((uiDir & 0x10) >> 4, m_cCUIntraPredSCModel.get(0, 0, 1));
1250      }
1251      else{
1252        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1253        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1254        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1255        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1256        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1257        m_pcBinIf->encodeBin((uiDir == 32) ? 1 : 0, m_cCUIntraPredSCModel.get(0, 0, 1));
1258       }
1259     }
1260   }
1261#if ADD_PLANAR_MODE
1262  uiDir = pcCU->getLumaIntraDir( uiAbsPartIdx );
1263  if ( (uiDir == PLANAR_IDX) || (uiDir == 2) )
1264  {
1265    m_pcBinIf->encodeBin( planarFlag, m_cPlanarFlagSCModel.get(0,0,0) );
1266  }
1267#endif
1268  }
1269  return;
1270}
1271#else
1272Void TEncSbac::codeIntraDirLumaAng( TComDataCU* pcCU, UInt uiAbsPartIdx )
1273{
1274  UInt uiDir = pcCU->getLumaIntraDir( uiAbsPartIdx );
1275#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
1276  if ( pcCU->getSlice()->getSPS()->isDepth() && pcCU->getSlice()->getSPS()->getUseDMM() && pcCU->getWidth( uiAbsPartIdx ) < 64 )
1277  {
1278    m_pcBinIf->encodeBin( uiDir > MAX_MODE_ID_INTRA_DIR, m_cIntraDMMSCModel.get(0, 0, 0) );
1279  }
1280  if( uiDir > MAX_MODE_ID_INTRA_DIR )
1281  {
1282    assert( pcCU->getWidth( uiAbsPartIdx ) < 64 );
1283    UInt uiDMMode = uiDir - (MAX_MODE_ID_INTRA_DIR+1);
1284
1285#if HHI_DMM_WEDGE_INTRA && HHI_DMM_PRED_TEX
1286    m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cIntraDMMSCModel.get(0, 0, 1) );
1287    m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cIntraDMMSCModel.get(0, 0, 1) );
1288
1289      if ( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1290      {
1291      m_pcBinIf->encodeBin( (uiDMMode & 0x04) >> 2, m_cIntraDMMSCModel.get(0, 0, 1) );
1292        }
1293#else
1294        m_pcBinIf->encodeBin( (uiDMMode & 0x01),      m_cIntraDMMSCModel.get(0, 0, 1) );
1295
1296      if ( pcCU->getPartitionSize( uiAbsPartIdx ) != SIZE_NxN && pcCU->getWidth( uiAbsPartIdx ) > 4 )
1297      {
1298      m_pcBinIf->encodeBin( (uiDMMode & 0x02) >> 1, m_cIntraDMMSCModel.get(0, 0, 1) );
1299      }
1300#endif
1301#if HHI_DMM_WEDGE_INTRA
1302    if( uiDir == DMM_WEDGE_FULL_IDX )          { xCodeWedgeFullInfo          ( pcCU, uiAbsPartIdx ); }
1303    if( uiDir == DMM_WEDGE_FULL_D_IDX )        { xCodeWedgeFullDeltaInfo     ( pcCU, uiAbsPartIdx ); }
1304    if( uiDir == DMM_WEDGE_PREDDIR_IDX )       { xCodeWedgePredDirInfo       ( pcCU, uiAbsPartIdx ); }
1305    if( uiDir == DMM_WEDGE_PREDDIR_D_IDX )     { xCodeWedgePredDirDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1306#endif
1307#if HHI_DMM_PRED_TEX
1308    if( uiDir == DMM_WEDGE_PREDTEX_D_IDX )     { xCodeWedgePredTexDeltaInfo  ( pcCU, uiAbsPartIdx ); }
1309    if( uiDir == DMM_CONTOUR_PREDTEX_D_IDX )   { xCodeContourPredTexDeltaInfo( pcCU, uiAbsPartIdx ); }
1310#endif
1311  }
1312  else
1313#endif
1314  {
1315  Int  iMostProbable = pcCU->getMostProbableIntraDirLuma( uiAbsPartIdx );
1316#if ADD_PLANAR_MODE
1317  UInt planarFlag    = 0;
1318  if (uiDir == PLANAR_IDX)
1319  {
1320    uiDir = 2;
1321    planarFlag = 1;
1322  }
1323#endif
1324 
1325  if (uiDir == iMostProbable)
1326    m_pcBinIf->encodeBin( 1, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1327  else
1328  {
1329    m_pcBinIf->encodeBin( 0, m_cCUIntraPredSCModel.get( 0, 0, 0 ) );
1330    uiDir = uiDir > iMostProbable ? uiDir - 1 : uiDir;
1331    Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
1332    if ( g_aucIntraModeBitsAng[iIntraIdx] < 6 )
1333    {
1334      m_pcBinIf->encodeBin((uiDir & 0x01), m_cCUIntraPredSCModel.get(0, 0, 1));
1335      if ( g_aucIntraModeBitsAng[iIntraIdx] > 2 ) m_pcBinIf->encodeBin((uiDir & 0x02) >> 1, m_cCUIntraPredSCModel.get(0, 0, 1));
1336      if ( g_aucIntraModeBitsAng[iIntraIdx] > 3 ) m_pcBinIf->encodeBin((uiDir & 0x04) >> 2, m_cCUIntraPredSCModel.get(0, 0, 1));
1337      if ( g_aucIntraModeBitsAng[iIntraIdx] > 4 ) m_pcBinIf->encodeBin((uiDir & 0x08) >> 3, m_cCUIntraPredSCModel.get(0, 0, 1));
1338    }
1339    else
1340    {
1341      if (uiDir < 31)
1342      { // uiDir is here 0...32, 5 bits for uiDir 0...30, 31 is an escape code for coding one more bit for 31 and 32
1343        m_pcBinIf->encodeBin((uiDir & 0x01),      m_cCUIntraPredSCModel.get(0, 0, 1));
1344        m_pcBinIf->encodeBin((uiDir & 0x02) >> 1, m_cCUIntraPredSCModel.get(0, 0, 1));
1345        m_pcBinIf->encodeBin((uiDir & 0x04) >> 2, m_cCUIntraPredSCModel.get(0, 0, 1));
1346        m_pcBinIf->encodeBin((uiDir & 0x08) >> 3, m_cCUIntraPredSCModel.get(0, 0, 1));
1347        m_pcBinIf->encodeBin((uiDir & 0x10) >> 4, m_cCUIntraPredSCModel.get(0, 0, 1));
1348      }
1349      else
1350      {
1351        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1352        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1353        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1354        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1355        m_pcBinIf->encodeBin(1, m_cCUIntraPredSCModel.get(0, 0, 1));
1356        m_pcBinIf->encodeBin((uiDir == 32) ? 1 : 0, m_cCUIntraPredSCModel.get(0, 0, 1));
1357      }
1358    }
1359  }
1360 
1361#if ADD_PLANAR_MODE
1362  uiDir = pcCU->getLumaIntraDir( uiAbsPartIdx );
1363  if ( (uiDir == PLANAR_IDX) || (uiDir == 2) )
1364  {
1365    m_pcBinIf->encodeBin( planarFlag, m_cPlanarFlagSCModel.get(0,0,0) );
1366  }
1367#endif
1368  }
1369  return;
1370}
1371#endif
1372Void TEncSbac::codeIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx )
1373{
1374  UInt uiCtx            = pcCU->getCtxIntraDirChroma( uiAbsPartIdx );
1375  UInt uiIntraDirChroma = pcCU->getChromaIntraDir   ( uiAbsPartIdx );
1376#if ADD_PLANAR_MODE
1377  UInt planarFlag       = 0;
1378  if (uiIntraDirChroma == PLANAR_IDX)
1379  {
1380    uiIntraDirChroma = 2;
1381    planarFlag = 1;
1382  }
1383#endif
1384 
1385#if CHROMA_CODEWORD
1386  UInt uiMode = pcCU->getLumaIntraDir(uiAbsPartIdx);
1387#if ADD_PLANAR_MODE
1388  if ( (uiMode == 2 ) || (uiMode == PLANAR_IDX) )
1389  {
1390    uiMode = 4;
1391  }
1392#endif
1393
1394#if LM_CHROMA
1395
1396  Bool bUseLMFlag = pcCU->getSlice()->getSPS()->getUseLMChroma();
1397
1398  Int  iMaxMode = bUseLMFlag ? 3 : 4;
1399
1400  Int  iMax = uiMode < iMaxMode ? 2 : 3; 
1401 
1402  //switch codeword
1403  if (uiIntraDirChroma == 4) 
1404  {
1405    uiIntraDirChroma = 0;
1406  } 
1407  else if (uiIntraDirChroma == 3 && bUseLMFlag )
1408  {
1409    uiIntraDirChroma = 1;
1410  }
1411  else
1412  {
1413    if (uiIntraDirChroma < uiMode)
1414      uiIntraDirChroma++;
1415
1416    if (bUseLMFlag)
1417      uiIntraDirChroma++;
1418
1419#if CHROMA_CODEWORD_SWITCH
1420    uiIntraDirChroma = ChromaMapping[iMax-2][uiIntraDirChroma];
1421#endif
1422
1423  }
1424
1425
1426#else // <-- LM_CHROMA
1427  Int  iMax = uiMode < 4 ? 2 : 3; 
1428 
1429  //switch codeword
1430  if (uiIntraDirChroma == 4) {
1431    uiIntraDirChroma = 0;
1432  } 
1433#if CHROMA_CODEWORD_SWITCH
1434  else {
1435    if (uiIntraDirChroma < uiMode) {
1436      uiIntraDirChroma++;
1437    }
1438    uiIntraDirChroma = ChromaMapping[iMax-2][uiIntraDirChroma];
1439  }
1440#else
1441  else if (uiIntraDirChroma < uiMode) {
1442    uiIntraDirChroma++;
1443  }
1444#endif
1445#endif // <-- LM_CHROMA
1446 
1447  if ( 0 == uiIntraDirChroma )
1448  {
1449    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, uiCtx ) );
1450  }
1451  else
1452  {
1453    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, uiCtx ) );
1454    xWriteUnaryMaxSymbol( uiIntraDirChroma - 1, m_cCUChromaPredSCModel.get( 0, 0 ) + 3, 0, iMax );
1455  }
1456#else // CHROMA_CODEWORD
1457  if ( 0 == uiIntraDirChroma )
1458  {
1459    m_pcBinIf->encodeBin( 0, m_cCUChromaPredSCModel.get( 0, 0, uiCtx ) );
1460  }
1461  else
1462  {
1463    m_pcBinIf->encodeBin( 1, m_cCUChromaPredSCModel.get( 0, 0, uiCtx ) );
1464    xWriteUnaryMaxSymbol( uiIntraDirChroma - 1, m_cCUChromaPredSCModel.get( 0, 0 ) + 3, 0, 3 );
1465  }
1466#endif
1467
1468#if ADD_PLANAR_MODE
1469  uiIntraDirChroma = pcCU->getChromaIntraDir( uiAbsPartIdx );
1470#if CHROMA_CODEWORD
1471  uiMode = pcCU->getLumaIntraDir(uiAbsPartIdx);
1472  mapPlanartoDC( uiIntraDirChroma );
1473  mapPlanartoDC( uiMode );
1474  if ( (uiIntraDirChroma == 2) && (uiMode != 2) )
1475#else
1476  if ( (uiIntraDirChroma == PLANAR_IDX) || (uiIntraDirChroma == 2) )
1477#endif
1478  {
1479    m_pcBinIf->encodeBin( planarFlag, m_cPlanarFlagSCModel.get(0,0,1) );
1480  }
1481#endif
1482  return;
1483}
1484
1485Void TEncSbac::codeInterDir( TComDataCU* pcCU, UInt uiAbsPartIdx )
1486{
1487  UInt uiInterDir = pcCU->getInterDir   ( uiAbsPartIdx );
1488  UInt uiCtx      = pcCU->getCtxInterDir( uiAbsPartIdx );
1489  uiInterDir--;
1490  m_pcBinIf->encodeBin( ( uiInterDir == 2 ? 1 : 0 ), m_cCUInterDirSCModel.get( 0, 0, uiCtx ) );
1491 
1492  if ( pcCU->getSlice()->getNoBackPredFlag() )
1493  {
1494    assert( uiInterDir != 1 );
1495    return;
1496  }
1497 
1498#if DCM_COMB_LIST
1499  if ( uiInterDir < 2 && pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 0)
1500#else
1501  if ( uiInterDir < 2 )
1502#endif
1503  {
1504    m_pcBinIf->encodeBin( uiInterDir, m_cCUInterDirSCModel.get( 0, 0, 3 ) );
1505  }
1506 
1507  return;
1508}
1509
1510Void TEncSbac::codeRefFrmIdx( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1511{
1512#if DCM_COMB_LIST
1513  if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getInterDir( uiAbsPartIdx ) != 3)
1514  {
1515    Int iRefFrame = pcCU->getSlice()->getRefIdxOfLC(eRefList, pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx ));
1516    UInt uiCtx;
1517
1518    uiCtx = pcCU->getCtxRefIdx( uiAbsPartIdx, RefPicList(pcCU->getSlice()->getListIdFromIdxOfLC(0)) );
1519
1520    m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), m_cCURefPicSCModel.get( 0, 0, uiCtx ) );
1521
1522    if ( iRefFrame > 0 )
1523    {
1524      xWriteUnaryMaxSymbol( iRefFrame - 1, &m_cCURefPicSCModel.get( 0, 0, 4 ), 1, pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_C )-2 );
1525    }
1526  }
1527  else
1528  {
1529#endif
1530  Int iRefFrame = pcCU->getCUMvField( eRefList )->getRefIdx( uiAbsPartIdx );
1531 
1532  UInt uiCtx = pcCU->getCtxRefIdx( uiAbsPartIdx, eRefList );
1533 
1534  m_pcBinIf->encodeBin( ( iRefFrame == 0 ? 0 : 1 ), m_cCURefPicSCModel.get( 0, 0, uiCtx ) );
1535 
1536  if ( iRefFrame > 0 )
1537  {
1538    xWriteUnaryMaxSymbol( iRefFrame - 1, &m_cCURefPicSCModel.get( 0, 0, 4 ), 1, pcCU->getSlice()->getNumRefIdx( eRefList )-2 );
1539  }
1540#if DCM_COMB_LIST
1541  }
1542#endif
1543  return;
1544}
1545
1546Void TEncSbac::codeViewIdx(Int iViewIdx)
1547{
1548  assert(0) ; // not implemented yet
1549  xWriteUnaryMaxSymbol(iViewIdx, &m_cViewIdxSCModel.get( 0, 0, 4),1, MAX_NUMBER_VIEWS   );
1550}
1551
1552Void TEncSbac::codeMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, RefPicList eRefList )
1553{
1554  TComCUMvField* pcCUMvField = pcCU->getCUMvField( eRefList );
1555  Int iHor = pcCUMvField->getMvd( uiAbsPartIdx ).getHor();
1556  Int iVer = pcCUMvField->getMvd( uiAbsPartIdx ).getVer();
1557 
1558  UInt uiAbsPartIdxL, uiAbsPartIdxA;
1559#if MVD_CTX
1560  Int iHorPredL, iVerPredL;
1561  Int iHorPredA, iVerPredA;
1562#else
1563  Int iHorPred, iVerPred;
1564#endif
1565
1566  TComDataCU* pcCUL   = pcCU->getPULeft ( uiAbsPartIdxL, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
1567  TComDataCU* pcCUA   = pcCU->getPUAbove( uiAbsPartIdxA, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
1568 
1569  TComCUMvField* pcCUMvFieldL = ( pcCUL == NULL || pcCUL->isIntra( uiAbsPartIdxL ) ) ? NULL : pcCUL->getCUMvField( eRefList );
1570  TComCUMvField* pcCUMvFieldA = ( pcCUA == NULL || pcCUA->isIntra( uiAbsPartIdxA ) ) ? NULL : pcCUA->getCUMvField( eRefList );
1571 
1572#if MVD_CTX
1573  iHorPredA = ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsHor() );
1574  iVerPredA = ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsVer() );
1575  iHorPredL = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsHor() );
1576  iVerPredL = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsVer() );
1577
1578  xWriteMvd( iHor, iHorPredL, iHorPredA, 0 );
1579  xWriteMvd( iVer, iVerPredL, iVerPredA, 1 );
1580#else 
1581  iHorPred = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsHor() ) +
1582  ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsHor() );
1583  iVerPred = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsVer() ) +
1584  ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsVer() );
1585
1586  xWriteMvd( iHor, iHorPred, 0 );
1587  xWriteMvd( iVer, iVerPred, 1 );
1588#endif
1589
1590 
1591  return;
1592}
1593
1594Void TEncSbac::codeDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx )
1595{
1596  Int iDQp  = pcCU->getQP( uiAbsPartIdx ) - pcCU->getSlice()->getSliceQp();
1597 
1598  if ( iDQp == 0 )
1599  {
1600    m_pcBinIf->encodeBin( 0, m_cCUDeltaQpSCModel.get( 0, 0, 0 ) );
1601  }
1602  else
1603  {
1604    m_pcBinIf->encodeBin( 1, m_cCUDeltaQpSCModel.get( 0, 0, 0 ) );
1605   
1606    UInt uiDQp = (UInt)( iDQp > 0 ? ( 2 * iDQp - 2 ) : ( -2 * iDQp - 1 ) );
1607    xWriteUnarySymbol( uiDQp, &m_cCUDeltaQpSCModel.get( 0, 0, 2 ), 1 );
1608  }
1609 
1610  return;
1611}
1612
1613Void TEncSbac::codeQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
1614{
1615  UInt uiCbf = pcCU->getCbf     ( uiAbsPartIdx, eType, uiTrDepth );
1616  UInt uiCtx = pcCU->getCtxQtCbf( uiAbsPartIdx, eType, uiTrDepth );
1617  m_pcBinIf->encodeBin( uiCbf , m_cCUQtCbfSCModel.get( 0, eType ? eType - 1 : eType, uiCtx ) );
1618  DTRACE_CABAC_V( g_nSymbolCounter++ )
1619  DTRACE_CABAC_T( "\tparseQtCbf()" )
1620  DTRACE_CABAC_T( "\tsymbol=" )
1621  DTRACE_CABAC_V( uiCbf )
1622  DTRACE_CABAC_T( "\tctx=" )
1623  DTRACE_CABAC_V( uiCtx )
1624  DTRACE_CABAC_T( "\tetype=" )
1625  DTRACE_CABAC_V( eType )
1626  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1627  DTRACE_CABAC_V( uiAbsPartIdx )
1628  DTRACE_CABAC_T( "\n" )
1629}
1630
1631UInt xCheckCoeffPlainCNoRecur( const TCoeff* pcCoef, UInt uiSize, UInt uiDepth )
1632{
1633  UInt uiNumofCoeff = 0;
1634  UInt ui = uiSize>>uiDepth;
1635  {
1636    UInt x, y;
1637    const TCoeff* pCeoff = pcCoef;
1638    for( y=0 ; y<ui ; y++ )
1639    {
1640      for( x=0 ; x<ui ; x++ )
1641      {
1642        if( pCeoff[x] != 0 )
1643        {
1644          uiNumofCoeff++;
1645        }
1646      }
1647      pCeoff += uiSize;
1648    }
1649  }
1650  return uiNumofCoeff;
1651}
1652
1653Void TEncSbac::codeQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx )
1654{
1655  UInt uiCbf = pcCU->getQtRootCbf( uiAbsPartIdx );
1656  UInt uiCtx = pcCU->getCtxQtRootCbf( uiAbsPartIdx );
1657  m_pcBinIf->encodeBin( uiCbf , m_cCUQtRootCbfSCModel.get( 0, 0, uiCtx ) );
1658  DTRACE_CABAC_V( g_nSymbolCounter++ )
1659  DTRACE_CABAC_T( "\tparseQtRootCbf()" )
1660  DTRACE_CABAC_T( "\tsymbol=" )
1661  DTRACE_CABAC_V( uiCbf )
1662  DTRACE_CABAC_T( "\tctx=" )
1663  DTRACE_CABAC_V( uiCtx )
1664  DTRACE_CABAC_T( "\tuiAbsPartIdx=" )
1665  DTRACE_CABAC_V( uiAbsPartIdx )
1666  DTRACE_CABAC_T( "\n" )
1667}
1668
1669Void TEncSbac::xCheckCoeff( TCoeff* pcCoef, UInt uiSize, UInt uiDepth, UInt& uiNumofCoeff, UInt& uiPart )
1670{
1671  UInt ui = uiSize>>uiDepth;
1672  if( uiPart == 0 )
1673  {
1674    if( ui <= 4 )
1675    {
1676      UInt x, y;
1677      TCoeff* pCeoff = pcCoef;
1678      for( y=0 ; y<ui ; y++ )
1679      {
1680        for( x=0 ; x<ui ; x++ )
1681        {
1682          if( pCeoff[x] != 0 )
1683          {
1684            uiNumofCoeff++;
1685          }
1686        }
1687        pCeoff += uiSize;
1688      }
1689    }
1690    else
1691    {
1692      xCheckCoeff( pcCoef,                            uiSize, uiDepth+1, uiNumofCoeff, uiPart ); uiPart++; //1st Part
1693      xCheckCoeff( pcCoef             + (ui>>1),      uiSize, uiDepth+1, uiNumofCoeff, uiPart ); uiPart++; //2nd Part
1694      xCheckCoeff( pcCoef + (ui>>1)*uiSize,           uiSize, uiDepth+1, uiNumofCoeff, uiPart ); uiPart++; //3rd Part
1695      xCheckCoeff( pcCoef + (ui>>1)*uiSize + (ui>>1), uiSize, uiDepth+1, uiNumofCoeff, uiPart );           //4th Part
1696    }
1697  }
1698  else
1699  {
1700    UInt x, y;
1701    TCoeff* pCeoff = pcCoef;
1702    for( y=0 ; y<ui ; y++ )
1703    {
1704      for( x=0 ; x<ui ; x++ )
1705      {
1706        if( pCeoff[x] != 0 )
1707        {
1708          uiNumofCoeff++;
1709        }
1710      }
1711      pCeoff += uiSize;
1712    }
1713  }
1714}
1715
1716UInt xCheckCoeffPlainCNoRecur( const TCoeff* pcCoef, UInt uiSize, UInt uiDepth );
1717
1718#if PCP_SIGMAP_SIMPLE_LAST
1719/** Encode (X,Y) position of the last significant coefficient
1720 * \param uiPosX X component of last coefficient
1721 * \param uiPosY Y component of last coefficient
1722 * \param uiWidth block width
1723 * \param eTType plane type / luminance or chrominance
1724 * \param uiCTXIdx block size context
1725 * \param uiScanIdx scan type (zig-zag, hor, ver)
1726 * \returns Void
1727 * This method encodes the X and Y component within a block of the last significant coefficient.
1728 */
1729__inline Void TEncSbac::codeLastSignificantXY( UInt uiPosX, UInt uiPosY, const UInt uiWidth, const TextType eTType, const UInt uiCTXIdx, const UInt uiScanIdx )
1730{ 
1731  UInt  uiCtxLast;
1732  const UInt uiCtxOffset = g_uiCtxXYOffset[uiCTXIdx];
1733
1734  if( uiScanIdx == SCAN_VER )
1735  {
1736    swap( uiPosX, uiPosY );
1737  }
1738
1739  for(uiCtxLast=0; uiCtxLast<uiPosX; uiCtxLast++)
1740  {
1741    m_pcBinIf->encodeBin( 0, m_cCuCtxLastX.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtxLast] ) );
1742  }
1743  if(uiPosX < uiWidth - 1)
1744  {
1745    m_pcBinIf->encodeBin( 1, m_cCuCtxLastX.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtxLast] ) );
1746  }
1747
1748  for(uiCtxLast=0; uiCtxLast<uiPosY; uiCtxLast++)
1749  {
1750    m_pcBinIf->encodeBin( 0, m_cCuCtxLastY.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtxLast] ) );
1751  }
1752  if(uiPosY < uiWidth - 1)
1753  {
1754    m_pcBinIf->encodeBin( 1, m_cCuCtxLastY.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtxLast] ) );
1755  }
1756}
1757#endif
1758
1759Void TEncSbac::codeCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType, Bool bRD )
1760{
1761  DTRACE_CABAC_V( g_nSymbolCounter++ )
1762  DTRACE_CABAC_T( "\tparseCoeffNxN()\teType=" )
1763  DTRACE_CABAC_V( eTType )
1764  DTRACE_CABAC_T( "\twidth=" )
1765  DTRACE_CABAC_V( uiWidth )
1766  DTRACE_CABAC_T( "\theight=" )
1767  DTRACE_CABAC_V( uiHeight )
1768  DTRACE_CABAC_T( "\tdepth=" )
1769  DTRACE_CABAC_V( uiDepth )
1770  DTRACE_CABAC_T( "\tabspartidx=" )
1771  DTRACE_CABAC_V( uiAbsPartIdx )
1772  DTRACE_CABAC_T( "\ttoCU-X=" )
1773  DTRACE_CABAC_V( pcCU->getCUPelX() )
1774  DTRACE_CABAC_T( "\ttoCU-Y=" )
1775  DTRACE_CABAC_V( pcCU->getCUPelY() )
1776  DTRACE_CABAC_T( "\tCU-addr=" )
1777  DTRACE_CABAC_V(  pcCU->getAddr() )
1778  DTRACE_CABAC_T( "\tinCU-X=" )
1779  DTRACE_CABAC_V( g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1780  DTRACE_CABAC_T( "\tinCU-Y=" )
1781  DTRACE_CABAC_V( g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ] )
1782  DTRACE_CABAC_T( "\tpredmode=" )
1783  DTRACE_CABAC_V(  pcCU->getPredictionMode( uiAbsPartIdx ) )
1784  DTRACE_CABAC_T( "\n" )
1785  if( uiWidth > m_pcSlice->getSPS()->getMaxTrSize() )
1786  {
1787    uiWidth  = m_pcSlice->getSPS()->getMaxTrSize();
1788    uiHeight = m_pcSlice->getSPS()->getMaxTrSize();
1789  }
1790 
1791  UInt uiNumSig = 0;
1792  UInt uiCTXIdx = 0;
1793 
1794  switch( uiWidth )
1795  {
1796    case  2: uiCTXIdx = 6; break;
1797    case  4: uiCTXIdx = 5; break;
1798    case  8: uiCTXIdx = 4; break;
1799    case 16: uiCTXIdx = 3; break;
1800    case 32: uiCTXIdx = 2; break;
1801    case 64: uiCTXIdx = 1; break;
1802    default: uiCTXIdx = 0; break;
1803  }
1804 
1805  // compute number of significant coefficients
1806  UInt  uiPart = 0;
1807  xCheckCoeff(pcCoef, uiWidth, 0, uiNumSig, uiPart );
1808 
1809  if ( bRD )
1810  {
1811    UInt uiTempDepth = uiDepth - pcCU->getDepth( uiAbsPartIdx );
1812    pcCU->setCbfSubParts( ( uiNumSig ? 1 : 0 ) << uiTempDepth, eTType, uiAbsPartIdx, uiDepth );
1813  }
1814 
1815  if ( uiNumSig == 0 )
1816    return;
1817 
1818  eTType = eTType == TEXT_LUMA ? TEXT_LUMA : ( eTType == TEXT_NONE ? TEXT_NONE : TEXT_CHROMA );
1819 
1820  //----- encode significance map -----
1821  const UInt   uiLog2BlockSize   = g_aucConvertToBit[ uiWidth ] + 2;
1822  const UInt   uiMaxNumCoeff     = 1 << ( uiLog2BlockSize << 1 );
1823#if !PCP_SIGMAP_SIMPLE_LAST
1824  const UInt   uiMaxNumCoeffM1   = uiMaxNumCoeff - 1;
1825#endif
1826  const UInt   uiNum4x4Blk       = max<UInt>( 1, uiMaxNumCoeff >> 4 );
1827#if QC_MDCS
1828  const UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1829#endif //QC_MDCS
1830 
1831#if PCP_SIGMAP_SIMPLE_LAST
1832    //===== code last coeff =====
1833    UInt uiScanPosLast = 0, uiPosLastX, uiPosLastY;
1834    for( UInt uiScanPos = 0; uiScanPos < uiMaxNumCoeff; uiScanPos++ )
1835    {
1836#if QC_MDCS
1837      UInt uiBlkPos    = g_auiSigLastScan[uiScanIdx][uiLog2BlockSize-1][uiScanPos]; 
1838#else
1839      UInt  uiBlkPos   = g_auiFrameScanXY[ uiLog2BlockSize-1 ][ uiScanPos ];
1840#endif //QC_MDCS
1841      uiPosLastY = uiBlkPos >> uiLog2BlockSize;
1842      uiPosLastX = uiBlkPos - ( uiPosLastY << uiLog2BlockSize );
1843
1844      if( pcCoef[ uiBlkPos ] != 0 )
1845      {
1846        uiNumSig--;
1847        if( uiNumSig == 0 )
1848        {
1849          codeLastSignificantXY(uiPosLastX, uiPosLastY, uiWidth, eTType, uiCTXIdx, uiScanIdx);
1850          uiScanPosLast = uiScanPos;
1851          break;
1852        }
1853      }
1854    }
1855
1856    //===== code significance flag =====
1857    {
1858      for( UInt uiScanPos = 0; uiScanPos < uiScanPosLast; uiScanPos++ )
1859      {
1860#if QC_MDCS
1861        UInt uiBlkPos   = g_auiSigLastScan[uiScanIdx][uiLog2BlockSize-1][uiScanPos]; 
1862#else
1863        UInt  uiBlkPos  = g_auiFrameScanXY[ uiLog2BlockSize-1 ][ uiScanPos ];
1864#endif //QC_MDCS
1865        UInt  uiPosY    = uiBlkPos >> uiLog2BlockSize;
1866        UInt  uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1867        UInt  uiSig     = pcCoef[ uiBlkPos ] != 0 ? 1 : 0;
1868        UInt  uiCtxSig  = TComTrQuant::getSigCtxInc( pcCoef, uiPosX, uiPosY, uiLog2BlockSize, uiWidth );
1869#if SIMPLE_CONTEXT_SIG
1870        if( uiCtxSig < 4 || eTType)
1871        {
1872          m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx-2, eTType, uiCtxSig ) );
1873        }
1874        else
1875        {
1876          m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx-2 ? uiCTXIdx-2 : 1, eTType, uiCtxSig ) );
1877        }
1878#else
1879        m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx, eTType, uiCtxSig ) );
1880#endif
1881      }
1882    }
1883
1884#else
1885  for( UInt uiScanPos = 0; uiScanPos < uiMaxNumCoeffM1; uiScanPos++ )
1886  {
1887#if QC_MDCS
1888    UInt uiBlkPos   = g_auiSigLastScan[uiScanIdx][uiLog2BlockSize-1][uiScanPos]; 
1889#else
1890    UInt  uiBlkPos  = g_auiFrameScanXY[ uiLog2BlockSize-1 ][ uiScanPos ];
1891#endif //QC_MDCS
1892    UInt  uiPosY    = uiBlkPos >> uiLog2BlockSize;
1893    UInt  uiPosX    = uiBlkPos - ( uiPosY << uiLog2BlockSize );
1894   
1895    //===== code significance flag =====
1896    UInt  uiSig    = pcCoef[ uiBlkPos ] != 0 ? 1 : 0;
1897    UInt  uiCtxSig = TComTrQuant::getSigCtxInc( pcCoef, uiPosX, uiPosY, uiLog2BlockSize, uiWidth );
1898#if SIMPLE_CONTEXT_SIG
1899      if( uiCtxSig < 4 || eTType)
1900      {
1901        m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx-2, eTType, uiCtxSig ) );
1902      }
1903      else
1904      {
1905        m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx-2 ? uiCTXIdx-2 : 1, eTType, uiCtxSig ) );
1906      }
1907#else
1908    m_pcBinIf->encodeBin( uiSig, m_cCUSigSCModel.get( uiCTXIdx, eTType, uiCtxSig ) );
1909#endif
1910    if( uiSig )
1911    {
1912      uiNumSig--;
1913     
1914      //===== code last flag =====
1915      UInt  uiLast    = ( uiNumSig == 0 ) ? 1 : 0;
1916      UInt  uiCtxLast = TComTrQuant::getLastCtxInc( uiPosX, uiPosY, uiLog2BlockSize );
1917      m_pcBinIf->encodeBin( uiLast, m_cCULastSCModel.get( uiCTXIdx, eTType, uiCtxLast ) );
1918     
1919      if( uiLast )
1920      {
1921        break;
1922      }
1923    }
1924  }
1925#endif
1926  /*
1927   * Sign and bin0 PCP (Section 3.2 and 3.3 of JCTVC-B088)
1928   */
1929  Int  c1, c2;
1930  UInt uiSign;
1931  UInt uiAbs;
1932#if E253
1933  UInt uiGoRiceParam = 0;
1934#endif
1935
1936  if( uiNum4x4Blk > 1 )
1937  {
1938    Bool b1stBlk  = true;
1939    UInt uiNumOne = 0;
1940   
1941    for( UInt uiSubBlk = 0; uiSubBlk < uiNum4x4Blk; uiSubBlk++ )
1942    {
1943      UInt uiCtxSet    = 0;
1944      UInt uiSubNumSig = 0;
1945      UInt uiSubPosX   = 0;
1946      UInt uiSubPosY   = 0;
1947#if E253
1948      uiGoRiceParam    = 0;
1949#endif
1950
1951      uiSubPosX = g_auiFrameScanX[ g_aucConvertToBit[ uiWidth ] - 1 ][ uiSubBlk ] << 2;
1952      uiSubPosY = g_auiFrameScanY[ g_aucConvertToBit[ uiWidth ] - 1 ][ uiSubBlk ] << 2;
1953     
1954      TCoeff* piCurr = &pcCoef[ uiSubPosX + uiSubPosY * uiWidth ];
1955     
1956      for( UInt uiY = 0; uiY < 4; uiY++ )
1957      {
1958        for( UInt uiX = 0; uiX < 4; uiX++ )
1959        {
1960          if( piCurr[ uiX ] )
1961          {
1962            uiSubNumSig++;
1963          }
1964        }
1965        piCurr += uiWidth;
1966      }
1967     
1968      if( uiSubNumSig > 0 )
1969      {
1970        c1 = 1;
1971        c2 = 0;
1972       
1973        if( b1stBlk )
1974        {
1975          b1stBlk  = false;
1976          uiCtxSet = 5;
1977        }
1978        else
1979        {
1980          uiCtxSet = ( uiNumOne >> 2 ) + 1;
1981          uiNumOne = 0;
1982        }
1983       
1984        for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
1985        {
1986          UInt  uiBlkPos  = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
1987          UInt  uiPosY    = uiBlkPos >> 2;
1988          UInt  uiPosX    = uiBlkPos - ( uiPosY << 2 );
1989          UInt  uiIndex   = ( ( uiSubPosY + uiPosY ) << uiLog2BlockSize ) + uiSubPosX + uiPosX;
1990         
1991          if( pcCoef[ uiIndex ]  )
1992          {
1993            if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
1994            else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
1995           
1996            UInt uiCtx    = min<UInt>(c1, 4);
1997            UInt uiSymbol = uiAbs > 1 ? 1 : 0;
1998            m_pcBinIf->encodeBin( uiSymbol, m_cCUOneSCModel.get( 0, eTType, ( uiCtxSet << 2 ) + uiCtxSet + uiCtx ) );
1999           
2000            if( uiSymbol )
2001            {
2002              c1     = 0;
2003            }
2004            else if( c1 )
2005            {
2006              c1++;
2007            }
2008          }
2009        }
2010       
2011        for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
2012        {
2013          UInt  uiBlkPos  = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
2014          UInt  uiPosY    = uiBlkPos >> 2;
2015          UInt  uiPosX    = uiBlkPos - ( uiPosY << 2 );
2016          UInt  uiIndex   = ( ( uiSubPosY + uiPosY ) << uiLog2BlockSize ) + uiSubPosX + uiPosX;
2017         
2018          if( pcCoef[ uiIndex ]  )
2019          {
2020            if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
2021            else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
2022           
2023            UInt uiSymbol = uiAbs > 1 ? 1 : 0;
2024           
2025            if( uiSymbol )
2026            {
2027              UInt uiCtx  = min<UInt>(c2, 4);
2028              uiAbs -= 2;
2029              c2++;
2030              uiNumOne++;
2031#if E253
2032              uiSymbol = uiAbs > 0 ? 1 : 0;
2033
2034              m_pcBinIf->encodeBin( uiSymbol, m_cCUAbsSCModel.get( 0, eTType, ( uiCtxSet << 2 ) + uiCtxSet + uiCtx ) );
2035
2036              if( uiSymbol )
2037              {
2038                uiAbs -= 1;
2039                xWriteGoRiceExGolomb( uiAbs, uiGoRiceParam );
2040              }
2041#else
2042              xWriteExGolombLevel( uiAbs, m_cCUAbsSCModel.get( 0, eTType, ( uiCtxSet << 2 ) + uiCtxSet + uiCtx ) );
2043#endif
2044            }
2045          }
2046        }
2047       
2048        for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
2049        {
2050          UInt  uiBlkPos  = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
2051          UInt  uiPosY    = uiBlkPos >> 2;
2052          UInt  uiPosX    = uiBlkPos - ( uiPosY << 2 );
2053          UInt  uiIndex   = ( ( uiSubPosY + uiPosY ) << uiLog2BlockSize ) + uiSubPosX + uiPosX;
2054         
2055          if( pcCoef[ uiIndex ]  )
2056          {
2057            if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
2058            else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
2059            m_pcBinIf->encodeBinEP( uiSign );
2060          }
2061        }
2062       
2063      }
2064    }
2065  }
2066  else
2067  {
2068    c1 = 1;
2069    c2 = 0;
2070   
2071    for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
2072    {
2073      UInt uiIndex = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
2074     
2075      if( pcCoef[ uiIndex ]  )
2076      {
2077        if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
2078        else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
2079       
2080        UInt uiCtx    = min<UInt>(c1, 4);
2081        UInt uiSymbol = uiAbs > 1 ? 1 : 0;
2082        m_pcBinIf->encodeBin( uiSymbol, m_cCUOneSCModel.get( 0, eTType, uiCtx ) );
2083       
2084        if( uiSymbol )
2085        {
2086          c1 = 0;
2087        }
2088        else if( c1 )
2089        {
2090          c1++;
2091        }
2092      }
2093    }
2094   
2095    for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
2096    {
2097      UInt uiIndex = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
2098     
2099      if( pcCoef[ uiIndex ]  )
2100      {
2101        if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
2102        else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
2103       
2104        UInt uiSymbol = uiAbs > 1 ? 1 : 0;
2105       
2106        if( uiSymbol )
2107        {
2108          UInt uiCtx  = min<UInt>(c2, 4);
2109          uiAbs -= 2;
2110          c2++;
2111#if E253
2112          uiSymbol = uiAbs > 0 ? 1 : 0;
2113
2114          m_pcBinIf->encodeBin( uiSymbol, m_cCUAbsSCModel.get( 0, eTType, uiCtx ) );
2115
2116          if( uiSymbol )
2117          {
2118            uiAbs -= 1;
2119            xWriteGoRiceExGolomb( uiAbs, uiGoRiceParam );
2120          }
2121#else
2122          xWriteExGolombLevel( uiAbs, m_cCUAbsSCModel.get( 0, eTType, uiCtx ) );
2123#endif
2124        }
2125      }
2126    }
2127   
2128    for( UInt uiScanPos = 0; uiScanPos < 16; uiScanPos++ )
2129    {
2130      UInt uiIndex = g_auiFrameScanXY[ 1 ][ 15 - uiScanPos ];
2131     
2132      if( pcCoef[ uiIndex ]  )
2133      {
2134        if( pcCoef[ uiIndex ] > 0) { uiAbs = static_cast<UInt>( pcCoef[ uiIndex ]);  uiSign = 0; }
2135        else                       { uiAbs = static_cast<UInt>(-pcCoef[ uiIndex ]);  uiSign = 1; }
2136       
2137        m_pcBinIf->encodeBinEP( uiSign );
2138      }
2139    }
2140  }
2141  return;
2142}
2143
2144#if MVD_CTX
2145/** Encode a motion vector difference
2146 * \param iMvd motion vector difference
2147 * \param uiAbsSumL motion vector difference of left PU
2148 * \param uiAbsSumA motion vector difference of above PU
2149 * \param uiCtx index for context set based on vertical or horizontal component
2150 */
2151Void TEncSbac::xWriteMvd( Int iMvd, UInt uiAbsSumL, UInt uiAbsSumA, UInt uiCtx )
2152#else
2153Void TEncSbac::xWriteMvd( Int iMvd, UInt uiAbsSum, UInt uiCtx )
2154#endif
2155{
2156  UInt uiLocalCtx = 0;
2157#if MVD_CTX
2158  uiLocalCtx += (uiAbsSumA>16) ? 1 : 0;
2159  uiLocalCtx += (uiAbsSumL>16) ? 1 : 0;
2160#else
2161  if ( uiAbsSum >= 3 )
2162  {
2163    uiLocalCtx += ( uiAbsSum > 32 ) ? 2 : 1;
2164  }
2165#endif
2166
2167  UInt uiSymbol = ( 0 == iMvd ) ? 0 : 1;
2168  m_pcBinIf->encodeBin( uiSymbol, m_cCUMvdSCModel.get( 0, uiCtx, uiLocalCtx ) );
2169  if ( 0 == uiSymbol )
2170  {
2171    return;
2172  }
2173 
2174  UInt uiSign = 0;
2175  if ( 0 > iMvd )
2176  {
2177    uiSign = 1;
2178    iMvd   = -iMvd;
2179  }
2180  xWriteExGolombMvd( iMvd-1, &m_cCUMvdSCModel.get( 0, uiCtx, 3 ), 3 );
2181  m_pcBinIf->encodeBinEP( uiSign );
2182 
2183  return;
2184}
2185
2186Void  TEncSbac::xWriteExGolombMvd( UInt uiSymbol, ContextModel* pcSCModel, UInt uiMaxBin )
2187{
2188  if ( ! uiSymbol )
2189  {
2190    m_pcBinIf->encodeBin( 0, *pcSCModel );
2191    return;
2192  }
2193 
2194  m_pcBinIf->encodeBin( 1, *pcSCModel );
2195 
2196  Bool bNoExGo = ( uiSymbol < 8 );
2197  UInt uiCount = 1;
2198  pcSCModel++;
2199 
2200  while ( --uiSymbol && ++uiCount <= 8 )
2201  {
2202    m_pcBinIf->encodeBin( 1, *pcSCModel );
2203    if ( uiCount == 2 )
2204    {
2205      pcSCModel++;
2206    }
2207    if ( uiCount == uiMaxBin )
2208    {
2209      pcSCModel++;
2210    }
2211  }
2212 
2213  if ( bNoExGo )
2214  {
2215    m_pcBinIf->encodeBin( 0, *pcSCModel );
2216  }
2217  else
2218  {
2219    xWriteEpExGolomb( uiSymbol, 3 );
2220  }
2221 
2222  return;
2223}
2224
2225Void TEncSbac::codeAlfFlag       ( UInt uiCode )
2226{
2227  UInt uiSymbol = ( ( uiCode == 0 ) ? 0 : 1 );
2228  m_pcBinIf->encodeBin( uiSymbol, m_cALFFlagSCModel.get( 0, 0, 0 ) );
2229}
2230
2231#if TSB_ALF_HEADER
2232Void TEncSbac::codeAlfFlagNum( UInt uiCode, UInt minValue )
2233{
2234  UInt uiLength = 0;
2235  UInt maxValue = (minValue << (this->getMaxAlfCtrlDepth()*2));
2236  assert((uiCode>=minValue)&&(uiCode<=maxValue));
2237  UInt temp = maxValue - minValue;
2238  for(UInt i=0; i<32; i++)
2239  {
2240    if(temp&0x1)
2241    {
2242      uiLength = i+1;
2243    }
2244    temp = (temp >> 1);
2245  }
2246  UInt uiSymbol = uiCode - minValue;
2247  if(uiLength)
2248  {
2249    while( uiLength-- )
2250    {
2251      m_pcBinIf->encodeBinEP( (uiSymbol>>uiLength) & 0x1 );
2252    }
2253  }
2254}
2255
2256Void TEncSbac::codeAlfCtrlFlag( UInt uiSymbol )
2257{
2258  m_pcBinIf->encodeBin( uiSymbol, m_cCUAlfCtrlFlagSCModel.get( 0, 0, 0) );
2259}
2260#endif
2261
2262Void TEncSbac::codeAlfUvlc       ( UInt uiCode )
2263{
2264  Int i;
2265 
2266  if ( uiCode == 0 )
2267  {
2268    m_pcBinIf->encodeBin( 0, m_cALFUvlcSCModel.get( 0, 0, 0 ) );
2269  }
2270  else
2271  {
2272    m_pcBinIf->encodeBin( 1, m_cALFUvlcSCModel.get( 0, 0, 0 ) );
2273    for ( i=0; i<uiCode-1; i++ )
2274    {
2275      m_pcBinIf->encodeBin( 1, m_cALFUvlcSCModel.get( 0, 0, 1 ) );
2276    }
2277    m_pcBinIf->encodeBin( 0, m_cALFUvlcSCModel.get( 0, 0, 1 ) );
2278  }
2279}
2280
2281Void TEncSbac::codeAlfSvlc       ( Int iCode )
2282{
2283  Int i;
2284 
2285  if ( iCode == 0 )
2286  {
2287    m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 0 ) );
2288  }
2289  else
2290  {
2291    m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 0 ) );
2292   
2293    // write sign
2294    if ( iCode > 0 )
2295    {
2296      m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 1 ) );
2297    }
2298    else
2299    {
2300      m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 1 ) );
2301      iCode = -iCode;
2302    }
2303   
2304    // write magnitude
2305    for ( i=0; i<iCode-1; i++ )
2306    {
2307      m_pcBinIf->encodeBin( 1, m_cALFSvlcSCModel.get( 0, 0, 2 ) );
2308    }
2309    m_pcBinIf->encodeBin( 0, m_cALFSvlcSCModel.get( 0, 0, 2 ) );
2310  }
2311}
2312
2313#if MTK_SAO
2314Void TEncSbac::codeAoFlag       ( UInt uiCode )
2315{
2316  UInt uiSymbol = ( ( uiCode == 0 ) ? 0 : 1 );
2317  m_pcBinIf->encodeBin( uiSymbol, m_cAOFlagSCModel.get( 0, 0, 0 ) );
2318}
2319Void TEncSbac::codeAoUvlc       ( UInt uiCode )
2320{
2321  Int i;
2322
2323  if ( uiCode == 0 )
2324  {
2325    m_pcBinIf->encodeBin( 0, m_cAOUvlcSCModel.get( 0, 0, 0 ) );
2326  }
2327  else
2328  {
2329    m_pcBinIf->encodeBin( 1, m_cAOUvlcSCModel.get( 0, 0, 0 ) );
2330    for ( i=0; i<uiCode-1; i++ )
2331    {
2332      m_pcBinIf->encodeBin( 1, m_cAOUvlcSCModel.get( 0, 0, 1 ) );
2333    }
2334    m_pcBinIf->encodeBin( 0, m_cAOUvlcSCModel.get( 0, 0, 1 ) );
2335  }
2336}
2337Void TEncSbac::codeAoSvlc       ( Int iCode )
2338{
2339  Int i;
2340
2341  if ( iCode == 0 )
2342  {
2343    m_pcBinIf->encodeBin( 0, m_cAOSvlcSCModel.get( 0, 0, 0 ) );
2344  }
2345  else
2346  {
2347    m_pcBinIf->encodeBin( 1, m_cAOSvlcSCModel.get( 0, 0, 0 ) );
2348
2349    // write sign
2350    if ( iCode > 0 )
2351    {
2352      m_pcBinIf->encodeBin( 0, m_cAOSvlcSCModel.get( 0, 0, 1 ) );
2353    }
2354    else
2355    {
2356      m_pcBinIf->encodeBin( 1, m_cAOSvlcSCModel.get( 0, 0, 1 ) );
2357      iCode = -iCode;
2358    }
2359
2360    // write magnitude
2361    for ( i=0; i<iCode-1; i++ )
2362    {
2363      m_pcBinIf->encodeBin( 1, m_cAOSvlcSCModel.get( 0, 0, 2 ) );
2364    }
2365    m_pcBinIf->encodeBin( 0, m_cAOSvlcSCModel.get( 0, 0, 2 ) );
2366  }
2367}
2368#endif
2369
2370/*!
2371 ****************************************************************************
2372 * \brief
2373 *   estimate bit cost for CBP, significant map and significant coefficients
2374 ****************************************************************************
2375 */
2376Void TEncSbac::estBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2377{
2378  estCBFBit( pcEstBitsSbac, 0, eTType );
2379 
2380  // encode significance map
2381  estSignificantMapBit( pcEstBitsSbac, uiCTXIdx, eTType );
2382 
2383  // encode significant coefficients
2384  estSignificantCoefficientsBit( pcEstBitsSbac, uiCTXIdx, eTType );
2385}
2386
2387/*!
2388 ****************************************************************************
2389 * \brief
2390 *    estimate bit cost for each CBP bit
2391 ****************************************************************************
2392 */
2393Void TEncSbac::estCBFBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2394{
2395  ContextModel *pCtx = m_cCUQtCbfSCModel.get( 0 );
2396
2397  for( UInt uiCtxInc = 0; uiCtxInc < 45; uiCtxInc++ )
2398  {
2399    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 0 ] = biari_no_bits( 0, pCtx[ uiCtxInc ] );
2400    pcEstBitsSbac->blockCbpBits[ uiCtxInc ][ 1 ] = biari_no_bits( 1, pCtx[ uiCtxInc ] );
2401  }
2402
2403  for( UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++ )
2404  {
2405    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 0 ] = biari_no_bits( 0, m_cCUQtRootCbfSCModel.get( 0, 0, uiCtxInc ) );
2406    pcEstBitsSbac->blockRootCbpBits[ uiCtxInc ][ 1 ] = biari_no_bits( 1, m_cCUQtRootCbfSCModel.get( 0, 0, uiCtxInc ) );
2407  }
2408}
2409
2410
2411/*!
2412 ****************************************************************************
2413 * \brief
2414 *    estimate SAMBAC bit cost for significant coefficient map
2415 ****************************************************************************
2416 */
2417Void TEncSbac::estSignificantMapBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2418{
2419  for ( UInt uiCtx = 0; uiCtx < 16; uiCtx++ )
2420  {
2421    for( UInt uiBin = 0; uiBin < 2; uiBin++ )
2422    {
2423#if SIMPLE_CONTEXT_SIG
2424      if( uiCtx < 4 || eTType )
2425      {
2426        pcEstBitsSbac->significantBits[ uiCtx ][ uiBin ] = biari_no_bits ( uiBin, m_cCUSigSCModel.get( uiCTXIdx-2, eTType, uiCtx ) );
2427      }
2428      else
2429      {
2430        pcEstBitsSbac->significantBits[ uiCtx ][ uiBin ] = biari_no_bits ( uiBin, m_cCUSigSCModel.get( uiCTXIdx-2 ? uiCTXIdx-2 : 1, eTType, uiCtx ) );
2431      }
2432#else
2433      pcEstBitsSbac->significantBits[ uiCtx ][ uiBin ] = biari_no_bits ( uiBin, m_cCUSigSCModel.get(  uiCTXIdx, eTType, uiCtx ) );
2434#endif
2435
2436#if !PCP_SIGMAP_SIMPLE_LAST   
2437      pcEstBitsSbac->lastBits[ uiCtx ][ uiBin ]        = biari_no_bits ( uiBin, m_cCULastSCModel.get( uiCTXIdx, eTType, uiCtx ) );
2438#endif
2439    }
2440  }
2441
2442#if PCP_SIGMAP_SIMPLE_LAST
2443  Int iBitsX = 0, iBitsY = 0;
2444  const UInt uiCtxOffset = g_uiCtxXYOffset[uiCTXIdx];
2445  const UInt uiWidthM1   = (1 << (7-uiCTXIdx)) - 1;
2446  for ( UInt uiCtx = 0; uiCtx < uiWidthM1; uiCtx++ )
2447  {
2448    pcEstBitsSbac->lastXBits[uiCtx] = iBitsX + biari_no_bits (1, m_cCuCtxLastX.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtx] ));
2449    pcEstBitsSbac->lastYBits[uiCtx] = iBitsY + biari_no_bits (1, m_cCuCtxLastY.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtx] ));
2450    iBitsX += biari_no_bits (0, m_cCuCtxLastX.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtx] ));
2451    iBitsY += biari_no_bits (0, m_cCuCtxLastY.get( 0, eTType, uiCtxOffset + g_uiCtxXY[uiCtx] ));
2452  }
2453  pcEstBitsSbac->lastXBits[uiWidthM1] = iBitsX;
2454  pcEstBitsSbac->lastYBits[uiWidthM1] = iBitsY;
2455#endif
2456}
2457
2458/*!
2459 ****************************************************************************
2460 * \brief
2461 *    estimate bit cost of significant coefficient
2462 ****************************************************************************
2463 */
2464Void TEncSbac::estSignificantCoefficientsBit( estBitsSbacStruct* pcEstBitsSbac, UInt uiCTXIdx, TextType eTType )
2465{
2466  for( UInt uiSet = 0; uiSet < 6; uiSet++ )
2467  {
2468    for( UInt uiCtx = 0; uiCtx < 5; uiCtx++ )
2469    {
2470      pcEstBitsSbac->greaterOneBits[ uiSet ][ 0 ][ uiCtx ][ 0 ] = biari_no_bits( 0, m_cCUOneSCModel.get( 0, eTType, ( uiSet << 2 ) + uiSet + uiCtx ) );
2471      pcEstBitsSbac->greaterOneBits[ uiSet ][ 0 ][ uiCtx ][ 1 ] = biari_no_bits( 1, m_cCUOneSCModel.get( 0, eTType, ( uiSet << 2 ) + uiSet + uiCtx ) );
2472      pcEstBitsSbac->greaterOneBits[ uiSet ][ 1 ][ uiCtx ][ 0 ] = biari_no_bits( 0, m_cCUAbsSCModel.get( 0, eTType, ( uiSet << 2 ) + uiSet + uiCtx ) );
2473      pcEstBitsSbac->greaterOneBits[ uiSet ][ 1 ][ uiCtx ][ 1 ] = biari_no_bits( 1, m_cCUAbsSCModel.get( 0, eTType, ( uiSet << 2 ) + uiSet + uiCtx ) );
2474    }
2475  }
2476}
2477
2478Int TEncSbac::biari_no_bits( Short symbol, ContextModel& rcSCModel )
2479{
2480  UInt  uiEstBits;
2481  Short ui16State;
2482 
2483  symbol    = (Short)( symbol != 0 );
2484  ui16State = symbol == rcSCModel.getMps() ? 64 + rcSCModel.getState() : 63 - rcSCModel.getState();
2485  uiEstBits = entropyBits[ 127 - ui16State ];
2486  return uiEstBits;
2487}
Note: See TracBrowser for help on using the repository browser.