Merge "Refactor: Replace rate dist sse skip by RD_STATS in VAR_TX" into nextgenv2
diff --git a/av1/common/entropy.c b/av1/common/entropy.c
index 80eaccc..95368c6 100644
--- a/av1/common/entropy.c
+++ b/av1/common/entropy.c
@@ -2924,7 +2924,7 @@
 
 #if CONFIG_ADAPT_SCAN
   for (tx_size = TX_4X4; tx_size < TX_SIZES; ++tx_size)
-    for (tx_type = TX_4X4; tx_type < TX_TYPES; ++tx_type) {
+    for (tx_type = DCT_DCT; tx_type < TX_TYPES; ++tx_type) {
       av1_update_scan_prob(cm, tx_size, tx_type, ADAPT_SCAN_UPDATE_RATE_16);
       av1_update_scan_order_facade(cm, tx_size, tx_type);
     }
diff --git a/av1/common/reconintra.c b/av1/common/reconintra.c
index cfd283f..5bb232d 100644
--- a/av1/common/reconintra.c
+++ b/av1/common/reconintra.c
@@ -317,8 +317,8 @@
 typedef void (*intra_high_pred_fn)(uint16_t *dst, ptrdiff_t stride,
                                    const uint16_t *above, const uint16_t *left,
                                    int bd);
-static intra_high_pred_fn pred_high[INTRA_MODES][4];
-static intra_high_pred_fn dc_pred_high[2][2][4];
+static intra_high_pred_fn pred_high[INTRA_MODES][TX_SIZES];
+static intra_high_pred_fn dc_pred_high[2][2][TX_SIZES];
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
 static void av1_init_intra_predictors_internal(void) {
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index 8f0a212..f1c4239 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -369,6 +369,7 @@
   const int tile_height = rst->tile_height >> rst->subsampling_y;
   int i, j, subtile_idx;
   int h_start, h_end, v_start, v_end;
+  const int shift = bit_depth - 8;
 
   for (subtile_idx = 0; subtile_idx < BILATERAL_SUBTILES; ++subtile_idx) {
     uint16_t *data_p, *tmpdata_p;
@@ -398,7 +399,7 @@
         for (y = -RESTORATION_HALFWIN; y <= RESTORATION_HALFWIN; ++y) {
           for (x = -RESTORATION_HALFWIN; x <= RESTORATION_HALFWIN; ++x) {
             wt = (int)wx_lut[y + RESTORATION_HALFWIN][x + RESTORATION_HALFWIN] *
-                 (int)wr_lut_[data_p2[x] - data_p[j]];
+                 (int)wr_lut_[(data_p2[x] >> shift) - (data_p[j] >> shift)];
             wtsum += (int64_t)wt;
             flsum += (int64_t)wt * data_p2[x];
           }
diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c
index 541db1d..5aa9198 100644
--- a/av1/common/thread_common.c
+++ b/av1/common/thread_common.c
@@ -528,7 +528,9 @@
   for (i = 0; i < n_counts; i++) acc[i] += cnt[i];
 
 #if CONFIG_DELTA_Q
-  for (i = 0; i < DELTA_Q_CONTEXTS; i++)
+  for (i = 0; i < DELTA_Q_CONTEXTS; i++) {
+    int j;
     for (j = 0; j < 2; ++j) cm->counts.delta_q[i][j] += counts->delta_q[i][j];
+  }
 #endif
 }
diff --git a/av1/common/warped_motion.c b/av1/common/warped_motion.c
index f73e777..89534de 100644
--- a/av1/common/warped_motion.c
+++ b/av1/common/warped_motion.c
@@ -215,7 +215,7 @@
   } else if (x == (1 << WARPEDPIXEL_PREC_BITS)) {
     return p[1];
   } else {
-    const int64_t v1 = x * x * x * (3 * (p[0] - p[1]) + p[2] - p[-1]);
+    const int64_t v1 = (int64_t)x * x * x * (3 * (p[0] - p[1]) + p[2] - p[-1]);
     const int64_t v2 = x * x * (2 * p[-1] - 5 * p[0] + 4 * p[1] - p[2]);
     const int64_t v3 = x * (p[1] - p[-1]);
     const int64_t v4 = 2 * p[0];
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 04c9fec..8b0de2f 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1114,7 +1114,7 @@
 
 #if CONFIG_DELTA_Q
   if (cpi->oxcf.aq_mode > NO_AQ && cpi->oxcf.aq_mode < DELTA_AQ)
-    av1_init_plane_quantizers(cpi, x);
+    av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
 #else
   if (cpi->oxcf.aq_mode)
     av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
@@ -1797,7 +1797,8 @@
 }
 #endif
 
-static void update_stats(const AV1_COMMON *const cm, ThreadData *td
+static void update_stats(const AV1_COMMON *const cm, ThreadData *td, int mi_row,
+                         int mi_col
 #if CONFIG_SUPERTX
                          ,
                          int supertx_enabled
@@ -1830,6 +1831,9 @@
     if (absdq < DELTA_Q_SMALL) td->counts->delta_q[absdq][0]++;
     xd->prev_qindex = mbmi->current_q_index;
   }
+#else
+  (void)mi_row;
+  (void)mi_col;
 #endif
   if (!frame_is_intra_only(cm)) {
     FRAME_COUNTS *const counts = td->counts;
@@ -2147,9 +2151,9 @@
 
   if (!dry_run) {
 #if CONFIG_SUPERTX
-    update_stats(&cpi->common, td, 0);
+    update_stats(&cpi->common, td, mi_row, mi_col, 0);
 #else
-    update_stats(&cpi->common, td);
+    update_stats(&cpi->common, td, mi_row, mi_col);
 #endif
   }
 }
@@ -4240,7 +4244,7 @@
       set_offsets(cpi, tile_info, x, mi_row, mi_col, BLOCK_64X64);
       xd->mi[0]->mbmi.current_q_index = current_qindex;
       xd->mi[0]->mbmi.segment_id = 0;
-      av1_init_plane_quantizers(cpi, x);
+      av1_init_plane_quantizers(cpi, x, xd->mi[0]->mbmi.segment_id);
     }
 #endif
 
@@ -5565,7 +5569,13 @@
 #endif  // CONFIG_EXT_INTER
                      mi_row_pred, mi_col_pred, bsize_pred, b_sub8x8, block);
 
-  if (!dry_run && !bextend) update_stats(&cpi->common, td, 1);
+  if (!dry_run && !bextend) {
+#if CONFIG_SUPERTX
+    update_stats(&cpi->common, td, mi_row_pred, mi_col_pred, 1);
+#else
+    update_stats(&cpi->common, td, mi_row_pred, mi_col_pred);
+#endif
+  }
 }
 
 static void extend_dir(const AV1_COMP *const cpi, ThreadData *td,
diff --git a/av1/encoder/global_motion.c b/av1/encoder/global_motion.c
index 5d88dbf..d8abea9 100644
--- a/av1/encoder/global_motion.c
+++ b/av1/encoder/global_motion.c
@@ -25,7 +25,7 @@
 #define MAX_CORNERS 4096
 #define MIN_INLIER_PROB 0.1
 
-INLINE RansacFunc get_ransac_type(TransformationType type) {
+static INLINE RansacFunc get_ransac_type(TransformationType type) {
   switch (type) {
     case HOMOGRAPHY: return ransac_homography;
     case AFFINE: return ransac_affine;
diff --git a/av1/encoder/ransac.c b/av1/encoder/ransac.c
index 714d567..2699c4f 100644
--- a/av1/encoder/ransac.c
+++ b/av1/encoder/ransac.c
@@ -81,10 +81,12 @@
                                              const int stride_points,
                                              const int stride_proj) {
   int i;
-  double x, y, Z;
+  double x, y, Z, Z_inv;
   for (i = 0; i < n; ++i) {
     x = *(points++), y = *(points++);
-    Z = 1. / (mat[7] * x + mat[6] * y + 1);
+    Z_inv = mat[7] * x + mat[6] * y + 1;
+    assert(fabs(Z_inv) > 0.00001);
+    Z = 1. / Z_inv;
     *(proj++) = (mat[1] * x + mat[0] * y + mat[3]) * Z;
     *(proj++) = (mat[2] * x + mat[4] * y + mat[4]) * Z;
     points += stride_points - 2;
@@ -155,7 +157,7 @@
   double T1[9], T2[9];
 
   *number_of_inliers = 0;
-  if (npoints < minpts * MINPTS_MULTIPLIER) {
+  if (npoints < minpts * MINPTS_MULTIPLIER || npoints == 0) {
     printf("Cannot find motion with %d matches\n", npoints);
     return 1;
   }
@@ -245,11 +247,15 @@
       }
     }
 
-    if (num_inliers >= max_inliers) {
-      double mean_distance = sum_distance / ((double)num_inliers);
-      double variance = sum_distance_squared / ((double)num_inliers - 1.0) -
-                        mean_distance * mean_distance * ((double)num_inliers) /
-                            ((double)num_inliers - 1.0);
+    if (num_inliers >= max_inliers && num_inliers > 1) {
+      int temp;
+      double fracinliers, pNoOutliers, mean_distance, variance;
+
+      assert(num_inliers > 1);
+      mean_distance = sum_distance / ((double)num_inliers);
+      variance = sum_distance_squared / ((double)num_inliers - 1.0) -
+                 mean_distance * mean_distance * ((double)num_inliers) /
+                     ((double)num_inliers - 1.0);
       if ((num_inliers > max_inliers) ||
           (num_inliers == max_inliers && variance < best_variance)) {
         best_variance = variance;
@@ -262,16 +268,15 @@
         memcpy(best_inlier_mask, inlier_mask,
                npoints * sizeof(*best_inlier_mask));
 
-        if (num_inliers > 0) {
-          double fracinliers = (double)num_inliers / (double)npoints;
-          double pNoOutliers = 1 - pow(fracinliers, minpts);
-          int temp;
-          pNoOutliers = fmax(EPS, pNoOutliers);
-          pNoOutliers = fmin(1 - EPS, pNoOutliers);
-          temp = (int)(log(1.0 - PROBABILITY_REQUIRED) / log(pNoOutliers));
-          if (temp > 0 && temp < N) {
-            N = AOMMAX(temp, MIN_TRIALS);
-          }
+        assert(npoints > 0);
+        fracinliers = (double)num_inliers / (double)npoints;
+        pNoOutliers = 1 - pow(fracinliers, minpts);
+        pNoOutliers = fmax(EPS, pNoOutliers);
+        pNoOutliers = fmin(1 - EPS, pNoOutliers);
+        assert(fabs(1.0 - pNoOutliers) > 0.00001);
+        temp = (int)(log(1.0 - PROBABILITY_REQUIRED) / log(pNoOutliers));
+        if (temp > 0 && temp < N) {
+          N = AOMMAX(temp, MIN_TRIALS);
         }
       }
     }
@@ -356,6 +361,7 @@
     // normalize so that H33 = 1
     int i;
     const double m = 1.0 / best_params[8];
+    assert(fabs(best_params[8]) > 0.00001);
     for (i = 0; i < 8; ++i) best_params[i] *= m;
     best_params[8] = 1.0;
   }
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 62edfa0..d9409d5 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -3012,8 +3012,8 @@
     inv_txfm_add(dqcoeff, rec_buffer, MAX_TX_SIZE, &inv_txfm_param);
 #endif  // CONFIG_AOM_HIGHBITDEPTH
 
-    if (txb_h + blk_col > max_blocks_wide ||
-        txb_w + blk_row > max_blocks_high) {
+    if (txb_w + blk_col > max_blocks_wide ||
+        txb_h + blk_row > max_blocks_high) {
       int idx, idy;
       unsigned int this_dist;
       int blocks_height = AOMMIN(txb_h, max_blocks_high - blk_row);
@@ -9446,7 +9446,10 @@
 #endif  // CONFIG_REF_MV
 #if CONFIG_GLOBAL_MOTION
     zeromv[0].as_int = cm->global_motion[refs[0]].motion_params.wmmat[0].as_int;
-    zeromv[1].as_int = cm->global_motion[refs[1]].motion_params.wmmat[0].as_int;
+    if (comp_pred_mode) {
+      zeromv[1].as_int =
+          cm->global_motion[refs[1]].motion_params.wmmat[0].as_int;
+    }
 #else
     zeromv[0].as_int = 0;
     zeromv[1].as_int = 0;