Make global_motion work with ext_inter

Change-Id: I2a490e144099d7692296992528192c1f11d2c06f
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 1574422..a40fcf4 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -4990,25 +4990,22 @@
       if (is_compound)
         this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int;
       break;
-    case ZEROMV:
+    case ZEROMV: {
+      int ref;
+      for (ref = 0; ref < 1 + is_compound; ++ref) {
 #if CONFIG_GLOBAL_MOTION
-      this_mv[0].as_int =
-          gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[0]],
-                               cpi->common.allow_high_precision_mv)
-              .as_int;
-      thismvcost += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[0]);
-      if (is_compound) {
-        this_mv[1].as_int =
-            gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[1]],
-                                 cpi->common.allow_high_precision_mv)
+        this_mv[ref].as_int =
+            gm_get_motion_vector(
+                &cpi->common.global_motion[mbmi->ref_frame[ref]],
+                cpi->common.allow_high_precision_mv)
                 .as_int;
-        thismvcost += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[1]);
-      }
-#else   // CONFIG_GLOBAL_MOTION
-      this_mv[0].as_int = 0;
-      if (is_compound) this_mv[1].as_int = 0;
+        thismvcost += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[ref]);
+#else
+        this_mv[ref].as_int = 0;
 #endif  // CONFIG_GLOBAL_MOTION
+      }
       break;
+    }
 #if CONFIG_EXT_INTER
     case NEW_NEWMV:
       if (compound_seg_newmvs[0].as_int == INVALID_MV ||
@@ -5054,8 +5051,21 @@
       this_mv[1].as_int = frame_mv[mode][mbmi->ref_frame[1]].as_int;
       break;
     case ZERO_ZEROMV:
+#if CONFIG_GLOBAL_MOTION
+      this_mv[0].as_int =
+          gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[0]],
+                               cpi->common.allow_high_precision_mv)
+              .as_int;
+      this_mv[1].as_int =
+          gm_get_motion_vector(&cpi->common.global_motion[mbmi->ref_frame[1]],
+                               cpi->common.allow_high_precision_mv)
+              .as_int;
+      thismvcost += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[0]) +
+                    GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[1]);
+#else
       this_mv[0].as_int = 0;
       this_mv[1].as_int = 0;
+#endif  // CONFIG_GLOBAL_MOTION
       break;
 #endif  // CONFIG_EXT_INTER
     default: break;
@@ -5303,15 +5313,31 @@
 #endif  // CONFIG_REF_MV && CONFIG_EXT_INTER
     int_mv frame_mv[MB_MODE_COUNT][TOTAL_REFS_PER_FRAME], int this_mode,
     const MV_REFERENCE_FRAME ref_frames[2], const BLOCK_SIZE bsize, int block) {
-
+  int_mv zeromv[2];
+  int comp_pred_mode = ref_frames[1] > INTRA_FRAME;
+  int cur_frm;
+  for (cur_frm = 0; cur_frm < 1 + comp_pred_mode; cur_frm++) {
+#if CONFIG_GLOBAL_MOTION
+    if (this_mode == ZEROMV
+#if CONFIG_EXT_INTER
+        || this_mode == ZERO_ZEROMV
+#endif  // CONFIG_EXT_INTER
+        )
+      zeromv[cur_frm].as_int =
+          gm_get_motion_vector(&cpi->common.global_motion[ref_frames[cur_frm]],
+                               cpi->common.allow_high_precision_mv)
+              .as_int;
+    else
+#endif  // CONFIG_GLOBAL_MOTION
+      zeromv[cur_frm].as_int = 0;
+  }
 #if !CONFIG_EXT_INTER
   assert(ref_frames[1] != INTRA_FRAME);  // Just sanity check
 #endif
-
   if ((this_mode == NEARMV || this_mode == NEARESTMV || this_mode == ZEROMV) &&
-      frame_mv[this_mode][ref_frames[0]].as_int == 0 &&
+      frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int &&
       (ref_frames[1] <= INTRA_FRAME ||
-       frame_mv[this_mode][ref_frames[1]].as_int == 0)) {
+       frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int)) {
 #if CONFIG_REF_MV
     int16_t rfc =
         av1_mode_context_analyzer(mode_context, ref_frames, bsize, block);
@@ -5356,8 +5382,8 @@
   else if ((this_mode == NEAREST_NEARESTMV || this_mode == NEAREST_NEARMV ||
             this_mode == NEAR_NEARESTMV || this_mode == NEAR_NEARMV ||
             this_mode == ZERO_ZEROMV) &&
-           frame_mv[this_mode][ref_frames[0]].as_int == 0 &&
-           frame_mv[this_mode][ref_frames[1]].as_int == 0) {
+           frame_mv[this_mode][ref_frames[0]].as_int == zeromv[0].as_int &&
+           frame_mv[this_mode][ref_frames[1]].as_int == zeromv[1].as_int) {
 #if CONFIG_REF_MV
     int16_t rfc = compound_mode_context[ref_frames[0]];
     int c1 = cost_mv_ref(cpi, NEAREST_NEARMV, 1, rfc);
@@ -5788,7 +5814,14 @@
                               &ref_mvs_sub8x8[0][ref], &ref_mvs_sub8x8[1][ref]);
 
         if (has_second_rf) {
+#if CONFIG_GLOBAL_MOTION
+          frame_mv[ZERO_ZEROMV][frame].as_int =
+              gm_get_motion_vector(&cm->global_motion[frame],
+                                   cm->allow_high_precision_mv)
+                  .as_int;
+#else
           frame_mv[ZERO_ZEROMV][frame].as_int = 0;
+#endif  // CONFIG_GLOBAL_MOTION
           frame_mv[NEAREST_NEARESTMV][frame].as_int =
               frame_mv[NEARESTMV][frame].as_int;
 
@@ -8848,7 +8881,11 @@
       single_skippable[this_mode][refs[0]] = rd_stats->skip;
 #endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
 #if CONFIG_GLOBAL_MOTION
-    if (this_mode == ZEROMV) {
+    if (this_mode == ZEROMV
+#if CONFIG_EXT_INTER
+        || this_mode == ZERO_ZEROMV
+#endif  // CONFIG_EXT_INTER
+        ) {
       rd_stats->rate += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[0]);
       if (is_comp_pred)
         rd_stats->rate += GLOBAL_MOTION_RATE(cpi, mbmi->ref_frame[1]);
@@ -9496,7 +9533,14 @@
 #if CONFIG_EXT_INTER
     frame_mv[NEWFROMNEARMV][ref_frame].as_int = INVALID_MV;
     frame_mv[NEW_NEWMV][ref_frame].as_int = INVALID_MV;
+#if CONFIG_GLOBAL_MOTION
+    frame_mv[ZERO_ZEROMV][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[ZERO_ZEROMV][ref_frame].as_int = 0;
+#endif  // CONFIG_GLOBAL_MOTION
 #endif  // CONFIG_EXT_INTER
   }
 
@@ -10841,7 +10885,8 @@
       else if (nearmv[0].as_int == best_mbmode.mv[0].as_int &&
                nearmv[1].as_int == best_mbmode.mv[1].as_int)
         best_mbmode.mode = NEAR_NEARMV;
-      else if (best_mbmode.mv[0].as_int == 0 && best_mbmode.mv[1].as_int == 0)
+      else if (best_mbmode.mv[0].as_int == zeromv[0].as_int &&
+               best_mbmode.mv[1].as_int == zeromv[1].as_int)
         best_mbmode.mode = ZERO_ZEROMV;
 #else
         best_mbmode.mode = NEARESTMV;
@@ -10872,6 +10917,22 @@
         best_mbmode.mode = ZEROMV;
 #if CONFIG_EXT_INTER
     } else {
+      const MV_REFERENCE_FRAME refs[2] = { best_mbmode.ref_frame[0],
+                                           best_mbmode.ref_frame[1] };
+      int_mv zeromv[2];
+#if CONFIG_GLOBAL_MOTION
+      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;
+#endif  // CONFIG_GLOBAL_MOTION
       if (frame_mv[NEAREST_NEARESTMV][refs[0]].as_int ==
               best_mbmode.mv[0].as_int &&
           frame_mv[NEAREST_NEARESTMV][refs[1]].as_int ==
@@ -10892,7 +10953,8 @@
                frame_mv[NEAR_NEARMV][refs[1]].as_int ==
                    best_mbmode.mv[1].as_int)
         best_mbmode.mode = NEAR_NEARMV;
-      else if (best_mbmode.mv[0].as_int == 0 && best_mbmode.mv[1].as_int == 0)
+      else if (best_mbmode.mv[0].as_int == zeromv[0].as_int &&
+               best_mbmode.mv[1].as_int == zeromv[1].as_int)
         best_mbmode.mode = ZERO_ZEROMV;
     }
 #endif  // CONFIG_EXT_INTER