From 3826e93d7658b5066ee1479cbb7b00baf7bcd552 Mon Sep 17 00:00:00 2001
From: Vadim Seregin <vseregin@qti.qualcomm.com>
Date: Thu, 15 Aug 2019 15:21:36 -0700
Subject: [PATCH] add chroma filters for picture upsampling

---
 source/Lib/CommonLib/InterpolationFilter.h | 10 ++++--
 source/Lib/CommonLib/Picture.cpp           | 42 +++++++---------------
 source/Lib/CommonLib/Picture.h             |  8 +++--
 source/Lib/CommonLib/Slice.cpp             |  2 +-
 source/Lib/EncoderLib/EncGOP.cpp           |  2 +-
 source/Lib/EncoderLib/EncLib.cpp           |  4 +--
 source/Lib/Utilities/VideoIOYuv.cpp        |  2 +-
 7 files changed, 31 insertions(+), 39 deletions(-)

diff --git a/source/Lib/CommonLib/InterpolationFilter.h b/source/Lib/CommonLib/InterpolationFilter.h
index 25ad67cd8..777a53aff 100644
--- a/source/Lib/CommonLib/InterpolationFilter.h
+++ b/source/Lib/CommonLib/InterpolationFilter.h
@@ -55,12 +55,18 @@
  */
 class InterpolationFilter
 {
-  static const TFilterCoeff m_lumaFilter4x4[LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_LUMA];
+  static const TFilterCoeff m_lumaFilter4x4[LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_LUMA];  
+#if JVET_O1164_RPR
+public:
+#endif
   static const TFilterCoeff m_lumaFilter[LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_LUMA]; ///< Luma filter taps
+  static const TFilterCoeff m_chromaFilter[CHROMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_CHROMA]; ///< Chroma filter taps
+#if JVET_O1164_RPR  
+private:
+#endif  
 #if JVET_O0057_ALTHPELIF
   static const TFilterCoeff m_lumaAltHpelIFilter[NTAPS_LUMA]; ///< Luma filter taps
 #endif
-  static const TFilterCoeff m_chromaFilter[CHROMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_CHROMA]; ///< Chroma filter taps
   static const TFilterCoeff m_bilinearFilter[LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_BILINEAR]; ///< bilinear filter taps
   static const TFilterCoeff m_bilinearFilterPrec4[LUMA_INTERPOLATION_FILTER_SUB_SAMPLE_POSITIONS][NTAPS_BILINEAR]; ///< bilinear filter taps
 public:
diff --git a/source/Lib/CommonLib/Picture.cpp b/source/Lib/CommonLib/Picture.cpp
index 41a977735..aefee96c0 100644
--- a/source/Lib/CommonLib/Picture.cpp
+++ b/source/Lib/CommonLib/Picture.cpp
@@ -1023,26 +1023,6 @@ void Picture::finishCtuPart( const UnitArea& ctuArea )
 #endif
 
 #if JVET_O1164_RPR
-const TFilterCoeff InterpolationFilterSRC[16][8] =
-{
-  {  0, 0,   0, 64,  0,   0,  0,  0 },
-  {  0, 1,  -3, 63,  4,  -2,  1,  0 },
-  { -1, 2,  -5, 62,  8,  -3,  1,  0 },
-  { -1, 3,  -8, 60, 13,  -4,  1,  0 },
-  { -1, 4, -10, 58, 17,  -5,  1,  0 },
-  { -1, 4, -11, 52, 26,  -8,  3, -1 },
-  { -1, 3,  -9, 47, 31, -10,  4, -1 },
-  { -1, 4, -11, 45, 34, -10,  4, -1 },
-  { -1, 4, -11, 40, 40, -11,  4, -1 },
-  { -1, 4, -10, 34, 45, -11,  4, -1 },
-  { -1, 4, -10, 31, 47,  -9,  3, -1 },
-  { -1, 3,  -8, 26, 52, -11,  4, -1 },
-  {  0, 1,  -5, 17, 58, -10,  4, -1 },
-  {  0, 1,  -4, 13, 60,  -8,  3, -1 },
-  {  0, 1,  -3,  8, 62,  -5,  2, -1 },
-  {  0, 1,  -2,  4, 63,  -3,  1,  0 }
-};
-
 const TFilterCoeff DownsamplingFilterSRC[8][16][12] =
 {
     { // D = 1
@@ -1195,7 +1175,7 @@ const TFilterCoeff DownsamplingFilterSRC[8][16][12] =
     }
 };
 
-void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType orgHeight, SizeType orgStride, Pel* scaledSrc, SizeType scaledWidth, SizeType scaledHeight, SizeType scaledStride, int bitDepth, const bool downsampling )
+void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType orgHeight, SizeType orgStride, Pel* scaledSrc, SizeType scaledWidth, SizeType scaledHeight, SizeType scaledStride, const int bitDepth, const bool useLumaFilter, const bool downsampling )
 {
   if( orgWidth == scaledWidth && orgHeight == scaledHeight )
   {
@@ -1207,8 +1187,9 @@ void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType org
     return;
   }
 
-  const TFilterCoeff* filterHor = &InterpolationFilterSRC[0][0];
-  const TFilterCoeff* filterVer = &InterpolationFilterSRC[0][0];
+  const TFilterCoeff* filterHor = useLumaFilter ? &InterpolationFilter::m_lumaFilter[0][0] : &InterpolationFilter::m_chromaFilter[0][0];
+  const TFilterCoeff* filterVer = useLumaFilter ? &InterpolationFilter::m_lumaFilter[0][0] : &InterpolationFilter::m_chromaFilter[0][0];
+  const int numFracPositions = useLumaFilter ? 15 : 31;
 
   if( downsampling )
   {
@@ -1234,7 +1215,7 @@ void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType org
     filterVer = &DownsamplingFilterSRC[verFilter][0][0];
   }
 
-  const int filerLength = downsampling ? 12 : 8;
+  const int filerLength = downsampling ? 12 : ( useLumaFilter ? NTAPS_LUMA : NTAPS_CHROMA );
   const int log2Norm = downsampling ? 14 : 12;
 
   int *buf = new int[orgHeight * scaledWidth];
@@ -1246,7 +1227,7 @@ void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType org
   {
     const Pel* org = orgSrc;
     int integer = ( i * orgWidth ) / scaledWidth;
-    int frac = ( ( i * orgWidth << 4 ) / scaledWidth ) & 15;
+    int frac = ( ( i * orgWidth << 4 ) / scaledWidth ) & numFracPositions;
 
     int* tmp = buf + i;
 
@@ -1273,7 +1254,7 @@ void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType org
   for( int j = 0; j < scaledHeight; j++ )
   {
     int integer = ( j * orgHeight ) / scaledHeight;
-    int frac = ( ( j * orgHeight << 4 ) / scaledHeight ) & 15;
+    int frac = ( ( j * orgHeight << 4 ) / scaledHeight ) & numFracPositions;
 
     for( int i = 0; i < scaledWidth; i++ )
     {
@@ -1296,14 +1277,15 @@ void Picture::sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType org
   delete[] buf;
 }
 
-void Picture::rescalePicture( const CPelUnitBuf& beforeScaling, const PelUnitBuf& afterScaling, const ChromaFormat chromaFormatIDC, const BitDepths& bitDepths, const bool downsampling )
+void Picture::rescalePicture( const CPelUnitBuf& beforeScaling, const PelUnitBuf& afterScaling, const ChromaFormat chromaFormatIDC, const BitDepths& bitDepths, const bool useLumaFilter, const bool downsampling )
 {
   for( int comp = 0; comp < ::getNumberValidComponents( chromaFormatIDC ); comp++ )
   {
-    const CPelBuf& beforeScale = beforeScaling.get( ComponentID( comp ) );
-    const PelBuf& afterScale = afterScaling.get( ComponentID( comp ) );
+    ComponentID compID = ComponentID( comp );
+    const CPelBuf& beforeScale = beforeScaling.get( compID );
+    const PelBuf& afterScale = afterScaling.get( compID );
 
-    Picture::sampleRateConv( beforeScale.buf, beforeScale.width, beforeScale.height, beforeScale.stride, afterScale.buf, afterScale.width, afterScale.height, afterScale.stride, bitDepths.recon[comp], downsampling );
+    Picture::sampleRateConv( beforeScale.buf, beforeScale.width, beforeScale.height, beforeScale.stride, afterScale.buf, afterScale.width, afterScale.height, afterScale.stride, bitDepths.recon[comp], downsampling || useLumaFilter ? true : isLuma(compID), downsampling );
   }
 }
 #endif
diff --git a/source/Lib/CommonLib/Picture.h b/source/Lib/CommonLib/Picture.h
index 34680f305..ca051557d 100644
--- a/source/Lib/CommonLib/Picture.h
+++ b/source/Lib/CommonLib/Picture.h
@@ -50,6 +50,10 @@
 #include "MCTS.h"
 #include <deque>
 
+#if JVET_O1164_RPR
+#include "CommonLib/InterpolationFilter.h"
+#endif
+
 #if ENABLE_WPP_PARALLELISM || ENABLE_SPLIT_PARALLELISM
 #if ENABLE_WPP_PARALLELISM
 #include <mutex>
@@ -244,9 +248,9 @@ struct Picture : public UnitArea
   void          createSpliceIdx(int nums);
   bool          getSpliceFull();
 #if JVET_O1164_RPR
-  static void   sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType orgHeight, SizeType orgStride, Pel* scaledSrc, SizeType scaledWidth, SizeType scaledHeight, SizeType scaledStride, int bitDepth, const bool downsampling = false );
+  static void   sampleRateConv( const Pel* orgSrc, SizeType orgWidth, SizeType orgHeight, SizeType orgStride, Pel* scaledSrc, SizeType scaledWidth, SizeType scaledHeight, SizeType scaledStride, const int bitDepth, const bool useLumaFilter, const bool downsampling = false );
 
-  static void rescalePicture( const CPelUnitBuf& beforeScaling, const PelUnitBuf& afterScaling, const ChromaFormat chromaFormatIDC, const BitDepths& bitDepths, const bool downsampling = false );
+  static void   rescalePicture( const CPelUnitBuf& beforeScaling, const PelUnitBuf& afterScaling, const ChromaFormat chromaFormatIDC, const BitDepths& bitDepths, const bool useLumaFilter, const bool downsampling = false );
 #endif
 
 public:
diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index 559b630f9..872027100 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -2364,7 +2364,7 @@ void Slice::scaleRefPicList( Picture *scaledRefPic[], APS** apss, APS& lmcsAps,
           scaledRefPic[j]->poc = poc;
 
           // rescale the reference picture
-          Picture::rescalePicture( m_apcRefPicList[refList][rIdx]->getRecoBuf(), scaledRefPic[j]->getRecoBuf(), sps->getChromaFormatIdc(), sps->getBitDepths() );
+          Picture::rescalePicture( m_apcRefPicList[refList][rIdx]->getRecoBuf(), scaledRefPic[j]->getRecoBuf(), sps->getChromaFormatIdc(), sps->getBitDepths(), true );
           scaledRefPic[j]->extendPicBorder();
 
           m_scaledRefPicList[refList][rIdx] = scaledRefPic[j];
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 62f4f725c..231161abc 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -3598,7 +3598,7 @@ void EncGOP::xCalculateAddPSNR(Picture* pcPic, PelUnitBuf cPicD, const AccessUni
   {
     const CPelBuf& upscaledOrg = sps.getUseReshaper() ? pcPic->m_bufs[PIC_TRUE_ORIGINAL_INPUT].get( COMPONENT_Y ) : pcPic->m_bufs[PIC_ORIGINAL_INPUT].get( COMPONENT_Y );
     upscaledRec.create( pic.chromaFormat, Area( Position(), upscaledOrg ) );
-    Picture::rescalePicture( picC, upscaledRec, format, sps.getBitDepths() );
+    Picture::rescalePicture( picC, upscaledRec, format, sps.getBitDepths(), false );
   }
 #endif
 
diff --git a/source/Lib/EncoderLib/EncLib.cpp b/source/Lib/EncoderLib/EncLib.cpp
index 95eba732e..88528b12d 100644
--- a/source/Lib/EncoderLib/EncLib.cpp
+++ b/source/Lib/EncoderLib/EncLib.cpp
@@ -698,8 +698,8 @@ void EncLib::encode( bool flush, PelStorage* pcPicYuvOrg, PelStorage* cPicYuvTru
 
         const ChromaFormat chromaFormatIDC = pSPS->getChromaFormatIdc();
 
-        Picture::rescalePicture( *pcPicYuvOrg, pcPicCurr->getOrigBuf(), chromaFormatIDC, pSPS->getBitDepths(), true );
-        Picture::rescalePicture( *cPicYuvTrueOrg, pcPicCurr->getTrueOrigBuf(), chromaFormatIDC, pSPS->getBitDepths(), true );
+        Picture::rescalePicture( *pcPicYuvOrg, pcPicCurr->getOrigBuf(), chromaFormatIDC, pSPS->getBitDepths(), true, true );
+        Picture::rescalePicture( *cPicYuvTrueOrg, pcPicCurr->getTrueOrigBuf(), chromaFormatIDC, pSPS->getBitDepths(), true, true );
       }
       else
       {
diff --git a/source/Lib/Utilities/VideoIOYuv.cpp b/source/Lib/Utilities/VideoIOYuv.cpp
index 86d0d7e44..9b0dcc1e4 100644
--- a/source/Lib/Utilities/VideoIOYuv.cpp
+++ b/source/Lib/Utilities/VideoIOYuv.cpp
@@ -1249,7 +1249,7 @@ bool VideoIOYuv::writeUpscaledPicture( const SPS& sps, const PPS& pps, const CPe
     {
       PelStorage upscaledPic;
       upscaledPic.create( chromaFormatIDC, Area( Position(), Size( sps.getMaxPicWidthInLumaSamples(), sps.getMaxPicHeightInLumaSamples() ) ) );
-      Picture::rescalePicture( pic, upscaledPic, chromaFormatIDC, sps.getBitDepths() );
+      Picture::rescalePicture( pic, upscaledPic, chromaFormatIDC, sps.getBitDepths(), false );
       const Window conf;
 
       ret = write( sps.getMaxPicWidthInLumaSamples(), sps.getMaxPicHeightInLumaSamples(), upscaledPic,
-- 
GitLab