Commit 771e3f036c for aom

commit 771e3f036cf72878b0f9794681e0eead3c14954d
Author: Alejandro Pacheco <alepacheco99@gmail.com>
Date:   Wed Oct 14 10:00:11 2026 +0200

    superres: pick the source by its size, not the render size

    av1_superres_post_encode() kept the full size source when the upscaled
    frame size matched the render size. The render size is set by the
    application (AV1E_SET_RENDER_SIZE), so with resize and superres both
    on and a render size equal to the resized size, the full size source
    was kept while the reconstruction had the smaller resized size. The
    PSNR computation then read the reconstruction past its end, and the
    restoration search compared mismatched frames.

    Compare the upscaled size with the size of the unscaled source
    instead, and add a test for this configuration.

    Bug: 573427857
    Change-Id: I9dc9409f82c9fc026447ea3affce1d5c36f93fa1

diff --git a/av1/encoder/superres_scale.c b/av1/encoder/superres_scale.c
index 8b96357ccc..36188197ce 100644
--- a/av1/encoder/superres_scale.c
+++ b/av1/encoder/superres_scale.c
@@ -408,13 +408,13 @@ void av1_superres_post_encode(AV1_COMP *cpi) {

   // If regular resizing is occurring the source will need to be downscaled to
   // match the upscaled superres resolution. Otherwise the original source is
-  // used.
-  if (!av1_resize_scaled(cm)) {
+  // used. Compare with the source size rather than the render size, which the
+  // application may set to any value.
+  if (cpi->unscaled_source->y_crop_width == cm->superres_upscaled_width &&
+      cpi->unscaled_source->y_crop_height == cm->superres_upscaled_height) {
     cpi->source = cpi->unscaled_source;
     if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
   } else {
-    assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
-    assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
     // Do downscale. cm->(width|height) has been updated by
     // av1_superres_upscale
     cpi->source = realloc_and_scale_source(cpi, cm->superres_upscaled_width,
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index 20d928a3a7..fe185b6315 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -1726,6 +1726,67 @@ TEST(EncodeAPI, IntraBCFrameSizeIncreaseGoodQuality) {
 TEST(EncodeAPI, IntraBCFrameSizeIncreaseAllIntra) {
   IntraBCFrameSizeIncreaseTest(AOM_USAGE_ALL_INTRA, 5);
 }
+
+// With resize and superres, a render size equal to the resized frame size
+// must not make the encoder compare the full size source with the smaller
+// upscaled frame.
+TEST(EncodeAPI, SuperresResizeWithRenderSize) {
+  constexpr int kWidth = 224;
+  constexpr int kHeight = 224;
+  // 224 * 8 / 14 = 128.
+  constexpr int kResizedSize = 128;
+  aom_codec_enc_cfg_t cfg;
+  ASSERT_EQ(aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg,
+                                         AOM_USAGE_GOOD_QUALITY),
+            AOM_CODEC_OK);
+  cfg.g_w = kWidth;
+  cfg.g_h = kHeight;
+  cfg.g_lag_in_frames = 0;
+  cfg.rc_end_usage = AOM_Q;
+  cfg.rc_resize_mode = 1;  // RESIZE_FIXED
+  cfg.rc_resize_denominator = 14;
+  cfg.rc_resize_kf_denominator = 14;
+  cfg.rc_superres_mode = AOM_SUPERRES_FIXED;
+  cfg.rc_superres_denominator = 9;
+  cfg.rc_superres_kf_denominator = 9;
+
+  aom_codec_ctx_t enc;
+  ASSERT_EQ(
+      aom_codec_enc_init(&enc, aom_codec_av1_cx(), &cfg, AOM_CODEC_USE_PSNR),
+      AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, 6), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CQ_LEVEL, 40), AOM_CODEC_OK);
+  int render_size[2] = { kResizedSize, kResizedSize };
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_RENDER_SIZE, render_size),
+            AOM_CODEC_OK);
+
+  std::unique_ptr<aom_image_t, decltype(&aom_img_free)> image(
+      aom_img_alloc(nullptr, AOM_IMG_FMT_I420, kWidth, kHeight, 32),
+      &aom_img_free);
+  aom_image_t *img = image.get();
+  ASSERT_NE(img, nullptr);
+  for (int frame = 0; frame < 2; ++frame) {
+    for (int y = 0; y < kHeight; ++y) {
+      for (int x = 0; x < kWidth; ++x) {
+        img->planes[AOM_PLANE_Y][y * img->stride[AOM_PLANE_Y] + x] =
+            static_cast<uint8_t>((x * 7 + y * 3 + frame * 5) ^ (x * y));
+      }
+    }
+    for (const int plane : { AOM_PLANE_U, AOM_PLANE_V }) {
+      for (int y = 0; y < aom_img_plane_height(img, plane); ++y) {
+        memset(img->planes[plane] + y * img->stride[plane], 128 + frame,
+               aom_img_plane_width(img, plane));
+      }
+    }
+    ASSERT_EQ(aom_codec_encode(&enc, img, frame, /*duration=*/1, /*flags=*/0),
+              AOM_CODEC_OK)
+        << aom_codec_error_detail(&enc);
+    aom_codec_iter_t iter = nullptr;
+    while (aom_codec_get_cx_data(&enc, &iter) != nullptr) {
+    }
+  }
+  EXPECT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+}
 #endif  // !CONFIG_REALTIME_ONLY

 TEST(EncodeAPI, IntraBCFrameSizeIncreaseRealtime) {