]> git.sesse.net Git - movit/blob - blur_effect.cpp
Release Movit 1.6.1.
[movit] / blur_effect.cpp
1 #include <epoxy/gl.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <algorithm>
5
6 #include "blur_effect.h"
7 #include "effect_chain.h"
8 #include "effect_util.h"
9 #include "init.h"
10 #include "util.h"
11
12 using namespace std;
13
14 namespace movit {
15         
16 BlurEffect::BlurEffect()
17         : num_taps(16),
18           radius(3.0f),
19           input_width(1280),
20           input_height(720)
21 {
22         // The first blur pass will forward resolution information to us.
23         hpass = new SingleBlurPassEffect(this);
24         CHECK(hpass->set_int("direction", SingleBlurPassEffect::HORIZONTAL));
25         vpass = new SingleBlurPassEffect(nullptr);
26         CHECK(vpass->set_int("direction", SingleBlurPassEffect::VERTICAL));
27
28         update_radius();
29 }
30
31 void BlurEffect::rewrite_graph(EffectChain *graph, Node *self)
32 {
33         Node *hpass_node = graph->add_node(hpass);
34         Node *vpass_node = graph->add_node(vpass);
35         graph->connect_nodes(hpass_node, vpass_node);
36         graph->replace_receiver(self, hpass_node);
37         graph->replace_sender(self, vpass_node);
38         self->disabled = true;
39
40
41 // We get this information forwarded from the first blur pass,
42 // since we are not part of the chain ourselves.
43 void BlurEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
44 {
45         assert(input_num == 0);
46         assert(width != 0);
47         assert(height != 0);
48         input_width = width;
49         input_height = height;
50         update_radius();
51 }
52                 
53 void BlurEffect::update_radius()
54 {
55         // We only have 16 taps to work with on each side, and we want that to
56         // reach out to about 2.5*sigma. Bump up the mipmap levels (giving us
57         // box blurs) until we have what we need.
58         unsigned mipmap_width = input_width, mipmap_height = input_height;
59         float adjusted_radius = radius;
60         while ((mipmap_width > 1 || mipmap_height > 1) && adjusted_radius * 1.5f > num_taps / 2) {
61                 // Find the next mipmap size (round down, minimum 1 pixel).
62                 mipmap_width = max(mipmap_width / 2, 1u);
63                 mipmap_height = max(mipmap_height / 2, 1u);
64
65                 // Approximate when mipmap sizes are odd, but good enough.
66                 adjusted_radius = radius * float(mipmap_width) / float(input_width);
67         }
68         
69         bool ok = hpass->set_float("radius", adjusted_radius);
70         ok |= hpass->set_int("width", mipmap_width);
71         ok |= hpass->set_int("height", mipmap_height);
72         ok |= hpass->set_int("virtual_width", mipmap_width);
73         ok |= hpass->set_int("virtual_height", mipmap_height);
74         ok |= hpass->set_int("num_taps", num_taps);
75
76         ok |= vpass->set_float("radius", adjusted_radius);
77         ok |= vpass->set_int("width", mipmap_width);
78         ok |= vpass->set_int("height", mipmap_height);
79         ok |= vpass->set_int("virtual_width", input_width);
80         ok |= vpass->set_int("virtual_height", input_height);
81         ok |= vpass->set_int("num_taps", num_taps);
82
83         assert(ok);
84 }
85
86 bool BlurEffect::set_float(const string &key, float value) {
87         if (key == "radius") {
88                 radius = value;
89                 update_radius();
90                 return true;
91         }
92         return false;
93 }
94
95 bool BlurEffect::set_int(const string &key, int value) {
96         if (key == "num_taps") {
97                 if (value < 2 || value % 2 != 0) {
98                         return false;
99                 }
100                 num_taps = value;
101                 update_radius();
102                 return true;
103         }
104         return false;
105 }
106
107 SingleBlurPassEffect::SingleBlurPassEffect(BlurEffect *parent)
108         : parent(parent),
109           num_taps(16),
110           radius(3.0f),
111           direction(HORIZONTAL),
112           width(1280),
113           height(720),
114           uniform_samples(nullptr)
115 {
116         register_float("radius", &radius);
117         register_int("direction", (int *)&direction);
118         register_int("width", &width);
119         register_int("height", &height);
120         register_int("virtual_width", &virtual_width);
121         register_int("virtual_height", &virtual_height);
122         register_int("num_taps", &num_taps);
123 }
124
125 SingleBlurPassEffect::~SingleBlurPassEffect()
126 {
127         delete[] uniform_samples;
128 }
129
130 string SingleBlurPassEffect::output_fragment_shader()
131 {
132         char buf[256];
133         sprintf(buf, "#define DIRECTION_VERTICAL %d\n#define NUM_TAPS %d\n",
134                 (direction == VERTICAL), num_taps);
135         uniform_samples = new float[2 * (num_taps / 2 + 1)];
136         register_uniform_vec2_array("samples", uniform_samples, num_taps / 2 + 1);
137         return buf + read_file("blur_effect.frag");
138 }
139
140 void SingleBlurPassEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
141 {
142         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
143
144         // Compute the weights; they will be symmetrical, so we only compute
145         // the right side.
146         float* weight = new float[num_taps + 1];
147         if (radius < 1e-3) {
148                 weight[0] = 1.0f;
149                 for (int i = 1; i < num_taps + 1; ++i) {
150                         weight[i] = 0.0f;
151                 }
152         } else {
153                 float sum = 0.0f;
154                 for (int i = 0; i < num_taps + 1; ++i) {
155                         // Gaussian blur is a common, but maybe not the prettiest choice;
156                         // it can feel a bit too blurry in the fine detail and too little
157                         // long-tail. This is a simple logistic distribution, which has
158                         // a narrower peak but longer tails.
159                         //
160                         // We interpret the radius as sigma, similar to Gaussian blur.
161                         // Wikipedia says that sigma² = pi² s² / 3, which yields:
162                         const float s = (sqrt(3.0) / M_PI) * radius;
163                         float z = i / (2.0 * s);
164
165                         weight[i] = 1.0f / (cosh(z) * cosh(z));
166
167                         if (i == 0) {
168                                 sum += weight[i];
169                         } else {
170                                 sum += 2.0f * weight[i];
171                         }
172                 }
173                 for (int i = 0; i < num_taps + 1; ++i) {
174                         weight[i] /= sum;
175                 }
176         }
177
178         // Since the GPU gives us bilinear sampling for free, we can get two
179         // samples for the price of one (for every but the center sample,
180         // in which case this trick doesn't buy us anything). Simply sample
181         // between the two pixel centers, and we can do with fewer weights.
182         // (This is right even in the vertical pass where we don't actually
183         // sample between the pixels, because we have linear interpolation
184         // there too.)
185         //
186         // We pack the parameters into a float4: The relative sample coordinates
187         // in (x,y), and the weight in z. w is unused.
188
189         // Center sample.
190         uniform_samples[2 * 0 + 0] = 0.0f;
191         uniform_samples[2 * 0 + 1] = weight[0];
192
193         int size;
194         if (direction == HORIZONTAL) {
195                 size = width;
196         } else if (direction == VERTICAL) {
197                 size = height;
198         } else {
199                 assert(false);
200         }
201         float num_subtexels = size / movit_texel_subpixel_precision;
202         float inv_num_subtexels = movit_texel_subpixel_precision / size;
203
204         // All other samples.
205         for (int i = 1; i < num_taps / 2 + 1; ++i) {
206                 unsigned base_pos = i * 2 - 1;
207                 float w1 = weight[base_pos];
208                 float w2 = weight[base_pos + 1];
209
210                 float pos1 = base_pos / (float)size;
211                 float pos, total_weight;
212                 combine_two_samples(w1, w2, pos1, 1.0 / (float)size, size, num_subtexels, inv_num_subtexels, &pos, &total_weight, nullptr);
213
214                 uniform_samples[2 * i + 0] = pos;
215                 uniform_samples[2 * i + 1] = total_weight;
216         }
217
218         delete[] weight;
219 }
220
221 void SingleBlurPassEffect::clear_gl_state()
222 {
223 }
224
225 }  // namespace movit