From 93a5dee91f9f53b9922f4c1e8dd97e5ca2f9417c Mon Sep 17 00:00:00 2001
From: Taoran Lu <tlu@dolby.com>
Date: Tue, 2 Apr 2019 06:34:27 +0200
Subject: [PATCH] N0220 LMCS simplifications

---
 source/Lib/CommonLib/Buffer.cpp  |  3 +++
 source/Lib/CommonLib/CommonDef.h |  4 ++++
 source/Lib/CommonLib/Reshape.cpp | 18 ++++++++++++++++++
 source/Lib/CommonLib/TypeDef.h   |  2 ++
 4 files changed, 27 insertions(+)

diff --git a/source/Lib/CommonLib/Buffer.cpp b/source/Lib/CommonLib/Buffer.cpp
index a773bd306..6dd8562fe 100644
--- a/source/Lib/CommonLib/Buffer.cpp
+++ b/source/Lib/CommonLib/Buffer.cpp
@@ -407,6 +407,9 @@ void AreaBuf<Pel>::scaleSignal(const int scale, const bool dir, const ClpRng& cl
     {
       for (unsigned x = 0; x < width; x++)
       {
+#if JVET_N0220_LMCS_SIMPLIFICATION
+        src[x] = (Pel)Clip3((Pel)(-maxAbsclipBD - 1), (Pel)maxAbsclipBD, src[x]);
+#endif
         sign = src[x] >= 0 ? 1 : -1;
         absval = sign * src[x];
         int val = sign * ((absval * scale + (1 << (CSCALE_FP_PREC - 1))) >> CSCALE_FP_PREC);
diff --git a/source/Lib/CommonLib/CommonDef.h b/source/Lib/CommonLib/CommonDef.h
index 6d37b4954..e047a0c52 100644
--- a/source/Lib/CommonLib/CommonDef.h
+++ b/source/Lib/CommonLib/CommonDef.h
@@ -419,7 +419,11 @@ static constexpr int MV_MANTISSA_LIMIT       = (1 << (MV_MANTISSA_BITCOUNT - 1))
 static constexpr int MV_EXPONENT_MASK        = ((1 << MV_EXPONENT_BITCOUNT) - 1);
 static const int PIC_ANALYZE_CW_BINS =                           32;
 static const int PIC_CODE_CW_BINS =                              16;
+#if JVET_N0220_LMCS_SIMPLIFICATION
+static const int FP_PREC =                                       11;
+#else
 static const int FP_PREC =                                       14;
+#endif
 static const int CSCALE_FP_PREC =                                11;
 // ====================================================================================================================
 // Macro functions
diff --git a/source/Lib/CommonLib/Reshape.cpp b/source/Lib/CommonLib/Reshape.cpp
index 85f06103e..d04164992 100644
--- a/source/Lib/CommonLib/Reshape.cpp
+++ b/source/Lib/CommonLib/Reshape.cpp
@@ -110,12 +110,19 @@ void Reshape::reverseLUT(std::vector<Pel>& inputLUT, std::vector<Pel>& outputLUT
   for (i = m_reshapePivot[m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1]; i < m_reshapeLUTSize; i++)
     outputLUT[i] = outputLUT[m_reshapePivot[m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1]];
 
+#if JVET_N0220_LMCS_SIMPLIFICATION
+  for (i = 0; i < lutSize; i++)
+  {
+    outputLUT[i] = Clip3((Pel)0, (Pel)((1<<m_lumaBD)-1), outputLUT[i]);
+  }
+#else
   bool clipRange = ((m_sliceReshapeInfo.reshaperModelMinBinIdx > 0) && (m_sliceReshapeInfo.reshaperModelMaxBinIdx < (PIC_CODE_CW_BINS - 1)));
   for (i = 0; i < lutSize; i++)
   {
     if (clipRange) outputLUT[i] = Clip3((Pel)(16<<(m_lumaBD-8)), (Pel)(235<<(m_lumaBD-8)), outputLUT[i]);
     else           outputLUT[i] = Clip3((Pel)0, (Pel)((1<<m_lumaBD)-1), outputLUT[i]);
   }
+#endif
 }
 
 
@@ -217,6 +224,16 @@ void Reshape::constructReshaper()
 */
 void Reshape::updateChromaScaleLUT()
 {
+#if JVET_N0220_LMCS_SIMPLIFICATION
+  for (int i = 0; i < PIC_CODE_CW_BINS; i++)
+  {
+    uint16_t binCW = m_lumaBD > 10 ? (m_binCW[i] >> (m_lumaBD - 10)) : m_lumaBD < 10 ? (m_binCW[i] << (10 -m_lumaBD)): m_binCW[i];
+    if (binCW == 0)
+      m_chromaAdjHelpLUT[i] = 1 << CSCALE_FP_PREC;
+    else
+      m_chromaAdjHelpLUT[i] = m_initCW * (1 << CSCALE_FP_PREC) / binCW;
+  }
+#else
   const int16_t  CW_bin_SC_LUT[2 * PIC_ANALYZE_CW_BINS] = { 16384, 16384, 16384, 16384, 16384, 16384, 16384, 8192, 8192, 8192, 8192, 5461, 5461, 5461, 5461, 4096, 4096, 4096, 4096, 3277, 3277, 3277, 3277, 2731, 2731, 2731, 2731, 2341, 2341, 2341, 2048, 2048, 2048, 1820, 1820, 1820, 1638, 1638, 1638, 1638, 1489, 1489, 1489, 1489, 1365, 1365, 1365, 1365, 1260, 1260, 1260, 1260, 1170, 1170, 1170, 1170, 1092, 1092, 1092, 1092, 1024, 1024, 1024, 1024 }; //p=11
   for (int i = 0; i < PIC_CODE_CW_BINS; i++)
   {
@@ -226,6 +243,7 @@ void Reshape::updateChromaScaleLUT()
     else
       m_chromaAdjHelpLUT[i] = CW_bin_SC_LUT[Clip3((uint16_t)1, (uint16_t)64, (uint16_t)(binCW >> 1)) - 1];
   }
+#endif
 }
 
 
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index bc1804254..0e4511076 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -50,6 +50,8 @@
 #include <assert.h>
 #include <cassert>
 
+#define JVET_N0220_LMCS_SIMPLIFICATION                    1
+
 #define JCTVC_Y0038_PARAMS                                1
 
 #define JVET_MMVD_OFF_MACRO                               0
-- 
GitLab