Always enable LAP for AOM_Q and passes=1

LAP is turned on for all lag in frame values. The
lag of firstpass stage will be toggled depending on
the value of kf interval and configured lag_in_frames.

Change-Id: I10a21564d5da0ae5e77be736b2b0ee4f7f814002
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 6e9a6b5..4c6f067 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -28,8 +28,6 @@
 
 #define MAG_SIZE (4)
 #define MAX_NUM_ENHANCEMENT_LAYERS 3
-// The lag between encode stage and the look ahead stage
-#define MIN_LAP_LAG 35
 
 struct av1_extracfg {
   int cpu_used;
@@ -1803,7 +1801,7 @@
 static aom_codec_err_t create_context_and_bufferpool(
     AV1_COMP **p_cpi, BufferPool **p_buffer_pool, AV1EncoderConfig *oxcf,
     struct aom_codec_pkt_list *pkt_list_head, FIRSTPASS_STATS *frame_stats_buf,
-    COMPRESSOR_STAGE stage, int num_lap_buffers,
+    COMPRESSOR_STAGE stage, int num_lap_buffers, int lap_lag_in_frames,
     STATS_BUFFER_CTX *stats_buf_context) {
   aom_codec_err_t res = AOM_CODEC_OK;
 
@@ -1816,7 +1814,8 @@
   }
 #endif
   *p_cpi = av1_create_compressor(oxcf, *p_buffer_pool, frame_stats_buf, stage,
-                                 num_lap_buffers, stats_buf_context);
+                                 num_lap_buffers, lap_lag_in_frames,
+                                 stats_buf_context);
   if (*p_cpi == NULL)
     res = AOM_CODEC_MEM_ERROR;
   else
@@ -1851,6 +1850,7 @@
 
     if (res == AOM_CODEC_OK) {
       int *num_lap_buffers = &priv->num_lap_buffers;
+      int lap_lag_in_frames = 0;
       *num_lap_buffers = 0;
       priv->timestamp_ratio.den = priv->cfg.g_timebase.den;
       priv->timestamp_ratio.num =
@@ -1858,11 +1858,18 @@
       reduce_ratio(&priv->timestamp_ratio);
 
       set_encoder_config(&priv->oxcf, &priv->cfg, &priv->extra_cfg);
-      if (((int)priv->cfg.g_lag_in_frames - LAP_LAG_IN_FRAMES) >= MIN_LAP_LAG &&
-          priv->oxcf.rc_mode == AOM_Q && priv->oxcf.pass == 0 &&
+      if (priv->oxcf.rc_mode == AOM_Q && priv->oxcf.pass == 0 &&
           priv->oxcf.mode == GOOD && priv->oxcf.fwd_kf_enabled == 0) {
         // Enable look ahead
-        *num_lap_buffers = priv->cfg.g_lag_in_frames - LAP_LAG_IN_FRAMES;
+        *num_lap_buffers = priv->cfg.g_lag_in_frames;
+        *num_lap_buffers =
+            clamp(*num_lap_buffers, 1,
+                  AOMMIN(MAX_LAP_BUFFERS,
+                         priv->oxcf.key_freq + SCENE_CUT_KEY_TEST_INTERVAL));
+        if ((int)priv->cfg.g_lag_in_frames - (*num_lap_buffers) >=
+            LAP_LAG_IN_FRAMES) {
+          lap_lag_in_frames = LAP_LAG_IN_FRAMES;
+        }
       }
       priv->oxcf.use_highbitdepth =
           (ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) ? 1 : 0;
@@ -1874,7 +1881,7 @@
 
       res = create_context_and_bufferpool(
           &priv->cpi, &priv->buffer_pool, &priv->oxcf, &priv->pkt_list.head,
-          priv->frame_stats_buffer, ENCODE_STAGE, *num_lap_buffers,
+          priv->frame_stats_buffer, ENCODE_STAGE, *num_lap_buffers, -1,
           &priv->stats_buf_context);
 
       // Create another compressor if look ahead is enabled
@@ -1882,7 +1889,7 @@
         res = create_context_and_bufferpool(
             &priv->cpi_lap, &priv->buffer_pool_lap, &priv->oxcf, NULL,
             priv->frame_stats_buffer, LAP_STAGE, *num_lap_buffers,
-            &priv->stats_buf_context);
+            lap_lag_in_frames, &priv->stats_buf_context);
       }
     }
   }
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 9032ad2..52492eb 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2944,6 +2944,7 @@
 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
                                 FIRSTPASS_STATS *frame_stats_buf,
                                 COMPRESSOR_STAGE stage, int num_lap_buffers,
+                                int lap_lag_in_frames,
                                 STATS_BUFFER_CTX *stats_buf_context) {
   AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
   AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
@@ -2987,7 +2988,7 @@
   cpi->lap_enabled = num_lap_buffers > 0;
   cpi->compressor_stage = stage;
   if (cpi->compressor_stage == LAP_STAGE) {
-    cpi->oxcf.lag_in_frames = LAP_LAG_IN_FRAMES;
+    cpi->oxcf.lag_in_frames = lap_lag_in_frames;
   }
 
   av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index cfe326e..3513103 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -56,6 +56,9 @@
 extern "C" {
 #endif
 
+// Number of frames required to test for scene cut detection
+#define SCENE_CUT_KEY_TEST_INTERVAL 16
+
 // Rational number with an int64 numerator
 // This structure holds a fractional value
 typedef struct aom_rational64 {
@@ -1178,6 +1181,7 @@
                                        FIRSTPASS_STATS *frame_stats_buf,
                                        COMPRESSOR_STAGE stage,
                                        int num_lap_buffers,
+                                       int lap_lag_in_frames,
                                        STATS_BUFFER_CTX *stats_buf_context);
 void av1_remove_compressor(AV1_COMP *cpi);
 
diff --git a/av1/encoder/lookahead.c b/av1/encoder/lookahead.c
index 3abc60c..ec5b858 100644
--- a/av1/encoder/lookahead.c
+++ b/av1/encoder/lookahead.c
@@ -47,7 +47,7 @@
     unsigned int subsampling_y, int use_highbitdepth, unsigned int depth,
     const int border_in_pixels, int is_scale, int num_lap_buffers) {
   struct lookahead_ctx *ctx = NULL;
-  int lag_in_frames = depth;
+  int lag_in_frames = clamp(depth, 1, MAX_LAG_BUFFERS);
 
   // Clamp the lookahead queue depth
   depth = clamp(depth, 0, MAX_LAG_BUFFERS);
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index d0ab8be..f3a1039 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -1354,7 +1354,7 @@
     double decay_accumulator = 1.0;
 
     // Examine how well the key frame predicts subsequent frames.
-    for (i = 0; i < 16; ++i) {
+    for (i = 0; i < SCENE_CUT_KEY_TEST_INTERVAL; ++i) {
       double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error /
                              DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));