1 /*
2 * Copyright (c) 2021, 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 <cstdlib>
13
14 #include "gtest/gtest.h"
15 #include "test/codec_factory.h"
16 #include "test/encode_test_driver.h"
17 #include "test/i420_video_source.h"
18 #include "test/util.h"
19
20 namespace {
21
22 const double kPsnrDiffThreshold = 0.1;
23
24 // Params: first pass cpu used, second pass cpu used
25 class CpuUsedFirstpassTest
26 : public ::libaom_test::CodecTestWith2Params<int, int>,
27 public ::libaom_test::EncoderTest {
28 protected:
CpuUsedFirstpassTest()29 CpuUsedFirstpassTest()
30 : EncoderTest(GET_PARAM(0)), second_pass_cpu_used_(GET_PARAM(2)) {}
31 ~CpuUsedFirstpassTest() override = default;
32
SetUp()33 void SetUp() override {
34 InitializeConfig(::libaom_test::kTwoPassGood);
35 const aom_rational timebase = { 1, 30 };
36 cfg_.g_timebase = timebase;
37 cfg_.rc_end_usage = AOM_VBR;
38 cfg_.rc_target_bitrate = 1000;
39 cfg_.g_lag_in_frames = 19;
40 cfg_.g_threads = 0;
41 init_flags_ = AOM_CODEC_USE_PSNR;
42 }
43
BeginPassHook(unsigned int pass)44 void BeginPassHook(unsigned int pass) override {
45 psnr_ = 0.0;
46 nframes_ = 0;
47
48 if (pass == 0)
49 cpu_used_ = first_pass_cpu_used_;
50 else
51 cpu_used_ = second_pass_cpu_used_;
52 }
53
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)54 void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
55 psnr_ += pkt->data.psnr.psnr[0];
56 nframes_++;
57 }
58
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)59 void PreEncodeFrameHook(::libaom_test::VideoSource *video,
60 ::libaom_test::Encoder *encoder) override {
61 if (video->frame() == 0) {
62 encoder->Control(AOME_SET_CPUUSED, cpu_used_);
63 encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
64 encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
65 encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
66 }
67 }
68
GetAveragePsnr() const69 double GetAveragePsnr() const {
70 if (nframes_) return psnr_ / nframes_;
71 return 0.0;
72 }
73
GetPsnrDiffThreshold()74 double GetPsnrDiffThreshold() { return kPsnrDiffThreshold; }
75
DoTest()76 void DoTest() {
77 if (GET_PARAM(1) == second_pass_cpu_used_) {
78 GTEST_SKIP() << "Reference cpu used values match test cpu used values.";
79 }
80
81 libaom_test::I420VideoSource video("niklas_640_480_30.yuv", 640, 480,
82 cfg_.g_timebase.den, cfg_.g_timebase.num,
83 0, 30);
84 double ref_psnr;
85 double psnr_diff;
86
87 first_pass_cpu_used_ = second_pass_cpu_used_;
88 ASSERT_NO_FATAL_FAILURE(RunLoop(&video)); // same preset case ref_psnr
89 ref_psnr = GetAveragePsnr();
90
91 first_pass_cpu_used_ = GET_PARAM(1);
92 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
93 psnr_diff = std::abs(ref_psnr - GetAveragePsnr());
94 EXPECT_LT(psnr_diff, GetPsnrDiffThreshold())
95 << "first pass cpu used = " << first_pass_cpu_used_
96 << ", second pass cpu used = " << second_pass_cpu_used_;
97 }
98
99 int cpu_used_;
100 int first_pass_cpu_used_;
101 int second_pass_cpu_used_;
102 unsigned int nframes_;
103 double psnr_;
104 };
105
TEST_P(CpuUsedFirstpassTest,FirstPassTest)106 TEST_P(CpuUsedFirstpassTest, FirstPassTest) { DoTest(); }
107
108 class CpuUsedFirstpassTestLarge : public CpuUsedFirstpassTest {};
109
TEST_P(CpuUsedFirstpassTestLarge,FirstPassTest)110 TEST_P(CpuUsedFirstpassTestLarge, FirstPassTest) { DoTest(); }
111
112 #if defined(__has_feature)
113 #if __has_feature(memory_sanitizer)
114 static const int kSecondPassCpuUsedLarge[] = { 2, 4 };
115 static const int kSecondPassCpuUsed[] = { 6 };
116 #else
117 static const int kSecondPassCpuUsedLarge[] = { 2 };
118 static const int kSecondPassCpuUsed[] = { 4, 6 };
119 #endif
120 #else
121 static const int kSecondPassCpuUsedLarge[] = { 2 };
122 static const int kSecondPassCpuUsed[] = { 4, 6 };
123 #endif
124
125 AV1_INSTANTIATE_TEST_SUITE(
126 CpuUsedFirstpassTestLarge, ::testing::Values(2, 4, 6),
127 ::testing::ValuesIn(kSecondPassCpuUsedLarge)); // cpu_used
128
129 AV1_INSTANTIATE_TEST_SUITE(
130 CpuUsedFirstpassTest, ::testing::Values(2, 4, 6),
131 ::testing::ValuesIn(kSecondPassCpuUsed)); // cpu_used
132
133 } // namespace
134