From 6ff0e8d7b56305dad5b7266be1716fdf2222a38c Mon Sep 17 00:00:00 2001
From: Frank Bossen <frank@bossentech.com>
Date: Sun, 8 Jan 2023 04:45:46 +0000
Subject: [PATCH] Remove g_tbMax and use floorLog2 instead

---
 source/Lib/CommonLib/Rom.h            | 11 -------
 source/Lib/DecoderLib/CABACReader.cpp | 31 ++++---------------
 source/Lib/DecoderLib/CABACReader.h   |  2 +-
 source/Lib/EncoderLib/CABACWriter.cpp | 36 ++++++----------------
 source/Lib/EncoderLib/CABACWriter.h   |  2 +-
 source/Lib/EncoderLib/IntraSearch.cpp | 43 +++++++--------------------
 source/Lib/EncoderLib/IntraSearch.h   |  2 +-
 7 files changed, 28 insertions(+), 99 deletions(-)

diff --git a/source/Lib/CommonLib/Rom.h b/source/Lib/CommonLib/Rom.h
index ce33c8280f..366181a486 100644
--- a/source/Lib/CommonLib/Rom.h
+++ b/source/Lib/CommonLib/Rom.h
@@ -195,17 +195,6 @@ int8_t   getBcwWeight(uint8_t bcwIdx, uint8_t refFrameList);
 void     resetBcwCodingOrder(bool runDecoding, const CodingStructure &cs);
 uint32_t deriveWeightIdxBits(uint8_t bcwIdx);
 
-constexpr uint8_t g_tbMax[257] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
-4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
-5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
-6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
-6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7,
-7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
-7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
-7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
-7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
-7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8 };
-
 //! \}
 
 
diff --git a/source/Lib/DecoderLib/CABACReader.cpp b/source/Lib/DecoderLib/CABACReader.cpp
index 33c0eb2e83..91392cca44 100644
--- a/source/Lib/DecoderLib/CABACReader.cpp
+++ b/source/Lib/DecoderLib/CABACReader.cpp
@@ -1320,35 +1320,16 @@ void CABACReader::cu_bcw_flag(CodingUnit& cu)
   DTRACE(g_trace_ctx, D_SYNTAX, "cu_bcw_flag() bcw_idx=%d\n", cu.bcwIdx ? 1 : 0);
 }
 
-void CABACReader::xReadTruncBinCode(uint32_t& symbol, uint32_t maxSymbol)
+void CABACReader::xReadTruncBinCode(uint32_t &symbol, uint32_t numSymbols)
 {
-  int thresh;
-  if (maxSymbol > 256)
-  {
-    int threshVal = 1 << 8;
-    thresh = 8;
-    while (threshVal <= maxSymbol)
-    {
-      thresh++;
-      threshVal <<= 1;
-    }
-    thresh--;
-  }
-  else
-  {
-    thresh = g_tbMax[maxSymbol];
-  }
+  const int thresh = floorLog2(numSymbols);
+  const int val    = 1 << thresh;
+  const int b      = numSymbols - val;
 
-  int val = 1 << thresh;
-  int b = maxSymbol - val;
-  symbol  = m_binDecoder.decodeBinsEP(thresh);
+  symbol = m_binDecoder.decodeBinsEP(thresh);
   if (symbol >= val - b)
   {
-    uint32_t altSymbol;
-    altSymbol = m_binDecoder.decodeBinEP();
-    symbol <<= 1;
-    symbol += altSymbol;
-    symbol -= (val - b);
+    symbol = 2 * symbol - (val - b) + m_binDecoder.decodeBinEP();
   }
 }
 
diff --git a/source/Lib/DecoderLib/CABACReader.h b/source/Lib/DecoderLib/CABACReader.h
index d6c855a2ea..e8fcf54ebf 100644
--- a/source/Lib/DecoderLib/CABACReader.h
+++ b/source/Lib/DecoderLib/CABACReader.h
@@ -153,7 +153,7 @@ private:
   unsigned    exp_golomb_eqprob         ( unsigned count );
   unsigned    get_num_bits_read() { return m_binDecoder.getNumBitsRead(); }
 
-  void        xReadTruncBinCode(uint32_t& symbol, uint32_t maxSymbol);
+  void        xReadTruncBinCode(uint32_t &symbol, uint32_t numSymbols);
   void        parseScanRotationModeFlag ( CodingUnit& cu,           ComponentID compBegin );
   void        xDecodePLTPredIndicator   ( CodingUnit& cu,           uint32_t maxPLTSize,   ComponentID compBegin );
   void        xAdjustPLTIndex           ( CodingUnit& cu,           Pel curLevel,          uint32_t idx, PelBuf& paletteIdx, PLTtypeBuf& paletteRunType, int maxSymbol, ComponentID compBegin );
diff --git a/source/Lib/EncoderLib/CABACWriter.cpp b/source/Lib/EncoderLib/CABACWriter.cpp
index 9e06cdc552..f0ee04a372 100644
--- a/source/Lib/EncoderLib/CABACWriter.cpp
+++ b/source/Lib/EncoderLib/CABACWriter.cpp
@@ -924,41 +924,23 @@ void CABACWriter::cu_bcw_flag(const CodingUnit& cu)
   DTRACE(g_trace_ctx, D_SYNTAX, "cu_bcw_flag() bcw_idx=%d\n", cu.bcwIdx ? 1 : 0);
 }
 
-void CABACWriter::xWriteTruncBinCode(uint32_t symbol, uint32_t maxSymbol)
+void CABACWriter::xWriteTruncBinCode(const uint32_t symbol, const uint32_t numSymbols)
 {
-  int thresh;
-  if (maxSymbol > 256)
-  {
-    int threshVal = 1 << 8;
-    thresh = 8;
-    while (threshVal <= maxSymbol)
-    {
-      thresh++;
-      threshVal <<= 1;
-    }
-    thresh--;
-  }
-  else
-  {
-    thresh = g_tbMax[maxSymbol];
-  }
+  CHECKD(symbol >= numSymbols, "symbol must be less than numSymbols");
+
+  const int thresh = floorLog2(numSymbols);
+
+  const int val = 1 << thresh;
+
+  const int b = numSymbols - val;
 
-  int val = 1 << thresh;
-  assert(val <= maxSymbol);
-  assert((val << 1) > maxSymbol);
-  assert(symbol < maxSymbol);
-  int b = maxSymbol - val;
-  assert(b < val);
   if (symbol < val - b)
   {
     m_binEncoder.encodeBinsEP(symbol, thresh);
   }
   else
   {
-    symbol += val - b;
-    assert(symbol < (val << 1));
-    assert((symbol >> 1) >= val - b);
-    m_binEncoder.encodeBinsEP(symbol, thresh + 1);
+    m_binEncoder.encodeBinsEP(symbol + val - b, thresh + 1);
   }
 }
 
diff --git a/source/Lib/EncoderLib/CABACWriter.h b/source/Lib/EncoderLib/CABACWriter.h
index 8b1c5957f9..291f3fe850 100644
--- a/source/Lib/EncoderLib/CABACWriter.h
+++ b/source/Lib/EncoderLib/CABACWriter.h
@@ -180,7 +180,7 @@ private:
   void        unary_max_eqprob          ( unsigned symbol,                                   unsigned maxSymbol );
   void        exp_golomb_eqprob         ( unsigned symbol, unsigned count );
 
-  void  xWriteTruncBinCode(uint32_t uiSymbol, uint32_t uiMaxSymbol);
+  void        xWriteTruncBinCode(uint32_t symbol, uint32_t numSymbols);
   void        codeScanRotationModeFlag   ( const CodingUnit& cu,     ComponentID compBegin);
   void        xEncodePLTPredIndicator    ( const CodingUnit& cu,     uint32_t    maxPltSize, ComponentID compBegin);
 private:
diff --git a/source/Lib/EncoderLib/IntraSearch.cpp b/source/Lib/EncoderLib/IntraSearch.cpp
index de026a2aaf..31a96ea4b8 100644
--- a/source/Lib/EncoderLib/IntraSearch.cpp
+++ b/source/Lib/EncoderLib/IntraSearch.cpp
@@ -2454,40 +2454,17 @@ uint32_t IntraSearch::getEpExGolombNumBins(uint32_t symbol, uint32_t count)
   return numBins;
 }
 
-uint32_t IntraSearch::getTruncBinBits(uint32_t symbol, uint32_t maxSymbol)
+uint32_t IntraSearch::getTruncBinBits(const uint32_t symbol, const uint32_t numSymbols)
 {
-  uint32_t idxCodeBit = 0;
-  uint32_t thresh;
-  if (maxSymbol > 256)
-  {
-    uint32_t threshVal = 1 << 8;
-    thresh = 8;
-    while (threshVal <= maxSymbol)
-    {
-      thresh++;
-      threshVal <<= 1;
-    }
-    thresh--;
-  }
-  else
-  {
-    thresh = g_tbMax[maxSymbol];
-  }
-  uint32_t uiVal = 1 << thresh;
-  assert(uiVal <= maxSymbol);
-  assert((uiVal << 1) > maxSymbol);
-  assert(symbol < maxSymbol);
-  uint32_t b = maxSymbol - uiVal;
-  assert(b < uiVal);
-  if (symbol < uiVal - b)
-  {
-    idxCodeBit = thresh;
-  }
-  else
-  {
-    idxCodeBit = thresh + 1;
-  }
-  return idxCodeBit;
+  CHECKD(symbol >= numSymbols, "symbol must be less than numSymbols");
+
+  const uint32_t thresh = floorLog2(numSymbols);
+
+  const uint32_t val = 1 << thresh;
+
+  const uint32_t b = numSymbols - val;
+
+  return symbol < val - b ? thresh : thresh + 1;
 }
 
 void IntraSearch::calcPixelPred(CodingStructure& cs, Partitioner& partitioner, uint32_t yPos, uint32_t xPos, ComponentID compBegin, uint32_t numComp)
diff --git a/source/Lib/EncoderLib/IntraSearch.h b/source/Lib/EncoderLib/IntraSearch.h
index 9ec34793c2..b750264ebb 100644
--- a/source/Lib/EncoderLib/IntraSearch.h
+++ b/source/Lib/EncoderLib/IntraSearch.h
@@ -490,7 +490,7 @@ protected:
                           PLTScanMode pltScanMode, double &cost, bool *idxExist);
   bool     deriveSubblockIndexMap(CodingStructure& cs, Partitioner& partitioner, ComponentID compBegin, PLTScanMode pltScanMode, int minSubPos, int maxSubPos, const BinFracBits& fracBitsPltRunType, const BinFracBits* fracBitsPltIndexINDEX, const BinFracBits* fracBitsPltIndexCOPY, const double minCost, bool useRotate);
   double   rateDistOptPLT         (bool RunType, uint8_t RunIndex, bool prevRunType, uint8_t prevRunIndex, uint8_t aboveRunIndex, bool& prevCodedRunType, int& prevCodedRunPos, int scanPos, uint32_t width, int dist, int indexMaxValue, const BinFracBits* IndexfracBits, const BinFracBits& TypefracBits);
-  uint32_t getTruncBinBits        (uint32_t symbol, uint32_t maxSymbol);
+  uint32_t getTruncBinBits(uint32_t symbol, uint32_t numSymbols);
   uint32_t getEpExGolombNumBins   (uint32_t symbol, uint32_t count);
 
   void xGetNextISPMode                    ( ModeInfo& modeInfo, const ModeInfo* lastMode, const Size cuSize );
-- 
GitLab