Re-enable av1_nmv_ctx() when ext-inter is enabled

Currently, mbmi->ref_mv_idx can be set to a nonzero value
on the encoder side if mbmi->mode is one of NEARMV, NEWMV,
NEAR_NEARMV, or NEW_NEWMV. But it can only be nonzero on the
decoder side if the mode is NEARMV or NEWMV. Hence av1_nmv_ctx
has previously been disabled when ext-inter is enabled, to
prevent a mismatch due to this.

This patch changes the encoder behaviour to match the decoder
behaviour.

Change-Id: Icfe41fb72e76731ae373fe8c6065f5e003f6414f
diff --git a/av1/common/mvref_common.h b/av1/common/mvref_common.h
index eea2b8e..9baef8d 100644
--- a/av1/common/mvref_common.h
+++ b/av1/common/mvref_common.h
@@ -391,10 +391,6 @@
 static INLINE int av1_nmv_ctx(const uint8_t ref_mv_count,
                               const CANDIDATE_MV *ref_mv_stack, int ref,
                               int ref_mv_idx) {
-#if CONFIG_EXT_INTER
-  return 0;
-#endif
-
   if (ref_mv_stack[ref_mv_idx].weight >= REF_CAT_LEVEL && ref_mv_count > 0)
     return ref_mv_stack[ref_mv_idx].pred_diff[ref];
 
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 9252b00..9a3d3be 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1721,6 +1721,10 @@
 #if CONFIG_REF_MV
         if (mode == NEARMV || mode == NEWMV)
           write_drl_idx(cm, mbmi, mbmi_ext, w);
+#if CONFIG_EXT_INTER
+        else
+          assert(mbmi->ref_mv_idx == 0);
+#endif
 #endif
       }
     }
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index c5ef788..6ab7c02 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -2203,39 +2203,39 @@
                                   has_second_ref(mbmi),
 #endif  // CONFIG_EXT_INTER
                                   mode_ctx);
-
-          if (mode == NEWMV) {
-            uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
-            int idx;
-
-            for (idx = 0; idx < 2; ++idx) {
-              if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
-                uint8_t drl_ctx =
-                    av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
-                ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx];
-
-                if (mbmi->ref_mv_idx == idx) break;
-              }
-            }
-          }
-
-          if (mode == NEARMV) {
-            uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
-            int idx;
-
-            for (idx = 1; idx < 3; ++idx) {
-              if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
-                uint8_t drl_ctx =
-                    av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
-                ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx - 1];
-
-                if (mbmi->ref_mv_idx == idx - 1) break;
-              }
-            }
-          }
 #if CONFIG_EXT_INTER
         }
 #endif  // CONFIG_EXT_INTER
+
+        if (mode == NEWMV) {
+          uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+          int idx;
+
+          for (idx = 0; idx < 2; ++idx) {
+            if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+              uint8_t drl_ctx =
+                  av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
+              ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx];
+
+              if (mbmi->ref_mv_idx == idx) break;
+            }
+          }
+        }
+
+        if (mode == NEARMV) {
+          uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+          int idx;
+
+          for (idx = 1; idx < 3; ++idx) {
+            if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+              uint8_t drl_ctx =
+                  av1_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
+              ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx - 1];
+
+              if (mbmi->ref_mv_idx == idx - 1) break;
+            }
+          }
+        }
 #else
 #if CONFIG_EXT_INTER
         if (is_inter_compound_mode(mode))
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index e3843a5..3b06151 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -10489,18 +10489,11 @@
       }
 
 #if CONFIG_REF_MV
-// TODO(jingning): This needs some refactoring to improve code quality
-// and reduce redundant steps.
-#if CONFIG_EXT_INTER
-      if (((mbmi->mode == NEARMV || mbmi->mode == NEAR_NEARMV) &&
-           mbmi_ext->ref_mv_count[ref_frame_type] > 2) ||
-          ((mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) &&
-           mbmi_ext->ref_mv_count[ref_frame_type] > 1)) {
-#else
+      // TODO(jingning): This needs some refactoring to improve code quality
+      // and reduce redundant steps.
       if ((mbmi->mode == NEARMV &&
            mbmi_ext->ref_mv_count[ref_frame_type] > 2) ||
           (mbmi->mode == NEWMV && mbmi_ext->ref_mv_count[ref_frame_type] > 1)) {
-#endif  // CONFIG_EXT_INTER
         int_mv backup_mv = frame_mv[NEARMV][ref_frame];
         MB_MODE_INFO backup_mbmi = *mbmi;
         int backup_skip = x->skip;
@@ -11322,6 +11315,13 @@
 #endif  // CONFIG_REF_MV
   }
 
+  // Make sure that the ref_mv_idx is only nonzero when we're
+  // using a mode which can support ref_mv_idx
+  if (best_mbmode.ref_mv_idx != 0 &&
+      !(best_mbmode.mode == NEARMV || best_mbmode.mode == NEWMV)) {
+    best_mbmode.ref_mv_idx = 0;
+  }
+
 #if CONFIG_REF_MV
   {
     int8_t ref_frame_type = av1_ref_frame_type(best_mbmode.ref_frame);