Ticket #133: TDecCAVLC.cpp

File TDecCAVLC.cpp, 61.9 KB (added by libin, 13 years ago)
Line 
1/* ====================================================================================================================
2
3  The copyright in this software is being made available under the License included below.
4  This software may be subject to other third party and   contributor rights, including patent rights, and no such
5  rights are granted under this license.
6
7  Copyright (c) 2010, SAMSUNG ELECTRONICS CO., LTD. and BRITISH BROADCASTING CORPORATION
8  All rights reserved.
9
10  Redistribution and use in source and binary forms, with or without modification, are permitted only for
11  the purpose of developing standards within the Joint Collaborative Team on Video Coding and for testing and
12  promoting such standards. The following conditions are required to be met:
13
14    * Redistributions of source code must retain the above copyright notice, this list of conditions and
15      the following disclaimer.
16    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17      the following disclaimer in the documentation and/or other materials provided with the distribution.
18    * Neither the name of SAMSUNG ELECTRONICS CO., LTD. nor the name of the BRITISH BROADCASTING CORPORATION
19      may be used to endorse or promote products derived from this software without specific prior written permission.
20
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 * ====================================================================================================================
30*/
31
32/** \file     TDecCAVLC.cpp
33    \brief    CAVLC decoder class
34*/
35
36#include "TDecCAVLC.h"
37
38// ====================================================================================================================
39// Constructor / destructor / create / destroy
40// ====================================================================================================================
41
42TDecCavlc::TDecCavlc()
43{
44  m_bAlfCtrl = false;
45  m_uiMaxAlfCtrlDepth = 0;
46}
47
48TDecCavlc::~TDecCavlc()
49{
50 
51}
52
53// ====================================================================================================================
54// Public member functions
55// ====================================================================================================================
56
57Void TDecCavlc::parsePPS(TComPPS* pcPPS)
58{
59  UInt  uiCode;
60 
61  xReadCode ( 2, uiCode ); //NalRefIdc
62  xReadCode ( 1, uiCode ); assert( 0 == uiCode); // zero bit
63  xReadCode ( 5, uiCode ); assert( NAL_UNIT_PPS == uiCode);//NalUnitType
64  return;
65}
66
67Void TDecCavlc::parseSPS(TComSPS* pcSPS)
68{
69  UInt  uiCode;
70  xReadCode ( 2, uiCode ); //NalRefIdc
71  xReadCode ( 1, uiCode ); assert( 0 == uiCode); // zero bit
72  xReadCode ( 5, uiCode ); assert( NAL_UNIT_SPS == uiCode);//NalUnitType
73 
74  // Structure
75  xReadUvlc ( uiCode ); pcSPS->setWidth       ( uiCode    );
76  xReadUvlc ( uiCode ); pcSPS->setHeight      ( uiCode    );
77  xReadUvlc ( uiCode ); pcSPS->setPadX        ( uiCode    );
78  xReadUvlc ( uiCode ); pcSPS->setPadY        ( uiCode    );
79 
80  xReadUvlc ( uiCode ); 
81  pcSPS->setMaxCUWidth  ( uiCode    ); g_uiMaxCUWidth  = uiCode;
82  pcSPS->setMaxCUHeight ( uiCode    ); g_uiMaxCUHeight = uiCode;
83 
84  xReadUvlc ( uiCode ); 
85  pcSPS->setMaxCUDepth  ( uiCode+1  ); g_uiMaxCUDepth  = uiCode + 1;
86  UInt uiMaxCUDepthCorrect = uiCode;
87 
88  xReadUvlc( uiCode ); pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
89  xReadUvlc( uiCode ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
90  pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) ); 
91  xReadUvlc ( uiCode ); pcSPS->setQuadtreeTUMaxDepthInter( uiCode+1 );
92  xReadUvlc ( uiCode ); pcSPS->setQuadtreeTUMaxDepthIntra( uiCode+1 );
93  g_uiAddCUDepth = 0;
94  while( ( pcSPS->getMaxCUWidth() >> uiMaxCUDepthCorrect ) > ( 1 << ( pcSPS->getQuadtreeTULog2MinSize() + g_uiAddCUDepth )  ) ) g_uiAddCUDepth++;   
95  pcSPS->setMaxCUDepth( uiMaxCUDepthCorrect+g_uiAddCUDepth  ); g_uiMaxCUDepth  = uiMaxCUDepthCorrect+g_uiAddCUDepth;
96  // BB: these parameters may be removed completly and replaced by the fixed values
97  pcSPS->setMinTrDepth( 0 );
98  pcSPS->setMaxTrDepth( 1 );
99 
100  // Tool on/off
101  xReadFlag( uiCode ); pcSPS->setUseALF ( uiCode ? true : false );
102  xReadFlag( uiCode ); pcSPS->setUseDQP ( uiCode ? true : false );
103  xReadFlag( uiCode ); pcSPS->setUseLDC ( uiCode ? true : false );
104#if HHI_MRG
105  xReadFlag( uiCode ); pcSPS->setUseMRG ( uiCode ? true : false ); // SOPH:
106#endif
107 
108#if HHI_RMP_SWITCH
109  xReadFlag( uiCode ); pcSPS->setUseRMP( uiCode ? true : false );
110#endif
111
112#if !DCTIF_8_6_LUMA
113  // number of taps for DIF
114  xReadUvlc( uiCode ); pcSPS->setDIFTap ( (uiCode+2)<<1 );  // 4, 6, 8, 10, 12
115#endif
116 
117  // AMVP mode for each depth (AM_NONE or AM_EXPL)
118  for (Int i = 0; i < pcSPS->getMaxCUDepth(); i++)
119  {
120    xReadFlag( uiCode );
121    pcSPS->setAMVPMode( i, (AMVP_MODE)uiCode );
122  }
123 
124  // Bit-depth information
125#if FULL_NBIT
126  xReadUvlc( uiCode );
127  g_uiBitDepth = 8 + uiCode;
128  g_uiBitIncrement = 0;
129  pcSPS->setBitDepth(g_uiBitDepth);
130  pcSPS->setBitIncrement(g_uiBitIncrement);
131#else
132#if ENABLE_IBDI
133  xReadUvlc( uiCode ); pcSPS->setBitDepth     ( uiCode+8 ); g_uiBitDepth     = uiCode + 8;
134  xReadUvlc( uiCode ); pcSPS->setBitIncrement ( uiCode   ); g_uiBitIncrement = uiCode;
135#else
136  xReadUvlc( uiCode );
137  g_uiBitDepth = 8;
138  g_uiBitIncrement = uiCode;
139  pcSPS->setBitDepth(g_uiBitDepth);
140  pcSPS->setBitIncrement(g_uiBitIncrement);
141#endif
142#endif
143 
144  g_uiBASE_MAX  = ((1<<(g_uiBitDepth))-1);
145 
146#if IBDI_NOCLIP_RANGE
147  g_uiIBDI_MAX  = g_uiBASE_MAX << g_uiBitIncrement;
148#else
149  g_uiIBDI_MAX  = ((1<<(g_uiBitDepth+g_uiBitIncrement))-1);
150#endif
151 
152  return;
153}
154
155Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice)
156{
157  UInt  uiCode;
158  Int   iCode;
159  xReadCode ( 2, uiCode ); //NalRefIdc
160  xReadCode ( 1, uiCode ); assert( 0 == uiCode); // zero bit
161#if DCM_DECODING_REFRESH
162  xReadCode ( 5, uiCode ); 
163  rpcSlice->setNalUnitType        ((NalUnitType)uiCode);//NalUnitType
164#else
165  xReadCode ( 5, uiCode ); assert( NAL_UNIT_CODED_SLICE == uiCode);//NalUnitType
166#endif
167 
168  xReadCode (10, uiCode);  rpcSlice->setPOC              (uiCode);             // 9 == SPS->Log2MaxFrameNum()
169  xReadUvlc (   uiCode);  rpcSlice->setSliceType        ((SliceType)uiCode);
170  xReadSvlc (    iCode);  rpcSlice->setSliceQp          (iCode);
171 
172  xReadFlag ( uiCode );
173  rpcSlice->setSymbolMode( uiCode );
174 
175  if (!rpcSlice->isIntra())
176    xReadFlag (   uiCode);
177  else
178    uiCode = 1;
179 
180  rpcSlice->setReferenced       (uiCode ? true : false);
181
182#if !HIGH_ACCURACY_BI
183#ifdef ROUNDING_CONTROL_BIPRED
184  if(!rpcSlice->isIntra())
185  {
186    xReadFlag( uiCode );
187    Bool b = (uiCode != 0);
188    rpcSlice->setRounding(b);
189  }
190#endif
191#else
192  if(!rpcSlice->isIntra())
193  {
194    rpcSlice->setRounding(false);
195  }
196#endif
197 
198  xReadFlag (   uiCode);  rpcSlice->setLoopFilterDisable(uiCode ? 1 : 0);
199 
200  if (!rpcSlice->isIntra())
201  {
202    xReadCode (3, uiCode);  rpcSlice->setNumRefIdx      (REF_PIC_LIST_0, uiCode);
203  }
204  else
205  {
206    rpcSlice->setNumRefIdx(REF_PIC_LIST_0, 0);
207  }
208  if (rpcSlice->isInterB())
209  {
210    xReadCode (3, uiCode);  rpcSlice->setNumRefIdx      (REF_PIC_LIST_1, uiCode);
211  }
212  else
213  {
214    rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
215  }
216 
217#if DCM_COMB_LIST
218  if (rpcSlice->isInterB())
219  {
220    xReadFlag (uiCode);      rpcSlice->setRefPicListCombinationFlag(uiCode ? 1 : 0);
221    if(uiCode)
222    {
223      xReadUvlc(uiCode);      rpcSlice->setNumRefIdx      (REF_PIC_LIST_C, uiCode+1);
224
225      xReadFlag (uiCode);     rpcSlice->setRefPicListModificationFlagLC(uiCode ? 1 : 0);
226      if(uiCode)
227      {
228        for (UInt i=0;i<rpcSlice->getNumRefIdx(REF_PIC_LIST_C);i++)
229        {
230          xReadFlag(uiCode);
231          rpcSlice->setListIdFromIdxOfLC(i, uiCode);
232          xReadUvlc(uiCode);
233          rpcSlice->setRefIdxFromIdxOfLC(i, uiCode);
234          rpcSlice->setRefIdxOfLC((RefPicList)rpcSlice->getListIdFromIdxOfLC(i), rpcSlice->getRefIdxFromIdxOfLC(i), i);
235        }
236      }
237    }
238    else
239    {
240      rpcSlice->setRefPicListCombinationFlag(false);
241      rpcSlice->setRefPicListModificationFlagLC(false);
242      rpcSlice->setNumRefIdx(REF_PIC_LIST_C, -1);
243    }
244  }
245#endif
246
247  xReadFlag (uiCode);     rpcSlice->setDRBFlag          (uiCode ? 1 : 0);
248  if ( !rpcSlice->getDRBFlag() )
249  {
250    xReadCode(2, uiCode); rpcSlice->setERBIndex( (ERBIndex)uiCode );    assert (uiCode == ERB_NONE || uiCode == ERB_LTR);
251  }
252#if !DCTIF_8_6_LUMA
253  xReadUvlc( uiCode ); rpcSlice->setInterpFilterType( uiCode );
254#endif
255 
256
257#if AMVP_NEIGH_COL
258  if ( rpcSlice->getSliceType() == B_SLICE )
259  {
260    xReadFlag (uiCode);
261    rpcSlice->setColDir(uiCode);
262  }
263#endif
264  return;
265}
266
267Void TDecCavlc::resetEntropy          (TComSlice* pcSlice)
268{
269  m_bRunLengthCoding = ! pcSlice->isIntra();
270  m_uiRun = 0;
271 
272  ::memcpy(m_uiLPTableD8,        g_auiLPTableD8,        10*128*sizeof(UInt));
273  ::memcpy(m_uiLPTableD4,        g_auiLPTableD4,        3*32*sizeof(UInt));
274  ::memcpy(m_uiLastPosVlcIndex,  g_auiLastPosVlcIndex,  10*sizeof(UInt));
275 
276  ::memcpy(m_uiCBPTableD,        g_auiCBPTableD,        2*8*sizeof(UInt));
277  m_uiCbpVlcIdx[0] = 0;
278  m_uiCbpVlcIdx[1] = 0;
279 
280#if QC_BLK_CBP
281  ::memcpy(m_uiBlkCBPTableD,     g_auiBlkCBPTableD,     2*15*sizeof(UInt));
282  m_uiBlkCbpVlcIdx = 0;
283#endif
284 
285  ::memcpy(m_uiMI1TableD,        g_auiMI1TableD,        8*sizeof(UInt));
286  ::memcpy(m_uiMI2TableD,        g_auiMI2TableD,        15*sizeof(UInt));
287 
288#if MS_NO_BACK_PRED_IN_B0
289#if DCM_COMB_LIST
290  if ( pcSlice->getNoBackPredFlag() || pcSlice->getNumRefIdx(REF_PIC_LIST_C)>0)
291#else
292  if ( pcSlice->getNoBackPredFlag() )
293#endif
294  {
295    ::memcpy(m_uiMI1TableD,        g_auiMI1TableDNoL1,        8*sizeof(UInt));
296    ::memcpy(m_uiMI2TableD,        g_auiMI2TableDNoL1,        15*sizeof(UInt));
297  }
298#endif
299 
300#if MS_LCEC_ONE_FRAME
301  if ( pcSlice->getNumRefIdx(REF_PIC_LIST_0) <= 1 && pcSlice->getNumRefIdx(REF_PIC_LIST_1) <= 1 )
302  {
303    if ( pcSlice->getNoBackPredFlag() || ( pcSlice->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcSlice->getNumRefIdx(REF_PIC_LIST_C) <= 1 ) )
304    {
305      ::memcpy(m_uiMI1TableD,        g_auiMI1TableDOnly1RefNoL1,        8*sizeof(UInt));
306    }
307    else
308    {
309      ::memcpy(m_uiMI1TableD,        g_auiMI1TableDOnly1Ref,        8*sizeof(UInt));
310    }
311  }
312#endif
313#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
314  if (pcSlice->getNumRefIdx(REF_PIC_LIST_C)>0)
315  {
316    m_uiMI1TableD[8] = 8;
317  }
318  else  // GPB case
319  {
320    m_uiMI1TableD[8] = m_uiMI1TableD[6];
321    m_uiMI1TableD[6] = 8;
322  }
323#endif
324 
325#if LCEC_INTRA_MODE
326  ::memcpy(m_uiIntraModeTableD17, g_auiIntraModeTableD17, 16*sizeof(UInt));
327  ::memcpy(m_uiIntraModeTableD34, g_auiIntraModeTableD34, 33*sizeof(UInt));
328#endif
329#if QC_LCEC_INTER_MODE
330  ::memcpy(m_uiSplitTableD, g_auiInterModeTableD, 4*7*sizeof(UInt));
331#endif
332  m_uiMITableVlcIdx = 0;
333}
334
335Void TDecCavlc::parseTerminatingBit( UInt& ruiBit )
336{
337#if BUGFIX102
338  ruiBit = false;
339#else
340  xReadFlag( ruiBit );
341#endif
342}
343
344Void TDecCavlc::parseAlfCtrlDepth              ( UInt& ruiAlfCtrlDepth )
345{
346  UInt uiSymbol;
347  xReadUnaryMaxSymbol(uiSymbol, g_uiMaxCUDepth-1);
348  ruiAlfCtrlDepth = uiSymbol;
349}
350
351Void TDecCavlc::parseAlfCtrlFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
352{
353  if (!m_bAlfCtrl)
354    return;
355 
356  if( uiDepth > m_uiMaxAlfCtrlDepth && !pcCU->isFirstAbsZorderIdxInDepth(uiAbsPartIdx, m_uiMaxAlfCtrlDepth))
357  {
358    return;
359  }
360 
361  UInt uiSymbol;
362  xReadFlag( uiSymbol );
363 
364  if (uiDepth > m_uiMaxAlfCtrlDepth)
365  {
366    pcCU->setAlfCtrlFlagSubParts( uiSymbol, uiAbsPartIdx, m_uiMaxAlfCtrlDepth);
367  }
368  else
369  {
370    pcCU->setAlfCtrlFlagSubParts( uiSymbol, uiAbsPartIdx, uiDepth );
371  }
372}
373
374Void TDecCavlc::parseSkipFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
375{
376#if QC_LCEC_INTER_MODE
377  return;
378#else
379#if HHI_MRG && !SAMSUNG_MRG_SKIP_DIRECT
380  if ( pcCU->getSlice()->getSPS()->getUseMRG() )
381  {
382    return;
383  }
384#endif
385 
386  if( pcCU->getSlice()->isIntra() )
387  {
388    return;
389  }
390 
391  UInt uiSymbol;
392  xReadFlag( uiSymbol );
393 
394  if( uiSymbol )
395  {
396    pcCU->setPredModeSubParts( MODE_SKIP,  uiAbsPartIdx, uiDepth );
397    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
398    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
399   
400    TComMv cZeroMv(0,0);
401    pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
402    pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
403   
404    pcCU->setTrIdxSubParts( 0, uiAbsPartIdx, uiDepth );
405    pcCU->setCbfSubParts  ( 0, 0, 0, uiAbsPartIdx, uiDepth );
406   
407    if ( pcCU->getSlice()->isInterP() )
408    {
409      pcCU->setInterDirSubParts( 1, uiAbsPartIdx, 0, uiDepth );
410     
411      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
412        pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllRefIdx(  0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
413      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 )
414        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( NOT_VALID, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
415    }
416    else
417    {
418      pcCU->setInterDirSubParts( 3, uiAbsPartIdx, 0, uiDepth );
419     
420      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
421        pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllRefIdx(  0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
422      if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 )
423        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( 0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
424    }
425  }
426#endif
427}
428
429Void TDecCavlc::parseMVPIdx( TComDataCU* pcCU, Int& riMVPIdx, Int iMVPNum, UInt uiAbsPartIdx, UInt uiDepth, RefPicList eRefList )
430{
431  UInt uiSymbol;
432  xReadUnaryMaxSymbol(uiSymbol, iMVPNum-1);
433  riMVPIdx = uiSymbol;
434}
435#if QC_LCEC_INTER_MODE
436Void TDecCavlc::parseSplitFlag     ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
437{
438  if (pcCU->getSlice()->isIntra())
439  {
440    if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
441    {
442      pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
443      return ;
444    }
445
446    UInt uiSymbol;
447    xReadFlag( uiSymbol );
448    pcCU->setDepthSubParts( uiDepth + uiSymbol, uiAbsPartIdx );
449
450    return ;
451  }
452  UInt tmp=0;
453  UInt cx=0;
454  UInt uiMode ;
455  {
456    UInt iMaxLen= (uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth)?5:6;
457    while (tmp==0 && cx<iMaxLen)
458    {
459      xReadFlag( tmp );
460      cx++;
461    };
462    if(tmp!=0)
463      cx--;
464
465#if LCEC_INTER_MODE_BUG_FIX
466  UInt uiDepthRemember = uiDepth;
467  if ( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
468  {
469    uiDepth = 3;
470  }
471#endif
472    UInt x = m_uiSplitTableD[uiDepth][cx];
473    /* Adapt table */
474    uiMode = x;
475    if (cx>0)
476    {   
477      UInt cy = Max(0,cx-1);
478      UInt y = m_uiSplitTableD[uiDepth][cy];
479      m_uiSplitTableD[uiDepth][cy] = x;
480      m_uiSplitTableD[uiDepth][cx] = y;
481    }
482#if LCEC_INTER_MODE_BUG_FIX
483    uiDepth = uiDepthRemember;
484#endif
485  }
486  if (uiMode==0)
487  {
488    pcCU->setDepthSubParts( uiDepth + 1, uiAbsPartIdx );
489  }
490  else
491  {
492    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
493    pcCU->setMergeFlagSubParts(false, uiAbsPartIdx,0, uiDepth );
494    pcCU->setDepthSubParts( uiDepth    , uiAbsPartIdx );
495    if (uiMode ==1)
496    {
497      TComMv cZeroMv(0,0);
498      pcCU->setPredModeSubParts( MODE_SKIP,  uiAbsPartIdx, uiDepth );
499      pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
500      pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
501      pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
502      pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
503      pcCU->setTrIdxSubParts( 0, uiAbsPartIdx, uiDepth );
504      pcCU->setCbfSubParts  ( 0, 0, 0, uiAbsPartIdx, uiDepth );
505     
506      if ( pcCU->getSlice()->isInterP() )
507      {
508        pcCU->setInterDirSubParts( 1, uiAbsPartIdx, 0, uiDepth );
509       
510        if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
511          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllRefIdx(  0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
512        if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 )
513          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( NOT_VALID, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
514      }
515      else
516      {
517        pcCU->setInterDirSubParts( 3, uiAbsPartIdx, 0, uiDepth );
518       
519        if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
520          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllRefIdx(  0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
521        if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 )
522          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( 0, SIZE_2Nx2N, uiAbsPartIdx, 0, uiDepth );
523      }
524    }
525    else if (uiMode==2)
526    {
527      pcCU->setPredModeSubParts( MODE_INTER, uiAbsPartIdx, uiDepth );
528      pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
529      pcCU->setMergeFlagSubParts(true, uiAbsPartIdx,0, uiDepth );
530      pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
531    }
532    else if (uiMode==6)
533    {
534#if MTK_DISABLE_INTRA_NxN_SPLIT && HHI_DISABLE_INTER_NxN_SPLIT
535      if (uiDepth != g_uiMaxCUDepth - g_uiAddCUDepth)
536      {
537        pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
538        pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
539        pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
540        UInt uiTrLevel = 0;
541        UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)]+2;
542        UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()]+2;
543        uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
544              pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );       
545      }
546      else
547#endif
548      {
549        pcCU->setPredModeSubParts( MODE_INTER, uiAbsPartIdx, uiDepth );
550        pcCU->setPartSizeSubParts( SIZE_NxN, uiAbsPartIdx, uiDepth );
551      }
552    }
553    else
554    {
555      pcCU->setPredModeSubParts( MODE_INTER, uiAbsPartIdx, uiDepth );
556      pcCU->setPartSizeSubParts( PartSize(uiMode-3), uiAbsPartIdx, uiDepth );
557      pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
558    }
559  }
560}
561#else
562Void TDecCavlc::parseSplitFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
563{
564  if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth )
565  {
566    pcCU->setDepthSubParts( uiDepth, uiAbsPartIdx );
567    return ;
568  }
569 
570  UInt uiSymbol;
571  xReadFlag( uiSymbol );
572  pcCU->setDepthSubParts( uiDepth + uiSymbol, uiAbsPartIdx );
573 
574  return ;
575}
576#endif
577#if QC_LCEC_INTER_MODE
578Void TDecCavlc::parsePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
579{
580  if ( pcCU->isSkip( uiAbsPartIdx ))
581  {
582    return ;
583  }
584  UInt uiMode=0;
585  if ( pcCU->getSlice()->isIntra()&& pcCU->isIntra( uiAbsPartIdx ) )
586  {
587#if MTK_DISABLE_INTRA_NxN_SPLIT
588    uiMode = 1;
589    if ( uiDepth == (g_uiMaxCUDepth - g_uiAddCUDepth ))
590#endif
591    {
592      UInt uiSymbol;
593      xReadFlag( uiSymbol );
594      uiMode = uiSymbol ? 1 : 2;
595    }
596  }
597#if MTK_DISABLE_INTRA_NxN_SPLIT && HHI_DISABLE_INTER_NxN_SPLIT
598  else if (uiDepth != (g_uiMaxCUDepth - g_uiAddCUDepth ) || pcCU->getPartitionSize(uiAbsPartIdx ) != SIZE_NxN)
599#else
600  else if (pcCU->getPartitionSize(uiAbsPartIdx ) != SIZE_NxN)
601#endif
602  { 
603    return;
604  }
605  else
606  {
607    UInt uiSymbol;
608    xReadFlag( uiSymbol );
609    if(uiSymbol)
610    {
611      uiMode = 1;
612    }
613    else
614    {
615#if (MTK_DISABLE_INTRA_NxN_SPLIT && !HHI_DISABLE_INTER_NxN_SPLIT) || (!MTK_DISABLE_INTRA_NxN_SPLIT && HHI_DISABLE_INTER_NxN_SPLIT )
616      if ( uiDepth != (g_uiMaxCUDepth - g_uiAddCUDepth ))
617      {
618#if MTK_DISABLE_INTRA_NxN_SPLIT && !HHI_DISABLE_INTER_NxN_SPLIT
619        uiMode = 0;
620#elif !MTK_DISABLE_INTRA_NxN_SPLIT && HHI_DISABLE_INTER_NxN_SPLIT
621        uiMode = 2;
622#endif
623      }
624      else
625#endif
626      {
627        xReadFlag( uiSymbol );
628        uiMode = uiSymbol ? 2 : 0;
629      }
630    }
631  }
632  PartSize ePartSize;
633  PredMode eMode;
634  if (uiMode > 0)
635  {
636    eMode = MODE_INTRA;
637    ePartSize = (uiMode==1) ? SIZE_2Nx2N:SIZE_NxN;
638  }
639  else
640  {
641    eMode = MODE_INTER;
642    ePartSize = SIZE_NxN;
643  }
644  pcCU->setPredModeSubParts( eMode    , uiAbsPartIdx, uiDepth );
645  pcCU->setPartSizeSubParts( ePartSize, uiAbsPartIdx, uiDepth );
646  pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
647
648  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA )
649  {
650    UInt uiTrLevel = 0;
651    UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)] + 2;
652    UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()] + 2;
653    uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
654    if( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN )
655    {
656      pcCU->setTrIdxSubParts( 1 + uiTrLevel, uiAbsPartIdx, uiDepth );
657    }
658    else
659    {
660      pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );
661    }
662  }
663}
664#else
665Void TDecCavlc::parsePartSize( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
666{
667  if ( pcCU->isSkip( uiAbsPartIdx ) )
668  {
669    pcCU->setPartSizeSubParts( SIZE_2Nx2N, uiAbsPartIdx, uiDepth );
670    pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
671    return ;
672  }
673 
674  UInt uiSymbol, uiMode = 0;
675  PartSize eMode;
676 
677  if ( pcCU->isIntra( uiAbsPartIdx ) )
678  {
679#if MTK_DISABLE_INTRA_NxN_SPLIT
680    eMode = SIZE_2Nx2N;
681    if ( (g_uiMaxCUWidth >> uiDepth) == 8 )
682#endif
683    {
684      xReadFlag( uiSymbol );
685      eMode = uiSymbol ? SIZE_2Nx2N : SIZE_NxN;
686    }
687  }
688  else
689  {
690#if HHI_RMP_SWITCH
691    if ( !pcCU->getSlice()->getSPS()->getUseRMP())
692    {
693      xReadFlag( uiSymbol );
694      if( uiSymbol )
695        uiMode = 0;
696      else
697        uiMode = 3;
698    }
699    else
700#endif
701    {
702      UInt uiMaxNumBits = 3;
703      for ( UInt ui = 0; ui < uiMaxNumBits; ui++ )
704      {
705        xReadFlag( uiSymbol );
706        if ( uiSymbol )
707        {
708          break;
709        }
710        uiMode++;
711      }
712    }
713    eMode = (PartSize) uiMode;
714   
715    if (pcCU->getSlice()->isInterB() && uiMode == 3)
716    {
717#if HHI_DISABLE_INTER_NxN_SPLIT
718      uiSymbol = 0;
719      if( g_uiMaxCUWidth>>uiDepth == 8 )
720#endif
721      {
722        xReadFlag( uiSymbol );
723      }
724     
725      if (uiSymbol == 0)
726      {
727        pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
728#if MTK_DISABLE_INTRA_NxN_SPLIT
729        if ( (g_uiMaxCUWidth >> uiDepth) == 8 )
730#endif
731        {
732          xReadFlag( uiSymbol );
733        }
734        if (uiSymbol == 0)
735          eMode = SIZE_2Nx2N;
736      }
737    }
738   
739  }
740 
741  pcCU->setPartSizeSubParts( eMode, uiAbsPartIdx, uiDepth );
742  pcCU->setSizeSubParts( g_uiMaxCUWidth>>uiDepth, g_uiMaxCUHeight>>uiDepth, uiAbsPartIdx, uiDepth );
743 
744  UInt uiTrLevel = 0;
745 
746  UInt uiWidthInBit  = g_aucConvertToBit[pcCU->getWidth(uiAbsPartIdx)]+2;
747  UInt uiTrSizeInBit = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxTrSize()]+2;
748  uiTrLevel          = uiWidthInBit >= uiTrSizeInBit ? uiWidthInBit - uiTrSizeInBit : 0;
749 
750  if( pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTRA )
751  {
752    if( pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_NxN )
753    {
754      pcCU->setTrIdxSubParts( 1+uiTrLevel, uiAbsPartIdx, uiDepth );
755    }
756    else
757    {
758      pcCU->setTrIdxSubParts( uiTrLevel, uiAbsPartIdx, uiDepth );
759    }
760  }
761}
762#endif
763Void TDecCavlc::parsePredMode( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
764{
765  if( pcCU->getSlice()->isIntra() )
766  {
767    pcCU->setPredModeSubParts( MODE_INTRA, uiAbsPartIdx, uiDepth );
768    return ;
769  }
770#if !QC_LCEC_INTER_MODE 
771  UInt uiSymbol;
772  Int  iPredMode = MODE_INTER;
773 
774  if ( pcCU->getSlice()->isInterB() )
775  {
776    pcCU->setPredModeSubParts( (PredMode)iPredMode, uiAbsPartIdx, uiDepth );
777    return;
778  }
779 
780  if ( iPredMode != MODE_SKIP )
781  {
782    xReadFlag( uiSymbol );
783    iPredMode += uiSymbol;
784  }
785 
786  pcCU->setPredModeSubParts( (PredMode)iPredMode, uiAbsPartIdx, uiDepth );
787#endif
788}
789
790#if LCEC_INTRA_MODE
791Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
792{
793  UInt uiSymbol;
794  Int  uiIPredMode;
795  Int  iMostProbable = pcCU->getMostProbableIntraDirLuma( uiAbsPartIdx );
796  Int  iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
797  Int  iDir, iDirLarger, iRankIntraMode, iRankIntraModeLarger;
798
799  Int  iLeft          = pcCU->getLeftIntraDirLuma( uiAbsPartIdx );
800  Int  iAbove         = pcCU->getAboveIntraDirLuma( uiAbsPartIdx );
801  UInt ind=(iLeft==iAbove)? 0 : 1;
802
803  const UInt *huff17=huff17_2[ind];
804  const UInt *lengthHuff17=lengthHuff17_2[ind];
805  const UInt *huff34=huff34_2[ind];
806  const UInt *lengthHuff34=lengthHuff34_2[ind];
807
808  if ( g_aucIntraModeBitsAng[iIntraIdx] < 5 )
809  {
810    xReadFlag( uiSymbol );
811    if ( uiSymbol )
812      uiIPredMode = iMostProbable;
813    else
814    {
815      xReadFlag( uiSymbol ); uiIPredMode  = uiSymbol;
816      if ( g_aucIntraModeBitsAng[iIntraIdx] > 2 ) { xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 1; }
817      if ( g_aucIntraModeBitsAng[iIntraIdx] > 3 ) { xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 2; }
818      if(uiIPredMode >= iMostProbable) 
819        uiIPredMode ++;
820    }
821  }
822  else if ( g_aucIntraModeBitsAng[iIntraIdx] == 5 )
823  {
824    UInt uiCode;
825    UInt uiLength = lengthHuff17[15];
826    m_pcBitstream->pseudoRead(uiLength,uiCode);
827    if ((uiCode>>(uiLength- lengthHuff17[0])) == huff17[0])
828    {
829      m_pcBitstream->read(lengthHuff17[0],uiCode);
830      uiIPredMode = iMostProbable;
831    }
832    else
833    {
834      iRankIntraMode = 0;
835      for (Int i=1;i<17;i++)
836      { 
837        if( (uiCode>>(uiLength- lengthHuff17[i])) == huff17[i])
838        {
839          m_pcBitstream->read(lengthHuff17[i], uiCode);
840          iRankIntraMode = i;
841          break;
842        }
843      }
844     
845      if ( iRankIntraMode > 0 )
846        iRankIntraMode --;
847      iDir = m_uiIntraModeTableD17[iRankIntraMode];
848     
849      iRankIntraModeLarger = Max(0,iRankIntraMode-1);
850      iDirLarger = m_uiIntraModeTableD17[iRankIntraModeLarger];
851     
852      m_uiIntraModeTableD17[iRankIntraModeLarger] = iDir;
853      m_uiIntraModeTableD17[iRankIntraMode] = iDirLarger;
854     
855      uiIPredMode = (iDir>=iMostProbable? iDir+1: iDir);
856    }
857  }
858  else
859  {
860    UInt uiCode;
861    UInt uiLength = lengthHuff34[32];
862    m_pcBitstream->pseudoRead(uiLength,uiCode);
863    if ((uiCode>>(uiLength- lengthHuff34[0])) == huff34[0])
864    {
865      m_pcBitstream->read(lengthHuff34[0],uiCode);
866      uiIPredMode = iMostProbable;
867    }
868    else
869    {
870      iRankIntraMode = 0;
871      for (Int i=1;i<34;i++)
872      { 
873        if( (uiCode>>(uiLength- lengthHuff34[i])) == huff34[i])
874        {
875          m_pcBitstream->read(lengthHuff34[i], uiCode);
876          iRankIntraMode = i;
877          break;
878        }
879      }
880     
881      if ( iRankIntraMode > 0 )
882        iRankIntraMode --;
883      iDir = m_uiIntraModeTableD34[iRankIntraMode];
884     
885      iRankIntraModeLarger = Max(0,iRankIntraMode-1);
886      iDirLarger = m_uiIntraModeTableD34[iRankIntraModeLarger];
887     
888      m_uiIntraModeTableD34[iRankIntraModeLarger] = iDir;
889      m_uiIntraModeTableD34[iRankIntraMode] = iDirLarger;
890     
891      uiIPredMode = (iDir>=iMostProbable? iDir+1: iDir);
892    }
893  }
894 
895  pcCU->setLumaIntraDirSubParts( (UChar)uiIPredMode, uiAbsPartIdx, uiDepth );     
896}
897
898#else
899Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
900{
901  UInt uiSymbol;
902  Int  uiIPredMode;
903  Int  iMostProbable = pcCU->getMostProbableIntraDirLuma( uiAbsPartIdx );
904 
905  xReadFlag( uiSymbol );
906 
907  if ( uiSymbol )
908    uiIPredMode = iMostProbable;
909  else
910  {
911    Int iIntraIdx = pcCU->getIntraSizeIdx(uiAbsPartIdx);
912    if ( g_aucIntraModeBitsAng[iIntraIdx] < 6 )
913    {
914      xReadFlag( uiSymbol ); uiIPredMode  = uiSymbol;
915      if ( g_aucIntraModeBitsAng[iIntraIdx] > 2 ) { xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 1; }
916      if ( g_aucIntraModeBitsAng[iIntraIdx] > 3 ) { xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 2; }
917      if ( g_aucIntraModeBitsAng[iIntraIdx] > 4 ) { xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 3; }
918    }
919    else
920    {
921      xReadFlag( uiSymbol ); uiIPredMode  = uiSymbol;
922      xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 1;
923      xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 2;
924      xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 3;
925      xReadFlag( uiSymbol ); uiIPredMode |= uiSymbol << 4;
926     
927      if (uiIPredMode == 31)
928      { // Escape coding for the last two modes
929        xReadFlag( uiSymbol );
930        uiIPredMode = uiSymbol ? 32 : 31;
931      }
932    }
933   
934    if (uiIPredMode >= iMostProbable)
935      uiIPredMode++;
936  }
937 
938  pcCU->setLumaIntraDirSubParts( (UChar)uiIPredMode, uiAbsPartIdx, uiDepth );
939}
940#endif
941
942Void TDecCavlc::parseIntraDirChroma( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
943{
944  UInt uiSymbol;
945#if CHROMA_CODEWORD
946  UInt uiMode = pcCU->getLumaIntraDir(uiAbsPartIdx);
947  Int  iMax = uiMode < 4 ? 3 : 4;
948  xReadUnaryMaxSymbol( uiSymbol, iMax );
949 
950  //switch codeword
951  if (uiSymbol == 0)
952  {
953    uiSymbol = 4;
954  }
955#if CHROMA_CODEWORD_SWITCH
956  else
957  {
958    uiSymbol = ChromaMapping[iMax-3][uiSymbol];
959    if (uiSymbol <= uiMode)
960    {
961      uiSymbol --;
962    }
963  }
964#else
965  else if (uiSymbol <= uiMode)
966  {
967    uiSymbol --;
968  }
969#endif
970  //printf("uiMode %d, chroma %d, codeword %d, imax %d\n", uiMode, uiSymbol, uiRead, iMax);
971#else
972  xReadFlag( uiSymbol );
973 
974  if ( uiSymbol )
975  {
976    xReadUnaryMaxSymbol( uiSymbol, 3 );
977    uiSymbol++;
978  }
979#endif
980  pcCU->setChromIntraDirSubParts( uiSymbol, uiAbsPartIdx, uiDepth );
981 
982  return ;
983}
984
985Void TDecCavlc::parseInterDir( TComDataCU* pcCU, UInt& ruiInterDir, UInt uiAbsPartIdx, UInt uiDepth )
986{
987  UInt uiSymbol;
988 
989#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
990  if ( pcCU->getSlice()->getRefIdxCombineCoding() )
991#else
992  if(pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) <= 2 && pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) <= 2)
993#endif
994  {
995    UInt uiIndex,uiInterDir,tmp;
996    Int x,cx,y,cy;
997   
998#if MS_LCEC_LOOKUP_TABLE_MAX_VALUE
999    UInt uiMaxVal = 7;
1000#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1001    uiMaxVal = 8;
1002#endif
1003    if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) <= 1 && pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) <= 1 )
1004    {
1005      if ( pcCU->getSlice()->getNoBackPredFlag() || ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 1 ) )
1006      {
1007        uiMaxVal = 1;
1008      }
1009      else
1010      {
1011        uiMaxVal = 2;
1012      }
1013    }
1014    else if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) <= 2 && pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) <= 2 )
1015    {
1016      if ( pcCU->getSlice()->getNoBackPredFlag() || ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 2 ) )
1017      {
1018        uiMaxVal = 5;
1019      }
1020      else
1021      {
1022        uiMaxVal = 7;
1023      }
1024    }
1025    else if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 0 )
1026    {
1027      uiMaxVal = 4+1+MS_LCEC_UNI_EXCEPTION_THRES;
1028    }
1029   
1030    xReadUnaryMaxSymbol( tmp, uiMaxVal );
1031#else   
1032    UInt vlcn = g_auiMITableVlcNum[m_uiMITableVlcIdx];
1033    tmp = xReadVlc( vlcn );
1034#endif
1035    UInt *m_uiMITableD = m_uiMI1TableD;
1036    x = m_uiMITableD[tmp];
1037    uiIndex = x;
1038   
1039    /* Adapt table */
1040   
1041    cx = tmp;
1042    cy = Max(0,cx-1); 
1043    y = m_uiMITableD[cy];
1044    m_uiMITableD[cy] = x;
1045    m_uiMITableD[cx] = y;
1046    m_uiMITableVlcIdx += cx == m_uiMITableVlcIdx ? 0 : (cx < m_uiMITableVlcIdx ? -1 : 1);
1047   
1048    {
1049      uiInterDir = Min(2,uiIndex>>1); 
1050#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1051      if ( uiIndex >=4 )
1052      {
1053        uiInterDir = 2;
1054      }
1055      else
1056      {
1057        uiInterDir = 0;
1058      }
1059#endif
1060#if DCM_COMB_LIST
1061      if(uiInterDir!=2 && pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C)>0)
1062      {
1063        uiInterDir = 0;
1064        m_iRefFrame0[uiAbsPartIdx] = uiIndex;
1065      }
1066      else 
1067#endif
1068      if (uiInterDir==0)
1069      {
1070#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1071        m_iRefFrame0[uiAbsPartIdx] = uiIndex;
1072#else
1073        m_iRefFrame0[uiAbsPartIdx] = uiIndex&1;
1074#endif
1075      }
1076      else if (uiInterDir==1)
1077        m_iRefFrame1[uiAbsPartIdx] = uiIndex&1;
1078      else
1079      {
1080        m_iRefFrame0[uiAbsPartIdx] = (uiIndex>>1)&1;
1081        m_iRefFrame1[uiAbsPartIdx] = (uiIndex>>0)&1;
1082      }
1083    }
1084    ruiInterDir = uiInterDir+1;
1085#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1086    if ( x < 8 )
1087#endif
1088    {
1089      return;
1090    }
1091  }
1092 
1093#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1094  m_iRefFrame0[uiAbsPartIdx] = 1000;
1095  m_iRefFrame1[uiAbsPartIdx] = 1000;
1096#endif
1097 
1098  xReadFlag( uiSymbol );
1099 
1100  if ( uiSymbol )
1101  {
1102    uiSymbol = 2;
1103  }
1104#if DCM_COMB_LIST
1105  else if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0)
1106  {
1107    uiSymbol = 0;
1108  }
1109#endif
1110#if MS_NO_BACK_PRED_IN_B0
1111  else if ( pcCU->getSlice()->getNoBackPredFlag() )
1112  {
1113    uiSymbol = 0;
1114  }
1115#endif
1116  else
1117  {
1118    xReadFlag( uiSymbol );
1119  }
1120  uiSymbol++;
1121  ruiInterDir = uiSymbol;
1122  return;
1123}
1124
1125Void TDecCavlc::parseRefFrmIdx( TComDataCU* pcCU, Int& riRefFrmIdx, UInt uiAbsPartIdx, UInt uiDepth, RefPicList eRefList )
1126{
1127  UInt uiSymbol;
1128 
1129  if (pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) <= 2 && pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) <= 2 && pcCU->getSlice()->isInterB())
1130  {
1131#if DCM_COMB_LIST
1132    if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C ) > 0 && eRefList==REF_PIC_LIST_C)
1133    {
1134      riRefFrmIdx = m_iRefFrame0[uiAbsPartIdx]; 
1135    }
1136    else 
1137#endif
1138    if (eRefList==REF_PIC_LIST_0)
1139    {
1140      riRefFrmIdx = m_iRefFrame0[uiAbsPartIdx];     
1141    }
1142    if (eRefList==REF_PIC_LIST_1)
1143    {
1144      riRefFrmIdx = m_iRefFrame1[uiAbsPartIdx];
1145    }
1146    return;
1147  }   
1148 
1149#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1150  if ( ( m_iRefFrame0[uiAbsPartIdx] != 1000 || m_iRefFrame1[uiAbsPartIdx] != 1000 ) &&
1151      pcCU->getSlice()->getRefIdxCombineCoding() )
1152  {
1153    if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C ) > 0 && eRefList==REF_PIC_LIST_C )
1154    {
1155      riRefFrmIdx = m_iRefFrame0[uiAbsPartIdx]; 
1156    }
1157    else if (eRefList==REF_PIC_LIST_0)
1158    {
1159      riRefFrmIdx = m_iRefFrame0[uiAbsPartIdx];     
1160    }
1161    else if (eRefList==REF_PIC_LIST_1)
1162    {
1163      riRefFrmIdx = m_iRefFrame1[uiAbsPartIdx];
1164    }
1165    return;
1166  }
1167 
1168  UInt uiRefFrmIdxMinus = 0;
1169  if ( pcCU->getSlice()->getRefIdxCombineCoding() )
1170  {
1171    if ( pcCU->getInterDir( uiAbsPartIdx ) != 3 )
1172    {
1173      if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 )
1174      {
1175        uiRefFrmIdxMinus = 4;
1176      }
1177      else
1178      {
1179        uiRefFrmIdxMinus = MS_LCEC_UNI_EXCEPTION_THRES+1;
1180      }
1181    }
1182    else if ( eRefList == REF_PIC_LIST_1 && pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiAbsPartIdx ) < 2 )
1183    {
1184      uiRefFrmIdxMinus = 2;
1185    }
1186  }
1187  if ( pcCU->getSlice()->getNumRefIdx( eRefList ) - uiRefFrmIdxMinus <= 1 )
1188  {
1189    uiSymbol = 0;
1190    riRefFrmIdx = uiSymbol;
1191    riRefFrmIdx += uiRefFrmIdxMinus;
1192    return;
1193  }
1194#endif
1195 
1196  xReadFlag ( uiSymbol );
1197  if ( uiSymbol )
1198  {
1199#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1200    xReadUnaryMaxSymbol( uiSymbol, pcCU->getSlice()->getNumRefIdx( eRefList )-2 - uiRefFrmIdxMinus );
1201#else
1202    xReadUnaryMaxSymbol( uiSymbol, pcCU->getSlice()->getNumRefIdx( eRefList )-2 );
1203#endif
1204   
1205    uiSymbol++;
1206  }
1207  riRefFrmIdx = uiSymbol;
1208#if MS_LCEC_LOOKUP_TABLE_EXCEPTION
1209  riRefFrmIdx += uiRefFrmIdxMinus;
1210#endif
1211 
1212  return;
1213}
1214
1215Void TDecCavlc::parseMvd( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiPartIdx, UInt uiDepth, RefPicList eRefList )
1216{
1217  Int iHor, iVer;
1218  UInt uiAbsPartIdxL, uiAbsPartIdxA;
1219  Int iHorPred, iVerPred;
1220 
1221  TComDataCU* pcCUL   = pcCU->getPULeft ( uiAbsPartIdxL, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
1222  TComDataCU* pcCUA   = pcCU->getPUAbove( uiAbsPartIdxA, pcCU->getZorderIdxInCU() + uiAbsPartIdx );
1223 
1224  TComCUMvField* pcCUMvFieldL = ( pcCUL == NULL || pcCUL->isIntra( uiAbsPartIdxL ) ) ? NULL : pcCUL->getCUMvField( eRefList );
1225  TComCUMvField* pcCUMvFieldA = ( pcCUA == NULL || pcCUA->isIntra( uiAbsPartIdxA ) ) ? NULL : pcCUA->getCUMvField( eRefList );
1226 
1227  iHorPred = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsHor() ) +
1228  ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsHor() );
1229  iVerPred = ( (pcCUMvFieldL == NULL) ? 0 : pcCUMvFieldL->getMvd( uiAbsPartIdxL ).getAbsVer() ) +
1230  ( (pcCUMvFieldA == NULL) ? 0 : pcCUMvFieldA->getMvd( uiAbsPartIdxA ).getAbsVer() );
1231 
1232  TComMv cTmpMv( 0, 0 );
1233  pcCU->getCUMvField( eRefList )->setAllMv( cTmpMv, pcCU->getPartitionSize( uiAbsPartIdx ), uiAbsPartIdx, uiPartIdx, uiDepth );
1234 
1235  xReadSvlc( iHor );
1236  xReadSvlc( iVer );
1237 
1238  // set mvd
1239  TComMv cMv( iHor, iVer );
1240  pcCU->getCUMvField( eRefList )->setAllMvd( cMv, pcCU->getPartitionSize( uiAbsPartIdx ), uiAbsPartIdx, uiPartIdx, uiDepth );
1241 
1242  return;
1243}
1244
1245Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
1246{
1247  UInt uiDQp;
1248  Int  iDQp;
1249 
1250  xReadFlag( uiDQp );
1251 
1252  if ( uiDQp == 0 )
1253  {
1254    uiDQp = pcCU->getSlice()->getSliceQp();
1255  }
1256  else
1257  {
1258    xReadSvlc( iDQp );
1259    uiDQp = pcCU->getSlice()->getSliceQp() + iDQp;
1260  }
1261 
1262  pcCU->setQPSubParts( uiDQp, uiAbsPartIdx, uiDepth );
1263}
1264
1265#if LCEC_CBP_YUV_ROOT
1266Void TDecCavlc::parseCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth, UInt uiDepth )
1267{
1268  if (eType == TEXT_ALL)
1269  {
1270    UInt uiCbf,tmp;
1271    UInt uiCBP,uiCbfY,uiCbfU,uiCbfV;
1272    Int n,x,cx,y,cy;
1273   
1274    /* Start adaptation */
1275    n = pcCU->isIntra( uiAbsPartIdx ) ? 0 : 1;
1276    UInt vlcn = g_auiCbpVlcNum[n][m_uiCbpVlcIdx[n]];
1277    tmp = xReadVlc( vlcn );   
1278    uiCBP = m_uiCBPTableD[n][tmp];
1279   
1280    /* Adapt LP table */
1281    cx = tmp;
1282    cy = Max(0,cx-1);
1283    x = uiCBP;
1284    y = m_uiCBPTableD[n][cy];
1285    m_uiCBPTableD[n][cy] = x;
1286    m_uiCBPTableD[n][cx] = y;
1287    m_uiCbpVlcIdx[n] += cx == m_uiCbpVlcIdx[n] ? 0 : (cx < m_uiCbpVlcIdx[n] ? -1 : 1);
1288   
1289    uiCbfY = (uiCBP>>0)&1;
1290    uiCbfU = (uiCBP>>1)&1;
1291    uiCbfV = (uiCBP>>2)&1;
1292   
1293    uiCbf = pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA );
1294    pcCU->setCbfSubParts( uiCbf | ( uiCbfY << uiTrDepth ), TEXT_LUMA, uiAbsPartIdx, uiDepth );
1295   
1296    uiCbf = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U );
1297    pcCU->setCbfSubParts( uiCbf | ( uiCbfU << uiTrDepth ), TEXT_CHROMA_U, uiAbsPartIdx, uiDepth );
1298   
1299    uiCbf = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V );
1300    pcCU->setCbfSubParts( uiCbf | ( uiCbfV << uiTrDepth ), TEXT_CHROMA_V, uiAbsPartIdx, uiDepth );
1301  }
1302}
1303
1304Void TDecCavlc::parseBlockCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth, UInt uiDepth, UInt uiQPartNum )
1305{
1306  assert(uiTrDepth > 0);
1307  UInt uiCbf4, uiCbf;
1308 
1309#if QC_BLK_CBP
1310  Int x,cx,y,cy;
1311  UInt tmp;
1312 
1313  UInt n = (pcCU->isIntra(uiAbsPartIdx) && eType == TEXT_LUMA)? 0:1;
1314  UInt vlcn = (n==0)?g_auiBlkCbpVlcNum[m_uiBlkCbpVlcIdx]:11;
1315 
1316  tmp = xReadVlc( vlcn );   
1317  uiCbf4 = m_uiBlkCBPTableD[n][tmp];
1318 
1319  cx = tmp;
1320  cy = Max(0,cx-1);
1321  x = uiCbf4;
1322  y = m_uiBlkCBPTableD[n][cy];
1323  m_uiBlkCBPTableD[n][cy] = x;
1324  m_uiBlkCBPTableD[n][cx] = y;
1325  if(n==0)
1326    m_uiBlkCbpVlcIdx += cx == m_uiBlkCbpVlcIdx ? 0 : (cx < m_uiBlkCbpVlcIdx ? -1 : 1);
1327 
1328  uiCbf4++;
1329#else
1330  xReadCode(4, uiCbf4);
1331#endif
1332 
1333  uiCbf = pcCU->getCbf( uiAbsPartIdx, eType );
1334  pcCU->setCbfSubParts( uiCbf | ( ((uiCbf4>>3)&0x01) << uiTrDepth ), eType, uiAbsPartIdx, uiDepth ); uiAbsPartIdx += uiQPartNum;
1335  uiCbf = pcCU->getCbf( uiAbsPartIdx, eType );
1336  pcCU->setCbfSubParts( uiCbf | ( ((uiCbf4>>2)&0x01) << uiTrDepth ), eType, uiAbsPartIdx, uiDepth ); uiAbsPartIdx += uiQPartNum;
1337  uiCbf = pcCU->getCbf( uiAbsPartIdx, eType );
1338  pcCU->setCbfSubParts( uiCbf | ( ((uiCbf4>>1)&0x01) << uiTrDepth ), eType, uiAbsPartIdx, uiDepth ); uiAbsPartIdx += uiQPartNum;
1339  uiCbf = pcCU->getCbf( uiAbsPartIdx, eType );
1340  pcCU->setCbfSubParts( uiCbf | ( (uiCbf4&0x01) << uiTrDepth ), eType, uiAbsPartIdx, uiDepth );
1341 
1342  return;
1343}
1344#endif
1345
1346Void TDecCavlc::parseCoeffNxN( TComDataCU* pcCU, TCoeff* pcCoef, UInt uiAbsPartIdx, UInt uiWidth, UInt uiHeight, UInt uiDepth, TextType eTType )
1347{
1348 
1349  if ( uiWidth > pcCU->getSlice()->getSPS()->getMaxTrSize() )
1350  {
1351    uiWidth  = pcCU->getSlice()->getSPS()->getMaxTrSize();
1352    uiHeight = pcCU->getSlice()->getSPS()->getMaxTrSize();
1353  }
1354  UInt uiSize   = uiWidth*uiHeight;
1355 
1356  // point to coefficient
1357  TCoeff* piCoeff = pcCoef;
1358 
1359  // initialize scan
1360  const UInt*  pucScan;
1361 
1362  //UInt uiConvBit = g_aucConvertToBit[ Min(8,uiWidth) ];
1363  UInt uiConvBit = g_aucConvertToBit[ pcCU->isIntra( uiAbsPartIdx ) ? uiWidth : Min(8,uiWidth)    ];
1364  pucScan        = g_auiFrameScanXY  [ uiConvBit + 1 ];
1365 
1366#if QC_MDCS
1367  UInt uiBlkPos;
1368  UInt uiLog2BlkSize = g_aucConvertToBit[ pcCU->isIntra( uiAbsPartIdx ) ? uiWidth : Min(8,uiWidth)    ] + 2;
1369  const UInt uiScanIdx = pcCU->getCoefScanIdx(uiAbsPartIdx, uiWidth, eTType==TEXT_LUMA, pcCU->isIntra(uiAbsPartIdx));
1370#endif //QC_MDCS
1371 
1372  UInt uiDecodeDCCoeff = 0;
1373  Int dcCoeff = 0;
1374  if (pcCU->isIntra(uiAbsPartIdx))
1375  {
1376    UInt uiAbsPartIdxL, uiAbsPartIdxA;
1377    TComDataCU* pcCUL   = pcCU->getPULeft (uiAbsPartIdxL, pcCU->getZorderIdxInCU() + uiAbsPartIdx);
1378    TComDataCU* pcCUA   = pcCU->getPUAbove(uiAbsPartIdxA, pcCU->getZorderIdxInCU() + uiAbsPartIdx);
1379    if (pcCUL == NULL && pcCUA == NULL)
1380    {
1381      uiDecodeDCCoeff = 1;
1382      dcCoeff = xReadVlc(eTType == TEXT_LUMA ? 3 : 1);
1383      if (dcCoeff)
1384      {
1385        UInt sign;
1386        xReadFlag(sign);
1387        if (sign)
1388        {
1389          dcCoeff = -dcCoeff;
1390        }
1391      }
1392    }
1393  }
1394 
1395  UInt uiScanning;
1396 
1397  TCoeff scoeff[64];
1398  Int iBlockType;
1399  if( uiSize == 2*2 )
1400  {
1401    // hack: re-use 4x4 coding
1402#if QC_MOD_LCEC
1403    if (eTType==TEXT_CHROMA_U || eTType==TEXT_CHROMA_V)
1404      iBlockType = eTType-2;
1405    else
1406      iBlockType = 2 + ( pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType() );
1407#else
1408    iBlockType = pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType();
1409#endif
1410    xParseCoeff4x4( scoeff, iBlockType );
1411   
1412    for (uiScanning=0; uiScanning<4; uiScanning++)
1413    {
1414#if QC_MDCS
1415      uiBlkPos = g_auiSigLastScan[uiScanIdx][uiLog2BlkSize-1][uiScanning]; 
1416      piCoeff[ uiBlkPos ] =  scoeff[15-uiScanning];
1417#else
1418      piCoeff[ pucScan[ uiScanning ] ] = scoeff[15-uiScanning];
1419#endif //QC_MDCS
1420    }
1421  }
1422  else if ( uiSize == 4*4 )
1423  {
1424#if QC_MOD_LCEC
1425    if (eTType==TEXT_CHROMA_U || eTType==TEXT_CHROMA_V)
1426      iBlockType = eTType-2;
1427    else
1428      iBlockType = 2 + ( pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType() );
1429#else
1430    iBlockType = pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType();
1431#endif
1432    xParseCoeff4x4( scoeff, iBlockType );
1433   
1434    for (uiScanning=0; uiScanning<16; uiScanning++)
1435    {
1436#if QC_MDCS
1437      uiBlkPos = g_auiSigLastScan[uiScanIdx][uiLog2BlkSize-1][uiScanning]; 
1438      piCoeff[ uiBlkPos ] =  scoeff[15-uiScanning];
1439#else
1440      piCoeff[ pucScan[ uiScanning ] ] = scoeff[15-uiScanning];
1441#endif //QC_MDCS
1442    }
1443  }
1444  else if ( uiSize == 8*8 )
1445  {
1446    if (eTType==TEXT_CHROMA_U || eTType==TEXT_CHROMA_V) //8x8 specific
1447      iBlockType = eTType-2;
1448    else
1449      iBlockType = 2 + ( pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType() );
1450    xParseCoeff8x8( scoeff, iBlockType );
1451   
1452    for (uiScanning=0; uiScanning<64; uiScanning++)
1453    {
1454#if QC_MDCS
1455      uiBlkPos = g_auiSigLastScan[uiScanIdx][uiLog2BlkSize-1][uiScanning]; 
1456      piCoeff[ uiBlkPos ] =  scoeff[63-uiScanning];
1457#else
1458      piCoeff[ pucScan[ uiScanning ] ] = scoeff[63-uiScanning];
1459#endif //QC_MDCS
1460    }
1461   
1462  }
1463  else
1464  {
1465    if (!pcCU->isIntra( uiAbsPartIdx ))
1466    {
1467      memset(piCoeff,0,sizeof(TCoeff)*uiSize);
1468      if (eTType==TEXT_CHROMA_U || eTType==TEXT_CHROMA_V) 
1469        iBlockType = eTType-2;
1470      else
1471        iBlockType = 5 + ( pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType() );
1472      xParseCoeff8x8( scoeff, iBlockType );
1473     
1474      for (uiScanning=0; uiScanning<64; uiScanning++)
1475      { 
1476#if QC_MDCS
1477        uiBlkPos = g_auiSigLastScan[uiScanIdx][uiLog2BlkSize-1][uiScanning]; 
1478        uiBlkPos = (uiBlkPos/8)* uiWidth + uiBlkPos%8;
1479        piCoeff[ uiBlkPos ] =  scoeff[63-uiScanning];
1480#else
1481        piCoeff[(pucScan[uiScanning]/8)*uiWidth + (pucScan[uiScanning]%8)] = scoeff[63-uiScanning];
1482#endif //QC_MDCS
1483      }
1484      return;
1485    }
1486   
1487    if(pcCU->isIntra( uiAbsPartIdx ))
1488    {
1489      memset(piCoeff,0,sizeof(TCoeff)*uiSize);
1490     
1491      if (eTType==TEXT_CHROMA_U || eTType==TEXT_CHROMA_V) 
1492        iBlockType = eTType-2;
1493      else
1494        iBlockType = 5 + ( pcCU->isIntra(uiAbsPartIdx) ? 0 : pcCU->getSlice()->getSliceType() );
1495      xParseCoeff8x8( scoeff, iBlockType );
1496     
1497      for (uiScanning=0; uiScanning<64; uiScanning++)
1498      {
1499#if QC_MDCS
1500        uiBlkPos = g_auiSigLastScan[uiScanIdx][uiLog2BlkSize-1][uiScanning]; 
1501        piCoeff[ uiBlkPos ] =  scoeff[63-uiScanning];
1502#else
1503        piCoeff[ pucScan[ uiScanning ] ] = scoeff[63-uiScanning];
1504#endif //QC_MDCS
1505      }
1506    }
1507  }
1508 
1509  if (uiDecodeDCCoeff == 1)
1510  {
1511    piCoeff[0] = dcCoeff;
1512  }
1513 
1514  return ;
1515}
1516
1517Void TDecCavlc::parseTransformSubdivFlag( UInt& ruiSubdivFlag, UInt uiLog2TransformBlockSize )
1518{
1519  xReadFlag( ruiSubdivFlag );
1520}
1521
1522Void TDecCavlc::parseQtCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth, UInt uiDepth )
1523{
1524  UInt uiSymbol;
1525  xReadFlag( uiSymbol );
1526  pcCU->setCbfSubParts( uiSymbol << uiTrDepth, eType, uiAbsPartIdx, uiDepth );
1527}
1528
1529Void TDecCavlc::parseQtRootCbf( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt& uiQtRootCbf )
1530{
1531  UInt uiSymbol;
1532  xReadFlag( uiSymbol );
1533  uiQtRootCbf = uiSymbol;
1534}
1535
1536Void TDecCavlc::parseAlfFlag (UInt& ruiVal)
1537{
1538  xReadFlag( ruiVal );
1539}
1540
1541#if TSB_ALF_HEADER
1542Void TDecCavlc::parseAlfFlagNum( UInt& ruiVal, UInt minValue, UInt depth )
1543{
1544  UInt uiLength = 0;
1545  UInt maxValue = (minValue << (depth*2));
1546  UInt temp = maxValue - minValue;
1547  for(UInt i=0; i<32; i++)
1548  {
1549    if(temp&0x1)
1550    {
1551      uiLength = i+1;
1552    }
1553    temp = (temp >> 1);
1554  }
1555  if(uiLength)
1556  {
1557    xReadCode( uiLength, ruiVal );
1558  }
1559  else
1560  {
1561    ruiVal = 0;
1562  }
1563  ruiVal += minValue;
1564}
1565
1566Void TDecCavlc::parseAlfCtrlFlag( UInt &ruiAlfCtrlFlag )
1567{
1568  UInt uiSymbol;
1569  xReadFlag( uiSymbol );
1570  ruiAlfCtrlFlag = uiSymbol;
1571}
1572#endif
1573
1574Void TDecCavlc::parseAlfUvlc (UInt& ruiVal)
1575{
1576  xReadUvlc( ruiVal );
1577}
1578
1579Void TDecCavlc::parseAlfSvlc (Int&  riVal)
1580{
1581  xReadSvlc( riVal );
1582}
1583
1584
1585#if HHI_MRG
1586Void TDecCavlc::parseMergeFlag ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiPUIdx )
1587{
1588#if QC_LCEC_INTER_MODE
1589  if (pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2Nx2N )
1590    return;
1591#endif
1592  UInt uiSymbol;
1593  xReadFlag( uiSymbol );
1594  pcCU->setMergeFlagSubParts( uiSymbol ? true : false, uiAbsPartIdx, uiPUIdx, uiDepth );
1595}
1596
1597Void TDecCavlc::parseMergeIndex ( TComDataCU* pcCU, UInt& ruiMergeIndex, UInt uiAbsPartIdx, UInt uiDepth )
1598{
1599  Bool bLeftInvolved = false;
1600  Bool bAboveInvolved = false;
1601  Bool bCollocatedInvolved = false;
1602  Bool bCornerInvolved = false;
1603  Bool bCornerBLInvolved = false;
1604  UInt uiNumCand = 0;
1605  for( UInt uiIter = 0; uiIter < HHI_NUM_MRG_CAND; ++uiIter )
1606  {
1607    if( pcCU->getNeighbourCandIdx( uiIter, uiAbsPartIdx ) == uiIter + 1 )
1608    {
1609      uiNumCand++;
1610      if( uiIter == 0 )
1611      {
1612        bLeftInvolved = true;
1613      }
1614      else if( uiIter == 1 )
1615      {
1616        bAboveInvolved = true;
1617      }
1618      else if( uiIter == 2 )
1619      {
1620        bCollocatedInvolved = true;
1621      }
1622      else if( uiIter == 3 )
1623      {
1624        bCornerInvolved = true;
1625      }
1626      else if( uiIter == 4 )
1627      {
1628        bCornerBLInvolved = true;
1629      }
1630    }
1631  }
1632  assert( uiNumCand > 1 );
1633  UInt uiOffset = 0;
1634  if( bAboveInvolved && !bCollocatedInvolved && !bCornerInvolved && !bCornerBLInvolved )
1635  {
1636    uiOffset = 0;
1637  }
1638  else if( uiNumCand < 3 )
1639  {
1640    uiOffset = 1;
1641  }
1642  else
1643  {
1644    uiOffset = 2;
1645  }
1646  UInt uiSymbol = 0;
1647  xReadFlag( uiSymbol );
1648
1649  if( uiNumCand == 2 )
1650  {
1651    if( !bCollocatedInvolved )
1652    {
1653      if( !bCornerInvolved && !bCornerBLInvolved )
1654      {
1655        ruiMergeIndex = uiSymbol;
1656      }
1657      else if( bAboveInvolved && bCornerInvolved)
1658      {
1659        ruiMergeIndex = ( uiSymbol == 1 ) ? 3 : 1;
1660      }
1661      else if( bAboveInvolved && bCornerBLInvolved)
1662      {
1663        ruiMergeIndex = ( uiSymbol == 1 ) ? 4 : 1;
1664      }
1665      else if( bCornerInvolved && bCornerBLInvolved )
1666      {
1667        ruiMergeIndex = ( uiSymbol == 1 ) ? 4 : 3;
1668      }
1669      else if( !bAboveInvolved && !bCornerBLInvolved)
1670      {
1671        ruiMergeIndex = ( uiSymbol == 1 ) ? 3 : 0;
1672      }
1673      else if( !bAboveInvolved && !bCornerInvolved )
1674      {
1675        ruiMergeIndex = ( uiSymbol == 1 ) ? 4 : 0;
1676      }
1677    }
1678    else
1679    {
1680      if( bAboveInvolved )
1681      {
1682        ruiMergeIndex = ( uiSymbol == 1 ) ? 2 : 1;
1683      }
1684      else if( bCornerInvolved )
1685      {
1686        ruiMergeIndex = ( uiSymbol == 1 ) ? 3 : 2;
1687      }
1688      else if( bCornerBLInvolved )
1689      {
1690        ruiMergeIndex = ( uiSymbol == 1 ) ? 4 : 2;
1691      }
1692      else
1693      {
1694        ruiMergeIndex = ( uiSymbol == 1 ) ? 2 : 0;
1695      }
1696    }
1697    return;
1698  }
1699  else if( uiNumCand == 3 )
1700  {
1701    if( uiSymbol == 0 )
1702    {
1703      if( bLeftInvolved )
1704      {
1705        ruiMergeIndex = 0;
1706      }
1707      else if( !bLeftInvolved && bAboveInvolved )
1708      {
1709        ruiMergeIndex = 1;
1710      }
1711      else if(!bLeftInvolved && !bAboveInvolved )
1712      {
1713        ruiMergeIndex = 2;
1714      }
1715    }
1716    else
1717    {
1718      xReadFlag( uiSymbol );
1719      if( uiSymbol == 1 )
1720      {
1721        if( bCornerBLInvolved )
1722        {
1723          ruiMergeIndex = 4;
1724        }
1725        else if( !bCornerBLInvolved && bCornerInvolved )
1726        {
1727          ruiMergeIndex = 3;
1728        }
1729        else if( !bCornerBLInvolved && !bCornerInvolved && bCollocatedInvolved )
1730        {
1731          ruiMergeIndex = 2;
1732        }
1733      }
1734      else
1735      {
1736        if( bLeftInvolved && bAboveInvolved )
1737        {
1738          ruiMergeIndex = 1;
1739        }
1740        else if( ( ( !bLeftInvolved && bAboveInvolved) || ( bLeftInvolved && !bAboveInvolved ) )&& bCollocatedInvolved )
1741        {
1742          ruiMergeIndex = 2;
1743        }
1744        else if( bCornerBLInvolved && bCornerInvolved )
1745        {
1746          ruiMergeIndex = 3;
1747        }
1748      }
1749    }
1750  }
1751  else //uiNumCand > 3
1752  {
1753    if( uiSymbol == 1 )
1754    {
1755      UInt uiAbove = 0;
1756      xReadFlag( uiAbove );
1757      if( uiAbove == 0 )
1758      {
1759        ruiMergeIndex = 1;
1760      }
1761      else
1762      {
1763        UInt uiCol = 0;
1764        xReadFlag( uiCol );
1765        if( uiCol == 0 )
1766        {
1767          ruiMergeIndex = 2;
1768        }
1769        else
1770        {
1771          UInt uiCorner = 0;
1772          xReadFlag( uiCorner );
1773          if( uiCorner == 0 )
1774          {
1775            ruiMergeIndex = 3;
1776          }
1777          else
1778          {
1779            ruiMergeIndex = 4;
1780          }
1781        }
1782      }
1783    }
1784    else
1785    {
1786      ruiMergeIndex = 0;
1787    }
1788  }
1789}
1790#endif
1791
1792// ====================================================================================================================
1793// Protected member functions
1794// ====================================================================================================================
1795
1796Void TDecCavlc::xReadCode (UInt uiLength, UInt& ruiCode)
1797{
1798  assert ( uiLength > 0 );
1799  m_pcBitstream->read (uiLength, ruiCode);
1800}
1801
1802Void TDecCavlc::xReadUvlc( UInt& ruiVal)
1803{
1804  UInt uiVal = 0;
1805  UInt uiCode = 0;
1806  UInt uiLength;
1807  m_pcBitstream->read( 1, uiCode );
1808 
1809  if( 0 == uiCode )
1810  {
1811    uiLength = 0;
1812   
1813    while( ! ( uiCode & 1 ))
1814    {
1815      m_pcBitstream->read( 1, uiCode );
1816      uiLength++;
1817    }
1818   
1819    m_pcBitstream->read( uiLength, uiVal );
1820   
1821    uiVal += (1 << uiLength)-1;
1822  }
1823 
1824  ruiVal = uiVal;
1825}
1826
1827Void TDecCavlc::xReadSvlc( Int& riVal)
1828{
1829  UInt uiBits = 0;
1830  m_pcBitstream->read( 1, uiBits );
1831  if( 0 == uiBits )
1832  {
1833    UInt uiLength = 0;
1834   
1835    while( ! ( uiBits & 1 ))
1836    {
1837      m_pcBitstream->read( 1, uiBits );
1838      uiLength++;
1839    }
1840   
1841    m_pcBitstream->read( uiLength, uiBits );
1842   
1843    uiBits += (1 << uiLength);
1844    riVal = ( uiBits & 1) ? -(Int)(uiBits>>1) : (Int)(uiBits>>1);
1845  }
1846  else
1847  {
1848    riVal = 0;
1849  }
1850}
1851
1852Void TDecCavlc::xReadFlag (UInt& ruiCode)
1853{
1854  m_pcBitstream->read( 1, ruiCode );
1855}
1856
1857Void TDecCavlc::xReadUnaryMaxSymbol( UInt& ruiSymbol, UInt uiMaxSymbol )
1858{
1859  if (uiMaxSymbol == 0)
1860  {
1861    ruiSymbol = 0;
1862    return;
1863  }
1864 
1865  xReadFlag( ruiSymbol );
1866 
1867  if (ruiSymbol == 0 || uiMaxSymbol == 1)
1868  {
1869    return;
1870  }
1871 
1872  UInt uiSymbol = 0;
1873  UInt uiCont;
1874 
1875  do
1876  {
1877    xReadFlag( uiCont );
1878    uiSymbol++;
1879  }
1880  while( uiCont && (uiSymbol < uiMaxSymbol-1) );
1881 
1882  if( uiCont && (uiSymbol == uiMaxSymbol-1) )
1883  {
1884    uiSymbol++;
1885  }
1886 
1887  ruiSymbol = uiSymbol;
1888}
1889
1890Void TDecCavlc::xReadExGolombLevel( UInt& ruiSymbol )
1891{
1892  UInt uiSymbol ;
1893  UInt uiCount = 0;
1894  do
1895  {
1896    xReadFlag( uiSymbol );
1897    uiCount++;
1898  }
1899  while( uiSymbol && (uiCount != 13));
1900 
1901  ruiSymbol = uiCount-1;
1902 
1903  if( uiSymbol )
1904  {
1905    xReadEpExGolomb( uiSymbol, 0 );
1906    ruiSymbol += uiSymbol+1;
1907  }
1908 
1909  return;
1910}
1911
1912Void TDecCavlc::xReadEpExGolomb( UInt& ruiSymbol, UInt uiCount )
1913{
1914  UInt uiSymbol = 0;
1915  UInt uiBit = 1;
1916 
1917 
1918  while( uiBit )
1919  {
1920    xReadFlag( uiBit );
1921    uiSymbol += uiBit << uiCount++;
1922  }
1923 
1924  uiCount--;
1925  while( uiCount-- )
1926  {
1927    xReadFlag( uiBit );
1928    uiSymbol += uiBit << uiCount;
1929  }
1930 
1931  ruiSymbol = uiSymbol;
1932 
1933  return;
1934}
1935
1936UInt TDecCavlc::xGetBit()
1937{
1938  UInt ruiCode;
1939  m_pcBitstream->read( 1, ruiCode );
1940  return ruiCode;
1941}
1942
1943Int TDecCavlc::xReadVlc( Int n )
1944{
1945#if QC_BLK_CBP
1946  assert( n>=0 && n<=11 );
1947#else
1948  assert( n>=0 && n<=10 );
1949#endif
1950 
1951  UInt zeroes=0, done=0, tmp;
1952  UInt cw, bit;
1953  UInt val = 0;
1954  UInt first;
1955  UInt lead = 0;
1956 
1957  if (n < 5)
1958  {
1959    while (!done && zeroes < 6)
1960    {
1961      xReadFlag( bit );
1962      if (bit)
1963      {
1964        if (n)
1965        {
1966          xReadCode( n, cw );
1967        }
1968        else
1969        {
1970          cw = 0;
1971        }
1972        done = 1;
1973      }
1974      else
1975      {
1976        zeroes++;
1977      }
1978    }
1979    if ( done )
1980    {
1981      val = (zeroes<<n)+cw;
1982    }
1983    else
1984    {
1985      lead = n;
1986      while (!done)
1987      {
1988        xReadFlag( first );
1989        if ( !first )
1990        {
1991          lead++;
1992        }
1993        else
1994        {
1995          if ( lead )
1996          {
1997            xReadCode( lead, tmp );
1998          }
1999          else
2000          {
2001            tmp = 0;
2002          }
2003          val = 6 * (1 << n) + (1 << lead) + tmp - (1 << n);
2004          done = 1;
2005        }
2006      }
2007    }
2008  }
2009  else if (n < 8)
2010  {
2011    while (!done)
2012    {
2013      xReadFlag( bit );
2014      if ( bit )
2015      {
2016        xReadCode( n-4, cw );
2017        done = 1;
2018      }
2019      else
2020      {
2021        zeroes++;
2022      }
2023    }
2024    val = (zeroes<<(n-4))+cw;
2025  }
2026  else if (n == 8)
2027  {
2028    if ( xGetBit() )
2029    {
2030      val = 0;
2031    }
2032    else if ( xGetBit() )
2033    {
2034      val = 1;
2035    }
2036    else
2037    {
2038      val = 2;
2039    }
2040  }
2041  else if (n == 9)
2042  {
2043    if ( xGetBit() )
2044    {
2045      if ( xGetBit() )
2046      {
2047        xReadCode(3, val);
2048        val += 3;
2049      }
2050      else if ( xGetBit() )
2051      {
2052        val = xGetBit() + 1;
2053      }
2054      else
2055      {
2056        val = 0;
2057      }
2058    }
2059    else
2060    {
2061      while (!done)
2062      {
2063        xReadFlag( bit );
2064        if ( bit )
2065        {
2066          xReadCode(4, cw);
2067          done = 1;
2068        }
2069        else
2070        {
2071          zeroes++;
2072        }
2073      }
2074      val = (zeroes<<4)+cw+11;
2075    }
2076  }
2077  else if (n == 10)
2078  {
2079    while (!done)
2080    {
2081      xReadFlag( first );
2082      if ( !first )
2083      {
2084        lead++;
2085      }
2086      else
2087      {
2088        if ( !lead )
2089        {
2090          val = 0;
2091        }
2092        else
2093        {
2094          xReadCode(lead, val);
2095          val += (1<<lead);
2096          val--;
2097        }
2098        done = 1;
2099      }
2100    }
2101  }
2102#if QC_BLK_CBP
2103  else if (n == 11)
2104  {
2105    UInt code;
2106    xReadCode(3, val);
2107    if(val)
2108    {
2109      xReadCode(1, code);
2110      val = (val<<1)|code;
2111      val--;
2112    }
2113  }
2114#endif
2115 
2116  return val;
2117}
2118
2119Void TDecCavlc::xParseCoeff4x4( TCoeff* scoeff, Int n )
2120{
2121  Int i;
2122  UInt sign;
2123  Int tmp;
2124  Int vlc,cn,this_pos;
2125  Int maxrun;
2126  Int last_position;
2127  Int atable[5] = {4,6,14,28,0xfffffff};
2128  Int vlc_adaptive=0;
2129  Int done;
2130  LastCoeffStruct combo;
2131 
2132#if QC_MOD_LCEC
2133  Int nTab;
2134  Int tr1;
2135  nTab=max(0,n-2);
2136#endif
2137
2138  for (i = 0; i < 16; i++)
2139  {
2140    scoeff[i] = 0;
2141  }
2142 
2143  {
2144    /* Get the last nonzero coeff */
2145    Int x,y,cx,cy,vlcNum;
2146    Int vlcTable[8] = {2,2,2};
2147   
2148    /* Decode according to current LP table */
2149#if QC_MOD_LCEC
2150    vlcNum = vlcTable[nTab];
2151    tmp = xReadVlc( vlcNum );
2152    cn = m_uiLPTableD4[nTab][tmp];
2153#else
2154    vlcNum = vlcTable[n];
2155   
2156    tmp = xReadVlc( vlcNum );
2157    cn = m_uiLPTableD4[n][tmp];
2158#endif
2159    combo.level = (cn>15);
2160    combo.last_pos = cn&0x0f;
2161   
2162    /* Adapt LP table */
2163    cx = tmp;
2164    cy = Max( 0, cx-1 );
2165    x = cn;
2166#if QC_MOD_LCEC
2167    y = m_uiLPTableD4[nTab][cy];
2168    m_uiLPTableD4[nTab][cy] = x;
2169    m_uiLPTableD4[nTab][cx] = y;
2170#else
2171    y = m_uiLPTableD4[n][cy];
2172    m_uiLPTableD4[n][cy] = x;
2173    m_uiLPTableD4[n][cx] = y;
2174#endif
2175  }
2176 
2177  if ( combo.level == 1 )
2178  {
2179    tmp = xReadVlc( 0 );
2180    sign = tmp&1;
2181    tmp = (tmp>>1)+2;
2182  }
2183  else
2184  {
2185    tmp = 1;
2186    xReadFlag( sign );
2187  }
2188 
2189#if QC_MOD_LCEC
2190  if (tmp>1)
2191  {
2192    tr1=0;
2193  }
2194  else
2195  {
2196    tr1=1;
2197  }
2198#endif
2199
2200  if ( sign )
2201  {
2202    tmp = -tmp;
2203  }
2204 
2205  last_position = combo.last_pos;
2206  this_pos = 15 - last_position;
2207  scoeff[this_pos] = tmp;
2208  i = this_pos;
2209  i++;
2210 
2211  done = 0;
2212  {
2213    while (!done && i < 16)
2214    {
2215      maxrun = 15-i;
2216#if QC_MOD_LCEC
2217      if(n==2)
2218        vlc = g_auiVlcTable8x8Intra[maxrun];
2219      else
2220        vlc = g_auiVlcTable8x8Inter[maxrun];
2221#else
2222      if (maxrun > 27)
2223      {
2224        maxrun = 28;
2225        vlc = 3;
2226      }
2227      else
2228      {
2229        vlc = g_auiVlcTable8x8[maxrun];
2230      }
2231#endif
2232     
2233      /* Go into run mode */
2234      cn = xReadVlc( vlc );
2235#if QC_MOD_LCEC
2236      if(n==2)
2237      {
2238        xRunLevelIndInv(&combo, maxrun, g_auiLumaRunTr14x4[tr1][maxrun], cn);
2239      }
2240      else
2241#endif
2242      {
2243        combo = g_acstructLumaRun8x8[maxrun][cn];
2244      }
2245      i += combo.last_pos;
2246      /* No sign for last zeroes */
2247      if (i < 16)
2248      {
2249        if (combo.level == 1)
2250        {
2251          tmp = xReadVlc( 0 );
2252          sign = tmp&1;
2253          tmp = (tmp>>1)+2;
2254          done = 1;
2255        }
2256        else
2257        {
2258          tmp = 1;
2259          xReadFlag( sign );
2260        }
2261        if ( sign )
2262        {
2263          tmp = -tmp;
2264        }
2265        scoeff[i] = tmp;
2266      }
2267      i++;
2268#if QC_MOD_LCEC
2269      if (tr1>0 && tr1<MAX_TR1)
2270      {
2271        tr1++;
2272      }
2273#endif
2274    }
2275  }
2276  if (i < 16)
2277  {
2278    /* Get the rest in level mode */
2279    while ( i < 16 )
2280    {
2281      tmp = xReadVlc( vlc_adaptive );
2282      if ( tmp > atable[vlc_adaptive] )
2283      {
2284        vlc_adaptive++;
2285      }
2286      if ( tmp )
2287      {
2288        xReadFlag( sign );
2289        if ( sign )
2290        {
2291          tmp = -tmp;
2292        }
2293      }
2294      scoeff[i] = tmp;
2295      i++;
2296    }
2297  }
2298 
2299  return;
2300}
2301
2302#if QC_MOD_LCEC
2303
2304Void TDecCavlc::xRunLevelIndInv(LastCoeffStruct *combo, Int maxrun, UInt lrg1Pos, UInt cn)
2305{
2306  int lev, run;
2307  if (lrg1Pos>0)
2308  {
2309    if(cn < min(lrg1Pos, maxrun+2))
2310    {
2311      lev = 0; 
2312      run = cn; 
2313    }
2314    else if(cn < (maxrun<<1) + 4 - (Int)lrg1Pos)
2315    {
2316      if((cn+lrg1Pos)&1)
2317      {
2318        lev = 0;
2319        run = (cn + lrg1Pos - 1) >> 1;
2320      }
2321      else
2322      {
2323        lev = 1; 
2324        run = (cn - lrg1Pos)>>1;
2325      }
2326    }
2327    else
2328    {
2329      lev = 1;
2330      run = cn - maxrun - 2;
2331    }
2332  }
2333  else
2334  {
2335    if( cn & 1 )
2336    {
2337      lev = 0; run = (cn-1)>>1;
2338    }
2339    else
2340    {
2341      run = cn >> 1;
2342      lev = (run <= maxrun)?1:0;
2343    }
2344  }
2345  combo->level = lev;
2346  combo->last_pos = run;
2347}
2348#endif
2349
2350
2351Void TDecCavlc::xParseCoeff8x8(TCoeff* scoeff, int n)
2352{
2353  Int i;
2354  UInt sign;
2355  Int tmp;
2356  LastCoeffStruct combo;
2357  Int vlc,cn,this_pos;
2358  Int maxrun;
2359  Int last_position;
2360  Int atable[5] = {4,6,14,28,0xfffffff};
2361  Int vlc_adaptive=0;
2362  Int done;
2363#if QC_MOD_LCEC
2364  Int tr1;
2365#endif
2366 
2367  static const Int switch_thr[10] = {49,49,0,49,49,0,49,49,49,49};
2368  Int sum_big_coef = 0;
2369 
2370  for (i = 0; i < 64; i++)
2371  {
2372    scoeff[i] = 0;
2373  }
2374 
2375  /* Get the last nonzero coeff */
2376  {
2377    Int x,y,cx,cy,vlcNum;
2378   
2379    /* Decode according to current LP table */
2380    // ADAPT_VLC_NUM
2381    vlcNum = g_auiLastPosVlcNum[n][Min(16,m_uiLastPosVlcIndex[n])];
2382    tmp = xReadVlc( vlcNum );
2383    cn = m_uiLPTableD8[n][tmp];
2384    combo.level = (cn>63);
2385    combo.last_pos = cn&0x3f;
2386   
2387    /* Adapt LP table */
2388    cx = tmp;
2389    cy = Max(0,cx-1);
2390    x = cn;
2391    y = m_uiLPTableD8[n][cy];
2392    m_uiLPTableD8[n][cy] = x;
2393    m_uiLPTableD8[n][cx] = y;
2394    // ADAPT_VLC_NUM
2395    m_uiLastPosVlcIndex[n] += cx == m_uiLastPosVlcIndex[n] ? 0 : (cx < m_uiLastPosVlcIndex[n] ? -1 : 1);
2396  }
2397 
2398  if (combo.level == 1)
2399  {
2400    tmp = xReadVlc( 0 );
2401    sign = tmp&1;
2402    tmp = (tmp>>1)+2;
2403  }
2404  else
2405  {
2406    tmp = 1;
2407    xReadFlag( sign );
2408  }
2409
2410#if QC_MOD_LCEC
2411  if (tmp>1)
2412  {
2413    tr1=0;
2414  }
2415  else
2416  {
2417    tr1=1;
2418  }
2419#endif
2420
2421  if ( sign )
2422  {
2423    tmp = -tmp;
2424  }
2425 
2426  last_position = combo.last_pos;
2427  this_pos = 63 - last_position;
2428  scoeff[this_pos] = tmp;
2429  i = this_pos;
2430  i++;
2431 
2432  done = 0;
2433  {
2434    while (!done && i < 64)
2435    {
2436      maxrun = 63-i;
2437#if QC_MOD_LCEC
2438      if (n == 2 || n == 5)
2439        vlc = g_auiVlcTable8x8Intra[Min(maxrun,28)];
2440      else
2441        vlc = g_auiVlcTable8x8Inter[Min(maxrun,28)];
2442#else
2443      if (maxrun > 27)
2444      {
2445        maxrun = 28;
2446        vlc = 3;
2447      }
2448      else
2449      {
2450        vlc = g_auiVlcTable8x8[maxrun];
2451      }
2452#endif
2453     
2454      /* Go into run mode */
2455      cn = xReadVlc( vlc );
2456#if QC_MOD_LCEC
2457      if (n == 2 || n == 5)
2458        xRunLevelIndInv(&combo, maxrun, g_auiLumaRunTr18x8[tr1][min(maxrun,28)], cn);
2459      else
2460        combo = g_acstructLumaRun8x8[Min(maxrun,28)][cn];
2461#else
2462      combo = g_acstructLumaRun8x8[maxrun][cn];
2463#endif
2464      i += combo.last_pos;
2465      /* No sign for last zeroes */
2466      if (i < 64)
2467      {
2468        if (combo.level == 1)
2469        {
2470          tmp = xReadVlc( 0 );
2471          sign = tmp&1;
2472          tmp = (tmp>>1)+2;
2473         
2474          sum_big_coef += tmp;
2475          if (i > switch_thr[n] || sum_big_coef > 2)
2476          {
2477            done = 1;
2478          }
2479        }
2480        else
2481        {
2482          tmp = 1;
2483          xReadFlag( sign );
2484        }
2485        if ( sign )
2486        {
2487          tmp = -tmp;
2488        }
2489        scoeff[i] = tmp;
2490      }
2491      i++;
2492#if QC_MOD_LCEC
2493      if (tr1==0 || combo.level != 0)
2494      {
2495        tr1=0;
2496      }
2497      else if( tr1 < MAX_TR1)
2498      {
2499        tr1++;
2500      }
2501#endif
2502    }
2503  }
2504  if (i < 64)
2505  {
2506    /* Get the rest in level mode */
2507    while (i<64)
2508    {
2509      tmp = xReadVlc( vlc_adaptive );
2510     
2511      if (tmp>atable[vlc_adaptive])
2512      {
2513        vlc_adaptive++;
2514      }
2515      if (tmp)
2516      {
2517        xReadFlag( sign );
2518        if ( sign )
2519        {
2520          tmp = -tmp;
2521        }
2522      }
2523      scoeff[i] = tmp;
2524      i++;
2525    }
2526  }
2527  return;
2528}