Make max gop length account for the look ahead length

Account for the look ahead frame numbers to decide the maximum
GOP length. This resolves an issue in GOP structure when the look
ahead is set to be 32.

STATS_CHANGED

Change-Id: I5c5e5762a85d79efbd58fabce0421fe20778079b
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index b123376..96d082f 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -2711,10 +2711,12 @@
 
     reset_fpf_position(twopass, start_position);
 
-    int max_gop_length = (oxcf->gf_cfg.lag_in_frames >= 32 &&
-                          is_stat_consumption_stage_twopass(cpi))
-                             ? MAX_GF_INTERVAL
-                             : MAX_GF_LENGTH_LAP;
+    int max_gop_length =
+        (oxcf->gf_cfg.lag_in_frames >= 32 &&
+         is_stat_consumption_stage_twopass(cpi))
+            ? AOMMIN(MAX_GF_INTERVAL,
+                     oxcf->gf_cfg.lag_in_frames - oxcf->arnr_max_frames / 2)
+            : MAX_GF_LENGTH_LAP;
     if (rc->intervals_till_gf_calculate_due == 0) {
       calculate_gf_length(cpi, max_gop_length, MAX_NUM_GF_INTERVALS);
     }