2 * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef AVFILTER_DESHAKE_OPENCL_KERNEL_H
23 #define AVFILTER_DESHAKE_OPENCL_KERNEL_H
25 #include "libavutil/opencl.h"
27 const char *ff_kernel_deshake_opencl = AV_OPENCL_KERNEL(
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);
40 unsigned char interpolate_bilinear(float x, float y, global const unsigned char *src,
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;
100 inline int mirror(int v, int m)
102 while ((unsigned)v > (unsigned)m) {
110 kernel void avfilter_transform(global unsigned char *src,
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;
157 temp_height = height;
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) {
187 case 1: //FILL_ORIGINAL
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];
195 case 3: //FILL_MIRROR
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) {
202 case 0: //INTERPOLATE_NEAREST
203 tempdst[curpos] = interpolate_nearest(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
205 case 1: //INTERPOLATE_BILINEAR
206 tempdst[curpos] = interpolate_bilinear(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
208 case 2: //INTERPOLATE_BIQUADRATIC
209 tempdst[curpos] = interpolate_biquadratic(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
217 #endif /* AVFILTER_DESHAKE_OPENCL_KERNEL_H */