1 #include <assert.h>
3 #include "effect_util.h"
4 #include "gamma_compression_effect.h"
5 #include "util.h"
7 using namespace std;
9 namespace movit {
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_array("c", uniform_c, 5);
17         register_uniform_float("beta", &uniform_beta);
18 }
21 {
22         if (destination_curve == GAMMA_LINEAR) {
24         }
25         if (destination_curve == GAMMA_sRGB ||
26             destination_curve == GAMMA_REC_709 ||  // Also includes Rec. 601, and 10-bit Rec. 2020.
27             destination_curve == GAMMA_REC_2020_12_BIT) {
29         }
30         assert(false);
31 }
33 void GammaCompressionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
34 {
35         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
37         // See GammaExpansionEffect for more details about the approximations in use;
38         // we will primarily deal with the differences here.
39         //
40         // Like in expansion, we have a piecewise curve that for very low values
41         // (up to some β) are linear. Above β, we have a power curve that looks
42         // like this:
43         //
44         //   y = ɑ x^ɣ - (ɑ - 1)
45         //
46         // Like in expansion, we want to approximate this by some minimax polynomial
47         // in the range β..1. However, in this case, ɣ is typically around 0.4, and
48         // x^0.4 is actually very hard to approximate accurately in this range.
49         // We do a little trick by instead asking for a polynomial of s=sqrt(x),
50         // which means we instead need something like s^0.8, which is much easier.
51         // This warps the input space a bit as seen by the minimax algorithm,
52         // but since we are optimizing for _maximum_ error and not _average_,
53         // we should not add any extra weighting factors.
54         //
55         // However, since we have problems reaching the desired accuracy (~25%
56         // of a pixel level), especially for sRGB, we modify w(x) from
57         // GammaExpansionEffect to remove the special handling of the area
58         // around β; it is not really as useful when the next step is just a
59         // dither and round anyway. We keep it around 1, though, since that
60         // seems to hurt less.
61         //
62         // The Maple commands this time around become (again using sRGB as an example):
63         //
64         // > alpha := 1.055;
65         // > beta := 0.0031308;
66         // > gamma_ := 1.0/2.4;
67         // > w := x -> piecewise(x > 0.999, 10, 1);
68         // > numapprox[minimax](alpha * (x^2)^gamma_ - (alpha - 1), x=sqrt(beta)..1, [4,0], w(x^2), 'maxerror');
69         //
70         // Since the error here is possible to interpret on a uniform scale,
71         // we also show it as a value relative to a 8-, 10- or 12-bit pixel level,
72         // as appropriate.
74         if (destination_curve == GAMMA_sRGB) {
75                 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
76                 // β = 0.0031308, ɣ = 1/2.4.
77                 // maxerror      = 0.000785 = 0.200 * 255
78                 // error at 1.0  = 0.000078 = 0.020 * 255
79                 uniform_linear_scale = 12.92;
80                 uniform_c = -0.03679675939;
81                 uniform_c = 1.443803073;
82                 uniform_c = -0.9239780987;
83                 uniform_c = 0.8060491596;
84                 uniform_c = -0.2891558568;
85                 uniform_beta = 0.0031308;
86         }
87         if (destination_curve == GAMMA_REC_709) {  // Also includes Rec. 601, and 10-bit Rec. 2020.
88                 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018, ɣ = 0.45.
89                 // maxerror      = 0.000131 = 0.033 * 255 = 0.134 * 1023
90                 // error at 1.0  = 0.000013 = 0.003 * 255 = 0.013 * 1023
91                 uniform_linear_scale = 4.5;
92                 uniform_c = -0.08541688528;
93                 uniform_c = 1.292793370;
94                 uniform_c = -0.4070417645;
95                 uniform_c = 0.2923891828;
96                 uniform_c = -0.09273699351;
97                 uniform_beta = 0.018;
98         }
99         if (destination_curve == GAMMA_REC_2020_12_BIT) {
100                 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181, ɣ = 0.45.
101                 // maxerror      = 0.000130 = 0.533 * 4095
102                 // error at 1.0  = 0.000013 = 0.053 * 4095
103                 //
104                 // Note that this error is above one half of a pixel level,
105                 // which means that a few values will actually be off in the lowest
106                 // bit. (Removing the constraint for x=1 will only take this down
107                 // from 0.553 to 0.501; adding a fifth order can get it down to
108                 // 0.167, although this assumes working in fp64 and not fp32.)
109                 uniform_linear_scale = 4.5;
110                 uniform_c = -0.08569685663;
111                 uniform_c = 1.293000900;
112                 uniform_c = -0.4067291321;
113                 uniform_c = 0.2919741179;
114                 uniform_c = -0.09256205770;
115                 uniform_beta = 0.0181;
116         }
117 }
119 }  // namespace movit
{"ft_lang_label":"__label__en","ft_lang_prob":0.6250978,"math_prob":0.8829751,"size":4350,"snap":"2019-43-2019-47","text_gpt3_token_len":1400,"char_repetition_ratio":0.13092498,"word_repetition_ratio":0.035612535,"special_character_ratio":0.44367817,"punctuation_ratio":0.206621,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99800736,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-23T12:27:57Z\",\"WARC-Record-ID\":\"<urn:uuid:77cc5698-7884-4053-96bf-72f804be8cb5>\",\"Content-Length\":\"41404\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a4b64aae-c8f5-4ffb-b740-331fbe947b00>\",\"WARC-Concurrent-To\":\"<urn:uuid:327de01d-8752-4848-9203-7024c720932b>\",\"WARC-IP-Address\":\"193.35.52.50\",\"WARC-Target-URI\":\"https://git.sesse.net/?p=movit;a=blob;f=gamma_compression_effect.cpp;h=83489be5dbf669bcf692e3e497394b58ee22655b;hb=refs/heads/1.3.x-release\",\"WARC-Payload-Digest\":\"sha1:TMY2JANL3FJ7YDBKARZRXUQ6SJ4XUI4F\",\"WARC-Block-Digest\":\"sha1:HE6MR3QS2TIDOTFEQA7756RHTMZRXW5B\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570987833766.94_warc_CC-MAIN-20191023122219-20191023145719-00343.warc.gz\"}"}