From d1bef3d06beb969fc054bffaa55997393125e5ee Mon Sep 17 00:00:00 2001
From: Vadim Seregin <vseregin@qti.qualcomm.com>
Date: Wed, 14 Sep 2022 03:48:56 +0000
Subject: [PATCH] Fix: Memory release and uninitialized variables

---
 source/Lib/CommonLib/DepQuant.cpp             |  5 ++++
 source/Lib/CommonLib/InterPrediction.cpp      | 27 ++++++++++++-------
 source/Lib/CommonLib/InterPrediction.h        |  4 +--
 source/Lib/CommonLib/SampleAdaptiveOffset.cpp |  5 +++-
 source/Lib/DecoderLib/VLCReader.cpp           |  1 +
 source/Lib/EncoderLib/EncGOP.cpp              |  1 +
 6 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/source/Lib/CommonLib/DepQuant.cpp b/source/Lib/CommonLib/DepQuant.cpp
index e2d683869..3ee9c10cf 100644
--- a/source/Lib/CommonLib/DepQuant.cpp
+++ b/source/Lib/CommonLib/DepQuant.cpp
@@ -1856,6 +1856,9 @@ namespace DQIntern
     CHECKD( tu.cs->sps->getSpsRangeExtension().getExtendedPrecisionProcessingFlag(), "ext precision is not supported" );
 #if SIGN_PREDICTION
     CoeffBuf signBuff = tu.getCoeffSigns(compID);
+#if JVET_Y0141_SIGN_PRED_IMPROVE
+    IdxBuf signScanIdxBuff = tu.getCoeffSignsScanIdx( compID );
+#endif    
     uint32_t uiWidth  = tu.blocks[compID].width;
     uint32_t uiHeight = tu.blocks[compID].height;
 
@@ -1864,6 +1867,7 @@ namespace DQIntern
       TCoeff *coeff = signBuff.buf;
 #if JVET_Y0141_SIGN_PRED_IMPROVE
       uint32_t spArea = tu.cs->sps->getSignPredArea();
+      unsigned int *coeffIdx = signScanIdxBuff.buf;
       uint32_t spWidth = std::min(uiWidth, spArea);
       uint32_t spHeight = std::min(uiHeight, spArea);
       CHECK(TrQuant::SIGN_PRED_BYPASS, "SIGN_PRED_BYPASS should be equal to 0");
@@ -1874,6 +1878,7 @@ namespace DQIntern
       {
 #if JVET_Y0141_SIGN_PRED_IMPROVE
         memset(coeff, 0, sizeof(TCoeff) * spWidth);
+        memset( coeffIdx, 0, sizeof( unsigned int ) * spWidth );
 #else
         coeff[0] = TrQuant::SIGN_PRED_BYPASS;
         coeff[1] = TrQuant::SIGN_PRED_BYPASS;
diff --git a/source/Lib/CommonLib/InterPrediction.cpp b/source/Lib/CommonLib/InterPrediction.cpp
index 26dad52b7..4497c1293 100644
--- a/source/Lib/CommonLib/InterPrediction.cpp
+++ b/source/Lib/CommonLib/InterPrediction.cpp
@@ -560,17 +560,23 @@ void InterPrediction::init( RdCost* pcRdCost, ChromaFormat chromaFormatIDC, cons
   {
     for (uint32_t tmplt = 0; tmplt < 2; tmplt++)
     {
-      m_acYuvCurAMLTemplate[tmplt][ch] = (Pel*)xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
-      m_acYuvRefAboveTemplate[tmplt][ch] = (Pel*)xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
-      m_acYuvRefLeftTemplate[tmplt][ch] = (Pel*)xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
-      m_acYuvRefAMLTemplate[tmplt][ch] = (Pel*)xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
+      if( m_acYuvCurAMLTemplate[tmplt][ch] == nullptr )
+      {
+        m_acYuvCurAMLTemplate[tmplt][ch]   = ( Pel* ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+        m_acYuvRefAboveTemplate[tmplt][ch] = ( Pel* ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+        m_acYuvRefLeftTemplate[tmplt][ch]  = ( Pel* ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+        m_acYuvRefAMLTemplate[tmplt][ch]   = ( Pel* ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+      }
     }
   }
 #if JVET_Z0056_GPM_SPLIT_MODE_REORDERING
   for (uint32_t tmplt = 0; tmplt < 2 + 2; tmplt++)
   {
-    m_acYuvRefAMLTemplatePart0[tmplt] = (Pel*)xMalloc(Pel, GEO_MAX_CU_SIZE * GEO_MODE_SEL_TM_SIZE);
-    m_acYuvRefAMLTemplatePart1[tmplt] = (Pel*)xMalloc(Pel, GEO_MAX_CU_SIZE * GEO_MODE_SEL_TM_SIZE);
+    if( m_acYuvRefAMLTemplatePart0[tmplt] == nullptr )
+    {
+      m_acYuvRefAMLTemplatePart0[tmplt] = ( Pel* ) xMalloc( Pel, GEO_MAX_CU_SIZE * GEO_MODE_SEL_TM_SIZE );
+      m_acYuvRefAMLTemplatePart1[tmplt] = ( Pel* ) xMalloc( Pel, GEO_MAX_CU_SIZE * GEO_MODE_SEL_TM_SIZE );
+    }
   }
 #endif
 #endif
@@ -579,9 +585,12 @@ void InterPrediction::init( RdCost* pcRdCost, ChromaFormat chromaFormatIDC, cons
   {
     for (uint32_t tmplt = 0; tmplt < 2; tmplt++)
     {
-      m_acYuvRefAboveTemplateOBMC[tmplt][ch] = (Pel *) xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
-      m_acYuvRefLeftTemplateOBMC[tmplt][ch]  = (Pel *) xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
-      m_acYuvBlendTemplateOBMC[tmplt][ch]    = (Pel *) xMalloc(Pel, MAX_CU_SIZE * MAX_CU_SIZE);
+      if( m_acYuvRefAboveTemplateOBMC[tmplt][ch] == nullptr )
+      {
+        m_acYuvRefAboveTemplateOBMC[tmplt][ch] = ( Pel * ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+        m_acYuvRefLeftTemplateOBMC[tmplt][ch]  = ( Pel * ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+        m_acYuvBlendTemplateOBMC[tmplt][ch]    = ( Pel * ) xMalloc( Pel, MAX_CU_SIZE * MAX_CU_SIZE );
+      }
     }
   }
 #endif
diff --git a/source/Lib/CommonLib/InterPrediction.h b/source/Lib/CommonLib/InterPrediction.h
index 59434fea3..90191b11a 100644
--- a/source/Lib/CommonLib/InterPrediction.h
+++ b/source/Lib/CommonLib/InterPrediction.h
@@ -325,8 +325,6 @@ protected:
 #if !BDOF_RM_CONSTRAINTS
   void xSubPuBio(PredictionUnit& pu, PelUnitBuf& predBuf, const RefPicList &eRefPicList = REF_PIC_LIST_X, PelUnitBuf* yuvDstTmp = NULL);
 #endif
-  void destroy();
-
 
   MotionInfo      m_SubPuMiBuf[(MAX_CU_SIZE * MAX_CU_SIZE) >> (MIN_CU_LOG2 << 1)];
 #if JVET_W0090_ARMC_TM || JVET_Z0056_GPM_SPLIT_MODE_REORDERING || JVET_Z0061_TM_OBMC || JVET_AA0061_IBC_MBVD
@@ -375,6 +373,8 @@ public:
   void    init                (RdCost* pcRdCost, ChromaFormat chromaFormatIDC, const int ctuSize);
 #endif
 
+  void destroy();
+
 #if JVET_Z0054_BLK_REF_PIC_REORDER
   void     setUniRefIdxLC(PredictionUnit &pu);
   void     setUniRefListAndIdx(PredictionUnit &pu);
diff --git a/source/Lib/CommonLib/SampleAdaptiveOffset.cpp b/source/Lib/CommonLib/SampleAdaptiveOffset.cpp
index 1793ba099..ca64c5d37 100644
--- a/source/Lib/CommonLib/SampleAdaptiveOffset.cpp
+++ b/source/Lib/CommonLib/SampleAdaptiveOffset.cpp
@@ -163,7 +163,10 @@ void SampleAdaptiveOffset::create( int picWidth, int picHeight, ChromaFormat for
 
   for (int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++)
   {
-    m_ccSaoControl[compIdx] = new uint8_t[m_numCTUsInPic];
+    if( m_ccSaoControl[compIdx] == nullptr )
+    {
+      m_ccSaoControl[compIdx] = new uint8_t[m_numCTUsInPic];
+    }
     ::memset(m_ccSaoControl[compIdx], 0, sizeof(uint8_t) * m_numCTUsInPic);
   }
 #endif
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index eaa5b2924..176452f11 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -286,6 +286,7 @@ HLSyntaxReader::HLSyntaxReader()
 {
 #if JVET_Z0118_GDR
   m_lastGdrPoc = -1;
+  m_lastGdrRecoveryPocCnt = -1;
 #endif
 }
 
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 5c5164467..5c8ec9d1e 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -4241,6 +4241,7 @@ void EncGOP::compressGOP(int iPOCLast, int iNumPicRcvd, PicList &rcListPic, std:
     m_pcFrameMcPadPrediction->init(m_pcEncLib->getRdCost(), pcSlice->getSPS()->getChromaFormatIdc(),
                                    pcSlice->getSPS()->getMaxCUHeight(), NULL, pcPic->getPicWidthInLumaSamples());
     m_pcFrameMcPadPrediction->mcFramePad(pcPic, *(pcPic->slices[0]));
+    m_pcFrameMcPadPrediction->destroy();
 #endif
   } // iGOPid-loop
 
-- 
GitLab