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/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];