]> git.sesse.net Git - movit/blob - gamma_expansion_effect_test.cpp
Fix some stack overflows in unit tests; found with asan.
[movit] / gamma_expansion_effect_test.cpp
1 // Unit tests for GammaExpansionEffect.
2
3 #include <epoxy/gl.h>
4 #include <math.h>
5
6 #include "gamma_expansion_effect.h"
7 #include "gtest/gtest.h"
8 #include "gtest/gtest-message.h"
9 #include "test_util.h"
10
11 namespace movit {
12
13 TEST(GammaExpansionEffectTest, sRGB_KeyValues) {
14         float data[] = {
15                 0.0f, 1.0f,
16                 0.040f, 0.041f,  // On either side of the discontinuity.
17         };
18         float expected_data[] = {
19                 0.0f, 1.0f,
20                 0.00309f, 0.00317f, 
21         };
22         float out_data[4];
23         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
24         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
25
26         expect_equal(expected_data, out_data, 2, 2);
27 }
28
29 TEST(GammaExpansionEffectTest, sRGB_RampAlwaysIncreases) {
30         float data[256], out_data[256];
31         for (unsigned i = 0; i < 256; ++i) {
32                 data[i] = i / 255.0f;
33         }
34         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
35         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
36
37         for (unsigned i = 1; i < 256; ++i) {
38                 EXPECT_GT(out_data[i], out_data[i - 1])
39                    << "No increase between " << i-1 << " and " << i;
40         }
41 }
42
43 TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
44         float data[] = {
45                 0.0f, 0.0f, 0.0f, 0.0f,
46                 0.0f, 0.0f, 0.0f, 0.25f,
47                 0.0f, 0.0f, 0.0f, 0.5f,
48                 0.0f, 0.0f, 0.0f, 0.75f,
49                 0.0f, 0.0f, 0.0f, 1.0f,
50         };
51         float out_data[5 * 4];
52         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
53         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
54
55         expect_equal(data, out_data, 5, 1);
56 }
57
58 TEST(GammaExpansionEffectTest, sRGB_Accuracy) {
59         float data[256], expected_data[256], out_data[256];
60
61         for (int i = 0; i < 256; ++i) {
62                 double x = i / 255.0;
63
64                 data[i] = x;
65
66                 // From the Wikipedia article on sRGB.
67                 if (x < 0.04045) {
68                         expected_data[i] = x / 12.92;
69                 } else {
70                         expected_data[i] = pow((x + 0.055) / 1.055, 2.4);
71                 }
72         }
73
74         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
75         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
76
77         // Accuracy limits; for comparison, limits for a straightforward ALU solution
78         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
79         // indicate limitations of float arithmetic etc.:
80         //
81         //   Maximum absolute error: 0.1% of max energy (0.051%)
82         //   Maximum relative error: 2.5% of correct answer (0.093%)
83         //                           25% of difference to next pixel level (6.18%)
84         //   Allowed RMS error:      0.0001 (0.000010)
85         //
86         test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
87 }
88
89 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
90         float data[] = {
91                 0.0f, 1.0f,
92                 0.080f, 0.082f,  // On either side of the discontinuity.
93         };
94         float expected_data[] = {
95                 0.0f, 1.0f,
96                 0.017778f, 0.018167f,
97         };
98         float out_data[4];
99         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
100         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
101
102         expect_equal(expected_data, out_data, 2, 2);
103 }
104
105 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
106         float data[256], out_data[256];
107         for (unsigned i = 0; i < 256; ++i) {
108                 data[i] = i / 255.0f;
109         }
110         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
111         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
112
113         for (unsigned i = 1; i < 256; ++i) {
114                 EXPECT_GT(out_data[i], out_data[i - 1])
115                    << "No increase between " << i-1 << " and " << i;
116         }
117 }
118
119 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
120         float data[] = {
121                 0.0f, 0.0f, 0.0f, 0.0f,
122                 0.0f, 0.0f, 0.0f, 0.25f,
123                 0.0f, 0.0f, 0.0f, 0.5f,
124                 0.0f, 0.0f, 0.0f, 0.75f,
125                 0.0f, 0.0f, 0.0f, 1.0f,
126         };
127         float out_data[5 * 4];
128         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
129         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
130
131         expect_equal(data, out_data, 5, 1);
132 }
133
134 TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
135         float data[256], expected_data[256], out_data[256];
136
137         for (int i = 0; i < 256; ++i) {
138                 double x = i / 255.0;
139
140                 data[i] = x;
141
142                 // Rec. 2020, page 3.
143                 if (x < 0.018 * 4.5) {
144                         expected_data[i] = x / 4.5;
145                 } else {
146                         expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
147                 }
148         }
149
150         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
151         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
152
153         // Accuracy limits; for comparison, limits for a straightforward ALU solution
154         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
155         // indicate limitations of float arithmetic etc.:
156         //
157         //   Maximum absolute error: 0.1% of max energy (0.046%)
158         //   Maximum relative error: 1.0% of correct answer (0.080%)
159         //                           10% of difference to next pixel level (6.19%)
160         //   Allowed RMS error:      0.0001 (0.000010)
161         //
162         test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
163 }
164
165 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
166 // input range and somewhat looser error bounds. (One could claim that this is
167 // already on the limit of what we can reasonably do with fp16 input, if you
168 // look at the local relative error.)
169 TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
170         float data[1024], expected_data[1024], out_data[1024];
171
172         for (int i = 0; i < 1024; ++i) {
173                 double x = i / 1023.0;
174
175                 data[i] = x;
176
177                 // Rec. 2020, page 3.
178                 if (x < 0.018 * 4.5) {
179                         expected_data[i] = x / 4.5;
180                 } else {
181                         expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
182                 }
183         }
184
185         EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
186         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
187
188         // Accuracy limits; for comparison, limits for a straightforward ALU solution
189         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
190         // indicate limitations of float arithmetic etc.:
191         //
192         //   Maximum absolute error: 0.1% of max energy (0.036%)
193         //   Maximum relative error: 1.0% of correct answer (0.064%)
194         //                           30% of difference to next pixel level (24.9%)
195         //   Allowed RMS error:      0.0001 (0.000005)
196         //
197         test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
198 }
199
200 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
201         float data[256];
202         for (unsigned i = 0; i < 256; ++i) {
203                 data[i] = i / 255.0f;
204         }
205         float out_data_709[256];
206         float out_data_2020[256];
207
208         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
209         tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
210         EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
211         tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
212
213         double sqdiff = 0.0;
214         for (unsigned i = 0; i < 256; ++i) {
215                 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
216                 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
217         }
218         EXPECT_GT(sqdiff, 1e-6);
219 }
220
221 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
222 // all of the possible 12-bit input values (every other level translates to the
223 // same value). Thus, this test has extremely loose bounds; if we ever decide
224 // to start supporting fp32, we should re-run this and tighten them a lot.
225 TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
226         float data[4096], expected_data[4096], out_data[4096];
227
228         for (int i = 0; i < 4096; ++i) {
229                 double x = i / 4095.0;
230
231                 data[i] = x;
232
233                 // Rec. 2020, page 3.
234                 if (x < 0.0181 * 4.5) {
235                         expected_data[i] = x / 4.5;
236                 } else {
237                         expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
238                 }
239         }
240
241         EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
242         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
243
244         // Accuracy limits; for comparison, limits for a straightforward ALU solution
245         // (using a branch and pow()) in parenthesis, used as a “high anchor” to
246         // indicate limitations of float arithmetic etc.:
247         //
248         //   Maximum absolute error: 0.1% of max energy (0.050%)
249         //   Maximum relative error: 1.0% of correct answer (0.050%)
250         //                           250% of difference to next pixel level (100.00%)
251         //   Allowed RMS error:      0.0001 (0.000003)
252         //
253         test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);
254 }
255
256 }  // namespace movit