Changeset 125 in SHVCSoftware for trunk/source/Lib/TLibEncoder/TEncSearch.cpp


Ignore:
Timestamp:
16 Apr 2013, 06:39:31 (12 years ago)
Author:
seregin
Message:

copy from HM-10.0-dev-SHM

File:
1 edited

Legend:

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

    r55 r125  
    44 * granted under this license. 
    55 *
    6  * Copyright (c) 2010-2012, ITU/ISO/IEC
     6 * Copyright (c) 2010-2013, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    333333
    334334  // distortion
     335  m_cDistParam.bitDepth = g_bitDepthY;
    335336  uiSad = m_cDistParam.DistFunc( &m_cDistParam );
    336337 
     
    745746
    746747    m_cDistParam.pCur = piRefPos;
     748    m_cDistParam.bitDepth = g_bitDepthY;
    747749    uiDist = m_cDistParam.DistFunc( &m_cDistParam );
    748750    uiDist += m_pcRdCost->getCost( cMvTest.getHor(), cMvTest.getVer() );
     
    794796      if( bLuma )
    795797      {
    796 #if TRANS_SPLIT_FLAG_CTX_REDUCTION
    797798        m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, 5 - uiLog2TrafoSize );
    798 #else
    799         m_pcEntropyCoder->encodeTransformSubdivFlag( uiSubdiv, uiFullDepth );
    800 #endif
    801799      }
    802800    }
     
    10641062    else
    10651063#endif
    1066     {
    1067       predIntraLumaAng( pcCU->getPattern(), uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    1068     }
     1064    predIntraLumaAng( pcCU->getPattern(), uiLumaPredMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
    10691065    // save prediction
    10701066    if(default0Save1Load2 == 1)
     
    11181114  //===== transform and quantization =====
    11191115  //--- init rate estimation arrays for RDOQ ---
    1120   if(m_pcEncCfg->getUseRDOQ() && useTransformSkip == false)
     1116  if( useTransformSkip? m_pcEncCfg->getUseRDOQTS():m_pcEncCfg->getUseRDOQ())
    11211117  {
    11221118    m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, TEXT_LUMA );
     
    11691165      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
    11701166      {
    1171         pReco    [ uiX ] = Clip( pPred[ uiX ] + pResi[ uiX ] );
     1167        pReco    [ uiX ] = ClipY( pPred[ uiX ] + pResi[ uiX ] );
    11721168        pRecQt   [ uiX ] = pReco[ uiX ];
    11731169        pRecIPred[ uiX ] = pReco[ uiX ];
     
    11821178 
    11831179  //===== update distortion =====
    1184   ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
     1180  ruiDist += m_pcRdCost->getDistPart(g_bitDepthY, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
    11851181}
    11861182
     
    12471243    pcCU->getPattern()->initPattern         ( pcCU, uiTrDepth, uiAbsPartIdx );
    12481244
    1249 #if !REMOVE_LMCHROMA
    1250     if( uiChromaPredMode == LM_CHROMA_IDX && uiChromaId == 0 )
    1251     {
    1252       pcCU->getPattern()->initAdiPattern( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail, true );
    1253       getLumaRecPixels( pcCU->getPattern(), uiWidth, uiHeight );
    1254     }
    1255 #endif
    1256    
    12571245    pcCU->getPattern()->initAdiPatternChroma( pcCU, uiAbsPartIdx, uiTrDepth, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
    12581246    Int*  pPatChroma  = ( uiChromaId > 0 ? pcCU->getPattern()->getAdiCrBuf( uiWidth, uiHeight, m_piYuvExt ) : pcCU->getPattern()->getAdiCbBuf( uiWidth, uiHeight, m_piYuvExt ) );
     
    12661254  else
    12671255#endif
    1268 #if !REMOVE_LMCHROMA
    1269     if( uiChromaPredMode == LM_CHROMA_IDX )
    1270     {
    1271       predLMIntraChroma( pcCU->getPattern(), pPatChroma, piPred, uiStride, uiWidth, uiHeight, uiChromaId );
    1272     }
    1273     else
    1274 #endif
    1275     {
    1276       predIntraChromaAng( pcCU->getPattern(), pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail ); 
    1277     }
    1278 
     1256    {
     1257      predIntraChromaAng( pPatChroma, uiChromaPredMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
     1258    }
    12791259    // save prediction
    12801260    if( default0Save1Load2 == 1 )
     
    13291309  {
    13301310    //--- init rate estimation arrays for RDOQ ---
    1331     if( m_pcEncCfg->getUseRDOQ() && !useTransformSkipChroma)
     1311    if( useTransformSkipChroma? m_pcEncCfg->getUseRDOQTS():m_pcEncCfg->getUseRDOQ())
    13321312    {
    13331313      m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, uiWidth, uiWidth, eText );
     
    13361316    UInt uiAbsSum = 0;
    13371317
    1338 #if CHROMA_QP_EXTENSION
    13391318    Int curChromaQpOffset;
    13401319    if(eText == TEXT_CHROMA_U)
     
    13471326    }
    13481327    m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    1349 #else
    1350     if(eText == TEXT_CHROMA_U)
    1351     {
    1352       m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCbQpOffset() );
    1353     }
    1354     else
    1355     {
    1356       m_pcTrQuant->setQPforQuant     ( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCrQpOffset() );
    1357     }
    1358 #endif
    13591328
    13601329#if RDOQ_CHROMA_LAMBDA
     
    13981367      for( UInt uiX = 0; uiX < uiWidth; uiX++ )
    13991368      {
    1400         pReco    [ uiX ] = Clip( pPred[ uiX ] + pResi[ uiX ] );
     1369        pReco    [ uiX ] = ClipC( pPred[ uiX ] + pResi[ uiX ] );
    14011370        pRecQt   [ uiX ] = pReco[ uiX ];
    14021371        pRecIPred[ uiX ] = pReco[ uiX ];
     
    14121381  //===== update distortion =====
    14131382#if WEIGHTED_CHROMA_DISTORTION
    1414   ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight, true );
     1383  ruiDist += m_pcRdCost->getDistPart(g_bitDepthC, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight, eText );
    14151384#else
    1416   ruiDist += m_pcRdCost->getDistPart( piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
     1385  ruiDist += m_pcRdCost->getDistPart(g_bitDepthC, piReco, uiStride, piOrg, uiStride, uiWidth, uiHeight );
    14171386#endif
    14181387}
     
    14411410 
    14421411#if HHI_RQT_INTRA_SPEEDUP
     1412#if L0232_RD_PENALTY
     1413  Int maxTuSize = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize();
     1414  Int isIntraSlice = (pcCU->getSlice()->getSliceType() == I_SLICE);
     1415  // don't check split if TU size is less or equal to max TU size
     1416  Bool noSplitIntraMaxTuSize = bCheckFull;
     1417  if(m_pcEncCfg->getRDpenalty() && ! isIntraSlice)
     1418  {
     1419    // in addition don't check split if TU size is less or equal to 16x16 TU size for non-intra slice
     1420    noSplitIntraMaxTuSize = ( uiLog2TrSize  <= min(maxTuSize,4) );
     1421
     1422    // if maximum RD-penalty don't check TU size 32x32
     1423    if(m_pcEncCfg->getRDpenalty()==2)
     1424    {
     1425      bCheckFull    = ( uiLog2TrSize  <= min(maxTuSize,4));
     1426    }
     1427  }
     1428  if( bCheckFirst && noSplitIntraMaxTuSize )
     1429#else
    14431430  if( bCheckFirst && bCheckFull )
     1431#endif
    14441432  {
    14451433    bCheckSplit = false;
    14461434  }
     1435#else
     1436#if L0232_RD_PENALTY
     1437  Int maxTuSize = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize();
     1438  Int isIntraSlice = (pcCU->getSlice()->getSliceType() == I_SLICE);
     1439  // if maximum RD-penalty don't check TU size 32x32
     1440  if((m_pcEncCfg->getRDpenalty()==2)  && !isIntraSlice)
     1441  {
     1442    bCheckFull    = ( uiLog2TrSize  <= min(maxTuSize,4));
     1443  }
     1444#endif
    14471445#endif
    14481446  Double  dSingleCost   = MAX_DOUBLE;
     
    14521450  UInt    uiSingleCbfU  = 0;
    14531451  UInt    uiSingleCbfV  = 0;
    1454 #if PPS_TS_FLAG
    14551452  Bool    checkTransformSkip  = pcCU->getSlice()->getPPS()->getUseTransformSkip();
    1456 #else
    1457   Bool    checkTransformSkip  = pcCU->getSlice()->getSPS()->getUseTransformSkip();
    1458 #endif
    14591453  UInt    widthTransformSkip  = pcCU->getWidth ( 0 ) >> uiTrDepth;
    14601454  UInt    heightTransformSkip = pcCU->getHeight( 0 ) >> uiTrDepth;
     
    14641458  checkTransformSkip         &= (!pcCU->getCUTransquantBypass(0));
    14651459  checkTransformSkip         &= (!((pcCU->getQP( 0 ) == 0) && (pcCU->getSlice()->getSPS()->getUseLossless())));
    1466 #if INTRA_TRANSFORMSKIP_FAST
    14671460  if ( m_pcEncCfg->getUseTransformSkipFast() )
    14681461  {
    14691462    checkTransformSkip       &= (pcCU->getPartitionSize(uiAbsPartIdx)==SIZE_NxN);
    14701463  }
    1471 #endif
    14721464  if( bCheckFull )
    14731465  {
     
    16251617      //----- determine rate and r-d cost -----
    16261618      UInt uiSingleBits = xGetIntraBitsQT( pcCU, uiTrDepth, uiAbsPartIdx, true, !bLumaOnly, false );
     1619#if L0232_RD_PENALTY
     1620      if(m_pcEncCfg->getRDpenalty() && (uiLog2TrSize==5) && !isIntraSlice)
     1621      {
     1622        uiSingleBits=uiSingleBits*4;
     1623      }
     1624#endif
    16271625      dSingleCost       = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDistY + uiSingleDistC );
    16281626    }
     
    21972195  if(  uiTrMode == uiTrDepth )
    21982196  {
    2199 #if PPS_TS_FLAG
    22002197    Bool checkTransformSkip = pcCU->getSlice()->getPPS()->getUseTransformSkip();
    2201 #else
    2202     Bool checkTransformSkip = pcCU->getSlice()->getSPS()->getUseTransformSkip();
    2203 #endif
    22042198    UInt uiLog2TrSize = g_aucConvertToBit[ pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiFullDepth ] + 2;
    22052199
     
    22182212
    22192213    checkTransformSkip &= (uiLog2TrSize <= 3);
    2220 #if INTRA_TRANSFORMSKIP_FAST
    22212214    if ( m_pcEncCfg->getUseTransformSkipFast() )
    22222215    {
     
    22322225      }
    22332226    }
    2234 #endif
    22352227
    22362228    if(checkTransformSkip)
    22372229    {
    2238 #if INTRA_TRANSFORMSKIP_FAST==0
    2239       Bool checkTSFast = m_pcEncCfg->getUseTransformSkipFast();
    2240       if(checkTSFast)
    2241       {
    2242         //fast algorithm for chroma TS
    2243         Bool useTransformSkip = checkTransformSkip;
    2244         UInt blkNumberOfTS = 0;
    2245         for(UInt absPartIdxSub = uiAbsPartIdx; absPartIdxSub < uiAbsPartIdx + 4; absPartIdxSub ++)
    2246         {
    2247           blkNumberOfTS += pcCU->getTransformSkip(absPartIdxSub, TEXT_LUMA);
    2248         }
    2249         if(blkNumberOfTS < 4)
    2250         {
    2251           useTransformSkip = false;
    2252         }
    2253         pcCU ->setTransformSkipSubParts( useTransformSkip, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth);
    2254         pcCU ->setTransformSkipSubParts( useTransformSkip, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth);
    2255         xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 0 );
    2256         xIntraCodingChromaBlk( pcCU, uiTrDepth, uiAbsPartIdx, pcOrgYuv, pcPredYuv, pcResiYuv, ruiDist, 1 );
    2257         UInt singleCbfCb = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrDepth);
    2258         UInt singleCbfCr = pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrDepth);
    2259         if(useTransformSkip && singleCbfCb == 0)
    2260         {
    2261           pcCU ->setTransformSkipSubParts( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth);
    2262         }
    2263         if(useTransformSkip && singleCbfCr == 0)
    2264         {
    2265           pcCU ->setTransformSkipSubParts( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth( 0 ) +  actualTrDepth);
    2266         }
    2267       }
    2268       else
    2269       {
    2270 #endif
    22712230        //use RDO to decide whether Cr/Cb takes TS
    22722231        if( m_bUseSBACRD )
     
    23562315          }
    23572316        }
    2358 #if INTRA_TRANSFORMSKIP_FAST==0
    2359       }
    2360 #endif
    23612317    }
    23622318    else
     
    24792435  {
    24802436    //--- get prediction ---
    2481     predIntraChromaAng( pcCU->getPattern(), pPatChromaU, uiMode, piPredU, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
    2482     predIntraChromaAng( pcCU->getPattern(), pPatChromaV, uiMode, piPredV, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
     2437    predIntraChromaAng( pPatChromaU, uiMode, piPredU, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
     2438    predIntraChromaAng( pPatChromaV, uiMode, piPredV, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
    24832439   
    24842440    //--- get SAD ---
    2485     UInt  uiSAD  = m_pcRdCost->calcHAD( piOrgU, uiStride, piPredU, uiStride, uiWidth, uiHeight );
    2486     uiSAD       += m_pcRdCost->calcHAD( piOrgV, uiStride, piPredV, uiStride, uiWidth, uiHeight );
     2441    UInt  uiSAD  = m_pcRdCost->calcHAD(g_bitDepthC, piOrgU, uiStride, piPredU, uiStride, uiWidth, uiHeight );
     2442    uiSAD       += m_pcRdCost->calcHAD(g_bitDepthC, piOrgV, uiStride, piPredV, uiStride, uiWidth, uiHeight );
    24872443    //--- check ---
    24882444    if( uiSAD < uiMinSAD )
     
    25612517        UInt uiMode = modeIdx;
    25622518
    2563         predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, pcCU, bAboveAvail, bLeftAvail );
     2519        predIntraLumaAng( pcCU->getPattern(), uiMode, piPred, uiStride, uiWidth, uiHeight, bAboveAvail, bLeftAvail );
    25642520       
    25652521        // use hadamard transform here
    2566         UInt uiSad = m_pcRdCost->calcHAD( piOrg, uiStride, piPred, uiStride, uiWidth, uiHeight );
     2522        UInt uiSad = m_pcRdCost->calcHAD(g_bitDepthY, piOrg, uiStride, piPred, uiStride, uiWidth, uiHeight );
    25672523       
    25682524        UInt   iModeBits = xModeBitsIntra( pcCU, uiMode, uiPU, uiPartOffset, uiDepth, uiInitTrDepth );
     
    28292785}
    28302786
    2831 #if INTRA_BL
    2832 Void
    2833 TEncSearch::estIntraBLPredQT( TComDataCU* pcCU,
    2834                            TComYuv*    pcOrgYuv,
    2835                            TComYuv*    pcPredYuv,
    2836                            TComYuv*    pcResiYuv,
    2837                            TComYuv*    pcRecoYuv )
    2838 {
    2839   UInt    uiDepth        = pcCU->getDepth(0);
    2840   UInt    uiOverallDistY = 0;
    2841   UInt    uiOverallDistC = 0;
    2842  
    2843   //===== set QP and clear Cbf =====
    2844   if ( pcCU->getSlice()->getPPS()->getUseDQP() == true)
    2845   {
    2846     pcCU->setQPSubParts( pcCU->getQP(0), 0, uiDepth );
    2847   }
    2848   else
    2849   {
    2850     pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
    2851   }
    2852  
    2853   //===== init pattern for luma prediction =====
    2854   Bool bAboveAvail = false;
    2855   Bool bLeftAvail  = false;
    2856   pcCU->getPattern()->initPattern   ( pcCU, 0, 0 );
    2857   pcCU->getPattern()->initAdiPattern( pcCU, 0, 0, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
    2858  
    2859   pcCU->setLumaIntraDirSubParts ( DC_IDX, 0, uiDepth );
    2860  
    2861   // set context models
    2862   if( m_bUseSBACRD )
    2863   {
    2864     m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
    2865   }
    2866  
    2867   // determine residual for partition
    2868   Double dPUCost   = 0.0;
    2869   xRecurIntraCodingQT( pcCU, 0, 0, false, pcOrgYuv, pcPredYuv, pcResiYuv, uiOverallDistY, uiOverallDistC, false, dPUCost );
    2870   xSetIntraResultQT( pcCU, 0, 0, false, pcRecoYuv );
    2871  
    2872   //=== update PU data ====
    2873   pcCU->copyToPic( uiDepth, 0, 0 );
    2874    
    2875   //===== reset context models =====
    2876   if(m_bUseSBACRD)
    2877   {
    2878     m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
    2879   }
    2880 
    2881   //===== set distortion (rate and r-d costs are determined later) =====
    2882   pcCU->getTotalDistortion() = uiOverallDistY + uiOverallDistC;
    2883 }
    2884 
    2885 #endif
     2787
    28862788
    28872789Void
     
    29072809  for( UInt uiMode = uiMinMode; uiMode < uiMaxMode; uiMode++ )
    29082810  {
    2909 #if !REMOVE_LMCHROMA
    2910     if ( !pcCU->getSlice()->getSPS()->getUseLMChroma() && uiModeList[uiMode] == LM_CHROMA_IDX )
    2911     {
    2912       continue;
    2913     }
    2914 #endif
    29152811    //----- restore context models -----
    29162812    if( m_bUseSBACRD )
     
    29232819    pcCU->setChromIntraDirSubParts  ( uiModeList[uiMode], 0, uiDepth );
    29242820    xRecurIntraChromaCodingQT       ( pcCU,   0, 0, pcOrgYuv, pcPredYuv, pcResiYuv, uiDist );
    2925 #if PPS_TS_FLAG
    2926 #if INTRA_TRANSFORMSKIP_FAST
    29272821    if( m_bUseSBACRD && pcCU->getSlice()->getPPS()->getUseTransformSkip() )
    2928 #else
    2929     if( m_bUseSBACRD && pcCU->getSlice()->getPPS()->getUseTransformSkip() && !m_pcEncCfg->getUseTransformSkipFast())
    2930 #endif
    2931 #else
    2932 #if INTRA_TRANSFORMSKIP_FAST
    2933     if( m_bUseSBACRD && pcCU->getSlice()->getSPS()->getUseTransformSkip() )
    2934 #else
    2935     if( m_bUseSBACRD && pcCU->getSlice()->getSPS()->getUseTransformSkip() && !m_pcEncCfg->getUseTransformSkipFast())
    2936 #endif
    2937 #endif
    29382822    {
    29392823      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
     
    29972881  Pel* pReco = piReco;
    29982882  Pel* pRecoPic;
    2999   UInt uiInternalBitDepth = g_uiBitDepth + g_uiBitIncrement;
    3000   UInt uiPCMBitDepth;
     2883  Int shiftPcm;
    30012884
    30022885  if( eText == TEXT_LUMA)
     
    30042887    uiReconStride = pcCU->getPic()->getPicYuvRec()->getStride();
    30052888    pRecoPic      = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
    3006     uiPCMBitDepth = pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
     2889    shiftPcm = g_bitDepthY - pcCU->getSlice()->getSPS()->getPCMBitDepthLuma();
    30072890  }
    30082891  else
     
    30182901      pRecoPic = pcCU->getPic()->getPicYuvRec()->getCrAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiAbsPartIdx);
    30192902    }
    3020     uiPCMBitDepth = pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
     2903    shiftPcm = g_bitDepthC - pcCU->getSlice()->getSPS()->getPCMBitDepthChroma();
    30212904  }
    30222905
     
    30382921    for( uiX = 0; uiX < uiWidth; uiX++ )
    30392922    {
    3040       pPCM[uiX] = (pOrg[uiX]>>(uiInternalBitDepth - uiPCMBitDepth));
     2923      pPCM[uiX] = pOrg[uiX]>> shiftPcm;
    30412924    }
    30422925    pPCM += uiWidth;
     
    30512934    for( uiX = 0; uiX < uiWidth; uiX++ )
    30522935    {
    3053       pReco   [uiX] = (pPCM[uiX]<<(uiInternalBitDepth - uiPCMBitDepth));
     2936      pReco   [uiX] = pPCM[uiX]<< shiftPcm;
    30542937      pRecoPic[uiX] = pReco[uiX];
    30552938    }
     
    31523035  cDistParam.bApplyWeight = false;
    31533036
    3154   m_pcRdCost->setDistParam( cDistParam,
     3037  m_pcRdCost->setDistParam( cDistParam, g_bitDepthY,
    31553038                            pcYuvOrg->getLumaAddr( uiAbsPartIdx ), pcYuvOrg->getStride(),
    31563039                            m_tmpYuvPred .getLumaAddr( uiAbsPartIdx ), m_tmpYuvPred .getStride(),
     
    31823065  Int iHeight = 0;
    31833066
    3184 #if REF_IDX_FRAMEWORK  // HM bug fix
     3067#if 0 //REF_IDX_FRAMEWORK  // HM bug fix
    31853068  for( UInt uiMergeCand = 0; uiMergeCand < pcCU->getSlice()->getMaxNumMergeCand(); ++uiMergeCand )
    31863069  {
     
    31983081    if ( iPUIdx == 0 )
    31993082    {
    3200       pcCU->getInterMergeCandidates( 0, 0, uiDepth, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
     3083      pcCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand );
    32013084    }
    32023085    pcCU->setPartSizeSubParts( partSize, 0, uiDepth );
     
    32043087  else
    32053088  {
    3206     pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, uiDepth, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
     3089    pcCU->getInterMergeCandidates( uiAbsPartIdx, iPUIdx, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
    32073090  }
    32083091  xRestrictBipredMergeCand( pcCU, iPUIdx, cMvFieldNeighbours, uhInterDirNeighbours, numValidMergeCand );
     
    32423125    }
    32433126#endif
    3244     }
     3127  }
    32453128}
    32463129
     
    34063289#if (ENCODER_FAST_MODE)
    34073290        TComPic* pcPic    = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxTemp );
    3408         if( !testILR && pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N) )
     3291        if( !testILR && pcPic->isILR() && (ePartSize == SIZE_2Nx2N) )
    34093292        {
    34103293          continue;
     
    35093392#endif
    35103393        xCopyAMVPInfo(pcCU->getCUMvField(eRefPicList)->getAMVPInfo(), &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
    3511 #if !SPS_AMVP_CLEANUP
    3512         if ( pcCU->getAMVPMode(uiPartAddr) == AM_EXPL )
    3513         {         
    3514 #endif
    35153394        xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
    3516 #if !SPS_AMVP_CLEANUP
    3517         }
    3518 #endif
    35193395
    35203396        if(pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_C) > 0 && !pcCU->getSlice()->getNoBackPredFlag())
     
    36573533        Bool     testIter = true;
    36583534        TComPic* pcPic    = pcCU->getSlice()->getRefPic( RefPicList(1 - iRefList), iRefIdxBi[1 - iRefList] );
    3659         if(pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N))
     3535        if(pcPic->isILR() && (ePartSize == SIZE_2Nx2N))
     3536        {
    36603537          testIter = false;  //the fixed part is ILR, skip this iteration       
     3538        }
    36613539        if(testIter)
    36623540        {
     
    36683546        {
    36693547#if (ENCODER_FAST_MODE)
    3670             Bool testRefIdx = true;
    3671             pcPic           = pcCU->getSlice()->getRefPic( RefPicList(iRefList) , iRefIdxTemp );
    3672             if(pcPic->getIsILR() && (ePartSize == SIZE_2Nx2N))
    3673               testRefIdx = false;  //the refined part is ILR, skip this reference pic           
    3674             if(testRefIdx)
    3675             {
     3548          Bool testRefIdx = true;
     3549          pcPic           = pcCU->getSlice()->getRefPic( RefPicList(iRefList) , iRefIdxTemp );
     3550          if(pcPic->isILR() && (ePartSize == SIZE_2Nx2N))
     3551          {
     3552            testRefIdx = false;  //the refined part is ILR, skip this reference pic           
     3553          }
     3554          if(testRefIdx)
     3555          {
    36763556#endif
    36773557          uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
     
    36843564          // call ME
    36853565          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, true );
    3686 #if !SPS_AMVP_CLEANUP
    3687           if ( pcCU->getAMVPMode(uiPartAddr) == AM_EXPL )
    3688           {
    3689 #endif
    36903566          xCopyAMVPInfo(&aacAMVPInfo[iRefList][iRefIdxTemp], pcCU->getCUMvField(eRefPicList)->getAMVPInfo());
    36913567          xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
    3692 #if !SPS_AMVP_CLEANUP
    3693           }
    3694 #endif
    36953568
    36963569          if ( uiCostTemp < uiCostBi )
     
    37163589          }
    37173590#if (ENCODER_FAST_MODE)
    3718             }
    3719 #endif
    37203591          }
     3592#endif
     3593        }
    37213594#if (ENCODER_FAST_MODE)
    37223595        } // for loop-iRefIdxTemp
     
    37253598        if ( !bChanged )
    37263599        {
    3727 #if !SPS_AMVP_CLEANUP
    3728           if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1] && pcCU->getAMVPMode(uiPartAddr) == AM_EXPL )
    3729 #else
    37303600          if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1] )
    3731 #endif
    37323601          {
    37333602            xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], pcCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
     
    40053874  return;
    40063875}
    4007 #if ENCODER_FAST_MODE
    4008 Bool TEncSearch::predInterSearchILRUni( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
    4009 {
    4010   rpcPredYuv->clear();
    4011   rpcRecoYuv->clear();
    4012 
    4013   Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
    4014 
    4015   TComMv        cMv[2];
    4016   TComMv        cMvPred[2][33];
    4017   TComMv        cMvTemp[2][33];
    4018   TComMv        TempMv;
    4019 
    4020   Int           iRefIdx[2]={0,0};
    4021 
    4022   Int           aaiMvpIdx[2][33];
    4023   Int           aaiMvpNum[2][33];
    4024 
    4025   UInt          uiMbBits[3] = {1, 1, 0};
    4026   UInt          uiLastMode = 0;
    4027 
    4028   UInt          uiCost[2]   = { MAX_UINT, MAX_UINT };     //uni, rdCost
    4029   UInt          uiCostTemp;
    4030   UInt          biPDistTemp = MAX_INT;
    4031   UInt          uiBitsTemp;
    4032 
    4033   PartSize      ePartSize = pcCU->getPartitionSize( 0 );  //2Nx2N
    4034   Int           iPartIdx  = 0;                            //one PU in CU
    4035   UInt          uiPartAddr;
    4036   Int           iRoiWidth, iRoiHeight;
    4037 
    4038   xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
    4039   pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
    4040 
    4041   for( Int iRefList = 0; iRefList < iNumPredDir; iRefList++)  //list
    4042   {
    4043     RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
    4044 
    4045     Int  iRefIdxTemp = -1;
    4046     Bool foundILR    = false;
    4047     for( Int refIdx = 0; refIdx < pcCU->getSlice()->getNumRefIdx(eRefPicList); refIdx++ )
    4048       if( pcCU->getSlice()->getRefPic(eRefPicList, refIdx)->getIsILR() )
    4049       {
    4050         iRefIdxTemp = refIdx;
    4051         foundILR    = true;
    4052         break;
    4053       }
    4054 
    4055     if(!foundILR)  //no ILR in eRefPiclist
    4056       continue; 
    4057 
    4058     uiBitsTemp = uiMbBits[iRefList];
    4059     if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
    4060     {
    4061       uiBitsTemp += iRefIdxTemp+1;
    4062       if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
    4063     }
    4064 
    4065     xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
    4066     aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
    4067     aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
    4068 
    4069     uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
    4070 
    4071     xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
    4072     xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
    4073 
    4074     if( uiCostTemp < uiCost[iRefList] )
    4075     {
    4076       uiCost[iRefList] = uiCostTemp;
    4077 
    4078       cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
    4079       iRefIdx[iRefList] = iRefIdxTemp;
    4080 
    4081       pcCU->getCUMvField(eRefPicList)->setAllMv( cMv[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
    4082       pcCU->getCUMvField(eRefPicList)->setAllRefIdx( iRefIdx[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
    4083     }
    4084   }
    4085 
    4086   if( uiCost[0] == MAX_UINT && uiCost[1] == MAX_UINT )  //no ILR in both list0 and list1
    4087     return false;
    4088 
    4089   //  Clear Motion Field
    4090   pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
    4091   pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
    4092   pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx );
    4093   pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx );
    4094 
    4095   pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4096   pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4097   pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4098   pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4099 
    4100   if( uiCost[0] <= uiCost[1] )  //list0 ILR
    4101   {
    4102     pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv    ( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
    4103     pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
    4104 
    4105     TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
    4106     pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
    4107 
    4108     pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    4109 
    4110     pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4111     pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4112   }
    4113   else if( uiCost[1] < uiCost[0] )  //list1 ILR
    4114   {
    4115     pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv    ( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
    4116     pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
    4117 
    4118     TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
    4119     pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
    4120 
    4121     pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
    4122 
    4123     pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4124     pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
    4125   }
    4126   else
    4127     assert(0);
    4128 
    4129   pcCU->setMergeFlagSubParts( false, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
    4130 
    4131   motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
    4132 
    4133   setWpScalingDistParam( pcCU, -1, REF_PIC_LIST_X );
    4134 
    4135   return true;
    4136 }
    4137 
    4138 
    4139 #endif
    41403876
    41413877// AMVP
     
    41683904  cBestMv  = pcAMVPInfo->m_acMvCand[0];
    41693905#if !ZERO_MVD_EST
    4170 #if !SPS_AMVP_CLEANUP
    4171   if( pcCU->getAMVPMode(uiPartAddr) == AM_NONE || (pcAMVPInfo->iN <= 1 && pcCU->getAMVPMode(uiPartAddr) == AM_EXPL) )
    4172 #else
    41733906  if (pcAMVPInfo->iN <= 1)
    4174 #endif
    41753907  {
    41763908    rcMvPred = cBestMv;
     
    41903922  }
    41913923#endif 
    4192 #if !SPS_AMVP_CLEANUP
    4193   if (pcCU->getAMVPMode(uiPartAddr) == AM_EXPL && bFilled)
    4194 #else
    41953924  if (bFilled)
    4196 #endif
    41973925  {
    41983926    assert(pcCU->getMVPIdx(eRefPicList,uiPartAddr) >= 0);
     
    42013929  }
    42023930 
    4203 #if !SPS_AMVP_CLEANUP
    4204   if (pcCU->getAMVPMode(uiPartAddr) == AM_EXPL)
    4205   {
    4206 #endif
    42073931  m_cYuvPredTemp.clear();
    42083932#if ZERO_MVD_EST
     
    42313955
    42323956  m_cYuvPredTemp.clear();
    4233 #if !SPS_AMVP_CLEANUP
    4234   }
    4235 #endif
    42363957 
    42373958  // Setting Best MVP
     
    44064127  if ( pcCU->getSlice()->getPPS()->getUseWP() && pcCU->getSlice()->getSliceType()==P_SLICE )
    44074128  {
    4408     xWeightedPredictionUni( pcCU, pcTemplateCand, uiPartAddr, iSizeX, iSizeY, eRefPicList, pcTemplateCand, uiPartIdx, iRefIdx );
     4129    xWeightedPredictionUni( pcCU, pcTemplateCand, uiPartAddr, iSizeX, iSizeY, eRefPicList, pcTemplateCand, iRefIdx );
    44094130  }
    44104131
     
    44134134  m_pcRdCost->getMotionCost( 1, 0 );
    44144135  DistParam cDistParam;
    4415   m_pcRdCost->setDistParam( cDistParam,
     4136  m_pcRdCost->setDistParam( cDistParam, g_bitDepthY,
    44164137                            pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(),
    44174138                            pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(),
     
    44254146#else
    44264147#if WEIGHTED_CHROMA_DISTORTION
    4427   uiCost = m_pcRdCost->getDistPart( pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, false, DF_SAD );
     4148  uiCost = m_pcRdCost->getDistPart(g_bitDepthY, pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, TEXT_LUMA, DF_SAD );
    44284149#else
    4429   uiCost = m_pcRdCost->getDistPart( pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, DF_SAD );
     4150  uiCost = m_pcRdCost->getDistPart(g_bitDepthY, pcTemplateCand->getLumaAddr(uiPartAddr), pcTemplateCand->getStride(), pcOrgYuv->getLumaAddr(uiPartAddr), pcOrgYuv->getStride(), iSizeX, iSizeY, DF_SAD );
    44304151#endif
    44314152  uiCost = (UInt) m_pcRdCost->calcRdCost( m_auiMVPIdxCost[iMVPIdx][iMVPNum], uiCost, false, DF_SAD );
     
    44734194                            iRoiHeight,
    44744195                            pcYuv->getStride(),
    4475                             0, 0, 0, 0 );
     4196                            0, 0 );
    44764197 
    44774198  Pel*        piRefY      = pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getPicYuvRec()->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiPartAddr );
     
    44904211  setWpScalingDistParam( pcCU, iRefIdxPred, eRefPicList );
    44914212  //  Do integer search
    4492 #if REF_IDX_ME_AROUND_ZEROMV || REF_IDX_ME_ZEROMV
    4493 #if REF_IDX_ME_AROUND_ZEROMV
    4494   if( pcCU->getSlice()->getRefPic(eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
    4495   {
    4496     xPatternSearchILR  ( pcCU, pcPatternKey, piRefY, iRefStride, rcMv, ruiCost );
    4497   }
    4498   else  //non ILR reference pic
    4499   {
    4500   if ( !m_iFastSearch || bBi )
    4501   {
    4502     xPatternSearch      ( pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    4503   }
    4504   else
    4505   {
    4506     rcMv = *pcMvPred;
    4507     xPatternSearchFast  ( pcCU, pcPatternKey, piRefY, iRefStride, &cMvSrchRngLT, &cMvSrchRngRB, rcMv, ruiCost );
    4508     }
    4509   }
    4510 #endif
    45114213#if REF_IDX_ME_ZEROMV
    4512   if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
     4214  if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->isILR())  //ILR reference pic
    45134215  {
    45144216    rcMv.setZero();  //use Mv(0, 0) for integer ME
     
    45264228    }
    45274229  }
    4528 #endif
    45294230#else
    45304231  if ( !m_iFastSearch || bBi )
     
    45444245  {
    45454246#if REF_IDX_ME_ZEROMV
    4546     if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->getIsILR())  //ILR reference pic
     4247    if( pcCU->getSlice()->getRefPic( eRefPicList, iRefIdxPred )->isILR())  //ILR reference pic
    45474248    {
    45484249      xPatternSearchFracDIFMv0( pcCU, pcPatternKey, piRefY, iRefStride, &rcMv, cMvHalf, cMvQter, ruiCost, bBi );
     
    46284329      setDistParamComp(0);
    46294330
     4331      m_cDistParam.bitDepth = g_bitDepthY;
    46304332      uiSad = m_cDistParam.DistFunc( &m_cDistParam );
    46314333     
     
    46664368}
    46674369
    4668 #if REF_IDX_ME_AROUND_ZEROMV
    4669 //ILR integer pixel motion estimation search
    4670 //pcCU is CU pointer
    4671 //pcPatterney contains target PU infor
    4672 //piRefY is the PU at the same position as the source PU, but in the reference pic
    4673 //iRefStride is the reference stride
    4674 //rcMv output best integer MV
    4675 //ruiSAD outputs the SAD of best integer MV
    4676 Void TEncSearch::xPatternSearchILR( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv& rcMv, UInt& ruiSAD )
    4677 {
    4678   Int      candMvX[5] = {0, -1, 1,  0, 0};
    4679   Int      candMvY[5] = {0,  0, 0, -1, 1};
    4680   Int      numCand    = 1;
    4681   Int      iBestX = 0;
    4682   Int      iBestY = 0;
    4683   Int      uiSadBest = MAX_UINT;
    4684   UInt     uiSad;
    4685 
    4686   Pel*  piRefSrch;
    4687 
    4688   //-- jclee for using the SAD function pointer
    4689   m_pcRdCost->setDistParam( pcPatternKey, piRefY, iRefStride,  m_cDistParam );
    4690 
    4691   // fast encoder decision: use subsampled SAD for integer ME
    4692   if ( m_pcEncCfg->getUseFastEnc() )
    4693   {
    4694     if ( m_cDistParam.iRows > 8 )
    4695     {
    4696       m_cDistParam.iSubShift = 1;
    4697     }
    4698   }
    4699 
    4700   for(Int candId = 0; candId < numCand; candId++)
    4701   {
    4702     piRefSrch         = piRefY + (candMvY[candId] * iRefStride) + candMvX[candId];
    4703     m_cDistParam.pCur = piRefSrch;
    4704 
    4705     setDistParamComp(0);
    4706    
    4707     uiSad = m_cDistParam.DistFunc( &m_cDistParam );  //SAD
    4708 
    4709     //motion cost
    4710     uiSad += m_pcRdCost->getCost( candMvX[candId], candMvY[candId] );  //SAD rdCost
    4711 
    4712     if(uiSad < uiSadBest)
    4713     {
    4714       uiSadBest = uiSad;
    4715       iBestX    = candMvX[candId];
    4716       iBestY    = candMvY[candId];
    4717     }
    4718   }
    4719 
    4720   rcMv.set( iBestX, iBestY );
    4721 
    4722   ruiSAD = uiSadBest - m_pcRdCost->getCost( iBestX, iBestY );
    4723   return;
    4724 }
    4725 #endif
    47264370Void TEncSearch::xTZSearch( TComDataCU* pcCU, TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, TComMv* pcMvSrchRngLT, TComMv* pcMvSrchRngRB, TComMv& rcMv, UInt& ruiSAD )
    47274371{
     
    49184562                          pcPatternKey->getROIYHeight(),
    49194563                          iRefStride,
    4920                           0, 0, 0, 0 );
     4564                          0, 0 );
    49214565 
    49224566  //  Half-pel refinement
     
    49364580  rcMvQter += rcMvHalf;  rcMvQter <<= 1;
    49374581  ruiCost = xPatternRefinement( pcPatternKey, baseRefMv, 1, rcMvQter );
     4582}
     4583
     4584/** encode residual and calculate rate-distortion for a CU block
     4585 * \param pcCU
     4586 * \param pcYuvOrg
     4587 * \param pcYuvPred
     4588 * \param rpcYuvResi
     4589 * \param rpcYuvResiBest
     4590 * \param rpcYuvRec
     4591 * \param bSkipRes
     4592 * \returns Void
     4593 */
     4594Void TEncSearch::encodeResAndCalcRdInterCU( TComDataCU* pcCU, TComYuv* pcYuvOrg, TComYuv* pcYuvPred, TComYuv*& rpcYuvResi, TComYuv*& rpcYuvResiBest, TComYuv*& rpcYuvRec, Bool bSkipRes )
     4595{
     4596#if NO_RESIDUAL_FLAG_FOR_BLPRED
     4597  if ( pcCU->isIntra(0) && !pcCU->isIntraBL(0))
     4598#else
     4599  if ( pcCU->isIntra(0) )
     4600#endif
     4601  {
     4602    return;
     4603  }
     4604 
     4605  Bool      bHighPass    = pcCU->getSlice()->getDepth() ? true : false;
     4606  UInt      uiBits       = 0, uiBitsBest = 0;
     4607  UInt      uiDistortion = 0, uiDistortionBest = 0;
     4608 
     4609  UInt      uiWidth      = pcCU->getWidth ( 0 );
     4610  UInt      uiHeight     = pcCU->getHeight( 0 );
     4611 
     4612  //  No residual coding : SKIP mode
     4613  if ( bSkipRes )
     4614  {
     4615    pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
     4616
     4617    rpcYuvResi->clear();
     4618   
     4619    pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
     4620   
     4621#if WEIGHTED_CHROMA_DISTORTION
     4622    uiDistortion = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4623    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_U )
     4624    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_V );
     4625#else
     4626    uiDistortion = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4627    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
     4628    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
     4629#endif
     4630
     4631    if( m_bUseSBACRD )
     4632      m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST]);
     4633   
     4634    m_pcEntropyCoder->resetBits();
     4635    if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
     4636    {
     4637      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
     4638    }
     4639    m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
     4640    m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, true );
     4641   
     4642    uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
     4643    pcCU->getTotalBits()       = uiBits;
     4644    pcCU->getTotalDistortion() = uiDistortion;
     4645    pcCU->getTotalCost()       = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
     4646   
     4647    if( m_bUseSBACRD )
     4648      m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_TEMP_BEST]);
     4649   
     4650    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
     4651    pcCU->setTrIdxSubParts( 0, 0, pcCU->getDepth(0) );
     4652   
     4653    return;
     4654  }
     4655 
     4656  //  Residual coding.
     4657  Int    qp, qpBest = 0, qpMin, qpMax;
     4658  Double  dCost, dCostBest = MAX_DOUBLE;
     4659 
     4660  UInt uiTrLevel = 0;
     4661  if( (pcCU->getWidth(0) > pcCU->getSlice()->getSPS()->getMaxTrSize()) )
     4662  {
     4663    while( pcCU->getWidth(0) > (pcCU->getSlice()->getSPS()->getMaxTrSize()<<uiTrLevel) ) uiTrLevel++;
     4664  }
     4665  UInt uiMaxTrMode = pcCU->getSlice()->getSPS()->getMaxTrDepth() + uiTrLevel;
     4666 
     4667  while((uiWidth>>uiMaxTrMode) < (g_uiMaxCUWidth>>g_uiMaxCUDepth)) uiMaxTrMode--;
     4668 
     4669  qpMin =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) : pcCU->getQP( 0 );
     4670  qpMax =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) : pcCU->getQP( 0 );
     4671
     4672  rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
     4673
     4674  for ( qp = qpMin; qp <= qpMax; qp++ )
     4675  {
     4676    dCost = 0.;
     4677    uiBits = 0;
     4678    uiDistortion = 0;
     4679    if( m_bUseSBACRD )
     4680    {
     4681      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_CURR_BEST ] );
     4682    }
     4683   
     4684    UInt uiZeroDistortion = 0;
     4685    xEstimateResidualQT( pcCU, 0, 0, 0, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
     4686   
     4687    m_pcEntropyCoder->resetBits();
     4688    m_pcEntropyCoder->encodeQtRootCbfZero( pcCU );
     4689    UInt zeroResiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
     4690    Double dZeroCost = m_pcRdCost->calcRdCost( zeroResiBits, uiZeroDistortion );
     4691    if(pcCU->isLosslessCoded( 0 ))
     4692    { 
     4693      dZeroCost = dCost + 1;
     4694    }
     4695    if ( dZeroCost < dCost )
     4696    {
     4697      dCost        = dZeroCost;
     4698      uiBits       = 0;
     4699      uiDistortion = uiZeroDistortion;
     4700     
     4701      const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
     4702      ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
     4703      ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
     4704      ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
     4705      ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
     4706      ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
     4707      ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4708      ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4709      pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
     4710    }
     4711#if NO_RESIDUAL_FLAG_FOR_BLPRED
     4712    else if(pcCU->getLayerId() > 0 && pcCU->isIntraBL(0) && uiZeroDistortion == uiDistortion) // all zeros
     4713    {
     4714      const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
     4715      ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
     4716      ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
     4717      ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
     4718      ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
     4719      ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
     4720      ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4721      ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4722      pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
     4723    }
     4724#endif
     4725    else
     4726    {
     4727      xSetResidualQTData( pcCU, 0, 0, 0, NULL, pcCU->getDepth(0), false );
     4728    }
     4729   
     4730    if( m_bUseSBACRD )
     4731    {
     4732      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
     4733    }
     4734#if 0 // check
     4735    {
     4736      m_pcEntropyCoder->resetBits();
     4737      m_pcEntropyCoder->encodeCoeff( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0) );
     4738      const UInt uiBitsForCoeff = m_pcEntropyCoder->getNumberOfWrittenBits();
     4739      if( m_bUseSBACRD )
     4740      {
     4741        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
     4742      }
     4743      if( uiBitsForCoeff != uiBits )
     4744        assert( 0 );
     4745    }
     4746#endif
     4747    uiBits = 0;
     4748    {
     4749      TComYuv *pDummy = NULL;
     4750      xAddSymbolBitsInter( pcCU, 0, 0, uiBits, pDummy, NULL, pDummy );
     4751    }
     4752   
     4753   
     4754    Double dExactCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
     4755    dCost = dExactCost;
     4756   
     4757    if ( dCost < dCostBest )
     4758    {
     4759      if ( !pcCU->getQtRootCbf( 0 ) )
     4760      {
     4761        rpcYuvResiBest->clear();
     4762      }
     4763      else
     4764      {
     4765        xSetResidualQTData( pcCU, 0, 0, 0, rpcYuvResiBest, pcCU->getDepth(0), true );
     4766      }
     4767     
     4768      if( qpMin != qpMax && qp != qpMax )
     4769      {
     4770        const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
     4771        ::memcpy( m_puhQTTempTrIdx, pcCU->getTransformIdx(),        uiQPartNum * sizeof(UChar) );
     4772        ::memcpy( m_puhQTTempCbf[0], pcCU->getCbf( TEXT_LUMA ),     uiQPartNum * sizeof(UChar) );
     4773        ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ), uiQPartNum * sizeof(UChar) );
     4774        ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ), uiQPartNum * sizeof(UChar) );
     4775        ::memcpy( m_pcQTTempCoeffY,  pcCU->getCoeffY(),  uiWidth * uiHeight * sizeof( TCoeff )      );
     4776        ::memcpy( m_pcQTTempCoeffCb, pcCU->getCoeffCb(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4777        ::memcpy( m_pcQTTempCoeffCr, pcCU->getCoeffCr(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4778#if ADAPTIVE_QP_SELECTION
     4779        ::memcpy( m_pcQTTempArlCoeffY,  pcCU->getArlCoeffY(),  uiWidth * uiHeight * sizeof( Int )      );
     4780        ::memcpy( m_pcQTTempArlCoeffCb, pcCU->getArlCoeffCb(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4781        ::memcpy( m_pcQTTempArlCoeffCr, pcCU->getArlCoeffCr(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4782#endif
     4783        ::memcpy( m_puhQTTempTransformSkipFlag[0], pcCU->getTransformSkip(TEXT_LUMA),     uiQPartNum * sizeof( UChar ) );
     4784        ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip(TEXT_CHROMA_U), uiQPartNum * sizeof( UChar ) );
     4785        ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip(TEXT_CHROMA_V), uiQPartNum * sizeof( UChar ) );
     4786      }
     4787      uiBitsBest       = uiBits;
     4788      uiDistortionBest = uiDistortion;
     4789      dCostBest        = dCost;
     4790      qpBest           = qp;
     4791      if( m_bUseSBACRD )
     4792      {
     4793        m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
     4794      }
     4795    }
     4796  }
     4797 
     4798  assert ( dCostBest != MAX_DOUBLE );
     4799 
     4800  if( qpMin != qpMax && qpBest != qpMax )
     4801  {
     4802    if( m_bUseSBACRD )
     4803    {
     4804      assert( 0 ); // check
     4805      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
     4806    }
     4807    // copy best cbf and trIdx to pcCU
     4808    const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
     4809    ::memcpy( pcCU->getTransformIdx(),       m_puhQTTempTrIdx,  uiQPartNum * sizeof(UChar) );
     4810    ::memcpy( pcCU->getCbf( TEXT_LUMA ),     m_puhQTTempCbf[0], uiQPartNum * sizeof(UChar) );
     4811    ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ), m_puhQTTempCbf[1], uiQPartNum * sizeof(UChar) );
     4812    ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ), m_puhQTTempCbf[2], uiQPartNum * sizeof(UChar) );
     4813    ::memcpy( pcCU->getCoeffY(),  m_pcQTTempCoeffY,  uiWidth * uiHeight * sizeof( TCoeff )      );
     4814    ::memcpy( pcCU->getCoeffCb(), m_pcQTTempCoeffCb, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4815    ::memcpy( pcCU->getCoeffCr(), m_pcQTTempCoeffCr, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
     4816#if ADAPTIVE_QP_SELECTION
     4817    ::memcpy( pcCU->getArlCoeffY(),  m_pcQTTempArlCoeffY,  uiWidth * uiHeight * sizeof( Int )      );
     4818    ::memcpy( pcCU->getArlCoeffCb(), m_pcQTTempArlCoeffCb, uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4819    ::memcpy( pcCU->getArlCoeffCr(), m_pcQTTempArlCoeffCr, uiWidth * uiHeight * sizeof( Int ) >> 2 );
     4820#endif
     4821    ::memcpy( pcCU->getTransformSkip(TEXT_LUMA),     m_puhQTTempTransformSkipFlag[0], uiQPartNum * sizeof( UChar ) );
     4822    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_U), m_puhQTTempTransformSkipFlag[1], uiQPartNum * sizeof( UChar ) );
     4823    ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_V), m_puhQTTempTransformSkipFlag[2], uiQPartNum * sizeof( UChar ) );
     4824  }
     4825  rpcYuvRec->addClip ( pcYuvPred, rpcYuvResiBest, 0, uiWidth );
     4826 
     4827  // update with clipped distortion and cost (qp estimation loop uses unclipped values)
     4828#if WEIGHTED_CHROMA_DISTORTION
     4829    uiDistortionBest = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4830    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_U )
     4831    + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, TEXT_CHROMA_V );
     4832#else
     4833  uiDistortionBest = m_pcRdCost->getDistPart(g_bitDepthY, rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
     4834  + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
     4835  + m_pcRdCost->getDistPart(g_bitDepthC, rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
     4836#endif
     4837  dCostBest = m_pcRdCost->calcRdCost( uiBitsBest, uiDistortionBest );
     4838 
     4839  pcCU->getTotalBits()       = uiBitsBest;
     4840  pcCU->getTotalDistortion() = uiDistortionBest;
     4841  pcCU->getTotalCost()       = dCostBest;
     4842 
     4843  if ( pcCU->isSkipped(0) )
     4844  {
     4845    pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
     4846  }
     4847 
     4848  pcCU->setQPSubParts( qpBest, 0, pcCU->getDepth(0) );
     4849}
     4850
     4851Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, UInt &ruiDist, UInt *puiZeroDist )
     4852{
     4853  const UInt uiTrMode = uiDepth - pcCU->getDepth( 0 );
     4854 
     4855  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
     4856  const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
     4857 
     4858  UInt SplitFlag = ((pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && ( pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N ));
     4859  Bool bCheckFull;
     4860  if ( SplitFlag && uiDepth == pcCU->getDepth(uiAbsPartIdx) && ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) )
     4861     bCheckFull = false;
     4862  else
     4863     bCheckFull =  ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
     4864
     4865  const Bool bCheckSplit  = ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
     4866 
     4867  assert( bCheckFull || bCheckSplit );
     4868 
     4869  Bool  bCodeChroma   = true;
     4870  UInt  uiTrModeC     = uiTrMode;
     4871  UInt  uiLog2TrSizeC = uiLog2TrSize-1;
     4872  if( uiLog2TrSize == 2 )
     4873  {
     4874    uiLog2TrSizeC++;
     4875    uiTrModeC    --;
     4876    UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
     4877    bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
     4878  }
     4879 
     4880  const UInt uiSetCbf = 1 << uiTrMode;
     4881  // code full block
     4882  Double dSingleCost = MAX_DOUBLE;
     4883  UInt uiSingleBits = 0;
     4884  UInt uiSingleDist = 0;
     4885  UInt uiAbsSumY = 0, uiAbsSumU = 0, uiAbsSumV = 0;
     4886  UInt uiBestTransformMode[3] = {0};
     4887
     4888  if( m_bUseSBACRD )
     4889  {
     4890    m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     4891  }
     4892 
     4893  if( bCheckFull )
     4894  {
     4895    const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
     4896    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
     4897    TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     4898    TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     4899    TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     4900#if ADAPTIVE_QP_SELECTION   
     4901    Int *pcArlCoeffCurrY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     4902    Int *pcArlCoeffCurrU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     4903    Int *pcArlCoeffCurrV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);   
     4904#endif
     4905   
     4906    Int trWidth = 0, trHeight = 0, trWidthC = 0, trHeightC = 0;
     4907    UInt absTUPartIdxC = uiAbsPartIdx;
     4908
     4909    trWidth  = trHeight  = 1 << uiLog2TrSize;
     4910    trWidthC = trHeightC = 1 <<uiLog2TrSizeC;
     4911    pcCU->setTrIdxSubParts( uiDepth - pcCU->getDepth( 0 ), uiAbsPartIdx, uiDepth );
     4912    Double minCostY = MAX_DOUBLE;
     4913    Double minCostU = MAX_DOUBLE;
     4914    Double minCostV = MAX_DOUBLE;
     4915    Bool checkTransformSkipY  = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidth == 4 && trHeight == 4;
     4916    Bool checkTransformSkipUV = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidthC == 4 && trHeightC == 4;
     4917
     4918    checkTransformSkipY         &= (!pcCU->isLosslessCoded(0));
     4919    checkTransformSkipUV        &= (!pcCU->isLosslessCoded(0));
     4920
     4921    pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     4922    if( bCodeChroma )
     4923    {
     4924      pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     4925      pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     4926    }
     4927
     4928    if (m_pcEncCfg->getUseRDOQ())
     4929    {
     4930      m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
     4931    }
     4932
     4933    m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     4934
     4935#if RDOQ_CHROMA_LAMBDA
     4936    m_pcTrQuant->selectLambda(TEXT_LUMA); 
     4937#endif
     4938    m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY,
     4939#if ADAPTIVE_QP_SELECTION
     4940                                 pcArlCoeffCurrY,
     4941#endif     
     4942                                 trWidth,   trHeight,    uiAbsSumY, TEXT_LUMA,     uiAbsPartIdx );
     4943   
     4944    pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     4945   
     4946    if( bCodeChroma )
     4947    {
     4948      if (m_pcEncCfg->getUseRDOQ())
     4949      {
     4950        m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
     4951      }
     4952
     4953      Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
     4954      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     4955
     4956#if RDOQ_CHROMA_LAMBDA
     4957      m_pcTrQuant->selectLambda(TEXT_CHROMA);
     4958#endif
     4959
     4960      m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU,
     4961#if ADAPTIVE_QP_SELECTION
     4962                                 pcArlCoeffCurrU,
     4963#endif       
     4964                                 trWidthC, trHeightC, uiAbsSumU, TEXT_CHROMA_U, uiAbsPartIdx );
     4965
     4966      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
     4967      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     4968      m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV,
     4969#if ADAPTIVE_QP_SELECTION
     4970                                 pcArlCoeffCurrV,
     4971#endif       
     4972                                 trWidthC, trHeightC, uiAbsSumV, TEXT_CHROMA_V, uiAbsPartIdx );
     4973
     4974      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     4975      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     4976    }
     4977   
     4978    m_pcEntropyCoder->resetBits();
     4979   
     4980    {
     4981      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
     4982    }
     4983   
     4984    m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx,  trWidth,  trHeight,    uiDepth, TEXT_LUMA );
     4985    const UInt uiSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
     4986   
     4987    UInt uiSingleBitsU = 0;
     4988    UInt uiSingleBitsV = 0;
     4989    if( bCodeChroma )
     4990    {
     4991      {
     4992        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
     4993      }
     4994      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
     4995      uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsY;
     4996     
     4997      {
     4998        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
     4999      }
     5000      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
     5001      uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - ( uiSingleBitsY + uiSingleBitsU );
     5002    }
     5003   
     5004    const UInt uiNumSamplesLuma = 1 << (uiLog2TrSize<<1);
     5005    const UInt uiNumSamplesChro = 1 << (uiLog2TrSizeC<<1);
     5006   
     5007    ::memset( m_pTempPel, 0, sizeof( Pel ) * uiNumSamplesLuma ); // not necessary needed for inside of recursion (only at the beginning)
     5008   
     5009    UInt uiDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pTempPel, trWidth, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight ); // initialized with zero residual destortion
     5010
     5011    if ( puiZeroDist )
     5012    {
     5013      *puiZeroDist += uiDistY;
     5014    }
     5015    if( uiAbsSumY )
     5016    {
     5017      Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
     5018
     5019      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     5020
     5021      Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
     5022      assert(scalingListType < 6);     
     5023#if NO_RESIDUAL_FLAG_FOR_BLPRED
     5024      if(pcCU->isIntraBL(uiAbsPartIdx) )
     5025      {
     5026        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
     5027      }
     5028      else
     5029      {
     5030        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
     5031      }
     5032#else
     5033      m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
     5034#endif
     5035     
     5036      const UInt uiNonzeroDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
     5037      pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth,trHeight );
     5038      if (pcCU->isLosslessCoded(0))
     5039      {
     5040        uiDistY = uiNonzeroDistY;
     5041      }
     5042      else
     5043      {
     5044        const Double singleCostY = m_pcRdCost->calcRdCost( uiSingleBitsY, uiNonzeroDistY );
     5045        m_pcEntropyCoder->resetBits();
     5046        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_LUMA,     uiTrMode );
     5047        const UInt uiNullBitsY   = m_pcEntropyCoder->getNumberOfWrittenBits();
     5048        const Double nullCostY   = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
     5049        if( nullCostY < singleCostY ) 
     5050        {   
     5051          uiAbsSumY = 0;
     5052          ::memset( pcCoeffCurrY, 0, sizeof( TCoeff ) * uiNumSamplesLuma );
     5053          if( checkTransformSkipY )
     5054          {
     5055            minCostY = nullCostY;
     5056          }
     5057        }
     5058        else
     5059        {
     5060          uiDistY = uiNonzeroDistY;
     5061          if( checkTransformSkipY )
     5062          {
     5063            minCostY = singleCostY;
     5064          }
     5065        }
     5066      }
     5067    }
     5068    else if( checkTransformSkipY )
     5069    {
     5070      m_pcEntropyCoder->resetBits();
     5071      m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_LUMA, uiTrMode );
     5072      const UInt uiNullBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
     5073      minCostY = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
     5074    }
     5075
     5076    if( !uiAbsSumY )
     5077    {
     5078      Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx );
     5079      const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride();
     5080      for( UInt uiY = 0; uiY < trHeight; ++uiY )
     5081      {
     5082        ::memset( pcPtr, 0, sizeof( Pel ) * trWidth );
     5083        pcPtr += uiStride;
     5084      }
     5085    }
     5086   
     5087    UInt uiDistU = 0;
     5088    UInt uiDistV = 0;
     5089    if( bCodeChroma )
     5090    {
     5091      uiDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pTempPel, trWidthC, pcResi->getCbAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
     5092#if WEIGHTED_CHROMA_DISTORTION
     5093                                        , TEXT_CHROMA_U
     5094#endif
     5095                                        ); // initialized with zero residual destortion
     5096      if ( puiZeroDist )
     5097      {
     5098        *puiZeroDist += uiDistU;
     5099      }
     5100      if( uiAbsSumU )
     5101      {
     5102        Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
     5103
     5104        Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
     5105        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5106
     5107        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
     5108        assert(scalingListType < 6);
     5109        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType  );
     5110       
     5111        const UInt uiNonzeroDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
     5112                                                            pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
     5113#if WEIGHTED_CHROMA_DISTORTION
     5114                                                            , TEXT_CHROMA_U
     5115#endif
     5116                                                            );
     5117
     5118        if(pcCU->isLosslessCoded(0)) 
     5119        {
     5120          uiDistU = uiNonzeroDistU;
     5121        }
     5122        else
     5123        {
     5124          const Double dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
     5125          m_pcEntropyCoder->resetBits();
     5126          m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_U,     uiTrMode );
     5127          const UInt uiNullBitsU    = m_pcEntropyCoder->getNumberOfWrittenBits();
     5128          const Double dNullCostU   = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
     5129          if( dNullCostU < dSingleCostU )
     5130          {
     5131            uiAbsSumU = 0;
     5132            ::memset( pcCoeffCurrU, 0, sizeof( TCoeff ) * uiNumSamplesChro );
     5133            if( checkTransformSkipUV )
     5134            {
     5135              minCostU = dNullCostU;
     5136            }
     5137          }
     5138          else
     5139          {
     5140            uiDistU = uiNonzeroDistU;
     5141            if( checkTransformSkipUV )
     5142            {
     5143              minCostU = dSingleCostU;
     5144            }
     5145          }
     5146        }
     5147      }
     5148      else if( checkTransformSkipUV )
     5149      {
     5150        m_pcEntropyCoder->resetBits();
     5151        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_U, uiTrModeC );
     5152        const UInt uiNullBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();
     5153        minCostU = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
     5154      }
     5155      if( !uiAbsSumU )
     5156      {
     5157        Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
     5158          const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
     5159        for( UInt uiY = 0; uiY < trHeightC; ++uiY )
     5160        {
     5161          ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
     5162          pcPtr += uiStride;
     5163        }
     5164      }
     5165     
     5166      uiDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pTempPel, trWidthC, pcResi->getCrAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
     5167#if WEIGHTED_CHROMA_DISTORTION
     5168                                        , TEXT_CHROMA_V
     5169#endif
     5170                                        ); // initialized with zero residual destortion
     5171      if ( puiZeroDist )
     5172      {
     5173        *puiZeroDist += uiDistV;
     5174      }
     5175      if( uiAbsSumV )
     5176      {
     5177        Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
     5178        Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
     5179        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5180
     5181        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
     5182        assert(scalingListType < 6);
     5183        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType );
     5184       
     5185        const UInt uiNonzeroDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
     5186                                                            pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
     5187#if WEIGHTED_CHROMA_DISTORTION
     5188                                                            , TEXT_CHROMA_V
     5189#endif
     5190                                                            );
     5191        if (pcCU->isLosslessCoded(0))
     5192        {
     5193          uiDistV = uiNonzeroDistV;
     5194        }
     5195        else
     5196        {
     5197          const Double dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
     5198          m_pcEntropyCoder->resetBits();
     5199          m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_V,     uiTrMode );
     5200          const UInt uiNullBitsV    = m_pcEntropyCoder->getNumberOfWrittenBits();
     5201          const Double dNullCostV   = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
     5202          if( dNullCostV < dSingleCostV )
     5203          {
     5204            uiAbsSumV = 0;
     5205            ::memset( pcCoeffCurrV, 0, sizeof( TCoeff ) * uiNumSamplesChro );
     5206            if( checkTransformSkipUV )
     5207            {
     5208              minCostV = dNullCostV;
     5209            }
     5210          }
     5211          else
     5212          {
     5213            uiDistV = uiNonzeroDistV;
     5214            if( checkTransformSkipUV )
     5215            {
     5216              minCostV = dSingleCostV;
     5217            }
     5218          }
     5219        }
     5220      }
     5221      else if( checkTransformSkipUV )
     5222      {
     5223        m_pcEntropyCoder->resetBits();
     5224        m_pcEntropyCoder->encodeQtCbfZero( pcCU, TEXT_CHROMA_V, uiTrModeC );
     5225        const UInt uiNullBitsV = m_pcEntropyCoder->getNumberOfWrittenBits();
     5226        minCostV = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
     5227      }
     5228      if( !uiAbsSumV )
     5229      {
     5230        Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
     5231        const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
     5232        for( UInt uiY = 0; uiY < trHeightC; ++uiY )
     5233        {   
     5234          ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
     5235          pcPtr += uiStride;
     5236        }
     5237      }
     5238    }
     5239    pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5240    if( bCodeChroma )
     5241    {
     5242      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5243      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5244    }
     5245
     5246    if( checkTransformSkipY )
     5247    {
     5248      UInt uiNonzeroDistY, uiAbsSumTransformSkipY;
     5249      Double dSingleCostY;
     5250
     5251      Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
     5252      UInt resiYStride = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getStride();
     5253
     5254      TCoeff bestCoeffY[32*32];
     5255      memcpy( bestCoeffY, pcCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
     5256     
     5257#if ADAPTIVE_QP_SELECTION
     5258      TCoeff bestArlCoeffY[32*32];
     5259      memcpy( bestArlCoeffY, pcArlCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
     5260#endif
     5261
     5262      Pel bestResiY[32*32];
     5263      for ( Int i = 0; i < trHeight; ++i )
     5264      {
     5265        memcpy( &bestResiY[i*trWidth], pcResiCurrY+i*resiYStride, sizeof(Pel) * trWidth );
     5266      }
     5267
     5268      if( m_bUseSBACRD )
     5269      {
     5270        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     5271      }
     5272
     5273      pcCU->setTransformSkipSubParts ( 1, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5274
     5275      if (m_pcEncCfg->getUseRDOQTS())
     5276      {
     5277        m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
     5278      }
     5279
     5280      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     5281
     5282#if RDOQ_CHROMA_LAMBDA
     5283      m_pcTrQuant->selectLambda(TEXT_LUMA);
     5284#endif
     5285      m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY,
     5286#if ADAPTIVE_QP_SELECTION
     5287        pcArlCoeffCurrY,
     5288#endif     
     5289        trWidth,   trHeight,    uiAbsSumTransformSkipY, TEXT_LUMA, uiAbsPartIdx, true );
     5290      pcCU->setCbfSubParts( uiAbsSumTransformSkipY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5291
     5292      if( uiAbsSumTransformSkipY != 0 )
     5293      {
     5294        m_pcEntropyCoder->resetBits();
     5295        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiTrMode );
     5296        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
     5297        const UInt uiTsSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
     5298
     5299        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
     5300
     5301        Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
     5302        assert(scalingListType < 6);     
     5303
     5304#if NO_RESIDUAL_FLAG_FOR_BLPRED
     5305        if(pcCU->isIntraBL(uiAbsPartIdx) )
     5306        {
     5307          m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
     5308        }
     5309        else
     5310        {
     5311          m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
     5312        }
     5313#else
     5314        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
     5315#endif
     5316
     5317        uiNonzeroDistY = m_pcRdCost->getDistPart(g_bitDepthY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
     5318          pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight );
     5319
     5320        dSingleCostY = m_pcRdCost->calcRdCost( uiTsSingleBitsY, uiNonzeroDistY );
     5321      }
     5322
     5323      if( !uiAbsSumTransformSkipY || minCostY < dSingleCostY )
     5324      {
     5325        pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5326        memcpy( pcCoeffCurrY, bestCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
     5327#if ADAPTIVE_QP_SELECTION
     5328        memcpy( pcArlCoeffCurrY, bestArlCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
     5329#endif
     5330        for( Int i = 0; i < trHeight; ++i )
     5331        {
     5332          memcpy( pcResiCurrY+i*resiYStride, &bestResiY[i*trWidth], sizeof(Pel) * trWidth );
     5333        }
     5334      }
     5335      else
     5336      {
     5337        uiDistY = uiNonzeroDistY;
     5338        uiAbsSumY = uiAbsSumTransformSkipY;
     5339        uiBestTransformMode[0] = 1;
     5340      }
     5341
     5342      pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5343    }
     5344
     5345    if( bCodeChroma && checkTransformSkipUV  )
     5346    {
     5347      UInt uiNonzeroDistU, uiNonzeroDistV, uiAbsSumTransformSkipU, uiAbsSumTransformSkipV;
     5348      Double dSingleCostU, dSingleCostV;
     5349
     5350      Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
     5351      Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
     5352      UInt resiCStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
     5353
     5354      TCoeff bestCoeffU[32*32], bestCoeffV[32*32];
     5355      memcpy( bestCoeffU, pcCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
     5356      memcpy( bestCoeffV, pcCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
     5357
     5358#if ADAPTIVE_QP_SELECTION
     5359      TCoeff bestArlCoeffU[32*32], bestArlCoeffV[32*32];
     5360      memcpy( bestArlCoeffU, pcArlCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
     5361      memcpy( bestArlCoeffV, pcArlCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
     5362#endif
     5363
     5364      Pel bestResiU[32*32], bestResiV[32*32];
     5365      for (Int i = 0; i < trHeightC; ++i )
     5366      {
     5367        memcpy( &bestResiU[i*trWidthC], pcResiCurrU+i*resiCStride, sizeof(Pel) * trWidthC );
     5368        memcpy( &bestResiV[i*trWidthC], pcResiCurrV+i*resiCStride, sizeof(Pel) * trWidthC );
     5369      }
     5370
     5371      if( m_bUseSBACRD )
     5372      {
     5373        m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     5374      }
     5375
     5376      pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5377      pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5378
     5379      if (m_pcEncCfg->getUseRDOQTS())
     5380      {
     5381        m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
     5382      }
     5383
     5384      Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
     5385      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5386
     5387#if RDOQ_CHROMA_LAMBDA
     5388      m_pcTrQuant->selectLambda(TEXT_CHROMA);
     5389#endif
     5390
     5391      m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU,
     5392#if ADAPTIVE_QP_SELECTION
     5393        pcArlCoeffCurrU,
     5394#endif       
     5395        trWidthC, trHeightC, uiAbsSumTransformSkipU, TEXT_CHROMA_U, uiAbsPartIdx, true );
     5396      curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
     5397      m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5398      m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV,
     5399#if ADAPTIVE_QP_SELECTION
     5400        pcArlCoeffCurrV,
     5401#endif       
     5402        trWidthC, trHeightC, uiAbsSumTransformSkipV, TEXT_CHROMA_V, uiAbsPartIdx, true );
     5403
     5404      pcCU->setCbfSubParts( uiAbsSumTransformSkipU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5405      pcCU->setCbfSubParts( uiAbsSumTransformSkipV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5406
     5407      m_pcEntropyCoder->resetBits();
     5408      uiSingleBitsU = 0;
     5409      uiSingleBitsV = 0;
     5410
     5411      if( uiAbsSumTransformSkipU )
     5412      {
     5413        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
     5414        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
     5415        uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();   
     5416
     5417        curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
     5418        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5419
     5420        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
     5421        assert(scalingListType < 6);
     5422
     5423        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType, true  );
     5424
     5425        uiNonzeroDistU = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
     5426                                                 pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
     5427#if WEIGHTED_CHROMA_DISTORTION
     5428                                                 , TEXT_CHROMA_U
     5429#endif
     5430                                                 );
     5431
     5432        dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
     5433      }
     5434
     5435      if( !uiAbsSumTransformSkipU || minCostU < dSingleCostU )
     5436      {
     5437        pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5438
     5439        memcpy( pcCoeffCurrU, bestCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
     5440#if ADAPTIVE_QP_SELECTION
     5441        memcpy( pcArlCoeffCurrU, bestArlCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
     5442#endif
     5443        for( Int i = 0; i < trHeightC; ++i )
     5444        {
     5445          memcpy( pcResiCurrU+i*resiCStride, &bestResiU[i*trWidthC], sizeof(Pel) * trWidthC );
     5446        }
     5447      }
     5448      else
     5449      {
     5450        uiDistU = uiNonzeroDistU;
     5451        uiAbsSumU = uiAbsSumTransformSkipU;
     5452        uiBestTransformMode[1] = 1;
     5453      }
     5454
     5455      if( uiAbsSumTransformSkipV )
     5456      {
     5457        m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
     5458        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
     5459        uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsU;
     5460
     5461        curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
     5462        m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
     5463
     5464        Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
     5465        assert(scalingListType < 6);
     5466
     5467        m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType, true );
     5468
     5469        uiNonzeroDistV = m_pcRdCost->getDistPart(g_bitDepthC, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
     5470                                                 pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
     5471#if WEIGHTED_CHROMA_DISTORTION
     5472                                                 , TEXT_CHROMA_V
     5473#endif
     5474                                                 );
     5475
     5476        dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
     5477      }
     5478
     5479      if( !uiAbsSumTransformSkipV || minCostV < dSingleCostV )
     5480      {
     5481        pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5482
     5483        memcpy( pcCoeffCurrV, bestCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
     5484#if ADAPTIVE_QP_SELECTION
     5485        memcpy( pcArlCoeffCurrV, bestArlCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
     5486#endif
     5487        for( Int i = 0; i < trHeightC; ++i )
     5488        {
     5489          memcpy( pcResiCurrV+i*resiCStride, &bestResiV[i*trWidthC], sizeof(Pel) * trWidthC );
     5490        }
     5491      }
     5492      else
     5493      {
     5494        uiDistV = uiNonzeroDistV;
     5495        uiAbsSumV = uiAbsSumTransformSkipV;
     5496        uiBestTransformMode[2] = 1;
     5497      }
     5498
     5499      pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5500      pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5501    }
     5502
     5503    if( m_bUseSBACRD )
     5504    {
     5505      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     5506    }
     5507
     5508    m_pcEntropyCoder->resetBits();
     5509
     5510    {
     5511      if( uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
     5512      {
     5513        m_pcEntropyCoder->encodeTransformSubdivFlag( 0, 5 - uiLog2TrSize );
     5514      }
     5515    }
     5516
     5517    {
     5518      if( bCodeChroma )
     5519      {
     5520        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
     5521        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
     5522      }
     5523
     5524      m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
     5525    }
     5526
     5527    m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
     5528
     5529    if( bCodeChroma )
     5530    {
     5531      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
     5532      m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
     5533    }
     5534
     5535    uiSingleBits = m_pcEntropyCoder->getNumberOfWrittenBits();
     5536
     5537    uiSingleDist = uiDistY + uiDistU + uiDistV;
     5538    dSingleCost = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDist );
     5539  } 
     5540 
     5541  // code sub-blocks
     5542  if( bCheckSplit )
     5543  {
     5544    if( m_bUseSBACRD && bCheckFull )
     5545    {
     5546      m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
     5547      m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     5548    }
     5549    UInt uiSubdivDist = 0;
     5550    UInt uiSubdivBits = 0;
     5551    Double dSubdivCost = 0.0;
     5552   
     5553    const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
     5554    for( UInt ui = 0; ui < 4; ++ui )
     5555    {
     5556      UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
     5557      xEstimateResidualQT( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, dSubdivCost, uiSubdivBits, uiSubdivDist, bCheckFull ? NULL : puiZeroDist );
     5558    }
     5559   
     5560    UInt uiYCbf = 0;
     5561    UInt uiUCbf = 0;
     5562    UInt uiVCbf = 0;
     5563    for( UInt ui = 0; ui < 4; ++ui )
     5564    {
     5565      uiYCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_LUMA,     uiTrMode + 1 );
     5566      uiUCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_U, uiTrMode + 1 );
     5567      uiVCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_V, uiTrMode + 1 );
     5568    }
     5569    for( UInt ui = 0; ui < 4 * uiQPartNumSubdiv; ++ui )
     5570    {
     5571      pcCU->getCbf( TEXT_LUMA     )[uiAbsPartIdx + ui] |= uiYCbf << uiTrMode;
     5572      pcCU->getCbf( TEXT_CHROMA_U )[uiAbsPartIdx + ui] |= uiUCbf << uiTrMode;
     5573      pcCU->getCbf( TEXT_CHROMA_V )[uiAbsPartIdx + ui] |= uiVCbf << uiTrMode;
     5574    }
     5575   
     5576    if( m_bUseSBACRD )
     5577    {
     5578      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
     5579    }
     5580    m_pcEntropyCoder->resetBits();
     5581   
     5582    {
     5583      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, true,  TEXT_LUMA );
     5584      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_LUMA );
     5585      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_U );
     5586      xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_V );
     5587    }
     5588   
     5589    uiSubdivBits = m_pcEntropyCoder->getNumberOfWrittenBits();
     5590    dSubdivCost  = m_pcRdCost->calcRdCost( uiSubdivBits, uiSubdivDist );
     5591   
     5592    if( uiYCbf || uiUCbf || uiVCbf || !bCheckFull )
     5593    {
     5594      if( dSubdivCost < dSingleCost )
     5595      {
     5596        rdCost += dSubdivCost;
     5597        ruiBits += uiSubdivBits;
     5598        ruiDist += uiSubdivDist;
     5599        return;
     5600      }
     5601    }
     5602    pcCU->setTransformSkipSubParts ( uiBestTransformMode[0], TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5603    if(bCodeChroma)
     5604    {
     5605      pcCU->setTransformSkipSubParts ( uiBestTransformMode[1], TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5606      pcCU->setTransformSkipSubParts ( uiBestTransformMode[2], TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5607    }
     5608    assert( bCheckFull );
     5609    if( m_bUseSBACRD )
     5610    {
     5611      m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
     5612    }
     5613  }
     5614  rdCost += dSingleCost;
     5615  ruiBits += uiSingleBits;
     5616  ruiDist += uiSingleDist;
     5617 
     5618  pcCU->setTrIdxSubParts( uiTrMode, uiAbsPartIdx, uiDepth );
     5619 
     5620  pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
     5621  if( bCodeChroma )
     5622  {
     5623    pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5624    pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
     5625  }
     5626}
     5627
     5628Void TEncSearch::xEncodeResidualQT( TComDataCU* pcCU, UInt uiAbsPartIdx, const UInt uiDepth, Bool bSubdivAndCbf, TextType eType )
     5629{
     5630  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
     5631  const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
     5632  const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
     5633 
     5634  const Bool bSubdiv = uiCurrTrMode != uiTrMode;
     5635 
     5636  const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
     5637
     5638  {
     5639    if( bSubdivAndCbf && uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() && uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
     5640    {
     5641      m_pcEntropyCoder->encodeTransformSubdivFlag( bSubdiv, 5 - uiLog2TrSize );
     5642    }
     5643  }
     5644
     5645  {
     5646#if INTRA_BL
     5647#if NO_RESIDUAL_FLAG_FOR_BLPRED
     5648    assert( !pcCU->isIntra(uiAbsPartIdx) || pcCU->isIntraBL(uiAbsPartIdx));
     5649#else
     5650    assert( !pcCU->isIntra(uiAbsPartIdx) );
     5651#endif
     5652#else
     5653    assert( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA );
     5654#endif
     5655    if( bSubdivAndCbf )
     5656    {
     5657      const Bool bFirstCbfOfCU = uiCurrTrMode == 0;
     5658      if( bFirstCbfOfCU || uiLog2TrSize > 2 )
     5659      {
     5660        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) )
     5661        {
     5662          m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode );
     5663        }
     5664        if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) )
     5665        {
     5666          m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode );
     5667        }
     5668      }
     5669      else if( uiLog2TrSize == 2 )
     5670      {
     5671        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) );
     5672        assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) );
     5673      }
     5674    }
     5675  }
     5676 
     5677  if( !bSubdiv )
     5678  {
     5679    const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
     5680    //assert( 16 == uiNumCoeffPerAbsPartIdxIncrement ); // check
     5681    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
     5682    TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     5683    TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5684    TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5685   
     5686    Bool  bCodeChroma   = true;
     5687    UInt  uiTrModeC     = uiTrMode;
     5688    UInt  uiLog2TrSizeC = uiLog2TrSize-1;
     5689    if( uiLog2TrSize == 2 )
     5690    {
     5691      uiLog2TrSizeC++;
     5692      uiTrModeC    --;
     5693      UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
     5694      bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
     5695    }
     5696   
     5697    if( bSubdivAndCbf )
     5698    {
     5699      {
     5700        m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
     5701      }
     5702    }
     5703    else
     5704    {
     5705      if( eType == TEXT_LUMA     && pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA,     uiTrMode ) )
     5706      {
     5707        Int trWidth  = 1 << uiLog2TrSize;
     5708        Int trHeight = 1 << uiLog2TrSize;
     5709        m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
     5710      }
     5711      if( bCodeChroma )
     5712      {
     5713        Int trWidth  = 1 << uiLog2TrSizeC;
     5714        Int trHeight = 1 << uiLog2TrSizeC;
     5715        if( eType == TEXT_CHROMA_U && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode ) )
     5716        {
     5717          m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
     5718        }
     5719        if( eType == TEXT_CHROMA_V && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode ) )
     5720        {
     5721          m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
     5722        }
     5723      }
     5724    }
     5725  }
     5726  else
     5727  {
     5728    if( bSubdivAndCbf || pcCU->getCbf( uiAbsPartIdx, eType, uiCurrTrMode ) )
     5729    {
     5730      const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
     5731      for( UInt ui = 0; ui < 4; ++ui )
     5732      {
     5733        xEncodeResidualQT( pcCU, uiAbsPartIdx + ui * uiQPartNumSubdiv, uiDepth + 1, bSubdivAndCbf, eType );
     5734      }
     5735    }
     5736  }
     5737}
     5738
     5739Void TEncSearch::xSetResidualQTData( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, UInt uiDepth, Bool bSpatial )
     5740{
     5741  assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
     5742  const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
     5743  const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
     5744
     5745  if( uiCurrTrMode == uiTrMode )
     5746  {
     5747    const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
     5748    const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
     5749
     5750    Bool  bCodeChroma   = true;
     5751    UInt  uiTrModeC     = uiTrMode;
     5752    UInt  uiLog2TrSizeC = uiLog2TrSize-1;
     5753    if( uiLog2TrSize == 2 )
     5754    {
     5755      uiLog2TrSizeC++;
     5756      uiTrModeC    --;
     5757      UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
     5758      bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
     5759    }
     5760
     5761    if( bSpatial )
     5762    {     
     5763      Int trWidth  = 1 << uiLog2TrSize;
     5764      Int trHeight = 1 << uiLog2TrSize;
     5765      m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartLuma    ( pcResi, absTUPartIdx, trWidth , trHeight );
     5766
     5767      if( bCodeChroma )
     5768      {
     5769        {
     5770          m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartChroma( pcResi, uiAbsPartIdx, 1 << uiLog2TrSizeC, 1 << uiLog2TrSizeC );
     5771        }
     5772      }
     5773    }
     5774    else
     5775    {
     5776      UInt    uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
     5777      UInt    uiNumCoeffY = ( 1 << ( uiLog2TrSize << 1 ) );
     5778      TCoeff* pcCoeffSrcY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     5779      TCoeff* pcCoeffDstY = pcCU->getCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     5780      ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
     5781#if ADAPTIVE_QP_SELECTION
     5782      Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     5783      Int* pcArlCoeffDstY = pcCU->getArlCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
     5784      ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
     5785#endif
     5786      if( bCodeChroma )
     5787      {
     5788        UInt    uiNumCoeffC = ( 1 << ( uiLog2TrSizeC << 1 ) );
     5789        TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5790        TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5791        TCoeff* pcCoeffDstU = pcCU->getCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5792        TCoeff* pcCoeffDstV = pcCU->getCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5793        ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
     5794        ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
     5795#if ADAPTIVE_QP_SELECTION
     5796        Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5797        Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5798        Int* pcArlCoeffDstU = pcCU->getArlCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5799        Int* pcArlCoeffDstV = pcCU->getArlCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
     5800        ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
     5801        ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
     5802#endif
     5803      }
     5804    }
     5805  }
     5806  else
     5807  {
     5808    const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
     5809    for( UInt ui = 0; ui < 4; ++ui )
     5810    {
     5811      UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
     5812      xSetResidualQTData( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, bSpatial );
     5813    }
     5814  }
     5815}
     5816
     5817UInt TEncSearch::xModeBitsIntra( TComDataCU* pcCU, UInt uiMode, UInt uiPU, UInt uiPartOffset, UInt uiDepth, UInt uiInitTrDepth )
     5818{
     5819  if( m_bUseSBACRD )
     5820  {
     5821    // Reload only contexts required for coding intra mode information
     5822    m_pcRDGoOnSbacCoder->loadIntraDirModeLuma( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
     5823  }
     5824 
     5825  pcCU->setLumaIntraDirSubParts ( uiMode, uiPartOffset, uiDepth + uiInitTrDepth );
     5826 
     5827  m_pcEntropyCoder->resetBits();
     5828  m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, uiPartOffset);
     5829 
     5830  return m_pcEntropyCoder->getNumberOfWrittenBits();
     5831}
     5832
     5833UInt TEncSearch::xUpdateCandList( UInt uiMode, Double uiCost, UInt uiFastCandNum, UInt * CandModeList, Double * CandCostList )
     5834{
     5835  UInt i;
     5836  UInt shift=0;
     5837 
     5838  while ( shift<uiFastCandNum && uiCost<CandCostList[ uiFastCandNum-1-shift ] ) shift++;
     5839 
     5840  if( shift!=0 )
     5841  {
     5842    for(i=1; i<shift; i++)
     5843    {
     5844      CandModeList[ uiFastCandNum-i ] = CandModeList[ uiFastCandNum-1-i ];
     5845      CandCostList[ uiFastCandNum-i ] = CandCostList[ uiFastCandNum-1-i ];
     5846    }
     5847    CandModeList[ uiFastCandNum-shift ] = uiMode;
     5848    CandCostList[ uiFastCandNum-shift ] = uiCost;
     5849    return 1;
     5850  }
     5851 
     5852  return 0;
     5853}
     5854
     5855/** add inter-prediction syntax elements for a CU block
     5856 * \param pcCU
     5857 * \param uiQp
     5858 * \param uiTrMode
     5859 * \param ruiBits
     5860 * \param rpcYuvRec
     5861 * \param pcYuvPred
     5862 * \param rpcYuvResi
     5863 * \returns Void
     5864 */
     5865Void  TEncSearch::xAddSymbolBitsInter( TComDataCU* pcCU, UInt uiQp, UInt uiTrMode, UInt& ruiBits, TComYuv*& rpcYuvRec, TComYuv*pcYuvPred, TComYuv*& rpcYuvResi )
     5866{
     5867  if(pcCU->getMergeFlag( 0 ) && pcCU->getPartitionSize( 0 ) == SIZE_2Nx2N && !pcCU->getQtRootCbf( 0 ))
     5868  {
     5869    pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
     5870
     5871    m_pcEntropyCoder->resetBits();
     5872    if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
     5873    {
     5874      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
     5875    }
     5876    m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
     5877    m_pcEntropyCoder->encodeMergeIndex(pcCU, 0, true);
     5878    ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
     5879  }
     5880  else
     5881  {
     5882    m_pcEntropyCoder->resetBits();
     5883    if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
     5884    {
     5885      m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
     5886    }
     5887    m_pcEntropyCoder->encodeSkipFlag ( pcCU, 0, true );
     5888#if INTRA_BL
     5889    if(m_pcEncCfg->getLayerId())
     5890    {
     5891      m_pcEntropyCoder->encodeIntraBLFlag(pcCU, 0, true);
     5892#if !NO_RESIDUAL_FLAG_FOR_BLPRED
     5893      assert( pcCU->isIntraBL( 0 ) == false );
     5894#endif
     5895    }
     5896#endif
     5897#if NO_RESIDUAL_FLAG_FOR_BLPRED
     5898    if( !pcCU->isIntraBL(0))
     5899    {
     5900#endif
     5901    m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
     5902    m_pcEntropyCoder->encodePartSize( pcCU, 0, pcCU->getDepth(0), true );
     5903    m_pcEntropyCoder->encodePredInfo( pcCU, 0, true );
     5904#if NO_RESIDUAL_FLAG_FOR_BLPRED
     5905    }
     5906#endif
     5907    Bool bDummy = false;
     5908    m_pcEntropyCoder->encodeCoeff   ( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0), bDummy );
     5909   
     5910    ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
     5911  }
     5912}
     5913
     5914/**
     5915 * \brief Generate half-sample interpolated block
     5916 *
     5917 * \param pattern Reference picture ROI
     5918 * \param biPred    Flag indicating whether block is for biprediction
     5919 */
     5920Void TEncSearch::xExtDIFUpSamplingH( TComPattern* pattern, Bool biPred )
     5921{
     5922  Int width      = pattern->getROIYWidth();
     5923  Int height     = pattern->getROIYHeight();
     5924  Int srcStride  = pattern->getPatternLStride();
     5925 
     5926  Int intStride = m_filteredBlockTmp[0].getStride();
     5927  Int dstStride = m_filteredBlock[0][0].getStride();
     5928  Short *intPtr;
     5929  Short *dstPtr;
     5930  Int filterSize = NTAPS_LUMA;
     5931  Int halfFilterSize = (filterSize>>1);
     5932  Pel *srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
     5933 
     5934  m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[0].getLumaAddr(), intStride, width+1, height+filterSize, 0, false);
     5935  m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[2].getLumaAddr(), intStride, width+1, height+filterSize, 2, false);
     5936 
     5937  intPtr = m_filteredBlockTmp[0].getLumaAddr() + halfFilterSize * intStride + 1; 
     5938  dstPtr = m_filteredBlock[0][0].getLumaAddr();
     5939  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+0, 0, false, true);
     5940 
     5941  intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1; 
     5942  dstPtr = m_filteredBlock[2][0].getLumaAddr();
     5943  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+1, 2, false, true);
     5944 
     5945  intPtr = m_filteredBlockTmp[2].getLumaAddr() + halfFilterSize * intStride;
     5946  dstPtr = m_filteredBlock[0][2].getLumaAddr();
     5947  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+0, 0, false, true);
     5948 
     5949  intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
     5950  dstPtr = m_filteredBlock[2][2].getLumaAddr();
     5951  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+1, 2, false, true);
     5952}
     5953
     5954/**
     5955 * \brief Generate quarter-sample interpolated blocks
     5956 *
     5957 * \param pattern    Reference picture ROI
     5958 * \param halfPelRef Half-pel mv
     5959 * \param biPred     Flag indicating whether block is for biprediction
     5960 */
     5961Void TEncSearch::xExtDIFUpSamplingQ( TComPattern* pattern, TComMv halfPelRef, Bool biPred )
     5962{
     5963  Int width      = pattern->getROIYWidth();
     5964  Int height     = pattern->getROIYHeight();
     5965  Int srcStride  = pattern->getPatternLStride();
     5966 
     5967  Pel *srcPtr;
     5968  Int intStride = m_filteredBlockTmp[0].getStride();
     5969  Int dstStride = m_filteredBlock[0][0].getStride();
     5970  Short *intPtr;
     5971  Short *dstPtr;
     5972  Int filterSize = NTAPS_LUMA;
     5973 
     5974  Int halfFilterSize = (filterSize>>1);
     5975
     5976  Int extHeight = (halfPelRef.getVer() == 0) ? height + filterSize : height + filterSize-1;
     5977 
     5978  // Horizontal filter 1/4
     5979  srcPtr = pattern->getROIY() - halfFilterSize * srcStride - 1;
     5980  intPtr = m_filteredBlockTmp[1].getLumaAddr();
     5981  if (halfPelRef.getVer() > 0)
     5982  {
     5983    srcPtr += srcStride;
     5984  }
     5985  if (halfPelRef.getHor() >= 0)
     5986  {
     5987    srcPtr += 1;
     5988  }
     5989  m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 1, false);
     5990 
     5991  // Horizontal filter 3/4
     5992  srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
     5993  intPtr = m_filteredBlockTmp[3].getLumaAddr();
     5994  if (halfPelRef.getVer() > 0)
     5995  {
     5996    srcPtr += srcStride;
     5997  }
     5998  if (halfPelRef.getHor() > 0)
     5999  {
     6000    srcPtr += 1;
     6001  }
     6002  m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 3, false);       
     6003 
     6004  // Generate @ 1,1
     6005  intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
     6006  dstPtr = m_filteredBlock[1][1].getLumaAddr();
     6007  if (halfPelRef.getVer() == 0)
     6008  {
     6009    intPtr += intStride;
     6010  }
     6011  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
     6012 
     6013  // Generate @ 3,1
     6014  intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
     6015  dstPtr = m_filteredBlock[3][1].getLumaAddr();
     6016  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
     6017 
     6018  if (halfPelRef.getVer() != 0)
     6019  {
     6020    // Generate @ 2,1
     6021    intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
     6022    dstPtr = m_filteredBlock[2][1].getLumaAddr();
     6023    if (halfPelRef.getVer() == 0)
     6024    {
     6025      intPtr += intStride;
     6026    }
     6027    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
     6028   
     6029    // Generate @ 2,3
     6030    intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
     6031    dstPtr = m_filteredBlock[2][3].getLumaAddr();
     6032    if (halfPelRef.getVer() == 0)
     6033    {
     6034      intPtr += intStride;
     6035    }
     6036    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
     6037  }
     6038  else
     6039  {
     6040    // Generate @ 0,1
     6041    intPtr = m_filteredBlockTmp[1].getLumaAddr() + halfFilterSize * intStride;
     6042    dstPtr = m_filteredBlock[0][1].getLumaAddr();
     6043    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
     6044   
     6045    // Generate @ 0,3
     6046    intPtr = m_filteredBlockTmp[3].getLumaAddr() + halfFilterSize * intStride;
     6047    dstPtr = m_filteredBlock[0][3].getLumaAddr();
     6048    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
     6049  }
     6050 
     6051  if (halfPelRef.getHor() != 0)
     6052  {
     6053    // Generate @ 1,2
     6054    intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
     6055    dstPtr = m_filteredBlock[1][2].getLumaAddr();
     6056    if (halfPelRef.getHor() > 0)
     6057    {
     6058      intPtr += 1;
     6059    }
     6060    if (halfPelRef.getVer() >= 0)
     6061    {
     6062      intPtr += intStride;
     6063    }
     6064    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
     6065   
     6066    // Generate @ 3,2
     6067    intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
     6068    dstPtr = m_filteredBlock[3][2].getLumaAddr();
     6069    if (halfPelRef.getHor() > 0)
     6070    {
     6071      intPtr += 1;
     6072    }
     6073    if (halfPelRef.getVer() > 0)
     6074    {
     6075      intPtr += intStride;
     6076    }
     6077    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true); 
     6078  }
     6079  else
     6080  {
     6081    // Generate @ 1,0
     6082    intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
     6083    dstPtr = m_filteredBlock[1][0].getLumaAddr();
     6084    if (halfPelRef.getVer() >= 0)
     6085    {
     6086      intPtr += intStride;
     6087    }
     6088    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
     6089   
     6090    // Generate @ 3,0
     6091    intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
     6092    dstPtr = m_filteredBlock[3][0].getLumaAddr();
     6093    if (halfPelRef.getVer() > 0)
     6094    {
     6095      intPtr += intStride;
     6096    }
     6097    m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
     6098  }
     6099 
     6100  // Generate @ 1,3
     6101  intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
     6102  dstPtr = m_filteredBlock[1][3].getLumaAddr();
     6103  if (halfPelRef.getVer() == 0)
     6104  {
     6105    intPtr += intStride;
     6106  }
     6107  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
     6108 
     6109  // Generate @ 3,3
     6110  intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
     6111  dstPtr = m_filteredBlock[3][3].getLumaAddr();
     6112  m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
     6113}
     6114
     6115/** set wp tables
     6116 * \param TComDataCU* pcCU
     6117 * \param iRefIdx
     6118 * \param eRefPicListCur
     6119 * \returns Void
     6120 */
     6121Void  TEncSearch::setWpScalingDistParam( TComDataCU* pcCU, Int iRefIdx, RefPicList eRefPicListCur )
     6122{
     6123  if ( iRefIdx<0 )
     6124  {
     6125    m_cDistParam.bApplyWeight = false;
     6126    return;
     6127  }
     6128
     6129  TComSlice       *pcSlice  = pcCU->getSlice();
     6130  TComPPS         *pps      = pcCU->getSlice()->getPPS();
     6131  wpScalingParam  *wp0 , *wp1;
     6132  m_cDistParam.bApplyWeight = ( pcSlice->getSliceType()==P_SLICE && pps->getUseWP() ) || ( pcSlice->getSliceType()==B_SLICE && pps->getWPBiPred() ) ;
     6133  if ( !m_cDistParam.bApplyWeight ) return;
     6134
     6135  Int iRefIdx0 = ( eRefPicListCur == REF_PIC_LIST_0 ) ? iRefIdx : (-1);
     6136  Int iRefIdx1 = ( eRefPicListCur == REF_PIC_LIST_1 ) ? iRefIdx : (-1);
     6137
     6138  getWpScaling( pcCU, iRefIdx0, iRefIdx1, wp0 , wp1 );
     6139
     6140  if ( iRefIdx0 < 0 ) wp0 = NULL;
     6141  if ( iRefIdx1 < 0 ) wp1 = NULL;
     6142
     6143  m_cDistParam.wpCur  = NULL;
     6144
     6145  if ( eRefPicListCur == REF_PIC_LIST_0 )
     6146  {
     6147    m_cDistParam.wpCur = wp0;
     6148  }
     6149  else
     6150  {
     6151    m_cDistParam.wpCur = wp1;
     6152  }
    49386153}
    49396154
     
    49616176}
    49626177#endif
    4963 /** encode residual and calculate rate-distortion for a CU block
    4964  * \param pcCU
    4965  * \param pcYuvOrg
    4966  * \param pcYuvPred
    4967  * \param rpcYuvResi
    4968  * \param rpcYuvResiBest
    4969  * \param rpcYuvRec
    4970  * \param bSkipRes
    4971  * \returns Void
    4972  */
    4973 Void TEncSearch::encodeResAndCalcRdInterCU( TComDataCU* pcCU, TComYuv* pcYuvOrg, TComYuv* pcYuvPred, TComYuv*& rpcYuvResi, TComYuv*& rpcYuvResiBest, TComYuv*& rpcYuvRec, Bool bSkipRes )
     6178
     6179#if ENCODER_FAST_MODE
     6180Bool TEncSearch::predInterSearchILRUni( TComDataCU* pcCU, TComYuv* pcOrgYuv, TComYuv*& rpcPredYuv, TComYuv*& rpcResiYuv, TComYuv*& rpcRecoYuv )
    49746181{
    4975 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    4976   if ( pcCU->isIntra(0) && !pcCU->isIntraBL(0))
    4977 #else
    4978   if ( pcCU->isIntra(0) )
    4979 #endif
    4980   {
    4981     return;
    4982   }
    4983  
    4984   Bool      bHighPass    = pcCU->getSlice()->getDepth() ? true : false;
    4985   UInt      uiBits       = 0, uiBitsBest = 0;
    4986   UInt      uiDistortion = 0, uiDistortionBest = 0;
    4987  
    4988   UInt      uiWidth      = pcCU->getWidth ( 0 );
    4989   UInt      uiHeight     = pcCU->getHeight( 0 );
    4990  
    4991   //  No residual coding : SKIP mode
    4992   if ( bSkipRes )
    4993   {
    4994 #if SKIP_FLAG
    4995     pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
    4996 #endif
    4997 
    4998     rpcYuvResi->clear();
    4999    
    5000     pcYuvPred->copyToPartYuv( rpcYuvRec, 0 );
    5001    
    5002 #if WEIGHTED_CHROMA_DISTORTION
    5003     uiDistortion = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    5004     + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true )
    5005     + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true );
    5006 #else
    5007     uiDistortion = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    5008     + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
    5009     + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
    5010 #endif
    5011 
    5012     if( m_bUseSBACRD )
    5013       m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST]);
    5014    
    5015     m_pcEntropyCoder->resetBits();
    5016     if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
    5017     {
    5018       m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
    5019     }
    5020     m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
    5021     m_pcEntropyCoder->encodeMergeIndex( pcCU, 0, 0, true );
    5022    
    5023     uiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
    5024     pcCU->getTotalBits()       = uiBits;
    5025     pcCU->getTotalDistortion() = uiDistortion;
    5026     pcCU->getTotalCost()       = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
    5027    
    5028     if( m_bUseSBACRD )
    5029       m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_TEMP_BEST]);
    5030    
    5031     pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
    5032     pcCU->setTrIdxSubParts( 0, 0, pcCU->getDepth(0) );
    5033    
    5034     return;
    5035   }
    5036  
    5037   //  Residual coding.
    5038   Int    qp, qpBest = 0, qpMin, qpMax;
    5039   Double  dCost, dCostBest = MAX_DOUBLE;
    5040  
    5041   UInt uiTrLevel = 0;
    5042   if( (pcCU->getWidth(0) > pcCU->getSlice()->getSPS()->getMaxTrSize()) )
    5043   {
    5044     while( pcCU->getWidth(0) > (pcCU->getSlice()->getSPS()->getMaxTrSize()<<uiTrLevel) ) uiTrLevel++;
    5045   }
    5046   UInt uiMaxTrMode = pcCU->getSlice()->getSPS()->getMaxTrDepth() + uiTrLevel;
    5047  
    5048   while((uiWidth>>uiMaxTrMode) < (g_uiMaxCUWidth>>g_uiMaxCUDepth)) uiMaxTrMode--;
    5049  
    5050   qpMin =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) - m_iMaxDeltaQP ) : pcCU->getQP( 0 );
    5051   qpMax =  bHighPass ? Clip3( -pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, pcCU->getQP(0) + m_iMaxDeltaQP ) : pcCU->getQP( 0 );
    5052 
    5053   rpcYuvResi->subtract( pcYuvOrg, pcYuvPred, 0, uiWidth );
    5054 
    5055   for ( qp = qpMin; qp <= qpMax; qp++ )
    5056   {
    5057     dCost = 0.;
    5058     uiBits = 0;
    5059     uiDistortion = 0;
    5060     if( m_bUseSBACRD )
    5061     {
    5062       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_CURR_BEST ] );
    5063     }
    5064    
    5065     UInt uiZeroDistortion = 0;
    5066 #if IBDI_DISTORTION
    5067     xEstimateResidualQT( pcCU, 0, 0, 0, pcYuvOrg, pcYuvPred, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
    5068 #else
    5069     xEstimateResidualQT( pcCU, 0, 0, 0, rpcYuvResi,  pcCU->getDepth(0), dCost, uiBits, uiDistortion, &uiZeroDistortion );
    5070 #endif
    5071    
    5072 #if TU_ZERO_CBF_RDO
    5073     m_pcEntropyCoder->resetBits();
    5074     m_pcEntropyCoder->encodeQtRootCbfZero( pcCU, 0 );
    5075     UInt zeroResiBits = m_pcEntropyCoder->getNumberOfWrittenBits();
    5076     double dZeroCost = m_pcRdCost->calcRdCost( zeroResiBits, uiZeroDistortion );
    5077 #else
    5078     double dZeroCost = m_pcRdCost->calcRdCost( 0, uiZeroDistortion );
    5079 #endif
    5080     if(pcCU->isLosslessCoded( 0 ))
    5081     { 
    5082       dZeroCost = dCost + 1;
    5083     }
    5084     if ( dZeroCost < dCost )
    5085     {
    5086       dCost        = dZeroCost;
    5087       uiBits       = 0;
    5088       uiDistortion = uiZeroDistortion;
    5089      
    5090       const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
    5091       ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
    5092       ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
    5093       ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
    5094       ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
    5095       ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
    5096       ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5097       ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5098 #if INTER_TRANSFORMSKIP
    5099       pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
    5100 #endif
    5101     }
    5102 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    5103     else if(pcCU->getLayerId() > 0 && pcCU->isIntraBL(0) && uiZeroDistortion == uiDistortion) // all zeros
    5104     {
    5105       const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
    5106       ::memset( pcCU->getTransformIdx()      , 0, uiQPartNum * sizeof(UChar) );
    5107       ::memset( pcCU->getCbf( TEXT_LUMA )    , 0, uiQPartNum * sizeof(UChar) );
    5108       ::memset( pcCU->getCbf( TEXT_CHROMA_U ), 0, uiQPartNum * sizeof(UChar) );
    5109       ::memset( pcCU->getCbf( TEXT_CHROMA_V ), 0, uiQPartNum * sizeof(UChar) );
    5110       ::memset( pcCU->getCoeffY()            , 0, uiWidth * uiHeight * sizeof( TCoeff )      );
    5111       ::memset( pcCU->getCoeffCb()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5112       ::memset( pcCU->getCoeffCr()           , 0, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5113 #if INTER_TRANSFORMSKIP
    5114       pcCU->setTransformSkipSubParts ( 0, 0, 0, 0, pcCU->getDepth(0) );
    5115 #endif
    5116     }
    5117 #endif
    5118     else
    5119     {
    5120       xSetResidualQTData( pcCU, 0, 0, 0, NULL, pcCU->getDepth(0), false );
    5121     }
    5122    
    5123     if( m_bUseSBACRD )
    5124     {
    5125       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
    5126     }
    5127 #if 0 // check
    5128     {
    5129       m_pcEntropyCoder->resetBits();
    5130       m_pcEntropyCoder->encodeCoeff( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0) );
    5131       const UInt uiBitsForCoeff = m_pcEntropyCoder->getNumberOfWrittenBits();
    5132       if( m_bUseSBACRD )
    5133       {
    5134         m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[pcCU->getDepth(0)][CI_CURR_BEST] );
    5135       }
    5136       if( uiBitsForCoeff != uiBits )
    5137         assert( 0 );
    5138     }
    5139 #endif
    5140     uiBits = 0;
    5141     {
    5142       TComYuv *pDummy = NULL;
    5143       xAddSymbolBitsInter( pcCU, 0, 0, uiBits, pDummy, NULL, pDummy );
    5144     }
    5145    
    5146    
    5147     Double dExactCost = m_pcRdCost->calcRdCost( uiBits, uiDistortion );
    5148     dCost = dExactCost;
    5149    
    5150     if ( dCost < dCostBest )
    5151     {
    5152       if ( !pcCU->getQtRootCbf( 0 ) )
    5153       {
    5154         rpcYuvResiBest->clear();
    5155       }
    5156       else
    5157       {
    5158         xSetResidualQTData( pcCU, 0, 0, 0, rpcYuvResiBest, pcCU->getDepth(0), true );
    5159       }
    5160      
    5161       if( qpMin != qpMax && qp != qpMax )
    5162       {
    5163         const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
    5164         ::memcpy( m_puhQTTempTrIdx, pcCU->getTransformIdx(),        uiQPartNum * sizeof(UChar) );
    5165         ::memcpy( m_puhQTTempCbf[0], pcCU->getCbf( TEXT_LUMA ),     uiQPartNum * sizeof(UChar) );
    5166         ::memcpy( m_puhQTTempCbf[1], pcCU->getCbf( TEXT_CHROMA_U ), uiQPartNum * sizeof(UChar) );
    5167         ::memcpy( m_puhQTTempCbf[2], pcCU->getCbf( TEXT_CHROMA_V ), uiQPartNum * sizeof(UChar) );
    5168         ::memcpy( m_pcQTTempCoeffY,  pcCU->getCoeffY(),  uiWidth * uiHeight * sizeof( TCoeff )      );
    5169         ::memcpy( m_pcQTTempCoeffCb, pcCU->getCoeffCb(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5170         ::memcpy( m_pcQTTempCoeffCr, pcCU->getCoeffCr(), uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5171 #if ADAPTIVE_QP_SELECTION
    5172         ::memcpy( m_pcQTTempArlCoeffY,  pcCU->getArlCoeffY(),  uiWidth * uiHeight * sizeof( Int )      );
    5173         ::memcpy( m_pcQTTempArlCoeffCb, pcCU->getArlCoeffCb(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
    5174         ::memcpy( m_pcQTTempArlCoeffCr, pcCU->getArlCoeffCr(), uiWidth * uiHeight * sizeof( Int ) >> 2 );
    5175 #endif
    5176 #if INTER_TRANSFORMSKIP
    5177         ::memcpy( m_puhQTTempTransformSkipFlag[0], pcCU->getTransformSkip(TEXT_LUMA),     uiQPartNum * sizeof( UChar ) );
    5178         ::memcpy( m_puhQTTempTransformSkipFlag[1], pcCU->getTransformSkip(TEXT_CHROMA_U), uiQPartNum * sizeof( UChar ) );
    5179         ::memcpy( m_puhQTTempTransformSkipFlag[2], pcCU->getTransformSkip(TEXT_CHROMA_V), uiQPartNum * sizeof( UChar ) );
    5180 #endif
    5181       }
    5182       uiBitsBest       = uiBits;
    5183       uiDistortionBest = uiDistortion;
    5184       dCostBest        = dCost;
    5185       qpBest           = qp;
    5186       if( m_bUseSBACRD )
    5187       {
    5188         m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
    5189       }
    5190     }
    5191   }
    5192  
    5193   assert ( dCostBest != MAX_DOUBLE );
    5194  
    5195   if( qpMin != qpMax && qpBest != qpMax )
    5196   {
    5197     if( m_bUseSBACRD )
    5198     {
    5199       assert( 0 ); // check
    5200       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ pcCU->getDepth( 0 ) ][ CI_TEMP_BEST ] );
    5201     }
    5202     // copy best cbf and trIdx to pcCU
    5203     const UInt uiQPartNum = pcCU->getPic()->getNumPartInCU() >> (pcCU->getDepth(0) << 1);
    5204     ::memcpy( pcCU->getTransformIdx(),       m_puhQTTempTrIdx,  uiQPartNum * sizeof(UChar) );
    5205     ::memcpy( pcCU->getCbf( TEXT_LUMA ),     m_puhQTTempCbf[0], uiQPartNum * sizeof(UChar) );
    5206     ::memcpy( pcCU->getCbf( TEXT_CHROMA_U ), m_puhQTTempCbf[1], uiQPartNum * sizeof(UChar) );
    5207     ::memcpy( pcCU->getCbf( TEXT_CHROMA_V ), m_puhQTTempCbf[2], uiQPartNum * sizeof(UChar) );
    5208     ::memcpy( pcCU->getCoeffY(),  m_pcQTTempCoeffY,  uiWidth * uiHeight * sizeof( TCoeff )      );
    5209     ::memcpy( pcCU->getCoeffCb(), m_pcQTTempCoeffCb, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5210     ::memcpy( pcCU->getCoeffCr(), m_pcQTTempCoeffCr, uiWidth * uiHeight * sizeof( TCoeff ) >> 2 );
    5211 #if ADAPTIVE_QP_SELECTION
    5212     ::memcpy( pcCU->getArlCoeffY(),  m_pcQTTempArlCoeffY,  uiWidth * uiHeight * sizeof( Int )      );
    5213     ::memcpy( pcCU->getArlCoeffCb(), m_pcQTTempArlCoeffCb, uiWidth * uiHeight * sizeof( Int ) >> 2 );
    5214     ::memcpy( pcCU->getArlCoeffCr(), m_pcQTTempArlCoeffCr, uiWidth * uiHeight * sizeof( Int ) >> 2 );
    5215 #endif
    5216 #if INTER_TRANSFORMSKIP
    5217     ::memcpy( pcCU->getTransformSkip(TEXT_LUMA),     m_puhQTTempTransformSkipFlag[0], uiQPartNum * sizeof( UChar ) );
    5218     ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_U), m_puhQTTempTransformSkipFlag[1], uiQPartNum * sizeof( UChar ) );
    5219     ::memcpy( pcCU->getTransformSkip(TEXT_CHROMA_V), m_puhQTTempTransformSkipFlag[2], uiQPartNum * sizeof( UChar ) );
    5220 #endif
    5221   }
    5222   rpcYuvRec->addClip ( pcYuvPred, rpcYuvResiBest, 0, uiWidth );
    5223  
    5224   // update with clipped distortion and cost (qp estimation loop uses unclipped values)
    5225 #if WEIGHTED_CHROMA_DISTORTION
    5226   uiDistortionBest = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    5227   + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true )
    5228   + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1, true );
    5229 #else
    5230   uiDistortionBest = m_pcRdCost->getDistPart( rpcYuvRec->getLumaAddr(), rpcYuvRec->getStride(),  pcYuvOrg->getLumaAddr(), pcYuvOrg->getStride(),  uiWidth,      uiHeight      )
    5231   + m_pcRdCost->getDistPart( rpcYuvRec->getCbAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCbAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 )
    5232   + m_pcRdCost->getDistPart( rpcYuvRec->getCrAddr(),   rpcYuvRec->getCStride(), pcYuvOrg->getCrAddr(),   pcYuvOrg->getCStride(), uiWidth >> 1, uiHeight >> 1 );
    5233 #endif
    5234   dCostBest = m_pcRdCost->calcRdCost( uiBitsBest, uiDistortionBest );
    5235  
    5236   pcCU->getTotalBits()       = uiBitsBest;
    5237   pcCU->getTotalDistortion() = uiDistortionBest;
    5238   pcCU->getTotalCost()       = dCostBest;
    5239  
    5240   if ( pcCU->isSkipped(0) )
    5241   {
    5242     pcCU->setCbfSubParts( 0, 0, 0, 0, pcCU->getDepth( 0 ) );
    5243   }
    5244  
    5245   pcCU->setQPSubParts( qpBest, 0, pcCU->getDepth(0) );
     6182  rpcPredYuv->clear();
     6183  rpcRecoYuv->clear();
     6184
     6185  Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
     6186
     6187  TComMv        cMv[2];
     6188  TComMv        cMvPred[2][33];
     6189  TComMv        cMvTemp[2][33];
     6190  TComMv        TempMv;
     6191
     6192  Int           iRefIdx[2]={0,0};
     6193
     6194  Int           aaiMvpIdx[2][33];
     6195  Int           aaiMvpNum[2][33];
     6196
     6197  UInt          uiMbBits[3] = {1, 1, 0};
     6198  UInt          uiLastMode = 0;
     6199
     6200  UInt          uiCost[2]   = { MAX_UINT, MAX_UINT };     //uni, rdCost
     6201  UInt          uiCostTemp;
     6202  UInt          biPDistTemp = MAX_INT;
     6203  UInt          uiBitsTemp;
     6204
     6205  PartSize      ePartSize = pcCU->getPartitionSize( 0 );  //2Nx2N
     6206  Int           iPartIdx  = 0;                            //one PU in CU
     6207  UInt          uiPartAddr;
     6208  Int           iRoiWidth, iRoiHeight;
     6209
     6210  xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
     6211  pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
     6212
     6213  for( Int iRefList = 0; iRefList < iNumPredDir; iRefList++)  //list
     6214  {
     6215    RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
     6216
     6217    Int  iRefIdxTemp = -1;
     6218    Bool foundILR    = false;
     6219    for( Int refIdx = 0; refIdx < pcCU->getSlice()->getNumRefIdx(eRefPicList); refIdx++ )
     6220    {
     6221      if( pcCU->getSlice()->getRefPic(eRefPicList, refIdx)->isILR() )
     6222      {
     6223        iRefIdxTemp = refIdx;
     6224        foundILR    = true;
     6225        break;
     6226      }
     6227    }
     6228
     6229    if(!foundILR)  //no ILR in eRefPiclist
     6230    {
     6231      continue; 
     6232    }
     6233
     6234    uiBitsTemp = uiMbBits[iRefList];
     6235    if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
     6236    {
     6237      uiBitsTemp += iRefIdxTemp+1;
     6238      if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
     6239    }
     6240
     6241    xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
     6242    aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
     6243    aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
     6244
     6245    uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
     6246
     6247    xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
     6248    xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
     6249
     6250    if( uiCostTemp < uiCost[iRefList] )
     6251    {
     6252      uiCost[iRefList] = uiCostTemp;
     6253
     6254      cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
     6255      iRefIdx[iRefList] = iRefIdxTemp;
     6256
     6257      pcCU->getCUMvField(eRefPicList)->setAllMv( cMv[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     6258      pcCU->getCUMvField(eRefPicList)->setAllRefIdx( iRefIdx[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
     6259    }
     6260  }
     6261
     6262  if( uiCost[0] == MAX_UINT && uiCost[1] == MAX_UINT )  //no ILR in both list0 and list1
     6263  {
     6264    return false;
     6265  }
     6266
     6267  //  Clear Motion Field
     6268  pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
     6269  pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( TComMvField(), ePartSize, uiPartAddr, 0, iPartIdx );
     6270  pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx );
     6271  pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TComMv(),      ePartSize, uiPartAddr, 0, iPartIdx );
     6272
     6273  pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6274  pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6275  pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6276  pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6277
     6278  if( uiCost[0] <= uiCost[1] )  //list0 ILR
     6279  {
     6280    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv    ( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
     6281    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
     6282
     6283    TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
     6284    pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
     6285
     6286    pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
     6287
     6288    pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6289    pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6290  }
     6291  else if( uiCost[1] < uiCost[0] )  //list1 ILR
     6292  {
     6293    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv    ( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
     6294    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
     6295
     6296    TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
     6297    pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd( TempMv, ePartSize, uiPartAddr, 0, iPartIdx );
     6298
     6299    pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
     6300
     6301    pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6302    pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     6303  }
     6304  else
     6305  {
     6306    assert(0);
     6307  }
     6308
     6309  pcCU->setMergeFlagSubParts( false, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
     6310
     6311  motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
     6312
     6313  setWpScalingDistParam( pcCU, -1, REF_PIC_LIST_X );
     6314
     6315  return true;
    52466316}
    52476317
    5248 #if IBDI_DISTORTION
    5249 Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcOrg, TComYuv* pcPred, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, UInt &ruiDist, UInt *puiZeroDist )
    5250 #else
    5251 Void TEncSearch::xEstimateResidualQT( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, const UInt uiDepth, Double &rdCost, UInt &ruiBits, UInt &ruiDist, UInt *puiZeroDist )
    5252 #endif
     6318
     6319#endif
     6320
     6321#if INTRA_BL
     6322Void
     6323TEncSearch::estIntraBLPredQT( TComDataCU* pcCU,
     6324                           TComYuv*    pcOrgYuv,
     6325                           TComYuv*    pcPredYuv,
     6326                           TComYuv*    pcResiYuv,
     6327                           TComYuv*    pcRecoYuv )
    52536328{
    5254   const UInt uiTrMode = uiDepth - pcCU->getDepth( 0 );
    5255  
    5256   assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
    5257   const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
    5258  
    5259   UInt SplitFlag = ((pcCU->getSlice()->getSPS()->getQuadtreeTUMaxDepthInter() == 1) && pcCU->getPredictionMode(uiAbsPartIdx) == MODE_INTER && ( pcCU->getPartitionSize(uiAbsPartIdx) != SIZE_2Nx2N ));
    5260   Bool bCheckFull;
    5261   if ( SplitFlag && uiDepth == pcCU->getDepth(uiAbsPartIdx) && ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) ) )
    5262      bCheckFull = false;
     6329  UInt    uiDepth        = pcCU->getDepth(0);
     6330  UInt    uiOverallDistY = 0;
     6331  UInt    uiOverallDistC = 0;
     6332 
     6333  //===== set QP and clear Cbf =====
     6334  if ( pcCU->getSlice()->getPPS()->getUseDQP() == true)
     6335  {
     6336    pcCU->setQPSubParts( pcCU->getQP(0), 0, uiDepth );
     6337  }
    52636338  else
    5264      bCheckFull =  ( uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() );
    5265 
    5266   const Bool bCheckSplit  = ( uiLog2TrSize >  pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) );
    5267  
    5268   assert( bCheckFull || bCheckSplit );
    5269  
    5270   Bool  bCodeChroma   = true;
    5271   UInt  uiTrModeC     = uiTrMode;
    5272   UInt  uiLog2TrSizeC = uiLog2TrSize-1;
    5273   if( uiLog2TrSize == 2 )
    5274   {
    5275     uiLog2TrSizeC++;
    5276     uiTrModeC    --;
    5277     UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
    5278     bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
    5279   }
    5280  
    5281   const UInt uiSetCbf = 1 << uiTrMode;
    5282   // code full block
    5283   Double dSingleCost = MAX_DOUBLE;
    5284   UInt uiSingleBits = 0;
    5285   UInt uiSingleDist = 0;
    5286   UInt uiAbsSumY = 0, uiAbsSumU = 0, uiAbsSumV = 0;
    5287 #if INTER_TRANSFORMSKIP
    5288   UInt uiBestTransformMode[3] = {0};
    5289 #endif
    5290 
     6339  {
     6340    pcCU->setQPSubParts( pcCU->getSlice()->getSliceQp(), 0, uiDepth );
     6341  }
     6342 
     6343  //===== init pattern for luma prediction =====
     6344  Bool bAboveAvail = false;
     6345  Bool bLeftAvail  = false;
     6346  pcCU->getPattern()->initPattern   ( pcCU, 0, 0 );
     6347  pcCU->getPattern()->initAdiPattern( pcCU, 0, 0, m_piYuvExt, m_iYuvExtStride, m_iYuvExtHeight, bAboveAvail, bLeftAvail );
     6348 
     6349  pcCU->setLumaIntraDirSubParts ( DC_IDX, 0, uiDepth );
     6350 
     6351  // set context models
    52916352  if( m_bUseSBACRD )
    52926353  {
    5293     m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    5294   }
    5295  
    5296   if( bCheckFull )
    5297   {
    5298     const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
    5299     const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    5300     TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    5301     TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    5302     TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    5303 #if ADAPTIVE_QP_SELECTION   
    5304     Int *pcArlCoeffCurrY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    5305     Int *pcArlCoeffCurrU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    5306     Int *pcArlCoeffCurrV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);   
    5307 #endif
    5308    
    5309     Int trWidth = 0, trHeight = 0, trWidthC = 0, trHeightC = 0;
    5310     UInt absTUPartIdxC = uiAbsPartIdx;
    5311 
    5312     trWidth  = trHeight  = 1 << uiLog2TrSize;
    5313     trWidthC = trHeightC = 1 <<uiLog2TrSizeC;
    5314 #if !REMOVE_NSQT
    5315     pcCU->getNSQTSize ( uiTrMode, uiAbsPartIdx, trWidth, trHeight );
    5316     pcCU->getNSQTSize ( uiTrModeC, uiAbsPartIdx, trWidthC, trHeightC );
    5317 
    5318     if( bCodeChroma && pcCU->useNonSquareTrans( uiTrMode, uiAbsPartIdx ) && !( uiLog2TrSizeC  == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() && uiTrModeC == 1 ) )
    5319     { 
    5320       absTUPartIdxC = pcCU->getNSAddrChroma( uiLog2TrSizeC, uiTrModeC, uiQuadrant, absTUPartIdx );
    5321     }
    5322 #endif
    5323     pcCU->setTrIdxSubParts( uiDepth - pcCU->getDepth( 0 ), uiAbsPartIdx, uiDepth );
    5324 #if INTER_TRANSFORMSKIP
    5325     Double minCostY = MAX_DOUBLE;
    5326     Double minCostU = MAX_DOUBLE;
    5327     Double minCostV = MAX_DOUBLE;
    5328 #if PPS_TS_FLAG
    5329     Bool checkTransformSkipY  = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidth == 4 && trHeight == 4;
    5330     Bool checkTransformSkipUV = pcCU->getSlice()->getPPS()->getUseTransformSkip() && trWidthC == 4 && trHeightC == 4;
    5331 #else
    5332     Bool checkTransformSkipY  = pcCU->getSlice()->getSPS()->getUseTransformSkip() && trWidth == 4 && trHeight == 4;
    5333     Bool checkTransformSkipUV = pcCU->getSlice()->getSPS()->getUseTransformSkip() && trWidthC == 4 && trHeightC == 4;
    5334 #endif
    5335 
    5336     checkTransformSkipY         &= (!pcCU->isLosslessCoded(0));
    5337     checkTransformSkipUV        &= (!pcCU->isLosslessCoded(0));
    5338 
    5339     pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5340     if( bCodeChroma )
    5341     {
    5342       pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5343       pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5344     }
    5345 #endif
    5346 
    5347     if (m_pcEncCfg->getUseRDOQ())
    5348     {
    5349       m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
    5350     }
    5351 
    5352     m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
    5353 
    5354 #if RDOQ_CHROMA_LAMBDA
    5355     m_pcTrQuant->selectLambda(TEXT_LUMA); 
    5356 #endif
    5357     m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY,
    5358 #if ADAPTIVE_QP_SELECTION
    5359                                  pcArlCoeffCurrY,
    5360 #endif     
    5361                                  trWidth,   trHeight,    uiAbsSumY, TEXT_LUMA,     uiAbsPartIdx );
    5362    
    5363     pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5364    
    5365     if( bCodeChroma )
    5366     {
    5367       if (m_pcEncCfg->getUseRDOQ())
    5368       {
    5369         m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
    5370       }
    5371 
    5372 #if CHROMA_QP_EXTENSION
    5373       Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
    5374       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5375 #else
    5376       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCbQpOffset() );
    5377 #endif
    5378 
    5379 #if RDOQ_CHROMA_LAMBDA
    5380       m_pcTrQuant->selectLambda(TEXT_CHROMA);
    5381 #endif
    5382 
    5383       m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU,
    5384 #if ADAPTIVE_QP_SELECTION
    5385                                  pcArlCoeffCurrU,
    5386 #endif       
    5387                                  trWidthC, trHeightC, uiAbsSumU, TEXT_CHROMA_U, uiAbsPartIdx );
    5388 
    5389 #if CHROMA_QP_EXTENSION
    5390       curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
    5391       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5392 #else
    5393       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCrQpOffset() );
    5394 #endif
    5395       m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV,
    5396 #if ADAPTIVE_QP_SELECTION
    5397                                  pcArlCoeffCurrV,
    5398 #endif       
    5399                                  trWidthC, trHeightC, uiAbsSumV, TEXT_CHROMA_V, uiAbsPartIdx );
    5400 
    5401       pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5402       pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5403     }
    5404    
    5405     m_pcEntropyCoder->resetBits();
    5406    
    5407     {
    5408       m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    5409     }
    5410    
    5411     m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx,  trWidth,  trHeight,    uiDepth, TEXT_LUMA );
    5412     const UInt uiSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
    5413    
    5414     UInt uiSingleBitsU = 0;
    5415     UInt uiSingleBitsV = 0;
    5416     if( bCodeChroma )
    5417     {
    5418       {
    5419         m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
    5420       }
    5421       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
    5422       uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsY;
    5423      
    5424       {
    5425         m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
    5426       }
    5427       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
    5428       uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - ( uiSingleBitsY + uiSingleBitsU );
    5429     }
    5430    
    5431     const UInt uiNumSamplesLuma = 1 << (uiLog2TrSize<<1);
    5432     const UInt uiNumSamplesChro = 1 << (uiLog2TrSizeC<<1);
    5433    
    5434     ::memset( m_pTempPel, 0, sizeof( Pel ) * uiNumSamplesLuma ); // not necessary needed for inside of recursion (only at the beginning)
    5435    
    5436 #if IBDI_DISTORTION
    5437     UInt uiDistY = m_pcRdCost->getDistPart( pcPred->getLumaAddr( absTUPartIdx ), pcPred->getStride(), pcOrg->getLumaAddr( absTUPartIdx), pcOrg->getStride(), trWidth, trHeight);
    5438 #else
    5439     UInt uiDistY = m_pcRdCost->getDistPart( m_pTempPel, trWidth, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight ); // initialized with zero residual destortion
    5440 #endif
    5441     if ( puiZeroDist )
    5442     {
    5443       *puiZeroDist += uiDistY;
    5444     }
    5445     if( uiAbsSumY )
    5446     {
    5447       Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
    5448 
    5449       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
    5450 
    5451       Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
    5452       assert(scalingListType < 6);     
    5453 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    5454       if(pcCU->isIntraBL(uiAbsPartIdx) )
    5455       {
    5456         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
    5457       }
    5458       else
    5459       {
    5460         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
    5461       }
    5462 #else
    5463       m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType );//this is for inter mode only
    5464 #endif
    5465      
    5466       const UInt uiNonzeroDistY = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
    5467       pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth,trHeight );
    5468       if (pcCU->isLosslessCoded(0))
    5469       {
    5470         uiDistY = uiNonzeroDistY;
    5471       }
    5472       else
    5473       {
    5474         const Double singleCostY = m_pcRdCost->calcRdCost( uiSingleBitsY, uiNonzeroDistY );
    5475 #if TU_ZERO_CBF_RDO
    5476         m_pcEntropyCoder->resetBits();
    5477         m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    5478         const UInt uiNullBitsY   = m_pcEntropyCoder->getNumberOfWrittenBits();
    5479         const Double nullCostY   = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
    5480 #else
    5481         const Double nullCostY   = m_pcRdCost->calcRdCost( 0, uiDistY );
    5482 #endif
    5483         if( nullCostY < singleCostY ) 
    5484         {   
    5485           uiAbsSumY = 0;
    5486           ::memset( pcCoeffCurrY, 0, sizeof( TCoeff ) * uiNumSamplesLuma );
    5487 #if INTER_TRANSFORMSKIP
    5488           if( checkTransformSkipY )
    5489           {
    5490             minCostY = nullCostY;
    5491           }
    5492 #endif
    5493         }
    5494         else
    5495         {
    5496           uiDistY = uiNonzeroDistY;
    5497 #if INTER_TRANSFORMSKIP
    5498           if( checkTransformSkipY )
    5499           {
    5500             minCostY = singleCostY;
    5501           }
    5502 #endif
    5503         }
    5504       }
    5505     }
    5506 #if INTER_TRANSFORMSKIP
    5507     else if( checkTransformSkipY )
    5508     {
    5509 #if TU_ZERO_CBF_RDO
    5510       m_pcEntropyCoder->resetBits();
    5511       m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_LUMA, uiTrMode );
    5512       const UInt uiNullBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
    5513       minCostY = m_pcRdCost->calcRdCost( uiNullBitsY, uiDistY );
    5514 #else
    5515       minCostY = m_pcRdCost->calcRdCost( 0, uiDistY );
    5516 #endif
    5517     }
    5518 #endif
    5519 
    5520     if( !uiAbsSumY )
    5521     {
    5522       Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx );
    5523       const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride();
    5524       for( UInt uiY = 0; uiY < trHeight; ++uiY )
    5525       {
    5526         ::memset( pcPtr, 0, sizeof( Pel ) * trWidth );
    5527         pcPtr += uiStride;
    5528       }
    5529     }
    5530    
    5531     UInt uiDistU = 0;
    5532     UInt uiDistV = 0;
    5533     if( bCodeChroma )
    5534     {
    5535 #if IBDI_DISTORTION
    5536       uiDistU = m_pcRdCost->getDistPart( pcPred->getCbAddr( absTUPartIdxC ), pcPred->getCStride(), pcOrg->getCbAddr( absTUPartIdxC ), pcOrg->getCStride(), trWidthC, trHeightC
    5537 #if WEIGHTED_CHROMA_DISTORTION
    5538                                           , true
    5539 #endif
    5540                                           );
    5541 #else
    5542       uiDistU = m_pcRdCost->getDistPart( m_pTempPel, trWidthC, pcResi->getCbAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
    5543 #if WEIGHTED_CHROMA_DISTORTION
    5544                                           , true
    5545 #endif
    5546                                           ); // initialized with zero residual destortion
    5547 #endif
    5548       if ( puiZeroDist )
    5549       {
    5550         *puiZeroDist += uiDistU;
    5551       }
    5552       if( uiAbsSumU )
    5553       {
    5554         Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
    5555 
    5556 #if CHROMA_QP_EXTENSION
    5557         Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
    5558         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5559 #else
    5560         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCbQpOffset() );
    5561 #endif
    5562 
    5563         Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
    5564         assert(scalingListType < 6);
    5565         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType  );
    5566        
    5567         const UInt uiNonzeroDistU = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5568           pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
    5569 #if WEIGHTED_CHROMA_DISTORTION
    5570           , true
    5571 #endif
    5572           );
    5573 
    5574         if(pcCU->isLosslessCoded(0)) 
    5575         {
    5576           uiDistU = uiNonzeroDistU;
    5577         }
    5578         else
    5579         {
    5580           const Double dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
    5581 #if TU_ZERO_CBF_RDO
    5582           m_pcEntropyCoder->resetBits();
    5583           m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_CHROMA_U,     uiTrMode );
    5584           const UInt uiNullBitsU    = m_pcEntropyCoder->getNumberOfWrittenBits();
    5585           const Double dNullCostU   = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
    5586 #else
    5587           const Double dNullCostU   = m_pcRdCost->calcRdCost( 0, uiDistU );
    5588 #endif
    5589           if( dNullCostU < dSingleCostU )
    5590           {
    5591             uiAbsSumU = 0;
    5592             ::memset( pcCoeffCurrU, 0, sizeof( TCoeff ) * uiNumSamplesChro );
    5593 #if INTER_TRANSFORMSKIP
    5594             if( checkTransformSkipUV )
    5595             {
    5596               minCostU = dNullCostU;
    5597             }
    5598 #endif
    5599           }
    5600           else
    5601           {
    5602             uiDistU = uiNonzeroDistU;
    5603 #if INTER_TRANSFORMSKIP
    5604             if( checkTransformSkipUV )
    5605             {
    5606               minCostU = dSingleCostU;
    5607             }
    5608 #endif
    5609           }
    5610         }
    5611       }
    5612 #if INTER_TRANSFORMSKIP
    5613       else if( checkTransformSkipUV )
    5614       {
    5615 #if TU_ZERO_CBF_RDO
    5616         m_pcEntropyCoder->resetBits();
    5617         m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrModeC );
    5618         const UInt uiNullBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();
    5619         minCostU = m_pcRdCost->calcRdCost( uiNullBitsU, uiDistU );
    5620 #else
    5621         minCostU = m_pcRdCost->calcRdCost( 0, uiDistU );
    5622 #endif
    5623       }
    5624 #endif
    5625       if( !uiAbsSumU )
    5626       {
    5627         Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
    5628           const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
    5629         for( UInt uiY = 0; uiY < trHeightC; ++uiY )
    5630         {
    5631           ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
    5632           pcPtr += uiStride;
    5633         }
    5634       }
    5635      
    5636 #if IBDI_DISTORTION
    5637       uiDistV = m_pcRdCost->getDistPart( pcPred->getCrAddr( absTUPartIdxC ), pcPred->getCStride(), pcOrg->getCrAddr( absTUPartIdxC ), pcOrg->getCStride(), trWidthC, trHeightC
    5638 #if WEIGHTED_CHROMA_DISTORTION
    5639                                           , true
    5640 #endif
    5641                                           );
    5642 #else
    5643       uiDistV = m_pcRdCost->getDistPart( m_pTempPel, trWidthC, pcResi->getCrAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
    5644 #if WEIGHTED_CHROMA_DISTORTION
    5645                                           , true
    5646 #endif
    5647                                           ); // initialized with zero residual destortion
    5648 #endif
    5649       if ( puiZeroDist )
    5650       {
    5651         *puiZeroDist += uiDistV;
    5652       }
    5653       if( uiAbsSumV )
    5654       {
    5655         Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
    5656         if( !uiAbsSumU )
    5657         {
    5658 #if CHROMA_QP_EXTENSION
    5659           Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
    5660           m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5661 #else
    5662           m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCrQpOffset() );
    5663 #endif
    5664         }
    5665         Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
    5666         assert(scalingListType < 6);
    5667         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType );
    5668        
    5669         const UInt uiNonzeroDistV = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5670           pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
    5671 #if WEIGHTED_CHROMA_DISTORTION
    5672                                                    , true
    5673 #endif
    5674                                                    );
    5675         if (pcCU->isLosslessCoded(0))
    5676         {
    5677           uiDistV = uiNonzeroDistV;
    5678         }
    5679         else
    5680         {
    5681           const Double dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
    5682 #if TU_ZERO_CBF_RDO
    5683           m_pcEntropyCoder->resetBits();
    5684           m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_CHROMA_V,     uiTrMode );
    5685           const UInt uiNullBitsV    = m_pcEntropyCoder->getNumberOfWrittenBits();
    5686           const Double dNullCostV   = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
    5687 #else
    5688           const Double dNullCostV   = m_pcRdCost->calcRdCost( 0, uiDistV );
    5689 #endif
    5690           if( dNullCostV < dSingleCostV )
    5691           {
    5692             uiAbsSumV = 0;
    5693             ::memset( pcCoeffCurrV, 0, sizeof( TCoeff ) * uiNumSamplesChro );
    5694 #if INTER_TRANSFORMSKIP
    5695             if( checkTransformSkipUV )
    5696             {
    5697               minCostV = dNullCostV;
    5698             }
    5699 #endif
    5700           }
    5701           else
    5702           {
    5703             uiDistV = uiNonzeroDistV;
    5704 #if INTER_TRANSFORMSKIP
    5705             if( checkTransformSkipUV )
    5706             {
    5707               minCostV = dSingleCostV;
    5708             }
    5709 #endif
    5710           }
    5711         }
    5712       }
    5713 #if INTER_TRANSFORMSKIP
    5714       else if( checkTransformSkipUV )
    5715       {
    5716 #if TU_ZERO_CBF_RDO
    5717         m_pcEntropyCoder->resetBits();
    5718         m_pcEntropyCoder->encodeQtCbfZero( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrModeC );
    5719         const UInt uiNullBitsV = m_pcEntropyCoder->getNumberOfWrittenBits();
    5720         minCostV = m_pcRdCost->calcRdCost( uiNullBitsV, uiDistV );
    5721 #else
    5722         minCostV = m_pcRdCost->calcRdCost( 0, uiDistV );
    5723 #endif
    5724       }
    5725 #endif
    5726       if( !uiAbsSumV )
    5727       {
    5728         Pel *pcPtr =  m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
    5729         const UInt uiStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
    5730         for( UInt uiY = 0; uiY < trHeightC; ++uiY )
    5731         {   
    5732           ::memset( pcPtr, 0, sizeof(Pel) * trWidthC );
    5733           pcPtr += uiStride;
    5734         }
    5735       }
    5736     }
    5737     pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5738     if( bCodeChroma )
    5739     {
    5740       pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5741       pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5742     }
    5743 
    5744 #if INTER_TRANSFORMSKIP
    5745     if( checkTransformSkipY )
    5746     {
    5747       UInt uiNonzeroDistY, uiAbsSumTransformSkipY;
    5748       Double dSingleCostY;
    5749 
    5750       Pel *pcResiCurrY = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getLumaAddr( absTUPartIdx );
    5751       UInt resiYStride = m_pcQTTempTComYuv[ uiQTTempAccessLayer ].getStride();
    5752 
    5753       TCoeff bestCoeffY[32*32];
    5754       memcpy( bestCoeffY, pcCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
    5755      
    5756 #if ADAPTIVE_QP_SELECTION
    5757       TCoeff bestArlCoeffY[32*32];
    5758       memcpy( bestArlCoeffY, pcArlCoeffCurrY, sizeof(TCoeff) * uiNumSamplesLuma );
    5759 #endif
    5760 
    5761       Pel bestResiY[32*32];
    5762       for ( Int i = 0; i < trHeight; ++i )
    5763       {
    5764         memcpy( &bestResiY[i*trWidth], pcResiCurrY+i*resiYStride, sizeof(Pel) * trWidth );
    5765       }
    5766 
    5767       if( m_bUseSBACRD )
    5768       {
    5769         m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    5770       }
    5771 
    5772       pcCU->setTransformSkipSubParts ( 1, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5773 
    5774       if (m_pcEncCfg->getUseRDOQ())
    5775       {
    5776         m_pcEntropyCoder->estimateBit( m_pcTrQuant->m_pcEstBitsSbac, trWidth, trHeight, TEXT_LUMA );       
    5777       }
    5778 
    5779       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
    5780 
    5781 #if RDOQ_CHROMA_LAMBDA
    5782       m_pcTrQuant->selectLambda(TEXT_LUMA);
    5783 #endif
    5784       m_pcTrQuant->transformNxN( pcCU, pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride (), pcCoeffCurrY,
    5785 #if ADAPTIVE_QP_SELECTION
    5786         pcArlCoeffCurrY,
    5787 #endif     
    5788         trWidth,   trHeight,    uiAbsSumTransformSkipY, TEXT_LUMA, uiAbsPartIdx, true );
    5789       pcCU->setCbfSubParts( uiAbsSumTransformSkipY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5790 
    5791       if( uiAbsSumTransformSkipY != 0 )
    5792       {
    5793         m_pcEntropyCoder->resetBits();
    5794         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA, uiTrMode );
    5795         m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_LUMA );
    5796         const UInt uiTsSingleBitsY = m_pcEntropyCoder->getNumberOfWrittenBits();
    5797 
    5798         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_LUMA, pcCU->getSlice()->getSPS()->getQpBDOffsetY(), 0 );
    5799 
    5800         Int scalingListType = 3 + g_eTTable[(Int)TEXT_LUMA];
    5801         assert(scalingListType < 6);     
    5802 
    5803 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    5804         if(pcCU->isIntraBL(uiAbsPartIdx) )
    5805         {
    5806           m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,DC_IDX, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
    5807         }
    5808         else
    5809         {
    5810           m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
    5811         }
    5812 #else
    5813         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_LUMA,REG_DCT, pcResiCurrY, m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),  pcCoeffCurrY, trWidth, trHeight, scalingListType, true );
    5814 #endif
    5815 
    5816         uiNonzeroDistY = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getLumaAddr( absTUPartIdx ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getStride(),
    5817           pcResi->getLumaAddr( absTUPartIdx ), pcResi->getStride(), trWidth, trHeight );
    5818 
    5819         dSingleCostY = m_pcRdCost->calcRdCost( uiTsSingleBitsY, uiNonzeroDistY );
    5820       }
    5821 
    5822       if( !uiAbsSumTransformSkipY || minCostY < dSingleCostY )
    5823       {
    5824         pcCU->setTransformSkipSubParts ( 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5825         memcpy( pcCoeffCurrY, bestCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
    5826 #if ADAPTIVE_QP_SELECTION
    5827         memcpy( pcArlCoeffCurrY, bestArlCoeffY, sizeof(TCoeff) * uiNumSamplesLuma );
    5828 #endif
    5829         for( Int i = 0; i < trHeight; ++i )
    5830         {
    5831           memcpy( pcResiCurrY+i*resiYStride, &bestResiY[i*trWidth], sizeof(Pel) * trWidth );
    5832         }
    5833       }
    5834       else
    5835       {
    5836         uiDistY = uiNonzeroDistY;
    5837         uiAbsSumY = uiAbsSumTransformSkipY;
    5838         uiBestTransformMode[0] = 1;
    5839       }
    5840 
    5841       pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    5842     }
    5843 
    5844     if( bCodeChroma && checkTransformSkipUV  )
    5845     {
    5846       UInt uiNonzeroDistU, uiNonzeroDistV, uiAbsSumTransformSkipU, uiAbsSumTransformSkipV;
    5847       Double dSingleCostU, dSingleCostV;
    5848 
    5849       Pel *pcResiCurrU = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC );
    5850       Pel *pcResiCurrV = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC );
    5851       UInt resiCStride = m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride();
    5852 
    5853       TCoeff bestCoeffU[32*32], bestCoeffV[32*32];
    5854       memcpy( bestCoeffU, pcCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
    5855       memcpy( bestCoeffV, pcCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
    5856 
    5857 #if ADAPTIVE_QP_SELECTION
    5858       TCoeff bestArlCoeffU[32*32], bestArlCoeffV[32*32];
    5859       memcpy( bestArlCoeffU, pcArlCoeffCurrU, sizeof(TCoeff) * uiNumSamplesChro );
    5860       memcpy( bestArlCoeffV, pcArlCoeffCurrV, sizeof(TCoeff) * uiNumSamplesChro );
    5861 #endif
    5862 
    5863       Pel bestResiU[32*32], bestResiV[32*32];
    5864       for (Int i = 0; i < trHeightC; ++i )
    5865       {
    5866         memcpy( &bestResiU[i*trWidthC], pcResiCurrU+i*resiCStride, sizeof(Pel) * trWidthC );
    5867         memcpy( &bestResiV[i*trWidthC], pcResiCurrV+i*resiCStride, sizeof(Pel) * trWidthC );
    5868       }
    5869 
    5870       if( m_bUseSBACRD )
    5871       {
    5872         m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    5873       }
    5874 
    5875       pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5876       pcCU->setTransformSkipSubParts ( 1, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5877 
    5878       if (m_pcEncCfg->getUseRDOQ())
    5879       {
    5880         m_pcEntropyCoder->estimateBit(m_pcTrQuant->m_pcEstBitsSbac, trWidthC, trHeightC, TEXT_CHROMA );         
    5881       }
    5882 
    5883 #if CHROMA_QP_EXTENSION
    5884       Int curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
    5885       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5886 #else
    5887       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCbQpOffset() );
    5888 #endif
    5889 
    5890 #if RDOQ_CHROMA_LAMBDA
    5891       m_pcTrQuant->selectLambda(TEXT_CHROMA);
    5892 #endif
    5893 
    5894       m_pcTrQuant->transformNxN( pcCU, pcResi->getCbAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrU,
    5895 #if ADAPTIVE_QP_SELECTION
    5896         pcArlCoeffCurrU,
    5897 #endif       
    5898         trWidthC, trHeightC, uiAbsSumTransformSkipU, TEXT_CHROMA_U, uiAbsPartIdx, true );
    5899 #if CHROMA_QP_EXTENSION
    5900       curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
    5901       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5902 #else
    5903       m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCrQpOffset() );
    5904 #endif
    5905       m_pcTrQuant->transformNxN( pcCU, pcResi->getCrAddr(absTUPartIdxC), pcResi->getCStride(), pcCoeffCurrV,
    5906 #if ADAPTIVE_QP_SELECTION
    5907         pcArlCoeffCurrV,
    5908 #endif       
    5909         trWidthC, trHeightC, uiAbsSumTransformSkipV, TEXT_CHROMA_V, uiAbsPartIdx, true );
    5910 
    5911       pcCU->setCbfSubParts( uiAbsSumTransformSkipU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5912       pcCU->setCbfSubParts( uiAbsSumTransformSkipV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5913 
    5914       m_pcEntropyCoder->resetBits();
    5915       uiSingleBitsU = 0;
    5916       uiSingleBitsV = 0;
    5917 
    5918       if( uiAbsSumTransformSkipU )
    5919       {
    5920         m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
    5921         m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
    5922         uiSingleBitsU = m_pcEntropyCoder->getNumberOfWrittenBits();   
    5923 
    5924 #if CHROMA_QP_EXTENSION
    5925         curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCbQpOffset() + pcCU->getSlice()->getSliceQpDeltaCb();
    5926         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5927 #else
    5928         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCbQpOffset() );
    5929 #endif
    5930 
    5931         Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_U];
    5932         assert(scalingListType < 6);
    5933 
    5934         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrU, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrU, trWidthC, trHeightC, scalingListType, true  );
    5935 
    5936         uiNonzeroDistU = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCbAddr( absTUPartIdxC), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5937           pcResi->getCbAddr( absTUPartIdxC), pcResi->getCStride(), trWidthC, trHeightC
    5938 #if WEIGHTED_CHROMA_DISTORTION
    5939           , true
    5940 #endif
    5941           );
    5942 
    5943         dSingleCostU = m_pcRdCost->calcRdCost( uiSingleBitsU, uiNonzeroDistU );
    5944       }
    5945 
    5946       if( !uiAbsSumTransformSkipU || minCostU < dSingleCostU )
    5947       {
    5948         pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5949 
    5950         memcpy( pcCoeffCurrU, bestCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
    5951 #if ADAPTIVE_QP_SELECTION
    5952         memcpy( pcArlCoeffCurrU, bestArlCoeffU, sizeof (TCoeff) * uiNumSamplesChro );
    5953 #endif
    5954         for( Int i = 0; i < trHeightC; ++i )
    5955         {
    5956           memcpy( pcResiCurrU+i*resiCStride, &bestResiU[i*trWidthC], sizeof(Pel) * trWidthC );
    5957         }
    5958       }
    5959       else
    5960       {
    5961         uiDistU = uiNonzeroDistU;
    5962         uiAbsSumU = uiAbsSumTransformSkipU;
    5963         uiBestTransformMode[1] = 1;
    5964       }
    5965 
    5966       if( uiAbsSumTransformSkipV )
    5967       {
    5968         m_pcEntropyCoder->encodeQtCbf   ( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
    5969         m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
    5970         uiSingleBitsV = m_pcEntropyCoder->getNumberOfWrittenBits() - uiSingleBitsU;
    5971 
    5972 #if CHROMA_QP_EXTENSION
    5973         curChromaQpOffset = pcCU->getSlice()->getPPS()->getChromaCrQpOffset() + pcCU->getSlice()->getSliceQpDeltaCr();
    5974         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), curChromaQpOffset );
    5975 #else
    5976         m_pcTrQuant->setQPforQuant( pcCU->getQP( 0 ), TEXT_CHROMA, pcCU->getSlice()->getSPS()->getQpBDOffsetC(), pcCU->getSlice()->getPPS()->getChromaCrQpOffset() );
    5977 #endif
    5978 
    5979         Int scalingListType = 3 + g_eTTable[(Int)TEXT_CHROMA_V];
    5980         assert(scalingListType < 6);
    5981 
    5982         m_pcTrQuant->invtransformNxN( pcCU->getCUTransquantBypass(uiAbsPartIdx), TEXT_CHROMA,REG_DCT, pcResiCurrV, m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(), pcCoeffCurrV, trWidthC, trHeightC, scalingListType, true );
    5983 
    5984         uiNonzeroDistV = m_pcRdCost->getDistPart( m_pcQTTempTComYuv[uiQTTempAccessLayer].getCrAddr( absTUPartIdxC ), m_pcQTTempTComYuv[uiQTTempAccessLayer].getCStride(),
    5985           pcResi->getCrAddr( absTUPartIdxC ), pcResi->getCStride(), trWidthC, trHeightC
    5986 #if WEIGHTED_CHROMA_DISTORTION
    5987           , true
    5988 #endif
    5989           );
    5990 
    5991         dSingleCostV = m_pcRdCost->calcRdCost( uiSingleBitsV, uiNonzeroDistV );
    5992       }
    5993 
    5994       if( !uiAbsSumTransformSkipV || minCostV < dSingleCostV )
    5995       {
    5996         pcCU->setTransformSkipSubParts ( 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    5997 
    5998         memcpy( pcCoeffCurrV, bestCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
    5999 #if ADAPTIVE_QP_SELECTION
    6000         memcpy( pcArlCoeffCurrV, bestArlCoeffV, sizeof(TCoeff) * uiNumSamplesChro );
    6001 #endif
    6002         for( Int i = 0; i < trHeightC; ++i )
    6003         {
    6004           memcpy( pcResiCurrV+i*resiCStride, &bestResiV[i*trWidthC], sizeof(Pel) * trWidthC );
    6005         }
    6006       }
    6007       else
    6008       {
    6009         uiDistV = uiNonzeroDistV;
    6010         uiAbsSumV = uiAbsSumTransformSkipV;
    6011         uiBestTransformMode[2] = 1;
    6012       }
    6013 
    6014       pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6015       pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6016     }
    6017 #endif
    6018 
    6019     if( m_bUseSBACRD )
    6020     {
    6021       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    6022     }
    6023 
    6024     m_pcEntropyCoder->resetBits();
    6025 
    6026     {
    6027       if( uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
    6028       {
    6029 #if TRANS_SPLIT_FLAG_CTX_REDUCTION
    6030         m_pcEntropyCoder->encodeTransformSubdivFlag( 0, 5 - uiLog2TrSize );
    6031 #else
    6032         m_pcEntropyCoder->encodeTransformSubdivFlag( 0, uiDepth );
    6033 #endif
    6034       }
    6035     }
    6036 
    6037     {
    6038       if( bCodeChroma )
    6039       {
    6040         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode );
    6041         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode );
    6042       }
    6043 
    6044       m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    6045     }
    6046 
    6047     m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
    6048 
    6049     if( bCodeChroma )
    6050     {
    6051       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_U );
    6052       m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidthC, trHeightC, uiDepth, TEXT_CHROMA_V );
    6053     }
    6054 
    6055     uiSingleBits = m_pcEntropyCoder->getNumberOfWrittenBits();
    6056 
    6057     uiSingleDist = uiDistY + uiDistU + uiDistV;
    6058     dSingleCost = m_pcRdCost->calcRdCost( uiSingleBits, uiSingleDist );
    6059   } 
    6060  
    6061   // code sub-blocks
    6062   if( bCheckSplit )
    6063   {
    6064     if( m_bUseSBACRD && bCheckFull )
    6065     {
    6066       m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
    6067       m_pcRDGoOnSbacCoder->load ( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    6068     }
    6069     UInt uiSubdivDist = 0;
    6070     UInt uiSubdivBits = 0;
    6071     Double dSubdivCost = 0.0;
    6072    
    6073     const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
    6074     for( UInt ui = 0; ui < 4; ++ui )
    6075     {
    6076 #if REMOVE_NSQT
    6077       UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
    6078 #else
    6079       UInt nsAddr = 0;
    6080       nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrSize - 1, uiAbsPartIdx + ui * uiQPartNumSubdiv, absTUPartIdx, ui, uiTrMode + 1 );
    6081 #endif
    6082 #if IBDI_DISTORTION
    6083       xEstimateResidualQT( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcOrg, pcPred, pcResi, uiDepth + 1, dSubdivCost, uiSubdivBits, uiSubdivDist, bCheckFull ? NULL : puiZeroDist );
    6084 #else
    6085       xEstimateResidualQT( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, dSubdivCost, uiSubdivBits, uiSubdivDist, bCheckFull ? NULL : puiZeroDist );
    6086 #endif
    6087     }
    6088    
    6089     UInt uiYCbf = 0;
    6090     UInt uiUCbf = 0;
    6091     UInt uiVCbf = 0;
    6092     for( UInt ui = 0; ui < 4; ++ui )
    6093     {
    6094       uiYCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_LUMA,     uiTrMode + 1 );
    6095       uiUCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_U, uiTrMode + 1 );
    6096       uiVCbf |= pcCU->getCbf( uiAbsPartIdx + ui * uiQPartNumSubdiv, TEXT_CHROMA_V, uiTrMode + 1 );
    6097     }
    6098     for( UInt ui = 0; ui < 4 * uiQPartNumSubdiv; ++ui )
    6099     {
    6100       pcCU->getCbf( TEXT_LUMA     )[uiAbsPartIdx + ui] |= uiYCbf << uiTrMode;
    6101       pcCU->getCbf( TEXT_CHROMA_U )[uiAbsPartIdx + ui] |= uiUCbf << uiTrMode;
    6102       pcCU->getCbf( TEXT_CHROMA_V )[uiAbsPartIdx + ui] |= uiVCbf << uiTrMode;
    6103     }
    6104    
    6105     if( m_bUseSBACRD )
    6106     {
    6107       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_ROOT ] );
    6108     }
    6109     m_pcEntropyCoder->resetBits();
    6110    
    6111     {
    6112       xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, true,  TEXT_LUMA );
    6113       xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_LUMA );
    6114       xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_U );
    6115       xEncodeResidualQT( pcCU, uiAbsPartIdx, uiDepth, false, TEXT_CHROMA_V );
    6116     }
    6117    
    6118     uiSubdivBits = m_pcEntropyCoder->getNumberOfWrittenBits();
    6119     dSubdivCost  = m_pcRdCost->calcRdCost( uiSubdivBits, uiSubdivDist );
    6120    
    6121     if( uiYCbf || uiUCbf || uiVCbf || !bCheckFull )
    6122     {
    6123       if( dSubdivCost < dSingleCost )
    6124       {
    6125         rdCost += dSubdivCost;
    6126         ruiBits += uiSubdivBits;
    6127         ruiDist += uiSubdivDist;
    6128         return;
    6129       }
    6130     }
    6131 #if INTER_TRANSFORMSKIP
    6132     pcCU->setTransformSkipSubParts ( uiBestTransformMode[0], TEXT_LUMA, uiAbsPartIdx, uiDepth );
    6133     if(bCodeChroma)
    6134     {
    6135       pcCU->setTransformSkipSubParts ( uiBestTransformMode[1], TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6136       pcCU->setTransformSkipSubParts ( uiBestTransformMode[2], TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6137     }
    6138 #endif
    6139     assert( bCheckFull );
    6140     if( m_bUseSBACRD )
    6141     {
    6142       m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[ uiDepth ][ CI_QT_TRAFO_TEST ] );
    6143     }
    6144   }
    6145   rdCost += dSingleCost;
    6146   ruiBits += uiSingleBits;
    6147   ruiDist += uiSingleDist;
    6148  
    6149   pcCU->setTrIdxSubParts( uiTrMode, uiAbsPartIdx, uiDepth );
    6150  
    6151   pcCU->setCbfSubParts( uiAbsSumY ? uiSetCbf : 0, TEXT_LUMA, uiAbsPartIdx, uiDepth );
    6152   if( bCodeChroma )
    6153   {
    6154     pcCU->setCbfSubParts( uiAbsSumU ? uiSetCbf : 0, TEXT_CHROMA_U, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6155     pcCU->setCbfSubParts( uiAbsSumV ? uiSetCbf : 0, TEXT_CHROMA_V, uiAbsPartIdx, pcCU->getDepth(0)+uiTrModeC );
    6156   }
     6354    m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
     6355  }
     6356 
     6357  // determine residual for partition
     6358  Double dPUCost   = 0.0;
     6359  xRecurIntraCodingQT( pcCU, 0, 0, false, pcOrgYuv, pcPredYuv, pcResiYuv, uiOverallDistY, uiOverallDistC, false, dPUCost );
     6360  xSetIntraResultQT( pcCU, 0, 0, false, pcRecoYuv );
     6361 
     6362  //=== update PU data ====
     6363  pcCU->copyToPic( uiDepth, 0, 0 );
     6364   
     6365  //===== reset context models =====
     6366  if(m_bUseSBACRD)
     6367  {
     6368    m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
     6369  }
     6370
     6371  //===== set distortion (rate and r-d costs are determined later) =====
     6372  pcCU->getTotalDistortion() = uiOverallDistY + uiOverallDistC;
    61576373}
    61586374
    6159 Void TEncSearch::xEncodeResidualQT( TComDataCU* pcCU, UInt uiAbsPartIdx, const UInt uiDepth, Bool bSubdivAndCbf, TextType eType )
    6160 {
    6161   assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
    6162   const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
    6163   const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
    6164  
    6165   const Bool bSubdiv = uiCurrTrMode != uiTrMode;
    6166  
    6167   const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
    6168 
    6169   {
    6170     if( bSubdivAndCbf && uiLog2TrSize <= pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() && uiLog2TrSize > pcCU->getQuadtreeTULog2MinSizeInCU(uiAbsPartIdx) )
    6171     {
    6172 #if TRANS_SPLIT_FLAG_CTX_REDUCTION
    6173       m_pcEntropyCoder->encodeTransformSubdivFlag( bSubdiv, 5 - uiLog2TrSize );
    6174 #else
    6175       m_pcEntropyCoder->encodeTransformSubdivFlag( bSubdiv, uiDepth );
    6176 #endif
    6177     }
    6178   }
    6179 
    6180   {
    6181 #if INTRA_BL
    6182 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    6183     assert( !pcCU->isIntra(uiAbsPartIdx) || pcCU->isIntraBL(uiAbsPartIdx));
    6184 #else
    6185     assert( !pcCU->isIntra(uiAbsPartIdx) );
    6186 #endif
    6187 #else
    6188     assert( pcCU->getPredictionMode(uiAbsPartIdx) != MODE_INTRA );
    6189 #endif
    6190     if( bSubdivAndCbf )
    6191     {
    6192       const Bool bFirstCbfOfCU = uiCurrTrMode == 0;
    6193       if( bFirstCbfOfCU || uiLog2TrSize > 2 )
    6194       {
    6195         if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) )
    6196         {
    6197           m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode );
    6198         }
    6199         if( bFirstCbfOfCU || pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) )
    6200         {
    6201           m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode );
    6202         }
    6203       }
    6204       else if( uiLog2TrSize == 2 )
    6205       {
    6206         assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiCurrTrMode - 1 ) );
    6207         assert( pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode ) == pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiCurrTrMode - 1 ) );
    6208       }
    6209     }
    6210   }
    6211  
    6212   if( !bSubdiv )
    6213   {
    6214     const UInt uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
    6215     //assert( 16 == uiNumCoeffPerAbsPartIdxIncrement ); // check
    6216     const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    6217     TCoeff *pcCoeffCurrY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    6218     TCoeff *pcCoeffCurrU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6219     TCoeff *pcCoeffCurrV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6220    
    6221     Bool  bCodeChroma   = true;
    6222     UInt  uiTrModeC     = uiTrMode;
    6223     UInt  uiLog2TrSizeC = uiLog2TrSize-1;
    6224     if( uiLog2TrSize == 2 )
    6225     {
    6226       uiLog2TrSizeC++;
    6227       uiTrModeC    --;
    6228       UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
    6229       bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
    6230     }
    6231    
    6232     if( bSubdivAndCbf )
    6233     {
    6234       {
    6235         m_pcEntropyCoder->encodeQtCbf( pcCU, uiAbsPartIdx, TEXT_LUMA,     uiTrMode );
    6236       }
    6237     }
    6238     else
    6239     {
    6240       if( eType == TEXT_LUMA     && pcCU->getCbf( uiAbsPartIdx, TEXT_LUMA,     uiTrMode ) )
    6241       {
    6242         Int trWidth  = 1 << uiLog2TrSize;
    6243         Int trHeight = 1 << uiLog2TrSize;
    6244 #if !REMOVE_NSQT
    6245         pcCU->getNSQTSize( uiTrMode, uiAbsPartIdx, trWidth, trHeight );
    6246 #endif
    6247         m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrY, uiAbsPartIdx, trWidth, trHeight,    uiDepth, TEXT_LUMA );
    6248       }
    6249       if( bCodeChroma )
    6250       {
    6251         Int trWidth  = 1 << uiLog2TrSizeC;
    6252         Int trHeight = 1 << uiLog2TrSizeC;
    6253 #if !REMOVE_NSQT
    6254         pcCU->getNSQTSize( uiTrMode, uiAbsPartIdx, trWidth, trHeight );
    6255 #endif
    6256         if( eType == TEXT_CHROMA_U && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_U, uiTrMode ) )
    6257         {
    6258           m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrU, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_U );
    6259         }
    6260         if( eType == TEXT_CHROMA_V && pcCU->getCbf( uiAbsPartIdx, TEXT_CHROMA_V, uiTrMode ) )
    6261         {
    6262           m_pcEntropyCoder->encodeCoeffNxN( pcCU, pcCoeffCurrV, uiAbsPartIdx, trWidth, trHeight, uiDepth, TEXT_CHROMA_V );
    6263         }
    6264       }
    6265     }
    6266   }
    6267   else
    6268   {
    6269     if( bSubdivAndCbf || pcCU->getCbf( uiAbsPartIdx, eType, uiCurrTrMode ) )
    6270     {
    6271       const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
    6272       for( UInt ui = 0; ui < 4; ++ui )
    6273       {
    6274         xEncodeResidualQT( pcCU, uiAbsPartIdx + ui * uiQPartNumSubdiv, uiDepth + 1, bSubdivAndCbf, eType );
    6275       }
    6276     }
    6277   }
    6278 }
    6279 
    6280 Void TEncSearch::xSetResidualQTData( TComDataCU* pcCU, UInt uiQuadrant, UInt uiAbsPartIdx, UInt absTUPartIdx, TComYuv* pcResi, UInt uiDepth, Bool bSpatial )
    6281 {
    6282   assert( pcCU->getDepth( 0 ) == pcCU->getDepth( uiAbsPartIdx ) );
    6283   const UInt uiCurrTrMode = uiDepth - pcCU->getDepth( 0 );
    6284   const UInt uiTrMode = pcCU->getTransformIdx( uiAbsPartIdx );
    6285 
    6286   if( uiCurrTrMode == uiTrMode )
    6287   {
    6288     const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth]+2;
    6289     const UInt uiQTTempAccessLayer = pcCU->getSlice()->getSPS()->getQuadtreeTULog2MaxSize() - uiLog2TrSize;
    6290 
    6291     Bool  bCodeChroma   = true;
    6292     UInt  uiTrModeC     = uiTrMode;
    6293     UInt  uiLog2TrSizeC = uiLog2TrSize-1;
    6294     if( uiLog2TrSize == 2 )
    6295     {
    6296       uiLog2TrSizeC++;
    6297       uiTrModeC    --;
    6298       UInt  uiQPDiv = pcCU->getPic()->getNumPartInCU() >> ( ( pcCU->getDepth( 0 ) + uiTrModeC ) << 1 );
    6299       bCodeChroma   = ( ( uiAbsPartIdx % uiQPDiv ) == 0 );
    6300     }
    6301 
    6302     if( bSpatial )
    6303     {     
    6304       Int trWidth  = 1 << uiLog2TrSize;
    6305       Int trHeight = 1 << uiLog2TrSize;
    6306 #if !REMOVE_NSQT
    6307       pcCU->getNSQTSize( uiTrMode, uiAbsPartIdx, trWidth, trHeight );
    6308 #endif
    6309       m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartLuma    ( pcResi, absTUPartIdx, trWidth , trHeight );
    6310 
    6311       if( bCodeChroma )
    6312       {
    6313 #if !REMOVE_NSQT
    6314         Int trWidthC  = 1 << uiLog2TrSizeC;
    6315         Int trHeightC = 1 << uiLog2TrSizeC;
    6316         UInt absTUPartIdxC = absTUPartIdx;
    6317         pcCU->getNSQTSize( uiTrModeC, uiAbsPartIdx, trWidthC, trHeightC );
    6318 
    6319         if( pcCU->useNonSquareTrans( uiTrModeC, uiAbsPartIdx ) && !( uiLog2TrSizeC  == pcCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() && uiTrModeC == 1 ) )
    6320         {         
    6321           absTUPartIdxC = pcCU->getNSAddrChroma( uiLog2TrSizeC, uiTrModeC, uiQuadrant, absTUPartIdx );
    6322           m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartChroma( pcResi, absTUPartIdxC, trWidthC, trHeightC );
    6323         }
    6324         else
    6325 #endif
    6326         {
    6327           m_pcQTTempTComYuv[uiQTTempAccessLayer].copyPartToPartChroma( pcResi, uiAbsPartIdx, 1 << uiLog2TrSizeC, 1 << uiLog2TrSizeC );
    6328         }
    6329       }
    6330     }
    6331     else
    6332     {
    6333       UInt    uiNumCoeffPerAbsPartIdxIncrement = pcCU->getSlice()->getSPS()->getMaxCUWidth() * pcCU->getSlice()->getSPS()->getMaxCUHeight() >> ( pcCU->getSlice()->getSPS()->getMaxCUDepth() << 1 );
    6334       UInt    uiNumCoeffY = ( 1 << ( uiLog2TrSize << 1 ) );
    6335       TCoeff* pcCoeffSrcY = m_ppcQTTempCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    6336       TCoeff* pcCoeffDstY = pcCU->getCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    6337       ::memcpy( pcCoeffDstY, pcCoeffSrcY, sizeof( TCoeff ) * uiNumCoeffY );
    6338 #if ADAPTIVE_QP_SELECTION
    6339       Int* pcArlCoeffSrcY = m_ppcQTTempArlCoeffY [uiQTTempAccessLayer] +  uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    6340       Int* pcArlCoeffDstY = pcCU->getArlCoeffY() + uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx;
    6341       ::memcpy( pcArlCoeffDstY, pcArlCoeffSrcY, sizeof( Int ) * uiNumCoeffY );
    6342 #endif
    6343       if( bCodeChroma )
    6344       {
    6345         UInt    uiNumCoeffC = ( 1 << ( uiLog2TrSizeC << 1 ) );
    6346         TCoeff* pcCoeffSrcU = m_ppcQTTempCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6347         TCoeff* pcCoeffSrcV = m_ppcQTTempCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6348         TCoeff* pcCoeffDstU = pcCU->getCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6349         TCoeff* pcCoeffDstV = pcCU->getCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6350         ::memcpy( pcCoeffDstU, pcCoeffSrcU, sizeof( TCoeff ) * uiNumCoeffC );
    6351         ::memcpy( pcCoeffDstV, pcCoeffSrcV, sizeof( TCoeff ) * uiNumCoeffC );
    6352 #if ADAPTIVE_QP_SELECTION
    6353         Int* pcArlCoeffSrcU = m_ppcQTTempArlCoeffCb[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6354         Int* pcArlCoeffSrcV = m_ppcQTTempArlCoeffCr[uiQTTempAccessLayer] + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6355         Int* pcArlCoeffDstU = pcCU->getArlCoeffCb() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6356         Int* pcArlCoeffDstV = pcCU->getArlCoeffCr() + (uiNumCoeffPerAbsPartIdxIncrement * uiAbsPartIdx>>2);
    6357         ::memcpy( pcArlCoeffDstU, pcArlCoeffSrcU, sizeof( Int ) * uiNumCoeffC );
    6358         ::memcpy( pcArlCoeffDstV, pcArlCoeffSrcV, sizeof( Int ) * uiNumCoeffC );
    6359 #endif
    6360       }
    6361     }
    6362   }
    6363   else
    6364   {
    6365     const UInt uiQPartNumSubdiv = pcCU->getPic()->getNumPartInCU() >> ((uiDepth + 1 ) << 1);
    6366 #if !REMOVE_NSQT
    6367     const UInt uiLog2TrSize = g_aucConvertToBit[pcCU->getSlice()->getSPS()->getMaxCUWidth() >> uiDepth] + 2;
    6368 #endif
    6369     for( UInt ui = 0; ui < 4; ++ui )
    6370     {
    6371 #if REMOVE_NSQT
    6372       UInt nsAddr = uiAbsPartIdx + ui * uiQPartNumSubdiv;
    6373 #else
    6374       UInt nsAddr = 0;
    6375       nsAddr = pcCU->getNSAbsPartIdx( uiLog2TrSize-1, uiAbsPartIdx + ui * uiQPartNumSubdiv, absTUPartIdx, ui, uiCurrTrMode + 1);
    6376 #endif
    6377       xSetResidualQTData( pcCU, ui, uiAbsPartIdx + ui * uiQPartNumSubdiv, nsAddr, pcResi, uiDepth + 1, bSpatial );
    6378     }
    6379   }
    6380 }
    6381 
    6382 UInt TEncSearch::xModeBitsIntra( TComDataCU* pcCU, UInt uiMode, UInt uiPU, UInt uiPartOffset, UInt uiDepth, UInt uiInitTrDepth )
    6383 {
    6384   if( m_bUseSBACRD )
    6385   {
    6386     // Reload only contexts required for coding intra mode information
    6387     m_pcRDGoOnSbacCoder->loadIntraDirModeLuma( m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST] );
    6388   }
    6389  
    6390   pcCU->setLumaIntraDirSubParts ( uiMode, uiPartOffset, uiDepth + uiInitTrDepth );
    6391  
    6392   m_pcEntropyCoder->resetBits();
    6393   m_pcEntropyCoder->encodeIntraDirModeLuma ( pcCU, uiPartOffset);
    6394  
    6395   return m_pcEntropyCoder->getNumberOfWrittenBits();
    6396 }
    6397 
    6398 UInt TEncSearch::xUpdateCandList( UInt uiMode, Double uiCost, UInt uiFastCandNum, UInt * CandModeList, Double * CandCostList )
    6399 {
    6400   UInt i;
    6401   UInt shift=0;
    6402  
    6403   while ( shift<uiFastCandNum && uiCost<CandCostList[ uiFastCandNum-1-shift ] ) shift++;
    6404  
    6405   if( shift!=0 )
    6406   {
    6407     for(i=1; i<shift; i++)
    6408     {
    6409       CandModeList[ uiFastCandNum-i ] = CandModeList[ uiFastCandNum-1-i ];
    6410       CandCostList[ uiFastCandNum-i ] = CandCostList[ uiFastCandNum-1-i ];
    6411     }
    6412     CandModeList[ uiFastCandNum-shift ] = uiMode;
    6413     CandCostList[ uiFastCandNum-shift ] = uiCost;
    6414     return 1;
    6415   }
    6416  
    6417   return 0;
    6418 }
    6419 
    6420 /** add inter-prediction syntax elements for a CU block
    6421  * \param pcCU
    6422  * \param uiQp
    6423  * \param uiTrMode
    6424  * \param ruiBits
    6425  * \param rpcYuvRec
    6426  * \param pcYuvPred
    6427  * \param rpcYuvResi
    6428  * \returns Void
    6429  */
    6430 Void  TEncSearch::xAddSymbolBitsInter( TComDataCU* pcCU, UInt uiQp, UInt uiTrMode, UInt& ruiBits, TComYuv*& rpcYuvRec, TComYuv*pcYuvPred, TComYuv*& rpcYuvResi )
    6431 {
    6432 #if SKIP_FLAG
    6433   if(pcCU->getMergeFlag( 0 ) && pcCU->getPartitionSize( 0 ) == SIZE_2Nx2N && !pcCU->getQtRootCbf( 0 ))
    6434 #else
    6435   if ( pcCU->isSkipped( 0 ) )
    6436 #endif
    6437   {
    6438 #if SKIP_FLAG
    6439     pcCU->setSkipFlagSubParts( true, 0, pcCU->getDepth(0) );
    6440 #endif
    6441 
    6442     m_pcEntropyCoder->resetBits();
    6443     if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
    6444     {
    6445       m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
    6446     }
    6447     m_pcEntropyCoder->encodeSkipFlag(pcCU, 0, true);
    6448     m_pcEntropyCoder->encodeMergeIndex(pcCU, 0, 0, true);
    6449     ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
    6450   }
    6451   else
    6452   {
    6453     m_pcEntropyCoder->resetBits();
    6454     if(pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag())
    6455     {
    6456       m_pcEntropyCoder->encodeCUTransquantBypassFlag(pcCU, 0, true);
    6457     }
    6458     m_pcEntropyCoder->encodeSkipFlag ( pcCU, 0, true );
    6459 #if INTRA_BL
    6460     if(m_pcEncCfg->getLayerId())
    6461     {
    6462       m_pcEntropyCoder->encodeIntraBLFlag(pcCU, 0, true);
    6463 #if !NO_RESIDUAL_FLAG_FOR_BLPRED
    6464       assert( pcCU->isIntraBL( 0 ) == false );
    6465 #endif
    6466     }
    6467 #endif
    6468 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    6469     if( !pcCU->isIntraBL(0))
    6470     {
    6471 #endif
    6472     m_pcEntropyCoder->encodePredMode( pcCU, 0, true );
    6473     m_pcEntropyCoder->encodePartSize( pcCU, 0, pcCU->getDepth(0), true );
    6474     m_pcEntropyCoder->encodePredInfo( pcCU, 0, true );
    6475 #if NO_RESIDUAL_FLAG_FOR_BLPRED
    6476     }
    6477 #endif
    6478     Bool bDummy = false;
    6479     m_pcEntropyCoder->encodeCoeff   ( pcCU, 0, pcCU->getDepth(0), pcCU->getWidth(0), pcCU->getHeight(0), bDummy );
    6480    
    6481     ruiBits += m_pcEntropyCoder->getNumberOfWrittenBits();
    6482   }
    6483 }
    6484 
    6485 /**
    6486  * \brief Generate half-sample interpolated block
    6487  *
    6488  * \param pattern Reference picture ROI
    6489  * \param biPred    Flag indicating whether block is for biprediction
    6490  */
    6491 Void TEncSearch::xExtDIFUpSamplingH( TComPattern* pattern, Bool biPred )
    6492 {
    6493   Int width      = pattern->getROIYWidth();
    6494   Int height     = pattern->getROIYHeight();
    6495   Int srcStride  = pattern->getPatternLStride();
    6496  
    6497   Int intStride = m_filteredBlockTmp[0].getStride();
    6498   Int dstStride = m_filteredBlock[0][0].getStride();
    6499   Short *intPtr;
    6500   Short *dstPtr;
    6501   Int filterSize = NTAPS_LUMA;
    6502   Int halfFilterSize = (filterSize>>1);
    6503   Pel *srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
    6504  
    6505   m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[0].getLumaAddr(), intStride, width+1, height+filterSize, 0, false);
    6506   m_if.filterHorLuma(srcPtr, srcStride, m_filteredBlockTmp[2].getLumaAddr(), intStride, width+1, height+filterSize, 2, false);
    6507  
    6508   intPtr = m_filteredBlockTmp[0].getLumaAddr() + halfFilterSize * intStride + 1; 
    6509   dstPtr = m_filteredBlock[0][0].getLumaAddr();
    6510   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+0, 0, false, true);
    6511  
    6512   intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1; 
    6513   dstPtr = m_filteredBlock[2][0].getLumaAddr();
    6514   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+0, height+1, 2, false, true);
    6515  
    6516   intPtr = m_filteredBlockTmp[2].getLumaAddr() + halfFilterSize * intStride;
    6517   dstPtr = m_filteredBlock[0][2].getLumaAddr();
    6518   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+0, 0, false, true);
    6519  
    6520   intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
    6521   dstPtr = m_filteredBlock[2][2].getLumaAddr();
    6522   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width+1, height+1, 2, false, true);
    6523 }
    6524 
    6525 /**
    6526  * \brief Generate quarter-sample interpolated blocks
    6527  *
    6528  * \param pattern    Reference picture ROI
    6529  * \param halfPelRef Half-pel mv
    6530  * \param biPred     Flag indicating whether block is for biprediction
    6531  */
    6532 Void TEncSearch::xExtDIFUpSamplingQ( TComPattern* pattern, TComMv halfPelRef, Bool biPred )
    6533 {
    6534   Int width      = pattern->getROIYWidth();
    6535   Int height     = pattern->getROIYHeight();
    6536   Int srcStride  = pattern->getPatternLStride();
    6537  
    6538   Pel *srcPtr;
    6539   Int intStride = m_filteredBlockTmp[0].getStride();
    6540   Int dstStride = m_filteredBlock[0][0].getStride();
    6541   Short *intPtr;
    6542   Short *dstPtr;
    6543   Int filterSize = NTAPS_LUMA;
    6544  
    6545   Int halfFilterSize = (filterSize>>1);
    6546 
    6547   Int extHeight = (halfPelRef.getVer() == 0) ? height + filterSize : height + filterSize-1;
    6548  
    6549   // Horizontal filter 1/4
    6550   srcPtr = pattern->getROIY() - halfFilterSize * srcStride - 1;
    6551   intPtr = m_filteredBlockTmp[1].getLumaAddr();
    6552   if (halfPelRef.getVer() > 0)
    6553   {
    6554     srcPtr += srcStride;
    6555   }
    6556   if (halfPelRef.getHor() >= 0)
    6557   {
    6558     srcPtr += 1;
    6559   }
    6560   m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 1, false);
    6561  
    6562   // Horizontal filter 3/4
    6563   srcPtr = pattern->getROIY() - halfFilterSize*srcStride - 1;
    6564   intPtr = m_filteredBlockTmp[3].getLumaAddr();
    6565   if (halfPelRef.getVer() > 0)
    6566   {
    6567     srcPtr += srcStride;
    6568   }
    6569   if (halfPelRef.getHor() > 0)
    6570   {
    6571     srcPtr += 1;
    6572   }
    6573   m_if.filterHorLuma(srcPtr, srcStride, intPtr, intStride, width, extHeight, 3, false);       
    6574  
    6575   // Generate @ 1,1
    6576   intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
    6577   dstPtr = m_filteredBlock[1][1].getLumaAddr();
    6578   if (halfPelRef.getVer() == 0)
    6579   {
    6580     intPtr += intStride;
    6581   }
    6582   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
    6583  
    6584   // Generate @ 3,1
    6585   intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
    6586   dstPtr = m_filteredBlock[3][1].getLumaAddr();
    6587   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
    6588  
    6589   if (halfPelRef.getVer() != 0)
    6590   {
    6591     // Generate @ 2,1
    6592     intPtr = m_filteredBlockTmp[1].getLumaAddr() + (halfFilterSize-1) * intStride;
    6593     dstPtr = m_filteredBlock[2][1].getLumaAddr();
    6594     if (halfPelRef.getVer() == 0)
    6595     {
    6596       intPtr += intStride;
    6597     }
    6598     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
    6599    
    6600     // Generate @ 2,3
    6601     intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
    6602     dstPtr = m_filteredBlock[2][3].getLumaAddr();
    6603     if (halfPelRef.getVer() == 0)
    6604     {
    6605       intPtr += intStride;
    6606     }
    6607     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 2, false, true);
    6608   }
    6609   else
    6610   {
    6611     // Generate @ 0,1
    6612     intPtr = m_filteredBlockTmp[1].getLumaAddr() + halfFilterSize * intStride;
    6613     dstPtr = m_filteredBlock[0][1].getLumaAddr();
    6614     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
    6615    
    6616     // Generate @ 0,3
    6617     intPtr = m_filteredBlockTmp[3].getLumaAddr() + halfFilterSize * intStride;
    6618     dstPtr = m_filteredBlock[0][3].getLumaAddr();
    6619     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 0, false, true);
    6620   }
    6621  
    6622   if (halfPelRef.getHor() != 0)
    6623   {
    6624     // Generate @ 1,2
    6625     intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
    6626     dstPtr = m_filteredBlock[1][2].getLumaAddr();
    6627     if (halfPelRef.getHor() > 0)
    6628     {
    6629       intPtr += 1;
    6630     }
    6631     if (halfPelRef.getVer() >= 0)
    6632     {
    6633       intPtr += intStride;
    6634     }
    6635     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
    6636    
    6637     // Generate @ 3,2
    6638     intPtr = m_filteredBlockTmp[2].getLumaAddr() + (halfFilterSize-1) * intStride;
    6639     dstPtr = m_filteredBlock[3][2].getLumaAddr();
    6640     if (halfPelRef.getHor() > 0)
    6641     {
    6642       intPtr += 1;
    6643     }
    6644     if (halfPelRef.getVer() > 0)
    6645     {
    6646       intPtr += intStride;
    6647     }
    6648     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true); 
    6649   }
    6650   else
    6651   {
    6652     // Generate @ 1,0
    6653     intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
    6654     dstPtr = m_filteredBlock[1][0].getLumaAddr();
    6655     if (halfPelRef.getVer() >= 0)
    6656     {
    6657       intPtr += intStride;
    6658     }
    6659     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
    6660    
    6661     // Generate @ 3,0
    6662     intPtr = m_filteredBlockTmp[0].getLumaAddr() + (halfFilterSize-1) * intStride + 1;
    6663     dstPtr = m_filteredBlock[3][0].getLumaAddr();
    6664     if (halfPelRef.getVer() > 0)
    6665     {
    6666       intPtr += intStride;
    6667     }
    6668     m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
    6669   }
    6670  
    6671   // Generate @ 1,3
    6672   intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
    6673   dstPtr = m_filteredBlock[1][3].getLumaAddr();
    6674   if (halfPelRef.getVer() == 0)
    6675   {
    6676     intPtr += intStride;
    6677   }
    6678   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 1, false, true);
    6679  
    6680   // Generate @ 3,3
    6681   intPtr = m_filteredBlockTmp[3].getLumaAddr() + (halfFilterSize-1) * intStride;
    6682   dstPtr = m_filteredBlock[3][3].getLumaAddr();
    6683   m_if.filterVerLuma(intPtr, intStride, dstPtr, dstStride, width, height, 3, false, true);
    6684 }
    6685 
    6686 /** set wp tables
    6687  * \param TComDataCU* pcCU
    6688  * \param iRefIdx
    6689  * \param eRefPicListCur
    6690  * \returns Void
    6691  */
    6692 Void  TEncSearch::setWpScalingDistParam( TComDataCU* pcCU, Int iRefIdx, RefPicList eRefPicListCur )
    6693 {
    6694   if ( iRefIdx<0 )
    6695   {
    6696     m_cDistParam.bApplyWeight = false;
    6697     return;
    6698   }
    6699 
    6700   TComSlice       *pcSlice  = pcCU->getSlice();
    6701   TComPPS         *pps      = pcCU->getSlice()->getPPS();
    6702   wpScalingParam  *wp0 , *wp1;
    6703   m_cDistParam.bApplyWeight = ( pcSlice->getSliceType()==P_SLICE && pps->getUseWP() ) || ( pcSlice->getSliceType()==B_SLICE && pps->getWPBiPred() ) ;
    6704   if ( !m_cDistParam.bApplyWeight ) return;
    6705 
    6706   Int iRefIdx0 = ( eRefPicListCur == REF_PIC_LIST_0 ) ? iRefIdx : (-1);
    6707   Int iRefIdx1 = ( eRefPicListCur == REF_PIC_LIST_1 ) ? iRefIdx : (-1);
    6708 
    6709   getWpScaling( pcCU, iRefIdx0, iRefIdx1, wp0 , wp1 );
    6710 
    6711   if ( iRefIdx0 < 0 ) wp0 = NULL;
    6712   if ( iRefIdx1 < 0 ) wp1 = NULL;
    6713 
    6714   m_cDistParam.wpCur  = NULL;
    6715 
    6716   if ( eRefPicListCur == REF_PIC_LIST_0 )
    6717   {
    6718     m_cDistParam.wpCur = wp0;
    6719   }
    6720   else
    6721   {
    6722     m_cDistParam.wpCur = wp1;
    6723   }
    6724 }
    6725 
     6375#endif
    67266376//! \}
Note: See TracChangeset for help on using the changeset viewer.