From 1cf8f041f5daad9018c99288c4eb6063112a1ba2 Mon Sep 17 00:00:00 2001
From: Vadim Seregin <vseregin@qti.qualcomm.com>
Date: Wed, 21 Aug 2019 14:39:21 -0700
Subject: [PATCH] reduce memory allocation

---
 source/Lib/CommonLib/Slice.cpp   | 44 ++++++++++++++++++--------------
 source/Lib/DecoderLib/DecLib.cpp |  2 +-
 source/Lib/EncoderLib/EncGOP.cpp |  2 +-
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index a9b183fef..f7fa29e11 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -2306,22 +2306,8 @@ void Slice::scaleRefPicList( Picture *scaledRefPic[], APS** apss, APS& lmcsAps,
     return;
   }
 
-  // the handling here mimics what happened in xGetNewPicBuffer()
-  for( i = 0; i < MAX_NUM_REF; i++ )
-  {
-    scaledRefPic[i] = new Picture;
-
-    scaledRefPic[i]->setBorderExtension( false );
-    scaledRefPic[i]->reconstructed = false;
-    scaledRefPic[i]->referenced = true;
-
-    scaledRefPic[i]->finalInit( *sps, *pps, apss, lmcsAps );
-
-    scaledRefPic[i]->poc = -1;
-
-    scaledRefPic[i]->create( sps->getChromaFormatIdc(), Size( pps->getPicWidthInLumaSamples(), pps->getPicHeightInLumaSamples() ), sps->getMaxCUWidth(), sps->getMaxCUWidth() + 16, isDecoder );
-  }
-
+  freeScaledRefPicList( scaledRefPic );
+  
   for( int refList = 0; refList < NUM_REF_PIC_LIST_01; refList++ )
   {
     if( refList == 1 && m_eSliceType != B_SLICE )
@@ -2343,7 +2329,7 @@ void Slice::scaleRefPicList( Picture *scaledRefPic[], APS** apss, APS& lmcsAps,
         // check whether the reference picture has already been scaled 
         for( i = 0; i < MAX_NUM_REF; i++ )
         {
-          if( scaledRefPic[i]->poc == poc )
+          if( scaledRefPic[i] != nullptr && scaledRefPic[i]->poc == poc )
           {
             break;
           }
@@ -2355,13 +2341,29 @@ void Slice::scaleRefPicList( Picture *scaledRefPic[], APS** apss, APS& lmcsAps,
           // search for unused Picture structure in scaledRefPic
           for( j = 0; j < MAX_NUM_REF; j++ )
           {
-            if( scaledRefPic[j]->poc == -1 )
+            if( scaledRefPic[j] == nullptr )
             {
               break;
             }
           }
+
           CHECK( j >= MAX_NUM_REF, "scaledRefPic can not hold all reference pictures!" );
 
+          if( scaledRefPic[j] == nullptr )
+          {
+            scaledRefPic[j] = new Picture;
+
+            scaledRefPic[j]->setBorderExtension( false );
+            scaledRefPic[j]->reconstructed = false;
+            scaledRefPic[j]->referenced = true;
+
+            scaledRefPic[j]->finalInit( *sps, *pps, apss, lmcsAps );
+
+            scaledRefPic[j]->poc = -1;
+
+            scaledRefPic[j]->create( sps->getChromaFormatIdc(), Size( pps->getPicWidthInLumaSamples(), pps->getPicHeightInLumaSamples() ), sps->getMaxCUWidth(), sps->getMaxCUWidth() + 16, isDecoder );
+          }
+
           scaledRefPic[j]->poc = poc;
           scaledRefPic[j]->longTerm = m_apcRefPicList[refList][rIdx]->longTerm;
 
@@ -2411,7 +2413,11 @@ void Slice::freeScaledRefPicList( Picture *scaledRefPic[] )
   }
   for( int i = 0; i < MAX_NUM_REF; i++ )
   {
-    scaledRefPic[i]->destroy();
+    if( scaledRefPic[i] != nullptr )
+    {
+      scaledRefPic[i]->destroy();
+      scaledRefPic[i] = nullptr;
+    }
   }
 }
 
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index 8e54d3cc0..4e8c8886d 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -1069,7 +1069,7 @@ bool DecLib::xDecodeSlice(InputNALUnit &nalu, int &iSkipFrame, int iPOCLastDispl
                                 // it is not associated with picture, sps or pps structures.
 
 #if JVET_O1164_RPR
-  Picture* scaledRefPic[MAX_NUM_REF];
+  Picture* scaledRefPic[MAX_NUM_REF] = {};
 #endif
 
   if (m_bFirstSliceInPicture)
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 095243d31..a6f992720 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -1779,7 +1779,7 @@ void EncGOP::compressGOP( int iPOCLast, int iNumPicRcvd, PicList& rcListPic,
   pcBitstreamRedirect = new OutputBitstream;
   AccessUnit::iterator  itLocationToPushSliceHeaderNALU; // used to store location where NALU containing slice header is to be inserted
 #if JVET_O1164_RPR
-  Picture* scaledRefPic[MAX_NUM_REF];
+  Picture* scaledRefPic[MAX_NUM_REF] = {};
 #endif
 
   xInitGOP(iPOCLast, iNumPicRcvd, isField
-- 
GitLab