From da03de93185499e7e2b26e44f352f5c375c69251 Mon Sep 17 00:00:00 2001
From: Frank Bossen <fbossen@gmail.com>
Date: Sat, 12 Nov 2022 20:34:49 -0500
Subject: [PATCH] Rename JVET_C0024_ZERO_OUT_TH and clean up related functions

---
 CMakeLists.txt                            |   4 +-
 doc/software-manual.tex                   |   2 +-
 source/Lib/CommonLib/CommonDef.h          |   5 +-
 source/Lib/CommonLib/ContextModelling.cpp |  16 +--
 source/Lib/CommonLib/ContextModelling.h   |   2 +
 source/Lib/CommonLib/DepQuant.cpp         | 117 ++++++++++++----------
 source/Lib/CommonLib/Quant.cpp            |   6 +-
 source/Lib/CommonLib/QuantRDOQ.cpp        |  10 +-
 source/Lib/CommonLib/Rom.cpp              |   6 +-
 source/Lib/CommonLib/TrQuant.cpp          |  18 ++--
 source/Lib/CommonLib/Unit.cpp             |   4 +-
 source/Lib/DecoderLib/CABACReader.cpp     |   4 +-
 source/Lib/EncoderLib/CABACWriter.cpp     |   4 +-
 13 files changed, 104 insertions(+), 94 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 465e1644f3..1c58e05f63 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,8 +36,8 @@ if( CMAKE_COMPILER_IS_GNUCC )
   set( BUILD_STATIC OFF CACHE BOOL "Build static executables" )
 endif()
 
-# set c++11
-set( CMAKE_CXX_STANDARD 11 )
+# set c++14
+set( CMAKE_CXX_STANDARD 14 )
 set( CMAKE_CXX_STANDARD_REQUIRED ON )
 
 # compile everything position independent (even static libraries)
diff --git a/doc/software-manual.tex b/doc/software-manual.tex
index 0edb666c84..ec8c9d9e7b 100644
--- a/doc/software-manual.tex
+++ b/doc/software-manual.tex
@@ -245,7 +245,7 @@ The software may be retrieved from the GitLab server located at:
 Table~\ref{tab:project-files} lists the compiler environments and versions 
 for which building the software is tested.
 
-Note that the software makes use of C++11 language features, which may not
+Note that the software makes use of C++14 language features, which may not
 be available in older compilers.
 
 \begin{table}[ht]
diff --git a/source/Lib/CommonLib/CommonDef.h b/source/Lib/CommonLib/CommonDef.h
index ccc721b6be..c99c93444f 100644
--- a/source/Lib/CommonLib/CommonDef.h
+++ b/source/Lib/CommonLib/CommonDef.h
@@ -307,7 +307,10 @@ static constexpr int MAX_CU_TILING_PARTITIONS = 1 << ( MAX_LOG2_DIFF_CU_TR_SIZE
 
 static constexpr int PIC_MARGIN = 16;
 
-static constexpr int JVET_C0024_ZERO_OUT_TH =                          32;
+static constexpr int MAX_NONZERO_TU_SIZE = 32;
+
+// returns the size of the part of a TU that is not zero'ed out
+static inline constexpr int getNonzeroTuSize(int s) { return std::min(s, MAX_NONZERO_TU_SIZE); }
 
 static constexpr int MAX_NUM_PART_IDXS_IN_CTU_WIDTH = MAX_CU_SIZE/MIN_PU_SIZE; ///< maximum number of partition indices across the width of a CTU (or height of a CTU)
 static constexpr int SCALING_LIST_REM_NUM =                             6;
diff --git a/source/Lib/CommonLib/ContextModelling.cpp b/source/Lib/CommonLib/ContextModelling.cpp
index 7a7b0a89a3..dc24f4dbb6 100644
--- a/source/Lib/CommonLib/ContextModelling.cpp
+++ b/source/Lib/CommonLib/ContextModelling.cpp
@@ -40,6 +40,8 @@
 #include "CodingStructure.h"
 #include "Picture.h"
 
+const int CoeffCodingContext::prefixCtx[8] = { 0, 0, 0, 3, 6, 10, 15, 21 };
+
 CoeffCodingContext::CoeffCodingContext(const TransformUnit &tu, ComponentID component, bool signHide, bool bdpcm)
   : m_compID(component)
   , m_chType(toChannelType(m_compID))
@@ -48,8 +50,8 @@ CoeffCodingContext::CoeffCodingContext(const TransformUnit &tu, ComponentID comp
   , m_log2CGWidth(g_log2SbbSize[floorLog2(m_width)][floorLog2(m_height)][0])
   , m_log2CGHeight(g_log2SbbSize[floorLog2(m_width)][floorLog2(m_height)][1])
   , m_log2CGSize(m_log2CGWidth + m_log2CGHeight)
-  , m_widthInGroups(std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, m_width) >> m_log2CGWidth)
-  , m_heightInGroups(std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, m_height) >> m_log2CGHeight)
+  , m_widthInGroups(getNonzeroTuSize(m_width) >> m_log2CGWidth)
+  , m_heightInGroups(getNonzeroTuSize(m_height) >> m_log2CGHeight)
   , m_log2BlockWidth((unsigned) floorLog2(m_width))
   , m_log2BlockHeight((unsigned) floorLog2(m_height))
   , m_maxNumCoeff(m_width * m_height)
@@ -63,8 +65,8 @@ CoeffCodingContext::CoeffCodingContext(const TransformUnit &tu, ComponentID comp
                         [gp_sizeIdxInfo->idxFrom(m_heightInGroups)])
   , m_CtxSetLastX(Ctx::LastX[m_chType])
   , m_CtxSetLastY(Ctx::LastY[m_chType])
-  , m_maxLastPosX(g_groupIdx[std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, m_width) - 1])
-  , m_maxLastPosY(g_groupIdx[std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, m_height) - 1])
+  , m_maxLastPosX(g_groupIdx[getNonzeroTuSize(m_width) - 1])
+  , m_maxLastPosY(g_groupIdx[getNonzeroTuSize(m_height) - 1])
   , m_lastOffsetX(0)
   , m_lastOffsetY(0)
   , m_lastShiftX(0)
@@ -103,9 +105,9 @@ CoeffCodingContext::CoeffCodingContext(const TransformUnit &tu, ComponentID comp
   }
   else
   {
-    static const int prefix_ctx[8]  = { 0, 0, 0, 3, 6, 10, 15, 21 };
-    const_cast<int&>(m_lastOffsetX) = prefix_ctx[ log2sizeX ];
-    const_cast<int&>(m_lastOffsetY) = prefix_ctx[ log2sizeY ];;
+    const_cast<int &>(m_lastOffsetX) = prefixCtx[log2sizeX];
+    const_cast<int &>(m_lastOffsetY) = prefixCtx[log2sizeY];
+
     const_cast<int&>(m_lastShiftX)  = (log2sizeX + 1) >> 2;
     const_cast<int&>(m_lastShiftY)  = (log2sizeY + 1) >> 2;
   }
diff --git a/source/Lib/CommonLib/ContextModelling.h b/source/Lib/CommonLib/ContextModelling.h
index 8dd7ed29cb..a3899d79cf 100644
--- a/source/Lib/CommonLib/ContextModelling.h
+++ b/source/Lib/CommonLib/ContextModelling.h
@@ -51,6 +51,8 @@
 struct CoeffCodingContext
 {
 public:
+  static const int prefixCtx[8];
+
   CoeffCodingContext( const TransformUnit& tu, ComponentID component, bool signHide, bool bdpcm = false );
 public:
   void  initSubblock     ( int SubsetId, bool sigGroupFlag = false );
diff --git a/source/Lib/CommonLib/DepQuant.cpp b/source/Lib/CommonLib/DepQuant.cpp
index d52ff9ae06..038abec595 100644
--- a/source/Lib/CommonLib/DepQuant.cpp
+++ b/source/Lib/CommonLib/DepQuant.cpp
@@ -183,8 +183,8 @@ namespace DQIntern
         NbInfoSbb*&         sId2NbSbb     = m_scanId2NbInfoSbbArray[hd][vd];
         NbInfoOut*&         sId2NbOut     = m_scanId2NbInfoOutArray[hd][vd];
         // consider only non-zero-out region
-        const uint32_t      blkWidthNZOut = std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockWidth  );
-        const uint32_t      blkHeightNZOut= std::min<unsigned>( JVET_C0024_ZERO_OUT_TH, blockHeight );
+        const uint32_t      blkWidthNZOut  = getNonzeroTuSize(blockWidth);
+        const uint32_t      blkHeightNZOut = getNonzeroTuSize(blockHeight);
         const uint32_t      totalValues   = blkWidthNZOut * blkHeightNZOut;
 
         sId2NbSbb = new NbInfoSbb[ totalValues ];
@@ -342,8 +342,8 @@ namespace DQIntern
     m_chType              = chType;
     m_width               = width;
     m_height              = height;
-    const uint32_t nonzeroWidth  = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_width);
-    const uint32_t nonzeroHeight = std::min<uint32_t>(JVET_C0024_ZERO_OUT_TH, m_height);
+    const uint32_t nonzeroWidth  = getNonzeroTuSize(m_width);
+    const uint32_t nonzeroHeight = getNonzeroTuSize(m_height);
     m_numCoeff                   = nonzeroWidth * nonzeroHeight;
     const int log2W       = floorLog2( m_width  );
     const int log2H       = floorLog2( m_height );
@@ -482,72 +482,81 @@ namespace DQIntern
 
   void RateEstimator::xSetLastCoeffOffset( const FracBitsAccess& fracBitsAccess, const TUParameters& tuPars, const TransformUnit& tu, const ComponentID compID )
   {
-    const ChannelType chType = ( compID == COMPONENT_Y ? CHANNEL_TYPE_LUMA : CHANNEL_TYPE_CHROMA );
-    int32_t cbfDeltaBits = 0;
-    if( compID == COMPONENT_Y && !CU::isIntra(*tu.cu) && !tu.depth )
+    const ChannelType chType = toChannelType(compID);
+
+    BinFracBits bits = { 0, 0 };
+
+    if (isLuma(chType) && !CU::isIntra(*tu.cu) && !tu.depth)
     {
-      const BinFracBits bits  = fracBitsAccess.getFracBitsArray( Ctx::QtRootCbf() );
-      cbfDeltaBits            = int32_t( bits.intBits[1] ) - int32_t( bits.intBits[0] );
+      bits = fracBitsAccess.getFracBitsArray(Ctx::QtRootCbf());
     }
-    else
+    else if (tu.cu->ispMode && isLuma(chType))
     {
-      BinFracBits bits;
-      bool prevLumaCbf           = false;
-      bool lastCbfIsInferred     = false;
-      bool useIntraSubPartitions = tu.cu->ispMode && isLuma(chType);
-      if( useIntraSubPartitions )
+      bool lastCbfIsInferred = false;
+      if (CU::isISPLast(*tu.cu, tu.Y(), compID))
       {
-        bool rootCbfSoFar = false;
-        bool isLastSubPartition = CU::isISPLast(*tu.cu, tu.Y(), compID);
-        uint32_t nTus = tu.cu->ispMode == HOR_INTRA_SUBPARTITIONS ? tu.cu->lheight() >> floorLog2(tu.lheight()) : tu.cu->lwidth() >> floorLog2(tu.lwidth());
-        if( isLastSubPartition )
+        TransformUnit *tuPointer = tu.cu->firstTU;
+
+        const uint32_t nTus = tu.cu->ispMode == HOR_INTRA_SUBPARTITIONS ? tu.cu->lheight() >> floorLog2(tu.lheight())
+                                                                        : tu.cu->lwidth() >> floorLog2(tu.lwidth());
+
+        lastCbfIsInferred = true;
+        for (int tuIdx = 0; tuIdx < nTus - 1; tuIdx++)
         {
-          TransformUnit* tuPointer = tu.cu->firstTU;
-          for( int tuIdx = 0; tuIdx < nTus - 1; tuIdx++ )
-          {
-            rootCbfSoFar |= TU::getCbfAtDepth(*tuPointer, COMPONENT_Y, tu.depth);
-            tuPointer     = tuPointer->next;
-          }
-          if( !rootCbfSoFar )
+          if (TU::getCbfAtDepth(*tuPointer, COMPONENT_Y, tu.depth))
           {
-            lastCbfIsInferred = true;
+            lastCbfIsInferred = false;
+            break;
           }
+          tuPointer = tuPointer->next;
         }
-        if( !lastCbfIsInferred )
-        {
-          prevLumaCbf = TU::getPrevTuCbfAtDepth(tu, compID, tu.depth);
-        }
-        bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, prevLumaCbf, true)));
       }
-      else
+
+      if (!lastCbfIsInferred)
       {
-        bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, tu.cbf[COMPONENT_Cb])));
+        const bool prevLumaCbf = TU::getPrevTuCbfAtDepth(tu, compID, tu.depth);
+        bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, prevLumaCbf, true)));
       }
-      cbfDeltaBits = lastCbfIsInferred ? 0 : int32_t(bits.intBits[1]) - int32_t(bits.intBits[0]);
+    }
+    else
+    {
+      bits = fracBitsAccess.getFracBitsArray(Ctx::QtCbf[compID](DeriveCtx::CtxQtCbf(compID, tu.cbf[COMPONENT_Cb])));
     }
 
-    static const unsigned prefixCtx[] = { 0, 0, 0, 3, 6, 10, 15, 21 };
-    uint32_t              ctxBits  [ LAST_SIGNIFICANT_GROUPS ];
-    for( unsigned xy = 0; xy < 2; xy++ )
+    const int32_t cbfDeltaBits = int32_t(bits.intBits[1]) - int32_t(bits.intBits[0]);
+
+    for (int xy = 0; xy < 2; xy++)
     {
-      int32_t             bitOffset   = ( xy ? cbfDeltaBits : 0 );
-      int32_t*            lastBits    = ( xy ? m_lastBitsY : m_lastBitsX );
-      const unsigned      size        = ( xy ? tuPars.m_height : tuPars.m_width );
-      const unsigned      log2Size    = ceilLog2( size );
-      const bool          useYCtx     = ( xy != 0 );
-      const CtxSet&       ctxSetLast  = ( useYCtx ? Ctx::LastY : Ctx::LastX )[ chType ];
-      const unsigned      lastShift   = ( compID == COMPONENT_Y ? (log2Size+1)>>2 : Clip3<unsigned>(0,2,size>>3) );
-      const unsigned      lastOffset  = ( compID == COMPONENT_Y ? ( prefixCtx[log2Size] ) : 0 );
-      uint32_t            sumFBits    = 0;
-      unsigned            maxCtxId    = g_groupIdx[std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, size) - 1];
-      for( unsigned ctxId = 0; ctxId < maxCtxId; ctxId++ )
+      const bool isY = xy != 0;
+
+      const unsigned size       = isY ? tuPars.m_height : tuPars.m_width;
+      const int      log2Size   = ceilLog2(size);
+      const CtxSet  &ctxSetLast = (isY ? Ctx::LastY : Ctx::LastX)[chType];
+      const unsigned lastShift  = isLuma(chType) ? (log2Size + 1) >> 2 : Clip3<unsigned>(0, 2, size >> 3);
+      const unsigned lastOffset = isLuma(chType) ? CoeffCodingContext::prefixCtx[log2Size] : 0;
+      const int      nzSize     = getNonzeroTuSize(size);
+      const int      maxCtxId   = g_groupIdx[nzSize - 1];
+
+      int sumBits = isY ? cbfDeltaBits : 0;
+
+      std::array<int32_t, LAST_SIGNIFICANT_GROUPS> ctxBits;
+
+      for (int ctxId = 0; ctxId <= maxCtxId; ctxId++)
       {
-        const BinFracBits bits  = fracBitsAccess.getFracBitsArray( ctxSetLast( lastOffset + ( ctxId >> lastShift ) ) );
-        ctxBits[ ctxId ]        = sumFBits + bits.intBits[0] + ( ctxId>3 ? ((ctxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset;
-        sumFBits               +=            bits.intBits[1];
+        ctxBits[ctxId] = sumBits + (ctxId > 3 ? ((ctxId - 2) >> 1) << SCALE_BITS : 0);
+
+        if (ctxId < maxCtxId)
+        {
+          const BinFracBits bits = fracBitsAccess.getFracBitsArray(ctxSetLast(lastOffset + (ctxId >> lastShift)));
+
+          ctxBits[ctxId] += bits.intBits[0];
+          sumBits += bits.intBits[1];
+        }
       }
-      ctxBits  [ maxCtxId ]     = sumFBits + ( maxCtxId>3 ? ((maxCtxId-2)>>1)<<SCALE_BITS : 0 ) + bitOffset;
-      for (unsigned pos = 0; pos < std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, size); pos++)
+
+      int32_t *lastBits = isY ? m_lastBitsY : m_lastBitsX;
+
+      for (int pos = 0; pos < nzSize; pos++)
       {
         lastBits[pos] = ctxBits[g_groupIdx[pos]];
       }
diff --git a/source/Lib/CommonLib/Quant.cpp b/source/Lib/CommonLib/Quant.cpp
index 249df2fe57..656d69d17e 100644
--- a/source/Lib/CommonLib/Quant.cpp
+++ b/source/Lib/CommonLib/Quant.cpp
@@ -753,7 +753,7 @@ void Quant::processScalingListEnc( int *coeff, int *quantcoeff, int quantScales,
     {
       for (uint32_t i = 0; i<width; i++)
       {
-        if (j >= JVET_C0024_ZERO_OUT_TH || i >= JVET_C0024_ZERO_OUT_TH)
+        if (j >= MAX_NONZERO_TU_SIZE || i >= MAX_NONZERO_TU_SIZE)
         {
           quantcoeff[j*width + i] = 0;
           continue;
@@ -807,7 +807,7 @@ void Quant::processScalingListDec( const int *coeff, int *dequantcoeff, int invQ
         int dequantCoeffLineSep = j * width;
         for (uint32_t i = 0; i < width; i++)
         {
-          if (i >= JVET_C0024_ZERO_OUT_TH || j >= JVET_C0024_ZERO_OUT_TH)
+          if (i >= MAX_NONZERO_TU_SIZE || j >= MAX_NONZERO_TU_SIZE)
           {
             dequantcoeff[dequantCoeffLineSep + i] = 0;
             continue;
@@ -824,7 +824,7 @@ void Quant::processScalingListDec( const int *coeff, int *dequantcoeff, int invQ
         int dequantCoeffLineSep = j * width;
         for (uint32_t i = 0; i < width; i++)
         {
-          if (i >= JVET_C0024_ZERO_OUT_TH || j >= JVET_C0024_ZERO_OUT_TH)
+          if (i >= MAX_NONZERO_TU_SIZE || j >= MAX_NONZERO_TU_SIZE)
           {
             dequantcoeff[dequantCoeffLineSep + i] = 0;
             continue;
diff --git a/source/Lib/CommonLib/QuantRDOQ.cpp b/source/Lib/CommonLib/QuantRDOQ.cpp
index 760f28d393..989b83ff5b 100644
--- a/source/Lib/CommonLib/QuantRDOQ.cpp
+++ b/source/Lib/CommonLib/QuantRDOQ.cpp
@@ -675,7 +675,7 @@ void QuantRDOQ::xRateDistOptQuant(TransformUnit &tu, const ComponentID &compID,
 
   const uint32_t lfnstIdx = tu.cu->lfnstIdx;
 
-  const int iCGNum = lfnstIdx > 0 ? 1 : std::min<int>(JVET_C0024_ZERO_OUT_TH, uiWidth) * std::min<int>(JVET_C0024_ZERO_OUT_TH, uiHeight) >> cctx.log2CGSize();
+  const int iCGNum = lfnstIdx > 0 ? 1 : getNonzeroTuSize(uiWidth) * getNonzeroTuSize(uiHeight) >> cctx.log2CGSize();
 
   for (int subSetId = iCGNum - 1; subSetId >= 0; subSetId--)
   {
@@ -955,8 +955,8 @@ void QuantRDOQ::xRateDistOptQuant(TransformUnit &tu, const ComponentID &compID,
   int lastBitsX[LAST_SIGNIFICANT_GROUPS] = { 0 };
   int lastBitsY[LAST_SIGNIFICANT_GROUPS] = { 0 };
   {
-    int dim1 = std::min<int>(JVET_C0024_ZERO_OUT_TH, uiWidth);
-    int dim2 = std::min<int>(JVET_C0024_ZERO_OUT_TH, uiHeight);
+    int dim1  = getNonzeroTuSize(uiWidth);
+    int dim2  = getNonzeroTuSize(uiHeight);
     int bitsX = 0;
     int bitsY = 0;
     int ctxId;
@@ -978,8 +978,8 @@ void QuantRDOQ::xRateDistOptQuant(TransformUnit &tu, const ComponentID &compID,
     lastBitsY[ctxId] = bitsY;
   }
 
-  unsigned zoTbWdith  = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.width());
-  unsigned zoTbHeight = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.height());
+  unsigned zoTbWdith  = getNonzeroTuSize(cctx.width());
+  unsigned zoTbHeight = getNonzeroTuSize(cctx.height());
   if (tu.cs->sps->getMtsEnabled() && tu.cu->sbtInfo != 0 && tu.blocks[compID].width <= 32
       && tu.blocks[compID].height <= 32 && compID == COMPONENT_Y)
   {
diff --git a/source/Lib/CommonLib/Rom.cpp b/source/Lib/CommonLib/Rom.cpp
index 051f130283..8d10a285db 100644
--- a/source/Lib/CommonLib/Rom.cpp
+++ b/source/Lib/CommonLib/Rom.cpp
@@ -337,8 +337,8 @@ void initROM()
 
       const uint32_t  groupWidth     = 1 << log2CGWidth;
       const uint32_t  groupHeight    = 1 << log2CGHeight;
-      const uint32_t  widthInGroups = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, blockWidth) >> log2CGWidth;
-      const uint32_t  heightInGroups = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, blockHeight) >> log2CGHeight;
+      const uint32_t  widthInGroups  = getNonzeroTuSize(blockWidth) >> log2CGWidth;
+      const uint32_t  heightInGroups = getNonzeroTuSize(blockHeight) >> log2CGHeight;
 
       const uint32_t  groupSize      = groupWidth    * groupHeight;
       const uint32_t  totalGroups    = widthInGroups * heightInGroups;
@@ -351,7 +351,7 @@ void initROM()
 
         g_scanOrder[SCAN_GROUPED_4x4][scanType][blockWidthIdx][blockHeightIdx] = scan;
 
-        if ( blockWidth > JVET_C0024_ZERO_OUT_TH || blockHeight > JVET_C0024_ZERO_OUT_TH )
+        if (blockWidth > MAX_NONZERO_TU_SIZE || blockHeight > MAX_NONZERO_TU_SIZE)
         {
           for (uint32_t i = 0; i < totalValues; i++)
           {
diff --git a/source/Lib/CommonLib/TrQuant.cpp b/source/Lib/CommonLib/TrQuant.cpp
index 4ac291a3a4..ae3c85e8b6 100644
--- a/source/Lib/CommonLib/TrQuant.cpp
+++ b/source/Lib/CommonLib/TrQuant.cpp
@@ -736,12 +736,9 @@ void TrQuant::xT( const TransformUnit &tu, const ComponentID &compID, const CPel
 
   getTrTypes ( tu, compID, trTypeHor, trTypeVer );
 
-  int skipWidth  = (trTypeHor != TransType::DCT2 && width == 32) ? 16
-                   : width > JVET_C0024_ZERO_OUT_TH              ? width - JVET_C0024_ZERO_OUT_TH
-                                                                 : 0;
-  int skipHeight = (trTypeVer != TransType::DCT2 && height == 32) ? 16
-                   : height > JVET_C0024_ZERO_OUT_TH              ? height - JVET_C0024_ZERO_OUT_TH
-                                                                  : 0;
+  int skipWidth  = (trTypeHor != TransType::DCT2 && width == 32) ? 16 : std::max(width - MAX_NONZERO_TU_SIZE, 0);
+  int skipHeight = (trTypeVer != TransType::DCT2 && height == 32) ? 16 : std::max(height - MAX_NONZERO_TU_SIZE, 0);
+
   if( tu.cs->sps->getUseLFNST() && tu.cu->lfnstIdx )
   {
     if( (width == 4 && height > 4) || (width > 4 && height == 4) )
@@ -821,12 +818,9 @@ void TrQuant::xIT( const TransformUnit &tu, const ComponentID &compID, const CCo
   auto trTypeVer = TransType::DCT2;
 
   getTrTypes ( tu, compID, trTypeHor, trTypeVer );
-  int skipWidth  = (trTypeHor != TransType::DCT2 && width == 32) ? 16
-                   : width > JVET_C0024_ZERO_OUT_TH              ? width - JVET_C0024_ZERO_OUT_TH
-                                                                 : 0;
-  int skipHeight = (trTypeVer != TransType::DCT2 && height == 32) ? 16
-                   : height > JVET_C0024_ZERO_OUT_TH              ? height - JVET_C0024_ZERO_OUT_TH
-                                                                  : 0;
+  int skipWidth  = (trTypeHor != TransType::DCT2 && width == 32) ? 16 : std::max(width - MAX_NONZERO_TU_SIZE, 0);
+  int skipHeight = (trTypeVer != TransType::DCT2 && height == 32) ? 16 : std::max(height - MAX_NONZERO_TU_SIZE, 0);
+
   if( tu.cs->sps->getUseLFNST() && tu.cu->lfnstIdx )
   {
     if( (width == 4 && height > 4) || (width > 4 && height == 4) )
diff --git a/source/Lib/CommonLib/Unit.cpp b/source/Lib/CommonLib/Unit.cpp
index d512377a88..841f0cd981 100644
--- a/source/Lib/CommonLib/Unit.cpp
+++ b/source/Lib/CommonLib/Unit.cpp
@@ -889,8 +889,8 @@ int TransformUnit::getTbAreaAfterCoefZeroOut(ComponentID compID) const
     tbZeroOutWidth = (blocks[compID].width == 32) ? 16 : tbZeroOutWidth;
     tbZeroOutHeight = (blocks[compID].height == 32) ? 16 : tbZeroOutHeight;
   }
-  tbZeroOutWidth = std::min<int>(JVET_C0024_ZERO_OUT_TH, tbZeroOutWidth);
-  tbZeroOutHeight = std::min<int>(JVET_C0024_ZERO_OUT_TH, tbZeroOutHeight);
+  tbZeroOutWidth  = getNonzeroTuSize(tbZeroOutWidth);
+  tbZeroOutHeight = getNonzeroTuSize(tbZeroOutHeight);
   tbArea = tbZeroOutWidth * tbZeroOutHeight;
   return tbArea;
 }
diff --git a/source/Lib/DecoderLib/CABACReader.cpp b/source/Lib/DecoderLib/CABACReader.cpp
index de826f2ba6..8661fbd9a3 100644
--- a/source/Lib/DecoderLib/CABACReader.cpp
+++ b/source/Lib/DecoderLib/CABACReader.cpp
@@ -3168,8 +3168,8 @@ int CABACReader::last_sig_coeff( CoeffCodingContext& cctx, TransformUnit& tu, Co
   unsigned PosLastX = 0, PosLastY = 0;
   unsigned maxLastPosX = cctx.maxLastPosX();
   unsigned maxLastPosY = cctx.maxLastPosY();
-  unsigned zoTbWdith  = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.width());
-  unsigned zoTbHeight = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.height());
+  unsigned zoTbWdith   = getNonzeroTuSize(cctx.width());
+  unsigned zoTbHeight  = getNonzeroTuSize(cctx.height());
 
   if (tu.cs->sps->getMtsEnabled() && tu.cu->sbtInfo != 0 && tu.blocks[compID].width <= 32
       && tu.blocks[compID].height <= 32 && compID == COMPONENT_Y)
diff --git a/source/Lib/EncoderLib/CABACWriter.cpp b/source/Lib/EncoderLib/CABACWriter.cpp
index 7b11f533e0..82b21d24f7 100644
--- a/source/Lib/EncoderLib/CABACWriter.cpp
+++ b/source/Lib/EncoderLib/CABACWriter.cpp
@@ -2871,8 +2871,8 @@ void CABACWriter::last_sig_coeff( CoeffCodingContext& cctx, const TransformUnit&
   unsigned maxLastPosX = cctx.maxLastPosX();
   unsigned maxLastPosY = cctx.maxLastPosY();
 
-  unsigned zoTbWdith  = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.width());
-  unsigned zoTbHeight = std::min<unsigned>(JVET_C0024_ZERO_OUT_TH, cctx.height());
+  unsigned zoTbWdith  = getNonzeroTuSize(cctx.width());
+  unsigned zoTbHeight = getNonzeroTuSize(cctx.height());
 
   if (tu.cs->sps->getMtsEnabled() && tu.cu->sbtInfo != 0 && tu.blocks[compID].width <= 32
       && tu.blocks[compID].height <= 32 && compID == COMPONENT_Y)
-- 
GitLab