Commit 06c52ae1ac for aom
commit 06c52ae1ac98151eff2212836eb146f08498f119
Author: Marvin Scholz <marvinscholz@xwf.google.com>
Date: Thu Nov 26 21:53:04 2026 +0000
Add a test for rate control overflows at max bitrate
Add an encode API test that encodes a few frames at the maximum target
bitrate and one frame per second, so that the average per-frame bit
budget is 2000000000 bits. It does so twice, with all frames as key
frames and with a key frame followed by inter frames, because the two
cases take different paths in the rate control.
Note that a UBSan build is needed for this test to trigger any visible
issues.
Change-Id: I978095e0c633284a0a1655d92d475384d7a760b8
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index a6a26f55b6..33bb20b4a4 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -1617,6 +1617,67 @@ TEST(EncodeAPI, AomediaIssue3509VbrMinSection101Percent) {
ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
}
+#if !CONFIG_REALTIME_ONLY
+// Encodes a few frames at the maximum target bitrate and one frame per second.
+// The average per-frame bit budget is 2000000000 bits, which is greater than
+// INT_MAX / 2 and causes signed integer overflows in the rate control if the
+// calculations are done in the `int` type.
+TEST(EncodeAPI, MaxBitrateOneFramePerSecond) {
+ // Encode all frames as key frames first, then a key frame followed by inter
+ // frames. The two cases take different paths in the rate control.
+ for (const unsigned int kf_max_dist : { 0u, 9999u }) {
+ SCOPED_TRACE(kf_max_dist);
+ // Initialize libaom encoder.
+ aom_codec_iface_t *const iface = aom_codec_av1_cx();
+ aom_codec_ctx_t enc;
+ aom_codec_enc_cfg_t cfg;
+
+ ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_GOOD_QUALITY),
+ AOM_CODEC_OK);
+
+ cfg.g_w = 64;
+ cfg.g_h = 64;
+ cfg.g_timebase.num = 1;
+ cfg.g_timebase.den = 1;
+ cfg.rc_target_bitrate = 2000000;
+ cfg.kf_max_dist = kf_max_dist;
+
+ ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
+ ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, 5), AOM_CODEC_OK);
+
+ // Create input image.
+ aom_image_t *const image =
+ CreateGrayImage(AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h);
+ ASSERT_NE(image, nullptr);
+
+ // Encode frames.
+ for (int i = 0; i < 4; ++i) {
+ ASSERT_EQ(aom_codec_encode(&enc, image, i, /*duration=*/1, 0),
+ AOM_CODEC_OK);
+ aom_codec_iter_t iter = nullptr;
+ while (aom_codec_get_cx_data(&enc, &iter) != nullptr) {
+ // Ignore the output.
+ }
+ }
+
+ // Flush the encoder.
+ bool got_data;
+ do {
+ ASSERT_EQ(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK);
+ got_data = false;
+ aom_codec_iter_t iter = nullptr;
+ while (aom_codec_get_cx_data(&enc, &iter) != nullptr) {
+ got_data = true;
+ }
+ } while (got_data);
+
+ // Free resources.
+ aom_img_free(image);
+ ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+ }
+}
+#endif // !CONFIG_REALTIME_ONLY
+
TEST(EncodeAPI, Buganizer392929025) {
// Initialize libaom encoder.
aom_codec_iface_t *const iface = aom_codec_av1_cx();