1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
#include "Halide.h"
using namespace Halide;
enum InterpolationType {
Box,
Linear,
Cubic,
Lanczos
};
Expr kernel_box(Expr x) {
Expr xx = abs(x);
return select(xx <= 0.5f, 1.0f, 0.0f);
}
Expr kernel_linear(Expr x) {
Expr xx = abs(x);
return select(xx < 1.0f, 1.0f - xx, 0.0f);
}
Expr kernel_cubic(Expr x) {
Expr xx = abs(x);
Expr xx2 = xx * xx;
Expr xx3 = xx2 * xx;
float a = -0.5f;
return select(xx < 1.0f, (a + 2.0f) * xx3 - (a + 3.0f) * xx2 + 1,
select(xx < 2.0f, a * xx3 - 5 * a * xx2 + 8 * a * xx - 4.0f * a,
0.0f));
}
Expr sinc(Expr x) {
x *= 3.14159265359f;
return sin(x) / x;
}
Expr kernel_lanczos(Expr x) {
Expr value = sinc(x) * sinc(x / 3);
value = select(x == 0.0f, 1.0f, value); // Take care of singularity at zero
value = select(x > 3 || x < -3, 0.0f, value); // Clamp to zero out of bounds
return value;
}
struct KernelInfo {
const char *name;
int taps;
Expr (*kernel)(Expr);
};
static KernelInfo kernel_info[] = {
{"box", 1, kernel_box},
{"linear", 2, kernel_linear},
{"cubic", 4, kernel_cubic},
{"lanczos", 6, kernel_lanczos}};
class Resize : public Halide::Generator<Resize> {
public:
GeneratorParam<InterpolationType> interpolation_type{"interpolation_type", Cubic, {{"box", Box}, {"linear", Linear}, {"cubic", Cubic}, {"lanczos", Lanczos}}};
// If we statically know whether we're upsampling or downsampling,
// we can generate different pipelines (we want to reorder the
// resample in x and in y).
GeneratorParam<bool> upsample{"upsample", false};
Input<Buffer<void, 3>> input{"input"};
Input<float> scale_factor{"scale_factor"};
Output<Buffer<void, 3>> output{"output"};
// Common Vars
Var x{"x"}, y{"y"}, c{"c"}, k{"k"};
// Intermediate Funcs
Func as_float{"as_float"},
resized_x{"resized_x"},
resized_y{"resized_y"},
unnormalized_kernel_x{"unnormalized_kernel_x"},
unnormalized_kernel_y{"unnormalized_kernel_y"},
kernel_x{"kernel_x"},
kernel_y{"kernel_y"},
kernel_sum_x{"kernel_sum_x"},
kernel_sum_y{"kernel_sum_y"};
void generate() {
// Handle different types by just casting to float
as_float(x, y, c) = cast<float>(input(x, y, c));
// For downscaling, widen the interpolation kernel to perform lowpass
// filtering.
// Invert the scale factor in a single place and do it
// strictly, to avoid getting different ratios showing up in
// different places.
Expr inverse_scale_factor = strict_float(1.0f / scale_factor);
Expr kernel_scaling = upsample ? Expr(1.0f) : scale_factor;
Expr inverse_kernel_scaling = upsample ? Expr(1.0f) : inverse_scale_factor;
Expr kernel_radius = 0.5f * kernel_info[interpolation_type].taps * inverse_kernel_scaling;
Expr kernel_taps = cast<int>(ceil(kernel_info[interpolation_type].taps * inverse_kernel_scaling));
// source[xy] are the (non-integer) coordinates inside the source image
Expr sourcex = (x + 0.5f) * inverse_scale_factor - 0.5f;
Expr sourcey = (y + 0.5f) * inverse_scale_factor - 0.5f;
// Initialize interpolation kernels. Since we allow an
// arbitrary scaling factor, the filter coefficients are
// different for each x and y coordinate. Use strict-float to
// ensure fast-math doesn't mess up our bounds inference.
Expr beginx = cast<int>(strict_float(ceil(sourcex - kernel_radius)));
Expr beginy = cast<int>(strict_float(ceil(sourcey - kernel_radius)));
beginx = clamp(beginx, input.dim(0).min(), input.dim(0).max() + 1 - kernel_taps);
beginy = clamp(beginy, input.dim(1).min(), input.dim(1).max() + 1 - kernel_taps);
RDom r(0, kernel_taps);
const KernelInfo &info = kernel_info[interpolation_type];
unnormalized_kernel_x(x, k) = info.kernel((k + beginx - sourcex) * kernel_scaling);
unnormalized_kernel_y(y, k) = info.kernel((k + beginy - sourcey) * kernel_scaling);
kernel_sum_x(x) = sum(unnormalized_kernel_x(x, r), "kernel_sum_x");
kernel_sum_y(y) = sum(unnormalized_kernel_y(y, r), "kernel_sum_y");
kernel_x(x, k) = unnormalized_kernel_x(x, k) / kernel_sum_x(x);
kernel_y(y, k) = unnormalized_kernel_y(y, k) / kernel_sum_y(y);
// Perform separable resizing. The resize in x vectorizes
// poorly compared to the resize in y, so do it first if we're
// upsampling, and do it second if we're downsampling.
Func resized;
if (upsample) {
resized_x(x, y, c) = sum(kernel_x(x, r) * as_float(r + beginx, y, c), "resized_x");
resized_y(x, y, c) = sum(kernel_y(y, r) * resized_x(x, r + beginy, c), "resized_y");
resized = resized_y;
} else {
resized_y(x, y, c) = sum(kernel_y(y, r) * as_float(x, r + beginy, c), "resized_y");
resized_x(x, y, c) = sum(kernel_x(x, r) * resized_y(r + beginx, y, c), "resized_x");
resized = resized_x;
}
if (input.type().is_float()) {
output(x, y, c) = clamp(resized(x, y, c), 0.0f, 1.0f);
} else {
output(x, y, c) = saturating_cast(input.type(), resized(x, y, c));
}
}
void schedule() {
const int vec = natural_vector_size<float>();
Var xi("xi"), yi("yi");
unnormalized_kernel_x
.compute_at(kernel_x, x)
.store_in(MemoryType::Stack)
.vectorize(x);
kernel_sum_x
.compute_at(kernel_x, x)
.vectorize(x);
kernel_x
.compute_root()
.reorder(k, x)
.vectorize(x, vec);
unnormalized_kernel_y
.compute_at(kernel_y, y)
.vectorize(y, vec);
kernel_sum_y
.compute_at(kernel_y, y)
.vectorize(y);
kernel_y
.compute_at(output, y)
.reorder(k, y)
.vectorize(y, vec);
if (upsample) {
output
.tile(x, y, xi, yi, 16, 64)
.parallel(y)
.vectorize(xi);
resized_x
.compute_at(output, x)
.hoist_storage(output, y)
.vectorize(x);
resized_y
.compute_at(output, xi)
.unroll(c);
} else {
output
.tile(x, y, xi, yi, 32, 8)
.parallel(y)
.vectorize(xi);
resized_y
.compute_at(output, y)
.vectorize(x, vec);
resized_x
.compute_at(output, xi)
.unroll(c);
}
// Allow the input and output to have arbitrary memory layout,
// and add some specializations for a few common cases. If
// your case is not covered (e.g. planar input, packed rgb
// output), you could add a new specialization here.
output.dim(0).set_stride(Expr());
input.dim(0).set_stride(Expr());
Expr planar = (output.dim(0).stride() == 1 &&
input.dim(0).stride() == 1);
Expr packed_rgb = (output.dim(0).stride() == 3 &&
output.dim(2).stride() == 1 &&
output.dim(2).min() == 0 &&
output.dim(2).extent() == 3 &&
input.dim(0).stride() == 3 &&
input.dim(2).stride() == 1 &&
input.dim(2).min() == 0 &&
input.dim(2).extent() == 3);
Expr packed_rgba = (output.dim(0).stride() == 4 &&
output.dim(2).stride() == 1 &&
output.dim(2).min() == 0 &&
output.dim(2).extent() == 4 &&
input.dim(0).stride() == 4 &&
input.dim(2).stride() == 1 &&
input.dim(2).min() == 0 &&
input.dim(2).extent() == 4);
output.specialize(planar);
output.specialize(packed_rgb)
.reorder(c, xi, yi, x, y)
.unroll(c);
output.specialize(packed_rgba)
.reorder(c, xi, yi, x, y)
.unroll(c);
}
};
HALIDE_REGISTER_GENERATOR(Resize, resize);
|