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