AV1 RT: VB Partition adjustment for use_partition

This slowdowns lowres 40k speed6 about 10%, but gives 3.9% BDRate
improvements for low bitrates overall and some clips are
more than 10% improvement

Change-Id: I27ca1c05ae4e3c7d0a6ec88b33c7f3855452ec6d
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index fbff008..a363e1c 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1901,7 +1901,6 @@
   BLOCK_SIZE sub_subsize = BLOCK_4X4;
   int splits_below = 0;
   BLOCK_SIZE bs_type = mib[0]->sb_type;
-  int do_partition_search = 1;
   PICK_MODE_CONTEXT *ctx_none = &pc_tree->none;
 
   if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return;
@@ -1930,9 +1929,12 @@
   const int orig_rdmult = x->rdmult;
   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);
 
-  if (do_partition_search &&
-      cpi->sf.part_sf.partition_search_type == SEARCH_PARTITION &&
-      cpi->sf.part_sf.adjust_partitioning_from_last_frame) {
+  if (cpi->sf.part_sf.partition_search_type == SEARCH_PARTITION ||
+      (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION &&
+       (cpi->sf.part_sf.adjust_var_based_rd_partitioning == 2 ||
+        (cpi->sf.part_sf.adjust_var_based_rd_partitioning == 1 &&
+         cm->quant_params.base_qindex > 190 && bsize <= BLOCK_32X32 &&
+         !frame_is_intra_only(cm))))) {
     // Check if any of the sub blocks are further split.
     if (partition == PARTITION_SPLIT && subsize > BLOCK_8X8) {
       sub_subsize = get_partition_subsize(subsize, PARTITION_SPLIT);
@@ -2021,6 +2023,11 @@
       }
       break;
     case PARTITION_SPLIT:
+      if (cpi->sf.part_sf.adjust_var_based_rd_partitioning == 1 &&
+          none_rdc.rate < INT_MAX && none_rdc.skip == 1) {
+        av1_invalid_rd_stats(&last_part_rdc);
+        break;
+      }
       last_part_rdc.rate = 0;
       last_part_rdc.dist = 0;
       last_part_rdc.rdcost = 0;
@@ -2062,9 +2069,9 @@
         RDCOST(x->rdmult, last_part_rdc.rate, last_part_rdc.dist);
   }
 
-  if (do_partition_search &&
-      cpi->sf.part_sf.adjust_partitioning_from_last_frame &&
-      cpi->sf.part_sf.partition_search_type == SEARCH_PARTITION &&
+  if ((cpi->sf.part_sf.partition_search_type == SEARCH_PARTITION ||
+       (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION &&
+        cpi->sf.part_sf.adjust_var_based_rd_partitioning == 2)) &&
       partition != PARTITION_SPLIT && bsize > BLOCK_8X8 &&
       (mi_row + bs < mi_params->mi_rows ||
        mi_row + hbs == mi_params->mi_rows) &&
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 4ef3d46..eb83d7a 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -800,6 +800,7 @@
     sf->part_sf.max_intra_bsize = BLOCK_32X32;
     sf->part_sf.partition_search_breakout_rate_thr = 500;
     sf->part_sf.partition_search_type = VAR_BASED_PARTITION;
+    sf->part_sf.adjust_var_based_rd_partitioning = 1;
 
     sf->mv_sf.search_method = FAST_DIAMOND;
     sf->mv_sf.subpel_force_stop = QUARTER_PEL;
@@ -911,7 +912,7 @@
   part_sf->auto_min_partition_based_on_simple_motion = 0;
   part_sf->default_max_partition_size = BLOCK_LARGEST;
   part_sf->default_min_partition_size = BLOCK_4X4;
-  part_sf->adjust_partitioning_from_last_frame = 0;
+  part_sf->adjust_var_based_rd_partitioning = 0;
   part_sf->allow_partition_search_skip = 0;
   part_sf->max_intra_bsize = BLOCK_LARGEST;
   // This setting only takes effect when partition_search_type is set
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index 45b0164..9174df5 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -367,9 +367,10 @@
   BLOCK_SIZE default_min_partition_size;
   BLOCK_SIZE default_max_partition_size;
 
-  // Whether or not we allow partitions one smaller or one greater than the last
-  // frame's partitioning. Only used if use_lastframe_partitioning is set.
-  int adjust_partitioning_from_last_frame;
+  // Sets level of adjustmet of variace-based partitioning during
+  // rd_use_partition 0 - no partition adjusment 1 - adjust partition for small
+  // blocks and high QP 2 - always adjust partitioning
+  int adjust_var_based_rd_partitioning;
 
   // Partition search early breakout thresholds.
   int64_t partition_search_breakout_dist_thr;