1 /*
2 * Copyright (c) 2018, 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 <string>
13 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
14 #include "aom_dsp/grain_table.h"
15 #include "aom/internal/aom_codec_internal.h"
16 #include "av1/encoder/grain_test_vectors.h"
17 #include "test/codec_factory.h"
18 #include "test/encode_test_driver.h"
19 #include "test/i420_video_source.h"
20 #include "test/util.h"
21 #include "test/video_source.h"
22
grain_equal(const aom_film_grain_t * expected,const aom_film_grain_t * actual)23 void grain_equal(const aom_film_grain_t *expected,
24 const aom_film_grain_t *actual) {
25 EXPECT_EQ(expected->apply_grain, actual->apply_grain);
26 EXPECT_EQ(expected->update_parameters, actual->update_parameters);
27 if (!expected->update_parameters) return;
28 EXPECT_EQ(expected->num_y_points, actual->num_y_points);
29 EXPECT_EQ(expected->num_cb_points, actual->num_cb_points);
30 EXPECT_EQ(expected->num_cr_points, actual->num_cr_points);
31 EXPECT_EQ(0, memcmp(expected->scaling_points_y, actual->scaling_points_y,
32 expected->num_y_points *
33 sizeof(expected->scaling_points_y[0])));
34 EXPECT_EQ(0, memcmp(expected->scaling_points_cb, actual->scaling_points_cb,
35 expected->num_cb_points *
36 sizeof(expected->scaling_points_cb[0])));
37 EXPECT_EQ(0, memcmp(expected->scaling_points_cr, actual->scaling_points_cr,
38 expected->num_cr_points *
39 sizeof(expected->scaling_points_cr[0])));
40 EXPECT_EQ(expected->scaling_shift, actual->scaling_shift);
41 EXPECT_EQ(expected->ar_coeff_lag, actual->ar_coeff_lag);
42 EXPECT_EQ(expected->ar_coeff_shift, actual->ar_coeff_shift);
43
44 const int num_pos_luma =
45 2 * expected->ar_coeff_lag * (expected->ar_coeff_lag + 1);
46 const int num_pos_chroma = num_pos_luma;
47 EXPECT_EQ(0, memcmp(expected->ar_coeffs_y, actual->ar_coeffs_y,
48 sizeof(expected->ar_coeffs_y[0]) * num_pos_luma));
49 if (actual->num_cb_points || actual->chroma_scaling_from_luma) {
50 EXPECT_EQ(0, memcmp(expected->ar_coeffs_cb, actual->ar_coeffs_cb,
51 sizeof(expected->ar_coeffs_cb[0]) * num_pos_chroma));
52 }
53 if (actual->num_cr_points || actual->chroma_scaling_from_luma) {
54 EXPECT_EQ(0, memcmp(expected->ar_coeffs_cr, actual->ar_coeffs_cr,
55 sizeof(expected->ar_coeffs_cr[0]) * num_pos_chroma));
56 }
57 EXPECT_EQ(expected->overlap_flag, actual->overlap_flag);
58 EXPECT_EQ(expected->chroma_scaling_from_luma,
59 actual->chroma_scaling_from_luma);
60 EXPECT_EQ(expected->grain_scale_shift, actual->grain_scale_shift);
61 // EXPECT_EQ(expected->random_seed, actual->random_seed);
62
63 // clip_to_restricted and bit_depth aren't written
64 if (expected->num_cb_points) {
65 EXPECT_EQ(expected->cb_mult, actual->cb_mult);
66 EXPECT_EQ(expected->cb_luma_mult, actual->cb_luma_mult);
67 EXPECT_EQ(expected->cb_offset, actual->cb_offset);
68 }
69 if (expected->num_cr_points) {
70 EXPECT_EQ(expected->cr_mult, actual->cr_mult);
71 EXPECT_EQ(expected->cr_luma_mult, actual->cr_luma_mult);
72 EXPECT_EQ(expected->cr_offset, actual->cr_offset);
73 }
74 }
75
TEST(FilmGrainTableTest,AddAndLookupSingleSegment)76 TEST(FilmGrainTableTest, AddAndLookupSingleSegment) {
77 aom_film_grain_table_t table;
78 memset(&table, 0, sizeof(table));
79
80 aom_film_grain_t grain;
81 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 0, 1000, false, &grain));
82
83 aom_film_grain_table_append(&table, 1000, 2000, film_grain_test_vectors + 0);
84 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 0, 1000, false, &grain));
85 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 2000, 3000, false, &grain));
86
87 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 1000, 2000, false, &grain));
88
89 grain.bit_depth = film_grain_test_vectors[0].bit_depth;
90 EXPECT_EQ(0, memcmp(&grain, film_grain_test_vectors + 0, sizeof(table)));
91
92 // Extend the existing segment
93 aom_film_grain_table_append(&table, 2000, 3000, film_grain_test_vectors + 0);
94 EXPECT_EQ(nullptr, table.head->next);
95
96 // Lookup and remove and check that the entry is no longer there
97 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 1000, 2000, true, &grain));
98 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 1000, 2000, false, &grain));
99
100 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 2000, 3000, true, &grain));
101 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 2000, 3000, false, &grain));
102
103 EXPECT_EQ(nullptr, table.head);
104 EXPECT_EQ(nullptr, table.tail);
105 aom_film_grain_table_free(&table);
106 }
107
TEST(FilmGrainTableTest,AddSingleSegmentRemoveBiggerSegment)108 TEST(FilmGrainTableTest, AddSingleSegmentRemoveBiggerSegment) {
109 aom_film_grain_table_t table;
110 aom_film_grain_t grain;
111
112 memset(&table, 0, sizeof(table));
113
114 aom_film_grain_table_append(&table, 0, 1000, film_grain_test_vectors + 0);
115 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 0, 1100, true, &grain));
116
117 EXPECT_EQ(nullptr, table.head);
118 EXPECT_EQ(nullptr, table.tail);
119 aom_film_grain_table_free(&table);
120 }
121
TEST(FilmGrainTableTest,SplitSingleSegment)122 TEST(FilmGrainTableTest, SplitSingleSegment) {
123 aom_film_grain_table_t table;
124 aom_film_grain_t grain;
125 memset(&table, 0, sizeof(table));
126
127 aom_film_grain_table_append(&table, 0, 1000, film_grain_test_vectors + 0);
128
129 // Test lookup and remove that adjusts start time
130 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 0, 100, true, &grain));
131 EXPECT_EQ(nullptr, table.head->next);
132 EXPECT_EQ(100, table.head->start_time);
133
134 // Test lookup and remove that adjusts end time
135 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 900, 1000, true, &grain));
136 EXPECT_EQ(nullptr, table.head->next);
137 EXPECT_EQ(100, table.head->start_time);
138 EXPECT_EQ(900, table.head->end_time);
139
140 // Test lookup and remove that splits the first entry
141 EXPECT_TRUE(aom_film_grain_table_lookup(&table, 400, 600, true, &grain));
142 EXPECT_EQ(100, table.head->start_time);
143 EXPECT_EQ(400, table.head->end_time);
144
145 ASSERT_NE(nullptr, table.head->next);
146 EXPECT_EQ(table.tail, table.head->next);
147 EXPECT_EQ(600, table.head->next->start_time);
148 EXPECT_EQ(900, table.head->next->end_time);
149
150 aom_film_grain_table_free(&table);
151 }
152
TEST(FilmGrainTableTest,AddAndLookupMultipleSegments)153 TEST(FilmGrainTableTest, AddAndLookupMultipleSegments) {
154 aom_film_grain_table_t table;
155 memset(&table, 0, sizeof(table));
156
157 aom_film_grain_t grain;
158 const int kNumTestVectors =
159 sizeof(film_grain_test_vectors) / sizeof(film_grain_test_vectors[0]);
160 for (int i = 0; i < kNumTestVectors; ++i) {
161 aom_film_grain_table_append(&table, i * 1000, (i + 1) * 1000,
162 film_grain_test_vectors + i);
163 }
164
165 for (int i = kNumTestVectors - 1; i >= 0; --i) {
166 EXPECT_TRUE(aom_film_grain_table_lookup(&table, i * 1000, (i + 1) * 1000,
167 true, &grain));
168 grain_equal(film_grain_test_vectors + i, &grain);
169 EXPECT_FALSE(aom_film_grain_table_lookup(&table, i * 1000, (i + 1) * 1000,
170 true, &grain));
171 }
172
173 // Verify that all the data has been removed
174 for (int i = 0; i < kNumTestVectors; ++i) {
175 EXPECT_FALSE(aom_film_grain_table_lookup(&table, i * 1000, (i + 1) * 1000,
176 true, &grain));
177 }
178 aom_film_grain_table_free(&table);
179 }
180
181 class FilmGrainTableIOTest : public ::testing::Test {
182 protected:
SetUp()183 void SetUp() override { memset(&error_, 0, sizeof(error_)); }
184 struct aom_internal_error_info error_;
185 };
186
TEST_F(FilmGrainTableIOTest,ReadMissingFile)187 TEST_F(FilmGrainTableIOTest, ReadMissingFile) {
188 aom_film_grain_table_t table;
189 memset(&table, 0, sizeof(table));
190 ASSERT_EQ(AOM_CODEC_ERROR, aom_film_grain_table_read(
191 &table, "/path/to/missing/file", &error_));
192 }
193
TEST_F(FilmGrainTableIOTest,ReadTruncatedFile)194 TEST_F(FilmGrainTableIOTest, ReadTruncatedFile) {
195 aom_film_grain_table_t table;
196 memset(&table, 0, sizeof(table));
197
198 std::string grain_file;
199 FILE *file = libaom_test::GetTempOutFile(&grain_file);
200 ASSERT_NE(file, nullptr);
201 fwrite("deadbeef", 8, 1, file);
202 fclose(file);
203 ASSERT_EQ(AOM_CODEC_ERROR,
204 aom_film_grain_table_read(&table, grain_file.c_str(), &error_));
205 EXPECT_EQ(0, remove(grain_file.c_str()));
206 }
207
TEST_F(FilmGrainTableIOTest,RoundTripReadWrite)208 TEST_F(FilmGrainTableIOTest, RoundTripReadWrite) {
209 aom_film_grain_table_t table;
210 memset(&table, 0, sizeof(table));
211
212 aom_film_grain_t expected_grain[16];
213 const int kNumTestVectors =
214 sizeof(film_grain_test_vectors) / sizeof(film_grain_test_vectors[0]);
215 for (int i = 0; i < kNumTestVectors; ++i) {
216 expected_grain[i] = film_grain_test_vectors[i];
217 expected_grain[i].random_seed = i;
218 expected_grain[i].update_parameters = i % 2;
219 expected_grain[i].apply_grain = (i + 1) % 2;
220 expected_grain[i].bit_depth = 0;
221 aom_film_grain_table_append(&table, i * 1000, (i + 1) * 1000,
222 expected_grain + i);
223 }
224 std::string grain_file;
225 FILE *tmpfile = libaom_test::GetTempOutFile(&grain_file);
226 ASSERT_NE(tmpfile, nullptr);
227 fclose(tmpfile);
228 ASSERT_EQ(AOM_CODEC_OK,
229 aom_film_grain_table_write(&table, grain_file.c_str(), &error_));
230 aom_film_grain_table_free(&table);
231
232 memset(&table, 0, sizeof(table));
233 ASSERT_EQ(AOM_CODEC_OK,
234 aom_film_grain_table_read(&table, grain_file.c_str(), &error_));
235 for (int i = 0; i < kNumTestVectors; ++i) {
236 aom_film_grain_t grain;
237 EXPECT_TRUE(aom_film_grain_table_lookup(&table, i * 1000, (i + 1) * 1000,
238 true, &grain));
239 grain_equal(expected_grain + i, &grain);
240 }
241 aom_film_grain_table_free(&table);
242 EXPECT_EQ(0, remove(grain_file.c_str()));
243 }
244
TEST_F(FilmGrainTableIOTest,RoundTripSplit)245 TEST_F(FilmGrainTableIOTest, RoundTripSplit) {
246 std::string grain_file;
247 FILE *tmpfile = libaom_test::GetTempOutFile(&grain_file);
248 ASSERT_NE(tmpfile, nullptr);
249 fclose(tmpfile);
250
251 aom_film_grain_table_t table;
252 memset(&table, 0, sizeof(table));
253
254 aom_film_grain_t grain = film_grain_test_vectors[0];
255 aom_film_grain_table_append(&table, 0, 3000, &grain);
256 ASSERT_TRUE(aom_film_grain_table_lookup(&table, 1000, 2000, true, &grain));
257 ASSERT_TRUE(aom_film_grain_table_lookup(&table, 0, 1000, false, &grain));
258 EXPECT_FALSE(aom_film_grain_table_lookup(&table, 1000, 2000, false, &grain));
259 ASSERT_TRUE(aom_film_grain_table_lookup(&table, 2000, 3000, false, &grain));
260 ASSERT_EQ(AOM_CODEC_OK,
261 aom_film_grain_table_write(&table, grain_file.c_str(), &error_));
262 aom_film_grain_table_free(&table);
263
264 memset(&table, 0, sizeof(table));
265 ASSERT_EQ(AOM_CODEC_OK,
266 aom_film_grain_table_read(&table, grain_file.c_str(), &error_));
267 ASSERT_TRUE(aom_film_grain_table_lookup(&table, 0, 1000, false, &grain));
268 ASSERT_FALSE(aom_film_grain_table_lookup(&table, 1000, 2000, false, &grain));
269 ASSERT_TRUE(aom_film_grain_table_lookup(&table, 2000, 3000, false, &grain));
270 aom_film_grain_table_free(&table);
271
272 EXPECT_EQ(0, remove(grain_file.c_str()));
273 }
274
275 const ::libaom_test::TestMode kFilmGrainEncodeTestModes[] = {
276 ::libaom_test::kRealTime,
277 #if !CONFIG_REALTIME_ONLY
278 ::libaom_test::kOnePassGood
279 #endif
280 };
281
282 class FilmGrainEncodeTest
283 : public ::libaom_test::CodecTestWith3Params<int, int,
284 ::libaom_test::TestMode>,
285 public ::libaom_test::EncoderTest {
286 protected:
FilmGrainEncodeTest()287 FilmGrainEncodeTest()
288 : EncoderTest(GET_PARAM(0)), test_monochrome_(GET_PARAM(1)),
289 key_frame_dist_(GET_PARAM(2)), test_mode_(GET_PARAM(3)) {}
290 ~FilmGrainEncodeTest() override = default;
291
SetUp()292 void SetUp() override {
293 InitializeConfig(test_mode_);
294 cfg_.monochrome = test_monochrome_ == 1;
295 cfg_.rc_target_bitrate = 300;
296 cfg_.kf_max_dist = key_frame_dist_;
297 cfg_.g_lag_in_frames = 0;
298 }
299
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)300 void PreEncodeFrameHook(::libaom_test::VideoSource *video,
301 ::libaom_test::Encoder *encoder) override {
302 if (video->frame() == 0) {
303 encoder->Control(AOME_SET_CPUUSED,
304 test_mode_ == ::libaom_test::kRealTime ? 7 : 5);
305 encoder->Control(AV1E_SET_TUNE_CONTENT, AOM_CONTENT_FILM);
306 encoder->Control(AV1E_SET_DENOISE_NOISE_LEVEL, 1);
307 } else if (video->frame() == 1) {
308 cfg_.monochrome = (test_monochrome_ == 1 || test_monochrome_ == 2);
309 encoder->Config(&cfg_);
310 } else {
311 cfg_.monochrome = test_monochrome_ == 1;
312 encoder->Config(&cfg_);
313 }
314 }
315
DoDecode() const316 bool DoDecode() const override { return false; }
317
DoTest()318 void DoTest() {
319 ::libaom_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
320 288, 30, 1, 0, 3);
321 cfg_.g_w = video.img()->d_w;
322 cfg_.g_h = video.img()->d_h;
323 ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
324 }
325
326 private:
327 // 0: monochroome always off.
328 // 1: monochrome always on.
329 // 2: monochrome changes from 0, 1, 0, for encoded frames 0, 1, 2.
330 // The case where monochrome changes from 1 to 0 (i.e., encoder initialized
331 // with monochrome = 1 and then subsequently encoded with monochrome = 0)
332 // will fail. The test InitMonochrome1_EncodeMonochrome0 below verifies this.
333 int test_monochrome_;
334 int key_frame_dist_;
335 ::libaom_test::TestMode test_mode_;
336 };
337
TEST_P(FilmGrainEncodeTest,Test)338 TEST_P(FilmGrainEncodeTest, Test) { DoTest(); }
339
340 AV1_INSTANTIATE_TEST_SUITE(FilmGrainEncodeTest, ::testing::Range(0, 3),
341 ::testing::Values(0, 10),
342 ::testing::ValuesIn(kFilmGrainEncodeTestModes));
343
344 // Initialize encoder with monochrome = 1, and then encode frame with
345 // monochrome = 0. This will result in an error: see the following check
346 // in encoder_set_config() in av1/av1_cx_iface.c.
347 // TODO(marpan): Consider moving this test to another file, as the failure
348 // has nothing to do with film grain mode.
TEST(FilmGrainEncodeTest,InitMonochrome1EncodeMonochrome0)349 TEST(FilmGrainEncodeTest, InitMonochrome1EncodeMonochrome0) {
350 const int kWidth = 352;
351 const int kHeight = 288;
352 const int usage = AOM_USAGE_REALTIME;
353 aom_codec_iface_t *iface = aom_codec_av1_cx();
354 aom_codec_enc_cfg_t cfg;
355 ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, usage), AOM_CODEC_OK);
356 aom_codec_ctx_t enc;
357 cfg.g_w = kWidth;
358 cfg.g_h = kHeight;
359 // Initialize encoder, with monochrome = 0.
360 cfg.monochrome = 1;
361 aom_codec_err_t init_status = aom_codec_enc_init(&enc, iface, &cfg, 0);
362 ASSERT_EQ(init_status, AOM_CODEC_OK);
363 ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, 7), AOM_CODEC_OK);
364 ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_TUNE_CONTENT, AOM_CONTENT_FILM),
365 AOM_CODEC_OK);
366 ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_DENOISE_NOISE_LEVEL, 1),
367 AOM_CODEC_OK);
368 // Set image with zero values.
369 constexpr size_t kBufferSize =
370 kWidth * kHeight + 2 * (kWidth + 1) / 2 * (kHeight + 1) / 2;
371 std::vector<unsigned char> buffer(kBufferSize);
372 aom_image_t img;
373 EXPECT_EQ(&img, aom_img_wrap(&img, AOM_IMG_FMT_I420, kWidth, kHeight, 1,
374 buffer.data()));
375 // Encode first frame.
376 ASSERT_EQ(aom_codec_encode(&enc, &img, 0, 1, 0), AOM_CODEC_OK);
377 // Second frame: update config with monochrome = 1.
378 cfg.monochrome = 0;
379 ASSERT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_INVALID_PARAM);
380 ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
381 }
382