[NORMATIVE] Consolidate reference mv clamping

Clamp_mv_ref happens in multiple places in ref_mv search, which can be
convoluted as reported in issue 1124. This change is to consolidate
the clamping into one place.

Borg test result on lowres set:
avg_psnr:    ovr_psnr:   ssim:
  0.000       0.000      0.001

BUG=aomedia:1377
BUG=aomedia:1124
BUG=aomedia:857

Change-Id: I1649d5b5f37683c9c30e493c6eed13a808ab543a
diff --git a/av1/common/mvref_common.c b/av1/common/mvref_common.c
index b7e3b2e..c402938 100644
--- a/av1/common/mvref_common.c
+++ b/av1/common/mvref_common.c
@@ -1207,12 +1207,16 @@
     for (int idx = refmv_count[ref_frame]; idx < MAX_MV_REF_CANDIDATES; ++idx)
       mv_ref_list[rf[0]][idx].as_int = gm_mv_candidates[0].as_int;
 #endif
+
+    for (int idx = 0; idx < refmv_count[ref_frame]; ++idx) {
+      clamp_mv_ref(&ref_mv_stack[ref_frame][idx].this_mv.as_mv,
+                   xd->n8_w << MI_SIZE_LOG2, xd->n8_h << MI_SIZE_LOG2, xd);
+    }
+
     for (int idx = 0;
          idx < AOMMIN(MAX_MV_REF_CANDIDATES, refmv_count[ref_frame]); ++idx) {
       mv_ref_list[rf[0]][idx].as_int =
           ref_mv_stack[ref_frame][idx].this_mv.as_int;
-      clamp_mv_ref(&mv_ref_list[rf[0]][idx].as_mv, xd->n8_w << MI_SIZE_LOG2,
-                   xd->n8_h << MI_SIZE_LOG2, xd);
     }
   }
   (void)nearest_match;
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 66f5a26..0522db3 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -1867,8 +1867,6 @@
       uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
       if (xd->ref_mv_count[ref_frame_type] > 1) {
         ref_mv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
-        clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
-                     xd->n8_h << MI_SIZE_LOG2, xd);
       }
       nearestmv[0] = ref_mv[0];
     }
@@ -1876,8 +1874,6 @@
       uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
       if (xd->ref_mv_count[ref_frame_type] > 1) {
         ref_mv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
-        clamp_mv_ref(&ref_mv[1].as_mv, xd->n8_w << MI_SIZE_LOG2,
-                     xd->n8_h << MI_SIZE_LOG2, xd);
       }
       nearestmv[1] = ref_mv[1];
     }
@@ -1886,8 +1882,6 @@
       uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
       if (xd->ref_mv_count[ref_frame_type] > 1) {
         ref_mv[0] = xd->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx].this_mv;
-        clamp_mv_ref(&ref_mv[0].as_mv, xd->n8_w << MI_SIZE_LOG2,
-                     xd->n8_h << MI_SIZE_LOG2, xd);
       }
       nearestmv[0] = ref_mv[0];
     }
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 083f35a..c635234 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -400,8 +400,6 @@
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
 
-  const int bw = xd->n8_w << MI_SIZE_LOG2;
-  const int bh = xd->n8_h << MI_SIZE_LOG2;
   int ref_mv_idx = mbmi->ref_mv_idx;
   MB_MODE_INFO_EXT *const mbmi_ext = x->mbmi_ext;
   CANDIDATE_MV *const curr_ref_mv_stack = mbmi_ext->ref_mv_stack[rf_type];
@@ -413,14 +411,12 @@
 
     if (compound_ref0_mode(mbmi->mode) == NEWMV) {
       int_mv this_mv = curr_ref_mv_stack[ref_mv_idx].this_mv;
-      clamp_mv_ref(&this_mv.as_mv, bw, bh, xd);
       mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
       mbmi->pred_mv[0] = this_mv;
       mi_pred_mv[0] = this_mv;
     }
     if (compound_ref1_mode(mbmi->mode) == NEWMV) {
       int_mv this_mv = curr_ref_mv_stack[ref_mv_idx].comp_mv;
-      clamp_mv_ref(&this_mv.as_mv, bw, bh, xd);
       mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
       mbmi->pred_mv[1] = this_mv;
       mi_pred_mv[1] = this_mv;
@@ -431,7 +427,6 @@
       for (i = 0; i < 1 + has_second_ref(mbmi); ++i) {
         int_mv this_mv = (i == 0) ? curr_ref_mv_stack[ref_mv_idx].this_mv
                                   : curr_ref_mv_stack[ref_mv_idx].comp_mv;
-        clamp_mv_ref(&this_mv.as_mv, bw, bh, xd);
         mbmi_ext->ref_mvs[mbmi->ref_frame[i]][0] = this_mv;
         mbmi->pred_mv[i] = this_mv;
         mi_pred_mv[i] = this_mv;
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index f90d18b..3122aff 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -10042,15 +10042,11 @@
           if (compound_ref0_mode(mbmi->mode) == NEWMV) {
             int_mv this_mv =
                 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
-            clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                         xd->n8_h << MI_SIZE_LOG2, xd);
             mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
           }
           if (compound_ref1_mode(mbmi->mode) == NEWMV) {
             int_mv this_mv =
                 mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
-            clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                         xd->n8_h << MI_SIZE_LOG2, xd);
             mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
           }
         }
@@ -10061,8 +10057,6 @@
             int_mv this_mv =
                 (ref == 0) ? mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv
                            : mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
-            clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                         xd->n8_h << MI_SIZE_LOG2, xd);
             mbmi_ext->ref_mvs[mbmi->ref_frame[ref]][0] = this_mv;
           }
         }
@@ -10171,28 +10165,20 @@
             if (compound_ref0_mode(mbmi->mode) == NEWMV) {
               int_mv this_mv =
                   mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
-              clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                           xd->n8_h << MI_SIZE_LOG2, xd);
               mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
             } else if (compound_ref0_mode(mbmi->mode) == NEARESTMV) {
               int_mv this_mv =
                   mbmi_ext->ref_mv_stack[ref_frame_type][0].this_mv;
-              clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                           xd->n8_h << MI_SIZE_LOG2, xd);
               mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
             }
 
             if (compound_ref1_mode(mbmi->mode) == NEWMV) {
               int_mv this_mv =
                   mbmi_ext->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
-              clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                           xd->n8_h << MI_SIZE_LOG2, xd);
               mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
             } else if (compound_ref1_mode(mbmi->mode) == NEARESTMV) {
               int_mv this_mv =
                   mbmi_ext->ref_mv_stack[ref_frame_type][0].comp_mv;
-              clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                           xd->n8_h << MI_SIZE_LOG2, xd);
               mbmi_ext->ref_mvs[mbmi->ref_frame[1]][0] = this_mv;
             }
           } else {
@@ -10200,8 +10186,6 @@
                                  ->ref_mv_stack[ref_frame_type]
                                                [mbmi->ref_mv_idx + idx_offset]
                                  .this_mv;
-            clamp_mv_ref(&this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
-                         xd->n8_h << MI_SIZE_LOG2, xd);
             mbmi_ext->ref_mvs[mbmi->ref_frame[0]][0] = this_mv;
           }