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