]> git.sesse.net Git - movit/blob - resample_effect.cpp
Add an effect for Lanczos resampling.
[movit] / resample_effect.cpp
1 // Three-lobed Lanczos, the most common choice.
2 #define LANCZOS_RADIUS 3.0
3
4 #include <math.h>
5 #include <assert.h>
6
7 #include "resample_effect.h"
8 #include "effect_chain.h"
9 #include "util.h"
10 #include "opengl.h"
11
12 namespace {
13
14 float sinc(float x)
15 {
16         if (fabs(x) < 1e-6) {
17                 return 1.0f - fabs(x);
18         } else {
19                 return sin(x) / x;
20         }
21 }
22
23 float lanczos_weight(float x, float a)
24 {
25         if (fabs(x) > a) {
26                 return 0.0f;
27         } else {
28                 return sinc(M_PI * x) * sinc(M_PI * x / a);
29         }
30 }
31
32 }  // namespace
33
34 ResampleEffect::ResampleEffect()
35         : input_width(1280),
36           input_height(720)
37 {
38         register_int("width", &output_width);
39         register_int("height", &output_height);
40
41         // The first blur pass will forward resolution information to us.
42         hpass = new SingleResamplePassEffect(this);
43         hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL);
44         vpass = new SingleResamplePassEffect(NULL);
45         vpass->set_int("direction", SingleResamplePassEffect::VERTICAL);
46
47         update_size();
48 }
49
50 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
51 {
52         Node *hpass_node = graph->add_node(hpass);
53         Node *vpass_node = graph->add_node(vpass);
54         graph->connect_nodes(hpass_node, vpass_node);
55         graph->replace_receiver(self, hpass_node);
56         graph->replace_sender(self, vpass_node);
57         self->disabled = true;
58
59
60 // We get this information forwarded from the first blur pass,
61 // since we are not part of the chain ourselves.
62 void ResampleEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
63 {
64         assert(input_num == 0);
65         assert(width != 0);
66         assert(height != 0);
67         input_width = width;
68         input_height = height;
69         update_size();
70 }
71                 
72 void ResampleEffect::update_size()
73 {
74         bool ok = true;
75         ok |= hpass->set_int("input_width", input_width);
76         ok |= hpass->set_int("input_height", input_height);
77         ok |= hpass->set_int("output_width", output_width);
78         ok |= hpass->set_int("output_height", input_height);
79
80         ok |= vpass->set_int("input_width", output_width);
81         ok |= vpass->set_int("input_height", input_height);
82         ok |= vpass->set_int("output_width", output_width);
83         ok |= vpass->set_int("output_height", output_height);
84
85         assert(ok);
86 }
87
88 bool ResampleEffect::set_float(const std::string &key, float value) {
89         if (key == "width") {
90                 output_width = value;
91                 update_size();
92                 return true;
93         }
94         if (key == "height") {
95                 output_height = value;
96                 update_size();
97                 return true;
98         }
99         return false;
100 }
101
102 SingleResamplePassEffect::SingleResamplePassEffect(ResampleEffect *parent)
103         : parent(parent),
104           direction(HORIZONTAL),
105           input_width(1280),
106           input_height(720),
107           last_input_width(-1),
108           last_input_height(-1),
109           last_output_width(-1),
110           last_output_height(-1)
111 {
112         register_int("direction", (int *)&direction);
113         register_int("input_width", &input_width);
114         register_int("input_height", &input_height);
115         register_int("output_width", &output_width);
116         register_int("output_height", &output_height);
117
118         glGenTextures(1, &texnum);
119 }
120
121 SingleResamplePassEffect::~SingleResamplePassEffect()
122 {
123         glDeleteTextures(1, &texnum);
124 }
125
126 std::string SingleResamplePassEffect::output_fragment_shader()
127 {
128         char buf[256];
129         sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
130         return buf + read_file("resample_effect.frag");
131 }
132
133 // Using vertical scaling as an example:
134 //
135 // Generally out[y] = w0 * in[yi] + w1 * in[yi + 1] + w2 * in[yi + 2] + ...
136 //
137 // Obviously, yi will depend on y (in a not-quite-linear way), but so will
138 // the weights w0, w1, w2, etc.. The easiest way of doing this is to encode,
139 // for each sample, the weight and the yi value, e.g. <yi, w0>, <yi + 1, w1>,
140 // and so on. For each y, we encode these along the x-axis (since that is spare),
141 // so out[0] will read from parameters <x,y> = <0,0>, <1,0>, <2,0> and so on.
142 //
143 // For horizontal scaling, we fill in the exact same texture;
144 // the shader just interprets is differently.
145 //
146 // TODO: Support optimization of wrapping the sample texture.
147 // TODO: Support optimization using free linear sampling, like in BlurEffect.
148 void SingleResamplePassEffect::update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
149 {
150         unsigned src_size, dst_size;
151         if (direction == SingleResamplePassEffect::HORIZONTAL) {
152                 assert(input_height == output_height);
153                 src_size = input_width;
154                 dst_size = output_width;
155         } else if (direction == SingleResamplePassEffect::VERTICAL) {
156                 assert(input_width == output_width);
157                 src_size = input_height;
158                 dst_size = output_height;
159         } else {
160                 assert(false);
161         }
162
163         // Sample the kernel in the right place. A diagram with a triangular kernel
164         // (corresponding to linear filtering, and obviously with radius 1)
165         // for easier ASCII art drawing:
166         //
167         //                *
168         //               / \                      |
169         //              /   \                     |
170         //             /     \                    |
171         //    x---x---x   x   x---x---x---x
172         //
173         // Scaling up (in this case, 2x) means sampling more densely:
174         //
175         //                *
176         //               / \                      |
177         //              /   \                     |
178         //             /     \                    |
179         //   x-x-x-x-x-x x x x-x-x-x-x-x-x-x
180         //
181         // When scaling up, any destination pixel will only be influenced by a few
182         // (in this case, two) neighboring pixels, and more importantly, the number
183         // will not be influenced by the scaling factor. (Note, however, that the
184         // pixel centers have moved, due to OpenGL's center-pixel convention.)
185         // The only thing that changes is the weights themselves, as the sampling
186         // points are at different distances from the original pixels.
187         //
188         // Scaling down is a different story:
189         //
190         //                *
191         //               / \                      |
192         //              /   \                     |
193         //             /     \                    |
194         //    --x------ x     --x-------x--
195         //
196         // Again, the pixel centers have moved in a maybe unintuitive fashion,
197         // although when you consider that there are multiple source pixels around,
198         // it's not so bad as at first look:
199         //
200         //            *   *   *   *
201         //           / \ / \ / \ / \              |
202         //          /   X   X   X   \             |
203         //         /   / \ / \ / \   \            |
204         //    --x-------x-------x-------x--
205         //
206         // As you can see, the new pixels become averages of the two neighboring old
207         // ones (the situation for Lanczos is of course more complex).
208         //
209         // Anyhow, in this case we clearly need to look at more source pixels
210         // to compute the destination pixel, and how many depend on the scaling factor.
211         // Thus, the kernel width will vary with how much we scale.
212         float radius_scaling_factor = std::min(float(dst_size) / float(src_size), 1.0f);
213         int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
214         src_samples = int_radius * 2 + 1;
215         float *weights = new float[dst_size * src_samples * 2];
216         for (unsigned y = 0; y < dst_size; ++y) {
217                 // Find the point around which we want to sample the source image,
218                 // compensating for differing pixel centers as the scale changes.
219                 float center_src_y = (y + 0.5f) * float(src_size) / float(dst_size) - 0.5f;
220                 int base_src_y = lrintf(center_src_y);
221
222                 // Now sample <int_radius> pixels on each side around that point.
223                 for (int i = 0; i < src_samples; ++i) {
224                         int src_y = base_src_y + i - int_radius;
225                         float weight = lanczos_weight(radius_scaling_factor * (src_y - center_src_y), LANCZOS_RADIUS);
226                         weights[(y * src_samples + i) * 2 + 0] = weight * radius_scaling_factor;
227                         weights[(y * src_samples + i) * 2 + 1] = (src_y + 0.5) / float(src_size);
228                 }
229         }
230
231         // Encode as a two-component texture. Note the GL_REPEAT, which is not relevant
232         // right now, but will be later.
233         glActiveTexture(GL_TEXTURE0 + *sampler_num);
234         check_error();
235         glBindTexture(GL_TEXTURE_2D, texnum);
236         check_error();
237         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
238         check_error();
239         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
240         check_error();
241         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
242         check_error();
243         glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, src_samples, dst_size, 0, GL_RG, GL_FLOAT, weights);
244         check_error();
245
246         delete[] weights;
247
248 }
249
250 void SingleResamplePassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
251 {
252         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
253
254         if (input_width != last_input_width ||
255             input_height != last_input_height ||
256             output_width != last_output_width ||
257             output_height != last_output_height) {
258                 update_texture(glsl_program_num, prefix, sampler_num);
259                 last_input_width = input_width;
260                 last_input_height = input_height;
261                 last_output_width = output_width;
262                 last_output_height = output_height;
263         }
264
265         glActiveTexture(GL_TEXTURE0 + *sampler_num);
266         check_error();
267         glBindTexture(GL_TEXTURE_2D, texnum);
268         check_error();
269
270         set_uniform_int(glsl_program_num, prefix, "sample_tex", *sampler_num);
271         ++sampler_num;
272         set_uniform_int(glsl_program_num, prefix, "num_samples", src_samples);
273
274         // Instructions for how to convert integer sample numbers to positions in the weight texture.
275         set_uniform_float(glsl_program_num, prefix, "sample_x_scale", 1.0f / src_samples);
276         set_uniform_float(glsl_program_num, prefix, "sample_x_offset", 0.5f / src_samples);
277
278         // We specifically do not want mipmaps on the input texture;
279         // they break minification.
280         glActiveTexture(GL_TEXTURE0);
281         check_error();
282         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
283         check_error();
284 }