1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <climits>
13*77c1e3ccSAndroid Build Coastguard Worker #include <vector>
14*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "test/codec_factory.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "test/encode_test_driver.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "test/i420_video_source.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "test/video_source.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "test/util.h"
20*77c1e3ccSAndroid Build Coastguard Worker
21*77c1e3ccSAndroid Build Coastguard Worker namespace {
22*77c1e3ccSAndroid Build Coastguard Worker
23*77c1e3ccSAndroid Build Coastguard Worker const unsigned int kCqLevel = 18;
24*77c1e3ccSAndroid Build Coastguard Worker const double kMaxPsnr = 100.0;
25*77c1e3ccSAndroid Build Coastguard Worker
26*77c1e3ccSAndroid Build Coastguard Worker // kPsnrThreshold represents the psnr threshold used to validate the quality of
27*77c1e3ccSAndroid Build Coastguard Worker // the first frame. The indices correspond to one/two-pass, allintra and
28*77c1e3ccSAndroid Build Coastguard Worker // realtime encoding modes.
29*77c1e3ccSAndroid Build Coastguard Worker const double kPsnrThreshold[3] = { 29.0, 41.5, 41.5 };
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker // kPsnrFluctuation represents the maximum allowed psnr fluctuation w.r.t first
32*77c1e3ccSAndroid Build Coastguard Worker // frame. The indices correspond to one/two-pass, allintra and realtime
33*77c1e3ccSAndroid Build Coastguard Worker // encoding modes.
34*77c1e3ccSAndroid Build Coastguard Worker const double kPsnrFluctuation[3] = { 2.5, 0.3, 17.0 };
35*77c1e3ccSAndroid Build Coastguard Worker
36*77c1e3ccSAndroid Build Coastguard Worker class MonochromeTest
37*77c1e3ccSAndroid Build Coastguard Worker : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode, int,
38*77c1e3ccSAndroid Build Coastguard Worker int>,
39*77c1e3ccSAndroid Build Coastguard Worker public ::libaom_test::EncoderTest {
40*77c1e3ccSAndroid Build Coastguard Worker protected:
MonochromeTest()41*77c1e3ccSAndroid Build Coastguard Worker MonochromeTest()
42*77c1e3ccSAndroid Build Coastguard Worker : EncoderTest(GET_PARAM(0)), lossless_(GET_PARAM(2)),
43*77c1e3ccSAndroid Build Coastguard Worker frame0_psnr_y_(0.0) {}
44*77c1e3ccSAndroid Build Coastguard Worker
45*77c1e3ccSAndroid Build Coastguard Worker ~MonochromeTest() override = default;
46*77c1e3ccSAndroid Build Coastguard Worker
SetUp()47*77c1e3ccSAndroid Build Coastguard Worker void SetUp() override { InitializeConfig(GET_PARAM(1)); }
48*77c1e3ccSAndroid Build Coastguard Worker
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)49*77c1e3ccSAndroid Build Coastguard Worker void PreEncodeFrameHook(::libaom_test::VideoSource *video,
50*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::Encoder *encoder) override {
51*77c1e3ccSAndroid Build Coastguard Worker if (video->frame() == 0) {
52*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_CPUUSED, GET_PARAM(3));
53*77c1e3ccSAndroid Build Coastguard Worker if (mode_ == ::libaom_test::kAllIntra) {
54*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AOME_SET_CQ_LEVEL, kCqLevel);
55*77c1e3ccSAndroid Build Coastguard Worker }
56*77c1e3ccSAndroid Build Coastguard Worker if (lossless_) {
57*77c1e3ccSAndroid Build Coastguard Worker encoder->Control(AV1E_SET_LOSSLESS, 1);
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker }
60*77c1e3ccSAndroid Build Coastguard Worker }
61*77c1e3ccSAndroid Build Coastguard Worker
DecompressedFrameHook(const aom_image_t & img,aom_codec_pts_t pts)62*77c1e3ccSAndroid Build Coastguard Worker void DecompressedFrameHook(const aom_image_t &img,
63*77c1e3ccSAndroid Build Coastguard Worker aom_codec_pts_t pts) override {
64*77c1e3ccSAndroid Build Coastguard Worker (void)pts;
65*77c1e3ccSAndroid Build Coastguard Worker
66*77c1e3ccSAndroid Build Coastguard Worker // Get value of top-left corner pixel of U plane
67*77c1e3ccSAndroid Build Coastguard Worker int chroma_value = img.planes[AOM_PLANE_U][0];
68*77c1e3ccSAndroid Build Coastguard Worker
69*77c1e3ccSAndroid Build Coastguard Worker bool is_chroma_constant =
70*77c1e3ccSAndroid Build Coastguard Worker ComparePlaneToValue(img, AOM_PLANE_U, chroma_value) &&
71*77c1e3ccSAndroid Build Coastguard Worker ComparePlaneToValue(img, AOM_PLANE_V, chroma_value);
72*77c1e3ccSAndroid Build Coastguard Worker
73*77c1e3ccSAndroid Build Coastguard Worker // Chroma planes should be constant
74*77c1e3ccSAndroid Build Coastguard Worker EXPECT_TRUE(is_chroma_constant);
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker // Monochrome flag on image should be set
77*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(img.monochrome, 1);
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker chroma_value_list_.push_back(chroma_value);
80*77c1e3ccSAndroid Build Coastguard Worker }
81*77c1e3ccSAndroid Build Coastguard Worker
82*77c1e3ccSAndroid Build Coastguard Worker // Returns true if all pixels on the plane are equal to value, and returns
83*77c1e3ccSAndroid Build Coastguard Worker // false otherwise.
ComparePlaneToValue(const aom_image_t & img,const int plane,const int value)84*77c1e3ccSAndroid Build Coastguard Worker bool ComparePlaneToValue(const aom_image_t &img, const int plane,
85*77c1e3ccSAndroid Build Coastguard Worker const int value) {
86*77c1e3ccSAndroid Build Coastguard Worker const int w = aom_img_plane_width(&img, plane);
87*77c1e3ccSAndroid Build Coastguard Worker const int h = aom_img_plane_height(&img, plane);
88*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *const buf = img.planes[plane];
89*77c1e3ccSAndroid Build Coastguard Worker const int stride = img.stride[plane];
90*77c1e3ccSAndroid Build Coastguard Worker
91*77c1e3ccSAndroid Build Coastguard Worker for (int r = 0; r < h; ++r) {
92*77c1e3ccSAndroid Build Coastguard Worker for (int c = 0; c < w; ++c) {
93*77c1e3ccSAndroid Build Coastguard Worker if (buf[r * stride + c] != value) return false;
94*77c1e3ccSAndroid Build Coastguard Worker }
95*77c1e3ccSAndroid Build Coastguard Worker }
96*77c1e3ccSAndroid Build Coastguard Worker return true;
97*77c1e3ccSAndroid Build Coastguard Worker }
98*77c1e3ccSAndroid Build Coastguard Worker
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)99*77c1e3ccSAndroid Build Coastguard Worker void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
100*77c1e3ccSAndroid Build Coastguard Worker // Check average PSNR value is >= 100 db in case of lossless encoding.
101*77c1e3ccSAndroid Build Coastguard Worker if (lossless_) {
102*77c1e3ccSAndroid Build Coastguard Worker EXPECT_GE(pkt->data.psnr.psnr[0], kMaxPsnr);
103*77c1e3ccSAndroid Build Coastguard Worker return;
104*77c1e3ccSAndroid Build Coastguard Worker }
105*77c1e3ccSAndroid Build Coastguard Worker const int psnr_index = (mode_ == ::libaom_test::kRealTime) ? 2
106*77c1e3ccSAndroid Build Coastguard Worker : (mode_ == ::libaom_test::kAllIntra) ? 1
107*77c1e3ccSAndroid Build Coastguard Worker : 0;
108*77c1e3ccSAndroid Build Coastguard Worker // Check that the initial Y PSNR value is 'high enough', and check that
109*77c1e3ccSAndroid Build Coastguard Worker // subsequent Y PSNR values are 'close' to this initial value.
110*77c1e3ccSAndroid Build Coastguard Worker if (frame0_psnr_y_ == 0.0) {
111*77c1e3ccSAndroid Build Coastguard Worker frame0_psnr_y_ = pkt->data.psnr.psnr[1];
112*77c1e3ccSAndroid Build Coastguard Worker EXPECT_GT(frame0_psnr_y_, kPsnrThreshold[psnr_index]);
113*77c1e3ccSAndroid Build Coastguard Worker }
114*77c1e3ccSAndroid Build Coastguard Worker EXPECT_NEAR(pkt->data.psnr.psnr[1], frame0_psnr_y_,
115*77c1e3ccSAndroid Build Coastguard Worker kPsnrFluctuation[psnr_index]);
116*77c1e3ccSAndroid Build Coastguard Worker }
117*77c1e3ccSAndroid Build Coastguard Worker
118*77c1e3ccSAndroid Build Coastguard Worker int lossless_;
119*77c1e3ccSAndroid Build Coastguard Worker std::vector<int> chroma_value_list_;
120*77c1e3ccSAndroid Build Coastguard Worker double frame0_psnr_y_;
121*77c1e3ccSAndroid Build Coastguard Worker };
122*77c1e3ccSAndroid Build Coastguard Worker
TEST_P(MonochromeTest,TestMonochromeEncoding)123*77c1e3ccSAndroid Build Coastguard Worker TEST_P(MonochromeTest, TestMonochromeEncoding) {
124*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
125*77c1e3ccSAndroid Build Coastguard Worker 30, 1, 0, 5);
126*77c1e3ccSAndroid Build Coastguard Worker
127*77c1e3ccSAndroid Build Coastguard Worker init_flags_ = AOM_CODEC_USE_PSNR;
128*77c1e3ccSAndroid Build Coastguard Worker
129*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_initial_sz = 500;
130*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_optimal_sz = 600;
131*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_sz = 1000;
132*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_min_quantizer = 2;
133*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_max_quantizer = 56;
134*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_undershoot_pct = 50;
135*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_overshoot_pct = 50;
136*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_end_usage = AOM_CBR;
137*77c1e3ccSAndroid Build Coastguard Worker cfg_.g_lag_in_frames = 1;
138*77c1e3ccSAndroid Build Coastguard Worker cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
139*77c1e3ccSAndroid Build Coastguard Worker // Enable dropped frames.
140*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_dropframe_thresh = 1;
141*77c1e3ccSAndroid Build Coastguard Worker // Run at low bitrate.
142*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_target_bitrate = 40;
143*77c1e3ccSAndroid Build Coastguard Worker // Set monochrome encoding flag
144*77c1e3ccSAndroid Build Coastguard Worker cfg_.monochrome = 1;
145*77c1e3ccSAndroid Build Coastguard Worker
146*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
147*77c1e3ccSAndroid Build Coastguard Worker
148*77c1e3ccSAndroid Build Coastguard Worker // Check that the chroma planes are equal across all frames
149*77c1e3ccSAndroid Build Coastguard Worker std::vector<int>::const_iterator iter = chroma_value_list_.begin();
150*77c1e3ccSAndroid Build Coastguard Worker int initial_chroma_value = *iter;
151*77c1e3ccSAndroid Build Coastguard Worker for (; iter != chroma_value_list_.end(); ++iter) {
152*77c1e3ccSAndroid Build Coastguard Worker // Check that all decoded frames have the same constant chroma planes.
153*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(*iter, initial_chroma_value);
154*77c1e3ccSAndroid Build Coastguard Worker }
155*77c1e3ccSAndroid Build Coastguard Worker }
156*77c1e3ccSAndroid Build Coastguard Worker
157*77c1e3ccSAndroid Build Coastguard Worker class MonochromeAllIntraTest : public MonochromeTest {};
158*77c1e3ccSAndroid Build Coastguard Worker
TEST_P(MonochromeAllIntraTest,TestMonochromeEncoding)159*77c1e3ccSAndroid Build Coastguard Worker TEST_P(MonochromeAllIntraTest, TestMonochromeEncoding) {
160*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
161*77c1e3ccSAndroid Build Coastguard Worker 30, 1, 0, 5);
162*77c1e3ccSAndroid Build Coastguard Worker init_flags_ = AOM_CODEC_USE_PSNR;
163*77c1e3ccSAndroid Build Coastguard Worker // Set monochrome encoding flag
164*77c1e3ccSAndroid Build Coastguard Worker cfg_.monochrome = 1;
165*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
166*77c1e3ccSAndroid Build Coastguard Worker
167*77c1e3ccSAndroid Build Coastguard Worker // Check that the chroma planes are equal across all frames
168*77c1e3ccSAndroid Build Coastguard Worker std::vector<int>::const_iterator iter = chroma_value_list_.begin();
169*77c1e3ccSAndroid Build Coastguard Worker int initial_chroma_value = *iter;
170*77c1e3ccSAndroid Build Coastguard Worker for (; iter != chroma_value_list_.end(); ++iter) {
171*77c1e3ccSAndroid Build Coastguard Worker // Check that all decoded frames have the same constant chroma planes.
172*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(*iter, initial_chroma_value);
173*77c1e3ccSAndroid Build Coastguard Worker }
174*77c1e3ccSAndroid Build Coastguard Worker }
175*77c1e3ccSAndroid Build Coastguard Worker
176*77c1e3ccSAndroid Build Coastguard Worker class MonochromeRealtimeTest : public MonochromeTest {};
177*77c1e3ccSAndroid Build Coastguard Worker
TEST_P(MonochromeRealtimeTest,TestMonochromeEncoding)178*77c1e3ccSAndroid Build Coastguard Worker TEST_P(MonochromeRealtimeTest, TestMonochromeEncoding) {
179*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
180*77c1e3ccSAndroid Build Coastguard Worker 30, 1, 0, 30);
181*77c1e3ccSAndroid Build Coastguard Worker init_flags_ = AOM_CODEC_USE_PSNR;
182*77c1e3ccSAndroid Build Coastguard Worker // Set monochrome encoding flag
183*77c1e3ccSAndroid Build Coastguard Worker cfg_.monochrome = 1;
184*77c1e3ccSAndroid Build Coastguard Worker // Run at low bitrate.
185*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_target_bitrate = 40;
186*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_sz = 6000;
187*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_initial_sz = 4000;
188*77c1e3ccSAndroid Build Coastguard Worker cfg_.rc_buf_optimal_sz = 5000;
189*77c1e3ccSAndroid Build Coastguard Worker ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
190*77c1e3ccSAndroid Build Coastguard Worker
191*77c1e3ccSAndroid Build Coastguard Worker // Check that the chroma planes are equal across all frames
192*77c1e3ccSAndroid Build Coastguard Worker std::vector<int>::const_iterator iter = chroma_value_list_.begin();
193*77c1e3ccSAndroid Build Coastguard Worker int initial_chroma_value = *iter;
194*77c1e3ccSAndroid Build Coastguard Worker for (; iter != chroma_value_list_.end(); ++iter) {
195*77c1e3ccSAndroid Build Coastguard Worker // Check that all decoded frames have the same constant chroma planes.
196*77c1e3ccSAndroid Build Coastguard Worker EXPECT_EQ(*iter, initial_chroma_value);
197*77c1e3ccSAndroid Build Coastguard Worker }
198*77c1e3ccSAndroid Build Coastguard Worker }
199*77c1e3ccSAndroid Build Coastguard Worker
200*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(MonochromeTest,
201*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(::libaom_test::kOnePassGood,
202*77c1e3ccSAndroid Build Coastguard Worker ::libaom_test::kTwoPassGood),
203*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(0), // lossless
204*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(0)); // cpu_used
205*77c1e3ccSAndroid Build Coastguard Worker
206*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(MonochromeAllIntraTest,
207*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(::libaom_test::kAllIntra),
208*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(0, 1), // lossless
209*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(6, 9)); // cpu_used
210*77c1e3ccSAndroid Build Coastguard Worker
211*77c1e3ccSAndroid Build Coastguard Worker AV1_INSTANTIATE_TEST_SUITE(MonochromeRealtimeTest,
212*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(::libaom_test::kRealTime),
213*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(0), // lossless
214*77c1e3ccSAndroid Build Coastguard Worker ::testing::Values(6, 8, 10)); // cpu_used
215*77c1e3ccSAndroid Build Coastguard Worker
216*77c1e3ccSAndroid Build Coastguard Worker } // namespace
217