Define the maximum GOP size for the 4-layer structure

Allow a GOP of size <= 24 to use pyramid struture, but currently this
change has no effect since we use fix GF size = 16.

We might want to group few stray frames into the current GOP for
forward keyframe setting or use ALTREF to match the quality of the
next keyframe.

Change-Id: I228d4515a6775d14f0ea22ad2e88b418964bcdc2
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index c19e071..69dd20c 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -1695,7 +1695,8 @@
   GF_GROUP *const gf_group = &twopass->gf_group;
   const int key_frame = cpi->common.frame_type == KEY_FRAME;
 
-  assert(rc->baseline_gf_interval >= 4 && rc->baseline_gf_interval <= 16);
+  assert(rc->baseline_gf_interval >= 4 &&
+         rc->baseline_gf_interval <= MAX_PYRAMID_SIZE);
 
   const int gf_update_frames =
       construct_multi_layer_gf_structure(gf_group, rc->baseline_gf_interval);
@@ -1896,7 +1897,8 @@
 
 #if USE_SYMM_MULTI_LAYER
   const int valid_customized_gf_length =
-      rc->baseline_gf_interval >= 4 && rc->baseline_gf_interval <= 16;
+      rc->baseline_gf_interval >= 4 &&
+      rc->baseline_gf_interval <= MAX_PYRAMID_SIZE;
   // used the new structure only if extra_arf is allowed
   if (valid_customized_gf_length && rc->source_alt_ref_pending &&
       cpi->extra_arf_allowed > 0) {
@@ -2580,7 +2582,7 @@
                 AOMMAX(MIN_FWD_KF_INTERVAL, rc->min_gf_interval)) &&
                (rc->frames_to_key != i)) {
       // if possible, merge the last two gf groups
-      if (rc->frames_to_key <= 16) {
+      if (rc->frames_to_key <= MAX_PYRAMID_SIZE) {
         rc->baseline_gf_interval = rc->frames_to_key;
         // if merging the last two gf groups creates a group that is too long,
         // split them and force the last gf group to be the MIN_FWD_KF_INTERVAL
diff --git a/av1/encoder/ratectrl.h b/av1/encoder/ratectrl.h
index d5c46a4..198ecab 100644
--- a/av1/encoder/ratectrl.h
+++ b/av1/encoder/ratectrl.h
@@ -29,6 +29,14 @@
 #if CONFIG_FIX_GF_LENGTH
 #define FIXED_GF_LENGTH 16
 #define MAX_PYRAMID_LVL 4
+// We allow a frame to have at most two left/right descendants before changing
+// them into to a subtree, i.e., we allow the following structure:
+/*                    OUT_OF_ORDER_FRAME
+                     / /              \ \
+(two left children) F F                F F (two right children) */
+// Therefore the max gf size supported by 4 layer structure is
+// 1 (KEY/OVERLAY) + 1 + 2 + 4 + 16 (two children on both side of their parent)
+#define MAX_PYRAMID_SIZE 24
 #define USE_SYMM_MULTI_LAYER 1
 #define REDUCE_LAST_ALT_BOOST 1
 #define REDUCE_LAST_GF_LENGTH 1