Make gm_get_motion_vector respect allow_high_precision_mv

This fixes a rare encode/decode mismatch due to inconsistent
rounding of NEARMV/NEARESTMV vs. ZEROMV motion vectors when both
global motion and ref-mv are enabled.

Change-Id: Ia2bbaf63020f5ce7762e027f9bf835fd96797bec
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 8ead4ac..7b24b32 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -4400,12 +4400,14 @@
     case ZEROMV:
 #if CONFIG_GLOBAL_MOTION
       this_mv[0].as_int =
-          gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[0]])
+          gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[0]],
+                               cpi->common.allow_high_precision_mv)
               .as_int;
       thismvcost += GLOBAL_MOTION_RATE(mbmi->ref_frame[0]);
       if (is_compound) {
         this_mv[1].as_int =
-            gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[1]])
+            gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[1]],
+                                 cpi->common.allow_high_precision_mv)
                 .as_int;
         thismvcost += GLOBAL_MOTION_RATE(mbmi->ref_frame[1]);
       }
@@ -5193,7 +5195,9 @@
 #endif  // CONFIG_EXT_INTER
 #if CONFIG_GLOBAL_MOTION
         frame_mv[ZEROMV][frame].as_int =
-            gm_get_motion_vector(&cm->global_motion[frame]).as_int;
+            gm_get_motion_vector(&cm->global_motion[frame],
+                                 cm->allow_high_precision_mv)
+                .as_int;
 #else   // CONFIG_GLOBAL_MOTION
         frame_mv[ZEROMV][frame].as_int = 0;
 #endif  // CONFIG_GLOBAL_MOTION
@@ -8649,7 +8653,9 @@
     frame_mv[NEWMV][ref_frame].as_int = INVALID_MV;
 #if CONFIG_GLOBAL_MOTION
     frame_mv[ZEROMV][ref_frame].as_int =
-        gm_get_motion_vector(&cm->global_motion[ref_frame]).as_int;
+        gm_get_motion_vector(&cm->global_motion[ref_frame],
+                             cm->allow_high_precision_mv)
+            .as_int;
 #else   // CONFIG_GLOBAL_MOTION
     frame_mv[ZEROMV][ref_frame].as_int = 0;
 #endif  // CONFIG_GLOBAL_MOTION
@@ -8675,10 +8681,13 @@
     if (mbmi_ext->ref_mv_count[ref_frame] < 2) {
       MV_REFERENCE_FRAME rf[2];
       av1_set_ref_frame(rf, ref_frame);
-      if (mbmi_ext->ref_mvs[rf[0]][0].as_int != 0 ||
-          mbmi_ext->ref_mvs[rf[0]][1].as_int != 0 ||
-          mbmi_ext->ref_mvs[rf[1]][0].as_int != 0 ||
-          mbmi_ext->ref_mvs[rf[1]][1].as_int != 0)
+      if (mbmi_ext->ref_mvs[rf[0]][0].as_int !=
+              frame_mv[ZEROMV][rf[0]].as_int ||
+          mbmi_ext->ref_mvs[rf[0]][1].as_int !=
+              frame_mv[ZEROMV][rf[0]].as_int ||
+          mbmi_ext->ref_mvs[rf[1]][0].as_int !=
+              frame_mv[ZEROMV][rf[1]].as_int ||
+          mbmi_ext->ref_mvs[rf[1]][1].as_int != frame_mv[ZEROMV][rf[1]].as_int)
         mbmi_ext->mode_context[ref_frame] &= ~(1 << ALL_ZERO_FLAG_OFFSET);
     }
   }
@@ -8751,8 +8760,9 @@
       //               BWDREF_FRAME as well.
       mode_skip_mask[ALTREF_FRAME] = ~INTER_NEAREST_NEAR_ZERO;
 #if CONFIG_GLOBAL_MOTION
-      zeromv.as_int =
-          gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME]).as_int;
+      zeromv.as_int = gm_get_motion_vector(&cm->global_motion[ALTREF_FRAME],
+                                           cm->allow_high_precision_mv)
+                          .as_int;
 #else
       zeromv.as_int = 0;
 #endif  // CONFIG_GLOBAL_MOTION
@@ -9898,11 +9908,14 @@
     const uint8_t rf_type = av1_ref_frame_type(best_mbmode.ref_frame);
 #endif  // CONFIG_REF_MV
 #if CONFIG_GLOBAL_MOTION
-    zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]]).as_int;
-    zeromv[1].as_int =
-        comp_pred_mode
-            ? gm_get_motion_vector(&cm->global_motion[refs[1]]).as_int
-            : 0;
+    zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]],
+                                            cm->allow_high_precision_mv)
+                           .as_int;
+    zeromv[1].as_int = comp_pred_mode
+                           ? gm_get_motion_vector(&cm->global_motion[refs[1]],
+                                                  cm->allow_high_precision_mv)
+                                 .as_int
+                           : 0;
 #else
     zeromv[0].as_int = 0;
     zeromv[1].as_int = 0;
@@ -10033,27 +10046,36 @@
   }
 
 #if CONFIG_REF_MV
-  if (best_mbmode.ref_frame[0] > INTRA_FRAME && best_mbmode.mv[0].as_int == 0 &&
-#if CONFIG_EXT_INTER
-      (best_mbmode.ref_frame[1] <= INTRA_FRAME)
-#else
-      (best_mbmode.ref_frame[1] == NONE || best_mbmode.mv[1].as_int == 0)
-#endif  // CONFIG_EXT_INTER
-          ) {
+  {
     int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame);
     int16_t mode_ctx = mbmi_ext->mode_context[ref_frame_type];
-
     if (mode_ctx & (1 << ALL_ZERO_FLAG_OFFSET)) {
-      best_mbmode.mode = ZEROMV;
+      int_mv zeromv[2];
 #if CONFIG_GLOBAL_MOTION
-      best_mbmode.mv[0].as_int =
-          gm_get_motion_vector(&cm->global_motion[best_mbmode.ref_frame[0]])
-              .as_int;
-      if (best_mbmode.ref_frame[1] != NONE)
-        best_mbmode.mv[1].as_int =
-            gm_get_motion_vector(&cm->global_motion[best_mbmode.ref_frame[1]])
-                .as_int;
-#endif
+      const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
+                                           best_mbmode.ref_frame[1] };
+      zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[refs[0]],
+                                              cm->allow_high_precision_mv)
+                             .as_int;
+      zeromv[1].as_int = gm_get_motion_vector(&cm->global_motion[refs[1]],
+                                              cm->allow_high_precision_mv)
+                             .as_int;
+      lower_mv_precision(&zeromv[0].as_mv, cm->allow_high_precision_mv);
+      lower_mv_precision(&zeromv[1].as_mv, cm->allow_high_precision_mv);
+#else
+      zeromv[0].as_int = zeromv[1].as_int = 0;
+#endif  // CONFIG_GLOBAL_MOTION
+      if (best_mbmode.ref_frame[0] > INTRA_FRAME &&
+          best_mbmode.mv[0].as_int == zeromv[0].as_int &&
+#if CONFIG_EXT_INTER
+          (best_mbmode.ref_frame[1] <= INTRA_FRAME)
+#else
+          (best_mbmode.ref_frame[1] == NONE ||
+           best_mbmode.mv[1].as_int == zeromv[1].as_int)
+#endif  // CONFIG_EXT_INTER
+              ) {
+        best_mbmode.mode = ZEROMV;
+      }
     }
   }
 #endif
@@ -10170,7 +10192,9 @@
   mbmi->ref_frame[1] = NONE;
 #if CONFIG_GLOBAL_MOTION
   mbmi->mv[0].as_int =
-      gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]]).as_int;
+      gm_get_motion_vector(&cm->global_motion[mbmi->ref_frame[0]],
+                           cm->allow_high_precision_mv)
+          .as_int;
 #else   // CONFIG_GLOBAL_MOTION
   mbmi->mv[0].as_int = 0;
 #endif  // CONFIG_GLOBAL_MOTION