A few fixes for global motion

Handles a rare divisin by 0 case.
Also adds a check on global motion parameters to disable
if the parameters obtained are outside the range that the
shear supports. This fixes a rare assert failure.
Also changes the recode loop threshold somewhat.

Change-Id: I4c6e74b914ac653cd9caa0563d78b0a19a2a8627
diff --git a/av1/common/mv.h b/av1/common/mv.h
index e033ec6..96b2a1c 100644
--- a/av1/common/mv.h
+++ b/av1/common/mv.h
@@ -139,17 +139,6 @@
 #define GM_ALPHA_MIN -GM_ALPHA_MAX
 #define GM_ROW3HOMO_MIN -GM_ROW3HOMO_MAX
 
-// Maximum number of bits used for different models
-#define GM_IDENTITY_BITS 0
-#define GM_TRANSLATION_BITS ((GM_ABS_TRANS_BITS + 2) * 2)
-#define GM_ROTZOOM_BITS (GM_TRANSLATION_BITS + (GM_ABS_ALPHA_BITS + 2) * 2)
-#define GM_AFFINE_BITS (GM_ROTZOOM_BITS + (GM_ABS_ALPHA_BITS + 2) * 2)
-#define GM_HOMOGRAPHY_BITS (GM_AFFINE_BITS + (GM_ABS_ROW3HOMO_BITS + 2) * 2)
-#define GM_HORTRAPEZOID_BITS \
-  (GM_AFFINE_BITS - GM_ABS_ALPHA_BITS + GM_ABS_ROW3HOMO_BITS)
-#define GM_VERTRAPEZOID_BITS \
-  (GM_AFFINE_BITS - GM_ABS_ALPHA_BITS + GM_ABS_ROW3HOMO_BITS)
-
 // Use global motion parameters for sub8x8 blocks
 #define GLOBAL_SUB8X8_USED 0
 
diff --git a/av1/common/warped_motion.c b/av1/common/warped_motion.c
index 29b26ed..9bf2aa9 100644
--- a/av1/common/warped_motion.c
+++ b/av1/common/warped_motion.c
@@ -661,8 +661,8 @@
 
 static int is_affine_shear_allowed(int32_t alpha, int32_t beta, int32_t gamma,
                                    int32_t delta) {
-  if ((4 * abs(alpha) + 7 * abs(beta) >= (1 << WARPEDMODEL_PREC_BITS)) ||
-      (4 * abs(gamma) + 4 * abs(delta) >= (1 << WARPEDMODEL_PREC_BITS)))
+  if ((4 * abs(alpha) + 7 * abs(beta) > (1 << WARPEDMODEL_PREC_BITS)) ||
+      (4 * abs(gamma) + 4 * abs(delta) > (1 << WARPEDMODEL_PREC_BITS)))
     return 0;
   else
     return 1;
@@ -683,9 +683,15 @@
   v = ((int64_t)mat[3] * mat[4]) * y;
   *delta = mat[5] - ROUND_POWER_OF_TWO_SIGNED_64(v, shift) -
            (1 << WARPEDMODEL_PREC_BITS);
+  if (!is_affine_shear_allowed(*alpha, *beta, *gamma, *delta)) return 0;
   return 1;
 }
 
+int is_shearable_params(WarpedMotionParams *wm) {
+  int32_t alpha, beta, gamma, delta;
+  return get_shear_params(wm, &alpha, &beta, &gamma, &delta);
+}
+
 #if CONFIG_AOM_HIGHBITDEPTH
 static INLINE void highbd_get_subcolumn(int taps, uint16_t *ref, int32_t *col,
                                         int stride, int x, int y_start) {
@@ -869,10 +875,6 @@
       assert(0 && "Warped motion model is incompatible with shear warp filter");
       return;
     }
-    if (!is_affine_shear_allowed(alpha, beta, gamma, delta)) {
-      assert(0 && "Warped motion model is incompatible with shear warp filter");
-      return;
-    }
 
     for (i = p_row; i < p_row + p_height; i += 8) {
       for (j = p_col; j < p_col + p_width; j += 8) {
@@ -1204,10 +1206,6 @@
       assert(0 && "Warped motion model is incompatible with shear warp filter");
       return;
     }
-    if (!is_affine_shear_allowed(alpha, beta, gamma, delta)) {
-      assert(0 && "Warped motion model is incompatible with shear warp filter");
-      return;
-    }
 
     // printf("%d %d %d %d\n", mat[2], mat[3], mat[4], mat[5]);
     av1_warp_affine(mat, ref, width, height, stride, pred, p_col, p_row,
@@ -1561,7 +1559,6 @@
       // check compatibility with the fast warp filter
       int32_t alpha, beta, gamma, delta;
       if (!get_shear_params(wm_params, &alpha, &beta, &gamma, &delta)) return 1;
-      if (!is_affine_shear_allowed(alpha, beta, gamma, delta)) return 1;
     }
   }
 
diff --git a/av1/common/warped_motion.h b/av1/common/warped_motion.h
index 4528288..13b275e 100644
--- a/av1/common/warped_motion.h
+++ b/av1/common/warped_motion.h
@@ -89,4 +89,6 @@
 int find_projection(const int np, int *pts1, int *pts2, BLOCK_SIZE bsize,
                     int mvy, int mvx, WarpedMotionParams *wm_params, int mi_row,
                     int mi_col);
+
+int is_shearable_params(WarpedMotionParams *wm);
 #endif  // AV1_COMMON_WARPED_MOTION_H_
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index cfa192a..d8f6fcd 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -5139,6 +5139,9 @@
               }
             }
           }
+          if (cm->global_motion[frame].wmtype <= AFFINE)
+            if (!is_shearable_params(&cm->global_motion[frame]))
+              set_default_gmparams(&cm->global_motion[frame]);
 
           // If the best error advantage found doesn't meet the threshold for
           // this motion type, revert to IDENTITY.
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index bc63da4..eeabdc7 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -3038,7 +3038,7 @@
 }
 
 #if CONFIG_GLOBAL_MOTION
-#define GM_RECODE_LOOP_NUM4X4_FACTOR 256
+#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
 static int recode_loop_test_global_motion(AV1_COMP *cpi) {
   int i;
   int recode = 0;
diff --git a/av1/encoder/ransac.c b/av1/encoder/ransac.c
index 44dc7f5..198bc39 100644
--- a/av1/encoder/ransac.c
+++ b/av1/encoder/ransac.c
@@ -454,7 +454,7 @@
     msqe += sqrt(p[0] * p[0] + p[1] * p[1]);
   }
   msqe /= n;
-  scale = sqrt(2) / msqe;
+  scale = (msqe == 0 ? 1.0 : sqrt(2) / msqe);
   T[0] = scale;
   T[1] = 0;
   T[2] = -scale * mean[0];