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 #include "compat/w32pthreads.h"
36 #include "compat/os2threads.h"
40 static volatile pthread_mutex_t *atomic_opencl_lock = NULL;
41 #define LOCK_OPENCL pthread_mutex_lock(atomic_opencl_lock)
42 #define UNLOCK_OPENCL pthread_mutex_unlock(atomic_opencl_lock)
48 #define MAX_KERNEL_CODE_NUM 200
52 const char *kernel_string;
62 * if set to 1, the OpenCL environment was created by the user and
63 * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
68 cl_platform_id platform_id;
69 cl_device_type device_type;
71 cl_device_id device_id;
72 cl_command_queue command_queue;
73 int kernel_code_count;
74 KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
75 AVOpenCLDeviceList device_list;
78 #define OFFSET(x) offsetof(OpenclContext, x)
80 static const AVOption opencl_options[] = {
81 { "platform_idx", "set platform index value", OFFSET(platform_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
82 { "device_idx", "set device index value", OFFSET(device_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
86 static const AVClass openclutils_class = {
87 .class_name = "OPENCLUTILS",
88 .option = opencl_options,
89 .item_name = av_default_item_name,
90 .version = LIBAVUTIL_VERSION_INT,
91 .log_level_offset_offset = offsetof(OpenclContext, log_offset),
92 .parent_log_context_offset = offsetof(OpenclContext, log_ctx),
95 static OpenclContext opencl_ctx = {&openclutils_class};
97 static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU};
104 static const OpenclErrorMsg opencl_err_msg[] = {
105 {CL_DEVICE_NOT_FOUND, "DEVICE NOT FOUND"},
106 {CL_DEVICE_NOT_AVAILABLE, "DEVICE NOT AVAILABLE"},
107 {CL_COMPILER_NOT_AVAILABLE, "COMPILER NOT AVAILABLE"},
108 {CL_MEM_OBJECT_ALLOCATION_FAILURE, "MEM OBJECT ALLOCATION FAILURE"},
109 {CL_OUT_OF_RESOURCES, "OUT OF RESOURCES"},
110 {CL_OUT_OF_HOST_MEMORY, "OUT OF HOST MEMORY"},
111 {CL_PROFILING_INFO_NOT_AVAILABLE, "PROFILING INFO NOT AVAILABLE"},
112 {CL_MEM_COPY_OVERLAP, "MEM COPY OVERLAP"},
113 {CL_IMAGE_FORMAT_MISMATCH, "IMAGE FORMAT MISMATCH"},
114 {CL_IMAGE_FORMAT_NOT_SUPPORTED, "IMAGE FORMAT NOT_SUPPORTED"},
115 {CL_BUILD_PROGRAM_FAILURE, "BUILD PROGRAM FAILURE"},
116 {CL_MAP_FAILURE, "MAP FAILURE"},
117 {CL_MISALIGNED_SUB_BUFFER_OFFSET, "MISALIGNED SUB BUFFER OFFSET"},
118 {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST, "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
119 {CL_COMPILE_PROGRAM_FAILURE, "COMPILE PROGRAM FAILURE"},
120 {CL_LINKER_NOT_AVAILABLE, "LINKER NOT AVAILABLE"},
121 {CL_LINK_PROGRAM_FAILURE, "LINK PROGRAM FAILURE"},
122 {CL_DEVICE_PARTITION_FAILED, "DEVICE PARTITION FAILED"},
123 {CL_KERNEL_ARG_INFO_NOT_AVAILABLE, "KERNEL ARG INFO NOT AVAILABLE"},
124 {CL_INVALID_VALUE, "INVALID VALUE"},
125 {CL_INVALID_DEVICE_TYPE, "INVALID DEVICE TYPE"},
126 {CL_INVALID_PLATFORM, "INVALID PLATFORM"},
127 {CL_INVALID_DEVICE, "INVALID DEVICE"},
128 {CL_INVALID_CONTEXT, "INVALID CONTEXT"},
129 {CL_INVALID_QUEUE_PROPERTIES, "INVALID QUEUE PROPERTIES"},
130 {CL_INVALID_COMMAND_QUEUE, "INVALID COMMAND QUEUE"},
131 {CL_INVALID_HOST_PTR, "INVALID HOST PTR"},
132 {CL_INVALID_MEM_OBJECT, "INVALID MEM OBJECT"},
133 {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, "INVALID IMAGE FORMAT DESCRIPTOR"},
134 {CL_INVALID_IMAGE_SIZE, "INVALID IMAGE SIZE"},
135 {CL_INVALID_SAMPLER, "INVALID SAMPLER"},
136 {CL_INVALID_BINARY, "INVALID BINARY"},
137 {CL_INVALID_BUILD_OPTIONS, "INVALID BUILD OPTIONS"},
138 {CL_INVALID_PROGRAM, "INVALID PROGRAM"},
139 {CL_INVALID_PROGRAM_EXECUTABLE, "INVALID PROGRAM EXECUTABLE"},
140 {CL_INVALID_KERNEL_NAME, "INVALID KERNEL NAME"},
141 {CL_INVALID_KERNEL_DEFINITION, "INVALID KERNEL DEFINITION"},
142 {CL_INVALID_KERNEL, "INVALID KERNEL"},
143 {CL_INVALID_ARG_INDEX, "INVALID ARG INDEX"},
144 {CL_INVALID_ARG_VALUE, "INVALID ARG VALUE"},
145 {CL_INVALID_ARG_SIZE, "INVALID ARG_SIZE"},
146 {CL_INVALID_KERNEL_ARGS, "INVALID KERNEL ARGS"},
147 {CL_INVALID_WORK_DIMENSION, "INVALID WORK DIMENSION"},
148 {CL_INVALID_WORK_GROUP_SIZE, "INVALID WORK GROUP SIZE"},
149 {CL_INVALID_WORK_ITEM_SIZE, "INVALID WORK ITEM SIZE"},
150 {CL_INVALID_GLOBAL_OFFSET, "INVALID GLOBAL OFFSET"},
151 {CL_INVALID_EVENT_WAIT_LIST, "INVALID EVENT WAIT LIST"},
152 {CL_INVALID_EVENT, "INVALID EVENT"},
153 {CL_INVALID_OPERATION, "INVALID OPERATION"},
154 {CL_INVALID_GL_OBJECT, "INVALID GL OBJECT"},
155 {CL_INVALID_BUFFER_SIZE, "INVALID BUFFER SIZE"},
156 {CL_INVALID_MIP_LEVEL, "INVALID MIP LEVEL"},
157 {CL_INVALID_GLOBAL_WORK_SIZE, "INVALID GLOBAL WORK SIZE"},
158 {CL_INVALID_PROPERTY, "INVALID PROPERTY"},
159 {CL_INVALID_IMAGE_DESCRIPTOR, "INVALID IMAGE DESCRIPTOR"},
160 {CL_INVALID_COMPILER_OPTIONS, "INVALID COMPILER OPTIONS"},
161 {CL_INVALID_LINKER_OPTIONS, "INVALID LINKER OPTIONS"},
162 {CL_INVALID_DEVICE_PARTITION_COUNT, "INVALID DEVICE PARTITION COUNT"},
165 const char *av_opencl_errstr(cl_int status)
168 for (i = 0; i < FF_ARRAY_ELEMS(opencl_err_msg); i++) {
169 if (opencl_err_msg[i].err_code == status)
170 return opencl_err_msg[i].err_str;
172 return "unknown error";
175 static void free_device_list(AVOpenCLDeviceList *device_list)
180 for (i = 0; i < device_list->platform_num; i++) {
181 if (!device_list->platform_node[i])
183 for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
184 av_freep(&(device_list->platform_node[i]->device_node[j]->device_name));
185 av_freep(&(device_list->platform_node[i]->device_node[j]));
187 av_freep(&device_list->platform_node[i]->device_node);
188 av_freep(&(device_list->platform_node[i]->platform_name));
189 av_freep(&device_list->platform_node[i]);
191 av_freep(&device_list->platform_node);
192 device_list->platform_num = 0;
195 static int get_device_list(AVOpenCLDeviceList *device_list)
198 int i, j, k, device_num, total_devices_num, ret = 0;
200 cl_platform_id *platform_ids = NULL;
201 cl_device_id *device_ids = NULL;
202 AVOpenCLDeviceNode *device_node = NULL;
203 size_t platform_name_size = 0;
204 size_t device_name_size = 0;
205 status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
206 if (status != CL_SUCCESS) {
207 av_log(&opencl_ctx, AV_LOG_ERROR,
208 "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
209 return AVERROR_EXTERNAL;
211 platform_ids = av_mallocz_array(device_list->platform_num, sizeof(cl_platform_id));
213 return AVERROR(ENOMEM);
214 status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
215 if (status != CL_SUCCESS) {
216 av_log(&opencl_ctx, AV_LOG_ERROR,
217 "Could not get OpenCL platform ids: %s\n", av_opencl_errstr(status));
218 ret = AVERROR_EXTERNAL;
221 device_list->platform_node = av_mallocz_array(device_list->platform_num, sizeof(AVOpenCLPlatformNode *));
222 if (!device_list->platform_node) {
223 ret = AVERROR(ENOMEM);
226 devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
228 ret = AVERROR(ENOMEM);
231 for (i = 0; i < device_list->platform_num; i++) {
232 device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
233 if (!device_list->platform_node[i]) {
234 ret = AVERROR(ENOMEM);
237 device_list->platform_node[i]->platform_id = platform_ids[i];
238 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
239 0, NULL, &platform_name_size);
240 if (status != CL_SUCCESS) {
241 av_log(&opencl_ctx, AV_LOG_WARNING,
242 "Could not get size of platform name: %s\n", av_opencl_errstr(status));
244 device_list->platform_node[i]->platform_name = av_malloc(platform_name_size * sizeof(char));
245 if (!device_list->platform_node[i]->platform_name) {
246 av_log(&opencl_ctx, AV_LOG_WARNING,
247 "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
249 status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
250 platform_name_size * sizeof(char),
251 device_list->platform_node[i]->platform_name, NULL);
252 if (status != CL_SUCCESS) {
253 av_log(&opencl_ctx, AV_LOG_WARNING,
254 "Could not get platform name: %s\n", av_opencl_errstr(status));
258 total_devices_num = 0;
259 for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
260 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
261 device_type[j], 0, NULL, &devices_num[j]);
262 total_devices_num += devices_num[j];
264 device_list->platform_node[i]->device_node = av_mallocz_array(total_devices_num, sizeof(AVOpenCLDeviceNode *));
265 if (!device_list->platform_node[i]->device_node) {
266 ret = AVERROR(ENOMEM);
269 for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
270 if (devices_num[j]) {
271 device_ids = av_mallocz_array(devices_num[j], sizeof(cl_device_id));
273 ret = AVERROR(ENOMEM);
276 status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
277 devices_num[j], device_ids, NULL);
278 if (status != CL_SUCCESS) {
279 av_log(&opencl_ctx, AV_LOG_WARNING,
280 "Could not get device ID: %s:\n", av_opencl_errstr(status));
281 av_freep(&device_ids);
284 for (k = 0; k < devices_num[j]; k++) {
285 device_num = device_list->platform_node[i]->device_num;
286 device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
287 if (!device_list->platform_node[i]->device_node[device_num]) {
288 ret = AVERROR(ENOMEM);
291 device_node = device_list->platform_node[i]->device_node[device_num];
292 device_node->device_id = device_ids[k];
293 device_node->device_type = device_type[j];
294 status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
295 0, NULL, &device_name_size);
296 if (status != CL_SUCCESS) {
297 av_log(&opencl_ctx, AV_LOG_WARNING,
298 "Could not get size of device name: %s\n", av_opencl_errstr(status));
301 device_node->device_name = av_malloc(device_name_size * sizeof(char));
302 if (!device_node->device_name) {
303 av_log(&opencl_ctx, AV_LOG_WARNING,
304 "Could not allocate memory for device name: %s\n", av_opencl_errstr(status));
307 status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
308 device_name_size * sizeof(char),
309 device_node->device_name, NULL);
310 if (status != CL_SUCCESS) {
311 av_log(&opencl_ctx, AV_LOG_WARNING,
312 "Could not get device name: %s\n", av_opencl_errstr(status));
315 device_list->platform_node[i]->device_num++;
317 av_freep(&device_ids);
322 av_freep(&platform_ids);
323 av_freep(&devices_num);
324 av_freep(&device_ids);
326 free_device_list(device_list);
330 int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
333 *device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
334 if (!(*device_list)) {
335 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n");
336 return AVERROR(ENOMEM);
338 ret = get_device_list(*device_list);
340 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n");
341 free_device_list(*device_list);
342 av_freep(device_list);
348 void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
350 free_device_list(*device_list);
351 av_freep(device_list);
354 static inline int init_opencl_mtx(void)
357 if (!atomic_opencl_lock) {
359 pthread_mutex_t *tmp = av_malloc(sizeof(pthread_mutex_t));
361 return AVERROR(ENOMEM);
362 if ((err = pthread_mutex_init(tmp, NULL))) {
366 if (avpriv_atomic_ptr_cas(&atomic_opencl_lock, NULL, tmp)) {
367 pthread_mutex_destroy(tmp);
375 int av_opencl_set_option(const char *key, const char *val)
377 int ret = init_opencl_mtx( );
381 if (!opencl_ctx.opt_init_flag) {
382 av_opt_set_defaults(&opencl_ctx);
383 opencl_ctx.opt_init_flag = 1;
385 ret = av_opt_set(&opencl_ctx, key, val, 0);
390 int av_opencl_get_option(const char *key, uint8_t **out_val)
394 ret = av_opt_get(&opencl_ctx, key, 0, out_val);
399 void av_opencl_free_option(void)
401 /*FIXME: free openclutils context*/
403 av_opt_free(&opencl_ctx);
407 AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
409 AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
411 av_log(&opencl_ctx, AV_LOG_ERROR,
412 "Could not malloc external opencl environment data space\n");
417 void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
419 av_freep(ext_opencl_env);
422 int av_opencl_register_kernel_code(const char *kernel_code)
424 int i, ret = init_opencl_mtx( );
428 if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
429 av_log(&opencl_ctx, AV_LOG_ERROR,
430 "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
431 MAX_KERNEL_CODE_NUM);
432 ret = AVERROR(EINVAL);
435 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
436 if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) {
437 av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n");
441 opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code;
442 opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0;
443 opencl_ctx.kernel_code_count++;
449 cl_program av_opencl_compile(const char *program_name, const char *build_opts)
453 int kernel_code_idx = 0;
454 const char *kernel_source;
455 size_t kernel_code_len;
457 cl_program program = NULL;
460 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
461 // identify a program using a unique name within the kernel source
462 ptr = av_stristr(opencl_ctx.kernel_code[i].kernel_string, program_name);
463 if (ptr && !opencl_ctx.kernel_code[i].is_compiled) {
464 kernel_source = opencl_ctx.kernel_code[i].kernel_string;
465 kernel_code_len = strlen(opencl_ctx.kernel_code[i].kernel_string);
470 if (!kernel_source) {
471 av_log(&opencl_ctx, AV_LOG_ERROR,
472 "Unable to find OpenCL kernel source '%s'\n", program_name);
476 /* create a CL program from kernel source */
477 program = clCreateProgramWithSource(opencl_ctx.context, 1, &kernel_source, &kernel_code_len, &status);
478 if(status != CL_SUCCESS) {
479 av_log(&opencl_ctx, AV_LOG_ERROR,
480 "Unable to create OpenCL program '%s': %s\n", program_name, av_opencl_errstr(status));
484 status = clBuildProgram(program, 1, &(opencl_ctx.device_id), build_opts, NULL, NULL);
485 if (status != CL_SUCCESS) {
486 av_log(&opencl_ctx, AV_LOG_ERROR,
487 "Compilation failed with OpenCL program: %s\n", program_name);
492 opencl_ctx.kernel_code[kernel_code_idx].is_compiled = 1;
498 cl_command_queue av_opencl_get_command_queue(void)
500 return opencl_ctx.command_queue;
503 static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
506 cl_context_properties cps[3];
508 AVOpenCLDeviceNode *device_node = NULL;
510 if (ext_opencl_env) {
511 if (opencl_ctx->is_user_created)
513 opencl_ctx->platform_id = ext_opencl_env->platform_id;
514 opencl_ctx->is_user_created = 1;
515 opencl_ctx->command_queue = ext_opencl_env->command_queue;
516 opencl_ctx->context = ext_opencl_env->context;
517 opencl_ctx->device_id = ext_opencl_env->device_id;
518 opencl_ctx->device_type = ext_opencl_env->device_type;
520 if (!opencl_ctx->is_user_created) {
521 if (!opencl_ctx->device_list.platform_num) {
522 ret = get_device_list(&opencl_ctx->device_list);
527 if (opencl_ctx->platform_idx >= 0) {
528 if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
529 av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
530 return AVERROR(EINVAL);
532 if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
533 av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
534 opencl_ctx->platform_idx);
535 return AVERROR(EINVAL);
537 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
539 /* get a usable platform by default*/
540 for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
541 if (opencl_ctx->device_list.platform_node[i]->device_num) {
542 opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
543 opencl_ctx->platform_idx = i;
548 if (!opencl_ctx->platform_id) {
549 av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
550 return AVERROR_EXTERNAL;
552 /* get a usable device*/
553 if (opencl_ctx->device_idx >= 0) {
554 if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
555 av_log(opencl_ctx, AV_LOG_ERROR,
556 "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
557 return AVERROR(EINVAL);
560 opencl_ctx->device_idx = 0;
563 device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
564 opencl_ctx->device_id = device_node->device_id;
565 opencl_ctx->device_type = device_node->device_type;
568 * Use available platform.
570 av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, Device Name: %s\n",
571 opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
572 device_node->device_name);
573 cps[0] = CL_CONTEXT_PLATFORM;
574 cps[1] = (cl_context_properties)opencl_ctx->platform_id;
577 opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
578 NULL, NULL, &status);
579 if (status != CL_SUCCESS) {
580 av_log(opencl_ctx, AV_LOG_ERROR,
581 "Could not get OpenCL context from device type: %s\n", av_opencl_errstr(status));
582 return AVERROR_EXTERNAL;
584 opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
586 if (status != CL_SUCCESS) {
587 av_log(opencl_ctx, AV_LOG_ERROR,
588 "Could not create OpenCL command queue: %s\n", av_opencl_errstr(status));
589 return AVERROR_EXTERNAL;
596 int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
598 int ret = init_opencl_mtx( );
602 if (!opencl_ctx.init_count) {
603 if (!opencl_ctx.opt_init_flag) {
604 av_opt_set_defaults(&opencl_ctx);
605 opencl_ctx.opt_init_flag = 1;
607 ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
610 if (opencl_ctx.kernel_code_count <= 0) {
611 av_log(&opencl_ctx, AV_LOG_ERROR,
612 "No kernel code is registered, compile kernel file failed\n");
613 ret = AVERROR(EINVAL);
617 opencl_ctx.init_count++;
623 void av_opencl_uninit(void)
628 opencl_ctx.init_count--;
629 if (opencl_ctx.is_user_created)
631 if (opencl_ctx.init_count > 0)
633 if (opencl_ctx.command_queue) {
634 status = clReleaseCommandQueue(opencl_ctx.command_queue);
635 if (status != CL_SUCCESS) {
636 av_log(&opencl_ctx, AV_LOG_ERROR,
637 "Could not release OpenCL command queue: %s\n", av_opencl_errstr(status));
639 opencl_ctx.command_queue = NULL;
641 if (opencl_ctx.context) {
642 status = clReleaseContext(opencl_ctx.context);
643 if (status != CL_SUCCESS) {
644 av_log(&opencl_ctx, AV_LOG_ERROR,
645 "Could not release OpenCL context: %s\n", av_opencl_errstr(status));
647 opencl_ctx.context = NULL;
649 for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
650 opencl_ctx.kernel_code[i].is_compiled = 0;
652 free_device_list(&opencl_ctx.device_list);
654 if (opencl_ctx.init_count <= 0)
655 av_opt_free(&opencl_ctx); //FIXME: free openclutils context
659 int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
662 *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
663 if (status != CL_SUCCESS) {
664 av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", av_opencl_errstr(status));
665 return AVERROR_EXTERNAL;
670 void av_opencl_buffer_release(cl_mem *cl_buf)
675 status = clReleaseMemObject(*cl_buf);
676 if (status != CL_SUCCESS) {
677 av_log(&opencl_ctx, AV_LOG_ERROR,
678 "Could not release OpenCL buffer: %s\n", av_opencl_errstr(status));
680 memset(cl_buf, 0, sizeof(*cl_buf));
683 int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
686 void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
687 CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
688 0, NULL, NULL, &status);
690 if (status != CL_SUCCESS) {
691 av_log(&opencl_ctx, AV_LOG_ERROR,
692 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
693 return AVERROR_EXTERNAL;
695 memcpy(mapped, src_buf, buf_size);
697 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
698 if (status != CL_SUCCESS) {
699 av_log(&opencl_ctx, AV_LOG_ERROR,
700 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
701 return AVERROR_EXTERNAL;
706 int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
709 void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
710 CL_TRUE, CL_MAP_READ, 0, buf_size,
711 0, NULL, NULL, &status);
713 if (status != CL_SUCCESS) {
714 av_log(&opencl_ctx, AV_LOG_ERROR,
715 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
716 return AVERROR_EXTERNAL;
718 memcpy(dst_buf, mapped, buf_size);
720 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
721 if (status != CL_SUCCESS) {
722 av_log(&opencl_ctx, AV_LOG_ERROR,
723 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
724 return AVERROR_EXTERNAL;
729 int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
730 uint8_t **src_data, int *plane_size, int plane_num)
732 int i, buffer_size = 0;
736 if ((unsigned int)plane_num > 8) {
737 return AVERROR(EINVAL);
739 for (i = 0;i < plane_num;i++) {
740 buffer_size += plane_size[i];
742 if (buffer_size > cl_buffer_size) {
743 av_log(&opencl_ctx, AV_LOG_ERROR,
744 "Cannot write image to OpenCL buffer: buffer too small\n");
745 return AVERROR(EINVAL);
747 mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
748 CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
749 0, NULL, NULL, &status);
750 if (status != CL_SUCCESS) {
751 av_log(&opencl_ctx, AV_LOG_ERROR,
752 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
753 return AVERROR_EXTERNAL;
756 temp += dst_cl_offset;
757 for (i = 0; i < plane_num; i++) {
758 memcpy(temp, src_data[i], plane_size[i]);
759 temp += plane_size[i];
761 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
762 if (status != CL_SUCCESS) {
763 av_log(&opencl_ctx, AV_LOG_ERROR,
764 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
765 return AVERROR_EXTERNAL;
770 int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
771 cl_mem src_cl_buf, size_t cl_buffer_size)
773 int i,buffer_size = 0,ret = 0;
777 if ((unsigned int)plane_num > 8) {
778 return AVERROR(EINVAL);
780 for (i = 0; i < plane_num; i++) {
781 buffer_size += plane_size[i];
783 if (buffer_size > cl_buffer_size) {
784 av_log(&opencl_ctx, AV_LOG_ERROR,
785 "Cannot write image to CPU buffer: OpenCL buffer too small\n");
786 return AVERROR(EINVAL);
788 mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
789 CL_TRUE, CL_MAP_READ, 0, buffer_size,
790 0, NULL, NULL, &status);
792 if (status != CL_SUCCESS) {
793 av_log(&opencl_ctx, AV_LOG_ERROR,
794 "Could not map OpenCL buffer: %s\n", av_opencl_errstr(status));
795 return AVERROR_EXTERNAL;
799 for (i = 0; i < plane_num; i++) {
800 memcpy(dst_data[i], temp, plane_size[i]);
801 temp += plane_size[i];
804 status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
805 if (status != CL_SUCCESS) {
806 av_log(&opencl_ctx, AV_LOG_ERROR,
807 "Could not unmap OpenCL buffer: %s\n", av_opencl_errstr(status));
808 return AVERROR_EXTERNAL;
813 int64_t av_opencl_benchmark(AVOpenCLDeviceNode *device_node, cl_platform_id platform,
814 int64_t (*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
818 cl_context_properties cps[3];
819 AVOpenCLExternalEnv *ext_opencl_env = NULL;
821 ext_opencl_env = av_opencl_alloc_external_env();
822 ext_opencl_env->device_id = device_node->device_id;
823 ext_opencl_env->device_type = device_node->device_type;
824 av_log(&opencl_ctx, AV_LOG_VERBOSE, "Performing test on OpenCL device %s\n",
825 device_node->device_name);
827 cps[0] = CL_CONTEXT_PLATFORM;
828 cps[1] = (cl_context_properties)platform;
830 ext_opencl_env->context = clCreateContextFromType(cps, ext_opencl_env->device_type,
831 NULL, NULL, &status);
832 if (status != CL_SUCCESS || !ext_opencl_env->context) {
833 ret = AVERROR_EXTERNAL;
836 ext_opencl_env->command_queue = clCreateCommandQueue(ext_opencl_env->context,
837 ext_opencl_env->device_id, 0, &status);
838 if (status != CL_SUCCESS || !ext_opencl_env->command_queue) {
839 ret = AVERROR_EXTERNAL;
842 ret = benchmark(ext_opencl_env);
844 av_log(&opencl_ctx, AV_LOG_ERROR, "Benchmark failed with OpenCL device %s\n",
845 device_node->device_name);
847 if (ext_opencl_env->command_queue)
848 clReleaseCommandQueue(ext_opencl_env->command_queue);
849 if (ext_opencl_env->context)
850 clReleaseContext(ext_opencl_env->context);
851 av_opencl_free_external_env(&ext_opencl_env);