]> git.sesse.net Git - movit/blob - gamma_expansion_effect.cpp
Add SHELL override from autoconf, since otherwise libtool might break on systems...
[movit] / gamma_expansion_effect.cpp
1 #include <math.h>
2 #include <assert.h>
3
4 #include "effect_util.h"
5 #include "gamma_expansion_effect.h"
6 #include "util.h"
7
8 using namespace std;
9
10 GammaExpansionEffect::GammaExpansionEffect()
11         : source_curve(GAMMA_LINEAR)
12 {
13         register_int("source_curve", (int *)&source_curve);
14 }
15
16 string GammaExpansionEffect::output_fragment_shader()
17 {
18         if (source_curve == GAMMA_LINEAR) {
19                 return read_file("identity.frag");
20         }
21         if (source_curve == GAMMA_sRGB ||
22             source_curve == GAMMA_REC_709 ||  // Also includes Rec. 601, and 10-bit Rec. 2020.
23             source_curve == GAMMA_REC_2020_12_BIT) {
24                 return read_file("gamma_expansion_effect.frag");
25         }
26         assert(false);
27 }
28
29 void GammaExpansionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
30 {
31         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
32
33         // All of these curves follow a continuous curve that's piecewise defined;
34         // very low values (up to some β) are linear. Above β, we have a power curve
35         // that looks like this:
36         //
37         //   y = ((x + ɑ - 1) / ɑ)^ɣ
38         //
39         // However, pow() is relatively slow in GLSL, so we approximate this
40         // part by a minimax polynomial, whose coefficients are precalculated
41         // in Maple. (It is very hard to accurately model the curve as a whole
42         // using minimax polynomials; both Maple and Mathematically generally
43         // just error out if you ask them to optimize over 0..1 with a higher-degree
44         // polynomial.)
45         //
46         // We put some extra weight on areas near β to keep a continuous curve,
47         // and near 1.0, since we'd really like f(1.0) = 1.0, or approximately so.
48         // The following Maple commands, using sRGB below as an example, will
49         // compute the coefficients:
50         //
51         // > alpha := 1.055;
52         // > beta := 0.04045;
53         // > gamma_ := 2.4;
54         // > w := x -> piecewise(x < beta + 0.001, 10, x > 0.999, 10, 1);
55         // > numapprox[minimax](((x + alpha - 1) / alpha)^gamma_, x=beta..1, [4,0], w(x), 'maxerror');
56         //
57         // The variable 'maxerror' will then contain the maximum absolute error
58         // at any point of the curve, and we report this along with the absolute
59         // error at beta and at 1.0. Keep in mind that along this curve,
60         // the smallest minimum difference between any two 8-bit sRGB pixel levels
61         // (in the exponential part of the curve) in linear light is that
62         // between 11/255 and 12/255, which is about 0.00033 (or three to four
63         // times of the sRGB maxerror below). The choice of a fourth-degree
64         // polynomial was made with this in mind; we have not cared equally
65         // much about 10- and 12-bit Rec. 2020.
66         //
67         // NOTE: The error at beta is compared to the _linear_ part of the curve.
68         // Since the standards give these with only a few decimals, it means that
69         // the linear and exponential parts will not match up exactly, and even
70         // a perfect approximation will have error > 0 here; sometimes, even larger
71         // than maxerror for the curve itself.
72
73         if (source_curve == GAMMA_sRGB) {
74                 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
75                 // β = 0.04045, ɣ = 2.4.
76                 // maxerror      = 0.000094
77                 // error at beta = 0.000012
78                 // error at 1.0  = 0.000012
79                 //
80                 // Note that the worst _relative_ error by far is just at the beginning
81                 // of the exponential curve, ie., just around β.
82                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 12.92);
83                 set_uniform_float(glsl_program_num, prefix, "c0", 0.001324469581);
84                 set_uniform_float(glsl_program_num, prefix, "c1", 0.02227416690);
85                 set_uniform_float(glsl_program_num, prefix, "c2", 0.5917615253);
86                 set_uniform_float(glsl_program_num, prefix, "c3", 0.4733532353);
87                 set_uniform_float(glsl_program_num, prefix, "c4", -0.08880738120);
88                 set_uniform_float(glsl_program_num, prefix, "beta", 0.04045);
89         }
90         if (source_curve == GAMMA_REC_709) {  // Also includes Rec. 601, and 10-bit Rec. 2020.
91                 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018 * 4.5, ɣ = 1/0.45.
92                 // maxerror      = 0.000043
93                 // error at beta = 0.000051 (see note above!)
94                 // error at 1.0  = 0.000004
95                 //
96                 // Note that Rec. 2020 only gives the other direction, which is why
97                 // our beta and gamma are different from the numbers mentioned
98                 // (we've inverted the formula).
99                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 4.5);
100                 set_uniform_float(glsl_program_num, prefix, "c0", 0.005137028744);
101                 set_uniform_float(glsl_program_num, prefix, "c1", 0.09802596889);
102                 set_uniform_float(glsl_program_num, prefix, "c2", 0.7255768864);
103                 set_uniform_float(glsl_program_num, prefix, "c3", 0.2135067966);
104                 set_uniform_float(glsl_program_num, prefix, "c4", -0.04225094667);
105                 set_uniform_float(glsl_program_num, prefix, "beta", 0.018 * 4.5);
106         }
107         if (source_curve == GAMMA_REC_2020_12_BIT) {
108                 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181 * 4.5, ɣ = 1/0.45.
109                 // maxerror      = 0.000042
110                 // error at beta = 0.000005
111                 // error at 1.0  = 0.000004
112                 //
113                 // Note that Rec. 2020 only gives the other direction, which is why
114                 // our beta and gamma are different from the numbers mentioned
115                 // (we've inverted the formula).
116                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 4.5);
117                 set_uniform_float(glsl_program_num, prefix, "c0", 0.005167545928);
118                 set_uniform_float(glsl_program_num, prefix, "c1", 0.09835585809);
119                 set_uniform_float(glsl_program_num, prefix, "c2", 0.7254820139);
120                 set_uniform_float(glsl_program_num, prefix, "c3", 0.2131291155);
121                 set_uniform_float(glsl_program_num, prefix, "c4", -0.04213877222);
122                 set_uniform_float(glsl_program_num, prefix, "beta", 0.0181 * 4.5);
123         }
124 }