From 00a6b4e042a0e954e91c368881b069362deb932d Mon Sep 17 00:00:00 2001
From: Nikolai Shostak <nikolai.shostak@vicuesoft.com>
Date: Wed, 27 Jul 2022 15:55:30 +0300
Subject: [PATCH] Use BitDepths stored inside Picture

Ticket #1563
---
 source/App/DecoderApp/DecApp.cpp | 25 ++++++++++++++-----------
 source/Lib/CommonLib/Picture.cpp |  1 +
 source/Lib/CommonLib/Picture.h   |  1 +
 source/Lib/DecoderLib/DecLib.cpp |  2 ++
 4 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/source/App/DecoderApp/DecApp.cpp b/source/App/DecoderApp/DecApp.cpp
index 477738a97..a0d065278 100644
--- a/source/App/DecoderApp/DecApp.cpp
+++ b/source/App/DecoderApp/DecApp.cpp
@@ -464,7 +464,7 @@ uint32_t DecApp::decode()
       // update file bitdepth shift if recon bitdepth changed between sequences
       for( uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++ )
       {
-        int reconBitdepth = pcListPic->front()->cs->sps->getBitDepth((ChannelType)channelType);
+        int reconBitdepth = pcListPic->front()->m_bitDepths[(ChannelType)channelType];
         int fileBitdepth  = m_cVideoIOYuvReconFile[nalu.m_nuhLayerId].getFileBitdepth(channelType);
         int bitdepthShift = m_cVideoIOYuvReconFile[nalu.m_nuhLayerId].getBitdepthShift(channelType);
         if( fileBitdepth + bitdepthShift != reconBitdepth )
@@ -475,7 +475,7 @@ uint32_t DecApp::decode()
 
       if (!m_SEIFGSFileName.empty() && !m_videoIOYuvSEIFGSFile[nalu.m_nuhLayerId].isOpen())
       {
-        const BitDepths &bitDepths = pcListPic->front()->cs->sps->getBitDepths();   // use bit depths of first reconstructed picture.
+        const BitDepths &bitDepths = pcListPic->front()->m_bitDepths;   // use bit depths of first reconstructed picture.
         for (uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
           if (m_outputBitDepth[channelType] == 0)
@@ -513,7 +513,7 @@ uint32_t DecApp::decode()
       {
         for (uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
-          int reconBitdepth = pcListPic->front()->cs->sps->getBitDepth((ChannelType) channelType);
+          int reconBitdepth = pcListPic->front()->m_bitDepths[(ChannelType)channelType];
           int fileBitdepth  = m_videoIOYuvSEIFGSFile[nalu.m_nuhLayerId].getFileBitdepth(channelType);
           int bitdepthShift = m_videoIOYuvSEIFGSFile[nalu.m_nuhLayerId].getBitdepthShift(channelType);
           if (fileBitdepth + bitdepthShift != reconBitdepth)
@@ -525,7 +525,7 @@ uint32_t DecApp::decode()
 
       if (!m_SEICTIFileName.empty() && !m_cVideoIOYuvSEICTIFile[nalu.m_nuhLayerId].isOpen())
       {
-        const BitDepths& bitDepths = pcListPic->front()->cs->sps->getBitDepths(); // use bit depths of first reconstructed picture.
+        const BitDepths& bitDepths = pcListPic->front()->m_bitDepths; // use bit depths of first reconstructed picture.
         for (uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
           if (m_outputBitDepth[channelType] == 0)
@@ -712,7 +712,7 @@ uint32_t DecApp::decode()
 
       if ((!m_shutterIntervalPostFileName.empty()) && (!openedPostFile) && getShutterFilterFlag())
       {
-        const BitDepths &bitDepths = pcListPic->front()->cs->sps->getBitDepths();
+        const BitDepths &bitDepths = pcListPic->front()->m_bitDepths;
         for (uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
           if (m_outputBitDepth[channelType] == 0)
@@ -933,21 +933,24 @@ void DecApp::xWriteOutput( PicList* pcListPic, uint32_t tId )
   PicList::iterator iterPic   = pcListPic->begin();
   int numPicsNotYetDisplayed = 0;
   int dpbFullness = 0;
-  const SPS* activeSPS = (pcListPic->front()->cs->sps);
   uint32_t maxNumReorderPicsHighestTid;
   uint32_t maxDecPicBufferingHighestTid;
-  uint32_t maxNrSublayers = activeSPS->getMaxTLayers();
-
   const VPS* referredVPS = pcListPic->front()->cs->vps;
-  const int temporalId = ( m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= maxNrSublayers ) ? maxNrSublayers - 1 : m_iMaxTemporalLayer;
 
   if( referredVPS == nullptr || referredVPS->m_numLayersInOls[referredVPS->m_targetOlsIdx] == 1 )
   {
+    const SPS* activeSPS = (pcListPic->front()->cs->sps);
+    const int temporalId = (m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= activeSPS->getMaxTLayers())
+      ? activeSPS->getMaxTLayers() - 1
+      : m_iMaxTemporalLayer;
     maxNumReorderPicsHighestTid = activeSPS->getMaxNumReorderPics( temporalId );
     maxDecPicBufferingHighestTid = activeSPS->getMaxDecPicBuffering( temporalId );
   }
   else
   {
+    const int temporalId = (m_iMaxTemporalLayer == -1 || m_iMaxTemporalLayer >= referredVPS->getMaxSubLayers())
+      ? referredVPS->getMaxSubLayers() - 1
+      : m_iMaxTemporalLayer;
     maxNumReorderPicsHighestTid = referredVPS->getMaxNumReorderPics( temporalId );
     maxDecPicBufferingHighestTid = referredVPS->getMaxDecPicBuffering( temporalId );
   }
@@ -1054,10 +1057,10 @@ void DecApp::xWriteOutput( PicList* pcListPic, uint32_t tId )
         if (!m_reconFileName.empty())
         {
           const Window &conf = pcPic->getConformanceWindow();
-          const SPS* sps = pcPic->cs->sps;
-          ChromaFormat chromaFormatIDC = sps->getChromaFormatIdc();
+          ChromaFormat chromaFormatIDC = pcPic->m_chromaFormatIDC;
           if( m_upscaledOutput )
           {
+            const SPS* sps = pcPic->cs->sps;
             m_cVideoIOYuvReconFile[pcPic->layerId].writeUpscaledPicture( *sps, *pcPic->cs->pps, pcPic->getRecoBuf(), m_outputColourSpaceConvert, m_packedYUVMode, m_upscaledOutput, NUM_CHROMA_FORMAT, m_bClipOutputVideoToRec709Range );
           }
           else
diff --git a/source/Lib/CommonLib/Picture.cpp b/source/Lib/CommonLib/Picture.cpp
index 5ed255e89..28beae0dc 100644
--- a/source/Lib/CommonLib/Picture.cpp
+++ b/source/Lib/CommonLib/Picture.cpp
@@ -282,6 +282,7 @@ void Picture::finalInit( const VPS* vps, const SPS& sps, const PPS& pps, PicHead
   mixedNaluTypesInPicFlag = pps.getMixedNaluTypesInPicFlag();
   nonReferencePictureFlag = picHeader->getNonReferencePictureFlag();
   m_chromaFormatIDC = sps.getChromaFormatIdc();
+  m_bitDepths = sps.getBitDepths();
 
   if (m_spliceIdx == nullptr)
   {
diff --git a/source/Lib/CommonLib/Picture.h b/source/Lib/CommonLib/Picture.h
index 48db0b981..883899822 100644
--- a/source/Lib/CommonLib/Picture.h
+++ b/source/Lib/CommonLib/Picture.h
@@ -271,6 +271,7 @@ public:
   std::vector<AQpLayer*> aqlayer;
 
   ChromaFormat m_chromaFormatIDC;
+  BitDepths    m_bitDepths;
 
 #if !KEEP_PRED_AND_RESI_SIGNALS
 private:
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index f19d1146e..f6f11b936 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -1052,6 +1052,8 @@ void  DecLib::xCreateUnavailablePicture( const PPS *pps, const int iUnavailableP
   cFillPic->cs->vps = m_parameterSetManager.getVPS(sps->getVPSId());
   cFillPic->cs->create(cFillPic->cs->sps->getChromaFormatIdc(), Area(0, 0, cFillPic->cs->pps->getPicWidthInLumaSamples(), cFillPic->cs->pps->getPicHeightInLumaSamples()), true, (bool)(cFillPic->cs->sps->getPLTMode()));
   cFillPic->allocateNewSlice();
+  cFillPic->m_chromaFormatIDC = sps->getChromaFormatIdc();
+  cFillPic->m_bitDepths = sps->getBitDepths();
 
   cFillPic->slices[0]->initSlice();
 
-- 
GitLab