22 #ifndef AVFILTER_UNSHARP_OPENCL_KERNEL_H
23 #define AVFILTER_UNSHARP_OPENCL_KERNEL_H
28 inline unsigned char clip_uint8(
int a)
36 kernel
void unsharp_luma(
37 global
unsigned char *
src,
38 global
unsigned char *dst,
49 int2 threadIdx, blockIdx, globalIdx;
50 threadIdx.x = get_local_id(0);
51 threadIdx.y = get_local_id(1);
52 blockIdx.x = get_group_id(0);
53 blockIdx.y = get_group_id(1);
54 globalIdx.x = get_global_id(0);
55 globalIdx.y = get_global_id(1);
58 if (globalIdx.x < width && globalIdx.y < height)
59 dst[globalIdx.x + globalIdx.y*dst_stride] = src[globalIdx.x + globalIdx.y*src_stride];
63 local
unsigned int l[32][32];
64 local
unsigned int lcx[LU_RADIUS_X];
65 local
unsigned int lcy[LU_RADIUS_Y];
66 int indexIx, indexIy, i, j;
69 for(i = 0; i <= 1; i++) {
70 indexIy = -8 + (blockIdx.y + i) * 16 + threadIdx.y;
71 indexIy = indexIy < 0 ? 0 : indexIy;
72 indexIy = indexIy >= height ? height - 1: indexIy;
73 for(j = 0; j <= 1; j++) {
74 indexIx = -8 + (blockIdx.x + j) * 16 + threadIdx.x;
75 indexIx = indexIx < 0 ? 0 : indexIx;
76 indexIx = indexIx >= width ? width - 1: indexIx;
77 l[i*16 + threadIdx.y][j*16 + threadIdx.x] = src[indexIy*src_stride + indexIx];
81 int indexL = threadIdx.y*16 + threadIdx.x;
82 if (indexL < LU_RADIUS_X)
83 lcx[indexL] = mask_x[indexL];
84 if (indexL < LU_RADIUS_Y)
85 lcy[indexL] = mask_y[indexL];
86 barrier(CLK_LOCAL_MEM_FENCE);
89 int orig_value = (int)l[threadIdx.y + 8][threadIdx.x + 8];
91 int idx, idy, maskIndex;
93 int steps_x = (LU_RADIUS_X-1)/2;
94 int steps_y = (LU_RADIUS_Y-1)/2;
98 for (j = 0; j <=1; j++) {
100 idx = 16*j + threadIdx.x;
102 for (i = -steps_y; i <= steps_y; i++) {
103 idy = 8 + i + threadIdx.y;
104 maskIndex = (i + steps_y);
105 temp[j] += (int)l[idy][idx] * lcy[maskIndex];
108 barrier(CLK_LOCAL_MEM_FENCE);
110 idy = 8 + threadIdx.y;
112 for (j = 0; j <=1; j++) {
113 idx = 16*j + threadIdx.x;
114 l[idy][idx] =
temp[j];
116 barrier(CLK_LOCAL_MEM_FENCE);
120 idy = 8 + threadIdx.y;
122 for (j = -steps_x; j <= steps_x; j++) {
123 idx = 8 + j + threadIdx.x;
124 maskIndex = j + steps_x;
125 sum += (int)l[idy][idx] * lcx[maskIndex];
128 int res = orig_value + (((orig_value - (int)((sum + halfscale) >> scalebits)) * amount) >> 16);
130 if (globalIdx.x < width && globalIdx.y < height)
131 dst[globalIdx.x + globalIdx.y*dst_stride] = clip_uint8(res);
134 kernel
void unsharp_chroma(
135 global
unsigned char *src_y,
136 global
unsigned char *dst_y,
151 global
unsigned char *dst_u = dst_y + height * dst_stride_lu;
152 global
unsigned char *dst_v = dst_u + ch * dst_stride_ch;
153 global
unsigned char *src_u = src_y + height * src_stride_lu;
154 global
unsigned char *src_v = src_u + ch * src_stride_ch;
155 int2 threadIdx, blockIdx, globalIdx;
156 threadIdx.x = get_local_id(0);
157 threadIdx.y = get_local_id(1);
158 blockIdx.x = get_group_id(0);
159 blockIdx.y = get_group_id(1);
160 globalIdx.x = get_global_id(0);
161 globalIdx.y = get_global_id(1);
162 int padch = get_global_size(1)/2;
163 global
unsigned char *src = globalIdx.y>=padch ? src_v : src_u;
164 global
unsigned char *dst = globalIdx.y>=padch ? dst_v : dst_u;
166 blockIdx.y = globalIdx.y>=padch ? blockIdx.y - get_num_groups(1)/2 : blockIdx.y;
167 globalIdx.y = globalIdx.y>=padch ? globalIdx.y - padch : globalIdx.y;
170 if (globalIdx.x < cw && globalIdx.y < ch)
171 dst[globalIdx.x + globalIdx.y*dst_stride_ch] = src[globalIdx.x + globalIdx.y*src_stride_ch];
175 local
unsigned int l[32][32];
176 local
unsigned int lcx[CH_RADIUS_X];
177 local
unsigned int lcy[CH_RADIUS_Y];
178 int indexIx, indexIy, i, j;
179 for(i = 0; i <= 1; i++) {
180 indexIy = -8 + (blockIdx.y + i) * 16 + threadIdx.y;
181 indexIy = indexIy < 0 ? 0 : indexIy;
182 indexIy = indexIy >= ch ? ch - 1: indexIy;
183 for(j = 0; j <= 1; j++) {
184 indexIx = -8 + (blockIdx.x + j) * 16 + threadIdx.x;
185 indexIx = indexIx < 0 ? 0 : indexIx;
186 indexIx = indexIx >= cw ? cw - 1: indexIx;
187 l[i*16 + threadIdx.y][j*16 + threadIdx.x] = src[indexIy * src_stride_ch + indexIx];
191 int indexL = threadIdx.y*16 + threadIdx.x;
192 if (indexL < CH_RADIUS_X)
193 lcx[indexL] = mask_x[indexL];
194 if (indexL < CH_RADIUS_Y)
195 lcy[indexL] = mask_y[indexL];
196 barrier(CLK_LOCAL_MEM_FENCE);
198 int orig_value = (int)l[threadIdx.y + 8][threadIdx.x + 8];
200 int idx, idy, maskIndex;
201 int steps_x = CH_RADIUS_X/2;
202 int steps_y = CH_RADIUS_Y/2;
206 for (j = 0; j <= 1; j++) {
207 idx = 16*j + threadIdx.x;
209 for (i = -steps_y; i <= steps_y; i++) {
210 idy = 8 + i + threadIdx.y;
211 maskIndex = i + steps_y;
212 temp[j] += (int)l[idy][idx] * lcy[maskIndex];
216 barrier(CLK_LOCAL_MEM_FENCE);
217 idy = 8 + threadIdx.y;
219 for (j = 0; j <= 1; j++) {
220 idx = 16*j + threadIdx.x;
221 l[idy][idx] =
temp[j];
223 barrier(CLK_LOCAL_MEM_FENCE);
227 idy = 8 + threadIdx.y;
229 for (j = -steps_x; j <= steps_x; j++) {
230 idx = 8 + j + threadIdx.x;
231 maskIndex = j + steps_x;
232 sum += (int)l[idy][idx] * lcx[maskIndex];
235 int res = orig_value + (((orig_value - (int)((sum + halfscale) >> scalebits)) * amount) >> 16);
237 if (globalIdx.x < cw && globalIdx.y < ch)
238 dst[globalIdx.x + globalIdx.y*dst_stride_ch] = clip_uint8(res);
241 kernel
void unsharp_default(global
unsigned char *src,
242 global
unsigned char *dst,
243 const global
unsigned int *mask_lu,
244 const global
unsigned int *mask_ch,
264 global
unsigned char *dst_y = dst;
265 global
unsigned char *dst_u = dst_y + height * dst_stride_lu;
266 global
unsigned char *dst_v = dst_u + ch * dst_stride_ch;
268 global
unsigned char *src_y =
src;
269 global
unsigned char *src_u = src_y + height * src_stride_lu;
270 global
unsigned char *src_v = src_u + ch * src_stride_ch;
272 global
unsigned char *temp_dst;
273 global
unsigned char *temp_src;
274 const global
unsigned int *temp_mask;
275 int global_id = get_global_id(0);
276 int i, j, x,
y, temp_src_stride, temp_dst_stride, temp_height, temp_width, temp_steps_x, temp_steps_y,
277 temp_amount, temp_scalebits, temp_halfscale, sum, idx_x, idx_y,
temp, res;
278 if (global_id < width * height) {
279 y = global_id /
width;
280 x = global_id %
width;
283 temp_src_stride = src_stride_lu;
284 temp_dst_stride = dst_stride_lu;
287 temp_steps_x = step_x_lu;
288 temp_steps_y = step_y_lu;
290 temp_amount = amount_lu;
291 temp_scalebits = scalebits_lu;
292 temp_halfscale = halfscale_lu;
293 }
else if ((global_id >= width * height) && (global_id < width * height + ch * cw)) {
294 y = (global_id - width *
height) / cw;
295 x = (global_id - width *
height) % cw;
298 temp_src_stride = src_stride_ch;
299 temp_dst_stride = dst_stride_ch;
302 temp_steps_x = step_x_ch;
303 temp_steps_y = step_y_ch;
305 temp_amount = amount_ch;
306 temp_scalebits = scalebits_ch;
307 temp_halfscale = halfscale_ch;
309 y = (global_id - width * height - ch * cw) / cw;
310 x = (global_id - width * height - ch * cw) % cw;
313 temp_src_stride = src_stride_ch;
314 temp_dst_stride = dst_stride_ch;
317 temp_steps_x = step_x_ch;
318 temp_steps_y = step_y_ch;
320 temp_amount = amount_ch;
321 temp_scalebits = scalebits_ch;
322 temp_halfscale = halfscale_ch;
326 for (j = 0; j <= 2 * temp_steps_y; j++) {
327 idx_y = (y - temp_steps_y + j) <= 0 ? 0 : (y - temp_steps_y + j) >= temp_height ? temp_height-1 : y - temp_steps_y + j;
328 for (i = 0; i <= 2 * temp_steps_x; i++) {
329 idx_x = (x - temp_steps_x + i) <= 0 ? 0 : (x - temp_steps_x + i) >= temp_width ? temp_width-1 : x - temp_steps_x + i;
330 sum += temp_mask[i + j * (2 * temp_steps_x + 1)] * temp_src[idx_x + idx_y * temp_src_stride];
333 temp = (int)temp_src[x + y * temp_src_stride];
334 res = temp + (((temp - (int)((sum + temp_halfscale) >> temp_scalebits)) * temp_amount) >> 16);
335 temp_dst[x + y * temp_dst_stride] = clip_uint8(res);
337 temp_dst[x + y * temp_dst_stride] = temp_src[x + y * temp_src_stride];
#define AV_OPENCL_KERNEL(...)
const char * ff_kernel_unsharp_opencl
BYTE int const BYTE int int int height