yading@11: /* yading@11: * Copyright (C) 2012 Peng Gao yading@11: * Copyright (C) 2012 Li Cao yading@11: * Copyright (C) 2012 Wei Gao yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "opencl.h" yading@11: #include "avstring.h" yading@11: #include "log.h" yading@11: #include "avassert.h" yading@11: #include "opt.h" yading@11: yading@11: #if HAVE_PTHREADS yading@11: yading@11: #include yading@11: static pthread_mutex_t atomic_opencl_lock = PTHREAD_MUTEX_INITIALIZER; yading@11: yading@11: #define LOCK_OPENCL pthread_mutex_lock(&atomic_opencl_lock); yading@11: #define UNLOCK_OPENCL pthread_mutex_unlock(&atomic_opencl_lock); yading@11: yading@11: #elif !HAVE_THREADS yading@11: #define LOCK_OPENCL yading@11: #define UNLOCK_OPENCL yading@11: #endif yading@11: yading@11: yading@11: #define MAX_KERNEL_NUM 500 yading@11: #define MAX_KERNEL_CODE_NUM 200 yading@11: yading@11: typedef struct { yading@11: int is_compiled; yading@11: const char *kernel_string; yading@11: } KernelCode; yading@11: yading@11: typedef struct { yading@11: const AVClass *class; yading@11: int log_offset; yading@11: void *log_ctx; yading@11: int init_count; yading@11: int opt_init_flag; yading@11: /** yading@11: * if set to 1, the OpenCL environment was created by the user and yading@11: * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper. yading@11: */ yading@11: int is_user_created; yading@11: int platform_idx; yading@11: int device_idx; yading@11: char *build_options; yading@11: cl_platform_id platform_id; yading@11: cl_device_type device_type; yading@11: cl_context context; yading@11: cl_device_id device_id; yading@11: cl_command_queue command_queue; yading@11: int program_count; yading@11: cl_program programs[MAX_KERNEL_CODE_NUM]; yading@11: int kernel_code_count; yading@11: KernelCode kernel_code[MAX_KERNEL_CODE_NUM]; yading@11: int kernel_count; yading@11: AVOpenCLDeviceList device_list; yading@11: } OpenclContext; yading@11: yading@11: #define OFFSET(x) offsetof(OpenclContext, x) yading@11: yading@11: static const AVOption opencl_options[] = { yading@11: { "platform_idx", "set platform index value", OFFSET(platform_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX}, yading@11: { "device_idx", "set device index value", OFFSET(device_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX}, yading@11: { "build_options", "build options of opencl", OFFSET(build_options), AV_OPT_TYPE_STRING, {.str="-I."}, CHAR_MIN, CHAR_MAX}, yading@11: }; yading@11: yading@11: static const AVClass openclutils_class = { yading@11: .class_name = "OPENCLUTILS", yading@11: .option = opencl_options, yading@11: .item_name = av_default_item_name, yading@11: .version = LIBAVUTIL_VERSION_INT, yading@11: .log_level_offset_offset = offsetof(OpenclContext, log_offset), yading@11: .parent_log_context_offset = offsetof(OpenclContext, log_ctx), yading@11: }; yading@11: yading@11: static OpenclContext opencl_ctx = {&openclutils_class}; yading@11: yading@11: static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT}; yading@11: yading@11: typedef struct { yading@11: int err_code; yading@11: const char *err_str; yading@11: } OpenclErrorMsg; yading@11: yading@11: static const OpenclErrorMsg opencl_err_msg[] = { yading@11: {CL_DEVICE_NOT_FOUND, "DEVICE NOT FOUND"}, yading@11: {CL_DEVICE_NOT_AVAILABLE, "DEVICE NOT AVAILABLE"}, yading@11: {CL_COMPILER_NOT_AVAILABLE, "COMPILER NOT AVAILABLE"}, yading@11: {CL_MEM_OBJECT_ALLOCATION_FAILURE, "MEM OBJECT ALLOCATION FAILURE"}, yading@11: {CL_OUT_OF_RESOURCES, "OUT OF RESOURCES"}, yading@11: {CL_OUT_OF_HOST_MEMORY, "OUT OF HOST MEMORY"}, yading@11: {CL_PROFILING_INFO_NOT_AVAILABLE, "PROFILING INFO NOT AVAILABLE"}, yading@11: {CL_MEM_COPY_OVERLAP, "MEM COPY OVERLAP"}, yading@11: {CL_IMAGE_FORMAT_MISMATCH, "IMAGE FORMAT MISMATCH"}, yading@11: {CL_IMAGE_FORMAT_NOT_SUPPORTED, "IMAGE FORMAT NOT_SUPPORTED"}, yading@11: {CL_BUILD_PROGRAM_FAILURE, "BUILD PROGRAM FAILURE"}, yading@11: {CL_MAP_FAILURE, "MAP FAILURE"}, yading@11: {CL_MISALIGNED_SUB_BUFFER_OFFSET, "MISALIGNED SUB BUFFER OFFSET"}, yading@11: {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST, "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"}, yading@11: {CL_COMPILE_PROGRAM_FAILURE, "COMPILE PROGRAM FAILURE"}, yading@11: {CL_LINKER_NOT_AVAILABLE, "LINKER NOT AVAILABLE"}, yading@11: {CL_LINK_PROGRAM_FAILURE, "LINK PROGRAM FAILURE"}, yading@11: {CL_DEVICE_PARTITION_FAILED, "DEVICE PARTITION FAILED"}, yading@11: {CL_KERNEL_ARG_INFO_NOT_AVAILABLE, "KERNEL ARG INFO NOT AVAILABLE"}, yading@11: {CL_INVALID_VALUE, "INVALID VALUE"}, yading@11: {CL_INVALID_DEVICE_TYPE, "INVALID DEVICE TYPE"}, yading@11: {CL_INVALID_PLATFORM, "INVALID PLATFORM"}, yading@11: {CL_INVALID_DEVICE, "INVALID DEVICE"}, yading@11: {CL_INVALID_CONTEXT, "INVALID CONTEXT"}, yading@11: {CL_INVALID_QUEUE_PROPERTIES, "INVALID QUEUE PROPERTIES"}, yading@11: {CL_INVALID_COMMAND_QUEUE, "INVALID COMMAND QUEUE"}, yading@11: {CL_INVALID_HOST_PTR, "INVALID HOST PTR"}, yading@11: {CL_INVALID_MEM_OBJECT, "INVALID MEM OBJECT"}, yading@11: {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, "INVALID IMAGE FORMAT DESCRIPTOR"}, yading@11: {CL_INVALID_IMAGE_SIZE, "INVALID IMAGE SIZE"}, yading@11: {CL_INVALID_SAMPLER, "INVALID SAMPLER"}, yading@11: {CL_INVALID_BINARY, "INVALID BINARY"}, yading@11: {CL_INVALID_BUILD_OPTIONS, "INVALID BUILD OPTIONS"}, yading@11: {CL_INVALID_PROGRAM, "INVALID PROGRAM"}, yading@11: {CL_INVALID_PROGRAM_EXECUTABLE, "INVALID PROGRAM EXECUTABLE"}, yading@11: {CL_INVALID_KERNEL_NAME, "INVALID KERNEL NAME"}, yading@11: {CL_INVALID_KERNEL_DEFINITION, "INVALID KERNEL DEFINITION"}, yading@11: {CL_INVALID_KERNEL, "INVALID KERNEL"}, yading@11: {CL_INVALID_ARG_INDEX, "INVALID ARG INDEX"}, yading@11: {CL_INVALID_ARG_VALUE, "INVALID ARG VALUE"}, yading@11: {CL_INVALID_ARG_SIZE, "INVALID ARG_SIZE"}, yading@11: {CL_INVALID_KERNEL_ARGS, "INVALID KERNEL ARGS"}, yading@11: {CL_INVALID_WORK_DIMENSION, "INVALID WORK DIMENSION"}, yading@11: {CL_INVALID_WORK_GROUP_SIZE, "INVALID WORK GROUP SIZE"}, yading@11: {CL_INVALID_WORK_ITEM_SIZE, "INVALID WORK ITEM SIZE"}, yading@11: {CL_INVALID_GLOBAL_OFFSET, "INVALID GLOBAL OFFSET"}, yading@11: {CL_INVALID_EVENT_WAIT_LIST, "INVALID EVENT WAIT LIST"}, yading@11: {CL_INVALID_EVENT, "INVALID EVENT"}, yading@11: {CL_INVALID_OPERATION, "INVALID OPERATION"}, yading@11: {CL_INVALID_GL_OBJECT, "INVALID GL OBJECT"}, yading@11: {CL_INVALID_BUFFER_SIZE, "INVALID BUFFER SIZE"}, yading@11: {CL_INVALID_MIP_LEVEL, "INVALID MIP LEVEL"}, yading@11: {CL_INVALID_GLOBAL_WORK_SIZE, "INVALID GLOBAL WORK SIZE"}, yading@11: {CL_INVALID_PROPERTY, "INVALID PROPERTY"}, yading@11: {CL_INVALID_IMAGE_DESCRIPTOR, "INVALID IMAGE DESCRIPTOR"}, yading@11: {CL_INVALID_COMPILER_OPTIONS, "INVALID COMPILER OPTIONS"}, yading@11: {CL_INVALID_LINKER_OPTIONS, "INVALID LINKER OPTIONS"}, yading@11: {CL_INVALID_DEVICE_PARTITION_COUNT, "INVALID DEVICE PARTITION COUNT"}, yading@11: }; yading@11: yading@11: static const char *opencl_errstr(cl_int status) yading@11: { yading@11: int i; yading@11: for (i = 0; i < sizeof(opencl_err_msg); i++) { yading@11: if (opencl_err_msg[i].err_code == status) yading@11: return opencl_err_msg[i].err_str; yading@11: } yading@11: return "unknown error"; yading@11: } yading@11: yading@11: static void free_device_list(AVOpenCLDeviceList *device_list) yading@11: { yading@11: int i, j; yading@11: if (!device_list) yading@11: return; yading@11: for (i = 0; i < device_list->platform_num; i++) { yading@11: if (!device_list->platform_node[i]) yading@11: continue; yading@11: for (j = 0; j < device_list->platform_node[i]->device_num; j++) { yading@11: av_freep(&(device_list->platform_node[i]->device_node[j])); yading@11: } yading@11: av_freep(&device_list->platform_node[i]->device_node); yading@11: av_freep(&device_list->platform_node[i]); yading@11: } yading@11: av_freep(&device_list->platform_node); yading@11: device_list->platform_num = 0; yading@11: } yading@11: yading@11: static int get_device_list(AVOpenCLDeviceList *device_list) yading@11: { yading@11: cl_int status; yading@11: int i, j, k, device_num, total_devices_num,ret = 0; yading@11: int *devices_num; yading@11: cl_platform_id *platform_ids = NULL; yading@11: cl_device_id *device_ids = NULL; yading@11: AVOpenCLDeviceNode *device_node = NULL; yading@11: status = clGetPlatformIDs(0, NULL, &device_list->platform_num); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not get OpenCL platform ids: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: platform_ids = av_mallocz(device_list->platform_num * sizeof(cl_platform_id)); yading@11: if (!platform_ids) yading@11: return AVERROR(ENOMEM); yading@11: status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not get OpenCL platform ids: %s\n", opencl_errstr(status)); yading@11: ret = AVERROR_EXTERNAL; yading@11: goto end; yading@11: } yading@11: device_list->platform_node = av_mallocz(device_list->platform_num * sizeof(AVOpenCLPlatformNode *)); yading@11: if (!device_list->platform_node) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type)); yading@11: if (!devices_num) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: for (i = 0; i < device_list->platform_num; i++) { yading@11: device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode)); yading@11: if (!device_list->platform_node[i]) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: device_list->platform_node[i]->platform_id = platform_ids[i]; yading@11: status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR, yading@11: sizeof(device_list->platform_node[i]->platform_name), yading@11: device_list->platform_node[i]->platform_name, NULL); yading@11: total_devices_num = 0; yading@11: for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) { yading@11: status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, yading@11: device_type[j], 0, NULL, &devices_num[j]); yading@11: total_devices_num += devices_num[j]; yading@11: } yading@11: device_list->platform_node[i]->device_node = av_mallocz(total_devices_num * sizeof(AVOpenCLDeviceNode *)); yading@11: if (!device_list->platform_node[i]->device_node) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) { yading@11: if (devices_num[j]) { yading@11: device_ids = av_mallocz(devices_num[j] * sizeof(cl_device_id)); yading@11: if (!device_ids) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j], yading@11: devices_num[j], device_ids, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_WARNING, yading@11: "Could not get device ID: %s:\n", opencl_errstr(status)); yading@11: av_freep(&device_ids); yading@11: continue; yading@11: } yading@11: for (k = 0; k < devices_num[j]; k++) { yading@11: device_num = device_list->platform_node[i]->device_num; yading@11: device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode)); yading@11: if (!device_list->platform_node[i]->device_node[device_num]) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto end; yading@11: } yading@11: device_node = device_list->platform_node[i]->device_node[device_num]; yading@11: device_node->device_id = device_ids[k]; yading@11: device_node->device_type = device_type[j]; yading@11: status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME, yading@11: sizeof(device_node->device_name), device_node->device_name, yading@11: NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_WARNING, yading@11: "Could not get device name: %s\n", opencl_errstr(status)); yading@11: continue; yading@11: } yading@11: device_list->platform_node[i]->device_num++; yading@11: } yading@11: av_freep(&device_ids); yading@11: } yading@11: } yading@11: } yading@11: end: yading@11: av_freep(&platform_ids); yading@11: av_freep(&devices_num); yading@11: av_freep(&device_ids); yading@11: if (ret < 0) yading@11: free_device_list(device_list); yading@11: return ret; yading@11: } yading@11: yading@11: int av_opencl_get_device_list(AVOpenCLDeviceList **device_list) yading@11: { yading@11: int ret = 0; yading@11: *device_list = av_mallocz(sizeof(AVOpenCLDeviceList)); yading@11: if (!(*device_list)) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n"); yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: ret = get_device_list(*device_list); yading@11: if (ret < 0) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n"); yading@11: free_device_list(*device_list); yading@11: av_freep(device_list); yading@11: return ret; yading@11: } yading@11: return ret; yading@11: } yading@11: yading@11: void av_opencl_free_device_list(AVOpenCLDeviceList **device_list) yading@11: { yading@11: free_device_list(*device_list); yading@11: av_freep(device_list); yading@11: } yading@11: yading@11: int av_opencl_set_option(const char *key, const char *val) yading@11: { yading@11: int ret = 0; yading@11: LOCK_OPENCL yading@11: if (!opencl_ctx.opt_init_flag) { yading@11: av_opt_set_defaults(&opencl_ctx); yading@11: opencl_ctx.opt_init_flag = 1; yading@11: } yading@11: ret = av_opt_set(&opencl_ctx, key, val, 0); yading@11: UNLOCK_OPENCL yading@11: return ret; yading@11: } yading@11: yading@11: int av_opencl_get_option(const char *key, uint8_t **out_val) yading@11: { yading@11: int ret = 0; yading@11: LOCK_OPENCL yading@11: ret = av_opt_get(&opencl_ctx, key, 0, out_val); yading@11: UNLOCK_OPENCL yading@11: return ret; yading@11: } yading@11: yading@11: void av_opencl_free_option(void) yading@11: { yading@11: /*FIXME: free openclutils context*/ yading@11: LOCK_OPENCL yading@11: av_opt_free(&opencl_ctx); yading@11: UNLOCK_OPENCL yading@11: } yading@11: yading@11: AVOpenCLExternalEnv *av_opencl_alloc_external_env(void) yading@11: { yading@11: AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv)); yading@11: if (!ext) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not malloc external opencl environment data space\n"); yading@11: } yading@11: return ext; yading@11: } yading@11: yading@11: void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env) yading@11: { yading@11: av_freep(ext_opencl_env); yading@11: } yading@11: yading@11: int av_opencl_register_kernel_code(const char *kernel_code) yading@11: { yading@11: int i, ret = 0; yading@11: LOCK_OPENCL; yading@11: if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not register kernel code, maximum number of registered kernel code %d already reached\n", yading@11: MAX_KERNEL_CODE_NUM); yading@11: ret = AVERROR(EINVAL); yading@11: goto end; yading@11: } yading@11: for (i = 0; i < opencl_ctx.kernel_code_count; i++) { yading@11: if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) { yading@11: av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n"); yading@11: goto end; yading@11: } yading@11: } yading@11: opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code; yading@11: opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0; yading@11: opencl_ctx.kernel_code_count++; yading@11: end: yading@11: UNLOCK_OPENCL; yading@11: return ret; yading@11: } yading@11: yading@11: int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name) yading@11: { yading@11: cl_int status; yading@11: int i, ret = 0; yading@11: LOCK_OPENCL; yading@11: if (strlen(kernel_name) + 1 > AV_OPENCL_MAX_KERNEL_NAME_SIZE) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Created kernel name %s is too long\n", kernel_name); yading@11: ret = AVERROR(EINVAL); yading@11: goto end; yading@11: } yading@11: if (!env->kernel) { yading@11: if (opencl_ctx.kernel_count >= MAX_KERNEL_NUM) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not create kernel with name '%s', maximum number of kernels %d already reached\n", yading@11: kernel_name, MAX_KERNEL_NUM); yading@11: ret = AVERROR(EINVAL); yading@11: goto end; yading@11: } yading@11: if (opencl_ctx.program_count == 0) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Program count of OpenCL is 0, can not create kernel\n"); yading@11: ret = AVERROR(EINVAL); yading@11: goto end; yading@11: } yading@11: for (i = 0; i < opencl_ctx.program_count; i++) { yading@11: env->kernel = clCreateKernel(opencl_ctx.programs[i], kernel_name, &status); yading@11: if (status == CL_SUCCESS) yading@11: break; yading@11: } yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL kernel: %s\n", opencl_errstr(status)); yading@11: ret = AVERROR_EXTERNAL; yading@11: goto end; yading@11: } yading@11: opencl_ctx.kernel_count++; yading@11: env->command_queue = opencl_ctx.command_queue; yading@11: av_strlcpy(env->kernel_name, kernel_name, sizeof(env->kernel_name)); yading@11: } yading@11: end: yading@11: UNLOCK_OPENCL; yading@11: return ret; yading@11: } yading@11: yading@11: void av_opencl_release_kernel(AVOpenCLKernelEnv *env) yading@11: { yading@11: cl_int status; yading@11: LOCK_OPENCL yading@11: if (!env->kernel) yading@11: goto end; yading@11: status = clReleaseKernel(env->kernel); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Could not release kernel: %s\n", yading@11: opencl_errstr(status)); yading@11: } yading@11: env->kernel = NULL; yading@11: env->command_queue = NULL; yading@11: env->kernel_name[0] = 0; yading@11: opencl_ctx.kernel_count--; yading@11: end: yading@11: UNLOCK_OPENCL yading@11: } yading@11: yading@11: static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env) yading@11: { yading@11: cl_int status; yading@11: cl_context_properties cps[3]; yading@11: int i, ret = 0; yading@11: AVOpenCLDeviceNode *device_node = NULL; yading@11: yading@11: if (ext_opencl_env) { yading@11: if (opencl_ctx->is_user_created) yading@11: return 0; yading@11: opencl_ctx->platform_id = ext_opencl_env->platform_id; yading@11: opencl_ctx->is_user_created = 1; yading@11: opencl_ctx->command_queue = ext_opencl_env->command_queue; yading@11: opencl_ctx->context = ext_opencl_env->context; yading@11: opencl_ctx->device_id = ext_opencl_env->device_id; yading@11: opencl_ctx->device_type = ext_opencl_env->device_type; yading@11: } else { yading@11: if (!opencl_ctx->is_user_created) { yading@11: if (!opencl_ctx->device_list.platform_num) { yading@11: ret = get_device_list(&opencl_ctx->device_list); yading@11: if (ret < 0) { yading@11: return ret; yading@11: } yading@11: } yading@11: if (opencl_ctx->platform_idx >= 0) { yading@11: if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n", yading@11: opencl_ctx->platform_idx); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id; yading@11: } else { yading@11: /* get a usable platform by default*/ yading@11: for (i = 0; i < opencl_ctx->device_list.platform_num; i++) { yading@11: if (opencl_ctx->device_list.platform_node[i]->device_num) { yading@11: opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id; yading@11: opencl_ctx->platform_idx = i; yading@11: break; yading@11: } yading@11: } yading@11: } yading@11: if (!opencl_ctx->platform_id) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n"); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: /* get a usable device*/ yading@11: if (opencl_ctx->device_idx >= 0) { yading@11: if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, yading@11: "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: } else { yading@11: opencl_ctx->device_idx = 0; yading@11: } yading@11: yading@11: device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx]; yading@11: opencl_ctx->device_id = device_node->device_id; yading@11: opencl_ctx->device_type = device_node->device_type; yading@11: yading@11: /* yading@11: * Use available platform. yading@11: */ yading@11: av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, device id: 0x%x\n", yading@11: opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name, yading@11: (unsigned int)opencl_ctx->device_id); yading@11: cps[0] = CL_CONTEXT_PLATFORM; yading@11: cps[1] = (cl_context_properties)opencl_ctx->platform_id; yading@11: cps[2] = 0; yading@11: yading@11: opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type, yading@11: NULL, NULL, &status); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, yading@11: "Could not get OpenCL context from device type: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id, yading@11: 0, &status); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, yading@11: "Could not create OpenCL command queue: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: } yading@11: } yading@11: return ret; yading@11: } yading@11: yading@11: static int compile_kernel_file(OpenclContext *opencl_ctx) yading@11: { yading@11: cl_int status; yading@11: char *temp, *source_str = NULL; yading@11: size_t source_str_len = 0; yading@11: int i, ret = 0; yading@11: yading@11: for (i = 0; i < opencl_ctx->kernel_code_count; i++) { yading@11: if (!opencl_ctx->kernel_code[i].is_compiled) yading@11: source_str_len += strlen(opencl_ctx->kernel_code[i].kernel_string); yading@11: } yading@11: if (!source_str_len) { yading@11: return 0; yading@11: } yading@11: source_str = av_mallocz(source_str_len + 1); yading@11: if (!source_str) { yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: temp = source_str; yading@11: for (i = 0; i < opencl_ctx->kernel_code_count; i++) { yading@11: if (!opencl_ctx->kernel_code[i].is_compiled) { yading@11: memcpy(temp, opencl_ctx->kernel_code[i].kernel_string, yading@11: strlen(opencl_ctx->kernel_code[i].kernel_string)); yading@11: opencl_ctx->kernel_code[i].is_compiled = 1; yading@11: temp += strlen(opencl_ctx->kernel_code[i].kernel_string); yading@11: } yading@11: } yading@11: /* create a CL program using the kernel source */ yading@11: opencl_ctx->programs[opencl_ctx->program_count] = clCreateProgramWithSource(opencl_ctx->context, yading@11: 1, (const char **)(&source_str), yading@11: &source_str_len, &status); yading@11: if(status != CL_SUCCESS) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, yading@11: "Could not create OpenCL program with source code: %s\n", opencl_errstr(status)); yading@11: ret = AVERROR_EXTERNAL; yading@11: goto end; yading@11: } yading@11: if (!opencl_ctx->programs[opencl_ctx->program_count]) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, "Created program is NULL\n"); yading@11: ret = AVERROR_EXTERNAL; yading@11: goto end; yading@11: } yading@11: status = clBuildProgram(opencl_ctx->programs[opencl_ctx->program_count], 1, &(opencl_ctx->device_id), yading@11: opencl_ctx->build_options, NULL, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(opencl_ctx, AV_LOG_ERROR, yading@11: "Could not compile OpenCL kernel: %s\n", opencl_errstr(status)); yading@11: ret = AVERROR_EXTERNAL; yading@11: goto end; yading@11: } yading@11: opencl_ctx->program_count++; yading@11: end: yading@11: av_free(source_str); yading@11: return ret; yading@11: } yading@11: yading@11: int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env) yading@11: { yading@11: int ret = 0; yading@11: LOCK_OPENCL yading@11: if (!opencl_ctx.init_count) { yading@11: if (!opencl_ctx.opt_init_flag) { yading@11: av_opt_set_defaults(&opencl_ctx); yading@11: opencl_ctx.opt_init_flag = 1; yading@11: } yading@11: ret = init_opencl_env(&opencl_ctx, ext_opencl_env); yading@11: if (ret < 0) yading@11: goto end; yading@11: } yading@11: ret = compile_kernel_file(&opencl_ctx); yading@11: if (ret < 0) yading@11: goto end; yading@11: if (opencl_ctx.kernel_code_count <= 0) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "No kernel code is registered, compile kernel file failed\n"); yading@11: ret = AVERROR(EINVAL); yading@11: goto end; yading@11: } yading@11: opencl_ctx.init_count++; yading@11: yading@11: end: yading@11: UNLOCK_OPENCL yading@11: return ret; yading@11: } yading@11: yading@11: void av_opencl_uninit(void) yading@11: { yading@11: cl_int status; yading@11: int i; yading@11: LOCK_OPENCL yading@11: opencl_ctx.init_count--; yading@11: if (opencl_ctx.is_user_created) yading@11: goto end; yading@11: if (opencl_ctx.init_count > 0 || opencl_ctx.kernel_count > 0) yading@11: goto end; yading@11: for (i = 0; i < opencl_ctx.program_count; i++) { yading@11: if (opencl_ctx.programs[i]) { yading@11: status = clReleaseProgram(opencl_ctx.programs[i]); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not release OpenCL program: %s\n", opencl_errstr(status)); yading@11: } yading@11: opencl_ctx.programs[i] = NULL; yading@11: } yading@11: } yading@11: if (opencl_ctx.command_queue) { yading@11: status = clReleaseCommandQueue(opencl_ctx.command_queue); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not release OpenCL command queue: %s\n", opencl_errstr(status)); yading@11: } yading@11: opencl_ctx.command_queue = NULL; yading@11: } yading@11: if (opencl_ctx.context) { yading@11: status = clReleaseContext(opencl_ctx.context); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not release OpenCL context: %s\n", opencl_errstr(status)); yading@11: } yading@11: opencl_ctx.context = NULL; yading@11: } yading@11: free_device_list(&opencl_ctx.device_list); yading@11: end: yading@11: if ((opencl_ctx.init_count <= 0) && (opencl_ctx.kernel_count <= 0)) yading@11: av_opt_free(&opencl_ctx); //FIXME: free openclutils context yading@11: UNLOCK_OPENCL yading@11: } yading@11: yading@11: int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr) yading@11: { yading@11: cl_int status; yading@11: *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: void av_opencl_buffer_release(cl_mem *cl_buf) yading@11: { yading@11: cl_int status = 0; yading@11: if (!cl_buf) yading@11: return; yading@11: status = clReleaseMemObject(*cl_buf); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not release OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: } yading@11: memset(cl_buf, 0, sizeof(*cl_buf)); yading@11: } yading@11: yading@11: int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size) yading@11: { yading@11: cl_int status; yading@11: void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf, yading@11: CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size, yading@11: 0, NULL, NULL, &status); yading@11: yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: memcpy(mapped, src_buf, buf_size); yading@11: yading@11: status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size) yading@11: { yading@11: cl_int status; yading@11: void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf, yading@11: CL_TRUE, CL_MAP_READ, 0, buf_size, yading@11: 0, NULL, NULL, &status); yading@11: yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: memcpy(dst_buf, mapped, buf_size); yading@11: yading@11: status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset, yading@11: uint8_t **src_data, int *plane_size, int plane_num) yading@11: { yading@11: int i, buffer_size = 0; yading@11: uint8_t *temp; yading@11: cl_int status; yading@11: void *mapped; yading@11: if ((unsigned int)plane_num > 8) { yading@11: return AVERROR(EINVAL); yading@11: } yading@11: for (i = 0;i < plane_num;i++) { yading@11: buffer_size += plane_size[i]; yading@11: } yading@11: if (buffer_size > cl_buffer_size) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Cannot write image to OpenCL buffer: buffer too small\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf, yading@11: CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset, yading@11: 0, NULL, NULL, &status); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: temp = mapped; yading@11: temp += dst_cl_offset; yading@11: for (i = 0; i < plane_num; i++) { yading@11: memcpy(temp, src_data[i], plane_size[i]); yading@11: temp += plane_size[i]; yading@11: } yading@11: status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num, yading@11: cl_mem src_cl_buf, size_t cl_buffer_size) yading@11: { yading@11: int i,buffer_size = 0,ret = 0; yading@11: uint8_t *temp; yading@11: void *mapped; yading@11: cl_int status; yading@11: if ((unsigned int)plane_num > 8) { yading@11: return AVERROR(EINVAL); yading@11: } yading@11: for (i = 0; i < plane_num; i++) { yading@11: buffer_size += plane_size[i]; yading@11: } yading@11: if (buffer_size > cl_buffer_size) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Cannot write image to CPU buffer: OpenCL buffer too small\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf, yading@11: CL_TRUE, CL_MAP_READ, 0, buffer_size, yading@11: 0, NULL, NULL, &status); yading@11: yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not map OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: temp = mapped; yading@11: if (ret >= 0) { yading@11: for (i = 0; i < plane_num; i++) { yading@11: memcpy(dst_data[i], temp, plane_size[i]); yading@11: temp += plane_size[i]; yading@11: } yading@11: } yading@11: status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL); yading@11: if (status != CL_SUCCESS) { yading@11: av_log(&opencl_ctx, AV_LOG_ERROR, yading@11: "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status)); yading@11: return AVERROR_EXTERNAL; yading@11: } yading@11: return 0; yading@11: }