22 #ifndef AVFILTER_DESHAKE_OPENCL_KERNEL_H
23 #define AVFILTER_DESHAKE_OPENCL_KERNEL_H
29 inline unsigned char pixel(global
const unsigned char *
src,
float x,
float y,
30 int w,
int h,
int stride,
unsigned char def)
32 return (x < 0 || y < 0 || x >= w || y >= h) ? def : src[(
int)x + (
int)y * stride];
34 unsigned char interpolate_nearest(
float x,
float y, global
const unsigned char *src,
35 int width,
int height,
int stride,
unsigned char def)
37 return pixel(src, (
int)(x + 0.5), (
int)(y + 0.5), width, height, stride, def);
41 int width,
int height,
int stride,
unsigned char def)
43 int x_c, x_f, y_c, y_f;
46 if (x < -1 || x > width || y < -1 || y > height) {
55 v1 =
pixel(src, x_c, y_c, width, height, stride, def);
56 v2 =
pixel(src, x_c, y_f, width, height, stride, def);
57 v3 =
pixel(src, x_f, y_c, width, height, stride, def);
58 v4 =
pixel(src, x_f, y_f, width, height, stride, def);
60 return (v1*(x - x_f)*(y - y_f) + v2*((x - x_f)*(y_c - y)) +
61 v3*(x_c - x)*(y - y_f) + v4*((x_c - x)*(y_c - y)));
65 unsigned char interpolate_biquadratic(
float x,
float y, global
const unsigned char *src,
66 int width,
int height,
int stride,
unsigned char def)
68 int x_c, x_f, y_c, y_f;
69 unsigned char v1, v2, v3, v4;
72 if (x < - 1 || x > width || y < -1 || y > height)
80 v1 =
pixel(src, x_c, y_c, width, height, stride, def);
81 v2 =
pixel(src, x_c, y_f, width, height, stride, def);
82 v3 =
pixel(src, x_f, y_c, width, height, stride, def);
83 v4 =
pixel(src, x_f, y_f, width, height, stride, def);
85 f1 = 1 - sqrt((x_c - x) * (y_c - y));
86 f2 = 1 - sqrt((x_c - x) * (y - y_f));
87 f3 = 1 - sqrt((x - x_f) * (y_c - y));
88 f4 = 1 - sqrt((x - x_f) * (y - y_f));
89 return (v1 * f1 + v2 * f2 + v3 * f3 + v4 * f4) / (f1 + f2 + f3 + f4);
93 inline const float clipf(
float a,
float amin,
float amax)
95 if (a < amin)
return amin;
96 else if (a > amax)
return amax;
102 while ((
unsigned)v > (
unsigned)m) {
111 global
unsigned char *dst,
112 global
float *matrix,
113 global
float *matrix2,
125 int global_id = get_global_id(0);
127 global
unsigned char *dst_y = dst;
128 global
unsigned char *dst_u = dst_y + height * dst_stride_lu;
129 global
unsigned char *dst_v = dst_u + ch * dst_stride_ch;
131 global
unsigned char *src_y =
src;
132 global
unsigned char *src_u = src_y + height * src_stride_lu;
133 global
unsigned char *src_v = src_u + ch * src_stride_ch;
135 global
unsigned char *tempdst;
136 global
unsigned char *tempsrc;
147 unsigned char def = 0;
148 if (global_id < width*height) {
151 x_s = x * matrix[0] + y * matrix[1] + matrix[2];
152 y_s = x * matrix[3] + y * matrix[4] + matrix[5];
155 tempsrc_stride = src_stride_lu;
156 tempdst_stride = dst_stride_lu;
159 }
else if ((global_id >= width*height)&&(global_id < width*height + ch*cw)) {
160 y = (global_id - width*
height)/cw;
161 x = (global_id - width*
height)%cw;
162 x_s = x * matrix2[0] + y * matrix2[1] + matrix2[2];
163 y_s = x * matrix2[3] + y * matrix2[4] + matrix2[5];
166 tempsrc_stride = src_stride_ch;
167 tempdst_stride = dst_stride_ch;
171 y = (global_id - width*height - ch*cw)/cw;
172 x = (global_id - width*height - ch*cw)%cw;
173 x_s = x * matrix2[0] + y * matrix2[1] + matrix2[2];
174 y_s = x * matrix2[3] + y * matrix2[4] + matrix2[5];
177 tempsrc_stride = src_stride_ch;
178 tempdst_stride = dst_stride_ch;
182 curpos = y * tempdst_stride + x;
183 switch (fillmethod) {
188 def = tempsrc[y*tempsrc_stride+x];
191 y_s = clipf(y_s, 0, temp_height - 1);
192 x_s = clipf(x_s, 0, temp_width - 1);
193 def = tempsrc[(int)y_s * tempsrc_stride + (
int)x_s];
196 y_s =
mirror(y_s,temp_height - 1);
197 x_s =
mirror(x_s,temp_width - 1);
198 def = tempsrc[(
int)y_s * tempsrc_stride + (
int)x_s];
201 switch (interpolate) {
203 tempdst[curpos] = interpolate_nearest(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
206 tempdst[curpos] =
interpolate_bilinear(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
209 tempdst[curpos] = interpolate_biquadratic(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);