35 inline unsigned char clip_uint8(
int a)
43 kernel
void unsharp_bench(
44 global
unsigned char *
src,
45 global
unsigned char *dst,
50 int i, j, local_idx, lc_idx, sum = 0;
51 int2 thread_idx, block_idx, global_idx, lm_idx;
52 thread_idx.x = get_local_id(0);
53 thread_idx.y = get_local_id(1);
54 block_idx.x = get_group_id(0);
55 block_idx.y = get_group_id(1);
56 global_idx.x = get_global_id(0);
57 global_idx.y = get_global_id(1);
58 local uchar
data[32][32];
61 for (i = 0; i <= 1; i++) {
62 lm_idx.y = -8 + (block_idx.y + i) * 16 + thread_idx.y;
63 lm_idx.y = lm_idx.y < 0 ? 0 : lm_idx.y;
64 lm_idx.y = lm_idx.y >= height ? height - 1: lm_idx.y;
65 for (j = 0; j <= 1; j++) {
66 lm_idx.x = -8 + (block_idx.x + j) * 16 + thread_idx.x;
67 lm_idx.x = lm_idx.x < 0 ? 0 : lm_idx.x;
68 lm_idx.x = lm_idx.x >= width ? width - 1: lm_idx.x;
69 data[i*16 + thread_idx.y][j*16 + thread_idx.x] = src[lm_idx.y*width + lm_idx.x];
72 local_idx = thread_idx.y*16 + thread_idx.x;
74 lc[local_idx] = mask[local_idx];
75 barrier(CLK_LOCAL_MEM_FENCE);
78 for (i = -4; i <= 4; i++) {
79 lm_idx.y = 8 + i + thread_idx.y;
81 for (j = -4; j <= 4; j++) {
82 lm_idx.x = 8 + j + thread_idx.x;
83 lc_idx = (i + 4)*8 + j + 4;
84 sum += (int)data[lm_idx.y][lm_idx.x] * lc[lc_idx];
87 int temp = (int)data[thread_idx.y + 8][thread_idx.x + 8];
88 int res = temp + (((temp - (
int)((sum + 1<<15) >> 16))) >> 16);
89 if (global_idx.x < width && global_idx.y < height)
90 dst[global_idx.x + global_idx.y*
width] = clip_uint8(res);
94 #define OCLCHECK(method, ... ) \
96 status = method(__VA_ARGS__); \
97 if (status != CL_SUCCESS) { \
98 av_log(NULL, AV_LOG_ERROR, # method " error '%s'\n", \
99 av_opencl_errstr(status)); \
100 ret = AVERROR_EXTERNAL; \
105 #define CREATEBUF(out, flags, size) \
107 out = clCreateBuffer(ext_opencl_env->context, flags, size, NULL, &status); \
108 if (status != CL_SUCCESS) { \
109 av_log(NULL, AV_LOG_ERROR, "Could not create OpenCL buffer\n"); \
110 ret = AVERROR_EXTERNAL; \
119 for (i = 0; i <
n; i++)
123 #define OPENCL_NB_ITER 5
126 int i,
arg = 0, width = 1920, height = 1088;
132 int buf_size = width * height *
sizeof(char);
133 int mask_size =
sizeof(uint32_t) * 128;
135 cl_mem cl_mask, cl_inbuf, cl_outbuf;
136 cl_kernel kernel = NULL;
137 cl_program program = NULL;
138 size_t local_work_size_2d[2] = {16, 16};
139 size_t global_work_size_2d[2] = {(size_t)width, (
size_t)height};
149 CREATEBUF(cl_mask, CL_MEM_READ_ONLY, mask_size);
150 CREATEBUF(cl_inbuf, CL_MEM_READ_ONLY, buf_size);
151 CREATEBUF(cl_outbuf, CL_MEM_READ_WRITE, buf_size);
155 &kernel_len, &status);
156 if (status != CL_SUCCESS || !program) {
161 status = clBuildProgram(program, 1, &(ext_opencl_env->
device_id), NULL, NULL, NULL);
162 if (status != CL_SUCCESS) {
167 kernel = clCreateKernel(program,
"unsharp_bench", &status);
168 if (status != CL_SUCCESS) {
175 buf_size, inbuf, 0, NULL, NULL);
177 mask_size, mask, 0, NULL, NULL);
178 OCLCHECK(clSetKernelArg, kernel, arg++,
sizeof(cl_mem), &cl_inbuf);
179 OCLCHECK(clSetKernelArg, kernel, arg++,
sizeof(cl_mem), &cl_outbuf);
180 OCLCHECK(clSetKernelArg, kernel, arg++,
sizeof(cl_mem), &cl_mask);
181 OCLCHECK(clSetKernelArg, kernel, arg++,
sizeof(cl_int), &width);
182 OCLCHECK(clSetKernelArg, kernel, arg++,
sizeof(cl_int), &height);
187 global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
192 clReleaseKernel(kernel);
194 clReleaseProgram(program);
196 clReleaseMemObject(cl_inbuf);
198 clReleaseMemObject(cl_outbuf);
200 clReleaseMemObject(cl_mask);
213 int i, j, nb_devices = 0,
count = 0;
218 cl_platform_id platform;
247 fprintf(stderr,
"platform_idx\tdevice_idx\tdevice_name\truntime\n");
248 for (i = 0; i <
count; i++)
249 fprintf(stdout,
"%d\t%d\t%s\t%"PRId64
"\n",
250 devices[i].platform_idx, devices[i].device_idx,
251 devices[i].device_name, devices[i].runtime);
261 const char *opts =
arg;