Fix inconsistent use of ref_mv_idx.

"+ 1" has been added in a few locations to match the indices for the
context for the CDFs and the motion vectors.

Suggested fix (of removing +1s entirely) can then be implemented
without further affecting the overall behaviour of the encoder and
decoder.

BUG=aomedia:858

Change-Id: I650a618d3fda1d27bd3550d25ad1ac52e4365061
diff --git a/av1/common/mvref_common.h b/av1/common/mvref_common.h
index 750fbd5..79be754 100644
--- a/av1/common/mvref_common.h
+++ b/av1/common/mvref_common.h
@@ -280,7 +280,8 @@
 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 (ref_mv_stack[ref_mv_idx].weight >= REF_CAT_LEVEL && ref_mv_count > 0)
+  if (ref_mv_stack[ref_mv_idx].weight >= REF_CAT_LEVEL &&
+      ref_mv_idx < ref_mv_count)
     return ref_mv_stack[ref_mv_idx].pred_diff[ref];
 
   return 0;
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 8877623..e42eed4 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -1711,8 +1711,9 @@
     }
     case NEAR_NEWMV: {
       int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
-      int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
-                                xd->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
+      int nmv_ctx =
+          av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 1,
+                      mbmi->ref_mv_idx + 1);
       nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
       mv[0].as_int = near_mv[0].as_int;
       read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
@@ -1723,8 +1724,9 @@
     }
     case NEW_NEARMV: {
       int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
-      int nmv_ctx = av1_nmv_ctx(xd->ref_mv_count[rf_type],
-                                xd->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
+      int nmv_ctx =
+          av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], 0,
+                      mbmi->ref_mv_idx + 1);
       nmv_context *const nmvc = &ec_ctx->nmvc[nmv_ctx];
       read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
       assert(is_compound);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 8afb572..b96a0ca 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1453,18 +1453,18 @@
       }
     } else if (mode == NEAREST_NEWMV || mode == NEAR_NEWMV) {
       int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
-      int nmv_ctx =
-          av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
-                      mbmi_ext->ref_mv_stack[rf_type], 1, mbmi->ref_mv_idx);
+      int nmv_ctx = av1_nmv_ctx(
+          mbmi_ext->ref_mv_count[rf_type], mbmi_ext->ref_mv_stack[rf_type], 1,
+          mbmi->ref_mv_idx + (mode == NEAR_NEWMV ? 1 : 0));
       nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
       av1_encode_mv(cpi, w, &mbmi->mv[1].as_mv,
                     &mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0].as_mv, nmvc,
                     allow_hp);
     } else if (mode == NEW_NEARESTMV || mode == NEW_NEARMV) {
       int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
-      int nmv_ctx =
-          av1_nmv_ctx(mbmi_ext->ref_mv_count[rf_type],
-                      mbmi_ext->ref_mv_stack[rf_type], 0, mbmi->ref_mv_idx);
+      int nmv_ctx = av1_nmv_ctx(
+          mbmi_ext->ref_mv_count[rf_type], mbmi_ext->ref_mv_stack[rf_type], 0,
+          mbmi->ref_mv_idx + (mode == NEW_NEARMV ? 1 : 0));
       nmv_context *nmvc = &ec_ctx->nmvc[nmv_ctx];
       av1_encode_mv(cpi, w, &mbmi->mv[0].as_mv,
                     &mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0].as_mv, nmvc,
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 9bce3bf..8fff1dd 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -5980,7 +5980,9 @@
     best_mv->col >>= 3;
     best_mv->row >>= 3;
 
-    av1_set_mvcost(x, refs[id], id, mbmi->ref_mv_idx);
+    av1_set_mvcost(
+        x, refs[id], id,
+        mbmi->ref_mv_idx + (have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0));
 
     // Small-range full-pixel motion search.
     bestsme = av1_refining_search_8p_c(x, sadpb, search_range,
@@ -6039,7 +6041,9 @@
         xd->plane[i].pre[ref] = backup_yv12[ref][i];
     }
 
-    av1_set_mvcost(x, refs[ref], ref, mbmi->ref_mv_idx);
+    av1_set_mvcost(
+        x, refs[ref], ref,
+        mbmi->ref_mv_idx + (have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0));
 
     *rate_mv += av1_mv_bit_cost(&frame_mv[refs[ref]].as_mv,
                                 &x->mbmi_ext->ref_mvs[refs[ref]][0].as_mv,
@@ -6351,7 +6355,9 @@
 
   av1_set_mv_search_range(&x->mv_limits, &ref_mv);
 
-  av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
+  av1_set_mvcost(
+      x, ref, ref_idx,
+      mbmi->ref_mv_idx + (have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0));
 
   // Work out the size of the first step in the mv step search.
   // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc.
@@ -6680,7 +6686,9 @@
   best_mv->col >>= 3;
   best_mv->row >>= 3;
 
-  av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
+  av1_set_mvcost(
+      x, ref, ref_idx,
+      mbmi->ref_mv_idx + (have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0));
 
   // Small-range full-pixel motion search.
   bestsme = av1_refining_search_8p_c(x, sadpb, search_range,
@@ -6732,7 +6740,9 @@
       xd->plane[i].pre[ref_idx] = backup_yv12[i];
   }
 
-  av1_set_mvcost(x, ref, ref_idx, mbmi->ref_mv_idx);
+  av1_set_mvcost(
+      x, ref, ref_idx,
+      mbmi->ref_mv_idx + (have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0));
   *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, x->nmvjointcost,
                               x->mvcost, MV_COST_WEIGHT);
 }
@@ -7399,7 +7409,8 @@
         compound_single_motion_search_interinter(
             cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, 0, rate_mv, 0, 1);
       } else {
-        av1_set_mvcost(x, refs[1], 1, mbmi->ref_mv_idx);
+        av1_set_mvcost(x, refs[1], 1,
+                       mbmi->ref_mv_idx + (this_mode == NEAR_NEWMV ? 1 : 0));
         *rate_mv = av1_mv_bit_cost(&frame_mv[refs[1]].as_mv,
                                    &mbmi_ext->ref_mvs[refs[1]][0].as_mv,
                                    x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);
@@ -7413,7 +7424,8 @@
         compound_single_motion_search_interinter(
             cpi, x, bsize, frame_mv, mi_row, mi_col, NULL, 0, rate_mv, 0, 0);
       } else {
-        av1_set_mvcost(x, refs[0], 0, mbmi->ref_mv_idx);
+        av1_set_mvcost(x, refs[0], 0,
+                       mbmi->ref_mv_idx + (this_mode == NEW_NEARMV ? 1 : 0));
         *rate_mv = av1_mv_bit_cost(&frame_mv[refs[0]].as_mv,
                                    &mbmi_ext->ref_mvs[refs[0]][0].as_mv,
                                    x->nmvjointcost, x->mvcost, MV_COST_WEIGHT);