Merge "Refactor transform type-size search function" into nextgenv2
diff --git a/vp10/common/entropymode.c b/vp10/common/entropymode.c
index 8afcbb8..407fe8c 100644
--- a/vp10/common/entropymode.c
+++ b/vp10/common/entropymode.c
@@ -186,7 +186,7 @@
 };
 
 static const vpx_prob default_drl_prob[DRL_MODE_CONTEXTS] = {
-    128, 128, 128,
+    128, 160, 180, 128, 160
 };
 
 #if CONFIG_EXT_INTER
@@ -1119,8 +1119,7 @@
   vp10_copy(fc->newmv_prob, default_newmv_prob);
   vp10_copy(fc->zeromv_prob, default_zeromv_prob);
   vp10_copy(fc->refmv_prob, default_refmv_prob);
-  vp10_copy(fc->drl_prob0, default_drl_prob);
-  vp10_copy(fc->drl_prob1, default_drl_prob);
+  vp10_copy(fc->drl_prob, default_drl_prob);
 #if CONFIG_EXT_INTER
   fc->new2mv_prob = default_new2mv_prob;
 #endif  // CONFIG_EXT_INTER
@@ -1204,12 +1203,8 @@
                                             counts->refmv_mode[i]);
 
   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    fc->drl_prob0[i] = mode_mv_merge_probs(pre_fc->drl_prob0[i],
-                                           counts->drl_mode0[i]);
-  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    fc->drl_prob1[i] = mode_mv_merge_probs(pre_fc->drl_prob1[i],
-                                           counts->drl_mode1[i]);
-
+    fc->drl_prob[i] = mode_mv_merge_probs(pre_fc->drl_prob[i],
+                                          counts->drl_mode[i]);
 #if CONFIG_EXT_INTER
   fc->new2mv_prob = mode_mv_merge_probs(pre_fc->new2mv_prob,
                                         counts->new2mv_mode);
diff --git a/vp10/common/entropymode.h b/vp10/common/entropymode.h
index 2443d60..ba93ea7 100644
--- a/vp10/common/entropymode.h
+++ b/vp10/common/entropymode.h
@@ -55,8 +55,7 @@
   vpx_prob newmv_prob[NEWMV_MODE_CONTEXTS];
   vpx_prob zeromv_prob[ZEROMV_MODE_CONTEXTS];
   vpx_prob refmv_prob[REFMV_MODE_CONTEXTS];
-  vpx_prob drl_prob0[DRL_MODE_CONTEXTS];
-  vpx_prob drl_prob1[DRL_MODE_CONTEXTS];
+  vpx_prob drl_prob[DRL_MODE_CONTEXTS];
 
 #if CONFIG_EXT_INTER
   vpx_prob new2mv_prob;
@@ -121,8 +120,7 @@
   unsigned int newmv_mode[NEWMV_MODE_CONTEXTS][2];
   unsigned int zeromv_mode[ZEROMV_MODE_CONTEXTS][2];
   unsigned int refmv_mode[REFMV_MODE_CONTEXTS][2];
-  unsigned int drl_mode0[DRL_MODE_CONTEXTS][2];
-  unsigned int drl_mode1[DRL_MODE_CONTEXTS][2];
+  unsigned int drl_mode[DRL_MODE_CONTEXTS][2];
 #if CONFIG_EXT_INTER
   unsigned int new2mv_mode[2];
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/common/enums.h b/vp10/common/enums.h
index 4a0e243..9160c5f 100644
--- a/vp10/common/enums.h
+++ b/vp10/common/enums.h
@@ -243,7 +243,7 @@
 #define NEWMV_MODE_CONTEXTS  7
 #define ZEROMV_MODE_CONTEXTS 2
 #define REFMV_MODE_CONTEXTS  9
-#define DRL_MODE_CONTEXTS    3
+#define DRL_MODE_CONTEXTS    5
 
 #define ZEROMV_OFFSET 3
 #define REFMV_OFFSET  4
diff --git a/vp10/common/mvref_common.c b/vp10/common/mvref_common.c
index c67beed..8d40bf2 100644
--- a/vp10/common/mvref_common.c
+++ b/vp10/common/mvref_common.c
@@ -749,6 +749,10 @@
 
 void vp10_append_sub8x8_mvs_for_idx(VP10_COMMON *cm, MACROBLOCKD *xd,
                                     int block, int ref, int mi_row, int mi_col,
+#if CONFIG_REF_MV
+                                    CANDIDATE_MV *ref_mv_stack,
+                                    uint8_t *ref_mv_count,
+#endif
 #if CONFIG_EXT_INTER
                                     int_mv *mv_list,
 #endif  // CONFIG_EXT_INTER
@@ -760,11 +764,11 @@
   b_mode_info *bmi = mi->bmi;
   int n;
 #if CONFIG_REF_MV
-  CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE];
   CANDIDATE_MV tmp_mv;
-  uint8_t ref_mv_count = 0, idx;
+  uint8_t idx;
   uint8_t above_count = 0, left_count = 0;
   MV_REFERENCE_FRAME rf[2] = { mi->mbmi.ref_frame[ref], NONE };
+  *ref_mv_count = 0;
 #endif
 
   assert(MAX_MV_REF_CANDIDATES == 2);
@@ -774,12 +778,12 @@
 
 #if CONFIG_REF_MV
   scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf,
-                -1, 0, ref_mv_stack, &ref_mv_count);
-  above_count = ref_mv_count;
+                -1, 0, ref_mv_stack, ref_mv_count);
+  above_count = *ref_mv_count;
 
   scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf,
-                0, -1, ref_mv_stack, &ref_mv_count);
-  left_count = ref_mv_count - above_count;
+                0, -1, ref_mv_stack, ref_mv_count);
+  left_count = *ref_mv_count - above_count;
 
   if (above_count > 1 && left_count > 0) {
     tmp_mv = ref_mv_stack[1];
@@ -787,7 +791,7 @@
     ref_mv_stack[above_count] = tmp_mv;
   }
 
-  for (idx = 0; idx < VPXMIN(MAX_MV_REF_CANDIDATES, ref_mv_count); ++idx) {
+  for (idx = 0; idx < VPXMIN(MAX_MV_REF_CANDIDATES, *ref_mv_count); ++idx) {
     mv_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
     clamp_mv_ref(&mv_list[idx].as_mv,
                  xd->n8_w << 3, xd->n8_h << 3, xd);
diff --git a/vp10/common/mvref_common.h b/vp10/common/mvref_common.h
index bc6d824..76530e9 100644
--- a/vp10/common/mvref_common.h
+++ b/vp10/common/mvref_common.h
@@ -289,16 +289,24 @@
 static INLINE uint8_t vp10_drl_ctx(const CANDIDATE_MV *ref_mv_stack,
                                    int ref_idx) {
   if (ref_mv_stack[ref_idx].weight > REF_CAT_LEVEL &&
-      ref_mv_stack[ref_idx + 1].weight > REF_CAT_LEVEL)
-    return 0;
+      ref_mv_stack[ref_idx + 1].weight > REF_CAT_LEVEL) {
+    if (ref_mv_stack[ref_idx].weight == ref_mv_stack[ref_idx + 1].weight)
+      return 0;
+    else
+      return 1;
+  }
 
   if (ref_mv_stack[ref_idx].weight > REF_CAT_LEVEL &&
       ref_mv_stack[ref_idx + 1].weight < REF_CAT_LEVEL)
-    return 1;
+    return 2;
 
   if (ref_mv_stack[ref_idx].weight < REF_CAT_LEVEL &&
-      ref_mv_stack[ref_idx + 1].weight < REF_CAT_LEVEL)
-    return 2;
+      ref_mv_stack[ref_idx + 1].weight < REF_CAT_LEVEL) {
+    if (ref_mv_stack[ref_idx].weight == ref_mv_stack[ref_idx + 1].weight)
+      return 3;
+    else
+      return 4;
+  }
 
   assert(0);
   return 0;
@@ -327,6 +335,10 @@
 
 void vp10_append_sub8x8_mvs_for_idx(VP10_COMMON *cm, MACROBLOCKD *xd,
                                     int block, int ref, int mi_row, int mi_col,
+#if CONFIG_REF_MV
+                                    CANDIDATE_MV *ref_mv_stack,
+                                    uint8_t *ref_mv_count,
+#endif
 #if CONFIG_EXT_INTER
                                     int_mv *mv_list,
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/common/thread_common.c b/vp10/common/thread_common.c
index f8bfc89..66e788b 100644
--- a/vp10/common/thread_common.c
+++ b/vp10/common/thread_common.c
@@ -387,11 +387,7 @@
 
   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
     for (j = 0; j < 2; ++j)
-      cm->counts.drl_mode0[i][j] += counts->drl_mode0[i][j];
-
-  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    for (j = 0; j < 2; ++j)
-      cm->counts.drl_mode1[i][j] += counts->drl_mode1[i][j];
+      cm->counts.drl_mode[i][j] += counts->drl_mode[i][j];
 
 #if CONFIG_EXT_INTER
   for (j = 0; j < 2; ++j)
diff --git a/vp10/decoder/decodeframe.c b/vp10/decoder/decodeframe.c
index 6aa5254..53ae632 100644
--- a/vp10/decoder/decodeframe.c
+++ b/vp10/decoder/decodeframe.c
@@ -117,9 +117,7 @@
   for (i = 0; i < REFMV_MODE_CONTEXTS; ++i)
     vp10_diff_update_prob(r, &fc->refmv_prob[i]);
   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    vp10_diff_update_prob(r, &fc->drl_prob0[i]);
-  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    vp10_diff_update_prob(r, &fc->drl_prob1[i]);
+    vp10_diff_update_prob(r, &fc->drl_prob[i]);
 #if CONFIG_EXT_INTER
   vp10_diff_update_prob(r, &fc->new2mv_prob);
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/decoder/decodemv.c b/vp10/decoder/decodemv.c
index f52fae4..5b2fa1f 100644
--- a/vp10/decoder/decodemv.c
+++ b/vp10/decoder/decodemv.c
@@ -155,55 +155,45 @@
   uint8_t ref_frame_type = vp10_ref_frame_type(mbmi->ref_frame);
   mbmi->ref_mv_idx = 0;
 
-  if (xd->ref_mv_count[ref_frame_type] > 1 && mbmi->mode == NEWMV) {
-    uint8_t drl_ctx = vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], 0);
-    vpx_prob drl_prob = cm->fc->drl_prob0[drl_ctx];
-
-    if (!vpx_read(r, drl_prob)) {
-      mbmi->ref_mv_idx = 0;
-      return;
-    }
-    mbmi->ref_mv_idx = 1;
-
-    if (xd->ref_mv_count[ref_frame_type] > 2) {
-      drl_ctx = vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], 1);
-      drl_prob = cm->fc->drl_prob0[drl_ctx];
-      if (!vpx_read(r, drl_prob)) {
-        mbmi->ref_mv_idx = 1;
-        return;
-      }
-      mbmi->ref_mv_idx = 2;
-    }
-    return;
-  }
-
-  if (xd->ref_mv_count[ref_frame_type] > 2 && mbmi->mode == NEARMV) {
-    uint8_t drl0_ctx = vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], 1);
-    vpx_prob drl0_prob = cm->fc->drl_prob0[drl0_ctx];
-    if (vpx_read(r, drl0_prob)) {
-      mbmi->ref_mv_idx = 1;
-      if (xd->counts)
-        ++xd->counts->drl_mode0[drl0_ctx][1];
-      if (xd->ref_mv_count[ref_frame_type] > 3) {
-        uint8_t drl1_ctx =
-            vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], 2);
-        vpx_prob drl1_prob = cm->fc->drl_prob1[drl1_ctx];
-        if (vpx_read(r, drl1_prob)) {
-          mbmi->ref_mv_idx = 2;
+  if (mbmi->mode == NEWMV) {
+    int idx;
+    for (idx = 0; idx < 2; ++idx) {
+      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
+        uint8_t drl_ctx = vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
+        vpx_prob drl_prob = cm->fc->drl_prob[drl_ctx];
+        if (!vpx_read(r, drl_prob)) {
+          mbmi->ref_mv_idx = idx;
           if (xd->counts)
-            ++xd->counts->drl_mode1[drl1_ctx][1];
-
+            ++xd->counts->drl_mode[drl_ctx][0];
           return;
         }
-
+        mbmi->ref_mv_idx = idx + 1;
         if (xd->counts)
-          ++xd->counts->drl_mode1[drl1_ctx][0];
+          ++xd->counts->drl_mode[drl_ctx][1];
       }
-      return;
     }
+  }
 
-    if (xd->counts)
-      ++xd->counts->drl_mode0[drl0_ctx][0];
+  if (mbmi->mode == NEARMV) {
+    int idx;
+    // Offset the NEARESTMV mode.
+    // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
+    // mode is factored in.
+    for (idx = 1; idx < 3; ++idx) {
+      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
+        uint8_t drl_ctx = vp10_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
+        vpx_prob drl_prob = cm->fc->drl_prob[drl_ctx];
+        if (!vpx_read(r, drl_prob)) {
+          mbmi->ref_mv_idx = idx - 1;
+          if (xd->counts)
+            ++xd->counts->drl_mode[drl_ctx][0];
+          return;
+        }
+        mbmi->ref_mv_idx = idx;
+        if (xd->counts)
+          ++xd->counts->drl_mode[drl_ctx][1];
+      }
+    }
   }
 }
 #endif
@@ -1401,6 +1391,10 @@
 #else
         if (b_mode == NEARESTMV || b_mode == NEARMV) {
 #endif  // CONFIG_EXT_INTER
+#if CONFIG_REF_MV
+          CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
+          uint8_t ref_mv_count[2];
+#endif
           for (ref = 0; ref < 1 + is_compound; ++ref)
 #if CONFIG_EXT_INTER
           {
@@ -1409,6 +1403,10 @@
                                    mv_ref_list, j, mi_row, mi_col, NULL);
 #endif  // CONFIG_EXT_INTER
             vp10_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
+#if CONFIG_REF_MV
+                                           ref_mv_stack[ref],
+                                           &ref_mv_count[ref],
+#endif
 #if CONFIG_EXT_INTER
                                            mv_ref_list,
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/encoder/bitstream.c b/vp10/encoder/bitstream.c
index 5ae69cf..56832e2 100644
--- a/vp10/encoder/bitstream.c
+++ b/vp10/encoder/bitstream.c
@@ -197,39 +197,37 @@
 
   assert(mbmi->ref_mv_idx < 3);
 
-  if (mbmi_ext->ref_mv_count[ref_frame_type] > 1 && mbmi->mode == NEWMV) {
-    uint8_t drl_ctx =
-        vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 0);
-    vpx_prob drl_prob = cm->fc->drl_prob0[drl_ctx];
+  if (mbmi->mode == NEWMV) {
+    int idx;
+    for (idx = 0; idx < 2; ++idx) {
+      if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+        uint8_t drl_ctx =
+            vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
+        vpx_prob drl_prob = cm->fc->drl_prob[drl_ctx];
 
-    vpx_write(w, mbmi->ref_mv_idx != 0, drl_prob);
-    if (mbmi->ref_mv_idx == 0)
-      return;
-
-    if (mbmi_ext->ref_mv_count[ref_frame_type] > 2) {
-      drl_ctx = vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 1);
-      drl_prob = cm->fc->drl_prob0[drl_ctx];
-      vpx_write(w, mbmi->ref_mv_idx != 1, drl_prob);
+        vpx_write(w, mbmi->ref_mv_idx != idx, drl_prob);
+        if (mbmi->ref_mv_idx == idx)
+          return;
+      }
     }
-    if (mbmi->ref_mv_idx == 1)
-      return;
-
-    assert(mbmi->ref_mv_idx == 2);
     return;
   }
 
-  if (mbmi_ext->ref_mv_count[ref_frame_type] > 2 && mbmi->mode == NEARMV) {
-    uint8_t drl0_ctx =
-        vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 1);
-    vpx_prob drl0_prob = cm->fc->drl_prob0[drl0_ctx];
-    vpx_write(w, mbmi->ref_mv_idx != 0, drl0_prob);
-    if (mbmi_ext->ref_mv_count[ref_frame_type] > 3 &&
-        mbmi->ref_mv_idx > 0) {
-      uint8_t drl1_ctx =
-          vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 2);
-      vpx_prob drl1_prob = cm->fc->drl_prob1[drl1_ctx];
-      vpx_write(w, mbmi->ref_mv_idx != 1, drl1_prob);
+  if (mbmi->mode == NEARMV) {
+    int idx;
+    // TODO(jingning): Temporary solution to compensate the NEARESTMV offset.
+    for (idx = 1; idx < 3; ++idx) {
+      if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+        uint8_t drl_ctx =
+            vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], idx);
+        vpx_prob drl_prob = cm->fc->drl_prob[drl_ctx];
+
+        vpx_write(w, mbmi->ref_mv_idx != (idx - 1), drl_prob);
+        if (mbmi->ref_mv_idx == (idx - 1))
+          return;
+      }
     }
+    return;
   }
 }
 #endif
@@ -369,11 +367,8 @@
     vp10_cond_prob_diff_update(w, &cm->fc->refmv_prob[i],
                                counts->refmv_mode[i]);
   for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    vp10_cond_prob_diff_update(w, &cm->fc->drl_prob0[i],
-                               counts->drl_mode0[i]);
-  for (i = 0; i < DRL_MODE_CONTEXTS; ++i)
-    vp10_cond_prob_diff_update(w, &cm->fc->drl_prob1[i],
-                               counts->drl_mode1[i]);
+    vp10_cond_prob_diff_update(w, &cm->fc->drl_prob[i],
+                               counts->drl_mode[i]);
 #if CONFIG_EXT_INTER
   vp10_cond_prob_diff_update(w, &cm->fc->new2mv_prob, counts->new2mv_mode);
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/encoder/encodeframe.c b/vp10/encoder/encodeframe.c
index 03ac189..08ba019 100644
--- a/vp10/encoder/encodeframe.c
+++ b/vp10/encoder/encodeframe.c
@@ -1912,24 +1912,34 @@
 #endif  // CONFIG_EXT_INTER
                                 mode_ctx);
 
+        if (mode == NEWMV) {
+          uint8_t ref_frame_type = vp10_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 =
+                  vp10_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 = vp10_ref_frame_type(mbmi->ref_frame);
-          if (mbmi_ext->ref_mv_count[ref_frame_type] > 2) {
-            uint8_t drl0_ctx =
-                vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 1);
-            if (mbmi->ref_mv_idx == 0)
-              ++counts->drl_mode0[drl0_ctx][0];
-            else
-              ++counts->drl_mode0[drl0_ctx][1];
+          int idx;
 
-            if (mbmi_ext->ref_mv_count[ref_frame_type] > 3 &&
-                mbmi->ref_mv_idx > 0) {
-              uint8_t drl1_ctx =
-                  vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 2);
-              if (mbmi->ref_mv_idx == 1)
-                ++counts->drl_mode1[drl1_ctx][0];
-              else
-                ++counts->drl_mode1[drl1_ctx][1];
+          for (idx = 1; idx < 3; ++idx) {
+            if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+              uint8_t drl_ctx =
+                  vp10_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;
             }
           }
         }
diff --git a/vp10/encoder/encoder.h b/vp10/encoder/encoder.h
index a319901..e02bf6c 100644
--- a/vp10/encoder/encoder.h
+++ b/vp10/encoder/encoder.h
@@ -487,7 +487,6 @@
   int zeromv_mode_cost[ZEROMV_MODE_CONTEXTS][2];
   int refmv_mode_cost[REFMV_MODE_CONTEXTS][2];
   int drl_mode_cost0[DRL_MODE_CONTEXTS][2];
-  int drl_mode_cost1[DRL_MODE_CONTEXTS][2];
 #if CONFIG_EXT_INTER
   int new2mv_mode_cost[2];
 #endif  // CONFIG_EXT_INTER
diff --git a/vp10/encoder/rd.c b/vp10/encoder/rd.c
index 78e8e9a..8d55ab4 100644
--- a/vp10/encoder/rd.c
+++ b/vp10/encoder/rd.c
@@ -418,13 +418,8 @@
       }
 
       for (i = 0; i < DRL_MODE_CONTEXTS; ++i) {
-        cpi->drl_mode_cost0[i][0] = vp10_cost_bit(cm->fc->drl_prob0[i], 0);
-        cpi->drl_mode_cost0[i][1] = vp10_cost_bit(cm->fc->drl_prob0[i], 1);
-      }
-
-      for (i = 0; i < DRL_MODE_CONTEXTS; ++i) {
-        cpi->drl_mode_cost1[i][0] = vp10_cost_bit(cm->fc->drl_prob1[i], 0);
-        cpi->drl_mode_cost1[i][1] = vp10_cost_bit(cm->fc->drl_prob1[i], 1);
+        cpi->drl_mode_cost0[i][0] = vp10_cost_bit(cm->fc->drl_prob[i], 0);
+        cpi->drl_mode_cost0[i][1] = vp10_cost_bit(cm->fc->drl_prob[i], 1);
       }
 #if CONFIG_EXT_INTER
       cpi->new2mv_mode_cost[0] = vp10_cost_bit(cm->fc->new2mv_prob, 0);
diff --git a/vp10/encoder/rdopt.c b/vp10/encoder/rdopt.c
index aca85f2..87c9d0f 100644
--- a/vp10/encoder/rdopt.c
+++ b/vp10/encoder/rdopt.c
@@ -4821,6 +4821,10 @@
       int64_t best_rd = INT64_MAX;
       const int i = idy * 2 + idx;
       int ref;
+#if CONFIG_REF_MV
+        CANDIDATE_MV ref_mv_stack[2][MAX_REF_MV_STACK_SIZE];
+        uint8_t ref_mv_count[2];
+#endif
 #if CONFIG_EXT_INTER
       int mv_idx;
       int_mv ref_mvs_sub8x8[2][2];
@@ -4835,6 +4839,10 @@
 #endif  // CONFIG_EXT_INTER
         frame_mv[ZEROMV][frame].as_int = 0;
         vp10_append_sub8x8_mvs_for_idx(cm, xd, i, ref, mi_row, mi_col,
+#if CONFIG_REF_MV
+                                       ref_mv_stack[ref],
+                                       &ref_mv_count[ref],
+#endif
 #if CONFIG_EXT_INTER
                                        mv_ref_list,
 #endif  // CONFIG_EXT_INTER
@@ -5209,6 +5217,7 @@
                                     bsi->rdstat[i][mode_idx].tl,
                                     idy, idx,
                                     mi_row, mi_col);
+
         if (bsi->rdstat[i][mode_idx].brdcost < INT64_MAX) {
           bsi->rdstat[i][mode_idx].brdcost += RDCOST(x->rdmult, x->rddiv,
                                             bsi->rdstat[i][mode_idx].brate, 0);
@@ -8090,26 +8099,18 @@
 
         // TODO(jingning): This should be deprecated shortly.
         int idx_offset = (mbmi->mode == NEARMV) ? 1 : 0;
-
         int ref_set =
             VPXMIN(2, mbmi_ext->ref_mv_count[ref_frame_type] - 1 - idx_offset);
 
-        uint8_t drl0_ctx = 0;
-        uint8_t drl_ctx = 0;
-
+        uint8_t drl_ctx = vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type],
+                                       idx_offset);
         // Dummy
         int_mv backup_fmv[2];
         backup_fmv[0] = frame_mv[NEWMV][ref_frame];
         if (comp_pred)
           backup_fmv[1] = frame_mv[NEWMV][second_ref_frame];
 
-        if (mbmi->mode == NEARMV) {
-          drl0_ctx = vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 1);
-          rate2 += cpi->drl_mode_cost0[drl0_ctx][0];
-        } else {
-          drl_ctx = vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 0);
-          rate2 += cpi->drl_mode_cost0[drl_ctx][0];
-        }
+        rate2 += cpi->drl_mode_cost0[drl_ctx][0];
 
         if (this_rd < INT64_MAX) {
           if (RDCOST(x->rdmult, x->rddiv, rate_y + rate_uv, distortion2) <
@@ -8195,23 +8196,20 @@
                                            &tmp_sse, best_rd);
           }
 
-          if (this_mode == NEARMV) {
-            tmp_rate += cpi->drl_mode_cost0[drl0_ctx][1];
-            if (mbmi_ext->ref_mv_count[ref_frame_type] > 3) {
-              uint8_t drl1_ctx =
-                  vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 2);
-              tmp_rate += cpi->drl_mode_cost1[drl1_ctx][ref_idx];
-            }
+          for (i = 0; i < mbmi->ref_mv_idx; ++i) {
+            uint8_t drl1_ctx = 0;
+            drl1_ctx = vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type],
+                                    i + idx_offset);
+            tmp_rate += cpi->drl_mode_cost0[drl1_ctx][1];
           }
 
-          if (this_mode == NEWMV) {
-            tmp_rate += cpi->drl_mode_cost0[drl_ctx][1];
-
-            if (mbmi_ext->ref_mv_count[ref_frame_type] > 2) {
-              uint8_t this_drl_ctx =
-                  vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type], 1);
-              tmp_rate += cpi->drl_mode_cost0[this_drl_ctx][ref_idx];
-            }
+          if (mbmi_ext->ref_mv_count[ref_frame_type] >
+              mbmi->ref_mv_idx + idx_offset + 1 &&
+              ref_idx < ref_set - 1) {
+            uint8_t drl1_ctx =
+                vp10_drl_ctx(mbmi_ext->ref_mv_stack[ref_frame_type],
+                             mbmi->ref_mv_idx + idx_offset);
+            tmp_rate += cpi->drl_mode_cost0[drl1_ctx][0];
           }
 
           if (tmp_alt_rd < INT64_MAX) {