intrabc: modify default ref dv

Make sure the ref dv is a valid dv.

Improves compression by about 0.3% on screen_content keyframes.

Change-Id: I3a20c1a04b0ebcad610276f21b410dcfc8ba4c4d
diff --git a/av1/common/mvref_common.h b/av1/common/mvref_common.h
index 4b0f884..4fed25a 100644
--- a/av1/common/mvref_common.h
+++ b/av1/common/mvref_common.h
@@ -461,22 +461,25 @@
 #endif  // CONFIG_EXT_WARPED_MOTION
 
 #if CONFIG_INTRABC
-static INLINE void av1_find_ref_dv(int_mv *ref_dv, int mi_row, int mi_col) {
-  // TODO(aconverse@google.com): Handle tiles and such
-  (void)mi_col;
-  if (mi_row < MAX_MIB_SIZE) {
-    ref_dv->as_mv.row = 0;
-    ref_dv->as_mv.col = -MI_SIZE * MAX_MIB_SIZE;
-  } else {
-    ref_dv->as_mv.row = -MI_SIZE * MAX_MIB_SIZE;
-    ref_dv->as_mv.col = 0;
-  }
-}
-
 #define INTRABC_DELAY_PIXELS 256  //  Delay of 256 pixels
 #define INTRABC_DELAY_SB64 (INTRABC_DELAY_PIXELS / 64)
 #define USE_WAVE_FRONT 1  // Use only top left area of frame for reference.
 #define INTRABC_ROW_DELAY 8
+
+static INLINE void av1_find_ref_dv(int_mv *ref_dv, const TileInfo *const tile,
+                                   int mib_size, int mi_row, int mi_col) {
+  (void)mi_col;
+  if (mi_row - mib_size < tile->mi_row_start) {
+    ref_dv->as_mv.row = 0;
+    ref_dv->as_mv.col = -MI_SIZE * mib_size - INTRABC_DELAY_PIXELS;
+  } else {
+    ref_dv->as_mv.row = -MI_SIZE * mib_size;
+    ref_dv->as_mv.col = 0;
+  }
+  ref_dv->as_mv.row *= 8;
+  ref_dv->as_mv.col *= 8;
+}
+
 static INLINE int av1_is_dv_valid(const MV dv, const TileInfo *const tile,
                                   int mi_row, int mi_col, BLOCK_SIZE bsize,
                                   int mib_size_log2) {
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 52a5bcc..94e03a9 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -1090,7 +1090,8 @@
     av1_find_best_ref_mvs(0, ref_mvs, &nearestmv, &nearmv);
 #endif
     int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
-    if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
+    if (dv_ref.as_int == 0)
+      av1_find_ref_dv(&dv_ref, &xd->tile, cm->mib_size, mi_row, mi_col);
     // Ref DV should not have sub-pel.
     assert((dv_ref.as_mv.col & 7) == 0);
     assert((dv_ref.as_mv.row & 7) == 0);
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 144a9fc..a713bcf 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -8690,7 +8690,8 @@
 #endif
 
   int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
-  if (dv_ref.as_int == 0) av1_find_ref_dv(&dv_ref, mi_row, mi_col);
+  if (dv_ref.as_int == 0)
+    av1_find_ref_dv(&dv_ref, tile, cm->mib_size, mi_row, mi_col);
   // Ref DV should not have sub-pel.
   assert((dv_ref.as_mv.col & 7) == 0);
   assert((dv_ref.as_mv.row & 7) == 0);