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 && CONFIG_HIGHBITDEPTH
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_HIGHBITDEPTH && 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, 1 },
57   { "park_joy_90p_8_444.y4m", 8, AOM_IMG_FMT_I444, AOM_BITS_8, 1 },
58   { "park_joy_90p_8_440.yuv", 8, AOM_IMG_FMT_I440, AOM_BITS_8, 1 },
59 #if CONFIG_HIGHBITDEPTH
60   { "park_joy_90p_10_420.y4m", 10, AOM_IMG_FMT_I42016, AOM_BITS_10, 2 },
61   { "park_joy_90p_10_422.y4m", 10, AOM_IMG_FMT_I42216, AOM_BITS_10, 3 },
62   { "park_joy_90p_10_444.y4m", 10, AOM_IMG_FMT_I44416, AOM_BITS_10, 3 },
63   { "park_joy_90p_10_440.yuv", 10, AOM_IMG_FMT_I44016, AOM_BITS_10, 3 },
64   { "park_joy_90p_12_420.y4m", 12, AOM_IMG_FMT_I42016, AOM_BITS_12, 2 },
65   { "park_joy_90p_12_422.y4m", 12, AOM_IMG_FMT_I42216, AOM_BITS_12, 3 },
66   { "park_joy_90p_12_444.y4m", 12, AOM_IMG_FMT_I44416, AOM_BITS_12, 3 },
67   { "park_joy_90p_12_440.yuv", 12, AOM_IMG_FMT_I44016, AOM_BITS_12, 3 },
68 #endif  // CONFIG_HIGHBITDEPTH
69 };
70 
71 // Encoding modes tested
72 const libaom_test::TestMode kEncodingModeVectors[] = {
73   ::libaom_test::kTwoPassGood, ::libaom_test::kOnePassGood,
74   ::libaom_test::kRealTime,
75 };
76 
77 // Speed settings tested
78 const int kCpuUsedVectors[] = { 1, 2, 3, 5, 6 };
79 
is_extension_y4m(const char * filename)80 int is_extension_y4m(const char *filename) {
81   const char *dot = strrchr(filename, '.');
82   if (!dot || dot == filename)
83     return 0;
84   else
85     return !strcmp(dot, ".y4m");
86 }
87 
88 class EndToEndTest
89     : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode,
90                                                  TestVideoParam, int>,
91       public ::libaom_test::EncoderTest {
92  protected:
EndToEndTest()93   EndToEndTest()
94       : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(2)),
95         cpu_used_(GET_PARAM(3)), psnr_(0.0), nframes_(0),
96         encoding_mode_(GET_PARAM(1)) {}
97 
~EndToEndTest()98   virtual ~EndToEndTest() {}
99 
SetUp()100   virtual void SetUp() {
101     InitializeConfig();
102     SetMode(encoding_mode_);
103     if (encoding_mode_ != ::libaom_test::kRealTime) {
104       cfg_.g_lag_in_frames = 5;
105       cfg_.rc_end_usage = AOM_VBR;
106     } else {
107       cfg_.g_lag_in_frames = 0;
108       cfg_.rc_end_usage = AOM_CBR;
109       cfg_.rc_buf_sz = 1000;
110       cfg_.rc_buf_initial_sz = 500;
111       cfg_.rc_buf_optimal_sz = 600;
112     }
113   }
114 
BeginPassHook(unsigned int)115   virtual void BeginPassHook(unsigned int) {
116     psnr_ = 0.0;
117     nframes_ = 0;
118   }
119 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)120   virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
121     psnr_ += pkt->data.psnr.psnr[0];
122     nframes_++;
123   }
124 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)125   virtual void PreEncodeFrameHook(::libaom_test::VideoSource *video,
126                                   ::libaom_test::Encoder *encoder) {
127     if (video->frame() == 1) {
128       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
129       encoder->Control(AV1E_SET_TILE_COLUMNS, 4);
130       encoder->Control(AOME_SET_CPUUSED, cpu_used_);
131       // Test screen coding tools at cpu_used = 1 && encoding mode is two-pass.
132       if (cpu_used_ == 1 && encoding_mode_ == ::libaom_test::kTwoPassGood)
133         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN);
134       else
135         encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_DEFAULT);
136       if (encoding_mode_ != ::libaom_test::kRealTime) {
137         encoder->Control(AOME_SET_ENABLEAUTOALTREF, 1);
138         encoder->Control(AOME_SET_ARNR_MAXFRAMES, 7);
139         encoder->Control(AOME_SET_ARNR_STRENGTH, 5);
140       }
141     }
142   }
143 
GetAveragePsnr() const144   double GetAveragePsnr() const {
145     if (nframes_) return psnr_ / nframes_;
146     return 0.0;
147   }
148 
GetPsnrThreshold()149   double GetPsnrThreshold() {
150     return kPsnrThreshold[cpu_used_][encoding_mode_];
151   }
152 
153   TestVideoParam test_video_param_;
154   int cpu_used_;
155 
156  private:
157   double psnr_;
158   unsigned int nframes_;
159   libaom_test::TestMode encoding_mode_;
160 };
161 
162 class EndToEndTestLarge : public EndToEndTest {};
163 
TEST_P(EndToEndTestLarge,EndtoEndPSNRTest)164 TEST_P(EndToEndTestLarge, EndtoEndPSNRTest) {
165   cfg_.rc_target_bitrate = kBitrate;
166   cfg_.g_error_resilient = 0;
167   cfg_.g_profile = test_video_param_.profile;
168   cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
169   cfg_.g_bit_depth = test_video_param_.bit_depth;
170   init_flags_ = AOM_CODEC_USE_PSNR;
171   if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
172 
173   testing::internal::scoped_ptr<libaom_test::VideoSource> video;
174   if (is_extension_y4m(test_video_param_.filename)) {
175     video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
176                                                 kFrames));
177   } else {
178     video.reset(new libaom_test::YUVVideoSource(
179         test_video_param_.filename, test_video_param_.fmt, kWidth, kHeight,
180         kFramerate, 1, 0, kFrames));
181   }
182   ASSERT_TRUE(video.get() != NULL);
183 
184   ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
185   const double psnr = GetAveragePsnr();
186   EXPECT_GT(psnr, GetPsnrThreshold());
187 }
188 
TEST_P(EndToEndTest,EndtoEndPSNRTest)189 TEST_P(EndToEndTest, EndtoEndPSNRTest) {
190   cfg_.rc_target_bitrate = kBitrate;
191   cfg_.g_error_resilient = 0;
192   cfg_.g_profile = test_video_param_.profile;
193   cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
194   cfg_.g_bit_depth = test_video_param_.bit_depth;
195   init_flags_ = AOM_CODEC_USE_PSNR;
196   if (cfg_.g_bit_depth > 8) init_flags_ |= AOM_CODEC_USE_HIGHBITDEPTH;
197 
198   testing::internal::scoped_ptr<libaom_test::VideoSource> video;
199   if (is_extension_y4m(test_video_param_.filename)) {
200     video.reset(new libaom_test::Y4mVideoSource(test_video_param_.filename, 0,
201                                                 kFrames));
202   } else {
203     video.reset(new libaom_test::YUVVideoSource(
204         test_video_param_.filename, test_video_param_.fmt, kWidth, kHeight,
205         kFramerate, 1, 0, kFrames));
206   }
207   ASSERT_TRUE(video.get() != NULL);
208 
209   ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
210   const double psnr = GetAveragePsnr();
211   EXPECT_GT(psnr, GetPsnrThreshold());
212 }
213 
214 AV1_INSTANTIATE_TEST_CASE(EndToEndTestLarge,
215                           ::testing::ValuesIn(kEncodingModeVectors),
216                           ::testing::ValuesIn(kTestVectors),
217                           ::testing::ValuesIn(kCpuUsedVectors));
218 
219 AV1_INSTANTIATE_TEST_CASE(EndToEndTest,
220                           ::testing::Values(kEncodingModeVectors[0]),
221                           ::testing::Values(kTestVectors[2]),  // 444
222                           ::testing::Values(kCpuUsedVectors[2]));
223 }  // namespace
224