Index: trunk/source/Lib/TLibCommon/TComPattern.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComPattern.cpp	(revision 21)
+++ trunk/source/Lib/TLibCommon/TComPattern.cpp	(revision 56)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.
+ * granted under this license.  
  *
- * Copyright (c) 2010-2011, ISO/IEC
+ * Copyright (c) 2010-2012, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -15,5 +15,5 @@
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
- *  * Neither the name of the ISO/IEC nor the names of its contributors may
+ *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
  *    be used to endorse or promote products derived from this software without
  *    specific prior written permission.
@@ -32,6 +32,4 @@
  */
 
-
-
 /** \file     TComPattern.cpp
     \brief    neighbouring pixel access classes
@@ -42,31 +40,35 @@
 #include "TComDataCU.h"
 
+//! \ingroup TLibCommon
+//! \{
+
 // ====================================================================================================================
 // Tables
 // ====================================================================================================================
 
-const UChar g_aaucAvailableBlkMask[16][8] =
-{
-  // 4x4 block neighbor availability      // MB neighbor availability
-  {0x0,0x0,0x0,0x0,  0x0,0x8,0x0,0x08  }, // L, A, AL, AR   <== WTF (blkIdx < 8)
-  {0x1,0x0,0x0,0x0,  0x1,0x8,0x0,0x08  }, //    A, AL, AR
-  {0xA,0xE,0xE,0x6,  0x0,0x8,0x0,0x08  }, // L,    AL, AR
-  {0xB,0xE,0xE,0x6,  0x1,0x8,0x0,0x08  }, //       AL, AR
-  
-  {0x4,0x0,0x0,0x0,  0x0,0x8,0x0,0x08  }, // L, A,     AR
-  {0x5,0x0,0x0,0x0,  0x1,0x8,0x0,0x08  }, //    A,     AR
-  {0xE,0xE,0xE,0x6,  0x0,0x8,0x0,0x08  }, // L,        AR
-  {0xF,0xE,0xE,0x6,  0x1,0x8,0x0,0x08  }, //           AR
-  
-  {0x0,0x0,0x0,0x8,  0x0,0x8,0x0,0x08  }, // L, A, AL       <== WTF (blkIdx < 8 || blkIdx >= 8)
-  {0x1,0x0,0x0,0x8,  0x1,0x8,0x0,0x08  }, //    A, AL
-  {0xA,0xE,0xE,0xE,  0x0,0x8,0x0,0x08  }, // L,    AL
-  {0xB,0xE,0xE,0xE,  0x1,0x8,0x0,0x08  }, //       AL
-  
-  {0x4,0x0,0x0,0x8,  0x0,0x8,0x0,0x08  }, // L, A,
-  {0x5,0x0,0x0,0x8,  0x1,0x8,0x0,0x08  }, //    A,
-  {0xE,0xE,0xE,0xE,  0x0,0x8,0x0,0x08  }, // L,
-  {0xF,0xE,0xE,0xE,  0x1,0x8,0x0,0x08  }  //
+#if LOGI_INTRA_NAME_3MPM
+const UChar TComPattern::m_aucIntraFilter[5] =
+{
+  10, //4x4
+  7, //8x8
+  1, //16x16
+  0, //32x32
+  10, //64x64
 };
+#else
+const UChar TComPattern::m_aucIntraFilter[5][NUM_INTRA_MODE] =
+{
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+  }, //4x4
+  {1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+  }, //8x8
+  {1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0
+  }, //16x16
+  {1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
+  }, //32x32
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+  }, //64x64
+};
+#endif
 
 // ====================================================================================================================
@@ -124,7 +126,7 @@
                                          Int         iOffsetBottom,
                                          UInt        uiPartDepth,
-                                         UInt        uiAbsPartIdx
-#if DEPTH_MAP_GENERATION
-                                        ,Bool        bPrdDepthMap
+                                         UInt        uiAbsPartIdx 
+#if DEPTH_MAP_GENERATION
+                                         ,Bool        bPrdDepthMap
 #endif
                                          )
@@ -145,4 +147,5 @@
   TComPicYuv* pcPic = pcCU->getPic()->getPicYuvRec();
 #endif
+
   if ( iComp == 0 )
   {
@@ -162,7 +165,11 @@
 #endif
     if ( iComp == 1 )
+    {
       m_piPatternOrigin = pcPic->getCbAddr(pcCU->getAddr(), uiAbsZorderIdx) - m_iOffsetAbove * m_iPatternStride - m_iOffsetLeft;
+    }
     else
+    {
       m_piPatternOrigin = pcPic->getCrAddr(pcCU->getAddr(), uiAbsZorderIdx) - m_iOffsetAbove * m_iPatternStride - m_iOffsetLeft;
+    }
   }
 }
@@ -190,9 +197,9 @@
 }
 
-Void TComPattern::initPattern( TComDataCU* pcCU, UInt uiPartDepth, UInt uiAbsPartIdx
-#if DEPTH_MAP_GENERATION
-                             , Bool bPrdDepthMap 
-#endif
-                             )
+Void TComPattern::initPattern( TComDataCU* pcCU, UInt uiPartDepth, UInt uiAbsPartIdx 
+#if DEPTH_MAP_GENERATION
+                              , Bool bPrdDepthMap 
+#endif
+                              )
 {
   Int   uiOffsetLeft  = 0;
@@ -209,5 +216,8 @@
   
   if( uiCurrPicPelX != 0 )
+  {
     uiOffsetLeft = 1;
+  }
+  
   if( uiCurrPicPelY != 0 )
   {
@@ -215,10 +225,12 @@
     uiOffsetAbove = 1;
     
-    if( uiCurrPicPelX + uiWidth < pcCU->getSlice()->getSPS()->getWidth() )
+    if( uiCurrPicPelX + uiWidth < pcCU->getSlice()->getSPS()->getPicWidthInLumaSamples() )
     {
       if( ( g_auiZscanToRaster[uiAbsZorderIdx] + uiNumPartInWidth ) % pcPic->getNumPartInWidth() ) // Not CU boundary
       {
         if( g_auiRasterToZscan[ (Int)g_auiZscanToRaster[uiAbsZorderIdx] - (Int)pcPic->getNumPartInWidth() + (Int)uiNumPartInWidth ] < uiAbsZorderIdx )
+        {
           uiOffsetRight = 1;
+        }
       }
       else // if it is CU boundary
@@ -232,9 +244,4 @@
   }
   
-#if LM_CHROMA 
-  m_bLeftAvailable = uiCurrPicPelX > 0 ? true : false;
-  m_bAboveAvailable = uiCurrPicPelY > 0 ? true : false;
-#endif
-
 #if DEPTH_MAP_GENERATION
   m_cPatternY .setPatternParamCU( pcCU, 0, uiWidth,      uiHeight,      uiOffsetLeft, uiOffsetRight, uiOffsetAbove, 0, uiPartDepth, uiAbsPartIdx, bPrdDepthMap );
@@ -248,9 +255,9 @@
 }
 
-Void TComPattern::initAdiPattern( TComDataCU* pcCU, UInt uiZorderIdxInPart, UInt uiPartDepth, Int* piAdiBuf, Int iOrgBufStride, Int iOrgBufHeight, Bool& bAbove, Bool& bLeft
-#if DEPTH_MAP_GENERATION
-                                , Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY
-#endif
-                                )
+  Void TComPattern::initAdiPattern( TComDataCU* pcCU, UInt uiZorderIdxInPart, UInt uiPartDepth, Int* piAdiBuf, Int iOrgBufStride, Int iOrgBufHeight, Bool& bAbove, Bool& bLeft, Bool bLMmode 
+#if DEPTH_MAP_GENERATION
+                                  , Bool bPrdDepthMap, UInt uiSubSampExpX, UInt uiSubSampExpY
+#endif
+    )
 {
   Pel*  piRoiOrigin;
@@ -262,5 +269,4 @@
   UInt  uiWidth;
   UInt  uiHeight;
-
 #if DEPTH_MAP_GENERATION
   Int   iPicStride = ( bPrdDepthMap ? pcCU->getPic()->getPredDepthMap()->getStride() : pcCU->getPic()->getStride() );
@@ -268,125 +274,30 @@
   Int   iPicStride = pcCU->getPic()->getStride();
 #endif
-
-  Int   iCuAddr;
-#if REFERENCE_SAMPLE_PADDING
   Int   iUnitSize = 0;
   Int   iNumUnitsInCu = 0;
   Int   iTotalUnits = 0;
-  Bool* bNeighborFlags;
+  Bool  bNeighborFlags[4 * MAX_NUM_SPU_W + 1];
   Int   iNumIntraNeighbor = 0;
-#else  // REFERENCE_SAMPLE_PADDING
-  Pel*  piRoiTemp;
-  Bool  bAboveFlag      = false;
-  Bool  bAboveRightFlag = false;
-  Bool  bLeftFlag       = false;
-  Bool  bBelowLeftFlag  = false;
-  Bool  bAboveLeftFlag  = false;
-#endif // REFERENCE_SAMPLE_PADDING
-  
-  iCuAddr = pcCU->getAddr();
-  
-  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB, uiPartDum;
+  
+  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
+
   
   pcCU->deriveLeftRightTopIdxAdi( uiPartIdxLT, uiPartIdxRT, uiZorderIdxInPart, uiPartDepth );
   pcCU->deriveLeftBottomIdxAdi  ( uiPartIdxLB,              uiZorderIdxInPart, uiPartDepth );
   
-#if CONSTRAINED_INTRA_PRED
-  if ( pcCU->getSlice()->getPPS()->getConstrainedIntraPred() )
-  {
-#if REFERENCE_SAMPLE_PADDING
-    iUnitSize      = g_uiMaxCUWidth >> g_uiMaxCUDepth;
-    iNumUnitsInCu  = uiCuWidth / iUnitSize;
-    iTotalUnits    = (iNumUnitsInCu << 2) + 1;
-    bNeighborFlags = new Bool[iTotalUnits];
-
-    bNeighborFlags[iNumUnitsInCu*2] = isAboveLeftAvailableForCIP( pcCU, uiPartIdxLT );
-    iNumIntraNeighbor  += (Int)(bNeighborFlags[iNumUnitsInCu*2]);
-    iNumIntraNeighbor  += isAboveAvailableForCIP     ( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*2)+1 );
-    iNumIntraNeighbor  += isAboveRightAvailableForCIP( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*3)+1 );
-    iNumIntraNeighbor  += isLeftAvailableForCIP      ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+(iNumUnitsInCu*2)-1 );
-    iNumIntraNeighbor  += isBelowLeftAvailableForCIP ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+ iNumUnitsInCu   -1 );
-#else // REFERENCE_SAMPLE_PADDING
-    bAboveFlag      = isAboveAvailableForCIP     ( pcCU, uiPartIdxLT, uiPartIdxRT );
-    bAboveRightFlag = isAboveRightAvailableForCIP( pcCU, uiPartIdxLT, uiPartIdxRT );
-    bLeftFlag       = isLeftAvailableForCIP      ( pcCU, uiPartIdxLT, uiPartIdxLB );
-    bBelowLeftFlag  = isBelowLeftAvailableForCIP ( pcCU, uiPartIdxLT, uiPartIdxLB );
-    bAboveLeftFlag  = isAboveLeftAvailableForCIP ( pcCU, uiPartIdxLT );
-#endif // REFERENCE_SAMPLE_PADDING
-  }
-  else
-  {
-#if REFERENCE_SAMPLE_PADDING
-    iUnitSize     = uiCuWidth;
-    iNumUnitsInCu = 1;
-    iTotalUnits   = 5;
-    bNeighborFlags = new Bool[iTotalUnits];
-
-    ::memset(bNeighborFlags, false, sizeof(Bool)*iTotalUnits);
-    if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[3] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, 1, true, false ) ) { bNeighborFlags[4] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[1] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, 1, true, false ) ) { bNeighborFlags[0] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[2] = true; iNumIntraNeighbor++; }
-#if MN_DC_PRED_FILTER
-    m_bAboveFlagForDCFilt = bNeighborFlags[3];
-    m_bLeftFlagForDCFilt  = bNeighborFlags[1];
-#endif
-#else // REFERENCE_SAMPLE_PADDING
-    if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT,    true, false ) ) bAboveFlag      = true;
-    if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, 1, true, false ) ) bAboveRightFlag = true;
-    if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT,    true, false ) ) bLeftFlag       = true;
-    if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, 1, true, false ) ) bBelowLeftFlag  = true;
-    if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT,    true, false ) ) bAboveLeftFlag  = true;
-#if MN_DC_PRED_FILTER
-    m_bAboveFlagForDCFilt = bAboveFlag;
-    m_bLeftFlagForDCFilt  = bLeftFlag;
-#endif
-#endif // REFERENCE_SAMPLE_PADDING
-  }
-#else //CONSTRAINED_INTRA_PRED
-#if REFERENCE_SAMPLE_PADDING
-  iUnitSize     = uiCuWidth;
-  iNumUnitsInCu = 1;
-  iTotalUnits   = 5;
-  bNeighborFlags = new Bool[iTotalUnits];
-
-  ::memset(bNeighborFlags, false, sizeof(Bool)*iTotalUnits);
-  if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[3] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, true, false ) ) { bNeighborFlags[4] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[1] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, true, false ) ) { bNeighborFlags[0] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[2] = true; iNumIntraNeighbor++; }
-#if MN_DC_PRED_FILTER
-  m_bAboveFlagForDCFilt = bNeighborFlags[3];
-  m_bLeftFlagForDCFilt  = bNeighborFlags[1];
-#endif
-#else // REFERENCE_SAMPLE_PADDING
-  if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT, true, false ) ) bAboveFlag      = true;
-  if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, true, false ) ) bAboveRightFlag = true;
-  if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT, true, false ) ) bLeftFlag       = true;
-  if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, true, false ) ) bBelowLeftFlag  = true;
-  if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT, true, false ) ) bAboveLeftFlag  = true;
-#if MN_DC_PRED_FILTER
-  m_bAboveFlagForDCFilt = bAboveFlag;
-  m_bLeftFlagForDCFilt  = bLeftFlag;
-#endif
-#endif // REFERENCE_SAMPLE_PADDING
-#endif //CONSTRAINED_INTRA_PRED
-
-#if MN_DC_PRED_FILTER
-  m_bDCPredFilterFlag = (m_bAboveFlagForDCFilt && m_bLeftFlagForDCFilt) ? true : false;
-#endif
-  
-#if REFERENCE_SAMPLE_PADDING
+  iUnitSize      = g_uiMaxCUWidth >> g_uiMaxCUDepth;
+  iNumUnitsInCu  = uiCuWidth / iUnitSize;
+  iTotalUnits    = (iNumUnitsInCu << 2) + 1;
+
+  bNeighborFlags[iNumUnitsInCu*2] = isAboveLeftAvailable( pcCU, uiPartIdxLT );
+  iNumIntraNeighbor  += (Int)(bNeighborFlags[iNumUnitsInCu*2]);
+  iNumIntraNeighbor  += isAboveAvailable     ( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*2)+1 );
+  iNumIntraNeighbor  += isAboveRightAvailable( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*3)+1 );
+  iNumIntraNeighbor  += isLeftAvailable      ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+(iNumUnitsInCu*2)-1 );
+  iNumIntraNeighbor  += isBelowLeftAvailable ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+ iNumUnitsInCu   -1 );
+  
   bAbove = true;
   bLeft  = true;
-#else // REFERENCE_SAMPLE_PADDING
-  bAbove = bAboveFlag;
-  bLeft  = bLeftFlag;
-
-  Int iDCValue = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
-#endif // REFERENCE_SAMPLE_PADDING
-  
+
 #if DEPTH_MAP_GENERATION
   if ( bPrdDepthMap )
@@ -397,14 +308,16 @@
   else
   {
+    uiWidth=uiCuWidth2+1;
+    uiHeight=uiCuHeight2+1;
+  }
+#else
   uiWidth=uiCuWidth2+1;
   uiHeight=uiCuHeight2+1;
-  }
-#else
-  uiWidth=uiCuWidth2+1;
-  uiHeight=uiCuHeight2+1;
 #endif
   
   if (((uiWidth<<2)>iOrgBufStride)||((uiHeight<<2)>iOrgBufHeight))
+  {
     return;
+  }
   
   piRoiOrigin = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), pcCU->getZorderIdxInCU()+uiZorderIdxInPart);
@@ -415,90 +328,20 @@
   {
     piRoiOrigin = pcCU->getPic()->getPredDepthMap()->getLumaAddr( pcCU->getAddr(), pcCU->getZorderIdxInCU() + uiZorderIdxInPart );
-#if REFERENCE_SAMPLE_PADDING
-#else
-    iDCValue    = PDM_UNDEFINED_DEPTH;
-#endif
-  }
-#endif
-
-
-#if REFERENCE_SAMPLE_PADDING
+  }
+#endif
+
 #if DEPTH_MAP_GENERATION
   if ( bPrdDepthMap )
-    fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize >> uiSubSampExpX, iNumUnitsInCu, iTotalUnits, uiCuWidth >> uiSubSampExpX, uiCuHeight >> uiSubSampExpY, uiWidth, uiHeight, iPicStride, bPrdDepthMap );
+    fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize >> uiSubSampExpX, iNumUnitsInCu, iTotalUnits, uiCuWidth >> uiSubSampExpX, uiCuHeight >> uiSubSampExpY, uiWidth, uiHeight, iPicStride, bLMmode, bPrdDepthMap );
   else
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, bPrdDepthMap );
-#else
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride );
-#endif
-  delete [] bNeighborFlags;
-  bNeighborFlags = NULL;
-#else // REFERENCE_SAMPLE_PADDING
-  //BB: fill border with DC value - needed if( bAboveFlag=false || bLeftFlag=false )
-  for (i=0;i<uiWidth;i++)
-    piAdiTemp[i]=iDCValue;
-  for (i=0;i<uiHeight;i++) //BB: can start from 1?
-    piAdiTemp[i*uiWidth]=iDCValue;
-  
-  piRoiTemp=piRoiOrigin; //BB: not needed?
-  
-  if (bAboveFlag)
-  {
-    // BB: fill top border with rec. samples
-    piRoiTemp=piRoiOrigin-iPicStride;
-    for (i=0;i<uiCuWidth;i++)
-      piAdiTemp[1+i]=piRoiTemp[i];
-    // BB: fill top right border with rec. samples
-    if (bAboveRightFlag)
-    {
-      piRoiTemp=piRoiOrigin-iPicStride+uiCuWidth;
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piRoiTemp[i];
-    }
-    // BB: fill top right border with last sample from top
-    else
-    {
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piAdiTemp[uiCuWidth];
-    }
-  }
-  
-  if (bAboveLeftFlag)
-  {
-    piRoiTemp=piRoiOrigin-iPicStride-1;
-    piAdiTemp[0]=piRoiTemp[0];
-  }
-
-  if (bLeftFlag)
-  {
-    // BB: fill left border with rec. samples
-    piRoiTemp=piRoiOrigin-1;
-    for (i=0;i<uiCuHeight;i++)
-    {
-      piAdiTemp[(1+i)*uiWidth]=piRoiTemp[0];
-      piRoiTemp+=iPicStride;
-    }
-    // BB: fill below left border with rec. samples
-    if (bBelowLeftFlag)
-    {
-      for (i=0;i<uiCuHeight;i++)
-      {
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piRoiTemp[0];
-        piRoiTemp+=iPicStride;
-      }
-    }
-    // BB: fill below left border with last sample from left
-    else
-    {
-      for (i=0;i<uiCuHeight;i++)
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piAdiTemp[(uiCuHeight)*uiWidth];
-    }
-  }
-#endif // REFERENCE_SAMPLE_PADDING
-  
-#if QC_MDIS
+    fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, bLMmode, bPrdDepthMap );
+#else
+  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, bLMmode);
+#endif
+
+  
+  Int   i;
   // generate filtered intra prediction samples
   Int iBufSize = uiCuHeight2 + uiCuWidth2 + 1;  // left and left above border + above and above right border + top left corner = length of 3. filter buffer
-  Int   i;
 
   UInt uiWH = uiWidth * uiHeight;               // number of elements in one buffer
@@ -512,10 +355,14 @@
   // left border from bottom to top
   for (i = 0; i < uiCuHeight2; i++)
+  {
     piFilterBuf[l++] = piAdiTemp[uiWidth * (uiCuHeight2 - i)];
+  }
   // top left corner
   piFilterBuf[l++] = piAdiTemp[0];
   // above border from left to right
   for (i=0; i < uiCuWidth2; i++)
+  {
     piFilterBuf[l++] = piAdiTemp[1 + i];
+  }
 
   // 1. filtering with [1 2 1]
@@ -523,31 +370,19 @@
   piFilterBufN[iBufSize - 1] = piFilterBuf[iBufSize - 1];
   for (i = 1; i < iBufSize - 1; i++)
+  {
     piFilterBufN[i] = (piFilterBuf[i - 1] + 2 * piFilterBuf[i]+piFilterBuf[i + 1] + 2) >> 2;
+  }
 
   // fill 1. filter buffer with filtered values
   l=0;
   for (i = 0; i < uiCuHeight2; i++)
+  {
     piFilteredBuf1[uiWidth * (uiCuHeight2 - i)] = piFilterBufN[l++];
+  }
   piFilteredBuf1[0] = piFilterBufN[l++];
   for (i = 0; i < uiCuWidth2; i++)
+  {
     piFilteredBuf1[1 + i] = piFilterBufN[l++];
-
-#if !MN_MDIS_SIMPLIFICATION
-  // 2. filtering with [1 2 1]
-  piFilterBuf[0] = piFilterBufN[0];                   
-  piFilterBuf[iBufSize - 1] = piFilterBufN[iBufSize - 1];
-  for (i = 1; i < iBufSize - 1; i++)
-    piFilterBuf[i] = (piFilterBufN[i - 1] + 2 * piFilterBufN[i] + piFilterBufN[i + 1] + 2) >> 2;
-
-  // fill 2. filter buffer with filtered values
-  l=0;
-  for (i = 0; i < uiCuHeight2; i++)
-    piFilteredBuf2[uiWidth * (uiCuHeight2 - i)] = piFilterBuf[l++];
-  piFilteredBuf2[0] = piFilterBuf[l++];
-  for (i = 0; i < uiCuWidth2; i++)
-    piFilteredBuf2[1 + i] = piFilterBuf[l++];
-#endif
-#endif //QC_MDIS
-  
+  }
 }
 
@@ -561,104 +396,29 @@
   UInt  uiHeight;
   Int   iPicStride = pcCU->getPic()->getCStride();
-  Int   iCuAddr;
-#if REFERENCE_SAMPLE_PADDING
+
   Int   iUnitSize = 0;
   Int   iNumUnitsInCu = 0;
   Int   iTotalUnits = 0;
-  Bool* bNeighborFlags;
+  Bool  bNeighborFlags[4 * MAX_NUM_SPU_W + 1];
   Int   iNumIntraNeighbor = 0;
-#else // REFERENCE_SAMPLE_PADDING
-  Pel*  piRoiTemp;
-  Int   i;
-  Bool  bAboveFlag=false;
-  Bool  bAboveRightFlag=false;
-  Bool  bLeftFlag=false;
-  Bool  bBelowLeftFlag=false;
-  Bool  bAboveLeftFlag    = false;
-#endif // REFERENCE_SAMPLE_PADDING
-  
-  iCuAddr = pcCU->getAddr();
-  
-  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB,uiPartDum;
+  
+  UInt uiPartIdxLT, uiPartIdxRT, uiPartIdxLB;
   
   pcCU->deriveLeftRightTopIdxAdi( uiPartIdxLT, uiPartIdxRT, uiZorderIdxInPart, uiPartDepth );
   pcCU->deriveLeftBottomIdxAdi  ( uiPartIdxLB,              uiZorderIdxInPart, uiPartDepth );
   
-#if CONSTRAINED_INTRA_PRED
-  if ( pcCU->getSlice()->getPPS()->getConstrainedIntraPred() )
-  {
-#if REFERENCE_SAMPLE_PADDING
-    iUnitSize      = (g_uiMaxCUWidth >> g_uiMaxCUDepth) >> 1; // for chroma
-    iNumUnitsInCu  = (uiCuWidth / iUnitSize) >> 1;            // for chroma
-    iTotalUnits    = (iNumUnitsInCu << 2) + 1;
-    bNeighborFlags = new Bool[iTotalUnits];
-
-    bNeighborFlags[iNumUnitsInCu*2] = isAboveLeftAvailableForCIP( pcCU, uiPartIdxLT );
-    iNumIntraNeighbor  += (Int)(bNeighborFlags[iNumUnitsInCu*2]);
-    iNumIntraNeighbor  += isAboveAvailableForCIP     ( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*2)+1 );
-    iNumIntraNeighbor  += isAboveRightAvailableForCIP( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*3)+1 );
-    iNumIntraNeighbor  += isLeftAvailableForCIP      ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+(iNumUnitsInCu*2)-1 );
-    iNumIntraNeighbor  += isBelowLeftAvailableForCIP ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+ iNumUnitsInCu   -1 );
-#else // REFERENCE_SAMPLE_PADDING
-    bAboveFlag      = isAboveAvailableForCIP     ( pcCU, uiPartIdxLT, uiPartIdxRT );
-    bAboveRightFlag = isAboveRightAvailableForCIP( pcCU, uiPartIdxLT, uiPartIdxRT );
-    bLeftFlag       = isLeftAvailableForCIP      ( pcCU, uiPartIdxLT, uiPartIdxLB );
-    bBelowLeftFlag  = isBelowLeftAvailableForCIP ( pcCU, uiPartIdxLT, uiPartIdxLB );
-    bAboveLeftFlag  = isAboveLeftAvailableForCIP ( pcCU, uiPartIdxLT );
-#endif // REFERENCE_SAMPLE_PADDING
-  }
-  else
-  {
-#if REFERENCE_SAMPLE_PADDING
-    iUnitSize     = uiCuWidth >> 1;
-    iNumUnitsInCu = 1;
-    iTotalUnits   = 5;
-    bNeighborFlags = new Bool[iTotalUnits];
-
-    ::memset(bNeighborFlags, false, sizeof(Bool)*iTotalUnits);
-    if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[3] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, 1, true, false ) ) { bNeighborFlags[4] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[1] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, 1, true, false ) ) { bNeighborFlags[0] = true; iNumIntraNeighbor++; }
-    if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT,    true, false ) ) { bNeighborFlags[2] = true; iNumIntraNeighbor++; }
-#else // REFERENCE_SAMPLE_PADDING
-    if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT,    true, false ) ) bAboveFlag      = true;
-    if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, 1, true, false ) ) bAboveRightFlag = true;
-    if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT,    true, false ) ) bLeftFlag       = true;
-    if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, 1, true, false ) ) bBelowLeftFlag  = true;
-    if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT,    true, false ) ) bAboveLeftFlag  = true;
-#endif // REFERENCE_SAMPLE_PADDING
-  }
-#else //CONSTRAINED_INTRA_PRED
-#if REFERENCE_SAMPLE_PADDING
-  iUnitSize     = uiCuWidth >> 1;
-  iNumUnitsInCu = 1;
-  iTotalUnits   = 5;
-  bNeighborFlags = new Bool[iTotalUnits];
-
-  ::memset(bNeighborFlags, false, sizeof(Bool)*iTotalUnits);
-  if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[3] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, true, false ) ) { bNeighborFlags[4] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[1] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, true, false ) ) { bNeighborFlags[0] = true; iNumIntraNeighbor++; }
-  if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT, true, false ) ) { bNeighborFlags[2] = true; iNumIntraNeighbor++; }
-#else // REFERENCE_SAMPLE_PADDING
-  if( pcCU->getPUAbove        ( uiPartDum,             uiPartIdxLT, true, false ) ) bAboveFlag      = true;
-  if( pcCU->getPUAboveRightAdi( uiPartDum, uiCuWidth,  uiPartIdxRT, true, false ) ) bAboveRightFlag = true;
-  if( pcCU->getPULeft         ( uiPartDum,             uiPartIdxLT, true, false ) ) bLeftFlag       = true;
-  if( pcCU->getPUBelowLeftAdi ( uiPartDum, uiCuHeight, uiPartIdxLB, true, false ) ) bBelowLeftFlag  = true;
-  if( pcCU->getPUAboveLeft    ( uiPartDum,             uiPartIdxLT, true, false ) ) bAboveLeftFlag  = true;
-#endif // REFERENCE_SAMPLE_PADDING
-#endif //CONSTRAINED_INTRA_PRED
-  
-#if REFERENCE_SAMPLE_PADDING
+  iUnitSize      = (g_uiMaxCUWidth >> g_uiMaxCUDepth) >> 1; // for chroma
+  iNumUnitsInCu  = (uiCuWidth / iUnitSize) >> 1;            // for chroma
+  iTotalUnits    = (iNumUnitsInCu << 2) + 1;
+
+  bNeighborFlags[iNumUnitsInCu*2] = isAboveLeftAvailable( pcCU, uiPartIdxLT );
+  iNumIntraNeighbor  += (Int)(bNeighborFlags[iNumUnitsInCu*2]);
+  iNumIntraNeighbor  += isAboveAvailable     ( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*2)+1 );
+  iNumIntraNeighbor  += isAboveRightAvailable( pcCU, uiPartIdxLT, uiPartIdxRT, bNeighborFlags+(iNumUnitsInCu*3)+1 );
+  iNumIntraNeighbor  += isLeftAvailable      ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+(iNumUnitsInCu*2)-1 );
+  iNumIntraNeighbor  += isBelowLeftAvailable ( pcCU, uiPartIdxLT, uiPartIdxLB, bNeighborFlags+ iNumUnitsInCu   -1 );
+  
   bAbove = true;
   bLeft  = true;
-#else // REFERENCE_SAMPLE_PADDING
-  bAbove = bAboveFlag;
-  bLeft  = bLeftFlag;
-
-  Int iDCValue = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
-#endif // REFERENCE_SAMPLE_PADDING
   
   uiCuWidth=uiCuWidth>>1;  // for chroma
@@ -669,5 +429,7 @@
   
   if ((4*uiWidth>iOrgBufStride)||(4*uiHeight>iOrgBufHeight))
+  {
     return;
+  }
   
   // get Cb pattern
@@ -675,65 +437,9 @@
   piAdiTemp   = piAdiBuf;
 
-#if REFERENCE_SAMPLE_PADDING
-#if DEPTH_MAP_GENERATION
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, false );
-#else
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride );
-#endif
-#else // REFERENCE_SAMPLE_PADDING
-  for (i=0;i<uiWidth;i++)
-    piAdiTemp[i]=iDCValue;
-  for (i=0;i<uiHeight;i++)
-    piAdiTemp[i*uiWidth]=iDCValue;
-  
-  piRoiTemp=piRoiOrigin;
-  
-  if (bAboveFlag)
-  {
-    piRoiTemp=piRoiOrigin-iPicStride;
-    for (i=0;i<uiCuWidth;i++)
-      piAdiTemp[1+i]=piRoiTemp[i];
-    if (bAboveRightFlag)
-    {
-      piRoiTemp=piRoiOrigin-iPicStride+uiCuWidth;
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piRoiTemp[i];
-    }
-    else
-    {
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piAdiTemp[uiCuWidth];
-    }
-  }
-  
-  if (bAboveLeftFlag)
-  {
-    piRoiTemp=piRoiOrigin-iPicStride-1;
-    piAdiTemp[0]=piRoiTemp[0];
-  }
-
-  if (bLeftFlag)
-  {
-    piRoiTemp=piRoiOrigin-1;
-    for (i=0;i<uiCuHeight;i++)
-    {
-      piAdiTemp[(1+i)*uiWidth]=piRoiTemp[0];
-      piRoiTemp+=iPicStride;
-    }
-    if (bBelowLeftFlag)
-    {
-      for (i=0;i<uiCuHeight;i++)
-      {
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piRoiTemp[0];
-        piRoiTemp+=iPicStride;
-      }
-    }
-    else
-    {
-      for (i=0;i<uiCuHeight;i++)
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piAdiTemp[(uiCuHeight)*uiWidth];
-    }
-  }
-#endif // REFERENCE_SAMPLE_PADDING
+#if DEPTH_MAP_GENERATION
+  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, false, false );
+#else
+  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride);
+#endif
   
   // get Cr pattern
@@ -741,75 +447,17 @@
   piAdiTemp   = piAdiBuf+uiWidth*uiHeight;
   
-#if REFERENCE_SAMPLE_PADDING
-#if DEPTH_MAP_GENERATION
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, false );
-#else
-  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride );
-#endif
-  delete [] bNeighborFlags;
-  bNeighborFlags = NULL;
-#else // REFERENCE_SAMPLE_PADDING
-  for (i=0;i<uiWidth;i++)
-    piAdiTemp[i]=iDCValue;
-  for (i=0;i<uiHeight;i++)
-    piAdiTemp[i*uiWidth]=iDCValue;
-  
-  piRoiTemp=piRoiOrigin;
-  
-  if (bAboveFlag)
-  {
-    piRoiTemp=piRoiOrigin-iPicStride;
-    for (i=0;i<uiCuWidth;i++)
-      piAdiTemp[1+i]=piRoiTemp[i];
-    if (bAboveRightFlag)
-    {
-      piRoiTemp=piRoiOrigin-iPicStride+uiCuWidth;
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piRoiTemp[i];
-    }
-    else
-    {
-      for (i=0;i<uiCuWidth;i++)
-        piAdiTemp[1+uiCuWidth+i]=piAdiTemp[uiCuWidth];
-    }
-  }
-
-  if (bAboveLeftFlag)
-  {
-    piRoiTemp=piRoiOrigin-iPicStride-1;
-    piAdiTemp[0]=piRoiTemp[0];
-  }
-
-  if (bLeftFlag)
-  {
-    piRoiTemp=piRoiOrigin-1;
-    for (i=0;i<uiCuHeight;i++)
-    {
-      piAdiTemp[(1+i)*uiWidth]=piRoiTemp[0];
-      piRoiTemp+=iPicStride;
-    }
-    if (bBelowLeftFlag)
-    {
-      for (i=0;i<uiCuHeight;i++)
-      {
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piRoiTemp[0];
-        piRoiTemp+=iPicStride;
-      }
-    }
-    else
-    {
-      for (i=0;i<uiCuHeight;i++)
-        piAdiTemp[(1+uiCuHeight+i)*uiWidth]=piAdiTemp[(uiCuHeight)*uiWidth];
-    }
-  }
-#endif // REFERENCE_SAMPLE_PADDING
-}
-
-#if REFERENCE_SAMPLE_PADDING
-Void TComPattern::fillReferenceSamples( TComDataCU* pcCU, Pel* piRoiOrigin, Int* piAdiTemp, Bool* bNeighborFlags, Int iNumIntraNeighbor, Int iUnitSize, Int iNumUnitsInCu, Int iTotalUnits, UInt uiCuWidth, UInt uiCuHeight, UInt uiWidth, UInt uiHeight, Int iPicStride
-#if DEPTH_MAP_GENERATION
-                                        , Bool bPrdDepthMap 
-#endif
-                                        )
+#if DEPTH_MAP_GENERATION
+  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride, false, false );
+#else
+  fillReferenceSamples ( pcCU, piRoiOrigin, piAdiTemp, bNeighborFlags, iNumIntraNeighbor, iUnitSize, iNumUnitsInCu, iTotalUnits, uiCuWidth, uiCuHeight, uiWidth, uiHeight, iPicStride);
+#endif
+
+}
+
+Void TComPattern::fillReferenceSamples( TComDataCU* pcCU, Pel* piRoiOrigin, Int* piAdiTemp, Bool* bNeighborFlags, Int iNumIntraNeighbor, Int iUnitSize, Int iNumUnitsInCu, Int iTotalUnits, UInt uiCuWidth, UInt uiCuHeight, UInt uiWidth, UInt uiHeight, Int iPicStride, Bool bLMmode 
+#if DEPTH_MAP_GENERATION
+                                       , Bool bPrdDepthMap 
+#endif
+                                       )
 {
   Pel* piRoiTemp;
@@ -820,12 +468,16 @@
   Int  iDCValue = ( 1<<( g_uiBitDepth + g_uiBitIncrement - 1) );
 #endif
-  
+
   if (iNumIntraNeighbor == 0)
   {
     // Fill border with DC value
     for (i=0; i<uiWidth; i++)
+    {
       piAdiTemp[i] = iDCValue;
+    }
     for (i=1; i<uiHeight; i++)
+    {
       piAdiTemp[i*uiWidth] = iDCValue;
+    }
   }
   else if (iNumIntraNeighbor == iTotalUnits)
@@ -837,4 +489,10 @@
     // Fill left border with rec. samples
     piRoiTemp = piRoiOrigin - 1;
+
+    if (bLMmode)
+    {
+      piRoiTemp --; // move to the second left column
+    }
+
     for (i=0; i<uiCuHeight; i++)
     {
@@ -853,10 +511,14 @@
     piRoiTemp = piRoiOrigin - iPicStride;
     for (i=0; i<uiCuWidth; i++)
+    {
       piAdiTemp[1+i] = piRoiTemp[i];
-
+    }
+    
     // Fill top right border with rec. samples
     piRoiTemp = piRoiOrigin - iPicStride + uiCuWidth;
     for (i=0; i<uiCuWidth; i++)
+    {
       piAdiTemp[1+uiCuWidth+i] = piRoiTemp[i];
+    }
   }
   else // reference samples are partially available
@@ -864,14 +526,16 @@
     Int  iNumUnits2 = iNumUnitsInCu<<1;
     Int  iTotalSamples = iTotalUnits*iUnitSize;
-    Pel  *piAdiLine = new Pel[iTotalSamples];
+    Pel  piAdiLine[5 * MAX_CU_SIZE];
     Pel  *piAdiLineTemp; 
     Bool *pbNeighborFlags;
-    Int  iPrev, iNext, iCurr;
+    Int  iNext, iCurr;
     Pel  piRef = 0;
 
     // Initialize
     for (i=0; i<iTotalSamples; i++)
+    {
       piAdiLine[i] = iDCValue;
-
+    }
+    
     // Fill top-left sample
     piRoiTemp = piRoiOrigin - iPicStride - 1;
@@ -882,9 +546,15 @@
       piAdiLineTemp[0] = piRoiTemp[0];
       for (i=1; i<iUnitSize; i++)
+      {
         piAdiLineTemp[i] = piAdiLineTemp[0];
+      }
     }
 
     // Fill left & below-left samples
     piRoiTemp += iPicStride;
+    if (bLMmode)
+    {
+      piRoiTemp --; // move the second left column
+    }
     piAdiLineTemp--;
     pbNeighborFlags--;
@@ -894,5 +564,7 @@
       {
         for (i=0; i<iUnitSize; i++)
+        {
           piAdiLineTemp[-i] = piRoiTemp[i*iPicStride];
+        }
       }
       piRoiTemp += iUnitSize*iPicStride;
@@ -910,5 +582,7 @@
       {
         for (i=0; i<iUnitSize; i++)
+        {
           piAdiLineTemp[i] = piRoiTemp[i];
+        }
       }
       piRoiTemp += iUnitSize;
@@ -918,5 +592,4 @@
 
     // Pad reference samples when necessary
-    iPrev = -1;
     iCurr = 0;
     iNext = 1;
@@ -924,38 +597,39 @@
     while (iCurr < iTotalUnits)
     {
-      if (bNeighborFlags[iCurr])
-      {
-        // Move on to next block if current unit is available
-        piAdiLineTemp += iUnitSize;
-        iPrev++;
-        iCurr++;
-        iNext++;
-      }
-      else
-      {
-        // Interpolate from nearest samples if current unit is not available
-        
-        while (iNext < iTotalUnits && !bNeighborFlags[iNext])
-          iNext++;
-
-        if (iPrev >= 0 && iNext < iTotalUnits)
-          piRef = (piAdiLine[iCurr*iUnitSize-1] + piAdiLine[iNext*iUnitSize] + 1) >> 1;
-        else if (iPrev >= 0)
+      if (!bNeighborFlags[iCurr])
+      {
+        if(iCurr == 0)
+        {
+          while (iNext < iTotalUnits && !bNeighborFlags[iNext])
+          {
+            iNext++;
+          }
+          piRef = piAdiLine[iNext*iUnitSize];
+          // Pad unavailable samples with new value
+          while (iCurr < iNext)
+          {
+            for (i=0; i<iUnitSize; i++)
+            {
+              piAdiLineTemp[i] = piRef;
+            }
+            piAdiLineTemp += iUnitSize;
+            iCurr++;
+          }
+        }
+        else
+        {
           piRef = piAdiLine[iCurr*iUnitSize-1];
-        else if (iNext < iTotalUnits)
-          piRef = piAdiLine[iNext*iUnitSize];
-        else
-          printf("\nERROR! No valid samples to interpolate.\n");
-
-        // Pad unavailable samples with new value
-        while (iCurr < iNext)
-        {
           for (i=0; i<iUnitSize; i++)
+          {
             piAdiLineTemp[i] = piRef;
+          }
           piAdiLineTemp += iUnitSize;
-          iPrev++;
           iCurr++;
         }
-        iNext++;
+      }
+      else
+      {
+        piAdiLineTemp += iUnitSize;
+        iCurr++;
       }
     }
@@ -964,14 +638,14 @@
     piAdiLineTemp = piAdiLine + uiHeight + iUnitSize - 2;
     for (i=0; i<uiWidth; i++)
+    {
       piAdiTemp[i] = piAdiLineTemp[i];
+    }
     piAdiLineTemp = piAdiLine + uiHeight - 1;
     for (i=1; i<uiHeight; i++)
+    {
       piAdiTemp[i*uiWidth] = piAdiLineTemp[-i];
-
-    delete [] piAdiLine;
-    piAdiLine = NULL;
-  }
-}
-#endif // REFERENCE_SAMPLE_PADDING
+    }
+  }
+}
 
 Int* TComPattern::getAdiOrgBuf( Int iCuWidth, Int iCuHeight, Int* piAdiBuf)
@@ -990,94 +664,68 @@
 }
 
-#if QC_MDIS
-Int* TComPattern::getPredictorPtr ( UInt uiDirMode, UInt uiWidthBits, Int iCuWidth, Int iCuHeight, Int* piAdiBuf )
-{
-#if MN_MDIS_SIMPLIFICATION
-  static const UChar g_aucIntraFilter[7][34] =
-  {
-      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //2x2
-      {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //4x4
-      {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //8x8
-      {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //16x16
-      {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //32x32
-      {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //64x64
-      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}  //128x128
-  };
-#else
-  static const UChar g_aucIntraFilter[7][34] =
-  {
-      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //2x2
-      {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //4x4
-      {0, 0, 0, 1, 1, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //8x8
-      {0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //16x16
-      {0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, //32x32
-      {0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //64x64
-      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}  //128x128
-  };
-#endif
-
+/** Get pointer to reference samples for intra prediction
+ * \param uiDirMode   prediction mode index
+ * \param log2BlkSize size of block (2 = 4x4, 3 = 8x8, 4 = 16x16, 5 = 32x32, 6 = 64x64)
+ * \param piAdiBuf    pointer to unfiltered reference samples
+ * \return            pointer to (possibly filtered) reference samples
+ *
+ * The prediction mode index is used to determine whether a smoothed reference sample buffer is returned.
+ */
+Int* TComPattern::getPredictorPtr( UInt uiDirMode, UInt log2BlkSize, Int* piAdiBuf )
+{
   Int* piSrc;
-#if ADD_PLANAR_MODE
-  mapPlanartoDC( uiDirMode );
-#endif
+  assert(log2BlkSize >= 2 && log2BlkSize < 7);
 #if HHI_DMM_WEDGE_INTRA || HHI_DMM_PRED_TEX
-  UChar ucFiltIdx = 0;
-  if ( uiDirMode < 34 )
-  {
-    ucFiltIdx = g_aucIntraFilter[uiWidthBits][uiDirMode];
-  }
-#else
-  UChar ucFiltIdx = g_aucIntraFilter[uiWidthBits][uiDirMode];
-#endif
-
-#if MN_MDIS_SIMPLIFICATION
+  mapDMMtoIntraMode( uiDirMode );
+#endif
+#if LOGI_INTRA_NAME_3MPM
+  Int diff = min<Int>(abs((Int) uiDirMode - HOR_IDX), abs((Int)uiDirMode - VER_IDX));
+  UChar ucFiltIdx = diff > m_aucIntraFilter[log2BlkSize - 2] ? 1 : 0;
+  if (uiDirMode == DC_IDX || uiDirMode == LM_CHROMA_IDX)
+  {
+    ucFiltIdx = 0; //no smoothing for DC or LM chroma
+  }
+#else
+  UChar ucFiltIdx = m_aucIntraFilter[log2BlkSize - 2][uiDirMode];
+#endif
+
   assert( ucFiltIdx <= 1 );
-#else
-  assert( ucFiltIdx <= 2 );
-#endif
-
-  piSrc = getAdiOrgBuf( iCuWidth, iCuHeight, piAdiBuf );
+
+  Int width  = 1 << log2BlkSize;
+  Int height = 1 << log2BlkSize;
+  
+  piSrc = getAdiOrgBuf( width, height, piAdiBuf );
 
   if ( ucFiltIdx )
   {
-#if MN_MDIS_SIMPLIFICATION
-    piSrc += ((iCuWidth << 1) + 1) * ((iCuHeight << 1) + 1);
-#else
-    piSrc += (((iCuWidth << 1) + 1) * ((iCuHeight << 1) + 1) << (ucFiltIdx - 1));
-#endif
+    piSrc += (2 * width + 1) * (2 * height + 1);
   }
 
   return piSrc;
 }
-#endif //QC_MDIS
-
-#if CONSTRAINED_INTRA_PRED
-Bool TComPattern::isAboveLeftAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT )
+
+Bool TComPattern::isAboveLeftAvailable( TComDataCU* pcCU, UInt uiPartIdxLT )
 {
   Bool bAboveLeftFlag;
   UInt uiPartAboveLeft;
   TComDataCU* pcCUAboveLeft = pcCU->getPUAboveLeft( uiPartAboveLeft, uiPartIdxLT, true, false );
-  bAboveLeftFlag = ( pcCUAboveLeft && pcCUAboveLeft->getPredictionMode( uiPartAboveLeft ) == MODE_INTRA );
+  if(pcCU->getSlice()->getPPS()->getConstrainedIntraPred())
+  {
+    bAboveLeftFlag = ( pcCUAboveLeft && pcCUAboveLeft->getPredictionMode( uiPartAboveLeft ) == MODE_INTRA );
+  }
+  else
+  {
+    bAboveLeftFlag = (pcCUAboveLeft ? true : false);
+  }
   return bAboveLeftFlag;
 }
 
-#if REFERENCE_SAMPLE_PADDING
-Int TComPattern::isAboveAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT, Bool *bValidFlags )
-#else
-Bool TComPattern::isAboveAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT )
-#endif
+Int TComPattern::isAboveAvailable( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT, Bool *bValidFlags )
 {
   const UInt uiRasterPartBegin = g_auiZscanToRaster[uiPartIdxLT];
   const UInt uiRasterPartEnd = g_auiZscanToRaster[uiPartIdxRT]+1;
   const UInt uiIdxStep = 1;
-#if REFERENCE_SAMPLE_PADDING
   Bool *pbValidFlags = bValidFlags;
   Int iNumIntra = 0;
-#else
-  Bool bAboveFlag = true;
-#endif
-#if MN_DC_PRED_FILTER
-  m_bAboveFlagForDCFilt = true;
-#endif
 
   for ( UInt uiRasterPart = uiRasterPartBegin; uiRasterPart < uiRasterPartEnd; uiRasterPart += uiIdxStep )
@@ -1085,54 +733,40 @@
     UInt uiPartAbove;
     TComDataCU* pcCUAbove = pcCU->getPUAbove( uiPartAbove, g_auiRasterToZscan[uiRasterPart], true, false );
-#if REFERENCE_SAMPLE_PADDING
-    if ( pcCUAbove && pcCUAbove->getPredictionMode( uiPartAbove ) == MODE_INTRA )
-    {
-      iNumIntra++;
-      *pbValidFlags = true;
+    if(pcCU->getSlice()->getPPS()->getConstrainedIntraPred())
+    {
+      if ( pcCUAbove && pcCUAbove->getPredictionMode( uiPartAbove ) == MODE_INTRA )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     else
     {
-      *pbValidFlags = false;
-#if MN_DC_PRED_FILTER
-      m_bAboveFlagForDCFilt = false;
-#endif
+      if (pcCUAbove)
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     pbValidFlags++;
-#else
-    if ( !pcCUAbove || pcCUAbove->getPredictionMode( uiPartAbove ) != MODE_INTRA )
-    {
-      bAboveFlag = false;
-#if MN_DC_PRED_FILTER
-      m_bAboveFlagForDCFilt = false;
-#endif
-      break;
-    }
-#endif
-  }
-#if REFERENCE_SAMPLE_PADDING
+  }
   return iNumIntra;
-#else
-  return bAboveFlag;
-#endif
-}
-
-#if REFERENCE_SAMPLE_PADDING
-Int TComPattern::isLeftAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB, Bool *bValidFlags )
-#else
-Bool TComPattern::isLeftAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB )
-#endif
+}
+
+Int TComPattern::isLeftAvailable( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB, Bool *bValidFlags )
 {
   const UInt uiRasterPartBegin = g_auiZscanToRaster[uiPartIdxLT];
   const UInt uiRasterPartEnd = g_auiZscanToRaster[uiPartIdxLB]+1;
   const UInt uiIdxStep = pcCU->getPic()->getNumPartInWidth();
-#if REFERENCE_SAMPLE_PADDING
   Bool *pbValidFlags = bValidFlags;
   Int iNumIntra = 0;
-#else
-  Bool bLeftFlag = true;
-#endif
-#if MN_DC_PRED_FILTER
-  m_bLeftFlagForDCFilt = true;
-#endif
 
   for ( UInt uiRasterPart = uiRasterPartBegin; uiRasterPart < uiRasterPartEnd; uiRasterPart += uiIdxStep )
@@ -1140,51 +774,40 @@
     UInt uiPartLeft;
     TComDataCU* pcCULeft = pcCU->getPULeft( uiPartLeft, g_auiRasterToZscan[uiRasterPart], true, false );
-#if REFERENCE_SAMPLE_PADDING
-    if ( pcCULeft && pcCULeft->getPredictionMode( uiPartLeft ) == MODE_INTRA )
-    {
-      iNumIntra++;
-      *pbValidFlags = true;
+    if(pcCU->getSlice()->getPPS()->getConstrainedIntraPred())
+    {
+      if ( pcCULeft && pcCULeft->getPredictionMode( uiPartLeft ) == MODE_INTRA )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     else
     {
-      *pbValidFlags = false;
-#if MN_DC_PRED_FILTER
-      m_bLeftFlagForDCFilt = false;
-#endif
+      if ( pcCULeft )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     pbValidFlags--; // opposite direction
-#else
-    if ( !pcCULeft || pcCULeft->getPredictionMode( uiPartLeft ) != MODE_INTRA )
-    {
-      bLeftFlag = false;
-#if MN_DC_PRED_FILTER
-      m_bLeftFlagForDCFilt = false;
-#endif
-      break;
-    }
-#endif
-  }
-
-#if REFERENCE_SAMPLE_PADDING
+  }
+
   return iNumIntra;
-#else
-  return bLeftFlag;
-#endif
-}
-
-#if REFERENCE_SAMPLE_PADDING
-Int TComPattern::isAboveRightAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT, Bool *bValidFlags )
-#else
-Bool TComPattern::isAboveRightAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT )
-#endif
+}
+
+Int TComPattern::isAboveRightAvailable( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxRT, Bool *bValidFlags )
 {
   const UInt uiNumUnitsInPU = g_auiZscanToRaster[uiPartIdxRT] - g_auiZscanToRaster[uiPartIdxLT] + 1;
   const UInt uiPuWidth = uiNumUnitsInPU * pcCU->getPic()->getMinCUWidth();
-#if REFERENCE_SAMPLE_PADDING
   Bool *pbValidFlags = bValidFlags;
   Int iNumIntra = 0;
-#else
-  Bool bAboveRightFlag = true;
-#endif
 
   for ( UInt uiOffset = 1; uiOffset <= uiNumUnitsInPU; uiOffset++ )
@@ -1192,45 +815,40 @@
     UInt uiPartAboveRight;
     TComDataCU* pcCUAboveRight = pcCU->getPUAboveRightAdi( uiPartAboveRight, uiPuWidth, uiPartIdxRT, uiOffset, true, false );
-#if REFERENCE_SAMPLE_PADDING
-    if ( pcCUAboveRight && pcCUAboveRight->getPredictionMode( uiPartAboveRight ) == MODE_INTRA )
-    {
-      iNumIntra++;
-      *pbValidFlags = true;
+    if(pcCU->getSlice()->getPPS()->getConstrainedIntraPred())
+    {
+      if ( pcCUAboveRight && pcCUAboveRight->getPredictionMode( uiPartAboveRight ) == MODE_INTRA )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     else
     {
-      *pbValidFlags = false;
+      if ( pcCUAboveRight )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     pbValidFlags++;
-#else
-    if ( !pcCUAboveRight || pcCUAboveRight->getPredictionMode( uiPartAboveRight ) != MODE_INTRA )
-    {
-      bAboveRightFlag = false;
-      break;
-    }
-#endif
-  }
-
-#if REFERENCE_SAMPLE_PADDING
+  }
+
   return iNumIntra;
-#else
-  return bAboveRightFlag;
-#endif
-}
-
-#if REFERENCE_SAMPLE_PADDING
-Int TComPattern::isBelowLeftAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB, Bool *bValidFlags )
-#else
-Bool TComPattern::isBelowLeftAvailableForCIP( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB )
-#endif
+}
+
+Int TComPattern::isBelowLeftAvailable( TComDataCU* pcCU, UInt uiPartIdxLT, UInt uiPartIdxLB, Bool *bValidFlags )
 {
   const UInt uiNumUnitsInPU = (g_auiZscanToRaster[uiPartIdxLB] - g_auiZscanToRaster[uiPartIdxLT]) / pcCU->getPic()->getNumPartInWidth() + 1;
   const UInt uiPuHeight = uiNumUnitsInPU * pcCU->getPic()->getMinCUHeight();
-#if REFERENCE_SAMPLE_PADDING
   Bool *pbValidFlags = bValidFlags;
   Int iNumIntra = 0;
-#else
-  Bool bBelowLeftFlag = true;
-#endif
 
   for ( UInt uiOffset = 1; uiOffset <= uiNumUnitsInPU; uiOffset++ )
@@ -1238,29 +856,32 @@
     UInt uiPartBelowLeft;
     TComDataCU* pcCUBelowLeft = pcCU->getPUBelowLeftAdi( uiPartBelowLeft, uiPuHeight, uiPartIdxLB, uiOffset, true, false );
-#if REFERENCE_SAMPLE_PADDING
-    if ( pcCUBelowLeft && pcCUBelowLeft->getPredictionMode( uiPartBelowLeft ) == MODE_INTRA )
-    {
-      iNumIntra++;
-      *pbValidFlags = true;
+    if(pcCU->getSlice()->getPPS()->getConstrainedIntraPred())
+    {
+      if ( pcCUBelowLeft && pcCUBelowLeft->getPredictionMode( uiPartBelowLeft ) == MODE_INTRA )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     else
     {
-      *pbValidFlags = false;
+      if ( pcCUBelowLeft )
+      {
+        iNumIntra++;
+        *pbValidFlags = true;
+      }
+      else
+      {
+        *pbValidFlags = false;
+      }
     }
     pbValidFlags--; // opposite direction
-#else
-    if ( !pcCUBelowLeft || pcCUBelowLeft->getPredictionMode( uiPartBelowLeft ) != MODE_INTRA )
-    {
-      bBelowLeftFlag = false;
-      break;
-    }
-#endif
-  }
-
-#if REFERENCE_SAMPLE_PADDING
+  }
+
   return iNumIntra;
-#else
-  return bBelowLeftFlag;
-#endif
-}
-#endif //CONSTRAINED_INTRA_PRED
+}
+//! \}
