Partialy revert commit dc2c3eb

Note: This is on the ironbark branch for the libaom 3.7.2 patch release.

Fix issue aomedia:3526 -- alloc_compressor_data() is called during every
aom_codec_control() call on the encoder.

Bug: aomedia:3349, aomedia:3526
Change-Id: I9c078abd4765d7546346dba015f46de33932fc19
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index e1cb49b..6f1c92a 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -907,16 +907,18 @@
   cm->width = frm_dim_cfg->width;
   cm->height = frm_dim_cfg->height;
 
-  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 (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;
+    }
   }
   av1_update_frame_size(cpi);
 
diff --git a/test/frame_size_tests.cc b/test/frame_size_tests.cc
index 3b35db8..74502cd 100644
--- a/test/frame_size_tests.cc
+++ b/test/frame_size_tests.cc
@@ -116,6 +116,12 @@
   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);
@@ -255,6 +261,12 @@
   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[2].width;
+  cfg.g_h = kFrameSizes[2].height;
 
   aom_codec_ctx_t ctx;
   EXPECT_EQ(aom_codec_enc_init(&ctx, iface, &cfg, 0), AOM_CODEC_OK);