From a4f2f9fe09eb1cb8dafe41246ef88ab244d3d843 Mon Sep 17 00:00:00 2001
From: Remy Foray <remy.foray@allegrodvt.com>
Date: Wed, 16 Sep 2020 14:24:02 +0200
Subject: [PATCH] Fix RPL construction: allow short-term to be marked as
 long-term (regression after !1868)

---
 source/Lib/CommonLib/Slice.cpp | 28 ++++++++++++++++++++++++++--
 source/Lib/CommonLib/Slice.h   |  1 +
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index 108d535e1..92194a481 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -421,6 +421,30 @@ Picture* Slice::xGetLongTermRefPic( PicList& rcListPic, const int poc, const boo
   return refPic;
 }
 
+Picture* Slice::xGetLongTermRefPicCandidate( PicList& rcListPic, const int poc, const bool pocHasMsb, const int layerId )
+{
+  // return a nullptr, if picture is not found (might be a short-term or a long-term)
+  Picture*  refPic = nullptr;
+  const int pocCycle = 1 << getSPS()->getBitsForPOC();
+
+  const int refPoc = pocHasMsb ? poc : (poc & (pocCycle - 1));
+
+  for ( auto &currPic : rcListPic )
+  {
+    if( currPic->getPOC() != this->getPOC() && currPic->referenced && currPic->layerId == layerId )
+    {
+      int currPicPoc = pocHasMsb ? currPic->getPOC() : (currPic->getPOC() & (pocCycle - 1));
+      if (refPoc == currPicPoc)
+      {
+        refPic = currPic;
+        break;
+      }
+    }
+  }
+
+  return refPic;
+}
+
 void Slice::setRefPOCList       ()
 {
   for (int iDir = 0; iDir < NUM_REF_PIC_LIST_01; iDir++)
@@ -496,7 +520,7 @@ void Slice::constructRefPicList(PicList& rcListPic)
       {
         ltrpPoc += getPOC() - m_localRPL0.getDeltaPocMSBCycleLT(ii) * (pocMask + 1) - (getPOC() & pocMask);
       }
-      pcRefPic = xGetLongTermRefPic( rcListPic, ltrpPoc, m_localRPL0.getDeltaPocMSBPresentFlag( ii ), m_pcPic->layerId );
+      pcRefPic = xGetLongTermRefPicCandidate( rcListPic, ltrpPoc, m_localRPL0.getDeltaPocMSBPresentFlag( ii ), m_pcPic->layerId );
       pcRefPic->longTerm = true;
     }
     pcRefPic->extendPicBorder( getPPS() );
@@ -536,7 +560,7 @@ void Slice::constructRefPicList(PicList& rcListPic)
       {
         ltrpPoc += getPOC() - m_localRPL1.getDeltaPocMSBCycleLT(ii) * (pocMask + 1) - (getPOC() & pocMask);
       }
-      pcRefPic = xGetLongTermRefPic( rcListPic, ltrpPoc, m_localRPL1.getDeltaPocMSBPresentFlag( ii ), m_pcPic->layerId );
+      pcRefPic = xGetLongTermRefPicCandidate( rcListPic, ltrpPoc, m_localRPL1.getDeltaPocMSBPresentFlag( ii ), m_pcPic->layerId );
       pcRefPic->longTerm = true;
     }
     pcRefPic->extendPicBorder( getPPS() );
diff --git a/source/Lib/CommonLib/Slice.h b/source/Lib/CommonLib/Slice.h
index 57a45f06e..84df6e134 100644
--- a/source/Lib/CommonLib/Slice.h
+++ b/source/Lib/CommonLib/Slice.h
@@ -3205,6 +3205,7 @@ public:
 protected:
   Picture*              xGetRefPic( PicList& rcListPic, const int poc, const int layerId );
   Picture*              xGetLongTermRefPic( PicList& rcListPic, const int poc, const bool pocHasMsb, const int layerId );
+  Picture*              xGetLongTermRefPicCandidate( PicList& rcListPic, const int poc, const bool pocHasMsb, const int layerId );
 public:
   std::unordered_map< Position, std::unordered_map< Size, double> > m_mapPltCost[2];
 private:
-- 
GitLab