Modify the warped motion mode context

Modified the warped motion mode context based on neighbor's motion modes
and current block's mode.

Change-Id: I77ca35fab37ec640bb38661ff1799f643d5aafdc
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 4b18e9a..3b50a6b 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -325,9 +325,22 @@
                        xd->tile_ctx->obmc_cdf[mbmi->sb_type], 2);
       break;
     default:
+#if CONFIG_EXT_WARPED_MOTION
+    {
+      int wm_ctx = 0;
+      if (mbmi->wm_ctx != -1) {
+        wm_ctx = 1;
+        if (mbmi->mode == NEARESTMV) wm_ctx = 2;
+      }
+      aom_write_symbol(w, mbmi->motion_mode,
+                       xd->tile_ctx->motion_mode_cdf[wm_ctx][mbmi->sb_type],
+                       MOTION_MODES);
+    }
+#else
       aom_write_symbol(w, mbmi->motion_mode,
                        xd->tile_ctx->motion_mode_cdf[mbmi->sb_type],
                        MOTION_MODES);
+#endif  // CONFIG_EXT_WARPED_MOTION
   }
 }
 
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 139f332..8de0a78 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -275,7 +275,11 @@
   int interintra_cost[BLOCK_SIZE_GROUPS][2];
   int wedge_interintra_cost[BLOCK_SIZES_ALL][2];
   int interintra_mode_cost[BLOCK_SIZE_GROUPS][INTERINTRA_MODES];
+#if CONFIG_EXT_WARPED_MOTION
+  int motion_mode_cost[MOTION_MODE_CTX][BLOCK_SIZES_ALL][MOTION_MODES];
+#else
   int motion_mode_cost[BLOCK_SIZES_ALL][MOTION_MODES];
+#endif  // CONFIG_EXT_WARPED_MOTION
   int motion_mode_cost1[BLOCK_SIZES_ALL][2];
   int intra_uv_mode_cost[INTRA_MODES][UV_INTRA_MODES];
   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 0f4af1d..c476a92 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1233,10 +1233,23 @@
             motion_mode_allowed(0, xd->global_motion, xd, mi);
         if (mbmi->ref_frame[1] != INTRA_FRAME) {
           if (motion_allowed == WARPED_CAUSAL) {
+#if CONFIG_EXT_WARPED_MOTION
+            int wm_ctx = 0;
+            if (mbmi->wm_ctx != -1) {
+              wm_ctx = 1;
+              if (mbmi->mode == NEARESTMV) wm_ctx = 2;
+            }
+
+            counts->motion_mode[wm_ctx][mbmi->sb_type][mbmi->motion_mode]++;
+            if (allow_update_cdf)
+              update_cdf(fc->motion_mode_cdf[wm_ctx][mbmi->sb_type],
+                         mbmi->motion_mode, MOTION_MODES);
+#else
             counts->motion_mode[mbmi->sb_type][mbmi->motion_mode]++;
             if (allow_update_cdf)
               update_cdf(fc->motion_mode_cdf[mbmi->sb_type], mbmi->motion_mode,
                          MOTION_MODES);
+#endif  // CONFIG_EXT_WARPED_MOTION
           } else if (motion_allowed == OBMC_CAUSAL) {
             counts->obmc[mbmi->sb_type][mbmi->motion_mode == OBMC_CAUSAL]++;
             if (allow_update_cdf)
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 4e74799..f1b109b 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -262,10 +262,20 @@
       av1_cost_tokens_from_cdf(x->wedge_interintra_cost[i],
                                fc->wedge_interintra_cdf[i], NULL);
     }
+#if CONFIG_EXT_WARPED_MOTION
+    for (i = 0; i < MOTION_MODE_CTX; i++) {
+      for (j = BLOCK_8X8; j < BLOCK_SIZES_ALL; j++) {
+        av1_cost_tokens_from_cdf(x->motion_mode_cost[i][j],
+                                 fc->motion_mode_cdf[i][j], NULL);
+      }
+    }
+#else
     for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
       av1_cost_tokens_from_cdf(x->motion_mode_cost[i], fc->motion_mode_cdf[i],
                                NULL);
     }
+#endif  // CONFIG_EXT_WARPED_MOTION
+
     for (i = BLOCK_8X8; i < BLOCK_SIZES_ALL; i++) {
       av1_cost_tokens_from_cdf(x->motion_mode_cost1[i], fc->obmc_cdf[i], NULL);
     }
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 6573e0d..098f11a 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -7655,6 +7655,7 @@
   int pts0[SAMPLES_ARRAY_SIZE], pts_inref0[SAMPLES_ARRAY_SIZE];
   int pts_mv0[SAMPLES_ARRAY_SIZE], pts_wm[SAMPLES_ARRAY_SIZE];
   int total_samples;
+  int best_cand = -1;
 #else
   int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
 #endif  // CONFIG_EXT_WARPED_MOTION
@@ -7667,6 +7668,20 @@
   mbmi->num_proj_ref[0] =
       findSamples(cm, xd, mi_row, mi_col, pts0, pts_inref0, pts_mv0, pts_wm);
   total_samples = mbmi->num_proj_ref[0];
+
+  // Find a warped neighbor.
+  int cand;
+  int best_weight = 0;
+
+  // if (this_mode == NEARESTMV)
+  for (cand = 0; cand < mbmi->num_proj_ref[0]; cand++) {
+    if (pts_wm[cand * 2 + 1] > best_weight) {
+      best_weight = pts_wm[cand * 2 + 1];
+      best_cand = cand;
+    }
+  }
+  mbmi->wm_ctx = best_cand;
+  best_bmc_mbmi->wm_ctx = mbmi->wm_ctx;
 #else
   mbmi->num_proj_ref[0] = findSamples(cm, xd, mi_row, mi_col, pts, pts_inref);
 #endif  // CONFIG_EXT_WARPED_MOTION
@@ -7725,22 +7740,7 @@
           av1_unswitchable_filter(cm->interp_filter));
 
 #if CONFIG_EXT_WARPED_MOTION
-      // Find a warped neighbor.
-      int cand;
-      int best_cand = -1;
-      int best_weight = 0;
-
-      assert(this_mode >= NEARESTMV && this_mode <= NEWMV);
-      if (this_mode == NEARESTMV) {
-        for (cand = 0; cand < mbmi->num_proj_ref[0]; cand++) {
-          if (pts_wm[cand * 2 + 1] > best_weight) {
-            best_weight = pts_wm[cand * 2 + 1];
-            best_cand = cand;
-          }
-        }
-      }
-
-      if (best_cand != -1) {
+      if (this_mode == NEARESTMV && best_cand != -1) {
         MODE_INFO *best_mi = xd->mi[pts_wm[2 * best_cand]];
         assert(best_mi->mbmi.motion_mode == WARPED_CAUSAL);
         mbmi->wm_params[0] = best_mi->mbmi.wm_params[0];
@@ -7839,10 +7839,21 @@
     rd_stats->skip = 1;
     rd_stats->rate = tmp_rate2;
     if (last_motion_mode_allowed > SIMPLE_TRANSLATION) {
-      if (last_motion_mode_allowed == WARPED_CAUSAL)
+      if (last_motion_mode_allowed == WARPED_CAUSAL) {
+#if CONFIG_EXT_WARPED_MOTION
+        int wm_ctx = 0;
+        if (mbmi->wm_ctx != -1) {
+          wm_ctx = 1;
+          if (mbmi->mode == NEARESTMV) wm_ctx = 2;
+        }
+
+        rd_stats->rate += x->motion_mode_cost[wm_ctx][bsize][mbmi->motion_mode];
+#else
         rd_stats->rate += x->motion_mode_cost[bsize][mbmi->motion_mode];
-      else
+#endif  // CONFIG_EXT_WARPED_MOTION
+      } else {
         rd_stats->rate += x->motion_mode_cost1[bsize][mbmi->motion_mode];
+      }
     }
     if (mbmi->motion_mode == WARPED_CAUSAL) {
       rd_stats->rate -= rs;