av1: Disable complexity AQ when base_qindex is 0 av1_setup_in_frame_q_adj() prevents non-zero base_qindex frames from assigning qindex 0 to a segment, because qindex 0 with zero DC/AC deltas uses 4x4 lossless transforms while complexity AQ (aq-mode=2) reassigns segment IDs after mode selection without re-running RD. However, when base_qindex itself was 0, segment 4 still received a positive qindex delta, mixing a lossy segment into a lossless frame with incompatible transform sizes. Disable segmentation and clear segment features on AQ-enabled frames when base_qindex is 0, and defensively skip av1_caq_select_segment() when segmentation is disabled. As before, segments with a zero qindex delta keep SEG_LVL_ALT_Q enabled so last_active_segid stays AQ_C_SEGMENTS - 1; no change for frames with base_qindex > 0. Add two-pass complexity AQ regression tests to test/aq_segment_test.cc for base_qindex 0 and for fixed q (best quality == worst quality), where every segment qindex delta is 0. Bug: 589732449 TAG=agy CONV=490488d4-7005-402a-8cc4-c245227f80fe Change-Id: I083aea3beb26a71aa75328d43414689bd3421616
diff --git a/av1/encoder/aq_complexity.c b/av1/encoder/aq_complexity.c index a497523..a83aacc 100644 --- a/av1/encoder/aq_complexity.c +++ b/av1/encoder/aq_complexity.c
@@ -78,6 +78,19 @@ if (is_frame_aq_enabled(cpi)) { int segment; + + // Disable segmentation when SB-level AQ is disabled or base_qindex is 0. + // A qindex of 0 with zero DC/AC deltas is lossless (4x4 WHT only), and AQ + // mode 2 reassigns segment IDs after mode selection without re-running RD, + // so lossless and lossy segments must not be mixed. + if (!is_sb_aq_enabled(cpi) || base_qindex == 0) { + memset(cpi->enc_seg.map, 0, + cm->mi_params.mi_rows * cm->mi_params.mi_cols); + av1_clearall_segfeatures(seg); + av1_disable_segmentation(seg); + return; + } + const int aq_strength = get_aq_c_strength(base_qindex, cm->seq_params->bit_depth); @@ -87,11 +100,6 @@ av1_clearall_segfeatures(seg); - if (!is_sb_aq_enabled(cpi)) { - av1_disable_segmentation(seg); - return; - } - av1_enable_segmentation(seg); // Default segment "Q" feature is disabled so it defaults to the baseline Q. @@ -129,8 +137,10 @@ // bits for the block vs a target average and its spatial complexity. void av1_caq_select_segment(const AV1_COMP *cpi, MACROBLOCK *mb, BLOCK_SIZE bs, int mi_row, int mi_col, int projected_rate) { - if ((!is_frame_aq_enabled(cpi)) || (!is_sb_aq_enabled(cpi))) return; const AV1_COMMON *const cm = &cpi->common; + if (!is_frame_aq_enabled(cpi) || !is_sb_aq_enabled(cpi) || !cm->seg.enabled) { + return; + } const int num_planes = av1_num_planes(cm); const int mi_offset = mi_row * cm->mi_params.mi_cols + mi_col;
diff --git a/test/aq_segment_test.cc b/test/aq_segment_test.cc index 9e8e42d..f0a69d4 100644 --- a/test/aq_segment_test.cc +++ b/test/aq_segment_test.cc
@@ -9,13 +9,20 @@ * PATENTS file, you can obtain it at www.aomedia.org/license/patent. */ +#include <algorithm> +#include <cstdint> + #include "config/aom_config.h" +#include "aom/aom_encoder.h" +#include "aom/aom_image.h" +#include "aom/aomcx.h" #include "gtest/gtest.h" #include "test/codec_factory.h" #include "test/encode_test_driver.h" #include "test/i420_video_source.h" #include "test/util.h" +#include "test/video_source.h" namespace { @@ -106,5 +113,150 @@ AV1_INSTANTIATE_TEST_SUITE(AqSegmentTestLarge, ::testing::Values(::libaom_test::kOnePassGood), ::testing::Range(3, 5), ::testing::Range(0, 4)); -#endif + +// Synthetic source with low-frequency gradients and textured patches so +// complexity AQ exercises large superblocks and high-complexity segments when +// base_qindex reaches 0. +class GradientTextureVideoSource : public ::libaom_test::DummyVideoSource { + protected: + void FillFrame() override { + ASSERT_NE(img_, nullptr); + const unsigned int f = frame_; + for (int plane = 0; plane < 3; ++plane) { + const int w = aom_img_plane_width(img_, plane); + const int h = aom_img_plane_height(img_, plane); + const int stride = img_->stride[plane]; + uint8_t *const buf = img_->planes[plane]; + for (int r = 0; r < h; ++r) { + for (int c = 0; c < w; ++c) { + if (plane == 0 && c >= w / 2) { + buf[r * stride + c] = + static_cast<uint8_t>((r * 37 + c * 53 + f * 19) & 0xff); + } else { + buf[r * stride + c] = + static_cast<uint8_t>((128 + ((r + c + f * 3) >> 2)) & 0xff); + } + } + } + } + } +}; + +// Maps a 0-63 quantizer level to a qindex, matching av1_quantizer_to_qindex(). +int QuantizerToQindex(int quantizer) { + if (quantizer >= 62) return quantizer == 62 ? 249 : 255; + return 4 * quantizer; +} + +// Fixture for complexity AQ (aq-mode=2) regression tests. The parameter is a +// 0-63 quantizer level whose meaning depends on the test. Records the min/max +// base_qindex over the frames that produced a compressed frame packet. +class AqComplexityTest + : public ::libaom_test::CodecTestWith2Params<libaom_test::TestMode, int>, + public ::libaom_test::EncoderTest { + protected: + AqComplexityTest() : EncoderTest(GET_PARAM(0)), q_level_(GET_PARAM(2)) {} + ~AqComplexityTest() override = default; + + void SetUp() override { + InitializeConfig(GET_PARAM(1)); + cfg_.rc_end_usage = AOM_Q; + cfg_.rc_min_quantizer = 0; + cfg_.rc_max_quantizer = 63; + // In two-pass encoding, rc_target_bitrate drives rc.sb64_target_rate + // even in AOM_Q mode. + cfg_.rc_target_bitrate = 5000; + } + + void BeginPassHook(unsigned int /*pass*/) override { + encoder_ = nullptr; + min_qindex_ = 256; + max_qindex_ = -1; + encoded_frames_ = 0; + } + + void EndPassHook() override { encoder_ = nullptr; } + + void PreEncodeFrameHook(::libaom_test::VideoSource *video, + ::libaom_test::Encoder *encoder) override { + encoder_ = encoder; + if (video->frame() == 0) { + encoder->Control(AOME_SET_CPUUSED, 5); + encoder->Control(AV1E_SET_AQ_MODE, 2); + encoder->Control(AOME_SET_CQ_LEVEL, q_level_); + encoder->Control(AV1E_SET_SUPERBLOCK_SIZE, AOM_SUPERBLOCK_SIZE_128X128); + } + } + + // AOME_GET_LAST_QUANTIZER reports the base_qindex of the last frame encoded + // by the preceding aom_codec_encode() call. + void FramePktHook(const aom_codec_cx_pkt_t * /*pkt*/) override { + ASSERT_NE(encoder_, nullptr); + int qindex = -1; + encoder_->Control(AOME_GET_LAST_QUANTIZER, &qindex); + min_qindex_ = std::min(min_qindex_, qindex); + max_qindex_ = std::max(max_qindex_, qindex); + ++encoded_frames_; + } + + void RunTest() { + GradientTextureVideoSource video; + video.SetSize(256, 256); + video.set_limit(kNumFrames); + ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); + EXPECT_EQ(encoded_frames_, kNumFrames); + } + + static constexpr int kNumFrames = 5; + const int q_level_; + ::libaom_test::Encoder *encoder_ = nullptr; + int min_qindex_ = 256; + int max_qindex_ = -1; + int encoded_frames_ = 0; +}; + +// In one-pass (LAP) AOM_Q mode this_frame_target is 0, so +// rc.sb64_target_rate is 0 and complexity AQ never selects segments; use +// two-pass. +class AqComplexityLowCqTest : public AqComplexityTest {}; + +// Regression test for aomedia:589732449: verify complexity AQ encodes and +// decodes cleanly when rate control selects base_qindex 0. The cq_level 1 key +// frame reaches qindex 0 because its TPL-based q is clamped only to +// rc_min_quantizer. +TEST_P(AqComplexityLowCqTest, EncodeDecodeLowCq) { + RunTest(); + EXPECT_EQ(min_qindex_, 0); + if (q_level_ == 0) { + EXPECT_EQ(max_qindex_, 0); + } else { + EXPECT_GT(max_qindex_, 0); + } +} + +AV1_INSTANTIATE_TEST_SUITE(AqComplexityLowCqTest, + ::testing::Values(::libaom_test::kTwoPassGood), + ::testing::Values(0, 1)); + +class AqComplexityFixedQTest : public AqComplexityTest {}; + +// With rc_min_quantizer equal to rc_max_quantizer, rc->best_quality == +// rc->worst_quality, so every segment qindex delta is 0. All AQ segments must +// remain active since av1_caq_select_segment() assigns segment IDs after RD. +// Use a moderate bitrate target so high-Q blocks still exceed target_rate and +// exercise non-zero segment IDs. +TEST_P(AqComplexityFixedQTest, EncodeDecodeFixedQ) { + cfg_.rc_min_quantizer = q_level_; + cfg_.rc_max_quantizer = q_level_; + cfg_.rc_target_bitrate = 200; + RunTest(); + const int expected_qindex = QuantizerToQindex(q_level_); + EXPECT_EQ(min_qindex_, expected_qindex); + EXPECT_EQ(max_qindex_, expected_qindex); +} + +AV1_INSTANTIATE_TEST_SUITE(AqComplexityFixedQTest, + ::testing::Values(::libaom_test::kTwoPassGood), + ::testing::Values(16, 32, 48, 63)); +#endif // !CONFIG_REALTIME_ONLY } // namespace