Make global model acceptance depend on params cost

lowres:
BDRATE (overall PSNR): -1.661% (up from -1542%)

Change-Id: I612cb16f1d2362ab6375a5ef3258a4ed0777ac29
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index e413bae..7b8d8d3 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -5183,30 +5183,32 @@
               }
             }
           }
-          // If the best error advantage found doesn't meet the threshold for
-          // this motion type, revert to IDENTITY.
-          if (best_erroradvantage >
-              gm_advantage_thresh[cm->global_motion[frame].wmtype]) {
-            set_default_gmparams(&cm->global_motion[frame]);
-          }
-
           if (cm->global_motion[frame].wmtype <= AFFINE)
             if (!get_shear_params(&cm->global_motion[frame]))
               set_default_gmparams(&cm->global_motion[frame]);
 
-          if (cm->global_motion[frame].wmtype != IDENTITY) {
-            if (cm->global_motion[frame].wmtype == TRANSLATION) {
-              cm->global_motion[frame].wmmat[0] =
-                  convert_to_trans_prec(cm->allow_high_precision_mv,
-                                        cm->global_motion[frame].wmmat[0]) *
-                  GM_TRANS_ONLY_DECODE_FACTOR;
-              cm->global_motion[frame].wmmat[1] =
-                  convert_to_trans_prec(cm->allow_high_precision_mv,
-                                        cm->global_motion[frame].wmmat[1]) *
-                  GM_TRANS_ONLY_DECODE_FACTOR;
-            }
-            break;
+          if (cm->global_motion[frame].wmtype == TRANSLATION) {
+            cm->global_motion[frame].wmmat[0] =
+                convert_to_trans_prec(cm->allow_high_precision_mv,
+                                      cm->global_motion[frame].wmmat[0]) *
+                GM_TRANS_ONLY_DECODE_FACTOR;
+            cm->global_motion[frame].wmmat[1] =
+                convert_to_trans_prec(cm->allow_high_precision_mv,
+                                      cm->global_motion[frame].wmmat[1]) *
+                GM_TRANS_ONLY_DECODE_FACTOR;
           }
+
+          // If the best error advantage found doesn't meet the threshold for
+          // this motion type, revert to IDENTITY.
+          if (!is_enough_erroradvantage(
+                  best_erroradvantage,
+                  gm_get_params_cost(&cm->global_motion[frame],
+                                     &cm->prev_frame->global_motion[frame],
+                                     cm->allow_high_precision_mv))) {
+            set_default_gmparams(&cm->global_motion[frame]);
+          }
+
+          if (cm->global_motion[frame].wmtype != IDENTITY) break;
         }
         aom_clear_system_state();
       }
diff --git a/av1/encoder/global_motion.c b/av1/encoder/global_motion.c
index 7471bfc..b80622b 100644
--- a/av1/encoder/global_motion.c
+++ b/av1/encoder/global_motion.c
@@ -32,15 +32,13 @@
 // Border over which to compute the global motion
 #define ERRORADV_BORDER 0
 
-const double gm_advantage_thresh[TRANS_TYPES] = {
-  1.00,  // Identity (not used)
-  0.85,  // Translation
-  0.75,  // Rot zoom
-  0.65,  // Affine
-  0.65,  // Hor Trapezoid
-  0.65,  // Ver Trapezoid
-  0.50,  // Homography
-};
+#define ERRORADV_MAX_THRESH 0.995
+#define ERRORADV_COST_PRODUCT_THRESH 26000
+
+int is_enough_erroradvantage(double best_erroradvantage, int params_cost) {
+  return best_erroradvantage < ERRORADV_MAX_THRESH &&
+         best_erroradvantage * params_cost < ERRORADV_COST_PRODUCT_THRESH;
+}
 
 void convert_to_params(const double *params, int32_t *model) {
   int i;
diff --git a/av1/encoder/global_motion.h b/av1/encoder/global_motion.h
index f738bbe..08e94ab 100644
--- a/av1/encoder/global_motion.h
+++ b/av1/encoder/global_motion.h
@@ -22,10 +22,10 @@
 
 #define RANSAC_NUM_MOTIONS 1
 
-extern const double gm_advantage_thresh[TRANS_TYPES];
-
 void convert_model_to_params(const double *params, WarpedMotionParams *model);
 
+int is_enough_erroradvantage(double erroradv, int params_cost);
+
 // Adds some offset to a global motion parameter and handles
 // all of the necessary precision shifts, clamping, and
 // zero-centering.