Make the encoder aware of frame size increase from config

And then re-allocate associated buffers accordingly.

BUG=aomedia:3349

Change-Id: I2cc6d8312c403430dbae7ff6d8c887ab05242862
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 9906baf..a4b05d5 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -907,18 +907,16 @@
   cm->width = frm_dim_cfg->width;
   cm->height = frm_dim_cfg->height;
 
-  if (initial_dimensions->width || is_sb_size_changed) {
-    if (cm->width > initial_dimensions->width ||
-        cm->height > initial_dimensions->height || is_sb_size_changed) {
-      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;
-    }
+  if (cm->width > initial_dimensions->width ||
+      cm->height > initial_dimensions->height || is_sb_size_changed) {
+    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;
   }
   av1_update_frame_size(cpi);
 
@@ -1489,6 +1487,7 @@
   CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
                   aom_calloc((max_mi_rows * max_mi_cols) >> 2,
                              sizeof(*cpi->consec_zero_mv)));
+  cpi->consec_zero_mv_alloc_size = (max_mi_rows * max_mi_cols) >> 2;
 
   cpi->mb_weber_stats = NULL;
   cpi->mb_delta_q = NULL;
@@ -2565,9 +2564,18 @@
       cm, unscaled, &cpi->scaled_source, filter_scaler, phase_scaler, true,
       false, cpi->oxcf.border_in_pixels, cpi->image_pyramid_levels);
   if (frame_is_intra_only(cm) || resize_pending != 0) {
+    const int current_size = cm->mi_params.mi_rows * cm->mi_params.mi_cols >> 2;
+    if (cpi->consec_zero_mv &&
+        (cpi->consec_zero_mv_alloc_size < current_size)) {
+      aom_free(cpi->consec_zero_mv);
+      CHECK_MEM_ERROR(cm, cpi->consec_zero_mv,
+                      aom_calloc(current_size, sizeof(*cpi->consec_zero_mv)));
+    }
+    assert(cpi->consec_zero_mv != NULL);
     memset(cpi->consec_zero_mv, 0,
            ((cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2) *
                sizeof(*cpi->consec_zero_mv));
+    cpi->consec_zero_mv_alloc_size = current_size;
   }
 
   if (cpi->unscaled_last_source != NULL) {
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index f5ee041..881cdc3 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3385,6 +3385,11 @@
   uint8_t *consec_zero_mv;
 
   /*!
+   * Allocated memory size for |consec_zero_mv|.
+   */
+  int consec_zero_mv_alloc_size;
+
+  /*!
    * Block size of first pass encoding
    */
   BLOCK_SIZE fp_block_size;
diff --git a/test/frame_size_tests.cc b/test/frame_size_tests.cc
index 0a9eabf..7f61169 100644
--- a/test/frame_size_tests.cc
+++ b/test/frame_size_tests.cc
@@ -116,12 +116,6 @@
   cfg.g_pass = AOM_RC_ONE_PASS;
   cfg.g_lag_in_frames = 0;
   cfg.rc_end_usage = rc_mode_;
-  // TODO(https://crbug.com/aomedia/3349): Setting g_w and g_h shouldn't be
-  // necessary due to the call to aom_codec_enc_config_set() at the start of
-  // the loop. Without this, however, there will be some heap overflows due to
-  // the default being a lower resolution (320x240).
-  cfg.g_w = kFrameSizes[0].width;
-  cfg.g_h = kFrameSizes[0].height;
 
   aom_codec_ctx_t ctx;
   EXPECT_EQ(aom_codec_enc_init(&ctx, iface, &cfg, 0), AOM_CODEC_OK);