From 12fa2c6c4b8db330326714aabfa8a57fde18e207 Mon Sep 17 00:00:00 2001
From: Chao-Hsiung Hung <chaohsiu@qti.qualcomm.com>
Date: Tue, 26 Nov 2019 23:04:46 +0100
Subject: [PATCH] remove the macro "PALETTE_BUFFER_REDUCTION" use
 MAX_NUM_CHANNEL_TYPE instead of MAX_NUM_COMPONENT - 1

---
 source/Lib/CommonLib/CodingStructure.cpp | 86 +++++++++++++++---------
 source/Lib/CommonLib/CodingStructure.h   | 18 +++--
 source/Lib/CommonLib/IntraPrediction.cpp |  4 +-
 source/Lib/CommonLib/Picture.cpp         |  2 +-
 source/Lib/CommonLib/Unit.cpp            | 61 +++++++++++++----
 source/Lib/CommonLib/Unit.h              | 18 ++---
 source/Lib/DecoderLib/DecLib.cpp         |  2 +-
 source/Lib/EncoderLib/EncCu.cpp          | 14 ++--
 source/Lib/EncoderLib/EncGOP.cpp         |  2 +-
 source/Lib/EncoderLib/EncModeCtrl.cpp    | 25 ++++---
 source/Lib/EncoderLib/IntraSearch.cpp    | 63 ++++++++---------
 11 files changed, 187 insertions(+), 108 deletions(-)

diff --git a/source/Lib/CommonLib/CodingStructure.cpp b/source/Lib/CommonLib/CodingStructure.cpp
index 1928eb937..7ce256813 100644
--- a/source/Lib/CommonLib/CodingStructure.cpp
+++ b/source/Lib/CommonLib/CodingStructure.cpp
@@ -78,11 +78,15 @@ CodingStructure::CodingStructure(CUCache& cuCache, PUCache& puCache, TUCache& tu
   {
     m_coeffs[ i ] = nullptr;
     m_pcmbuf[ i ] = nullptr;
-    m_runType[i]   = nullptr;
+    m_offsets[ i ] = 0;
+  }
+
+  for (uint32_t i = 0; i < MAX_NUM_CHANNEL_TYPE; i++)
+  {
+    m_runType[i] = nullptr;
 #if !JVET_P0077_LINE_CG_PALETTE
     m_runLength[i] = nullptr;
 #endif
-    m_offsets[ i ] = 0;
   }
 
   for( uint32_t i = 0; i < MAX_NUM_CHANNEL_TYPE; i++ )
@@ -655,9 +659,9 @@ TransformUnit& CodingStructure::addTU( const UnitArea &unit, const ChannelType c
 
   uint32_t numCh = ::getNumberValidComponents( area.chromaFormat );
 
-  for( uint32_t i = 0; i < numCh; i++ )
+  for (uint32_t i = 0; i < numCh; i++)
   {
-    if( !tu->blocks[i].valid() )
+    if (!tu->blocks[i].valid())
     {
       continue;
     }
@@ -665,33 +669,38 @@ TransformUnit& CodingStructure::addTU( const UnitArea &unit, const ChannelType c
     if (i < ::getNumberValidChannels(area.chromaFormat))
     {
       const CompArea &_selfBlk = area.blocks[i];
-      const CompArea     &_blk = tu-> blocks[i];
+      const CompArea     &_blk = tu->blocks[i];
 
-      bool isIspTu = tu->cu != nullptr && tu->cu->ispMode && isLuma( _blk.compID );
+      bool isIspTu = tu->cu != nullptr && tu->cu->ispMode && isLuma(_blk.compID);
 
       bool isFirstIspTu = false;
-      if( isIspTu )
+      if (isIspTu)
       {
-        isFirstIspTu = CU::isISPFirst( *tu->cu, _blk, getFirstComponentOfChannel( ChannelType( i ) ) );
+        isFirstIspTu = CU::isISPFirst(*tu->cu, _blk, getFirstComponentOfChannel(ChannelType(i)));
       }
-      if( !isIspTu || isFirstIspTu )
+      if (!isIspTu || isFirstIspTu)
       {
         const UnitScale& scale = unitScale[_blk.compID];
 
-        const Area scaledSelf  = scale.scale( _selfBlk );
-        const Area scaledBlk   = isIspTu ? scale.scale( tu->cu->blocks[i] ) : scale.scale( _blk );
-        unsigned *idxPtr       = m_tuIdx[i] + rsAddr( scaledBlk.pos(), scaledSelf.pos(), scaledSelf.width );
-        CHECK( *idxPtr, "Overwriting a pre-existing value, should be '0'!" );
-        AreaBuf<uint32_t>( idxPtr, scaledSelf.width, scaledBlk.size() ).fill( idx );
+        const Area scaledSelf = scale.scale(_selfBlk);
+        const Area scaledBlk = isIspTu ? scale.scale(tu->cu->blocks[i]) : scale.scale(_blk);
+        unsigned *idxPtr = m_tuIdx[i] + rsAddr(scaledBlk.pos(), scaledSelf.pos(), scaledSelf.width);
+        CHECK(*idxPtr, "Overwriting a pre-existing value, should be '0'!");
+        AreaBuf<uint32_t>(idxPtr, scaledSelf.width, scaledBlk.size()).fill(idx);
       }
     }
 
     coeffs[i] = m_coeffs[i] + m_offsets[i];
     pcmbuf[i] = m_pcmbuf[i] + m_offsets[i];
-    runType[i]   = m_runType[i]   + m_offsets[i];
+
+    if (i < MAX_NUM_CHANNEL_TYPE)
+    {
+      if (m_runType[i] != nullptr) runType[i] = m_runType[i] + m_offsets[i];
 #if !JVET_P0077_LINE_CG_PALETTE
-    runLength[i] = m_runLength[i] + m_offsets[i];
+      if (m_runLength[i] != nullptr) runLength[i] = m_runLength[i] + m_offsets[i];
 #endif
+    }
+
     unsigned areaSize = tu->blocks[i].area();
     m_offsets[i] += areaSize;
   }
@@ -811,9 +820,9 @@ void CodingStructure::allocateVectorsAtPicLevel()
 
 
 
-void CodingStructure::create(const ChromaFormat &_chromaFormat, const Area& _area, const bool isTopLayer)
+void CodingStructure::create(const ChromaFormat &_chromaFormat, const Area& _area, const bool isTopLayer, const bool isPLTused)
 {
-  createInternals( UnitArea( _chromaFormat, _area ), isTopLayer );
+  createInternals(UnitArea(_chromaFormat, _area), isTopLayer, isPLTused);
 
   if( isTopLayer ) return;
 
@@ -823,9 +832,9 @@ void CodingStructure::create(const ChromaFormat &_chromaFormat, const Area& _are
   m_orgr.create( area );
 }
 
-void CodingStructure::create(const UnitArea& _unit, const bool isTopLayer)
+void CodingStructure::create(const UnitArea& _unit, const bool isTopLayer, const bool isPLTused)
 {
-  createInternals( _unit, isTopLayer );
+  createInternals(_unit, isTopLayer, isPLTused);
 
   if( isTopLayer ) return;
 
@@ -835,7 +844,7 @@ void CodingStructure::create(const UnitArea& _unit, const bool isTopLayer)
   m_orgr.create( area );
 }
 
-void CodingStructure::createInternals( const UnitArea& _unit, const bool isTopLayer )
+void CodingStructure::createInternals(const UnitArea& _unit, const bool isTopLayer, const bool isPLTused)
 {
   area = _unit;
 
@@ -863,7 +872,7 @@ void CodingStructure::createInternals( const UnitArea& _unit, const bool isTopLa
     m_offsets[i] = 0;
   }
 
-  if( !isTopLayer ) createCoeffs();
+  if( !isTopLayer ) createCoeffs(isPLTused);
 
   unsigned _lumaAreaScaled = g_miScaling.scale( area.lumaSize() ).area();
   m_motionBuf       = new MotionInfo[_lumaAreaScaled];
@@ -897,18 +906,22 @@ void CodingStructure::addMiToLut(static_vector<MotionInfo, MAX_NUM_HMVP_CANDS> &
 
 void CodingStructure::resetPrevPLT(PLTBuf& prevPLT)
 {
-  for (int comp = 0; comp < MAX_NUM_COMPONENT; comp++)
+  for (int comp = 0; comp < MAX_NUM_CHANNEL_TYPE; comp++)
   {
     prevPLT.curPLTSize[comp] = 0;
+  }
+
+  for (int comp = 0; comp < MAX_NUM_COMPONENT; comp++)
+  {
     memset(prevPLT.curPLT[comp], 0, MAXPLTPREDSIZE * sizeof(Pel));
   }
 }
 
-void CodingStructure::reorderPrevPLT(PLTBuf& prevPLT, uint32_t curPLTSize[MAX_NUM_COMPONENT], Pel curPLT[MAX_NUM_COMPONENT][MAXPLTSIZE], bool reuseflag[MAX_NUM_COMPONENT][MAXPLTPREDSIZE], uint32_t compBegin, uint32_t numComp, bool jointPLT)
+void CodingStructure::reorderPrevPLT(PLTBuf& prevPLT, uint8_t curPLTSize[MAX_NUM_CHANNEL_TYPE], Pel curPLT[MAX_NUM_COMPONENT][MAXPLTSIZE], bool reuseflag[MAX_NUM_CHANNEL_TYPE][MAXPLTPREDSIZE], uint32_t compBegin, uint32_t numComp, bool jointPLT)
 {
   Pel stuffedPLT[MAX_NUM_COMPONENT][MAXPLTPREDSIZE];
-  uint32_t tempCurPLTsize[MAX_NUM_COMPONENT];
-  uint32_t stuffPLTsize[MAX_NUM_COMPONENT];
+  uint8_t tempCurPLTsize[MAX_NUM_CHANNEL_TYPE];
+  uint8_t stuffPLTsize[MAX_NUM_COMPONENT];
 
   for (int i = compBegin; i < (compBegin + numComp); i++)
   {
@@ -968,7 +981,7 @@ void CodingStructure::rebindPicBufs()
   }
 }
 
-void CodingStructure::createCoeffs()
+void CodingStructure::createCoeffs(const bool isPLTused)
 {
   const unsigned numCh = getNumberValidComponents( area.chromaFormat );
 
@@ -978,10 +991,19 @@ void CodingStructure::createCoeffs()
 
     m_coeffs[i] = _area > 0 ? ( TCoeff* ) xMalloc( TCoeff, _area ) : nullptr;
     m_pcmbuf[i] = _area > 0 ? ( Pel*    ) xMalloc( Pel,    _area ) : nullptr;
-    m_runType[i]   = _area > 0 ? ( bool*  ) xMalloc( bool, _area ) : nullptr;
+  }
+
+  if (isPLTused)
+  {
+    for (unsigned i = 0; i < numCh - 1; i++)
+    {
+      unsigned _area = area.blocks[i].area();
+
+      m_runType[i] = _area > 0 ? (bool*)xMalloc(bool, _area) : nullptr;
 #if !JVET_P0077_LINE_CG_PALETTE
-    m_runLength[i] = _area > 0 ? ( Pel*   ) xMalloc( Pel,  _area ) : nullptr;
+      m_runLength[i] = _area > 0 ? (Pel*)xMalloc(Pel, _area) : nullptr;
 #endif
+    }
   }
 }
 
@@ -991,7 +1013,11 @@ void CodingStructure::destroyCoeffs()
   {
     if( m_coeffs[i] ) { xFree( m_coeffs[i] ); m_coeffs[i] = nullptr; }
     if( m_pcmbuf[i] ) { xFree( m_pcmbuf[i] ); m_pcmbuf[i] = nullptr; }
-    if (m_runType[i])   { xFree(m_runType[i]);   m_runType[i]   = nullptr; }
+  }
+
+  for (uint32_t i = 0; i < MAX_NUM_CHANNEL_TYPE; i++)
+  {
+    if (m_runType[i]) { xFree(m_runType[i]);   m_runType[i] = nullptr; }
 #if !JVET_P0077_LINE_CG_PALETTE
     if (m_runLength[i]) { xFree(m_runLength[i]); m_runLength[i] = nullptr; }
 #endif
diff --git a/source/Lib/CommonLib/CodingStructure.h b/source/Lib/CommonLib/CodingStructure.h
index 8bf19a426..974c4a759 100644
--- a/source/Lib/CommonLib/CodingStructure.h
+++ b/source/Lib/CommonLib/CodingStructure.h
@@ -102,13 +102,16 @@ public:
   const PreCalcValues* pcv;
 
   CodingStructure(CUCache&, PUCache&, TUCache&);
-  void create( const UnitArea &_unit, const bool isTopLayer );
-  void create( const ChromaFormat &_chromaFormat, const Area& _area, const bool isTopLayer );
+
+  void create(const UnitArea &_unit, const bool isTopLayer, const bool isPLTused);
+  void create(const ChromaFormat &_chromaFormat, const Area& _area, const bool isTopLayer, const bool isPLTused);
+
   void destroy();
   void releaseIntermediateData();
 
   void rebindPicBufs();
-  void createCoeffs();
+
+  void createCoeffs(const bool isPLTused);
   void destroyCoeffs();
 
   void allocateVectorsAtPicLevel();
@@ -191,7 +194,7 @@ public:
 
 
 private:
-  void createInternals(const UnitArea& _unit, const bool isTopLayer);
+  void createInternals(const UnitArea& _unit, const bool isTopLayer, const bool isPLTused);
 
 public:
 
@@ -205,7 +208,8 @@ public:
 
   PLTBuf prevPLT;
   void resetPrevPLT(PLTBuf& prevPLT);
-  void reorderPrevPLT(PLTBuf& prevPLT, uint32_t curPLTSize[MAX_NUM_COMPONENT], Pel curPLT[MAX_NUM_COMPONENT][MAXPLTSIZE], bool reuseflag[MAX_NUM_COMPONENT][MAXPLTPREDSIZE], uint32_t compBegin, uint32_t numComp, bool jointPLT);
+  void reorderPrevPLT(PLTBuf& prevPLT, uint8_t curPLTSize[MAX_NUM_CHANNEL_TYPE], Pel curPLT[MAX_NUM_COMPONENT][MAXPLTSIZE], bool reuseflag[MAX_NUM_CHANNEL_TYPE][MAXPLTPREDSIZE], uint32_t compBegin, uint32_t numComp, bool jointPLT);
+
 private:
 
   // needed for TU encoding
@@ -233,9 +237,9 @@ private:
 
   TCoeff *m_coeffs [ MAX_NUM_COMPONENT ];
   Pel    *m_pcmbuf [ MAX_NUM_COMPONENT ];
-  bool   *m_runType  [MAX_NUM_COMPONENT];
+  bool   *m_runType[ MAX_NUM_CHANNEL_TYPE ];
 #if !JVET_P0077_LINE_CG_PALETTE
-  Pel    *m_runLength[MAX_NUM_COMPONENT];
+  Pel    *m_runLength[MAX_NUM_CHANNEL_TYPE];
 #endif
   int     m_offsets[ MAX_NUM_COMPONENT ];
 
diff --git a/source/Lib/CommonLib/IntraPrediction.cpp b/source/Lib/CommonLib/IntraPrediction.cpp
index ad24b54f1..28284946b 100644
--- a/source/Lib/CommonLib/IntraPrediction.cpp
+++ b/source/Lib/CommonLib/IntraPrediction.cpp
@@ -2047,8 +2047,8 @@ void IntraPrediction::reorderPLT(CodingStructure& cs, Partitioner& partitioner,
 {
   CodingUnit &cu = *cs.getCU(partitioner.chType);
 
-  uint32_t       reusePLTSizetmp = 0;
-  uint32_t       pltSizetmp = 0;
+  uint8_t        reusePLTSizetmp = 0;
+  uint8_t        pltSizetmp = 0;
   Pel            curPLTtmp[MAX_NUM_COMPONENT][MAXPLTSIZE];
   bool           curPLTpred[MAXPLTPREDSIZE];
 
diff --git a/source/Lib/CommonLib/Picture.cpp b/source/Lib/CommonLib/Picture.cpp
index 8fbb337ca..0b65a4cb1 100644
--- a/source/Lib/CommonLib/Picture.cpp
+++ b/source/Lib/CommonLib/Picture.cpp
@@ -985,7 +985,7 @@ void Picture::finalInit( const SPS& sps, const PPS& pps, APS** alfApss, APS* lmc
   {
     cs = new CodingStructure( g_globalUnitCache.cuCache, g_globalUnitCache.puCache, g_globalUnitCache.tuCache );
     cs->sps = &sps;
-    cs->create( chromaFormatIDC, Area( 0, 0, iWidth, iHeight ), true );
+    cs->create(chromaFormatIDC, Area(0, 0, iWidth, iHeight), true, (bool)sps.getPLTMode());
   }
 
   cs->picture = this;
diff --git a/source/Lib/CommonLib/Unit.cpp b/source/Lib/CommonLib/Unit.cpp
index 6db4bbe4f..cd3eb03ed 100644
--- a/source/Lib/CommonLib/Unit.cpp
+++ b/source/Lib/CommonLib/Unit.cpp
@@ -294,16 +294,28 @@ CodingUnit& CodingUnit::operator=( const CodingUnit& other )
   smvdMode        = other.smvdMode;
   ispMode           = other.ispMode;
   mipFlag           = other.mipFlag;
-  for (int idx = 0; idx < MAX_NUM_COMPONENT; idx++)
+
+  for (int idx = 0; idx < MAX_NUM_CHANNEL_TYPE; idx++)
   {
     curPLTSize[idx]   = other.curPLTSize[idx];
     useEscape[idx]    = other.useEscape[idx];
     useRotation[idx]  = other.useRotation[idx];
     reusePLTSize[idx] = other.reusePLTSize[idx];
     lastPLTSize[idx]  = other.lastPLTSize[idx];
-    memcpy( curPLT[idx],    other.curPLT[idx],        MAXPLTSIZE * sizeof(Pel) );
-    memcpy( reuseflag[idx], other.reuseflag[idx], MAXPLTPREDSIZE * sizeof(bool));
+    if (slice->getSPS()->getPLTMode())
+    {
+      memcpy(reuseflag[idx], other.reuseflag[idx], MAXPLTPREDSIZE * sizeof(bool));
+    }
+  }
+
+  if (slice->getSPS()->getPLTMode())
+  {
+    for (int idx = 0; idx < MAX_NUM_COMPONENT; idx++)
+    {
+      memcpy(curPLT[idx], other.curPLT[idx], MAXPLTSIZE * sizeof(Pel));
+    }
   }
+
   treeType          = other.treeType;
   modeType          = other.modeType;
   modeTypeSeries    = other.modeTypeSeries;
@@ -351,16 +363,22 @@ void CodingUnit::initData()
   smvdMode        = 0;
   ispMode           = 0;
   mipFlag           = false;
-  for (int idx = 0; idx < MAX_NUM_COMPONENT; idx++)
+
+  for (int idx = 0; idx < MAX_NUM_CHANNEL_TYPE; idx++)
   {
     curPLTSize[idx]   = 0;
     reusePLTSize[idx] = 0;
     lastPLTSize[idx]  = 0;
     useEscape[idx]    = false;
     useRotation[idx]  = false;
-    memset(curPLT[idx],        0,     MAXPLTSIZE * sizeof(Pel) );
     memset(reuseflag[idx], false, MAXPLTPREDSIZE * sizeof(bool));
   }
+
+  for (int idx = 0; idx < MAX_NUM_COMPONENT; idx++)
+  {
+    memset(curPLT[idx], 0, MAXPLTSIZE * sizeof(Pel));
+  }
+
   treeType          = TREE_D;
   modeType          = MODE_TYPE_ALL;
   modeTypeSeries    = 0;
@@ -728,7 +746,11 @@ TransformUnit::TransformUnit(const UnitArea& unit) : UnitArea(unit), cu(nullptr)
   {
     m_coeffs[i] = nullptr;
     m_pcmbuf[i] = nullptr;
-    m_runType[i]   = nullptr;
+  }
+
+  for (unsigned i = 0; i < MAX_NUM_TBLOCKS - 1; i++)
+  {
+    m_runType[i] = nullptr;
 #if !JVET_P0077_LINE_CG_PALETTE
     m_runLength[i] = nullptr;
 #endif
@@ -743,7 +765,11 @@ TransformUnit::TransformUnit(const ChromaFormat _chromaFormat, const Area &_area
   {
     m_coeffs[i] = nullptr;
     m_pcmbuf[i] = nullptr;
-    m_runType[i]   = nullptr;
+  }
+
+  for (unsigned i = 0; i < MAX_NUM_TBLOCKS - 1; i++)
+  {
+    m_runType[i] = nullptr;
 #if !JVET_P0077_LINE_CG_PALETTE
     m_runLength[i] = nullptr;
 #endif
@@ -783,7 +809,11 @@ void TransformUnit::init(TCoeff **coeffs, Pel **pcmbuf, Pel **runLength, bool **
   {
     m_coeffs[i] = coeffs[i];
     m_pcmbuf[i] = pcmbuf[i];
-    m_runType[i]   = runType[i];
+  }
+
+  for (uint32_t i = 0; i < numBlocks - 1; i++)
+  {
+    m_runType[i] = runType[i];
 #if !JVET_P0077_LINE_CG_PALETTE
     m_runLength[i] = runLength[i];
 #endif
@@ -803,10 +833,13 @@ TransformUnit& TransformUnit::operator=(const TransformUnit& other)
 
     if (m_coeffs[i] && other.m_coeffs[i] && m_coeffs[i] != other.m_coeffs[i]) memcpy(m_coeffs[i], other.m_coeffs[i], sizeof(TCoeff) * area);
     if (m_pcmbuf[i] && other.m_pcmbuf[i] && m_pcmbuf[i] != other.m_pcmbuf[i]) memcpy(m_pcmbuf[i], other.m_pcmbuf[i], sizeof(Pel   ) * area);
-    if (m_runType[i]   && other.m_runType[i]   && m_runType[i]   != other.m_runType[i]  ) memcpy(m_runType[i],   other.m_runType[i],   sizeof(bool) * area);
+    if (cu->slice->getSPS()->getPLTMode() && i < 2)
+    {
+      if (m_runType[i]   && other.m_runType[i]   && m_runType[i]   != other.m_runType[i]  ) memcpy(m_runType[i],   other.m_runType[i],   sizeof(bool) * area);
 #if !JVET_P0077_LINE_CG_PALETTE
-    if (m_runLength[i] && other.m_runLength[i] && m_runLength[i] != other.m_runLength[i]) memcpy(m_runLength[i], other.m_runLength[i], sizeof(Pel) * area );
+      if (m_runLength[i] && other.m_runLength[i] && m_runLength[i] != other.m_runLength[i]) memcpy(m_runLength[i], other.m_runLength[i], sizeof(Pel) * area );
 #endif
+    }
     cbf[i]           = other.cbf[i];
     rdpcm[i]         = other.rdpcm[i];
     compAlpha[i]     = other.compAlpha[i];
@@ -833,10 +866,14 @@ void TransformUnit::copyComponentFrom(const TransformUnit& other, const Componen
 
   if (m_coeffs[i] && other.m_coeffs[i] && m_coeffs[i] != other.m_coeffs[i]) memcpy(m_coeffs[i], other.m_coeffs[i], sizeof(TCoeff) * area);
   if (m_pcmbuf[i] && other.m_pcmbuf[i] && m_pcmbuf[i] != other.m_pcmbuf[i]) memcpy(m_pcmbuf[i], other.m_pcmbuf[i], sizeof(Pel   ) * area);
-  if (m_runType[i]   && other.m_runType[i]   && m_runType[i]   != other.m_runType[i])   memcpy(m_runType[i],   other.m_runType[i],   sizeof(bool) * area);
+  if ((i == COMPONENT_Y || i == COMPONENT_Cb))
+  {
+    if (m_runType[i] && other.m_runType[i] && m_runType[i] != other.m_runType[i])   memcpy(m_runType[i], other.m_runType[i], sizeof(bool) * area);
 #if !JVET_P0077_LINE_CG_PALETTE
-  if (m_runLength[i] && other.m_runLength[i] && m_runLength[i] != other.m_runLength[i]) memcpy(m_runLength[i], other.m_runLength[i], sizeof(Pel) * area );
+    if (m_runLength[i] && other.m_runLength[i] && m_runLength[i] != other.m_runLength[i]) memcpy(m_runLength[i], other.m_runLength[i], sizeof(Pel) * area);
 #endif
+  }
+
   cbf[i]           = other.cbf[i];
   rdpcm[i]         = other.rdpcm[i];
   compAlpha[i]     = other.compAlpha[i];
diff --git a/source/Lib/CommonLib/Unit.h b/source/Lib/CommonLib/Unit.h
index 872fa18a7..3c8484034 100644
--- a/source/Lib/CommonLib/Unit.h
+++ b/source/Lib/CommonLib/Unit.h
@@ -49,7 +49,7 @@
 // tools
 // ---------------------------------------------------------------------------
 struct PLTBuf {
-  uint32_t       curPLTSize[MAX_NUM_COMPONENT];
+  uint8_t        curPLTSize[MAX_NUM_CHANNEL_TYPE];
   Pel            curPLT[MAX_NUM_COMPONENT][MAXPLTPREDSIZE];
 };
 inline Position recalcPosition(const ChromaFormat _cf, const ComponentID srcCId, const ComponentID dstCId, const Position &pos)
@@ -335,12 +335,12 @@ struct CodingUnit : public UnitArea
 #endif
   uint8_t          smvdMode;
   uint8_t        ispMode;
-  bool           useEscape[MAX_NUM_COMPONENT];
-  bool           useRotation[MAX_NUM_COMPONENT];
-  bool           reuseflag[MAX_NUM_COMPONENT][MAXPLTPREDSIZE];
-  uint32_t       lastPLTSize[MAX_NUM_COMPONENT];
-  uint32_t       reusePLTSize[MAX_NUM_COMPONENT];
-  uint32_t       curPLTSize[MAX_NUM_COMPONENT];
+  bool           useEscape[MAX_NUM_CHANNEL_TYPE];
+  bool           useRotation[MAX_NUM_CHANNEL_TYPE];
+  bool           reuseflag[MAX_NUM_CHANNEL_TYPE][MAXPLTPREDSIZE];
+  uint8_t        lastPLTSize[MAX_NUM_CHANNEL_TYPE];
+  uint8_t        reusePLTSize[MAX_NUM_CHANNEL_TYPE];
+  uint8_t        curPLTSize[MAX_NUM_CHANNEL_TYPE];
   Pel            curPLT[MAX_NUM_COMPONENT][MAXPLTSIZE];
 
   CodingUnit() : chType( CH_L ) { }
@@ -533,9 +533,9 @@ struct TransformUnit : public UnitArea
 private:
   TCoeff *m_coeffs[ MAX_NUM_TBLOCKS ];
   Pel    *m_pcmbuf[ MAX_NUM_TBLOCKS ];
-  bool   *m_runType[MAX_NUM_TBLOCKS];
+  bool   *m_runType[ MAX_NUM_TBLOCKS - 1 ];
 #if !JVET_P0077_LINE_CG_PALETTE
-  Pel    *m_runLength[MAX_NUM_TBLOCKS];
+  Pel    *m_runLength[MAX_NUM_TBLOCKS - 1];
 #endif
 };
 
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index e68d3655a..a61f646c0 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -1047,7 +1047,7 @@ void DecLib::xActivateParameterSets()
     m_parameterSetManager.getPPS(m_apcSlicePilot->getPPSId())->setNumBricksInPic((int)m_pcPic->brickMap->bricks.size());
 #endif
     m_pcPic->createTempBuffers( m_pcPic->cs->pps->pcv->maxCUWidth );
-    m_pcPic->cs->createCoeffs();
+    m_pcPic->cs->createCoeffs((bool)m_pcPic->cs->sps->getPLTMode());
 
     m_pcPic->allocateNewSlice();
     // make the slice-pilot a real slice, and set up the slice-pilot for the next slice
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index 9c2778913..537ad3dc4 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -106,14 +106,14 @@ void EncCu::create( EncCfg* encCfg )
         m_pTempCS[w][h] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
         m_pBestCS[w][h] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
 
-        m_pTempCS[w][h]->create( chromaFormat, Area( 0, 0, width, height ), false );
-        m_pBestCS[w][h]->create( chromaFormat, Area( 0, 0, width, height ), false );
+        m_pTempCS[w][h]->create(chromaFormat, Area(0, 0, width, height), false, (bool)encCfg->getPLTMode());
+        m_pBestCS[w][h]->create(chromaFormat, Area(0, 0, width, height), false, (bool)encCfg->getPLTMode());
 
         m_pTempCS2[w][h] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
         m_pBestCS2[w][h] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
 
-        m_pTempCS2[w][h]->create( chromaFormat, Area( 0, 0, width, height ), false );
-        m_pBestCS2[w][h]->create( chromaFormat, Area( 0, 0, width, height ), false );
+        m_pTempCS2[w][h]->create(chromaFormat, Area(0, 0, width, height), false, (bool)encCfg->getPLTMode());
+        m_pBestCS2[w][h]->create(chromaFormat, Area(0, 0, width, height), false, (bool)encCfg->getPLTMode());
       }
       else
       {
@@ -638,9 +638,9 @@ void EncCu::xCompressCU( CodingStructure*& tempCS, CodingStructure*& bestCS, Par
     jointPLT = true;
   }
   SplitSeries splitmode = -1;
-  uint32_t  bestLastPLTSize[MAX_NUM_COMPONENT];
-  Pel       bestLastPLT[MAX_NUM_COMPONENT][MAXPLTPREDSIZE]; // store LastPLT for partition
-  uint32_t  curLastPLTSize[MAX_NUM_COMPONENT];
+  uint8_t   bestLastPLTSize[MAX_NUM_CHANNEL_TYPE];
+  Pel       bestLastPLT[MAX_NUM_COMPONENT][MAXPLTPREDSIZE]; // store LastPLT for 
+  uint8_t   curLastPLTSize[MAX_NUM_CHANNEL_TYPE];
   Pel       curLastPLT[MAX_NUM_COMPONENT][MAXPLTPREDSIZE]; // store LastPLT if no partition
   for (int i = compBegin; i < (compBegin + numComp); i++)
   {
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 521a6c70a..650c48329 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -2193,7 +2193,7 @@ void EncGOP::compressGOP( int iPOCLast, int iNumPicRcvd, PicList& rcListPic,
     pcPic->scheduler.init( pcPic->cs->pcv->heightInCtus, pcPic->cs->pcv->widthInCtus, m_pcCfg->getNumWppThreads(), m_pcCfg->getNumWppExtraLines(), 1                             );
 #endif
     pcPic->createTempBuffers( pcPic->cs->pps->pcv->maxCUWidth );
-    pcPic->cs->createCoeffs();
+    pcPic->cs->createCoeffs((bool)pcPic->cs->sps->getPLTMode());
 
     //  Slice data initialization
     pcPic->clearSliceBuffer();
diff --git a/source/Lib/EncoderLib/EncModeCtrl.cpp b/source/Lib/EncoderLib/EncModeCtrl.cpp
index 266cc3a99..e781453c4 100644
--- a/source/Lib/EncoderLib/EncModeCtrl.cpp
+++ b/source/Lib/EncoderLib/EncModeCtrl.cpp
@@ -795,17 +795,23 @@ void BestEncInfoCache::init( const Slice &slice )
 #if REUSE_CU_RESULTS_WITH_MULTIPLE_TUS
   m_pCoeff  = new TCoeff[numCoeff*MAX_NUM_TUS];
   m_pPcmBuf = new Pel   [numCoeff*MAX_NUM_TUS];
-  m_runType   = new bool[numCoeff*MAX_NUM_TUS];
+  if (slice.getSPS()->getPLTMode())
+  {
+    m_runType   = new bool[numCoeff*MAX_NUM_TUS];
 #if !JVET_P0077_LINE_CG_PALETTE
-  m_runLength = new Pel [numCoeff*MAX_NUM_TUS];
+    m_runLength = new Pel [numCoeff*MAX_NUM_TUS];
 #endif
+  }
 #else
   m_pCoeff  = new TCoeff[numCoeff];
   m_pPcmBuf = new Pel   [numCoeff];
-  m_runType   = new bool[numCoeff];
+  if (slice.getSPS()->getPLTMode())
+  {
+    m_runType   = new bool[numCoeff];
 #if !JVET_P0077_LINE_CG_PALETTE
-  m_runLength = new Pel [numCoeff];
+    m_runLength = new Pel [numCoeff];
 #endif
+  }
 #endif
 
   TCoeff *coeffPtr = m_pCoeff;
@@ -828,9 +834,9 @@ void BestEncInfoCache::init( const Slice &slice )
           {
             TCoeff *coeff[MAX_NUM_TBLOCKS] = { 0, };
             Pel    *pcmbf[MAX_NUM_TBLOCKS] = { 0, };
-            bool   *runType[MAX_NUM_TBLOCKS]   = { 0, };
+            bool   *runType[MAX_NUM_TBLOCKS - 1] = { 0, };
 #if !JVET_P0077_LINE_CG_PALETTE
-            Pel    *runLength[MAX_NUM_TBLOCKS] = { 0, };
+            Pel    *runLength[MAX_NUM_TBLOCKS - 1] = { 0, };
 #endif
 
 #if REUSE_CU_RESULTS_WITH_MULTIPLE_TUS
@@ -843,10 +849,13 @@ void BestEncInfoCache::init( const Slice &slice )
               {
                 coeff[i] = coeffPtr; coeffPtr += area.blocks[i].area();
                 pcmbf[i] = pcmPtr;   pcmPtr += area.blocks[i].area();
-                runType[i]   = runTypePtr;   runTypePtr   += area.blocks[i].area();
+                if (i < 2)
+                {
+                  runType[i]   = runTypePtr;   runTypePtr   += area.blocks[i].area();
 #if !JVET_P0077_LINE_CG_PALETTE
-                runLength[i] = runLengthPtr; runLengthPtr += area.blocks[i].area();
+                  runLength[i] = runLengthPtr; runLengthPtr += area.blocks[i].area();
 #endif
+                }
               }
 
               tu.cs = &m_dummyCS;
diff --git a/source/Lib/EncoderLib/IntraSearch.cpp b/source/Lib/EncoderLib/IntraSearch.cpp
index e99c01be6..e9a6fffdc 100644
--- a/source/Lib/EncoderLib/IntraSearch.cpp
+++ b/source/Lib/EncoderLib/IntraSearch.cpp
@@ -277,8 +277,9 @@ void IntraSearch::init( EncCfg*        pcEncCfg,
         m_pBestCS[width][height] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
         m_pTempCS[width][height] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
 
-        m_pBestCS[width][height]->create( m_pcEncCfg->getChromaFormatIdc(), Area( 0, 0, gp_sizeIdxInfo->sizeFrom( width ), gp_sizeIdxInfo->sizeFrom( height ) ), false );
-        m_pTempCS[width][height]->create( m_pcEncCfg->getChromaFormatIdc(), Area( 0, 0, gp_sizeIdxInfo->sizeFrom( width ), gp_sizeIdxInfo->sizeFrom( height ) ), false );
+        m_pBestCS[width][height]->create(m_pcEncCfg->getChromaFormatIdc(), Area(0, 0, gp_sizeIdxInfo->sizeFrom(width), gp_sizeIdxInfo->sizeFrom(height)), false, (bool)pcEncCfg->getPLTMode());
+        m_pTempCS[width][height]->create(m_pcEncCfg->getChromaFormatIdc(), Area(0, 0, gp_sizeIdxInfo->sizeFrom(width), gp_sizeIdxInfo->sizeFrom(height)), false, (bool)pcEncCfg->getPLTMode());
+
         m_pFullCS [width][height] = new CodingStructure*[uiNumLayersToAllocateFull];
         m_pSplitCS[width][height] = new CodingStructure*[uiNumLayersToAllocateSplit];
 
@@ -286,14 +287,13 @@ void IntraSearch::init( EncCfg*        pcEncCfg,
         {
           m_pFullCS [width][height][layer] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
 
-          m_pFullCS [width][height][layer]->create( m_pcEncCfg->getChromaFormatIdc(), Area( 0, 0, gp_sizeIdxInfo->sizeFrom( width ), gp_sizeIdxInfo->sizeFrom( height ) ), false );
+          m_pFullCS[width][height][layer]->create(m_pcEncCfg->getChromaFormatIdc(), Area(0, 0, gp_sizeIdxInfo->sizeFrom(width), gp_sizeIdxInfo->sizeFrom(height)), false, (bool)pcEncCfg->getPLTMode());
         }
 
         for( uint32_t layer = 0; layer < uiNumLayersToAllocateSplit; layer++ )
         {
           m_pSplitCS[width][height][layer] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
-
-          m_pSplitCS[width][height][layer]->create( m_pcEncCfg->getChromaFormatIdc(), Area( 0, 0, gp_sizeIdxInfo->sizeFrom( width ), gp_sizeIdxInfo->sizeFrom( height ) ), false );
+          m_pSplitCS[width][height][layer]->create(m_pcEncCfg->getChromaFormatIdc(), Area(0, 0, gp_sizeIdxInfo->sizeFrom(width), gp_sizeIdxInfo->sizeFrom(height)), false, (bool)pcEncCfg->getPLTMode());
         }
       }
       else
@@ -314,41 +314,44 @@ void IntraSearch::init( EncCfg*        pcEncCfg,
   for( uint32_t depth = 0; depth < uiNumSaveLayersToAllocate; depth++ )
   {
     m_pSaveCS[depth] = new CodingStructure( m_unitCache.cuCache, m_unitCache.puCache, m_unitCache.tuCache );
-    m_pSaveCS[depth]->create( UnitArea( cform, Area( 0, 0, maxCUWidth, maxCUHeight ) ), false );
+    m_pSaveCS[depth]->create(UnitArea(cform, Area(0, 0, maxCUWidth, maxCUHeight)), false, (bool)pcEncCfg->getPLTMode());
   }
 
   m_isInitialized = true;
 #if JVET_P0077_LINE_CG_PALETTE
-  m_symbolSize = (1 << bitDepthY); // pixel values are within [0, SymbolSize-1] with size SymbolSize
-  if (m_truncBinBits == nullptr)
+  if (pcEncCfg->getPLTMode())
   {
-    m_truncBinBits = new uint16_t*[m_symbolSize];
-    for (unsigned i = 0; i < m_symbolSize; i++)
+    m_symbolSize = (1 << bitDepthY); // pixel values are within [0, SymbolSize-1] with size SymbolSize
+    if (m_truncBinBits == nullptr)
     {
-      m_truncBinBits[i] = new uint16_t[m_symbolSize + 1];
+      m_truncBinBits = new uint16_t*[m_symbolSize];
+      for (unsigned i = 0; i < m_symbolSize; i++)
+      {
+        m_truncBinBits[i] = new uint16_t[m_symbolSize + 1];
+      }
     }
-  }
-  if (m_escapeNumBins == nullptr)
-  {
-    m_escapeNumBins = new uint16_t[m_symbolSize];
-  }
-  initTBCTable(bitDepthY);
-  if (m_indexError[0] == nullptr)
-  {
-    for (unsigned i = 0; i < (MAXPLTSIZE + 1); i++)
+    if (m_escapeNumBins == nullptr)
     {
-      m_indexError[i] = new double[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
+      m_escapeNumBins = new uint16_t[m_symbolSize];
     }
-  }
-  if (m_minErrorIndexMap == nullptr)
-  {
-    m_minErrorIndexMap = new uint8_t[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
-  }
-  if (m_statePtRDOQ[0] == nullptr)
-  {
-    for (unsigned i = 0; i < NUM_TRELLIS_STATE; i++)
+    initTBCTable(bitDepthY);
+    if (m_indexError[0] == nullptr)
     {
-      m_statePtRDOQ[i] = new uint8_t[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
+      for (unsigned i = 0; i < (MAXPLTSIZE + 1); i++)
+      {
+        m_indexError[i] = new double[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
+      }
+    }
+    if (m_minErrorIndexMap == nullptr)
+    {
+      m_minErrorIndexMap = new uint8_t[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
+    }
+    if (m_statePtRDOQ[0] == nullptr)
+    {
+      for (unsigned i = 0; i < NUM_TRELLIS_STATE; i++)
+      {
+        m_statePtRDOQ[i] = new uint8_t[MAX_CU_BLKSIZE_PLT*MAX_CU_BLKSIZE_PLT];
+      }
     }
   }
 #endif
-- 
GitLab