Set initial_dimensions in encoder init

Set initial_dimensions in encoder initialization, so that
initial_dimensions are not 0, and some conditions related to
frame size and initial_dimensions comparisons in encoder config
are not triggered.

This avoids unnecessary memory allocations during encode config
calls.

A new variable cpi->frame_size_related_setup_done is added to
track whether frame size related setup and allocations have been
done, which is necessary since initial_dimensions are no longer able
to achieve this functionality after change.

Bug=aomedia:3526

Change-Id: I7b2d7aa6da4e616bd5999c8089e86a445b4ac73c
(cherry picked from commit f30a3ed4e80e97c2df4cb5c71eb11cc90c034ae6)
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 76f023a..1175a32 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -1499,8 +1499,6 @@
 
 static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
                                           const aom_codec_enc_cfg_t *cfg) {
-  InitialDimensions *const initial_dimensions =
-      &ctx->ppi->cpi->initial_dimensions;
   aom_codec_err_t res;
   int force_key = 0;
 
@@ -1514,15 +1512,14 @@
     // to update the frame width and height only when the actual encoding is
     // performed. cpi->last_coded_width and cpi->last_coded_height are used to
     // track the actual coded frame size.
-    if ((ctx->ppi->cpi->last_coded_width && ctx->ppi->cpi->last_coded_height &&
-         !valid_ref_frame_size(ctx->ppi->cpi->last_coded_width,
+    if (ctx->ppi->cpi->last_coded_width && ctx->ppi->cpi->last_coded_height &&
+        (!valid_ref_frame_size(ctx->ppi->cpi->last_coded_width,
                                ctx->ppi->cpi->last_coded_height, cfg->g_w,
-                               cfg->g_h)) ||
-        (initial_dimensions->width &&
-         (int)cfg->g_w > initial_dimensions->width) ||
-        (initial_dimensions->height &&
-         (int)cfg->g_h > initial_dimensions->height))
+                               cfg->g_h) ||
+         ((int)cfg->g_w > ctx->ppi->cpi->last_coded_width) ||
+         ((int)cfg->g_h > ctx->ppi->cpi->last_coded_height))) {
       force_key = 1;
+    }
   }
 
   if (ctx->monochrome_on_init && cfg->monochrome == 0) {
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 4c9fb23..4c15ebf 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -642,6 +642,12 @@
   cm->height = oxcf->frm_dim_cfg.height;
   cpi->is_dropped_frame = false;
 
+  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
+  initial_dimensions->width = cm->width;
+  initial_dimensions->height = cm->height;
+
+  cpi->frame_size_related_setup_done = false;
+
   alloc_compressor_data(cpi);
 
   // Single thread case: use counts in common.
@@ -916,7 +922,9 @@
     cpi->td.firstpass_ctx = NULL;
     alloc_compressor_data(cpi);
     realloc_segmentation_maps(cpi);
-    initial_dimensions->width = initial_dimensions->height = 0;
+    initial_dimensions->width = cm->width;
+    initial_dimensions->height = cm->height;
+    cpi->frame_size_related_setup_done = false;
   }
   av1_update_frame_size(cpi);
 
@@ -2061,13 +2069,15 @@
 #endif
 }
 
+// TODO(chengchen): consider renaming this function as it is necessary
+// for the encoder to setup critical parameters, and it does not
+// deal with initial width any longer.
 void av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
                              int subsampling_x, int subsampling_y) {
   AV1_COMMON *const cm = &cpi->common;
   SequenceHeader *const seq_params = cm->seq_params;
-  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
 
-  if (!initial_dimensions->width ||
+  if (!cpi->frame_size_related_setup_done ||
       seq_params->use_highbitdepth != use_highbitdepth ||
       seq_params->subsampling_x != subsampling_x ||
       seq_params->subsampling_y != subsampling_y) {
@@ -2087,9 +2097,8 @@
 
     init_motion_estimation(cpi);  // TODO(agrange) This can be removed.
 
-    initial_dimensions->width = cm->width;
-    initial_dimensions->height = cm->height;
     cpi->initial_mbs = cm->mi_params.MBs;
+    cpi->frame_size_related_setup_done = true;
   }
 }
 
@@ -2112,7 +2121,6 @@
 // Returns 1 if the assigned width or height was <= 0.
 int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
   AV1_COMMON *cm = &cpi->common;
-  InitialDimensions *const initial_dimensions = &cpi->initial_dimensions;
   av1_check_initial_width(cpi, cm->seq_params->use_highbitdepth,
                           cm->seq_params->subsampling_x,
                           cm->seq_params->subsampling_y);
@@ -2126,18 +2134,10 @@
   setup_denoiser_buffer(cpi);
 #endif
 
-  if (initial_dimensions->width && initial_dimensions->height &&
-      (cm->width > initial_dimensions->width ||
-       cm->height > initial_dimensions->height)) {
-    av1_free_context_buffers(cm);
-    av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
-    av1_free_sms_tree(&cpi->td);
-    av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
-    cpi->td.firstpass_ctx = NULL;
-    alloc_compressor_data(cpi);
-    realloc_segmentation_maps(cpi);
-    initial_dimensions->width = initial_dimensions->height = 0;
-  }
+  // No need to reallocate compressor data.
+  assert(cm->width <= cpi->initial_dimensions.width &&
+         cm->height <= cpi->initial_dimensions.height);
+
   alloc_mb_mode_info_buffers(cpi);
   av1_update_frame_size(cpi);
 
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index d733209..0a8bcde 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3163,7 +3163,9 @@
   FRAME_INDEX_SET frame_index_set;
 
   /*!
-   * Structure to store the dimensions of current frame.
+   * Structure to store the cm->width and cm->height in the last call
+   * of alloc_compressor_data().
+   * TODO(chengchen): rename this variable or delete it.
    */
   InitialDimensions initial_dimensions;
 
@@ -3176,6 +3178,12 @@
   int initial_mbs;
 
   /*!
+   * Flag to indicate whether the frame size inforamation has been
+   * setup and propagated to associated allocations.
+   */
+  bool frame_size_related_setup_done;
+
+  /*!
    * The width of the frame that is lastly encoded.
    * It is updated in the function "encoder_encode()".
    */