2 * Copyright (C) 2012 Peng Gao <peng@multicorewareinc.com>
3 * Copyright (C) 2012 Li Cao <li@multicorewareinc.com>
4 * Copyright (C) 2012 Wei Gao <weigao@multicorewareinc.com>
5 * Copyright (C) 2013 Lenny Wang <lwanghpc@gmail.com>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 static pthread_mutex_t * volatile atomic_opencl_lock = NULL;
35 #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
36 #define UNLOCK_OPENCL pthread_mutex_unlock(atomic_opencl_lock)
42 #define MAX_KERNEL_CODE_NUM 200
46 const char *kernel_string;
56 * if set to 1, the OpenCL environment was created by the user and
57 * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
62 cl_platform_id platform_id;
63 cl_device_type device_type;
65 cl_device_id device_id;
66 cl_command_queue command_queue;
67 int kernel_code_count;
68 KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
69 AVOpenCLDeviceList device_list;
72 #define OFFSET(x) offsetof(OpenclContext, x)
74 static const AVOption opencl_options[] = {
75 { "platform_idx", "set platform index value", OFFSET(platform_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
76 { "device_idx", "set device index value", OFFSET(device_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
80 static const AVClass openclutils_class = {
81 .class_name = "opencl",
82 .option = opencl_options,
83 .item_name = av_default_item_name,
84 .version = LIBAVUTIL_VERSION_INT,
85 .log_level_offset_offset = offsetof(OpenclContext, log_offset),
86 .parent_log_context_offset = offsetof(OpenclContext, log_ctx),
89 static OpenclContext opencl_ctx = {&openclutils_class};
91 static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
98 static const OpenclErrorMsg opencl_err_msg[] = {
99 {CL_DEVICE_NOT_FOUND, "DEVICE NOT FOUND"},
100 {CL_DEVICE_NOT_AVAILABLE, "DEVICE NOT AVAILABLE"},
101 {CL_COMPILER_NOT_AVAILABLE, "COMPILER NOT AVAILABLE"},
102 {CL_MEM_OBJECT_ALLOCATION_FAILURE, "MEM OBJECT ALLOCATION FAILURE"},
103 {CL_OUT_OF_RESOURCES, "OUT OF RESOURCES"},
104 {CL_OUT_OF_HOST_MEMORY, "OUT OF HOST MEMORY"},
105 {CL_PROFILING_INFO_NOT_AVAILABLE, "PROFILING INFO NOT AVAILABLE"},
106 {CL_MEM_COPY_OVERLAP, "MEM COPY OVERLAP"},
107 {CL_IMAGE_FORMAT_MISMATCH, "IMAGE FORMAT MISMATCH"},
108 {CL_IMAGE_FORMAT_NOT_SUPPORTED, "IMAGE FORMAT NOT_SUPPORTED"},
109 {CL_BUILD_PROGRAM_FAILURE, "BUILD PROGRAM FAILURE"},
110 {CL_MAP_FAILURE, "MAP FAILURE"},
111 {CL_MISALIGNED_SUB_BUFFER_OFFSET, "MISALIGNED SUB BUFFER OFFSET"},
112 {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST, "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
113 {CL_COMPILE_PROGRAM_FAILURE, "COMPILE PROGRAM FAILURE"},
114 {CL_LINKER_NOT_AVAILABLE, "LINKER NOT AVAILABLE"},
115 {CL_LINK_PROGRAM_FAILURE, "LINK PROGRAM FAILURE"},
116 {CL_DEVICE_PARTITION_FAILED, "DEVICE PARTITION FAILED"},
117 {CL_KERNEL_ARG_INFO_NOT_AVAILABLE, "KERNEL ARG INFO NOT AVAILABLE"},
118 {CL_INVALID_VALUE, "INVALID VALUE"},
119 {CL_INVALID_DEVICE_TYPE, "INVALID DEVICE TYPE"},
120 {CL_INVALID_PLATFORM, "INVALID PLATFORM"},
121 {CL_INVALID_DEVICE, "INVALID DEVICE"},
122 {CL_INVALID_CONTEXT, "INVALID CONTEXT"},
123 {CL_INVALID_QUEUE_PROPERTIES, "INVALID QUEUE PROPERTIES"},
124 {CL_INVALID_COMMAND_QUEUE, "INVALID COMMAND QUEUE"},
125 {CL_INVALID_HOST_PTR, "INVALID HOST PTR"},
126 {CL_INVALID_MEM_OBJECT, "INVALID MEM OBJECT"},
127 {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, "INVALID IMAGE FORMAT DESCRIPTOR"},
128 {CL_INVALID_IMAGE_SIZE, "INVALID IMAGE SIZE"},
129 {CL_INVALID_SAMPLER, "INVALID SAMPLER"},
130 {CL_INVALID_BINARY, "INVALID BINARY"},
131 {CL_INVALID_BUILD_OPTIONS, "INVALID BUILD OPTIONS"},
132 {CL_INVALID_PROGRAM, "INVALID PROGRAM"},
133 {CL_INVALID_PROGRAM_EXECUTABLE, "INVALID PROGRAM EXECUTABLE"},
134 {CL_INVALID_KERNEL_NAME, "INVALID KERNEL NAME"},
135 {CL_INVALID_KERNEL_DEFINITION, "INVALID KERNEL DEFINITION"},
136 {CL_INVALID_KERNEL, "INVALID KERNEL"},
137 {CL_INVALID_ARG_INDEX, "INVALID ARG INDEX"},
138 {CL_INVALID_ARG_VALUE, "INVALID ARG VALUE"},
139 {CL_INVALID_ARG_SIZE, "INVALID ARG_SIZE"},
140 {CL_INVALID_KERNEL_ARGS, "INVALID KERNEL ARGS"},
141 {CL_INVALID_WORK_DIMENSION, "INVALID WORK DIMENSION"},
142 {CL_INVALID_WORK_GROUP_SIZE, "INVALID WORK GROUP SIZE"},
143 {CL_INVALID_WORK_ITEM_SIZE, "INVALID WORK ITEM SIZE"},
144 {CL_INVALID_GLOBAL_OFFSET, "INVALID GLOBAL OFFSET"},
145 {CL_INVALID_EVENT_WAIT_LIST, "INVALID EVENT WAIT LIST"},
146 {CL_INVALID_EVENT, "INVALID EVENT"},
147 {CL_INVALID_OPERATION, "INVALID OPERATION"},
148 {CL_INVALID_GL_OBJECT, "INVALID GL OBJECT"},
149 {CL_INVALID_BUFFER_SIZE, "INVALID BUFFER SIZE"},
150 {CL_INVALID_MIP_LEVEL, "INVALID MIP LEVEL"},
151 {CL_INVALID_GLOBAL_WORK_SIZE, "INVALID GLOBAL WORK SIZE"},
152 {CL_INVALID_PROPERTY, "INVALID PROPERTY"},
153 {CL_INVALID_IMAGE_DESCRIPTOR, "INVALID IMAGE DESCRIPTOR"},
154 {CL_INVALID_COMPILER_OPTIONS, "INVALID COMPILER OPTIONS"},
155 {CL_INVALID_LINKER_OPTIONS, "INVALID LINKER OPTIONS"},
156 {CL_INVALID_DEVICE_PARTITION_COUNT, "INVALID DEVICE PARTITION COUNT"},
159 const char *av_opencl_errstr(cl_int status)
162 for (i = 0; i < FF_ARRAY_ELEMS(opencl_err_msg); i++) {
163 if (opencl_err_msg[i].err_code == status)
164 return opencl_err_msg[i].err_str;
166 return "unknown error";
169 static void free_device_list(AVOpenCLDeviceList *device_list)
174 for (i = 0; i < device_list->platform_num; i++) {
175 if (!device_list->platform_node[i])
177 for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
178 av_freep(&(device_list->platform_node[i]->device_node[j]->device_name));
179 av_freep(&(device_list->platform_node[i]->device_node[j]));
181 av_freep(&device_list->platform_node[i]->device_node);
182 av_freep(&(device_list->platform_node[i]->platform_name));
183 av_freep(&device_list->platform_node[i]);
185 av_freep(&device_list->platform_node);
186 device_list->platform_num = 0;
189 static int get_device_list(AVOpenCLDeviceList *device_list)
192 int i, j, k, device_num, total_devices_num, ret = 0;
194 cl_platform_id *platform_ids = NULL;
195 cl_device_id *device_ids = NULL;
196 AVOpenCLDeviceNode *device_node = NULL;
197 size_t platform_name_size = 0;
198 size_t device_name_size = 0;
199 status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
200 if (status != CL_SUCCESS) {
201 av_log(&opencl_ctx, AV_LOG_ERROR,
202 "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
203 return AVERROR_EXTERNAL;
205 platform_ids = av_mallocz_array(device_list->platform_num, sizeof(cl_platform_id));
207 return AVERROR(ENOMEM);
208 status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
209 if (status != CL_SUCCESS) {
210 av_log(&opencl_ctx, AV_LOG_ERROR,
211 "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
212 ret = AVERROR_EXTERNAL;
215 device_list->platform_node = av_mallocz_array(device_list->platform_num, sizeof(AVOpenCLPlatformNode *));
216 if (!device_list->platform_node) {
217 ret = AVERROR(ENOMEM);
220 devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
222 ret = AVERROR(ENOMEM);
225 for (i = 0; i < device_list->platform_num; i++) {
226 device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
227 if (!device_list->platform_node[i]) {
228 ret = AVERROR(ENOMEM);
231 device_list->platform_node[i]->platform_id = platform_ids[i];
232 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
233 0, NULL, &platform_name_size);
234 if (status != CL_SUCCESS) {
235 av_log(&opencl_ctx, AV_LOG_WARNING,
236 "Could not get size of platform name: %s\n", av_opencl_errstr(status));
238 device_list->platform_node[i]->platform_name = av_malloc(platform_name_size * sizeof(char));
239 if (!device_list->platform_node[i]->platform_name) {
240 av_log(&opencl_ctx, AV_LOG_WARNING,
241 "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
243 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
244 platform_name_size * sizeof(char),
245 device_list->platform_node[i]->platform_name, NULL);
246 if (status != CL_SUCCESS) {
247 av_log(&opencl_ctx, AV_LOG_WARNING,
248 "Could not get platform name: %s\n", av_opencl_errstr(status));
252 total_devices_num = 0;
253 for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
254 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
255 device_type[j], 0, NULL, &devices_num[j]);
256 total_devices_num += devices_num[j];
258 device_list->platform_node[i]->device_node = av_mallocz_array(total_devices_num, sizeof(AVOpenCLDeviceNode *));
259 if (!device_list->platform_node[i]->device_node) {
260 ret = AVERROR(ENOMEM);
263 for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
264 if (devices_num[j]) {
265 device_ids = av_mallocz_array(devices_num[j], sizeof(cl_device_id));
267 ret = AVERROR(ENOMEM);
270 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
271 devices_num[j], device_ids, NULL);
272 if (status != CL_SUCCESS) {
273 av_log(&opencl_ctx, AV_LOG_WARNING,
274 "Could not get device ID: %s:\n", av_opencl_errstr(status));
275 av_freep(&device_ids);
278 for (k = 0; k < devices_num[j]; k++) {
279 device_num = device_list->platform_node[i]->device_num;
280 device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
281 if (!device_list->platform_node[i]->device_node[device_num]) {
282 ret = AVERROR(ENOMEM);
285 device_node = device_list->platform_node[i]->device_node[device_num];
286 device_node->device_id = device_ids[k];
287 device_node->device_type = device_type[j];
288 status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
289 0, NULL, &device_name_size);
290 if (status != CL_SUCCESS) {
291 av_log(&opencl_ctx, AV_LOG_WARNING,
292 "Could not get size of device name: %s\n", av_opencl_errstr(status));
295 device_node->device_name = av_malloc(device_name_size * sizeof(char));
296 if (!device_node->device_name) {
297 av_log(&opencl_ctx, AV_LOG_WARNING,
298 "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
301 status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
302 device_name_size * sizeof(char),
303 device_node->device_name, NULL);
304 if (status != CL_SUCCESS) {
305 av_log(&opencl_ctx, AV_LOG_WARNING,
306 "Could not get device name: %s\n", av_opencl_errstr(status));
309 device_list->platform_node[i]->device_num++;
311 av_freep(&device_ids);
316 av_freep(&platform_ids);
317 av_freep(&devices_num);
318 av_freep(&device_ids);
320 free_device_list(device_list);
324 int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
327 *device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
328 if (!(*device_list)) {
329 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n");
330 return AVERROR(ENOMEM);
332 ret = get_device_list(*device_list);
334 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n");
335 free_device_list(*device_list);
336 av_freep(device_list);
342 void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
344 free_device_list(*device_list);
345 av_freep(device_list);
348 static inline int init_opencl_mtx(void)
351 if (!atomic_opencl_lock) {
353 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
355 return AVERROR(ENOMEM);
356 if ((err = pthread_mutex_init(tmp, NULL))) {
360 if (avpriv_atomic_ptr_cas((void * volatile *)&atomic_opencl_lock, NULL, tmp)) {
361 pthread_mutex_destroy(tmp);
369 int av_opencl_set_option(const char *key, const char *val)
371 int ret = init_opencl_mtx( );
375 if (!opencl_ctx.opt_init_flag) {
376 av_opt_set_defaults(&opencl_ctx);
377 opencl_ctx.opt_init_flag = 1;
379 ret = av_opt_set(&opencl_ctx, key, val, 0);
384 int av_opencl_get_option(const char *key, uint8_t **out_val)
388 ret = av_opt_get(&opencl_ctx, key, 0, out_val);
393 void av_opencl_free_option(void)
395 /*FIXME: free openclutils context*/
397 av_opt_free(&opencl_ctx);
401 AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
403 AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
405 av_log(&opencl_ctx, AV_LOG_ERROR,
406 "Could not malloc external opencl environment data space\n");
411 void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
413 av_freep(ext_opencl_env);
416 int av_opencl_register_kernel_code(const char *kernel_code)
418 int i, ret = init_opencl_mtx( );
422 if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
423 av_log(&opencl_ctx, AV_LOG_ERROR,
424 "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
425 MAX_KERNEL_CODE_NUM);
426 ret = AVERROR(EINVAL);
429 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
430 if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) {
431 av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n");
435 opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code;
436 opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0;
437 opencl_ctx.kernel_code_count++;
443 cl_program av_opencl_compile(const char *program_name, const char *build_opts)
446 cl_int status, build_status;
447 int kernel_code_idx = 0;
448 const char *kernel_source = NULL;
449 size_t kernel_code_len;
451 cl_program program = NULL;
456 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
457 // identify a program using a unique name within the kernel source
458 ptr = av_stristr(opencl_ctx.kernel_code[i].kernel_string, program_name);
459 if (ptr && !opencl_ctx.kernel_code[i].is_compiled) {
460 kernel_source = opencl_ctx.kernel_code[i].kernel_string;
461 kernel_code_len = strlen(opencl_ctx.kernel_code[i].kernel_string);
466 if (!kernel_source) {
467 av_log(&opencl_ctx, AV_LOG_ERROR,
468 "Unable to find OpenCL kernel source '%s'\n", program_name);
472 /* create a CL program from kernel source */
473 program = clCreateProgramWithSource(opencl_ctx.context, 1, &kernel_source, &kernel_code_len, &status);
474 if(status != CL_SUCCESS) {
475 av_log(&opencl_ctx, AV_LOG_ERROR,
476 "Unable to create OpenCL program '%s': %s\n", program_name, av_opencl_errstr(status));
481 build_status = clBuildProgram(program, 1, &(opencl_ctx.device_id), build_opts, NULL, NULL);
482 status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
483 CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
484 if (status != CL_SUCCESS) {
485 av_log(&opencl_ctx, AV_LOG_WARNING,
486 "Failed to get compilation log: %s\n",
487 av_opencl_errstr(status));
489 log = av_malloc(log_size);
491 status = clGetProgramBuildInfo(program, opencl_ctx.device_id,
492 CL_PROGRAM_BUILD_LOG, log_size,
494 if (status != CL_SUCCESS) {
495 av_log(&opencl_ctx, AV_LOG_WARNING,
496 "Failed to get compilation log: %s\n",
497 av_opencl_errstr(status));
499 int level = build_status == CL_SUCCESS ? AV_LOG_DEBUG :
501 av_log(&opencl_ctx, level, "Compilation log:\n%s\n", log);
506 if (build_status != CL_SUCCESS) {
507 av_log(&opencl_ctx, AV_LOG_ERROR,
508 "Compilation failed with OpenCL program '%s': %s\n",
509 program_name, av_opencl_errstr(build_status));
514 opencl_ctx.kernel_code[kernel_code_idx].is_compiled = 1;
520 cl_command_queue av_opencl_get_command_queue(void)
522 return opencl_ctx.command_queue;
525 static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
528 cl_context_properties cps[3];
530 AVOpenCLDeviceNode *device_node = NULL;
532 if (ext_opencl_env) {
533 if (opencl_ctx->is_user_created)
535 opencl_ctx->platform_id = ext_opencl_env->platform_id;
536 opencl_ctx->is_user_created = 1;
537 opencl_ctx->command_queue = ext_opencl_env->command_queue;
538 opencl_ctx->context = ext_opencl_env->context;
539 opencl_ctx->device_id = ext_opencl_env->device_id;
540 opencl_ctx->device_type = ext_opencl_env->device_type;
542 if (!opencl_ctx->is_user_created) {
543 if (!opencl_ctx->device_list.platform_num) {
544 ret = get_device_list(&opencl_ctx->device_list);
549 if (opencl_ctx->platform_idx >= 0) {
550 if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
551 av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
552 return AVERROR(EINVAL);
554 if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
555 av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
556 opencl_ctx->platform_idx);
557 return AVERROR(EINVAL);
559 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
561 /* get a usable platform by default*/
562 for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
563 if (opencl_ctx->device_list.platform_node[i]->device_num) {
564 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
565 opencl_ctx->platform_idx = i;
570 if (!opencl_ctx->platform_id) {
571 av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
572 return AVERROR_EXTERNAL;
574 /* get a usable device*/
575 if (opencl_ctx->device_idx >= 0) {
576 if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
577 av_log(opencl_ctx, AV_LOG_ERROR,
578 "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
579 return AVERROR(EINVAL);
582 opencl_ctx->device_idx = 0;
585 device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
586 opencl_ctx->device_id = device_node->device_id;
587 opencl_ctx->device_type = device_node->device_type;
590 * Use available platform.
592 av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, Device Name: %s\n",
593 opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
594 device_node->device_name);
595 cps[0] = CL_CONTEXT_PLATFORM;
596 cps[1] = (cl_context_properties)opencl_ctx->platform_id;
599 opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
600 NULL, NULL, &status);
601 if (status != CL_SUCCESS) {
602 av_log(opencl_ctx, AV_LOG_ERROR,
603 "Could not get OpenCL context from device type: %s\n", av_opencl_errstr(status));
604 return AVERROR_EXTERNAL;
606 opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
608 if (status != CL_SUCCESS) {
609 av_log(opencl_ctx, AV_LOG_ERROR,
610 "Could not create OpenCL command queue: %s\n", av_opencl_errstr(status));
611 return AVERROR_EXTERNAL;
618 int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
620 int ret = init_opencl_mtx( );
624 if (!opencl_ctx.init_count) {
625 if (!opencl_ctx.opt_init_flag) {
626 av_opt_set_defaults(&opencl_ctx);
627 opencl_ctx.opt_init_flag = 1;
629 ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
632 if (opencl_ctx.kernel_code_count <= 0) {
633 av_log(&opencl_ctx, AV_LOG_ERROR,
634 "No kernel code is registered, compile kernel file failed\n");
635 ret = AVERROR(EINVAL);
639 opencl_ctx.init_count++;
645 void av_opencl_uninit(void)
650 opencl_ctx.init_count--;
651 if (opencl_ctx.is_user_created)
653 if (opencl_ctx.init_count > 0)
655 if (opencl_ctx.command_queue) {
656 status = clReleaseCommandQueue(opencl_ctx.command_queue);
657 if (status != CL_SUCCESS) {
658 av_log(&opencl_ctx, AV_LOG_ERROR,
659 "Could not release OpenCL command queue: %s\n", av_opencl_errstr(status));
661 opencl_ctx.command_queue = NULL;
663 if (opencl_ctx.context) {
664 status = clReleaseContext(opencl_ctx.context);
665 if (status != CL_SUCCESS) {
666 av_log(&opencl_ctx, AV_LOG_ERROR,
667 "Could not release OpenCL context: %s\n", av_opencl_errstr(status));
669 opencl_ctx.context = NULL;
671 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
672 opencl_ctx.kernel_code[i].is_compiled = 0;
674 free_device_list(&opencl_ctx.device_list);
676 if (opencl_ctx.init_count <= 0)
677 av_opt_free(&opencl_ctx); //FIXME: free openclutils context
681 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
684 *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
685 if (status != CL_SUCCESS) {
686 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", av_opencl_errstr(status));
687 return AVERROR_EXTERNAL;
692 void av_opencl_buffer_release(cl_mem *cl_buf)
697 status = clReleaseMemObject(*cl_buf);
698 if (status != CL_SUCCESS) {
699 av_log(&opencl_ctx, AV_LOG_ERROR,
700 "Could not release OpenCL buffer: %s\n", av_opencl_errstr(status));
702 memset(cl_buf, 0, sizeof(*cl_buf));
705 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
708 void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
709 CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
710 0, NULL, NULL, &status);
712 if (status != CL_SUCCESS) {
713 av_log(&opencl_ctx, AV_LOG_ERROR,
714 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
715 return AVERROR_EXTERNAL;
717 memcpy(mapped, src_buf, buf_size);
719 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
720 if (status != CL_SUCCESS) {
721 av_log(&opencl_ctx, AV_LOG_ERROR,
722 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
723 return AVERROR_EXTERNAL;
728 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
731 void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
732 CL_TRUE, CL_MAP_READ, 0, buf_size,
733 0, NULL, NULL, &status);
735 if (status != CL_SUCCESS) {
736 av_log(&opencl_ctx, AV_LOG_ERROR,
737 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
738 return AVERROR_EXTERNAL;
740 memcpy(dst_buf, mapped, buf_size);
742 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
743 if (status != CL_SUCCESS) {
744 av_log(&opencl_ctx, AV_LOG_ERROR,
745 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
746 return AVERROR_EXTERNAL;
751 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
752 uint8_t **src_data, int *plane_size, int plane_num)
754 int i, buffer_size = 0;
758 if ((unsigned int)plane_num > 8) {
759 return AVERROR(EINVAL);
761 for (i = 0;i < plane_num;i++) {
762 buffer_size += plane_size[i];
764 if (buffer_size > cl_buffer_size) {
765 av_log(&opencl_ctx, AV_LOG_ERROR,
766 "Cannot write image to OpenCL buffer: buffer too small\n");
767 return AVERROR(EINVAL);
769 mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
770 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
771 0, NULL, NULL, &status);
772 if (status != CL_SUCCESS) {
773 av_log(&opencl_ctx, AV_LOG_ERROR,
774 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
775 return AVERROR_EXTERNAL;
778 temp += dst_cl_offset;
779 for (i = 0; i < plane_num; i++) {
780 memcpy(temp, src_data[i], plane_size[i]);
781 temp += plane_size[i];
783 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
784 if (status != CL_SUCCESS) {
785 av_log(&opencl_ctx, AV_LOG_ERROR,
786 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
787 return AVERROR_EXTERNAL;
792 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
793 cl_mem src_cl_buf, size_t cl_buffer_size)
795 int i,buffer_size = 0,ret = 0;
799 if ((unsigned int)plane_num > 8) {
800 return AVERROR(EINVAL);
802 for (i = 0; i < plane_num; i++) {
803 buffer_size += plane_size[i];
805 if (buffer_size > cl_buffer_size) {
806 av_log(&opencl_ctx, AV_LOG_ERROR,
807 "Cannot write image to CPU buffer: OpenCL buffer too small\n");
808 return AVERROR(EINVAL);
810 mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
811 CL_TRUE, CL_MAP_READ, 0, buffer_size,
812 0, NULL, NULL, &status);
814 if (status != CL_SUCCESS) {
815 av_log(&opencl_ctx, AV_LOG_ERROR,
816 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
817 return AVERROR_EXTERNAL;
821 for (i = 0; i < plane_num; i++) {
822 memcpy(dst_data[i], temp, plane_size[i]);
823 temp += plane_size[i];
826 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
827 if (status != CL_SUCCESS) {
828 av_log(&opencl_ctx, AV_LOG_ERROR,
829 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
830 return AVERROR_EXTERNAL;
835 int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
836 int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
840 cl_context_properties cps[3];
841 AVOpenCLExternalEnv *ext_opencl_env = NULL;
843 ext_opencl_env = av_opencl_alloc_external_env();
844 ext_opencl_env->device_id = device_node->device_id;
845 ext_opencl_env->device_type = device_node->device_type;
846 av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
847 device_node->device_name);
849 cps[0] = CL_CONTEXT_PLATFORM;
850 cps[1] = (cl_context_properties)platform;
852 ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
853 NULL, NULL, &status);
854 if (status != CL_SUCCESS || !ext_opencl_env->context) {
855 ret = AVERROR_EXTERNAL;
858 ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
859 ext_opencl_env->device_id, 0, &status);
860 if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
861 ret = AVERROR_EXTERNAL;
864 ret = benchmark(ext_opencl_env);
866 av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
867 device_node->device_name);
869 if (ext_opencl_env->command_queue)
870 clReleaseCommandQueue(ext_opencl_env->command_queue);
871 if (ext_opencl_env->context)
872 clReleaseContext(ext_opencl_env->context);
873 av_opencl_free_external_env(&ext_opencl_env);