]> git.sesse.net Git - movit/blob - resample_effect.h
Fix some invalid code in DeinterlaceTest; GCC 7 would optimize it into simply an...
[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 <string>
22
23 #include "effect.h"
24 #include "fp16.h"
25
26 namespace movit {
27
28 class EffectChain;
29 class Node;
30 class SingleResamplePassEffect;
31
32 // Public so that it can be benchmarked externally.
33 template<class T>
34 struct Tap {
35         T weight;
36         T pos;
37 };
38 struct ScalingWeights {
39         unsigned src_bilinear_samples;
40         unsigned dst_samples, num_loops;
41
42         // Exactly one of these is set.
43         Tap<fp16_int_t> *bilinear_weights_fp16;
44         Tap<float> *bilinear_weights_fp32;
45 };
46 ScalingWeights calculate_scaling_weights(unsigned src_size, unsigned dst_size, float zoom, float offset);
47
48 class ResampleEffect : public Effect {
49 public:
50         ResampleEffect();
51         ~ResampleEffect();
52
53         virtual std::string effect_type_id() const { return "ResampleEffect"; }
54
55         // We want this for the same reason as ResizeEffect; we could end up scaling
56         // down quite a lot.
57         virtual bool needs_texture_bounce() const { return true; }
58         virtual bool needs_srgb_primaries() const { return false; }
59
60         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
61
62         virtual std::string output_fragment_shader() {
63                 assert(false);
64         }
65         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
66                 assert(false);
67         }
68
69         virtual void rewrite_graph(EffectChain *graph, Node *self);
70         virtual bool set_float(const std::string &key, float value);
71         
72 private:
73         void update_size();
74         void update_offset_and_zoom();
75         
76         // Both of these are owned by us if owns_effects is true (before finalize()),
77         // and otherwise owned by the EffectChain.
78         bool owns_effects;
79         SingleResamplePassEffect *hpass, *vpass;
80         int input_width, input_height, output_width, output_height;
81
82         float offset_x, offset_y;
83         float zoom_x, zoom_y;
84         float zoom_center_x, zoom_center_y;
85         float unused;
86 };
87
88 class SingleResamplePassEffect : public Effect {
89 public:
90         // If parent is non-NULL, calls to inform_input_size will be forwarded,
91         // so that it can inform both passes about the right input and output
92         // resolutions.
93         SingleResamplePassEffect(ResampleEffect *parent);
94         ~SingleResamplePassEffect();
95         virtual std::string effect_type_id() const { return "SingleResamplePassEffect"; }
96
97         std::string output_fragment_shader();
98
99         virtual bool needs_texture_bounce() const { return true; }
100         virtual bool needs_srgb_primaries() const { return false; }
101         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
102
103         virtual void inform_added(EffectChain *chain) { this->chain = chain; }
104         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
105                 if (parent != NULL) {
106                         parent->inform_input_size(input_num, width, height);
107                 }
108         }
109         virtual bool changes_output_size() const { return true; }
110         virtual bool sets_virtual_output_size() const { return false; }
111
112         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
113                 *virtual_width = *width = this->output_width;
114                 *virtual_height = *height = this->output_height;
115         }
116
117         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
118         
119         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
120
121 private:
122         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
123
124         ResampleEffect *parent;
125         EffectChain *chain;
126         Direction direction;
127         GLuint texnum;
128         GLint uniform_sample_tex;
129         float uniform_num_loops, uniform_slice_height, uniform_sample_x_scale, uniform_sample_x_offset;
130         float uniform_whole_pixel_offset;
131         int uniform_num_samples;
132
133         int input_width, input_height, output_width, output_height;
134         float offset, zoom;
135         float unused;
136         int last_input_width, last_input_height, last_output_width, last_output_height;
137         float last_offset, last_zoom;
138         int src_bilinear_samples, num_loops;
139         float slice_height;
140         int last_texture_width, last_texture_height;
141         GLuint last_texture_internal_format;
142 };
143
144 }  // namespace movit
145
146 #endif // !defined(_MOVIT_RESAMPLE_EFFECT_H)