]> git.sesse.net Git - movit/blobdiff - resample_effect.cpp
Bump compute shader header to #version 150.
[movit] / resample_effect.cpp
index ba8a71c0241443c0730a5ce99d68a2fa7c014ad3..4598d34e7bb61b44d91acea41648345a059f6757 100644 (file)
@@ -109,7 +109,7 @@ unsigned gcd(unsigned a, unsigned b)
 }
 
 template<class DestFloat>
-unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_subtexels, float inv_num_subtexels, unsigned num_src_samples, unsigned max_samples_saved)
+unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_subtexels, float inv_num_subtexels, unsigned num_src_samples, unsigned max_samples_saved, float pos1_pos2_diff, float inv_pos1_pos2_diff)
 {
        // Cut off near-zero values at both sides.
        unsigned num_samples_saved = 0;
@@ -129,7 +129,7 @@ unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_s
 
        for (unsigned i = 0, j = 0; i < num_src_samples; ++i, ++j) {
                // Copy the sample directly; it will be overwritten later if we can combine.
-               if (dst != NULL) {
+               if (dst != nullptr) {
                        dst[j].weight = convert_float<float, DestFloat>(src[i].weight);
                        dst[j].pos = convert_float<float, DestFloat>(src[i].pos);
                }
@@ -157,7 +157,7 @@ unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_s
 
                DestFloat pos, total_weight;
                float sum_sq_error;
-               combine_two_samples(w1, w2, pos1, pos2, num_subtexels, inv_num_subtexels, &pos, &total_weight, &sum_sq_error);
+               combine_two_samples(w1, w2, pos1, pos1_pos2_diff, inv_pos1_pos2_diff, num_subtexels, inv_num_subtexels, &pos, &total_weight, &sum_sq_error);
 
                // If the interpolation error is larger than that of about sqrt(2) of
                // a level at 8-bit precision, don't combine. (You'd think 1.0 was enough,
@@ -169,7 +169,7 @@ unsigned combine_samples(const Tap<float> *src, Tap<DestFloat> *dst, float num_s
                }
 
                // OK, we can combine this and the next sample.
-               if (dst != NULL) {
+               if (dst != nullptr) {
                        dst[j].weight = total_weight;
                        dst[j].pos = pos;
                }
@@ -210,10 +210,12 @@ unsigned combine_many_samples(const Tap<float> *weights, unsigned src_size, unsi
 {
        float num_subtexels = src_size / movit_texel_subpixel_precision;
        float inv_num_subtexels = movit_texel_subpixel_precision / src_size;
+       float pos1_pos2_diff = 1.0f / src_size;
+       float inv_pos1_pos2_diff = src_size;
 
        unsigned max_samples_saved = UINT_MAX;
        for (unsigned y = 0; y < dst_samples && max_samples_saved > 0; ++y) {
-               unsigned num_samples_saved = combine_samples<DestFloat>(weights + y * src_samples, NULL, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved);
+               unsigned num_samples_saved = combine_samples<DestFloat>(weights + y * src_samples, nullptr, num_subtexels, inv_num_subtexels, src_samples, max_samples_saved, pos1_pos2_diff, inv_pos1_pos2_diff);
                max_samples_saved = min(max_samples_saved, num_samples_saved);
        }
 
@@ -228,7 +230,9 @@ unsigned combine_many_samples(const Tap<float> *weights, unsigned src_size, unsi
                        num_subtexels,
                        inv_num_subtexels,
                        src_samples,
-                       max_samples_saved);
+                       max_samples_saved,
+                       pos1_pos2_diff,
+                       inv_pos1_pos2_diff);
                assert(num_samples_saved == max_samples_saved);
                normalize_sum(bilinear_weights_ptr, src_bilinear_samples);
        }
@@ -296,7 +300,8 @@ double compute_sum_sq_error(const Tap<float>* weights, unsigned num_weights,
 }  // namespace
 
 ResampleEffect::ResampleEffect()
-       : input_width(1280),
+       : owns_effects(true),
+         input_width(1280),
          input_height(720),
          offset_x(0.0f), offset_y(0.0f),
          zoom_x(1.0f), zoom_y(1.0f),
@@ -308,12 +313,20 @@ ResampleEffect::ResampleEffect()
        // The first blur pass will forward resolution information to us.
        hpass = new SingleResamplePassEffect(this);
        CHECK(hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL));
-       vpass = new SingleResamplePassEffect(NULL);
+       vpass = new SingleResamplePassEffect(nullptr);
        CHECK(vpass->set_int("direction", SingleResamplePassEffect::VERTICAL));
 
        update_size();
 }
 
+ResampleEffect::~ResampleEffect()
+{
+       if (owns_effects) {
+               delete hpass;
+               delete vpass;
+       }
+}
+
 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
 {
        Node *hpass_node = graph->add_node(hpass);
@@ -322,6 +335,7 @@ void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
        graph->replace_receiver(self, hpass_node);
        graph->replace_sender(self, vpass_node);
        self->disabled = true;
+       owns_effects = false;
 } 
 
 // We get this information forwarded from the first blur pass,
@@ -654,7 +668,7 @@ ScalingWeights calculate_scaling_weights(unsigned src_size, unsigned dst_size, f
        const float max_error = 2.0f / (255.0f * 255.0f);
        unique_ptr<Tap<fp16_int_t>[]> bilinear_weights_fp16;
        int src_bilinear_samples = combine_many_samples(weights.get(), src_size, src_samples, dst_samples, &bilinear_weights_fp16);
-       unique_ptr<Tap<float>[]> bilinear_weights_fp32 = NULL;
+       unique_ptr<Tap<float>[]> bilinear_weights_fp32 = nullptr;
        double max_sum_sq_error_fp16 = 0.0;
        for (unsigned y = 0; y < dst_samples; ++y) {
                double sum_sq_error_fp16 = compute_sum_sq_error(