From 8ef20c57373d56b3e8c1bae57ed9a898215ed1f8 Mon Sep 17 00:00:00 2001
From: Xiaozhong Xu <xiaozhongxu@tencent.com>
Date: Wed, 3 Apr 2019 22:32:51 -0400
Subject: [PATCH] re-arrange IBC search range for small CTU sizes

---
 source/Lib/CommonLib/TypeDef.h     | 2 ++
 source/Lib/CommonLib/UnitTools.cpp | 9 +++++++++
 2 files changed, 11 insertions(+)

diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index dc4a2bb99..f65b687a8 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -52,6 +52,8 @@
 
 #define JVET_N0462_FIX_CTX_MODELING                       1 // Fix context modeling of inter_pred_idc
 
+#define JVET_N0175_N0251_N0384_IBC_SMALL_CTU              1 // IBC search range arrangement for small CTU sizes
+
 #define JVET_N0286_SIMPLIFIED_GBI_IDX                     1 // Simplified coding of the GBi index
 
 #define JVET_N600_AMVR_TPM_CTX_REDUCTION                  1
diff --git a/source/Lib/CommonLib/UnitTools.cpp b/source/Lib/CommonLib/UnitTools.cpp
index 4ecabb63e..d6148aedd 100644
--- a/source/Lib/CommonLib/UnitTools.cpp
+++ b/source/Lib/CommonLib/UnitTools.cpp
@@ -1925,12 +1925,21 @@ bool PU::isBlockVectorValid(PredictionUnit& pu, int xPos, int yPos, int width, i
   }
 
   // in the same CTU line
+#if JVET_N0175_N0251_N0384_IBC_SMALL_CTU
+  int numLeftCTUs = (1 << ((7 - ctuSizeLog2) << 1)) - ((ctuSizeLog2 < 7) ? 1 : 0);
+  if ((refRightX >> ctuSizeLog2 <= xPos >> ctuSizeLog2) && (refLeftX >> ctuSizeLog2 >= (xPos >> ctuSizeLog2) - numLeftCTUs))
+#else
   if ((refRightX >> ctuSizeLog2 <= xPos >> ctuSizeLog2) && (refLeftX >> ctuSizeLog2 >= (xPos >> ctuSizeLog2) - 1))
+#endif
   {
 
     // in the same CTU, or left CTU
     // if part of ref block is in the left CTU, some area can be referred from the not-yet updated local CTU buffer
+#if JVET_N0175_N0251_N0384_IBC_SMALL_CTU
+    if (((refLeftX >> ctuSizeLog2) == ((xPos >> ctuSizeLog2) - 1)) && (ctuSizeLog2 == 7))
+#else
     if ((refLeftX >> ctuSizeLog2) == ((xPos >> ctuSizeLog2) - 1))
+#endif
     {
       // ref block's collocated block in current CTU
       const Position refPosCol = pu.Y().topLeft().offset(xBv + ctuSize, yBv);
-- 
GitLab