Refinement search improvement

Improves the refinement search for global motion.

Change-Id: If6160ebf0f5309a0d7fb648ec5efb0778766cb87
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index b0636d5..d26f65f 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4793,12 +4793,13 @@
                                  dst + border * d_stride + border, border,
                                  border, d_width - 2 * border,
                                  d_height - 2 * border, d_stride, 0, 0, 16, 16);
-  for (p = 0; p < n_params; ++p) {
-    param = param_mat + p;
-    step = 1 << (n_refinements + 1);
-    curr_param = *param;
-    best_param = curr_param;
-    for (i = 0; i < n_refinements; i++) {
+  step = 1 << (n_refinements + 1);
+  for (i = 0; i < n_refinements; i++, step >>= 1) {
+    for (p = 0; p < n_params; ++p) {
+      int step_dir = 0;
+      param = param_mat + p;
+      curr_param = *param;
+      best_param = curr_param;
       // look to the left
       *param = add_param_offset(p, curr_param, -step);
       step_error = av1_warp_erroradv(
@@ -4810,11 +4811,9 @@
           border, border, d_width - 2 * border, d_height - 2 * border, d_stride,
           0, 0, 16, 16);
       if (step_error < best_error) {
-        step >>= 1;
         best_error = step_error;
         best_param = *param;
-        curr_param = best_param;
-        continue;
+        step_dir = -1;
       }
 
       // look to the right
@@ -4828,18 +4827,33 @@
           border, border, d_width - 2 * border, d_height - 2 * border, d_stride,
           0, 0, 16, 16);
       if (step_error < best_error) {
-        step >>= 1;
         best_error = step_error;
         best_param = *param;
-        curr_param = best_param;
-        continue;
+        step_dir = 1;
       }
+      *param = best_param;
 
-      // no improvement found-> means we're either already at a minimum or
-      // step is too wide
-      step >>= 1;
+      // look to the direction chosen above repeatedly until error increases
+      // for the biggest step size
+      while (step_dir) {
+        *param = add_param_offset(p, best_param, step * step_dir);
+        step_error = av1_warp_erroradv(
+            wm,
+#if CONFIG_AOM_HIGHBITDEPTH
+            use_hbd, bd,
+#endif  // CONFIG_AOM_HIGHBITDEPTH
+            ref, r_width, r_height, r_stride, dst + border * d_stride + border,
+            border, border, d_width - 2 * border, d_height - 2 * border,
+            d_stride, 0, 0, 16, 16);
+        if (step_error < best_error) {
+          best_error = step_error;
+          best_param = *param;
+        } else {
+          *param = best_param;
+          step_dir = 0;
+        }
+      }
     }
-    *param = best_param;
   }
   force_wmtype(wm, wmtype);
   wm->wmtype = get_gmtype(wm);