blob: f22b5fe0f2bff3033d68be633c072b39ca1aba2f [file] [log] [blame]
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +00001/*
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.
Johannf152ff62018-02-08 14:33:07 -080010 */
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000011
12#include <climits>
13#include <vector>
14#include "third_party/googletest/src/googletest/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/video_source.h"
19#include "test/util.h"
20
21namespace {
22
Jayashri Muruganc36cd872021-04-16 08:37:52 +053023const unsigned int kCqLevel = 18;
24const double kMaxPsnr = 100.0;
25
26// kPsnrThreshold represents the psnr threshold used to validate the quality of
James Zernbf9d1f82022-10-27 18:17:47 -070027// the first frame. The indices correspond to one/two-pass, allintra and
28// realtime encoding modes.
29const double kPsnrThreshold[3] = { 29.0, 41.5, 41.5 };
Jayashri Muruganc36cd872021-04-16 08:37:52 +053030
31// kPsnrFluctuation represents the maximum allowed psnr fluctuation w.r.t first
James Zernbf9d1f82022-10-27 18:17:47 -070032// frame. The indices correspond to one/two-pass, allintra and realtime
33// encoding modes.
34const double kPsnrFluctuation[3] = { 2.5, 0.3, 16.0 };
Jayashri Muruganc36cd872021-04-16 08:37:52 +053035
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000036class MonochromeTest
Jayashri Muruganc36cd872021-04-16 08:37:52 +053037 : public ::libaom_test::CodecTestWith3Params<libaom_test::TestMode, int,
38 int>,
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000039 public ::libaom_test::EncoderTest {
40 protected:
Jayashri Muruganc36cd872021-04-16 08:37:52 +053041 MonochromeTest()
42 : EncoderTest(GET_PARAM(0)), lossless_(GET_PARAM(2)),
43 frame0_psnr_y_(0.0) {}
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000044
James Zernf1fa1eb2023-07-25 15:34:13 -070045 ~MonochromeTest() override = default;
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000046
James Zernfaa2dcf2023-07-24 18:29:51 -070047 void SetUp() override { InitializeConfig(GET_PARAM(1)); }
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000048
James Zernfaa2dcf2023-07-24 18:29:51 -070049 void PreEncodeFrameHook(::libaom_test::VideoSource *video,
50 ::libaom_test::Encoder *encoder) override {
Jayashri Muruganc36cd872021-04-16 08:37:52 +053051 if (video->frame() == 0) {
52 encoder->Control(AOME_SET_CPUUSED, GET_PARAM(3));
53 if (mode_ == ::libaom_test::kAllIntra) {
54 encoder->Control(AOME_SET_CQ_LEVEL, kCqLevel);
55 }
56 if (lossless_) {
57 encoder->Control(AV1E_SET_LOSSLESS, 1);
58 }
59 }
60 }
61
James Zernfaa2dcf2023-07-24 18:29:51 -070062 void DecompressedFrameHook(const aom_image_t &img,
63 aom_codec_pts_t pts) override {
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +000064 (void)pts;
65
66 // Get value of top-left corner pixel of U plane
67 int chroma_value = img.planes[AOM_PLANE_U][0];
68
69 bool is_chroma_constant =
70 ComparePlaneToValue(img, AOM_PLANE_U, chroma_value) &&
71 ComparePlaneToValue(img, AOM_PLANE_V, chroma_value);
72
73 // Chroma planes should be constant
74 EXPECT_TRUE(is_chroma_constant);
75
76 // Monochrome flag on image should be set
77 EXPECT_EQ(img.monochrome, 1);
78
79 chroma_value_list_.push_back(chroma_value);
80 }
81
82 // Returns true if all pixels on the plane are equal to value, and returns
83 // false otherwise.
84 bool ComparePlaneToValue(const aom_image_t &img, const int plane,
85 const int value) {
86 const int w = aom_img_plane_width(&img, plane);
87 const int h = aom_img_plane_height(&img, plane);
88 const uint8_t *const buf = img.planes[plane];
89 const int stride = img.stride[plane];
90
91 for (int r = 0; r < h; ++r) {
92 for (int c = 0; c < w; ++c) {
93 if (buf[r * stride + c] != value) return false;
94 }
95 }
96 return true;
97 }
98
James Zernfaa2dcf2023-07-24 18:29:51 -070099 void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) override {
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530100 // Check average PSNR value is >= 100 db in case of lossless encoding.
101 if (lossless_) {
102 EXPECT_GE(pkt->data.psnr.psnr[0], kMaxPsnr);
103 return;
104 }
James Zernbf9d1f82022-10-27 18:17:47 -0700105 const int psnr_index = (mode_ == ::libaom_test::kRealTime) ? 2
106 : (mode_ == ::libaom_test::kAllIntra) ? 1
107 : 0;
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000108 // Check that the initial Y PSNR value is 'high enough', and check that
109 // subsequent Y PSNR values are 'close' to this initial value.
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530110 if (frame0_psnr_y_ == 0.0) {
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000111 frame0_psnr_y_ = pkt->data.psnr.psnr[1];
James Zernbf9d1f82022-10-27 18:17:47 -0700112 EXPECT_GT(frame0_psnr_y_, kPsnrThreshold[psnr_index]);
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000113 }
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530114 EXPECT_NEAR(pkt->data.psnr.psnr[1], frame0_psnr_y_,
James Zernbf9d1f82022-10-27 18:17:47 -0700115 kPsnrFluctuation[psnr_index]);
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000116 }
117
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530118 int lossless_;
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000119 std::vector<int> chroma_value_list_;
120 double frame0_psnr_y_;
121};
122
123TEST_P(MonochromeTest, TestMonochromeEncoding) {
124 ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
125 30, 1, 0, 5);
126
127 init_flags_ = AOM_CODEC_USE_PSNR;
128
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000129 cfg_.rc_buf_initial_sz = 500;
130 cfg_.rc_buf_optimal_sz = 600;
131 cfg_.rc_buf_sz = 1000;
132 cfg_.rc_min_quantizer = 2;
133 cfg_.rc_max_quantizer = 56;
134 cfg_.rc_undershoot_pct = 50;
135 cfg_.rc_overshoot_pct = 50;
136 cfg_.rc_end_usage = AOM_CBR;
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000137 cfg_.g_lag_in_frames = 1;
138 cfg_.kf_min_dist = cfg_.kf_max_dist = 3000;
139 // Enable dropped frames.
140 cfg_.rc_dropframe_thresh = 1;
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000141 // Run at low bitrate.
142 cfg_.rc_target_bitrate = 40;
143 // Set monochrome encoding flag
144 cfg_.monochrome = 1;
145
146 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
147
148 // Check that the chroma planes are equal across all frames
149 std::vector<int>::const_iterator iter = chroma_value_list_.begin();
150 int initial_chroma_value = *iter;
151 for (; iter != chroma_value_list_.end(); ++iter) {
152 // Check that all decoded frames have the same constant chroma planes.
153 EXPECT_EQ(*iter, initial_chroma_value);
154 }
155}
156
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530157class MonochromeAllIntraTest : public MonochromeTest {};
158
159TEST_P(MonochromeAllIntraTest, TestMonochromeEncoding) {
160 ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
161 30, 1, 0, 5);
162 init_flags_ = AOM_CODEC_USE_PSNR;
163 // Set monochrome encoding flag
164 cfg_.monochrome = 1;
165 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
166
167 // Check that the chroma planes are equal across all frames
168 std::vector<int>::const_iterator iter = chroma_value_list_.begin();
169 int initial_chroma_value = *iter;
170 for (; iter != chroma_value_list_.end(); ++iter) {
171 // Check that all decoded frames have the same constant chroma planes.
172 EXPECT_EQ(*iter, initial_chroma_value);
173 }
174}
175
James Zernbf9d1f82022-10-27 18:17:47 -0700176class MonochromeRealtimeTest : public MonochromeTest {};
177
178TEST_P(MonochromeRealtimeTest, TestMonochromeEncoding) {
179 ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
180 30, 1, 0, 30);
181 init_flags_ = AOM_CODEC_USE_PSNR;
182 // Set monochrome encoding flag
183 cfg_.monochrome = 1;
184 // Run at low bitrate.
185 cfg_.rc_target_bitrate = 40;
186 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
187
188 // Check that the chroma planes are equal across all frames
189 std::vector<int>::const_iterator iter = chroma_value_list_.begin();
190 int initial_chroma_value = *iter;
191 for (; iter != chroma_value_list_.end(); ++iter) {
192 // Check that all decoded frames have the same constant chroma planes.
193 EXPECT_EQ(*iter, initial_chroma_value);
194 }
195}
196
chiyotsai9dfac722020-07-07 17:43:02 -0700197AV1_INSTANTIATE_TEST_SUITE(MonochromeTest,
198 ::testing::Values(::libaom_test::kOnePassGood,
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530199 ::libaom_test::kTwoPassGood),
200 ::testing::Values(0), // lossless
201 ::testing::Values(0)); // cpu_used
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000202
Jayashri Muruganc36cd872021-04-16 08:37:52 +0530203AV1_INSTANTIATE_TEST_SUITE(MonochromeAllIntraTest,
204 ::testing::Values(::libaom_test::kAllIntra),
205 ::testing::Values(0, 1), // lossless
206 ::testing::Values(6, 9)); // cpu_used
James Zernbf9d1f82022-10-27 18:17:47 -0700207
208AV1_INSTANTIATE_TEST_SUITE(MonochromeRealtimeTest,
209 ::testing::Values(::libaom_test::kRealTime),
210 ::testing::Values(0), // lossless
211 ::testing::Values(6, 8, 10)); // cpu_used
212
Imdad Sardharwalla26ac0472018-01-18 15:00:24 +0000213} // namespace