xref: /aosp_15_r20/external/libaom/test/sb_qp_sweep_test.cc (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2022, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <initializer_list>
13*77c1e3ccSAndroid Build Coastguard Worker #include <memory>
14*77c1e3ccSAndroid Build Coastguard Worker #include <string>
15*77c1e3ccSAndroid Build Coastguard Worker #include <vector>
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "test/md5_helper.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "test/yuv_video_source.h"
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker namespace {
25*77c1e3ccSAndroid Build Coastguard Worker 
26*77c1e3ccSAndroid Build Coastguard Worker // Parameters: cpu-used, row-mt.
27*77c1e3ccSAndroid Build Coastguard Worker class AV1SBQPSweepTest : public ::libaom_test::CodecTestWith2Params<int, bool>,
28*77c1e3ccSAndroid Build Coastguard Worker                          public ::libaom_test::EncoderTest {
29*77c1e3ccSAndroid Build Coastguard Worker  protected:
AV1SBQPSweepTest()30*77c1e3ccSAndroid Build Coastguard Worker   AV1SBQPSweepTest()
31*77c1e3ccSAndroid Build Coastguard Worker       : EncoderTest(GET_PARAM(0)), set_cpu_used_(GET_PARAM(1)),
32*77c1e3ccSAndroid Build Coastguard Worker         row_mt_(GET_PARAM(2)) {
33*77c1e3ccSAndroid Build Coastguard Worker     init_flags_ = AOM_CODEC_USE_PSNR;
34*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_dec_cfg_t cfg = aom_codec_dec_cfg_t();
35*77c1e3ccSAndroid Build Coastguard Worker     cfg.w = 1280;
36*77c1e3ccSAndroid Build Coastguard Worker     cfg.h = 720;
37*77c1e3ccSAndroid Build Coastguard Worker     cfg.allow_lowbitdepth = 1;
38*77c1e3ccSAndroid Build Coastguard Worker     decoder_ =
39*77c1e3ccSAndroid Build Coastguard Worker         std::unique_ptr<::libaom_test::Decoder>(codec_->CreateDecoder(cfg, 0));
40*77c1e3ccSAndroid Build Coastguard Worker   }
41*77c1e3ccSAndroid Build Coastguard Worker   ~AV1SBQPSweepTest() override = default;
42*77c1e3ccSAndroid Build Coastguard Worker 
SetUp()43*77c1e3ccSAndroid Build Coastguard Worker   void SetUp() override {
44*77c1e3ccSAndroid Build Coastguard Worker     InitializeConfig(::libaom_test::kTwoPassGood);
45*77c1e3ccSAndroid Build Coastguard Worker 
46*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NE(decoder_, nullptr);
47*77c1e3ccSAndroid Build Coastguard Worker     if (decoder_->IsAV1()) {
48*77c1e3ccSAndroid Build Coastguard Worker       decoder_->Control(AV1_SET_DECODE_TILE_ROW, -1);
49*77c1e3ccSAndroid Build Coastguard Worker       decoder_->Control(AV1_SET_DECODE_TILE_COL, -1);
50*77c1e3ccSAndroid Build Coastguard Worker     }
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker     cfg_.g_lag_in_frames = 5;
53*77c1e3ccSAndroid Build Coastguard Worker     cfg_.rc_end_usage = AOM_Q;
54*77c1e3ccSAndroid Build Coastguard Worker   }
55*77c1e3ccSAndroid Build Coastguard Worker 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)56*77c1e3ccSAndroid Build Coastguard Worker   void PreEncodeFrameHook(::libaom_test::VideoSource *video,
57*77c1e3ccSAndroid Build Coastguard Worker                           ::libaom_test::Encoder *encoder) override {
58*77c1e3ccSAndroid Build Coastguard Worker     if (video->frame() == 0) {
59*77c1e3ccSAndroid Build Coastguard Worker       SetTileSize(encoder);
60*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_CPUUSED, set_cpu_used_);
61*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AV1E_ENABLE_SB_QP_SWEEP, use_sb_sweep_);
62*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AV1E_SET_ROW_MT, row_mt_);
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
65*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
66*77c1e3ccSAndroid Build Coastguard Worker       encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
67*77c1e3ccSAndroid Build Coastguard Worker     }
68*77c1e3ccSAndroid Build Coastguard Worker   }
69*77c1e3ccSAndroid Build Coastguard Worker 
SetTileSize(libaom_test::Encoder * encoder)70*77c1e3ccSAndroid Build Coastguard Worker   virtual void SetTileSize(libaom_test::Encoder *encoder) {
71*77c1e3ccSAndroid Build Coastguard Worker     encoder->Control(AV1E_SET_TILE_COLUMNS, 1);
72*77c1e3ccSAndroid Build Coastguard Worker     encoder->Control(AV1E_SET_TILE_ROWS, 1);
73*77c1e3ccSAndroid Build Coastguard Worker   }
74*77c1e3ccSAndroid Build Coastguard Worker 
BeginPassHook(unsigned int)75*77c1e3ccSAndroid Build Coastguard Worker   void BeginPassHook(unsigned int) override {
76*77c1e3ccSAndroid Build Coastguard Worker     psnr_ = 0.0;
77*77c1e3ccSAndroid Build Coastguard Worker     nframes_ = 0;
78*77c1e3ccSAndroid Build Coastguard Worker   }
79*77c1e3ccSAndroid Build Coastguard Worker 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)80*77c1e3ccSAndroid Build Coastguard Worker   void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
81*77c1e3ccSAndroid Build Coastguard Worker     psnr_ += pkt->data.psnr.psnr[0];
82*77c1e3ccSAndroid Build Coastguard Worker     nframes_++;
83*77c1e3ccSAndroid Build Coastguard Worker   }
84*77c1e3ccSAndroid Build Coastguard Worker 
GetAveragePsnr() const85*77c1e3ccSAndroid Build Coastguard Worker   double GetAveragePsnr() const {
86*77c1e3ccSAndroid Build Coastguard Worker     if (nframes_) return psnr_ / nframes_;
87*77c1e3ccSAndroid Build Coastguard Worker     return 0.0;
88*77c1e3ccSAndroid Build Coastguard Worker   }
89*77c1e3ccSAndroid Build Coastguard Worker 
GetAverageFrameSize() const90*77c1e3ccSAndroid Build Coastguard Worker   double GetAverageFrameSize() const {
91*77c1e3ccSAndroid Build Coastguard Worker     if (nframes_) return psnr_ / nframes_;
92*77c1e3ccSAndroid Build Coastguard Worker     return 0.0;
93*77c1e3ccSAndroid Build Coastguard Worker   }
94*77c1e3ccSAndroid Build Coastguard Worker 
FramePktHook(const aom_codec_cx_pkt_t * pkt)95*77c1e3ccSAndroid Build Coastguard Worker   void FramePktHook(const aom_codec_cx_pkt_t *pkt) override {
96*77c1e3ccSAndroid Build Coastguard Worker     sum_frame_size_ += pkt->data.frame.sz;
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker     const aom_codec_err_t res = decoder_->DecodeFrame(
99*77c1e3ccSAndroid Build Coastguard Worker         reinterpret_cast<uint8_t *>(pkt->data.frame.buf), pkt->data.frame.sz);
100*77c1e3ccSAndroid Build Coastguard Worker     if (res != AOM_CODEC_OK) {
101*77c1e3ccSAndroid Build Coastguard Worker       abort_ = true;
102*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_EQ(AOM_CODEC_OK, res);
103*77c1e3ccSAndroid Build Coastguard Worker     }
104*77c1e3ccSAndroid Build Coastguard Worker   }
105*77c1e3ccSAndroid Build Coastguard Worker 
DoTest()106*77c1e3ccSAndroid Build Coastguard Worker   void DoTest() {
107*77c1e3ccSAndroid Build Coastguard Worker     ::libaom_test::YUVVideoSource video(
108*77c1e3ccSAndroid Build Coastguard Worker         "niklas_640_480_30.yuv", AOM_IMG_FMT_I420, 640, 480, 30, 1, 0, 6);
109*77c1e3ccSAndroid Build Coastguard Worker     cfg_.rc_target_bitrate = 1000;
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker     // Encode without sb_qp_sweep
112*77c1e3ccSAndroid Build Coastguard Worker     use_sb_sweep_ = false;
113*77c1e3ccSAndroid Build Coastguard Worker     sum_frame_size_ = 0;
114*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
115*77c1e3ccSAndroid Build Coastguard Worker     const double psnr_1 = GetAveragePsnr();
116*77c1e3ccSAndroid Build Coastguard Worker     const size_t avg_frame_size_1 = sum_frame_size_ / nframes_;
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker     // Encode with sb_qp_sweep
119*77c1e3ccSAndroid Build Coastguard Worker     use_sb_sweep_ = true;
120*77c1e3ccSAndroid Build Coastguard Worker     sum_frame_size_ = 0;
121*77c1e3ccSAndroid Build Coastguard Worker     ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
122*77c1e3ccSAndroid Build Coastguard Worker     const double psnr_2 = GetAveragePsnr();
123*77c1e3ccSAndroid Build Coastguard Worker     const size_t avg_frame_size_2 = sum_frame_size_ / nframes_;
124*77c1e3ccSAndroid Build Coastguard Worker 
125*77c1e3ccSAndroid Build Coastguard Worker     if (psnr_1 >= psnr_2) {
126*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_GE(avg_frame_size_1, avg_frame_size_2);
127*77c1e3ccSAndroid Build Coastguard Worker     }
128*77c1e3ccSAndroid Build Coastguard Worker     if (avg_frame_size_1 <= avg_frame_size_2) {
129*77c1e3ccSAndroid Build Coastguard Worker       ASSERT_LE(psnr_1, psnr_2);
130*77c1e3ccSAndroid Build Coastguard Worker     }
131*77c1e3ccSAndroid Build Coastguard Worker   }
132*77c1e3ccSAndroid Build Coastguard Worker 
133*77c1e3ccSAndroid Build Coastguard Worker   bool use_sb_sweep_;
134*77c1e3ccSAndroid Build Coastguard Worker   int set_cpu_used_;
135*77c1e3ccSAndroid Build Coastguard Worker   bool row_mt_;
136*77c1e3ccSAndroid Build Coastguard Worker   double psnr_;
137*77c1e3ccSAndroid Build Coastguard Worker   unsigned int nframes_;
138*77c1e3ccSAndroid Build Coastguard Worker   size_t sum_frame_size_;
139*77c1e3ccSAndroid Build Coastguard Worker   std::unique_ptr<::libaom_test::Decoder> decoder_;
140*77c1e3ccSAndroid Build Coastguard Worker };
141*77c1e3ccSAndroid Build Coastguard Worker 
TEST_P(AV1SBQPSweepTest,SweepMatchTest)142*77c1e3ccSAndroid Build Coastguard Worker TEST_P(AV1SBQPSweepTest, SweepMatchTest) { DoTest(); }
143*77c1e3ccSAndroid Build Coastguard Worker 
144*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(AV1SBQPSweepTest, ::testing::Range(4, 6),
145*77c1e3ccSAndroid Build Coastguard Worker                            ::testing::Bool());
146*77c1e3ccSAndroid Build Coastguard Worker 
147*77c1e3ccSAndroid Build Coastguard Worker }  // namespace
148