Do not use adaptive error estimate

When the reference frame size is different than the current,
we will not use adaptive error estimate.

STATS_CHANGED

Bug: b:314858909
Change-Id: Ic64d9b4a1d94889d7283c044b17ffc24627478d7
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 02c89bc..253a310 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3601,6 +3601,8 @@
 
   /*!
    * SSE between the current frame and the reconstructed last frame
+   * It is only used for CBR mode.
+   * It is not used if the reference frame has a different frame size.
    */
   uint64_t rec_sse;
 
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index 2000111..ecbfa9e 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -188,8 +188,7 @@
          correction_factor >= MIN_BPB_FACTOR);
 
   if (cpi->oxcf.rc_cfg.mode == AOM_CBR && frame_type != KEY_FRAME &&
-      accurate_estimate) {
-    assert(cpi->rec_sse != UINT64_MAX);
+      accurate_estimate && cpi->rec_sse != UINT64_MAX) {
     const int mbs = cm->mi_params.MBs;
     const double sse_sqrt =
         (double)((int)sqrt((double)(cpi->rec_sse)) << BPER_MB_NORMBITS) /
@@ -2086,6 +2085,13 @@
   // TODO(yunqing): support scaled reference frames.
   if (cpi->scaled_ref_buf[LAST_FRAME - 1]) return;
 
+  for (int i = 0; i < 2; ++i) {
+    if (unscaled_src->widths[i] != yv12->widths[i] ||
+        unscaled_src->heights[i] != yv12->heights[i]) {
+      return;
+    }
+  }
+
   const int num_mi_cols = cm->mi_params.mi_cols;
   const int num_mi_rows = cm->mi_params.mi_rows;
   const BLOCK_SIZE bsize = BLOCK_64X64;
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index 044519a..7613056 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -392,7 +392,6 @@
  public:
   explicit AV1Encoder(int speed) : speed_(speed) {}
   ~AV1Encoder();
-
   void Configure(unsigned int threads, unsigned int width, unsigned int height,
                  aom_rc_mode end_usage, unsigned int usage);
   void Encode(bool key_frame);
@@ -483,6 +482,25 @@
   } while (got_data);
 }
 
+TEST(EncodeAPI, Buganizer314858909) {
+  AV1Encoder encoder(7);
+
+  encoder.Configure(6, 1582, 750, AOM_CBR, AOM_USAGE_REALTIME);
+
+  // Encode a frame.
+  encoder.Encode(false);
+
+  encoder.Configure(0, 1582, 23, AOM_CBR, AOM_USAGE_REALTIME);
+
+  // Encode a frame..
+  encoder.Encode(false);
+
+  encoder.Configure(16, 1542, 363, AOM_CBR, AOM_USAGE_REALTIME);
+
+  // Encode a frame..
+  encoder.Encode(false);
+}
+
 // Run this test to reproduce the bug in fuzz test: ASSERT: cpi->rec_sse !=
 // UINT64_MAX in av1_rc_bits_per_mb.
 TEST(EncodeAPI, Buganizer310766628) {