Changeset 56 in 3DVCSoftware for trunk/source/Lib/TLibEncoder/TEncSearch.cpp


Ignore:
Timestamp:
11 May 2012, 21:20:17 (12 years ago)
Author:
hschwarz
Message:

updated trunk (move to HM6.1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TLibEncoder/TEncSearch.cpp

    r42 r56  
    22 * License, included below. This software may be subject to other third party
    33 * and contributor rights, including patent rights, and no such rights are
    4  * granted under this license.
     4 * granted under this license. 
    55 *
    6  * Copyright (c) 2010-2011, ISO/IEC
     6 * Copyright (c) 2010-2012, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    1515 *    this list of conditions and the following disclaimer in the documentation
    1616 *    and/or other materials provided with the distribution.
    17  *  * Neither the name of the ISO/IEC nor the names of its contributors may
     17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
    1818 *    be used to endorse or promote products derived from this software without
    1919 *    specific prior written permission.
     
    3232 */
    3333
    34 
    3534/** \file     TEncSearch.cpp
    3635 \brief    encoder search class
    3736 */
    3837
    39 #include "../TLibCommon/TypeDef.h"
    40 #include "../TLibCommon/TComMotionInfo.h"
     38#include "TLibCommon/TypeDef.h"
     39#include "TLibCommon/TComRom.h"
     40#include "TLibCommon/TComMotionInfo.h"
    4141#include "TEncSearch.h"
     42#include <math.h>
     43
     44//! \ingroup TLibEncoder
     45//! \{
    4246
    4347static TComMv s_acMvRefineH[9] =
     
    8286  m_pcQTTempCoeffCb  = NULL;
    8387  m_pcQTTempCoeffCr  = NULL;
     88#if ADAPTIVE_QP_SELECTION
     89  m_ppcQTTempArlCoeffY  = NULL;
     90  m_ppcQTTempArlCoeffCb = NULL;
     91  m_ppcQTTempArlCoeffCr = NULL;
     92  m_pcQTTempArlCoeffY   = NULL;
     93  m_pcQTTempArlCoeffCb  = NULL;
     94  m_pcQTTempArlCoeffCr  = NULL;
     95#endif
    8496  m_puhQTTempTrIdx   = NULL;
    8597  m_puhQTTempCbf[0] = m_puhQTTempCbf[1] = m_puhQTTempCbf[2] = NULL;
     
    88100  m_pcEntropyCoder = NULL;
    89101  m_pTempPel = NULL;
    90 #ifdef WEIGHT_PRED
    91   setWpScalingDistParam( NULL, -1, -1, REF_PIC_LIST_X );
    92 #endif
     102
     103  setWpScalingDistParam( NULL, -1, REF_PIC_LIST_X );
    93104}
    94105
     
    100111    m_pTempPel = NULL;
    101112  }
    102 
     113 
    103114  if ( m_pcEncCfg )
    104115  {
     
    109120      delete[] m_ppcQTTempCoeffCb[ui];
    110121      delete[] m_ppcQTTempCoeffCr[ui];
     122#if ADAPTIVE_QP_SELECTION
     123      delete[] m_ppcQTTempArlCoeffY[ui];
     124      delete[] m_ppcQTTempArlCoeffCb[ui];
     125      delete[] m_ppcQTTempArlCoeffCr[ui];
     126#endif
    111127      m_pcQTTempTComYuv[ui].destroy();
    112128    }
     
    118134  delete[] m_pcQTTempCoeffCb;
    119135  delete[] m_pcQTTempCoeffCr;
     136#if ADAPTIVE_QP_SELECTION
     137  delete[] m_ppcQTTempArlCoeffY;
     138  delete[] m_ppcQTTempArlCoeffCb;
     139  delete[] m_ppcQTTempArlCoeffCr;
     140  delete[] m_pcQTTempArlCoeffY;
     141  delete[] m_pcQTTempArlCoeffCb;
     142  delete[] m_pcQTTempArlCoeffCr;
     143#endif
    120144  delete[] m_puhQTTempTrIdx;
    121145  delete[] m_puhQTTempCbf[0];
     
    123147  delete[] m_puhQTTempCbf[2];
    124148  delete[] m_pcQTTempTComYuv;
     149 
     150  m_tmpYuvPred.destroy();
    125151}
    126152
     
    145171  m_pcEntropyCoder       = pcEntropyCoder;
    146172  m_pcRdCost             = pcRdCost;
    147 
     173 
    148174  m_pppcRDSbacCoder     = pppcRDSbacCoder;
    149175  m_pcRDGoOnSbacCoder   = pcRDGoOnSbacCoder;
    150 
     176 
    151177  m_bUseSBACRD          = pppcRDSbacCoder ? true : false;
    152 
     178 
    153179  for (Int iDir = 0; iDir < 2; iDir++)
    154180  {
     
    158184    }
    159185  }
    160 
     186 
    161187  m_puiDFilter = s_auiDFilter + 4;
    162 
     188 
    163189  // initialize motion cost
     190#if !FIX203
    164191  m_pcRdCost->initRateDistortionModel( m_iSearchRange << 2 );
    165 
     192#endif
     193 
     194#if HHI_INTER_VIEW_MOTION_PRED
     195  const Int iNumAMVPCands = AMVP_MAX_NUM_CANDS + 1;
     196  for( Int iNum = 0; iNum < iNumAMVPCands+1; iNum++)
     197  {
     198    for( Int iIdx = 0; iIdx < iNumAMVPCands; iIdx++)
     199#else
    166200  for( Int iNum = 0; iNum < AMVP_MAX_NUM_CANDS+1; iNum++)
    167201  {
    168202    for( Int iIdx = 0; iIdx < AMVP_MAX_NUM_CANDS; iIdx++)
     203#endif
    169204    {
    170205      if (iIdx < iNum)
     
    174209    }
    175210  }
    176 
     211 
    177212  initTempBuff();
    178 
     213 
    179214  m_pTempPel = new Pel[g_uiMaxCUWidth*g_uiMaxCUHeight];
    180 
     215 
    181216  const UInt uiNumLayersToAllocate = pcEncCfg->getQuadtreeTULog2MaxSize()-pcEncCfg->getQuadtreeTULog2MinSize()+1;
    182217  m_ppcQTTempCoeffY  = new TCoeff*[uiNumLayersToAllocate];
     
    186221  m_pcQTTempCoeffCb  = new TCoeff [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
    187222  m_pcQTTempCoeffCr  = new TCoeff [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
    188 
     223#if ADAPTIVE_QP_SELECTION
     224  m_ppcQTTempArlCoeffY  = new Int*[uiNumLayersToAllocate];
     225  m_ppcQTTempArlCoeffCb = new Int*[uiNumLayersToAllocate];
     226  m_ppcQTTempArlCoeffCr = new Int*[uiNumLayersToAllocate];
     227  m_pcQTTempArlCoeffY   = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight   ];
     228  m_pcQTTempArlCoeffCb  = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
     229  m_pcQTTempArlCoeffCr  = new Int [g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
     230#endif
     231 
    189232  const UInt uiNumPartitions = 1<<(g_uiMaxCUDepth<<1);
    190233  m_puhQTTempTrIdx   = new UChar  [uiNumPartitions];
     
    198241    m_ppcQTTempCoeffCb[ui] = new TCoeff[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
    199242    m_ppcQTTempCoeffCr[ui] = new TCoeff[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
     243#if ADAPTIVE_QP_SELECTION
     244    m_ppcQTTempArlCoeffY[ui]  = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight   ];
     245    m_ppcQTTempArlCoeffCb[ui] = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
     246    m_ppcQTTempArlCoeffCr[ui] = new Int[g_uiMaxCUWidth*g_uiMaxCUHeight>>2];
     247#endif
    200248    m_pcQTTempTComYuv[ui].create( g_uiMaxCUWidth, g_uiMaxCUHeight );
    201249  }
     250 
     251  m_tmpYuvPred.create(MAX_CU_SIZE, MAX_CU_SIZE);
    202252}
    203253
     
    230280{
    231281  UInt  uiSad;
    232 
     282 
    233283  Pel*  piRefSrch;
    234 
     284 
    235285  piRefSrch = rcStruct.piRefY + iSearchY * rcStruct.iYStride + iSearchX;
    236 
     286 
    237287  //-- jclee for using the SAD function pointer
    238288  m_pcRdCost->setDistParam( pcPatternKey, piRefSrch, rcStruct.iYStride,  m_cDistParam );
    239 
     289 
    240290  // fast encoder decision: use subsampled SAD when rows > 8 for integer ME
    241291  if ( m_pcEncCfg->getUseFastEnc() )
     
    246296    }
    247297  }
    248 #ifdef WEIGHT_PRED
     298
    249299  setDistParamComp(0);  // Y component
    250 #endif
    251300
    252301  // distortion
    253302  uiSad = m_cDistParam.DistFunc( &m_cDistParam );
    254 
     303 
    255304  // motion cost
    256305  uiSad += m_pcRdCost->getCost( iSearchX, iSearchY );
    257 
    258   // regularization cost
    259   if( m_pcRdCost->useMultiviewReg() )
    260   {
    261     uiSad += m_pcRdCost->getMultiviewRegCost( iSearchX, iSearchY );
    262   }
    263 
     306 
    264307  if( uiSad < rcStruct.uiBestSad )
    265308  {
     
    279322  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    280323  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    281 
     324 
    282325  // 2 point search,                   //   1 2 3
    283326  // check only the 2 untested points  //   4 0 5
     
    409452  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    410453  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    411 
     454 
    412455  // 8 point search,                   //   1 2 3
    413456  // search around the start point     //   4 0 5
     
    419462  const Int iRight      = iStartX + iDist;
    420463  rcStruct.uiBestRound += 1;
    421 
     464 
    422465  if ( iTop >= iSrchRngVerTop ) // check top
    423466  {
     
    428471    // top middle
    429472    xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iTop, 2, iDist );
    430 
     473   
    431474    if ( iRight <= iSrchRngHorRight ) // check top right
    432475    {
     
    450493    // check bottom middle
    451494    xTZSearchHelp( pcPatternKey, rcStruct, iStartX, iBottom, 7, iDist );
    452 
     495   
    453496    if ( iRight <= iSrchRngHorRight ) // check bottom right
    454497    {
     
    464507  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    465508  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    466 
     509 
    467510  // 8 point search,                   //   1 2 3
    468511  // search around the start point     //   4 0 5
     
    474517  const Int iRight      = iStartX + iDist;
    475518  rcStruct.uiBestRound += 1;
    476 
     519 
    477520  if ( iDist == 1 ) // iDist == 1
    478521  {
     
    502545      const Int iLeft_2     = iStartX - (iDist>>1);
    503546      const Int iRight_2    = iStartX + (iDist>>1);
    504 
     547     
    505548      if (  iTop >= iSrchRngVerTop && iLeft >= iSrchRngHorLeft &&
    506549          iRight <= iSrchRngHorRight && iBottom <= iSrchRngVerBottom ) // check border
     
    602645          Int iPosXL = iStartX - ((iDist>>2) * index);
    603646          Int iPosXR = iStartX + ((iDist>>2) * index);
    604 
     647         
    605648          if ( iPosYT >= iSrchRngVerTop ) // check top
    606649          {
     
    631674}
    632675
    633 #ifdef ROUNDING_CONTROL_BIPRED
    634 UInt TEncSearch::xPatternRefinement_Bi    ( TComPattern* pcPatternKey, Pel* piRef, Int iRefStride, Int iIntStep, Int iFrac, TComMv& rcMvFrac, Pel* pcRef2, Bool bRound, Bool bInterview )
     676//<--
     677
     678UInt TEncSearch::xPatternRefinement( TComPattern* pcPatternKey,
     679                                    TComMv baseRefMv,
     680                                    Int iFrac, TComMv& rcMvFrac )
    635681{
    636682  UInt  uiDist;
    637683  UInt  uiDistBest  = MAX_UINT;
    638684  UInt  uiDirecBest = 0;
    639 
     685 
    640686  Pel*  piRefPos;
    641 
    642   m_pcRdCost->setDistParam_Bi( pcPatternKey, piRef, iRefStride, iIntStep, m_cDistParam, m_pcEncCfg->getUseHADME() );
    643 
     687  Int iRefStride = m_filteredBlock[0][0].getStride();
     688#if NS_HAD
     689  m_pcRdCost->setDistParam( pcPatternKey, m_filteredBlock[0][0].getLumaAddr(), iRefStride, 1, m_cDistParam, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
     690#else
     691  m_pcRdCost->setDistParam( pcPatternKey, m_filteredBlock[0][0].getLumaAddr(), iRefStride, 1, m_cDistParam, m_pcEncCfg->getUseHADME() );
     692#endif
     693 
    644694  TComMv* pcMvRefine = (iFrac == 2 ? s_acMvRefineH : s_acMvRefineQ);
    645 
     695 
    646696  for (UInt i = 0; i < 9; i++)
    647697  {
    648698    TComMv cMvTest = pcMvRefine[i];
     699    cMvTest += baseRefMv;
     700   
     701    Int horVal = cMvTest.getHor() * iFrac;
     702    Int verVal = cMvTest.getVer() * iFrac;
     703    piRefPos = m_filteredBlock[ verVal & 3 ][ horVal & 3 ].getLumaAddr();
     704    if ( horVal == 2 && ( verVal & 1 ) == 0 )
     705      piRefPos += 1;
     706    if ( ( horVal & 1 ) == 0 && verVal == 2 )
     707      piRefPos += iRefStride;
     708    cMvTest = pcMvRefine[i];
    649709    cMvTest += rcMvFrac;
    650     piRefPos = piRef + (pcMvRefine[i].getHor() + iRefStride * pcMvRefine[i].getVer()) * iFrac;
     710
     711    setDistParamComp(0);  // Y component
     712
    651713    m_cDistParam.pCur = piRefPos;
    652     uiDist = m_cDistParam.DistFuncRnd( &m_cDistParam, pcRef2, bRound );
     714    uiDist = m_cDistParam.DistFunc( &m_cDistParam );
    653715    uiDist += m_pcRdCost->getCost( cMvTest.getHor(), cMvTest.getVer() );
    654 
     716   
    655717    if ( uiDist < uiDistBest )
    656718    {
     
    659721    }
    660722  }
    661 
     723 
    662724  rcMvFrac = pcMvRefine[uiDirecBest];
    663 
    664   return uiDistBest;
    665 }
    666 #endif
    667 
    668 //<--
    669 
    670 UInt TEncSearch::xPatternRefinement( TComPattern* pcPatternKey, Pel* piRef, Int iRefStride, Int iIntStep, Int iFrac, TComMv& rcMvFrac )
    671 {
    672   UInt  uiDist;
    673   UInt  uiDistBest  = MAX_UINT;
    674   UInt  uiDirecBest = 0;
    675 
    676   Pel*  piRefPos;
    677   m_pcRdCost->setDistParam( pcPatternKey, piRef, iRefStride, iIntStep, m_cDistParam, m_pcEncCfg->getUseHADME() );
    678 
    679   TComMv* pcMvRefine = (iFrac == 2 ? s_acMvRefineH : s_acMvRefineQ);
    680 
    681   for (UInt i = 0; i < 9; i++)
    682   {
    683     TComMv cMvTest = pcMvRefine[i];
    684     cMvTest += rcMvFrac;
    685     piRefPos = piRef + (pcMvRefine[i].getHor() + iRefStride * pcMvRefine[i].getVer()) * iFrac;
    686     m_cDistParam.pCur = piRefPos;
    687 #ifdef WEIGHT_PRED
    688     setDistParamComp(0);  // Y component
    689 #endif
    690     uiDist = m_cDistParam.DistFunc( &m_cDistParam );
    691     uiDist += m_pcRdCost->getCost( cMvTest.getHor(), cMvTest.getVer() );
    692 
    693     if ( uiDist < uiDistBest )
    694     {
    695       uiDistBest  = uiDist;
    696       uiDirecBest = i;
    697     }
    698   }
    699 
    700   rcMvFrac = pcMvRefine[uiDirecBest];
    701 
     725 
    702726  return uiDistBest;
    703727}
     
    715739  UInt  uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - uiFullDepth;
    716740
    717 #if CAVLC_RQT_CBP
    718   if(pcCU->getSlice()->getSymbolMode() == 0)
    719   {
    720     if(bLuma && bChroma)
    721     {
    722       m_pcEntropyCoder->m_pcEntropyCoderIf->codeCbfTrdiv( pcCU, uiAbsPartIdx, uiFullDepth);
     741  {
     742    if( pcCU->getPredictionMode(0) == MODE_INTRA && pcCU->getPartitionSize(0) == SIZE_NxN && uiTrDepth == 0 )
     743    {
     744      assert( uiSubdiv );
     745    }
     746    else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
     747    {
     748      assert( uiSubdiv );
     749    }
     750    else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     751    {
     752      assert( !uiSubdiv );
     753    }
     754    else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
     755    {
     756      assert( !uiSubdiv );
    723757    }
    724758    else
    725759    {
    726       UInt uiFlagPattern = m_pcEntropyCoder->m_pcEntropyCoderIf->xGetFlagPattern(pcCU, uiAbsPartIdx, uiFullDepth);
    727       if(bLuma)
    728       {
    729         if(uiTrDepth==0 || pcCU->getCbf(uiAbsPartIdx, TEXT_LUMA, uiTrDepth-1))
    730           m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiTrDepth);
    731         if(uiFlagPattern & 0x01)
    732           m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, uiFullDepth );
    733       }
    734       else if(bChroma)
    735       {
    736         if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
    737         {
    738           if(uiTrDepth==0 || pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth-1))
    739             m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth);
    740           if(uiTrDepth==0 || pcCU->getCbf(uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth-1))
    741             m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth);
    742         }
    743         if(uiFlagPattern & 0x01)
    744           m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, uiFullDepth );
    745       }
    746     }
    747   }
    748 #endif
    749 
    750 #if CAVLC_RQT_CBP
    751   if(pcCU->getSlice()->getSymbolMode())
    752   {
    753 #endif
    754   if( pcCU->getPredictionMode(0) == MODE_INTRA && pcCU->getPartitionSize(0) == SIZE_NxN && uiTrDepth == 0 )
    755   {
    756     assert( uiSubdiv );
    757   }
    758   else if( uiLog2TrafoSize > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() )
    759   {
    760     assert( uiSubdiv );
    761   }
    762   else if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
    763   {
    764     assert( !uiSubdiv );
    765   }
    766   else if( uiLog2TrafoSize == pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
    767   {
    768     assert( !uiSubdiv );
    769   }
    770   else
    771   {
    772     assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
    773     if( bLuma )
    774     {
    775       m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, uiFullDepth );
    776     }
    777   }
    778 #if CAVLC_RQT_CBP
    779   }
    780 #endif
     760      assert( uiLog2TrafoSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
     761      if( bLuma )
     762      {
     763        m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, uiFullDepth );
     764      }
     765    }
     766  }
     767 
     768  if ( bChroma )
     769  {
     770    if( uiLog2TrafoSize > 2 )
     771    {
     772      if( uiTrDepth==0 || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth-1 ) )
     773        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth );
     774      if( uiTrDepth==0 || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth-1 ) )
     775        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth );
     776    }
     777  }
    781778
    782779  if( uiSubdiv )
     
    789786    return;
    790787  }
    791 
    792   if(pcCU->getSlice()->getSymbolMode())
     788 
    793789  {
    794790    //===== Cbfs =====
     
    796792    {
    797793      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    798     }
    799     if( bChroma )
    800     {
    801       Bool bCodeChroma = true;
    802       if( uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
    803       {
    804         assert( uiTrDepth > 0 );
    805         UInt uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrDepth - 1 ) << 1 );
    806         bCodeChroma  = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
    807       }
    808       if( bCodeChroma )
    809       {
    810         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth );
    811         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth );
    812       }
    813794    }
    814795  }
     
    828809  UInt  uiLog2TrafoSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth()] + 2 - uiFullDepth;
    829810  UInt  uiChroma        = ( eTextType != TEXT_LUMA ? 1 : 0 );
    830 
     811 
    831812  if( uiSubdiv )
    832813  {
    833814    UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( uiFullDepth + 1 ) << 1 );
    834 #if !CAVLC_RQT_CBP
    835     if ( pcCU->getSlice()->getSymbolMode() || pcCU->getCbf( uiAbsPartIdx, eTextType, uiTrDepth ))
    836     {
    837       if(pcCU->getSlice()->getSymbolMode() == 0)
    838       {
    839         if( eTextType == TEXT_LUMA || uiLog2TrafoSize-1 > pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
    840           m_pcEntropyCoder->m_pcEntropyCoderIf->codeBlockCbf(pcCU, uiAbsPartIdx, eTextType, uiTrDepth + 1, uiQPartNum, true);
    841       }
    842 #endif
    843815      for( UInt uiPart = 0; uiPart < 4; uiPart++ )
    844816      {
    845817        xEncCoeffQT( pcCU, uiTrDepth + 1, uiAbsPartIdx + uiPart * uiQPartNum, eTextType, bRealCoeff );
    846818      }
    847 #if !CAVLC_RQT_CBP
    848     }
    849 #endif
    850819    return;
    851820  }
    852 
    853   if( eTextType != TEXT_LUMA && uiLog2TrafoSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     821 
     822  if( eTextType != TEXT_LUMA && uiLog2TrafoSize == 2 )
    854823  {
    855824    assert( uiTrDepth > 0 );
     
    862831    }
    863832  }
    864 
     833 
    865834  //===== coefficients =====
    866835  UInt    uiWidth         = pcCU->getWidth  ( 0 ) >> ( uiTrDepth + uiChroma );
     
    877846  }
    878847  pcCoeff += uiCoeffOffset;
    879 
    880   m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiWidth, uiHeight, uiFullDepth, eTextType, false );
     848 
     849  m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeff, uiAbsPartIdx, uiWidth, uiHeight, uiFullDepth, eTextType );
    881850}
    882851
     
    899868        m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
    900869      }
    901 
     870     
    902871      m_pcEntropyCoder  ->encodePartSize( pcCU, 0, pcCU->getDepth(0), true );
     872
     873      if (pcCU->isIntra(0) && pcCU->getPartitionSize(0) == SIZE_2Nx2N )
     874      {
     875        m_pcEntropyCoder->encodeIPCMInfo( pcCU, 0, true );
     876
     877        if ( pcCU->getIPCMFlag (0))
     878        {
     879          return;
     880        }
     881      }
    903882    }
    904883    // luma prediction mode
     
    944923                            Bool         bLuma,
    945924                            Bool         bChroma,
    946                             Bool         bRealCoeff /* just for test */
    947                             )
     925                            Bool         bRealCoeff /* just for test */ )
    948926{
    949927  m_pcEntropyCoder->resetBits();
     
    970948                                UInt        uiTrDepth,
    971949                                UInt        uiAbsPartIdx,
    972                                 TComYuv*    pcOrgYuv,
    973                                 TComYuv*    pcPredYuv,
    974                                 TComYuv*    pcResiYuv,
    975                                 Dist&       ruiDist
    976                                 )
     950                                TComYuv*    pcOrgYuv,
     951                                TComYuv*    pcPredYuv,
     952                                TComYuv*    pcResiYuv,
     953                                Dist&       ruiDist )
    977954{
    978955  UInt    uiLumaPredMode    = pcCU     ->getLumaIntraDir     ( uiAbsPartIdx );
     
    985962  Pel*    piResi            = pcResiYuv->getLumaAddr( uiAbsPartIdx );
    986963  Pel*    piReco            = pcPredYuv->getLumaAddr( uiAbsPartIdx );
    987 
     964 
    988965  UInt    uiLog2TrSize      = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
    989966  UInt    uiQTLayer         = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    990967  UInt    uiNumCoeffPerInc  = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
    991968  TCoeff* pcCoeff           = m_ppcQTTempCoeffY[ uiQTLayer ] + uiNumCoeffPerInc * uiAbsPartIdx;
     969#if ADAPTIVE_QP_SELECTION
     970  Int*    pcArlCoeff        = m_ppcQTTempArlCoeffY[ uiQTLayer ] + uiNumCoeffPerInc * uiAbsPartIdx;
     971#endif
    992972  Pel*    piRecQt           = m_pcQTTempTComYuv[ uiQTLayer ].getLumaAddr( uiAbsPartIdx );
    993973  UInt    uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getStride  ();
    994 
     974 
    995975  UInt    uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
    996976  Pel*    piRecIPred        = pcCU->getPic()->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), uiZOrder );
    997977  UInt    uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getStride  ();
    998 
     978 
    999979  //===== init availability pattern =====
    1000980  Bool  bAboveAvail = false;
     
    1002982  pcCU->getPattern()->initPattern   ( pcCU, uiTrDepth, uiAbsPartIdx );
    1003983  pcCU->getPattern()->initAdiPattern( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
    1004 
     984 
     985  //===== get prediction signal =====
    1005986#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1006   if( uiLumaPredMode > MAX_MODE_ID_INTRA_DIR )
     987  if( uiLumaPredMode >= NUM_INTRA_MODE )
    1007988  {
    1008989    predIntraLumaDMM( pcCU, uiAbsPartIdx, uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail, true );
    1009990  }
    1010991  else
    1011 #endif
    1012   {
    1013     //===== get prediction signal =====
    1014     predIntraLumaAng( pcCU->getPattern(), uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1015   }
    1016 
     992  {
     993#endif
     994  predIntraLumaAng( pcCU->getPattern(), uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
     995#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
     996  }
     997#endif
     998 
    1017999  //===== get residual signal =====
    10181000  {
     
    10321014    }
    10331015  }
    1034 
     1016 
    10351017  //===== transform and quantization =====
    10361018  //--- init rate estimation arrays for RDOQ ---
    10371019  if( m_pcEncCfg->getUseRDOQ() )
    10381020  {
    1039     m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, TEXT_LUMA );
     1021    m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, TEXT_LUMA );
    10401022  }
    10411023  //--- transform and quantization ---
    10421024  UInt uiAbsSum = 0;
    10431025  pcCU       ->setTrIdxSubParts ( uiTrDepth, uiAbsPartIdx, uiFullDepth );
    1044   m_pcTrQuant->setQPforQuant    ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_LUMA );
    1045   m_pcTrQuant->transformNxN     ( pcCU, piResi, uiStride, pcCoeff, uiWidth, uiHeight, uiAbsSum, TEXT_LUMA, uiAbsPartIdx );
    1046 
     1026
     1027#if H0736_AVC_STYLE_QP_RANGE
     1028  m_pcTrQuant->setQPforQuant    ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     1029#else
     1030  m_pcTrQuant->setQPforQuant    ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_LUMA, 0 );
     1031#endif
     1032
     1033#if RDOQ_CHROMA_LAMBDA
     1034  m_pcTrQuant->selectLambda     (TEXT_LUMA); 
     1035#endif
     1036  m_pcTrQuant->transformNxN     ( pcCU, piResi, uiStride, pcCoeff,
     1037#if ADAPTIVE_QP_SELECTION
     1038                                 pcArlCoeff,
     1039#endif
     1040                                 uiWidth, uiHeight, uiAbsSum, TEXT_LUMA, uiAbsPartIdx );
     1041 
    10471042  //--- set coded block flag ---
    10481043  pcCU->setCbfSubParts          ( ( uiAbsSum ? 1 : 0 ) << uiTrDepth, TEXT_LUMA, uiAbsPartIdx, uiFullDepth );
     
    10501045  if( uiAbsSum )
    10511046  {
    1052 #if INTRA_DST_TYPE_7
    1053     m_pcTrQuant->invtransformNxN( TEXT_LUMA,pcCU->getLumaIntraDir( uiAbsPartIdx ), piResi, uiStride, pcCoeff, uiWidth, uiHeight );
    1054 #else
    1055     m_pcTrQuant->invtransformNxN( piResi, uiStride, pcCoeff, uiWidth, uiHeight );
     1047    Int scalingListType = 0 + g_eTTable[(Int)TEXT_LUMA];
     1048    assert(scalingListType < 6);
     1049#if LOSSLESS_CODING
     1050    m_pcTrQuant->invtransformNxN( pcCU, TEXT_LUMA,pcCU->getLumaIntraDir( uiAbsPartIdx ), piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType );
     1051#else
     1052    m_pcTrQuant->invtransformNxN( TEXT_LUMA,pcCU->getLumaIntraDir( uiAbsPartIdx ), piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType );
    10561053#endif
    10571054  }
     
    10661063    }
    10671064  }
    1068 
     1065 
    10691066  //===== reconstruction =====
    10701067  {
     
    10891086    }
    10901087  }
    1091 
     1088 
    10921089  //===== update distortion =====
    10931090#if HHI_VSO
     
    10991096#endif
    11001097  {
    1101     ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
    1102   }
     1098  ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
    11031099}
     1100}
     1101
    11041102
    11051103Void
     
    11071105                                  UInt        uiTrDepth,
    11081106                                  UInt        uiAbsPartIdx,
    1109                                   TComYuv*    pcOrgYuv,
    1110                                   TComYuv*    pcPredYuv,
    1111                                   TComYuv*    pcResiYuv,
     1107                                  TComYuv*    pcOrgYuv, 
     1108                                  TComYuv*    pcPredYuv, 
     1109                                  TComYuv*    pcResiYuv, 
    11121110                                  Dist&       ruiDist,
    11131111                                  UInt        uiChromaId )
     
    11161114  UInt uiFullDepth  = pcCU->getDepth( 0 ) + uiTrDepth;
    11171115  UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
    1118   if( uiLog2TrSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     1116  if( uiLog2TrSize == 2 )
    11191117  {
    11201118    assert( uiTrDepth > 0 );
     
    11271125    }
    11281126  }
    1129 
     1127 
    11301128  TextType  eText             = ( uiChromaId > 0 ? TEXT_CHROMA_V : TEXT_CHROMA_U );
    11311129  UInt      uiChromaPredMode  = pcCU     ->getChromaIntraDir( uiAbsPartIdx );
     
    11371135  Pel*      piResi            = ( uiChromaId > 0 ? pcResiYuv->getCrAddr( uiAbsPartIdx ) : pcResiYuv->getCbAddr( uiAbsPartIdx ) );
    11381136  Pel*      piReco            = ( uiChromaId > 0 ? pcPredYuv->getCrAddr( uiAbsPartIdx ) : pcPredYuv->getCbAddr( uiAbsPartIdx ) );
    1139 
     1137 
    11401138  UInt      uiQTLayer         = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    11411139  UInt      uiNumCoeffPerInc  = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 ) ) >> 2;
    11421140  TCoeff*   pcCoeff           = ( uiChromaId > 0 ? m_ppcQTTempCoeffCr[ uiQTLayer ] : m_ppcQTTempCoeffCb[ uiQTLayer ] ) + uiNumCoeffPerInc * uiAbsPartIdx;
     1141#if ADAPTIVE_QP_SELECTION
     1142  Int*      pcArlCoeff        = ( uiChromaId > 0 ? m_ppcQTTempArlCoeffCr[ uiQTLayer ] : m_ppcQTTempArlCoeffCb[ uiQTLayer ] ) + uiNumCoeffPerInc * uiAbsPartIdx;
     1143#endif
    11431144  Pel*      piRecQt           = ( uiChromaId > 0 ? m_pcQTTempTComYuv[ uiQTLayer ].getCrAddr( uiAbsPartIdx ) : m_pcQTTempTComYuv[ uiQTLayer ].getCbAddr( uiAbsPartIdx ) );
    11441145  UInt      uiRecQtStride     = m_pcQTTempTComYuv[ uiQTLayer ].getCStride();
    1145 
     1146 
    11461147  UInt      uiZOrder          = pcCU->getZorderIdxInCU() + uiAbsPartIdx;
    11471148  Pel*      piRecIPred        = ( uiChromaId > 0 ? pcCU->getPic()->getPicYuvRec()->getCrAddr( pcCU->getAddr(), uiZOrder ) : pcCU->getPic()->getPicYuvRec()->getCbAddr( pcCU->getAddr(), uiZOrder ) );
    11481149  UInt      uiRecIPredStride  = pcCU->getPic()->getPicYuvRec()->getCStride();
    1149 
     1150 
    11501151  //===== update chroma mode =====
    1151 #if !LM_CHROMA
    1152   if( uiChromaPredMode == 4 )
     1152  if( uiChromaPredMode == DM_CHROMA_IDX )
    11531153  {
    11541154    uiChromaPredMode          = pcCU->getLumaIntraDir( 0 );
    1155   }
    1156 #endif
    1157 
     1155#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
     1156    mapDMMtoIntraMode( uiChromaPredMode );
     1157#endif
     1158  }
     1159 
    11581160  //===== init availability pattern =====
    11591161  Bool  bAboveAvail = false;
    11601162  Bool  bLeftAvail  = false;
    11611163  pcCU->getPattern()->initPattern         ( pcCU, uiTrDepth, uiAbsPartIdx );
     1164
     1165  if( uiChromaPredMode == LM_CHROMA_IDX && uiChromaId == 0 )
     1166  {
     1167    pcCU->getPattern()->initAdiPattern( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail, true );
     1168    getLumaRecPixels( pcCU->getPattern(), uiWidth, uiHeight );
     1169  }
     1170 
    11621171  pcCU->getPattern()->initAdiPatternChroma( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
    11631172  Int*  pPatChroma  = ( uiChromaId > 0 ? pcCU->getPattern()->getAdiCrBuf( uiWidth, uiHeight, m_piYuvExt ) : pcCU->getPattern()->getAdiCbBuf( uiWidth, uiHeight, m_piYuvExt ) );
    1164 
     1173 
    11651174  //===== get prediction signal =====
    1166 #if LM_CHROMA
    1167   if(pcCU->getSlice()->getSPS()->getUseLMChroma() && uiChromaPredMode == 3)
     1175  if( uiChromaPredMode == LM_CHROMA_IDX )
    11681176  {
    11691177    predLMIntraChroma( pcCU->getPattern(), pPatChroma, piPred, uiStride, uiWidth, uiHeight, uiChromaId );
     
    11711179  else
    11721180  {
    1173     if( uiChromaPredMode == 4 )
    1174     {
    1175       uiChromaPredMode          = pcCU->getLumaIntraDir( 0 );
    1176     }
    1177   predIntraChromaAng( pcCU->getPattern(), pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1178   }
    1179 #else // LM_CHROMA
    1180   predIntraChromaAng( pcCU->getPattern(), pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1181 #endif
    1182 
     1181    predIntraChromaAng( pcCU->getPattern(), pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail ); 
     1182  }
     1183 
    11831184  //===== get residual signal =====
    11841185  {
     
    11981199    }
    11991200  }
    1200 
     1201 
    12011202  //===== transform and quantization =====
    12021203  {
     
    12041205    if( m_pcEncCfg->getUseRDOQ() )
    12051206    {
    1206       m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, eText );
     1207      m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, eText );
    12071208    }
    12081209    //--- transform and quantization ---
    12091210    UInt uiAbsSum = 0;
    1210     m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_CHROMA );
    1211     m_pcTrQuant->transformNxN      ( pcCU, piResi, uiStride, pcCoeff, uiWidth, uiHeight, uiAbsSum, eText, uiAbsPartIdx );
     1211
     1212#if H0736_AVC_STYLE_QP_RANGE
     1213    if(eText == TEXT_CHROMA_U)
     1214    {
     1215      m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     1216    }
     1217    else
     1218    {
     1219      m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaQpOffset2nd() );
     1220    }
     1221#else
     1222    if(eText == TEXT_CHROMA_U)
     1223      m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     1224    else
     1225      m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), !pcCU->getSlice()->getDepth(), pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getPPS()->getChromaQpOffset2nd() );
     1226#endif
     1227
     1228#if RDOQ_CHROMA_LAMBDA
     1229    m_pcTrQuant->selectLambda      (TEXT_CHROMA); 
     1230#endif
     1231    m_pcTrQuant->transformNxN      ( pcCU, piResi, uiStride, pcCoeff,
     1232#if ADAPTIVE_QP_SELECTION
     1233                                     pcArlCoeff,
     1234#endif
     1235                                     uiWidth, uiHeight, uiAbsSum, eText, uiAbsPartIdx );
    12121236    //--- set coded block flag ---
    12131237    pcCU->setCbfSubParts           ( ( uiAbsSum ? 1 : 0 ) << uiOrgTrDepth, eText, uiAbsPartIdx, pcCU->getDepth(0) + uiTrDepth );
     
    12151239    if( uiAbsSum )
    12161240    {
    1217 #if INTRA_DST_TYPE_7
    1218     m_pcTrQuant->invtransformNxN( TEXT_CHROMA, REG_DCT, piResi, uiStride, pcCoeff, uiWidth, uiHeight );
    1219 #else
    1220     m_pcTrQuant->invtransformNxN( piResi, uiStride, pcCoeff, uiWidth, uiHeight );
     1241      Int scalingListType = 0 + g_eTTable[(Int)eText];
     1242      assert(scalingListType < 6);
     1243#if LOSSLESS_CODING
     1244      m_pcTrQuant->invtransformNxN( pcCU, TEXT_CHROMA, REG_DCT, piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType );
     1245#else
     1246      m_pcTrQuant->invtransformNxN( TEXT_CHROMA, REG_DCT, piResi, uiStride, pcCoeff, uiWidth, uiHeight, scalingListType );
    12211247#endif
    12221248    }
     
    12321258    }
    12331259  }
    1234 
     1260 
    12351261  //===== reconstruction =====
    12361262  {
     
    12551281    }
    12561282  }
    1257 
     1283 
    12581284  //===== update distortion =====
     1285#if WEIGHTED_CHROMA_DISTORTION
     1286  ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight, true );
     1287#else
    12591288  ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
     1289#endif
    12601290}
    12611291
    12621292
    12631293
    1264 Void
    1265 TEncSearch::xRecurIntraCodingQT( TComDataCU*  pcCU,
     1294Void 
     1295TEncSearch::xRecurIntraCodingQT( TComDataCU*  pcCU, 
    12661296                                UInt         uiTrDepth,
    1267                                 UInt         uiAbsPartIdx,
     1297                                UInt         uiAbsPartIdx, 
    12681298                                Bool         bLumaOnly,
    1269                                 TComYuv*     pcOrgYuv,
    1270                                 TComYuv*     pcPredYuv,
    1271                                 TComYuv*     pcResiYuv,
     1299                                TComYuv*     pcOrgYuv, 
     1300                                TComYuv*     pcPredYuv, 
     1301                                TComYuv*     pcResiYuv, 
    12721302                                Dist&        ruiDistY,
    12731303                                Dist&        ruiDistC,
     
    12811311  Bool    bCheckFull    = ( uiLog2TrSize  <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
    12821312  Bool    bCheckSplit   = ( uiLog2TrSize  >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
    1283 
     1313 
    12841314#if HHI_RQT_INTRA_SPEEDUP
    12851315  if( bCheckFirst && bCheckFull )
     
    12891319#endif
    12901320#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1291   if( pcCU->getLumaIntraDir( uiAbsPartIdx ) > MAX_MODE_ID_INTRA_DIR )
     1321  if( pcCU->getLumaIntraDir( uiAbsPartIdx ) >= NUM_INTRA_MODE )
    12921322  {
    12931323    bCheckSplit = false;
     
    13001330  UInt    uiSingleCbfU  = 0;
    13011331  UInt    uiSingleCbfV  = 0;
    1302 
     1332 
    13031333  if( bCheckFull )
    13041334  {
     
    13081338      m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_ROOT ] );
    13091339    }
    1310 
    13111340    //----- code luma block with given intra prediction mode and store Cbf-----
    13121341    dSingleCost   = 0.0;
    1313     xIntraCodingLumaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistY );
     1342    xIntraCodingLumaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistY ); 
    13141343    if( bCheckSplit )
    13151344    {
     
    13191348    if( !bLumaOnly )
    13201349    {
    1321       xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 0 );
    1322       xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 1 );
     1350      xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 0 ); 
     1351      xIntraCodingChromaBlk ( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, uiSingleDistC, 1 ); 
    13231352      if( bCheckSplit )
    13241353      {
     
    13361365    }
    13371366    else
    1338     {
    1339       dSingleCost = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDistY + uiSingleDistC );
    1340     }
    1341 #else
    1342     dSingleCost = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDistY + uiSingleDistC );
    1343 #endif
    1344   }
    1345 
     1367#endif
     1368    {
     1369    dSingleCost       = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDistY + uiSingleDistC );
     1370  }
     1371  }
     1372 
    13461373  if( bCheckSplit )
    13471374  {
     
    14111438    }
    14121439    else
    1413     {
    1414       dSplitCost       = m_pcRdCost->calcRdCost( uiSplitBits, uiSplitDistY + uiSplitDistC );
    1415     }
    1416 #else
     1440#endif
     1441    {
    14171442    dSplitCost       = m_pcRdCost->calcRdCost( uiSplitBits, uiSplitDistY + uiSplitDistC );
    1418 #endif
    1419 
     1443    }
     1444   
    14201445    //===== compare and set best =====
    14211446    if( dSplitCost < dSingleCost )
     
    14321457      m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiFullDepth ][ CI_QT_TRAFO_TEST ] );
    14331458    }
    1434   }
    1435 
    1436   if( bCheckSplit )
    1437   {
     1459   
    14381460    //--- set transform index and Cbf values ---
    14391461    pcCU->setTrIdxSubParts( uiTrDepth, uiAbsPartIdx, uiFullDepth );
     
    14441466      pcCU->setCbfSubParts( uiSingleCbfV << uiTrDepth, TEXT_CHROMA_V, uiAbsPartIdx, uiFullDepth );
    14451467    }
    1446 
     1468   
    14471469    //--- set reconstruction for next intra prediction blocks ---
    14481470    UInt  uiWidth     = pcCU->getWidth ( 0 ) >> uiTrDepth;
     
    15201542    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
    15211543    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    1522 
     1544   
    15231545    Bool bSkipChroma  = false;
    15241546    Bool bChromaSame  = false;
    1525     if( !bLumaOnly && uiLog2TrSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     1547    if( !bLumaOnly && uiLog2TrSize == 2 )
    15261548    {
    15271549      assert( uiTrDepth > 0 );
     
    15301552      bChromaSame  = true;
    15311553    }
    1532 
     1554   
    15331555    //===== copy transform coefficients =====
    15341556    UInt uiNumCoeffY    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
     
    15371559    TCoeff* pcCoeffDstY = pcCU->getCoeffY ()              + ( uiNumCoeffIncY * uiAbsPartIdx );
    15381560    ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
     1561#if ADAPTIVE_QP_SELECTION
     1562    Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [ uiQTLayer ] + ( uiNumCoeffIncY * uiAbsPartIdx );
     1563    Int* pcArlCoeffDstY = pcCU->getArlCoeffY ()              + ( uiNumCoeffIncY * uiAbsPartIdx );
     1564    ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
     1565#endif
    15391566    if( !bLumaOnly && !bSkipChroma )
    15401567    {
     
    15471574      ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
    15481575      ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
    1549     }
    1550 
     1576#if ADAPTIVE_QP_SELECTION
     1577      Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
     1578      Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
     1579      Int* pcArlCoeffDstU = pcCU->getArlCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
     1580      Int* pcArlCoeffDstV = pcCU->getArlCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
     1581      ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
     1582      ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
     1583#endif
     1584    }
     1585   
    15511586    //===== copy reconstruction =====
    15521587    m_pcQTTempTComYuv[ uiQTLayer ].copyPartToPartLuma( pcRecoYuv, uiAbsPartIdx, 1 << uiLog2TrSize, 1 << uiLog2TrSize );
     
    15691604
    15701605
    1571 Void
    1572 TEncSearch::xRecurIntraChromaCodingQT( TComDataCU*  pcCU,
     1606Void 
     1607TEncSearch::xRecurIntraChromaCodingQT( TComDataCU*  pcCU, 
    15731608                                      UInt         uiTrDepth,
    1574                                       UInt         uiAbsPartIdx,
    1575                                       TComYuv*     pcOrgYuv,
    1576                                       TComYuv*     pcPredYuv,
    1577                                       TComYuv*     pcResiYuv,
     1609                                      UInt         uiAbsPartIdx, 
     1610                                      TComYuv*     pcOrgYuv, 
     1611                                      TComYuv*     pcPredYuv, 
     1612                                      TComYuv*     pcResiYuv, 
    15781613                                      Dist&        ruiDist )
    15791614{
     
    15821617  if(  uiTrMode == uiTrDepth )
    15831618  {
    1584     xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 0 );
    1585     xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 1 );
     1619    xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 0 ); 
     1620    xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 1 ); 
    15861621  }
    15871622  else
     
    16171652    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
    16181653    UInt uiQTLayer    = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    1619 
     1654   
    16201655    Bool bChromaSame  = false;
    1621     if( uiLog2TrSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     1656    if( uiLog2TrSize == 2 )
    16221657    {
    16231658      assert( uiTrDepth > 0 );
     
    16291664      bChromaSame     = true;
    16301665    }
    1631 
     1666   
    16321667    //===== copy transform coefficients =====
    16331668    UInt uiNumCoeffC    = ( pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() ) >> ( uiFullDepth << 1 );
     
    16431678    ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
    16441679    ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
    1645 
     1680#if ADAPTIVE_QP_SELECTION   
     1681    Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
     1682    Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[ uiQTLayer ] + ( uiNumCoeffIncC * uiAbsPartIdx );
     1683    Int* pcArlCoeffDstU = pcCU->getArlCoeffCb()              + ( uiNumCoeffIncC * uiAbsPartIdx );
     1684    Int* pcArlCoeffDstV = pcCU->getArlCoeffCr()              + ( uiNumCoeffIncC * uiAbsPartIdx );
     1685    ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
     1686    ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
     1687#endif
     1688   
    16461689    //===== copy reconstruction =====
    16471690    UInt uiLog2TrSizeChroma = ( bChromaSame ? uiLog2TrSize : uiLog2TrSize - 1 );
     
    16591702
    16601703
    1661 Void
    1662 TEncSearch::preestChromaPredMode( TComDataCU* pcCU,
    1663                                  TComYuv*    pcOrgYuv,
     1704Void 
     1705TEncSearch::preestChromaPredMode( TComDataCU* pcCU, 
     1706                                 TComYuv*    pcOrgYuv, 
    16641707                                 TComYuv*    pcPredYuv )
    16651708{
     
    16711714  Pel*  piPredU     = pcPredYuv->getCbAddr ( 0 );
    16721715  Pel*  piPredV     = pcPredYuv->getCrAddr ( 0 );
    1673 
     1716 
    16741717  //===== init pattern =====
    16751718  Bool  bAboveAvail = false;
     
    16791722  Int*  pPatChromaU = pcCU->getPattern()->getAdiCbBuf( uiWidth, uiHeight, m_piYuvExt );
    16801723  Int*  pPatChromaV = pcCU->getPattern()->getAdiCrBuf( uiWidth, uiHeight, m_piYuvExt );
    1681 
     1724 
    16821725  //===== get best prediction modes (using SAD) =====
    16831726  UInt  uiMinMode   = 0;
     
    16901733    predIntraChromaAng( pcCU->getPattern(), pPatChromaU, uiMode, piPredU, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    16911734    predIntraChromaAng( pcCU->getPattern(), pPatChromaV, uiMode, piPredV, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1692 
     1735   
    16931736    //--- get SAD ---
    1694     UInt  uiSAD  = m_pcRdCost->calcHAD( piOrgU, uiStride, piPredU, uiStride, uiWidth, uiHeight );  //GT: change metric here?
    1695     uiSAD       += m_pcRdCost->calcHAD( piOrgV, uiStride, piPredV, uiStride, uiWidth, uiHeight );  //GT: change metric here?
     1737    UInt  uiSAD  = m_pcRdCost->calcHAD( piOrgU, uiStride, piPredU, uiStride, uiWidth, uiHeight );
     1738    uiSAD       += m_pcRdCost->calcHAD( piOrgV, uiStride, piPredV, uiStride, uiWidth, uiHeight );
    16961739    //--- check ---
    16971740    if( uiSAD < uiMinSAD )
     
    17011744    }
    17021745  }
    1703 
     1746 
    17041747  //===== set chroma pred mode =====
    17051748  pcCU->setChromIntraDirSubParts( uiBestMode, 0, pcCU->getDepth( 0 ) );
    17061749}
    17071750
    1708 Void
    1709 TEncSearch::estIntraPredQT( TComDataCU* pcCU,
    1710                             TComYuv*    pcOrgYuv,
    1711                             TComYuv*    pcPredYuv,
    1712                             TComYuv*    pcResiYuv,
    1713                             TComYuv*    pcRecoYuv,
    1714                             Dist&       ruiDistC,
    1715                             Bool        bLumaOnly )
     1751Void 
     1752TEncSearch::estIntraPredQT( TComDataCU* pcCU, 
     1753                           TComYuv*    pcOrgYuv,
     1754                           TComYuv*    pcPredYuv,
     1755                           TComYuv*    pcResiYuv,
     1756                           TComYuv*    pcRecoYuv,
     1757                           Dist&       ruiDistC,
     1758                           Bool        bLumaOnly )
    17161759{
    17171760  UInt    uiDepth        = pcCU->getDepth(0);
     
    17221765  UInt    uiQNumParts    = pcCU->getTotalNumPart() >> 2;
    17231766  UInt    uiWidthBit     = pcCU->getIntraSizeIdx(0);
    1724   Dist    uiOverallDistY = 0;
    1725   Dist    uiOverallDistC = 0;
     1767  UInt    uiOverallDistY = 0;
     1768  UInt    uiOverallDistC = 0;
    17261769  UInt    CandNum;
    1727   UInt    CandModeList[ FAST_UDI_MAX_RDMODE_NUM ];
    17281770  Double  CandCostList[ FAST_UDI_MAX_RDMODE_NUM ];
    1729   UInt    uiFastCandNum=g_aucIntraModeNumFast[ uiWidthBit ];
    1730 
     1771 
    17311772  //===== set QP and clear Cbf =====
    1732   pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
    1733 
     1773  if ( pcCU->getSlice()->getPPS()->getUseDQP() == true)
     1774  {
     1775    pcCU->setQPSubParts( pcCU->getQP(0), 0, uiDepth );
     1776  }
     1777  else
     1778  {
     1779    pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
     1780  }
     1781 
    17341782  //===== loop over partitions =====
    17351783  UInt uiPartOffset = 0;
     
    17411789    pcCU->getPattern()->initPattern   ( pcCU, uiInitTrDepth, uiPartOffset );
    17421790    pcCU->getPattern()->initAdiPattern( pcCU, uiPartOffset, uiInitTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
    1743 
     1791   
    17441792    //===== determine set of modes to be tested (using prediction signal only) =====
    1745     UInt uiMaxMode     = g_aucIntraModeNumAng[uiWidthBit];
    1746 #if ADD_PLANAR_MODE
    1747     uiMaxMode += 1;
    1748 #endif
    1749 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1750     Bool bTestDmm = false;
    1751     if ( m_pcEncCfg->isDepthCoder() && m_pcEncCfg->getUseDMM() )
    1752       bTestDmm = true;
    1753 #endif
    1754     UInt uiMaxModeFast = g_aucIntraModeNumFast[ uiWidthBit ];
     1793#if LOGI_INTRA_NAME_3MPM
     1794    Int numModesAvailable     = 35; //total number of Intra modes
     1795#else
     1796    Int numModesAvailable     = g_aucIntraModeNumAng[uiWidthBit];
     1797#endif
    17551798    Pel* piOrg         = pcOrgYuv ->getLumaAddr( uiPU, uiWidth );
    17561799    Pel* piPred        = pcPredYuv->getLumaAddr( uiPU, uiWidth );
    17571800    UInt uiStride      = pcPredYuv->getStride();
    1758 
    1759     if ( uiFastCandNum != uiMaxMode ) uiMaxModeFast = 0;
    1760     for( Int i=0; i < uiFastCandNum; i++ )
    1761     {
    1762       CandCostList[ i ] = MAX_DOUBLE;
    1763     }
    1764     CandNum = 0;
    1765 
    1766 #if ADD_PLANAR_MODE
    1767     UInt uiHdModeList[NUM_INTRA_MODE];
    1768     uiHdModeList[0] = PLANAR_IDX;
    1769     for( Int i=1; i < uiMaxMode; i++) uiHdModeList[i] = i-1;
    1770 
    1771     for( Int iMode = Int(uiMaxModeFast); iMode < Int(uiMaxMode); iMode++ )
    1772     {
    1773       UInt uiMode = uiHdModeList[iMode];
    1774 #if (!REFERENCE_SAMPLE_PADDING)
    1775       if ( !predIntraLumaDirAvailable( uiMode, uiWidthBit, bAboveAvail, bLeftAvail ) )
    1776         continue;
    1777 #endif
    1778       predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1779 #else
    1780     for( UInt uiMode = uiMaxModeFast; uiMode < uiMaxMode; uiMode++ )
    1781     {
    1782 #if (!REFERENCE_SAMPLE_PADDING)
    1783       if ( !predIntraLumaDirAvailable( uiMode, uiWidthBit, bAboveAvail, bLeftAvail ) )
    1784         continue;
    1785 #endif
    1786 
    1787       predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1788 #endif
    1789       // use hadamard transform here
     1801    UInt uiRdModeList[FAST_UDI_MAX_RDMODE_NUM];
     1802    Int numModesForFullRD = g_aucIntraModeNumFast[ uiWidthBit ];
     1803   
     1804#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
     1805    Bool bTestDmm = ( m_pcEncCfg->getUseDMM() );
     1806#endif
     1807
     1808    Bool doFastSearch = (numModesForFullRD != numModesAvailable);
     1809    if (doFastSearch)
     1810    {
     1811      assert(numModesForFullRD < numModesAvailable);
     1812
     1813      for( Int i=0; i < numModesForFullRD; i++ )
     1814      {
     1815        CandCostList[ i ] = MAX_DOUBLE;
     1816      }
     1817      CandNum = 0;
     1818     
     1819      for( Int modeIdx = 0; modeIdx < numModesAvailable; modeIdx++ )
     1820      {
     1821        UInt uiMode = modeIdx;
     1822
     1823        predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
     1824       
     1825        // use hadamard transform here
    17901826      Dist uiSad;
    17911827#if HHI_VSO
     
    17961832      }
    17971833      else
     1834#endif
    17981835      {
    17991836        uiSad = (Dist) m_pcRdCost->calcHAD( piOrg, uiStride, piPred, uiStride, uiWidth, uiHeight );
    18001837      }
    1801 #else
    1802         uiSad = (Dist) m_pcRdCost->calcHAD( piOrg, uiStride, piPred, uiStride, uiWidth, uiHeight );
    1803 #endif
    1804 
    1805       UInt   iModeBits = xModeBitsIntra( pcCU, uiMode, uiPU, uiPartOffset, uiDepth, uiInitTrDepth );
     1838       
     1839        UInt   iModeBits = xModeBitsIntra( pcCU, uiMode, uiPU, uiPartOffset, uiDepth, uiInitTrDepth );
    18061840
    18071841      Double dLambda;
     
    18211855
    18221856      Double cost = (Double)uiSad + (Double)iModeBits *  dLambda;
    1823 
    1824       CandNum += xUpdateCandList( uiMode, cost, uiFastCandNum, CandModeList, CandCostList );
     1857       
     1858        CandNum += xUpdateCandList( uiMode, cost, numModesForFullRD, uiRdModeList, CandCostList );
     1859
    18251860#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1826       if ( bTestDmm ) bTestDmm = uiSad ? true : false;
    1827 #endif
    1828     }
    1829     UInt uiRdModeList[FAST_UDI_MAX_RDMODE_NUM];
    1830     UInt uiNewMaxMode;
    1831     UInt uiMinMode = 0;
    1832 
    1833     if(uiFastCandNum!=uiMaxMode)
    1834     {
    1835       uiNewMaxMode = Min( uiFastCandNum, CandNum );
    1836       for( Int i = 0; i < uiNewMaxMode; i++)
    1837       {
    1838         uiRdModeList[i] = CandModeList[i];
    1839       }
     1861        if( bTestDmm ) bTestDmm = uiSad ? true : false;
     1862#endif
     1863      }
     1864   
    18401865#if FAST_UDI_USE_MPM
    1841 #if MTK_DCM_MPM
     1866#if LOGI_INTRA_NAME_3MPM
     1867      Int uiPreds[3] = {-1, -1, -1};
     1868#else
    18421869      Int uiPreds[2] = {-1, -1};
    1843       Int numCand = pcCU->getIntraDirLumaPredictor(uiPartOffset, uiPreds);
    1844 
     1870#endif
     1871      Int iMode = -1;
     1872      Int numCand = pcCU->getIntraDirLumaPredictor( uiPartOffset, uiPreds, &iMode );
     1873#if LOGI_INTRA_NAME_3MPM
     1874      if( iMode >= 0 )
     1875      {
     1876        numCand = iMode;
     1877      }
     1878#else
     1879      if( iMode >= 0 )
     1880      {
     1881        numCand = 1;
     1882        uiPreds[0] = iMode;
     1883      }
     1884#endif
     1885     
    18451886      for( Int j=0; j < numCand; j++)
     1887
    18461888      {
    18471889        Bool mostProbableModeIncluded = false;
    18481890        Int mostProbableMode = uiPreds[j];
    1849 #if ADD_PLANAR_MODE
    1850       if (mostProbableMode == 2)
    1851       {
    1852         mostProbableMode = PLANAR_IDX;
    1853       }
    1854 #endif
    1855         for( Int i=0; i < uiNewMaxMode; i++)
     1891       
     1892        for( Int i=0; i < numModesForFullRD; i++)
    18561893        {
    18571894          mostProbableModeIncluded |= (mostProbableMode == uiRdModeList[i]);
     
    18591896        if (!mostProbableModeIncluded)
    18601897        {
    1861           uiRdModeList[uiNewMaxMode++] = mostProbableMode;
    1862         }
    1863       }
    1864 #else
    1865       Int mostProbableMode = pcCU->getMostProbableIntraDirLuma( uiPartOffset );
    1866 #if ADD_PLANAR_MODE
    1867       if (mostProbableMode == 2)
    1868       {
    1869         mostProbableMode = PLANAR_IDX;
    1870       }
    1871 #endif
    1872       Bool mostProbableModeIncluded = false;
    1873       for( Int i=0; i < uiNewMaxMode; i++)
    1874       {
    1875         mostProbableModeIncluded |= (mostProbableMode == uiRdModeList[i]);
    1876       }
    1877       if (!mostProbableModeIncluded)
    1878       {
    1879         uiRdModeList[uiNewMaxMode++] = mostProbableMode;
    1880       }
    1881 #endif
    1882 #endif
     1898          uiRdModeList[numModesForFullRD++] = mostProbableMode;
     1899        }
     1900      }
     1901#endif // FAST_UDI_USE_MPM
    18831902    }
    18841903    else
    18851904    {
    1886       uiNewMaxMode = uiMaxMode;
    1887 #if ADD_PLANAR_MODE
    1888       uiRdModeList[0] = PLANAR_IDX;
    1889       for( Int i=1; i < uiNewMaxMode; i++) uiRdModeList[i] = i-1;
    1890 #else
    1891       for( Int i=0; i < uiNewMaxMode; i++) uiRdModeList[i] = i;
    1892 #endif
    1893     }
    1894 
     1905      for( Int i=0; i < numModesForFullRD; i++)
     1906      {
     1907        uiRdModeList[i] = i;
     1908      }
     1909    }
     1910   
    18951911#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1896     if( m_pcEncCfg->isDepthCoder() && uiWidth >= 4 && uiWidth < 64 && m_pcEncCfg->getUseDMM() && bTestDmm && uiWidth == uiHeight )
     1912    if( m_pcEncCfg->getUseDMM() && bTestDmm && uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE && uiWidth == uiHeight )
    18971913    {
    18981914#if HHI_DMM_WEDGE_INTRA
     
    19001916      Int  iDeltaDC1 = 0;
    19011917      Int  iDeltaDC2 = 0;
    1902       findWedgeFullMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTabIdx, iDeltaDC1, iDeltaDC2, bAboveAvail, bLeftAvail, WedgeDist_SAD );
     1918      findWedgeFullMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTabIdx, iDeltaDC1, iDeltaDC2, bAboveAvail, bLeftAvail );
    19031919      pcCU->setWedgeFullTabIdxSubParts  ( uiTabIdx,  uiPartOffset, uiDepth + uiInitTrDepth );
    19041920      pcCU->setWedgeFullDeltaDC1SubParts( iDeltaDC1, uiPartOffset, uiDepth + uiInitTrDepth );
    19051921      pcCU->setWedgeFullDeltaDC2SubParts( iDeltaDC2, uiPartOffset, uiDepth + uiInitTrDepth );
    19061922
    1907       uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_FULL_IDX;
    1908       uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_FULL_D_IDX;
     1923      uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_FULL_IDX;
     1924      uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_FULL_D_IDX;
    19091925
    19101926      if ( uiWidth > 4 )
     
    19151931        iDeltaDC2 = 0;
    19161932
    1917         findWedgePredDirMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTabIdx, iWedgeDeltaEnd, iDeltaDC1, iDeltaDC2, bAboveAvail, bLeftAvail, WedgeDist_SAD );
     1933        findWedgePredDirMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTabIdx, iWedgeDeltaEnd, iDeltaDC1, iDeltaDC2, bAboveAvail, bLeftAvail );
    19181934        pcCU->setWedgePredDirTabIdxSubParts  ( uiTabIdx,       uiPartOffset, uiDepth + uiInitTrDepth );
    19191935        pcCU->setWedgePredDirDeltaEndSubParts( iWedgeDeltaEnd, uiPartOffset, uiDepth + uiInitTrDepth );
     
    19211937        pcCU->setWedgePredDirDeltaDC2SubParts( iDeltaDC2,      uiPartOffset, uiDepth + uiInitTrDepth );
    19221938
    1923         uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_PREDDIR_IDX;
    1924         uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_PREDDIR_D_IDX;
     1939        uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_PREDDIR_IDX;
     1940        uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_PREDDIR_D_IDX;
    19251941      }
    19261942#endif
    19271943#if HHI_DMM_PRED_TEX
    1928 #if FLEX_CODING_ORDER
    1929       if ( pcCU->getSlice()->getSPS()->getUseDMM34() )
    1930       {
    1931 #endif
    1932       TComYuv cTempYuv; cTempYuv.create( uiWidth, uiHeight ); cTempYuv.clear();
    1933       Pel* piTempY      = cTempYuv.getLumaAddr();
    1934 
    1935       fillTexturePicTempBlock( pcCU, uiPartOffset, piTempY, uiWidth, uiHeight );
    1936 
    1937       piTempY = cTempYuv.getLumaAddr();
    1938 
    19391944      UInt uiTexTabIdx  = 0;
    19401945      Int  iTexDeltaDC1 = 0;
    19411946      Int  iTexDeltaDC2 = 0;
    1942       findWedgeTexMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTexTabIdx, iTexDeltaDC1, iTexDeltaDC2, bAboveAvail, bLeftAvail, WedgeDist_SAD, piTempY );
     1947      findWedgeTexMinDist( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, uiTexTabIdx, iTexDeltaDC1, iTexDeltaDC2, bAboveAvail, bLeftAvail );
    19431948      pcCU->setWedgePredTexTabIdxSubParts  ( uiTexTabIdx,  uiPartOffset, uiDepth + uiInitTrDepth );
    19441949      pcCU->setWedgePredTexDeltaDC1SubParts( iTexDeltaDC1, uiPartOffset, uiDepth + uiInitTrDepth );
    19451950      pcCU->setWedgePredTexDeltaDC2SubParts( iTexDeltaDC2, uiPartOffset, uiDepth + uiInitTrDepth );
    19461951
    1947       uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_PREDTEX_IDX;
    1948       uiRdModeList[ uiNewMaxMode++ ] = DMM_WEDGE_PREDTEX_D_IDX;
     1952      uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_PREDTEX_IDX;
     1953      uiRdModeList[ numModesForFullRD++ ] = DMM_WEDGE_PREDTEX_D_IDX;
    19491954
    19501955      if ( uiWidth > 4 )
    19511956      {
    1952         piTempY = cTempYuv.getLumaAddr();
    1953 
    19541957        iTexDeltaDC1 = 0;
    19551958        iTexDeltaDC2 = 0;
    19561959
    1957         findContourPredTex( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, iTexDeltaDC1, iTexDeltaDC2, bAboveAvail, bLeftAvail, piTempY );
     1960        findContourPredTex( pcCU, uiPartOffset, piOrg, piPred, uiStride, uiWidth, uiHeight, iTexDeltaDC1, iTexDeltaDC2, bAboveAvail, bLeftAvail );
    19581961        pcCU->setContourPredTexDeltaDC1SubParts( iTexDeltaDC1, uiPartOffset, uiDepth + uiInitTrDepth );
    19591962        pcCU->setContourPredTexDeltaDC2SubParts( iTexDeltaDC2, uiPartOffset, uiDepth + uiInitTrDepth );
    19601963
    1961         uiRdModeList[ uiNewMaxMode++ ] = DMM_CONTOUR_PREDTEX_IDX;
    1962         uiRdModeList[ uiNewMaxMode++ ] = DMM_CONTOUR_PREDTEX_D_IDX;
    1963       }
    1964       cTempYuv.destroy();
    1965 #if FLEX_CODING_ORDER
    1966       }
    1967 #endif
    1968 #endif
    1969     }
    1970 #endif
     1964        uiRdModeList[ numModesForFullRD++ ] = DMM_CONTOUR_PREDTEX_IDX;
     1965        uiRdModeList[ numModesForFullRD++ ] = DMM_CONTOUR_PREDTEX_D_IDX;
     1966      }
     1967#endif
     1968    }
     1969#endif
     1970
    19711971    //===== check modes (using r-d costs) =====
    19721972#if HHI_RQT_INTRA_SPEEDUP_MOD
     
    19741974    Double dSecondBestPUCost = MAX_DOUBLE;
    19751975#endif
    1976 
     1976   
    19771977    UInt    uiBestPUMode  = 0;
    1978     Dist    uiBestPUDistY = 0;
    1979     Dist    uiBestPUDistC = 0;
     1978    UInt    uiBestPUDistY = 0;
     1979    UInt    uiBestPUDistC = 0;
    19801980    Double  dBestPUCost   = MAX_DOUBLE;
    1981     for( UInt uiMode = uiMinMode; uiMode < uiNewMaxMode; uiMode++ )
     1981    for( UInt uiMode = 0; uiMode < numModesForFullRD; uiMode++ )
    19821982    {
    19831983      // set luma prediction mode
    19841984      UInt uiOrgMode = uiRdModeList[uiMode];
    1985 
    1986 #if (!REFERENCE_SAMPLE_PADDING)
     1985     
    19871986#if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1988       if ( !predIntraLumaDirAvailable( uiOrgMode, uiWidthBit, bAboveAvail, bLeftAvail, uiWidth, uiHeight, pcCU, uiPartOffset ) )
     1987      if( m_pcEncCfg->getIsDepth() && !predIntraLumaDMMAvailable( uiOrgMode, uiWidth, uiHeight ) )
     1988      {
    19891989        continue;
    1990 #else
    1991       if ( !predIntraLumaDirAvailable( uiOrgMode, uiWidthBit, bAboveAvail, bLeftAvail ) )
    1992         continue;
    1993 #endif
    1994 #else
    1995 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    1996 #if HHI_DMM_PRED_TEX && FLEX_CODING_ORDER
    1997       if( m_pcEncCfg->isDepthCoder() && !predIntraLumaDMMAvailable( uiOrgMode, uiWidth, uiHeight, pcCU->getSlice()->getSPS()->getUseDMM34() ) )
    1998 #else
    1999       if( m_pcEncCfg->isDepthCoder() && !predIntraLumaDMMAvailable( uiOrgMode, uiWidth, uiHeight ) )
    2000 #endif
    2001         continue;
    2002 #endif
     1990      }
    20031991#endif
    20041992
    20051993      pcCU->setLumaIntraDirSubParts ( uiOrgMode, uiPartOffset, uiDepth + uiInitTrDepth );
    2006 
     1994     
    20071995      // set context models
    20081996      if( m_bUseSBACRD )
     
    20101998        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
    20111999      }
    2012 
     2000     
    20132001      // determine residual for partition
    20142002      Dist   uiPUDistY = 0;
     
    20302018      xRecurIntraCodingQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiPUDistY, uiPUDistC, dPUCost );
    20312019#endif
    2032 
     2020     
    20332021      // check r-d cost
    20342022      if( dPUCost < dBestPUCost )
     
    20422030        uiBestPUDistC = uiPUDistC;
    20432031        dBestPUCost   = dPUCost;
    2044 
     2032       
    20452033        xSetIntraResultQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcRecoYuv );
    2046 
     2034       
    20472035        UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
    20482036        ::memcpy( m_puhQTTempTrIdx,  pcCU->getTransformIdx()       + uiPartOffset, uiQPartNum * sizeof( UChar ) );
     
    20502038        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
    20512039        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
    2052 
     2040       
    20532041      }
    20542042#if HHI_RQT_INTRA_SPEEDUP_MOD
     
    20602048#endif
    20612049    } // Mode loop
    2062 
     2050   
    20632051#if HHI_RQT_INTRA_SPEEDUP
    20642052#if HHI_RQT_INTRA_SPEEDUP_MOD
     
    20752063      UInt uiOrgMode = uiBestPUMode;
    20762064#endif
    2077 
    2078 #if (!REFERENCE_SAMPLE_PADDING)
    2079 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2080       if ( !predIntraLumaDirAvailable( uiOrgMode, uiWidthBit, bAboveAvail, bLeftAvail, uiWidth, uiHeight, pcCU, uiPartOffset ) )
    2081         continue;
    2082 #else
    2083       if ( !predIntraLumaDirAvailable( uiOrgMode, uiWidthBit, bAboveAvail, bLeftAvail ) )
    2084         continue;
    2085 #endif
    2086 #else
    2087 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2088 #if HHI_DMM_PRED_TEX && FLEX_CODING_ORDER
    2089       if( m_pcEncCfg->isDepthCoder() && !predIntraLumaDMMAvailable( uiOrgMode, uiWidth, uiHeight, pcCU->getSlice()->getSPS()->getUseDMM34() ) )
    2090 #else
    2091       if( m_pcEncCfg->isDepthCoder() && !predIntraLumaDMMAvailable( uiOrgMode, uiWidth, uiHeight ) )
    2092 #endif
    2093         continue;
    2094 #endif
    2095 #endif
    2096 
     2065     
    20972066      pcCU->setLumaIntraDirSubParts ( uiOrgMode, uiPartOffset, uiDepth + uiInitTrDepth );
    2098 
     2067     
    20992068      // set context models
    21002069      if( m_bUseSBACRD )
     
    21022071        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
    21032072      }
    2104 
     2073     
    21052074      // determine residual for partition
    21062075      Dist   uiPUDistY = 0;
     
    21172086
    21182087      xRecurIntraCodingQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcOrgYuv, pcPredYuv, pcResiYuv, uiPUDistY, uiPUDistC, false, dPUCost );
    2119 
     2088     
    21202089      // check r-d cost
    21212090      if( dPUCost < dBestPUCost )
     
    21252094        uiBestPUDistC = uiPUDistC;
    21262095        dBestPUCost   = dPUCost;
    2127 
     2096       
    21282097        xSetIntraResultQT( pcCU, uiInitTrDepth, uiPartOffset, bLumaOnly, pcRecoYuv );
    2129 
     2098       
    21302099        UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
    21312100        ::memcpy( m_puhQTTempTrIdx,  pcCU->getTransformIdx()       + uiPartOffset, uiQPartNum * sizeof( UChar ) );
     
    21332102        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
    21342103        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, uiQPartNum * sizeof( UChar ) );
    2135 
     2104       
    21362105      }
    21372106    } // Mode loop
    21382107#endif
    2139 
     2108   
    21402109    //--- update overall distortion ---
    21412110    uiOverallDistY += uiBestPUDistY;
    21422111    uiOverallDistC += uiBestPUDistC;
    2143 
     2112   
    21442113    //--- update transform index and cbf ---
    21452114    UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth(0) + uiInitTrDepth ) << 1 );
     
    21482117    ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ) + uiPartOffset, m_puhQTTempCbf[1], uiQPartNum * sizeof( UChar ) );
    21492118    ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ) + uiPartOffset, m_puhQTTempCbf[2], uiQPartNum * sizeof( UChar ) );
    2150 
     2119   
    21512120    //--- set reconstruction for next intra prediction blocks ---
    21522121    if( uiPU != uiNumPU - 1 )
     
    21552124      Bool bChromaSame  = false;
    21562125      UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> ( pcCU->getDepth(0) + uiInitTrDepth ) ] + 2;
    2157       if( !bLumaOnly && uiLog2TrSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     2126      if( !bLumaOnly && uiLog2TrSize == 2 )
    21582127      {
    21592128        assert( uiInitTrDepth  > 0 );
     
    21612130        bChromaSame  = true;
    21622131      }
    2163 
     2132     
    21642133      UInt    uiCompWidth   = pcCU->getWidth ( 0 ) >> uiInitTrDepth;
    21652134      UInt    uiCompHeight  = pcCU->getHeight( 0 ) >> uiInitTrDepth;
     
    22152184      }
    22162185    }
    2217 
     2186   
    22182187    //=== update PU data ====
    22192188    pcCU->setLumaIntraDirSubParts     ( uiBestPUMode, uiPartOffset, uiDepth + uiInitTrDepth );
    22202189    pcCU->copyToPic                   ( uiDepth, uiPU, uiInitTrDepth );
    22212190  } // PU loop
    2222 
    2223 
     2191 
     2192 
    22242193  if( uiNumPU > 1 )
    22252194  { // set Cbf for all blocks
     
    22412210    }
    22422211  }
    2243 
     2212 
    22442213  //===== reset context models =====
    22452214  if(m_bUseSBACRD)
     
    22472216    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
    22482217  }
    2249 
     2218 
    22502219  //===== set distortion (rate and r-d costs are determined later) =====
    22512220  ruiDistC                   = uiOverallDistC;
     
    22552224
    22562225
    2257 Void
    2258 TEncSearch::estIntraPredChromaQT( TComDataCU* pcCU,
    2259                                  TComYuv*    pcOrgYuv,
    2260                                  TComYuv*    pcPredYuv,
    2261                                  TComYuv*    pcResiYuv,
     2226Void 
     2227TEncSearch::estIntraPredChromaQT( TComDataCU* pcCU, 
     2228                                 TComYuv*    pcOrgYuv, 
     2229                                 TComYuv*    pcPredYuv, 
     2230                                 TComYuv*    pcResiYuv, 
    22622231                                 TComYuv*    pcRecoYuv,
    22632232                                 Dist        uiPreCalcDistC )
     
    22672236  Dist    uiBestDist  = 0;
    22682237  Double  dBestCost   = MAX_DOUBLE;
    2269 
     2238 
    22702239  //----- init mode list -----
    2271 #if ADD_PLANAR_MODE
    2272   UInt  uiModeList[6];
    2273   uiModeList[0] = PLANAR_IDX;
    2274   for( Int i = 0; i < 5; i++ )
    2275   {
    2276     uiModeList[i+1] = i;
    2277   }
    2278   UInt uiLumaMode = pcCU->getLumaIntraDir(0);
    2279 #else
    2280   UInt  uiModeList[5];
    2281   for( Int i = 0; i < 4; i++ )
    2282   {
    2283     uiModeList[i] = i;
    2284   }
    2285 
    2286   uiModeList[4]   = pcCU->getLumaIntraDir(0);
    2287 #endif
    2288 
    22892240  UInt  uiMinMode = 0;
    2290 #if CHROMA_CODEWORD
    2291 #if ADD_PLANAR_MODE
    2292   UInt  uiMaxMode = 6;
    2293 
    2294 #if LM_CHROMA
    2295   UInt  uiIgnore;
    2296   if(pcCU->getSlice()->getSPS()->getUseLMChroma())
    2297   {
    2298     uiIgnore = ( ( (uiLumaMode != PLANAR_IDX) && (uiLumaMode >= 3) ) ? uiMaxMode : uiLumaMode );
    2299   }
    2300   else
    2301   {
    2302     uiIgnore = ( ( (uiLumaMode != PLANAR_IDX) && (uiLumaMode >= 4) ) ? uiMaxMode : uiLumaMode );
    2303   }
    2304 #else
    2305   UInt  uiIgnore = ( ( (uiLumaMode != PLANAR_IDX) && (uiLumaMode >= 4) ) ? uiMaxMode : uiLumaMode );
    2306 #endif
    2307 
    2308 #else
    2309   UInt  uiMaxMode = 5;
    2310 
    2311 #if LM_CHROMA
    2312   UInt  uiIgnore;
    2313   if(pcCU->getSlice()->getSPS()->getUseLMChroma())
    2314   {
    2315     uiIgnore = (uiModeList[4] >= 0 && uiModeList[4] < 3) ? uiModeList[4] : 6;
    2316   }
    2317   else
    2318   {
    2319     uiIgnore = (uiModeList[4] >= 0 && uiModeList[4] < 4) ? uiModeList[4] : 6;
    2320   }
    2321 #else
    2322   UInt  uiIgnore = (uiModeList[4] < 4) ? uiModeList[4] : 6;
    2323 #endif
    2324 
    2325 #endif
    2326 #else
    2327 #if ADD_PLANAR_MODE
    2328   UInt  uiMaxMode = ( ( (uiLumaMode != PLANAR_IDX) && (uiLumaMode >= 4) ) ? 6 : 5 );
    2329 #else
    2330   UInt  uiMaxMode = ( uiModeList[4] >= 4 ? 5 : 4 );
    2331 #endif
    2332 #endif
     2241  UInt  uiModeList[ NUM_CHROMA_MODE ];
     2242  pcCU->getAllowedChromaDir( 0, uiModeList );
     2243  UInt  uiMaxMode = NUM_CHROMA_MODE;
    23332244
    23342245  //----- check chroma modes -----
    23352246  for( UInt uiMode = uiMinMode; uiMode < uiMaxMode; uiMode++ )
    23362247  {
    2337 #if CHROMA_CODEWORD
    2338 #if ADD_PLANAR_MODE
    2339     if ( uiModeList[uiMode] == uiIgnore )
    2340 #else
    2341     if (uiMode == uiIgnore)
    2342 #endif
     2248    if ( !pcCU->getSlice()->getSPS()->getUseLMChroma() && uiModeList[uiMode] == LM_CHROMA_IDX )
    23432249    {
    23442250      continue;
    23452251    }
    2346 #endif
    2347 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2348 #if ADD_PLANAR_MODE
    2349     if ( uiModeList[uiMode] == 4 && pcCU->getLumaIntraDir(0) > MAX_MODE_ID_INTRA_DIR )
    2350     {
    2351       continue;
    2352     }
    2353 #else
    2354     if ( uiMode == 4 && pcCU->getLumaIntraDir(0) > MAX_MODE_ID_INTRA_DIR )
    2355     {
    2356       continue;
    2357     }
    2358 #endif
    2359 #endif
    23602252    //----- restore context models -----
    23612253    if( m_bUseSBACRD )
     
    23632255      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
    23642256    }
    2365 
     2257   
    23662258    //----- chroma coding -----
    23672259    Dist    uiDist = 0;
    2368 #if ADD_PLANAR_MODE
    23692260    pcCU->setChromIntraDirSubParts  ( uiModeList[uiMode], 0, uiDepth );
    2370 #else
    2371     pcCU->setChromIntraDirSubParts  ( uiMode, 0, uiDepth );
    2372 #endif
    23732261    xRecurIntraChromaCodingQT       ( pcCU,   0, 0, pcOrgYuv, pcPredYuv, pcResiYuv, uiDist );
    23742262    UInt    uiBits = xGetIntraBitsQT( pcCU,   0, 0, false, true, false );
    23752263    Double  dCost  = m_pcRdCost->calcRdCost( uiBits, uiDist );
    2376 
     2264   
    23772265    //----- compare -----
    23782266    if( dCost < dBestCost )
     
    23802268      dBestCost   = dCost;
    23812269      uiBestDist  = uiDist;
    2382 #if ADD_PLANAR_MODE
    23832270      uiBestMode  = uiModeList[uiMode];
    2384 #else
    2385       uiBestMode  = uiMode;
    2386 #endif
    23872271      UInt  uiQPN = pcCU->getPic()->getNumPartInCU() >> ( uiDepth << 1 );
    23882272      xSetIntraResultChromaQT( pcCU, 0, 0, pcRecoYuv );
     
    23912275    }
    23922276  }
    2393 
     2277 
    23942278  //----- set data -----
    23952279  UInt  uiQPN = pcCU->getPic()->getNumPartInCU() >> ( uiDepth << 1 );
     
    23982282  pcCU->setChromIntraDirSubParts( uiBestMode, 0, uiDepth );
    23992283  pcCU->getTotalDistortion      () += uiBestDist - uiPreCalcDistC;
    2400 
     2284 
    24012285  //----- restore context models -----
    24022286  if( m_bUseSBACRD )
     
    24062290}
    24072291
    2408 #if (!REFERENCE_SAMPLE_PADDING)
    2409 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2410 Bool TEncSearch::predIntraLumaDirAvailable( UInt uiMode, UInt uiWidthBit, Bool bAboveAvail, Bool bLeftAvail, UInt uiWidth, UInt uiHeight, TComDataCU* pcCU, UInt uiAbsPartIdx   )
    2411 #else
    2412 Bool TEncSearch::predIntraLumaDirAvailable( UInt uiMode, UInt uiWidthBit, Bool bAboveAvail, Bool bLeftAvail )
    2413 #endif
     2292/** Function for encoding and reconstructing luma/chroma samples of a PCM mode CU.
     2293 * \param pcCU pointer to current CU
     2294 * \param uiAbsPartIdx part index
     2295 * \param piOrg pointer to original sample arrays
     2296 * \param piPCM pointer to PCM code arrays
     2297 * \param piPred pointer to prediction signal arrays
     2298 * \param piResi pointer to residual signal arrays
     2299 * \param piReco pointer to reconstructed sample arrays
     2300 * \param uiStride stride of the original/prediction/residual sample arrays
     2301 * \param uiWidth block width
     2302 * \param uiHeight block height
     2303 * \param ttText texture component type
     2304 * \returns Void
     2305 */
     2306Void TEncSearch::xEncPCM (TComDataCU* pcCU, UInt uiAbsPartIdx, Pel* piOrg, Pel* piPCM, Pel* piPred, Pel* piResi, Pel* piReco, UInt uiStride, UInt uiWidth, UInt uiHeight, TextType eText )
    24142307{
    2415   Bool bDirAvailable;
    2416 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2417   if( uiMode > MAX_MODE_ID_INTRA_DIR )
    2418   {
    2419     return predIntraLumaDMMAvailable( uiMode, bAboveAvail, bLeftAvail, uiWidth, uiHeight );
     2308  UInt uiX, uiY;
     2309  UInt uiReconStride;
     2310  Pel* pOrg  = piOrg;
     2311  Pel* pPCM  = piPCM;
     2312  Pel* pPred = piPred;
     2313  Pel* pResi = piResi;
     2314  Pel* pReco = piReco;
     2315  Pel* pRecoPic;
     2316  UInt uiInternalBitDepth = g_uiBitDepth + g_uiBitIncrement;
     2317  UInt uiPCMBitDepth;
     2318
     2319  if( eText == TEXT_LUMA)
     2320  {
     2321    uiReconStride = pcCU->getPic()->getPicYuvRec()->getStride();
     2322    pRecoPic      = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
     2323    uiPCMBitDepth = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
    24202324  }
    24212325  else
    2422 #endif
    2423   {
    2424     bDirAvailable = true;
    2425     UInt uiNewMode = g_aucAngIntraModeOrder[uiMode];
    2426     if ( uiNewMode > 0 && ( ( (!bAboveAvail) && uiNewMode < 18 ) || ( (!bLeftAvail) && uiNewMode > 17 ) ) )
    2427     {
    2428       bDirAvailable = false;
    2429     }
    2430   }
    2431 
    2432   return bDirAvailable;
     2326  {
     2327    uiReconStride = pcCU->getPic()->getPicYuvRec()->getCStride();
     2328
     2329    if( eText == TEXT_CHROMA_U )
     2330    {
     2331      pRecoPic = pcCU->getPic()->getPicYuvRec()->getCbAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
     2332    }
     2333    else
     2334    {
     2335      pRecoPic = pcCU->getPic()->getPicYuvRec()->getCrAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
     2336    }
     2337    uiPCMBitDepth = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
     2338  }
     2339
     2340  // Reset pred and residual
     2341  for( uiY = 0; uiY < uiHeight; uiY++ )
     2342  {
     2343    for( uiX = 0; uiX < uiWidth; uiX++ )
     2344    {
     2345      pPred[uiX] = 0;
     2346      pResi[uiX] = 0;
     2347    }
     2348    pPred += uiStride;
     2349    pResi += uiStride;
     2350  }
     2351
     2352  // Encode
     2353  for( uiY = 0; uiY < uiHeight; uiY++ )
     2354  {
     2355    for( uiX = 0; uiX < uiWidth; uiX++ )
     2356    {
     2357      pPCM[uiX] = (pOrg[uiX]>>(uiInternalBitDepth - uiPCMBitDepth));
     2358    }
     2359    pPCM += uiWidth;
     2360    pOrg += uiStride;
     2361  }
     2362
     2363  pPCM  = piPCM;
     2364
     2365  // Reconstruction
     2366  for( uiY = 0; uiY < uiHeight; uiY++ )
     2367  {
     2368    for( uiX = 0; uiX < uiWidth; uiX++ )
     2369    {
     2370      pReco   [uiX] = (pPCM[uiX]<<(uiInternalBitDepth - uiPCMBitDepth));
     2371      pRecoPic[uiX] = pReco[uiX];
     2372    }
     2373    pPCM += uiWidth;
     2374    pReco += uiStride;
     2375    pRecoPic += uiReconStride;
     2376  }
    24332377}
    2434 #endif
    2435 
    2436 #if HHI_DMM_WEDGE_INTRA
    2437 Void TEncSearch::findWedgeFullMinDist( TComDataCU*  pcCU,
    2438                                       UInt         uiAbsPtIdx,
    2439                                       Pel*         piOrig,
    2440                                       Pel*         piPredic,
    2441                                       UInt         uiStride,
    2442                                       UInt         uiWidth,
    2443                                       UInt         uiHeight,
    2444                                       UInt&        ruiTabIdx,
    2445                                       Int&         riDeltaDC1,
    2446                                       Int&         riDeltaDC2,
    2447                                       Bool         bAbove,
    2448                                       Bool         bLeft,
    2449                                       WedgeDist    eWedgeDist
    2450                                       )
     2378
     2379/**  Function for PCM mode estimation.
     2380 * \param pcCU
     2381 * \param pcOrgYuv
     2382 * \param rpcPredYuv
     2383 * \param rpcResiYuv
     2384 * \param rpcRecoYuv
     2385 * \returns Void
     2386 */
     2387Void TEncSearch::IPCMSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
    24512388{
    2452   assert( uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE );
    2453 
    2454   WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiWidth])];
    2455   xSearchWedgeFullMinDist( pcCU, uiAbsPtIdx, pacWedgeList, piOrig, uiStride, uiWidth, uiHeight, ruiTabIdx );
    2456 
    2457   TComWedgelet* pcBestWedgelet = &(pacWedgeList->at(ruiTabIdx));
    2458   xGetWedgeDeltaDCsMinDist( pcBestWedgelet, pcCU, uiAbsPtIdx, piOrig, piPredic, uiStride, uiWidth, uiHeight, riDeltaDC1, riDeltaDC2, bAbove, bLeft );
     2389  UInt   uiDepth        = pcCU->getDepth(0);
     2390  UInt   uiWidth        = pcCU->getWidth(0);
     2391  UInt   uiHeight       = pcCU->getHeight(0);
     2392  UInt   uiStride       = rpcPredYuv->getStride();
     2393  UInt   uiStrideC      = rpcPredYuv->getCStride();
     2394  UInt   uiWidthC       = uiWidth  >> 1;
     2395  UInt   uiHeightC      = uiHeight >> 1;
     2396  Dist   uiDistortion = 0;
     2397  UInt   uiBits;
     2398
     2399  Double dCost;
     2400
     2401  Pel*    pOrig;
     2402  Pel*    pResi;
     2403  Pel*    pReco;
     2404  Pel*    pPred;
     2405  Pel*    pPCM;
     2406
     2407  UInt uiAbsPartIdx = 0;
     2408
     2409  UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight();
     2410  UInt uiLumaOffset   = uiMinCoeffSize*uiAbsPartIdx;
     2411  UInt uiChromaOffset = uiLumaOffset>>2;
     2412
     2413  // Luminance
     2414  pOrig    = pcOrgYuv->getLumaAddr(0, uiWidth);
     2415  pResi    = rpcResiYuv->getLumaAddr(0, uiWidth);
     2416  pPred    = rpcPredYuv->getLumaAddr(0, uiWidth);
     2417  pReco    = rpcRecoYuv->getLumaAddr(0, uiWidth);
     2418  pPCM     = pcCU->getPCMSampleY() + uiLumaOffset;
     2419
     2420  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStride, uiWidth, uiHeight, TEXT_LUMA );
     2421
     2422  // Chroma U
     2423  pOrig    = pcOrgYuv->getCbAddr();
     2424  pResi    = rpcResiYuv->getCbAddr();
     2425  pPred    = rpcPredYuv->getCbAddr();
     2426  pReco    = rpcRecoYuv->getCbAddr();
     2427  pPCM     = pcCU->getPCMSampleCb() + uiChromaOffset;
     2428
     2429  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStrideC, uiWidthC, uiHeightC, TEXT_CHROMA_U );
     2430
     2431  // Chroma V
     2432  pOrig    = pcOrgYuv->getCrAddr();
     2433  pResi    = rpcResiYuv->getCrAddr();
     2434  pPred    = rpcPredYuv->getCrAddr();
     2435  pReco    = rpcRecoYuv->getCrAddr();
     2436  pPCM     = pcCU->getPCMSampleCr() + uiChromaOffset;
     2437
     2438  xEncPCM ( pcCU, 0, pOrig, pPCM, pPred, pResi, pReco, uiStrideC, uiWidthC, uiHeightC, TEXT_CHROMA_V );
     2439
     2440  m_pcEntropyCoder->resetBits();
     2441  xEncIntraHeader ( pcCU, uiDepth, uiAbsPartIdx, true, false);
     2442  uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
     2443
     2444#if HHI_VSO
     2445  if( m_pcRdCost->getUseLambdaScaleVSO() )
     2446  {
     2447    dCost =  m_pcRdCost->calcRdCostVSO( uiBits, uiDistortion );
     2448  }
     2449  else
     2450#endif
     2451  {
     2452  dCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
     2453  }
     2454
     2455  if(m_bUseSBACRD)
     2456  {
     2457    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
     2458  }
     2459
     2460  pcCU->getTotalBits()       = uiBits;
     2461  pcCU->getTotalCost()       = dCost;
     2462  pcCU->getTotalDistortion() = uiDistortion;
     2463
     2464  pcCU->copyToPic(uiDepth, 0, 0);
    24592465}
    2460 
    2461 Void TEncSearch::xSearchWedgeFullMinDist( TComDataCU* pcCU, UInt uiAbsPtIdx, WedgeList* pacWedgeList, Pel* piRef, UInt uiRefStride, UInt uiWidth, UInt uiHeight, UInt& ruiTabIdx )
    2462 {
    2463   ruiTabIdx = 0;
    2464 
    2465   // local pred buffer
    2466   TComYuv cPredYuv;
    2467   cPredYuv.create( uiWidth, uiHeight );
    2468   cPredYuv.clear();
    2469 
    2470   UInt uiPredStride = cPredYuv.getStride();
    2471   Pel* piPred       = cPredYuv.getLumaAddr();
    2472 
    2473   Int  iDC1 = 0;
    2474   Int  iDC2 = 0;
    2475   // regular wedge search
    2476   Dist uiBestDist   = RDO_DIST_MAX;
    2477   UInt uiBestTabIdx = 0;
    2478 
    2479   for( UInt uiIdx = 0; uiIdx < pacWedgeList->size(); uiIdx++ )
    2480   {
    2481     calcWedgeDCs       ( &(pacWedgeList->at(uiIdx)), piRef,  uiRefStride,  iDC1, iDC2 );
    2482     assignWedgeDCs2Pred( &(pacWedgeList->at(uiIdx)), piPred, uiPredStride, iDC1, iDC2 );
    2483 
    2484     Dist uiActDist = RDO_DIST_MAX;
    2485 #if HHI_VSO
    2486     if( m_pcRdCost->getUseVSO() )
    2487     {
    2488       uiActDist = m_pcRdCost->getDistVS( pcCU, 0, piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, false, 0 );
    2489     }
    2490     else
    2491     {
    2492       uiActDist = m_pcRdCost->getDistPart( piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, DF_SAD );
    2493     }
    2494 #else
    2495     uiActDist = m_pcRdCost->getDistPart( piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, DF_SAD );
    2496 #endif
    2497 
    2498     if( uiActDist < uiBestDist || uiBestDist == RDO_DIST_MAX )
    2499     {
    2500       uiBestDist   = uiActDist;
    2501       uiBestTabIdx = uiIdx;
    2502     }
    2503   }
    2504   ruiTabIdx = uiBestTabIdx;
    2505 
    2506   cPredYuv.destroy();
    2507   return;
    2508 }
    2509 
    2510 Void TEncSearch::findWedgePredDirMinDist( TComDataCU*  pcCU,
    2511                                          UInt         uiAbsPtIdx,
    2512                                          Pel*         piOrig,
    2513                                          Pel*         piPredic,
    2514                                          UInt         uiStride,
    2515                                          UInt         uiWidth,
    2516                                          UInt         uiHeight,
    2517                                          UInt&        ruiTabIdx,
    2518                                          Int&         riWedgeDeltaEnd,
    2519                                          Int&         riDeltaDC1,
    2520                                          Int&         riDeltaDC2,
    2521                                          Bool         bAbove,
    2522                                          Bool         bLeft,
    2523                                          WedgeDist    eWedgeDist )
    2524 {
    2525   assert( uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE );
    2526   WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiWidth])];
    2527 
    2528   ruiTabIdx       = 0;
    2529   riWedgeDeltaEnd = 0;
    2530 
    2531   xSearchWedgePredDirMinDist( pcCU, uiAbsPtIdx, pacWedgeList, piOrig, uiStride, uiWidth, uiHeight, ruiTabIdx, riWedgeDeltaEnd );
    2532 
    2533   TComWedgelet* pcBestWedgelet = &(pacWedgeList->at(ruiTabIdx));
    2534   xGetWedgeDeltaDCsMinDist( pcBestWedgelet, pcCU, uiAbsPtIdx, piOrig, piPredic, uiStride, uiWidth, uiHeight, riDeltaDC1, riDeltaDC2, bAbove, bLeft );
    2535 }
    2536 
    2537 Void TEncSearch::xSearchWedgePredDirMinDist( TComDataCU* pcCU, UInt uiAbsPtIdx, WedgeList* pacWedgeList, Pel* piRef, UInt uiRefStride, UInt uiWidth, UInt uiHeight, UInt& ruiTabIdx, Int& riWedgeDeltaEnd )
    2538 {
    2539   ruiTabIdx       = 0;
    2540   riWedgeDeltaEnd = 0;
    2541 
    2542   // local pred buffer
    2543   TComYuv cPredYuv;
    2544   cPredYuv.create( uiWidth, uiHeight );
    2545   cPredYuv.clear();
    2546 
    2547   UInt uiPredStride = cPredYuv.getStride();
    2548   Pel* piPred       = cPredYuv.getLumaAddr();
    2549 
    2550   Int  iDC1 = 0;
    2551   Int  iDC2 = 0;
    2552 
    2553   // regular wedge search
    2554   Dist uiBestDist    = RDO_DIST_MAX;
    2555   UInt uiBestTabIdx  = 0;
    2556   Int  iBestDeltaEnd = 0;
    2557 
    2558   UInt uiIdx = 0;
    2559   for( Int iTestDeltaEnd = -DMM_WEDGE_PREDDIR_DELTAEND_MAX; iTestDeltaEnd <= DMM_WEDGE_PREDDIR_DELTAEND_MAX; iTestDeltaEnd++ )
    2560   {
    2561     uiIdx = getBestContinueWedge( pcCU, uiAbsPtIdx, uiWidth, uiHeight, iTestDeltaEnd );
    2562     calcWedgeDCs       ( &(pacWedgeList->at(uiIdx)), piRef,  uiRefStride,  iDC1, iDC2 );
    2563     assignWedgeDCs2Pred( &(pacWedgeList->at(uiIdx)), piPred, uiPredStride, iDC1, iDC2 );
    2564 
    2565     Dist uiActDist = RDO_DIST_MAX;
    2566 #if HHI_VSO
    2567     if( m_pcRdCost->getUseVSO() )
    2568     {
    2569       uiActDist = m_pcRdCost->getDistVS( pcCU, 0, piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, false, 0 );
    2570     }
    2571     else
    2572     {
    2573       uiActDist = m_pcRdCost->getDistPart( piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, DF_SAD );
    2574     }
    2575 #else
    2576     uiActDist = m_pcRdCost->getDistPart( piPred, uiPredStride, piRef, uiRefStride, uiWidth, uiHeight, DF_SAD );
    2577 #endif
    2578 
    2579     if( uiActDist < uiBestDist || uiBestDist == RDO_DIST_MAX )
    2580     {
    2581       uiBestDist    = uiActDist;
    2582       uiBestTabIdx  = uiIdx;
    2583       iBestDeltaEnd = iTestDeltaEnd;
    2584     }
    2585     else if( uiIdx == uiBestTabIdx && abs(iTestDeltaEnd) < abs(iBestDeltaEnd) )
    2586     {
    2587       iBestDeltaEnd = iTestDeltaEnd;
    2588     }
    2589   }
    2590 
    2591   ruiTabIdx       = uiBestTabIdx;
    2592   riWedgeDeltaEnd = iBestDeltaEnd;
    2593 
    2594   cPredYuv.destroy();
    2595   return;
    2596 }
    2597 #endif
    2598 
    2599 #if HHI_DMM_PRED_TEX
    2600 Void TEncSearch::findWedgeTexMinDist( TComDataCU*  pcCU,
    2601                                       UInt         uiAbsPtIdx,
    2602                                       Pel*         piOrig,
    2603                                       Pel*         piPredic,
    2604                                       UInt         uiStride,
    2605                                       UInt         uiWidth,
    2606                                       UInt         uiHeight,
    2607                                       UInt&        ruiTabIdx,
    2608                                       Int&         riDeltaDC1,
    2609                                       Int&         riDeltaDC2,
    2610                                       Bool         bAbove,
    2611                                       Bool         bLeft,
    2612                                       WedgeDist    eWedgeDist,
    2613                                       Pel*         piTextureRef
    2614                                     )
    2615   {
    2616   assert( uiWidth >= DMM_WEDGEMODEL_MIN_SIZE && uiWidth <= DMM_WEDGEMODEL_MAX_SIZE );
    2617   WedgeList* pacWedgeList = &g_aacWedgeLists[(g_aucConvertToBit[uiWidth])];
    2618 
    2619   ruiTabIdx = getBestWedgeFromText( pcCU, uiAbsPtIdx, uiWidth, uiHeight, eWedgeDist, piTextureRef );
    2620 
    2621   TComWedgelet* pcBestWedgelet = &(pacWedgeList->at(ruiTabIdx));
    2622   xGetWedgeDeltaDCsMinDist( pcBestWedgelet, pcCU, uiAbsPtIdx, piOrig, piPredic, uiStride, uiWidth, uiHeight, riDeltaDC1, riDeltaDC2, bAbove, bLeft );
    2623 }
    2624 
    2625 Void TEncSearch::findContourPredTex( TComDataCU*  pcCU,
    2626                                      UInt         uiAbsPtIdx,
    2627                                      Pel*         piOrig,
    2628                                      Pel*         piPredic,
    2629                                      UInt         uiStride,
    2630                                      UInt         uiWidth,
    2631                                      UInt         uiHeight,
    2632                                      Int&         riDeltaDC1,
    2633                                      Int&         riDeltaDC2,
    2634                                      Bool         bAbove,
    2635                                      Bool         bLeft,
    2636                                      Pel*         piTextureRef )
    2637 {
    2638   // get contour pattern
    2639   TComWedgelet* pcContourWedge = new TComWedgelet( uiWidth, uiHeight );
    2640   getBestContourFromText( pcCU, uiAbsPtIdx, uiWidth, uiHeight, pcContourWedge, piTextureRef );
    2641 
    2642   xGetWedgeDeltaDCsMinDist( pcContourWedge, pcCU, uiAbsPtIdx, piOrig, piPredic, uiStride, uiWidth, uiHeight, riDeltaDC1, riDeltaDC2, bAbove, bLeft );
    2643 
    2644   pcContourWedge->destroy();
    2645   delete pcContourWedge;
    2646 }
    2647 #endif
    2648 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
    2649 Void TEncSearch::xGetWedgeDeltaDCsMinDist( TComWedgelet* pcWedgelet,
    2650                                            TComDataCU*   pcCU,
    2651                                            UInt          uiAbsPtIdx,
    2652                                            Pel*          piOrig,
    2653                                            Pel*          piPredic,
    2654                                            UInt          uiStride,
    2655                                            UInt          uiWidth,
    2656                                            UInt          uiHeight,
    2657                                            Int&          riDeltaDC1,
    2658                                            Int&          riDeltaDC2,
    2659                                            Bool          bAbove,
    2660                                            Bool          bLeft )
    2661 {
    2662   Int iDC1 = 0;
    2663   Int iDC2 = 0;
    2664   calcWedgeDCs       ( pcWedgelet, piOrig,   uiStride, iDC1, iDC2 );
    2665   assignWedgeDCs2Pred( pcWedgelet, piPredic, uiStride, iDC1, iDC2 );
    2666 
    2667   Int iPredDC1 = 0;
    2668   Int iPredDC2 = 0;
    2669   Int* piMask = pcCU->getPattern()->getAdiOrgBuf( uiWidth, uiHeight, m_piYuvExt );
    2670   Int iMaskStride = ( uiWidth<<1 ) + 1;
    2671   piMask += iMaskStride+1;
    2672   getWedgePredDCs( pcWedgelet, piMask, iMaskStride, iPredDC1, iPredDC2, bAbove, bLeft );
    2673 
    2674   riDeltaDC1 = iDC1 - iPredDC1;
    2675   riDeltaDC2 = iDC2 - iPredDC2;
    2676 
    2677 #if HHI_VSO
    2678   if( m_pcRdCost->getUseVSO() )
    2679   {
    2680     Int iFullDeltaDC1 = riDeltaDC1;
    2681     Int iFullDeltaDC2 = riDeltaDC2;
    2682 
    2683     xDeltaDCQuantScaleDown( pcCU, iFullDeltaDC1 );
    2684     xDeltaDCQuantScaleDown( pcCU, iFullDeltaDC2 );
    2685 
    2686     Dist uiBestDist     = RDO_DIST_MAX;
    2687     UInt  uiBestQStepDC1 = 0;
    2688     UInt  uiBestQStepDC2 = 0;
    2689 
    2690     UInt uiDeltaDC1Max = abs(iFullDeltaDC1);
    2691     UInt uiDeltaDC2Max = abs(iFullDeltaDC2);
    2692 
    2693     //VSO Level delta DC check range extension
    2694     uiDeltaDC1Max += (uiDeltaDC1Max>>1);
    2695     uiDeltaDC2Max += (uiDeltaDC2Max>>1);
    2696 
    2697     for( UInt uiQStepDC1 = 1; uiQStepDC1 <= uiDeltaDC1Max; uiQStepDC1++  )
    2698     {
    2699       Int iLevelDeltaDC1 = (Int)(uiQStepDC1) * (Int)(( iFullDeltaDC1 < 0 ) ? -1 : 1);
    2700       xDeltaDCQuantScaleUp( pcCU, iLevelDeltaDC1 );
    2701 
    2702       Int iTestDC1 = Clip( iPredDC1 + iLevelDeltaDC1 );
    2703       for( UInt uiQStepDC2 = 1; uiQStepDC2 <= uiDeltaDC2Max; uiQStepDC2++  )
    2704       {
    2705         Int iLevelDeltaDC2 = (Int)(uiQStepDC2) * (Int)(( iFullDeltaDC2 < 0 ) ? -1 : 1);
    2706         xDeltaDCQuantScaleUp( pcCU, iLevelDeltaDC2 );
    2707 
    2708         Int iTestDC2 = Clip( iPredDC2 + iLevelDeltaDC2 );
    2709 
    2710         assignWedgeDCs2Pred( pcWedgelet, piPredic, uiStride, iTestDC1, iTestDC2 );
    2711 
    2712         Dist uiActDist = m_pcRdCost->getDistVS( pcCU, 0, piPredic, uiStride,  piOrig, uiStride, uiWidth, uiHeight, false, 0 );
    2713         if( uiActDist < uiBestDist || uiBestDist == RDO_DIST_MAX )
    2714         {
    2715           uiBestDist     = uiActDist;
    2716           uiBestQStepDC1 = uiQStepDC1;
    2717           uiBestQStepDC2 = uiQStepDC2;
    2718         }
    2719       }
    2720     }
    2721 
    2722     iFullDeltaDC1 = (Int)(uiBestQStepDC1) * (Int)(( iFullDeltaDC1 < 0 ) ? -1 : 1);
    2723     iFullDeltaDC2 = (Int)(uiBestQStepDC2) * (Int)(( iFullDeltaDC2 < 0 ) ? -1 : 1);
    2724     xDeltaDCQuantScaleUp( pcCU, iFullDeltaDC1 );
    2725     xDeltaDCQuantScaleUp( pcCU, iFullDeltaDC2 );
    2726     riDeltaDC1 = iFullDeltaDC1;
    2727     riDeltaDC2 = iFullDeltaDC2;
    2728   }
    2729 #endif
    2730 
    2731   xDeltaDCQuantScaleDown( pcCU, riDeltaDC1 );
    2732   xDeltaDCQuantScaleDown( pcCU, riDeltaDC2 );
    2733 }
    2734 
    2735 #if HHI_DMM_PRED_TEX && FLEX_CODING_ORDER
    2736 Bool TEncSearch::predIntraLumaDMMAvailable( UInt uiMode, UInt uiWidth, UInt uiHeight, Bool bDMMAvailable34 )
    2737 #else
    2738 Bool TEncSearch::predIntraLumaDMMAvailable( UInt uiMode, UInt uiWidth, UInt uiHeight )
    2739 #endif
    2740 {
    2741   if( uiMode <= MAX_MODE_ID_INTRA_DIR ) return true;
    2742 
    2743   Bool bDMMAvailable = m_pcEncCfg->getUseDMM();
    2744 
    2745 #if HHI_DMM_WEDGE_INTRA
    2746   if( uiMode == DMM_WEDGE_FULL_IDX        ||
    2747       uiMode == DMM_WEDGE_FULL_D_IDX      ||
    2748       uiMode == DMM_WEDGE_PREDDIR_IDX     ||
    2749       uiMode == DMM_WEDGE_PREDDIR_D_IDX )
    2750   {
    2751     if( (uiWidth != uiHeight) || (uiWidth < DMM_WEDGEMODEL_MIN_SIZE) || (uiWidth > DMM_WEDGEMODEL_MAX_SIZE) || ( ( uiMode == DMM_WEDGE_PREDDIR_IDX || uiMode == DMM_WEDGE_PREDDIR_D_IDX ) && uiWidth == 4 ) )
    2752     {
    2753       bDMMAvailable = false;
    2754     }
    2755   }
    2756 #endif
    2757 #if HHI_DMM_PRED_TEX
    2758   if( uiMode == DMM_WEDGE_PREDTEX_IDX     ||
    2759       uiMode == DMM_WEDGE_PREDTEX_D_IDX   ||
    2760       uiMode == DMM_CONTOUR_PREDTEX_IDX   ||
    2761       uiMode == DMM_CONTOUR_PREDTEX_D_IDX )
    2762   {
    2763     if( (uiWidth != uiHeight) || (uiWidth < DMM_WEDGEMODEL_MIN_SIZE) || (uiWidth > DMM_WEDGEMODEL_MAX_SIZE) || ( ( uiMode == DMM_CONTOUR_PREDTEX_IDX || uiMode == DMM_CONTOUR_PREDTEX_D_IDX ) && uiWidth == 4 ) )
    2764     {
    2765       bDMMAvailable = false;
    2766     }
    2767 #if FLEX_CODING_ORDER
    2768     if ( !bDMMAvailable34 )
    2769     {
    2770       bDMMAvailable = false;
    2771     }
    2772 #endif
    2773   }
    2774 
    2775 #endif
    2776 
    2777   return bDMMAvailable;
    2778 }
    2779 
    2780 Void TEncSearch::xDeltaDCQuantScaleDown( TComDataCU*  pcCU, Int& riDeltaDC )
    2781 {
    2782   Int  iSign  = riDeltaDC < 0 ? -1 : 1;
    2783   UInt uiAbs  = abs( riDeltaDC );
    2784 
    2785   Int riB = 0;
    2786   Int iQp = pcCU->getQP(0);
    2787   Int iMax = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
    2788   Double dStepSize = Clip3( 1, iMax, pow( 2.0, iQp/10.0 + g_dDeltaDCsQuantOffset ) );
    2789 
    2790   riB = roftoi( uiAbs / dStepSize );
    2791 
    2792   riDeltaDC = riB * iSign;
    2793   return;
    2794 }
    2795 #endif
    2796 
    27972466
    27982467Void TEncSearch::xGetInterPredictionError( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPartIdx, UInt& ruiErr, Bool bHadamard )
    27992468{
    2800   TComYuv cYuvPred;
    2801   cYuvPred.create( pcYuvOrg->getWidth(), pcYuvOrg->getHeight() );
    2802 
    2803 #ifdef WEIGHT_PRED
    2804   UInt uiAbsPartIdx = 0;
    2805   Int iWidth = 0;
    2806   Int iHeight = 0;
    2807   Int iRefIdx[2];
    2808   pcCU->getPartIndexAndSize( iPartIdx, uiAbsPartIdx, iWidth, iHeight );
    2809 
    2810   iRefIdx[0] = pcCU->getCUMvField( REF_PIC_LIST_0 )->getRefIdx( uiAbsPartIdx );
    2811   iRefIdx[1] = pcCU->getCUMvField( REF_PIC_LIST_1 )->getRefIdx( uiAbsPartIdx );
    2812   if ( iRefIdx[0]>=0 && iRefIdx[1]<1 )
    2813     setWpScalingDistParam( pcCU, iRefIdx[0], iRefIdx[1], REF_PIC_LIST_0);
    2814   else
    2815     setWpScalingDistParam( pcCU, iRefIdx[0], iRefIdx[1], REF_PIC_LIST_1);
    2816 
    2817   motionCompensation( pcCU, &cYuvPred, REF_PIC_LIST_X, iPartIdx );
    2818 #else
    2819   motionCompensation( pcCU, &cYuvPred, REF_PIC_LIST_X, iPartIdx );
     2469  motionCompensation( pcCU, &m_tmpYuvPred, REF_PIC_LIST_X, iPartIdx );
    28202470
    28212471  UInt uiAbsPartIdx = 0;
     
    28232473  Int iHeight = 0;
    28242474  pcCU->getPartIndexAndSize( iPartIdx, uiAbsPartIdx, iWidth, iHeight );
    2825 #endif
    28262475
    28272476  DistParam cDistParam;
    2828 #ifdef WEIGHT_PRED
    2829   cDistParam.applyWeight = false;
    2830 #endif
    2831   m_pcRdCost->setDistParam( cDistParam,
    2832                             pcYuvOrg->getLumaAddr( uiAbsPartIdx ), pcYuvOrg->getStride(),
    2833                             cYuvPred .getLumaAddr( uiAbsPartIdx ), cYuvPred .getStride(),
     2477
     2478  cDistParam.bApplyWeight = false;
     2479
     2480  m_pcRdCost->setDistParam( cDistParam,
     2481                            pcYuvOrg->getLumaAddr( uiAbsPartIdx ), pcYuvOrg->getStride(),
     2482                            m_tmpYuvPred .getLumaAddr( uiAbsPartIdx ), m_tmpYuvPred .getStride(),
     2483#if NS_HAD
     2484                            iWidth, iHeight, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
     2485#else
    28342486                            iWidth, iHeight, m_pcEncCfg->getUseHADME() );
     2487#endif
    28352488  ruiErr = cDistParam.DistFunc( &cDistParam );
    2836 
    2837   cYuvPred.destroy();
    28382489}
    28392490
     
    28482499 * \param ruiBits
    28492500 * \param puhNeighCands
    2850  * \param bValid
     2501 * \param bValid 
    28512502 * \returns Void
    28522503 */
    2853 Void TEncSearch::xMergeEstimation( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPUIdx, UInt& uiInterDir, TComMvField* pacMvField, UInt& uiMergeIndex, UInt& ruiCost, UInt& ruiBits, UChar* puhNeighCands,Bool& bValid )
     2504#if CU_BASED_MRG_CAND_LIST
     2505Void TEncSearch::xMergeEstimation( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPUIdx, UInt& uiInterDir, TComMvField* pacMvField, UInt& uiMergeIndex, UInt& ruiCost, TComMvField* cMvFieldNeighbours, UChar* uhInterDirNeighbours, Int& numValidMergeCand )
     2506#else
     2507Void TEncSearch::xMergeEstimation( TComDataCU* pcCU, TComYuv* pcYuvOrg, Int iPUIdx, UInt& uiInterDir, TComMvField* pacMvField, UInt& uiMergeIndex, UInt& ruiCost )
     2508#endif
    28542509{
     2510#if !CU_BASED_MRG_CAND_LIST
     2511#if HHI_INTER_VIEW_MOTION_PRED
     2512  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
     2513  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
     2514  Int numValidMergeCand = 0;
     2515  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS_MEM; ++ui )
     2516#else
    28552517  TComMvField  cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
    28562518  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
    2857   UInt uiNeighbourCandIdx[MRG_MAX_NUM_CANDS]; //MVs with same idx => same cand
    2858 
     2519  Int numValidMergeCand = 0;
    28592520  for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ++ui )
     2521#endif
    28602522  {
    28612523    uhInterDirNeighbours[ui] = 0;
    2862     uiNeighbourCandIdx[ui] = 0;
    2863   }
     2524  }
     2525#endif
    28642526
    28652527  UInt uiAbsPartIdx = 0;
    28662528  Int iWidth = 0;
    2867   Int iHeight = 0;
     2529  Int iHeight = 0; 
    28682530
    28692531  pcCU->getPartIndexAndSize( iPUIdx, uiAbsPartIdx, iWidth, iHeight );
    28702532  UInt uiDepth = pcCU->getDepth( uiAbsPartIdx );
    2871   pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, uiDepth, cMvFieldNeighbours,uhInterDirNeighbours, uiNeighbourCandIdx );
    2872 
    2873   UInt uiNumCand = 0;
    2874   for( UInt uiMergeCand = 0; uiMergeCand < MRG_MAX_NUM_CANDS; ++uiMergeCand )
    2875   {
    2876     if( uiNeighbourCandIdx[uiMergeCand] == ( uiMergeCand + 1 ) )
    2877     {
    2878       uiNumCand++;
    2879     }
    2880   }
    2881 
    2882   UInt uiBestSAD = MAX_UINT;
    2883   UInt uiBestBitCost = MAX_UINT;
    2884   UInt uiBestBits = MAX_UINT;
     2533#if CU_BASED_MRG_CAND_LIST
     2534  PartSize partSize = pcCU->getPartitionSize( 0 );
     2535  if ( pcCU->getSlice()->getPPS()->getLog2ParallelMergeLevelMinus2() && partSize != SIZE_2Nx2N && pcCU->getWidth( 0 ) <= 8 )
     2536  {
     2537    pcCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth );
     2538    if ( iPUIdx == 0 )
     2539    {
     2540      pcCU->getInterMergeCandidates( 0, 0, uiDepth, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
     2541    }
     2542    pcCU->setPartSizeSubParts( partSize, 0, uiDepth );
     2543  }
     2544  else
     2545  {
     2546    pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, uiDepth, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
     2547  }
     2548#else
     2549  pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, uiDepth, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
     2550#endif
     2551
     2552#if HHI_INTER_VIEW_MOTION_PRED
     2553  const int maxNumMergeCand = MRG_MAX_NUM_CANDS_SIGNALED + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
     2554#endif
    28852555
    28862556  ruiCost = MAX_UINT;
    2887   ruiBits = MAX_UINT;
    2888 
    2889   bValid = false;
    2890 
    2891   for( UInt uiMergeCand = 0; uiMergeCand < MRG_MAX_NUM_CANDS; ++uiMergeCand )
    2892   {
    2893     if( uiNeighbourCandIdx[uiMergeCand] == ( uiMergeCand + 1 ) )
    2894     {
    2895       bValid = true;
     2557  for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
     2558  {
     2559    {
    28962560      UInt uiCostCand = MAX_UINT;
    28972561      UInt uiBitsCand = 0;
    2898 
     2562     
    28992563      PartSize ePartSize = pcCU->getPartitionSize( 0 );
    29002564
    2901       pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand].getMv(), cMvFieldNeighbours[0 + 2*uiMergeCand].getRefIdx(), ePartSize, uiAbsPartIdx, iPUIdx, 0 );
    2902       pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand].getMv(), cMvFieldNeighbours[1 + 2*uiMergeCand].getRefIdx(), ePartSize, uiAbsPartIdx, iPUIdx, 0 );
     2565      pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
     2566      pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
    29032567
    29042568      xGetInterPredictionError( pcCU, pcYuvOrg, iPUIdx, uiCostCand, m_pcEncCfg->getUseHADME() );
    2905 
    2906       if( uiNumCand == 1 )
    2907       {
    2908         uiBitsCand = 1;
    2909       }
    2910       else
    2911       {
    2912         if( uiMergeCand == 0 || uiNumCand == 2 )
    2913         {
    2914           uiBitsCand = 2;
    2915         }
    2916         else if( uiMergeCand == 1 || uiNumCand == 3 )
    2917         {
    2918           uiBitsCand = 3;
    2919         }
    2920         else if( uiMergeCand == 2 || uiNumCand == 4 )
    2921         {
    2922           uiBitsCand = 4;
    2923         }
    2924         else
    2925         {
    2926           uiBitsCand = 5;
    2927         }
    2928       }
    2929 
     2569      uiBitsCand = uiMergeCand + 1;
     2570#if HHI_INTER_VIEW_MOTION_PRED
     2571      if (uiMergeCand == maxNumMergeCand - 1 )
     2572#else
     2573      if (uiMergeCand == MRG_MAX_NUM_CANDS_SIGNALED -1)
     2574#endif
     2575      {
     2576         uiBitsCand--;
     2577      }
     2578      uiCostCand = uiCostCand + m_pcRdCost->getCost( uiBitsCand );
    29302579      if ( uiCostCand < ruiCost )
    29312580      {
    29322581        ruiCost = uiCostCand;
    2933         ruiBits = uiBitsCand;
    29342582        pacMvField[0] = cMvFieldNeighbours[0 + 2*uiMergeCand];
    29352583        pacMvField[1] = cMvFieldNeighbours[1 + 2*uiMergeCand];
    29362584        uiInterDir = uhInterDirNeighbours[uiMergeCand];
    29372585        uiMergeIndex = uiMergeCand;
    2938         for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
    2939         {
    2940           UChar uhNeighCand = uiNeighbourCandIdx[ui];
    2941           puhNeighCands[ui] = uhNeighCand;
    2942         }
    2943 
    2944         uiBestSAD = uiCostCand;
    2945         uiBestBitCost = m_pcRdCost->getCost( uiBitsCand );
    2946         uiBestBits = uiBitsCand;
    29472586      }
    29482587    }
     
    29592598 * \returns Void
    29602599 */
     2600#if AMP_MRG
     2601Void TEncSearch::predInterSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv, Bool bUseRes, Bool bUseMRG )
     2602#else
    29612603Void TEncSearch::predInterSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv, Bool bUseRes )
     2604#endif
    29622605{
    29632606  m_acYuvPred[0].clear();
     
    29652608  m_cYuvPredTemp.clear();
    29662609  rpcPredYuv->clear();
    2967 
     2610 
    29682611  if ( !bUseRes )
    29692612  {
    29702613    rpcResiYuv->clear();
    29712614  }
    2972 
     2615 
    29732616  rpcRecoYuv->clear();
    2974 
     2617 
    29752618  TComMv        cMvSrchRngLT;
    29762619  TComMv        cMvSrchRngRB;
    2977 
     2620 
    29782621  TComMv        cMvZero;
    29792622  TComMv        TempMv; //kolya
    2980 
     2623 
    29812624  TComMv        cMv[2];
    29822625  TComMv        cMvBi[2];
    29832626  TComMv        cMvTemp[2][33];
    2984 
     2627 
    29852628  Int           iNumPart    = pcCU->getNumPartInter();
    29862629  Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
    2987 
     2630 
    29882631  TComMv        cMvPred[2][33];
    2989 
     2632 
    29902633  TComMv        cMvPredBi[2][33];
    29912634  Int           aaiMvpIdxBi[2][33];
    2992 
     2635 
    29932636  Int           aaiMvpIdx[2][33];
    29942637  Int           aaiMvpNum[2][33];
    2995 
     2638 
    29962639  AMVPInfo aacAMVPInfo[2][33];
    2997 
    2998 #ifdef WEIGHT_PRED
     2640 
    29992641  Int           iRefIdx[2]={0,0}; //If un-initialized, may cause SEGV in bi-directional prediction iterative stage.
    3000 #else
    3001   Int           iRefIdx[2];
    3002 #endif
    30032642  Int           iRefIdxBi[2];
    3004 
     2643 
    30052644  UInt          uiPartAddr;
    30062645  Int           iRoiWidth, iRoiHeight;
    3007 
     2646 
    30082647  UInt          uiMbBits[3] = {1, 1, 0};
    3009 
     2648 
    30102649  UInt          uiLastMode = 0;
    30112650  Int           iRefStart, iRefEnd;
    3012 
     2651 
    30132652  PartSize      ePartSize = pcCU->getPartitionSize( 0 );
     2653
     2654#if H0111_MVD_L1_ZERO
     2655  Int           bestBiPRefIdxL1 = 0;
     2656  Int           bestBiPMvpL1 = 0;
     2657  UInt          biPDistTemp = MAX_INT;
     2658#endif
    30142659
    30152660#if ZERO_MVD_EST
     
    30192664#endif
    30202665
     2666#if CU_BASED_MRG_CAND_LIST
     2667#if HHI_INTER_VIEW_MOTION_PRED
     2668  TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS_MEM << 1]; // double length for mv of both lists
     2669  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS_MEM];
     2670  Int numValidMergeCand = 0 ;
     2671#else
     2672  TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
     2673  UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
     2674  Int numValidMergeCand = 0 ;
     2675#endif
     2676#endif
     2677
     2678#if HHI_INTER_VIEW_MOTION_PRED
     2679  Int iNumAMVPCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
     2680#endif
     2681
    30212682  for ( Int iPartIdx = 0; iPartIdx < iNumPart; iPartIdx++ )
    30222683  {
     
    30242685    UInt          uiCostBi  =   MAX_UINT;
    30252686    UInt          uiCostTemp;
    3026 
     2687   
    30272688    UInt          uiBits[3];
    30282689    UInt          uiBitsTemp;
     
    30342695    UInt          auiZeroMvdBits[3];
    30352696#endif
    3036 
    3037 #if DCM_COMB_LIST
     2697#if H0111_MVD_L1_ZERO
     2698    UInt          bestBiPDist = MAX_INT;
     2699#endif
     2700
    30382701    UInt          uiCostTempL0[MAX_NUM_REF];
    30392702    for (Int iNumRef=0; iNumRef < MAX_NUM_REF; iNumRef++) uiCostTempL0[iNumRef] = MAX_UINT;
    3040 #endif
     2703    UInt          uiBitsTempL0[MAX_NUM_REF];
    30412704
    30422705    xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
    3043 
     2706   
    30442707    pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
    3045 
    3046 #if PART_MRG
     2708   
     2709#if AMP_MRG
    30472710    Bool bTestNormalMC = true;
    3048     if (pcCU->getWidth( 0 ) > 8 && iNumPart == 2 && iPartIdx == 0)
     2711   
     2712    if ( bUseMRG && pcCU->getWidth( 0 ) > 8 && iNumPart == 2 )
     2713    {
    30492714      bTestNormalMC = false;
     2715    }
     2716   
    30502717    if (bTestNormalMC)
    30512718    {
     
    30562723    {
    30572724      RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
    3058 
     2725     
    30592726      for ( Int iRefIdxTemp = 0; iRefIdxTemp < pcCU->getSlice()->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
    30602727      {
    3061 #ifdef WEIGHT_PRED
    3062         if ( eRefPicList == REF_PIC_LIST_0 ) setWpScalingDistParam( pcCU, iRefIdxTemp, -1 , eRefPicList);
    3063         if ( eRefPicList == REF_PIC_LIST_1 ) setWpScalingDistParam( pcCU, -1, iRefIdxTemp , eRefPicList);
    3064 #endif
    30652728        uiBitsTemp = uiMbBits[iRefList];
    30662729        if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
     
    30692732          if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
    30702733        }
     2734#if H0111_MVD_L1_ZERO
     2735#if ZERO_MVD_EST
     2736        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp, &uiZeroMvdDistTemp);
     2737#else
     2738        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
     2739#endif
     2740#else
    30712741#if ZERO_MVD_EST
    30722742        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &uiZeroMvdDistTemp);
     
    30742744        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp]);
    30752745#endif
     2746#endif
    30762747        aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
    30772748        aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
    3078 
    3079         uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][aaiMvpNum[iRefList][iRefIdxTemp]];
     2749       
     2750#if H0111_MVD_L1_ZERO
     2751        if(pcCU->getSlice()->getMvdL1ZeroFlag() && iRefList==1 && biPDistTemp < bestBiPDist)
     2752        {
     2753          bestBiPDist = biPDistTemp;
     2754          bestBiPMvpL1 = aaiMvpIdx[iRefList][iRefIdxTemp];
     2755          bestBiPRefIdxL1 = iRefIdxTemp;
     2756        }
     2757#endif
     2758
     2759#if HHI_INTER_VIEW_MOTION_PRED
     2760        uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][iNumAMVPCands];
     2761#else
     2762        uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
     2763#endif
    30802764#if ZERO_MVD_EST
    3081 #if DCM_COMB_LIST
    30822765        if ((iRefList != 1 || !pcCU->getSlice()->getNoBackPredFlag()) &&
    30832766            (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) <= 0 || pcCU->getSlice()->getRefIdxOfLC(eRefPicList, iRefIdxTemp)>=0))
    3084 #endif
    30852767        {
    30862768          uiZeroMvdBitsTemp = uiBitsTemp;
     
    30972779            aiZeroMvdMvpIdx[iRefList] = aaiMvpIdx[iRefList][iRefIdxTemp];
    30982780            auiZeroMvdBits[iRefList] = uiZeroMvdBitsTemp;
    3099           }
    3100         }
    3101 #endif
    3102 
     2781          }         
     2782        }
     2783#endif
     2784       
    31032785#if GPB_SIMPLE_UNI
    3104 #if DCM_COMB_LIST
    3105         if ( pcCU->getSlice()->getSPS()->getUseLDC() || pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0)
    3106 #else
    3107         if ( pcCU->getSlice()->getSPS()->getUseLDC() )
    3108 #endif
    3109         {
    3110 #if DCM_COMB_LIST
    3111           if ( iRefList && ( (pcCU->getSlice()->getSPS()->getUseLDC() && (iRefIdxTemp != iRefIdx[0])) || pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag() && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)>=0 ) ) )
    3112 #else
    3113           if ( iRefList && ( iRefIdxTemp != iRefIdx[0] || pcCU->getSlice()->getNoBackPredFlag() ) )
    3114 #endif
     2786        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0)
     2787        {
     2788          if ( iRefList && ( pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag() && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)>=0 ) ) )
    31152789            {
    3116 #if DCM_COMB_LIST
    3117               if (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag())
     2790              if ( pcCU->getSlice()->getNoBackPredFlag() )
    31182791              {
    3119                 uiCostTemp = uiCostTempL0[pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)];
     2792                cMvTemp[1][iRefIdxTemp] = cMvTemp[0][iRefIdxTemp];
     2793                uiCostTemp = uiCostTempL0[iRefIdxTemp];
     2794                /*first subtract the bit-rate part of the cost of the other list*/
     2795                uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[iRefIdxTemp] );
    31202796              }
    31212797              else
    31222798              {
    3123                 uiCostTemp = MAX_UINT;
     2799                cMvTemp[1][iRefIdxTemp] = cMvTemp[0][pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)];
     2800                uiCostTemp = uiCostTempL0[pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)];
     2801                /*first subtract the bit-rate part of the cost of the other list*/
     2802                uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)] );
    31242803              }
    3125 #else
    3126               uiCostTemp = MAX_UINT;
    3127 #endif
    3128 #if DCM_COMB_LIST
    3129               if ( pcCU->getSlice()->getNoBackPredFlag() || pcCU->getSlice()->getSPS()->getUseLDC() )
    3130 #else
    3131               if ( pcCU->getSlice()->getNoBackPredFlag() )
    3132 #endif
    3133               {
    3134                 cMvTemp[1][iRefIdxTemp] = cMvTemp[0][iRefIdxTemp];
    3135               }
    3136 #if DCM_COMB_LIST
    3137               else
    3138               {
    3139                 cMvTemp[1][iRefIdxTemp] = cMvTemp[0][pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)];
    3140               }
    3141 #endif
     2804              /*correct the bit-rate part of the current ref*/
     2805              m_pcRdCost->setPredictor  ( cMvPred[iRefList][iRefIdxTemp] );
     2806              uiBitsTemp += m_pcRdCost->getBits( cMvTemp[1][iRefIdxTemp].getHor(), cMvTemp[1][iRefIdxTemp].getVer() );
     2807              /*calculate the correct cost*/
     2808              uiCostTemp += m_pcRdCost->getCost( uiBitsTemp );
    31422809            }
    31432810            else
     
    31542821          }
    31552822          else
    3156           {
     2823          { 
    31572824            xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
    3158           }
     2825          }       
    31592826        }
    31602827#else
     
    31632830        xCopyAMVPInfo(pcCU->getCUMvField(eRefPicList)->getAMVPInfo(), &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
    31642831        if ( pcCU->getAMVPMode(uiPartAddr) == AM_EXPL )
    3165         {
     2832        {         
    31662833          xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
    31672834        }
    31682835
    3169 #if DCM_COMB_LIST
    31702836        if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag())
    31712837        {
     
    31732839          {
    31742840            uiCostTempL0[iRefIdxTemp] = uiCostTemp;
     2841            uiBitsTempL0[iRefIdxTemp] = uiBitsTemp;
    31752842            if(pcCU->getSlice()->getRefIdxOfLC(REF_PIC_LIST_0, iRefIdxTemp)<0)
    31762843            {
     
    31832850            {
    31842851              uiCostTemp = MAX_UINT;
    3185             }
     2852            }           
    31862853          }
    31872854        }
    3188 #endif
    31892855
    31902856        if ( ( iRefList == 0 && uiCostTemp < uiCost[iRefList] ) ||
    31912857            ( iRefList == 1 &&  pcCU->getSlice()->getNoBackPredFlag() && iRefIdxTemp == iRefIdx[0] ) ||
    3192 #if DCM_COMB_LIST
    31932858            ( iRefList == 1 && (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0) && (iRefIdxTemp==0 || iRefIdxTemp == iRefIdx[0]) && !pcCU->getSlice()->getNoBackPredFlag() && (iRefIdxTemp == pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(iRefIdxTemp)) ) ||
    3194 #endif
    31952859            ( iRefList == 1 && !pcCU->getSlice()->getNoBackPredFlag() && uiCostTemp < uiCost[iRefList] ) )
    31962860          {
    31972861            uiCost[iRefList] = uiCostTemp;
    31982862            uiBits[iRefList] = uiBitsTemp; // storing for bi-prediction
    3199 
     2863           
    32002864            // set motion
    32012865            cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
    32022866            iRefIdx[iRefList] = iRefIdxTemp;
    3203             pcCU->getCUMvField(eRefPicList)->setAllMvField( cMv[iRefList], iRefIdx[iRefList], ePartSize, uiPartAddr, iPartIdx, 0 );
    3204 
    3205             // storing list 1 prediction signal for iterative bi-directional prediction
    3206             if ( eRefPicList == REF_PIC_LIST_1 )
     2867            pcCU->getCUMvField(eRefPicList)->setAllMv( cMv[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     2868            pcCU->getCUMvField(eRefPicList)->setAllRefIdx( iRefIdx[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     2869
     2870#if H0111_MVD_L1_ZERO
     2871            if(!pcCU->getSlice()->getMvdL1ZeroFlag())
    32072872            {
    3208               TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
    3209               motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
     2873#endif
     2874              // storing list 1 prediction signal for iterative bi-directional prediction
     2875              if ( eRefPicList == REF_PIC_LIST_1 )
     2876              {
     2877                TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
     2878                motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
     2879              }
     2880              if ( (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) && eRefPicList == REF_PIC_LIST_0 )
     2881              {
     2882                TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
     2883                motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
     2884              }
     2885#if H0111_MVD_L1_ZERO
    32102886            }
    3211 #if DCM_COMB_LIST
    3212             if ( (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) && eRefPicList == REF_PIC_LIST_0 )
    3213 #else
    3214             if ( pcCU->getSlice()->getNoBackPredFlag() && eRefPicList == REF_PIC_LIST_0 )
    3215 #endif
    3216             {
    3217               TComYuv*  pcYuvPred = &m_acYuvPred[iRefList];
    3218               motionCompensation ( pcCU, pcYuvPred, eRefPicList, iPartIdx );
    3219             }
     2887#endif
    32202888          }
    32212889      }
     
    32242892    if ( pcCU->getSlice()->isInterB() )
    32252893    {
    3226 
     2894     
    32272895      cMvBi[0] = cMv[0];            cMvBi[1] = cMv[1];
    32282896      iRefIdxBi[0] = iRefIdx[0];    iRefIdxBi[1] = iRefIdx[1];
    3229 
     2897     
    32302898      ::memcpy(cMvPredBi, cMvPred, sizeof(cMvPred));
    32312899      ::memcpy(aaiMvpIdxBi, aaiMvpIdx, sizeof(aaiMvpIdx));
    3232 
     2900     
     2901#if H0111_MVD_L1_ZERO
     2902      UInt uiMotBits[2];
     2903
     2904      if(pcCU->getSlice()->getMvdL1ZeroFlag())
     2905      {
     2906        xCopyAMVPInfo(&aacAMVPInfo[1][bestBiPRefIdxL1], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
     2907        pcCU->setMVPIdxSubParts( bestBiPMvpL1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     2908        aaiMvpIdxBi[1][bestBiPRefIdxL1] = bestBiPMvpL1;
     2909        cMvPredBi[1][bestBiPRefIdxL1]   = pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo()->m_acMvCand[bestBiPMvpL1];
     2910
     2911        cMvBi[1] = cMvPredBi[1][bestBiPRefIdxL1];
     2912        iRefIdxBi[1] = bestBiPRefIdxL1;
     2913        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
     2914        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
     2915        TComYuv* pcYuvPred = &m_acYuvPred[1];
     2916        motionCompensation( pcCU, pcYuvPred, REF_PIC_LIST_1, iPartIdx );
     2917
     2918        uiMotBits[0] = uiBits[0] - uiMbBits[0];
     2919        uiMotBits[1] = uiMbBits[1];
     2920
     2921        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
     2922        {
     2923          uiMotBits[1] += bestBiPRefIdxL1+1;
     2924          if ( bestBiPRefIdxL1 == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiMotBits[1]--;
     2925        }
     2926
     2927#if HHI_INTER_VIEW_MOTION_PRED
     2928        uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][iNumAMVPCands];
     2929#else
     2930        uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][AMVP_MAX_NUM_CANDS];
     2931#endif
     2932
     2933        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
     2934
     2935        cMvTemp[1][bestBiPRefIdxL1] = cMvBi[1];
     2936      }
     2937      else
     2938      {
     2939        uiMotBits[0] = uiBits[0] - uiMbBits[0];
     2940        uiMotBits[1] = uiBits[1] - uiMbBits[1];
     2941        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
     2942      }
     2943#else   
    32332944      UInt uiMotBits[2] = { uiBits[0] - uiMbBits[0], uiBits[1] - uiMbBits[1] };
    32342945      uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
     2946#endif     
    32352947
    32362948      // 4-times iteration (default)
    32372949      Int iNumIter = 4;
    3238 
     2950     
    32392951      // fast encoder setting: only one iteration
     2952#if H0111_MVD_L1_ZERO
     2953      if ( m_pcEncCfg->getUseFastEnc() || pcCU->getSlice()->getMvdL1ZeroFlag())
     2954#else
    32402955      if ( m_pcEncCfg->getUseFastEnc() )
     2956#endif
    32412957      {
    32422958        iNumIter = 1;
    32432959      }
    3244 
     2960     
    32452961      for ( Int iIter = 0; iIter < iNumIter; iIter++ )
    32462962      {
    3247 
     2963       
    32482964        Int         iRefList    = iIter % 2;
    3249 #if DCM_COMB_LIST
    32502965        if ( m_pcEncCfg->getUseFastEnc() && (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) )
    3251 #else
    3252         if ( m_pcEncCfg->getUseFastEnc() && pcCU->getSlice()->getNoBackPredFlag() )
    3253 #endif
    32542966        {
    32552967          iRefList = 1;
     
    32572969        RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
    32582970
     2971#if H0111_MVD_L1_ZERO
     2972        if(pcCU->getSlice()->getMvdL1ZeroFlag())
     2973        {
     2974          iRefList = 0;
     2975          eRefPicList = REF_PIC_LIST_0;
     2976        }
     2977#endif
     2978
    32592979        Bool bChanged = false;
    3260 
    3261 #if GPB_SIMPLE
    3262         if ( pcCU->getSlice()->getSPS()->getUseLDC() && iRefList )
    3263         {
    3264           iRefStart = iRefIdxBi[1-iRefList];
    3265           iRefEnd   = iRefIdxBi[1-iRefList];
    3266         }
    3267         else
    3268         {
    3269           iRefStart = 0;
    3270           iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
    3271         }
    3272 #else
     2980       
    32732981        iRefStart = 0;
    32742982        iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
    3275 #endif
    3276 
    3277 #if DCM_COMB_LIST
    3278         if ( m_pcEncCfg->getUseFastEnc() && (pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 )) )
    3279 #else
    3280         if ( m_pcEncCfg->getUseFastEnc() && pcCU->getSlice()->getNoBackPredFlag() )
    3281 #endif
    3282         {
    3283           iRefStart = 0;
    3284           iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
    3285         }
    3286 
     2983       
    32872984        for ( Int iRefIdxTemp = iRefStart; iRefIdxTemp <= iRefEnd; iRefIdxTemp++ )
    32882985        {
    3289 #ifdef WEIGHT_PRED
    3290           if ( eRefPicList == REF_PIC_LIST_0 ) setWpScalingDistParam( pcCU, iRefIdxTemp, -1 , eRefPicList);
    3291           if ( eRefPicList == REF_PIC_LIST_1 ) setWpScalingDistParam( pcCU, -1, iRefIdxTemp , eRefPicList);
    3292 #endif
    32932986          uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
    32942987          if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
     
    32972990            if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
    32982991          }
    3299 
    3300           uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][aaiMvpNum[iRefList][iRefIdxTemp]];
     2992#if HHI_INTER_VIEW_MOTION_PRED
     2993          uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][iNumAMVPCands];
     2994#else
     2995          uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
     2996#endif
    33012997          // call ME
    33022998          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, true );
     
    33063002            xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
    33073003          }
    3308 
     3004         
    33093005          if ( uiCostTemp < uiCostBi )
    33103006          {
    33113007            bChanged = true;
    3312 
     3008           
    33133009            cMvBi[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
    33143010            iRefIdxBi[iRefList] = iRefIdxTemp;
    3315 
     3011           
    33163012            uiCostBi            = uiCostTemp;
    33173013            uiMotBits[iRefList] = uiBitsTemp - uiMbBits[2] - uiMotBits[1-iRefList];
    33183014            uiBits[2]           = uiBitsTemp;
    3319 
    3320             //  Set motion
    3321             pcCU->getCUMvField( eRefPicList )->setAllMvField( cMvBi[iRefList], iRefIdxBi[iRefList], ePartSize, uiPartAddr, iPartIdx, 0 );
    3322 
    3323             TComYuv* pcYuvPred = &m_acYuvPred[iRefList];
    3324             motionCompensation( pcCU, pcYuvPred, eRefPicList, iPartIdx );
     3015           
     3016#if H0111_MVD_L1_ZERO
     3017            if(iNumIter!=1)
     3018            {
     3019#endif
     3020              //  Set motion
     3021              pcCU->getCUMvField( eRefPicList )->setAllMv( cMvBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     3022              pcCU->getCUMvField( eRefPicList )->setAllRefIdx( iRefIdxBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     3023
     3024              TComYuv* pcYuvPred = &m_acYuvPred[iRefList];
     3025              motionCompensation( pcCU, pcYuvPred, eRefPicList, iPartIdx );
     3026#if H0111_MVD_L1_ZERO
     3027            }
     3028#endif
    33253029          }
    33263030        } // for loop-iRefIdxTemp
    3327 
     3031       
    33283032        if ( !bChanged )
    33293033        {
     
    33323036            xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], pcCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
    33333037            xCheckBestMVP(pcCU, REF_PIC_LIST_0, cMvBi[0], cMvPredBi[0][iRefIdxBi[0]], aaiMvpIdxBi[0][iRefIdxBi[0]], uiBits[2], uiCostBi);
    3334             xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
    3335             xCheckBestMVP(pcCU, REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], uiBits[2], uiCostBi);
     3038#if H0111_MVD_L1_ZERO
     3039            if(!pcCU->getSlice()->getMvdL1ZeroFlag())
     3040            {
     3041#endif
     3042              xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
     3043              xCheckBestMVP(pcCU, REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], uiBits[2], uiCostBi);
     3044#if H0111_MVD_L1_ZERO
     3045            }
     3046#endif
    33363047          }
    33373048          break;
     
    33723083            pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( aacAMVPInfo[0][iL0RefIdxTemp].m_acMvCand[iL0MVPIdx], iL0RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
    33733084            pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( aacAMVPInfo[1][iL1RefIdxTemp].m_acMvCand[iL1MVPIdx], iL1RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
    3374 
     3085 
    33753086            xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiZeroMvdDistTemp, m_pcEncCfg->getUseHADME() );
    33763087            uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost( uiZeroMvdBitsTemp );
     
    33903101    }
    33913102#endif
    3392 #if PART_MRG
     3103
     3104#if AMP_MRG
    33933105    } //end if bTestNormalMC
    33943106#endif
    33953107    //  Clear Motion Field
    3396     {
    3397       pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvZero, NOT_VALID, ePartSize, uiPartAddr, iPartIdx, 0 );
    3398       pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvZero, NOT_VALID, ePartSize, uiPartAddr, iPartIdx, 0 );
    3399     }
    3400     pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, iPartIdx, 0 );
    3401     pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, iPartIdx, 0 );
     3108    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
     3109    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
     3110    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,       ePartSize, uiPartAddr, 0, iPartIdx );
     3111    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,       ePartSize, uiPartAddr, 0, iPartIdx );
    34023112
    34033113    pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    34053115    pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    34063116    pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    3407 
     3117   
    34083118    UInt uiMEBits = 0;
    34093119    // Set Motion Field_
    3410 #if DCM_COMB_LIST
    34113120    if ( pcCU->getSlice()->getNoBackPredFlag() || (pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && pcCU->getSlice()->getRefIdxOfL0FromRefIdxOfL1(0)==0 ) )
    3412 #else
    3413     if ( pcCU->getSlice()->getNoBackPredFlag() )
    3414 #endif
    34153121    {
    34163122      uiCost[1] = MAX_UINT;
    34173123    }
    3418 #if PART_MRG
     3124#if AMP_MRG
    34193125    if (bTestNormalMC)
    34203126    {
     
    34293135        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
    34303136        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
    3431 
     3137 
    34323138        pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3433 
     3139       
    34343140        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    34353141        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    34393145      }
    34403146      else if (iZeroMvdDir == 1)
    3441       {
     3147      {       
    34423148        uiLastMode = 0;
    34433149
     
    34453151
    34463152        pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3447 
     3153       
    34483154        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    34493155        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    34573163
    34583164        pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3459 
     3165       
    34603166        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    34613167        pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    34733179      uiLastMode = 2;
    34743180      {
    3475         pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvBi[0], iRefIdxBi[0], ePartSize, uiPartAddr, iPartIdx, 0 );
    3476         pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvBi[1], iRefIdxBi[1], ePartSize, uiPartAddr, iPartIdx, 0 );
     3181            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMvBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
     3182            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdxBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
     3183            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
     3184            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
    34773185      }
    34783186      {
    34793187        TempMv = cMvBi[0] - cMvPredBi[0][iRefIdxBi[0]];
    3480         pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, iPartIdx, 0 );
    3481 #if DCM_SIMPLIFIED_MVP==0
    3482         if (pcCU->clearMVPCand(TempMv, &aacAMVPInfo[0][iRefIdxBi[0]]))
    3483         {
    3484           aaiMvpIdxBi[0][iRefIdxBi[0]] = pcCU->searchMVPIdx(cMvPredBi[0][iRefIdxBi[0]], &aacAMVPInfo[0][iRefIdxBi[0]]);
    3485           aaiMvpNum[0][iRefIdxBi[0]] = aacAMVPInfo[0][iRefIdxBi[0]].iN;
    3486         }
    3487 #endif
     3188            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
    34883189      }
    34893190      {
    34903191        TempMv = cMvBi[1] - cMvPredBi[1][iRefIdxBi[1]];
    3491         pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, iPartIdx, 0 );
    3492 #if DCM_SIMPLIFIED_MVP==0
    3493         if (pcCU->clearMVPCand(TempMv, &aacAMVPInfo[1][iRefIdxBi[1]]))
    3494         {
    3495           aaiMvpIdxBi[1][iRefIdxBi[1]] = pcCU->searchMVPIdx(cMvPredBi[1][iRefIdxBi[1]], &aacAMVPInfo[1][iRefIdxBi[1]]);
    3496           aaiMvpNum[1][iRefIdxBi[1]] = aacAMVPInfo[1][iRefIdxBi[1]].iN;
    3497         }
    3498 #endif
    3499       }
    3500 
     3192            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
     3193      }
     3194     
    35013195      pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3502 
     3196     
    35033197      pcCU->setMVPIdxSubParts( aaiMvpIdxBi[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    35043198      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    35113205    {
    35123206      uiLastMode = 0;
    3513       pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMv[0],   iRefIdx[0],   ePartSize, uiPartAddr, iPartIdx, 0 );
    3514 
     3207          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
     3208          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
    35153209      {
    35163210        TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
    3517         pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, iPartIdx, 0 );
    3518 #if DCM_SIMPLIFIED_MVP==0
    3519         if (pcCU->clearMVPCand(TempMv, &aacAMVPInfo[0][iRefIdx[0]]))
    3520         {
    3521           aaiMvpIdx[0][iRefIdx[0]] = pcCU->searchMVPIdx(cMvPred[0][iRefIdx[0]], &aacAMVPInfo[0][iRefIdx[0]]);
    3522           aaiMvpNum[0][iRefIdx[0]] = aacAMVPInfo[0][iRefIdx[0]].iN;
    3523         }
    3524 #endif
     3211            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
    35253212      }
    35263213      pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3527 
     3214     
    35283215      pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    35293216      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    35343221    {
    35353222      uiLastMode = 1;
    3536       pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMv[1],   iRefIdx[1],   ePartSize, uiPartAddr, iPartIdx, 0 );
    3537 
     3223          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
     3224          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
    35383225      {
    35393226        TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
    3540         pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, iPartIdx, 0 );
    3541 #if DCM_SIMPLIFIED_MVP==0
    3542         if (pcCU->clearMVPCand(TempMv, &aacAMVPInfo[1][iRefIdx[1]]))
    3543         {
    3544           aaiMvpIdx[1][iRefIdx[1]] = pcCU->searchMVPIdx(cMvPred[1][iRefIdx[1]], &aacAMVPInfo[1][iRefIdx[1]]);
    3545           aaiMvpNum[1][iRefIdx[1]] = aacAMVPInfo[1][iRefIdx[1]].iN;
    3546         }
    3547 #endif
     3227            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
    35483228      }
    35493229      pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    3550 
     3230     
    35513231      pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    35523232      pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    35543234      uiMEBits = uiBits[1];
    35553235    }
    3556 #if PART_MRG
     3236#if AMP_MRG
    35573237    } // end if bTestNormalMC
    35583238#endif
    35593239
    3560     if ( pcCU->getSlice()->getSPS()->getUseMRG() && pcCU->getPartitionSize( uiPartAddr ) == SIZE_2Nx2N )
    3561     {
    3562       UChar       uhInterDirNeighbours[MRG_MAX_NUM_CANDS];
    3563       UInt        uiNeighbourCandIdx  [MRG_MAX_NUM_CANDS];
    3564       TComMvField cMvFieldNeighbours  [MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists
    3565       for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
    3566       {
    3567         uhInterDirNeighbours[ui] = 0;
    3568         uiNeighbourCandIdx  [ui] = 0;
    3569       }
    3570       pcCU->getInterMergeCandidates( uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ), cMvFieldNeighbours, uhInterDirNeighbours, uiNeighbourCandIdx );
    3571       for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
    3572       {
    3573         pcCU->setNeighbourCandIdxSubParts( ui, uiNeighbourCandIdx[ui], uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
    3574       }
    3575     }
    3576 
    3577     if ( pcCU->getSlice()->getSPS()->getUseMRG() && pcCU->getPartitionSize( uiPartAddr ) != SIZE_2Nx2N )
    3578     {
    3579       UInt uiMRGInterDir = 0;
     3240    if ( pcCU->getPartitionSize( uiPartAddr ) != SIZE_2Nx2N )
     3241    {
     3242      UInt uiMRGInterDir = 0;     
    35803243      TComMvField cMRGMvField[2];
    35813244      UInt uiMRGIndex = 0;
     
    35853248
    35863249      m_pcRdCost->getMotionCost( 1, 0 );
    3587 #if PART_MRG
     3250#if AMP_MRG
    35883251      // calculate ME cost
    35893252      UInt uiMEError = MAX_UINT;
     
    36003263      xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiMEError, m_pcEncCfg->getUseHADME() );
    36013264      UInt uiMECost = uiMEError + m_pcRdCost->getCost( uiMEBits );
    3602 #endif
     3265#endif 
    36033266      // save ME result.
    36043267      uiMEInterDir = pcCU->getInterDir( uiPartAddr );
     
    36073270
    36083271      // find Merge result
    3609       UInt uiMRGError = MAX_UINT;
    3610       UInt uiMRGBits = MAX_UINT;
    3611       Bool bMergeValid = false;
    3612       UChar ucNeighCand[MRG_MAX_NUM_CANDS];
    3613       for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
    3614       {
    3615         ucNeighCand[ui] = 0;
    3616       }
    3617       xMergeEstimation( pcCU, pcOrgYuv, iPartIdx, uiMRGInterDir, cMRGMvField, uiMRGIndex, uiMRGError, uiMRGBits, ucNeighCand, bMergeValid );
    3618       UInt uiMRGCost = uiMRGError + m_pcRdCost->getCost( uiMRGBits );
    3619 
    3620       for( UInt ui = 0; ui < MRG_MAX_NUM_CANDS; ui++ )
    3621       {
    3622         pcCU->setNeighbourCandIdxSubParts( ui, ucNeighCand[ui], uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
    3623       }
    3624       if ( bMergeValid && uiMRGCost < uiMECost )
     3272      UInt uiMRGCost = MAX_UINT;
     3273#if CU_BASED_MRG_CAND_LIST
     3274      xMergeEstimation( pcCU, pcOrgYuv, iPartIdx, uiMRGInterDir, cMRGMvField, uiMRGIndex, uiMRGCost, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand);
     3275#else
     3276      xMergeEstimation( pcCU, pcOrgYuv, iPartIdx, uiMRGInterDir, cMRGMvField, uiMRGIndex, uiMRGCost );
     3277#endif
     3278      if ( uiMRGCost < uiMECost )
    36253279      {
    36263280        // set Merge result
     
    36293283        pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
    36303284        {
    3631           pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0].getMv(), cMRGMvField[0].getRefIdx(), ePartSize, uiPartAddr, iPartIdx, 0 );
    3632           pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1].getMv(), cMRGMvField[1].getRefIdx(), ePartSize, uiPartAddr, iPartIdx, 0 );
    3633         }
    3634 
    3635         pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, iPartIdx, 0 );
    3636         pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, iPartIdx, 0 );
     3285          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
     3286          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
     3287        }
     3288
     3289        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
     3290        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
     3291
    36373292        pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    36383293        pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     
    36463301        pcCU->setInterDirSubParts ( uiMEInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
    36473302        {
    3648           pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMEMvField[0].getMv(), cMEMvField[0].getRefIdx(), ePartSize, uiPartAddr, iPartIdx, 0 );
    3649           pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMEMvField[1].getMv(), cMEMvField[1].getRefIdx(), ePartSize, uiPartAddr, iPartIdx, 0 );
    3650         }
    3651       }
    3652 #if PART_MRG
    3653       if (!bTestNormalMC && !bMergeValid)
    3654       {
    3655         assert(pcCU->getWidth( 0 ) > 8 && iNumPart == 2 && iPartIdx == 0);
    3656         return;
    3657       }
    3658 #endif
     3303          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMEMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
     3304          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMEMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
     3305        }
     3306      }
    36593307    }
    36603308
    36613309    //  MC
    36623310    motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
    3663 
     3311   
    36643312  } //  end of for ( Int iPartIdx = 0; iPartIdx < iNumPart; iPartIdx++ )
    36653313
    3666 #ifdef WEIGHT_PRED
    3667   setWpScalingDistParam( pcCU, -1, -1, REF_PIC_LIST_X );
    3668 #endif
     3314  setWpScalingDistParam( pcCU, -1, REF_PIC_LIST_X );
     3315
    36693316  return;
    36703317}
    36713318
    36723319// AMVP
     3320#if H0111_MVD_L1_ZERO
     3321#if ZERO_MVD_EST
     3322Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled, UInt* puiDistBiP, UInt* puiDist  )
     3323#else
     3324Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled, UInt* puiDistBiP )
     3325#endif
     3326#else
    36733327#if ZERO_MVD_EST
    36743328Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled, UInt* puiDist )
     
    36763330Void TEncSearch::xEstimateMvPredAMVP( TComDataCU* pcCU, TComYuv* pcOrgYuv, UInt uiPartIdx, RefPicList eRefPicList, Int iRefIdx, TComMv& rcMvPred, Bool bFilled )
    36773331#endif
     3332#endif
    36783333{
    36793334  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
    3680 
     3335 
    36813336  TComMv  cBestMv;
    36823337  Int     iBestIdx = 0;
     
    36873342  Int     iRoiWidth, iRoiHeight;
    36883343  Int     i;
    3689 
     3344 
    36903345  pcCU->getPartIndexAndSize( uiPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
    36913346  // Fill the MV Candidates
     
    36943349    pcCU->fillMvpCand( uiPartIdx, uiPartAddr, eRefPicList, iRefIdx, pcAMVPInfo );
    36953350  }
    3696 
     3351 
    36973352  // initialize Mvp index & Mvp
    36983353  iBestIdx = 0;
     
    37023357  {
    37033358    rcMvPred = cBestMv;
    3704 
     3359   
    37053360    pcCU->setMVPIdxSubParts( iBestIdx, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
    37063361    pcCU->setMVPNumSubParts( pcAMVPInfo->iN, eRefPicList, uiPartAddr, uiPartIdx, pcCU->getDepth(uiPartAddr));
     3362
     3363#if H0111_MVD_L1_ZERO
     3364    if(pcCU->getSlice()->getMvdL1ZeroFlag() && eRefPicList==REF_PIC_LIST_1)
     3365    {
     3366#if HHI_INTER_VIEW_MOTION_PRED
     3367      Int iNumAMVPCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
     3368#if ZERO_MVD_EST
     3369      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, iNumAMVPCands, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
     3370#else
     3371      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, iNumAMVPCands, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
     3372#endif
     3373#else
     3374#if ZERO_MVD_EST
     3375      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
     3376#else
     3377      (*puiDistBiP) = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, rcMvPred, 0, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
     3378#endif
     3379#endif
     3380    }
     3381#endif
    37073382    return;
    37083383  }
    3709 #endif
     3384#endif 
    37103385  if (pcCU->getAMVPMode(uiPartAddr) == AM_EXPL && bFilled)
    37113386  {
     
    37143389    return;
    37153390  }
    3716 
     3391 
    37173392  if (pcCU->getAMVPMode(uiPartAddr) == AM_EXPL)
    37183393  {
     
    37253400    {
    37263401      UInt uiTmpCost;
     3402#if HHI_INTER_VIEW_MOTION_PRED
     3403      Int iNumAMVPCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
    37273404#if ZERO_MVD_EST
    3728       uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, pcAMVPInfo->iN, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
    3729 #else
    3730       uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, pcAMVPInfo->iN, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
     3405      uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, iNumAMVPCands, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
     3406#else
     3407      uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, iNumAMVPCands, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
     3408#endif
     3409#else
     3410#if ZERO_MVD_EST
     3411      uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight, uiDist );
     3412#else
     3413      uiTmpCost = xGetTemplateCost( pcCU, uiPartIdx, uiPartAddr, pcOrgYuv, &m_cYuvPredTemp, pcAMVPInfo->m_acMvCand[i], i, AMVP_MAX_NUM_CANDS, eRefPicList, iRefIdx, iRoiWidth, iRoiHeight);
     3414#endif     
    37313415#endif
    37323416      if ( uiBestCost > uiTmpCost )
     
    37353419        cBestMv   = pcAMVPInfo->m_acMvCand[i];
    37363420        iBestIdx  = i;
     3421        #if H0111_MVD_L1_ZERO
     3422        (*puiDistBiP) = uiTmpCost;
     3423        #endif
    37373424        #if ZERO_MVD_EST
    37383425        (*puiDist) = uiDist;
     
    37403427      }
    37413428    }
    3742 
     3429   
    37433430    m_cYuvPredTemp.clear();
    37443431  }
    3745 
     3432 
    37463433  // Setting Best MVP
    37473434  rcMvPred = cBestMv;
     
    37543441{
    37553442  assert(iIdx >= 0 && iNum >= 0 && iIdx < iNum);
    3756 
     3443 
    37573444  if (iNum == 1)
    37583445    return 0;
    3759 
     3446 
    37603447  UInt uiLength = 1;
    37613448  Int iTemp = iIdx;
     
    37643451    return uiLength;
    37653452  }
    3766 
     3453 
    37673454  Bool bCodeLast = ( iNum-1 > iTemp );
    3768 
     3455 
    37693456  uiLength += (iTemp-1);
    3770 
     3457 
    37713458  if( bCodeLast )
    37723459  {
    37733460    uiLength++;
    37743461  }
    3775 
     3462 
    37763463  return uiLength;
    37773464}
     
    37853472    uiBlkBit[2] = 5;
    37863473  }
    3787   else if ( eCUMode == SIZE_2NxN )
     3474  else if ( (eCUMode == SIZE_2NxN || eCUMode == SIZE_2NxnU) || eCUMode == SIZE_2NxnD )
    37883475  {
    37893476    UInt aauiMbBits[2][3][3] = { { {0,0,3}, {0,0,0}, {0,0,0} } , { {5,7,7}, {7,5,7}, {9-3,9-3,9-3} } };
     
    37993486    }
    38003487  }
    3801   else if ( eCUMode == SIZE_Nx2N )
     3488  else if ( (eCUMode == SIZE_Nx2N || eCUMode == SIZE_nLx2N) || eCUMode == SIZE_nRx2N )
    38023489  {
    38033490    UInt aauiMbBits[2][3][3] = { { {0,2,3}, {0,0,0}, {0,0,0} } , { {5,7,7}, {7-2,7-2,9-2}, {9-3,9-3,9-3} } };
     
    38383525{
    38393526  AMVPInfo* pcAMVPInfo = pcCU->getCUMvField(eRefPicList)->getAMVPInfo();
    3840 
     3527 
    38413528  assert(pcAMVPInfo->m_acMvCand[riMVPIdx] == rcMvPred);
    3842 
     3529 
    38433530  if (pcAMVPInfo->iN < 2) return;
    3844 
     3531 
    38453532  m_pcRdCost->getMotionCost( 1, 0 );
    38463533  m_pcRdCost->setCostScale ( 0    );
    3847 
     3534 
    38483535  Int iBestMVPIdx = riMVPIdx;
    3849 
     3536 
     3537#if HHI_INTER_VIEW_MOTION_PRED
     3538  Int iNumAMVPCands = AMVP_MAX_NUM_CANDS + ( pcCU->getSlice()->getSPS()->getMultiviewMvPredMode() ? 1 : 0 );
     3539#endif
     3540 
    38503541  m_pcRdCost->setPredictor( rcMvPred );
    38513542  Int iOrgMvBits  = m_pcRdCost->getBits(cMv.getHor(), cMv.getVer());
     3543#if HHI_INTER_VIEW_MOTION_PRED
     3544  iOrgMvBits += m_auiMVPIdxCost[riMVPIdx][iNumAMVPCands];
     3545#else
     3546  iOrgMvBits += m_auiMVPIdxCost[riMVPIdx][AMVP_MAX_NUM_CANDS];
     3547#endif
    38523548  Int iBestMvBits = iOrgMvBits;
    3853 
     3549 
    38543550  for (Int iMVPIdx = 0; iMVPIdx < pcAMVPInfo->iN; iMVPIdx++)
    38553551  {
    38563552    if (iMVPIdx == riMVPIdx) continue;
    3857 
     3553   
    38583554    m_pcRdCost->setPredictor( pcAMVPInfo->m_acMvCand[iMVPIdx] );
    3859 
     3555   
    38603556    Int iMvBits = m_pcRdCost->getBits(cMv.getHor(), cMv.getVer());
    3861 
     3557#if HHI_INTER_VIEW_MOTION_PRED
     3558    iMvBits += m_auiMVPIdxCost[iMVPIdx][iNumAMVPCands];
     3559#else
     3560    iMvBits += m_auiMVPIdxCost[iMVPIdx][AMVP_MAX_NUM_CANDS];
     3561#endif
     3562   
    38623563    if (iMvBits < iBestMvBits)
    38633564    {
     
    38663567    }
    38673568  }
    3868 
     3569 
    38693570  if (iBestMVPIdx != riMVPIdx)  //if changed
    38703571  {
    38713572    rcMvPred = pcAMVPInfo->m_acMvCand[iBestMVPIdx];
    3872 
    3873     iOrgMvBits  += m_auiMVPIdxCost[riMVPIdx][pcAMVPInfo->iN];
    3874     iBestMvBits += m_auiMVPIdxCost[iBestMVPIdx][pcAMVPInfo->iN];
    3875 
     3573   
    38763574    riMVPIdx = iBestMVPIdx;
    38773575    UInt uiOrgBits = ruiBits;
     
    38993597{
    39003598  UInt uiCost  = MAX_INT;
    3901 
     3599 
    39023600  TComPicYuv* pcPicYuvRef = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdx )->getPicYuvRec();
    3903 
     3601 
     3602  pcCU->clipMv( cMvCand );
     3603
     3604#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
     3605  if( pcCU->getSlice()->getIsDepth() )
     3606    cMvCand <<= 2;
     3607#endif
    39043608  // prediction pattern
    3905   xPredInterLumaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMvCand, iSizeX, iSizeY, pcTemplateCand );
    3906 
    3907 #ifdef WEIGHT_PRED
    3908   if ( pcCU->getSlice()->getPPS()->getUseWP() )
    3909   {
    3910     PartSize  ePartSize = pcCU->getPartitionSize( 0 );
    3911     pcCU->getCUMvField(eRefPicList)->setAllMvField( cMvCand, iRefIdx, ePartSize, uiPartAddr, uiPartIdx, 0 );
    3912 #if 0
    3913     TComMvField mvField;
    3914     mvField.setMvField( cMvCand, iRefIdx);
    3915     pcCU->getCUMvField(eRefPicList)->setAllMvField( mvField, ePartSize, uiPartAddr, 0 );
    3916 #endif
    3917     xWeightedPredictionUni( pcCU, pcTemplateCand, uiPartAddr, iSizeX, iSizeY, eRefPicList, pcTemplateCand, uiPartIdx );
    3918   }
    3919 #endif
     3609  if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType()==P_SLICE )
     3610  {
     3611    xPredInterLumaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMvCand, iSizeX, iSizeY, pcTemplateCand, true );
     3612  }
     3613  else
     3614  {
     3615    xPredInterLumaBlk( pcCU, pcPicYuvRef, uiPartAddr, &cMvCand, iSizeX, iSizeY, pcTemplateCand, false );
     3616  }
     3617
     3618  if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType()==P_SLICE )
     3619  {
     3620    xWeightedPredictionUni( pcCU, pcTemplateCand, uiPartAddr, iSizeX, iSizeY, eRefPicList, pcTemplateCand, uiPartIdx, iRefIdx );
     3621  }
    39203622
    39213623  // calc distortion
     
    39233625  m_pcRdCost->getMotionCost( 1, 0 );
    39243626  DistParam cDistParam;
    3925   m_pcRdCost->setDistParam( cDistParam,
    3926                             pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(),
    3927                             pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(),
     3627  m_pcRdCost->setDistParam( cDistParam,
     3628                            pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(),
     3629                            pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(),
     3630#if NS_HAD
     3631                            iSizeX, iSizeY, m_pcEncCfg->getUseHADME(), m_pcEncCfg->getUseNSQT() );
     3632#else
    39283633                            iSizeX, iSizeY, m_pcEncCfg->getUseHADME() );
    3929   // MW: check VSO here
     3634#endif
    39303635  ruiDist = cDistParam.DistFunc( &cDistParam );
    39313636  uiCost = ruiDist + m_pcRdCost->getCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum] );
    39323637#else
    3933 #if HHI_VSO
    3934   if ( false /*m_pcRdCost->getUseVSO()*/ ) // GT: currently disabled
    3935   {
    3936     uiCost = m_pcRdCost->getDistVS  ( pcCU, uiPartAddr,  pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, true, 0);
    3937     uiCost = (UInt) m_pcRdCost->calcRdCostVSO( m_auiMVPIdxCost[iMVPIdx][iMVPNum], uiCost, false, DF_SAD );
    3938   }
    3939   else
    3940 #endif
    3941   {
    3942     uiCost = m_pcRdCost->getDistPart( pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, DF_SAD );
    3943     uiCost = (UInt) m_pcRdCost->calcRdCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum], uiCost, false, DF_SAD );
    3944   }
     3638// GT: CONSIDER ADDING VSO HERE
     3639#if WEIGHTED_CHROMA_DISTORTION
     3640  uiCost = m_pcRdCost->getDistPart( pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, false, DF_SAD );
     3641#else
     3642  uiCost = m_pcRdCost->getDistPart( pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, DF_SAD );
     3643#endif
     3644  uiCost = (UInt) m_pcRdCost->calcRdCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum], uiCost, false, DF_SAD );
    39453645#endif
    39463646  return uiCost;
     
    39523652  Int           iRoiWidth;
    39533653  Int           iRoiHeight;
    3954 
     3654 
    39553655  TComMv        cMvHalf, cMvQter;
    39563656  TComMv        cMvSrchRngLT;
    39573657  TComMv        cMvSrchRngRB;
    3958 
     3658 
    39593659  TComYuv*      pcYuv = pcYuvOrg;
    3960 #ifdef ROUNDING_CONTROL_BIPRED
    3961   Pel           pRefBufY[16384];  // 128x128
    3962 #endif
    39633660  m_iSearchRange = m_aaiAdaptSR[eRefPicList][iRefIdxPred];
    3964 
     3661 
    39653662  Int           iSrchRng      = ( bBi ? m_bipredSearchRange : m_iSearchRange );
    39663663  TComPattern*  pcPatternKey  = pcCU->getPattern        ();
    3967 
     3664 
    39683665  Double        fWeight       = 1.0;
    3969 
     3666 
    39703667  pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
    3971 
     3668 
    39723669  if ( bBi )
    39733670  {
    39743671    TComYuv*  pcYuvOther = &m_acYuvPred[1-(Int)eRefPicList];
    39753672    pcYuv                = &m_cYuvPredTemp;
    3976 
     3673   
    39773674    pcYuvOrg->copyPartToPartYuv( pcYuv, uiPartAddr, iRoiWidth, iRoiHeight );
    3978 
    3979 #ifdef ROUNDING_CONTROL_BIPRED
    3980     Int y;
    3981     //Int x;
    3982     Pel *pRefY = pcYuvOther->getLumaAddr(uiPartAddr);
    3983     Int iRefStride = pcYuvOther->getStride();
    3984 
    3985     // copy the MC block into pRefBufY
    3986     for( y = 0; y < iRoiHeight; y++)
    3987     {
    3988       memcpy(pRefBufY+y*iRoiWidth,pRefY,sizeof(Pel)*iRoiWidth);
    3989       pRefY += iRefStride;
    3990     }
    3991 #else
     3675   
    39923676    pcYuv->removeHighFreq( pcYuvOther, uiPartAddr, iRoiWidth, iRoiHeight );
    3993 #endif
    3994 
     3677   
    39953678    fWeight = 0.5;
    39963679  }
    3997 
     3680 
    39983681  //  Search key pattern initialization
    39993682  pcPatternKey->initPattern( pcYuv->getLumaAddr( uiPartAddr ),
     
    40043687                            pcYuv->getStride(),
    40053688                            0, 0, 0, 0 );
    4006 
     3689 
    40073690  Pel*        piRefY      = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr );
    40083691  Int         iRefStride  = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getStride();
    4009 
     3692 
    40103693  TComMv      cMvPred = *pcMvPred;
    4011 
     3694 
    40123695  if ( bBi )  xSetSearchRange   ( pcCU, rcMv   , iSrchRng, cMvSrchRngLT, cMvSrchRngRB );
    40133696  else        xSetSearchRange   ( pcCU, cMvPred, iSrchRng, cMvSrchRngLT, cMvSrchRngRB );
    4014 
     3697 
    40153698  m_pcRdCost->getMotionCost ( 1, 0 );
    4016 
     3699 
    40173700  m_pcRdCost->setPredictor  ( *pcMvPred );
    40183701#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4019   if( pcCU->getSlice()->getSPS()->isDepth() )
     3702  if( pcCU->getSlice()->getIsDepth() )
    40203703    m_pcRdCost->setCostScale  ( 0 );
    40213704  else
     
    40423725#endif
    40433726
     3727  setWpScalingDistParam( pcCU, iRefIdxPred, eRefPicList );
    40443728  //  Do integer search
    4045 #ifdef ROUNDING_CONTROL_BIPRED
    4046   if( bBi )
    4047   {
    4048     xPatternSearch_Bi      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost, pRefBufY, pcCU->getSlice()->isRounding() );
     3729  if ( !m_iFastSearch || bBi )
     3730  {
     3731    xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    40493732  }
    40503733  else
    40513734  {
    4052     if ( !m_iFastSearch)
    4053     {
    4054       xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    4055     }
    4056     else
    4057     {
    4058       rcMv = *pcMvPred;
    4059       xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    4060     }
    4061   }
    4062 #else
    4063   if ( !m_iFastSearch || bBi )
    4064   {
    4065     xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    4066   }
    4067   else
    4068   {
    4069     rcMv = ( m_pcRdCost->useMultiviewReg() ? m_pcRdCost->getMultiviewOrgMvPred() : *pcMvPred );
     3735    rcMv = *pcMvPred;
    40703736    xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    40713737  }
    4072 #endif
    4073 
    4074 
     3738 
    40753739  m_pcRdCost->getMotionCost( 1, 0 );
    40763740#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4077   if( ! pcCU->getSlice()->getSPS()->isDepth() )
     3741  if( ! pcCU->getSlice()->getIsDepth() )
    40783742  {
    40793743#endif
    40803744  m_pcRdCost->setCostScale ( 1 );
    4081 
    4082 #ifdef ROUNDING_CONTROL_BIPRED
    4083   if( bBi )
    4084   {
    4085     Bool bRound =  pcCU->getSlice()->isRounding() ;
    4086     xPatternSearchFracDIF_Bi( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost, pRefBufY, bRound );
    4087   }
    4088   else
    4089 #endif
    4090   {
    4091     xPatternSearchFracDIF( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost );
    4092   }
    4093 
    4094 
    4095 
     3745 
     3746  {
     3747    xPatternSearchFracDIF( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost
     3748                          ,bBi
     3749                          );
     3750  }
     3751 
     3752 
     3753 
    40963754  m_pcRdCost->setCostScale( 0 );
    40973755  rcMv <<= 2;
     
    41013759  }
    41023760#endif
    4103 
     3761 
    41043762  UInt uiMvBits = m_pcRdCost->getBits( rcMv.getHor(), rcMv.getVer() );
    41053763#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4106   if( pcCU->getSlice()->getSPS()->isDepth() )
     3764  if( pcCU->getSlice()->getIsDepth() )
    41073765    ruiCost += m_pcRdCost->getCost( uiMvBits );
    41083766#endif
    4109 
    4110   ruiBits   += uiMvBits;
     3767 
     3768  ruiBits      += uiMvBits;
    41113769  ruiCost       = (UInt)( floor( fWeight * ( (Double)ruiCost - (Double)m_pcRdCost->getCost( uiMvBits ) ) ) + (Double)m_pcRdCost->getCost( ruiBits ) );
    41123770}
     
    41173775  Int  iMvShift = 2;
    41183776#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4119   if( pcCU->getSlice()->getSPS()->isDepth() )
     3777  if( pcCU->getSlice()->getIsDepth() )
    41203778    iMvShift = 0;
    41213779#endif
    4122   pcCU->clipMv( cMvPred );
    4123 
    4124   rcMvSrchRngLT.setHor( cMvPred.getHor() - (iSrchRng << iMvShift) );
    4125   rcMvSrchRngLT.setVer( cMvPred.getVer() - (iSrchRng << iMvShift) );
    4126 
    4127   rcMvSrchRngRB.setHor( cMvPred.getHor() + (iSrchRng << iMvShift) );
    4128   rcMvSrchRngRB.setVer( cMvPred.getVer() + (iSrchRng << iMvShift) );
    4129 
     3780  TComMv cTmpMvPred = cMvPred;
     3781  pcCU->clipMv( cTmpMvPred );
     3782
     3783  rcMvSrchRngLT.setHor( cTmpMvPred.getHor() - (iSrchRng << iMvShift) );
     3784  rcMvSrchRngLT.setVer( cTmpMvPred.getVer() - (iSrchRng << iMvShift) );
     3785 
     3786  rcMvSrchRngRB.setHor( cTmpMvPred.getHor() + (iSrchRng << iMvShift) );
     3787  rcMvSrchRngRB.setVer( cTmpMvPred.getVer() + (iSrchRng << iMvShift) );
    41303788  pcCU->clipMv        ( rcMvSrchRngLT );
    41313789  pcCU->clipMv        ( rcMvSrchRngRB );
    4132 
     3790 
    41333791  rcMvSrchRngLT >>= iMvShift;
    41343792  rcMvSrchRngRB >>= iMvShift;
    41353793}
    4136 
    4137 #ifdef ROUNDING_CONTROL_BIPRED
    4138 Void TEncSearch::xPatternSearch_Bi( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD, Pel* pcRefY2, Bool bRound )
    4139 {
    4140   Int   iSrchRngHorLeft   = pcMvSrchRngLT->getHor();
    4141   Int   iSrchRngHorRight  = pcMvSrchRngRB->getHor();
    4142   Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    4143   Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    4144 
    4145   UInt  uiSad;
    4146   Dist  uiSadBest         = RDO_DIST_MAX;
    4147   Int   iBestX = 0;
    4148   Int   iBestY = 0;
    4149 
    4150   Pel*  piRefSrch;
    4151 
    4152   //-- jclee for using the SAD function pointer
    4153   m_pcRdCost->setDistParam_Bi( pcPatternKey, piRefY, iRefStride,  m_cDistParam);
    4154 
    4155   // fast encoder decision: use subsampled SAD for integer ME
    4156   if ( m_pcEncCfg->getUseFastEnc() )
    4157   {
    4158     if ( m_cDistParam.iRows > 8 )
    4159     {
    4160       m_cDistParam.iSubShift = 1;
    4161     }
    4162   }
    4163 
    4164   piRefY += (iSrchRngVerTop * iRefStride);
    4165   for ( Int y = iSrchRngVerTop; y <= iSrchRngVerBottom; y++ )
    4166   {
    4167     for ( Int x = iSrchRngHorLeft; x <= iSrchRngHorRight; x++ )
    4168     {
    4169       //  find min. distortion position
    4170       piRefSrch = piRefY + x;
    4171       m_cDistParam.pCur = piRefSrch;
    4172       uiSad = m_cDistParam.DistFuncRnd( &m_cDistParam, pcRefY2, bRound );
    4173 
    4174       // motion cost
    4175       uiSad += m_pcRdCost->getCost( x, y );
    4176 
    4177       // regularization cost
    4178       if( m_pcRdCost->useMultiviewReg() )
    4179       {
    4180         uiSad += m_pcRdCost->getMultiviewRegCost( x, y );
    4181       }
    4182 
    4183       if ( uiSad < uiSadBest )
    4184       {
    4185         uiSadBest = uiSad;
    4186         iBestX    = x;
    4187         iBestY    = y;
    4188       }
    4189     }
    4190     piRefY += iRefStride;
    4191   }
    4192 
    4193   rcMv.set( iBestX, iBestY );
    4194   ruiSAD = uiSadBest - m_pcRdCost->getCost( iBestX, iBestY );
    4195   return;
    4196 }
    4197 #endif
    41983794
    41993795Void TEncSearch::xPatternSearch( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD )
     
    42033799  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    42043800  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    4205 
     3801 
    42063802  UInt  uiSad;
    42073803  UInt  uiSadBest         = MAX_UINT;
    42083804  Int   iBestX = 0;
    42093805  Int   iBestY = 0;
    4210 
     3806 
    42113807  Pel*  piRefSrch;
    4212 
     3808 
    42133809  //-- jclee for using the SAD function pointer
    42143810  m_pcRdCost->setDistParam( pcPatternKey, piRefY, iRefStride,  m_cDistParam );
    4215 
     3811 
    42163812  // fast encoder decision: use subsampled SAD for integer ME
    42173813  if ( m_pcEncCfg->getUseFastEnc() )
     
    42223818    }
    42233819  }
    4224 
     3820 
    42253821  piRefY += (iSrchRngVerTop * iRefStride);
    42263822  for ( Int y = iSrchRngVerTop; y <= iSrchRngVerBottom; y++ )
     
    42313827      piRefSrch = piRefY + x;
    42323828      m_cDistParam.pCur = piRefSrch;
    4233 #ifdef WEIGHT_PRED
     3829
    42343830      setDistParamComp(0);
    4235 #endif
     3831
    42363832      uiSad = m_cDistParam.DistFunc( &m_cDistParam );
    4237 
     3833     
    42383834      // motion cost
    42393835      uiSad += m_pcRdCost->getCost( x, y );
    4240 
    4241       // regularization cost
    4242       if( m_pcRdCost->useMultiviewReg() )
    4243       {
    4244         uiSad += m_pcRdCost->getMultiviewRegCost( x, y );
    4245       }
    4246 
     3836     
    42473837      if ( uiSad < uiSadBest )
    42483838      {
     
    42543844    piRefY += iRefStride;
    42553845  }
    4256 
     3846 
    42573847  rcMv.set( iBestX, iBestY );
    4258 
     3848 
    42593849  ruiSAD = uiSadBest - m_pcRdCost->getCost( iBestX, iBestY );
    42603850  return;
     
    42663856  pcCU->getMvPredAbove      ( m_acMvPredictors[1] );
    42673857  pcCU->getMvPredAboveRight ( m_acMvPredictors[2] );
    4268 
     3858 
    42693859  switch ( m_iFastSearch )
    42703860  {
     
    42723862      xTZSearch( pcCU, pcPatternKey, piRefY, iRefStride, pcMvSrchRngLT, pcMvSrchRngRB, rcMv, ruiSAD );
    42733863      break;
    4274 
     3864     
    42753865    default:
    42763866      break;
     
    42843874  Int   iSrchRngVerTop    = pcMvSrchRngLT->getVer();
    42853875  Int   iSrchRngVerBottom = pcMvSrchRngRB->getVer();
    4286 
     3876 
    42873877  TZ_SEARCH_CONFIGURATION
    4288 
     3878 
    42893879  UInt uiSearchRange = m_iSearchRange;
    42903880  pcCU->clipMv( rcMv );
    42913881#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4292   if( ! pcCU->getSlice()->getSPS()->isDepth() )
     3882  if( ! pcCU->getSlice()->getIsDepth() )
    42933883#endif
    42943884  rcMv >>= 2;
     
    42983888  cStruct.piRefY      = piRefY;
    42993889  cStruct.uiBestSad   = MAX_UINT;
    4300 
     3890 
    43013891  // set rcMv (Median predictor) as start point and as best point
    43023892  xTZSearchHelp( pcPatternKey, cStruct, rcMv.getHor(), rcMv.getVer(), 0, 0 );
    4303 
     3893 
    43043894  // test whether one of PRED_A, PRED_B, PRED_C MV is better start point than Median predictor
    43053895  if ( bTestOtherPredictedMV )
     
    43103900      pcCU->clipMv( cMv );
    43113901#if HHI_FULL_PEL_DEPTH_MAP_MV_ACC
    4312       if( ! pcCU->getSlice()->getSPS()->isDepth() )
     3902      if( ! pcCU->getSlice()->getIsDepth() )
    43133903#endif
    43143904      cMv >>= 2;
     
    43163906    }
    43173907  }
    4318 
     3908 
    43193909  // test whether zero Mv is better start point than Median predictor
    43203910  if ( bTestZeroVector )
     
    43223912    xTZSearchHelp( pcPatternKey, cStruct, 0, 0, 0, 0 );
    43233913  }
    4324 
     3914 
    43253915  // start search
    43263916  Int  iDist = 0;
    43273917  Int  iStartX = cStruct.iBestX;
    43283918  Int  iStartY = cStruct.iBestY;
    4329 
     3919 
    43303920  // first search
    43313921  for ( iDist = 1; iDist <= (Int)uiSearchRange; iDist*=2 )
     
    43393929      xTZ8PointSquareSearch  ( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB, iStartX, iStartY, iDist );
    43403930    }
    4341 
     3931   
    43423932    if ( bFirstSearchStop && ( cStruct.uiBestRound >= uiFirstSearchRounds ) ) // stop criterion
    43433933    {
     
    43453935    }
    43463936  }
    4347 
     3937 
    43483938  // test whether zero Mv is a better start point than Median predictor
    43493939  if ( bTestZeroVectorStart && ((cStruct.iBestX != 0) || (cStruct.iBestY != 0)) )
     
    43633953    }
    43643954  }
    4365 
     3955 
    43663956  // calculate only 2 missing points instead 8 points if cStruct.uiBestDistance == 1
    43673957  if ( cStruct.uiBestDistance == 1 )
     
    43703960    xTZ2PointSearch( pcPatternKey, cStruct, pcMvSrchRngLT, pcMvSrchRngRB );
    43713961  }
    4372 
     3962 
    43733963  // raster search if distance is too big
    43743964  if ( bEnableRasterSearch && ( ((Int)(cStruct.uiBestDistance) > iRaster) || bAlwaysRasterSearch ) )
     
    43833973    }
    43843974  }
    4385 
     3975 
    43863976  // raster refinement
    43873977  if ( bRasterRefinementEnable && cStruct.uiBestDistance > 0 )
     
    44033993        }
    44043994      }
    4405 
     3995     
    44063996      // calculate only 2 missing points instead 8 points if cStruct.uiBestDistance == 1
    44073997      if ( cStruct.uiBestDistance == 1 )
     
    44154005    }
    44164006  }
    4417 
     4007 
    44184008  // start refinement
    44194009  if ( bStarRefinementEnable && cStruct.uiBestDistance > 0 )
     
    44404030        }
    44414031      }
    4442 
     4032     
    44434033      // calculate only 2 missing points instead 8 points if cStrukt.uiBestDistance == 1
    44444034      if ( cStruct.uiBestDistance == 1 )
     
    44524042    }
    44534043  }
    4454 
     4044 
    44554045  // write out best match
    44564046  rcMv.set( cStruct.iBestX, cStruct.iBestY );
     
    44584048}
    44594049
    4460 
    4461 #ifdef ROUNDING_CONTROL_BIPRED
    4462 Void TEncSearch::xPatternSearchFracDIF_Bi( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvInt, TComMv& rcMvHalf, TComMv& rcMvQter, UInt& ruiCost, Pel* piRefY2, Bool bRound )
     4050Void TEncSearch::xPatternSearchFracDIF(TComDataCU* pcCU,
     4051                                       TComPattern* pcPatternKey,
     4052                                       Pel* piRefY,
     4053                                       Int iRefStride,
     4054                                       TComMv* pcMvInt,
     4055                                       TComMv& rcMvHalf,
     4056                                       TComMv& rcMvQter,
     4057                                       UInt& ruiCost
     4058                                       ,Bool biPred
     4059                                       )
    44634060{
    44644061  //  Reference pattern initialization (integer scale)
     
    44724069                          iRefStride,
    44734070                          0, 0, 0, 0 );
    4474   Pel*  piRef;
    4475   iRefStride  = m_cYuvExt.getStride();
    4476 
     4071 
    44774072  //  Half-pel refinement
    4478   xExtDIFUpSamplingH ( &cPatternRoi, &m_cYuvExt );
    4479   piRef = m_cYuvExt.getLumaAddr() + ((iRefStride + 4) << 2);
    4480 
     4073  xExtDIFUpSamplingH ( &cPatternRoi, biPred );
     4074 
    44814075  rcMvHalf = *pcMvInt;   rcMvHalf <<= 1;    // for mv-cost
    4482   ruiCost = xPatternRefinement_Bi( pcPatternKey, piRef, iRefStride, 4, 2, rcMvHalf, piRefY2, bRound );
    4483 
     4076  TComMv baseRefMv(0, 0);
     4077  ruiCost = xPatternRefinement( pcPatternKey, baseRefMv, 2, rcMvHalf   );
     4078 
    44844079  m_pcRdCost->setCostScale( 0 );
    4485 
    4486   //  Quater-pel refinement
    4487   Pel*  piSrcPel = cPatternRoi.getROIY() + (rcMvHalf.getHor() >> 1) + cPatternRoi.getPatternLStride() * (rcMvHalf.getVer() >> 1);
    4488   Int*  piSrc    = m_piYuvExt  + ((m_iYuvExtStride + 4) << 2) + (rcMvHalf.getHor() << 1) + m_iYuvExtStride * (rcMvHalf.getVer() << 1);
    4489   piRef += (rcMvHalf.getHor() << 1) + iRefStride * (rcMvHalf.getVer() << 1);
    4490   xExtDIFUpSamplingQ ( pcPatternKey, piRef, iRefStride, piSrcPel, cPatternRoi.getPatternLStride(), piSrc, m_iYuvExtStride, m_puiDFilter[rcMvHalf.getHor()+rcMvHalf.getVer()*3] );
    4491 
     4080 
     4081  xExtDIFUpSamplingQ ( &cPatternRoi, rcMvHalf, biPred );
     4082  baseRefMv = rcMvHalf;
     4083  baseRefMv <<= 1;
     4084 
    44924085  rcMvQter = *pcMvInt;   rcMvQter <<= 1;    // for mv-cost
    44934086  rcMvQter += rcMvHalf;  rcMvQter <<= 1;
    4494   ruiCost = xPatternRefinement_Bi( pcPatternKey, piRef, iRefStride, 4, 1, rcMvQter, piRefY2, bRound );
    4495 }
    4496 
    4497 #endif
    4498 
    4499 Void TEncSearch::xPatternSearchFracDIF( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvInt, TComMv& rcMvHalf, TComMv& rcMvQter, UInt& ruiCost )
    4500 {
    4501   //  Reference pattern initialization (integer scale)
    4502   TComPattern cPatternRoi;
    4503   Int         iOffset    = pcMvInt->getHor() + pcMvInt->getVer() * iRefStride;
    4504   cPatternRoi.initPattern( piRefY +  iOffset,
    4505                           NULL,
    4506                           NULL,
    4507                           pcPatternKey->getROIYWidth(),
    4508                           pcPatternKey->getROIYHeight(),
    4509                           iRefStride,
    4510                           0, 0, 0, 0 );
    4511   Pel*  piRef;
    4512   iRefStride  = m_cYuvExt.getStride();
    4513 
    4514   //  Half-pel refinement
    4515   xExtDIFUpSamplingH ( &cPatternRoi, &m_cYuvExt );
    4516   piRef = m_cYuvExt.getLumaAddr() + ((iRefStride + 4) << 2);
    4517 
    4518   rcMvHalf = *pcMvInt;   rcMvHalf <<= 1;    // for mv-cost
    4519   ruiCost = xPatternRefinement( pcPatternKey, piRef, iRefStride, 4, 2, rcMvHalf   );
    4520 
    4521   m_pcRdCost->setCostScale( 0 );
    4522 
    4523   //  Quater-pel refinement
    4524   Pel*  piSrcPel = cPatternRoi.getROIY() + (rcMvHalf.getHor() >> 1) + cPatternRoi.getPatternLStride() * (rcMvHalf.getVer() >> 1);
    4525   Int*  piSrc    = m_piYuvExt  + ((m_iYuvExtStride + 4) << 2) + (rcMvHalf.getHor() << 1) + m_iYuvExtStride * (rcMvHalf.getVer() << 1);
    4526   piRef += (rcMvHalf.getHor() << 1) + iRefStride * (rcMvHalf.getVer() << 1);
    4527   xExtDIFUpSamplingQ ( pcPatternKey, piRef, iRefStride, piSrcPel, cPatternRoi.getPatternLStride(), piSrc, m_iYuvExtStride, m_puiDFilter[rcMvHalf.getHor()+rcMvHalf.getVer()*3] );
    4528 
    4529   rcMvQter = *pcMvInt;   rcMvQter <<= 1;    // for mv-cost
    4530   rcMvQter += rcMvHalf;  rcMvQter <<= 1;
    4531   ruiCost = xPatternRefinement( pcPatternKey, piRef, iRefStride, 4, 1, rcMvQter );
    4532 }
    4533 
    4534 Void TEncSearch::predInterSkipSearch( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
    4535 {
    4536   SliceType eSliceType = pcCU->getSlice()->getSliceType();
    4537   if ( eSliceType == I_SLICE )
    4538     return;
    4539 
    4540   if ( eSliceType == P_SLICE && pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 )
    4541   {
    4542     pcCU->setInterDirSubParts( 1, 0, 0, pcCU->getDepth( 0 ) );
    4543 
    4544     TComMv cMv;
    4545     TComMv cZeroMv;
    4546     xEstimateMvPredAMVP( pcCU, pcOrgYuv, 0, REF_PIC_LIST_0, 0, cMv, (pcCU->getCUMvField( REF_PIC_LIST_0 )->getAMVPInfo()->iN > 0?  true:false) );
    4547     pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMv, 0, SIZE_2Nx2N, 0, 0, 0 );
    4548     pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, 0, 0, 0 );  //unnecessary
    4549     pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cZeroMv, NOT_VALID, SIZE_2Nx2N, 0, 0, 0 );  //unnecessary
    4550     pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, 0, 0, 0 );  //unnecessary
    4551 
    4552     pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, 0, 0, pcCU->getDepth(0));
    4553     pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, 0, 0, pcCU->getDepth(0));
    4554 
    4555     //  Motion compensation
    4556     motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_0 );
    4557 
    4558   }
    4559   else if ( eSliceType == B_SLICE &&
    4560            pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 &&
    4561            pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0  )
    4562   {
    4563     TComMv cMv;
    4564     TComMv cZeroMv;
    4565 
    4566     if (pcCU->getInterDir(0)!=2)
    4567     {
    4568       xEstimateMvPredAMVP( pcCU, pcOrgYuv, 0, REF_PIC_LIST_0, 0, cMv, (pcCU->getCUMvField( REF_PIC_LIST_0 )->getAMVPInfo()->iN > 0?  true:false) );
    4569       pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMv, 0, SIZE_2Nx2N, 0, 0, 0 );
    4570       pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, 0, 0, 0 ); //unnecessary
    4571     }
    4572 
    4573     if (pcCU->getInterDir(0)!=1)
    4574     {
    4575       xEstimateMvPredAMVP( pcCU, pcOrgYuv, 0, REF_PIC_LIST_1, 0, cMv, (pcCU->getCUMvField( REF_PIC_LIST_1 )->getAMVPInfo()->iN > 0?  true:false) );
    4576       pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMv, 0, SIZE_2Nx2N, 0, 0, 0 );
    4577       pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvd    ( cZeroMv, SIZE_2Nx2N, 0, 0, 0 );  //unnecessary
    4578     }
    4579 
    4580     motionCompensation ( pcCU, rpcPredYuv );
    4581 
    4582   }
    4583   else
    4584   {
    4585     assert( 0 );
    4586   }
    4587 
    4588   return;
     4087  ruiCost = xPatternRefinement( pcPatternKey, baseRefMv, 1, rcMvQter );
    45894088}
    45904089
     
    45994098 * \returns Void
    46004099 */
     4100#if HHI_INTER_VIEW_RESIDUAL_PRED
    46014101Void TEncSearch::encodeResAndCalcRdInterCU( TComDataCU* pcCU, TComYuv* pcYuvOrg, TComYuv* pcYuvPred, TComYuv*& rpcYuvResi, TComYuv*& rpcYuvResiBest, TComYuv*& rpcYuvRec, TComYuv*& rpcYuvResPrd, Bool bSkipRes )
     4102#else
     4103Void TEncSearch::encodeResAndCalcRdInterCU( TComDataCU* pcCU, TComYuv* pcYuvOrg, TComYuv* pcYuvPred, TComYuv*& rpcYuvResi, TComYuv*& rpcYuvResiBest, TComYuv*& rpcYuvRec, Bool bSkipRes )
     4104#endif
    46024105{
    46034106  if ( pcCU->isIntra(0) )
     
    46054108    return;
    46064109  }
    4607 
     4110 
    46084111  PredMode  ePredMode    = pcCU->getPredictionMode( 0 );
    46094112  Bool      bHighPass    = pcCU->getSlice()->getDepth() ? true : false;
    46104113  UInt      uiBits       = 0, uiBitsBest = 0;
    46114114  Dist      uiDistortion = 0, uiDistortionBest = 0;
    4612 
     4115 
    46134116  UInt      uiWidth      = pcCU->getWidth ( 0 );
    46144117  UInt      uiHeight     = pcCU->getHeight( 0 );
    4615 
     4118 
    46164119  //  No residual coding : SKIP mode
    46174120  if ( ePredMode == MODE_SKIP && bSkipRes )
    46184121  {
    46194122    rpcYuvResi->clear();
    4620 
     4123   
    46214124    pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
    4622 
     4125   
    46234126#if HHI_INTER_VIEW_RESIDUAL_PRED
    46244127    // add residual prediction
     
    46344137    {
    46354138      uiDistortion = m_pcRdCost->getDistVS( pcCU, 0, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight     , false, 0 );
    4636     }
    4637     else
    4638 #endif
    4639     {
    4640       uiDistortion = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    4641       + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
    4642       + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
    4643     }
    4644    
     4139        }
     4140    else   
     4141    {
     4142#endif
     4143#if WEIGHTED_CHROMA_DISTORTION
     4144    uiDistortion = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4145    + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true )
     4146    + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true );
     4147#else
     4148    uiDistortion = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4149    + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
     4150    + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
     4151#endif
     4152#if HHI_VSO   
     4153    }
     4154#endif
    46454155
    46464156    if( m_bUseSBACRD )
    46474157      m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST]);
    4648 
     4158   
    46494159    m_pcEntropyCoder->resetBits();
    46504160#if HHI_MPI
     
    46534163#endif
    46544164    m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
    4655 #if HHI_MRG_SKIP
    46564165    m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, 0, true );
    4657 #else
    4658     if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_0 ) > 0 ) //if ( ref. frame list0 has at least 1 entry )
    4659     {
    4660       m_pcEntropyCoder->encodeMVPIdx( pcCU, 0, REF_PIC_LIST_0);
    4661     }
    4662     if ( pcCU->getSlice()->getNumRefIdx( REF_PIC_LIST_1 ) > 0 ) //if ( ref. frame list1 has at least 1 entry )
    4663     {
    4664       m_pcEntropyCoder->encodeMVPIdx( pcCU, 0, REF_PIC_LIST_1);
    4665     }
    4666 #endif
    46674166#if HHI_INTER_VIEW_RESIDUAL_PRED
    46684167    m_pcEntropyCoder->encodeResPredFlag( pcCU, 0, 0, true );
     
    46714170    }
    46724171#endif
    4673 
     4172   
    46744173    uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
    46754174    pcCU->getTotalBits()       = uiBits;
     
    46844183#endif
    46854184    {
    4686       pcCU->getTotalCost() = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
    4687     }
    4688 
     4185    pcCU->getTotalCost()      = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
     4186    }
     4187   
    46894188    if( m_bUseSBACRD )
    46904189      m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_TEMP_BEST]);
    4691 
     4190   
    46924191    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
    46934192    pcCU->setTrIdxSubParts( 0, 0, pcCU->getDepth(0) );
    4694 
    4695 #if HHI_VSO
     4193   
     4194#if HHI_VSO // necessary?
    46964195    // set Model
    46974196    if( m_pcRdCost->getUseRenModel() )
     
    47054204    return;
    47064205  }
    4707 
     4206 
    47084207  //  Residual coding.
     4208#if H0736_AVC_STYLE_QP_RANGE
     4209  Int    qp, qpBest = 0, qpMin, qpMax;
     4210#else
    47094211  UInt    uiQp, uiQpBest = 0, uiQpMin, uiQpMax;
     4212#endif
    47104213  Double  dCost, dCostBest = MAX_DOUBLE;
    4711 
    4712   UInt uiBestTrMode = 0;
    4713 
     4214 
    47144215  UInt uiTrLevel = 0;
    47154216  if( (pcCU->getWidth(0) > pcCU->getSlice()->getSPS()->getMaxTrSize()) )
     
    47184219  }
    47194220  UInt uiMaxTrMode = pcCU->getSlice()->getSPS()->getMaxTrDepth() + uiTrLevel;
    4720 
     4221 
    47214222  while((uiWidth>>uiMaxTrMode) < (g_uiMaxCUWidth>>g_uiMaxCUDepth)) uiMaxTrMode--;
    4722 
    4723   uiQpMin      = bHighPass ? Min( MAX_QP, Max( MIN_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) ) : pcCU->getQP( 0 );
    4724   uiQpMax      = bHighPass ? Min( MAX_QP, Max( MIN_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) ) : pcCU->getQP( 0 );
    4725 
    4726   pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
    4727 
     4223 
     4224#if H0736_AVC_STYLE_QP_RANGE
     4225  qpMin =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) : pcCU->getQP( 0 );
     4226  qpMax =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) : pcCU->getQP( 0 );
     4227#else
     4228  uiQpMin      = bHighPass ? min( MAX_QP, max( MIN_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) ) : pcCU->getQP( 0 );
     4229  uiQpMax      = bHighPass ? min( MAX_QP, max( MIN_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) ) : pcCU->getQP( 0 );
     4230#endif
     4231
     4232  #if HHI_INTERVIEW_SKIP
     4233  if( bSkipRes)
     4234  {
     4235    rpcYuvResi->clear() ;
     4236  }
     4237  else
     4238  {
     4239  rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
     4240#if HHI_INTER_VIEW_RESIDUAL_PRED
     4241    // subtract residual prediction
     4242    if( pcCU->getResPredFlag( 0 ) )
     4243    {
     4244      rpcYuvResi->subtract( rpcYuvResi, rpcYuvResPrd, 0, uiWidth );
     4245    }
     4246#endif
     4247  }
     4248#else
     4249  rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
    47284250#if HHI_INTER_VIEW_RESIDUAL_PRED
    47294251  // add residual prediction
    47304252  if( pcCU->getResPredFlag( 0 ) )
    47314253  {
    4732     rpcYuvRec->add( rpcYuvResPrd, uiWidth, uiHeight );
    4733   }
    4734 #endif
    4735 #if HHI_INTERVIEW_SKIP
    4736   if( bSkipRes)
    4737   {
    4738     rpcYuvResi->clear() ;
    4739   }
    4740   else
    4741   {
    4742     rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
    4743   }
    4744 #else
    4745   rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
    4746 #endif
    4747 
     4254    rpcYuvResi->subtract( rpcYuvResi, rpcYuvResPrd, uiWidth, uiHeight );
     4255  }
     4256#endif
     4257#endif
     4258
     4259#if H0736_AVC_STYLE_QP_RANGE
     4260  for ( qp = qpMin; qp <= qpMax; qp++ )
     4261#else
    47484262  for ( uiQp = uiQpMin; uiQp <= uiQpMax; uiQp++ )
    4749   {
    4750     pcCU->setQPSubParts( uiQp, 0, pcCU->getDepth(0) );
     4263#endif
     4264  {
    47514265    dCost = 0.;
    47524266    uiBits = 0;
     
    47564270      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_CURR_BEST ] );
    47574271    }
    4758 
     4272   
    47594273    Dist uiZeroDistortion = 0;
    47604274#if HHI_VSO
     
    47644278    }
    47654279#endif
    4766 
    4767     xEstimateResidualQT( pcCU, 0, 0, pcYuvOrg, rpcYuvRec, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
     4280#if IBDI_DISTORTION || HHI_VSO
     4281    xEstimateResidualQT( pcCU, 0, 0, 0, pcYuvOrg, pcYuvPred, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
     4282#else
     4283    xEstimateResidualQT( pcCU, 0, 0, 0, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
     4284#endif
     4285   
    47684286
    47694287#if HHI_VSO
     
    47734291    }
    47744292#endif
    4775 
    47764293
    47774294    double dZeroCost;
     
    47874304    }
    47884305
     4306#if LOSSLESS_CODING
     4307    if(pcCU->isLosslessCoded( 0 ))
     4308    { 
     4309      dZeroCost = dCost + 1;
     4310    }
     4311#endif
    47894312    if ( dZeroCost < dCost )
    47904313    {
     
    47924315      uiBits       = 0;
    47934316      uiDistortion = uiZeroDistortion;
    4794 
     4317     
    47954318      const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
    47964319      ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
     
    48044327    else
    48054328    {
    4806       xSetResidualQTData( pcCU, 0, NULL, pcCU->getDepth(0), false );
    4807     }
    4808 
     4329      xSetResidualQTData( pcCU, 0, 0, 0, NULL, pcCU->getDepth(0), false );
     4330    }
     4331   
    48094332    if( m_bUseSBACRD )
    48104333    {
     
    48294352      xAddSymbolBitsInter( pcCU, 0, 0, uiBits, pDummy, NULL, pDummy );
    48304353    }
    4831 
     4354   
    48324355    Double dExactCost;
    48334356#if HHI_VSO
     
    48414364      dExactCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
    48424365    }
    4843 
     4366   
    48444367    dCost = dExactCost;
    4845 
     4368   
    48464369    if ( dCost < dCostBest )
    48474370    {
     
    48524375      else
    48534376      {
    4854         xSetResidualQTData( pcCU, 0, rpcYuvResiBest, pcCU->getDepth(0), true );
    4855       }
    4856 
     4377        xSetResidualQTData( pcCU, 0, 0, 0, rpcYuvResiBest, pcCU->getDepth(0), true );
     4378      }
     4379     
     4380#if H0736_AVC_STYLE_QP_RANGE
     4381      if( qpMin != qpMax && qp != qpMax )
     4382#else
    48574383      if( uiQpMin != uiQpMax && uiQp != uiQpMax )
     4384#endif
    48584385      {
    48594386        const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
     
    48654392        ::memcpy( m_pcQTTempCoeffCb, pcCU->getCoeffCb(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    48664393        ::memcpy( m_pcQTTempCoeffCr, pcCU->getCoeffCr(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4394#if ADAPTIVE_QP_SELECTION
     4395        ::memcpy( m_pcQTTempArlCoeffY,  pcCU->getArlCoeffY(),  uiWidth * uiHeight * sizeof( Int )      );
     4396        ::memcpy( m_pcQTTempArlCoeffCb, pcCU->getArlCoeffCb(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4397        ::memcpy( m_pcQTTempArlCoeffCr, pcCU->getArlCoeffCr(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4398#endif
    48674399      }
    48684400      uiBitsBest       = uiBits;
    48694401      uiDistortionBest = uiDistortion;
    48704402      dCostBest        = dCost;
     4403#if H0736_AVC_STYLE_QP_RANGE
     4404      qpBest           = qp;
     4405#else
    48714406      uiQpBest         = uiQp;
    4872 
     4407#endif     
    48734408      if( m_bUseSBACRD )
    48744409      {
     
    48784413
    48794414#if HHI_VSO
    4880     // reset Model
     4415    // GT: reset Model, only fordQP necessary??
    48814416    if( m_pcRdCost->getUseRenModel() )
    48824417    {
     
    48874422#endif
    48884423  }
    4889 
     4424 
    48904425  assert ( dCostBest != MAX_DOUBLE );
    4891 
     4426 
     4427#if H0736_AVC_STYLE_QP_RANGE
     4428  if( qpMin != qpMax && qpBest != qpMax )
     4429#else
    48924430  if( uiQpMin != uiQpMax && uiQpBest != uiQpMax )
     4431#endif
    48934432  {
    48944433    if( m_bUseSBACRD )
     
    49064445    ::memcpy( pcCU->getCoeffCb(), m_pcQTTempCoeffCb, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    49074446    ::memcpy( pcCU->getCoeffCr(), m_pcQTTempCoeffCr, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    4908   }
    4909   rpcYuvRec->addClip ( rpcYuvRec, rpcYuvResiBest, 0, uiWidth );
    4910 
     4447#if ADAPTIVE_QP_SELECTION
     4448    ::memcpy( pcCU->getArlCoeffY(),  m_pcQTTempArlCoeffY,  uiWidth * uiHeight * sizeof( Int )      );
     4449    ::memcpy( pcCU->getArlCoeffCb(), m_pcQTTempArlCoeffCb, uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4450    ::memcpy( pcCU->getArlCoeffCr(), m_pcQTTempArlCoeffCr, uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4451#endif
     4452  }
     4453#if HHI_INTER_VIEW_RESIDUAL_PRED
     4454  // add residual prediction
     4455  if( pcCU->getResPredFlag( 0 ) )
     4456  {
     4457    pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
     4458    rpcYuvRec->add( rpcYuvResPrd,   uiWidth, uiHeight );
     4459    rpcYuvRec->add( rpcYuvResiBest, uiWidth, uiHeight );
     4460    rpcYuvRec->clip( uiWidth, uiHeight );
     4461  }
     4462  else
     4463#endif
     4464  rpcYuvRec->addClip ( pcYuvPred, rpcYuvResiBest, 0, uiWidth );
     4465 
    49114466  // update with clipped distortion and cost (qp estimation loop uses unclipped values)
    49124467
    4913 #if HHI_VSO
     4468#if HHI_VSO // GT: might be removed since VSO already provided clipped distortion
    49144469  if ( m_pcRdCost->getUseVSO() )
    49154470  {
     
    49184473  else
    49194474#endif
    4920   {
    4921     uiDistortionBest = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    4922     + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
    4923     + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
    4924   }
    4925 
     4475{
     4476#if WEIGHTED_CHROMA_DISTORTION
     4477  uiDistortionBest = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4478  + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true )
     4479  + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true );
     4480#else
     4481  uiDistortionBest = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4482  + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
     4483  + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
     4484#endif
     4485}
    49264486#if HHI_VSO
    49274487  if ( m_pcRdCost->getUseLambdaScaleVSO() )
     
    49324492#endif
    49334493  {
    4934     dCostBest = m_pcRdCost->calcRdCost( uiBitsBest, uiDistortionBest );
    4935   }
    4936 
     4494  dCostBest = m_pcRdCost->calcRdCost( uiBitsBest, uiDistortionBest );
     4495  }
     4496 
    49374497  pcCU->getTotalBits()       = uiBitsBest;
    49384498  pcCU->getTotalDistortion() = uiDistortionBest;
    49394499  pcCU->getTotalCost()       = dCostBest;
    4940 
     4500 
    49414501  if ( pcCU->isSkipped(0) )
    49424502  {
    4943     uiBestTrMode = 0;
    49444503    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
    49454504  }
    4946 
     4505 
     4506#if H0736_AVC_STYLE_QP_RANGE
     4507  pcCU->setQPSubParts( qpBest, 0, pcCU->getDepth(0) );
     4508#else
    49474509  pcCU->setQPSubParts( uiQpBest, 0, pcCU->getDepth(0) );
     4510#endif
    49484511
    49494512  // set Model
    4950 #if HHI_VSO
     4513#if HHI_VSO // necessary??
    49514514  if( m_pcRdCost->getUseRenModel() )
    49524515  {
     
    49544517    UInt  uiSrcStride = rpcYuvRec->getStride();
    49554518    m_pcRdCost->setRenModelData( pcCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
    4956   }
    4957 #endif
    49584519}
    4959 
    4960 Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, TComYuv* pcOrg, TComYuv* pcPred, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, Dist &ruiDist, Dist *puiZeroDist )
     4520#endif
     4521
     4522}
     4523
     4524#if IBDI_DISTORTION || HHI_VSO
     4525Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcOrg, TComYuv* pcPred, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, Dist &ruiDist, Dist *puiZeroDist )
     4526#else
     4527Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, Dist &ruiDist, Dist *puiZeroDist )
     4528#endif
    49614529{
    49624530  const UInt uiTrMode = uiDepth - pcCU->getDepth( 0 );
    4963 
     4531 
    49644532  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
    49654533  const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
    4966 
    4967 #if HHI_RQT_FORCE_SPLIT_ACC2_PU
    4968 #if HHI_RQT_FORCE_SPLIT_NxN
    4969   const Bool bNxNOK = pcCU->getPartitionSize( 0 ) == SIZE_NxN && uiTrMode > 0;
    4970 #else
    4971   const Bool bNxNOK = pcCU->getPartitionSize( 0 ) == SIZE_NxN;
    4972 #endif
    4973 #if HHI_RQT_FORCE_SPLIT_RECT
    4974   const Bool bSymmetricOK  = pcCU->getPartitionSize( 0 ) >= SIZE_2NxN  && pcCU->getPartitionSize( 0 ) < SIZE_NxN   && uiTrMode > 0;
    4975 #else
    4976   const Bool bSymmetricOK  = pcCU->getPartitionSize( 0 ) >= SIZE_2NxN  && pcCU->getPartitionSize( 0 ) < SIZE_NxN;
    4977 #endif
    4978   const Bool bNoForceSplit = pcCU->getPartitionSize( 0 ) == SIZE_2Nx2N || bNxNOK || bSymmetricOK;
    4979   const Bool bCheckFull    = bNoForceSplit && ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
    4980 #else
    4981   const Bool bCheckFull    = ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
    4982 #endif
     4534 
     4535#if G519_TU_AMP_NSQT_HARMONIZATION
     4536  UInt SplitFlag = ((pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && ( pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N ));
     4537#else
     4538  UInt SplitFlag = ((pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && ( pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_2NxN || pcCU->getPartitionSize(uiAbsPartIdx) == SIZE_Nx2N));
     4539#endif
     4540  Bool bCheckFull;
     4541  if ( SplitFlag && uiDepth == pcCU->getDepth(uiAbsPartIdx) && ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) )
     4542     bCheckFull = false;
     4543  else
     4544     bCheckFull =  ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
    49834545
    49844546  const Bool bCheckSplit  = ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
    4985 
     4547 
    49864548  assert( bCheckFull || bCheckSplit );
    4987 
     4549 
    49884550  Bool  bCodeChroma   = true;
    49894551  UInt  uiTrModeC     = uiTrMode;
    49904552  UInt  uiLog2TrSizeC = uiLog2TrSize-1;
    4991   if( uiLog2TrSize == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() )
     4553  if( uiLog2TrSize == 2 )
    49924554  {
    49934555    uiLog2TrSizeC++;
     
    49964558    bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
    49974559  }
    4998 
     4560 
    49994561  const UInt uiSetCbf = 1 << uiTrMode;
    50004562  // code full block
     
    50034565  Dist uiSingleDist = 0;
    50044566  UInt uiAbsSumY = 0, uiAbsSumU = 0, uiAbsSumV = 0;
    5005 
     4567 
    50064568  if( m_bUseSBACRD )
    50074569  {
    50084570    m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    50094571  }
    5010 
     4572 
    50114573  if( bCheckFull )
    50124574  {
     
    50164578    TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    50174579    TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    5018 
     4580#if ADAPTIVE_QP_SELECTION   
     4581    Int *pcArlCoeffCurrY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     4582    Int *pcArlCoeffCurrU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     4583    Int *pcArlCoeffCurrV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);   
     4584#endif
     4585   
     4586    Int trWidth = 0, trHeight = 0, trWidthC = 0, trHeightC = 0;
     4587    UInt absTUPartIdxC = uiAbsPartIdx;
     4588
     4589    trWidth  = trHeight  = 1 << uiLog2TrSize;
     4590    trWidthC = trHeightC = 1 <<uiLog2TrSizeC;
     4591    pcCU->getNSQTSize ( uiTrMode, uiAbsPartIdx, trWidth, trHeight );
     4592    pcCU->getNSQTSize ( uiTrModeC, uiAbsPartIdx, trWidthC, trHeightC );
     4593
     4594    if( bCodeChroma && pcCU->useNonSquareTrans( uiTrMode, uiAbsPartIdx ) && !( uiLog2TrSizeC  == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() && uiTrModeC == 1 ) )
     4595    { 
     4596      absTUPartIdxC = pcCU->getNSAddrChroma( uiLog2TrSizeC, uiTrModeC, uiQuadrant, absTUPartIdx );
     4597    }
    50194598    pcCU->setTrIdxSubParts( uiDepth - pcCU->getDepth( 0 ), uiAbsPartIdx, uiDepth );
    50204599    if (m_pcEncCfg->getUseRDOQ())
    50214600    {
    5022       m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, 1<< uiLog2TrSize, TEXT_LUMA );
    5023     }
    5024     m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA );
    5025     m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( uiAbsPartIdx ), pcResi->getStride (), pcCoeffCurrY, 1<< uiLog2TrSize,    1<< uiLog2TrSize,    uiAbsSumY, TEXT_LUMA,     uiAbsPartIdx );
    5026 
     4601      m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
     4602    }
     4603
     4604#if H0736_AVC_STYLE_QP_RANGE
     4605    m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     4606#else
     4607    m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA, 0 );
     4608#endif
     4609
     4610#if RDOQ_CHROMA_LAMBDA
     4611    m_pcTrQuant->selectLambda(TEXT_LUMA); 
     4612#endif
     4613    m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY,
     4614#if ADAPTIVE_QP_SELECTION
     4615                                 pcArlCoeffCurrY,
     4616#endif     
     4617                                 trWidth,   trHeight,    uiAbsSumY, TEXT_LUMA,     uiAbsPartIdx );
     4618   
    50274619    pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5028 
     4620   
    50294621    if( bCodeChroma )
    50304622    {
    50314623      if (m_pcEncCfg->getUseRDOQ())
    50324624      {
    5033         m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, 1<<uiLog2TrSizeC, TEXT_CHROMA );
    5034       }
    5035       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA );
    5036       m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr( uiAbsPartIdx ), pcResi->getCStride(), pcCoeffCurrU, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC, uiAbsSumU, TEXT_CHROMA_U, uiAbsPartIdx );
    5037       m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr( uiAbsPartIdx ), pcResi->getCStride(), pcCoeffCurrV, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC, uiAbsSumV, TEXT_CHROMA_V, uiAbsPartIdx );
     4625        m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
     4626      }
     4627
     4628#if H0736_AVC_STYLE_QP_RANGE
     4629      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     4630#else
     4631      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     4632#endif
     4633
     4634#if RDOQ_CHROMA_LAMBDA
     4635      m_pcTrQuant->selectLambda(TEXT_CHROMA);
     4636#endif
     4637
     4638      m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU,
     4639#if ADAPTIVE_QP_SELECTION
     4640                                 pcArlCoeffCurrU,
     4641#endif       
     4642                                 trWidthC, trHeightC, uiAbsSumU, TEXT_CHROMA_U, uiAbsPartIdx );
     4643#if H0736_AVC_STYLE_QP_RANGE
     4644      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaQpOffset2nd() );
     4645#else
     4646      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getPPS()->getChromaQpOffset2nd() );
     4647#endif
     4648      m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV,
     4649#if ADAPTIVE_QP_SELECTION
     4650                                 pcArlCoeffCurrV,
     4651#endif       
     4652                                 trWidthC, trHeightC, uiAbsSumV, TEXT_CHROMA_V, uiAbsPartIdx );
     4653
    50384654      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    50394655      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    50404656    }
    5041 
     4657   
    50424658    m_pcEntropyCoder->resetBits();
    5043 
    5044     if (pcCU->getSlice()->getSymbolMode())
     4659   
     4660    {
    50454661      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    5046 
    5047     m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, 1<< uiLog2TrSize,    1<< uiLog2TrSize,    uiDepth, TEXT_LUMA,     false );
     4662    }
     4663   
     4664    m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx,  trWidth,  trHeight,    uiDepth, TEXT_LUMA );
    50484665    const UInt uiSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
    5049 
     4666   
    50504667    UInt uiSingleBitsU = 0;
    50514668    UInt uiSingleBitsV = 0;
    50524669    if( bCodeChroma )
    50534670    {
    5054       if (pcCU->getSlice()->getSymbolMode())
     4671      {
    50554672        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
    5056       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC, uiDepth, TEXT_CHROMA_U, false );
     4673      }
     4674      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
    50574675      uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsY;
    5058 
    5059       if (pcCU->getSlice()->getSymbolMode())
     4676     
     4677      {
    50604678        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
    5061       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC, uiDepth, TEXT_CHROMA_V, false );
     4679      }
     4680      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
    50624681      uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - ( uiSingleBitsY + uiSingleBitsU );
    50634682    }
    5064 
     4683   
    50654684    const UInt uiNumSamplesLuma = 1 << (uiLog2TrSize<<1);
    50664685    const UInt uiNumSamplesChro = 1 << (uiLog2TrSizeC<<1);
    5067 
     4686   
    50684687    Dist uiDistY;
    50694688
     
    50724691    // GT Fix: Not necessary for VSO, however used for chroma later, irrelevant except from valgrind error message
    50734692    ::memset( m_pTempPel, 0, sizeof( Pel ) * uiNumSamplesLuma ); // not necessary needed for inside of recursion (only at the beginning)
    5074 
     4693   
    50754694#if HHI_VSO
    50764695    if ( m_pcRdCost->getUseVSO() )
     
    50824701    {
    50834702#if IBDI_DISTORTION
    5084       uiDistY = m_pcRdCost->getDistPart( pcPred->getLumaAddr( uiAbsPartIdx ), pcPred->getStride(), pcOrg->getLumaAddr( uiAbsPartIdx ), pcOrg->getStride(), 1<< uiLog2TrSize, 1<< uiLog2TrSize);
    5085 #else
    5086       uiDistY = m_pcRdCost->getDistPart( m_pTempPel, 1<< uiLog2TrSize, pcResi->getLumaAddr( uiAbsPartIdx ), pcResi->getStride(), 1<< uiLog2TrSize, 1<< uiLog2TrSize ); // initialized with zero residual distortion
     4703     uiDistY = m_pcRdCost->getDistPart( pcPred->getLumaAddr( absTUPartIdx ), pcPred->getStride(), pcOrg->getLumaAddr( absTUPartIdx), pcOrg->getStride(), trWidth, trHeight);
     4704#else
     4705     uiDistY = m_pcRdCost->getDistPart( m_pTempPel, trWidth, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight ); // initialized with zero residual destortion
    50874706#endif
    50884707    }
     
    50954714    if( uiAbsSumY )
    50964715    {
    5097       Pel *pcResiCurrY = m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( uiAbsPartIdx );
    5098       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA );
    5099 #if INTRA_DST_TYPE_7 // Inside Inter Encoder Search. So use conventional DCT.
    5100     m_pcTrQuant->invtransformNxN( TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, 1<< uiLog2TrSize,    1<< uiLog2TrSize );//this is for inter mode only
    5101 #else
    5102     m_pcTrQuant->invtransformNxN( pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, 1<< uiLog2TrSize,    1<< uiLog2TrSize );
    5103 #endif
    5104 
     4716      Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
     4717
     4718#if H0736_AVC_STYLE_QP_RANGE
     4719      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     4720#else
     4721      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_LUMA, 0 );
     4722#endif
     4723
     4724      Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
     4725      assert(scalingListType < 6);     
     4726#if LOSSLESS_CODING
     4727      m_pcTrQuant->invtransformNxN( pcCU, TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
     4728#else     
     4729      m_pcTrQuant->invtransformNxN( TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
     4730#endif
     4731     
    51054732      Dist uiNonzeroDistY;
    51064733
     
    51084735      if ( m_pcRdCost->getUseVSO() )
    51094736      {
     4737        static int iCount = 1;
     4738        iCount++;
    51104739        m_cYuvRecTemp.addClipPartLuma( &m_pcQTTempTComYuv[uiQTTempAccessLayer], pcPred, uiAbsPartIdx, 1<< uiLog2TrSize  );
    51114740        uiNonzeroDistY = m_pcRdCost->getDistVS( pcCU, uiAbsPartIdx, m_cYuvRecTemp.getLumaAddr(uiAbsPartIdx), m_cYuvRecTemp.getStride(),
     
    51154744#endif
    51164745      {
    5117         uiNonzeroDistY = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( uiAbsPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
    5118                                                   pcResi->getLumaAddr( uiAbsPartIdx ), pcResi->getStride(), 1<< uiLog2TrSize,    1<< uiLog2TrSize );
    5119       }
    5120 
     4746        uiNonzeroDistY = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
     4747          pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth,trHeight );
     4748      }
     4749
     4750#if LOSSLESS_CODING
     4751      if (pcCU->isLosslessCoded(0))
     4752      {
     4753        uiDistY = uiNonzeroDistY;
     4754      }
     4755      else
     4756      {
     4757
     4758      Double singleCostY;
     4759      Double nullCostY;
     4760
     4761#if HHI_VSO     
     4762      if ( m_pcRdCost->getUseLambdaScaleVSO())
     4763      {
     4764        singleCostY = m_pcRdCost->calcRdCostVSO( uiSingleBitsY, uiNonzeroDistY );
     4765        nullCostY   = m_pcRdCost->calcRdCostVSO( 0, uiDistY );
     4766      }
     4767      else
     4768#endif
     4769      {
     4770        singleCostY = m_pcRdCost->calcRdCost( uiSingleBitsY, uiNonzeroDistY );
     4771        nullCostY   = m_pcRdCost->calcRdCost( 0, uiDistY );
     4772      }
     4773        if( nullCostY < singleCostY ) 
     4774        {   
     4775          uiAbsSumY = 0;
     4776          ::memset( pcCoeffCurrY, 0, sizeof( TCoeff ) * uiNumSamplesLuma );
     4777        }
     4778        else
     4779        {
     4780          uiDistY = uiNonzeroDistY;
     4781        }
     4782      }
     4783#else
    51214784      Double dSingleCostY;
    51224785      Double dNullCostY;
     
    51344797        dNullCostY   = m_pcRdCost->calcRdCost( 0, uiDistY );
    51354798      }
    5136 
    5137 
    51384799      if( dNullCostY < dSingleCostY )
    51394800      {
     
    51454806        uiDistY = uiNonzeroDistY;
    51464807      }
    5147     }
    5148 
     4808#endif
     4809    }
     4810   
    51494811    if( !uiAbsSumY )
    51504812    {
    5151       Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( uiAbsPartIdx );
     4813      Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx );
    51524814      const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride();
    5153       for( UInt uiY = 0; uiY < 1<< uiLog2TrSize; ++uiY )
    5154       {
    5155         ::memset( pcPtr, 0, sizeof(Pel) << uiLog2TrSize );
     4815      for( UInt uiY = 0; uiY < trHeight; ++uiY )
     4816      {
     4817        ::memset( pcPtr, 0, sizeof( Pel ) * trWidth );
    51564818        pcPtr += uiStride;
    5157       }
    5158     }
    5159 
    5160     Dist uiDistU = 0;
    5161     Dist uiDistV = 0;
     4819      } 
     4820    }
     4821   
     4822    UInt uiDistU = 0;
     4823    UInt uiDistV = 0;
    51624824    if( bCodeChroma )
    51634825    {
    51644826#if IBDI_DISTORTION
    5165       uiDistU = m_pcRdCost->getDistPart( pcPred->getCbAddr( uiAbsPartIdx ), pcPred->getCStride(), pcOrg->getCbAddr( uiAbsPartIdx ), pcOrg->getCStride(), 1<< uiLog2TrSizeC, 1<< uiLog2TrSizeC);
    5166 #else
    5167       uiDistU = m_pcRdCost->getDistPart( m_pTempPel, 1<<uiLog2TrSizeC, pcResi->getCbAddr( uiAbsPartIdx ), pcResi->getCStride(), 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC ); // initialized with zero residual destortion
     4827      uiDistU = m_pcRdCost->getDistPart( pcPred->getCbAddr( absTUPartIdxC ), pcPred->getCStride(), pcOrg->getCbAddr( absTUPartIdxC ), pcOrg->getCStride(), trWidthC, trHeightC
     4828#if WEIGHTED_CHROMA_DISTORTION
     4829                                          , true
     4830#endif
     4831                                          );
     4832#else
     4833      uiDistU = m_pcRdCost->getDistPart( m_pTempPel, trWidthC, pcResi->getCbAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
     4834#if WEIGHTED_CHROMA_DISTORTION
     4835                                          , true
     4836#endif
     4837                                          ); // initialized with zero residual destortion
    51684838#endif
    51694839      if ( puiZeroDist )
     
    51734843      if( uiAbsSumU )
    51744844      {
    5175         Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( uiAbsPartIdx );
    5176         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA );
    5177 #if INTRA_DST_TYPE_7  // Inside Inter Encoder Search. So use conventional DCT.
    5178         m_pcTrQuant->invtransformNxN( TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC);
    5179 #else
    5180         m_pcTrQuant->invtransformNxN( pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC );
    5181 #endif
    5182         const UInt uiNonzeroDistU = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( uiAbsPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5183                                                             pcResi->getCbAddr( uiAbsPartIdx ), pcResi->getCStride(), 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC );
     4845        Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
     4846
     4847#if H0736_AVC_STYLE_QP_RANGE
     4848        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     4849#else
     4850        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA, pcCU->getSlice()->getPPS()->getChromaQpOffset() );
     4851#endif
     4852
     4853        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
     4854        assert(scalingListType < 6);
     4855#if LOSSLESS_CODING
     4856        m_pcTrQuant->invtransformNxN( pcCU, TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType  );
     4857#else
     4858        m_pcTrQuant->invtransformNxN( TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType );
     4859#endif       
     4860       
     4861        const UInt uiNonzeroDistU = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
     4862          pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
     4863#if WEIGHTED_CHROMA_DISTORTION
     4864          , true
     4865#endif
     4866          );
     4867
     4868#if LOSSLESS_CODING
     4869        if(pcCU->isLosslessCoded(0)) 
     4870        {
     4871          uiDistU = uiNonzeroDistU;
     4872        }
     4873        else
     4874        {
     4875          const Double dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
     4876          const Double dNullCostU   = m_pcRdCost->calcRdCost( 0, uiDistU );
     4877          if( dNullCostU < dSingleCostU )
     4878          {
     4879            uiAbsSumU = 0;
     4880            ::memset( pcCoeffCurrU, 0, sizeof( TCoeff ) * uiNumSamplesChro );
     4881          }
     4882          else
     4883          {
     4884            uiDistU = uiNonzeroDistU;
     4885          }
     4886        }
     4887#else
    51844888        const Double dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
    51854889        const Double dNullCostU   = m_pcRdCost->calcRdCost( 0, uiDistU );
     
    51934897          uiDistU = uiNonzeroDistU;
    51944898        }
     4899#endif
    51954900      }
    51964901      if( !uiAbsSumU )
    51974902      {
    5198         Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( uiAbsPartIdx );
    5199         const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
    5200         for( UInt uiY = 0; uiY < 1<<uiLog2TrSizeC; ++uiY )
    5201         {
    5202           ::memset( pcPtr, 0, sizeof(Pel) << uiLog2TrSizeC );
     4903        Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
     4904          const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
     4905        for( UInt uiY = 0; uiY < trHeightC; ++uiY )
     4906        {
     4907          ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
    52034908          pcPtr += uiStride;
    52044909        }
    52054910      }
    5206 
     4911     
    52074912#if IBDI_DISTORTION
    5208       uiDistV = m_pcRdCost->getDistPart( pcPred->getCrAddr( uiAbsPartIdx ), pcPred->getCStride(), pcOrg->getCrAddr( uiAbsPartIdx ), pcOrg->getCStride(), 1<< uiLog2TrSizeC, 1<< uiLog2TrSizeC);
    5209 #else
    5210       uiDistV = m_pcRdCost->getDistPart( m_pTempPel, 1<<uiLog2TrSizeC, pcResi->getCrAddr( uiAbsPartIdx ), pcResi->getCStride(), 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC ); // initialized with zero residual destortion
     4913      uiDistV = m_pcRdCost->getDistPart( pcPred->getCrAddr( absTUPartIdxC ), pcPred->getCStride(), pcOrg->getCrAddr( absTUPartIdxC ), pcOrg->getCStride(), trWidthC, trHeightC
     4914#if WEIGHTED_CHROMA_DISTORTION
     4915                                          , true
     4916#endif
     4917                                          );
     4918#else
     4919      uiDistV = m_pcRdCost->getDistPart( m_pTempPel, trWidthC, pcResi->getCrAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
     4920#if WEIGHTED_CHROMA_DISTORTION
     4921                                          , true
     4922#endif
     4923                                          ); // initialized with zero residual destortion
    52114924#endif
    52124925      if ( puiZeroDist )
     
    52164929      if( uiAbsSumV )
    52174930      {
    5218         Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr  ( uiAbsPartIdx );
     4931        Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
    52194932        if( !uiAbsSumU )
    52204933        {
    5221           m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), false, pcCU->getSlice()->getSliceType(), TEXT_CHROMA );
    5222         }
    5223 #if INTRA_DST_TYPE_7   // Inside Inter Encoder Search. So use conventional DCT.
    5224         m_pcTrQuant->invtransformNxN( TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC );
    5225 #else
    5226         m_pcTrQuant->invtransformNxN( pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC );
    5227 #endif
    5228         const UInt uiNonzeroDistV = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( uiAbsPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5229                                                             pcResi->getCrAddr( uiAbsPartIdx ), pcResi->getCStride(), 1<<uiLog2TrSizeC, 1<<uiLog2TrSizeC );
     4934#if H0736_AVC_STYLE_QP_RANGE