From e9300d39033a26ef7b2e09ffed497c8761550b0d Mon Sep 17 00:00:00 2001
From: Jane Zhao <jie.zhao@lge.com>
Date: Fri, 22 Feb 2019 10:54:04 -0800
Subject: [PATCH] Reshaper High bitdepth bug fix

---
 source/App/EncoderApp/EncAppCfg.cpp  |  6 ++++++
 source/Lib/CommonLib/Reshape.cpp     |  7 +++++--
 source/Lib/CommonLib/TypeDef.h       |  2 ++
 source/Lib/EncoderLib/EncReshape.cpp | 12 ++++++++++++
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/source/App/EncoderApp/EncAppCfg.cpp b/source/App/EncoderApp/EncAppCfg.cpp
index eb18b7e..75d476c 100644
--- a/source/App/EncoderApp/EncAppCfg.cpp
+++ b/source/App/EncoderApp/EncAppCfg.cpp
@@ -1570,6 +1570,9 @@ bool EncAppCfg::parseCfg( int argc, char* argv[] )
   {
     m_chromaFormatConstraint = (tmpConstraintChromaFormat == 0) ? m_chromaFormatIDC : numberToChromaFormat(tmpConstraintChromaFormat);
     m_bitDepthConstraint     = ( ( m_profile == Profile::MAIN10 || m_profile == Profile::NEXT ) ? 10 : 8 );
+#if LGE_TEST_HIGH_BITDEPTH
+    if (m_profile == Profile::NEXT) { m_bitDepthConstraint = 16; }
+#endif
   }
 
 
@@ -2094,7 +2097,10 @@ bool EncAppCfg::xCheckParameter()
   }
   else
   {
+#if !LGE_TEST_HIGH_BITDEPTH
     xConfirmPara(m_bitDepthConstraint!=((m_profile==Profile::MAIN10 || m_profile==Profile::NEXT)?10:8), "BitDepthConstraint must be 8 for MAIN profile and 10 for MAIN10 profile.");
+#endif
+
     xConfirmPara(m_chromaFormatConstraint!=CHROMA_420 && m_profile!=Profile::NEXT, "ChromaFormatConstraint must be 420 for non main-RExt and non-Next profiles.");
     xConfirmPara(m_intraConstraintFlag==true, "IntraConstraintFlag must be false for non main_RExt profiles.");
     xConfirmPara(m_lowerBitRateConstraintFlag==false, "LowerBitrateConstraintFlag must be true for non main-RExt profiles.");
diff --git a/source/Lib/CommonLib/Reshape.cpp b/source/Lib/CommonLib/Reshape.cpp
index 31ecca8..b88aefb 100644
--- a/source/Lib/CommonLib/Reshape.cpp
+++ b/source/Lib/CommonLib/Reshape.cpp
@@ -198,9 +198,12 @@ void Reshape::constructReshaper()
     int16_t Y2 = m_reshapePivot[i + 1];
 
     m_fwdLUT[i*pwlFwdBinLen] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)Y1);
-
+#if LGE_BUG_FIX
+    int log2PwlFwdBinLen = (pwlFwdBinLen>=MAX_CU_SIZE)? g_aucLog2[pwlFwdBinLen>> MAX_CU_DEPTH] + MAX_CU_DEPTH : g_aucLog2[pwlFwdBinLen];
+#else
     int log2PwlFwdBinLen = g_aucLog2[pwlFwdBinLen];
-
+#endif
+    
     int32_t scale = ((int32_t)(Y2 - Y1) * (1 << FP_PREC) + (1 << (log2PwlFwdBinLen - 1))) >> (log2PwlFwdBinLen);
     for (int j = 1; j < pwlFwdBinLen; j++)
     {
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index e4da03d..a70ef1f 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -50,6 +50,8 @@
 #include <assert.h>
 #include <cassert>
 
+#define LGE_TEST_HIGH_BITDEPTH                          1
+#define LGE_BUG_FIX                                     1
 #define JVET_M0055_DEBUG_CTU                              1 // DebugCTU encoder debug option
 
 #define JVET_M0297_32PT_MTS_ZERO_OUT                      1 // 32 point MTS based on skipping high frequency coefficients
diff --git a/source/Lib/EncoderLib/EncReshape.cpp b/source/Lib/EncoderLib/EncReshape.cpp
index ce7d76a..62c7ffb 100644
--- a/source/Lib/EncoderLib/EncReshape.cpp
+++ b/source/Lib/EncoderLib/EncReshape.cpp
@@ -1102,7 +1102,11 @@ void EncReshape::initLUTfromdQPModel()
     int16_t Y1 = m_reshapePivot[i];
     int16_t Y2 = m_reshapePivot[i + 1];
     m_fwdLUT[i*pwlFwdBinLen] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)Y1);
+#if LGE_BUG_FIX
+    int log2PwlFwdBinLen = (pwlFwdBinLen >= MAX_CU_SIZE) ? g_aucLog2[pwlFwdBinLen >> MAX_CU_DEPTH] + MAX_CU_DEPTH : g_aucLog2[pwlFwdBinLen];
+#else
     int log2PwlFwdBinLen = g_aucLog2[pwlFwdBinLen];
+#endif
     int32_t scale = ((int32_t)(Y2 - Y1) * (1 << FP_PREC) + (1 << (log2PwlFwdBinLen - 1))) >> (log2PwlFwdBinLen);
     for (int j = 1; j < pwlFwdBinLen; j++)
     {
@@ -1142,7 +1146,11 @@ void EncReshape::constructReshaperSDR()
   int totCW = bdShift != 0 ? (bdShift > 0 ? m_reshapeLUTSize / (1<<bdShift) : m_reshapeLUTSize * (1 << (-bdShift))) : m_reshapeLUTSize;
   int histBins = PIC_ANALYZE_CW_BINS;
   int histLenth = totCW/histBins;
+#if LGE_BUG_FIX
+  int log2HistLenth = (histLenth >= MAX_CU_SIZE) ? g_aucLog2[histLenth >> MAX_CU_DEPTH] + MAX_CU_DEPTH : g_aucLog2[histLenth];
+#else
   int log2HistLenth = g_aucLog2[histLenth];
+#endif
   int16_t *tempFwdLUT = new int16_t[m_reshapeLUTSize + 1]();
   int i, j;
   int cwScaleBins1, cwScaleBins2;
@@ -1271,7 +1279,11 @@ void EncReshape::constructReshaperSDR()
   m_sliceReshapeInfo.maxNbitsNeededDeltaCW = std::max(1, (int)g_aucLog2[maxAbsDeltaCW << 1]);
 
   histLenth = m_initCW;
+#if LGE_BUG_FIX
+  log2HistLenth = (histLenth >= MAX_CU_SIZE) ? g_aucLog2[histLenth >> MAX_CU_DEPTH] + MAX_CU_DEPTH : g_aucLog2[histLenth];
+#else
   log2HistLenth = g_aucLog2[histLenth];
+#endif
 
   int sumBins = 0;
   for (i = 0; i < PIC_CODE_CW_BINS; i++)   { sumBins += m_binCW[i];  }
-- 
2.18.0.windows.1

