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