2 * Copyright (C) 2013 Lenny Wang
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/opt.h"
22 #include "libavutil/time.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opencl.h"
25 #include "libavutil/avstring.h"
33 } OpenCLDeviceBenchmark;
35 const char *ocl_bench_source = AV_OPENCL_KERNEL(
36 inline unsigned char clip_uint8(int a)
44 kernel void unsharp_bench(
45 global unsigned char *src,
46 global unsigned char *dst,
51 int i, j, local_idx, lc_idx, sum = 0;
52 int2 thread_idx, block_idx, global_idx, lm_idx;
53 thread_idx.x = get_local_id(0);
54 thread_idx.y = get_local_id(1);
55 block_idx.x = get_group_id(0);
56 block_idx.y = get_group_id(1);
57 global_idx.x = get_global_id(0);
58 global_idx.y = get_global_id(1);
59 local uchar data[32][32];
62 for (i = 0; i <= 1; i++) {
63 lm_idx.y = -8 + (block_idx.y + i) * 16 + thread_idx.y;
64 lm_idx.y = lm_idx.y < 0 ? 0 : lm_idx.y;
65 lm_idx.y = lm_idx.y >= height ? height - 1: lm_idx.y;
66 for (j = 0; j <= 1; j++) {
67 lm_idx.x = -8 + (block_idx.x + j) * 16 + thread_idx.x;
68 lm_idx.x = lm_idx.x < 0 ? 0 : lm_idx.x;
69 lm_idx.x = lm_idx.x >= width ? width - 1: lm_idx.x;
70 data[i*16 + thread_idx.y][j*16 + thread_idx.x] = src[lm_idx.y*width + lm_idx.x];
73 local_idx = thread_idx.y*16 + thread_idx.x;
75 lc[local_idx] = mask[local_idx];
76 barrier(CLK_LOCAL_MEM_FENCE);
79 for (i = -4; i <= 4; i++) {
80 lm_idx.y = 8 + i + thread_idx.y;
82 for (j = -4; j <= 4; j++) {
83 lm_idx.x = 8 + j + thread_idx.x;
84 lc_idx = (i + 4)*8 + j + 4;
85 sum += (int)data[lm_idx.y][lm_idx.x] * lc[lc_idx];
88 int temp = (int)data[thread_idx.y + 8][thread_idx.x + 8];
89 int res = temp + (((temp - (int)((sum + 1<<15) >> 16))) >> 16);
90 if (global_idx.x < width && global_idx.y < height)
91 dst[global_idx.x + global_idx.y*width] = clip_uint8(res);
95 #define OCLCHECK(method, ... ) \
97 status = method(__VA_ARGS__); \
98 if (status != CL_SUCCESS) { \
99 av_log(NULL, AV_LOG_ERROR, # method " error '%s'\n", \
100 av_opencl_errstr(status)); \
101 ret = AVERROR_EXTERNAL; \
106 #define CREATEBUF(out, flags, size) \
108 out = clCreateBuffer(ext_opencl_env->context, flags, size, NULL, &status); \
109 if (status != CL_SUCCESS) { \
110 av_log(NULL, AV_LOG_ERROR, "Could not create OpenCL buffer\n"); \
111 ret = AVERROR_EXTERNAL; \
116 static void fill_rand_int(int *data, int n)
120 for (i = 0; i < n; i++)
124 #define OPENCL_NB_ITER 5
125 static int64_t run_opencl_bench(AVOpenCLExternalEnv *ext_opencl_env)
127 int i, arg = 0, width = 1920, height = 1088;
128 int64_t start, ret = 0;
133 int buf_size = width * height * sizeof(char);
134 int mask_size = sizeof(uint32_t) * 128;
136 cl_mem cl_mask, cl_inbuf, cl_outbuf;
137 cl_kernel kernel = NULL;
138 cl_program program = NULL;
139 size_t local_work_size_2d[2] = {16, 16};
140 size_t global_work_size_2d[2] = {(size_t)width, (size_t)height};
142 if (!(inbuf = av_malloc(buf_size)) || !(mask = av_malloc(mask_size))) {
143 av_log(NULL, AV_LOG_ERROR, "Out of memory\n");
144 ret = AVERROR(ENOMEM);
147 fill_rand_int((int*)inbuf, buf_size/4);
148 fill_rand_int(mask, mask_size/4);
150 CREATEBUF(cl_mask, CL_MEM_READ_ONLY, mask_size);
151 CREATEBUF(cl_inbuf, CL_MEM_READ_ONLY, buf_size);
152 CREATEBUF(cl_outbuf, CL_MEM_READ_WRITE, buf_size);
154 kernel_len = strlen(ocl_bench_source);
155 program = clCreateProgramWithSource(ext_opencl_env->context, 1, &ocl_bench_source,
156 &kernel_len, &status);
157 if (status != CL_SUCCESS || !program) {
158 av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark program\n");
159 ret = AVERROR_EXTERNAL;
162 status = clBuildProgram(program, 1, &(ext_opencl_env->device_id), NULL, NULL, NULL);
163 if (status != CL_SUCCESS) {
164 av_log(NULL, AV_LOG_ERROR, "OpenCL unable to build benchmark program\n");
165 ret = AVERROR_EXTERNAL;
168 kernel = clCreateKernel(program, "unsharp_bench", &status);
169 if (status != CL_SUCCESS) {
170 av_log(NULL, AV_LOG_ERROR, "OpenCL unable to create benchmark kernel\n");
171 ret = AVERROR_EXTERNAL;
175 OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_inbuf, CL_TRUE, 0,
176 buf_size, inbuf, 0, NULL, NULL);
177 OCLCHECK(clEnqueueWriteBuffer, ext_opencl_env->command_queue, cl_mask, CL_TRUE, 0,
178 mask_size, mask, 0, NULL, NULL);
179 OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_inbuf);
180 OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_outbuf);
181 OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_mem), &cl_mask);
182 OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &width);
183 OCLCHECK(clSetKernelArg, kernel, arg++, sizeof(cl_int), &height);
185 start = av_gettime_relative();
186 for (i = 0; i < OPENCL_NB_ITER; i++)
187 OCLCHECK(clEnqueueNDRangeKernel, ext_opencl_env->command_queue, kernel, 2, NULL,
188 global_work_size_2d, local_work_size_2d, 0, NULL, NULL);
189 clFinish(ext_opencl_env->command_queue);
190 ret = (av_gettime_relative() - start)/OPENCL_NB_ITER;
193 clReleaseKernel(kernel);
195 clReleaseProgram(program);
197 clReleaseMemObject(cl_inbuf);
199 clReleaseMemObject(cl_outbuf);
201 clReleaseMemObject(cl_mask);
207 static int compare_ocl_device_desc(const void *a, const void *b)
209 const OpenCLDeviceBenchmark* va = (const OpenCLDeviceBenchmark*)a;
210 const OpenCLDeviceBenchmark* vb = (const OpenCLDeviceBenchmark*)b;
211 return FFDIFFSIGN(va->runtime , vb->runtime);
214 int opt_opencl_bench(void *optctx, const char *opt, const char *arg)
216 int i, j, nb_devices = 0, count = 0;
218 AVOpenCLDeviceList *device_list;
219 AVOpenCLDeviceNode *device_node = NULL;
220 OpenCLDeviceBenchmark *devices = NULL;
221 cl_platform_id platform;
223 av_opencl_get_device_list(&device_list);
224 for (i = 0; i < device_list->platform_num; i++)
225 nb_devices += device_list->platform_node[i]->device_num;
227 av_log(NULL, AV_LOG_ERROR, "No OpenCL device detected!\n");
228 return AVERROR(EINVAL);
230 if (!(devices = av_malloc_array(nb_devices, sizeof(OpenCLDeviceBenchmark)))) {
231 av_log(NULL, AV_LOG_ERROR, "Could not allocate buffer\n");
232 return AVERROR(ENOMEM);
235 for (i = 0; i < device_list->platform_num; i++) {
236 for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
237 device_node = device_list->platform_node[i]->device_node[j];
238 platform = device_list->platform_node[i]->platform_id;
239 score = av_opencl_benchmark(device_node, platform, run_opencl_bench);
241 devices[count].platform_idx = i;
242 devices[count].device_idx = j;
243 devices[count].runtime = score;
244 av_strlcpy(devices[count].device_name, device_node->device_name,
245 sizeof(devices[count].device_name));
250 qsort(devices, count, sizeof(OpenCLDeviceBenchmark), compare_ocl_device_desc);
251 fprintf(stderr, "platform_idx\tdevice_idx\tdevice_name\truntime\n");
252 for (i = 0; i < count; i++)
253 fprintf(stdout, "%d\t%d\t%s\t%"PRId64"\n",
254 devices[i].platform_idx, devices[i].device_idx,
255 devices[i].device_name, devices[i].runtime);
257 av_opencl_free_device_list(&device_list);
262 int opt_opencl(void *optctx, const char *opt, const char *arg)
265 const char *opts = arg;
268 ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
271 ret = av_opencl_set_option(key, value);