Per SB Rd adjustment for ALLINTRA mode.

This patch adjusts the rd value for each SB in ALLINTRA mode.

Experiments showed that lowering the RD value a little at the frame
level improved metrics slightly on most images in our png-crush
tests set and also helped visual quality somewhat.

This patch is the result of extensive experiments  where the
adjustment was applied instead at the per SB level.

At the moment the choice is based on the relative min and max
variance of 4x4 sub blocks within each SB though various other
possible measures were considered and further gains may be
possible.

Having tuned using the png crush data set the behavior was then
validated using the larger Tecnick data set.

Metrics results (PSNR, SSIM, Butteraugli) were as follows.

Png Crush -1.37, 0.25, -3.59
Tecnick -2.60, -0.46, -4.42

Change-Id: I0a5342e1d74a3e64f3768f07686b58eb755c60d7
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 673192e..5094f3a 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -894,6 +894,9 @@
    */
   int rdmult;
 
+  // Intra only, per sb rd adjustment.
+  int intra_sb_rdmult_modifier;
+
   //! Superblock level distortion propagation factor.
   double rb;
 
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 3f0a76d..990c201 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -600,6 +600,10 @@
   (void)gather_tpl_data;
 #endif
 
+  if (cpi->oxcf.mode == ALLINTRA) {
+    x->intra_sb_rdmult_modifier = 128;
+  }
+
   reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
   av1_zero(x->picked_ref_frames_mask);
   av1_invalid_rd_stats(rd_cost);
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index 95341e0..45458c9 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -624,6 +624,9 @@
     av1_set_butteraugli_rdmult(cpi, x, bsize, mi_row, mi_col, &x->rdmult);
   }
 #endif
+  if (cpi->oxcf.mode == ALLINTRA) {
+    x->rdmult = (x->rdmult * x->intra_sb_rdmult_modifier) >> 7;
+  }
 }
 
 void av1_set_offsets_without_segment_id(const AV1_COMP *const cpi,
@@ -4773,6 +4776,22 @@
   // Set buffers and offsets.
   av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, bsize);
 
+  if (cpi->oxcf.mode == ALLINTRA) {
+    if (bsize == cm->seq_params->sb_size) {
+      double var_min, var_max;
+      log_sub_block_var(cpi, x, bsize, &var_min, &var_max);
+
+      x->intra_sb_rdmult_modifier = 128;
+      if ((var_min < 2.0) && (var_max > 4.0)) {
+        if ((var_max - var_min) > 8.0) {
+          x->intra_sb_rdmult_modifier -= 48;
+        } else {
+          x->intra_sb_rdmult_modifier -= (int)((var_max - var_min) * 6);
+        }
+      }
+    }
+  }
+
   // Save rdmult before it might be changed, so it can be restored later.
   const int orig_rdmult = x->rdmult;
   setup_block_rdmult(cpi, x, mi_row, mi_col, bsize, NO_AQ, NULL);