// Three-lobed Lanczos, the most common choice.
// Note that if you change this, the accuracy for LANCZOS_TABLE_SIZE
// needs to be recomputed.
-#define LANCZOS_RADIUS 3.0
+#define LANCZOS_RADIUS 3.0f
#include <epoxy/gl.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
+#include <mutex>
#include <Eigen/Sparse>
#include <Eigen/SparseQR>
#include <Eigen/OrderingMethods>
// You need to call lanczos_table_init_done before the first call to
// lanczos_weight_cached.
#define LANCZOS_TABLE_SIZE 2048
-bool lanczos_table_init_done = false;
+static once_flag lanczos_table_init_done;
float lanczos_table[LANCZOS_TABLE_SIZE + 2];
void init_lanczos_table()
for (unsigned i = 0; i < LANCZOS_TABLE_SIZE + 2; ++i) {
lanczos_table[i] = lanczos_weight(float(i) * (LANCZOS_RADIUS / LANCZOS_TABLE_SIZE));
}
- lanczos_table_init_done = true;
}
float lanczos_weight_cached(float x)
return 0.0f;
}
float table_pos = x * (LANCZOS_TABLE_SIZE / LANCZOS_RADIUS);
- int table_pos_int = int(table_pos); // Truncate towards zero.
+ unsigned table_pos_int = int(table_pos); // Truncate towards zero.
float table_pos_frac = table_pos - table_pos_int;
assert(table_pos < LANCZOS_TABLE_SIZE + 2);
return lanczos_table[table_pos_int] +
}
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;
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);
}
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,
}
// OK, we can combine this and the next sample.
- if (dst != NULL) {
+ if (dst != nullptr) {
dst[j].weight = total_weight;
dst[j].pos = pos;
}
{
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);
}
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);
}
// Find the effective range of the bilinear-optimized kernel.
// Due to rounding of the positions, this is not necessarily the same
// as the intended range (ie., the range of the original weights).
- int lower_pos = int(floor(to_fp32(bilinear_weights[0].pos) * size - 0.5));
- int upper_pos = int(ceil(to_fp32(bilinear_weights[num_bilinear_weights - 1].pos) * size - 0.5)) + 2;
- lower_pos = min<int>(lower_pos, lrintf(weights[0].pos * size - 0.5));
- upper_pos = max<int>(upper_pos, lrintf(weights[num_weights - 1].pos * size - 0.5) + 1);
+ int lower_pos = int(floor(to_fp32(bilinear_weights[0].pos) * size - 0.5f));
+ int upper_pos = int(ceil(to_fp32(bilinear_weights[num_bilinear_weights - 1].pos) * size - 0.5f)) + 2;
+ lower_pos = min<int>(lower_pos, lrintf(weights[0].pos * size - 0.5f));
+ upper_pos = max<int>(upper_pos, lrintf(weights[num_weights - 1].pos * size - 0.5f) + 1);
float* effective_weights = new float[upper_pos - lower_pos];
for (int i = 0; i < upper_pos - lower_pos; ++i) {
assert(x0 < upper_pos - lower_pos);
assert(x1 < upper_pos - lower_pos);
- effective_weights[x0] += to_fp32(bilinear_weights[i].weight) * (1.0 - f);
+ effective_weights[x0] += to_fp32(bilinear_weights[i].weight) * (1.0f - f);
effective_weights[x1] += to_fp32(bilinear_weights[i].weight) * f;
}
} // 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),
// 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);
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,
glGenTextures(1, &texnum);
- if (!lanczos_table_init_done) {
- // Could in theory race between two threads if we are unlucky,
- // but that is harmless, since they'll write the same data.
- init_lanczos_table();
- }
+ call_once(lanczos_table_init_done, init_lanczos_table);
}
SingleResamplePassEffect::~SingleResamplePassEffect()
ScalingWeights calculate_scaling_weights(unsigned src_size, unsigned dst_size, float zoom, float offset)
{
- if (!lanczos_table_init_done) {
- // Only needed if run from outside ResampleEffect.
- init_lanczos_table();
- }
+ // Only needed if run from outside ResampleEffect.
+ call_once(lanczos_table_init_done, init_lanczos_table);
// For many resamplings (e.g. 640 -> 1280), we will end up with the same
// set of samples over and over again in a loop. Thus, we can compute only
int base_src_y = lrintf(center_src_y);
// Now sample <int_radius> pixels on each side around that point.
+ float inv_src_size = 1.0 / float(src_size);
for (int i = 0; i < src_samples; ++i) {
int src_y = base_src_y + i - int_radius;
float weight = lanczos_weight_cached(radius_scaling_factor * (src_y - center_src_y - subpixel_offset));
weights[y * src_samples + i].weight = weight * radius_scaling_factor;
- weights[y * src_samples + i].pos = (src_y + 0.5) / float(src_size);
+ weights[y * src_samples + i].pos = (src_y + 0.5f) * inv_src_size;
}
}
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(