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