]> git.sesse.net Git - movit/blob - gamma_compression_effect_test.cpp
In EffectChainTest, do not re-call init_movit()
[movit] / gamma_compression_effect_test.cpp
1 // Unit tests for GammaCompressionEffect.
2 //
3 // Pretty much the inverse of the GammaExpansionEffect tests;
4 // EffectChainTest tests that they are actually inverses.
5 // However, the accuracy tests are somewhat simpler, since we
6 // only need to care about absolute errors and not relative.
7
8 #include <GL/glew.h>
9 #include <math.h>
10
11 #include "gtest/gtest.h"
12 #include "gtest/gtest-message.h"
13 #include "image_format.h"
14 #include "test_util.h"
15
16 TEST(GammaCompressionEffectTest, sRGB_KeyValues) {
17         float data[] = {
18                 0.0f, 1.0f,
19                 0.00309f, 0.00317f,   // On either side of the discontinuity.
20                 -0.5f, 1.5f,          // To check clamping.
21         };
22         float expected_data[] = {
23                 0.0f, 1.0f,
24                 0.040f, 0.041f,
25                 0.0f, 1.0f,
26         };
27         float out_data[4];
28         EffectChainTester tester(data, 2, 3, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
29         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
30
31         expect_equal(expected_data, out_data, 2, 3);
32 }
33
34 TEST(GammaCompressionEffectTest, sRGB_RampAlwaysIncreases) {
35         float data[256], out_data[256];
36         for (unsigned i = 0; i < 256; ++i) {
37                 data[i] = i / 255.0f;
38         }
39         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
40         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
41
42         for (unsigned i = 1; i < 256; ++i) {
43                 EXPECT_GT(out_data[i], out_data[i - 1])
44                    << "No increase between " << i-1 << " and " << i;
45         }
46 }
47
48 TEST(GammaCompressionEffectTest, sRGB_Accuracy) {
49         float data[256], expected_data[256], out_data[256];
50
51         for (int i = 0; i < 256; ++i) {
52                 double x = i / 255.0;
53
54                 expected_data[i] = x;
55
56                 // From the Wikipedia article on sRGB.
57                 if (x < 0.04045) {
58                         data[i] = x / 12.92;
59                 } else {
60                         data[i] = pow((x + 0.055) / 1.055, 2.4);
61                 }
62         }
63
64         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
65         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
66
67         // Maximum absolute error is 25% of one pixel level. For comparison,
68         // a straightforward ALU solution (using a branch and pow()), used as a
69         // “high anchor” to indicate limitations of float arithmetic etc.,
70         // reaches maximum absolute error of 3.7% of one pixel level
71         // and rms of 3.2e-6.
72         expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-4);
73 }
74
75 TEST(GammaCompressionEffectTest, Rec709_KeyValues) {
76         float data[] = {
77                 0.0f, 1.0f,
78                 0.017778f, 0.018167f,  // On either side of the discontinuity.
79         };
80         float expected_data[] = {
81                 0.0f, 1.0f,
82                 0.080f, 0.082f,
83         };
84         float out_data[4];
85         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
86         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
87
88         expect_equal(expected_data, out_data, 2, 2);
89 }
90
91 TEST(GammaCompressionEffectTest, Rec709_RampAlwaysIncreases) {
92         float data[256], out_data[256];
93         for (unsigned i = 0; i < 256; ++i) {
94                 data[i] = i / 255.0f;
95         }
96         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
97         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
98
99         for (unsigned i = 1; i < 256; ++i) {
100                 EXPECT_GT(out_data[i], out_data[i - 1])
101                    << "No increase between " << i-1 << " and " << i;
102         }
103 }
104
105 TEST(GammaCompressionEffectTest, Rec709_Accuracy) {
106         float data[256], expected_data[256], out_data[256];
107
108         for (int i = 0; i < 256; ++i) {
109                 double x = i / 255.0;
110
111                 expected_data[i] = x;
112
113                 // Rec. 2020, page 3.
114                 if (x < 0.018 * 4.5) {
115                         data[i] = x / 4.5;
116                 } else {
117                         data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
118                 }
119         }
120
121         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
122         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
123
124         // Maximum absolute error is 25% of one pixel level. For comparison,
125         // a straightforward ALU solution (using a branch and pow()), used as a
126         // “high anchor” to indicate limitations of float arithmetic etc.,
127         // reaches maximum absolute error of 3.7% of one pixel level
128         // and rms of 3.5e-6.
129         expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-5);
130 }
131
132 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
133 // input range and somewhat looser error bounds. (One could claim that this is
134 // already on the limit of what we can reasonably do with fp16 input, if you
135 // look at the local relative error.)
136 TEST(GammaCompressionEffectTest, Rec2020_10Bit_Accuracy) {
137         float data[1024], expected_data[1024], out_data[1024];
138
139         for (int i = 0; i < 1024; ++i) {
140                 double x = i / 1023.0;
141
142                 expected_data[i] = x;
143
144                 // Rec. 2020, page 3.
145                 if (x < 0.018 * 4.5) {
146                         data[i] = x / 4.5;
147                 } else {
148                         data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
149                 }
150         }
151
152         EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
153         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT);
154
155         // Maximum absolute error is 30% of one pixel level. For comparison,
156         // a straightforward ALU solution (using a branch and pow()), used as a
157         // “high anchor” to indicate limitations of float arithmetic etc.,
158         // reaches maximum absolute error of 25.2% of one pixel level
159         // and rms of 1.8e-6, so this is probably mostly related to input precision.
160         expect_equal(expected_data, out_data, 1024, 1, 0.30 / 1023.0, 1e-5);
161 }
162
163 TEST(GammaCompressionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
164         float data[4096];
165         for (unsigned i = 0; i < 4096; ++i) {
166                 data[i] = i / 4095.0f;
167         }
168         float out_data_709[4096];
169         float out_data_2020[4096];
170
171         EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
172         tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
173         EffectChainTester tester2(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
174         tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
175
176         double sqdiff = 0.0;
177         for (unsigned i = 0; i < 4096; ++i) {
178                 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 0.001);
179                 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
180         }
181         EXPECT_GT(sqdiff, 1e-6);
182 }
183
184 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
185 // all of the possible 12-bit input values (every other level translates to the
186 // same value). Thus, this test has extremely loose bounds; if we ever decide
187 // to start supporting fp32, we should re-run this and tighten them a lot.
188 TEST(GammaCompressionEffectTest, Rec2020_12Bit_Inaccuracy) {
189         float data[4096], expected_data[4096], out_data[4096];
190
191         for (int i = 0; i < 4096; ++i) {
192                 double x = i / 4095.0;
193
194                 expected_data[i] = x;
195
196                 // Rec. 2020, page 3.
197                 if (x < 0.0181 * 4.5) {
198                         data[i] = x / 4.5;
199                 } else {
200                         data[i] = pow((x + 0.0993) / 1.0993, 1.0 / 0.45);
201                 }
202         }
203
204         EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
205         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
206
207         // Maximum absolute error is 120% of one pixel level. For comparison,
208         // a straightforward ALU solution (using a branch and pow()), used as a
209         // “high anchor” to indicate limitations of float arithmetic etc.,
210         // reaches maximum absolute error of 71.1% of one pixel level
211         // and rms of 0.9e-6, so this is probably a combination of input
212         // precision and inaccuracies in the polynomial approximation.
213         expect_equal(expected_data, out_data, 4096, 1, 1.2 / 4095.0, 1e-5);
214 }