]> git.sesse.net Git - movit/blob - gamma_compression_effect.cpp
Make EffectChainTester capable of testing chains with multiple outputs.
[movit] / gamma_compression_effect.cpp
1 #include <assert.h>
2
3 #include "effect_util.h"
4 #include "gamma_compression_effect.h"
5 #include "util.h"
6
7 using namespace std;
8
9 namespace movit {
10
11 GammaCompressionEffect::GammaCompressionEffect()
12         : destination_curve(GAMMA_LINEAR)
13 {
14         register_int("destination_curve", (int *)&destination_curve);
15         register_uniform_float("linear_scale", &uniform_linear_scale);
16         register_uniform_float("c0", &uniform_c0);
17         register_uniform_float("c1", &uniform_c1);
18         register_uniform_float("c2", &uniform_c2);
19         register_uniform_float("c3", &uniform_c3);
20         register_uniform_float("c4", &uniform_c4);
21         register_uniform_float("beta", &uniform_beta);
22 }
23
24 string GammaCompressionEffect::output_fragment_shader()
25 {
26         if (destination_curve == GAMMA_LINEAR) {
27                 return read_file("identity.frag");
28         }
29         if (destination_curve == GAMMA_sRGB ||
30             destination_curve == GAMMA_REC_709 ||  // Also includes Rec. 601, and 10-bit Rec. 2020.
31             destination_curve == GAMMA_REC_2020_12_BIT) {
32                 return read_file("gamma_compression_effect.frag");
33         }
34         assert(false);
35 }
36
37 void GammaCompressionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
38 {
39         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
40
41         // See GammaExpansionEffect for more details about the approximations in use;
42         // we will primarily deal with the differences here.
43         //
44         // Like in expansion, we have a piecewise curve that for very low values
45         // (up to some β) are linear. Above β, we have a power curve that looks
46         // like this:
47         //
48         //   y = ɑ x^ɣ - (ɑ - 1)
49         //
50         // Like in expansion, we want to approximate this by some minimax polynomial
51         // in the range β..1. However, in this case, ɣ is typically around 0.4, and
52         // x^0.4 is actually very hard to approximate accurately in this range.
53         // We do a little trick by instead asking for a polynomial of s=sqrt(x),
54         // which means we instead need something like s^0.8, which is much easier.
55         // This warps the input space a bit as seen by the minimax algorithm,
56         // but since we are optimizing for _maximum_ error and not _average_,
57         // we should not add any extra weighting factors.
58         //
59         // However, since we have problems reaching the desired accuracy (~25%
60         // of a pixel level), especially for sRGB, we modify w(x) from
61         // GammaExpansionEffect to remove the special handling of the area
62         // around β; it is not really as useful when the next step is just a
63         // dither and round anyway. We keep it around 1, though, since that
64         // seems to hurt less.
65         //
66         // The Maple commands this time around become (again using sRGB as an example):
67         //
68         // > alpha := 1.055;
69         // > beta := 0.0031308;
70         // > gamma_ := 1.0/2.4;
71         // > w := x -> piecewise(x > 0.999, 10, 1);
72         // > numapprox[minimax](alpha * (x^2)^gamma_ - (alpha - 1), x=sqrt(beta)..1, [4,0], w(x^2), 'maxerror');
73         //
74         // Since the error here is possible to interpret on a uniform scale,
75         // we also show it as a value relative to a 8-, 10- or 12-bit pixel level,
76         // as appropriate.
77
78         if (destination_curve == GAMMA_sRGB) {
79                 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
80                 // β = 0.0031308, ɣ = 1/2.4.
81                 // maxerror      = 0.000785 = 0.200 * 255
82                 // error at 1.0  = 0.000078 = 0.020 * 255
83                 uniform_linear_scale = 12.92;
84                 uniform_c0 = -0.03679675939;
85                 uniform_c1 = 1.443803073;
86                 uniform_c2 = -0.9239780987;
87                 uniform_c3 = 0.8060491596;
88                 uniform_c4 = -0.2891558568;
89                 uniform_beta = 0.0031308;
90         }
91         if (destination_curve == GAMMA_REC_709) {  // Also includes Rec. 601, and 10-bit Rec. 2020.
92                 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018, ɣ = 0.45.
93                 // maxerror      = 0.000131 = 0.033 * 255 = 0.134 * 1023
94                 // error at 1.0  = 0.000013 = 0.003 * 255 = 0.013 * 1023
95                 uniform_linear_scale = 4.5;
96                 uniform_c0 = -0.08541688528;
97                 uniform_c1 = 1.292793370;
98                 uniform_c2 = -0.4070417645;
99                 uniform_c3 = 0.2923891828;
100                 uniform_c4 = -0.09273699351;
101                 uniform_beta = 0.018;
102         }
103         if (destination_curve == GAMMA_REC_2020_12_BIT) {
104                 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181, ɣ = 0.45.
105                 // maxerror      = 0.000130 = 0.533 * 4095
106                 // error at 1.0  = 0.000013 = 0.053 * 4095
107                 //
108                 // Note that this error is above one half of a pixel level,
109                 // which means that a few values will actually be off in the lowest
110                 // bit. (Removing the constraint for x=1 will only take this down
111                 // from 0.553 to 0.501; adding a fifth order can get it down to
112                 // 0.167, although this assumes working in fp64 and not fp32.)
113                 uniform_linear_scale = 4.5;
114                 uniform_c0 = -0.08569685663;
115                 uniform_c1 = 1.293000900;
116                 uniform_c2 = -0.4067291321;
117                 uniform_c3 = 0.2919741179;
118                 uniform_c4 = -0.09256205770;
119                 uniform_beta = 0.0181;
120         }
121 }
122
123 }  // namespace movit