Skip to content
Snippets Groups Projects
IntraPrediction.cpp 72.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
    
      Pel *refBufUnfiltered   = m_piYuvExt[area.compID][PRED_BUF_UNFILTERED];
      Pel *refBufFiltered     = m_piYuvExt[area.compID][PRED_BUF_FILTERED];
    
      setReferenceArrayLengths(area);
    
      // ----- Step 1: unfiltered reference samples -----
      xFillReferenceSamples( cs.picture->getRecoBuf( area ), refBufUnfiltered, area, cu );
      // ----- Step 2: filtered reference samples -----
      if( bFilterRefSamples )
      {
        xFilterReferenceSamples( refBufUnfiltered, refBufFiltered, area, *cs.sps );
      }
    }
    
    void IntraPrediction::xFillReferenceSamples( const CPelBuf &recoBuf, Pel* refBufUnfiltered, const CompArea &area, const CodingUnit &cu )
    {
      const ChannelType      chType = toChannelType( area.compID );
      const CodingStructure &cs     = *cu.cs;
      const SPS             &sps    = *cs.sps;
      const PreCalcValues   &pcv    = *cs.pcv;
    
      const int  tuWidth            = area.width;
      const int  tuHeight           = area.height;
      const int  predSize           = m_topRefLength;
      const int  predHSize          = m_leftRefLength;
      const int  predStride         = predSize + 1;
    
      const bool noShift            = pcv.noChroma2x2 && area.width == 4; // don't shift on the lowest level (chroma not-split)
      const int  unitWidth          = pcv.minCUWidth  >> (noShift ? 0 : getComponentScaleX( area.compID, sps.getChromaFormatIdc() ));
      const int  unitHeight         = pcv.minCUHeight >> (noShift ? 0 : getComponentScaleY( area.compID, sps.getChromaFormatIdc() ));
    
      const int  totalAboveUnits    = (predSize + (unitWidth - 1)) / unitWidth;
      const int  totalLeftUnits     = (predHSize + (unitHeight - 1)) / unitHeight;
      const int  totalUnits         = totalAboveUnits + totalLeftUnits + 1; //+1 for top-left
      const int  numAboveUnits      = std::max<int>( tuWidth / unitWidth, 1 );
      const int  numLeftUnits       = std::max<int>( tuHeight / unitHeight, 1 );
      const int  numAboveRightUnits = totalAboveUnits - numAboveUnits;
      const int  numLeftBelowUnits  = totalLeftUnits - numLeftUnits;
    
      CHECK( numAboveUnits <= 0 || numLeftUnits <= 0 || numAboveRightUnits <= 0 || numLeftBelowUnits <= 0, "Size not supported" );
    
      // ----- Step 1: analyze neighborhood -----
      const Position posLT          = area;
      const Position posRT          = area.topRight();
      const Position posLB          = area.bottomLeft();
    
      bool  neighborFlags[4 * MAX_NUM_PART_IDXS_IN_CTU_WIDTH + 1];
      int   numIntraNeighbor = 0;
    
      memset( neighborFlags, 0, totalUnits );
    
      neighborFlags[totalLeftUnits] = isAboveLeftAvailable( cu, chType, posLT );
      numIntraNeighbor += neighborFlags[totalLeftUnits] ? 1 : 0;
      numIntraNeighbor += isAboveAvailable     ( cu, chType, posLT, numAboveUnits,      unitWidth,  (neighborFlags + totalLeftUnits + 1) );
      numIntraNeighbor += isAboveRightAvailable( cu, chType, posRT, numAboveRightUnits, unitWidth,  (neighborFlags + totalLeftUnits + 1 + numAboveUnits) );
      numIntraNeighbor += isLeftAvailable      ( cu, chType, posLT, numLeftUnits,       unitHeight, (neighborFlags + totalLeftUnits - 1) );
      numIntraNeighbor += isBelowLeftAvailable ( cu, chType, posLB, numLeftBelowUnits,  unitHeight, (neighborFlags + totalLeftUnits - 1 - numLeftUnits) );
    
      // ----- Step 2: fill reference samples (depending on neighborhood) -----
      CHECK((predHSize + 1) * predStride > m_iYuvExtSize, "Reference sample area not supported");
    
      const Pel*  srcBuf    = recoBuf.buf;
      const int   srcStride = recoBuf.stride;
            Pel*  ptrDst    = refBufUnfiltered;
      const Pel*  ptrSrc;
      const Pel   valueDC   = 1 << (sps.getBitDepth( chType ) - 1);
    
    
      if( numIntraNeighbor == 0 )
      {
        // Fill border with DC value
        for( int j = 0; j <= predSize; j++ ) { ptrDst[j]            = valueDC; }
        for( int i = 1; i <= predHSize; i++ ) { ptrDst[i*predStride] = valueDC; }
      }
      else if( numIntraNeighbor == totalUnits )
      {
        // Fill top-left border and top and top right with rec. samples
        ptrSrc = srcBuf - srcStride - 1;
        for( int j = 0; j <= predSize; j++ ) { ptrDst[j] = ptrSrc[j]; }
        // Fill left and below left border with rec. samples
        ptrSrc = srcBuf - 1;
        for( int i = 1; i <= predHSize; i++ ) { ptrDst[i*predStride] = *(ptrSrc); ptrSrc += srcStride; }
      }
      else // reference samples are partially available
      {
        // BB: old implementation using tmpLineBuf
        // ---------------------------------------
        Pel  tmpLineBuf[5 * MAX_CU_SIZE];
        Pel* ptrTmp;
        int  unitIdx;
    
        // Initialize
        const int totalSamples = (totalLeftUnits * unitHeight) + ((totalAboveUnits + 1) * unitWidth); // all above units have "unitWidth" samples each, all left/below-left units have "unitHeight" samples each
        for( int k = 0; k < totalSamples; k++ ) { tmpLineBuf[k] = valueDC; }
    
        // Fill top-left sample
        ptrSrc = srcBuf - srcStride - 1;
        ptrTmp = tmpLineBuf + (totalLeftUnits * unitHeight);
        unitIdx = totalLeftUnits;
        if( neighborFlags[unitIdx] )
        {
          Pel topLeftVal = ptrSrc[0];
          for( int j = 0; j < unitWidth; j++ ) { ptrTmp[j] = topLeftVal; }
        }
    
        // Fill left & below-left samples (downwards)
        ptrSrc += srcStride;
        ptrTmp--;
        unitIdx--;
    
        for( int k = 0; k < totalLeftUnits; k++ )
        {
          if( neighborFlags[unitIdx] )
          {
            for( int i = 0; i < unitHeight; i++ ) { ptrTmp[-i] = ptrSrc[i*srcStride]; }
          }
          ptrSrc += unitHeight*srcStride;
          ptrTmp -= unitHeight;
          unitIdx--;
        }
    
        // Fill above & above-right samples (left-to-right) (each unit has "unitWidth" samples)
        ptrSrc = srcBuf - srcStride;
        ptrTmp = tmpLineBuf + (totalLeftUnits * unitHeight) + unitWidth; // offset line buffer by totalLeftUnits*unitHeight (for left/below-left) + unitWidth (for above-left)
        unitIdx = totalLeftUnits + 1;
        for( int k = 0; k < totalAboveUnits; k++ )
        {
          if( neighborFlags[unitIdx] )
          {
            for( int j = 0; j < unitWidth; j++ ) { ptrTmp[j] = ptrSrc[j]; }
          }
          ptrSrc += unitWidth;
          ptrTmp += unitWidth;
          unitIdx++;
        }
    
        // Pad reference samples when necessary
        int  currUnit       = 0;
        Pel* ptrTmpCurrUnit = tmpLineBuf;
    
        if( !neighborFlags[0] )
        {
          int nextUnit = 1;
          while( nextUnit < totalUnits && !neighborFlags[nextUnit] )
          {
            nextUnit++;
          }
          Pel* ptrTmpRef = tmpLineBuf + ((nextUnit < totalLeftUnits) ? (nextUnit * unitHeight) : ((totalLeftUnits * (unitHeight - unitWidth)) + (nextUnit * unitWidth)));
          const Pel refSample = *ptrTmpRef;
          // Pad unavailable samples with new value
          // fill left column
          while( currUnit < std::min<int>( nextUnit, totalLeftUnits ) )
          {
            for( int i = 0; i < unitHeight; i++ ) { ptrTmpCurrUnit[i] = refSample; }
            ptrTmpCurrUnit += unitHeight;
            currUnit++;
          }
          // fill top row
          while( currUnit < nextUnit )
          {
            for( int j = 0; j < unitWidth; j++ ) { ptrTmpCurrUnit[j] = refSample; }
            ptrTmpCurrUnit += unitWidth;
            currUnit++;
          }
        }
    
        // pad all other reference samples.
        while( currUnit < totalUnits )
        {
          const int numSamplesInCurrUnit = (currUnit >= totalLeftUnits) ? unitWidth : unitHeight;
          if( !neighborFlags[currUnit] ) // samples not available
          {
            const Pel refSample = *(ptrTmpCurrUnit - 1);
            for( int k = 0; k < numSamplesInCurrUnit; k++ ) { ptrTmpCurrUnit[k] = refSample; }
    
          }
          ptrTmpCurrUnit += numSamplesInCurrUnit;
          currUnit++;
        }
    
        // Copy processed samples
        ptrTmp = tmpLineBuf + (totalLeftUnits * unitHeight) + (unitWidth - 1);
        for( int j = 0; j <= predSize; j++ ) { ptrDst[j] = ptrTmp[j]; } // top left, top and top right samples
    
        ptrTmp = tmpLineBuf + (totalLeftUnits * unitHeight);
        for( int i = 1; i <= predHSize; i++ ) { ptrDst[i*predStride] = ptrTmp[-i]; }
      }
    }
    
    void IntraPrediction::xFilterReferenceSamples( const Pel* refBufUnfiltered, Pel* refBufFiltered, const CompArea &area, const SPS &sps )
    {
      const int  predSize   = m_topRefLength;
      const int  predHSize  = m_leftRefLength;
      const int  predStride = predSize + 1;
    
    
    #if HEVC_USE_INTRA_SMOOTHING_T32 || HEVC_USE_INTRA_SMOOTHING_T64
      // Strong intra smoothing
      ChannelType chType = toChannelType( area.compID );
      if( sps.getUseStrongIntraSmoothing() && isLuma( chType ) )
      {
        const Pel bottomLeft = refBufUnfiltered[predStride * predHSize];
        const Pel topLeft    = refBufUnfiltered[0];
        const Pel topRight   = refBufUnfiltered[predSize];
    
        const int  threshold     = 1 << (sps.getBitDepth( chType ) - 5);
        const bool bilinearLeft  = abs( (bottomLeft + topLeft)  - (2 * refBufUnfiltered[predStride * tuHeight]) ) < threshold; //difference between the
        const bool bilinearAbove = abs( (topLeft    + topRight) - (2 * refBufUnfiltered[             tuWidth ]) ) < threshold; //ends and the middle
    
        if( tuWidth >= 32 && tuHeight >= 32 && bilinearLeft && bilinearAbove )
    #if !HEVC_USE_INTRA_SMOOTHING_T32
        if( tuWidth > 32 && tuHeight > 32 )
    #endif
    #if !HEVC_USE_INTRA_SMOOTHING_T64
        if( tuWidth < 64 && tuHeight < 64 )
    #endif
        {
          Pel *piDestPtr = refBufFiltered + (predStride * predHSize); // bottom left
    
          // apply strong intra smoothing
          for (int i = 0; i < predHSize; i++, piDestPtr -= predStride) //left column (bottom to top)
          {
            *piDestPtr = (((predHSize - i) * bottomLeft) + (i * topLeft) + predHSize / 2) / predHSize;
          }
          for( uint32_t i = 0; i <= predSize; i++, piDestPtr++ )            //full top row (left-to-right)
          {
            *piDestPtr = (((predSize - i) * topLeft) + (i * topRight) + predSize / 2) / predSize;
          }
    
          return;
        }
      }
    #endif
    
      // Regular reference sample filter
      const Pel *piSrcPtr  = refBufUnfiltered + (predStride * predHSize); // bottom left
            Pel *piDestPtr = refBufFiltered   + (predStride * predHSize); // bottom left
    
      // bottom left (not filtered)
      *piDestPtr = *piSrcPtr;
      piDestPtr -= predStride;
      piSrcPtr  -= predStride;
      //left column (bottom to top)
      for( int i = 1; i < predHSize; i++, piDestPtr -= predStride, piSrcPtr -= predStride)
      {
        *piDestPtr = (piSrcPtr[predStride] + 2 * piSrcPtr[0] + piSrcPtr[-predStride] + 2) >> 2;
      }
      //top-left
      *piDestPtr = (piSrcPtr[predStride] + 2 * piSrcPtr[0] + piSrcPtr[1] + 2) >> 2;
      piDestPtr++;
      piSrcPtr++;
      //top row (left-to-right)
      for( uint32_t i=1; i < predSize; i++, piDestPtr++, piSrcPtr++ )
      {
        *piDestPtr = (piSrcPtr[1] + 2 * piSrcPtr[0] + piSrcPtr[-1] + 2) >> 2;
      }
      // top right (not filtered)
      *piDestPtr=*piSrcPtr;
    }
    
    bool IntraPrediction::useFilteredIntraRefSamples( const ComponentID &compID, const PredictionUnit &pu, bool modeSpecific, const UnitArea &tuArea )
    {
      const SPS         &sps    = *pu.cs->sps;
      const ChannelType  chType = toChannelType( compID );
    
      // high level conditions
      if( sps.getSpsRangeExtension().getIntraSmoothingDisabledFlag() )                                       { return false; }
      if( !isLuma( chType ) && pu.chromaFormat != CHROMA_444 )                                               { return false; }
    
    
      if( !modeSpecific )                                                                                    { return true; }
    
      // pred. mode related conditions
      const int dirMode = PU::getFinalIntraMode( pu, chType );
      int predMode = getWideAngle(tuArea.blocks[compID].width, tuArea.blocks[compID].height, dirMode);
      if (predMode != dirMode && (predMode < 2 || predMode > VDIA_IDX))                                      { return true; }
      if (dirMode == DC_IDX)                                                                                 { return false; }
      if (dirMode == PLANAR_IDX)
      {
        return tuArea.blocks[compID].width * tuArea.blocks[compID].height > 32 ? true : false;
      }
    
      int diff = std::min<int>( abs( dirMode - HOR_IDX ), abs( dirMode - VER_IDX ) );
      int log2Size = ((g_aucLog2[tuArea.blocks[compID].width] + g_aucLog2[tuArea.blocks[compID].height]) >> 1);
      CHECK( log2Size >= MAX_INTRA_FILTER_DEPTHS, "Size not supported" );
      return (diff > m_aucIntraFilter[chType][log2Size]);
    }
    
    
    bool isAboveLeftAvailable(const CodingUnit &cu, const ChannelType &chType, const Position &posLT)
    {
      const CodingStructure& cs = *cu.cs;
      const Position refPos = posLT.offset(-1, -1);
      const CodingUnit* pcCUAboveLeft = cs.isDecomp( refPos, chType ) ? cs.getCURestricted( refPos, cu, chType ) : nullptr;
      const bool isConstrained = cs.pps->getConstrainedIntraPred();
      bool bAboveLeftFlag;
    
      if (isConstrained)
      {
        bAboveLeftFlag = pcCUAboveLeft && CU::isIntra(*pcCUAboveLeft);
      }
      else
      {
        bAboveLeftFlag = (pcCUAboveLeft ? true : false);
      }
    
      return bAboveLeftFlag;
    }
    
    int isAboveAvailable(const CodingUnit &cu, const ChannelType &chType, const Position &posLT, const uint32_t uiNumUnitsInPU, const uint32_t unitWidth, bool *bValidFlags)
    {
      const CodingStructure& cs = *cu.cs;
      const bool isConstrained = cs.pps->getConstrainedIntraPred();
      bool *pbValidFlags = bValidFlags;
      int iNumIntra = 0;
      int maxDx = uiNumUnitsInPU * unitWidth;
    
      for (uint32_t dx = 0; dx < maxDx; dx += unitWidth)
      {
        const Position refPos = posLT.offset(dx, -1);
    
        const CodingUnit* pcCUAbove = cs.isDecomp(refPos, chType) ? cs.getCURestricted(refPos, cu, chType) : nullptr;
    
        if( pcCUAbove && ( ( isConstrained && CU::isIntra( *pcCUAbove ) ) || !isConstrained ) )
        {
          iNumIntra++;
          *pbValidFlags = true;
        }
        else if( !pcCUAbove )
        {
          return iNumIntra;
        }
    
        pbValidFlags++;
      }
      return iNumIntra;
    }
    
    int isLeftAvailable(const CodingUnit &cu, const ChannelType &chType, const Position &posLT, const uint32_t uiNumUnitsInPU, const uint32_t unitHeight, bool *bValidFlags)
    {
      const CodingStructure& cs = *cu.cs;
      const bool isConstrained = cs.pps->getConstrainedIntraPred();
      bool *pbValidFlags = bValidFlags;
      int iNumIntra = 0;
      int maxDy = uiNumUnitsInPU * unitHeight;
    
      for (uint32_t dy = 0; dy < maxDy; dy += unitHeight)
      {
        const Position refPos = posLT.offset(-1, dy);
    
        const CodingUnit* pcCULeft = cs.isDecomp(refPos, chType) ? cs.getCURestricted(refPos, cu, chType) : nullptr;
    
        if( pcCULeft && ( ( isConstrained && CU::isIntra( *pcCULeft ) ) || !isConstrained ) )
        {
          iNumIntra++;
          *pbValidFlags = true;
        }
        else if( !pcCULeft )
        {
          return iNumIntra;
        }
    
        pbValidFlags--; // opposite direction
      }
    
      return iNumIntra;
    }
    
    int isAboveRightAvailable(const CodingUnit &cu, const ChannelType &chType, const Position &posRT, const uint32_t uiNumUnitsInPU, const uint32_t unitWidth, bool *bValidFlags )
    {
      const CodingStructure& cs = *cu.cs;
      const bool isConstrained = cs.pps->getConstrainedIntraPred();
      bool *pbValidFlags = bValidFlags;
      int iNumIntra = 0;
    
      uint32_t maxDx = uiNumUnitsInPU * unitWidth;
    
      for (uint32_t dx = 0; dx < maxDx; dx += unitWidth)
      {
        const Position refPos = posRT.offset(unitWidth + dx, -1);
    
        const CodingUnit* pcCUAbove = cs.isDecomp(refPos, chType) ? cs.getCURestricted(refPos, cu, chType) : nullptr;
    
        if( pcCUAbove && ( ( isConstrained && CU::isIntra( *pcCUAbove ) ) || !isConstrained ) )
        {
          iNumIntra++;
          *pbValidFlags = true;
        }
        else if( !pcCUAbove )
        {
          return iNumIntra;
        }
    
        pbValidFlags++;
      }
    
      return iNumIntra;
    }
    
    int isBelowLeftAvailable(const CodingUnit &cu, const ChannelType &chType, const Position &posLB, const uint32_t uiNumUnitsInPU, const uint32_t unitHeight, bool *bValidFlags )
    {
      const CodingStructure& cs = *cu.cs;
      const bool isConstrained = cs.pps->getConstrainedIntraPred();
      bool *pbValidFlags = bValidFlags;
      int iNumIntra = 0;
      int maxDy = uiNumUnitsInPU * unitHeight;
    
      for (uint32_t dy = 0; dy < maxDy; dy += unitHeight)
      {
        const Position refPos = posLB.offset(-1, unitHeight + dy);
    
        const CodingUnit* pcCULeft = cs.isDecomp(refPos, chType) ? cs.getCURestricted(refPos, cu, chType) : nullptr;
    
        if( pcCULeft && ( ( isConstrained && CU::isIntra( *pcCULeft ) ) || !isConstrained ) )
        {
          iNumIntra++;
          *pbValidFlags = true;
        }
        else if ( !pcCULeft )
        {
          return iNumIntra;
        }
    
        pbValidFlags--; // opposite direction
      }
    
      return iNumIntra;
    }
    // LumaRecPixels
    void IntraPrediction::xGetLumaRecPixels(const PredictionUnit &pu, CompArea chromaArea)
    {
      int iDstStride = 0;
      Pel* pDst0 = 0;
    
    #if JVET_L0338_MDLM
      int curChromaMode = pu.intraDir[1];
      if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
      {
        iDstStride = 2 * MAX_CU_SIZE + 1;
        pDst0 = m_pMdlmTemp + iDstStride + 1;
      }
      else
      {
    #endif
    
      iDstStride = MAX_CU_SIZE + 1;
      pDst0 = m_piTemp + iDstStride + 1; //MMLM_SAMPLE_NEIGHBOR_LINES;
    
    #if JVET_L0338_MDLM
      }
    #endif
    
      //assert 420 chroma subsampling
      CompArea lumaArea = CompArea( COMPONENT_Y, pu.chromaFormat, chromaArea.lumaPos(), recalcSize( pu.chromaFormat, CHANNEL_TYPE_CHROMA, CHANNEL_TYPE_LUMA, chromaArea.size() ) );//needed for correct pos/size (4x4 Tus)
    
    
      CHECK( lumaArea.width  == chromaArea.width, "" );
      CHECK( lumaArea.height == chromaArea.height, "" );
    
      const SizeType uiCWidth = chromaArea.width;
      const SizeType uiCHeight = chromaArea.height;
    
      const CPelBuf Src = pu.cs->picture->getRecoBuf( lumaArea );
      Pel const* pRecSrc0   = Src.bufAt( 0, 0 );
      int iRecStride        = Src.stride;
      int iRecStride2       = iRecStride << 1;
    
      CodingStructure&      cs = *pu.cs;
      const CodingUnit& lumaCU = isChroma( pu.chType ) ? *pu.cs->picture->cs->getCU( lumaArea.pos(), CH_L ) : *pu.cu;
      const CodingUnit&     cu = *pu.cu;
    
      const CompArea& area = isChroma( pu.chType ) ? chromaArea : lumaArea;
    
      const SPS &sps = *cs.sps;
    
      const uint32_t uiTuWidth  = area.width;
      const uint32_t uiTuHeight = area.height;
    
      int iBaseUnitSize = ( 1 << MIN_CU_LOG2 );
    
      if( !cs.pcv->rectCUs )
      {
        iBaseUnitSize = sps.getMaxCUWidth() >> sps.getMaxCodingDepth();
      }
    
      const int  iUnitWidth       = iBaseUnitSize >> getComponentScaleX( area.compID, area.chromaFormat );
      const int  iUnitHeight      = iBaseUnitSize >> getComponentScaleX( area.compID, area.chromaFormat );
      const int  iTUWidthInUnits  = uiTuWidth  / iUnitWidth;
      const int  iTUHeightInUnits = uiTuHeight / iUnitHeight;
      const int  iAboveUnits      = iTUWidthInUnits;
      const int  iLeftUnits       = iTUHeightInUnits;
    
    #if JVET_L0338_MDLM
      const int  chromaUnitWidth = iBaseUnitSize >> getComponentScaleX(COMPONENT_Cb, area.chromaFormat);
      const int  chromaUnitHeight = iBaseUnitSize >> getComponentScaleX(COMPONENT_Cb, area.chromaFormat);
      const int  topTemplateSampNum = 2 * uiCWidth; // for MDLM, the number of template samples is 2W or 2H.
      const int  leftTemplateSampNum = 2 * uiCHeight;
      assert(m_topRefLength >= topTemplateSampNum);
      assert(m_leftRefLength >= leftTemplateSampNum);
      const int  totalAboveUnits = (topTemplateSampNum + (chromaUnitWidth - 1)) / chromaUnitWidth;
      const int  totalLeftUnits = (leftTemplateSampNum + (chromaUnitHeight - 1)) / chromaUnitHeight;
      const int  totalUnits = totalLeftUnits + totalAboveUnits + 1;
      const int  aboveRightUnits = totalAboveUnits - iAboveUnits;
      const int  leftBelowUnits = totalLeftUnits - iLeftUnits;
    
      int avaiAboveRightUnits = 0;
      int avaiLeftBelowUnits = 0;
    #endif
    
      bool  bNeighborFlags[4 * MAX_NUM_PART_IDXS_IN_CTU_WIDTH + 1];
    
    #if JVET_L0338_MDLM
      memset(bNeighborFlags, 0, totalUnits);
    #else
    
      memset( bNeighborFlags, 0, 1 + iLeftUnits + iAboveUnits );
    
    #endif
    
      int availlableUnit = isLeftAvailable( isChroma( pu.chType ) ? cu : lumaCU, toChannelType( area.compID ), area.pos(), iLeftUnits, iUnitHeight,
    #if JVET_L0338_MDLM
      ( bNeighborFlags + iLeftUnits + leftBelowUnits - 1 ) );
    #else
      ( bNeighborFlags + iLeftUnits - 1 ) );
    #endif
    
    
      if( lumaCU.cs->pcv->rectCUs )
      {
        bLeftAvaillable = availlableUnit == iTUHeightInUnits;
      }
      else
      {
        bLeftAvaillable = availlableUnit == iTUWidthInUnits;
      }
    
    
      availlableUnit = isAboveAvailable( isChroma( pu.chType ) ? cu : lumaCU, toChannelType( area.compID ), area.pos(), iAboveUnits, iUnitWidth,
    #if JVET_L0338_MDLM
      ( bNeighborFlags + iLeftUnits + leftBelowUnits + 1 ) );
    #else
      ( bNeighborFlags + iLeftUnits + 1 ) );
    #endif
    
    
      if( lumaCU.cs->pcv->rectCUs )
      {
        bAboveAvaillable = availlableUnit == iTUWidthInUnits;
      }
      else
      {
        bAboveAvaillable = availlableUnit == iTUHeightInUnits;
      }
    
    #if JVET_L0338_MDLM
      if (bLeftAvaillable) // if left is not available, then the below left is not available
      {
        avaiLeftBelowUnits = isBelowLeftAvailable(isChroma(pu.chType) ? cu : lumaCU, toChannelType(area.compID), area.bottomLeftComp(area.compID), leftBelowUnits, iUnitHeight, (bNeighborFlags + leftBelowUnits - 1));
      }
    
      if (bAboveAvaillable) // if above is not available, then  the above right is not available.
      {
        avaiAboveRightUnits = isAboveRightAvailable(isChroma(pu.chType) ? cu : lumaCU, toChannelType(area.compID), area.topRightComp(area.compID), aboveRightUnits, iUnitWidth, (bNeighborFlags + iLeftUnits + leftBelowUnits + iAboveUnits + 1));
      }
    #endif
    
    
      Pel*       pDst  = nullptr;
      Pel const* piSrc = nullptr;
    
      if( bAboveAvaillable )
      {
        pDst  = pDst0    - iDstStride;
        piSrc = pRecSrc0 - iRecStride2;
    
    #if JVET_L0338_MDLM
        int addedAboveRight = 0;
        if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
        {
          addedAboveRight = avaiAboveRightUnits*chromaUnitWidth;
        }
        for (int i = 0; i < uiCWidth + addedAboveRight; i++)
    #else
    
    #endif
    
        {
          if( i == 0 && !bLeftAvaillable )
          {
            pDst[i] = ( piSrc[2 * i] + piSrc[2 * i + iRecStride] + 1 ) >> 1;
          }
          else
          {
            pDst[i] = ( ( ( piSrc[2 * i             ] * 2 ) + piSrc[2 * i - 1             ] + piSrc[2 * i + 1             ] )
                      + ( ( piSrc[2 * i + iRecStride] * 2 ) + piSrc[2 * i - 1 + iRecStride] + piSrc[2 * i + 1 + iRecStride] )
                      + 4 ) >> 3;
          }
        }
      }
    
      if( bLeftAvaillable )
      {
        pDst  = pDst0    - 1;
        piSrc = pRecSrc0 - 3;
    
    #if JVET_L0338_MDLM
        int addedLeftBelow = 0;
        if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
        {
          addedLeftBelow = avaiLeftBelowUnits*chromaUnitHeight;
        }
        for (int j = 0; j < uiCHeight + addedLeftBelow; j++)
    #else
    
    #endif
    
        {
          pDst[0] = ( ( piSrc[1             ] * 2 + piSrc[0         ] + piSrc[2             ] )
                    + ( piSrc[1 + iRecStride] * 2 + piSrc[iRecStride] + piSrc[2 + iRecStride] )
                    + 4 ) >> 3;
    
          piSrc += iRecStride2;
          pDst  += iDstStride;
        }
      }
    
    
      // inner part from reconstructed picture buffer
      for( int j = 0; j < uiCHeight; j++ )
      {
        for( int i = 0; i < uiCWidth; i++ )
        {
          if( i == 0 && !bLeftAvaillable )
          {
            pDst0[i] = ( pRecSrc0[2 * i] + pRecSrc0[2 * i + iRecStride] + 1 ) >> 1;
          }
          else
          {
            pDst0[i] = ( pRecSrc0[2 * i             ] * 2 + pRecSrc0[2 * i + 1             ] + pRecSrc0[2 * i - 1             ]
                       + pRecSrc0[2 * i + iRecStride] * 2 + pRecSrc0[2 * i + 1 + iRecStride] + pRecSrc0[2 * i - 1 + iRecStride]
                       + 4 ) >> 3;
          }
        }
    
        pDst0    += iDstStride;
        pRecSrc0 += iRecStride2;
      }
    }
    
    #if JVET_L0338_MDLM && !JVET_L0191_LM_WO_LMS
    void IntraPrediction::xPadMdlmTemplateSample(Pel*pSrc, Pel*pCur, int cWidth, int cHeight, int existSampNum, int targetSampNum)
    {
      int sampNumToBeAdd = targetSampNum - existSampNum;
      Pel*pTempSrc = pSrc + existSampNum;
      Pel*pTempCur = pCur + existSampNum;
      for (int i = 0; i < sampNumToBeAdd; i++)
      {
        pTempSrc[i] = pSrc[existSampNum - 1];
        pTempCur[i] = pCur[existSampNum - 1];
      }
    }
    #endif
    
    #if JVET_L0191_LM_WO_LMS
    void IntraPrediction::xGetLMParameters(const PredictionUnit &pu, const ComponentID compID,
                                                  const CompArea &chromaArea,
                                                  int &a, int &b, int &iShift)
    {
      CHECK(compID == COMPONENT_Y, "");
    
      const SizeType cWidth  = chromaArea.width;
      const SizeType cHeight = chromaArea.height;
    
      const Position posLT = chromaArea;
    
      CodingStructure & cs = *(pu.cs);
      const CodingUnit &cu = *(pu.cu);
    
      const SPS &        sps           = *cs.sps;
      const uint32_t     tuWidth     = chromaArea.width;
      const uint32_t     tuHeight    = chromaArea.height;
      const ChromaFormat nChromaFormat = sps.getChromaFormatIdc();
    
      const int baseUnitSize = 1 << MIN_CU_LOG2;
      const int unitWidth    = baseUnitSize >> getComponentScaleX(chromaArea.compID, nChromaFormat);
      const int unitHeight   = baseUnitSize >> getComponentScaleX(chromaArea.compID, nChromaFormat);
    
      const int tuWidthInUnits  = tuWidth / unitWidth;
      const int tuHeightInUnits = tuHeight / unitHeight;
      const int aboveUnits      = tuWidthInUnits;
      const int leftUnits       = tuHeightInUnits;
    
    #if JVET_L0338_MDLM
      int topTemplateSampNum = 2 * cWidth; // for MDLM, the template sample number is 2W or 2H;
      int leftTemplateSampNum = 2 * cHeight;
      assert(m_topRefLength >= topTemplateSampNum);
      assert(m_leftRefLength >= leftTemplateSampNum);
      int totalAboveUnits = (topTemplateSampNum + (unitWidth - 1)) / unitWidth;
      int totalLeftUnits = (leftTemplateSampNum + (unitHeight - 1)) / unitHeight;
      int totalUnits = totalLeftUnits + totalAboveUnits + 1;
      int aboveRightUnits = totalAboveUnits - aboveUnits;
      int leftBelowUnits = totalLeftUnits - leftUnits;
      int avaiAboveRightUnits = 0;
      int avaiLeftBelowUnits = 0;
      int avaiAboveUnits = 0;
      int avaiLeftUnits = 0;
    
      int curChromaMode = pu.intraDir[1];
    #endif
    
      bool neighborFlags[4 * MAX_NUM_PART_IDXS_IN_CTU_WIDTH + 1];
    
    #if JVET_L0338_MDLM
      memset(neighborFlags, 0, totalUnits);
    #else
    
      memset(neighborFlags, 0, 1 + leftUnits + aboveUnits);
    
    #endif
    
    
      bool aboveAvailable, leftAvailable;
    
      int availableUnit =
    
        isAboveAvailable(cu, CHANNEL_TYPE_CHROMA, posLT, aboveUnits, unitWidth, 
    #if JVET_L0338_MDLM
        (neighborFlags + leftUnits + leftBelowUnits + 1));
    #else
        (neighborFlags + leftUnits + 1));
    #endif
    
      aboveAvailable = availableUnit == tuWidthInUnits;
    
      availableUnit =
    
        isLeftAvailable(cu, CHANNEL_TYPE_CHROMA, posLT, leftUnits, unitHeight, 
    #if JVET_L0338_MDLM
        (neighborFlags + leftUnits + leftBelowUnits - 1));
    #else
        (neighborFlags + leftUnits - 1));
    #endif
    
      leftAvailable = availableUnit == tuHeightInUnits;
    
    #if JVET_L0338_MDLM
      if (leftAvailable) // if left is not available, then the below left is not available
      {
        avaiLeftUnits = tuHeightInUnits;
        avaiLeftBelowUnits = isBelowLeftAvailable(cu, CHANNEL_TYPE_CHROMA, chromaArea.bottomLeftComp(chromaArea.compID), leftBelowUnits, unitHeight, (neighborFlags + leftBelowUnits - 1));
      }
      if (aboveAvailable) // if above is not available, then  the above right is not available.
      {
        avaiAboveUnits = tuWidthInUnits;
        avaiAboveRightUnits = isAboveRightAvailable(cu, CHANNEL_TYPE_CHROMA, chromaArea.topRightComp(chromaArea.compID), aboveRightUnits, unitWidth, (neighborFlags + leftUnits + leftBelowUnits + aboveUnits + 1));
      }
    #endif
    
      Pel *srcColor0, *curChroma0;
      int  srcStride, curStride;
    
      PelBuf temp;
    
    #if JVET_L0338_MDLM
      if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
      {
        srcStride = 2 * MAX_CU_SIZE + 1;
        temp = PelBuf(m_pMdlmTemp + srcStride + 1, srcStride, Size(chromaArea));
      }
      else
      {
    #endif
    
      srcStride = MAX_CU_SIZE + 1;
      temp        = PelBuf(m_piTemp + srcStride + 1, srcStride, Size(chromaArea));
    
    #if JVET_L0338_MDLM
      }
    #endif
    
      srcColor0 = temp.bufAt(0, 0);
      curChroma0 = getPredictorPtr(compID);
    
      curStride = m_topRefLength + 1;
    
      curChroma0 += curStride + 1;
    
      unsigned internalBitDepth = sps.getBitDepth(CHANNEL_TYPE_CHROMA);
    
      int minLuma[2] = { MAX_INT, 0 };
      int maxLuma[2] = { -MAX_INT, 0 };
    
      Pel *src = srcColor0 - srcStride;
      Pel *cur = curChroma0 - curStride;
    
    #if JVET_L0338_MDLM
      int minDim = 1;
      int actualTopTemplateSampNum = 0;
      int actualLeftTemplateSampNum = 0;
      if (curChromaMode == MDLM_T_IDX)
      {
        leftAvailable = 0;
        actualTopTemplateSampNum = unitWidth*(avaiAboveUnits + avaiAboveRightUnits);
        minDim = actualTopTemplateSampNum;
      }
      else if (curChromaMode == MDLM_L_IDX)
      {
        aboveAvailable = 0;
        actualLeftTemplateSampNum = unitHeight*(avaiLeftUnits + avaiLeftBelowUnits);
        minDim = actualLeftTemplateSampNum;
      }
      else if (curChromaMode == LM_CHROMA_IDX)
      {
        actualTopTemplateSampNum = cWidth;
        actualLeftTemplateSampNum = cHeight;
        minDim = leftAvailable && aboveAvailable ? 1 << g_aucPrevLog2[std::min(actualLeftTemplateSampNum, actualTopTemplateSampNum)]
          : 1 << g_aucPrevLog2[leftAvailable ? actualLeftTemplateSampNum : actualTopTemplateSampNum];
      }
    #endif
    #if !JVET_L0338_MDLM
    
      int minDim = leftAvailable && aboveAvailable ? 1 << g_aucPrevLog2[std::min(cHeight, cWidth)]
                                                       : 1 << g_aucPrevLog2[leftAvailable ? cHeight : cWidth];
    
    #endif
    
      int numSteps = minDim;
    
      if (aboveAvailable)
      {
        for (int j = 0; j < numSteps; j++)
        {
    
    #if JVET_L0338_MDLM
          int idx = (j * actualTopTemplateSampNum) / minDim;
    #else
    
          int idx = (j * cWidth) / minDim;
    
    #endif
    
    
          if (minLuma[0] > src[idx])
          {
            minLuma[0] = src[idx];
            minLuma[1] = cur[idx];
          }
          if (maxLuma[0] < src[idx])
          {
            maxLuma[0] = src[idx];
            maxLuma[1] = cur[idx];
          }
        }
      }
    
      if (leftAvailable)
      {
        src = srcColor0 - 1;
        cur = curChroma0 - 1;
    
        for (int i = 0; i < numSteps; i++)
        {
    
    #if JVET_L0338_MDLM
          int idx = (i * actualLeftTemplateSampNum) / minDim;
    #else
    
          int idx = (i * cHeight) / minDim;
    
    #endif
    
          if (minLuma[0] > src[srcStride * idx])
          {
            minLuma[0] = src[srcStride * idx];
            minLuma[1] = cur[curStride * idx];
          }
          if (maxLuma[0] < src[srcStride * idx])
          {
            maxLuma[0] = src[srcStride * idx];
            maxLuma[1] = cur[curStride * idx];
          }
        }
      }
    
      if ((leftAvailable || aboveAvailable))
      {
        a         = 0;
        iShift    = 16;
        int shift = (internalBitDepth > 8) ? internalBitDepth - 9 : 0;
        int add   = shift ? 1 << (shift - 1) : 0;
        int diff  = (maxLuma[0] - minLuma[0] + add) >> shift;
        if (diff > 0)
        {
          int div = ((maxLuma[1] - minLuma[1]) * g_aiLMDivTableLow[diff - 1] + 32768) >> 16;
          a       = (((maxLuma[1] - minLuma[1]) * g_aiLMDivTableHigh[diff - 1] + div + add) >> shift);
        }
        b = minLuma[1] - ((a * minLuma[0]) >> iShift);
      }
      else
      {
        a = 0;
    
        b = 1 << (internalBitDepth - 1);
    
        iShift = 0;
      }
    }
    #else
    
    static int GetFloorLog2( unsigned x )
    {
      int bits = -1;
      while( x > 0 )
      {
        bits++;
        x >>= 1;
      }
      return bits;
    }
    
    
    void IntraPrediction::xGetLMParameters(const PredictionUnit &pu, const ComponentID compID, const CompArea& chromaArea,
      int& a, int&  b, int& iShift)
    {
      CHECK( compID == COMPONENT_Y, "" );
    
      const SizeType uiCWidth  = chromaArea.width;
      const SizeType uiCHeight = chromaArea.height;
    
      const Position posLT = chromaArea;
    
      CodingStructure&  cs = *(pu.cs);
      const CodingUnit& cu = *(pu.cu);
    
      const SPS &sps        = *cs.sps;
      const uint32_t uiTuWidth  = chromaArea.width;
      const uint32_t uiTuHeight = chromaArea.height;
      const ChromaFormat nChromaFormat = sps.getChromaFormatIdc();
    
      const int iBaseUnitSize = 1 << MIN_CU_LOG2;
      const int  iUnitWidth   = iBaseUnitSize >> getComponentScaleX( chromaArea.compID, nChromaFormat );
      const int  iUnitHeight  = iBaseUnitSize >> getComponentScaleX( chromaArea.compID, nChromaFormat );
    
    
      const int  iTUWidthInUnits  = uiTuWidth / iUnitWidth;
      const int  iTUHeightInUnits = uiTuHeight / iUnitHeight;
      const int  iAboveUnits      = iTUWidthInUnits;
      const int  iLeftUnits       = iTUHeightInUnits;
    
    #if JVET_L0338_MDLM
      int topTemplateSampNum = 2 * uiCWidth; // for MDLM, the template sample number is 2W or 2H;
      int leftTemplateSampNum = 2 * uiCHeight;
      assert(m_topRefLength >= topTemplateSampNum);
      assert(m_leftRefLength >= leftTemplateSampNum);
      int totalAboveUnits = (topTemplateSampNum + (iUnitWidth - 1)) / iUnitWidth;
      int totalLeftUnits = (leftTemplateSampNum + (iUnitHeight - 1)) / iUnitHeight;
      int totalUnits = totalLeftUnits + totalAboveUnits + 1;
      int aboveRightUnits = totalAboveUnits - iAboveUnits;
      int leftBelowUnits = totalLeftUnits - iLeftUnits;
      int avaiAboveRightUnits = 0;
      int avaiLeftBelowUnits = 0;
      int avaiAboveUnits = 0;
      int avaiLeftUnits = 0;
    
      int curChromaMode = pu.intraDir[1];
    #endif
    
      bool  bNeighborFlags[4 * MAX_NUM_PART_IDXS_IN_CTU_WIDTH + 1];
    
    #if JVET_L0338_MDLM
      memset(bNeighborFlags, 0, totalUnits);
    #else
    
      memset( bNeighborFlags, 0, 1 + iLeftUnits + iAboveUnits );
    
    #endif
    
      int availlableUnit = isAboveAvailable( cu, CHANNEL_TYPE_CHROMA, posLT, iAboveUnits, iUnitWidth, 
    #if JVET_L0338_MDLM
        (bNeighborFlags + iLeftUnits + leftBelowUnits + 1 ) );
    #else
        ( bNeighborFlags + iLeftUnits + 1 ) );
    #endif
    
      bAboveAvaillable = availlableUnit == iTUWidthInUnits;
    
    
      availlableUnit = isLeftAvailable( cu, CHANNEL_TYPE_CHROMA, posLT, iLeftUnits, iUnitHeight, 
    #if JVET_L0338_MDLM
        (bNeighborFlags + iLeftUnits + leftBelowUnits - 1 ) );
    #else
        ( bNeighborFlags + iLeftUnits - 1 ) );
    #endif
    
      bLeftAvaillable = availlableUnit == iTUHeightInUnits;
    
    #if JVET_L0338_MDLM
      if (bLeftAvaillable) // if left is not available, then the below left is not available
      {
        avaiLeftUnits = iTUHeightInUnits;
        avaiLeftBelowUnits = isBelowLeftAvailable(cu, CHANNEL_TYPE_CHROMA, chromaArea.bottomLeftComp(chromaArea.compID), leftBelowUnits, iUnitHeight, (bNeighborFlags + leftBelowUnits - 1));
      }
      if (bAboveAvaillable) // if above is not available, then  the above right is not available.
      {
        avaiAboveUnits = iTUWidthInUnits;
        avaiAboveRightUnits = isAboveRightAvailable(cu, CHANNEL_TYPE_CHROMA, chromaArea.topRightComp(chromaArea.compID), aboveRightUnits, iUnitWidth, (bNeighborFlags + iLeftUnits + leftBelowUnits + iAboveUnits + 1));
      }
    #endif
    
      Pel *pSrcColor0, *pCurChroma0;
      int  iSrcStride,  iCurStride;
    
      PelBuf Temp;  
    
    #if JVET_L0338_MDLM
      if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
      {
        iSrcStride = 2 * MAX_CU_SIZE + 1;
        Temp = PelBuf(m_pMdlmTemp + iSrcStride + 1, iSrcStride, Size(chromaArea));
      }
      else
      {
    #endif
    
      iSrcStride = MAX_CU_SIZE + 1;
      Temp = PelBuf(m_piTemp + iSrcStride + 1, iSrcStride, Size(chromaArea));
    
    #if JVET_L0338_MDLM
      }
    #endif
    
      pSrcColor0 = Temp.bufAt(0, 0);
      pCurChroma0 = getPredictorPtr(compID);
      iCurStride = m_topRefLength + 1;
      pCurChroma0 += iCurStride + 1;
      int x = 0, y = 0, xx = 0, xy = 0;
      int iCountShift = 0;
      unsigned uiInternalBitDepth = sps.getBitDepth( CHANNEL_TYPE_CHROMA );
    
      Pel *pSrc = pSrcColor0  - iSrcStride;
      Pel *pCur = pCurChroma0 - iCurStride;
    
    #if JVET_L0338_MDLM
      //get the temp buffer to store the downsampled luma and chroma
      Pel* pTempBufferSrc = new Pel[2 * MAX_CU_SIZE]; // for MDLM, use tempalte size 2W or 2H,
      Pel* pTempBufferCur = new Pel[2 * MAX_CU_SIZE];
    
      int actualTopTemplateSampNum = iUnitWidth*(avaiAboveUnits + avaiAboveRightUnits);
      int actualLeftTemplateSampNum = iUnitHeight*(avaiLeftUnits + avaiLeftBelowUnits);
    
      if ((curChromaMode == MDLM_L_IDX) || (curChromaMode == MDLM_T_IDX))
      {
        if (curChromaMode == MDLM_T_IDX)
        {
          if (bAboveAvaillable)
          {
            for (int j = 0; j < actualTopTemplateSampNum; j++)
            {
              pTempBufferSrc[j] = pSrc[j];