Set frame border size based on sb_size

Currently, the frame border size is hard-coded to 128+32=160 pixels.
But in the RTC use case, often the superblock size is set to 64X64, so
the border padding only needs to be 64+32=96 pixels. This commit makes
the encoder to set the border size based on the current sb_size.

This change gives about 10% memory reduction and some minor speedup.

Performance:
 | SPD_SET | TEST_SET | ENC_TIME  |
 |---------|----------|-----------|
 |    6    | rtc_derf |  -0.205%  |
 |    6    |   rtc    |  -0.043%  |
 |---------|----------|-----------|
 |    7    | rtc_derf |  -0.478%  |
 |    7    |   rtc    |  -0.133%  |
 |---------|----------|-----------|
 |    8    | rtc_derf |  -0.641%  |
 |    8    |   rtc    |  -0.174%  |
 |---------|----------|-----------|
 |    9    | rtc_derf |  -0.996%  |
 |    9    |   rtc    |  -0.248%  |
 |---------|----------|-----------|
 |   10    | rtc_derf |  -1.127%  |
 |   10    |   rtc    |  -0.296%  |
 |---------|----------|-----------|

Change-Id: I3628d02bc02e201dc4e13a5dd3efd5d94c24eb5c
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 0db25e6..c5db241 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -23,6 +23,7 @@
 #include "av1/av1_iface_common.h"
 #include "av1/encoder/bitstream.h"
 #include "av1/encoder/encoder.h"
+#include "av1/encoder/encoder_utils.h"
 #include "av1/encoder/ethread.h"
 #include "av1/encoder/external_partition.h"
 #include "av1/encoder/firstpass.h"
@@ -1406,14 +1407,9 @@
   oxcf->unit_test_cfg.sb_multipass_unit_test =
       extra_cfg->sb_multipass_unit_test;
 
-  // For allintra encoding mode, inter-frame motion search is not applicable and
-  // the intraBC motion vectors are restricted within the tile boundaries. Hence
-  // a smaller frame border size (AOM_ENC_ALLINTRA_BORDER) is used in this case.
   oxcf->border_in_pixels =
-      (resize_cfg->resize_mode || superres_cfg->superres_mode)
-          ? AOM_BORDER_IN_PIXELS
-          : (oxcf->kf_cfg.key_freq_max == 0) ? AOM_ENC_ALLINTRA_BORDER
-                                             : AOM_ENC_NO_SCALE_BORDER;
+      av1_get_enc_border_size(av1_is_resize_needed(oxcf),
+                              (oxcf->kf_cfg.key_freq_max == 0), BLOCK_128X128);
   memcpy(oxcf->target_seq_level_idx, extra_cfg->target_seq_level_idx,
          sizeof(oxcf->target_seq_level_idx));
   oxcf->tier_mask = extra_cfg->tier_mask;
@@ -2871,6 +2867,13 @@
       if (!ppi->lookahead) {
         int lag_in_frames = cpi_lap != NULL ? cpi_lap->oxcf.gf_cfg.lag_in_frames
                                             : cpi->oxcf.gf_cfg.lag_in_frames;
+        AV1EncoderConfig *oxcf = &cpi->oxcf;
+        const BLOCK_SIZE sb_size = av1_select_sb_size(
+            oxcf, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height,
+            cpi->svc.number_spatial_layers);
+        oxcf->border_in_pixels =
+            av1_get_enc_border_size(av1_is_resize_needed(oxcf),
+                                    oxcf->kf_cfg.key_freq_max == 0, sb_size);
 
         ppi->lookahead = av1_lookahead_init(
             cpi->oxcf.frm_dim_cfg.width, cpi->oxcf.frm_dim_cfg.height,
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 65d3a10..2d20927 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2092,6 +2092,11 @@
                          "Failed to allocate context buffers");
   }
 
+  AV1EncoderConfig *oxcf = &cpi->oxcf;
+  oxcf->border_in_pixels = av1_get_enc_border_size(
+      av1_is_resize_needed(oxcf), oxcf->kf_cfg.key_freq_max == 0,
+      cm->seq_params->sb_size);
+
   // Reset the frame pointers to the current frame size.
   if (aom_realloc_frame_buffer(
           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
diff --git a/av1/encoder/encoder_utils.c b/av1/encoder/encoder_utils.c
index c8f6085..7b093cb 100644
--- a/av1/encoder/encoder_utils.c
+++ b/av1/encoder/encoder_utils.c
@@ -804,8 +804,7 @@
   if (oxcf->superres_cfg.superres_mode == AOM_SUPERRES_NONE &&
       oxcf->resize_cfg.resize_mode == RESIZE_NONE) {
     int is_480p_or_lesser = AOMMIN(width, height) <= 480;
-    if ((oxcf->speed >= 1 || oxcf->mode == REALTIME) && is_480p_or_lesser)
-      return BLOCK_64X64;
+    if (oxcf->speed >= 1 && is_480p_or_lesser) return BLOCK_64X64;
 
     // For 1080p and lower resolutions, choose SB size adaptively based on
     // resolution and speed level for multi-thread encode.
diff --git a/av1/encoder/encoder_utils.h b/av1/encoder/encoder_utils.h
index 5ff9ca3..da73292 100644
--- a/av1/encoder/encoder_utils.h
+++ b/av1/encoder/encoder_utils.h
@@ -1047,6 +1047,26 @@
 void av1_dump_filtered_recon_frames(AV1_COMP *cpi);
 #endif
 
+static AOM_INLINE int av1_get_enc_border_size(bool resize, bool all_intra,
+                                              BLOCK_SIZE sb_size) {
+  // For allintra encoding mode, inter-frame motion search is not applicable and
+  // the intraBC motion vectors are restricted within the tile boundaries. Hence
+  // a smaller frame border size (AOM_ENC_ALLINTRA_BORDER) is used in this case.
+  if (resize) {
+    return AOM_BORDER_IN_PIXELS;
+  } else if (all_intra) {
+    return AOM_ENC_ALLINTRA_BORDER;
+  } else {
+    return block_size_wide[sb_size] + 32;
+  }
+}
+
+static AOM_INLINE bool av1_is_resize_needed(const AV1EncoderConfig *oxcf) {
+  const ResizeCfg *resize_cfg = &oxcf->resize_cfg;
+  const SuperResCfg *superres_cfg = &oxcf->superres_cfg;
+  return resize_cfg->resize_mode || superres_cfg->superres_mode;
+}
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/examples/lightfield_encoder.c b/examples/lightfield_encoder.c
index 6be2101..9aef836 100644
--- a/examples/lightfield_encoder.c
+++ b/examples/lightfield_encoder.c
@@ -39,6 +39,7 @@
 #include "aom/aomcx.h"
 #include "aom_scale/yv12config.h"
 #include "av1/common/enums.h"
+#include "av1/encoder/encoder_utils.h"
 #include "common/tools_common.h"
 #include "common/video_writer.h"
 
@@ -291,10 +292,12 @@
   aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
   if (FORCE_HIGHBITDEPTH_DECODING) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
   // Allocate memory with the border so that it can be used as a reference.
+  const bool resize =
+      codec.config.enc->rc_resize_mode || codec.config.enc->rc_superres_mode;
+  const bool all_intra = reference_image_num - 1 == 0;
   int border_in_pixels =
-      (codec.config.enc->rc_resize_mode || codec.config.enc->rc_superres_mode)
-          ? AOM_BORDER_IN_PIXELS
-          : AOM_ENC_NO_SCALE_BORDER;
+      av1_get_enc_border_size(resize, all_intra, BLOCK_64X64);
+
   for (i = 0; i < reference_image_num; i++) {
     if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, cfg->g_w,
                                    cfg->g_h, 32, 8, border_in_pixels)) {