1 /*
2 * Copyright (c) 2019, 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 <memory>
13 #include <ostream>
14 #include <string>
15 #include <unordered_map>
16
17 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
18
19 #include "test/codec_factory.h"
20 #include "test/encode_test_driver.h"
21 #include "test/util.h"
22 #include "test/y4m_video_source.h"
23 #include "test/yuv_video_source.h"
24
25 namespace {
26
27 const unsigned int kFrames = 10;
28 const int kBitrate = 500;
29
30 // List of psnr thresholds for speed settings 6-8
31 // keys: video, speed, aq mode.
32 std::unordered_map<std::string,
33 std::unordered_map<int, std::unordered_map<int, double>>>
34 kPsnrThreshold = { { "park_joy_90p_8_420.y4m",
35 { { 5, { { 0, 35.4 }, { 3, 36.4 } } },
36 { 6, { { 0, 35.3 }, { 3, 36.2 } } },
37 { 7, { { 0, 34.9 }, { 3, 35.8 } } },
38 { 8, { { 0, 35.0 }, { 3, 35.8 } } } } },
39 { "paris_352_288_30.y4m",
40 { { 5, { { 0, 36.2 }, { 3, 36.7 } } },
41 { 6, { { 0, 36.1 }, { 3, 36.6 } } },
42 { 7, { { 0, 35.5 }, { 3, 36.0 } } },
43 { 8, { { 0, 36.0 }, { 3, 36.5 } } } } },
44 { "niklas_1280_720_30.y4m",
45 { { 5, { { 0, 34.6 }, { 3, 34.6 } } },
46 { 6, { { 0, 34.2 }, { 3, 34.2 } } },
47 { 7, { { 0, 33.7 }, { 3, 33.6 } } },
48 { 8, { { 0, 33.6 }, { 3, 33.4 } } } } } };
49
50 typedef struct {
51 const char *filename;
52 unsigned int input_bit_depth;
53 aom_img_fmt fmt;
54 aom_bit_depth_t bit_depth;
55 unsigned int profile;
56 } TestVideoParam;
57
operator <<(std::ostream & os,const TestVideoParam & test_arg)58 std::ostream &operator<<(std::ostream &os, const TestVideoParam &test_arg) {
59 return os << "TestVideoParam { filename:" << test_arg.filename
60 << " input_bit_depth:" << test_arg.input_bit_depth
61 << " fmt:" << test_arg.fmt << " bit_depth:" << test_arg.bit_depth
62 << " profile:" << test_arg.profile << " }";
63 }
64
65 const TestVideoParam kTestVectors[] = {
66 { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
67 { "paris_352_288_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
68 { "niklas_1280_720_30.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
69 };
70
71 // Params: test video, speed, aq mode, threads, tile columns.
72 class RTEndToEndTest
73 : public ::libaom_test::CodecTestWith5Params<TestVideoParam, int,
74 unsigned int, int, int>,
75 public ::libaom_test::EncoderTest {
76 protected:
RTEndToEndTest()77 RTEndToEndTest()
78 : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
79 cpu_used_(GET_PARAM(2)), psnr_(0.0), nframes_(0),
80 aq_mode_(GET_PARAM(3)), threads_(GET_PARAM(4)),
81 tile_columns_(GET_PARAM(5)) {}
82
~RTEndToEndTest()83 virtual ~RTEndToEndTest() {}
84
SetUp()85 virtual void SetUp() {
86 InitializeConfig();
87 SetMode(::libaom_test::kRealTime);
88
89 cfg_.rc_end_usage = AOM_CBR;
90 cfg_.g_threads = threads_;
91 cfg_.rc_buf_sz = 1000;
92 cfg_.rc_buf_initial_sz = 500;
93 cfg_.rc_buf_optimal_sz = 600;
94 }
95
BeginPassHook(unsigned int)96 virtual void BeginPassHook(unsigned int) {
97 psnr_ = 0.0;
98 nframes_ = 0;
99 }
100
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)101 virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
102 psnr_ += pkt->data.psnr.psnr[0];
103 nframes_++;
104 }
105
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)106 virtual void PreEncodeFrameHook(::libaom_test::VideoSource *video,
107 ::libaom_test::Encoder *encoder) {
108 if (video->frame() == 0) {
109 encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
110 encoder->Control(AV1E_SET_TILE_COLUMNS, tile_columns_);
111 encoder->Control(AOME_SET_CPUUSED, cpu_used_);
112 encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
113 encoder->Control(AV1E_SET_AQ_MODE, aq_mode_);
114 encoder->Control(AV1E_SET_ROW_MT, 1);
115 }
116 }
117
GetAveragePsnr() const118 double GetAveragePsnr() const {
119 if (nframes_) return psnr_ / nframes_;
120 return 0.0;
121 }
122
GetPsnrThreshold()123 double GetPsnrThreshold() {
124 return kPsnrThreshold[test_video_param_.filename][cpu_used_][aq_mode_];
125 }
126
DoTest()127 void DoTest() {
128 cfg_.rc_target_bitrate = kBitrate;
129 cfg_.g_error_resilient = 0;
130 cfg_.g_profile = test_video_param_.profile;
131 cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
132 cfg_.g_bit_depth = test_video_param_.bit_depth;
133 init_flags_ = AOM_CODEC_USE_PSNR;
134 if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
135
136 std::unique_ptr<libaom_test::VideoSource> video;
137 video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
138 kFrames));
139 ASSERT_TRUE(video.get() != NULL);
140
141 ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
142 const double psnr = GetAveragePsnr();
143 EXPECT_GT(psnr, GetPsnrThreshold())
144 << "cpu used = " << cpu_used_ << " aq mode = " << aq_mode_;
145 }
146
147 TestVideoParam test_video_param_;
148 int cpu_used_;
149
150 private:
151 double psnr_;
152 unsigned int nframes_;
153 unsigned int aq_mode_;
154 int threads_;
155 int tile_columns_;
156 };
157
158 class RTEndToEndTestThreaded : public RTEndToEndTest {};
159
TEST_P(RTEndToEndTest,EndtoEndPSNRTest)160 TEST_P(RTEndToEndTest, EndtoEndPSNRTest) { DoTest(); }
161
TEST_P(RTEndToEndTestThreaded,EndtoEndPSNRTest)162 TEST_P(RTEndToEndTestThreaded, EndtoEndPSNRTest) { DoTest(); }
163
164 AV1_INSTANTIATE_TEST_CASE(RTEndToEndTest, ::testing::ValuesIn(kTestVectors),
165 ::testing::Range(5, 9),
166 ::testing::Values<unsigned int>(0, 3),
167 ::testing::Values(1), ::testing::Values(1));
168
169 AV1_INSTANTIATE_TEST_CASE(RTEndToEndTestThreaded,
170 ::testing::ValuesIn(kTestVectors),
171 ::testing::Range(5, 9),
172 ::testing::Values<unsigned int>(0, 3),
173 ::testing::Range(2, 5), ::testing::Range(2, 5));
174 } // namespace
175