1 /*
2  * Copyright (c) 2016, 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 "third_party/googletest/src/googletest/include/gtest/gtest.h"
13 
14 #include "test/codec_factory.h"
15 #include "test/encode_test_driver.h"
16 #include "test/util.h"
17 #include "test/y4m_video_source.h"
18 #include "test/yuv_video_source.h"
19 
20 namespace {
21 
22 const unsigned int kWidth = 160;
23 const unsigned int kHeight = 90;
24 const unsigned int kFramerate = 50;
25 const unsigned int kFrames = 10;
26 const int kBitrate = 500;
27 // List of psnr thresholds for speed settings 0-7 and 5 encoding modes
28 const double kPsnrThreshold[][5] = {
29 // Note:
30 // AV1 HBD average PSNR is slightly lower than AV1.
31 // We make two cases here to enable the testing and
32 // guard picture quality.
33 #if CONFIG_AV1_ENCODER
34   { 36.0, 37.0, 37.0, 37.0, 37.0 }, { 31.0, 36.0, 36.0, 36.0, 36.0 },
35   { 31.0, 35.0, 35.0, 35.0, 35.0 }, { 31.0, 34.0, 34.0, 34.0, 34.0 },
36   { 31.0, 33.0, 33.0, 33.0, 33.0 }, { 31.0, 32.0, 32.0, 32.0, 32.0 },
37   { 30.0, 31.0, 31.0, 31.0, 31.0 }, { 29.0, 30.0, 30.0, 30.0, 30.0 },
38 #else
39   { 36.0, 37.0, 37.0, 37.0, 37.0 }, { 35.0, 36.0, 36.0, 36.0, 36.0 },
40   { 34.0, 35.0, 35.0, 35.0, 35.0 }, { 33.0, 34.0, 34.0, 34.0, 34.0 },
41   { 32.0, 33.0, 33.0, 33.0, 33.0 }, { 31.0, 32.0, 32.0, 32.0, 32.0 },
42   { 30.0, 31.0, 31.0, 31.0, 31.0 }, { 29.0, 30.0, 30.0, 30.0, 30.0 },
43 #endif  // CONFIG_AV1_ENCODER
44 };
45 
46 typedef struct {
47   const char *filename;
48   unsigned int input_bit_depth;
49   aom_img_fmt fmt;
50   aom_bit_depth_t bit_depth;
51   unsigned int profile;
52 } TestVideoParam;
53 
54 const TestVideoParam kTestVectors[] = {
55   { "park_joy_90p_8_420.y4m", 8, AOM_IMG_FMT_I420, AOM_BITS_8, 0 },
56   { "park_joy_90p_8_422.y4m", 8, AOM_IMG_FMT_I422, AOM_BITS_8, 2 },
57   { "park_joy_90p_8_444.y4m", 8, AOM_IMG_FMT_I444, AOM_BITS_8, 1 },
58   { "park_joy_90p_10_420.y4m", 10, AOM_IMG_FMT_I42016, AOM_BITS_10, 0 },
59   { "park_joy_90p_10_422.y4m", 10, AOM_IMG_FMT_I42216, AOM_BITS_10, 2 },
60   { "park_joy_90p_10_444.y4m", 10, AOM_IMG_FMT_I44416, AOM_BITS_10, 1 },
61   { "park_joy_90p_12_420.y4m", 12, AOM_IMG_FMT_I42016, AOM_BITS_12, 2 },
62   { "park_joy_90p_12_422.y4m", 12, AOM_IMG_FMT_I42216, AOM_BITS_12, 2 },
63   { "park_joy_90p_12_444.y4m", 12, AOM_IMG_FMT_I44416, AOM_BITS_12, 2 },
64 };
65 
66 // Encoding modes tested
67 const libaom_test::TestMode kEncodingModeVectors[] = {
68   ::libaom_test::kTwoPassGood,
69   ::libaom_test::kOnePassGood,
70   ::libaom_test::kRealTime,
71 };
72 
73 // Speed settings tested
74 const int kCpuUsedVectors[] = { 1, 2, 3, 5, 6 };
75 
is_extension_y4m(const char * filename)76 int is_extension_y4m(const char *filename) {
77   const char *dot = strrchr(filename, '.');
78   if (!dot || dot == filename)
79     return 0;
80   else
81     return !strcmp(dot, ".y4m");
82 }
83 
84 class EndToEndTest
85     : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode,
86                                                  TestVideoParam, int>,
87       public ::libaom_test::EncoderTest {
88  protected:
EndToEndTest()89   EndToEndTest()
90       : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(2)),
91         cpu_used_(GET_PARAM(3)), psnr_(0.0), nframes_(0),
92         encoding_mode_(GET_PARAM(1)) {}
93 
~EndToEndTest()94   virtual ~EndToEndTest() {}
95 
SetUp()96   virtual void SetUp() {
97     InitializeConfig();
98     SetMode(encoding_mode_);
99     if (encoding_mode_ != ::libaom_test::kRealTime) {
100       cfg_.g_lag_in_frames = 5;
101       cfg_.rc_end_usage = AOM_VBR;
102     } else {
103       cfg_.g_lag_in_frames = 0;
104       cfg_.rc_end_usage = AOM_CBR;
105       cfg_.rc_buf_sz = 1000;
106       cfg_.rc_buf_initial_sz = 500;
107       cfg_.rc_buf_optimal_sz = 600;
108     }
109   }
110 
BeginPassHook(unsigned int)111   virtual void BeginPassHook(unsigned int) {
112     psnr_ = 0.0;
113     nframes_ = 0;
114   }
115 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)116   virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
117     psnr_ += pkt->data.psnr.psnr[0];
118     nframes_++;
119   }
120 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)121   virtual void PreEncodeFrameHook(::libaom_test::VideoSource *video,
122                                   ::libaom_test::Encoder *encoder) {
123     if (video->frame() == 1) {
124       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
125       encoder->Control(AV1E_SET_TILE_COLUMNS, 4);
126       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
127       // Test screen coding tools at cpu_used = 1 && encoding mode is two-pass.
128       if (cpu_used_ == 1 && encoding_mode_ == ::libaom_test::kTwoPassGood)
129         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN);
130       else
131         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
132       if (encoding_mode_ != ::libaom_test::kRealTime) {
133         encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
134         encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
135         encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
136       }
137     }
138   }
139 
GetAveragePsnr() const140   double GetAveragePsnr() const {
141     if (nframes_) return psnr_ / nframes_;
142     return 0.0;
143   }
144 
GetPsnrThreshold()145   double GetPsnrThreshold() {
146     return kPsnrThreshold[cpu_used_][encoding_mode_];
147   }
148 
DoTest()149   void DoTest() {
150     cfg_.rc_target_bitrate = kBitrate;
151     cfg_.g_error_resilient = 0;
152     cfg_.g_profile = test_video_param_.profile;
153     cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
154     cfg_.g_bit_depth = test_video_param_.bit_depth;
155     init_flags_ = AOM_CODEC_USE_PSNR;
156     if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
157 
158     testing::internal::scoped_ptr<libaom_test::VideoSource> video;
159     if (is_extension_y4m(test_video_param_.filename)) {
160       video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
161                                                   kFrames));
162     } else {
163       video.reset(new libaom_test::YUVVideoSource(
164           test_video_param_.filename, test_video_param_.fmt, kWidth, kHeight,
165           kFramerate, 1, 0, kFrames));
166     }
167     ASSERT_TRUE(video.get() != NULL);
168 
169     ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
170     const double psnr = GetAveragePsnr();
171     EXPECT_GT(psnr, GetPsnrThreshold())
172         << "cpu used = " << cpu_used_ << ", encoding mode = " << encoding_mode_;
173   }
174 
175   TestVideoParam test_video_param_;
176   int cpu_used_;
177 
178  private:
179   double psnr_;
180   unsigned int nframes_;
181   libaom_test::TestMode encoding_mode_;
182 };
183 
184 class EndToEndTestLarge : public EndToEndTest {};
185 
TEST_P(EndToEndTestLarge,EndtoEndPSNRTest)186 TEST_P(EndToEndTestLarge, EndtoEndPSNRTest) { DoTest(); }
187 
TEST_P(EndToEndTest,EndtoEndPSNRTest)188 TEST_P(EndToEndTest, EndtoEndPSNRTest) { DoTest(); }
189 
190 AV1_INSTANTIATE_TEST_CASE(EndToEndTestLarge,
191                           ::testing::ValuesIn(kEncodingModeVectors),
192                           ::testing::ValuesIn(kTestVectors),
193                           ::testing::ValuesIn(kCpuUsedVectors));
194 
195 AV1_INSTANTIATE_TEST_CASE(EndToEndTest,
196                           ::testing::Values(kEncodingModeVectors[0]),
197                           ::testing::Values(kTestVectors[2]),  // 444
198                           ::testing::Values(kCpuUsedVectors[2]));
199 }  // namespace
200