]> git.sesse.net Git - movit/blob - resample_effect.h
Release Movit 1.6.2.
[movit] / resample_effect.h
1 #ifndef _MOVIT_RESAMPLE_EFFECT_H
2 #define _MOVIT_RESAMPLE_EFFECT_H 1
3
4 // High-quality image resizing, either up or down.
5 //
6 // The default scaling offered by the GPU (and as used in ResizeEffect)
7 // is bilinear (optionally mipmapped), which is not the highest-quality
8 // choice, especially for upscaling. ResampleEffect offers the three-lobed
9 // Lanczos kernel, which is among the most popular choices in image
10 // processing. While it does have its weaknesses, in particular a certain
11 // ringing/sharpening effect with artifacts that accumulate over several
12 // consecutive resizings, it is generally regarded as the best tradeoff.
13 //
14 // Works in two passes; first horizontal, then vertical (ResampleEffect,
15 // which is what the user is intended to use, instantiates two copies of
16 // SingleResamplePassEffect behind the scenes).
17
18 #include <epoxy/gl.h>
19 #include <assert.h>
20 #include <stddef.h>
21 #include <memory>
22 #include <string>
23
24 #include "effect.h"
25 #include "fp16.h"
26
27 namespace movit {
28
29 class EffectChain;
30 class Node;
31 class SingleResamplePassEffect;
32
33 // Public so that it can be benchmarked externally.
34 template<class T>
35 struct Tap {
36         T weight;
37         T pos;
38 };
39 struct ScalingWeights {
40         unsigned src_bilinear_samples;
41         unsigned dst_samples, num_loops;
42
43         // Exactly one of these is set.
44         std::unique_ptr<Tap<fp16_int_t>[]> bilinear_weights_fp16;
45         std::unique_ptr<Tap<float>[]> bilinear_weights_fp32;
46 };
47 ScalingWeights calculate_bilinear_scaling_weights(unsigned src_size, unsigned dst_size, float zoom, float offset);
48
49 // A simple manager for support data stored in a 2D texture.
50 // Consider moving it to a shared location of more classes
51 // should need similar functionality.
52 class Support2DTexture {
53 public:
54         Support2DTexture();
55         ~Support2DTexture();
56
57         void update(GLint width, GLint height, GLenum internal_format, GLenum format, GLenum type, const GLvoid * data);
58         GLint get_texnum() const { return texnum; }
59
60 private:
61         GLuint texnum = 0;
62         GLint last_texture_width = -1, last_texture_height = -1;
63         GLenum last_texture_internal_format = GL_INVALID_ENUM;
64 };
65
66 class ResampleEffect : public Effect {
67 public:
68         ResampleEffect();
69         ~ResampleEffect();
70
71         std::string effect_type_id() const override { return "ResampleEffect"; }
72
73         void inform_input_size(unsigned input_num, unsigned width, unsigned height) override;
74
75         std::string output_fragment_shader() override {
76                 assert(false);
77         }
78         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) override {
79                 assert(false);
80         }
81
82         void rewrite_graph(EffectChain *graph, Node *self) override;
83         bool set_float(const std::string &key, float value) override;
84         
85 private:
86         void update_size();
87         void update_offset_and_zoom();
88         
89         // Both of these are owned by us if owns_effects is true (before finalize()),
90         // and otherwise owned by the EffectChain.
91         std::unique_ptr<SingleResamplePassEffect> hpass_owner, vpass_owner;
92         SingleResamplePassEffect *hpass = nullptr, *vpass = nullptr;
93         int input_width, input_height, output_width, output_height;
94
95         float offset_x, offset_y;
96         float zoom_x, zoom_y;
97         float zoom_center_x, zoom_center_y;
98 };
99
100 class SingleResamplePassEffect : public Effect {
101 public:
102         // If parent is non-nullptr, calls to inform_input_size will be forwarded,
103         // so that it can inform both passes about the right input and output
104         // resolutions.
105         SingleResamplePassEffect(ResampleEffect *parent);
106         ~SingleResamplePassEffect();
107         std::string effect_type_id() const override { return "SingleResamplePassEffect"; }
108
109         std::string output_fragment_shader() override;
110
111         bool needs_texture_bounce() const override { return true; }
112         bool needs_srgb_primaries() const override { return false; }
113         AlphaHandling alpha_handling() const override { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
114
115         // We specifically do not want mipmaps on the input texture;
116         // they break minification.
117         MipmapRequirements needs_mipmaps() const override { return CANNOT_ACCEPT_MIPMAPS; }
118
119         void inform_added(EffectChain *chain) override { this->chain = chain; }
120         void inform_input_size(unsigned input_num, unsigned width, unsigned height) override {
121                 if (parent != nullptr) {
122                         parent->inform_input_size(input_num, width, height);
123                 }
124         }
125         bool changes_output_size() const override { return true; }
126         bool sets_virtual_output_size() const override { return false; }
127
128         void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const override {
129                 *virtual_width = *width = this->output_width;
130                 *virtual_height = *height = this->output_height;
131         }
132
133         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) override;
134         
135         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
136
137 private:
138         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
139
140         ResampleEffect *parent;
141         EffectChain *chain;
142         Direction direction;
143         GLint uniform_sample_tex;
144         float uniform_num_loops, uniform_slice_height, uniform_sample_x_scale, uniform_sample_x_offset;
145         float uniform_whole_pixel_offset;
146         int uniform_num_samples;
147
148         int input_width, input_height, output_width, output_height;
149         float offset, zoom;
150         int last_input_width, last_input_height, last_output_width, last_output_height;
151         float last_offset, last_zoom;
152         int src_bilinear_samples, num_loops;
153         float slice_height;
154         Support2DTexture tex;
155 };
156
157 }  // namespace movit
158
159 #endif // !defined(_MOVIT_RESAMPLE_EFFECT_H)